* tree.c (build_int_cst_wide): Add an assertion (gcc_unreachable)
[official-gcc.git] / gcc / c-decl.c
blob80f1a1a621f61da66373951f5b797b73d2b5b022
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "input.h"
33 #include "tm.h"
34 #include "intl.h"
35 #include "tree.h"
36 #include "tree-inline.h"
37 #include "rtl.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "output.h"
41 #include "expr.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tm_p.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "debug.h"
49 #include "opts.h"
50 #include "timevar.h"
51 #include "c-common.h"
52 #include "c-pragma.h"
53 #include "langhooks.h"
54 #include "tree-mudflap.h"
55 #include "tree-gimple.h"
56 #include "diagnostic.h"
57 #include "tree-dump.h"
58 #include "cgraph.h"
59 #include "hashtab.h"
60 #include "libfuncs.h"
61 #include "except.h"
62 #include "langhooks-def.h"
63 #include "pointer-set.h"
65 /* Set this to 1 if you want the standard ISO C99 semantics of 'inline'
66 when you specify -std=c99 or -std=gnu99, and to 0 if you want
67 behaviour compatible with the nonstandard semantics implemented by
68 GCC 2.95 through 4.2. */
69 #define WANT_C99_INLINE_SEMANTICS 1
71 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
72 enum decl_context
73 { NORMAL, /* Ordinary declaration */
74 FUNCDEF, /* Function definition */
75 PARM, /* Declaration of parm before function body */
76 FIELD, /* Declaration inside struct or union */
77 TYPENAME}; /* Typename (inside cast or sizeof) */
80 /* Nonzero if we have seen an invalid cross reference
81 to a struct, union, or enum, but not yet printed the message. */
82 tree pending_invalid_xref;
84 /* File and line to appear in the eventual error message. */
85 location_t pending_invalid_xref_location;
87 /* True means we've initialized exception handling. */
88 bool c_eh_initialized_p;
90 /* While defining an enum type, this is 1 plus the last enumerator
91 constant value. Note that will do not have to save this or `enum_overflow'
92 around nested function definition since such a definition could only
93 occur in an enum value expression and we don't use these variables in
94 that case. */
96 static tree enum_next_value;
98 /* Nonzero means that there was overflow computing enum_next_value. */
100 static int enum_overflow;
102 /* The file and line that the prototype came from if this is an
103 old-style definition; used for diagnostics in
104 store_parm_decls_oldstyle. */
106 static location_t current_function_prototype_locus;
108 /* Whether this prototype was built-in. */
110 static bool current_function_prototype_built_in;
112 /* The argument type information of this prototype. */
114 static tree current_function_prototype_arg_types;
116 /* The argument information structure for the function currently being
117 defined. */
119 static struct c_arg_info *current_function_arg_info;
121 /* The obstack on which parser and related data structures, which are
122 not live beyond their top-level declaration or definition, are
123 allocated. */
124 struct obstack parser_obstack;
126 /* The current statement tree. */
128 static GTY(()) struct stmt_tree_s c_stmt_tree;
130 /* State saving variables. */
131 tree c_break_label;
132 tree c_cont_label;
134 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
135 included in this invocation. Note that the current translation
136 unit is not included in this list. */
138 static GTY(()) tree all_translation_units;
140 /* A list of decls to be made automatically visible in each file scope. */
141 static GTY(()) tree visible_builtins;
143 /* Set to 0 at beginning of a function definition, set to 1 if
144 a return statement that specifies a return value is seen. */
146 int current_function_returns_value;
148 /* Set to 0 at beginning of a function definition, set to 1 if
149 a return statement with no argument is seen. */
151 int current_function_returns_null;
153 /* Set to 0 at beginning of a function definition, set to 1 if
154 a call to a noreturn function is seen. */
156 int current_function_returns_abnormally;
158 /* Set to nonzero by `grokdeclarator' for a function
159 whose return type is defaulted, if warnings for this are desired. */
161 static int warn_about_return_type;
163 /* Nonzero when the current toplevel function contains a declaration
164 of a nested function which is never defined. */
166 static bool undef_nested_function;
168 /* True means global_bindings_p should return false even if the scope stack
169 says we are in file scope. */
170 bool c_override_global_bindings_to_false;
173 /* Each c_binding structure describes one binding of an identifier to
174 a decl. All the decls in a scope - irrespective of namespace - are
175 chained together by the ->prev field, which (as the name implies)
176 runs in reverse order. All the decls in a given namespace bound to
177 a given identifier are chained by the ->shadowed field, which runs
178 from inner to outer scopes.
180 The ->decl field usually points to a DECL node, but there are two
181 exceptions. In the namespace of type tags, the bound entity is a
182 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
183 identifier is encountered, it is bound to error_mark_node to
184 suppress further errors about that identifier in the current
185 function.
187 The ->type field stores the type of the declaration in this scope;
188 if NULL, the type is the type of the ->decl field. This is only of
189 relevance for objects with external or internal linkage which may
190 be redeclared in inner scopes, forming composite types that only
191 persist for the duration of those scopes. In the external scope,
192 this stores the composite of all the types declared for this
193 object, visible or not. The ->inner_comp field (used only at file
194 scope) stores whether an incomplete array type at file scope was
195 completed at an inner scope to an array size other than 1.
197 The depth field is copied from the scope structure that holds this
198 decl. It is used to preserve the proper ordering of the ->shadowed
199 field (see bind()) and also for a handful of special-case checks.
200 Finally, the invisible bit is true for a decl which should be
201 ignored for purposes of normal name lookup, and the nested bit is
202 true for a decl that's been bound a second time in an inner scope;
203 in all such cases, the binding in the outer scope will have its
204 invisible bit true. */
206 struct c_binding GTY((chain_next ("%h.prev")))
208 tree decl; /* the decl bound */
209 tree type; /* the type in this scope */
210 tree id; /* the identifier it's bound to */
211 struct c_binding *prev; /* the previous decl in this scope */
212 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
213 unsigned int depth : 28; /* depth of this scope */
214 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
215 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
216 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
217 /* one free bit */
219 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
220 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
221 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
222 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
224 #define I_SYMBOL_BINDING(node) \
225 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
226 #define I_SYMBOL_DECL(node) \
227 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
229 #define I_TAG_BINDING(node) \
230 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
231 #define I_TAG_DECL(node) \
232 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
234 #define I_LABEL_BINDING(node) \
235 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
236 #define I_LABEL_DECL(node) \
237 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
239 /* Each C symbol points to three linked lists of c_binding structures.
240 These describe the values of the identifier in the three different
241 namespaces defined by the language. */
243 struct lang_identifier GTY(())
245 struct c_common_identifier common_id;
246 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
247 struct c_binding *tag_binding; /* struct/union/enum tags */
248 struct c_binding *label_binding; /* labels */
251 /* Validate c-lang.c's assumptions. */
252 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
253 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
255 /* The resulting tree type. */
257 union lang_tree_node
258 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
259 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : (union lang_tree_node *) TREE_CHAIN (&%h.generic)")))
261 union tree_node GTY ((tag ("0"),
262 desc ("tree_node_structure (&%h)")))
263 generic;
264 struct lang_identifier GTY ((tag ("1"))) identifier;
267 /* Each c_scope structure describes the complete contents of one
268 scope. Four scopes are distinguished specially: the innermost or
269 current scope, the innermost function scope, the file scope (always
270 the second to outermost) and the outermost or external scope.
272 Most declarations are recorded in the current scope.
274 All normal label declarations are recorded in the innermost
275 function scope, as are bindings of undeclared identifiers to
276 error_mark_node. (GCC permits nested functions as an extension,
277 hence the 'innermost' qualifier.) Explicitly declared labels
278 (using the __label__ extension) appear in the current scope.
280 Being in the file scope (current_scope == file_scope) causes
281 special behavior in several places below. Also, under some
282 conditions the Objective-C front end records declarations in the
283 file scope even though that isn't the current scope.
285 All declarations with external linkage are recorded in the external
286 scope, even if they aren't visible there; this models the fact that
287 such declarations are visible to the entire program, and (with a
288 bit of cleverness, see pushdecl) allows diagnosis of some violations
289 of C99 6.2.2p7 and 6.2.7p2:
291 If, within the same translation unit, the same identifier appears
292 with both internal and external linkage, the behavior is
293 undefined.
295 All declarations that refer to the same object or function shall
296 have compatible type; otherwise, the behavior is undefined.
298 Initially only the built-in declarations, which describe compiler
299 intrinsic functions plus a subset of the standard library, are in
300 this scope.
302 The order of the blocks list matters, and it is frequently appended
303 to. To avoid having to walk all the way to the end of the list on
304 each insertion, or reverse the list later, we maintain a pointer to
305 the last list entry. (FIXME: It should be feasible to use a reversed
306 list here.)
308 The bindings list is strictly in reverse order of declarations;
309 pop_scope relies on this. */
312 struct c_scope GTY((chain_next ("%h.outer")))
314 /* The scope containing this one. */
315 struct c_scope *outer;
317 /* The next outermost function scope. */
318 struct c_scope *outer_function;
320 /* All bindings in this scope. */
321 struct c_binding *bindings;
323 /* For each scope (except the global one), a chain of BLOCK nodes
324 for all the scopes that were entered and exited one level down. */
325 tree blocks;
326 tree blocks_last;
328 /* The depth of this scope. Used to keep the ->shadowed chain of
329 bindings sorted innermost to outermost. */
330 unsigned int depth : 28;
332 /* True if we are currently filling this scope with parameter
333 declarations. */
334 BOOL_BITFIELD parm_flag : 1;
336 /* True if we saw [*] in this scope. Used to give an error messages
337 if these appears in a function definition. */
338 BOOL_BITFIELD had_vla_unspec : 1;
340 /* True if we already complained about forward parameter decls
341 in this scope. This prevents double warnings on
342 foo (int a; int b; ...) */
343 BOOL_BITFIELD warned_forward_parm_decls : 1;
345 /* True if this is the outermost block scope of a function body.
346 This scope contains the parameters, the local variables declared
347 in the outermost block, and all the labels (except those in
348 nested functions, or declared at block scope with __label__). */
349 BOOL_BITFIELD function_body : 1;
351 /* True means make a BLOCK for this scope no matter what. */
352 BOOL_BITFIELD keep : 1;
355 /* The scope currently in effect. */
357 static GTY(()) struct c_scope *current_scope;
359 /* The innermost function scope. Ordinary (not explicitly declared)
360 labels, bindings to error_mark_node, and the lazily-created
361 bindings of __func__ and its friends get this scope. */
363 static GTY(()) struct c_scope *current_function_scope;
365 /* The C file scope. This is reset for each input translation unit. */
367 static GTY(()) struct c_scope *file_scope;
369 /* The outermost scope. This is used for all declarations with
370 external linkage, and only these, hence the name. */
372 static GTY(()) struct c_scope *external_scope;
374 /* A chain of c_scope structures awaiting reuse. */
376 static GTY((deletable)) struct c_scope *scope_freelist;
378 /* A chain of c_binding structures awaiting reuse. */
380 static GTY((deletable)) struct c_binding *binding_freelist;
382 /* Append VAR to LIST in scope SCOPE. */
383 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
384 struct c_scope *s_ = (scope); \
385 tree d_ = (decl); \
386 if (s_->list##_last) \
387 TREE_CHAIN (s_->list##_last) = d_; \
388 else \
389 s_->list = d_; \
390 s_->list##_last = d_; \
391 } while (0)
393 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
394 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
395 struct c_scope *t_ = (tscope); \
396 struct c_scope *f_ = (fscope); \
397 if (t_->to##_last) \
398 TREE_CHAIN (t_->to##_last) = f_->from; \
399 else \
400 t_->to = f_->from; \
401 t_->to##_last = f_->from##_last; \
402 } while (0)
404 /* True means unconditionally make a BLOCK for the next scope pushed. */
406 static bool keep_next_level_flag;
408 /* True means the next call to push_scope will be the outermost scope
409 of a function body, so do not push a new scope, merely cease
410 expecting parameter decls. */
412 static bool next_is_function_body;
414 /* Functions called automatically at the beginning and end of execution. */
416 static GTY(()) tree static_ctors;
417 static GTY(()) tree static_dtors;
419 /* Forward declarations. */
420 static tree lookup_name_in_scope (tree, struct c_scope *);
421 static tree c_make_fname_decl (tree, int);
422 static tree grokdeclarator (const struct c_declarator *,
423 struct c_declspecs *,
424 enum decl_context, bool, tree *);
425 static tree grokparms (struct c_arg_info *, bool);
426 static void layout_array_type (tree);
428 /* T is a statement. Add it to the statement-tree. This is the
429 C/ObjC version--C++ has a slightly different version of this
430 function. */
432 tree
433 add_stmt (tree t)
435 enum tree_code code = TREE_CODE (t);
437 if (EXPR_P (t) && code != LABEL_EXPR)
439 if (!EXPR_HAS_LOCATION (t))
440 SET_EXPR_LOCATION (t, input_location);
443 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
444 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
446 /* Add T to the statement-tree. Non-side-effect statements need to be
447 recorded during statement expressions. */
448 append_to_statement_list_force (t, &cur_stmt_list);
450 return t;
453 /* States indicating how grokdeclarator() should handle declspecs marked
454 with __attribute__((deprecated)). An object declared as
455 __attribute__((deprecated)) suppresses warnings of uses of other
456 deprecated items. */
458 enum deprecated_states {
459 DEPRECATED_NORMAL,
460 DEPRECATED_SUPPRESS
463 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
465 void
466 c_print_identifier (FILE *file, tree node, int indent)
468 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
469 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
470 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
471 if (C_IS_RESERVED_WORD (node))
473 tree rid = ridpointers[C_RID_CODE (node)];
474 indent_to (file, indent + 4);
475 fprintf (file, "rid %p \"%s\"",
476 (void *) rid, IDENTIFIER_POINTER (rid));
480 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
481 which may be any of several kinds of DECL or TYPE or error_mark_node,
482 in the scope SCOPE. */
483 static void
484 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
486 struct c_binding *b, **here;
488 if (binding_freelist)
490 b = binding_freelist;
491 binding_freelist = b->prev;
493 else
494 b = GGC_NEW (struct c_binding);
496 b->shadowed = 0;
497 b->decl = decl;
498 b->id = name;
499 b->depth = scope->depth;
500 b->invisible = invisible;
501 b->nested = nested;
502 b->inner_comp = 0;
504 b->type = 0;
506 b->prev = scope->bindings;
507 scope->bindings = b;
509 if (!name)
510 return;
512 switch (TREE_CODE (decl))
514 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
515 case ENUMERAL_TYPE:
516 case UNION_TYPE:
517 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
518 case VAR_DECL:
519 case FUNCTION_DECL:
520 case TYPE_DECL:
521 case CONST_DECL:
522 case PARM_DECL:
523 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
525 default:
526 gcc_unreachable ();
529 /* Locate the appropriate place in the chain of shadowed decls
530 to insert this binding. Normally, scope == current_scope and
531 this does nothing. */
532 while (*here && (*here)->depth > scope->depth)
533 here = &(*here)->shadowed;
535 b->shadowed = *here;
536 *here = b;
539 /* Clear the binding structure B, stick it on the binding_freelist,
540 and return the former value of b->prev. This is used by pop_scope
541 and get_parm_info to iterate destructively over all the bindings
542 from a given scope. */
543 static struct c_binding *
544 free_binding_and_advance (struct c_binding *b)
546 struct c_binding *prev = b->prev;
548 memset (b, 0, sizeof (struct c_binding));
549 b->prev = binding_freelist;
550 binding_freelist = b;
552 return prev;
556 /* Hook called at end of compilation to assume 1 elt
557 for a file-scope tentative array defn that wasn't complete before. */
559 void
560 c_finish_incomplete_decl (tree decl)
562 if (TREE_CODE (decl) == VAR_DECL)
564 tree type = TREE_TYPE (decl);
565 if (type != error_mark_node
566 && TREE_CODE (type) == ARRAY_TYPE
567 && !DECL_EXTERNAL (decl)
568 && TYPE_DOMAIN (type) == 0)
570 warning (0, "array %q+D assumed to have one element", decl);
572 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
574 layout_decl (decl, 0);
579 /* The Objective-C front-end often needs to determine the current scope. */
581 void *
582 objc_get_current_scope (void)
584 return current_scope;
587 /* The following function is used only by Objective-C. It needs to live here
588 because it accesses the innards of c_scope. */
590 void
591 objc_mark_locals_volatile (void *enclosing_blk)
593 struct c_scope *scope;
594 struct c_binding *b;
596 for (scope = current_scope;
597 scope && scope != enclosing_blk;
598 scope = scope->outer)
600 for (b = scope->bindings; b; b = b->prev)
601 objc_volatilize_decl (b->decl);
603 /* Do not climb up past the current function. */
604 if (scope->function_body)
605 break;
609 /* Nonzero if we are currently in file scope. */
612 global_bindings_p (void)
614 return current_scope == file_scope && !c_override_global_bindings_to_false;
617 void
618 keep_next_level (void)
620 keep_next_level_flag = true;
623 /* Identify this scope as currently being filled with parameters. */
625 void
626 declare_parm_level (void)
628 current_scope->parm_flag = true;
631 void
632 push_scope (void)
634 if (next_is_function_body)
636 /* This is the transition from the parameters to the top level
637 of the function body. These are the same scope
638 (C99 6.2.1p4,6) so we do not push another scope structure.
639 next_is_function_body is set only by store_parm_decls, which
640 in turn is called when and only when we are about to
641 encounter the opening curly brace for the function body.
643 The outermost block of a function always gets a BLOCK node,
644 because the debugging output routines expect that each
645 function has at least one BLOCK. */
646 current_scope->parm_flag = false;
647 current_scope->function_body = true;
648 current_scope->keep = true;
649 current_scope->outer_function = current_function_scope;
650 current_function_scope = current_scope;
652 keep_next_level_flag = false;
653 next_is_function_body = false;
655 else
657 struct c_scope *scope;
658 if (scope_freelist)
660 scope = scope_freelist;
661 scope_freelist = scope->outer;
663 else
664 scope = GGC_CNEW (struct c_scope);
666 scope->keep = keep_next_level_flag;
667 scope->outer = current_scope;
668 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
670 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
671 possible. */
672 if (current_scope && scope->depth == 0)
674 scope->depth--;
675 sorry ("GCC supports only %u nested scopes", scope->depth);
678 current_scope = scope;
679 keep_next_level_flag = false;
683 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
685 static void
686 set_type_context (tree type, tree context)
688 for (type = TYPE_MAIN_VARIANT (type); type;
689 type = TYPE_NEXT_VARIANT (type))
690 TYPE_CONTEXT (type) = context;
693 /* Exit a scope. Restore the state of the identifier-decl mappings
694 that were in effect when this scope was entered. Return a BLOCK
695 node containing all the DECLs in this scope that are of interest
696 to debug info generation. */
698 tree
699 pop_scope (void)
701 struct c_scope *scope = current_scope;
702 tree block, context, p;
703 struct c_binding *b;
705 bool functionbody = scope->function_body;
706 bool keep = functionbody || scope->keep || scope->bindings;
708 c_end_vm_scope (scope->depth);
710 /* If appropriate, create a BLOCK to record the decls for the life
711 of this function. */
712 block = 0;
713 if (keep)
715 block = make_node (BLOCK);
716 BLOCK_SUBBLOCKS (block) = scope->blocks;
717 TREE_USED (block) = 1;
719 /* In each subblock, record that this is its superior. */
720 for (p = scope->blocks; p; p = TREE_CHAIN (p))
721 BLOCK_SUPERCONTEXT (p) = block;
723 BLOCK_VARS (block) = 0;
726 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
727 scope must be set so that they point to the appropriate
728 construct, i.e. either to the current FUNCTION_DECL node, or
729 else to the BLOCK node we just constructed.
731 Note that for tagged types whose scope is just the formal
732 parameter list for some function type specification, we can't
733 properly set their TYPE_CONTEXTs here, because we don't have a
734 pointer to the appropriate FUNCTION_TYPE node readily available
735 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
736 type nodes get set in `grokdeclarator' as soon as we have created
737 the FUNCTION_TYPE node which will represent the "scope" for these
738 "parameter list local" tagged types. */
739 if (scope->function_body)
740 context = current_function_decl;
741 else if (scope == file_scope)
743 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
744 TREE_CHAIN (file_decl) = all_translation_units;
745 all_translation_units = file_decl;
746 context = file_decl;
748 else
749 context = block;
751 /* Clear all bindings in this scope. */
752 for (b = scope->bindings; b; b = free_binding_and_advance (b))
754 p = b->decl;
755 switch (TREE_CODE (p))
757 case LABEL_DECL:
758 /* Warnings for unused labels, errors for undefined labels. */
759 if (TREE_USED (p) && !DECL_INITIAL (p))
761 error ("label %q+D used but not defined", p);
762 DECL_INITIAL (p) = error_mark_node;
764 else if (!TREE_USED (p) && warn_unused_label)
766 if (DECL_INITIAL (p))
767 warning (0, "label %q+D defined but not used", p);
768 else
769 warning (0, "label %q+D declared but not defined", p);
771 /* Labels go in BLOCK_VARS. */
772 TREE_CHAIN (p) = BLOCK_VARS (block);
773 BLOCK_VARS (block) = p;
774 gcc_assert (I_LABEL_BINDING (b->id) == b);
775 I_LABEL_BINDING (b->id) = b->shadowed;
776 break;
778 case ENUMERAL_TYPE:
779 case UNION_TYPE:
780 case RECORD_TYPE:
781 set_type_context (p, context);
783 /* Types may not have tag-names, in which case the type
784 appears in the bindings list with b->id NULL. */
785 if (b->id)
787 gcc_assert (I_TAG_BINDING (b->id) == b);
788 I_TAG_BINDING (b->id) = b->shadowed;
790 break;
792 case FUNCTION_DECL:
793 /* Propagate TREE_ADDRESSABLE from nested functions to their
794 containing functions. */
795 if (!TREE_ASM_WRITTEN (p)
796 && DECL_INITIAL (p) != 0
797 && TREE_ADDRESSABLE (p)
798 && DECL_ABSTRACT_ORIGIN (p) != 0
799 && DECL_ABSTRACT_ORIGIN (p) != p)
800 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
801 if (!DECL_EXTERNAL (p)
802 && !DECL_INITIAL (p)
803 && scope != file_scope
804 && scope != external_scope)
806 error ("nested function %q+D declared but never defined", p);
807 undef_nested_function = true;
809 /* C99 6.7.4p6: "a function with external linkage... declared
810 with an inline function specifier ... shall also be defined in the
811 same translation unit." */
812 else if (DECL_DECLARED_INLINE_P (p)
813 && TREE_PUBLIC (p)
814 && !DECL_INITIAL (p)
815 && flag_isoc99)
816 pedwarn ("inline function %q+D declared but never defined", p);
818 goto common_symbol;
820 case VAR_DECL:
821 /* Warnings for unused variables. */
822 if (!TREE_USED (p)
823 && !TREE_NO_WARNING (p)
824 && !DECL_IN_SYSTEM_HEADER (p)
825 && DECL_NAME (p)
826 && !DECL_ARTIFICIAL (p)
827 && scope != file_scope
828 && scope != external_scope)
829 warning (OPT_Wunused_variable, "unused variable %q+D", p);
831 if (b->inner_comp)
833 error ("type of array %q+D completed incompatibly with"
834 " implicit initialization", p);
837 /* Fall through. */
838 case TYPE_DECL:
839 case CONST_DECL:
840 common_symbol:
841 /* All of these go in BLOCK_VARS, but only if this is the
842 binding in the home scope. */
843 if (!b->nested)
845 TREE_CHAIN (p) = BLOCK_VARS (block);
846 BLOCK_VARS (block) = p;
848 /* If this is the file scope, and we are processing more
849 than one translation unit in this compilation, set
850 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
851 This makes same_translation_unit_p work, and causes
852 static declarations to be given disambiguating suffixes. */
853 if (scope == file_scope && num_in_fnames > 1)
855 DECL_CONTEXT (p) = context;
856 if (TREE_CODE (p) == TYPE_DECL)
857 set_type_context (TREE_TYPE (p), context);
860 /* Fall through. */
861 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
862 already been put there by store_parm_decls. Unused-
863 parameter warnings are handled by function.c.
864 error_mark_node obviously does not go in BLOCK_VARS and
865 does not get unused-variable warnings. */
866 case PARM_DECL:
867 case ERROR_MARK:
868 /* It is possible for a decl not to have a name. We get
869 here with b->id NULL in this case. */
870 if (b->id)
872 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
873 I_SYMBOL_BINDING (b->id) = b->shadowed;
874 if (b->shadowed && b->shadowed->type)
875 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
877 break;
879 default:
880 gcc_unreachable ();
885 /* Dispose of the block that we just made inside some higher level. */
886 if ((scope->function_body || scope == file_scope) && context)
888 DECL_INITIAL (context) = block;
889 BLOCK_SUPERCONTEXT (block) = context;
891 else if (scope->outer)
893 if (block)
894 SCOPE_LIST_APPEND (scope->outer, blocks, block);
895 /* If we did not make a block for the scope just exited, any
896 blocks made for inner scopes must be carried forward so they
897 will later become subblocks of something else. */
898 else if (scope->blocks)
899 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
902 /* Pop the current scope, and free the structure for reuse. */
903 current_scope = scope->outer;
904 if (scope->function_body)
905 current_function_scope = scope->outer_function;
907 memset (scope, 0, sizeof (struct c_scope));
908 scope->outer = scope_freelist;
909 scope_freelist = scope;
911 return block;
914 void
915 push_file_scope (void)
917 tree decl;
919 if (file_scope)
920 return;
922 push_scope ();
923 file_scope = current_scope;
925 start_fname_decls ();
927 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
928 bind (DECL_NAME (decl), decl, file_scope,
929 /*invisible=*/false, /*nested=*/true);
932 void
933 pop_file_scope (void)
935 /* In case there were missing closebraces, get us back to the global
936 binding level. */
937 while (current_scope != file_scope)
938 pop_scope ();
940 /* __FUNCTION__ is defined at file scope (""). This
941 call may not be necessary as my tests indicate it
942 still works without it. */
943 finish_fname_decls ();
945 /* This is the point to write out a PCH if we're doing that.
946 In that case we do not want to do anything else. */
947 if (pch_file)
949 c_common_write_pch ();
950 return;
953 /* Pop off the file scope and close this translation unit. */
954 pop_scope ();
955 file_scope = 0;
957 maybe_apply_pending_pragma_weaks ();
958 cgraph_finalize_compilation_unit ();
961 /* Insert BLOCK at the end of the list of subblocks of the current
962 scope. This is used when a BIND_EXPR is expanded, to handle the
963 BLOCK node inside the BIND_EXPR. */
965 void
966 insert_block (tree block)
968 TREE_USED (block) = 1;
969 SCOPE_LIST_APPEND (current_scope, blocks, block);
972 /* Push a definition or a declaration of struct, union or enum tag "name".
973 "type" should be the type node.
974 We assume that the tag "name" is not already defined.
976 Note that the definition may really be just a forward reference.
977 In that case, the TYPE_SIZE will be zero. */
979 static void
980 pushtag (tree name, tree type)
982 /* Record the identifier as the type's name if it has none. */
983 if (name && !TYPE_NAME (type))
984 TYPE_NAME (type) = name;
985 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
987 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
988 tagged type we just added to the current scope. This fake
989 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
990 to output a representation of a tagged type, and it also gives
991 us a convenient place to record the "scope start" address for the
992 tagged type. */
994 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
996 /* An approximation for now, so we can tell this is a function-scope tag.
997 This will be updated in pop_scope. */
998 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1001 /* Subroutine of compare_decls. Allow harmless mismatches in return
1002 and argument types provided that the type modes match. This function
1003 return a unified type given a suitable match, and 0 otherwise. */
1005 static tree
1006 match_builtin_function_types (tree newtype, tree oldtype)
1008 tree newrettype, oldrettype;
1009 tree newargs, oldargs;
1010 tree trytype, tryargs;
1012 /* Accept the return type of the new declaration if same modes. */
1013 oldrettype = TREE_TYPE (oldtype);
1014 newrettype = TREE_TYPE (newtype);
1016 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1017 return 0;
1019 oldargs = TYPE_ARG_TYPES (oldtype);
1020 newargs = TYPE_ARG_TYPES (newtype);
1021 tryargs = newargs;
1023 while (oldargs || newargs)
1025 if (!oldargs
1026 || !newargs
1027 || !TREE_VALUE (oldargs)
1028 || !TREE_VALUE (newargs)
1029 || TYPE_MODE (TREE_VALUE (oldargs))
1030 != TYPE_MODE (TREE_VALUE (newargs)))
1031 return 0;
1033 oldargs = TREE_CHAIN (oldargs);
1034 newargs = TREE_CHAIN (newargs);
1037 trytype = build_function_type (newrettype, tryargs);
1038 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1041 /* Subroutine of diagnose_mismatched_decls. Check for function type
1042 mismatch involving an empty arglist vs a nonempty one and give clearer
1043 diagnostics. */
1044 static void
1045 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1046 tree newtype, tree oldtype)
1048 tree t;
1050 if (TREE_CODE (olddecl) != FUNCTION_DECL
1051 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1052 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1054 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1055 return;
1057 t = TYPE_ARG_TYPES (oldtype);
1058 if (t == 0)
1059 t = TYPE_ARG_TYPES (newtype);
1060 for (; t; t = TREE_CHAIN (t))
1062 tree type = TREE_VALUE (t);
1064 if (TREE_CHAIN (t) == 0
1065 && TYPE_MAIN_VARIANT (type) != void_type_node)
1067 inform ("a parameter list with an ellipsis can%'t match "
1068 "an empty parameter name list declaration");
1069 break;
1072 if (c_type_promotes_to (type) != type)
1074 inform ("an argument type that has a default promotion can%'t match "
1075 "an empty parameter name list declaration");
1076 break;
1081 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1082 old-style function definition, NEWDECL is a prototype declaration.
1083 Diagnose inconsistencies in the argument list. Returns TRUE if
1084 the prototype is compatible, FALSE if not. */
1085 static bool
1086 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1088 tree newargs, oldargs;
1089 int i;
1091 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1093 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1094 newargs = TYPE_ARG_TYPES (newtype);
1095 i = 1;
1097 for (;;)
1099 tree oldargtype = TREE_VALUE (oldargs);
1100 tree newargtype = TREE_VALUE (newargs);
1102 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1103 return false;
1105 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1106 newargtype = TYPE_MAIN_VARIANT (newargtype);
1108 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1109 break;
1111 /* Reaching the end of just one list means the two decls don't
1112 agree on the number of arguments. */
1113 if (END_OF_ARGLIST (oldargtype))
1115 error ("prototype for %q+D declares more arguments "
1116 "than previous old-style definition", newdecl);
1117 return false;
1119 else if (END_OF_ARGLIST (newargtype))
1121 error ("prototype for %q+D declares fewer arguments "
1122 "than previous old-style definition", newdecl);
1123 return false;
1126 /* Type for passing arg must be consistent with that declared
1127 for the arg. */
1128 else if (!comptypes (oldargtype, newargtype))
1130 error ("prototype for %q+D declares argument %d"
1131 " with incompatible type",
1132 newdecl, i);
1133 return false;
1136 oldargs = TREE_CHAIN (oldargs);
1137 newargs = TREE_CHAIN (newargs);
1138 i++;
1141 /* If we get here, no errors were found, but do issue a warning
1142 for this poor-style construct. */
1143 warning (0, "prototype for %q+D follows non-prototype definition",
1144 newdecl);
1145 return true;
1146 #undef END_OF_ARGLIST
1149 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1150 first in a pair of mismatched declarations, using the diagnostic
1151 function DIAG. */
1152 static void
1153 locate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2))
1155 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1157 else if (DECL_INITIAL (decl))
1158 diag (G_("previous definition of %q+D was here"), decl);
1159 else if (C_DECL_IMPLICIT (decl))
1160 diag (G_("previous implicit declaration of %q+D was here"), decl);
1161 else
1162 diag (G_("previous declaration of %q+D was here"), decl);
1165 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1166 Returns true if the caller should proceed to merge the two, false
1167 if OLDDECL should simply be discarded. As a side effect, issues
1168 all necessary diagnostics for invalid or poor-style combinations.
1169 If it returns true, writes the types of NEWDECL and OLDDECL to
1170 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1171 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1173 static bool
1174 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1175 tree *newtypep, tree *oldtypep)
1177 tree newtype, oldtype;
1178 bool pedwarned = false;
1179 bool warned = false;
1180 bool retval = true;
1182 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1183 && DECL_EXTERNAL (DECL))
1185 /* If we have error_mark_node for either decl or type, just discard
1186 the previous decl - we're in an error cascade already. */
1187 if (olddecl == error_mark_node || newdecl == error_mark_node)
1188 return false;
1189 *oldtypep = oldtype = TREE_TYPE (olddecl);
1190 *newtypep = newtype = TREE_TYPE (newdecl);
1191 if (oldtype == error_mark_node || newtype == error_mark_node)
1192 return false;
1194 /* Two different categories of symbol altogether. This is an error
1195 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1196 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1198 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1199 && DECL_BUILT_IN (olddecl)
1200 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1202 error ("%q+D redeclared as different kind of symbol", newdecl);
1203 locate_old_decl (olddecl, error);
1205 else if (TREE_PUBLIC (newdecl))
1206 warning (0, "built-in function %q+D declared as non-function",
1207 newdecl);
1208 else
1209 warning (OPT_Wshadow, "declaration of %q+D shadows "
1210 "a built-in function", newdecl);
1211 return false;
1214 /* Enumerators have no linkage, so may only be declared once in a
1215 given scope. */
1216 if (TREE_CODE (olddecl) == CONST_DECL)
1218 error ("redeclaration of enumerator %q+D", newdecl);
1219 locate_old_decl (olddecl, error);
1220 return false;
1223 if (!comptypes (oldtype, newtype))
1225 if (TREE_CODE (olddecl) == FUNCTION_DECL
1226 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1228 /* Accept harmless mismatch in function types.
1229 This is for the ffs and fprintf builtins. */
1230 tree trytype = match_builtin_function_types (newtype, oldtype);
1232 if (trytype && comptypes (newtype, trytype))
1233 *oldtypep = oldtype = trytype;
1234 else
1236 /* If types don't match for a built-in, throw away the
1237 built-in. No point in calling locate_old_decl here, it
1238 won't print anything. */
1239 warning (0, "conflicting types for built-in function %q+D",
1240 newdecl);
1241 return false;
1244 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1245 && DECL_IS_BUILTIN (olddecl))
1247 /* A conflicting function declaration for a predeclared
1248 function that isn't actually built in. Objective C uses
1249 these. The new declaration silently overrides everything
1250 but the volatility (i.e. noreturn) indication. See also
1251 below. FIXME: Make Objective C use normal builtins. */
1252 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1253 return false;
1255 /* Permit void foo (...) to match int foo (...) if the latter is
1256 the definition and implicit int was used. See
1257 c-torture/compile/920625-2.c. */
1258 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1259 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1260 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1261 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1263 pedwarn ("conflicting types for %q+D", newdecl);
1264 /* Make sure we keep void as the return type. */
1265 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1266 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1267 pedwarned = true;
1269 /* Permit void foo (...) to match an earlier call to foo (...) with
1270 no declared type (thus, implicitly int). */
1271 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1272 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1273 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1274 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1276 pedwarn ("conflicting types for %q+D", newdecl);
1277 /* Make sure we keep void as the return type. */
1278 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1279 pedwarned = true;
1281 else
1283 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1284 error ("conflicting type qualifiers for %q+D", newdecl);
1285 else
1286 error ("conflicting types for %q+D", newdecl);
1287 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1288 locate_old_decl (olddecl, error);
1289 return false;
1293 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1294 but silently ignore the redeclaration if either is in a system
1295 header. (Conflicting redeclarations were handled above.) */
1296 if (TREE_CODE (newdecl) == TYPE_DECL)
1298 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1299 return true; /* Allow OLDDECL to continue in use. */
1301 error ("redefinition of typedef %q+D", newdecl);
1302 locate_old_decl (olddecl, error);
1303 return false;
1306 /* Function declarations can either be 'static' or 'extern' (no
1307 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1308 can never conflict with each other on account of linkage
1309 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1310 gnu89 mode permits two definitions if one is 'extern inline' and
1311 one is not. The non- extern-inline definition supersedes the
1312 extern-inline definition. */
1314 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1316 /* If you declare a built-in function name as static, or
1317 define the built-in with an old-style definition (so we
1318 can't validate the argument list) the built-in definition is
1319 overridden, but optionally warn this was a bad choice of name. */
1320 if (DECL_BUILT_IN (olddecl)
1321 && !C_DECL_DECLARED_BUILTIN (olddecl)
1322 && (!TREE_PUBLIC (newdecl)
1323 || (DECL_INITIAL (newdecl)
1324 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1326 warning (OPT_Wshadow, "declaration of %q+D shadows "
1327 "a built-in function", newdecl);
1328 /* Discard the old built-in function. */
1329 return false;
1332 if (DECL_INITIAL (newdecl))
1334 if (DECL_INITIAL (olddecl))
1336 /* If both decls are in the same TU and the new declaration
1337 isn't overriding an extern inline reject the new decl.
1338 In c99, no overriding is allowed in the same translation
1339 unit. */
1340 if ((!DECL_EXTERN_INLINE (olddecl)
1341 || DECL_EXTERN_INLINE (newdecl)
1342 #if WANT_C99_INLINE_SEMANTICS
1343 || (flag_isoc99
1344 && (!DECL_DECLARED_INLINE_P (olddecl)
1345 || !lookup_attribute ("gnu_inline",
1346 DECL_ATTRIBUTES (olddecl)))
1347 && (!DECL_DECLARED_INLINE_P (newdecl)
1348 || !lookup_attribute ("gnu_inline",
1349 DECL_ATTRIBUTES (newdecl))))
1350 #endif /* WANT_C99_INLINE_SEMANTICS */
1352 && same_translation_unit_p (newdecl, olddecl))
1354 error ("redefinition of %q+D", newdecl);
1355 locate_old_decl (olddecl, error);
1356 return false;
1360 /* If we have a prototype after an old-style function definition,
1361 the argument types must be checked specially. */
1362 else if (DECL_INITIAL (olddecl)
1363 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1364 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1365 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1367 locate_old_decl (olddecl, error);
1368 return false;
1370 /* A non-static declaration (even an "extern") followed by a
1371 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1372 The same is true for a static forward declaration at block
1373 scope followed by a non-static declaration/definition at file
1374 scope. Static followed by non-static at the same scope is
1375 not undefined behavior, and is the most convenient way to get
1376 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1377 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1378 we do diagnose it if -Wtraditional. */
1379 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1381 /* Two exceptions to the rule. If olddecl is an extern
1382 inline, or a predeclared function that isn't actually
1383 built in, newdecl silently overrides olddecl. The latter
1384 occur only in Objective C; see also above. (FIXME: Make
1385 Objective C use normal builtins.) */
1386 if (!DECL_IS_BUILTIN (olddecl)
1387 && !DECL_EXTERN_INLINE (olddecl))
1389 error ("static declaration of %q+D follows "
1390 "non-static declaration", newdecl);
1391 locate_old_decl (olddecl, error);
1393 return false;
1395 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1397 if (DECL_CONTEXT (olddecl))
1399 error ("non-static declaration of %q+D follows "
1400 "static declaration", newdecl);
1401 locate_old_decl (olddecl, error);
1402 return false;
1404 else if (warn_traditional)
1406 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1407 "follows static declaration", newdecl);
1408 warned = true;
1412 /* Make sure gnu_inline attribute is either not present, or
1413 present on all inline decls. */
1414 if (DECL_DECLARED_INLINE_P (olddecl)
1415 && DECL_DECLARED_INLINE_P (newdecl))
1417 bool newa = lookup_attribute ("gnu_inline",
1418 DECL_ATTRIBUTES (newdecl)) != NULL;
1419 bool olda = lookup_attribute ("gnu_inline",
1420 DECL_ATTRIBUTES (olddecl)) != NULL;
1421 if (newa != olda)
1423 error ("%<gnu_inline%> attribute present on %q+D",
1424 newa ? newdecl : olddecl);
1425 error ("%Jbut not here", newa ? olddecl : newdecl);
1429 else if (TREE_CODE (newdecl) == VAR_DECL)
1431 /* Only variables can be thread-local, and all declarations must
1432 agree on this property. */
1433 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1435 /* Nothing to check. Since OLDDECL is marked threadprivate
1436 and NEWDECL does not have a thread-local attribute, we
1437 will merge the threadprivate attribute into NEWDECL. */
1440 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1442 if (DECL_THREAD_LOCAL_P (newdecl))
1443 error ("thread-local declaration of %q+D follows "
1444 "non-thread-local declaration", newdecl);
1445 else
1446 error ("non-thread-local declaration of %q+D follows "
1447 "thread-local declaration", newdecl);
1449 locate_old_decl (olddecl, error);
1450 return false;
1453 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1454 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1456 error ("redefinition of %q+D", newdecl);
1457 locate_old_decl (olddecl, error);
1458 return false;
1461 /* Objects declared at file scope: if the first declaration had
1462 external linkage (even if it was an external reference) the
1463 second must have external linkage as well, or the behavior is
1464 undefined. If the first declaration had internal linkage, then
1465 the second must too, or else be an external reference (in which
1466 case the composite declaration still has internal linkage).
1467 As for function declarations, we warn about the static-then-
1468 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1469 if (DECL_FILE_SCOPE_P (newdecl)
1470 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1472 if (DECL_EXTERNAL (newdecl))
1474 if (!DECL_FILE_SCOPE_P (olddecl))
1476 error ("extern declaration of %q+D follows "
1477 "declaration with no linkage", newdecl);
1478 locate_old_decl (olddecl, error);
1479 return false;
1481 else if (warn_traditional)
1483 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1484 "follows static declaration", newdecl);
1485 warned = true;
1488 else
1490 if (TREE_PUBLIC (newdecl))
1491 error ("non-static declaration of %q+D follows "
1492 "static declaration", newdecl);
1493 else
1494 error ("static declaration of %q+D follows "
1495 "non-static declaration", newdecl);
1497 locate_old_decl (olddecl, error);
1498 return false;
1501 /* Two objects with the same name declared at the same block
1502 scope must both be external references (6.7p3). */
1503 else if (!DECL_FILE_SCOPE_P (newdecl))
1505 if (DECL_EXTERNAL (newdecl))
1507 /* Extern with initializer at block scope, which will
1508 already have received an error. */
1510 else if (DECL_EXTERNAL (olddecl))
1512 error ("declaration of %q+D with no linkage follows "
1513 "extern declaration", newdecl);
1514 locate_old_decl (olddecl, error);
1516 else
1518 error ("redeclaration of %q+D with no linkage", newdecl);
1519 locate_old_decl (olddecl, error);
1522 return false;
1526 /* warnings */
1527 /* All decls must agree on a visibility. */
1528 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1529 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1530 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1532 warning (0, "redeclaration of %q+D with different visibility "
1533 "(old visibility preserved)", newdecl);
1534 warned = true;
1537 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1539 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1540 if (DECL_DECLARED_INLINE_P (newdecl)
1541 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1543 warning (OPT_Wattributes, "inline declaration of %qD follows "
1544 "declaration with attribute noinline", newdecl);
1545 warned = true;
1547 else if (DECL_DECLARED_INLINE_P (olddecl)
1548 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1550 warning (OPT_Wattributes, "declaration of %q+D with attribute "
1551 "noinline follows inline declaration ", newdecl);
1552 warned = true;
1555 /* Inline declaration after use or definition.
1556 ??? Should we still warn about this now we have unit-at-a-time
1557 mode and can get it right?
1558 Definitely don't complain if the decls are in different translation
1559 units.
1560 C99 permits this, so don't warn in that case. (The function
1561 may not be inlined everywhere in function-at-a-time mode, but
1562 we still shouldn't warn.) */
1563 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1564 && same_translation_unit_p (olddecl, newdecl)
1565 && ! flag_isoc99)
1567 if (TREE_USED (olddecl))
1569 warning (0, "%q+D declared inline after being called", olddecl);
1570 warned = true;
1572 else if (DECL_INITIAL (olddecl))
1574 warning (0, "%q+D declared inline after its definition", olddecl);
1575 warned = true;
1579 else /* PARM_DECL, VAR_DECL */
1581 /* Redeclaration of a parameter is a constraint violation (this is
1582 not explicitly stated, but follows from C99 6.7p3 [no more than
1583 one declaration of the same identifier with no linkage in the
1584 same scope, except type tags] and 6.2.2p6 [parameters have no
1585 linkage]). We must check for a forward parameter declaration,
1586 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1587 an extension, the mandatory diagnostic for which is handled by
1588 mark_forward_parm_decls. */
1590 if (TREE_CODE (newdecl) == PARM_DECL
1591 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1593 error ("redefinition of parameter %q+D", newdecl);
1594 locate_old_decl (olddecl, error);
1595 return false;
1599 /* Optional warning for completely redundant decls. */
1600 if (!warned && !pedwarned
1601 && warn_redundant_decls
1602 /* Don't warn about a function declaration followed by a
1603 definition. */
1604 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1605 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1606 /* Don't warn about redundant redeclarations of builtins. */
1607 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1608 && !DECL_BUILT_IN (newdecl)
1609 && DECL_BUILT_IN (olddecl)
1610 && !C_DECL_DECLARED_BUILTIN (olddecl))
1611 /* Don't warn about an extern followed by a definition. */
1612 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1613 /* Don't warn about forward parameter decls. */
1614 && !(TREE_CODE (newdecl) == PARM_DECL
1615 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1616 /* Don't warn about a variable definition following a declaration. */
1617 && !(TREE_CODE (newdecl) == VAR_DECL
1618 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1620 warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1621 newdecl);
1622 warned = true;
1625 /* Report location of previous decl/defn in a consistent manner. */
1626 if (warned || pedwarned)
1627 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1629 #undef DECL_EXTERN_INLINE
1631 return retval;
1634 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1635 consistent with OLDDECL, but carries new information. Merge the
1636 new information into OLDDECL. This function issues no
1637 diagnostics. */
1639 static void
1640 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1642 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1643 && DECL_INITIAL (newdecl) != 0);
1644 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1645 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1646 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1647 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1648 bool extern_changed = false;
1650 /* For real parm decl following a forward decl, rechain the old decl
1651 in its new location and clear TREE_ASM_WRITTEN (it's not a
1652 forward decl anymore). */
1653 if (TREE_CODE (newdecl) == PARM_DECL
1654 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1656 struct c_binding *b, **here;
1658 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1659 if ((*here)->decl == olddecl)
1660 goto found;
1661 gcc_unreachable ();
1663 found:
1664 b = *here;
1665 *here = b->prev;
1666 b->prev = current_scope->bindings;
1667 current_scope->bindings = b;
1669 TREE_ASM_WRITTEN (olddecl) = 0;
1672 DECL_ATTRIBUTES (newdecl)
1673 = targetm.merge_decl_attributes (olddecl, newdecl);
1675 /* Merge the data types specified in the two decls. */
1676 TREE_TYPE (newdecl)
1677 = TREE_TYPE (olddecl)
1678 = composite_type (newtype, oldtype);
1680 /* Lay the type out, unless already done. */
1681 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1683 if (TREE_TYPE (newdecl) != error_mark_node)
1684 layout_type (TREE_TYPE (newdecl));
1685 if (TREE_CODE (newdecl) != FUNCTION_DECL
1686 && TREE_CODE (newdecl) != TYPE_DECL
1687 && TREE_CODE (newdecl) != CONST_DECL)
1688 layout_decl (newdecl, 0);
1690 else
1692 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1693 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1694 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1695 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1696 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1697 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1699 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1700 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1705 /* Merge the type qualifiers. */
1706 if (TREE_READONLY (newdecl))
1707 TREE_READONLY (olddecl) = 1;
1709 if (TREE_THIS_VOLATILE (newdecl))
1711 TREE_THIS_VOLATILE (olddecl) = 1;
1712 if (TREE_CODE (newdecl) == VAR_DECL)
1713 make_var_volatile (newdecl);
1716 /* Merge deprecatedness. */
1717 if (TREE_DEPRECATED (newdecl))
1718 TREE_DEPRECATED (olddecl) = 1;
1720 /* Keep source location of definition rather than declaration and of
1721 prototype rather than non-prototype unless that prototype is
1722 built-in. */
1723 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1724 || (old_is_prototype && !new_is_prototype
1725 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1726 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1728 /* Merge the initialization information. */
1729 if (DECL_INITIAL (newdecl) == 0)
1730 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1732 /* Merge the threadprivate attribute. */
1733 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1735 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1736 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1739 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1741 /* Merge the unused-warning information. */
1742 if (DECL_IN_SYSTEM_HEADER (olddecl))
1743 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1744 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1745 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1747 /* Merge the section attribute.
1748 We want to issue an error if the sections conflict but that
1749 must be done later in decl_attributes since we are called
1750 before attributes are assigned. */
1751 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1752 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1754 /* Copy the assembler name.
1755 Currently, it can only be defined in the prototype. */
1756 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1758 /* Use visibility of whichever declaration had it specified */
1759 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1761 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1762 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1765 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1767 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1768 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1769 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1770 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1771 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1772 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1773 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1774 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1775 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1776 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1779 /* Merge the storage class information. */
1780 merge_weak (newdecl, olddecl);
1782 /* For functions, static overrides non-static. */
1783 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1785 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1786 /* This is since we don't automatically
1787 copy the attributes of NEWDECL into OLDDECL. */
1788 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1789 /* If this clears `static', clear it in the identifier too. */
1790 if (!TREE_PUBLIC (olddecl))
1791 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1795 #if WANT_C99_INLINE_SEMANTICS
1796 /* In c99, 'extern' declaration before (or after) 'inline' means this
1797 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1798 is present. */
1799 if (TREE_CODE (newdecl) == FUNCTION_DECL
1800 && flag_isoc99
1801 && (DECL_DECLARED_INLINE_P (newdecl)
1802 || DECL_DECLARED_INLINE_P (olddecl))
1803 && (!DECL_DECLARED_INLINE_P (newdecl)
1804 || !DECL_DECLARED_INLINE_P (olddecl)
1805 || !DECL_EXTERNAL (olddecl))
1806 && DECL_EXTERNAL (newdecl)
1807 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl)))
1808 DECL_EXTERNAL (newdecl) = 0;
1809 #endif /* WANT_C99_INLINE_SEMANTICS */
1811 if (DECL_EXTERNAL (newdecl))
1813 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1814 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1816 /* An extern decl does not override previous storage class. */
1817 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1818 if (!DECL_EXTERNAL (newdecl))
1820 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1821 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1824 else
1826 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1827 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1830 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1832 /* If we're redefining a function previously defined as extern
1833 inline, make sure we emit debug info for the inline before we
1834 throw it away, in case it was inlined into a function that
1835 hasn't been written out yet. */
1836 if (new_is_definition && DECL_INITIAL (olddecl))
1838 if (TREE_USED (olddecl)
1839 /* In unit-at-a-time mode we never inline re-defined extern
1840 inline functions. */
1841 && !flag_unit_at_a_time
1842 && cgraph_function_possibly_inlined_p (olddecl))
1843 (*debug_hooks->outlining_inline_function) (olddecl);
1845 /* The new defn must not be inline. */
1846 DECL_INLINE (newdecl) = 0;
1847 DECL_UNINLINABLE (newdecl) = 1;
1849 else
1851 /* If either decl says `inline', this fn is inline, unless
1852 its definition was passed already. */
1853 if (DECL_DECLARED_INLINE_P (newdecl)
1854 || DECL_DECLARED_INLINE_P (olddecl))
1855 DECL_DECLARED_INLINE_P (newdecl) = 1;
1857 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1858 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1861 if (DECL_BUILT_IN (olddecl))
1863 /* If redeclaring a builtin function, it stays built in.
1864 But it gets tagged as having been declared. */
1865 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1866 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1867 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1868 if (new_is_prototype)
1869 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1870 else
1871 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1872 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1875 /* Also preserve various other info from the definition. */
1876 if (!new_is_definition)
1878 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1879 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1880 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1881 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1882 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1884 /* Set DECL_INLINE on the declaration if we've got a body
1885 from which to instantiate. */
1886 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1888 DECL_INLINE (newdecl) = 1;
1889 DECL_ABSTRACT_ORIGIN (newdecl)
1890 = DECL_ABSTRACT_ORIGIN (olddecl);
1893 else
1895 /* If a previous declaration said inline, mark the
1896 definition as inlinable. */
1897 if (DECL_DECLARED_INLINE_P (newdecl)
1898 && !DECL_UNINLINABLE (newdecl))
1899 DECL_INLINE (newdecl) = 1;
1903 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1905 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1906 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1908 unsigned olddecl_uid = DECL_UID (olddecl);
1909 tree olddecl_context = DECL_CONTEXT (olddecl);
1911 memcpy ((char *) olddecl + sizeof (struct tree_common),
1912 (char *) newdecl + sizeof (struct tree_common),
1913 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1914 switch (TREE_CODE (olddecl))
1916 case FIELD_DECL:
1917 case VAR_DECL:
1918 case PARM_DECL:
1919 case LABEL_DECL:
1920 case RESULT_DECL:
1921 case CONST_DECL:
1922 case TYPE_DECL:
1923 case FUNCTION_DECL:
1924 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1925 (char *) newdecl + sizeof (struct tree_decl_common),
1926 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1927 break;
1929 default:
1931 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1932 (char *) newdecl + sizeof (struct tree_decl_common),
1933 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1935 DECL_UID (olddecl) = olddecl_uid;
1936 DECL_CONTEXT (olddecl) = olddecl_context;
1939 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1940 so that encode_section_info has a chance to look at the new decl
1941 flags and attributes. */
1942 if (DECL_RTL_SET_P (olddecl)
1943 && (TREE_CODE (olddecl) == FUNCTION_DECL
1944 || (TREE_CODE (olddecl) == VAR_DECL
1945 && TREE_STATIC (olddecl))))
1946 make_decl_rtl (olddecl);
1948 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1949 and the definition is coming from the old version, cgraph needs
1950 to be called again. */
1951 if (extern_changed && !new_is_definition
1952 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
1953 cgraph_finalize_function (olddecl, false);
1956 /* Handle when a new declaration NEWDECL has the same name as an old
1957 one OLDDECL in the same binding contour. Prints an error message
1958 if appropriate.
1960 If safely possible, alter OLDDECL to look like NEWDECL, and return
1961 true. Otherwise, return false. */
1963 static bool
1964 duplicate_decls (tree newdecl, tree olddecl)
1966 tree newtype = NULL, oldtype = NULL;
1968 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1970 /* Avoid `unused variable' and other warnings warnings for OLDDECL. */
1971 TREE_NO_WARNING (olddecl) = 1;
1972 return false;
1975 merge_decls (newdecl, olddecl, newtype, oldtype);
1976 return true;
1980 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1981 static void
1982 warn_if_shadowing (tree new_decl)
1984 struct c_binding *b;
1986 /* Shadow warnings wanted? */
1987 if (!warn_shadow
1988 /* No shadow warnings for internally generated vars. */
1989 || DECL_IS_BUILTIN (new_decl)
1990 /* No shadow warnings for vars made for inlining. */
1991 || DECL_FROM_INLINE (new_decl))
1992 return;
1994 /* Is anything being shadowed? Invisible decls do not count. */
1995 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1996 if (b->decl && b->decl != new_decl && !b->invisible)
1998 tree old_decl = b->decl;
2000 if (old_decl == error_mark_node)
2002 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2003 "non-variable", new_decl);
2004 break;
2006 else if (TREE_CODE (old_decl) == PARM_DECL)
2007 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2008 new_decl);
2009 else if (DECL_FILE_SCOPE_P (old_decl))
2010 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2011 "declaration", new_decl);
2012 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2013 && DECL_BUILT_IN (old_decl))
2015 warning (OPT_Wshadow, "declaration of %q+D shadows "
2016 "a built-in function", new_decl);
2017 break;
2019 else
2020 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2021 new_decl);
2023 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
2025 break;
2030 /* Subroutine of pushdecl.
2032 X is a TYPE_DECL for a typedef statement. Create a brand new
2033 ..._TYPE node (which will be just a variant of the existing
2034 ..._TYPE node with identical properties) and then install X
2035 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
2037 The whole point here is to end up with a situation where each
2038 and every ..._TYPE node the compiler creates will be uniquely
2039 associated with AT MOST one node representing a typedef name.
2040 This way, even though the compiler substitutes corresponding
2041 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2042 early on, later parts of the compiler can always do the reverse
2043 translation and get back the corresponding typedef name. For
2044 example, given:
2046 typedef struct S MY_TYPE;
2047 MY_TYPE object;
2049 Later parts of the compiler might only know that `object' was of
2050 type `struct S' if it were not for code just below. With this
2051 code however, later parts of the compiler see something like:
2053 struct S' == struct S
2054 typedef struct S' MY_TYPE;
2055 struct S' object;
2057 And they can then deduce (from the node for type struct S') that
2058 the original object declaration was:
2060 MY_TYPE object;
2062 Being able to do this is important for proper support of protoize,
2063 and also for generating precise symbolic debugging information
2064 which takes full account of the programmer's (typedef) vocabulary.
2066 Obviously, we don't want to generate a duplicate ..._TYPE node if
2067 the TYPE_DECL node that we are now processing really represents a
2068 standard built-in type.
2070 Since all standard types are effectively declared at line zero
2071 in the source file, we can easily check to see if we are working
2072 on a standard type by checking the current value of lineno. */
2074 static void
2075 clone_underlying_type (tree x)
2077 if (DECL_IS_BUILTIN (x))
2079 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2080 TYPE_NAME (TREE_TYPE (x)) = x;
2082 else if (TREE_TYPE (x) != error_mark_node
2083 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2085 tree tt = TREE_TYPE (x);
2086 DECL_ORIGINAL_TYPE (x) = tt;
2087 tt = build_variant_type_copy (tt);
2088 TYPE_NAME (tt) = x;
2089 TREE_USED (tt) = TREE_USED (x);
2090 TREE_TYPE (x) = tt;
2094 /* Record a decl-node X as belonging to the current lexical scope.
2095 Check for errors (such as an incompatible declaration for the same
2096 name already seen in the same scope).
2098 Returns either X or an old decl for the same name.
2099 If an old decl is returned, it may have been smashed
2100 to agree with what X says. */
2102 tree
2103 pushdecl (tree x)
2105 tree name = DECL_NAME (x);
2106 struct c_scope *scope = current_scope;
2107 struct c_binding *b;
2108 bool nested = false;
2110 /* Functions need the lang_decl data. */
2111 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
2112 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2114 /* Must set DECL_CONTEXT for everything not at file scope or
2115 DECL_FILE_SCOPE_P won't work. Local externs don't count
2116 unless they have initializers (which generate code). */
2117 if (current_function_decl
2118 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2119 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2120 DECL_CONTEXT (x) = current_function_decl;
2122 /* If this is of variably modified type, prevent jumping into its
2123 scope. */
2124 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2125 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2126 c_begin_vm_scope (scope->depth);
2128 /* Anonymous decls are just inserted in the scope. */
2129 if (!name)
2131 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2132 return x;
2135 /* First, see if there is another declaration with the same name in
2136 the current scope. If there is, duplicate_decls may do all the
2137 work for us. If duplicate_decls returns false, that indicates
2138 two incompatible decls in the same scope; we are to silently
2139 replace the old one (duplicate_decls has issued all appropriate
2140 diagnostics). In particular, we should not consider possible
2141 duplicates in the external scope, or shadowing. */
2142 b = I_SYMBOL_BINDING (name);
2143 if (b && B_IN_SCOPE (b, scope))
2145 struct c_binding *b_ext, *b_use;
2146 tree type = TREE_TYPE (x);
2147 tree visdecl = b->decl;
2148 tree vistype = TREE_TYPE (visdecl);
2149 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2150 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2151 b->inner_comp = false;
2152 b_use = b;
2153 b_ext = b;
2154 /* If this is an external linkage declaration, we should check
2155 for compatibility with the type in the external scope before
2156 setting the type at this scope based on the visible
2157 information only. */
2158 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2160 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2161 b_ext = b_ext->shadowed;
2162 if (b_ext)
2164 b_use = b_ext;
2165 if (b_use->type)
2166 TREE_TYPE (b_use->decl) = b_use->type;
2169 if (duplicate_decls (x, b_use->decl))
2171 if (b_use != b)
2173 /* Save the updated type in the external scope and
2174 restore the proper type for this scope. */
2175 tree thistype;
2176 if (comptypes (vistype, type))
2177 thistype = composite_type (vistype, type);
2178 else
2179 thistype = TREE_TYPE (b_use->decl);
2180 b_use->type = TREE_TYPE (b_use->decl);
2181 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2182 && DECL_BUILT_IN (b_use->decl))
2183 thistype
2184 = build_type_attribute_variant (thistype,
2185 TYPE_ATTRIBUTES
2186 (b_use->type));
2187 TREE_TYPE (b_use->decl) = thistype;
2189 return b_use->decl;
2191 else
2192 goto skip_external_and_shadow_checks;
2195 /* All declarations with external linkage, and all external
2196 references, go in the external scope, no matter what scope is
2197 current. However, the binding in that scope is ignored for
2198 purposes of normal name lookup. A separate binding structure is
2199 created in the requested scope; this governs the normal
2200 visibility of the symbol.
2202 The binding in the externals scope is used exclusively for
2203 detecting duplicate declarations of the same object, no matter
2204 what scope they are in; this is what we do here. (C99 6.2.7p2:
2205 All declarations that refer to the same object or function shall
2206 have compatible type; otherwise, the behavior is undefined.) */
2207 if (DECL_EXTERNAL (x) || scope == file_scope)
2209 tree type = TREE_TYPE (x);
2210 tree vistype = 0;
2211 tree visdecl = 0;
2212 bool type_saved = false;
2213 if (b && !B_IN_EXTERNAL_SCOPE (b)
2214 && (TREE_CODE (b->decl) == FUNCTION_DECL
2215 || TREE_CODE (b->decl) == VAR_DECL)
2216 && DECL_FILE_SCOPE_P (b->decl))
2218 visdecl = b->decl;
2219 vistype = TREE_TYPE (visdecl);
2221 if (scope != file_scope
2222 && !DECL_IN_SYSTEM_HEADER (x))
2223 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2225 while (b && !B_IN_EXTERNAL_SCOPE (b))
2227 /* If this decl might be modified, save its type. This is
2228 done here rather than when the decl is first bound
2229 because the type may change after first binding, through
2230 being completed or through attributes being added. If we
2231 encounter multiple such decls, only the first should have
2232 its type saved; the others will already have had their
2233 proper types saved and the types will not have changed as
2234 their scopes will not have been re-entered. */
2235 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2237 b->type = TREE_TYPE (b->decl);
2238 type_saved = true;
2240 if (B_IN_FILE_SCOPE (b)
2241 && TREE_CODE (b->decl) == VAR_DECL
2242 && TREE_STATIC (b->decl)
2243 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2244 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2245 && TREE_CODE (type) == ARRAY_TYPE
2246 && TYPE_DOMAIN (type)
2247 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2248 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2250 /* Array type completed in inner scope, which should be
2251 diagnosed if the completion does not have size 1 and
2252 it does not get completed in the file scope. */
2253 b->inner_comp = true;
2255 b = b->shadowed;
2258 /* If a matching external declaration has been found, set its
2259 type to the composite of all the types of that declaration.
2260 After the consistency checks, it will be reset to the
2261 composite of the visible types only. */
2262 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2263 && b->type)
2264 TREE_TYPE (b->decl) = b->type;
2266 /* The point of the same_translation_unit_p check here is,
2267 we want to detect a duplicate decl for a construct like
2268 foo() { extern bar(); } ... static bar(); but not if
2269 they are in different translation units. In any case,
2270 the static does not go in the externals scope. */
2271 if (b
2272 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2273 && duplicate_decls (x, b->decl))
2275 tree thistype;
2276 if (vistype)
2278 if (comptypes (vistype, type))
2279 thistype = composite_type (vistype, type);
2280 else
2281 thistype = TREE_TYPE (b->decl);
2283 else
2284 thistype = type;
2285 b->type = TREE_TYPE (b->decl);
2286 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2287 thistype
2288 = build_type_attribute_variant (thistype,
2289 TYPE_ATTRIBUTES (b->type));
2290 TREE_TYPE (b->decl) = thistype;
2291 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2292 return b->decl;
2294 else if (TREE_PUBLIC (x))
2296 if (visdecl && !b && duplicate_decls (x, visdecl))
2298 /* An external declaration at block scope referring to a
2299 visible entity with internal linkage. The composite
2300 type will already be correct for this scope, so we
2301 just need to fall through to make the declaration in
2302 this scope. */
2303 nested = true;
2304 x = visdecl;
2306 else
2308 bind (name, x, external_scope, /*invisible=*/true,
2309 /*nested=*/false);
2310 nested = true;
2315 if (TREE_CODE (x) != PARM_DECL)
2316 warn_if_shadowing (x);
2318 skip_external_and_shadow_checks:
2319 if (TREE_CODE (x) == TYPE_DECL)
2320 clone_underlying_type (x);
2322 bind (name, x, scope, /*invisible=*/false, nested);
2324 /* If x's type is incomplete because it's based on a
2325 structure or union which has not yet been fully declared,
2326 attach it to that structure or union type, so we can go
2327 back and complete the variable declaration later, if the
2328 structure or union gets fully declared.
2330 If the input is erroneous, we can have error_mark in the type
2331 slot (e.g. "f(void a, ...)") - that doesn't count as an
2332 incomplete type. */
2333 if (TREE_TYPE (x) != error_mark_node
2334 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2336 tree element = TREE_TYPE (x);
2338 while (TREE_CODE (element) == ARRAY_TYPE)
2339 element = TREE_TYPE (element);
2340 element = TYPE_MAIN_VARIANT (element);
2342 if ((TREE_CODE (element) == RECORD_TYPE
2343 || TREE_CODE (element) == UNION_TYPE)
2344 && (TREE_CODE (x) != TYPE_DECL
2345 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2346 && !COMPLETE_TYPE_P (element))
2347 C_TYPE_INCOMPLETE_VARS (element)
2348 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2350 return x;
2353 /* Record X as belonging to file scope.
2354 This is used only internally by the Objective-C front end,
2355 and is limited to its needs. duplicate_decls is not called;
2356 if there is any preexisting decl for this identifier, it is an ICE. */
2358 tree
2359 pushdecl_top_level (tree x)
2361 tree name;
2362 bool nested = false;
2363 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2365 name = DECL_NAME (x);
2367 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2369 if (TREE_PUBLIC (x))
2371 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2372 nested = true;
2374 if (file_scope)
2375 bind (name, x, file_scope, /*invisible=*/false, nested);
2377 return x;
2380 static void
2381 implicit_decl_warning (tree id, tree olddecl)
2383 void (*diag) (const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2);
2384 switch (mesg_implicit_function_declaration)
2386 case 0: return;
2387 case 1: diag = warning0; break;
2388 case 2: diag = error; break;
2389 default: gcc_unreachable ();
2392 diag (G_("implicit declaration of function %qE"), id);
2393 if (olddecl)
2394 locate_old_decl (olddecl, diag);
2397 /* Generate an implicit declaration for identifier FUNCTIONID as a
2398 function of type int (). */
2400 tree
2401 implicitly_declare (tree functionid)
2403 struct c_binding *b;
2404 tree decl = 0;
2405 tree asmspec_tree;
2407 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2409 if (B_IN_SCOPE (b, external_scope))
2411 decl = b->decl;
2412 break;
2416 if (decl)
2418 if (decl == error_mark_node)
2419 return decl;
2421 /* FIXME: Objective-C has weird not-really-builtin functions
2422 which are supposed to be visible automatically. They wind up
2423 in the external scope because they're pushed before the file
2424 scope gets created. Catch this here and rebind them into the
2425 file scope. */
2426 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2428 bind (functionid, decl, file_scope,
2429 /*invisible=*/false, /*nested=*/true);
2430 return decl;
2432 else
2434 tree newtype = default_function_type;
2435 if (b->type)
2436 TREE_TYPE (decl) = b->type;
2437 /* Implicit declaration of a function already declared
2438 (somehow) in a different scope, or as a built-in.
2439 If this is the first time this has happened, warn;
2440 then recycle the old declaration but with the new type. */
2441 if (!C_DECL_IMPLICIT (decl))
2443 implicit_decl_warning (functionid, decl);
2444 C_DECL_IMPLICIT (decl) = 1;
2446 if (DECL_BUILT_IN (decl))
2448 newtype = build_type_attribute_variant (newtype,
2449 TYPE_ATTRIBUTES
2450 (TREE_TYPE (decl)));
2451 if (!comptypes (newtype, TREE_TYPE (decl)))
2453 warning (0, "incompatible implicit declaration of built-in"
2454 " function %qD", decl);
2455 newtype = TREE_TYPE (decl);
2458 else
2460 if (!comptypes (newtype, TREE_TYPE (decl)))
2462 error ("incompatible implicit declaration of function %qD",
2463 decl);
2464 locate_old_decl (decl, error);
2467 b->type = TREE_TYPE (decl);
2468 TREE_TYPE (decl) = newtype;
2469 bind (functionid, decl, current_scope,
2470 /*invisible=*/false, /*nested=*/true);
2471 return decl;
2475 /* Not seen before. */
2476 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2477 DECL_EXTERNAL (decl) = 1;
2478 TREE_PUBLIC (decl) = 1;
2479 C_DECL_IMPLICIT (decl) = 1;
2480 implicit_decl_warning (functionid, 0);
2481 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2482 if (asmspec_tree)
2483 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2485 /* C89 says implicit declarations are in the innermost block.
2486 So we record the decl in the standard fashion. */
2487 decl = pushdecl (decl);
2489 /* No need to call objc_check_decl here - it's a function type. */
2490 rest_of_decl_compilation (decl, 0, 0);
2492 /* Write a record describing this implicit function declaration
2493 to the prototypes file (if requested). */
2494 gen_aux_info_record (decl, 0, 1, 0);
2496 /* Possibly apply some default attributes to this implicit declaration. */
2497 decl_attributes (&decl, NULL_TREE, 0);
2499 return decl;
2502 /* Issue an error message for a reference to an undeclared variable
2503 ID, including a reference to a builtin outside of function-call
2504 context. Establish a binding of the identifier to error_mark_node
2505 in an appropriate scope, which will suppress further errors for the
2506 same identifier. The error message should be given location LOC. */
2507 void
2508 undeclared_variable (tree id, location_t loc)
2510 static bool already = false;
2511 struct c_scope *scope;
2513 if (current_function_decl == 0)
2515 error ("%H%qE undeclared here (not in a function)", &loc, id);
2516 scope = current_scope;
2518 else
2520 error ("%H%qE undeclared (first use in this function)", &loc, id);
2522 if (!already)
2524 error ("%H(Each undeclared identifier is reported only once", &loc);
2525 error ("%Hfor each function it appears in.)", &loc);
2526 already = true;
2529 /* If we are parsing old-style parameter decls, current_function_decl
2530 will be nonnull but current_function_scope will be null. */
2531 scope = current_function_scope ? current_function_scope : current_scope;
2533 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2536 /* Subroutine of lookup_label, declare_label, define_label: construct a
2537 LABEL_DECL with all the proper frills. */
2539 static tree
2540 make_label (tree name, location_t location)
2542 tree label = build_decl (LABEL_DECL, name, void_type_node);
2544 DECL_CONTEXT (label) = current_function_decl;
2545 DECL_MODE (label) = VOIDmode;
2546 DECL_SOURCE_LOCATION (label) = location;
2548 return label;
2551 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2552 Create one if none exists so far for the current function.
2553 This is called when a label is used in a goto expression or
2554 has its address taken. */
2556 tree
2557 lookup_label (tree name)
2559 tree label;
2561 if (current_function_decl == 0)
2563 error ("label %qE referenced outside of any function", name);
2564 return 0;
2567 /* Use a label already defined or ref'd with this name, but not if
2568 it is inherited from a containing function and wasn't declared
2569 using __label__. */
2570 label = I_LABEL_DECL (name);
2571 if (label && (DECL_CONTEXT (label) == current_function_decl
2572 || C_DECLARED_LABEL_FLAG (label)))
2574 /* If the label has only been declared, update its apparent
2575 location to point here, for better diagnostics if it
2576 turns out not to have been defined. */
2577 if (!TREE_USED (label))
2578 DECL_SOURCE_LOCATION (label) = input_location;
2579 return label;
2582 /* No label binding for that identifier; make one. */
2583 label = make_label (name, input_location);
2585 /* Ordinary labels go in the current function scope. */
2586 bind (name, label, current_function_scope,
2587 /*invisible=*/false, /*nested=*/false);
2588 return label;
2591 /* Make a label named NAME in the current function, shadowing silently
2592 any that may be inherited from containing functions or containing
2593 scopes. This is called for __label__ declarations. */
2595 tree
2596 declare_label (tree name)
2598 struct c_binding *b = I_LABEL_BINDING (name);
2599 tree label;
2601 /* Check to make sure that the label hasn't already been declared
2602 at this scope */
2603 if (b && B_IN_CURRENT_SCOPE (b))
2605 error ("duplicate label declaration %qE", name);
2606 locate_old_decl (b->decl, error);
2608 /* Just use the previous declaration. */
2609 return b->decl;
2612 label = make_label (name, input_location);
2613 C_DECLARED_LABEL_FLAG (label) = 1;
2615 /* Declared labels go in the current scope. */
2616 bind (name, label, current_scope,
2617 /*invisible=*/false, /*nested=*/false);
2618 return label;
2621 /* Define a label, specifying the location in the source file.
2622 Return the LABEL_DECL node for the label, if the definition is valid.
2623 Otherwise return 0. */
2625 tree
2626 define_label (location_t location, tree name)
2628 /* Find any preexisting label with this name. It is an error
2629 if that label has already been defined in this function, or
2630 if there is a containing function with a declared label with
2631 the same name. */
2632 tree label = I_LABEL_DECL (name);
2633 struct c_label_list *nlist_se, *nlist_vm;
2635 if (label
2636 && ((DECL_CONTEXT (label) == current_function_decl
2637 && DECL_INITIAL (label) != 0)
2638 || (DECL_CONTEXT (label) != current_function_decl
2639 && C_DECLARED_LABEL_FLAG (label))))
2641 error ("%Hduplicate label %qD", &location, label);
2642 locate_old_decl (label, error);
2643 return 0;
2645 else if (label && DECL_CONTEXT (label) == current_function_decl)
2647 /* The label has been used or declared already in this function,
2648 but not defined. Update its location to point to this
2649 definition. */
2650 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2651 error ("%Jjump into statement expression", label);
2652 if (C_DECL_UNDEFINABLE_VM (label))
2653 error ("%Jjump into scope of identifier with variably modified type",
2654 label);
2655 DECL_SOURCE_LOCATION (label) = location;
2657 else
2659 /* No label binding for that identifier; make one. */
2660 label = make_label (name, location);
2662 /* Ordinary labels go in the current function scope. */
2663 bind (name, label, current_function_scope,
2664 /*invisible=*/false, /*nested=*/false);
2667 if (!in_system_header && lookup_name (name))
2668 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2669 "for labels, identifier %qE conflicts", &location, name);
2671 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2672 nlist_se->next = label_context_stack_se->labels_def;
2673 nlist_se->label = label;
2674 label_context_stack_se->labels_def = nlist_se;
2676 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2677 nlist_vm->next = label_context_stack_vm->labels_def;
2678 nlist_vm->label = label;
2679 label_context_stack_vm->labels_def = nlist_vm;
2681 /* Mark label as having been defined. */
2682 DECL_INITIAL (label) = error_mark_node;
2683 return label;
2686 /* Given NAME, an IDENTIFIER_NODE,
2687 return the structure (or union or enum) definition for that name.
2688 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2689 CODE says which kind of type the caller wants;
2690 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2691 If the wrong kind of type is found, an error is reported. */
2693 static tree
2694 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2696 struct c_binding *b = I_TAG_BINDING (name);
2697 int thislevel = 0;
2699 if (!b || !b->decl)
2700 return 0;
2702 /* We only care about whether it's in this level if
2703 thislevel_only was set or it might be a type clash. */
2704 if (thislevel_only || TREE_CODE (b->decl) != code)
2706 /* For our purposes, a tag in the external scope is the same as
2707 a tag in the file scope. (Primarily relevant to Objective-C
2708 and its builtin structure tags, which get pushed before the
2709 file scope is created.) */
2710 if (B_IN_CURRENT_SCOPE (b)
2711 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2712 thislevel = 1;
2715 if (thislevel_only && !thislevel)
2716 return 0;
2718 if (TREE_CODE (b->decl) != code)
2720 /* Definition isn't the kind we were looking for. */
2721 pending_invalid_xref = name;
2722 pending_invalid_xref_location = input_location;
2724 /* If in the same binding level as a declaration as a tag
2725 of a different type, this must not be allowed to
2726 shadow that tag, so give the error immediately.
2727 (For example, "struct foo; union foo;" is invalid.) */
2728 if (thislevel)
2729 pending_xref_error ();
2731 return b->decl;
2734 /* Print an error message now
2735 for a recent invalid struct, union or enum cross reference.
2736 We don't print them immediately because they are not invalid
2737 when used in the `struct foo;' construct for shadowing. */
2739 void
2740 pending_xref_error (void)
2742 if (pending_invalid_xref != 0)
2743 error ("%H%qE defined as wrong kind of tag",
2744 &pending_invalid_xref_location, pending_invalid_xref);
2745 pending_invalid_xref = 0;
2749 /* Look up NAME in the current scope and its superiors
2750 in the namespace of variables, functions and typedefs.
2751 Return a ..._DECL node of some kind representing its definition,
2752 or return 0 if it is undefined. */
2754 tree
2755 lookup_name (tree name)
2757 struct c_binding *b = I_SYMBOL_BINDING (name);
2758 if (b && !b->invisible)
2759 return b->decl;
2760 return 0;
2763 /* Similar to `lookup_name' but look only at the indicated scope. */
2765 static tree
2766 lookup_name_in_scope (tree name, struct c_scope *scope)
2768 struct c_binding *b;
2770 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2771 if (B_IN_SCOPE (b, scope))
2772 return b->decl;
2773 return 0;
2776 /* Create the predefined scalar types of C,
2777 and some nodes representing standard constants (0, 1, (void *) 0).
2778 Initialize the global scope.
2779 Make definitions for built-in primitive functions. */
2781 void
2782 c_init_decl_processing (void)
2784 location_t save_loc = input_location;
2786 /* Initialize reserved words for parser. */
2787 c_parse_init ();
2789 current_function_decl = 0;
2791 gcc_obstack_init (&parser_obstack);
2793 /* Make the externals scope. */
2794 push_scope ();
2795 external_scope = current_scope;
2797 /* Declarations from c_common_nodes_and_builtins must not be associated
2798 with this input file, lest we get differences between using and not
2799 using preprocessed headers. */
2800 #ifdef USE_MAPPED_LOCATION
2801 input_location = BUILTINS_LOCATION;
2802 #else
2803 input_location.file = "<built-in>";
2804 input_location.line = 0;
2805 #endif
2807 build_common_tree_nodes (flag_signed_char, false);
2809 c_common_nodes_and_builtins ();
2811 /* In C, comparisons and TRUTH_* expressions have type int. */
2812 truthvalue_type_node = integer_type_node;
2813 truthvalue_true_node = integer_one_node;
2814 truthvalue_false_node = integer_zero_node;
2816 /* Even in C99, which has a real boolean type. */
2817 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2818 boolean_type_node));
2820 input_location = save_loc;
2822 pedantic_lvalues = true;
2824 make_fname_decl = c_make_fname_decl;
2825 start_fname_decls ();
2828 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2829 decl, NAME is the initialization string and TYPE_DEP indicates whether
2830 NAME depended on the type of the function. As we don't yet implement
2831 delayed emission of static data, we mark the decl as emitted
2832 so it is not placed in the output. Anything using it must therefore pull
2833 out the STRING_CST initializer directly. FIXME. */
2835 static tree
2836 c_make_fname_decl (tree id, int type_dep)
2838 const char *name = fname_as_string (type_dep);
2839 tree decl, type, init;
2840 size_t length = strlen (name);
2842 type = build_array_type (char_type_node,
2843 build_index_type (size_int (length)));
2844 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2846 decl = build_decl (VAR_DECL, id, type);
2848 TREE_STATIC (decl) = 1;
2849 TREE_READONLY (decl) = 1;
2850 DECL_ARTIFICIAL (decl) = 1;
2852 init = build_string (length + 1, name);
2853 free ((char *) name);
2854 TREE_TYPE (init) = type;
2855 DECL_INITIAL (decl) = init;
2857 TREE_USED (decl) = 1;
2859 if (current_function_decl
2860 /* For invalid programs like this:
2862 void foo()
2863 const char* p = __FUNCTION__;
2865 the __FUNCTION__ is believed to appear in K&R style function
2866 parameter declarator. In that case we still don't have
2867 function_scope. */
2868 && (!errorcount || current_function_scope))
2870 DECL_CONTEXT (decl) = current_function_decl;
2871 bind (id, decl, current_function_scope,
2872 /*invisible=*/false, /*nested=*/false);
2875 finish_decl (decl, init, NULL_TREE);
2877 return decl;
2880 tree
2881 c_builtin_function (tree decl)
2883 tree type = TREE_TYPE (decl);
2884 tree id = DECL_NAME (decl);
2886 const char *name = IDENTIFIER_POINTER (id);
2887 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2888 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2890 /* Should never be called on a symbol with a preexisting meaning. */
2891 gcc_assert (!I_SYMBOL_BINDING (id));
2893 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2895 /* Builtins in the implementation namespace are made visible without
2896 needing to be explicitly declared. See push_file_scope. */
2897 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2899 TREE_CHAIN (decl) = visible_builtins;
2900 visible_builtins = decl;
2903 return decl;
2906 /* Called when a declaration is seen that contains no names to declare.
2907 If its type is a reference to a structure, union or enum inherited
2908 from a containing scope, shadow that tag name for the current scope
2909 with a forward reference.
2910 If its type defines a new named structure or union
2911 or defines an enum, it is valid but we need not do anything here.
2912 Otherwise, it is an error. */
2914 void
2915 shadow_tag (const struct c_declspecs *declspecs)
2917 shadow_tag_warned (declspecs, 0);
2920 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2921 but no pedwarn. */
2922 void
2923 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2925 bool found_tag = false;
2927 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2929 tree value = declspecs->type;
2930 enum tree_code code = TREE_CODE (value);
2932 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2933 /* Used to test also that TYPE_SIZE (value) != 0.
2934 That caused warning for `struct foo;' at top level in the file. */
2936 tree name = TYPE_NAME (value);
2937 tree t;
2939 found_tag = true;
2941 if (name == 0)
2943 if (warned != 1 && code != ENUMERAL_TYPE)
2944 /* Empty unnamed enum OK */
2946 pedwarn ("unnamed struct/union that defines no instances");
2947 warned = 1;
2950 else if (!declspecs->tag_defined_p
2951 && declspecs->storage_class != csc_none)
2953 if (warned != 1)
2954 pedwarn ("empty declaration with storage class specifier "
2955 "does not redeclare tag");
2956 warned = 1;
2957 pending_xref_error ();
2959 else if (!declspecs->tag_defined_p
2960 && (declspecs->const_p
2961 || declspecs->volatile_p
2962 || declspecs->restrict_p))
2964 if (warned != 1)
2965 pedwarn ("empty declaration with type qualifier "
2966 "does not redeclare tag");
2967 warned = 1;
2968 pending_xref_error ();
2970 else
2972 pending_invalid_xref = 0;
2973 t = lookup_tag (code, name, 1);
2975 if (t == 0)
2977 t = make_node (code);
2978 pushtag (name, t);
2982 else
2984 if (warned != 1 && !in_system_header)
2986 pedwarn ("useless type name in empty declaration");
2987 warned = 1;
2991 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2993 pedwarn ("useless type name in empty declaration");
2994 warned = 1;
2997 pending_invalid_xref = 0;
2999 if (declspecs->inline_p)
3001 error ("%<inline%> in empty declaration");
3002 warned = 1;
3005 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3007 error ("%<auto%> in file-scope empty declaration");
3008 warned = 1;
3011 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3013 error ("%<register%> in file-scope empty declaration");
3014 warned = 1;
3017 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3019 warning (0, "useless storage class specifier in empty declaration");
3020 warned = 2;
3023 if (!warned && !in_system_header && declspecs->thread_p)
3025 warning (0, "useless %<__thread%> in empty declaration");
3026 warned = 2;
3029 if (!warned && !in_system_header && (declspecs->const_p
3030 || declspecs->volatile_p
3031 || declspecs->restrict_p))
3033 warning (0, "useless type qualifier in empty declaration");
3034 warned = 2;
3037 if (warned != 1)
3039 if (!found_tag)
3040 pedwarn ("empty declaration");
3045 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3046 bits. SPECS represents declaration specifiers that the grammar
3047 only permits to contain type qualifiers and attributes. */
3050 quals_from_declspecs (const struct c_declspecs *specs)
3052 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3053 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3054 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3055 gcc_assert (!specs->type
3056 && !specs->decl_attr
3057 && specs->typespec_word == cts_none
3058 && specs->storage_class == csc_none
3059 && !specs->typedef_p
3060 && !specs->explicit_signed_p
3061 && !specs->deprecated_p
3062 && !specs->long_p
3063 && !specs->long_long_p
3064 && !specs->short_p
3065 && !specs->signed_p
3066 && !specs->unsigned_p
3067 && !specs->complex_p
3068 && !specs->inline_p
3069 && !specs->thread_p);
3070 return quals;
3073 /* Construct an array declarator. EXPR is the expression inside [],
3074 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3075 applied to the pointer to which a parameter array is converted).
3076 STATIC_P is true if "static" is inside the [], false otherwise.
3077 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3078 length which is nevertheless a complete type, false otherwise. The
3079 field for the contained declarator is left to be filled in by
3080 set_array_declarator_inner. */
3082 struct c_declarator *
3083 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3084 bool vla_unspec_p)
3086 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3087 struct c_declarator);
3088 declarator->kind = cdk_array;
3089 declarator->declarator = 0;
3090 declarator->u.array.dimen = expr;
3091 if (quals)
3093 declarator->u.array.attrs = quals->attrs;
3094 declarator->u.array.quals = quals_from_declspecs (quals);
3096 else
3098 declarator->u.array.attrs = NULL_TREE;
3099 declarator->u.array.quals = 0;
3101 declarator->u.array.static_p = static_p;
3102 declarator->u.array.vla_unspec_p = vla_unspec_p;
3103 if (pedantic && !flag_isoc99)
3105 if (static_p || quals != NULL)
3106 pedwarn ("ISO C90 does not support %<static%> or type "
3107 "qualifiers in parameter array declarators");
3108 if (vla_unspec_p)
3109 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3111 if (vla_unspec_p)
3113 if (!current_scope->parm_flag)
3115 /* C99 6.7.5.2p4 */
3116 error ("%<[*]%> not allowed in other than function prototype scope");
3117 declarator->u.array.vla_unspec_p = false;
3118 return NULL;
3120 current_scope->had_vla_unspec = true;
3122 return declarator;
3125 /* Set the contained declarator of an array declarator. DECL is the
3126 declarator, as constructed by build_array_declarator; INNER is what
3127 appears on the left of the []. ABSTRACT_P is true if it is an
3128 abstract declarator, false otherwise; this is used to reject static
3129 and type qualifiers in abstract declarators, where they are not in
3130 the C99 grammar (subject to possible change in DR#289). */
3132 struct c_declarator *
3133 set_array_declarator_inner (struct c_declarator *decl,
3134 struct c_declarator *inner, bool abstract_p)
3136 decl->declarator = inner;
3137 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3138 || decl->u.array.attrs != NULL_TREE
3139 || decl->u.array.static_p))
3140 error ("static or type qualifiers in abstract declarator");
3141 return decl;
3144 /* INIT is a constructor that forms DECL's initializer. If the final
3145 element initializes a flexible array field, add the size of that
3146 initializer to DECL's size. */
3148 static void
3149 add_flexible_array_elts_to_size (tree decl, tree init)
3151 tree elt, type;
3153 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3154 return;
3156 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3157 type = TREE_TYPE (elt);
3158 if (TREE_CODE (type) == ARRAY_TYPE
3159 && TYPE_SIZE (type) == NULL_TREE
3160 && TYPE_DOMAIN (type) != NULL_TREE
3161 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3163 complete_array_type (&type, elt, false);
3164 DECL_SIZE (decl)
3165 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3166 DECL_SIZE_UNIT (decl)
3167 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3171 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3173 tree
3174 groktypename (struct c_type_name *type_name)
3176 tree type;
3177 tree attrs = type_name->specs->attrs;
3179 type_name->specs->attrs = NULL_TREE;
3181 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3182 false, NULL);
3184 /* Apply attributes. */
3185 decl_attributes (&type, attrs, 0);
3187 return type;
3190 /* Decode a declarator in an ordinary declaration or data definition.
3191 This is called as soon as the type information and variable name
3192 have been parsed, before parsing the initializer if any.
3193 Here we create the ..._DECL node, fill in its type,
3194 and put it on the list of decls for the current context.
3195 The ..._DECL node is returned as the value.
3197 Exception: for arrays where the length is not specified,
3198 the type is left null, to be filled in by `finish_decl'.
3200 Function definitions do not come here; they go to start_function
3201 instead. However, external and forward declarations of functions
3202 do go through here. Structure field declarations are done by
3203 grokfield and not through here. */
3205 tree
3206 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3207 bool initialized, tree attributes)
3209 tree decl;
3210 tree tem;
3212 /* An object declared as __attribute__((deprecated)) suppresses
3213 warnings of uses of other deprecated items. */
3214 if (lookup_attribute ("deprecated", attributes))
3215 deprecated_state = DEPRECATED_SUPPRESS;
3217 decl = grokdeclarator (declarator, declspecs,
3218 NORMAL, initialized, NULL);
3219 if (!decl)
3220 return 0;
3222 deprecated_state = DEPRECATED_NORMAL;
3224 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3225 && MAIN_NAME_P (DECL_NAME (decl)))
3226 warning (OPT_Wmain, "%q+D is usually a function", decl);
3228 if (initialized)
3229 /* Is it valid for this decl to have an initializer at all?
3230 If not, set INITIALIZED to zero, which will indirectly
3231 tell 'finish_decl' to ignore the initializer once it is parsed. */
3232 switch (TREE_CODE (decl))
3234 case TYPE_DECL:
3235 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3236 initialized = 0;
3237 break;
3239 case FUNCTION_DECL:
3240 error ("function %qD is initialized like a variable", decl);
3241 initialized = 0;
3242 break;
3244 case PARM_DECL:
3245 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3246 error ("parameter %qD is initialized", decl);
3247 initialized = 0;
3248 break;
3250 default:
3251 /* Don't allow initializations for incomplete types except for
3252 arrays which might be completed by the initialization. */
3254 /* This can happen if the array size is an undefined macro.
3255 We already gave a warning, so we don't need another one. */
3256 if (TREE_TYPE (decl) == error_mark_node)
3257 initialized = 0;
3258 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3260 /* A complete type is ok if size is fixed. */
3262 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3263 || C_DECL_VARIABLE_SIZE (decl))
3265 error ("variable-sized object may not be initialized");
3266 initialized = 0;
3269 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3271 error ("variable %qD has initializer but incomplete type", decl);
3272 initialized = 0;
3274 else if (C_DECL_VARIABLE_SIZE (decl))
3276 /* Although C99 is unclear about whether incomplete arrays
3277 of VLAs themselves count as VLAs, it does not make
3278 sense to permit them to be initialized given that
3279 ordinary VLAs may not be initialized. */
3280 error ("variable-sized object may not be initialized");
3281 initialized = 0;
3285 if (initialized)
3287 if (current_scope == file_scope)
3288 TREE_STATIC (decl) = 1;
3290 /* Tell 'pushdecl' this is an initialized decl
3291 even though we don't yet have the initializer expression.
3292 Also tell 'finish_decl' it may store the real initializer. */
3293 DECL_INITIAL (decl) = error_mark_node;
3296 /* If this is a function declaration, write a record describing it to the
3297 prototypes file (if requested). */
3299 if (TREE_CODE (decl) == FUNCTION_DECL)
3300 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3302 /* ANSI specifies that a tentative definition which is not merged with
3303 a non-tentative definition behaves exactly like a definition with an
3304 initializer equal to zero. (Section 3.7.2)
3306 -fno-common gives strict ANSI behavior, though this tends to break
3307 a large body of code that grew up without this rule.
3309 Thread-local variables are never common, since there's no entrenched
3310 body of code to break, and it allows more efficient variable references
3311 in the presence of dynamic linking. */
3313 if (TREE_CODE (decl) == VAR_DECL
3314 && !initialized
3315 && TREE_PUBLIC (decl)
3316 && !DECL_THREAD_LOCAL_P (decl)
3317 && !flag_no_common)
3318 DECL_COMMON (decl) = 1;
3320 /* Set attributes here so if duplicate decl, will have proper attributes. */
3321 decl_attributes (&decl, attributes, 0);
3323 #if WANT_C99_INLINE_SEMANTICS
3324 /* Handle gnu_inline attribute. */
3325 if (declspecs->inline_p
3326 && flag_isoc99
3327 && TREE_CODE (decl) == FUNCTION_DECL
3328 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
3330 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3332 else if (declspecs->storage_class != csc_static)
3333 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3335 #endif /* WANT_C99_INLINE_SEMANTICS */
3337 if (TREE_CODE (decl) == FUNCTION_DECL
3338 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3340 struct c_declarator *ce = declarator;
3342 if (ce->kind == cdk_pointer)
3343 ce = declarator->declarator;
3344 if (ce->kind == cdk_function)
3346 tree args = ce->u.arg_info->parms;
3347 for (; args; args = TREE_CHAIN (args))
3349 tree type = TREE_TYPE (args);
3350 if (type && INTEGRAL_TYPE_P (type)
3351 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3352 DECL_ARG_TYPE (args) = integer_type_node;
3357 if (TREE_CODE (decl) == FUNCTION_DECL
3358 && DECL_DECLARED_INLINE_P (decl)
3359 && DECL_UNINLINABLE (decl)
3360 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3361 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3362 decl);
3364 /* C99 6.7.4p3: An inline definition of a function with external
3365 linkage shall not contain a definition of a modifiable object
3366 with static storage duration... */
3367 if (TREE_CODE (decl) == VAR_DECL
3368 && current_scope != file_scope
3369 && TREE_STATIC (decl)
3370 && DECL_DECLARED_INLINE_P (current_function_decl)
3371 && DECL_EXTERNAL (current_function_decl))
3372 pedwarn ("%q+D is static but declared in inline function %qD "
3373 "which is not static", decl, current_function_decl);
3375 /* Add this decl to the current scope.
3376 TEM may equal DECL or it may be a previous decl of the same name. */
3377 tem = pushdecl (decl);
3379 if (initialized && DECL_EXTERNAL (tem))
3381 DECL_EXTERNAL (tem) = 0;
3382 TREE_STATIC (tem) = 1;
3385 return tem;
3388 /* Finish processing of a declaration;
3389 install its initial value.
3390 If the length of an array type is not known before,
3391 it must be determined now, from the initial value, or it is an error. */
3393 void
3394 finish_decl (tree decl, tree init, tree asmspec_tree)
3396 tree type = TREE_TYPE (decl);
3397 int was_incomplete = (DECL_SIZE (decl) == 0);
3398 const char *asmspec = 0;
3400 /* If a name was specified, get the string. */
3401 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3402 && DECL_FILE_SCOPE_P (decl))
3403 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3404 if (asmspec_tree)
3405 asmspec = TREE_STRING_POINTER (asmspec_tree);
3407 /* If `start_decl' didn't like having an initialization, ignore it now. */
3408 if (init != 0 && DECL_INITIAL (decl) == 0)
3409 init = 0;
3411 /* Don't crash if parm is initialized. */
3412 if (TREE_CODE (decl) == PARM_DECL)
3413 init = 0;
3415 if (init)
3416 store_init_value (decl, init);
3418 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3419 || TREE_CODE (decl) == FUNCTION_DECL
3420 || TREE_CODE (decl) == FIELD_DECL))
3421 objc_check_decl (decl);
3423 /* Deduce size of array from initialization, if not already known. */
3424 if (TREE_CODE (type) == ARRAY_TYPE
3425 && TYPE_DOMAIN (type) == 0
3426 && TREE_CODE (decl) != TYPE_DECL)
3428 bool do_default
3429 = (TREE_STATIC (decl)
3430 /* Even if pedantic, an external linkage array
3431 may have incomplete type at first. */
3432 ? pedantic && !TREE_PUBLIC (decl)
3433 : !DECL_EXTERNAL (decl));
3434 int failure
3435 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3436 do_default);
3438 /* Get the completed type made by complete_array_type. */
3439 type = TREE_TYPE (decl);
3441 switch (failure)
3443 case 1:
3444 error ("initializer fails to determine size of %q+D", decl);
3445 break;
3447 case 2:
3448 if (do_default)
3449 error ("array size missing in %q+D", decl);
3450 /* If a `static' var's size isn't known,
3451 make it extern as well as static, so it does not get
3452 allocated.
3453 If it is not `static', then do not mark extern;
3454 finish_incomplete_decl will give it a default size
3455 and it will get allocated. */
3456 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3457 DECL_EXTERNAL (decl) = 1;
3458 break;
3460 case 3:
3461 error ("zero or negative size array %q+D", decl);
3462 break;
3464 case 0:
3465 /* For global variables, update the copy of the type that
3466 exists in the binding. */
3467 if (TREE_PUBLIC (decl))
3469 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3470 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3471 b_ext = b_ext->shadowed;
3472 if (b_ext)
3474 if (b_ext->type)
3475 b_ext->type = composite_type (b_ext->type, type);
3476 else
3477 b_ext->type = type;
3480 break;
3482 default:
3483 gcc_unreachable ();
3486 if (DECL_INITIAL (decl))
3487 TREE_TYPE (DECL_INITIAL (decl)) = type;
3489 layout_decl (decl, 0);
3492 if (TREE_CODE (decl) == VAR_DECL)
3494 if (init && TREE_CODE (init) == CONSTRUCTOR)
3495 add_flexible_array_elts_to_size (decl, init);
3497 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3498 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3499 layout_decl (decl, 0);
3501 if (DECL_SIZE (decl) == 0
3502 /* Don't give an error if we already gave one earlier. */
3503 && TREE_TYPE (decl) != error_mark_node
3504 && (TREE_STATIC (decl)
3505 /* A static variable with an incomplete type
3506 is an error if it is initialized.
3507 Also if it is not file scope.
3508 Otherwise, let it through, but if it is not `extern'
3509 then it may cause an error message later. */
3510 ? (DECL_INITIAL (decl) != 0
3511 || !DECL_FILE_SCOPE_P (decl))
3512 /* An automatic variable with an incomplete type
3513 is an error. */
3514 : !DECL_EXTERNAL (decl)))
3516 error ("storage size of %q+D isn%'t known", decl);
3517 TREE_TYPE (decl) = error_mark_node;
3520 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3521 && DECL_SIZE (decl) != 0)
3523 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3524 constant_expression_warning (DECL_SIZE (decl));
3525 else
3526 error ("storage size of %q+D isn%'t constant", decl);
3529 if (TREE_USED (type))
3530 TREE_USED (decl) = 1;
3533 /* If this is a function and an assembler name is specified, reset DECL_RTL
3534 so we can give it its new name. Also, update built_in_decls if it
3535 was a normal built-in. */
3536 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3538 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3539 set_builtin_user_assembler_name (decl, asmspec);
3540 set_user_assembler_name (decl, asmspec);
3543 /* If #pragma weak was used, mark the decl weak now. */
3544 maybe_apply_pragma_weak (decl);
3546 /* Output the assembler code and/or RTL code for variables and functions,
3547 unless the type is an undefined structure or union.
3548 If not, it will get done when the type is completed. */
3550 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3552 /* Determine the ELF visibility. */
3553 if (TREE_PUBLIC (decl))
3554 c_determine_visibility (decl);
3556 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3557 if (c_dialect_objc ())
3558 objc_check_decl (decl);
3560 if (asmspec)
3562 /* If this is not a static variable, issue a warning.
3563 It doesn't make any sense to give an ASMSPEC for an
3564 ordinary, non-register local variable. Historically,
3565 GCC has accepted -- but ignored -- the ASMSPEC in
3566 this case. */
3567 if (!DECL_FILE_SCOPE_P (decl)
3568 && TREE_CODE (decl) == VAR_DECL
3569 && !C_DECL_REGISTER (decl)
3570 && !TREE_STATIC (decl))
3571 warning (0, "ignoring asm-specifier for non-static local "
3572 "variable %q+D", decl);
3573 else
3574 set_user_assembler_name (decl, asmspec);
3577 if (DECL_FILE_SCOPE_P (decl))
3579 if (DECL_INITIAL (decl) == NULL_TREE
3580 || DECL_INITIAL (decl) == error_mark_node)
3581 /* Don't output anything
3582 when a tentative file-scope definition is seen.
3583 But at end of compilation, do output code for them. */
3584 DECL_DEFER_OUTPUT (decl) = 1;
3585 rest_of_decl_compilation (decl, true, 0);
3587 else
3589 /* In conjunction with an ASMSPEC, the `register'
3590 keyword indicates that we should place the variable
3591 in a particular register. */
3592 if (asmspec && C_DECL_REGISTER (decl))
3594 DECL_HARD_REGISTER (decl) = 1;
3595 /* This cannot be done for a structure with volatile
3596 fields, on which DECL_REGISTER will have been
3597 reset. */
3598 if (!DECL_REGISTER (decl))
3599 error ("cannot put object with volatile field into register");
3602 if (TREE_CODE (decl) != FUNCTION_DECL)
3604 /* If we're building a variable sized type, and we might be
3605 reachable other than via the top of the current binding
3606 level, then create a new BIND_EXPR so that we deallocate
3607 the object at the right time. */
3608 /* Note that DECL_SIZE can be null due to errors. */
3609 if (DECL_SIZE (decl)
3610 && !TREE_CONSTANT (DECL_SIZE (decl))
3611 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3613 tree bind;
3614 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3615 TREE_SIDE_EFFECTS (bind) = 1;
3616 add_stmt (bind);
3617 BIND_EXPR_BODY (bind) = push_stmt_list ();
3619 add_stmt (build_stmt (DECL_EXPR, decl));
3624 if (!DECL_FILE_SCOPE_P (decl))
3626 /* Recompute the RTL of a local array now
3627 if it used to be an incomplete type. */
3628 if (was_incomplete
3629 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3631 /* If we used it already as memory, it must stay in memory. */
3632 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3633 /* If it's still incomplete now, no init will save it. */
3634 if (DECL_SIZE (decl) == 0)
3635 DECL_INITIAL (decl) = 0;
3640 /* If this was marked 'used', be sure it will be output. */
3641 if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3642 mark_decl_referenced (decl);
3644 if (TREE_CODE (decl) == TYPE_DECL)
3646 if (!DECL_FILE_SCOPE_P (decl)
3647 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3648 add_stmt (build_stmt (DECL_EXPR, decl));
3650 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3653 /* At the end of a declaration, throw away any variable type sizes
3654 of types defined inside that declaration. There is no use
3655 computing them in the following function definition. */
3656 if (current_scope == file_scope)
3657 get_pending_sizes ();
3659 /* Install a cleanup (aka destructor) if one was given. */
3660 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3662 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3663 if (attr)
3665 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3666 tree cleanup_decl = lookup_name (cleanup_id);
3667 tree cleanup;
3669 /* Build "cleanup(&decl)" for the destructor. */
3670 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3671 cleanup = build_tree_list (NULL_TREE, cleanup);
3672 cleanup = build_function_call (cleanup_decl, cleanup);
3674 /* Don't warn about decl unused; the cleanup uses it. */
3675 TREE_USED (decl) = 1;
3676 TREE_USED (cleanup_decl) = 1;
3678 /* Initialize EH, if we've been told to do so. */
3679 if (flag_exceptions && !c_eh_initialized_p)
3681 c_eh_initialized_p = true;
3682 eh_personality_libfunc
3683 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3684 ? "__gcc_personality_sj0"
3685 : "__gcc_personality_v0");
3686 default_init_unwind_resume_libfunc ();
3687 using_eh_for_cleanups ();
3690 push_cleanup (decl, cleanup, false);
3695 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3697 tree
3698 grokparm (const struct c_parm *parm)
3700 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3701 NULL);
3703 decl_attributes (&decl, parm->attrs, 0);
3705 return decl;
3708 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3709 and push that on the current scope. */
3711 void
3712 push_parm_decl (const struct c_parm *parm)
3714 tree decl;
3716 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3717 decl_attributes (&decl, parm->attrs, 0);
3719 decl = pushdecl (decl);
3721 finish_decl (decl, NULL_TREE, NULL_TREE);
3724 /* Mark all the parameter declarations to date as forward decls.
3725 Also diagnose use of this extension. */
3727 void
3728 mark_forward_parm_decls (void)
3730 struct c_binding *b;
3732 if (pedantic && !current_scope->warned_forward_parm_decls)
3734 pedwarn ("ISO C forbids forward parameter declarations");
3735 current_scope->warned_forward_parm_decls = true;
3738 for (b = current_scope->bindings; b; b = b->prev)
3739 if (TREE_CODE (b->decl) == PARM_DECL)
3740 TREE_ASM_WRITTEN (b->decl) = 1;
3743 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3744 literal, which may be an incomplete array type completed by the
3745 initializer; INIT is a CONSTRUCTOR that initializes the compound
3746 literal. */
3748 tree
3749 build_compound_literal (tree type, tree init)
3751 /* We do not use start_decl here because we have a type, not a declarator;
3752 and do not use finish_decl because the decl should be stored inside
3753 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3754 tree decl;
3755 tree complit;
3756 tree stmt;
3758 if (type == error_mark_node)
3759 return error_mark_node;
3761 decl = build_decl (VAR_DECL, NULL_TREE, type);
3762 DECL_EXTERNAL (decl) = 0;
3763 TREE_PUBLIC (decl) = 0;
3764 TREE_STATIC (decl) = (current_scope == file_scope);
3765 DECL_CONTEXT (decl) = current_function_decl;
3766 TREE_USED (decl) = 1;
3767 TREE_TYPE (decl) = type;
3768 TREE_READONLY (decl) = TYPE_READONLY (type);
3769 store_init_value (decl, init);
3771 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3773 int failure = complete_array_type (&TREE_TYPE (decl),
3774 DECL_INITIAL (decl), true);
3775 gcc_assert (!failure);
3777 type = TREE_TYPE (decl);
3778 TREE_TYPE (DECL_INITIAL (decl)) = type;
3781 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3782 return error_mark_node;
3784 stmt = build_stmt (DECL_EXPR, decl);
3785 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3786 TREE_SIDE_EFFECTS (complit) = 1;
3788 layout_decl (decl, 0);
3790 if (TREE_STATIC (decl))
3792 /* This decl needs a name for the assembler output. */
3793 set_compound_literal_name (decl);
3794 DECL_DEFER_OUTPUT (decl) = 1;
3795 DECL_COMDAT (decl) = 1;
3796 DECL_ARTIFICIAL (decl) = 1;
3797 DECL_IGNORED_P (decl) = 1;
3798 pushdecl (decl);
3799 rest_of_decl_compilation (decl, 1, 0);
3802 return complit;
3805 /* Determine whether TYPE is a structure with a flexible array member,
3806 or a union containing such a structure (possibly recursively). */
3808 static bool
3809 flexible_array_type_p (tree type)
3811 tree x;
3812 switch (TREE_CODE (type))
3814 case RECORD_TYPE:
3815 x = TYPE_FIELDS (type);
3816 if (x == NULL_TREE)
3817 return false;
3818 while (TREE_CHAIN (x) != NULL_TREE)
3819 x = TREE_CHAIN (x);
3820 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3821 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3822 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3823 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3824 return true;
3825 return false;
3826 case UNION_TYPE:
3827 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3829 if (flexible_array_type_p (TREE_TYPE (x)))
3830 return true;
3832 return false;
3833 default:
3834 return false;
3838 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3839 replacing with appropriate values if they are invalid. */
3840 static void
3841 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3843 tree type_mv;
3844 unsigned int max_width;
3845 unsigned HOST_WIDE_INT w;
3846 const char *name = orig_name ? orig_name: _("<anonymous>");
3848 /* Detect and ignore out of range field width and process valid
3849 field widths. */
3850 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3851 || TREE_CODE (*width) != INTEGER_CST)
3853 error ("bit-field %qs width not an integer constant", name);
3854 *width = integer_one_node;
3856 else
3858 constant_expression_warning (*width);
3859 if (tree_int_cst_sgn (*width) < 0)
3861 error ("negative width in bit-field %qs", name);
3862 *width = integer_one_node;
3864 else if (integer_zerop (*width) && orig_name)
3866 error ("zero width for bit-field %qs", name);
3867 *width = integer_one_node;
3871 /* Detect invalid bit-field type. */
3872 if (TREE_CODE (*type) != INTEGER_TYPE
3873 && TREE_CODE (*type) != BOOLEAN_TYPE
3874 && TREE_CODE (*type) != ENUMERAL_TYPE)
3876 error ("bit-field %qs has invalid type", name);
3877 *type = unsigned_type_node;
3880 type_mv = TYPE_MAIN_VARIANT (*type);
3881 if (pedantic
3882 && !in_system_header
3883 && type_mv != integer_type_node
3884 && type_mv != unsigned_type_node
3885 && type_mv != boolean_type_node)
3886 pedwarn ("type of bit-field %qs is a GCC extension", name);
3888 if (type_mv == boolean_type_node)
3889 max_width = CHAR_TYPE_SIZE;
3890 else
3891 max_width = TYPE_PRECISION (*type);
3893 if (0 < compare_tree_int (*width, max_width))
3895 error ("width of %qs exceeds its type", name);
3896 w = max_width;
3897 *width = build_int_cst (NULL_TREE, w);
3899 else
3900 w = tree_low_cst (*width, 1);
3902 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3904 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3905 if (!lt
3906 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3907 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3908 warning (0, "%qs is narrower than values of its type", name);
3913 /* Given declspecs and a declarator,
3914 determine the name and type of the object declared
3915 and construct a ..._DECL node for it.
3916 (In one case we can return a ..._TYPE node instead.
3917 For invalid input we sometimes return 0.)
3919 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3921 DECL_CONTEXT says which syntactic context this declaration is in:
3922 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3923 FUNCDEF for a function definition. Like NORMAL but a few different
3924 error messages in each case. Return value may be zero meaning
3925 this definition is too screwy to try to parse.
3926 PARM for a parameter declaration (either within a function prototype
3927 or before a function body). Make a PARM_DECL, or return void_type_node.
3928 TYPENAME if for a typename (in a cast or sizeof).
3929 Don't make a DECL node; just return the ..._TYPE node.
3930 FIELD for a struct or union field; make a FIELD_DECL.
3931 INITIALIZED is true if the decl has an initializer.
3932 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3933 representing the width of the bit-field.
3935 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3936 It may also be so in the PARM case, for a prototype where the
3937 argument type is specified but not the name.
3939 This function is where the complicated C meanings of `static'
3940 and `extern' are interpreted. */
3942 static tree
3943 grokdeclarator (const struct c_declarator *declarator,
3944 struct c_declspecs *declspecs,
3945 enum decl_context decl_context, bool initialized, tree *width)
3947 tree type = declspecs->type;
3948 bool threadp = declspecs->thread_p;
3949 enum c_storage_class storage_class = declspecs->storage_class;
3950 int constp;
3951 int restrictp;
3952 int volatilep;
3953 int type_quals = TYPE_UNQUALIFIED;
3954 const char *name, *orig_name;
3955 tree typedef_type = 0;
3956 bool funcdef_flag = false;
3957 bool funcdef_syntax = false;
3958 int size_varies = 0;
3959 tree decl_attr = declspecs->decl_attr;
3960 int array_ptr_quals = TYPE_UNQUALIFIED;
3961 tree array_ptr_attrs = NULL_TREE;
3962 int array_parm_static = 0;
3963 bool array_parm_vla_unspec_p = false;
3964 tree returned_attrs = NULL_TREE;
3965 bool bitfield = width != NULL;
3966 tree element_type;
3967 struct c_arg_info *arg_info = 0;
3969 if (decl_context == FUNCDEF)
3970 funcdef_flag = true, decl_context = NORMAL;
3972 /* Look inside a declarator for the name being declared
3973 and get it as a string, for an error message. */
3975 const struct c_declarator *decl = declarator;
3976 name = 0;
3978 while (decl)
3979 switch (decl->kind)
3981 case cdk_function:
3982 case cdk_array:
3983 case cdk_pointer:
3984 funcdef_syntax = (decl->kind == cdk_function);
3985 decl = decl->declarator;
3986 break;
3988 case cdk_attrs:
3989 decl = decl->declarator;
3990 break;
3992 case cdk_id:
3993 if (decl->u.id)
3994 name = IDENTIFIER_POINTER (decl->u.id);
3995 decl = 0;
3996 break;
3998 default:
3999 gcc_unreachable ();
4001 orig_name = name;
4002 if (name == 0)
4003 name = "type name";
4006 /* A function definition's declarator must have the form of
4007 a function declarator. */
4009 if (funcdef_flag && !funcdef_syntax)
4010 return 0;
4012 /* If this looks like a function definition, make it one,
4013 even if it occurs where parms are expected.
4014 Then store_parm_decls will reject it and not use it as a parm. */
4015 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4016 decl_context = PARM;
4018 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4019 warn_deprecated_use (declspecs->type);
4021 if ((decl_context == NORMAL || decl_context == FIELD)
4022 && current_scope == file_scope
4023 && variably_modified_type_p (type, NULL_TREE))
4025 error ("variably modified %qs at file scope", name);
4026 type = integer_type_node;
4029 typedef_type = type;
4030 size_varies = C_TYPE_VARIABLE_SIZE (type);
4032 /* Diagnose defaulting to "int". */
4034 if (declspecs->default_int_p && !in_system_header)
4036 /* Issue a warning if this is an ISO C 99 program or if
4037 -Wreturn-type and this is a function, or if -Wimplicit;
4038 prefer the former warning since it is more explicit. */
4039 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4040 && funcdef_flag)
4041 warn_about_return_type = 1;
4042 else if (warn_implicit_int || flag_isoc99)
4043 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
4046 /* Adjust the type if a bit-field is being declared,
4047 -funsigned-bitfields applied and the type is not explicitly
4048 "signed". */
4049 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4050 && TREE_CODE (type) == INTEGER_TYPE)
4051 type = c_common_unsigned_type (type);
4053 /* Figure out the type qualifiers for the declaration. There are
4054 two ways a declaration can become qualified. One is something
4055 like `const int i' where the `const' is explicit. Another is
4056 something like `typedef const int CI; CI i' where the type of the
4057 declaration contains the `const'. A third possibility is that
4058 there is a type qualifier on the element type of a typedefed
4059 array type, in which case we should extract that qualifier so
4060 that c_apply_type_quals_to_decls receives the full list of
4061 qualifiers to work with (C90 is not entirely clear about whether
4062 duplicate qualifiers should be diagnosed in this case, but it
4063 seems most appropriate to do so). */
4064 element_type = strip_array_types (type);
4065 constp = declspecs->const_p + TYPE_READONLY (element_type);
4066 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4067 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4068 if (pedantic && !flag_isoc99)
4070 if (constp > 1)
4071 pedwarn ("duplicate %<const%>");
4072 if (restrictp > 1)
4073 pedwarn ("duplicate %<restrict%>");
4074 if (volatilep > 1)
4075 pedwarn ("duplicate %<volatile%>");
4077 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4078 type = TYPE_MAIN_VARIANT (type);
4079 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4080 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4081 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4083 /* Warn about storage classes that are invalid for certain
4084 kinds of declarations (parameters, typenames, etc.). */
4086 if (funcdef_flag
4087 && (threadp
4088 || storage_class == csc_auto
4089 || storage_class == csc_register
4090 || storage_class == csc_typedef))
4092 if (storage_class == csc_auto
4093 && (pedantic || current_scope == file_scope))
4094 pedwarn ("function definition declared %<auto%>");
4095 if (storage_class == csc_register)
4096 error ("function definition declared %<register%>");
4097 if (storage_class == csc_typedef)
4098 error ("function definition declared %<typedef%>");
4099 if (threadp)
4100 error ("function definition declared %<__thread%>");
4101 threadp = false;
4102 if (storage_class == csc_auto
4103 || storage_class == csc_register
4104 || storage_class == csc_typedef)
4105 storage_class = csc_none;
4107 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4109 if (decl_context == PARM && storage_class == csc_register)
4111 else
4113 switch (decl_context)
4115 case FIELD:
4116 error ("storage class specified for structure field %qs",
4117 name);
4118 break;
4119 case PARM:
4120 error ("storage class specified for parameter %qs", name);
4121 break;
4122 default:
4123 error ("storage class specified for typename");
4124 break;
4126 storage_class = csc_none;
4127 threadp = false;
4130 else if (storage_class == csc_extern
4131 && initialized
4132 && !funcdef_flag)
4134 /* 'extern' with initialization is invalid if not at file scope. */
4135 if (current_scope == file_scope)
4137 /* It is fine to have 'extern const' when compiling at C
4138 and C++ intersection. */
4139 if (!(warn_cxx_compat && constp))
4140 warning (0, "%qs initialized and declared %<extern%>", name);
4142 else
4143 error ("%qs has both %<extern%> and initializer", name);
4145 else if (current_scope == file_scope)
4147 if (storage_class == csc_auto)
4148 error ("file-scope declaration of %qs specifies %<auto%>", name);
4149 if (pedantic && storage_class == csc_register)
4150 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4152 else
4154 if (storage_class == csc_extern && funcdef_flag)
4155 error ("nested function %qs declared %<extern%>", name);
4156 else if (threadp && storage_class == csc_none)
4158 error ("function-scope %qs implicitly auto and declared "
4159 "%<__thread%>",
4160 name);
4161 threadp = false;
4165 /* Now figure out the structure of the declarator proper.
4166 Descend through it, creating more complex types, until we reach
4167 the declared identifier (or NULL_TREE, in an absolute declarator).
4168 At each stage we maintain an unqualified version of the type
4169 together with any qualifiers that should be applied to it with
4170 c_build_qualified_type; this way, array types including
4171 multidimensional array types are first built up in unqualified
4172 form and then the qualified form is created with
4173 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4175 while (declarator && declarator->kind != cdk_id)
4177 if (type == error_mark_node)
4179 declarator = declarator->declarator;
4180 continue;
4183 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4184 a cdk_pointer (for *...),
4185 a cdk_function (for ...(...)),
4186 a cdk_attrs (for nested attributes),
4187 or a cdk_id (for the name being declared
4188 or the place in an absolute declarator
4189 where the name was omitted).
4190 For the last case, we have just exited the loop.
4192 At this point, TYPE is the type of elements of an array,
4193 or for a function to return, or for a pointer to point to.
4194 After this sequence of ifs, TYPE is the type of the
4195 array or function or pointer, and DECLARATOR has had its
4196 outermost layer removed. */
4198 if (array_ptr_quals != TYPE_UNQUALIFIED
4199 || array_ptr_attrs != NULL_TREE
4200 || array_parm_static)
4202 /* Only the innermost declarator (making a parameter be of
4203 array type which is converted to pointer type)
4204 may have static or type qualifiers. */
4205 error ("static or type qualifiers in non-parameter array declarator");
4206 array_ptr_quals = TYPE_UNQUALIFIED;
4207 array_ptr_attrs = NULL_TREE;
4208 array_parm_static = 0;
4211 switch (declarator->kind)
4213 case cdk_attrs:
4215 /* A declarator with embedded attributes. */
4216 tree attrs = declarator->u.attrs;
4217 const struct c_declarator *inner_decl;
4218 int attr_flags = 0;
4219 declarator = declarator->declarator;
4220 inner_decl = declarator;
4221 while (inner_decl->kind == cdk_attrs)
4222 inner_decl = inner_decl->declarator;
4223 if (inner_decl->kind == cdk_id)
4224 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4225 else if (inner_decl->kind == cdk_function)
4226 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4227 else if (inner_decl->kind == cdk_array)
4228 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4229 returned_attrs = decl_attributes (&type,
4230 chainon (returned_attrs, attrs),
4231 attr_flags);
4232 break;
4234 case cdk_array:
4236 tree itype = NULL_TREE;
4237 tree size = declarator->u.array.dimen;
4238 /* The index is a signed object `sizetype' bits wide. */
4239 tree index_type = c_common_signed_type (sizetype);
4241 array_ptr_quals = declarator->u.array.quals;
4242 array_ptr_attrs = declarator->u.array.attrs;
4243 array_parm_static = declarator->u.array.static_p;
4244 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4246 declarator = declarator->declarator;
4248 /* Check for some types that there cannot be arrays of. */
4250 if (VOID_TYPE_P (type))
4252 error ("declaration of %qs as array of voids", name);
4253 type = error_mark_node;
4256 if (TREE_CODE (type) == FUNCTION_TYPE)
4258 error ("declaration of %qs as array of functions", name);
4259 type = error_mark_node;
4262 if (pedantic && !in_system_header && flexible_array_type_p (type))
4263 pedwarn ("invalid use of structure with flexible array member");
4265 if (size == error_mark_node)
4266 type = error_mark_node;
4268 if (type == error_mark_node)
4269 continue;
4271 /* If size was specified, set ITYPE to a range-type for
4272 that size. Otherwise, ITYPE remains null. finish_decl
4273 may figure it out from an initial value. */
4275 if (size)
4277 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4278 lvalue. */
4279 STRIP_TYPE_NOPS (size);
4281 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4283 error ("size of array %qs has non-integer type", name);
4284 size = integer_one_node;
4287 if (pedantic && integer_zerop (size))
4288 pedwarn ("ISO C forbids zero-size array %qs", name);
4290 if (TREE_CODE (size) == INTEGER_CST)
4292 constant_expression_warning (size);
4293 if (tree_int_cst_sgn (size) < 0)
4295 error ("size of array %qs is negative", name);
4296 size = integer_one_node;
4299 else if ((decl_context == NORMAL || decl_context == FIELD)
4300 && current_scope == file_scope)
4302 error ("variably modified %qs at file scope", name);
4303 size = integer_one_node;
4305 else
4307 /* Make sure the array size remains visibly
4308 nonconstant even if it is (eg) a const variable
4309 with known value. */
4310 size_varies = 1;
4312 if (!flag_isoc99 && pedantic)
4314 if (TREE_CONSTANT (size))
4315 pedwarn ("ISO C90 forbids array %qs whose size "
4316 "can%'t be evaluated",
4317 name);
4318 else
4319 pedwarn ("ISO C90 forbids variable-size array %qs",
4320 name);
4324 if (integer_zerop (size))
4326 /* A zero-length array cannot be represented with
4327 an unsigned index type, which is what we'll
4328 get with build_index_type. Create an
4329 open-ended range instead. */
4330 itype = build_range_type (sizetype, size, NULL_TREE);
4332 else
4334 /* Arrange for the SAVE_EXPR on the inside of the
4335 MINUS_EXPR, which allows the -1 to get folded
4336 with the +1 that happens when building TYPE_SIZE. */
4337 if (size_varies)
4338 size = variable_size (size);
4340 /* Compute the maximum valid index, that is, size
4341 - 1. Do the calculation in index_type, so that
4342 if it is a variable the computations will be
4343 done in the proper mode. */
4344 itype = fold_build2 (MINUS_EXPR, index_type,
4345 convert (index_type, size),
4346 convert (index_type,
4347 size_one_node));
4349 /* If that overflowed, the array is too big. ???
4350 While a size of INT_MAX+1 technically shouldn't
4351 cause an overflow (because we subtract 1), the
4352 overflow is recorded during the conversion to
4353 index_type, before the subtraction. Handling
4354 this case seems like an unnecessary
4355 complication. */
4356 if (TREE_CODE (itype) == INTEGER_CST
4357 && TREE_OVERFLOW (itype))
4359 error ("size of array %qs is too large", name);
4360 type = error_mark_node;
4361 continue;
4364 itype = build_index_type (itype);
4367 else if (decl_context == FIELD)
4369 if (pedantic && !flag_isoc99 && !in_system_header)
4370 pedwarn ("ISO C90 does not support flexible array members");
4372 /* ISO C99 Flexible array members are effectively
4373 identical to GCC's zero-length array extension. */
4374 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4376 else if (decl_context == PARM)
4378 if (array_parm_vla_unspec_p)
4380 if (! orig_name)
4382 /* C99 6.7.5.2p4 */
4383 error ("%<[*]%> not allowed in other than a declaration");
4386 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4387 size_varies = 1;
4390 else if (decl_context == TYPENAME)
4392 if (array_parm_vla_unspec_p)
4394 /* The error is printed elsewhere. We use this to
4395 avoid messing up with incomplete array types of
4396 the same type, that would otherwise be modified
4397 below. */
4398 itype = build_range_type (sizetype, size_zero_node,
4399 NULL_TREE);
4403 /* Complain about arrays of incomplete types. */
4404 if (!COMPLETE_TYPE_P (type))
4406 error ("array type has incomplete element type");
4407 type = error_mark_node;
4409 else
4410 /* When itype is NULL, a shared incomplete array type is
4411 returned for all array of a given type. Elsewhere we
4412 make sure we don't complete that type before copying
4413 it, but here we want to make sure we don't ever
4414 modify the shared type, so we gcc_assert (itype)
4415 below. */
4416 type = build_array_type (type, itype);
4418 if (type != error_mark_node)
4420 if (size_varies)
4422 /* It is ok to modify type here even if itype is
4423 NULL: if size_varies, we're in a
4424 multi-dimensional array and the inner type has
4425 variable size, so the enclosing shared array type
4426 must too. */
4427 if (size && TREE_CODE (size) == INTEGER_CST)
4428 type
4429 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4430 C_TYPE_VARIABLE_SIZE (type) = 1;
4433 /* The GCC extension for zero-length arrays differs from
4434 ISO flexible array members in that sizeof yields
4435 zero. */
4436 if (size && integer_zerop (size))
4438 gcc_assert (itype);
4439 TYPE_SIZE (type) = bitsize_zero_node;
4440 TYPE_SIZE_UNIT (type) = size_zero_node;
4442 if (array_parm_vla_unspec_p)
4444 gcc_assert (itype);
4445 /* The type is complete. C99 6.7.5.2p4 */
4446 TYPE_SIZE (type) = bitsize_zero_node;
4447 TYPE_SIZE_UNIT (type) = size_zero_node;
4451 if (decl_context != PARM
4452 && (array_ptr_quals != TYPE_UNQUALIFIED
4453 || array_ptr_attrs != NULL_TREE
4454 || array_parm_static))
4456 error ("static or type qualifiers in non-parameter array declarator");
4457 array_ptr_quals = TYPE_UNQUALIFIED;
4458 array_ptr_attrs = NULL_TREE;
4459 array_parm_static = 0;
4461 break;
4463 case cdk_function:
4465 /* Say it's a definition only for the declarator closest
4466 to the identifier, apart possibly from some
4467 attributes. */
4468 bool really_funcdef = false;
4469 tree arg_types;
4470 if (funcdef_flag)
4472 const struct c_declarator *t = declarator->declarator;
4473 while (t->kind == cdk_attrs)
4474 t = t->declarator;
4475 really_funcdef = (t->kind == cdk_id);
4478 /* Declaring a function type. Make sure we have a valid
4479 type for the function to return. */
4480 if (type == error_mark_node)
4481 continue;
4483 size_varies = 0;
4485 /* Warn about some types functions can't return. */
4486 if (TREE_CODE (type) == FUNCTION_TYPE)
4488 error ("%qs declared as function returning a function", name);
4489 type = integer_type_node;
4491 if (TREE_CODE (type) == ARRAY_TYPE)
4493 error ("%qs declared as function returning an array", name);
4494 type = integer_type_node;
4497 /* Construct the function type and go to the next
4498 inner layer of declarator. */
4499 arg_info = declarator->u.arg_info;
4500 arg_types = grokparms (arg_info, really_funcdef);
4501 if (really_funcdef)
4502 put_pending_sizes (arg_info->pending_sizes);
4504 /* Type qualifiers before the return type of the function
4505 qualify the return type, not the function type. */
4506 if (type_quals)
4508 /* Type qualifiers on a function return type are
4509 normally permitted by the standard but have no
4510 effect, so give a warning at -Wreturn-type.
4511 Qualifiers on a void return type are banned on
4512 function definitions in ISO C; GCC used to used
4513 them for noreturn functions. */
4514 if (VOID_TYPE_P (type) && really_funcdef)
4515 pedwarn ("function definition has qualified void return type");
4516 else
4517 warning (OPT_Wreturn_type,
4518 "type qualifiers ignored on function return type");
4520 type = c_build_qualified_type (type, type_quals);
4522 type_quals = TYPE_UNQUALIFIED;
4524 type = build_function_type (type, arg_types);
4525 declarator = declarator->declarator;
4527 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4528 the formal parameter list of this FUNCTION_TYPE to point to
4529 the FUNCTION_TYPE node itself. */
4531 tree link;
4533 for (link = arg_info->tags;
4534 link;
4535 link = TREE_CHAIN (link))
4536 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4538 break;
4540 case cdk_pointer:
4542 /* Merge any constancy or volatility into the target type
4543 for the pointer. */
4545 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4546 && type_quals)
4547 pedwarn ("ISO C forbids qualified function types");
4548 if (type_quals)
4549 type = c_build_qualified_type (type, type_quals);
4550 size_varies = 0;
4552 /* When the pointed-to type involves components of variable size,
4553 care must be taken to ensure that the size evaluation code is
4554 emitted early enough to dominate all the possible later uses
4555 and late enough for the variables on which it depends to have
4556 been assigned.
4558 This is expected to happen automatically when the pointed-to
4559 type has a name/declaration of it's own, but special attention
4560 is required if the type is anonymous.
4562 We handle the NORMAL and FIELD contexts here by attaching an
4563 artificial TYPE_DECL to such pointed-to type. This forces the
4564 sizes evaluation at a safe point and ensures it is not deferred
4565 until e.g. within a deeper conditional context.
4567 We expect nothing to be needed here for PARM or TYPENAME.
4568 Pushing a TYPE_DECL at this point for TYPENAME would actually
4569 be incorrect, as we might be in the middle of an expression
4570 with side effects on the pointed-to type size "arguments" prior
4571 to the pointer declaration point and the fake TYPE_DECL in the
4572 enclosing context would force the size evaluation prior to the
4573 side effects. */
4575 if (!TYPE_NAME (type)
4576 && (decl_context == NORMAL || decl_context == FIELD)
4577 && variably_modified_type_p (type, NULL_TREE))
4579 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4580 DECL_ARTIFICIAL (decl) = 1;
4581 pushdecl (decl);
4582 finish_decl (decl, NULL_TREE, NULL_TREE);
4583 TYPE_NAME (type) = decl;
4586 type = build_pointer_type (type);
4588 /* Process type qualifiers (such as const or volatile)
4589 that were given inside the `*'. */
4590 type_quals = declarator->u.pointer_quals;
4592 declarator = declarator->declarator;
4593 break;
4595 default:
4596 gcc_unreachable ();
4600 /* Now TYPE has the actual type, apart from any qualifiers in
4601 TYPE_QUALS. */
4603 /* Check the type and width of a bit-field. */
4604 if (bitfield)
4605 check_bitfield_type_and_width (&type, width, orig_name);
4607 /* Did array size calculations overflow? */
4609 if (TREE_CODE (type) == ARRAY_TYPE
4610 && COMPLETE_TYPE_P (type)
4611 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4612 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4614 error ("size of array %qs is too large", name);
4615 /* If we proceed with the array type as it is, we'll eventually
4616 crash in tree_low_cst(). */
4617 type = error_mark_node;
4620 /* If this is declaring a typedef name, return a TYPE_DECL. */
4622 if (storage_class == csc_typedef)
4624 tree decl;
4625 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4626 && type_quals)
4627 pedwarn ("ISO C forbids qualified function types");
4628 if (type_quals)
4629 type = c_build_qualified_type (type, type_quals);
4630 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4631 if (declspecs->explicit_signed_p)
4632 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4633 decl_attributes (&decl, returned_attrs, 0);
4634 if (declspecs->inline_p)
4635 pedwarn ("typedef %q+D declared %<inline%>", decl);
4636 return decl;
4639 /* If this is a type name (such as, in a cast or sizeof),
4640 compute the type and return it now. */
4642 if (decl_context == TYPENAME)
4644 /* Note that the grammar rejects storage classes in typenames
4645 and fields. */
4646 gcc_assert (storage_class == csc_none && !threadp
4647 && !declspecs->inline_p);
4648 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4649 && type_quals)
4650 pedwarn ("ISO C forbids const or volatile function types");
4651 if (type_quals)
4652 type = c_build_qualified_type (type, type_quals);
4653 decl_attributes (&type, returned_attrs, 0);
4654 return type;
4657 if (pedantic && decl_context == FIELD
4658 && variably_modified_type_p (type, NULL_TREE))
4660 /* C99 6.7.2.1p8 */
4661 pedwarn ("a member of a structure or union cannot have a variably modified type");
4664 /* Aside from typedefs and type names (handle above),
4665 `void' at top level (not within pointer)
4666 is allowed only in public variables.
4667 We don't complain about parms either, but that is because
4668 a better error message can be made later. */
4670 if (VOID_TYPE_P (type) && decl_context != PARM
4671 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4672 && (storage_class == csc_extern
4673 || (current_scope == file_scope
4674 && !(storage_class == csc_static
4675 || storage_class == csc_register)))))
4677 error ("variable or field %qs declared void", name);
4678 type = integer_type_node;
4681 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4682 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4685 tree decl;
4687 if (decl_context == PARM)
4689 tree type_as_written;
4690 tree promoted_type;
4692 /* A parameter declared as an array of T is really a pointer to T.
4693 One declared as a function is really a pointer to a function. */
4695 if (TREE_CODE (type) == ARRAY_TYPE)
4697 /* Transfer const-ness of array into that of type pointed to. */
4698 type = TREE_TYPE (type);
4699 if (type_quals)
4700 type = c_build_qualified_type (type, type_quals);
4701 type = build_pointer_type (type);
4702 type_quals = array_ptr_quals;
4704 /* We don't yet implement attributes in this context. */
4705 if (array_ptr_attrs != NULL_TREE)
4706 warning (OPT_Wattributes,
4707 "attributes in parameter array declarator ignored");
4709 size_varies = 0;
4711 else if (TREE_CODE (type) == FUNCTION_TYPE)
4713 if (pedantic && type_quals)
4714 pedwarn ("ISO C forbids qualified function types");
4715 if (type_quals)
4716 type = c_build_qualified_type (type, type_quals);
4717 type = build_pointer_type (type);
4718 type_quals = TYPE_UNQUALIFIED;
4720 else if (type_quals)
4721 type = c_build_qualified_type (type, type_quals);
4723 type_as_written = type;
4725 decl = build_decl (PARM_DECL, declarator->u.id, type);
4726 if (size_varies)
4727 C_DECL_VARIABLE_SIZE (decl) = 1;
4729 /* Compute the type actually passed in the parmlist,
4730 for the case where there is no prototype.
4731 (For example, shorts and chars are passed as ints.)
4732 When there is a prototype, this is overridden later. */
4734 if (type == error_mark_node)
4735 promoted_type = type;
4736 else
4737 promoted_type = c_type_promotes_to (type);
4739 DECL_ARG_TYPE (decl) = promoted_type;
4740 if (declspecs->inline_p)
4741 pedwarn ("parameter %q+D declared %<inline%>", decl);
4743 else if (decl_context == FIELD)
4745 /* Note that the grammar rejects storage classes in typenames
4746 and fields. */
4747 gcc_assert (storage_class == csc_none && !threadp
4748 && !declspecs->inline_p);
4750 /* Structure field. It may not be a function. */
4752 if (TREE_CODE (type) == FUNCTION_TYPE)
4754 error ("field %qs declared as a function", name);
4755 type = build_pointer_type (type);
4757 else if (TREE_CODE (type) != ERROR_MARK
4758 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4760 error ("field %qs has incomplete type", name);
4761 type = error_mark_node;
4763 type = c_build_qualified_type (type, type_quals);
4764 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4765 DECL_NONADDRESSABLE_P (decl) = bitfield;
4767 if (size_varies)
4768 C_DECL_VARIABLE_SIZE (decl) = 1;
4770 else if (TREE_CODE (type) == FUNCTION_TYPE)
4772 if (storage_class == csc_register || threadp)
4774 error ("invalid storage class for function %qs", name);
4776 else if (current_scope != file_scope)
4778 /* Function declaration not at file scope. Storage
4779 classes other than `extern' are not allowed, C99
4780 6.7.1p5, and `extern' makes no difference. However,
4781 GCC allows 'auto', perhaps with 'inline', to support
4782 nested functions. */
4783 if (storage_class == csc_auto)
4785 if (pedantic)
4786 pedwarn ("invalid storage class for function %qs", name);
4788 else if (storage_class == csc_static)
4790 error ("invalid storage class for function %qs", name);
4791 if (funcdef_flag)
4792 storage_class = declspecs->storage_class = csc_none;
4793 else
4794 return 0;
4798 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4799 decl = build_decl_attribute_variant (decl, decl_attr);
4801 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4803 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4804 pedwarn ("ISO C forbids qualified function types");
4806 /* GNU C interprets a volatile-qualified function type to indicate
4807 that the function does not return. */
4808 if ((type_quals & TYPE_QUAL_VOLATILE)
4809 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4810 warning (0, "%<noreturn%> function returns non-void value");
4812 /* Every function declaration is an external reference
4813 (DECL_EXTERNAL) except for those which are not at file
4814 scope and are explicitly declared "auto". This is
4815 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4816 GCC to signify a forward declaration of a nested function. */
4817 if (storage_class == csc_auto && current_scope != file_scope)
4818 DECL_EXTERNAL (decl) = 0;
4819 /* In C99, a function which is declared 'inline' with 'extern'
4820 is not an external reference (which is confusing). It
4821 means that the later definition of the function must be output
4822 in this file, C99 6.7.4p6. In GNU C89, a function declared
4823 'extern inline' is an external reference. */
4824 else if (declspecs->inline_p && storage_class != csc_static)
4825 #if WANT_C99_INLINE_SEMANTICS
4826 DECL_EXTERNAL (decl) = (storage_class == csc_extern) == !flag_isoc99;
4827 #else
4828 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4829 #endif
4830 else
4831 DECL_EXTERNAL (decl) = !initialized;
4833 /* Record absence of global scope for `static' or `auto'. */
4834 TREE_PUBLIC (decl)
4835 = !(storage_class == csc_static || storage_class == csc_auto);
4837 /* For a function definition, record the argument information
4838 block where store_parm_decls will look for it. */
4839 if (funcdef_flag)
4840 current_function_arg_info = arg_info;
4842 if (declspecs->default_int_p)
4843 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4845 /* Record presence of `inline', if it is reasonable. */
4846 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4848 if (declspecs->inline_p)
4849 pedwarn ("cannot inline function %<main%>");
4851 else if (declspecs->inline_p)
4853 /* Record that the function is declared `inline'. */
4854 DECL_DECLARED_INLINE_P (decl) = 1;
4856 /* Do not mark bare declarations as DECL_INLINE. Doing so
4857 in the presence of multiple declarations can result in
4858 the abstract origin pointing between the declarations,
4859 which will confuse dwarf2out. */
4860 if (initialized)
4861 DECL_INLINE (decl) = 1;
4863 /* If -finline-functions, assume it can be inlined. This does
4864 two things: let the function be deferred until it is actually
4865 needed, and let dwarf2 know that the function is inlinable. */
4866 else if (flag_inline_trees == 2 && initialized)
4867 DECL_INLINE (decl) = 1;
4869 else
4871 /* It's a variable. */
4872 /* An uninitialized decl with `extern' is a reference. */
4873 int extern_ref = !initialized && storage_class == csc_extern;
4875 type = c_build_qualified_type (type, type_quals);
4877 /* C99 6.2.2p7: It is invalid (compile-time undefined
4878 behavior) to create an 'extern' declaration for a
4879 variable if there is a global declaration that is
4880 'static' and the global declaration is not visible.
4881 (If the static declaration _is_ currently visible,
4882 the 'extern' declaration is taken to refer to that decl.) */
4883 if (extern_ref && current_scope != file_scope)
4885 tree global_decl = identifier_global_value (declarator->u.id);
4886 tree visible_decl = lookup_name (declarator->u.id);
4888 if (global_decl
4889 && global_decl != visible_decl
4890 && TREE_CODE (global_decl) == VAR_DECL
4891 && !TREE_PUBLIC (global_decl))
4892 error ("variable previously declared %<static%> redeclared "
4893 "%<extern%>");
4896 decl = build_decl (VAR_DECL, declarator->u.id, type);
4897 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4898 if (size_varies)
4899 C_DECL_VARIABLE_SIZE (decl) = 1;
4901 if (declspecs->inline_p)
4902 pedwarn ("variable %q+D declared %<inline%>", decl);
4904 /* At file scope, an initialized extern declaration may follow
4905 a static declaration. In that case, DECL_EXTERNAL will be
4906 reset later in start_decl. */
4907 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4909 /* At file scope, the presence of a `static' or `register' storage
4910 class specifier, or the absence of all storage class specifiers
4911 makes this declaration a definition (perhaps tentative). Also,
4912 the absence of `static' makes it public. */
4913 if (current_scope == file_scope)
4915 TREE_PUBLIC (decl) = storage_class != csc_static;
4916 TREE_STATIC (decl) = !extern_ref;
4918 /* Not at file scope, only `static' makes a static definition. */
4919 else
4921 TREE_STATIC (decl) = (storage_class == csc_static);
4922 TREE_PUBLIC (decl) = extern_ref;
4925 if (threadp)
4927 if (targetm.have_tls)
4928 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
4929 else
4930 /* A mere warning is sure to result in improper semantics
4931 at runtime. Don't bother to allow this to compile. */
4932 error ("thread-local storage not supported for this target");
4936 if (storage_class == csc_extern
4937 && variably_modified_type_p (type, NULL_TREE))
4939 /* C99 6.7.5.2p2 */
4940 error ("object with variably modified type must have no linkage");
4943 /* Record `register' declaration for warnings on &
4944 and in case doing stupid register allocation. */
4946 if (storage_class == csc_register)
4948 C_DECL_REGISTER (decl) = 1;
4949 DECL_REGISTER (decl) = 1;
4952 /* Record constancy and volatility. */
4953 c_apply_type_quals_to_decl (type_quals, decl);
4955 /* If a type has volatile components, it should be stored in memory.
4956 Otherwise, the fact that those components are volatile
4957 will be ignored, and would even crash the compiler.
4958 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
4959 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
4960 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
4961 || TREE_CODE (decl) == RESULT_DECL))
4963 /* It is not an error for a structure with volatile fields to
4964 be declared register, but reset DECL_REGISTER since it
4965 cannot actually go in a register. */
4966 int was_reg = C_DECL_REGISTER (decl);
4967 C_DECL_REGISTER (decl) = 0;
4968 DECL_REGISTER (decl) = 0;
4969 c_mark_addressable (decl);
4970 C_DECL_REGISTER (decl) = was_reg;
4973 /* This is the earliest point at which we might know the assembler
4974 name of a variable. Thus, if it's known before this, die horribly. */
4975 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4977 decl_attributes (&decl, returned_attrs, 0);
4979 return decl;
4983 /* Decode the parameter-list info for a function type or function definition.
4984 The argument is the value returned by `get_parm_info' (or made in c-parse.c
4985 if there is an identifier list instead of a parameter decl list).
4986 These two functions are separate because when a function returns
4987 or receives functions then each is called multiple times but the order
4988 of calls is different. The last call to `grokparms' is always the one
4989 that contains the formal parameter names of a function definition.
4991 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4993 FUNCDEF_FLAG is true for a function definition, false for
4994 a mere declaration. A nonempty identifier-list gets an error message
4995 when FUNCDEF_FLAG is false. */
4997 static tree
4998 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
5000 tree arg_types = arg_info->types;
5002 if (funcdef_flag && arg_info->had_vla_unspec)
5004 /* A function definition isn't function prototype scope C99 6.2.1p4. */
5005 /* C99 6.7.5.2p4 */
5006 error ("%<[*]%> not allowed in other than function prototype scope");
5009 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5010 warning (OPT_Wstrict_prototypes,
5011 "function declaration isn%'t a prototype");
5013 if (arg_types == error_mark_node)
5014 return 0; /* don't set TYPE_ARG_TYPES in this case */
5016 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5018 if (!funcdef_flag)
5019 pedwarn ("parameter names (without types) in function declaration");
5021 arg_info->parms = arg_info->types;
5022 arg_info->types = 0;
5023 return 0;
5025 else
5027 tree parm, type, typelt;
5028 unsigned int parmno;
5030 /* If there is a parameter of incomplete type in a definition,
5031 this is an error. In a declaration this is valid, and a
5032 struct or union type may be completed later, before any calls
5033 or definition of the function. In the case where the tag was
5034 first declared within the parameter list, a warning has
5035 already been given. If a parameter has void type, then
5036 however the function cannot be defined or called, so
5037 warn. */
5039 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5040 parm;
5041 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5043 type = TREE_VALUE (typelt);
5044 if (type == error_mark_node)
5045 continue;
5047 if (!COMPLETE_TYPE_P (type))
5049 if (funcdef_flag)
5051 if (DECL_NAME (parm))
5052 error ("parameter %u (%q+D) has incomplete type",
5053 parmno, parm);
5054 else
5055 error ("%Jparameter %u has incomplete type",
5056 parm, parmno);
5058 TREE_VALUE (typelt) = error_mark_node;
5059 TREE_TYPE (parm) = error_mark_node;
5061 else if (VOID_TYPE_P (type))
5063 if (DECL_NAME (parm))
5064 warning (0, "parameter %u (%q+D) has void type",
5065 parmno, parm);
5066 else
5067 warning (0, "%Jparameter %u has void type",
5068 parm, parmno);
5072 if (DECL_NAME (parm) && TREE_USED (parm))
5073 warn_if_shadowing (parm);
5075 return arg_types;
5079 /* Take apart the current scope and return a c_arg_info structure with
5080 info on a parameter list just parsed.
5082 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5084 ELLIPSIS being true means the argument list ended in '...' so don't
5085 append a sentinel (void_list_node) to the end of the type-list. */
5087 struct c_arg_info *
5088 get_parm_info (bool ellipsis)
5090 struct c_binding *b = current_scope->bindings;
5091 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5092 struct c_arg_info);
5093 tree parms = 0;
5094 tree tags = 0;
5095 tree types = 0;
5096 tree others = 0;
5098 static bool explained_incomplete_types = false;
5099 bool gave_void_only_once_err = false;
5101 arg_info->parms = 0;
5102 arg_info->tags = 0;
5103 arg_info->types = 0;
5104 arg_info->others = 0;
5105 arg_info->pending_sizes = 0;
5106 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5108 /* The bindings in this scope must not get put into a block.
5109 We will take care of deleting the binding nodes. */
5110 current_scope->bindings = 0;
5112 /* This function is only called if there was *something* on the
5113 parameter list. */
5114 gcc_assert (b);
5116 /* A parameter list consisting solely of 'void' indicates that the
5117 function takes no arguments. But if the 'void' is qualified
5118 (by 'const' or 'volatile'), or has a storage class specifier
5119 ('register'), then the behavior is undefined; issue an error.
5120 Typedefs for 'void' are OK (see DR#157). */
5121 if (b->prev == 0 /* one binding */
5122 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5123 && !DECL_NAME (b->decl) /* anonymous */
5124 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5126 if (TREE_THIS_VOLATILE (b->decl)
5127 || TREE_READONLY (b->decl)
5128 || C_DECL_REGISTER (b->decl))
5129 error ("%<void%> as only parameter may not be qualified");
5131 /* There cannot be an ellipsis. */
5132 if (ellipsis)
5133 error ("%<void%> must be the only parameter");
5135 arg_info->types = void_list_node;
5136 return arg_info;
5139 if (!ellipsis)
5140 types = void_list_node;
5142 /* Break up the bindings list into parms, tags, types, and others;
5143 apply sanity checks; purge the name-to-decl bindings. */
5144 while (b)
5146 tree decl = b->decl;
5147 tree type = TREE_TYPE (decl);
5148 const char *keyword;
5150 switch (TREE_CODE (decl))
5152 case PARM_DECL:
5153 if (b->id)
5155 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5156 I_SYMBOL_BINDING (b->id) = b->shadowed;
5159 /* Check for forward decls that never got their actual decl. */
5160 if (TREE_ASM_WRITTEN (decl))
5161 error ("parameter %q+D has just a forward declaration", decl);
5162 /* Check for (..., void, ...) and issue an error. */
5163 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5165 if (!gave_void_only_once_err)
5167 error ("%<void%> must be the only parameter");
5168 gave_void_only_once_err = true;
5171 else
5173 /* Valid parameter, add it to the list. */
5174 TREE_CHAIN (decl) = parms;
5175 parms = decl;
5177 /* Since there is a prototype, args are passed in their
5178 declared types. The back end may override this later. */
5179 DECL_ARG_TYPE (decl) = type;
5180 types = tree_cons (0, type, types);
5182 break;
5184 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5185 case UNION_TYPE: keyword = "union"; goto tag;
5186 case RECORD_TYPE: keyword = "struct"; goto tag;
5187 tag:
5188 /* Types may not have tag-names, in which case the type
5189 appears in the bindings list with b->id NULL. */
5190 if (b->id)
5192 gcc_assert (I_TAG_BINDING (b->id) == b);
5193 I_TAG_BINDING (b->id) = b->shadowed;
5196 /* Warn about any struct, union or enum tags defined in a
5197 parameter list. The scope of such types is limited to
5198 the parameter list, which is rarely if ever desirable
5199 (it's impossible to call such a function with type-
5200 correct arguments). An anonymous union parm type is
5201 meaningful as a GNU extension, so don't warn for that. */
5202 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5204 if (b->id)
5205 /* The %s will be one of 'struct', 'union', or 'enum'. */
5206 warning (0, "%<%s %E%> declared inside parameter list",
5207 keyword, b->id);
5208 else
5209 /* The %s will be one of 'struct', 'union', or 'enum'. */
5210 warning (0, "anonymous %s declared inside parameter list",
5211 keyword);
5213 if (!explained_incomplete_types)
5215 warning (0, "its scope is only this definition or declaration,"
5216 " which is probably not what you want");
5217 explained_incomplete_types = true;
5221 tags = tree_cons (b->id, decl, tags);
5222 break;
5224 case CONST_DECL:
5225 case TYPE_DECL:
5226 case FUNCTION_DECL:
5227 /* CONST_DECLs appear here when we have an embedded enum,
5228 and TYPE_DECLs appear here when we have an embedded struct
5229 or union. No warnings for this - we already warned about the
5230 type itself. FUNCTION_DECLs appear when there is an implicit
5231 function declaration in the parameter list. */
5233 TREE_CHAIN (decl) = others;
5234 others = decl;
5235 /* fall through */
5237 case ERROR_MARK:
5238 /* error_mark_node appears here when we have an undeclared
5239 variable. Just throw it away. */
5240 if (b->id)
5242 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5243 I_SYMBOL_BINDING (b->id) = b->shadowed;
5245 break;
5247 /* Other things that might be encountered. */
5248 case LABEL_DECL:
5249 case VAR_DECL:
5250 default:
5251 gcc_unreachable ();
5254 b = free_binding_and_advance (b);
5257 arg_info->parms = parms;
5258 arg_info->tags = tags;
5259 arg_info->types = types;
5260 arg_info->others = others;
5261 arg_info->pending_sizes = get_pending_sizes ();
5262 return arg_info;
5265 /* Get the struct, enum or union (CODE says which) with tag NAME.
5266 Define the tag as a forward-reference if it is not defined.
5267 Return a c_typespec structure for the type specifier. */
5269 struct c_typespec
5270 parser_xref_tag (enum tree_code code, tree name)
5272 struct c_typespec ret;
5273 /* If a cross reference is requested, look up the type
5274 already defined for this tag and return it. */
5276 tree ref = lookup_tag (code, name, 0);
5277 /* If this is the right type of tag, return what we found.
5278 (This reference will be shadowed by shadow_tag later if appropriate.)
5279 If this is the wrong type of tag, do not return it. If it was the
5280 wrong type in the same scope, we will have had an error
5281 message already; if in a different scope and declaring
5282 a name, pending_xref_error will give an error message; but if in a
5283 different scope and not declaring a name, this tag should
5284 shadow the previous declaration of a different type of tag, and
5285 this would not work properly if we return the reference found.
5286 (For example, with "struct foo" in an outer scope, "union foo;"
5287 must shadow that tag with a new one of union type.) */
5288 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5289 if (ref && TREE_CODE (ref) == code)
5291 ret.spec = ref;
5292 return ret;
5295 /* If no such tag is yet defined, create a forward-reference node
5296 and record it as the "definition".
5297 When a real declaration of this type is found,
5298 the forward-reference will be altered into a real type. */
5300 ref = make_node (code);
5301 if (code == ENUMERAL_TYPE)
5303 /* Give the type a default layout like unsigned int
5304 to avoid crashing if it does not get defined. */
5305 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5306 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5307 TYPE_USER_ALIGN (ref) = 0;
5308 TYPE_UNSIGNED (ref) = 1;
5309 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5310 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5311 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5314 pushtag (name, ref);
5316 ret.spec = ref;
5317 return ret;
5320 /* Get the struct, enum or union (CODE says which) with tag NAME.
5321 Define the tag as a forward-reference if it is not defined.
5322 Return a tree for the type. */
5324 tree
5325 xref_tag (enum tree_code code, tree name)
5327 return parser_xref_tag (code, name).spec;
5330 /* Make sure that the tag NAME is defined *in the current scope*
5331 at least as a forward reference.
5332 CODE says which kind of tag NAME ought to be. */
5334 tree
5335 start_struct (enum tree_code code, tree name)
5337 /* If there is already a tag defined at this scope
5338 (as a forward reference), just return it. */
5340 tree ref = 0;
5342 if (name != 0)
5343 ref = lookup_tag (code, name, 1);
5344 if (ref && TREE_CODE (ref) == code)
5346 if (TYPE_SIZE (ref))
5348 if (code == UNION_TYPE)
5349 error ("redefinition of %<union %E%>", name);
5350 else
5351 error ("redefinition of %<struct %E%>", name);
5353 else if (C_TYPE_BEING_DEFINED (ref))
5355 if (code == UNION_TYPE)
5356 error ("nested redefinition of %<union %E%>", name);
5357 else
5358 error ("nested redefinition of %<struct %E%>", name);
5359 /* Don't create structures that contain themselves. */
5360 ref = NULL_TREE;
5364 /* Otherwise create a forward-reference just so the tag is in scope. */
5366 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5368 ref = make_node (code);
5369 pushtag (name, ref);
5372 C_TYPE_BEING_DEFINED (ref) = 1;
5373 TYPE_PACKED (ref) = flag_pack_struct;
5374 return ref;
5377 /* Process the specs, declarator and width (NULL if omitted)
5378 of a structure component, returning a FIELD_DECL node.
5379 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5381 This is done during the parsing of the struct declaration.
5382 The FIELD_DECL nodes are chained together and the lot of them
5383 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5385 tree
5386 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5387 tree width)
5389 tree value;
5391 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5392 && width == NULL_TREE)
5394 /* This is an unnamed decl.
5396 If we have something of the form "union { list } ;" then this
5397 is the anonymous union extension. Similarly for struct.
5399 If this is something of the form "struct foo;", then
5400 If MS extensions are enabled, this is handled as an
5401 anonymous struct.
5402 Otherwise this is a forward declaration of a structure tag.
5404 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5405 If MS extensions are enabled and foo names a structure, then
5406 again this is an anonymous struct.
5407 Otherwise this is an error.
5409 Oh what a horrid tangled web we weave. I wonder if MS consciously
5410 took this from Plan 9 or if it was an accident of implementation
5411 that took root before someone noticed the bug... */
5413 tree type = declspecs->type;
5414 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5415 || TREE_CODE (type) == UNION_TYPE);
5416 bool ok = false;
5418 if (type_ok
5419 && (flag_ms_extensions || !declspecs->typedef_p))
5421 if (flag_ms_extensions)
5422 ok = true;
5423 else if (flag_iso)
5424 ok = false;
5425 else if (TYPE_NAME (type) == NULL)
5426 ok = true;
5427 else
5428 ok = false;
5430 if (!ok)
5432 pedwarn ("declaration does not declare anything");
5433 return NULL_TREE;
5435 if (pedantic)
5436 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5439 value = grokdeclarator (declarator, declspecs, FIELD, false,
5440 width ? &width : NULL);
5442 finish_decl (value, NULL_TREE, NULL_TREE);
5443 DECL_INITIAL (value) = width;
5445 return value;
5448 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5449 the list such that this does not present a problem later. */
5451 static void
5452 detect_field_duplicates (tree fieldlist)
5454 tree x, y;
5455 int timeout = 10;
5457 /* First, see if there are more than "a few" fields.
5458 This is trivially true if there are zero or one fields. */
5459 if (!fieldlist)
5460 return;
5461 x = TREE_CHAIN (fieldlist);
5462 if (!x)
5463 return;
5464 do {
5465 timeout--;
5466 x = TREE_CHAIN (x);
5467 } while (timeout > 0 && x);
5469 /* If there were "few" fields, avoid the overhead of allocating
5470 a hash table. Instead just do the nested traversal thing. */
5471 if (timeout > 0)
5473 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5474 if (DECL_NAME (x))
5476 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5477 if (DECL_NAME (y) == DECL_NAME (x))
5479 error ("duplicate member %q+D", x);
5480 DECL_NAME (x) = NULL_TREE;
5484 else
5486 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5487 void **slot;
5489 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5490 if ((y = DECL_NAME (x)) != 0)
5492 slot = htab_find_slot (htab, y, INSERT);
5493 if (*slot)
5495 error ("duplicate member %q+D", x);
5496 DECL_NAME (x) = NULL_TREE;
5498 *slot = y;
5501 htab_delete (htab);
5505 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5506 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5507 ATTRIBUTES are attributes to be applied to the structure. */
5509 tree
5510 finish_struct (tree t, tree fieldlist, tree attributes)
5512 tree x;
5513 bool toplevel = file_scope == current_scope;
5514 int saw_named_field;
5516 /* If this type was previously laid out as a forward reference,
5517 make sure we lay it out again. */
5519 TYPE_SIZE (t) = 0;
5521 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5523 if (pedantic)
5525 for (x = fieldlist; x; x = TREE_CHAIN (x))
5526 if (DECL_NAME (x) != 0)
5527 break;
5529 if (x == 0)
5531 if (TREE_CODE (t) == UNION_TYPE)
5533 if (fieldlist)
5534 pedwarn ("union has no named members");
5535 else
5536 pedwarn ("union has no members");
5538 else
5540 if (fieldlist)
5541 pedwarn ("struct has no named members");
5542 else
5543 pedwarn ("struct has no members");
5548 /* Install struct as DECL_CONTEXT of each field decl.
5549 Also process specified field sizes, found in the DECL_INITIAL,
5550 storing 0 there after the type has been changed to precision equal
5551 to its width, rather than the precision of the specified standard
5552 type. (Correct layout requires the original type to have been preserved
5553 until now.) */
5555 saw_named_field = 0;
5556 for (x = fieldlist; x; x = TREE_CHAIN (x))
5558 if (TREE_TYPE (x) == error_mark_node)
5559 continue;
5561 DECL_CONTEXT (x) = t;
5563 if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
5564 DECL_PACKED (x) = 1;
5566 /* If any field is const, the structure type is pseudo-const. */
5567 if (TREE_READONLY (x))
5568 C_TYPE_FIELDS_READONLY (t) = 1;
5569 else
5571 /* A field that is pseudo-const makes the structure likewise. */
5572 tree t1 = TREE_TYPE (x);
5573 while (TREE_CODE (t1) == ARRAY_TYPE)
5574 t1 = TREE_TYPE (t1);
5575 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5576 && C_TYPE_FIELDS_READONLY (t1))
5577 C_TYPE_FIELDS_READONLY (t) = 1;
5580 /* Any field that is volatile means variables of this type must be
5581 treated in some ways as volatile. */
5582 if (TREE_THIS_VOLATILE (x))
5583 C_TYPE_FIELDS_VOLATILE (t) = 1;
5585 /* Any field of nominal variable size implies structure is too. */
5586 if (C_DECL_VARIABLE_SIZE (x))
5587 C_TYPE_VARIABLE_SIZE (t) = 1;
5589 if (DECL_INITIAL (x))
5591 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5592 DECL_SIZE (x) = bitsize_int (width);
5593 DECL_BIT_FIELD (x) = 1;
5594 SET_DECL_C_BIT_FIELD (x);
5597 /* Detect flexible array member in an invalid context. */
5598 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5599 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5600 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5601 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5603 if (TREE_CODE (t) == UNION_TYPE)
5605 error ("%Jflexible array member in union", x);
5606 TREE_TYPE (x) = error_mark_node;
5608 else if (TREE_CHAIN (x) != NULL_TREE)
5610 error ("%Jflexible array member not at end of struct", x);
5611 TREE_TYPE (x) = error_mark_node;
5613 else if (!saw_named_field)
5615 error ("%Jflexible array member in otherwise empty struct", x);
5616 TREE_TYPE (x) = error_mark_node;
5620 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5621 && flexible_array_type_p (TREE_TYPE (x)))
5622 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5624 if (DECL_NAME (x))
5625 saw_named_field = 1;
5628 detect_field_duplicates (fieldlist);
5630 /* Now we have the nearly final fieldlist. Record it,
5631 then lay out the structure or union (including the fields). */
5633 TYPE_FIELDS (t) = fieldlist;
5635 layout_type (t);
5637 /* Give bit-fields their proper types. */
5639 tree *fieldlistp = &fieldlist;
5640 while (*fieldlistp)
5641 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5642 && TREE_TYPE (*fieldlistp) != error_mark_node)
5644 unsigned HOST_WIDE_INT width
5645 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5646 tree type = TREE_TYPE (*fieldlistp);
5647 if (width != TYPE_PRECISION (type))
5649 TREE_TYPE (*fieldlistp)
5650 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5651 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5653 DECL_INITIAL (*fieldlistp) = 0;
5655 else
5656 fieldlistp = &TREE_CHAIN (*fieldlistp);
5659 /* Now we have the truly final field list.
5660 Store it in this type and in the variants. */
5662 TYPE_FIELDS (t) = fieldlist;
5664 /* If there are lots of fields, sort so we can look through them fast.
5665 We arbitrarily consider 16 or more elts to be "a lot". */
5668 int len = 0;
5670 for (x = fieldlist; x; x = TREE_CHAIN (x))
5672 if (len > 15 || DECL_NAME (x) == NULL)
5673 break;
5674 len += 1;
5677 if (len > 15)
5679 tree *field_array;
5680 struct lang_type *space;
5681 struct sorted_fields_type *space2;
5683 len += list_length (x);
5685 /* Use the same allocation policy here that make_node uses, to
5686 ensure that this lives as long as the rest of the struct decl.
5687 All decls in an inline function need to be saved. */
5689 space = GGC_CNEW (struct lang_type);
5690 space2 = GGC_NEWVAR (struct sorted_fields_type,
5691 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5693 len = 0;
5694 space->s = space2;
5695 field_array = &space2->elts[0];
5696 for (x = fieldlist; x; x = TREE_CHAIN (x))
5698 field_array[len++] = x;
5700 /* If there is anonymous struct or union, break out of the loop. */
5701 if (DECL_NAME (x) == NULL)
5702 break;
5704 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5705 if (x == NULL)
5707 TYPE_LANG_SPECIFIC (t) = space;
5708 TYPE_LANG_SPECIFIC (t)->s->len = len;
5709 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5710 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5715 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5717 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5718 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5719 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5720 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5721 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5724 /* If this was supposed to be a transparent union, but we can't
5725 make it one, warn and turn off the flag. */
5726 if (TREE_CODE (t) == UNION_TYPE
5727 && TYPE_TRANSPARENT_UNION (t)
5728 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5730 TYPE_TRANSPARENT_UNION (t) = 0;
5731 warning (0, "union cannot be made transparent");
5734 /* If this structure or union completes the type of any previous
5735 variable declaration, lay it out and output its rtl. */
5736 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5738 x = TREE_CHAIN (x))
5740 tree decl = TREE_VALUE (x);
5741 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5742 layout_array_type (TREE_TYPE (decl));
5743 if (TREE_CODE (decl) != TYPE_DECL)
5745 layout_decl (decl, 0);
5746 if (c_dialect_objc ())
5747 objc_check_decl (decl);
5748 rest_of_decl_compilation (decl, toplevel, 0);
5749 if (!toplevel)
5750 expand_decl (decl);
5753 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5755 /* Finish debugging output for this type. */
5756 rest_of_type_compilation (t, toplevel);
5758 /* If we're inside a function proper, i.e. not file-scope and not still
5759 parsing parameters, then arrange for the size of a variable sized type
5760 to be bound now. */
5761 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5762 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5764 return t;
5767 /* Lay out the type T, and its element type, and so on. */
5769 static void
5770 layout_array_type (tree t)
5772 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5773 layout_array_type (TREE_TYPE (t));
5774 layout_type (t);
5777 /* Begin compiling the definition of an enumeration type.
5778 NAME is its name (or null if anonymous).
5779 Returns the type object, as yet incomplete.
5780 Also records info about it so that build_enumerator
5781 may be used to declare the individual values as they are read. */
5783 tree
5784 start_enum (tree name)
5786 tree enumtype = 0;
5788 /* If this is the real definition for a previous forward reference,
5789 fill in the contents in the same object that used to be the
5790 forward reference. */
5792 if (name != 0)
5793 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5795 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5797 enumtype = make_node (ENUMERAL_TYPE);
5798 pushtag (name, enumtype);
5801 if (C_TYPE_BEING_DEFINED (enumtype))
5802 error ("nested redefinition of %<enum %E%>", name);
5804 C_TYPE_BEING_DEFINED (enumtype) = 1;
5806 if (TYPE_VALUES (enumtype) != 0)
5808 /* This enum is a named one that has been declared already. */
5809 error ("redeclaration of %<enum %E%>", name);
5811 /* Completely replace its old definition.
5812 The old enumerators remain defined, however. */
5813 TYPE_VALUES (enumtype) = 0;
5816 enum_next_value = integer_zero_node;
5817 enum_overflow = 0;
5819 if (flag_short_enums)
5820 TYPE_PACKED (enumtype) = 1;
5822 return enumtype;
5825 /* After processing and defining all the values of an enumeration type,
5826 install their decls in the enumeration type and finish it off.
5827 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5828 and ATTRIBUTES are the specified attributes.
5829 Returns ENUMTYPE. */
5831 tree
5832 finish_enum (tree enumtype, tree values, tree attributes)
5834 tree pair, tem;
5835 tree minnode = 0, maxnode = 0;
5836 int precision, unsign;
5837 bool toplevel = (file_scope == current_scope);
5838 struct lang_type *lt;
5840 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5842 /* Calculate the maximum value of any enumerator in this type. */
5844 if (values == error_mark_node)
5845 minnode = maxnode = integer_zero_node;
5846 else
5848 minnode = maxnode = TREE_VALUE (values);
5849 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5851 tree value = TREE_VALUE (pair);
5852 if (tree_int_cst_lt (maxnode, value))
5853 maxnode = value;
5854 if (tree_int_cst_lt (value, minnode))
5855 minnode = value;
5859 /* Construct the final type of this enumeration. It is the same
5860 as one of the integral types - the narrowest one that fits, except
5861 that normally we only go as narrow as int - and signed iff any of
5862 the values are negative. */
5863 unsign = (tree_int_cst_sgn (minnode) >= 0);
5864 precision = MAX (min_precision (minnode, unsign),
5865 min_precision (maxnode, unsign));
5867 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5869 tem = c_common_type_for_size (precision, unsign);
5870 if (tem == NULL)
5872 warning (0, "enumeration values exceed range of largest integer");
5873 tem = long_long_integer_type_node;
5876 else
5877 tem = unsign ? unsigned_type_node : integer_type_node;
5879 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5880 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5881 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5882 TYPE_SIZE (enumtype) = 0;
5884 /* If the precision of the type was specific with an attribute and it
5885 was too small, give an error. Otherwise, use it. */
5886 if (TYPE_PRECISION (enumtype))
5888 if (precision > TYPE_PRECISION (enumtype))
5889 error ("specified mode too small for enumeral values");
5891 else
5892 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5894 layout_type (enumtype);
5896 if (values != error_mark_node)
5898 /* Change the type of the enumerators to be the enum type. We
5899 need to do this irrespective of the size of the enum, for
5900 proper type checking. Replace the DECL_INITIALs of the
5901 enumerators, and the value slots of the list, with copies
5902 that have the enum type; they cannot be modified in place
5903 because they may be shared (e.g. integer_zero_node) Finally,
5904 change the purpose slots to point to the names of the decls. */
5905 for (pair = values; pair; pair = TREE_CHAIN (pair))
5907 tree enu = TREE_PURPOSE (pair);
5908 tree ini = DECL_INITIAL (enu);
5910 TREE_TYPE (enu) = enumtype;
5912 /* The ISO C Standard mandates enumerators to have type int,
5913 even though the underlying type of an enum type is
5914 unspecified. Here we convert any enumerators that fit in
5915 an int to type int, to avoid promotions to unsigned types
5916 when comparing integers with enumerators that fit in the
5917 int range. When -pedantic is given, build_enumerator()
5918 would have already taken care of those that don't fit. */
5919 if (int_fits_type_p (ini, integer_type_node))
5920 tem = integer_type_node;
5921 else
5922 tem = enumtype;
5923 ini = convert (tem, ini);
5925 DECL_INITIAL (enu) = ini;
5926 TREE_PURPOSE (pair) = DECL_NAME (enu);
5927 TREE_VALUE (pair) = ini;
5930 TYPE_VALUES (enumtype) = values;
5933 /* Record the min/max values so that we can warn about bit-field
5934 enumerations that are too small for the values. */
5935 lt = GGC_CNEW (struct lang_type);
5936 lt->enum_min = minnode;
5937 lt->enum_max = maxnode;
5938 TYPE_LANG_SPECIFIC (enumtype) = lt;
5940 /* Fix up all variant types of this enum type. */
5941 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5943 if (tem == enumtype)
5944 continue;
5945 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5946 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5947 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5948 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5949 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5950 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5951 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5952 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5953 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5954 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5955 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5958 /* Finish debugging output for this type. */
5959 rest_of_type_compilation (enumtype, toplevel);
5961 return enumtype;
5964 /* Build and install a CONST_DECL for one value of the
5965 current enumeration type (one that was begun with start_enum).
5966 Return a tree-list containing the CONST_DECL and its value.
5967 Assignment of sequential values by default is handled here. */
5969 tree
5970 build_enumerator (tree name, tree value)
5972 tree decl, type;
5974 /* Validate and default VALUE. */
5976 if (value != 0)
5978 /* Don't issue more errors for error_mark_node (i.e. an
5979 undeclared identifier) - just ignore the value expression. */
5980 if (value == error_mark_node)
5981 value = 0;
5982 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5983 || TREE_CODE (value) != INTEGER_CST)
5985 error ("enumerator value for %qE is not an integer constant", name);
5986 value = 0;
5988 else
5990 value = default_conversion (value);
5991 constant_expression_warning (value);
5995 /* Default based on previous value. */
5996 /* It should no longer be possible to have NON_LVALUE_EXPR
5997 in the default. */
5998 if (value == 0)
6000 value = enum_next_value;
6001 if (enum_overflow)
6002 error ("overflow in enumeration values");
6005 if (pedantic && !int_fits_type_p (value, integer_type_node))
6007 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
6008 /* XXX This causes -pedantic to change the meaning of the program.
6009 Remove? -zw 2004-03-15 */
6010 value = convert (integer_type_node, value);
6013 /* Set basis for default for next value. */
6014 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
6015 enum_overflow = tree_int_cst_lt (enum_next_value, value);
6017 /* Now create a declaration for the enum value name. */
6019 type = TREE_TYPE (value);
6020 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6021 TYPE_PRECISION (integer_type_node)),
6022 (TYPE_PRECISION (type)
6023 >= TYPE_PRECISION (integer_type_node)
6024 && TYPE_UNSIGNED (type)));
6026 decl = build_decl (CONST_DECL, name, type);
6027 DECL_INITIAL (decl) = convert (type, value);
6028 pushdecl (decl);
6030 return tree_cons (decl, value, NULL_TREE);
6034 /* Create the FUNCTION_DECL for a function definition.
6035 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6036 the declaration; they describe the function's name and the type it returns,
6037 but twisted together in a fashion that parallels the syntax of C.
6039 This function creates a binding context for the function body
6040 as well as setting up the FUNCTION_DECL in current_function_decl.
6042 Returns 1 on success. If the DECLARATOR is not suitable for a function
6043 (it defines a datum instead), we return 0, which tells
6044 yyparse to report a parse error. */
6047 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6048 tree attributes)
6050 tree decl1, old_decl;
6051 tree restype, resdecl;
6052 struct c_label_context_se *nstack_se;
6053 struct c_label_context_vm *nstack_vm;
6055 current_function_returns_value = 0; /* Assume, until we see it does. */
6056 current_function_returns_null = 0;
6057 current_function_returns_abnormally = 0;
6058 warn_about_return_type = 0;
6059 c_switch_stack = NULL;
6061 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6062 nstack_se->labels_def = NULL;
6063 nstack_se->labels_used = NULL;
6064 nstack_se->next = label_context_stack_se;
6065 label_context_stack_se = nstack_se;
6067 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6068 nstack_vm->labels_def = NULL;
6069 nstack_vm->labels_used = NULL;
6070 nstack_vm->scope = 0;
6071 nstack_vm->next = label_context_stack_vm;
6072 label_context_stack_vm = nstack_vm;
6074 /* Indicate no valid break/continue context by setting these variables
6075 to some non-null, non-label value. We'll notice and emit the proper
6076 error message in c_finish_bc_stmt. */
6077 c_break_label = c_cont_label = size_zero_node;
6079 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
6081 /* If the declarator is not suitable for a function definition,
6082 cause a syntax error. */
6083 if (decl1 == 0)
6085 label_context_stack_se = label_context_stack_se->next;
6086 label_context_stack_vm = label_context_stack_vm->next;
6087 return 0;
6090 decl_attributes (&decl1, attributes, 0);
6092 if (DECL_DECLARED_INLINE_P (decl1)
6093 && DECL_UNINLINABLE (decl1)
6094 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6095 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6096 decl1);
6098 #if WANT_C99_INLINE_SEMANTICS
6099 /* Handle gnu_inline attribute. */
6100 if (declspecs->inline_p
6101 && flag_isoc99
6102 && TREE_CODE (decl1) == FUNCTION_DECL
6103 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1)))
6105 if (declspecs->storage_class != csc_static)
6106 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6108 #endif /* WANT_C99_INLINE_SEMANTICS */
6110 announce_function (decl1);
6112 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6114 error ("return type is an incomplete type");
6115 /* Make it return void instead. */
6116 TREE_TYPE (decl1)
6117 = build_function_type (void_type_node,
6118 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6121 if (warn_about_return_type)
6122 pedwarn_c99 ("return type defaults to %<int%>");
6124 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6125 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6126 DECL_INITIAL (decl1) = error_mark_node;
6128 /* If this definition isn't a prototype and we had a prototype declaration
6129 before, copy the arg type info from that prototype. */
6130 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6131 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6132 old_decl = 0;
6133 current_function_prototype_locus = UNKNOWN_LOCATION;
6134 current_function_prototype_built_in = false;
6135 current_function_prototype_arg_types = NULL_TREE;
6136 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6138 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6139 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6140 TREE_TYPE (TREE_TYPE (old_decl))))
6142 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6143 TREE_TYPE (decl1));
6144 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6145 current_function_prototype_built_in
6146 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6147 current_function_prototype_arg_types
6148 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6150 if (TREE_PUBLIC (decl1))
6152 /* If there is an external prototype declaration of this
6153 function, record its location but do not copy information
6154 to this decl. This may be an invisible declaration
6155 (built-in or in a scope which has finished) or simply
6156 have more refined argument types than any declaration
6157 found above. */
6158 struct c_binding *b;
6159 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6160 if (B_IN_SCOPE (b, external_scope))
6161 break;
6162 if (b)
6164 tree ext_decl, ext_type;
6165 ext_decl = b->decl;
6166 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6167 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6168 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6169 TREE_TYPE (ext_type)))
6171 current_function_prototype_locus
6172 = DECL_SOURCE_LOCATION (ext_decl);
6173 current_function_prototype_built_in
6174 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6175 current_function_prototype_arg_types
6176 = TYPE_ARG_TYPES (ext_type);
6182 /* Optionally warn of old-fashioned def with no previous prototype. */
6183 if (warn_strict_prototypes
6184 && old_decl != error_mark_node
6185 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6186 && C_DECL_ISNT_PROTOTYPE (old_decl))
6187 warning (OPT_Wstrict_prototypes,
6188 "function declaration isn%'t a prototype");
6189 /* Optionally warn of any global def with no previous prototype. */
6190 else if (warn_missing_prototypes
6191 && old_decl != error_mark_node
6192 && TREE_PUBLIC (decl1)
6193 && !MAIN_NAME_P (DECL_NAME (decl1))
6194 && C_DECL_ISNT_PROTOTYPE (old_decl))
6195 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6196 /* Optionally warn of any def with no previous prototype
6197 if the function has already been used. */
6198 else if (warn_missing_prototypes
6199 && old_decl != 0
6200 && old_decl != error_mark_node
6201 && TREE_USED (old_decl)
6202 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6203 warning (OPT_Wmissing_prototypes,
6204 "%q+D was used with no prototype before its definition", decl1);
6205 /* Optionally warn of any global def with no previous declaration. */
6206 else if (warn_missing_declarations
6207 && TREE_PUBLIC (decl1)
6208 && old_decl == 0
6209 && !MAIN_NAME_P (DECL_NAME (decl1)))
6210 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6211 decl1);
6212 /* Optionally warn of any def with no previous declaration
6213 if the function has already been used. */
6214 else if (warn_missing_declarations
6215 && old_decl != 0
6216 && old_decl != error_mark_node
6217 && TREE_USED (old_decl)
6218 && C_DECL_IMPLICIT (old_decl))
6219 warning (OPT_Wmissing_declarations,
6220 "%q+D was used with no declaration before its definition", decl1);
6222 /* This function exists in static storage.
6223 (This does not mean `static' in the C sense!) */
6224 TREE_STATIC (decl1) = 1;
6226 /* A nested function is not global. */
6227 if (current_function_decl != 0)
6228 TREE_PUBLIC (decl1) = 0;
6230 /* This is the earliest point at which we might know the assembler
6231 name of the function. Thus, if it's set before this, die horribly. */
6232 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6234 /* If #pragma weak was used, mark the decl weak now. */
6235 if (current_scope == file_scope)
6236 maybe_apply_pragma_weak (decl1);
6238 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6239 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6241 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6242 != integer_type_node)
6243 pedwarn ("return type of %q+D is not %<int%>", decl1);
6245 check_main_parameter_types(decl1);
6247 if (!TREE_PUBLIC (decl1))
6248 pedwarn ("%q+D is normally a non-static function", decl1);
6251 /* Record the decl so that the function name is defined.
6252 If we already have a decl for this name, and it is a FUNCTION_DECL,
6253 use the old decl. */
6255 current_function_decl = pushdecl (decl1);
6257 push_scope ();
6258 declare_parm_level ();
6260 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6261 /* Promote the value to int before returning it. */
6262 if (c_promoting_integer_type_p (restype))
6264 /* It retains unsignedness if not really getting wider. */
6265 if (TYPE_UNSIGNED (restype)
6266 && (TYPE_PRECISION (restype)
6267 == TYPE_PRECISION (integer_type_node)))
6268 restype = unsigned_type_node;
6269 else
6270 restype = integer_type_node;
6273 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6274 DECL_ARTIFICIAL (resdecl) = 1;
6275 DECL_IGNORED_P (resdecl) = 1;
6276 DECL_RESULT (current_function_decl) = resdecl;
6278 start_fname_decls ();
6280 return 1;
6283 /* Subroutine of store_parm_decls which handles new-style function
6284 definitions (prototype format). The parms already have decls, so we
6285 need only record them as in effect and complain if any redundant
6286 old-style parm decls were written. */
6287 static void
6288 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6290 tree decl;
6292 if (current_scope->bindings)
6294 error ("%Jold-style parameter declarations in prototyped "
6295 "function definition", fndecl);
6297 /* Get rid of the old-style declarations. */
6298 pop_scope ();
6299 push_scope ();
6301 /* Don't issue this warning for nested functions, and don't issue this
6302 warning if we got here because ARG_INFO_TYPES was error_mark_node
6303 (this happens when a function definition has just an ellipsis in
6304 its parameter list). */
6305 else if (!in_system_header && !current_function_scope
6306 && arg_info->types != error_mark_node)
6307 warning (OPT_Wtraditional,
6308 "%Jtraditional C rejects ISO C style function definitions",
6309 fndecl);
6311 /* Now make all the parameter declarations visible in the function body.
6312 We can bypass most of the grunt work of pushdecl. */
6313 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6315 DECL_CONTEXT (decl) = current_function_decl;
6316 if (DECL_NAME (decl))
6318 bind (DECL_NAME (decl), decl, current_scope,
6319 /*invisible=*/false, /*nested=*/false);
6320 if (!TREE_USED (decl))
6321 warn_if_shadowing (decl);
6323 else
6324 error ("%Jparameter name omitted", decl);
6327 /* Record the parameter list in the function declaration. */
6328 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6330 /* Now make all the ancillary declarations visible, likewise. */
6331 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6333 DECL_CONTEXT (decl) = current_function_decl;
6334 if (DECL_NAME (decl))
6335 bind (DECL_NAME (decl), decl, current_scope,
6336 /*invisible=*/false, /*nested=*/false);
6339 /* And all the tag declarations. */
6340 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6341 if (TREE_PURPOSE (decl))
6342 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6343 /*invisible=*/false, /*nested=*/false);
6346 /* Subroutine of store_parm_decls which handles old-style function
6347 definitions (separate parameter list and declarations). */
6349 static void
6350 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6352 struct c_binding *b;
6353 tree parm, decl, last;
6354 tree parmids = arg_info->parms;
6355 struct pointer_set_t *seen_args = pointer_set_create ();
6357 if (!in_system_header)
6358 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6359 fndecl);
6361 /* Match each formal parameter name with its declaration. Save each
6362 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6363 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6365 if (TREE_VALUE (parm) == 0)
6367 error ("%Jparameter name missing from parameter list", fndecl);
6368 TREE_PURPOSE (parm) = 0;
6369 continue;
6372 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6373 if (b && B_IN_CURRENT_SCOPE (b))
6375 decl = b->decl;
6376 /* If we got something other than a PARM_DECL it is an error. */
6377 if (TREE_CODE (decl) != PARM_DECL)
6378 error ("%q+D declared as a non-parameter", decl);
6379 /* If the declaration is already marked, we have a duplicate
6380 name. Complain and ignore the duplicate. */
6381 else if (pointer_set_contains (seen_args, decl))
6383 error ("multiple parameters named %q+D", decl);
6384 TREE_PURPOSE (parm) = 0;
6385 continue;
6387 /* If the declaration says "void", complain and turn it into
6388 an int. */
6389 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6391 error ("parameter %q+D declared with void type", decl);
6392 TREE_TYPE (decl) = integer_type_node;
6393 DECL_ARG_TYPE (decl) = integer_type_node;
6394 layout_decl (decl, 0);
6396 warn_if_shadowing (decl);
6398 /* If no declaration found, default to int. */
6399 else
6401 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6402 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6403 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6404 pushdecl (decl);
6405 warn_if_shadowing (decl);
6407 if (flag_isoc99)
6408 pedwarn ("type of %q+D defaults to %<int%>", decl);
6409 else if (extra_warnings)
6410 warning (OPT_Wextra, "type of %q+D defaults to %<int%>", decl);
6413 TREE_PURPOSE (parm) = decl;
6414 pointer_set_insert (seen_args, decl);
6417 /* Now examine the parms chain for incomplete declarations
6418 and declarations with no corresponding names. */
6420 for (b = current_scope->bindings; b; b = b->prev)
6422 parm = b->decl;
6423 if (TREE_CODE (parm) != PARM_DECL)
6424 continue;
6426 if (TREE_TYPE (parm) != error_mark_node
6427 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6429 error ("parameter %q+D has incomplete type", parm);
6430 TREE_TYPE (parm) = error_mark_node;
6433 if (!pointer_set_contains (seen_args, parm))
6435 error ("declaration for parameter %q+D but no such parameter", parm);
6437 /* Pretend the parameter was not missing.
6438 This gets us to a standard state and minimizes
6439 further error messages. */
6440 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6444 /* Chain the declarations together in the order of the list of
6445 names. Store that chain in the function decl, replacing the
6446 list of names. Update the current scope to match. */
6447 DECL_ARGUMENTS (fndecl) = 0;
6449 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6450 if (TREE_PURPOSE (parm))
6451 break;
6452 if (parm && TREE_PURPOSE (parm))
6454 last = TREE_PURPOSE (parm);
6455 DECL_ARGUMENTS (fndecl) = last;
6457 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6458 if (TREE_PURPOSE (parm))
6460 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6461 last = TREE_PURPOSE (parm);
6463 TREE_CHAIN (last) = 0;
6466 pointer_set_destroy (seen_args);
6468 /* If there was a previous prototype,
6469 set the DECL_ARG_TYPE of each argument according to
6470 the type previously specified, and report any mismatches. */
6472 if (current_function_prototype_arg_types)
6474 tree type;
6475 for (parm = DECL_ARGUMENTS (fndecl),
6476 type = current_function_prototype_arg_types;
6477 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6478 != void_type_node));
6479 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6481 if (parm == 0 || type == 0
6482 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6484 if (current_function_prototype_built_in)
6485 warning (0, "number of arguments doesn%'t match "
6486 "built-in prototype");
6487 else
6489 error ("number of arguments doesn%'t match prototype");
6490 error ("%Hprototype declaration",
6491 &current_function_prototype_locus);
6493 break;
6495 /* Type for passing arg must be consistent with that
6496 declared for the arg. ISO C says we take the unqualified
6497 type for parameters declared with qualified type. */
6498 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6499 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6501 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6502 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6504 /* Adjust argument to match prototype. E.g. a previous
6505 `int foo(float);' prototype causes
6506 `int foo(x) float x; {...}' to be treated like
6507 `int foo(float x) {...}'. This is particularly
6508 useful for argument types like uid_t. */
6509 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6511 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6512 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6513 && TYPE_PRECISION (TREE_TYPE (parm))
6514 < TYPE_PRECISION (integer_type_node))
6515 DECL_ARG_TYPE (parm) = integer_type_node;
6517 if (pedantic)
6519 /* ??? Is it possible to get here with a
6520 built-in prototype or will it always have
6521 been diagnosed as conflicting with an
6522 old-style definition and discarded? */
6523 if (current_function_prototype_built_in)
6524 warning (0, "promoted argument %qD "
6525 "doesn%'t match built-in prototype", parm);
6526 else
6528 pedwarn ("promoted argument %qD "
6529 "doesn%'t match prototype", parm);
6530 pedwarn ("%Hprototype declaration",
6531 &current_function_prototype_locus);
6535 else
6537 if (current_function_prototype_built_in)
6538 warning (0, "argument %qD doesn%'t match "
6539 "built-in prototype", parm);
6540 else
6542 error ("argument %qD doesn%'t match prototype", parm);
6543 error ("%Hprototype declaration",
6544 &current_function_prototype_locus);
6549 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6552 /* Otherwise, create a prototype that would match. */
6554 else
6556 tree actual = 0, last = 0, type;
6558 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6560 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6561 if (last)
6562 TREE_CHAIN (last) = type;
6563 else
6564 actual = type;
6565 last = type;
6567 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6568 if (last)
6569 TREE_CHAIN (last) = type;
6570 else
6571 actual = type;
6573 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6574 of the type of this function, but we need to avoid having this
6575 affect the types of other similarly-typed functions, so we must
6576 first force the generation of an identical (but separate) type
6577 node for the relevant function type. The new node we create
6578 will be a variant of the main variant of the original function
6579 type. */
6581 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6583 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6587 /* Store parameter declarations passed in ARG_INFO into the current
6588 function declaration. */
6590 void
6591 store_parm_decls_from (struct c_arg_info *arg_info)
6593 current_function_arg_info = arg_info;
6594 store_parm_decls ();
6597 /* Store the parameter declarations into the current function declaration.
6598 This is called after parsing the parameter declarations, before
6599 digesting the body of the function.
6601 For an old-style definition, construct a prototype out of the old-style
6602 parameter declarations and inject it into the function's type. */
6604 void
6605 store_parm_decls (void)
6607 tree fndecl = current_function_decl;
6608 bool proto;
6610 /* The argument information block for FNDECL. */
6611 struct c_arg_info *arg_info = current_function_arg_info;
6612 current_function_arg_info = 0;
6614 /* True if this definition is written with a prototype. Note:
6615 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6616 list in a function definition as equivalent to (void) -- an
6617 empty argument list specifies the function has no parameters,
6618 but only (void) sets up a prototype for future calls. */
6619 proto = arg_info->types != 0;
6621 if (proto)
6622 store_parm_decls_newstyle (fndecl, arg_info);
6623 else
6624 store_parm_decls_oldstyle (fndecl, arg_info);
6626 /* The next call to push_scope will be a function body. */
6628 next_is_function_body = true;
6630 /* Write a record describing this function definition to the prototypes
6631 file (if requested). */
6633 gen_aux_info_record (fndecl, 1, 0, proto);
6635 /* Initialize the RTL code for the function. */
6636 allocate_struct_function (fndecl);
6638 /* Begin the statement tree for this function. */
6639 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6641 /* ??? Insert the contents of the pending sizes list into the function
6642 to be evaluated. The only reason left to have this is
6643 void foo(int n, int array[n++])
6644 because we throw away the array type in favor of a pointer type, and
6645 thus won't naturally see the SAVE_EXPR containing the increment. All
6646 other pending sizes would be handled by gimplify_parameters. */
6648 tree t;
6649 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6650 add_stmt (TREE_VALUE (t));
6653 /* Even though we're inside a function body, we still don't want to
6654 call expand_expr to calculate the size of a variable-sized array.
6655 We haven't necessarily assigned RTL to all variables yet, so it's
6656 not safe to try to expand expressions involving them. */
6657 cfun->x_dont_save_pending_sizes_p = 1;
6660 /* Emit diagnostics that require gimple input for detection. Operate on
6661 FNDECL and all its nested functions. */
6663 static void
6664 c_gimple_diagnostics_recursively (tree fndecl)
6666 struct cgraph_node *cgn;
6668 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6669 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6671 /* Notice when OpenMP structured block constraints are violated. */
6672 if (flag_openmp)
6673 diagnose_omp_structured_block_errors (fndecl);
6675 /* Finalize all nested functions now. */
6676 cgn = cgraph_node (fndecl);
6677 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6678 c_gimple_diagnostics_recursively (cgn->decl);
6681 /* Finish up a function declaration and compile that function
6682 all the way to assembler language output. The free the storage
6683 for the function definition.
6685 This is called after parsing the body of the function definition. */
6687 void
6688 finish_function (void)
6690 tree fndecl = current_function_decl;
6692 label_context_stack_se = label_context_stack_se->next;
6693 label_context_stack_vm = label_context_stack_vm->next;
6695 if (TREE_CODE (fndecl) == FUNCTION_DECL
6696 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6698 tree args = DECL_ARGUMENTS (fndecl);
6699 for (; args; args = TREE_CHAIN (args))
6701 tree type = TREE_TYPE (args);
6702 if (INTEGRAL_TYPE_P (type)
6703 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6704 DECL_ARG_TYPE (args) = integer_type_node;
6708 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6709 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6711 /* Must mark the RESULT_DECL as being in this function. */
6713 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6714 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6716 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6718 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6719 != integer_type_node)
6721 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6722 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6723 if (!warn_main)
6724 pedwarn ("return type of %q+D is not %<int%>", fndecl);
6726 else
6728 if (flag_isoc99)
6730 tree stmt = c_finish_return (integer_zero_node);
6731 #ifdef USE_MAPPED_LOCATION
6732 /* Hack. We don't want the middle-end to warn that this return
6733 is unreachable, so we mark its location as special. Using
6734 UNKNOWN_LOCATION has the problem that it gets clobbered in
6735 annotate_one_with_locus. A cleaner solution might be to
6736 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6738 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6739 #else
6740 /* Hack. We don't want the middle-end to warn that this
6741 return is unreachable, so put the statement on the
6742 special line 0. */
6743 annotate_with_file_line (stmt, input_filename, 0);
6744 #endif
6749 /* Tie off the statement tree for this function. */
6750 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6752 finish_fname_decls ();
6754 /* Complain if there's just no return statement. */
6755 if (warn_return_type
6756 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6757 && !current_function_returns_value && !current_function_returns_null
6758 /* Don't complain if we are no-return. */
6759 && !current_function_returns_abnormally
6760 /* Don't warn for main(). */
6761 && !MAIN_NAME_P (DECL_NAME (fndecl))
6762 /* Or if they didn't actually specify a return type. */
6763 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6764 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6765 inline function, as we might never be compiled separately. */
6766 && DECL_INLINE (fndecl))
6768 warning (OPT_Wreturn_type,
6769 "no return statement in function returning non-void");
6770 TREE_NO_WARNING (fndecl) = 1;
6773 /* With just -Wextra, complain only if function returns both with
6774 and without a value. */
6775 if (extra_warnings
6776 && current_function_returns_value
6777 && current_function_returns_null)
6778 warning (OPT_Wextra, "this function may return with or without a value");
6780 /* Store the end of the function, so that we get good line number
6781 info for the epilogue. */
6782 cfun->function_end_locus = input_location;
6784 /* If we don't have ctors/dtors sections, and this is a static
6785 constructor or destructor, it must be recorded now. */
6786 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6787 && !targetm.have_ctors_dtors)
6788 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6789 if (DECL_STATIC_DESTRUCTOR (fndecl)
6790 && !targetm.have_ctors_dtors)
6791 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6793 /* Finalize the ELF visibility for the function. */
6794 c_determine_visibility (fndecl);
6796 /* Genericize before inlining. Delay genericizing nested functions
6797 until their parent function is genericized. Since finalizing
6798 requires GENERIC, delay that as well. */
6800 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6801 && !undef_nested_function)
6803 if (!decl_function_context (fndecl))
6805 c_genericize (fndecl);
6806 c_gimple_diagnostics_recursively (fndecl);
6808 /* ??? Objc emits functions after finalizing the compilation unit.
6809 This should be cleaned up later and this conditional removed. */
6810 if (cgraph_global_info_ready)
6812 c_expand_body (fndecl);
6813 return;
6816 cgraph_finalize_function (fndecl, false);
6818 else
6820 /* Register this function with cgraph just far enough to get it
6821 added to our parent's nested function list. Handy, since the
6822 C front end doesn't have such a list. */
6823 (void) cgraph_node (fndecl);
6827 if (!decl_function_context (fndecl))
6828 undef_nested_function = false;
6830 /* We're leaving the context of this function, so zap cfun.
6831 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6832 tree_rest_of_compilation. */
6833 cfun = NULL;
6834 current_function_decl = NULL;
6837 /* Generate the RTL for the body of FNDECL. */
6839 void
6840 c_expand_body (tree fndecl)
6843 if (!DECL_INITIAL (fndecl)
6844 || DECL_INITIAL (fndecl) == error_mark_node)
6845 return;
6847 tree_rest_of_compilation (fndecl);
6849 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6850 && targetm.have_ctors_dtors)
6851 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6852 DEFAULT_INIT_PRIORITY);
6853 if (DECL_STATIC_DESTRUCTOR (fndecl)
6854 && targetm.have_ctors_dtors)
6855 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6856 DEFAULT_INIT_PRIORITY);
6859 /* Check the declarations given in a for-loop for satisfying the C99
6860 constraints. If exactly one such decl is found, return it. */
6862 tree
6863 check_for_loop_decls (void)
6865 struct c_binding *b;
6866 tree one_decl = NULL_TREE;
6867 int n_decls = 0;
6870 if (!flag_isoc99)
6872 /* If we get here, declarations have been used in a for loop without
6873 the C99 for loop scope. This doesn't make much sense, so don't
6874 allow it. */
6875 error ("%<for%> loop initial declaration used outside C99 mode");
6876 return NULL_TREE;
6878 /* C99 subclause 6.8.5 paragraph 3:
6880 [#3] The declaration part of a for statement shall only
6881 declare identifiers for objects having storage class auto or
6882 register.
6884 It isn't clear whether, in this sentence, "identifiers" binds to
6885 "shall only declare" or to "objects" - that is, whether all identifiers
6886 declared must be identifiers for objects, or whether the restriction
6887 only applies to those that are. (A question on this in comp.std.c
6888 in November 2000 received no answer.) We implement the strictest
6889 interpretation, to avoid creating an extension which later causes
6890 problems. */
6892 for (b = current_scope->bindings; b; b = b->prev)
6894 tree id = b->id;
6895 tree decl = b->decl;
6897 if (!id)
6898 continue;
6900 switch (TREE_CODE (decl))
6902 case VAR_DECL:
6903 if (TREE_STATIC (decl))
6904 error ("declaration of static variable %q+D in %<for%> loop "
6905 "initial declaration", decl);
6906 else if (DECL_EXTERNAL (decl))
6907 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6908 "initial declaration", decl);
6909 break;
6911 case RECORD_TYPE:
6912 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6913 id);
6914 break;
6915 case UNION_TYPE:
6916 error ("%<union %E%> declared in %<for%> loop initial declaration",
6917 id);
6918 break;
6919 case ENUMERAL_TYPE:
6920 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6921 id);
6922 break;
6923 default:
6924 error ("declaration of non-variable %q+D in %<for%> loop "
6925 "initial declaration", decl);
6928 n_decls++;
6929 one_decl = decl;
6932 return n_decls == 1 ? one_decl : NULL_TREE;
6935 /* Save and reinitialize the variables
6936 used during compilation of a C function. */
6938 void
6939 c_push_function_context (struct function *f)
6941 struct language_function *p;
6942 p = GGC_NEW (struct language_function);
6943 f->language = p;
6945 p->base.x_stmt_tree = c_stmt_tree;
6946 p->x_break_label = c_break_label;
6947 p->x_cont_label = c_cont_label;
6948 p->x_switch_stack = c_switch_stack;
6949 p->arg_info = current_function_arg_info;
6950 p->returns_value = current_function_returns_value;
6951 p->returns_null = current_function_returns_null;
6952 p->returns_abnormally = current_function_returns_abnormally;
6953 p->warn_about_return_type = warn_about_return_type;
6956 /* Restore the variables used during compilation of a C function. */
6958 void
6959 c_pop_function_context (struct function *f)
6961 struct language_function *p = f->language;
6963 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6964 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6966 /* Stop pointing to the local nodes about to be freed. */
6967 /* But DECL_INITIAL must remain nonzero so we know this
6968 was an actual function definition. */
6969 DECL_INITIAL (current_function_decl) = error_mark_node;
6970 DECL_ARGUMENTS (current_function_decl) = 0;
6973 c_stmt_tree = p->base.x_stmt_tree;
6974 c_break_label = p->x_break_label;
6975 c_cont_label = p->x_cont_label;
6976 c_switch_stack = p->x_switch_stack;
6977 current_function_arg_info = p->arg_info;
6978 current_function_returns_value = p->returns_value;
6979 current_function_returns_null = p->returns_null;
6980 current_function_returns_abnormally = p->returns_abnormally;
6981 warn_about_return_type = p->warn_about_return_type;
6983 f->language = NULL;
6986 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6988 void
6989 c_dup_lang_specific_decl (tree decl)
6991 struct lang_decl *ld;
6993 if (!DECL_LANG_SPECIFIC (decl))
6994 return;
6996 ld = GGC_NEW (struct lang_decl);
6997 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6998 DECL_LANG_SPECIFIC (decl) = ld;
7001 /* The functions below are required for functionality of doing
7002 function at once processing in the C front end. Currently these
7003 functions are not called from anywhere in the C front end, but as
7004 these changes continue, that will change. */
7006 /* Returns the stmt_tree (if any) to which statements are currently
7007 being added. If there is no active statement-tree, NULL is
7008 returned. */
7010 stmt_tree
7011 current_stmt_tree (void)
7013 return &c_stmt_tree;
7016 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
7017 C. */
7020 anon_aggr_type_p (tree ARG_UNUSED (node))
7022 return 0;
7025 /* Return the global value of T as a symbol. */
7027 tree
7028 identifier_global_value (tree t)
7030 struct c_binding *b;
7032 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7033 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7034 return b->decl;
7036 return 0;
7039 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7040 otherwise the name is found in ridpointers from RID_INDEX. */
7042 void
7043 record_builtin_type (enum rid rid_index, const char *name, tree type)
7045 tree id, decl;
7046 if (name == 0)
7047 id = ridpointers[(int) rid_index];
7048 else
7049 id = get_identifier (name);
7050 decl = build_decl (TYPE_DECL, id, type);
7051 pushdecl (decl);
7052 if (debug_hooks->type_decl)
7053 debug_hooks->type_decl (decl, false);
7056 /* Build the void_list_node (void_type_node having been created). */
7057 tree
7058 build_void_list_node (void)
7060 tree t = build_tree_list (NULL_TREE, void_type_node);
7061 return t;
7064 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7066 struct c_parm *
7067 build_c_parm (struct c_declspecs *specs, tree attrs,
7068 struct c_declarator *declarator)
7070 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7071 ret->specs = specs;
7072 ret->attrs = attrs;
7073 ret->declarator = declarator;
7074 return ret;
7077 /* Return a declarator with nested attributes. TARGET is the inner
7078 declarator to which these attributes apply. ATTRS are the
7079 attributes. */
7081 struct c_declarator *
7082 build_attrs_declarator (tree attrs, struct c_declarator *target)
7084 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7085 ret->kind = cdk_attrs;
7086 ret->declarator = target;
7087 ret->u.attrs = attrs;
7088 return ret;
7091 /* Return a declarator for a function with arguments specified by ARGS
7092 and return type specified by TARGET. */
7094 struct c_declarator *
7095 build_function_declarator (struct c_arg_info *args,
7096 struct c_declarator *target)
7098 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7099 ret->kind = cdk_function;
7100 ret->declarator = target;
7101 ret->u.arg_info = args;
7102 return ret;
7105 /* Return a declarator for the identifier IDENT (which may be
7106 NULL_TREE for an abstract declarator). */
7108 struct c_declarator *
7109 build_id_declarator (tree ident)
7111 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7112 ret->kind = cdk_id;
7113 ret->declarator = 0;
7114 ret->u.id = ident;
7115 /* Default value - may get reset to a more precise location. */
7116 ret->id_loc = input_location;
7117 return ret;
7120 /* Return something to represent absolute declarators containing a *.
7121 TARGET is the absolute declarator that the * contains.
7122 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7123 to apply to the pointer type. */
7125 struct c_declarator *
7126 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7127 struct c_declarator *target)
7129 tree attrs;
7130 int quals = 0;
7131 struct c_declarator *itarget = target;
7132 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7133 if (type_quals_attrs)
7135 attrs = type_quals_attrs->attrs;
7136 quals = quals_from_declspecs (type_quals_attrs);
7137 if (attrs != NULL_TREE)
7138 itarget = build_attrs_declarator (attrs, target);
7140 ret->kind = cdk_pointer;
7141 ret->declarator = itarget;
7142 ret->u.pointer_quals = quals;
7143 return ret;
7146 /* Return a pointer to a structure for an empty list of declaration
7147 specifiers. */
7149 struct c_declspecs *
7150 build_null_declspecs (void)
7152 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7153 ret->type = 0;
7154 ret->decl_attr = 0;
7155 ret->attrs = 0;
7156 ret->typespec_word = cts_none;
7157 ret->storage_class = csc_none;
7158 ret->declspecs_seen_p = false;
7159 ret->type_seen_p = false;
7160 ret->non_sc_seen_p = false;
7161 ret->typedef_p = false;
7162 ret->tag_defined_p = false;
7163 ret->explicit_signed_p = false;
7164 ret->deprecated_p = false;
7165 ret->default_int_p = false;
7166 ret->long_p = false;
7167 ret->long_long_p = false;
7168 ret->short_p = false;
7169 ret->signed_p = false;
7170 ret->unsigned_p = false;
7171 ret->complex_p = false;
7172 ret->inline_p = false;
7173 ret->thread_p = false;
7174 ret->const_p = false;
7175 ret->volatile_p = false;
7176 ret->restrict_p = false;
7177 return ret;
7180 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7181 returning SPECS. */
7183 struct c_declspecs *
7184 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7186 enum rid i;
7187 bool dupe = false;
7188 specs->non_sc_seen_p = true;
7189 specs->declspecs_seen_p = true;
7190 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7191 && C_IS_RESERVED_WORD (qual));
7192 i = C_RID_CODE (qual);
7193 switch (i)
7195 case RID_CONST:
7196 dupe = specs->const_p;
7197 specs->const_p = true;
7198 break;
7199 case RID_VOLATILE:
7200 dupe = specs->volatile_p;
7201 specs->volatile_p = true;
7202 break;
7203 case RID_RESTRICT:
7204 dupe = specs->restrict_p;
7205 specs->restrict_p = true;
7206 break;
7207 default:
7208 gcc_unreachable ();
7210 if (dupe && pedantic && !flag_isoc99)
7211 pedwarn ("duplicate %qE", qual);
7212 return specs;
7215 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7216 returning SPECS. */
7218 struct c_declspecs *
7219 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7221 tree type = spec.spec;
7222 specs->non_sc_seen_p = true;
7223 specs->declspecs_seen_p = true;
7224 specs->type_seen_p = true;
7225 if (TREE_DEPRECATED (type))
7226 specs->deprecated_p = true;
7228 /* Handle type specifier keywords. */
7229 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
7231 enum rid i = C_RID_CODE (type);
7232 if (specs->type)
7234 error ("two or more data types in declaration specifiers");
7235 return specs;
7237 if ((int) i <= (int) RID_LAST_MODIFIER)
7239 /* "long", "short", "signed", "unsigned" or "_Complex". */
7240 bool dupe = false;
7241 switch (i)
7243 case RID_LONG:
7244 if (specs->long_long_p)
7246 error ("%<long long long%> is too long for GCC");
7247 break;
7249 if (specs->long_p)
7251 if (specs->typespec_word == cts_double)
7253 error ("both %<long long%> and %<double%> in "
7254 "declaration specifiers");
7255 break;
7257 if (pedantic && !flag_isoc99 && !in_system_header
7258 && warn_long_long)
7259 pedwarn ("ISO C90 does not support %<long long%>");
7260 specs->long_long_p = 1;
7261 break;
7263 if (specs->short_p)
7264 error ("both %<long%> and %<short%> in "
7265 "declaration specifiers");
7266 else if (specs->typespec_word == cts_void)
7267 error ("both %<long%> and %<void%> in "
7268 "declaration specifiers");
7269 else if (specs->typespec_word == cts_bool)
7270 error ("both %<long%> and %<_Bool%> in "
7271 "declaration specifiers");
7272 else if (specs->typespec_word == cts_char)
7273 error ("both %<long%> and %<char%> in "
7274 "declaration specifiers");
7275 else if (specs->typespec_word == cts_float)
7276 error ("both %<long%> and %<float%> in "
7277 "declaration specifiers");
7278 else if (specs->typespec_word == cts_dfloat32)
7279 error ("both %<long%> and %<_Decimal32%> in "
7280 "declaration specifiers");
7281 else if (specs->typespec_word == cts_dfloat64)
7282 error ("both %<long%> and %<_Decimal64%> in "
7283 "declaration specifiers");
7284 else if (specs->typespec_word == cts_dfloat128)
7285 error ("both %<long%> and %<_Decimal128%> in "
7286 "declaration specifiers");
7287 else
7288 specs->long_p = true;
7289 break;
7290 case RID_SHORT:
7291 dupe = specs->short_p;
7292 if (specs->long_p)
7293 error ("both %<long%> and %<short%> in "
7294 "declaration specifiers");
7295 else if (specs->typespec_word == cts_void)
7296 error ("both %<short%> and %<void%> in "
7297 "declaration specifiers");
7298 else if (specs->typespec_word == cts_bool)
7299 error ("both %<short%> and %<_Bool%> in "
7300 "declaration specifiers");
7301 else if (specs->typespec_word == cts_char)
7302 error ("both %<short%> and %<char%> in "
7303 "declaration specifiers");
7304 else if (specs->typespec_word == cts_float)
7305 error ("both %<short%> and %<float%> in "
7306 "declaration specifiers");
7307 else if (specs->typespec_word == cts_double)
7308 error ("both %<short%> and %<double%> in "
7309 "declaration specifiers");
7310 else if (specs->typespec_word == cts_dfloat32)
7311 error ("both %<short%> and %<_Decimal32%> in "
7312 "declaration specifiers");
7313 else if (specs->typespec_word == cts_dfloat64)
7314 error ("both %<short%> and %<_Decimal64%> in "
7315 "declaration specifiers");
7316 else if (specs->typespec_word == cts_dfloat128)
7317 error ("both %<short%> and %<_Decimal128%> in "
7318 "declaration specifiers");
7319 else
7320 specs->short_p = true;
7321 break;
7322 case RID_SIGNED:
7323 dupe = specs->signed_p;
7324 if (specs->unsigned_p)
7325 error ("both %<signed%> and %<unsigned%> in "
7326 "declaration specifiers");
7327 else if (specs->typespec_word == cts_void)
7328 error ("both %<signed%> and %<void%> in "
7329 "declaration specifiers");
7330 else if (specs->typespec_word == cts_bool)
7331 error ("both %<signed%> and %<_Bool%> in "
7332 "declaration specifiers");
7333 else if (specs->typespec_word == cts_float)
7334 error ("both %<signed%> and %<float%> in "
7335 "declaration specifiers");
7336 else if (specs->typespec_word == cts_double)
7337 error ("both %<signed%> and %<double%> in "
7338 "declaration specifiers");
7339 else if (specs->typespec_word == cts_dfloat32)
7340 error ("both %<signed%> and %<_Decimal32%> in "
7341 "declaration specifiers");
7342 else if (specs->typespec_word == cts_dfloat64)
7343 error ("both %<signed%> and %<_Decimal64%> in "
7344 "declaration specifiers");
7345 else if (specs->typespec_word == cts_dfloat128)
7346 error ("both %<signed%> and %<_Decimal128%> in "
7347 "declaration specifiers");
7348 else
7349 specs->signed_p = true;
7350 break;
7351 case RID_UNSIGNED:
7352 dupe = specs->unsigned_p;
7353 if (specs->signed_p)
7354 error ("both %<signed%> and %<unsigned%> in "
7355 "declaration specifiers");
7356 else if (specs->typespec_word == cts_void)
7357 error ("both %<unsigned%> and %<void%> in "
7358 "declaration specifiers");
7359 else if (specs->typespec_word == cts_bool)
7360 error ("both %<unsigned%> and %<_Bool%> in "
7361 "declaration specifiers");
7362 else if (specs->typespec_word == cts_float)
7363 error ("both %<unsigned%> and %<float%> in "
7364 "declaration specifiers");
7365 else if (specs->typespec_word == cts_double)
7366 error ("both %<unsigned%> and %<double%> in "
7367 "declaration specifiers");
7368 else if (specs->typespec_word == cts_dfloat32)
7369 error ("both %<unsigned%> and %<_Decimal32%> in "
7370 "declaration specifiers");
7371 else if (specs->typespec_word == cts_dfloat64)
7372 error ("both %<unsigned%> and %<_Decimal64%> in "
7373 "declaration specifiers");
7374 else if (specs->typespec_word == cts_dfloat128)
7375 error ("both %<unsigned%> and %<_Decimal128%> in "
7376 "declaration specifiers");
7377 else
7378 specs->unsigned_p = true;
7379 break;
7380 case RID_COMPLEX:
7381 dupe = specs->complex_p;
7382 if (pedantic && !flag_isoc99 && !in_system_header)
7383 pedwarn ("ISO C90 does not support complex types");
7384 if (specs->typespec_word == cts_void)
7385 error ("both %<complex%> and %<void%> in "
7386 "declaration specifiers");
7387 else if (specs->typespec_word == cts_bool)
7388 error ("both %<complex%> and %<_Bool%> in "
7389 "declaration specifiers");
7390 else if (specs->typespec_word == cts_dfloat32)
7391 error ("both %<complex%> and %<_Decimal32%> in "
7392 "declaration specifiers");
7393 else if (specs->typespec_word == cts_dfloat64)
7394 error ("both %<complex%> and %<_Decimal64%> in "
7395 "declaration specifiers");
7396 else if (specs->typespec_word == cts_dfloat128)
7397 error ("both %<complex%> and %<_Decimal128%> in "
7398 "declaration specifiers");
7399 else
7400 specs->complex_p = true;
7401 break;
7402 default:
7403 gcc_unreachable ();
7406 if (dupe)
7407 error ("duplicate %qE", type);
7409 return specs;
7411 else
7413 /* "void", "_Bool", "char", "int", "float" or "double". */
7414 if (specs->typespec_word != cts_none)
7416 error ("two or more data types in declaration specifiers");
7417 return specs;
7419 switch (i)
7421 case RID_VOID:
7422 if (specs->long_p)
7423 error ("both %<long%> and %<void%> in "
7424 "declaration specifiers");
7425 else if (specs->short_p)
7426 error ("both %<short%> and %<void%> in "
7427 "declaration specifiers");
7428 else if (specs->signed_p)
7429 error ("both %<signed%> and %<void%> in "
7430 "declaration specifiers");
7431 else if (specs->unsigned_p)
7432 error ("both %<unsigned%> and %<void%> in "
7433 "declaration specifiers");
7434 else if (specs->complex_p)
7435 error ("both %<complex%> and %<void%> in "
7436 "declaration specifiers");
7437 else
7438 specs->typespec_word = cts_void;
7439 return specs;
7440 case RID_BOOL:
7441 if (specs->long_p)
7442 error ("both %<long%> and %<_Bool%> in "
7443 "declaration specifiers");
7444 else if (specs->short_p)
7445 error ("both %<short%> and %<_Bool%> in "
7446 "declaration specifiers");
7447 else if (specs->signed_p)
7448 error ("both %<signed%> and %<_Bool%> in "
7449 "declaration specifiers");
7450 else if (specs->unsigned_p)
7451 error ("both %<unsigned%> and %<_Bool%> in "
7452 "declaration specifiers");
7453 else if (specs->complex_p)
7454 error ("both %<complex%> and %<_Bool%> in "
7455 "declaration specifiers");
7456 else
7457 specs->typespec_word = cts_bool;
7458 return specs;
7459 case RID_CHAR:
7460 if (specs->long_p)
7461 error ("both %<long%> and %<char%> in "
7462 "declaration specifiers");
7463 else if (specs->short_p)
7464 error ("both %<short%> and %<char%> in "
7465 "declaration specifiers");
7466 else
7467 specs->typespec_word = cts_char;
7468 return specs;
7469 case RID_INT:
7470 specs->typespec_word = cts_int;
7471 return specs;
7472 case RID_FLOAT:
7473 if (specs->long_p)
7474 error ("both %<long%> and %<float%> in "
7475 "declaration specifiers");
7476 else if (specs->short_p)
7477 error ("both %<short%> and %<float%> in "
7478 "declaration specifiers");
7479 else if (specs->signed_p)
7480 error ("both %<signed%> and %<float%> in "
7481 "declaration specifiers");
7482 else if (specs->unsigned_p)
7483 error ("both %<unsigned%> and %<float%> in "
7484 "declaration specifiers");
7485 else
7486 specs->typespec_word = cts_float;
7487 return specs;
7488 case RID_DOUBLE:
7489 if (specs->long_long_p)
7490 error ("both %<long long%> and %<double%> in "
7491 "declaration specifiers");
7492 else if (specs->short_p)
7493 error ("both %<short%> and %<double%> in "
7494 "declaration specifiers");
7495 else if (specs->signed_p)
7496 error ("both %<signed%> and %<double%> in "
7497 "declaration specifiers");
7498 else if (specs->unsigned_p)
7499 error ("both %<unsigned%> and %<double%> in "
7500 "declaration specifiers");
7501 else
7502 specs->typespec_word = cts_double;
7503 return specs;
7504 case RID_DFLOAT32:
7505 case RID_DFLOAT64:
7506 case RID_DFLOAT128:
7508 const char *str;
7509 if (i == RID_DFLOAT32)
7510 str = "_Decimal32";
7511 else if (i == RID_DFLOAT64)
7512 str = "_Decimal64";
7513 else
7514 str = "_Decimal128";
7515 if (specs->long_long_p)
7516 error ("both %<long long%> and %<%s%> in "
7517 "declaration specifiers", str);
7518 if (specs->long_p)
7519 error ("both %<long%> and %<%s%> in "
7520 "declaration specifiers", str);
7521 else if (specs->short_p)
7522 error ("both %<short%> and %<%s%> in "
7523 "declaration specifiers", str);
7524 else if (specs->signed_p)
7525 error ("both %<signed%> and %<%s%> in "
7526 "declaration specifiers", str);
7527 else if (specs->unsigned_p)
7528 error ("both %<unsigned%> and %<%s%> in "
7529 "declaration specifiers", str);
7530 else if (specs->complex_p)
7531 error ("both %<complex%> and %<%s%> in "
7532 "declaration specifiers", str);
7533 else if (i == RID_DFLOAT32)
7534 specs->typespec_word = cts_dfloat32;
7535 else if (i == RID_DFLOAT64)
7536 specs->typespec_word = cts_dfloat64;
7537 else
7538 specs->typespec_word = cts_dfloat128;
7540 if (!targetm.decimal_float_supported_p ())
7541 error ("decimal floating point not supported for this target");
7542 if (pedantic)
7543 pedwarn ("ISO C does not support decimal floating point");
7544 return specs;
7545 default:
7546 /* ObjC reserved word "id", handled below. */
7547 break;
7552 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7553 form of ObjC type, cases such as "int" and "long" being handled
7554 above), a TYPE (struct, union, enum and typeof specifiers) or an
7555 ERROR_MARK. In none of these cases may there have previously
7556 been any type specifiers. */
7557 if (specs->type || specs->typespec_word != cts_none
7558 || specs->long_p || specs->short_p || specs->signed_p
7559 || specs->unsigned_p || specs->complex_p)
7560 error ("two or more data types in declaration specifiers");
7561 else if (TREE_CODE (type) == TYPE_DECL)
7563 if (TREE_TYPE (type) == error_mark_node)
7564 ; /* Allow the type to default to int to avoid cascading errors. */
7565 else
7567 specs->type = TREE_TYPE (type);
7568 specs->decl_attr = DECL_ATTRIBUTES (type);
7569 specs->typedef_p = true;
7570 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7573 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7575 tree t = lookup_name (type);
7576 if (!t || TREE_CODE (t) != TYPE_DECL)
7577 error ("%qE fails to be a typedef or built in type", type);
7578 else if (TREE_TYPE (t) == error_mark_node)
7580 else
7581 specs->type = TREE_TYPE (t);
7583 else if (TREE_CODE (type) != ERROR_MARK)
7585 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7586 specs->tag_defined_p = true;
7587 if (spec.kind == ctsk_typeof)
7588 specs->typedef_p = true;
7589 specs->type = type;
7592 return specs;
7595 /* Add the storage class specifier or function specifier SCSPEC to the
7596 declaration specifiers SPECS, returning SPECS. */
7598 struct c_declspecs *
7599 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7601 enum rid i;
7602 enum c_storage_class n = csc_none;
7603 bool dupe = false;
7604 specs->declspecs_seen_p = true;
7605 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7606 && C_IS_RESERVED_WORD (scspec));
7607 i = C_RID_CODE (scspec);
7608 if (extra_warnings && specs->non_sc_seen_p)
7609 warning (OPT_Wextra, "%qE is not at beginning of declaration", scspec);
7610 switch (i)
7612 case RID_INLINE:
7613 /* C99 permits duplicate inline. Although of doubtful utility,
7614 it seems simplest to permit it in gnu89 mode as well, as
7615 there is also little utility in maintaining this as a
7616 difference between gnu89 and C99 inline. */
7617 dupe = false;
7618 specs->inline_p = true;
7619 break;
7620 case RID_THREAD:
7621 dupe = specs->thread_p;
7622 if (specs->storage_class == csc_auto)
7623 error ("%<__thread%> used with %<auto%>");
7624 else if (specs->storage_class == csc_register)
7625 error ("%<__thread%> used with %<register%>");
7626 else if (specs->storage_class == csc_typedef)
7627 error ("%<__thread%> used with %<typedef%>");
7628 else
7629 specs->thread_p = true;
7630 break;
7631 case RID_AUTO:
7632 n = csc_auto;
7633 break;
7634 case RID_EXTERN:
7635 n = csc_extern;
7636 /* Diagnose "__thread extern". */
7637 if (specs->thread_p)
7638 error ("%<__thread%> before %<extern%>");
7639 break;
7640 case RID_REGISTER:
7641 n = csc_register;
7642 break;
7643 case RID_STATIC:
7644 n = csc_static;
7645 /* Diagnose "__thread static". */
7646 if (specs->thread_p)
7647 error ("%<__thread%> before %<static%>");
7648 break;
7649 case RID_TYPEDEF:
7650 n = csc_typedef;
7651 break;
7652 default:
7653 gcc_unreachable ();
7655 if (n != csc_none && n == specs->storage_class)
7656 dupe = true;
7657 if (dupe)
7658 error ("duplicate %qE", scspec);
7659 if (n != csc_none)
7661 if (specs->storage_class != csc_none && n != specs->storage_class)
7663 error ("multiple storage classes in declaration specifiers");
7665 else
7667 specs->storage_class = n;
7668 if (n != csc_extern && n != csc_static && specs->thread_p)
7670 error ("%<__thread%> used with %qE", scspec);
7671 specs->thread_p = false;
7675 return specs;
7678 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7679 returning SPECS. */
7681 struct c_declspecs *
7682 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7684 specs->attrs = chainon (attrs, specs->attrs);
7685 specs->declspecs_seen_p = true;
7686 return specs;
7689 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7690 specifiers with any other type specifier to determine the resulting
7691 type. This is where ISO C checks on complex types are made, since
7692 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7693 double". */
7695 struct c_declspecs *
7696 finish_declspecs (struct c_declspecs *specs)
7698 /* If a type was specified as a whole, we have no modifiers and are
7699 done. */
7700 if (specs->type != NULL_TREE)
7702 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7703 && !specs->signed_p && !specs->unsigned_p
7704 && !specs->complex_p);
7705 return specs;
7708 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7709 has been specified, treat it as "int" unless "_Complex" is
7710 present and there are no other specifiers. If we just have
7711 "_Complex", it is equivalent to "_Complex double", but e.g.
7712 "_Complex short" is equivalent to "_Complex short int". */
7713 if (specs->typespec_word == cts_none)
7715 if (specs->long_p || specs->short_p
7716 || specs->signed_p || specs->unsigned_p)
7718 specs->typespec_word = cts_int;
7720 else if (specs->complex_p)
7722 specs->typespec_word = cts_double;
7723 if (pedantic)
7724 pedwarn ("ISO C does not support plain %<complex%> meaning "
7725 "%<double complex%>");
7727 else
7729 specs->typespec_word = cts_int;
7730 specs->default_int_p = true;
7731 /* We don't diagnose this here because grokdeclarator will
7732 give more specific diagnostics according to whether it is
7733 a function definition. */
7737 /* If "signed" was specified, record this to distinguish "int" and
7738 "signed int" in the case of a bit-field with
7739 -funsigned-bitfields. */
7740 specs->explicit_signed_p = specs->signed_p;
7742 /* Now compute the actual type. */
7743 switch (specs->typespec_word)
7745 case cts_void:
7746 gcc_assert (!specs->long_p && !specs->short_p
7747 && !specs->signed_p && !specs->unsigned_p
7748 && !specs->complex_p);
7749 specs->type = void_type_node;
7750 break;
7751 case cts_bool:
7752 gcc_assert (!specs->long_p && !specs->short_p
7753 && !specs->signed_p && !specs->unsigned_p
7754 && !specs->complex_p);
7755 specs->type = boolean_type_node;
7756 break;
7757 case cts_char:
7758 gcc_assert (!specs->long_p && !specs->short_p);
7759 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7760 if (specs->signed_p)
7761 specs->type = signed_char_type_node;
7762 else if (specs->unsigned_p)
7763 specs->type = unsigned_char_type_node;
7764 else
7765 specs->type = char_type_node;
7766 if (specs->complex_p)
7768 if (pedantic)
7769 pedwarn ("ISO C does not support complex integer types");
7770 specs->type = build_complex_type (specs->type);
7772 break;
7773 case cts_int:
7774 gcc_assert (!(specs->long_p && specs->short_p));
7775 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7776 if (specs->long_long_p)
7777 specs->type = (specs->unsigned_p
7778 ? long_long_unsigned_type_node
7779 : long_long_integer_type_node);
7780 else if (specs->long_p)
7781 specs->type = (specs->unsigned_p
7782 ? long_unsigned_type_node
7783 : long_integer_type_node);
7784 else if (specs->short_p)
7785 specs->type = (specs->unsigned_p
7786 ? short_unsigned_type_node
7787 : short_integer_type_node);
7788 else
7789 specs->type = (specs->unsigned_p
7790 ? unsigned_type_node
7791 : integer_type_node);
7792 if (specs->complex_p)
7794 if (pedantic)
7795 pedwarn ("ISO C does not support complex integer types");
7796 specs->type = build_complex_type (specs->type);
7798 break;
7799 case cts_float:
7800 gcc_assert (!specs->long_p && !specs->short_p
7801 && !specs->signed_p && !specs->unsigned_p);
7802 specs->type = (specs->complex_p
7803 ? complex_float_type_node
7804 : float_type_node);
7805 break;
7806 case cts_double:
7807 gcc_assert (!specs->long_long_p && !specs->short_p
7808 && !specs->signed_p && !specs->unsigned_p);
7809 if (specs->long_p)
7811 specs->type = (specs->complex_p
7812 ? complex_long_double_type_node
7813 : long_double_type_node);
7815 else
7817 specs->type = (specs->complex_p
7818 ? complex_double_type_node
7819 : double_type_node);
7821 break;
7822 case cts_dfloat32:
7823 case cts_dfloat64:
7824 case cts_dfloat128:
7825 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7826 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7827 if (specs->typespec_word == cts_dfloat32)
7828 specs->type = dfloat32_type_node;
7829 else if (specs->typespec_word == cts_dfloat64)
7830 specs->type = dfloat64_type_node;
7831 else
7832 specs->type = dfloat128_type_node;
7833 break;
7834 default:
7835 gcc_unreachable ();
7838 return specs;
7841 /* Synthesize a function which calls all the global ctors or global
7842 dtors in this file. This is only used for targets which do not
7843 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7844 static void
7845 build_cdtor (int method_type, tree cdtors)
7847 tree body = 0;
7849 if (!cdtors)
7850 return;
7852 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7853 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7854 &body);
7856 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7859 /* A subroutine of c_write_global_declarations. Perform final processing
7860 on one file scope's declarations (or the external scope's declarations),
7861 GLOBALS. */
7863 static void
7864 c_write_global_declarations_1 (tree globals)
7866 tree decl;
7867 bool reconsider;
7869 /* Process the decls in the order they were written. */
7870 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7872 /* Check for used but undefined static functions using the C
7873 standard's definition of "used", and set TREE_NO_WARNING so
7874 that check_global_declarations doesn't repeat the check. */
7875 if (TREE_CODE (decl) == FUNCTION_DECL
7876 && DECL_INITIAL (decl) == 0
7877 && DECL_EXTERNAL (decl)
7878 && !TREE_PUBLIC (decl)
7879 && C_DECL_USED (decl))
7881 pedwarn ("%q+F used but never defined", decl);
7882 TREE_NO_WARNING (decl) = 1;
7885 wrapup_global_declaration_1 (decl);
7890 reconsider = false;
7891 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7892 reconsider |= wrapup_global_declaration_2 (decl);
7894 while (reconsider);
7896 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7897 check_global_declaration_1 (decl);
7900 /* A subroutine of c_write_global_declarations Emit debug information for each
7901 of the declarations in GLOBALS. */
7903 static void
7904 c_write_global_declarations_2 (tree globals)
7906 tree decl;
7908 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
7909 debug_hooks->global_decl (decl);
7912 /* Preserve the external declarations scope across a garbage collect. */
7913 static GTY(()) tree ext_block;
7915 void
7916 c_write_global_declarations (void)
7918 tree t;
7920 /* We don't want to do this if generating a PCH. */
7921 if (pch_file)
7922 return;
7924 /* Don't waste time on further processing if -fsyntax-only or we've
7925 encountered errors. */
7926 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7927 return;
7929 /* Close the external scope. */
7930 ext_block = pop_scope ();
7931 external_scope = 0;
7932 gcc_assert (!current_scope);
7934 if (ext_block)
7936 tree tmp = BLOCK_VARS (ext_block);
7937 int flags;
7938 FILE * stream = dump_begin (TDI_tu, &flags);
7939 if (stream && tmp)
7941 dump_node (tmp, flags & ~TDF_SLIM, stream);
7942 dump_end (TDI_tu, stream);
7946 /* Process all file scopes in this compilation, and the external_scope,
7947 through wrapup_global_declarations and check_global_declarations. */
7948 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7949 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7950 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7952 /* Generate functions to call static constructors and destructors
7953 for targets that do not support .ctors/.dtors sections. These
7954 functions have magic names which are detected by collect2. */
7955 build_cdtor ('I', static_ctors); static_ctors = 0;
7956 build_cdtor ('D', static_dtors); static_dtors = 0;
7958 /* We're done parsing; proceed to optimize and emit assembly.
7959 FIXME: shouldn't be the front end's responsibility to call this. */
7960 cgraph_optimize ();
7962 /* After cgraph has had a chance to emit everything that's going to
7963 be emitted, output debug information for globals. */
7964 if (errorcount == 0 && sorrycount == 0)
7966 timevar_push (TV_SYMOUT);
7967 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7968 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
7969 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
7970 timevar_pop (TV_SYMOUT);
7973 ext_block = NULL;
7976 #include "gt-c-decl.h"