Update concepts branch to revision 131834
[official-gcc.git] / gcc / c-decl.c
blob89fdf3d1b45cdaf342a161021db9132677af6e51
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_CONTEXT (newdecl) == NULL_TREE
1768 || TREE_CODE (DECL_CONTEXT (newdecl)) != FUNCTION_DECL))
1769 DECL_EXTERNAL (newdecl) = 0;
1771 if (DECL_EXTERNAL (newdecl))
1773 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1774 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1776 /* An extern decl does not override previous storage class. */
1777 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1778 if (!DECL_EXTERNAL (newdecl))
1780 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1781 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1784 else
1786 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1787 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1790 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1792 /* If we're redefining a function previously defined as extern
1793 inline, make sure we emit debug info for the inline before we
1794 throw it away, in case it was inlined into a function that
1795 hasn't been written out yet. */
1796 if (new_is_definition && DECL_INITIAL (olddecl))
1798 if (TREE_USED (olddecl)
1799 /* In unit-at-a-time mode we never inline re-defined extern
1800 inline functions. */
1801 && !flag_unit_at_a_time
1802 && cgraph_function_possibly_inlined_p (olddecl))
1803 (*debug_hooks->outlining_inline_function) (olddecl);
1805 /* The new defn must not be inline. */
1806 DECL_INLINE (newdecl) = 0;
1807 DECL_UNINLINABLE (newdecl) = 1;
1809 else
1811 /* If either decl says `inline', this fn is inline, unless
1812 its definition was passed already. */
1813 if (DECL_DECLARED_INLINE_P (newdecl)
1814 || DECL_DECLARED_INLINE_P (olddecl))
1815 DECL_DECLARED_INLINE_P (newdecl) = 1;
1817 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1818 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1820 DECL_DISREGARD_INLINE_LIMITS (newdecl)
1821 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1822 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1823 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
1826 if (DECL_BUILT_IN (olddecl))
1828 /* If redeclaring a builtin function, it stays built in.
1829 But it gets tagged as having been declared. */
1830 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1831 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1832 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1833 if (new_is_prototype)
1834 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1835 else
1836 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1837 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1840 /* Also preserve various other info from the definition. */
1841 if (!new_is_definition)
1843 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1844 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1845 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1846 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1847 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1849 /* Set DECL_INLINE on the declaration if we've got a body
1850 from which to instantiate. */
1851 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1853 DECL_INLINE (newdecl) = 1;
1854 DECL_ABSTRACT_ORIGIN (newdecl)
1855 = DECL_ABSTRACT_ORIGIN (olddecl);
1858 else
1860 /* If a previous declaration said inline, mark the
1861 definition as inlinable. */
1862 if (DECL_DECLARED_INLINE_P (newdecl)
1863 && !DECL_UNINLINABLE (newdecl))
1864 DECL_INLINE (newdecl) = 1;
1868 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1870 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1871 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1873 unsigned olddecl_uid = DECL_UID (olddecl);
1874 tree olddecl_context = DECL_CONTEXT (olddecl);
1876 memcpy ((char *) olddecl + sizeof (struct tree_common),
1877 (char *) newdecl + sizeof (struct tree_common),
1878 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1879 switch (TREE_CODE (olddecl))
1881 case FIELD_DECL:
1882 case VAR_DECL:
1883 case PARM_DECL:
1884 case LABEL_DECL:
1885 case RESULT_DECL:
1886 case CONST_DECL:
1887 case TYPE_DECL:
1888 case FUNCTION_DECL:
1889 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1890 (char *) newdecl + sizeof (struct tree_decl_common),
1891 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1892 break;
1894 default:
1896 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1897 (char *) newdecl + sizeof (struct tree_decl_common),
1898 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1900 DECL_UID (olddecl) = olddecl_uid;
1901 DECL_CONTEXT (olddecl) = olddecl_context;
1904 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1905 so that encode_section_info has a chance to look at the new decl
1906 flags and attributes. */
1907 if (DECL_RTL_SET_P (olddecl)
1908 && (TREE_CODE (olddecl) == FUNCTION_DECL
1909 || (TREE_CODE (olddecl) == VAR_DECL
1910 && TREE_STATIC (olddecl))))
1911 make_decl_rtl (olddecl);
1913 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1914 and the definition is coming from the old version, cgraph needs
1915 to be called again. */
1916 if (extern_changed && !new_is_definition
1917 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
1918 cgraph_finalize_function (olddecl, false);
1921 /* Handle when a new declaration NEWDECL has the same name as an old
1922 one OLDDECL in the same binding contour. Prints an error message
1923 if appropriate.
1925 If safely possible, alter OLDDECL to look like NEWDECL, and return
1926 true. Otherwise, return false. */
1928 static bool
1929 duplicate_decls (tree newdecl, tree olddecl)
1931 tree newtype = NULL, oldtype = NULL;
1933 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1935 /* Avoid `unused variable' and other warnings for OLDDECL. */
1936 TREE_NO_WARNING (olddecl) = 1;
1937 return false;
1940 merge_decls (newdecl, olddecl, newtype, oldtype);
1941 return true;
1945 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1946 static void
1947 warn_if_shadowing (tree new_decl)
1949 struct c_binding *b;
1951 /* Shadow warnings wanted? */
1952 if (!warn_shadow
1953 /* No shadow warnings for internally generated vars. */
1954 || DECL_IS_BUILTIN (new_decl)
1955 /* No shadow warnings for vars made for inlining. */
1956 || DECL_FROM_INLINE (new_decl))
1957 return;
1959 /* Is anything being shadowed? Invisible decls do not count. */
1960 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1961 if (b->decl && b->decl != new_decl && !b->invisible)
1963 tree old_decl = b->decl;
1965 if (old_decl == error_mark_node)
1967 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1968 "non-variable", new_decl);
1969 break;
1971 else if (TREE_CODE (old_decl) == PARM_DECL)
1972 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
1973 new_decl);
1974 else if (DECL_FILE_SCOPE_P (old_decl))
1975 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
1976 "declaration", new_decl);
1977 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1978 && DECL_BUILT_IN (old_decl))
1980 warning (OPT_Wshadow, "declaration of %q+D shadows "
1981 "a built-in function", new_decl);
1982 break;
1984 else
1985 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
1986 new_decl);
1988 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
1990 break;
1995 /* Subroutine of pushdecl.
1997 X is a TYPE_DECL for a typedef statement. Create a brand new
1998 ..._TYPE node (which will be just a variant of the existing
1999 ..._TYPE node with identical properties) and then install X
2000 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
2002 The whole point here is to end up with a situation where each
2003 and every ..._TYPE node the compiler creates will be uniquely
2004 associated with AT MOST one node representing a typedef name.
2005 This way, even though the compiler substitutes corresponding
2006 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2007 early on, later parts of the compiler can always do the reverse
2008 translation and get back the corresponding typedef name. For
2009 example, given:
2011 typedef struct S MY_TYPE;
2012 MY_TYPE object;
2014 Later parts of the compiler might only know that `object' was of
2015 type `struct S' if it were not for code just below. With this
2016 code however, later parts of the compiler see something like:
2018 struct S' == struct S
2019 typedef struct S' MY_TYPE;
2020 struct S' object;
2022 And they can then deduce (from the node for type struct S') that
2023 the original object declaration was:
2025 MY_TYPE object;
2027 Being able to do this is important for proper support of protoize,
2028 and also for generating precise symbolic debugging information
2029 which takes full account of the programmer's (typedef) vocabulary.
2031 Obviously, we don't want to generate a duplicate ..._TYPE node if
2032 the TYPE_DECL node that we are now processing really represents a
2033 standard built-in type. */
2035 static void
2036 clone_underlying_type (tree x)
2038 if (DECL_IS_BUILTIN (x))
2040 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2041 TYPE_NAME (TREE_TYPE (x)) = x;
2043 else if (TREE_TYPE (x) != error_mark_node
2044 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2046 tree tt = TREE_TYPE (x);
2047 DECL_ORIGINAL_TYPE (x) = tt;
2048 tt = build_variant_type_copy (tt);
2049 TYPE_NAME (tt) = x;
2050 TREE_USED (tt) = TREE_USED (x);
2051 TREE_TYPE (x) = tt;
2055 /* Record a decl-node X as belonging to the current lexical scope.
2056 Check for errors (such as an incompatible declaration for the same
2057 name already seen in the same scope).
2059 Returns either X or an old decl for the same name.
2060 If an old decl is returned, it may have been smashed
2061 to agree with what X says. */
2063 tree
2064 pushdecl (tree x)
2066 tree name = DECL_NAME (x);
2067 struct c_scope *scope = current_scope;
2068 struct c_binding *b;
2069 bool nested = false;
2071 /* Must set DECL_CONTEXT for everything not at file scope or
2072 DECL_FILE_SCOPE_P won't work. Local externs don't count
2073 unless they have initializers (which generate code). */
2074 if (current_function_decl
2075 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2076 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2077 DECL_CONTEXT (x) = current_function_decl;
2079 /* If this is of variably modified type, prevent jumping into its
2080 scope. */
2081 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2082 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2083 c_begin_vm_scope (scope->depth);
2085 /* Anonymous decls are just inserted in the scope. */
2086 if (!name)
2088 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2089 return x;
2092 /* First, see if there is another declaration with the same name in
2093 the current scope. If there is, duplicate_decls may do all the
2094 work for us. If duplicate_decls returns false, that indicates
2095 two incompatible decls in the same scope; we are to silently
2096 replace the old one (duplicate_decls has issued all appropriate
2097 diagnostics). In particular, we should not consider possible
2098 duplicates in the external scope, or shadowing. */
2099 b = I_SYMBOL_BINDING (name);
2100 if (b && B_IN_SCOPE (b, scope))
2102 struct c_binding *b_ext, *b_use;
2103 tree type = TREE_TYPE (x);
2104 tree visdecl = b->decl;
2105 tree vistype = TREE_TYPE (visdecl);
2106 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2107 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2108 b->inner_comp = false;
2109 b_use = b;
2110 b_ext = b;
2111 /* If this is an external linkage declaration, we should check
2112 for compatibility with the type in the external scope before
2113 setting the type at this scope based on the visible
2114 information only. */
2115 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2117 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2118 b_ext = b_ext->shadowed;
2119 if (b_ext)
2121 b_use = b_ext;
2122 if (b_use->type)
2123 TREE_TYPE (b_use->decl) = b_use->type;
2126 if (duplicate_decls (x, b_use->decl))
2128 if (b_use != b)
2130 /* Save the updated type in the external scope and
2131 restore the proper type for this scope. */
2132 tree thistype;
2133 if (comptypes (vistype, type))
2134 thistype = composite_type (vistype, type);
2135 else
2136 thistype = TREE_TYPE (b_use->decl);
2137 b_use->type = TREE_TYPE (b_use->decl);
2138 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2139 && DECL_BUILT_IN (b_use->decl))
2140 thistype
2141 = build_type_attribute_variant (thistype,
2142 TYPE_ATTRIBUTES
2143 (b_use->type));
2144 TREE_TYPE (b_use->decl) = thistype;
2146 return b_use->decl;
2148 else
2149 goto skip_external_and_shadow_checks;
2152 /* All declarations with external linkage, and all external
2153 references, go in the external scope, no matter what scope is
2154 current. However, the binding in that scope is ignored for
2155 purposes of normal name lookup. A separate binding structure is
2156 created in the requested scope; this governs the normal
2157 visibility of the symbol.
2159 The binding in the externals scope is used exclusively for
2160 detecting duplicate declarations of the same object, no matter
2161 what scope they are in; this is what we do here. (C99 6.2.7p2:
2162 All declarations that refer to the same object or function shall
2163 have compatible type; otherwise, the behavior is undefined.) */
2164 if (DECL_EXTERNAL (x) || scope == file_scope)
2166 tree type = TREE_TYPE (x);
2167 tree vistype = 0;
2168 tree visdecl = 0;
2169 bool type_saved = false;
2170 if (b && !B_IN_EXTERNAL_SCOPE (b)
2171 && (TREE_CODE (b->decl) == FUNCTION_DECL
2172 || TREE_CODE (b->decl) == VAR_DECL)
2173 && DECL_FILE_SCOPE_P (b->decl))
2175 visdecl = b->decl;
2176 vistype = TREE_TYPE (visdecl);
2178 if (scope != file_scope
2179 && !DECL_IN_SYSTEM_HEADER (x))
2180 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2182 while (b && !B_IN_EXTERNAL_SCOPE (b))
2184 /* If this decl might be modified, save its type. This is
2185 done here rather than when the decl is first bound
2186 because the type may change after first binding, through
2187 being completed or through attributes being added. If we
2188 encounter multiple such decls, only the first should have
2189 its type saved; the others will already have had their
2190 proper types saved and the types will not have changed as
2191 their scopes will not have been re-entered. */
2192 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2194 b->type = TREE_TYPE (b->decl);
2195 type_saved = true;
2197 if (B_IN_FILE_SCOPE (b)
2198 && TREE_CODE (b->decl) == VAR_DECL
2199 && TREE_STATIC (b->decl)
2200 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2201 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2202 && TREE_CODE (type) == ARRAY_TYPE
2203 && TYPE_DOMAIN (type)
2204 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2205 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2207 /* Array type completed in inner scope, which should be
2208 diagnosed if the completion does not have size 1 and
2209 it does not get completed in the file scope. */
2210 b->inner_comp = true;
2212 b = b->shadowed;
2215 /* If a matching external declaration has been found, set its
2216 type to the composite of all the types of that declaration.
2217 After the consistency checks, it will be reset to the
2218 composite of the visible types only. */
2219 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2220 && b->type)
2221 TREE_TYPE (b->decl) = b->type;
2223 /* The point of the same_translation_unit_p check here is,
2224 we want to detect a duplicate decl for a construct like
2225 foo() { extern bar(); } ... static bar(); but not if
2226 they are in different translation units. In any case,
2227 the static does not go in the externals scope. */
2228 if (b
2229 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2230 && duplicate_decls (x, b->decl))
2232 tree thistype;
2233 if (vistype)
2235 if (comptypes (vistype, type))
2236 thistype = composite_type (vistype, type);
2237 else
2238 thistype = TREE_TYPE (b->decl);
2240 else
2241 thistype = type;
2242 b->type = TREE_TYPE (b->decl);
2243 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2244 thistype
2245 = build_type_attribute_variant (thistype,
2246 TYPE_ATTRIBUTES (b->type));
2247 TREE_TYPE (b->decl) = thistype;
2248 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2249 return b->decl;
2251 else if (TREE_PUBLIC (x))
2253 if (visdecl && !b && duplicate_decls (x, visdecl))
2255 /* An external declaration at block scope referring to a
2256 visible entity with internal linkage. The composite
2257 type will already be correct for this scope, so we
2258 just need to fall through to make the declaration in
2259 this scope. */
2260 nested = true;
2261 x = visdecl;
2263 else
2265 bind (name, x, external_scope, /*invisible=*/true,
2266 /*nested=*/false);
2267 nested = true;
2272 if (TREE_CODE (x) != PARM_DECL)
2273 warn_if_shadowing (x);
2275 skip_external_and_shadow_checks:
2276 if (TREE_CODE (x) == TYPE_DECL)
2277 clone_underlying_type (x);
2279 bind (name, x, scope, /*invisible=*/false, nested);
2281 /* If x's type is incomplete because it's based on a
2282 structure or union which has not yet been fully declared,
2283 attach it to that structure or union type, so we can go
2284 back and complete the variable declaration later, if the
2285 structure or union gets fully declared.
2287 If the input is erroneous, we can have error_mark in the type
2288 slot (e.g. "f(void a, ...)") - that doesn't count as an
2289 incomplete type. */
2290 if (TREE_TYPE (x) != error_mark_node
2291 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2293 tree element = TREE_TYPE (x);
2295 while (TREE_CODE (element) == ARRAY_TYPE)
2296 element = TREE_TYPE (element);
2297 element = TYPE_MAIN_VARIANT (element);
2299 if ((TREE_CODE (element) == RECORD_TYPE
2300 || TREE_CODE (element) == UNION_TYPE)
2301 && (TREE_CODE (x) != TYPE_DECL
2302 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2303 && !COMPLETE_TYPE_P (element))
2304 C_TYPE_INCOMPLETE_VARS (element)
2305 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2307 return x;
2310 /* Record X as belonging to file scope.
2311 This is used only internally by the Objective-C front end,
2312 and is limited to its needs. duplicate_decls is not called;
2313 if there is any preexisting decl for this identifier, it is an ICE. */
2315 tree
2316 pushdecl_top_level (tree x)
2318 tree name;
2319 bool nested = false;
2320 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2322 name = DECL_NAME (x);
2324 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2326 if (TREE_PUBLIC (x))
2328 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2329 nested = true;
2331 if (file_scope)
2332 bind (name, x, file_scope, /*invisible=*/false, nested);
2334 return x;
2337 static void
2338 implicit_decl_warning (tree id, tree olddecl)
2340 if (warn_implicit_function_declaration)
2342 if (flag_isoc99)
2343 pedwarn (G_("implicit declaration of function %qE"), id);
2344 else
2345 warning (OPT_Wimplicit_function_declaration,
2346 G_("implicit declaration of function %qE"), id);
2347 if (olddecl)
2348 locate_old_decl (olddecl, inform);
2352 /* Generate an implicit declaration for identifier FUNCTIONID as a
2353 function of type int (). */
2355 tree
2356 implicitly_declare (tree functionid)
2358 struct c_binding *b;
2359 tree decl = 0;
2360 tree asmspec_tree;
2362 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2364 if (B_IN_SCOPE (b, external_scope))
2366 decl = b->decl;
2367 break;
2371 if (decl)
2373 if (decl == error_mark_node)
2374 return decl;
2376 /* FIXME: Objective-C has weird not-really-builtin functions
2377 which are supposed to be visible automatically. They wind up
2378 in the external scope because they're pushed before the file
2379 scope gets created. Catch this here and rebind them into the
2380 file scope. */
2381 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2383 bind (functionid, decl, file_scope,
2384 /*invisible=*/false, /*nested=*/true);
2385 return decl;
2387 else
2389 tree newtype = default_function_type;
2390 if (b->type)
2391 TREE_TYPE (decl) = b->type;
2392 /* Implicit declaration of a function already declared
2393 (somehow) in a different scope, or as a built-in.
2394 If this is the first time this has happened, warn;
2395 then recycle the old declaration but with the new type. */
2396 if (!C_DECL_IMPLICIT (decl))
2398 implicit_decl_warning (functionid, decl);
2399 C_DECL_IMPLICIT (decl) = 1;
2401 if (DECL_BUILT_IN (decl))
2403 newtype = build_type_attribute_variant (newtype,
2404 TYPE_ATTRIBUTES
2405 (TREE_TYPE (decl)));
2406 if (!comptypes (newtype, TREE_TYPE (decl)))
2408 warning (0, "incompatible implicit declaration of built-in"
2409 " function %qD", decl);
2410 newtype = TREE_TYPE (decl);
2413 else
2415 if (!comptypes (newtype, TREE_TYPE (decl)))
2417 error ("incompatible implicit declaration of function %qD",
2418 decl);
2419 locate_old_decl (decl, error);
2422 b->type = TREE_TYPE (decl);
2423 TREE_TYPE (decl) = newtype;
2424 bind (functionid, decl, current_scope,
2425 /*invisible=*/false, /*nested=*/true);
2426 return decl;
2430 /* Not seen before. */
2431 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2432 DECL_EXTERNAL (decl) = 1;
2433 TREE_PUBLIC (decl) = 1;
2434 C_DECL_IMPLICIT (decl) = 1;
2435 implicit_decl_warning (functionid, 0);
2436 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2437 if (asmspec_tree)
2438 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2440 /* C89 says implicit declarations are in the innermost block.
2441 So we record the decl in the standard fashion. */
2442 decl = pushdecl (decl);
2444 /* No need to call objc_check_decl here - it's a function type. */
2445 rest_of_decl_compilation (decl, 0, 0);
2447 /* Write a record describing this implicit function declaration
2448 to the prototypes file (if requested). */
2449 gen_aux_info_record (decl, 0, 1, 0);
2451 /* Possibly apply some default attributes to this implicit declaration. */
2452 decl_attributes (&decl, NULL_TREE, 0);
2454 return decl;
2457 /* Issue an error message for a reference to an undeclared variable
2458 ID, including a reference to a builtin outside of function-call
2459 context. Establish a binding of the identifier to error_mark_node
2460 in an appropriate scope, which will suppress further errors for the
2461 same identifier. The error message should be given location LOC. */
2462 void
2463 undeclared_variable (tree id, location_t loc)
2465 static bool already = false;
2466 struct c_scope *scope;
2468 if (current_function_decl == 0)
2470 error ("%H%qE undeclared here (not in a function)", &loc, id);
2471 scope = current_scope;
2473 else
2475 error ("%H%qE undeclared (first use in this function)", &loc, id);
2477 if (!already)
2479 error ("%H(Each undeclared identifier is reported only once", &loc);
2480 error ("%Hfor each function it appears in.)", &loc);
2481 already = true;
2484 /* If we are parsing old-style parameter decls, current_function_decl
2485 will be nonnull but current_function_scope will be null. */
2486 scope = current_function_scope ? current_function_scope : current_scope;
2488 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2491 /* Subroutine of lookup_label, declare_label, define_label: construct a
2492 LABEL_DECL with all the proper frills. */
2494 static tree
2495 make_label (tree name, location_t location)
2497 tree label = build_decl (LABEL_DECL, name, void_type_node);
2499 DECL_CONTEXT (label) = current_function_decl;
2500 DECL_MODE (label) = VOIDmode;
2501 DECL_SOURCE_LOCATION (label) = location;
2503 return label;
2506 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2507 Create one if none exists so far for the current function.
2508 This is called when a label is used in a goto expression or
2509 has its address taken. */
2511 tree
2512 lookup_label (tree name)
2514 tree label;
2516 if (current_function_decl == 0)
2518 error ("label %qE referenced outside of any function", name);
2519 return 0;
2522 /* Use a label already defined or ref'd with this name, but not if
2523 it is inherited from a containing function and wasn't declared
2524 using __label__. */
2525 label = I_LABEL_DECL (name);
2526 if (label && (DECL_CONTEXT (label) == current_function_decl
2527 || C_DECLARED_LABEL_FLAG (label)))
2529 /* If the label has only been declared, update its apparent
2530 location to point here, for better diagnostics if it
2531 turns out not to have been defined. */
2532 if (!TREE_USED (label))
2533 DECL_SOURCE_LOCATION (label) = input_location;
2534 return label;
2537 /* No label binding for that identifier; make one. */
2538 label = make_label (name, input_location);
2540 /* Ordinary labels go in the current function scope. */
2541 bind (name, label, current_function_scope,
2542 /*invisible=*/false, /*nested=*/false);
2543 return label;
2546 /* Make a label named NAME in the current function, shadowing silently
2547 any that may be inherited from containing functions or containing
2548 scopes. This is called for __label__ declarations. */
2550 tree
2551 declare_label (tree name)
2553 struct c_binding *b = I_LABEL_BINDING (name);
2554 tree label;
2556 /* Check to make sure that the label hasn't already been declared
2557 at this scope */
2558 if (b && B_IN_CURRENT_SCOPE (b))
2560 error ("duplicate label declaration %qE", name);
2561 locate_old_decl (b->decl, error);
2563 /* Just use the previous declaration. */
2564 return b->decl;
2567 label = make_label (name, input_location);
2568 C_DECLARED_LABEL_FLAG (label) = 1;
2570 /* Declared labels go in the current scope. */
2571 bind (name, label, current_scope,
2572 /*invisible=*/false, /*nested=*/false);
2573 return label;
2576 /* Define a label, specifying the location in the source file.
2577 Return the LABEL_DECL node for the label, if the definition is valid.
2578 Otherwise return 0. */
2580 tree
2581 define_label (location_t location, tree name)
2583 /* Find any preexisting label with this name. It is an error
2584 if that label has already been defined in this function, or
2585 if there is a containing function with a declared label with
2586 the same name. */
2587 tree label = I_LABEL_DECL (name);
2588 struct c_label_list *nlist_se, *nlist_vm;
2590 if (label
2591 && ((DECL_CONTEXT (label) == current_function_decl
2592 && DECL_INITIAL (label) != 0)
2593 || (DECL_CONTEXT (label) != current_function_decl
2594 && C_DECLARED_LABEL_FLAG (label))))
2596 error ("%Hduplicate label %qD", &location, label);
2597 locate_old_decl (label, error);
2598 return 0;
2600 else if (label && DECL_CONTEXT (label) == current_function_decl)
2602 /* The label has been used or declared already in this function,
2603 but not defined. Update its location to point to this
2604 definition. */
2605 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2606 error ("%Jjump into statement expression", label);
2607 if (C_DECL_UNDEFINABLE_VM (label))
2608 error ("%Jjump into scope of identifier with variably modified type",
2609 label);
2610 DECL_SOURCE_LOCATION (label) = location;
2612 else
2614 /* No label binding for that identifier; make one. */
2615 label = make_label (name, location);
2617 /* Ordinary labels go in the current function scope. */
2618 bind (name, label, current_function_scope,
2619 /*invisible=*/false, /*nested=*/false);
2622 if (!in_system_header && lookup_name (name))
2623 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2624 "for labels, identifier %qE conflicts", &location, name);
2626 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2627 nlist_se->next = label_context_stack_se->labels_def;
2628 nlist_se->label = label;
2629 label_context_stack_se->labels_def = nlist_se;
2631 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2632 nlist_vm->next = label_context_stack_vm->labels_def;
2633 nlist_vm->label = label;
2634 label_context_stack_vm->labels_def = nlist_vm;
2636 /* Mark label as having been defined. */
2637 DECL_INITIAL (label) = error_mark_node;
2638 return label;
2641 /* Given NAME, an IDENTIFIER_NODE,
2642 return the structure (or union or enum) definition for that name.
2643 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2644 CODE says which kind of type the caller wants;
2645 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2646 If the wrong kind of type is found, an error is reported. */
2648 static tree
2649 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2651 struct c_binding *b = I_TAG_BINDING (name);
2652 int thislevel = 0;
2654 if (!b || !b->decl)
2655 return 0;
2657 /* We only care about whether it's in this level if
2658 thislevel_only was set or it might be a type clash. */
2659 if (thislevel_only || TREE_CODE (b->decl) != code)
2661 /* For our purposes, a tag in the external scope is the same as
2662 a tag in the file scope. (Primarily relevant to Objective-C
2663 and its builtin structure tags, which get pushed before the
2664 file scope is created.) */
2665 if (B_IN_CURRENT_SCOPE (b)
2666 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2667 thislevel = 1;
2670 if (thislevel_only && !thislevel)
2671 return 0;
2673 if (TREE_CODE (b->decl) != code)
2675 /* Definition isn't the kind we were looking for. */
2676 pending_invalid_xref = name;
2677 pending_invalid_xref_location = input_location;
2679 /* If in the same binding level as a declaration as a tag
2680 of a different type, this must not be allowed to
2681 shadow that tag, so give the error immediately.
2682 (For example, "struct foo; union foo;" is invalid.) */
2683 if (thislevel)
2684 pending_xref_error ();
2686 return b->decl;
2689 /* Print an error message now
2690 for a recent invalid struct, union or enum cross reference.
2691 We don't print them immediately because they are not invalid
2692 when used in the `struct foo;' construct for shadowing. */
2694 void
2695 pending_xref_error (void)
2697 if (pending_invalid_xref != 0)
2698 error ("%H%qE defined as wrong kind of tag",
2699 &pending_invalid_xref_location, pending_invalid_xref);
2700 pending_invalid_xref = 0;
2704 /* Look up NAME in the current scope and its superiors
2705 in the namespace of variables, functions and typedefs.
2706 Return a ..._DECL node of some kind representing its definition,
2707 or return 0 if it is undefined. */
2709 tree
2710 lookup_name (tree name)
2712 struct c_binding *b = I_SYMBOL_BINDING (name);
2713 if (b && !b->invisible)
2714 return b->decl;
2715 return 0;
2718 /* Similar to `lookup_name' but look only at the indicated scope. */
2720 static tree
2721 lookup_name_in_scope (tree name, struct c_scope *scope)
2723 struct c_binding *b;
2725 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2726 if (B_IN_SCOPE (b, scope))
2727 return b->decl;
2728 return 0;
2731 /* Create the predefined scalar types of C,
2732 and some nodes representing standard constants (0, 1, (void *) 0).
2733 Initialize the global scope.
2734 Make definitions for built-in primitive functions. */
2736 void
2737 c_init_decl_processing (void)
2739 location_t save_loc = input_location;
2741 /* Initialize reserved words for parser. */
2742 c_parse_init ();
2744 current_function_decl = 0;
2746 gcc_obstack_init (&parser_obstack);
2748 /* Make the externals scope. */
2749 push_scope ();
2750 external_scope = current_scope;
2752 /* Declarations from c_common_nodes_and_builtins must not be associated
2753 with this input file, lest we get differences between using and not
2754 using preprocessed headers. */
2755 input_location = BUILTINS_LOCATION;
2757 build_common_tree_nodes (flag_signed_char, false);
2759 c_common_nodes_and_builtins ();
2761 /* In C, comparisons and TRUTH_* expressions have type int. */
2762 truthvalue_type_node = integer_type_node;
2763 truthvalue_true_node = integer_one_node;
2764 truthvalue_false_node = integer_zero_node;
2766 /* Even in C99, which has a real boolean type. */
2767 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2768 boolean_type_node));
2770 input_location = save_loc;
2772 pedantic_lvalues = true;
2774 make_fname_decl = c_make_fname_decl;
2775 start_fname_decls ();
2778 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2779 decl, NAME is the initialization string and TYPE_DEP indicates whether
2780 NAME depended on the type of the function. As we don't yet implement
2781 delayed emission of static data, we mark the decl as emitted
2782 so it is not placed in the output. Anything using it must therefore pull
2783 out the STRING_CST initializer directly. FIXME. */
2785 static tree
2786 c_make_fname_decl (tree id, int type_dep)
2788 const char *name = fname_as_string (type_dep);
2789 tree decl, type, init;
2790 size_t length = strlen (name);
2792 type = build_array_type (char_type_node,
2793 build_index_type (size_int (length)));
2794 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2796 decl = build_decl (VAR_DECL, id, type);
2798 TREE_STATIC (decl) = 1;
2799 TREE_READONLY (decl) = 1;
2800 DECL_ARTIFICIAL (decl) = 1;
2802 init = build_string (length + 1, name);
2803 free (CONST_CAST (char *, name));
2804 TREE_TYPE (init) = type;
2805 DECL_INITIAL (decl) = init;
2807 TREE_USED (decl) = 1;
2809 if (current_function_decl
2810 /* For invalid programs like this:
2812 void foo()
2813 const char* p = __FUNCTION__;
2815 the __FUNCTION__ is believed to appear in K&R style function
2816 parameter declarator. In that case we still don't have
2817 function_scope. */
2818 && (!errorcount || current_function_scope))
2820 DECL_CONTEXT (decl) = current_function_decl;
2821 bind (id, decl, current_function_scope,
2822 /*invisible=*/false, /*nested=*/false);
2825 finish_decl (decl, init, NULL_TREE);
2827 return decl;
2830 tree
2831 c_builtin_function (tree decl)
2833 tree type = TREE_TYPE (decl);
2834 tree id = DECL_NAME (decl);
2836 const char *name = IDENTIFIER_POINTER (id);
2837 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2839 /* Should never be called on a symbol with a preexisting meaning. */
2840 gcc_assert (!I_SYMBOL_BINDING (id));
2842 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2844 /* Builtins in the implementation namespace are made visible without
2845 needing to be explicitly declared. See push_file_scope. */
2846 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2848 TREE_CHAIN (decl) = visible_builtins;
2849 visible_builtins = decl;
2852 return decl;
2855 /* Called when a declaration is seen that contains no names to declare.
2856 If its type is a reference to a structure, union or enum inherited
2857 from a containing scope, shadow that tag name for the current scope
2858 with a forward reference.
2859 If its type defines a new named structure or union
2860 or defines an enum, it is valid but we need not do anything here.
2861 Otherwise, it is an error. */
2863 void
2864 shadow_tag (const struct c_declspecs *declspecs)
2866 shadow_tag_warned (declspecs, 0);
2869 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2870 but no pedwarn. */
2871 void
2872 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2874 bool found_tag = false;
2876 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2878 tree value = declspecs->type;
2879 enum tree_code code = TREE_CODE (value);
2881 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2882 /* Used to test also that TYPE_SIZE (value) != 0.
2883 That caused warning for `struct foo;' at top level in the file. */
2885 tree name = TYPE_NAME (value);
2886 tree t;
2888 found_tag = true;
2890 if (name == 0)
2892 if (warned != 1 && code != ENUMERAL_TYPE)
2893 /* Empty unnamed enum OK */
2895 pedwarn ("unnamed struct/union that defines no instances");
2896 warned = 1;
2899 else if (!declspecs->tag_defined_p
2900 && declspecs->storage_class != csc_none)
2902 if (warned != 1)
2903 pedwarn ("empty declaration with storage class specifier "
2904 "does not redeclare tag");
2905 warned = 1;
2906 pending_xref_error ();
2908 else if (!declspecs->tag_defined_p
2909 && (declspecs->const_p
2910 || declspecs->volatile_p
2911 || declspecs->restrict_p))
2913 if (warned != 1)
2914 pedwarn ("empty declaration with type qualifier "
2915 "does not redeclare tag");
2916 warned = 1;
2917 pending_xref_error ();
2919 else
2921 pending_invalid_xref = 0;
2922 t = lookup_tag (code, name, 1);
2924 if (t == 0)
2926 t = make_node (code);
2927 pushtag (name, t);
2931 else
2933 if (warned != 1 && !in_system_header)
2935 pedwarn ("useless type name in empty declaration");
2936 warned = 1;
2940 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2942 pedwarn ("useless type name in empty declaration");
2943 warned = 1;
2946 pending_invalid_xref = 0;
2948 if (declspecs->inline_p)
2950 error ("%<inline%> in empty declaration");
2951 warned = 1;
2954 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2956 error ("%<auto%> in file-scope empty declaration");
2957 warned = 1;
2960 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2962 error ("%<register%> in file-scope empty declaration");
2963 warned = 1;
2966 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2968 warning (0, "useless storage class specifier in empty declaration");
2969 warned = 2;
2972 if (!warned && !in_system_header && declspecs->thread_p)
2974 warning (0, "useless %<__thread%> in empty declaration");
2975 warned = 2;
2978 if (!warned && !in_system_header && (declspecs->const_p
2979 || declspecs->volatile_p
2980 || declspecs->restrict_p))
2982 warning (0, "useless type qualifier in empty declaration");
2983 warned = 2;
2986 if (warned != 1)
2988 if (!found_tag)
2989 pedwarn ("empty declaration");
2994 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2995 bits. SPECS represents declaration specifiers that the grammar
2996 only permits to contain type qualifiers and attributes. */
2999 quals_from_declspecs (const struct c_declspecs *specs)
3001 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3002 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3003 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3004 gcc_assert (!specs->type
3005 && !specs->decl_attr
3006 && specs->typespec_word == cts_none
3007 && specs->storage_class == csc_none
3008 && !specs->typedef_p
3009 && !specs->explicit_signed_p
3010 && !specs->deprecated_p
3011 && !specs->long_p
3012 && !specs->long_long_p
3013 && !specs->short_p
3014 && !specs->signed_p
3015 && !specs->unsigned_p
3016 && !specs->complex_p
3017 && !specs->inline_p
3018 && !specs->thread_p);
3019 return quals;
3022 /* Construct an array declarator. EXPR is the expression inside [],
3023 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3024 applied to the pointer to which a parameter array is converted).
3025 STATIC_P is true if "static" is inside the [], false otherwise.
3026 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3027 length which is nevertheless a complete type, false otherwise. The
3028 field for the contained declarator is left to be filled in by
3029 set_array_declarator_inner. */
3031 struct c_declarator *
3032 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3033 bool vla_unspec_p)
3035 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3036 struct c_declarator);
3037 declarator->kind = cdk_array;
3038 declarator->declarator = 0;
3039 declarator->u.array.dimen = expr;
3040 if (quals)
3042 declarator->u.array.attrs = quals->attrs;
3043 declarator->u.array.quals = quals_from_declspecs (quals);
3045 else
3047 declarator->u.array.attrs = NULL_TREE;
3048 declarator->u.array.quals = 0;
3050 declarator->u.array.static_p = static_p;
3051 declarator->u.array.vla_unspec_p = vla_unspec_p;
3052 if (pedantic && !flag_isoc99)
3054 if (static_p || quals != NULL)
3055 pedwarn ("ISO C90 does not support %<static%> or type "
3056 "qualifiers in parameter array declarators");
3057 if (vla_unspec_p)
3058 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3060 if (vla_unspec_p)
3062 if (!current_scope->parm_flag)
3064 /* C99 6.7.5.2p4 */
3065 error ("%<[*]%> not allowed in other than function prototype scope");
3066 declarator->u.array.vla_unspec_p = false;
3067 return NULL;
3069 current_scope->had_vla_unspec = true;
3071 return declarator;
3074 /* Set the contained declarator of an array declarator. DECL is the
3075 declarator, as constructed by build_array_declarator; INNER is what
3076 appears on the left of the []. */
3078 struct c_declarator *
3079 set_array_declarator_inner (struct c_declarator *decl,
3080 struct c_declarator *inner)
3082 decl->declarator = inner;
3083 return decl;
3086 /* INIT is a constructor that forms DECL's initializer. If the final
3087 element initializes a flexible array field, add the size of that
3088 initializer to DECL's size. */
3090 static void
3091 add_flexible_array_elts_to_size (tree decl, tree init)
3093 tree elt, type;
3095 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3096 return;
3098 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3099 type = TREE_TYPE (elt);
3100 if (TREE_CODE (type) == ARRAY_TYPE
3101 && TYPE_SIZE (type) == NULL_TREE
3102 && TYPE_DOMAIN (type) != NULL_TREE
3103 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3105 complete_array_type (&type, elt, false);
3106 DECL_SIZE (decl)
3107 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3108 DECL_SIZE_UNIT (decl)
3109 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3113 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3115 tree
3116 groktypename (struct c_type_name *type_name)
3118 tree type;
3119 tree attrs = type_name->specs->attrs;
3121 type_name->specs->attrs = NULL_TREE;
3123 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3124 false, NULL, &attrs, DEPRECATED_NORMAL);
3126 /* Apply attributes. */
3127 decl_attributes (&type, attrs, 0);
3129 return type;
3132 /* Decode a declarator in an ordinary declaration or data definition.
3133 This is called as soon as the type information and variable name
3134 have been parsed, before parsing the initializer if any.
3135 Here we create the ..._DECL node, fill in its type,
3136 and put it on the list of decls for the current context.
3137 The ..._DECL node is returned as the value.
3139 Exception: for arrays where the length is not specified,
3140 the type is left null, to be filled in by `finish_decl'.
3142 Function definitions do not come here; they go to start_function
3143 instead. However, external and forward declarations of functions
3144 do go through here. Structure field declarations are done by
3145 grokfield and not through here. */
3147 tree
3148 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3149 bool initialized, tree attributes)
3151 tree decl;
3152 tree tem;
3153 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3155 /* An object declared as __attribute__((deprecated)) suppresses
3156 warnings of uses of other deprecated items. */
3157 if (lookup_attribute ("deprecated", attributes))
3158 deprecated_state = DEPRECATED_SUPPRESS;
3160 decl = grokdeclarator (declarator, declspecs,
3161 NORMAL, initialized, NULL, &attributes,
3162 deprecated_state);
3163 if (!decl)
3164 return 0;
3166 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3167 && MAIN_NAME_P (DECL_NAME (decl)))
3168 warning (OPT_Wmain, "%q+D is usually a function", decl);
3170 if (initialized)
3171 /* Is it valid for this decl to have an initializer at all?
3172 If not, set INITIALIZED to zero, which will indirectly
3173 tell 'finish_decl' to ignore the initializer once it is parsed. */
3174 switch (TREE_CODE (decl))
3176 case TYPE_DECL:
3177 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3178 initialized = 0;
3179 break;
3181 case FUNCTION_DECL:
3182 error ("function %qD is initialized like a variable", decl);
3183 initialized = 0;
3184 break;
3186 case PARM_DECL:
3187 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3188 error ("parameter %qD is initialized", decl);
3189 initialized = 0;
3190 break;
3192 default:
3193 /* Don't allow initializations for incomplete types except for
3194 arrays which might be completed by the initialization. */
3196 /* This can happen if the array size is an undefined macro.
3197 We already gave a warning, so we don't need another one. */
3198 if (TREE_TYPE (decl) == error_mark_node)
3199 initialized = 0;
3200 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3202 /* A complete type is ok if size is fixed. */
3204 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3205 || C_DECL_VARIABLE_SIZE (decl))
3207 error ("variable-sized object may not be initialized");
3208 initialized = 0;
3211 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3213 error ("variable %qD has initializer but incomplete type", decl);
3214 initialized = 0;
3216 else if (C_DECL_VARIABLE_SIZE (decl))
3218 /* Although C99 is unclear about whether incomplete arrays
3219 of VLAs themselves count as VLAs, it does not make
3220 sense to permit them to be initialized given that
3221 ordinary VLAs may not be initialized. */
3222 error ("variable-sized object may not be initialized");
3223 initialized = 0;
3227 if (initialized)
3229 if (current_scope == file_scope)
3230 TREE_STATIC (decl) = 1;
3232 /* Tell 'pushdecl' this is an initialized decl
3233 even though we don't yet have the initializer expression.
3234 Also tell 'finish_decl' it may store the real initializer. */
3235 DECL_INITIAL (decl) = error_mark_node;
3238 /* If this is a function declaration, write a record describing it to the
3239 prototypes file (if requested). */
3241 if (TREE_CODE (decl) == FUNCTION_DECL)
3242 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3244 /* ANSI specifies that a tentative definition which is not merged with
3245 a non-tentative definition behaves exactly like a definition with an
3246 initializer equal to zero. (Section 3.7.2)
3248 -fno-common gives strict ANSI behavior, though this tends to break
3249 a large body of code that grew up without this rule.
3251 Thread-local variables are never common, since there's no entrenched
3252 body of code to break, and it allows more efficient variable references
3253 in the presence of dynamic linking. */
3255 if (TREE_CODE (decl) == VAR_DECL
3256 && !initialized
3257 && TREE_PUBLIC (decl)
3258 && !DECL_THREAD_LOCAL_P (decl)
3259 && !flag_no_common)
3260 DECL_COMMON (decl) = 1;
3262 /* Set attributes here so if duplicate decl, will have proper attributes. */
3263 decl_attributes (&decl, attributes, 0);
3265 /* Handle gnu_inline attribute. */
3266 if (declspecs->inline_p
3267 && !flag_gnu89_inline
3268 && TREE_CODE (decl) == FUNCTION_DECL
3269 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
3270 || current_function_decl))
3272 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3274 else if (declspecs->storage_class != csc_static)
3275 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3278 if (TREE_CODE (decl) == FUNCTION_DECL
3279 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3281 struct c_declarator *ce = declarator;
3283 if (ce->kind == cdk_pointer)
3284 ce = declarator->declarator;
3285 if (ce->kind == cdk_function)
3287 tree args = ce->u.arg_info->parms;
3288 for (; args; args = TREE_CHAIN (args))
3290 tree type = TREE_TYPE (args);
3291 if (type && INTEGRAL_TYPE_P (type)
3292 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3293 DECL_ARG_TYPE (args) = integer_type_node;
3298 if (TREE_CODE (decl) == FUNCTION_DECL
3299 && DECL_DECLARED_INLINE_P (decl)
3300 && DECL_UNINLINABLE (decl)
3301 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3302 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3303 decl);
3305 /* C99 6.7.4p3: An inline definition of a function with external
3306 linkage shall not contain a definition of a modifiable object
3307 with static storage duration... */
3308 if (TREE_CODE (decl) == VAR_DECL
3309 && current_scope != file_scope
3310 && TREE_STATIC (decl)
3311 && !TREE_READONLY (decl)
3312 && DECL_DECLARED_INLINE_P (current_function_decl)
3313 && DECL_EXTERNAL (current_function_decl))
3314 pedwarn ("%q+D is static but declared in inline function %qD "
3315 "which is not static", decl, current_function_decl);
3317 /* Add this decl to the current scope.
3318 TEM may equal DECL or it may be a previous decl of the same name. */
3319 tem = pushdecl (decl);
3321 if (initialized && DECL_EXTERNAL (tem))
3323 DECL_EXTERNAL (tem) = 0;
3324 TREE_STATIC (tem) = 1;
3327 return tem;
3330 /* Initialize EH if not initialized yet and exceptions are enabled. */
3332 void
3333 c_maybe_initialize_eh (void)
3335 if (!flag_exceptions || c_eh_initialized_p)
3336 return;
3338 c_eh_initialized_p = true;
3339 eh_personality_libfunc
3340 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3341 ? "__gcc_personality_sj0"
3342 : "__gcc_personality_v0");
3343 default_init_unwind_resume_libfunc ();
3344 using_eh_for_cleanups ();
3347 /* Finish processing of a declaration;
3348 install its initial value.
3349 If the length of an array type is not known before,
3350 it must be determined now, from the initial value, or it is an error. */
3352 void
3353 finish_decl (tree decl, tree init, tree asmspec_tree)
3355 tree type;
3356 int was_incomplete = (DECL_SIZE (decl) == 0);
3357 const char *asmspec = 0;
3359 /* If a name was specified, get the string. */
3360 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3361 && DECL_FILE_SCOPE_P (decl))
3362 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3363 if (asmspec_tree)
3364 asmspec = TREE_STRING_POINTER (asmspec_tree);
3366 /* If `start_decl' didn't like having an initialization, ignore it now. */
3367 if (init != 0 && DECL_INITIAL (decl) == 0)
3368 init = 0;
3370 /* Don't crash if parm is initialized. */
3371 if (TREE_CODE (decl) == PARM_DECL)
3372 init = 0;
3374 if (init)
3375 store_init_value (decl, init);
3377 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3378 || TREE_CODE (decl) == FUNCTION_DECL
3379 || TREE_CODE (decl) == FIELD_DECL))
3380 objc_check_decl (decl);
3382 type = TREE_TYPE (decl);
3384 /* Deduce size of array from initialization, if not already known. */
3385 if (TREE_CODE (type) == ARRAY_TYPE
3386 && TYPE_DOMAIN (type) == 0
3387 && TREE_CODE (decl) != TYPE_DECL)
3389 bool do_default
3390 = (TREE_STATIC (decl)
3391 /* Even if pedantic, an external linkage array
3392 may have incomplete type at first. */
3393 ? pedantic && !TREE_PUBLIC (decl)
3394 : !DECL_EXTERNAL (decl));
3395 int failure
3396 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3397 do_default);
3399 /* Get the completed type made by complete_array_type. */
3400 type = TREE_TYPE (decl);
3402 switch (failure)
3404 case 1:
3405 error ("initializer fails to determine size of %q+D", decl);
3406 break;
3408 case 2:
3409 if (do_default)
3410 error ("array size missing in %q+D", decl);
3411 /* If a `static' var's size isn't known,
3412 make it extern as well as static, so it does not get
3413 allocated.
3414 If it is not `static', then do not mark extern;
3415 finish_incomplete_decl will give it a default size
3416 and it will get allocated. */
3417 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3418 DECL_EXTERNAL (decl) = 1;
3419 break;
3421 case 3:
3422 error ("zero or negative size array %q+D", decl);
3423 break;
3425 case 0:
3426 /* For global variables, update the copy of the type that
3427 exists in the binding. */
3428 if (TREE_PUBLIC (decl))
3430 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3431 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3432 b_ext = b_ext->shadowed;
3433 if (b_ext)
3435 if (b_ext->type)
3436 b_ext->type = composite_type (b_ext->type, type);
3437 else
3438 b_ext->type = type;
3441 break;
3443 default:
3444 gcc_unreachable ();
3447 if (DECL_INITIAL (decl))
3448 TREE_TYPE (DECL_INITIAL (decl)) = type;
3450 layout_decl (decl, 0);
3453 if (TREE_CODE (decl) == VAR_DECL)
3455 if (init && TREE_CODE (init) == CONSTRUCTOR)
3456 add_flexible_array_elts_to_size (decl, init);
3458 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3459 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3460 layout_decl (decl, 0);
3462 if (DECL_SIZE (decl) == 0
3463 /* Don't give an error if we already gave one earlier. */
3464 && TREE_TYPE (decl) != error_mark_node
3465 && (TREE_STATIC (decl)
3466 /* A static variable with an incomplete type
3467 is an error if it is initialized.
3468 Also if it is not file scope.
3469 Otherwise, let it through, but if it is not `extern'
3470 then it may cause an error message later. */
3471 ? (DECL_INITIAL (decl) != 0
3472 || !DECL_FILE_SCOPE_P (decl))
3473 /* An automatic variable with an incomplete type
3474 is an error. */
3475 : !DECL_EXTERNAL (decl)))
3477 error ("storage size of %q+D isn%'t known", decl);
3478 TREE_TYPE (decl) = error_mark_node;
3481 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3482 && DECL_SIZE (decl) != 0)
3484 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3485 constant_expression_warning (DECL_SIZE (decl));
3486 else
3488 error ("storage size of %q+D isn%'t constant", decl);
3489 TREE_TYPE (decl) = error_mark_node;
3493 if (TREE_USED (type))
3494 TREE_USED (decl) = 1;
3497 /* If this is a function and an assembler name is specified, reset DECL_RTL
3498 so we can give it its new name. Also, update built_in_decls if it
3499 was a normal built-in. */
3500 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3502 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3503 set_builtin_user_assembler_name (decl, asmspec);
3504 set_user_assembler_name (decl, asmspec);
3507 /* If #pragma weak was used, mark the decl weak now. */
3508 maybe_apply_pragma_weak (decl);
3510 /* Output the assembler code and/or RTL code for variables and functions,
3511 unless the type is an undefined structure or union.
3512 If not, it will get done when the type is completed. */
3514 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3516 /* Determine the ELF visibility. */
3517 if (TREE_PUBLIC (decl))
3518 c_determine_visibility (decl);
3520 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3521 if (c_dialect_objc ())
3522 objc_check_decl (decl);
3524 if (asmspec)
3526 /* If this is not a static variable, issue a warning.
3527 It doesn't make any sense to give an ASMSPEC for an
3528 ordinary, non-register local variable. Historically,
3529 GCC has accepted -- but ignored -- the ASMSPEC in
3530 this case. */
3531 if (!DECL_FILE_SCOPE_P (decl)
3532 && TREE_CODE (decl) == VAR_DECL
3533 && !C_DECL_REGISTER (decl)
3534 && !TREE_STATIC (decl))
3535 warning (0, "ignoring asm-specifier for non-static local "
3536 "variable %q+D", decl);
3537 else
3538 set_user_assembler_name (decl, asmspec);
3541 if (DECL_FILE_SCOPE_P (decl))
3543 if (DECL_INITIAL (decl) == NULL_TREE
3544 || DECL_INITIAL (decl) == error_mark_node)
3545 /* Don't output anything
3546 when a tentative file-scope definition is seen.
3547 But at end of compilation, do output code for them. */
3548 DECL_DEFER_OUTPUT (decl) = 1;
3549 rest_of_decl_compilation (decl, true, 0);
3551 else
3553 /* In conjunction with an ASMSPEC, the `register'
3554 keyword indicates that we should place the variable
3555 in a particular register. */
3556 if (asmspec && C_DECL_REGISTER (decl))
3558 DECL_HARD_REGISTER (decl) = 1;
3559 /* This cannot be done for a structure with volatile
3560 fields, on which DECL_REGISTER will have been
3561 reset. */
3562 if (!DECL_REGISTER (decl))
3563 error ("cannot put object with volatile field into register");
3566 if (TREE_CODE (decl) != FUNCTION_DECL)
3568 /* If we're building a variable sized type, and we might be
3569 reachable other than via the top of the current binding
3570 level, then create a new BIND_EXPR so that we deallocate
3571 the object at the right time. */
3572 /* Note that DECL_SIZE can be null due to errors. */
3573 if (DECL_SIZE (decl)
3574 && !TREE_CONSTANT (DECL_SIZE (decl))
3575 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3577 tree bind;
3578 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3579 TREE_SIDE_EFFECTS (bind) = 1;
3580 add_stmt (bind);
3581 BIND_EXPR_BODY (bind) = push_stmt_list ();
3583 add_stmt (build_stmt (DECL_EXPR, decl));
3588 if (!DECL_FILE_SCOPE_P (decl))
3590 /* Recompute the RTL of a local array now
3591 if it used to be an incomplete type. */
3592 if (was_incomplete
3593 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3595 /* If we used it already as memory, it must stay in memory. */
3596 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3597 /* If it's still incomplete now, no init will save it. */
3598 if (DECL_SIZE (decl) == 0)
3599 DECL_INITIAL (decl) = 0;
3604 /* If this was marked 'used', be sure it will be output. */
3605 if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3606 mark_decl_referenced (decl);
3608 if (TREE_CODE (decl) == TYPE_DECL)
3610 if (!DECL_FILE_SCOPE_P (decl)
3611 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3612 add_stmt (build_stmt (DECL_EXPR, decl));
3614 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3617 /* At the end of a declaration, throw away any variable type sizes
3618 of types defined inside that declaration. There is no use
3619 computing them in the following function definition. */
3620 if (current_scope == file_scope)
3621 get_pending_sizes ();
3623 /* Install a cleanup (aka destructor) if one was given. */
3624 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3626 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3627 if (attr)
3629 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3630 tree cleanup_decl = lookup_name (cleanup_id);
3631 tree cleanup;
3633 /* Build "cleanup(&decl)" for the destructor. */
3634 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3635 cleanup = build_tree_list (NULL_TREE, cleanup);
3636 cleanup = build_function_call (cleanup_decl, cleanup);
3638 /* Don't warn about decl unused; the cleanup uses it. */
3639 TREE_USED (decl) = 1;
3640 TREE_USED (cleanup_decl) = 1;
3642 /* Initialize EH, if we've been told to do so. */
3643 c_maybe_initialize_eh ();
3645 push_cleanup (decl, cleanup, false);
3650 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3652 tree
3653 grokparm (const struct c_parm *parm)
3655 tree attrs = parm->attrs;
3656 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3657 NULL, &attrs, DEPRECATED_NORMAL);
3659 decl_attributes (&decl, attrs, 0);
3661 return decl;
3664 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3665 and push that on the current scope. */
3667 void
3668 push_parm_decl (const struct c_parm *parm)
3670 tree attrs = parm->attrs;
3671 tree decl;
3673 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
3674 &attrs, DEPRECATED_NORMAL);
3675 decl_attributes (&decl, attrs, 0);
3677 decl = pushdecl (decl);
3679 finish_decl (decl, NULL_TREE, NULL_TREE);
3682 /* Mark all the parameter declarations to date as forward decls.
3683 Also diagnose use of this extension. */
3685 void
3686 mark_forward_parm_decls (void)
3688 struct c_binding *b;
3690 if (pedantic && !current_scope->warned_forward_parm_decls)
3692 pedwarn ("ISO C forbids forward parameter declarations");
3693 current_scope->warned_forward_parm_decls = true;
3696 for (b = current_scope->bindings; b; b = b->prev)
3697 if (TREE_CODE (b->decl) == PARM_DECL)
3698 TREE_ASM_WRITTEN (b->decl) = 1;
3701 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3702 literal, which may be an incomplete array type completed by the
3703 initializer; INIT is a CONSTRUCTOR that initializes the compound
3704 literal. */
3706 tree
3707 build_compound_literal (tree type, tree init)
3709 /* We do not use start_decl here because we have a type, not a declarator;
3710 and do not use finish_decl because the decl should be stored inside
3711 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3712 tree decl;
3713 tree complit;
3714 tree stmt;
3716 if (type == error_mark_node)
3717 return error_mark_node;
3719 decl = build_decl (VAR_DECL, NULL_TREE, type);
3720 DECL_EXTERNAL (decl) = 0;
3721 TREE_PUBLIC (decl) = 0;
3722 TREE_STATIC (decl) = (current_scope == file_scope);
3723 DECL_CONTEXT (decl) = current_function_decl;
3724 TREE_USED (decl) = 1;
3725 TREE_TYPE (decl) = type;
3726 TREE_READONLY (decl) = TYPE_READONLY (type);
3727 store_init_value (decl, init);
3729 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3731 int failure = complete_array_type (&TREE_TYPE (decl),
3732 DECL_INITIAL (decl), true);
3733 gcc_assert (!failure);
3735 type = TREE_TYPE (decl);
3736 TREE_TYPE (DECL_INITIAL (decl)) = type;
3739 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3740 return error_mark_node;
3742 stmt = build_stmt (DECL_EXPR, decl);
3743 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3744 TREE_SIDE_EFFECTS (complit) = 1;
3746 layout_decl (decl, 0);
3748 if (TREE_STATIC (decl))
3750 /* This decl needs a name for the assembler output. */
3751 set_compound_literal_name (decl);
3752 DECL_DEFER_OUTPUT (decl) = 1;
3753 DECL_COMDAT (decl) = 1;
3754 DECL_ARTIFICIAL (decl) = 1;
3755 DECL_IGNORED_P (decl) = 1;
3756 pushdecl (decl);
3757 rest_of_decl_compilation (decl, 1, 0);
3760 return complit;
3763 /* Determine whether TYPE is a structure with a flexible array member,
3764 or a union containing such a structure (possibly recursively). */
3766 static bool
3767 flexible_array_type_p (tree type)
3769 tree x;
3770 switch (TREE_CODE (type))
3772 case RECORD_TYPE:
3773 x = TYPE_FIELDS (type);
3774 if (x == NULL_TREE)
3775 return false;
3776 while (TREE_CHAIN (x) != NULL_TREE)
3777 x = TREE_CHAIN (x);
3778 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3779 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3780 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3781 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3782 return true;
3783 return false;
3784 case UNION_TYPE:
3785 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3787 if (flexible_array_type_p (TREE_TYPE (x)))
3788 return true;
3790 return false;
3791 default:
3792 return false;
3796 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3797 replacing with appropriate values if they are invalid. */
3798 static void
3799 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3801 tree type_mv;
3802 unsigned int max_width;
3803 unsigned HOST_WIDE_INT w;
3804 const char *name = orig_name ? orig_name: _("<anonymous>");
3806 /* Detect and ignore out of range field width and process valid
3807 field widths. */
3808 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3809 || TREE_CODE (*width) != INTEGER_CST)
3811 error ("bit-field %qs width not an integer constant", name);
3812 *width = integer_one_node;
3814 else
3816 constant_expression_warning (*width);
3817 if (tree_int_cst_sgn (*width) < 0)
3819 error ("negative width in bit-field %qs", name);
3820 *width = integer_one_node;
3822 else if (integer_zerop (*width) && orig_name)
3824 error ("zero width for bit-field %qs", name);
3825 *width = integer_one_node;
3829 /* Detect invalid bit-field type. */
3830 if (TREE_CODE (*type) != INTEGER_TYPE
3831 && TREE_CODE (*type) != BOOLEAN_TYPE
3832 && TREE_CODE (*type) != ENUMERAL_TYPE)
3834 error ("bit-field %qs has invalid type", name);
3835 *type = unsigned_type_node;
3838 type_mv = TYPE_MAIN_VARIANT (*type);
3839 if (pedantic
3840 && !in_system_header
3841 && type_mv != integer_type_node
3842 && type_mv != unsigned_type_node
3843 && type_mv != boolean_type_node)
3844 pedwarn ("type of bit-field %qs is a GCC extension", name);
3846 max_width = TYPE_PRECISION (*type);
3848 if (0 < compare_tree_int (*width, max_width))
3850 error ("width of %qs exceeds its type", name);
3851 w = max_width;
3852 *width = build_int_cst (NULL_TREE, w);
3854 else
3855 w = tree_low_cst (*width, 1);
3857 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3859 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3860 if (!lt
3861 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3862 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3863 warning (0, "%qs is narrower than values of its type", name);
3869 /* Print warning about variable length array if necessary. */
3871 static void
3872 warn_variable_length_array (const char *name, tree size)
3874 int ped = !flag_isoc99 && pedantic && warn_vla != 0;
3875 int const_size = TREE_CONSTANT (size);
3877 if (ped)
3879 if (const_size)
3881 if (name)
3882 pedwarn ("ISO C90 forbids array %qs whose size "
3883 "can%'t be evaluated",
3884 name);
3885 else
3886 pedwarn ("ISO C90 forbids array whose size "
3887 "can%'t be evaluated");
3889 else
3891 if (name)
3892 pedwarn ("ISO C90 forbids variable length array %qs",
3893 name);
3894 else
3895 pedwarn ("ISO C90 forbids variable length array");
3898 else if (warn_vla > 0)
3900 if (const_size)
3902 if (name)
3903 warning (OPT_Wvla,
3904 "the size of array %qs can"
3905 "%'t be evaluated", name);
3906 else
3907 warning (OPT_Wvla,
3908 "the size of array can %'t be evaluated");
3910 else
3912 if (name)
3913 warning (OPT_Wvla,
3914 "variable length array %qs is used",
3915 name);
3916 else
3917 warning (OPT_Wvla,
3918 "variable length array is used");
3923 /* Given declspecs and a declarator,
3924 determine the name and type of the object declared
3925 and construct a ..._DECL node for it.
3926 (In one case we can return a ..._TYPE node instead.
3927 For invalid input we sometimes return 0.)
3929 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3931 DECL_CONTEXT says which syntactic context this declaration is in:
3932 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3933 FUNCDEF for a function definition. Like NORMAL but a few different
3934 error messages in each case. Return value may be zero meaning
3935 this definition is too screwy to try to parse.
3936 PARM for a parameter declaration (either within a function prototype
3937 or before a function body). Make a PARM_DECL, or return void_type_node.
3938 TYPENAME if for a typename (in a cast or sizeof).
3939 Don't make a DECL node; just return the ..._TYPE node.
3940 FIELD for a struct or union field; make a FIELD_DECL.
3941 INITIALIZED is true if the decl has an initializer.
3942 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3943 representing the width of the bit-field.
3944 DECL_ATTRS points to the list of attributes that should be added to this
3945 decl. Any nested attributes that belong on the decl itself will be
3946 added to this list.
3947 DEPRECATED_STATE is a deprecated_states value indicating whether
3948 deprecation warnings should be suppressed.
3950 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3951 It may also be so in the PARM case, for a prototype where the
3952 argument type is specified but not the name.
3954 This function is where the complicated C meanings of `static'
3955 and `extern' are interpreted. */
3957 static tree
3958 grokdeclarator (const struct c_declarator *declarator,
3959 struct c_declspecs *declspecs,
3960 enum decl_context decl_context, bool initialized, tree *width,
3961 tree *decl_attrs, enum deprecated_states deprecated_state)
3963 tree type = declspecs->type;
3964 bool threadp = declspecs->thread_p;
3965 enum c_storage_class storage_class = declspecs->storage_class;
3966 int constp;
3967 int restrictp;
3968 int volatilep;
3969 int type_quals = TYPE_UNQUALIFIED;
3970 const char *name, *orig_name;
3971 bool funcdef_flag = false;
3972 bool funcdef_syntax = false;
3973 int size_varies = 0;
3974 tree decl_attr = declspecs->decl_attr;
3975 int array_ptr_quals = TYPE_UNQUALIFIED;
3976 tree array_ptr_attrs = NULL_TREE;
3977 int array_parm_static = 0;
3978 bool array_parm_vla_unspec_p = false;
3979 tree returned_attrs = NULL_TREE;
3980 bool bitfield = width != NULL;
3981 tree element_type;
3982 struct c_arg_info *arg_info = 0;
3984 if (decl_context == FUNCDEF)
3985 funcdef_flag = true, decl_context = NORMAL;
3987 /* Look inside a declarator for the name being declared
3988 and get it as a string, for an error message. */
3990 const struct c_declarator *decl = declarator;
3991 name = 0;
3993 while (decl)
3994 switch (decl->kind)
3996 case cdk_function:
3997 case cdk_array:
3998 case cdk_pointer:
3999 funcdef_syntax = (decl->kind == cdk_function);
4000 decl = decl->declarator;
4001 break;
4003 case cdk_attrs:
4004 decl = decl->declarator;
4005 break;
4007 case cdk_id:
4008 if (decl->u.id)
4009 name = IDENTIFIER_POINTER (decl->u.id);
4010 decl = 0;
4011 break;
4013 default:
4014 gcc_unreachable ();
4016 orig_name = name;
4017 if (name == 0)
4018 name = "type name";
4021 /* A function definition's declarator must have the form of
4022 a function declarator. */
4024 if (funcdef_flag && !funcdef_syntax)
4025 return 0;
4027 /* If this looks like a function definition, make it one,
4028 even if it occurs where parms are expected.
4029 Then store_parm_decls will reject it and not use it as a parm. */
4030 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4031 decl_context = PARM;
4033 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4034 warn_deprecated_use (declspecs->type);
4036 if ((decl_context == NORMAL || decl_context == FIELD)
4037 && current_scope == file_scope
4038 && variably_modified_type_p (type, NULL_TREE))
4040 error ("variably modified %qs at file scope", name);
4041 type = integer_type_node;
4044 size_varies = C_TYPE_VARIABLE_SIZE (type);
4046 /* Diagnose defaulting to "int". */
4048 if (declspecs->default_int_p && !in_system_header)
4050 /* Issue a warning if this is an ISO C 99 program or if
4051 -Wreturn-type and this is a function, or if -Wimplicit;
4052 prefer the former warning since it is more explicit. */
4053 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4054 && funcdef_flag)
4055 warn_about_return_type = 1;
4056 else if (warn_implicit_int || flag_isoc99)
4057 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
4060 /* Adjust the type if a bit-field is being declared,
4061 -funsigned-bitfields applied and the type is not explicitly
4062 "signed". */
4063 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4064 && TREE_CODE (type) == INTEGER_TYPE)
4065 type = c_common_unsigned_type (type);
4067 /* Figure out the type qualifiers for the declaration. There are
4068 two ways a declaration can become qualified. One is something
4069 like `const int i' where the `const' is explicit. Another is
4070 something like `typedef const int CI; CI i' where the type of the
4071 declaration contains the `const'. A third possibility is that
4072 there is a type qualifier on the element type of a typedefed
4073 array type, in which case we should extract that qualifier so
4074 that c_apply_type_quals_to_decl receives the full list of
4075 qualifiers to work with (C90 is not entirely clear about whether
4076 duplicate qualifiers should be diagnosed in this case, but it
4077 seems most appropriate to do so). */
4078 element_type = strip_array_types (type);
4079 constp = declspecs->const_p + TYPE_READONLY (element_type);
4080 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4081 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4082 if (pedantic && !flag_isoc99)
4084 if (constp > 1)
4085 pedwarn ("duplicate %<const%>");
4086 if (restrictp > 1)
4087 pedwarn ("duplicate %<restrict%>");
4088 if (volatilep > 1)
4089 pedwarn ("duplicate %<volatile%>");
4091 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4092 type = TYPE_MAIN_VARIANT (type);
4093 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4094 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4095 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4097 /* Warn about storage classes that are invalid for certain
4098 kinds of declarations (parameters, typenames, etc.). */
4100 if (funcdef_flag
4101 && (threadp
4102 || storage_class == csc_auto
4103 || storage_class == csc_register
4104 || storage_class == csc_typedef))
4106 if (storage_class == csc_auto
4107 && (pedantic || current_scope == file_scope))
4108 pedwarn ("function definition declared %<auto%>");
4109 if (storage_class == csc_register)
4110 error ("function definition declared %<register%>");
4111 if (storage_class == csc_typedef)
4112 error ("function definition declared %<typedef%>");
4113 if (threadp)
4114 error ("function definition declared %<__thread%>");
4115 threadp = false;
4116 if (storage_class == csc_auto
4117 || storage_class == csc_register
4118 || storage_class == csc_typedef)
4119 storage_class = csc_none;
4121 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4123 if (decl_context == PARM && storage_class == csc_register)
4125 else
4127 switch (decl_context)
4129 case FIELD:
4130 error ("storage class specified for structure field %qs",
4131 name);
4132 break;
4133 case PARM:
4134 error ("storage class specified for parameter %qs", name);
4135 break;
4136 default:
4137 error ("storage class specified for typename");
4138 break;
4140 storage_class = csc_none;
4141 threadp = false;
4144 else if (storage_class == csc_extern
4145 && initialized
4146 && !funcdef_flag)
4148 /* 'extern' with initialization is invalid if not at file scope. */
4149 if (current_scope == file_scope)
4151 /* It is fine to have 'extern const' when compiling at C
4152 and C++ intersection. */
4153 if (!(warn_cxx_compat && constp))
4154 warning (0, "%qs initialized and declared %<extern%>", name);
4156 else
4157 error ("%qs has both %<extern%> and initializer", name);
4159 else if (current_scope == file_scope)
4161 if (storage_class == csc_auto)
4162 error ("file-scope declaration of %qs specifies %<auto%>", name);
4163 if (pedantic && storage_class == csc_register)
4164 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4166 else
4168 if (storage_class == csc_extern && funcdef_flag)
4169 error ("nested function %qs declared %<extern%>", name);
4170 else if (threadp && storage_class == csc_none)
4172 error ("function-scope %qs implicitly auto and declared "
4173 "%<__thread%>",
4174 name);
4175 threadp = false;
4179 /* Now figure out the structure of the declarator proper.
4180 Descend through it, creating more complex types, until we reach
4181 the declared identifier (or NULL_TREE, in an absolute declarator).
4182 At each stage we maintain an unqualified version of the type
4183 together with any qualifiers that should be applied to it with
4184 c_build_qualified_type; this way, array types including
4185 multidimensional array types are first built up in unqualified
4186 form and then the qualified form is created with
4187 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4189 while (declarator && declarator->kind != cdk_id)
4191 if (type == error_mark_node)
4193 declarator = declarator->declarator;
4194 continue;
4197 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4198 a cdk_pointer (for *...),
4199 a cdk_function (for ...(...)),
4200 a cdk_attrs (for nested attributes),
4201 or a cdk_id (for the name being declared
4202 or the place in an absolute declarator
4203 where the name was omitted).
4204 For the last case, we have just exited the loop.
4206 At this point, TYPE is the type of elements of an array,
4207 or for a function to return, or for a pointer to point to.
4208 After this sequence of ifs, TYPE is the type of the
4209 array or function or pointer, and DECLARATOR has had its
4210 outermost layer removed. */
4212 if (array_ptr_quals != TYPE_UNQUALIFIED
4213 || array_ptr_attrs != NULL_TREE
4214 || array_parm_static)
4216 /* Only the innermost declarator (making a parameter be of
4217 array type which is converted to pointer type)
4218 may have static or type qualifiers. */
4219 error ("static or type qualifiers in non-parameter array declarator");
4220 array_ptr_quals = TYPE_UNQUALIFIED;
4221 array_ptr_attrs = NULL_TREE;
4222 array_parm_static = 0;
4225 switch (declarator->kind)
4227 case cdk_attrs:
4229 /* A declarator with embedded attributes. */
4230 tree attrs = declarator->u.attrs;
4231 const struct c_declarator *inner_decl;
4232 int attr_flags = 0;
4233 declarator = declarator->declarator;
4234 inner_decl = declarator;
4235 while (inner_decl->kind == cdk_attrs)
4236 inner_decl = inner_decl->declarator;
4237 if (inner_decl->kind == cdk_id)
4238 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4239 else if (inner_decl->kind == cdk_function)
4240 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4241 else if (inner_decl->kind == cdk_array)
4242 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4243 returned_attrs = decl_attributes (&type,
4244 chainon (returned_attrs, attrs),
4245 attr_flags);
4246 break;
4248 case cdk_array:
4250 tree itype = NULL_TREE;
4251 tree size = declarator->u.array.dimen;
4252 /* The index is a signed object `sizetype' bits wide. */
4253 tree index_type = c_common_signed_type (sizetype);
4255 array_ptr_quals = declarator->u.array.quals;
4256 array_ptr_attrs = declarator->u.array.attrs;
4257 array_parm_static = declarator->u.array.static_p;
4258 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4260 declarator = declarator->declarator;
4262 /* Check for some types that there cannot be arrays of. */
4264 if (VOID_TYPE_P (type))
4266 error ("declaration of %qs as array of voids", name);
4267 type = error_mark_node;
4270 if (TREE_CODE (type) == FUNCTION_TYPE)
4272 error ("declaration of %qs as array of functions", name);
4273 type = error_mark_node;
4276 if (pedantic && !in_system_header && flexible_array_type_p (type))
4277 pedwarn ("invalid use of structure with flexible array member");
4279 if (size == error_mark_node)
4280 type = error_mark_node;
4282 if (type == error_mark_node)
4283 continue;
4285 /* If size was specified, set ITYPE to a range-type for
4286 that size. Otherwise, ITYPE remains null. finish_decl
4287 may figure it out from an initial value. */
4289 if (size)
4291 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4292 lvalue. */
4293 STRIP_TYPE_NOPS (size);
4295 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4297 error ("size of array %qs has non-integer type", name);
4298 size = integer_one_node;
4301 if (pedantic && integer_zerop (size))
4302 pedwarn ("ISO C forbids zero-size array %qs", name);
4304 if (TREE_CODE (size) == INTEGER_CST)
4306 constant_expression_warning (size);
4307 if (tree_int_cst_sgn (size) < 0)
4309 error ("size of array %qs is negative", name);
4310 size = integer_one_node;
4313 else if ((decl_context == NORMAL || decl_context == FIELD)
4314 && current_scope == file_scope)
4316 error ("variably modified %qs at file scope", name);
4317 size = integer_one_node;
4319 else
4321 /* Make sure the array size remains visibly
4322 nonconstant even if it is (eg) a const variable
4323 with known value. */
4324 size_varies = 1;
4325 warn_variable_length_array (orig_name, size);
4328 if (integer_zerop (size))
4330 /* A zero-length array cannot be represented with
4331 an unsigned index type, which is what we'll
4332 get with build_index_type. Create an
4333 open-ended range instead. */
4334 itype = build_range_type (sizetype, size, NULL_TREE);
4336 else
4338 /* Arrange for the SAVE_EXPR on the inside of the
4339 MINUS_EXPR, which allows the -1 to get folded
4340 with the +1 that happens when building TYPE_SIZE. */
4341 if (size_varies)
4342 size = variable_size (size);
4344 /* Compute the maximum valid index, that is, size
4345 - 1. Do the calculation in index_type, so that
4346 if it is a variable the computations will be
4347 done in the proper mode. */
4348 itype = fold_build2 (MINUS_EXPR, index_type,
4349 convert (index_type, size),
4350 convert (index_type,
4351 size_one_node));
4353 /* If that overflowed, the array is too big. ???
4354 While a size of INT_MAX+1 technically shouldn't
4355 cause an overflow (because we subtract 1), the
4356 overflow is recorded during the conversion to
4357 index_type, before the subtraction. Handling
4358 this case seems like an unnecessary
4359 complication. */
4360 if (TREE_CODE (itype) == INTEGER_CST
4361 && TREE_OVERFLOW (itype))
4363 error ("size of array %qs is too large", name);
4364 type = error_mark_node;
4365 continue;
4368 itype = build_index_type (itype);
4371 else if (decl_context == FIELD)
4373 if (pedantic && !flag_isoc99 && !in_system_header)
4374 pedwarn ("ISO C90 does not support flexible array members");
4376 /* ISO C99 Flexible array members are effectively
4377 identical to GCC's zero-length array extension. */
4378 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4380 else if (decl_context == PARM)
4382 if (array_parm_vla_unspec_p)
4384 if (! orig_name)
4386 /* C99 6.7.5.2p4 */
4387 error ("%<[*]%> not allowed in other than a declaration");
4390 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4391 size_varies = 1;
4394 else if (decl_context == TYPENAME)
4396 if (array_parm_vla_unspec_p)
4398 /* The error is printed elsewhere. We use this to
4399 avoid messing up with incomplete array types of
4400 the same type, that would otherwise be modified
4401 below. */
4402 itype = build_range_type (sizetype, size_zero_node,
4403 NULL_TREE);
4407 /* Complain about arrays of incomplete types. */
4408 if (!COMPLETE_TYPE_P (type))
4410 error ("array type has incomplete element type");
4411 type = error_mark_node;
4413 else
4414 /* When itype is NULL, a shared incomplete array type is
4415 returned for all array of a given type. Elsewhere we
4416 make sure we don't complete that type before copying
4417 it, but here we want to make sure we don't ever
4418 modify the shared type, so we gcc_assert (itype)
4419 below. */
4420 type = build_array_type (type, itype);
4422 if (type != error_mark_node)
4424 if (size_varies)
4426 /* It is ok to modify type here even if itype is
4427 NULL: if size_varies, we're in a
4428 multi-dimensional array and the inner type has
4429 variable size, so the enclosing shared array type
4430 must too. */
4431 if (size && TREE_CODE (size) == INTEGER_CST)
4432 type
4433 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4434 C_TYPE_VARIABLE_SIZE (type) = 1;
4437 /* The GCC extension for zero-length arrays differs from
4438 ISO flexible array members in that sizeof yields
4439 zero. */
4440 if (size && integer_zerop (size))
4442 gcc_assert (itype);
4443 TYPE_SIZE (type) = bitsize_zero_node;
4444 TYPE_SIZE_UNIT (type) = size_zero_node;
4446 if (array_parm_vla_unspec_p)
4448 gcc_assert (itype);
4449 /* The type is complete. C99 6.7.5.2p4 */
4450 TYPE_SIZE (type) = bitsize_zero_node;
4451 TYPE_SIZE_UNIT (type) = size_zero_node;
4455 if (decl_context != PARM
4456 && (array_ptr_quals != TYPE_UNQUALIFIED
4457 || array_ptr_attrs != NULL_TREE
4458 || array_parm_static))
4460 error ("static or type qualifiers in non-parameter array declarator");
4461 array_ptr_quals = TYPE_UNQUALIFIED;
4462 array_ptr_attrs = NULL_TREE;
4463 array_parm_static = 0;
4465 break;
4467 case cdk_function:
4469 /* Say it's a definition only for the declarator closest
4470 to the identifier, apart possibly from some
4471 attributes. */
4472 bool really_funcdef = false;
4473 tree arg_types;
4474 if (funcdef_flag)
4476 const struct c_declarator *t = declarator->declarator;
4477 while (t->kind == cdk_attrs)
4478 t = t->declarator;
4479 really_funcdef = (t->kind == cdk_id);
4482 /* Declaring a function type. Make sure we have a valid
4483 type for the function to return. */
4484 if (type == error_mark_node)
4485 continue;
4487 size_varies = 0;
4489 /* Warn about some types functions can't return. */
4490 if (TREE_CODE (type) == FUNCTION_TYPE)
4492 error ("%qs declared as function returning a function", name);
4493 type = integer_type_node;
4495 if (TREE_CODE (type) == ARRAY_TYPE)
4497 error ("%qs declared as function returning an array", name);
4498 type = integer_type_node;
4501 /* Construct the function type and go to the next
4502 inner layer of declarator. */
4503 arg_info = declarator->u.arg_info;
4504 arg_types = grokparms (arg_info, really_funcdef);
4505 if (really_funcdef)
4506 put_pending_sizes (arg_info->pending_sizes);
4508 /* Type qualifiers before the return type of the function
4509 qualify the return type, not the function type. */
4510 if (type_quals)
4512 /* Type qualifiers on a function return type are
4513 normally permitted by the standard but have no
4514 effect, so give a warning at -Wreturn-type.
4515 Qualifiers on a void return type are banned on
4516 function definitions in ISO C; GCC used to used
4517 them for noreturn functions. */
4518 if (VOID_TYPE_P (type) && really_funcdef)
4519 pedwarn ("function definition has qualified void return type");
4520 else
4521 warning (OPT_Wignored_qualifiers,
4522 "type qualifiers ignored on function return type");
4524 type = c_build_qualified_type (type, type_quals);
4526 type_quals = TYPE_UNQUALIFIED;
4528 type = build_function_type (type, arg_types);
4529 declarator = declarator->declarator;
4531 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4532 the formal parameter list of this FUNCTION_TYPE to point to
4533 the FUNCTION_TYPE node itself. */
4535 tree link;
4537 for (link = arg_info->tags;
4538 link;
4539 link = TREE_CHAIN (link))
4540 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4542 break;
4544 case cdk_pointer:
4546 /* Merge any constancy or volatility into the target type
4547 for the pointer. */
4549 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4550 && type_quals)
4551 pedwarn ("ISO C forbids qualified function types");
4552 if (type_quals)
4553 type = c_build_qualified_type (type, type_quals);
4554 size_varies = 0;
4556 /* When the pointed-to type involves components of variable size,
4557 care must be taken to ensure that the size evaluation code is
4558 emitted early enough to dominate all the possible later uses
4559 and late enough for the variables on which it depends to have
4560 been assigned.
4562 This is expected to happen automatically when the pointed-to
4563 type has a name/declaration of it's own, but special attention
4564 is required if the type is anonymous.
4566 We handle the NORMAL and FIELD contexts here by attaching an
4567 artificial TYPE_DECL to such pointed-to type. This forces the
4568 sizes evaluation at a safe point and ensures it is not deferred
4569 until e.g. within a deeper conditional context.
4571 We expect nothing to be needed here for PARM or TYPENAME.
4572 Pushing a TYPE_DECL at this point for TYPENAME would actually
4573 be incorrect, as we might be in the middle of an expression
4574 with side effects on the pointed-to type size "arguments" prior
4575 to the pointer declaration point and the fake TYPE_DECL in the
4576 enclosing context would force the size evaluation prior to the
4577 side effects. */
4579 if (!TYPE_NAME (type)
4580 && (decl_context == NORMAL || decl_context == FIELD)
4581 && variably_modified_type_p (type, NULL_TREE))
4583 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4584 DECL_ARTIFICIAL (decl) = 1;
4585 pushdecl (decl);
4586 finish_decl (decl, NULL_TREE, NULL_TREE);
4587 TYPE_NAME (type) = decl;
4590 type = build_pointer_type (type);
4592 /* Process type qualifiers (such as const or volatile)
4593 that were given inside the `*'. */
4594 type_quals = declarator->u.pointer_quals;
4596 declarator = declarator->declarator;
4597 break;
4599 default:
4600 gcc_unreachable ();
4603 *decl_attrs = chainon (returned_attrs, *decl_attrs);
4605 /* Now TYPE has the actual type, apart from any qualifiers in
4606 TYPE_QUALS. */
4608 /* Check the type and width of a bit-field. */
4609 if (bitfield)
4610 check_bitfield_type_and_width (&type, width, orig_name);
4612 /* Did array size calculations overflow? */
4614 if (TREE_CODE (type) == ARRAY_TYPE
4615 && COMPLETE_TYPE_P (type)
4616 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4617 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4619 error ("size of array %qs is too large", name);
4620 /* If we proceed with the array type as it is, we'll eventually
4621 crash in tree_low_cst(). */
4622 type = error_mark_node;
4625 /* If this is declaring a typedef name, return a TYPE_DECL. */
4627 if (storage_class == csc_typedef)
4629 tree decl;
4630 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4631 && type_quals)
4632 pedwarn ("ISO C forbids qualified function types");
4633 if (type_quals)
4634 type = c_build_qualified_type (type, type_quals);
4635 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4636 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4637 if (declspecs->explicit_signed_p)
4638 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4639 if (declspecs->inline_p)
4640 pedwarn ("typedef %q+D declared %<inline%>", decl);
4641 return decl;
4644 /* If this is a type name (such as, in a cast or sizeof),
4645 compute the type and return it now. */
4647 if (decl_context == TYPENAME)
4649 /* Note that the grammar rejects storage classes in typenames
4650 and fields. */
4651 gcc_assert (storage_class == csc_none && !threadp
4652 && !declspecs->inline_p);
4653 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4654 && type_quals)
4655 pedwarn ("ISO C forbids const or volatile function types");
4656 if (type_quals)
4657 type = c_build_qualified_type (type, type_quals);
4658 return type;
4661 if (pedantic && decl_context == FIELD
4662 && variably_modified_type_p (type, NULL_TREE))
4664 /* C99 6.7.2.1p8 */
4665 pedwarn ("a member of a structure or union cannot have a variably modified type");
4668 /* Aside from typedefs and type names (handle above),
4669 `void' at top level (not within pointer)
4670 is allowed only in public variables.
4671 We don't complain about parms either, but that is because
4672 a better error message can be made later. */
4674 if (VOID_TYPE_P (type) && decl_context != PARM
4675 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4676 && (storage_class == csc_extern
4677 || (current_scope == file_scope
4678 && !(storage_class == csc_static
4679 || storage_class == csc_register)))))
4681 error ("variable or field %qs declared void", name);
4682 type = integer_type_node;
4685 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4686 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4689 tree decl;
4691 if (decl_context == PARM)
4693 tree promoted_type;
4695 /* A parameter declared as an array of T is really a pointer to T.
4696 One declared as a function is really a pointer to a function. */
4698 if (TREE_CODE (type) == ARRAY_TYPE)
4700 /* Transfer const-ness of array into that of type pointed to. */
4701 type = TREE_TYPE (type);
4702 if (type_quals)
4703 type = c_build_qualified_type (type, type_quals);
4704 type = build_pointer_type (type);
4705 type_quals = array_ptr_quals;
4706 if (type_quals)
4707 type = c_build_qualified_type (type, type_quals);
4709 /* We don't yet implement attributes in this context. */
4710 if (array_ptr_attrs != NULL_TREE)
4711 warning (OPT_Wattributes,
4712 "attributes in parameter array declarator ignored");
4714 size_varies = 0;
4716 else if (TREE_CODE (type) == FUNCTION_TYPE)
4718 if (pedantic && type_quals)
4719 pedwarn ("ISO C forbids qualified function types");
4720 if (type_quals)
4721 type = c_build_qualified_type (type, type_quals);
4722 type = build_pointer_type (type);
4723 type_quals = TYPE_UNQUALIFIED;
4725 else if (type_quals)
4726 type = c_build_qualified_type (type, type_quals);
4728 decl = build_decl (PARM_DECL, declarator->u.id, type);
4729 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4730 if (size_varies)
4731 C_DECL_VARIABLE_SIZE (decl) = 1;
4733 /* Compute the type actually passed in the parmlist,
4734 for the case where there is no prototype.
4735 (For example, shorts and chars are passed as ints.)
4736 When there is a prototype, this is overridden later. */
4738 if (type == error_mark_node)
4739 promoted_type = type;
4740 else
4741 promoted_type = c_type_promotes_to (type);
4743 DECL_ARG_TYPE (decl) = promoted_type;
4744 if (declspecs->inline_p)
4745 pedwarn ("parameter %q+D declared %<inline%>", decl);
4747 else if (decl_context == FIELD)
4749 /* Note that the grammar rejects storage classes in typenames
4750 and fields. */
4751 gcc_assert (storage_class == csc_none && !threadp
4752 && !declspecs->inline_p);
4754 /* Structure field. It may not be a function. */
4756 if (TREE_CODE (type) == FUNCTION_TYPE)
4758 error ("field %qs declared as a function", name);
4759 type = build_pointer_type (type);
4761 else if (TREE_CODE (type) != ERROR_MARK
4762 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4764 error ("field %qs has incomplete type", name);
4765 type = error_mark_node;
4767 type = c_build_qualified_type (type, type_quals);
4768 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4769 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4770 DECL_NONADDRESSABLE_P (decl) = bitfield;
4771 if (bitfield && !declarator->u.id)
4772 TREE_NO_WARNING (decl) = 1;
4774 if (size_varies)
4775 C_DECL_VARIABLE_SIZE (decl) = 1;
4777 else if (TREE_CODE (type) == FUNCTION_TYPE)
4779 if (storage_class == csc_register || threadp)
4781 error ("invalid storage class for function %qs", name);
4783 else if (current_scope != file_scope)
4785 /* Function declaration not at file scope. Storage
4786 classes other than `extern' are not allowed, C99
4787 6.7.1p5, and `extern' makes no difference. However,
4788 GCC allows 'auto', perhaps with 'inline', to support
4789 nested functions. */
4790 if (storage_class == csc_auto)
4792 if (pedantic)
4793 pedwarn ("invalid storage class for function %qs", name);
4795 else if (storage_class == csc_static)
4797 error ("invalid storage class for function %qs", name);
4798 if (funcdef_flag)
4799 storage_class = declspecs->storage_class = csc_none;
4800 else
4801 return 0;
4805 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4806 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4807 decl = build_decl_attribute_variant (decl, decl_attr);
4809 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4810 pedwarn ("ISO C forbids qualified function types");
4812 /* GNU C interprets a volatile-qualified function type to indicate
4813 that the function does not return. */
4814 if ((type_quals & TYPE_QUAL_VOLATILE)
4815 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4816 warning (0, "%<noreturn%> function returns non-void value");
4818 /* Every function declaration is an external reference
4819 (DECL_EXTERNAL) except for those which are not at file
4820 scope and are explicitly declared "auto". This is
4821 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4822 GCC to signify a forward declaration of a nested function. */
4823 if (storage_class == csc_auto && current_scope != file_scope)
4824 DECL_EXTERNAL (decl) = 0;
4825 /* In C99, a function which is declared 'inline' with 'extern'
4826 is not an external reference (which is confusing). It
4827 means that the later definition of the function must be output
4828 in this file, C99 6.7.4p6. In GNU C89, a function declared
4829 'extern inline' is an external reference. */
4830 else if (declspecs->inline_p && storage_class != csc_static)
4831 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
4832 == flag_gnu89_inline);
4833 else
4834 DECL_EXTERNAL (decl) = !initialized;
4836 /* Record absence of global scope for `static' or `auto'. */
4837 TREE_PUBLIC (decl)
4838 = !(storage_class == csc_static || storage_class == csc_auto);
4840 /* For a function definition, record the argument information
4841 block where store_parm_decls will look for it. */
4842 if (funcdef_flag)
4843 current_function_arg_info = arg_info;
4845 if (declspecs->default_int_p)
4846 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4848 /* Record presence of `inline', if it is reasonable. */
4849 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4851 if (declspecs->inline_p)
4852 pedwarn ("cannot inline function %<main%>");
4854 else if (declspecs->inline_p)
4856 /* Record that the function is declared `inline'. */
4857 DECL_DECLARED_INLINE_P (decl) = 1;
4859 /* Do not mark bare declarations as DECL_INLINE. Doing so
4860 in the presence of multiple declarations can result in
4861 the abstract origin pointing between the declarations,
4862 which will confuse dwarf2out. */
4863 if (initialized)
4864 DECL_INLINE (decl) = 1;
4866 /* If -finline-functions, assume it can be inlined. This does
4867 two things: let the function be deferred until it is actually
4868 needed, and let dwarf2 know that the function is inlinable. */
4869 else if (flag_inline_trees == 2 && initialized)
4870 DECL_INLINE (decl) = 1;
4872 else
4874 /* It's a variable. */
4875 /* An uninitialized decl with `extern' is a reference. */
4876 int extern_ref = !initialized && storage_class == csc_extern;
4878 type = c_build_qualified_type (type, type_quals);
4880 /* C99 6.2.2p7: It is invalid (compile-time undefined
4881 behavior) to create an 'extern' declaration for a
4882 variable if there is a global declaration that is
4883 'static' and the global declaration is not visible.
4884 (If the static declaration _is_ currently visible,
4885 the 'extern' declaration is taken to refer to that decl.) */
4886 if (extern_ref && current_scope != file_scope)
4888 tree global_decl = identifier_global_value (declarator->u.id);
4889 tree visible_decl = lookup_name (declarator->u.id);
4891 if (global_decl
4892 && global_decl != visible_decl
4893 && TREE_CODE (global_decl) == VAR_DECL
4894 && !TREE_PUBLIC (global_decl))
4895 error ("variable previously declared %<static%> redeclared "
4896 "%<extern%>");
4899 decl = build_decl (VAR_DECL, declarator->u.id, type);
4900 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4901 if (size_varies)
4902 C_DECL_VARIABLE_SIZE (decl) = 1;
4904 if (declspecs->inline_p)
4905 pedwarn ("variable %q+D declared %<inline%>", decl);
4907 /* At file scope, an initialized extern declaration may follow
4908 a static declaration. In that case, DECL_EXTERNAL will be
4909 reset later in start_decl. */
4910 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4912 /* At file scope, the presence of a `static' or `register' storage
4913 class specifier, or the absence of all storage class specifiers
4914 makes this declaration a definition (perhaps tentative). Also,
4915 the absence of `static' makes it public. */
4916 if (current_scope == file_scope)
4918 TREE_PUBLIC (decl) = storage_class != csc_static;
4919 TREE_STATIC (decl) = !extern_ref;
4921 /* Not at file scope, only `static' makes a static definition. */
4922 else
4924 TREE_STATIC (decl) = (storage_class == csc_static);
4925 TREE_PUBLIC (decl) = extern_ref;
4928 if (threadp)
4929 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
4932 if (storage_class == csc_extern
4933 && variably_modified_type_p (type, NULL_TREE))
4935 /* C99 6.7.5.2p2 */
4936 error ("object with variably modified type must have no linkage");
4939 /* Record `register' declaration for warnings on &
4940 and in case doing stupid register allocation. */
4942 if (storage_class == csc_register)
4944 C_DECL_REGISTER (decl) = 1;
4945 DECL_REGISTER (decl) = 1;
4948 /* Record constancy and volatility. */
4949 c_apply_type_quals_to_decl (type_quals, decl);
4951 /* If a type has volatile components, it should be stored in memory.
4952 Otherwise, the fact that those components are volatile
4953 will be ignored, and would even crash the compiler.
4954 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
4955 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
4956 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
4957 || TREE_CODE (decl) == RESULT_DECL))
4959 /* It is not an error for a structure with volatile fields to
4960 be declared register, but reset DECL_REGISTER since it
4961 cannot actually go in a register. */
4962 int was_reg = C_DECL_REGISTER (decl);
4963 C_DECL_REGISTER (decl) = 0;
4964 DECL_REGISTER (decl) = 0;
4965 c_mark_addressable (decl);
4966 C_DECL_REGISTER (decl) = was_reg;
4969 /* This is the earliest point at which we might know the assembler
4970 name of a variable. Thus, if it's known before this, die horribly. */
4971 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4973 return decl;
4977 /* Decode the parameter-list info for a function type or function definition.
4978 The argument is the value returned by `get_parm_info' (or made in c-parse.c
4979 if there is an identifier list instead of a parameter decl list).
4980 These two functions are separate because when a function returns
4981 or receives functions then each is called multiple times but the order
4982 of calls is different. The last call to `grokparms' is always the one
4983 that contains the formal parameter names of a function definition.
4985 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4987 FUNCDEF_FLAG is true for a function definition, false for
4988 a mere declaration. A nonempty identifier-list gets an error message
4989 when FUNCDEF_FLAG is false. */
4991 static tree
4992 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4994 tree arg_types = arg_info->types;
4996 if (funcdef_flag && arg_info->had_vla_unspec)
4998 /* A function definition isn't function prototype scope C99 6.2.1p4. */
4999 /* C99 6.7.5.2p4 */
5000 error ("%<[*]%> not allowed in other than function prototype scope");
5003 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5004 warning (OPT_Wstrict_prototypes,
5005 "function declaration isn%'t a prototype");
5007 if (arg_types == error_mark_node)
5008 return 0; /* don't set TYPE_ARG_TYPES in this case */
5010 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5012 if (!funcdef_flag)
5013 pedwarn ("parameter names (without types) in function declaration");
5015 arg_info->parms = arg_info->types;
5016 arg_info->types = 0;
5017 return 0;
5019 else
5021 tree parm, type, typelt;
5022 unsigned int parmno;
5024 /* If there is a parameter of incomplete type in a definition,
5025 this is an error. In a declaration this is valid, and a
5026 struct or union type may be completed later, before any calls
5027 or definition of the function. In the case where the tag was
5028 first declared within the parameter list, a warning has
5029 already been given. If a parameter has void type, then
5030 however the function cannot be defined or called, so
5031 warn. */
5033 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5034 parm;
5035 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5037 type = TREE_VALUE (typelt);
5038 if (type == error_mark_node)
5039 continue;
5041 if (!COMPLETE_TYPE_P (type))
5043 if (funcdef_flag)
5045 if (DECL_NAME (parm))
5046 error ("parameter %u (%q+D) has incomplete type",
5047 parmno, parm);
5048 else
5049 error ("%Jparameter %u has incomplete type",
5050 parm, parmno);
5052 TREE_VALUE (typelt) = error_mark_node;
5053 TREE_TYPE (parm) = error_mark_node;
5055 else if (VOID_TYPE_P (type))
5057 if (DECL_NAME (parm))
5058 warning (0, "parameter %u (%q+D) has void type",
5059 parmno, parm);
5060 else
5061 warning (0, "%Jparameter %u has void type",
5062 parm, parmno);
5066 if (DECL_NAME (parm) && TREE_USED (parm))
5067 warn_if_shadowing (parm);
5069 return arg_types;
5073 /* Take apart the current scope and return a c_arg_info structure with
5074 info on a parameter list just parsed.
5076 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5078 ELLIPSIS being true means the argument list ended in '...' so don't
5079 append a sentinel (void_list_node) to the end of the type-list. */
5081 struct c_arg_info *
5082 get_parm_info (bool ellipsis)
5084 struct c_binding *b = current_scope->bindings;
5085 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5086 struct c_arg_info);
5087 tree parms = 0;
5088 tree tags = 0;
5089 tree types = 0;
5090 tree others = 0;
5092 static bool explained_incomplete_types = false;
5093 bool gave_void_only_once_err = false;
5095 arg_info->parms = 0;
5096 arg_info->tags = 0;
5097 arg_info->types = 0;
5098 arg_info->others = 0;
5099 arg_info->pending_sizes = 0;
5100 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5102 /* The bindings in this scope must not get put into a block.
5103 We will take care of deleting the binding nodes. */
5104 current_scope->bindings = 0;
5106 /* This function is only called if there was *something* on the
5107 parameter list. */
5108 gcc_assert (b);
5110 /* A parameter list consisting solely of 'void' indicates that the
5111 function takes no arguments. But if the 'void' is qualified
5112 (by 'const' or 'volatile'), or has a storage class specifier
5113 ('register'), then the behavior is undefined; issue an error.
5114 Typedefs for 'void' are OK (see DR#157). */
5115 if (b->prev == 0 /* one binding */
5116 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5117 && !DECL_NAME (b->decl) /* anonymous */
5118 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5120 if (TREE_THIS_VOLATILE (b->decl)
5121 || TREE_READONLY (b->decl)
5122 || C_DECL_REGISTER (b->decl))
5123 error ("%<void%> as only parameter may not be qualified");
5125 /* There cannot be an ellipsis. */
5126 if (ellipsis)
5127 error ("%<void%> must be the only parameter");
5129 arg_info->types = void_list_node;
5130 return arg_info;
5133 if (!ellipsis)
5134 types = void_list_node;
5136 /* Break up the bindings list into parms, tags, types, and others;
5137 apply sanity checks; purge the name-to-decl bindings. */
5138 while (b)
5140 tree decl = b->decl;
5141 tree type = TREE_TYPE (decl);
5142 const char *keyword;
5144 switch (TREE_CODE (decl))
5146 case PARM_DECL:
5147 if (b->id)
5149 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5150 I_SYMBOL_BINDING (b->id) = b->shadowed;
5153 /* Check for forward decls that never got their actual decl. */
5154 if (TREE_ASM_WRITTEN (decl))
5155 error ("parameter %q+D has just a forward declaration", decl);
5156 /* Check for (..., void, ...) and issue an error. */
5157 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5159 if (!gave_void_only_once_err)
5161 error ("%<void%> must be the only parameter");
5162 gave_void_only_once_err = true;
5165 else
5167 /* Valid parameter, add it to the list. */
5168 TREE_CHAIN (decl) = parms;
5169 parms = decl;
5171 /* Since there is a prototype, args are passed in their
5172 declared types. The back end may override this later. */
5173 DECL_ARG_TYPE (decl) = type;
5174 types = tree_cons (0, type, types);
5176 break;
5178 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5179 case UNION_TYPE: keyword = "union"; goto tag;
5180 case RECORD_TYPE: keyword = "struct"; goto tag;
5181 tag:
5182 /* Types may not have tag-names, in which case the type
5183 appears in the bindings list with b->id NULL. */
5184 if (b->id)
5186 gcc_assert (I_TAG_BINDING (b->id) == b);
5187 I_TAG_BINDING (b->id) = b->shadowed;
5190 /* Warn about any struct, union or enum tags defined in a
5191 parameter list. The scope of such types is limited to
5192 the parameter list, which is rarely if ever desirable
5193 (it's impossible to call such a function with type-
5194 correct arguments). An anonymous union parm type is
5195 meaningful as a GNU extension, so don't warn for that. */
5196 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5198 if (b->id)
5199 /* The %s will be one of 'struct', 'union', or 'enum'. */
5200 warning (0, "%<%s %E%> declared inside parameter list",
5201 keyword, b->id);
5202 else
5203 /* The %s will be one of 'struct', 'union', or 'enum'. */
5204 warning (0, "anonymous %s declared inside parameter list",
5205 keyword);
5207 if (!explained_incomplete_types)
5209 warning (0, "its scope is only this definition or declaration,"
5210 " which is probably not what you want");
5211 explained_incomplete_types = true;
5215 tags = tree_cons (b->id, decl, tags);
5216 break;
5218 case CONST_DECL:
5219 case TYPE_DECL:
5220 case FUNCTION_DECL:
5221 /* CONST_DECLs appear here when we have an embedded enum,
5222 and TYPE_DECLs appear here when we have an embedded struct
5223 or union. No warnings for this - we already warned about the
5224 type itself. FUNCTION_DECLs appear when there is an implicit
5225 function declaration in the parameter list. */
5227 TREE_CHAIN (decl) = others;
5228 others = decl;
5229 /* fall through */
5231 case ERROR_MARK:
5232 /* error_mark_node appears here when we have an undeclared
5233 variable. Just throw it away. */
5234 if (b->id)
5236 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5237 I_SYMBOL_BINDING (b->id) = b->shadowed;
5239 break;
5241 /* Other things that might be encountered. */
5242 case LABEL_DECL:
5243 case VAR_DECL:
5244 default:
5245 gcc_unreachable ();
5248 b = free_binding_and_advance (b);
5251 arg_info->parms = parms;
5252 arg_info->tags = tags;
5253 arg_info->types = types;
5254 arg_info->others = others;
5255 arg_info->pending_sizes = get_pending_sizes ();
5256 return arg_info;
5259 /* Get the struct, enum or union (CODE says which) with tag NAME.
5260 Define the tag as a forward-reference if it is not defined.
5261 Return a c_typespec structure for the type specifier. */
5263 struct c_typespec
5264 parser_xref_tag (enum tree_code code, tree name)
5266 struct c_typespec ret;
5267 /* If a cross reference is requested, look up the type
5268 already defined for this tag and return it. */
5270 tree ref = lookup_tag (code, name, 0);
5271 /* If this is the right type of tag, return what we found.
5272 (This reference will be shadowed by shadow_tag later if appropriate.)
5273 If this is the wrong type of tag, do not return it. If it was the
5274 wrong type in the same scope, we will have had an error
5275 message already; if in a different scope and declaring
5276 a name, pending_xref_error will give an error message; but if in a
5277 different scope and not declaring a name, this tag should
5278 shadow the previous declaration of a different type of tag, and
5279 this would not work properly if we return the reference found.
5280 (For example, with "struct foo" in an outer scope, "union foo;"
5281 must shadow that tag with a new one of union type.) */
5282 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5283 if (ref && TREE_CODE (ref) == code)
5285 ret.spec = ref;
5286 return ret;
5289 /* If no such tag is yet defined, create a forward-reference node
5290 and record it as the "definition".
5291 When a real declaration of this type is found,
5292 the forward-reference will be altered into a real type. */
5294 ref = make_node (code);
5295 if (code == ENUMERAL_TYPE)
5297 /* Give the type a default layout like unsigned int
5298 to avoid crashing if it does not get defined. */
5299 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5300 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5301 TYPE_USER_ALIGN (ref) = 0;
5302 TYPE_UNSIGNED (ref) = 1;
5303 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5304 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5305 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5308 pushtag (name, ref);
5310 ret.spec = ref;
5311 return ret;
5314 /* Get the struct, enum or union (CODE says which) with tag NAME.
5315 Define the tag as a forward-reference if it is not defined.
5316 Return a tree for the type. */
5318 tree
5319 xref_tag (enum tree_code code, tree name)
5321 return parser_xref_tag (code, name).spec;
5324 /* Make sure that the tag NAME is defined *in the current scope*
5325 at least as a forward reference.
5326 CODE says which kind of tag NAME ought to be. */
5328 tree
5329 start_struct (enum tree_code code, tree name)
5331 /* If there is already a tag defined at this scope
5332 (as a forward reference), just return it. */
5334 tree ref = 0;
5336 if (name != 0)
5337 ref = lookup_tag (code, name, 1);
5338 if (ref && TREE_CODE (ref) == code)
5340 if (TYPE_SIZE (ref))
5342 if (code == UNION_TYPE)
5343 error ("redefinition of %<union %E%>", name);
5344 else
5345 error ("redefinition of %<struct %E%>", name);
5347 else if (C_TYPE_BEING_DEFINED (ref))
5349 if (code == UNION_TYPE)
5350 error ("nested redefinition of %<union %E%>", name);
5351 else
5352 error ("nested redefinition of %<struct %E%>", name);
5353 /* Don't create structures that contain themselves. */
5354 ref = NULL_TREE;
5358 /* Otherwise create a forward-reference just so the tag is in scope. */
5360 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5362 ref = make_node (code);
5363 pushtag (name, ref);
5366 C_TYPE_BEING_DEFINED (ref) = 1;
5367 TYPE_PACKED (ref) = flag_pack_struct;
5368 return ref;
5371 /* Process the specs, declarator and width (NULL if omitted)
5372 of a structure component, returning a FIELD_DECL node.
5373 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5374 DECL_ATTRS is as for grokdeclarator.
5376 This is done during the parsing of the struct declaration.
5377 The FIELD_DECL nodes are chained together and the lot of them
5378 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5380 tree
5381 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5382 tree width, tree *decl_attrs)
5384 tree value;
5386 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5387 && width == NULL_TREE)
5389 /* This is an unnamed decl.
5391 If we have something of the form "union { list } ;" then this
5392 is the anonymous union extension. Similarly for struct.
5394 If this is something of the form "struct foo;", then
5395 If MS extensions are enabled, this is handled as an
5396 anonymous struct.
5397 Otherwise this is a forward declaration of a structure tag.
5399 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5400 If MS extensions are enabled and foo names a structure, then
5401 again this is an anonymous struct.
5402 Otherwise this is an error.
5404 Oh what a horrid tangled web we weave. I wonder if MS consciously
5405 took this from Plan 9 or if it was an accident of implementation
5406 that took root before someone noticed the bug... */
5408 tree type = declspecs->type;
5409 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5410 || TREE_CODE (type) == UNION_TYPE);
5411 bool ok = false;
5413 if (type_ok
5414 && (flag_ms_extensions || !declspecs->typedef_p))
5416 if (flag_ms_extensions)
5417 ok = true;
5418 else if (flag_iso)
5419 ok = false;
5420 else if (TYPE_NAME (type) == NULL)
5421 ok = true;
5422 else
5423 ok = false;
5425 if (!ok)
5427 pedwarn ("declaration does not declare anything");
5428 return NULL_TREE;
5430 if (pedantic)
5431 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5434 value = grokdeclarator (declarator, declspecs, FIELD, false,
5435 width ? &width : NULL, decl_attrs,
5436 DEPRECATED_NORMAL);
5438 finish_decl (value, NULL_TREE, NULL_TREE);
5439 DECL_INITIAL (value) = width;
5441 return value;
5444 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5445 the list such that this does not present a problem later. */
5447 static void
5448 detect_field_duplicates (tree fieldlist)
5450 tree x, y;
5451 int timeout = 10;
5453 /* First, see if there are more than "a few" fields.
5454 This is trivially true if there are zero or one fields. */
5455 if (!fieldlist)
5456 return;
5457 x = TREE_CHAIN (fieldlist);
5458 if (!x)
5459 return;
5460 do {
5461 timeout--;
5462 x = TREE_CHAIN (x);
5463 } while (timeout > 0 && x);
5465 /* If there were "few" fields, avoid the overhead of allocating
5466 a hash table. Instead just do the nested traversal thing. */
5467 if (timeout > 0)
5469 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5470 if (DECL_NAME (x))
5472 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5473 if (DECL_NAME (y) == DECL_NAME (x))
5475 error ("duplicate member %q+D", x);
5476 DECL_NAME (x) = NULL_TREE;
5480 else
5482 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5483 void **slot;
5485 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5486 if ((y = DECL_NAME (x)) != 0)
5488 slot = htab_find_slot (htab, y, INSERT);
5489 if (*slot)
5491 error ("duplicate member %q+D", x);
5492 DECL_NAME (x) = NULL_TREE;
5494 *slot = y;
5497 htab_delete (htab);
5501 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5502 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5503 ATTRIBUTES are attributes to be applied to the structure. */
5505 tree
5506 finish_struct (tree t, tree fieldlist, tree attributes)
5508 tree x;
5509 bool toplevel = file_scope == current_scope;
5510 int saw_named_field;
5512 /* If this type was previously laid out as a forward reference,
5513 make sure we lay it out again. */
5515 TYPE_SIZE (t) = 0;
5517 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5519 if (pedantic)
5521 for (x = fieldlist; x; x = TREE_CHAIN (x))
5522 if (DECL_NAME (x) != 0)
5523 break;
5525 if (x == 0)
5527 if (TREE_CODE (t) == UNION_TYPE)
5529 if (fieldlist)
5530 pedwarn ("union has no named members");
5531 else
5532 pedwarn ("union has no members");
5534 else
5536 if (fieldlist)
5537 pedwarn ("struct has no named members");
5538 else
5539 pedwarn ("struct has no members");
5544 /* Install struct as DECL_CONTEXT of each field decl.
5545 Also process specified field sizes, found in the DECL_INITIAL,
5546 storing 0 there after the type has been changed to precision equal
5547 to its width, rather than the precision of the specified standard
5548 type. (Correct layout requires the original type to have been preserved
5549 until now.) */
5551 saw_named_field = 0;
5552 for (x = fieldlist; x; x = TREE_CHAIN (x))
5554 if (TREE_TYPE (x) == error_mark_node)
5555 continue;
5557 DECL_CONTEXT (x) = t;
5559 if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
5560 DECL_PACKED (x) = 1;
5562 /* If any field is const, the structure type is pseudo-const. */
5563 if (TREE_READONLY (x))
5564 C_TYPE_FIELDS_READONLY (t) = 1;
5565 else
5567 /* A field that is pseudo-const makes the structure likewise. */
5568 tree t1 = TREE_TYPE (x);
5569 while (TREE_CODE (t1) == ARRAY_TYPE)
5570 t1 = TREE_TYPE (t1);
5571 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5572 && C_TYPE_FIELDS_READONLY (t1))
5573 C_TYPE_FIELDS_READONLY (t) = 1;
5576 /* Any field that is volatile means variables of this type must be
5577 treated in some ways as volatile. */
5578 if (TREE_THIS_VOLATILE (x))
5579 C_TYPE_FIELDS_VOLATILE (t) = 1;
5581 /* Any field of nominal variable size implies structure is too. */
5582 if (C_DECL_VARIABLE_SIZE (x))
5583 C_TYPE_VARIABLE_SIZE (t) = 1;
5585 if (DECL_INITIAL (x))
5587 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5588 DECL_SIZE (x) = bitsize_int (width);
5589 DECL_BIT_FIELD (x) = 1;
5590 SET_DECL_C_BIT_FIELD (x);
5593 /* Detect flexible array member in an invalid context. */
5594 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5595 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5596 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5597 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5599 if (TREE_CODE (t) == UNION_TYPE)
5601 error ("%Jflexible array member in union", x);
5602 TREE_TYPE (x) = error_mark_node;
5604 else if (TREE_CHAIN (x) != NULL_TREE)
5606 error ("%Jflexible array member not at end of struct", x);
5607 TREE_TYPE (x) = error_mark_node;
5609 else if (!saw_named_field)
5611 error ("%Jflexible array member in otherwise empty struct", x);
5612 TREE_TYPE (x) = error_mark_node;
5616 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5617 && flexible_array_type_p (TREE_TYPE (x)))
5618 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5620 if (DECL_NAME (x))
5621 saw_named_field = 1;
5624 detect_field_duplicates (fieldlist);
5626 /* Now we have the nearly final fieldlist. Record it,
5627 then lay out the structure or union (including the fields). */
5629 TYPE_FIELDS (t) = fieldlist;
5631 layout_type (t);
5633 /* Give bit-fields their proper types. */
5635 tree *fieldlistp = &fieldlist;
5636 while (*fieldlistp)
5637 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5638 && TREE_TYPE (*fieldlistp) != error_mark_node)
5640 unsigned HOST_WIDE_INT width
5641 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5642 tree type = TREE_TYPE (*fieldlistp);
5643 if (width != TYPE_PRECISION (type))
5645 TREE_TYPE (*fieldlistp)
5646 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5647 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5649 DECL_INITIAL (*fieldlistp) = 0;
5651 else
5652 fieldlistp = &TREE_CHAIN (*fieldlistp);
5655 /* Now we have the truly final field list.
5656 Store it in this type and in the variants. */
5658 TYPE_FIELDS (t) = fieldlist;
5660 /* If there are lots of fields, sort so we can look through them fast.
5661 We arbitrarily consider 16 or more elts to be "a lot". */
5664 int len = 0;
5666 for (x = fieldlist; x; x = TREE_CHAIN (x))
5668 if (len > 15 || DECL_NAME (x) == NULL)
5669 break;
5670 len += 1;
5673 if (len > 15)
5675 tree *field_array;
5676 struct lang_type *space;
5677 struct sorted_fields_type *space2;
5679 len += list_length (x);
5681 /* Use the same allocation policy here that make_node uses, to
5682 ensure that this lives as long as the rest of the struct decl.
5683 All decls in an inline function need to be saved. */
5685 space = GGC_CNEW (struct lang_type);
5686 space2 = GGC_NEWVAR (struct sorted_fields_type,
5687 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5689 len = 0;
5690 space->s = space2;
5691 field_array = &space2->elts[0];
5692 for (x = fieldlist; x; x = TREE_CHAIN (x))
5694 field_array[len++] = x;
5696 /* If there is anonymous struct or union, break out of the loop. */
5697 if (DECL_NAME (x) == NULL)
5698 break;
5700 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5701 if (x == NULL)
5703 TYPE_LANG_SPECIFIC (t) = space;
5704 TYPE_LANG_SPECIFIC (t)->s->len = len;
5705 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5706 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5711 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5713 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5714 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5715 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5716 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5717 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5720 /* If this was supposed to be a transparent union, but we can't
5721 make it one, warn and turn off the flag. */
5722 if (TREE_CODE (t) == UNION_TYPE
5723 && TYPE_TRANSPARENT_UNION (t)
5724 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5726 TYPE_TRANSPARENT_UNION (t) = 0;
5727 warning (0, "union cannot be made transparent");
5730 /* If this structure or union completes the type of any previous
5731 variable declaration, lay it out and output its rtl. */
5732 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5734 x = TREE_CHAIN (x))
5736 tree decl = TREE_VALUE (x);
5737 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5738 layout_array_type (TREE_TYPE (decl));
5739 if (TREE_CODE (decl) != TYPE_DECL)
5741 layout_decl (decl, 0);
5742 if (c_dialect_objc ())
5743 objc_check_decl (decl);
5744 rest_of_decl_compilation (decl, toplevel, 0);
5745 if (!toplevel)
5746 expand_decl (decl);
5749 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5751 /* Finish debugging output for this type. */
5752 rest_of_type_compilation (t, toplevel);
5754 /* If we're inside a function proper, i.e. not file-scope and not still
5755 parsing parameters, then arrange for the size of a variable sized type
5756 to be bound now. */
5757 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5758 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5760 return t;
5763 /* Lay out the type T, and its element type, and so on. */
5765 static void
5766 layout_array_type (tree t)
5768 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5769 layout_array_type (TREE_TYPE (t));
5770 layout_type (t);
5773 /* Begin compiling the definition of an enumeration type.
5774 NAME is its name (or null if anonymous).
5775 Returns the type object, as yet incomplete.
5776 Also records info about it so that build_enumerator
5777 may be used to declare the individual values as they are read. */
5779 tree
5780 start_enum (struct c_enum_contents *the_enum, tree name)
5782 tree enumtype = 0;
5784 /* If this is the real definition for a previous forward reference,
5785 fill in the contents in the same object that used to be the
5786 forward reference. */
5788 if (name != 0)
5789 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5791 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5793 enumtype = make_node (ENUMERAL_TYPE);
5794 pushtag (name, enumtype);
5797 if (C_TYPE_BEING_DEFINED (enumtype))
5798 error ("nested redefinition of %<enum %E%>", name);
5800 C_TYPE_BEING_DEFINED (enumtype) = 1;
5802 if (TYPE_VALUES (enumtype) != 0)
5804 /* This enum is a named one that has been declared already. */
5805 error ("redeclaration of %<enum %E%>", name);
5807 /* Completely replace its old definition.
5808 The old enumerators remain defined, however. */
5809 TYPE_VALUES (enumtype) = 0;
5812 the_enum->enum_next_value = integer_zero_node;
5813 the_enum->enum_overflow = 0;
5815 if (flag_short_enums)
5816 TYPE_PACKED (enumtype) = 1;
5818 return enumtype;
5821 /* After processing and defining all the values of an enumeration type,
5822 install their decls in the enumeration type and finish it off.
5823 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5824 and ATTRIBUTES are the specified attributes.
5825 Returns ENUMTYPE. */
5827 tree
5828 finish_enum (tree enumtype, tree values, tree attributes)
5830 tree pair, tem;
5831 tree minnode = 0, maxnode = 0;
5832 int precision, unsign;
5833 bool toplevel = (file_scope == current_scope);
5834 struct lang_type *lt;
5836 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5838 /* Calculate the maximum value of any enumerator in this type. */
5840 if (values == error_mark_node)
5841 minnode = maxnode = integer_zero_node;
5842 else
5844 minnode = maxnode = TREE_VALUE (values);
5845 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5847 tree value = TREE_VALUE (pair);
5848 if (tree_int_cst_lt (maxnode, value))
5849 maxnode = value;
5850 if (tree_int_cst_lt (value, minnode))
5851 minnode = value;
5855 /* Construct the final type of this enumeration. It is the same
5856 as one of the integral types - the narrowest one that fits, except
5857 that normally we only go as narrow as int - and signed iff any of
5858 the values are negative. */
5859 unsign = (tree_int_cst_sgn (minnode) >= 0);
5860 precision = MAX (min_precision (minnode, unsign),
5861 min_precision (maxnode, unsign));
5863 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5865 tem = c_common_type_for_size (precision, unsign);
5866 if (tem == NULL)
5868 warning (0, "enumeration values exceed range of largest integer");
5869 tem = long_long_integer_type_node;
5872 else
5873 tem = unsign ? unsigned_type_node : integer_type_node;
5875 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5876 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5877 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5878 TYPE_SIZE (enumtype) = 0;
5880 /* If the precision of the type was specific with an attribute and it
5881 was too small, give an error. Otherwise, use it. */
5882 if (TYPE_PRECISION (enumtype))
5884 if (precision > TYPE_PRECISION (enumtype))
5885 error ("specified mode too small for enumeral values");
5887 else
5888 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5890 layout_type (enumtype);
5892 if (values != error_mark_node)
5894 /* Change the type of the enumerators to be the enum type. We
5895 need to do this irrespective of the size of the enum, for
5896 proper type checking. Replace the DECL_INITIALs of the
5897 enumerators, and the value slots of the list, with copies
5898 that have the enum type; they cannot be modified in place
5899 because they may be shared (e.g. integer_zero_node) Finally,
5900 change the purpose slots to point to the names of the decls. */
5901 for (pair = values; pair; pair = TREE_CHAIN (pair))
5903 tree enu = TREE_PURPOSE (pair);
5904 tree ini = DECL_INITIAL (enu);
5906 TREE_TYPE (enu) = enumtype;
5908 /* The ISO C Standard mandates enumerators to have type int,
5909 even though the underlying type of an enum type is
5910 unspecified. Here we convert any enumerators that fit in
5911 an int to type int, to avoid promotions to unsigned types
5912 when comparing integers with enumerators that fit in the
5913 int range. When -pedantic is given, build_enumerator()
5914 would have already taken care of those that don't fit. */
5915 if (int_fits_type_p (ini, integer_type_node))
5916 tem = integer_type_node;
5917 else
5918 tem = enumtype;
5919 ini = convert (tem, ini);
5921 DECL_INITIAL (enu) = ini;
5922 TREE_PURPOSE (pair) = DECL_NAME (enu);
5923 TREE_VALUE (pair) = ini;
5926 TYPE_VALUES (enumtype) = values;
5929 /* Record the min/max values so that we can warn about bit-field
5930 enumerations that are too small for the values. */
5931 lt = GGC_CNEW (struct lang_type);
5932 lt->enum_min = minnode;
5933 lt->enum_max = maxnode;
5934 TYPE_LANG_SPECIFIC (enumtype) = lt;
5936 /* Fix up all variant types of this enum type. */
5937 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5939 if (tem == enumtype)
5940 continue;
5941 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5942 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5943 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5944 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5945 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5946 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5947 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5948 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5949 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5950 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5951 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5954 /* Finish debugging output for this type. */
5955 rest_of_type_compilation (enumtype, toplevel);
5957 return enumtype;
5960 /* Build and install a CONST_DECL for one value of the
5961 current enumeration type (one that was begun with start_enum).
5962 Return a tree-list containing the CONST_DECL and its value.
5963 Assignment of sequential values by default is handled here. */
5965 tree
5966 build_enumerator (struct c_enum_contents *the_enum, tree name, tree value)
5968 tree decl, type;
5970 /* Validate and default VALUE. */
5972 if (value != 0)
5974 /* Don't issue more errors for error_mark_node (i.e. an
5975 undeclared identifier) - just ignore the value expression. */
5976 if (value == error_mark_node)
5977 value = 0;
5978 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5979 || TREE_CODE (value) != INTEGER_CST)
5981 error ("enumerator value for %qE is not an integer constant", name);
5982 value = 0;
5984 else
5986 value = default_conversion (value);
5987 constant_expression_warning (value);
5991 /* Default based on previous value. */
5992 /* It should no longer be possible to have NON_LVALUE_EXPR
5993 in the default. */
5994 if (value == 0)
5996 value = the_enum->enum_next_value;
5997 if (the_enum->enum_overflow)
5998 error ("overflow in enumeration values");
6001 if (pedantic && !int_fits_type_p (value, integer_type_node))
6003 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
6004 /* XXX This causes -pedantic to change the meaning of the program.
6005 Remove? -zw 2004-03-15 */
6006 value = convert (integer_type_node, value);
6009 /* Set basis for default for next value. */
6010 the_enum->enum_next_value = build_binary_op (PLUS_EXPR, value,
6011 integer_one_node, 0);
6012 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
6014 /* Now create a declaration for the enum value name. */
6016 type = TREE_TYPE (value);
6017 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6018 TYPE_PRECISION (integer_type_node)),
6019 (TYPE_PRECISION (type)
6020 >= TYPE_PRECISION (integer_type_node)
6021 && TYPE_UNSIGNED (type)));
6023 decl = build_decl (CONST_DECL, name, type);
6024 DECL_INITIAL (decl) = convert (type, value);
6025 pushdecl (decl);
6027 return tree_cons (decl, value, NULL_TREE);
6031 /* Create the FUNCTION_DECL for a function definition.
6032 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6033 the declaration; they describe the function's name and the type it returns,
6034 but twisted together in a fashion that parallels the syntax of C.
6036 This function creates a binding context for the function body
6037 as well as setting up the FUNCTION_DECL in current_function_decl.
6039 Returns 1 on success. If the DECLARATOR is not suitable for a function
6040 (it defines a datum instead), we return 0, which tells
6041 yyparse to report a parse error. */
6044 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6045 tree attributes)
6047 tree decl1, old_decl;
6048 tree restype, resdecl;
6049 struct c_label_context_se *nstack_se;
6050 struct c_label_context_vm *nstack_vm;
6052 current_function_returns_value = 0; /* Assume, until we see it does. */
6053 current_function_returns_null = 0;
6054 current_function_returns_abnormally = 0;
6055 warn_about_return_type = 0;
6056 c_switch_stack = NULL;
6058 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6059 nstack_se->labels_def = NULL;
6060 nstack_se->labels_used = NULL;
6061 nstack_se->next = label_context_stack_se;
6062 label_context_stack_se = nstack_se;
6064 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6065 nstack_vm->labels_def = NULL;
6066 nstack_vm->labels_used = NULL;
6067 nstack_vm->scope = 0;
6068 nstack_vm->next = label_context_stack_vm;
6069 label_context_stack_vm = nstack_vm;
6071 /* Indicate no valid break/continue context by setting these variables
6072 to some non-null, non-label value. We'll notice and emit the proper
6073 error message in c_finish_bc_stmt. */
6074 c_break_label = c_cont_label = size_zero_node;
6076 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
6077 &attributes, DEPRECATED_NORMAL);
6079 /* If the declarator is not suitable for a function definition,
6080 cause a syntax error. */
6081 if (decl1 == 0)
6083 label_context_stack_se = label_context_stack_se->next;
6084 label_context_stack_vm = label_context_stack_vm->next;
6085 return 0;
6088 decl_attributes (&decl1, attributes, 0);
6090 if (DECL_DECLARED_INLINE_P (decl1)
6091 && DECL_UNINLINABLE (decl1)
6092 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6093 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6094 decl1);
6096 /* Handle gnu_inline attribute. */
6097 if (declspecs->inline_p
6098 && !flag_gnu89_inline
6099 && TREE_CODE (decl1) == FUNCTION_DECL
6100 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
6101 || current_function_decl))
6103 if (declspecs->storage_class != csc_static)
6104 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6107 announce_function (decl1);
6109 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6111 error ("return type is an incomplete type");
6112 /* Make it return void instead. */
6113 TREE_TYPE (decl1)
6114 = build_function_type (void_type_node,
6115 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6118 if (warn_about_return_type)
6119 pedwarn_c99 ("return type defaults to %<int%>");
6121 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6122 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6123 DECL_INITIAL (decl1) = error_mark_node;
6125 /* If this definition isn't a prototype and we had a prototype declaration
6126 before, copy the arg type info from that prototype. */
6127 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6128 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6129 old_decl = 0;
6130 current_function_prototype_locus = UNKNOWN_LOCATION;
6131 current_function_prototype_built_in = false;
6132 current_function_prototype_arg_types = NULL_TREE;
6133 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6135 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6136 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6137 TREE_TYPE (TREE_TYPE (old_decl))))
6139 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6140 TREE_TYPE (decl1));
6141 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6142 current_function_prototype_built_in
6143 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6144 current_function_prototype_arg_types
6145 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6147 if (TREE_PUBLIC (decl1))
6149 /* If there is an external prototype declaration of this
6150 function, record its location but do not copy information
6151 to this decl. This may be an invisible declaration
6152 (built-in or in a scope which has finished) or simply
6153 have more refined argument types than any declaration
6154 found above. */
6155 struct c_binding *b;
6156 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6157 if (B_IN_SCOPE (b, external_scope))
6158 break;
6159 if (b)
6161 tree ext_decl, ext_type;
6162 ext_decl = b->decl;
6163 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6164 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6165 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6166 TREE_TYPE (ext_type)))
6168 current_function_prototype_locus
6169 = DECL_SOURCE_LOCATION (ext_decl);
6170 current_function_prototype_built_in
6171 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6172 current_function_prototype_arg_types
6173 = TYPE_ARG_TYPES (ext_type);
6179 /* Optionally warn of old-fashioned def with no previous prototype. */
6180 if (warn_strict_prototypes
6181 && old_decl != error_mark_node
6182 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6183 && C_DECL_ISNT_PROTOTYPE (old_decl))
6184 warning (OPT_Wstrict_prototypes,
6185 "function declaration isn%'t a prototype");
6186 /* Optionally warn of any global def with no previous prototype. */
6187 else if (warn_missing_prototypes
6188 && old_decl != error_mark_node
6189 && TREE_PUBLIC (decl1)
6190 && !MAIN_NAME_P (DECL_NAME (decl1))
6191 && C_DECL_ISNT_PROTOTYPE (old_decl))
6192 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6193 /* Optionally warn of any def with no previous prototype
6194 if the function has already been used. */
6195 else if (warn_missing_prototypes
6196 && old_decl != 0
6197 && old_decl != error_mark_node
6198 && TREE_USED (old_decl)
6199 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6200 warning (OPT_Wmissing_prototypes,
6201 "%q+D was used with no prototype before its definition", decl1);
6202 /* Optionally warn of any global def with no previous declaration. */
6203 else if (warn_missing_declarations
6204 && TREE_PUBLIC (decl1)
6205 && old_decl == 0
6206 && !MAIN_NAME_P (DECL_NAME (decl1)))
6207 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6208 decl1);
6209 /* Optionally warn of any def with no previous declaration
6210 if the function has already been used. */
6211 else if (warn_missing_declarations
6212 && old_decl != 0
6213 && old_decl != error_mark_node
6214 && TREE_USED (old_decl)
6215 && C_DECL_IMPLICIT (old_decl))
6216 warning (OPT_Wmissing_declarations,
6217 "%q+D was used with no declaration before its definition", decl1);
6219 /* This function exists in static storage.
6220 (This does not mean `static' in the C sense!) */
6221 TREE_STATIC (decl1) = 1;
6223 /* A nested function is not global. */
6224 if (current_function_decl != 0)
6225 TREE_PUBLIC (decl1) = 0;
6227 /* This is the earliest point at which we might know the assembler
6228 name of the function. Thus, if it's set before this, die horribly. */
6229 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6231 /* If #pragma weak was used, mark the decl weak now. */
6232 if (current_scope == file_scope)
6233 maybe_apply_pragma_weak (decl1);
6235 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6236 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6238 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6239 != integer_type_node)
6240 pedwarn ("return type of %q+D is not %<int%>", decl1);
6242 check_main_parameter_types(decl1);
6244 if (!TREE_PUBLIC (decl1))
6245 pedwarn ("%q+D is normally a non-static function", decl1);
6248 /* Record the decl so that the function name is defined.
6249 If we already have a decl for this name, and it is a FUNCTION_DECL,
6250 use the old decl. */
6252 current_function_decl = pushdecl (decl1);
6254 push_scope ();
6255 declare_parm_level ();
6257 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6258 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6259 DECL_ARTIFICIAL (resdecl) = 1;
6260 DECL_IGNORED_P (resdecl) = 1;
6261 DECL_RESULT (current_function_decl) = resdecl;
6263 start_fname_decls ();
6265 return 1;
6268 /* Subroutine of store_parm_decls which handles new-style function
6269 definitions (prototype format). The parms already have decls, so we
6270 need only record them as in effect and complain if any redundant
6271 old-style parm decls were written. */
6272 static void
6273 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6275 tree decl;
6277 if (current_scope->bindings)
6279 error ("%Jold-style parameter declarations in prototyped "
6280 "function definition", fndecl);
6282 /* Get rid of the old-style declarations. */
6283 pop_scope ();
6284 push_scope ();
6286 /* Don't issue this warning for nested functions, and don't issue this
6287 warning if we got here because ARG_INFO_TYPES was error_mark_node
6288 (this happens when a function definition has just an ellipsis in
6289 its parameter list). */
6290 else if (!in_system_header && !current_function_scope
6291 && arg_info->types != error_mark_node)
6292 warning (OPT_Wtraditional,
6293 "%Jtraditional C rejects ISO C style function definitions",
6294 fndecl);
6296 /* Now make all the parameter declarations visible in the function body.
6297 We can bypass most of the grunt work of pushdecl. */
6298 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6300 DECL_CONTEXT (decl) = current_function_decl;
6301 if (DECL_NAME (decl))
6303 bind (DECL_NAME (decl), decl, current_scope,
6304 /*invisible=*/false, /*nested=*/false);
6305 if (!TREE_USED (decl))
6306 warn_if_shadowing (decl);
6308 else
6309 error ("%Jparameter name omitted", decl);
6312 /* Record the parameter list in the function declaration. */
6313 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6315 /* Now make all the ancillary declarations visible, likewise. */
6316 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6318 DECL_CONTEXT (decl) = current_function_decl;
6319 if (DECL_NAME (decl))
6320 bind (DECL_NAME (decl), decl, current_scope,
6321 /*invisible=*/false, /*nested=*/false);
6324 /* And all the tag declarations. */
6325 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6326 if (TREE_PURPOSE (decl))
6327 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6328 /*invisible=*/false, /*nested=*/false);
6331 /* Subroutine of store_parm_decls which handles old-style function
6332 definitions (separate parameter list and declarations). */
6334 static void
6335 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6337 struct c_binding *b;
6338 tree parm, decl, last;
6339 tree parmids = arg_info->parms;
6340 struct pointer_set_t *seen_args = pointer_set_create ();
6342 if (!in_system_header)
6343 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6344 fndecl);
6346 /* Match each formal parameter name with its declaration. Save each
6347 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6348 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6350 if (TREE_VALUE (parm) == 0)
6352 error ("%Jparameter name missing from parameter list", fndecl);
6353 TREE_PURPOSE (parm) = 0;
6354 continue;
6357 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6358 if (b && B_IN_CURRENT_SCOPE (b))
6360 decl = b->decl;
6361 /* If we got something other than a PARM_DECL it is an error. */
6362 if (TREE_CODE (decl) != PARM_DECL)
6363 error ("%q+D declared as a non-parameter", decl);
6364 /* If the declaration is already marked, we have a duplicate
6365 name. Complain and ignore the duplicate. */
6366 else if (pointer_set_contains (seen_args, decl))
6368 error ("multiple parameters named %q+D", decl);
6369 TREE_PURPOSE (parm) = 0;
6370 continue;
6372 /* If the declaration says "void", complain and turn it into
6373 an int. */
6374 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6376 error ("parameter %q+D declared with void type", decl);
6377 TREE_TYPE (decl) = integer_type_node;
6378 DECL_ARG_TYPE (decl) = integer_type_node;
6379 layout_decl (decl, 0);
6381 warn_if_shadowing (decl);
6383 /* If no declaration found, default to int. */
6384 else
6386 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6387 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6388 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6389 pushdecl (decl);
6390 warn_if_shadowing (decl);
6392 if (flag_isoc99)
6393 pedwarn ("type of %q+D defaults to %<int%>", decl);
6394 else
6395 warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
6398 TREE_PURPOSE (parm) = decl;
6399 pointer_set_insert (seen_args, decl);
6402 /* Now examine the parms chain for incomplete declarations
6403 and declarations with no corresponding names. */
6405 for (b = current_scope->bindings; b; b = b->prev)
6407 parm = b->decl;
6408 if (TREE_CODE (parm) != PARM_DECL)
6409 continue;
6411 if (TREE_TYPE (parm) != error_mark_node
6412 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6414 error ("parameter %q+D has incomplete type", parm);
6415 TREE_TYPE (parm) = error_mark_node;
6418 if (!pointer_set_contains (seen_args, parm))
6420 error ("declaration for parameter %q+D but no such parameter", parm);
6422 /* Pretend the parameter was not missing.
6423 This gets us to a standard state and minimizes
6424 further error messages. */
6425 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6429 /* Chain the declarations together in the order of the list of
6430 names. Store that chain in the function decl, replacing the
6431 list of names. Update the current scope to match. */
6432 DECL_ARGUMENTS (fndecl) = 0;
6434 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6435 if (TREE_PURPOSE (parm))
6436 break;
6437 if (parm && TREE_PURPOSE (parm))
6439 last = TREE_PURPOSE (parm);
6440 DECL_ARGUMENTS (fndecl) = last;
6442 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6443 if (TREE_PURPOSE (parm))
6445 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6446 last = TREE_PURPOSE (parm);
6448 TREE_CHAIN (last) = 0;
6451 pointer_set_destroy (seen_args);
6453 /* If there was a previous prototype,
6454 set the DECL_ARG_TYPE of each argument according to
6455 the type previously specified, and report any mismatches. */
6457 if (current_function_prototype_arg_types)
6459 tree type;
6460 for (parm = DECL_ARGUMENTS (fndecl),
6461 type = current_function_prototype_arg_types;
6462 parm || (type && TREE_VALUE (type) != error_mark_node
6463 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
6464 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6466 if (parm == 0 || type == 0
6467 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6469 if (current_function_prototype_built_in)
6470 warning (0, "number of arguments doesn%'t match "
6471 "built-in prototype");
6472 else
6474 error ("number of arguments doesn%'t match prototype");
6475 error ("%Hprototype declaration",
6476 &current_function_prototype_locus);
6478 break;
6480 /* Type for passing arg must be consistent with that
6481 declared for the arg. ISO C says we take the unqualified
6482 type for parameters declared with qualified type. */
6483 if (TREE_TYPE (parm) != error_mark_node
6484 && TREE_TYPE (type) != error_mark_node
6485 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6486 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6488 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6489 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6491 /* Adjust argument to match prototype. E.g. a previous
6492 `int foo(float);' prototype causes
6493 `int foo(x) float x; {...}' to be treated like
6494 `int foo(float x) {...}'. This is particularly
6495 useful for argument types like uid_t. */
6496 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6498 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6499 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6500 && TYPE_PRECISION (TREE_TYPE (parm))
6501 < TYPE_PRECISION (integer_type_node))
6502 DECL_ARG_TYPE (parm) = integer_type_node;
6504 if (pedantic)
6506 /* ??? Is it possible to get here with a
6507 built-in prototype or will it always have
6508 been diagnosed as conflicting with an
6509 old-style definition and discarded? */
6510 if (current_function_prototype_built_in)
6511 warning (0, "promoted argument %qD "
6512 "doesn%'t match built-in prototype", parm);
6513 else
6515 pedwarn ("promoted argument %qD "
6516 "doesn%'t match prototype", parm);
6517 pedwarn ("%Hprototype declaration",
6518 &current_function_prototype_locus);
6522 else
6524 if (current_function_prototype_built_in)
6525 warning (0, "argument %qD doesn%'t match "
6526 "built-in prototype", parm);
6527 else
6529 error ("argument %qD doesn%'t match prototype", parm);
6530 error ("%Hprototype declaration",
6531 &current_function_prototype_locus);
6536 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6539 /* Otherwise, create a prototype that would match. */
6541 else
6543 tree actual = 0, last = 0, type;
6545 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6547 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6548 if (last)
6549 TREE_CHAIN (last) = type;
6550 else
6551 actual = type;
6552 last = type;
6554 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6555 if (last)
6556 TREE_CHAIN (last) = type;
6557 else
6558 actual = type;
6560 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6561 of the type of this function, but we need to avoid having this
6562 affect the types of other similarly-typed functions, so we must
6563 first force the generation of an identical (but separate) type
6564 node for the relevant function type. The new node we create
6565 will be a variant of the main variant of the original function
6566 type. */
6568 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6570 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6574 /* Store parameter declarations passed in ARG_INFO into the current
6575 function declaration. */
6577 void
6578 store_parm_decls_from (struct c_arg_info *arg_info)
6580 current_function_arg_info = arg_info;
6581 store_parm_decls ();
6584 /* Store the parameter declarations into the current function declaration.
6585 This is called after parsing the parameter declarations, before
6586 digesting the body of the function.
6588 For an old-style definition, construct a prototype out of the old-style
6589 parameter declarations and inject it into the function's type. */
6591 void
6592 store_parm_decls (void)
6594 tree fndecl = current_function_decl;
6595 bool proto;
6597 /* The argument information block for FNDECL. */
6598 struct c_arg_info *arg_info = current_function_arg_info;
6599 current_function_arg_info = 0;
6601 /* True if this definition is written with a prototype. Note:
6602 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6603 list in a function definition as equivalent to (void) -- an
6604 empty argument list specifies the function has no parameters,
6605 but only (void) sets up a prototype for future calls. */
6606 proto = arg_info->types != 0;
6608 if (proto)
6609 store_parm_decls_newstyle (fndecl, arg_info);
6610 else
6611 store_parm_decls_oldstyle (fndecl, arg_info);
6613 /* The next call to push_scope will be a function body. */
6615 next_is_function_body = true;
6617 /* Write a record describing this function definition to the prototypes
6618 file (if requested). */
6620 gen_aux_info_record (fndecl, 1, 0, proto);
6622 /* Initialize the RTL code for the function. */
6623 allocate_struct_function (fndecl, false);
6625 /* Begin the statement tree for this function. */
6626 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6628 /* ??? Insert the contents of the pending sizes list into the function
6629 to be evaluated. The only reason left to have this is
6630 void foo(int n, int array[n++])
6631 because we throw away the array type in favor of a pointer type, and
6632 thus won't naturally see the SAVE_EXPR containing the increment. All
6633 other pending sizes would be handled by gimplify_parameters. */
6635 tree t;
6636 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6637 add_stmt (TREE_VALUE (t));
6640 /* Even though we're inside a function body, we still don't want to
6641 call expand_expr to calculate the size of a variable-sized array.
6642 We haven't necessarily assigned RTL to all variables yet, so it's
6643 not safe to try to expand expressions involving them. */
6644 cfun->dont_save_pending_sizes_p = 1;
6647 /* Emit diagnostics that require gimple input for detection. Operate on
6648 FNDECL and all its nested functions. */
6650 static void
6651 c_gimple_diagnostics_recursively (tree fndecl)
6653 struct cgraph_node *cgn;
6655 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6656 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6658 /* Notice when OpenMP structured block constraints are violated. */
6659 if (flag_openmp)
6660 diagnose_omp_structured_block_errors (fndecl);
6662 /* Finalize all nested functions now. */
6663 cgn = cgraph_node (fndecl);
6664 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6665 c_gimple_diagnostics_recursively (cgn->decl);
6668 /* Finish up a function declaration and compile that function
6669 all the way to assembler language output. The free the storage
6670 for the function definition.
6672 This is called after parsing the body of the function definition. */
6674 void
6675 finish_function (void)
6677 tree fndecl = current_function_decl;
6679 label_context_stack_se = label_context_stack_se->next;
6680 label_context_stack_vm = label_context_stack_vm->next;
6682 if (TREE_CODE (fndecl) == FUNCTION_DECL
6683 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6685 tree args = DECL_ARGUMENTS (fndecl);
6686 for (; args; args = TREE_CHAIN (args))
6688 tree type = TREE_TYPE (args);
6689 if (INTEGRAL_TYPE_P (type)
6690 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6691 DECL_ARG_TYPE (args) = integer_type_node;
6695 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6696 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6698 /* Must mark the RESULT_DECL as being in this function. */
6700 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6701 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6703 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6705 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6706 != integer_type_node)
6708 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6709 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6710 if (!warn_main)
6711 pedwarn ("return type of %q+D is not %<int%>", fndecl);
6713 else
6715 if (flag_isoc99)
6717 tree stmt = c_finish_return (integer_zero_node);
6718 /* Hack. We don't want the middle-end to warn that this return
6719 is unreachable, so we mark its location as special. Using
6720 UNKNOWN_LOCATION has the problem that it gets clobbered in
6721 annotate_one_with_locus. A cleaner solution might be to
6722 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6724 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6729 /* Tie off the statement tree for this function. */
6730 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6732 finish_fname_decls ();
6734 /* Complain if there's just no return statement. */
6735 if (warn_return_type
6736 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6737 && !current_function_returns_value && !current_function_returns_null
6738 /* Don't complain if we are no-return. */
6739 && !current_function_returns_abnormally
6740 /* Don't warn for main(). */
6741 && !MAIN_NAME_P (DECL_NAME (fndecl))
6742 /* Or if they didn't actually specify a return type. */
6743 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6744 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6745 inline function, as we might never be compiled separately. */
6746 && DECL_INLINE (fndecl))
6748 warning (OPT_Wreturn_type,
6749 "no return statement in function returning non-void");
6750 TREE_NO_WARNING (fndecl) = 1;
6753 /* Store the end of the function, so that we get good line number
6754 info for the epilogue. */
6755 cfun->function_end_locus = input_location;
6757 /* Finalize the ELF visibility for the function. */
6758 c_determine_visibility (fndecl);
6760 /* For GNU C extern inline functions disregard inline limits. */
6761 if (DECL_EXTERNAL (fndecl)
6762 && DECL_DECLARED_INLINE_P (fndecl))
6763 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
6765 /* Genericize before inlining. Delay genericizing nested functions
6766 until their parent function is genericized. Since finalizing
6767 requires GENERIC, delay that as well. */
6769 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6770 && !undef_nested_function)
6772 if (!decl_function_context (fndecl))
6774 c_genericize (fndecl);
6775 c_gimple_diagnostics_recursively (fndecl);
6777 /* ??? Objc emits functions after finalizing the compilation unit.
6778 This should be cleaned up later and this conditional removed. */
6779 if (cgraph_global_info_ready)
6781 cgraph_add_new_function (fndecl, false);
6782 return;
6785 cgraph_finalize_function (fndecl, false);
6787 else
6789 /* Register this function with cgraph just far enough to get it
6790 added to our parent's nested function list. Handy, since the
6791 C front end doesn't have such a list. */
6792 (void) cgraph_node (fndecl);
6796 if (!decl_function_context (fndecl))
6797 undef_nested_function = false;
6799 /* We're leaving the context of this function, so zap cfun.
6800 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6801 tree_rest_of_compilation. */
6802 set_cfun (NULL);
6803 current_function_decl = NULL;
6806 /* Check the declarations given in a for-loop for satisfying the C99
6807 constraints. If exactly one such decl is found, return it. */
6809 tree
6810 check_for_loop_decls (void)
6812 struct c_binding *b;
6813 tree one_decl = NULL_TREE;
6814 int n_decls = 0;
6817 if (!flag_isoc99)
6819 /* If we get here, declarations have been used in a for loop without
6820 the C99 for loop scope. This doesn't make much sense, so don't
6821 allow it. */
6822 error ("%<for%> loop initial declaration used outside C99 mode");
6823 return NULL_TREE;
6825 /* C99 subclause 6.8.5 paragraph 3:
6827 [#3] The declaration part of a for statement shall only
6828 declare identifiers for objects having storage class auto or
6829 register.
6831 It isn't clear whether, in this sentence, "identifiers" binds to
6832 "shall only declare" or to "objects" - that is, whether all identifiers
6833 declared must be identifiers for objects, or whether the restriction
6834 only applies to those that are. (A question on this in comp.std.c
6835 in November 2000 received no answer.) We implement the strictest
6836 interpretation, to avoid creating an extension which later causes
6837 problems. */
6839 for (b = current_scope->bindings; b; b = b->prev)
6841 tree id = b->id;
6842 tree decl = b->decl;
6844 if (!id)
6845 continue;
6847 switch (TREE_CODE (decl))
6849 case VAR_DECL:
6850 if (TREE_STATIC (decl))
6851 error ("declaration of static variable %q+D in %<for%> loop "
6852 "initial declaration", decl);
6853 else if (DECL_EXTERNAL (decl))
6854 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6855 "initial declaration", decl);
6856 break;
6858 case RECORD_TYPE:
6859 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6860 id);
6861 break;
6862 case UNION_TYPE:
6863 error ("%<union %E%> declared in %<for%> loop initial declaration",
6864 id);
6865 break;
6866 case ENUMERAL_TYPE:
6867 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6868 id);
6869 break;
6870 default:
6871 error ("declaration of non-variable %q+D in %<for%> loop "
6872 "initial declaration", decl);
6875 n_decls++;
6876 one_decl = decl;
6879 return n_decls == 1 ? one_decl : NULL_TREE;
6882 /* Save and reinitialize the variables
6883 used during compilation of a C function. */
6885 void
6886 c_push_function_context (void)
6888 struct language_function *p;
6889 p = GGC_NEW (struct language_function);
6890 cfun->language = p;
6892 p->base.x_stmt_tree = c_stmt_tree;
6893 p->x_break_label = c_break_label;
6894 p->x_cont_label = c_cont_label;
6895 p->x_switch_stack = c_switch_stack;
6896 p->arg_info = current_function_arg_info;
6897 p->returns_value = current_function_returns_value;
6898 p->returns_null = current_function_returns_null;
6899 p->returns_abnormally = current_function_returns_abnormally;
6900 p->warn_about_return_type = warn_about_return_type;
6902 push_function_context ();
6905 /* Restore the variables used during compilation of a C function. */
6907 void
6908 c_pop_function_context (void)
6910 struct language_function *p;
6912 pop_function_context ();
6913 p = cfun->language;
6914 cfun->language = NULL;
6916 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6917 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6919 /* Stop pointing to the local nodes about to be freed. */
6920 /* But DECL_INITIAL must remain nonzero so we know this
6921 was an actual function definition. */
6922 DECL_INITIAL (current_function_decl) = error_mark_node;
6923 DECL_ARGUMENTS (current_function_decl) = 0;
6926 c_stmt_tree = p->base.x_stmt_tree;
6927 c_break_label = p->x_break_label;
6928 c_cont_label = p->x_cont_label;
6929 c_switch_stack = p->x_switch_stack;
6930 current_function_arg_info = p->arg_info;
6931 current_function_returns_value = p->returns_value;
6932 current_function_returns_null = p->returns_null;
6933 current_function_returns_abnormally = p->returns_abnormally;
6934 warn_about_return_type = p->warn_about_return_type;
6937 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6939 void
6940 c_dup_lang_specific_decl (tree decl)
6942 struct lang_decl *ld;
6944 if (!DECL_LANG_SPECIFIC (decl))
6945 return;
6947 ld = GGC_NEW (struct lang_decl);
6948 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6949 DECL_LANG_SPECIFIC (decl) = ld;
6952 /* The functions below are required for functionality of doing
6953 function at once processing in the C front end. Currently these
6954 functions are not called from anywhere in the C front end, but as
6955 these changes continue, that will change. */
6957 /* Returns the stmt_tree (if any) to which statements are currently
6958 being added. If there is no active statement-tree, NULL is
6959 returned. */
6961 stmt_tree
6962 current_stmt_tree (void)
6964 return &c_stmt_tree;
6967 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6968 C. */
6971 anon_aggr_type_p (const_tree ARG_UNUSED (node))
6973 return 0;
6976 /* Return the global value of T as a symbol. */
6978 tree
6979 identifier_global_value (tree t)
6981 struct c_binding *b;
6983 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6984 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6985 return b->decl;
6987 return 0;
6990 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6991 otherwise the name is found in ridpointers from RID_INDEX. */
6993 void
6994 record_builtin_type (enum rid rid_index, const char *name, tree type)
6996 tree id, decl;
6997 if (name == 0)
6998 id = ridpointers[(int) rid_index];
6999 else
7000 id = get_identifier (name);
7001 decl = build_decl (TYPE_DECL, id, type);
7002 pushdecl (decl);
7003 if (debug_hooks->type_decl)
7004 debug_hooks->type_decl (decl, false);
7007 /* Build the void_list_node (void_type_node having been created). */
7008 tree
7009 build_void_list_node (void)
7011 tree t = build_tree_list (NULL_TREE, void_type_node);
7012 return t;
7015 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7017 struct c_parm *
7018 build_c_parm (struct c_declspecs *specs, tree attrs,
7019 struct c_declarator *declarator)
7021 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7022 ret->specs = specs;
7023 ret->attrs = attrs;
7024 ret->declarator = declarator;
7025 return ret;
7028 /* Return a declarator with nested attributes. TARGET is the inner
7029 declarator to which these attributes apply. ATTRS are the
7030 attributes. */
7032 struct c_declarator *
7033 build_attrs_declarator (tree attrs, struct c_declarator *target)
7035 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7036 ret->kind = cdk_attrs;
7037 ret->declarator = target;
7038 ret->u.attrs = attrs;
7039 return ret;
7042 /* Return a declarator for a function with arguments specified by ARGS
7043 and return type specified by TARGET. */
7045 struct c_declarator *
7046 build_function_declarator (struct c_arg_info *args,
7047 struct c_declarator *target)
7049 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7050 ret->kind = cdk_function;
7051 ret->declarator = target;
7052 ret->u.arg_info = args;
7053 return ret;
7056 /* Return a declarator for the identifier IDENT (which may be
7057 NULL_TREE for an abstract declarator). */
7059 struct c_declarator *
7060 build_id_declarator (tree ident)
7062 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7063 ret->kind = cdk_id;
7064 ret->declarator = 0;
7065 ret->u.id = ident;
7066 /* Default value - may get reset to a more precise location. */
7067 ret->id_loc = input_location;
7068 return ret;
7071 /* Return something to represent absolute declarators containing a *.
7072 TARGET is the absolute declarator that the * contains.
7073 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7074 to apply to the pointer type. */
7076 struct c_declarator *
7077 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7078 struct c_declarator *target)
7080 tree attrs;
7081 int quals = 0;
7082 struct c_declarator *itarget = target;
7083 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7084 if (type_quals_attrs)
7086 attrs = type_quals_attrs->attrs;
7087 quals = quals_from_declspecs (type_quals_attrs);
7088 if (attrs != NULL_TREE)
7089 itarget = build_attrs_declarator (attrs, target);
7091 ret->kind = cdk_pointer;
7092 ret->declarator = itarget;
7093 ret->u.pointer_quals = quals;
7094 return ret;
7097 /* Return a pointer to a structure for an empty list of declaration
7098 specifiers. */
7100 struct c_declspecs *
7101 build_null_declspecs (void)
7103 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7104 ret->type = 0;
7105 ret->decl_attr = 0;
7106 ret->attrs = 0;
7107 ret->typespec_word = cts_none;
7108 ret->storage_class = csc_none;
7109 ret->declspecs_seen_p = false;
7110 ret->type_seen_p = false;
7111 ret->non_sc_seen_p = false;
7112 ret->typedef_p = false;
7113 ret->tag_defined_p = false;
7114 ret->explicit_signed_p = false;
7115 ret->deprecated_p = false;
7116 ret->default_int_p = false;
7117 ret->long_p = false;
7118 ret->long_long_p = false;
7119 ret->short_p = false;
7120 ret->signed_p = false;
7121 ret->unsigned_p = false;
7122 ret->complex_p = false;
7123 ret->inline_p = false;
7124 ret->thread_p = false;
7125 ret->const_p = false;
7126 ret->volatile_p = false;
7127 ret->restrict_p = false;
7128 ret->saturating_p = false;
7129 return ret;
7132 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7133 returning SPECS. */
7135 struct c_declspecs *
7136 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7138 enum rid i;
7139 bool dupe = false;
7140 specs->non_sc_seen_p = true;
7141 specs->declspecs_seen_p = true;
7142 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7143 && C_IS_RESERVED_WORD (qual));
7144 i = C_RID_CODE (qual);
7145 switch (i)
7147 case RID_CONST:
7148 dupe = specs->const_p;
7149 specs->const_p = true;
7150 break;
7151 case RID_VOLATILE:
7152 dupe = specs->volatile_p;
7153 specs->volatile_p = true;
7154 break;
7155 case RID_RESTRICT:
7156 dupe = specs->restrict_p;
7157 specs->restrict_p = true;
7158 break;
7159 default:
7160 gcc_unreachable ();
7162 if (dupe && pedantic && !flag_isoc99)
7163 pedwarn ("duplicate %qE", qual);
7164 return specs;
7167 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7168 returning SPECS. */
7170 struct c_declspecs *
7171 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7173 tree type = spec.spec;
7174 specs->non_sc_seen_p = true;
7175 specs->declspecs_seen_p = true;
7176 specs->type_seen_p = true;
7177 if (TREE_DEPRECATED (type))
7178 specs->deprecated_p = true;
7180 /* Handle type specifier keywords. */
7181 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
7183 enum rid i = C_RID_CODE (type);
7184 if (specs->type)
7186 error ("two or more data types in declaration specifiers");
7187 return specs;
7189 if ((int) i <= (int) RID_LAST_MODIFIER)
7191 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
7192 bool dupe = false;
7193 switch (i)
7195 case RID_LONG:
7196 if (specs->long_long_p)
7198 error ("%<long long long%> is too long for GCC");
7199 break;
7201 if (specs->long_p)
7203 if (specs->typespec_word == cts_double)
7205 error ("both %<long long%> and %<double%> in "
7206 "declaration specifiers");
7207 break;
7209 if (pedantic && !flag_isoc99 && !in_system_header
7210 && warn_long_long)
7211 pedwarn ("ISO C90 does not support %<long long%>");
7212 specs->long_long_p = 1;
7213 break;
7215 if (specs->short_p)
7216 error ("both %<long%> and %<short%> in "
7217 "declaration specifiers");
7218 else if (specs->typespec_word == cts_void)
7219 error ("both %<long%> and %<void%> in "
7220 "declaration specifiers");
7221 else if (specs->typespec_word == cts_bool)
7222 error ("both %<long%> and %<_Bool%> in "
7223 "declaration specifiers");
7224 else if (specs->typespec_word == cts_char)
7225 error ("both %<long%> and %<char%> in "
7226 "declaration specifiers");
7227 else if (specs->typespec_word == cts_float)
7228 error ("both %<long%> and %<float%> in "
7229 "declaration specifiers");
7230 else if (specs->typespec_word == cts_dfloat32)
7231 error ("both %<long%> and %<_Decimal32%> in "
7232 "declaration specifiers");
7233 else if (specs->typespec_word == cts_dfloat64)
7234 error ("both %<long%> and %<_Decimal64%> in "
7235 "declaration specifiers");
7236 else if (specs->typespec_word == cts_dfloat128)
7237 error ("both %<long%> and %<_Decimal128%> in "
7238 "declaration specifiers");
7239 else
7240 specs->long_p = true;
7241 break;
7242 case RID_SHORT:
7243 dupe = specs->short_p;
7244 if (specs->long_p)
7245 error ("both %<long%> and %<short%> in "
7246 "declaration specifiers");
7247 else if (specs->typespec_word == cts_void)
7248 error ("both %<short%> and %<void%> in "
7249 "declaration specifiers");
7250 else if (specs->typespec_word == cts_bool)
7251 error ("both %<short%> and %<_Bool%> in "
7252 "declaration specifiers");
7253 else if (specs->typespec_word == cts_char)
7254 error ("both %<short%> and %<char%> in "
7255 "declaration specifiers");
7256 else if (specs->typespec_word == cts_float)
7257 error ("both %<short%> and %<float%> in "
7258 "declaration specifiers");
7259 else if (specs->typespec_word == cts_double)
7260 error ("both %<short%> and %<double%> in "
7261 "declaration specifiers");
7262 else if (specs->typespec_word == cts_dfloat32)
7263 error ("both %<short%> and %<_Decimal32%> in "
7264 "declaration specifiers");
7265 else if (specs->typespec_word == cts_dfloat64)
7266 error ("both %<short%> and %<_Decimal64%> in "
7267 "declaration specifiers");
7268 else if (specs->typespec_word == cts_dfloat128)
7269 error ("both %<short%> and %<_Decimal128%> in "
7270 "declaration specifiers");
7271 else
7272 specs->short_p = true;
7273 break;
7274 case RID_SIGNED:
7275 dupe = specs->signed_p;
7276 if (specs->unsigned_p)
7277 error ("both %<signed%> and %<unsigned%> in "
7278 "declaration specifiers");
7279 else if (specs->typespec_word == cts_void)
7280 error ("both %<signed%> and %<void%> in "
7281 "declaration specifiers");
7282 else if (specs->typespec_word == cts_bool)
7283 error ("both %<signed%> and %<_Bool%> in "
7284 "declaration specifiers");
7285 else if (specs->typespec_word == cts_float)
7286 error ("both %<signed%> and %<float%> in "
7287 "declaration specifiers");
7288 else if (specs->typespec_word == cts_double)
7289 error ("both %<signed%> and %<double%> in "
7290 "declaration specifiers");
7291 else if (specs->typespec_word == cts_dfloat32)
7292 error ("both %<signed%> and %<_Decimal32%> in "
7293 "declaration specifiers");
7294 else if (specs->typespec_word == cts_dfloat64)
7295 error ("both %<signed%> and %<_Decimal64%> in "
7296 "declaration specifiers");
7297 else if (specs->typespec_word == cts_dfloat128)
7298 error ("both %<signed%> and %<_Decimal128%> in "
7299 "declaration specifiers");
7300 else
7301 specs->signed_p = true;
7302 break;
7303 case RID_UNSIGNED:
7304 dupe = specs->unsigned_p;
7305 if (specs->signed_p)
7306 error ("both %<signed%> and %<unsigned%> in "
7307 "declaration specifiers");
7308 else if (specs->typespec_word == cts_void)
7309 error ("both %<unsigned%> and %<void%> in "
7310 "declaration specifiers");
7311 else if (specs->typespec_word == cts_bool)
7312 error ("both %<unsigned%> and %<_Bool%> in "
7313 "declaration specifiers");
7314 else if (specs->typespec_word == cts_float)
7315 error ("both %<unsigned%> and %<float%> in "
7316 "declaration specifiers");
7317 else if (specs->typespec_word == cts_double)
7318 error ("both %<unsigned%> and %<double%> in "
7319 "declaration specifiers");
7320 else if (specs->typespec_word == cts_dfloat32)
7321 error ("both %<unsigned%> and %<_Decimal32%> in "
7322 "declaration specifiers");
7323 else if (specs->typespec_word == cts_dfloat64)
7324 error ("both %<unsigned%> and %<_Decimal64%> in "
7325 "declaration specifiers");
7326 else if (specs->typespec_word == cts_dfloat128)
7327 error ("both %<unsigned%> and %<_Decimal128%> in "
7328 "declaration specifiers");
7329 else
7330 specs->unsigned_p = true;
7331 break;
7332 case RID_COMPLEX:
7333 dupe = specs->complex_p;
7334 if (pedantic && !flag_isoc99 && !in_system_header)
7335 pedwarn ("ISO C90 does not support complex types");
7336 if (specs->typespec_word == cts_void)
7337 error ("both %<complex%> and %<void%> in "
7338 "declaration specifiers");
7339 else if (specs->typespec_word == cts_bool)
7340 error ("both %<complex%> and %<_Bool%> in "
7341 "declaration specifiers");
7342 else if (specs->typespec_word == cts_dfloat32)
7343 error ("both %<complex%> and %<_Decimal32%> in "
7344 "declaration specifiers");
7345 else if (specs->typespec_word == cts_dfloat64)
7346 error ("both %<complex%> and %<_Decimal64%> in "
7347 "declaration specifiers");
7348 else if (specs->typespec_word == cts_dfloat128)
7349 error ("both %<complex%> and %<_Decimal128%> in "
7350 "declaration specifiers");
7351 else if (specs->typespec_word == cts_fract)
7352 error ("both %<complex%> and %<_Fract%> in "
7353 "declaration specifiers");
7354 else if (specs->typespec_word == cts_accum)
7355 error ("both %<complex%> and %<_Accum%> in "
7356 "declaration specifiers");
7357 else if (specs->saturating_p)
7358 error ("both %<complex%> and %<_Sat%> in "
7359 "declaration specifiers");
7360 else
7361 specs->complex_p = true;
7362 break;
7363 case RID_SAT:
7364 dupe = specs->saturating_p;
7365 if (pedantic)
7366 pedwarn ("ISO C does not support saturating types");
7367 if (specs->typespec_word == cts_void)
7368 error ("both %<_Sat%> and %<void%> in "
7369 "declaration specifiers");
7370 else if (specs->typespec_word == cts_bool)
7371 error ("both %<_Sat%> and %<_Bool%> in "
7372 "declaration specifiers");
7373 else if (specs->typespec_word == cts_char)
7374 error ("both %<_Sat%> and %<char%> in "
7375 "declaration specifiers");
7376 else if (specs->typespec_word == cts_int)
7377 error ("both %<_Sat%> and %<int%> in "
7378 "declaration specifiers");
7379 else if (specs->typespec_word == cts_float)
7380 error ("both %<_Sat%> and %<float%> in "
7381 "declaration specifiers");
7382 else if (specs->typespec_word == cts_double)
7383 error ("both %<_Sat%> and %<double%> in "
7384 "declaration specifiers");
7385 else if (specs->typespec_word == cts_dfloat32)
7386 error ("both %<_Sat%> and %<_Decimal32%> in "
7387 "declaration specifiers");
7388 else if (specs->typespec_word == cts_dfloat64)
7389 error ("both %<_Sat%> and %<_Decimal64%> in "
7390 "declaration specifiers");
7391 else if (specs->typespec_word == cts_dfloat128)
7392 error ("both %<_Sat%> and %<_Decimal128%> in "
7393 "declaration specifiers");
7394 else if (specs->complex_p)
7395 error ("both %<_Sat%> and %<complex%> in "
7396 "declaration specifiers");
7397 else
7398 specs->saturating_p = true;
7399 break;
7400 default:
7401 gcc_unreachable ();
7404 if (dupe)
7405 error ("duplicate %qE", type);
7407 return specs;
7409 else
7411 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
7412 "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
7413 if (specs->typespec_word != cts_none)
7415 error ("two or more data types in declaration specifiers");
7416 return specs;
7418 switch (i)
7420 case RID_VOID:
7421 if (specs->long_p)
7422 error ("both %<long%> and %<void%> in "
7423 "declaration specifiers");
7424 else if (specs->short_p)
7425 error ("both %<short%> and %<void%> in "
7426 "declaration specifiers");
7427 else if (specs->signed_p)
7428 error ("both %<signed%> and %<void%> in "
7429 "declaration specifiers");
7430 else if (specs->unsigned_p)
7431 error ("both %<unsigned%> and %<void%> in "
7432 "declaration specifiers");
7433 else if (specs->complex_p)
7434 error ("both %<complex%> and %<void%> in "
7435 "declaration specifiers");
7436 else if (specs->saturating_p)
7437 error ("both %<_Sat%> and %<void%> in "
7438 "declaration specifiers");
7439 else
7440 specs->typespec_word = cts_void;
7441 return specs;
7442 case RID_BOOL:
7443 if (specs->long_p)
7444 error ("both %<long%> and %<_Bool%> in "
7445 "declaration specifiers");
7446 else if (specs->short_p)
7447 error ("both %<short%> and %<_Bool%> in "
7448 "declaration specifiers");
7449 else if (specs->signed_p)
7450 error ("both %<signed%> and %<_Bool%> in "
7451 "declaration specifiers");
7452 else if (specs->unsigned_p)
7453 error ("both %<unsigned%> and %<_Bool%> in "
7454 "declaration specifiers");
7455 else if (specs->complex_p)
7456 error ("both %<complex%> and %<_Bool%> in "
7457 "declaration specifiers");
7458 else if (specs->saturating_p)
7459 error ("both %<_Sat%> and %<_Bool%> in "
7460 "declaration specifiers");
7461 else
7462 specs->typespec_word = cts_bool;
7463 return specs;
7464 case RID_CHAR:
7465 if (specs->long_p)
7466 error ("both %<long%> and %<char%> in "
7467 "declaration specifiers");
7468 else if (specs->short_p)
7469 error ("both %<short%> and %<char%> in "
7470 "declaration specifiers");
7471 else if (specs->saturating_p)
7472 error ("both %<_Sat%> and %<char%> in "
7473 "declaration specifiers");
7474 else
7475 specs->typespec_word = cts_char;
7476 return specs;
7477 case RID_INT:
7478 if (specs->saturating_p)
7479 error ("both %<_Sat%> and %<int%> in "
7480 "declaration specifiers");
7481 else
7482 specs->typespec_word = cts_int;
7483 return specs;
7484 case RID_FLOAT:
7485 if (specs->long_p)
7486 error ("both %<long%> and %<float%> in "
7487 "declaration specifiers");
7488 else if (specs->short_p)
7489 error ("both %<short%> and %<float%> in "
7490 "declaration specifiers");
7491 else if (specs->signed_p)
7492 error ("both %<signed%> and %<float%> in "
7493 "declaration specifiers");
7494 else if (specs->unsigned_p)
7495 error ("both %<unsigned%> and %<float%> in "
7496 "declaration specifiers");
7497 else if (specs->saturating_p)
7498 error ("both %<_Sat%> and %<float%> in "
7499 "declaration specifiers");
7500 else
7501 specs->typespec_word = cts_float;
7502 return specs;
7503 case RID_DOUBLE:
7504 if (specs->long_long_p)
7505 error ("both %<long long%> and %<double%> in "
7506 "declaration specifiers");
7507 else if (specs->short_p)
7508 error ("both %<short%> and %<double%> in "
7509 "declaration specifiers");
7510 else if (specs->signed_p)
7511 error ("both %<signed%> and %<double%> in "
7512 "declaration specifiers");
7513 else if (specs->unsigned_p)
7514 error ("both %<unsigned%> and %<double%> in "
7515 "declaration specifiers");
7516 else if (specs->saturating_p)
7517 error ("both %<_Sat%> and %<double%> in "
7518 "declaration specifiers");
7519 else
7520 specs->typespec_word = cts_double;
7521 return specs;
7522 case RID_DFLOAT32:
7523 case RID_DFLOAT64:
7524 case RID_DFLOAT128:
7526 const char *str;
7527 if (i == RID_DFLOAT32)
7528 str = "_Decimal32";
7529 else if (i == RID_DFLOAT64)
7530 str = "_Decimal64";
7531 else
7532 str = "_Decimal128";
7533 if (specs->long_long_p)
7534 error ("both %<long long%> and %<%s%> in "
7535 "declaration specifiers", str);
7536 if (specs->long_p)
7537 error ("both %<long%> and %<%s%> in "
7538 "declaration specifiers", str);
7539 else if (specs->short_p)
7540 error ("both %<short%> and %<%s%> in "
7541 "declaration specifiers", str);
7542 else if (specs->signed_p)
7543 error ("both %<signed%> and %<%s%> in "
7544 "declaration specifiers", str);
7545 else if (specs->unsigned_p)
7546 error ("both %<unsigned%> and %<%s%> in "
7547 "declaration specifiers", str);
7548 else if (specs->complex_p)
7549 error ("both %<complex%> and %<%s%> in "
7550 "declaration specifiers", str);
7551 else if (specs->saturating_p)
7552 error ("both %<_Sat%> and %<%s%> in "
7553 "declaration specifiers", str);
7554 else if (i == RID_DFLOAT32)
7555 specs->typespec_word = cts_dfloat32;
7556 else if (i == RID_DFLOAT64)
7557 specs->typespec_word = cts_dfloat64;
7558 else
7559 specs->typespec_word = cts_dfloat128;
7561 if (!targetm.decimal_float_supported_p ())
7562 error ("decimal floating point not supported for this target");
7563 if (pedantic)
7564 pedwarn ("ISO C does not support decimal floating point");
7565 return specs;
7566 case RID_FRACT:
7567 case RID_ACCUM:
7569 const char *str;
7570 if (i == RID_FRACT)
7571 str = "_Fract";
7572 else
7573 str = "_Accum";
7574 if (specs->complex_p)
7575 error ("both %<complex%> and %<%s%> in "
7576 "declaration specifiers", str);
7577 else if (i == RID_FRACT)
7578 specs->typespec_word = cts_fract;
7579 else
7580 specs->typespec_word = cts_accum;
7582 if (!targetm.fixed_point_supported_p ())
7583 error ("fixed-point types not supported for this target");
7584 if (pedantic)
7585 pedwarn ("ISO C does not support fixed-point types");
7586 return specs;
7587 default:
7588 /* ObjC reserved word "id", handled below. */
7589 break;
7594 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7595 form of ObjC type, cases such as "int" and "long" being handled
7596 above), a TYPE (struct, union, enum and typeof specifiers) or an
7597 ERROR_MARK. In none of these cases may there have previously
7598 been any type specifiers. */
7599 if (specs->type || specs->typespec_word != cts_none
7600 || specs->long_p || specs->short_p || specs->signed_p
7601 || specs->unsigned_p || specs->complex_p)
7602 error ("two or more data types in declaration specifiers");
7603 else if (TREE_CODE (type) == TYPE_DECL)
7605 if (TREE_TYPE (type) == error_mark_node)
7606 ; /* Allow the type to default to int to avoid cascading errors. */
7607 else
7609 specs->type = TREE_TYPE (type);
7610 specs->decl_attr = DECL_ATTRIBUTES (type);
7611 specs->typedef_p = true;
7612 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7615 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7617 tree t = lookup_name (type);
7618 if (!t || TREE_CODE (t) != TYPE_DECL)
7619 error ("%qE fails to be a typedef or built in type", type);
7620 else if (TREE_TYPE (t) == error_mark_node)
7622 else
7623 specs->type = TREE_TYPE (t);
7625 else if (TREE_CODE (type) != ERROR_MARK)
7627 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7628 specs->tag_defined_p = true;
7629 if (spec.kind == ctsk_typeof)
7630 specs->typedef_p = true;
7631 specs->type = type;
7634 return specs;
7637 /* Add the storage class specifier or function specifier SCSPEC to the
7638 declaration specifiers SPECS, returning SPECS. */
7640 struct c_declspecs *
7641 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7643 enum rid i;
7644 enum c_storage_class n = csc_none;
7645 bool dupe = false;
7646 specs->declspecs_seen_p = true;
7647 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7648 && C_IS_RESERVED_WORD (scspec));
7649 i = C_RID_CODE (scspec);
7650 if (specs->non_sc_seen_p)
7651 warning (OPT_Wold_style_declaration,
7652 "%qE is not at beginning of declaration", scspec);
7653 switch (i)
7655 case RID_INLINE:
7656 /* C99 permits duplicate inline. Although of doubtful utility,
7657 it seems simplest to permit it in gnu89 mode as well, as
7658 there is also little utility in maintaining this as a
7659 difference between gnu89 and C99 inline. */
7660 dupe = false;
7661 specs->inline_p = true;
7662 break;
7663 case RID_THREAD:
7664 dupe = specs->thread_p;
7665 if (specs->storage_class == csc_auto)
7666 error ("%<__thread%> used with %<auto%>");
7667 else if (specs->storage_class == csc_register)
7668 error ("%<__thread%> used with %<register%>");
7669 else if (specs->storage_class == csc_typedef)
7670 error ("%<__thread%> used with %<typedef%>");
7671 else
7672 specs->thread_p = true;
7673 break;
7674 case RID_AUTO:
7675 n = csc_auto;
7676 break;
7677 case RID_EXTERN:
7678 n = csc_extern;
7679 /* Diagnose "__thread extern". */
7680 if (specs->thread_p)
7681 error ("%<__thread%> before %<extern%>");
7682 break;
7683 case RID_REGISTER:
7684 n = csc_register;
7685 break;
7686 case RID_STATIC:
7687 n = csc_static;
7688 /* Diagnose "__thread static". */
7689 if (specs->thread_p)
7690 error ("%<__thread%> before %<static%>");
7691 break;
7692 case RID_TYPEDEF:
7693 n = csc_typedef;
7694 break;
7695 default:
7696 gcc_unreachable ();
7698 if (n != csc_none && n == specs->storage_class)
7699 dupe = true;
7700 if (dupe)
7701 error ("duplicate %qE", scspec);
7702 if (n != csc_none)
7704 if (specs->storage_class != csc_none && n != specs->storage_class)
7706 error ("multiple storage classes in declaration specifiers");
7708 else
7710 specs->storage_class = n;
7711 if (n != csc_extern && n != csc_static && specs->thread_p)
7713 error ("%<__thread%> used with %qE", scspec);
7714 specs->thread_p = false;
7718 return specs;
7721 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7722 returning SPECS. */
7724 struct c_declspecs *
7725 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7727 specs->attrs = chainon (attrs, specs->attrs);
7728 specs->declspecs_seen_p = true;
7729 return specs;
7732 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7733 specifiers with any other type specifier to determine the resulting
7734 type. This is where ISO C checks on complex types are made, since
7735 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7736 double". */
7738 struct c_declspecs *
7739 finish_declspecs (struct c_declspecs *specs)
7741 /* If a type was specified as a whole, we have no modifiers and are
7742 done. */
7743 if (specs->type != NULL_TREE)
7745 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7746 && !specs->signed_p && !specs->unsigned_p
7747 && !specs->complex_p);
7748 return specs;
7751 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7752 has been specified, treat it as "int" unless "_Complex" is
7753 present and there are no other specifiers. If we just have
7754 "_Complex", it is equivalent to "_Complex double", but e.g.
7755 "_Complex short" is equivalent to "_Complex short int". */
7756 if (specs->typespec_word == cts_none)
7758 if (specs->saturating_p)
7760 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
7761 specs->typespec_word = cts_fract;
7763 else if (specs->long_p || specs->short_p
7764 || specs->signed_p || specs->unsigned_p)
7766 specs->typespec_word = cts_int;
7768 else if (specs->complex_p)
7770 specs->typespec_word = cts_double;
7771 if (pedantic)
7772 pedwarn ("ISO C does not support plain %<complex%> meaning "
7773 "%<double complex%>");
7775 else
7777 specs->typespec_word = cts_int;
7778 specs->default_int_p = true;
7779 /* We don't diagnose this here because grokdeclarator will
7780 give more specific diagnostics according to whether it is
7781 a function definition. */
7785 /* If "signed" was specified, record this to distinguish "int" and
7786 "signed int" in the case of a bit-field with
7787 -funsigned-bitfields. */
7788 specs->explicit_signed_p = specs->signed_p;
7790 /* Now compute the actual type. */
7791 switch (specs->typespec_word)
7793 case cts_void:
7794 gcc_assert (!specs->long_p && !specs->short_p
7795 && !specs->signed_p && !specs->unsigned_p
7796 && !specs->complex_p);
7797 specs->type = void_type_node;
7798 break;
7799 case cts_bool:
7800 gcc_assert (!specs->long_p && !specs->short_p
7801 && !specs->signed_p && !specs->unsigned_p
7802 && !specs->complex_p);
7803 specs->type = boolean_type_node;
7804 break;
7805 case cts_char:
7806 gcc_assert (!specs->long_p && !specs->short_p);
7807 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7808 if (specs->signed_p)
7809 specs->type = signed_char_type_node;
7810 else if (specs->unsigned_p)
7811 specs->type = unsigned_char_type_node;
7812 else
7813 specs->type = char_type_node;
7814 if (specs->complex_p)
7816 if (pedantic)
7817 pedwarn ("ISO C does not support complex integer types");
7818 specs->type = build_complex_type (specs->type);
7820 break;
7821 case cts_int:
7822 gcc_assert (!(specs->long_p && specs->short_p));
7823 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7824 if (specs->long_long_p)
7825 specs->type = (specs->unsigned_p
7826 ? long_long_unsigned_type_node
7827 : long_long_integer_type_node);
7828 else if (specs->long_p)
7829 specs->type = (specs->unsigned_p
7830 ? long_unsigned_type_node
7831 : long_integer_type_node);
7832 else if (specs->short_p)
7833 specs->type = (specs->unsigned_p
7834 ? short_unsigned_type_node
7835 : short_integer_type_node);
7836 else
7837 specs->type = (specs->unsigned_p
7838 ? unsigned_type_node
7839 : integer_type_node);
7840 if (specs->complex_p)
7842 if (pedantic)
7843 pedwarn ("ISO C does not support complex integer types");
7844 specs->type = build_complex_type (specs->type);
7846 break;
7847 case cts_float:
7848 gcc_assert (!specs->long_p && !specs->short_p
7849 && !specs->signed_p && !specs->unsigned_p);
7850 specs->type = (specs->complex_p
7851 ? complex_float_type_node
7852 : float_type_node);
7853 break;
7854 case cts_double:
7855 gcc_assert (!specs->long_long_p && !specs->short_p
7856 && !specs->signed_p && !specs->unsigned_p);
7857 if (specs->long_p)
7859 specs->type = (specs->complex_p
7860 ? complex_long_double_type_node
7861 : long_double_type_node);
7863 else
7865 specs->type = (specs->complex_p
7866 ? complex_double_type_node
7867 : double_type_node);
7869 break;
7870 case cts_dfloat32:
7871 case cts_dfloat64:
7872 case cts_dfloat128:
7873 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7874 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7875 if (specs->typespec_word == cts_dfloat32)
7876 specs->type = dfloat32_type_node;
7877 else if (specs->typespec_word == cts_dfloat64)
7878 specs->type = dfloat64_type_node;
7879 else
7880 specs->type = dfloat128_type_node;
7881 break;
7882 case cts_fract:
7883 gcc_assert (!specs->complex_p);
7884 if (specs->saturating_p)
7886 if (specs->long_long_p)
7887 specs->type = specs->unsigned_p
7888 ? sat_unsigned_long_long_fract_type_node
7889 : sat_long_long_fract_type_node;
7890 else if (specs->long_p)
7891 specs->type = specs->unsigned_p
7892 ? sat_unsigned_long_fract_type_node
7893 : sat_long_fract_type_node;
7894 else if (specs->short_p)
7895 specs->type = specs->unsigned_p
7896 ? sat_unsigned_short_fract_type_node
7897 : sat_short_fract_type_node;
7898 else
7899 specs->type = specs->unsigned_p
7900 ? sat_unsigned_fract_type_node
7901 : sat_fract_type_node;
7903 else
7905 if (specs->long_long_p)
7906 specs->type = specs->unsigned_p
7907 ? unsigned_long_long_fract_type_node
7908 : long_long_fract_type_node;
7909 else if (specs->long_p)
7910 specs->type = specs->unsigned_p
7911 ? unsigned_long_fract_type_node
7912 : long_fract_type_node;
7913 else if (specs->short_p)
7914 specs->type = specs->unsigned_p
7915 ? unsigned_short_fract_type_node
7916 : short_fract_type_node;
7917 else
7918 specs->type = specs->unsigned_p
7919 ? unsigned_fract_type_node
7920 : fract_type_node;
7922 break;
7923 case cts_accum:
7924 gcc_assert (!specs->complex_p);
7925 if (specs->saturating_p)
7927 if (specs->long_long_p)
7928 specs->type = specs->unsigned_p
7929 ? sat_unsigned_long_long_accum_type_node
7930 : sat_long_long_accum_type_node;
7931 else if (specs->long_p)
7932 specs->type = specs->unsigned_p
7933 ? sat_unsigned_long_accum_type_node
7934 : sat_long_accum_type_node;
7935 else if (specs->short_p)
7936 specs->type = specs->unsigned_p
7937 ? sat_unsigned_short_accum_type_node
7938 : sat_short_accum_type_node;
7939 else
7940 specs->type = specs->unsigned_p
7941 ? sat_unsigned_accum_type_node
7942 : sat_accum_type_node;
7944 else
7946 if (specs->long_long_p)
7947 specs->type = specs->unsigned_p
7948 ? unsigned_long_long_accum_type_node
7949 : long_long_accum_type_node;
7950 else if (specs->long_p)
7951 specs->type = specs->unsigned_p
7952 ? unsigned_long_accum_type_node
7953 : long_accum_type_node;
7954 else if (specs->short_p)
7955 specs->type = specs->unsigned_p
7956 ? unsigned_short_accum_type_node
7957 : short_accum_type_node;
7958 else
7959 specs->type = specs->unsigned_p
7960 ? unsigned_accum_type_node
7961 : accum_type_node;
7963 break;
7964 default:
7965 gcc_unreachable ();
7968 return specs;
7971 /* A subroutine of c_write_global_declarations. Perform final processing
7972 on one file scope's declarations (or the external scope's declarations),
7973 GLOBALS. */
7975 static void
7976 c_write_global_declarations_1 (tree globals)
7978 tree decl;
7979 bool reconsider;
7981 /* Process the decls in the order they were written. */
7982 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7984 /* Check for used but undefined static functions using the C
7985 standard's definition of "used", and set TREE_NO_WARNING so
7986 that check_global_declarations doesn't repeat the check. */
7987 if (TREE_CODE (decl) == FUNCTION_DECL
7988 && DECL_INITIAL (decl) == 0
7989 && DECL_EXTERNAL (decl)
7990 && !TREE_PUBLIC (decl)
7991 && C_DECL_USED (decl))
7993 pedwarn ("%q+F used but never defined", decl);
7994 TREE_NO_WARNING (decl) = 1;
7997 wrapup_global_declaration_1 (decl);
8002 reconsider = false;
8003 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8004 reconsider |= wrapup_global_declaration_2 (decl);
8006 while (reconsider);
8008 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8009 check_global_declaration_1 (decl);
8012 /* A subroutine of c_write_global_declarations Emit debug information for each
8013 of the declarations in GLOBALS. */
8015 static void
8016 c_write_global_declarations_2 (tree globals)
8018 tree decl;
8020 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
8021 debug_hooks->global_decl (decl);
8024 /* Preserve the external declarations scope across a garbage collect. */
8025 static GTY(()) tree ext_block;
8027 void
8028 c_write_global_declarations (void)
8030 tree t;
8032 /* We don't want to do this if generating a PCH. */
8033 if (pch_file)
8034 return;
8036 /* Don't waste time on further processing if -fsyntax-only or we've
8037 encountered errors. */
8038 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
8039 return;
8041 /* Close the external scope. */
8042 ext_block = pop_scope ();
8043 external_scope = 0;
8044 gcc_assert (!current_scope);
8046 if (ext_block)
8048 tree tmp = BLOCK_VARS (ext_block);
8049 int flags;
8050 FILE * stream = dump_begin (TDI_tu, &flags);
8051 if (stream && tmp)
8053 dump_node (tmp, flags & ~TDF_SLIM, stream);
8054 dump_end (TDI_tu, stream);
8058 /* Process all file scopes in this compilation, and the external_scope,
8059 through wrapup_global_declarations and check_global_declarations. */
8060 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8061 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
8062 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8064 /* We're done parsing; proceed to optimize and emit assembly.
8065 FIXME: shouldn't be the front end's responsibility to call this. */
8066 cgraph_optimize ();
8068 /* After cgraph has had a chance to emit everything that's going to
8069 be emitted, output debug information for globals. */
8070 if (errorcount == 0 && sorrycount == 0)
8072 timevar_push (TV_SYMOUT);
8073 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8074 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8075 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8076 timevar_pop (TV_SYMOUT);
8079 ext_block = NULL;
8082 #include "gt-c-decl.h"