gcc/ChangeLog:
[official-gcc.git] / gcc / c-decl.c
blob648c805828d77a2ed41fcc7cb6d7c09af83bb6af
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 behavior 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) : (GIMPLE_TUPLE_P (&%h.generic) ? (union lang_tree_node *) 0 : (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 (CAN_HAVE_LOCATION_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
765 warn_for_unused_label (p);
767 /* Labels go in BLOCK_VARS. */
768 TREE_CHAIN (p) = BLOCK_VARS (block);
769 BLOCK_VARS (block) = p;
770 gcc_assert (I_LABEL_BINDING (b->id) == b);
771 I_LABEL_BINDING (b->id) = b->shadowed;
772 break;
774 case ENUMERAL_TYPE:
775 case UNION_TYPE:
776 case RECORD_TYPE:
777 set_type_context (p, context);
779 /* Types may not have tag-names, in which case the type
780 appears in the bindings list with b->id NULL. */
781 if (b->id)
783 gcc_assert (I_TAG_BINDING (b->id) == b);
784 I_TAG_BINDING (b->id) = b->shadowed;
786 break;
788 case FUNCTION_DECL:
789 /* Propagate TREE_ADDRESSABLE from nested functions to their
790 containing functions. */
791 if (!TREE_ASM_WRITTEN (p)
792 && DECL_INITIAL (p) != 0
793 && TREE_ADDRESSABLE (p)
794 && DECL_ABSTRACT_ORIGIN (p) != 0
795 && DECL_ABSTRACT_ORIGIN (p) != p)
796 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
797 if (!DECL_EXTERNAL (p)
798 && !DECL_INITIAL (p)
799 && scope != file_scope
800 && scope != external_scope)
802 error ("nested function %q+D declared but never defined", p);
803 undef_nested_function = true;
805 /* C99 6.7.4p6: "a function with external linkage... declared
806 with an inline function specifier ... shall also be defined in the
807 same translation unit." */
808 else if (DECL_DECLARED_INLINE_P (p)
809 && TREE_PUBLIC (p)
810 && !DECL_INITIAL (p)
811 && flag_isoc99)
812 pedwarn ("inline function %q+D declared but never defined", p);
814 goto common_symbol;
816 case VAR_DECL:
817 /* Warnings for unused variables. */
818 if (!TREE_USED (p)
819 && !TREE_NO_WARNING (p)
820 && !DECL_IN_SYSTEM_HEADER (p)
821 && DECL_NAME (p)
822 && !DECL_ARTIFICIAL (p)
823 && scope != file_scope
824 && scope != external_scope)
825 warning (OPT_Wunused_variable, "unused variable %q+D", p);
827 if (b->inner_comp)
829 error ("type of array %q+D completed incompatibly with"
830 " implicit initialization", p);
833 /* Fall through. */
834 case TYPE_DECL:
835 case CONST_DECL:
836 common_symbol:
837 /* All of these go in BLOCK_VARS, but only if this is the
838 binding in the home scope. */
839 if (!b->nested)
841 TREE_CHAIN (p) = BLOCK_VARS (block);
842 BLOCK_VARS (block) = p;
844 /* If this is the file scope, and we are processing more
845 than one translation unit in this compilation, set
846 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
847 This makes same_translation_unit_p work, and causes
848 static declarations to be given disambiguating suffixes. */
849 if (scope == file_scope && num_in_fnames > 1)
851 DECL_CONTEXT (p) = context;
852 if (TREE_CODE (p) == TYPE_DECL)
853 set_type_context (TREE_TYPE (p), context);
856 /* Fall through. */
857 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
858 already been put there by store_parm_decls. Unused-
859 parameter warnings are handled by function.c.
860 error_mark_node obviously does not go in BLOCK_VARS and
861 does not get unused-variable warnings. */
862 case PARM_DECL:
863 case ERROR_MARK:
864 /* It is possible for a decl not to have a name. We get
865 here with b->id NULL in this case. */
866 if (b->id)
868 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
869 I_SYMBOL_BINDING (b->id) = b->shadowed;
870 if (b->shadowed && b->shadowed->type)
871 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
873 break;
875 default:
876 gcc_unreachable ();
881 /* Dispose of the block that we just made inside some higher level. */
882 if ((scope->function_body || scope == file_scope) && context)
884 DECL_INITIAL (context) = block;
885 BLOCK_SUPERCONTEXT (block) = context;
887 else if (scope->outer)
889 if (block)
890 SCOPE_LIST_APPEND (scope->outer, blocks, block);
891 /* If we did not make a block for the scope just exited, any
892 blocks made for inner scopes must be carried forward so they
893 will later become subblocks of something else. */
894 else if (scope->blocks)
895 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
898 /* Pop the current scope, and free the structure for reuse. */
899 current_scope = scope->outer;
900 if (scope->function_body)
901 current_function_scope = scope->outer_function;
903 memset (scope, 0, sizeof (struct c_scope));
904 scope->outer = scope_freelist;
905 scope_freelist = scope;
907 return block;
910 void
911 push_file_scope (void)
913 tree decl;
915 if (file_scope)
916 return;
918 push_scope ();
919 file_scope = current_scope;
921 start_fname_decls ();
923 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
924 bind (DECL_NAME (decl), decl, file_scope,
925 /*invisible=*/false, /*nested=*/true);
928 void
929 pop_file_scope (void)
931 /* In case there were missing closebraces, get us back to the global
932 binding level. */
933 while (current_scope != file_scope)
934 pop_scope ();
936 /* __FUNCTION__ is defined at file scope (""). This
937 call may not be necessary as my tests indicate it
938 still works without it. */
939 finish_fname_decls ();
941 /* This is the point to write out a PCH if we're doing that.
942 In that case we do not want to do anything else. */
943 if (pch_file)
945 c_common_write_pch ();
946 return;
949 /* Pop off the file scope and close this translation unit. */
950 pop_scope ();
951 file_scope = 0;
953 maybe_apply_pending_pragma_weaks ();
954 cgraph_finalize_compilation_unit ();
957 /* Insert BLOCK at the end of the list of subblocks of the current
958 scope. This is used when a BIND_EXPR is expanded, to handle the
959 BLOCK node inside the BIND_EXPR. */
961 void
962 insert_block (tree block)
964 TREE_USED (block) = 1;
965 SCOPE_LIST_APPEND (current_scope, blocks, block);
968 /* Push a definition or a declaration of struct, union or enum tag "name".
969 "type" should be the type node.
970 We assume that the tag "name" is not already defined.
972 Note that the definition may really be just a forward reference.
973 In that case, the TYPE_SIZE will be zero. */
975 static void
976 pushtag (tree name, tree type)
978 /* Record the identifier as the type's name if it has none. */
979 if (name && !TYPE_NAME (type))
980 TYPE_NAME (type) = name;
981 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
983 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
984 tagged type we just added to the current scope. This fake
985 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
986 to output a representation of a tagged type, and it also gives
987 us a convenient place to record the "scope start" address for the
988 tagged type. */
990 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
992 /* An approximation for now, so we can tell this is a function-scope tag.
993 This will be updated in pop_scope. */
994 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
997 /* Subroutine of compare_decls. Allow harmless mismatches in return
998 and argument types provided that the type modes match. This function
999 return a unified type given a suitable match, and 0 otherwise. */
1001 static tree
1002 match_builtin_function_types (tree newtype, tree oldtype)
1004 tree newrettype, oldrettype;
1005 tree newargs, oldargs;
1006 tree trytype, tryargs;
1008 /* Accept the return type of the new declaration if same modes. */
1009 oldrettype = TREE_TYPE (oldtype);
1010 newrettype = TREE_TYPE (newtype);
1012 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1013 return 0;
1015 oldargs = TYPE_ARG_TYPES (oldtype);
1016 newargs = TYPE_ARG_TYPES (newtype);
1017 tryargs = newargs;
1019 while (oldargs || newargs)
1021 if (!oldargs
1022 || !newargs
1023 || !TREE_VALUE (oldargs)
1024 || !TREE_VALUE (newargs)
1025 || TYPE_MODE (TREE_VALUE (oldargs))
1026 != TYPE_MODE (TREE_VALUE (newargs)))
1027 return 0;
1029 oldargs = TREE_CHAIN (oldargs);
1030 newargs = TREE_CHAIN (newargs);
1033 trytype = build_function_type (newrettype, tryargs);
1034 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1037 /* Subroutine of diagnose_mismatched_decls. Check for function type
1038 mismatch involving an empty arglist vs a nonempty one and give clearer
1039 diagnostics. */
1040 static void
1041 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1042 tree newtype, tree oldtype)
1044 tree t;
1046 if (TREE_CODE (olddecl) != FUNCTION_DECL
1047 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1048 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1050 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1051 return;
1053 t = TYPE_ARG_TYPES (oldtype);
1054 if (t == 0)
1055 t = TYPE_ARG_TYPES (newtype);
1056 for (; t; t = TREE_CHAIN (t))
1058 tree type = TREE_VALUE (t);
1060 if (TREE_CHAIN (t) == 0
1061 && TYPE_MAIN_VARIANT (type) != void_type_node)
1063 inform ("a parameter list with an ellipsis can%'t match "
1064 "an empty parameter name list declaration");
1065 break;
1068 if (c_type_promotes_to (type) != type)
1070 inform ("an argument type that has a default promotion can%'t match "
1071 "an empty parameter name list declaration");
1072 break;
1077 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1078 old-style function definition, NEWDECL is a prototype declaration.
1079 Diagnose inconsistencies in the argument list. Returns TRUE if
1080 the prototype is compatible, FALSE if not. */
1081 static bool
1082 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1084 tree newargs, oldargs;
1085 int i;
1087 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1089 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1090 newargs = TYPE_ARG_TYPES (newtype);
1091 i = 1;
1093 for (;;)
1095 tree oldargtype = TREE_VALUE (oldargs);
1096 tree newargtype = TREE_VALUE (newargs);
1098 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1099 return false;
1101 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1102 newargtype = TYPE_MAIN_VARIANT (newargtype);
1104 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1105 break;
1107 /* Reaching the end of just one list means the two decls don't
1108 agree on the number of arguments. */
1109 if (END_OF_ARGLIST (oldargtype))
1111 error ("prototype for %q+D declares more arguments "
1112 "than previous old-style definition", newdecl);
1113 return false;
1115 else if (END_OF_ARGLIST (newargtype))
1117 error ("prototype for %q+D declares fewer arguments "
1118 "than previous old-style definition", newdecl);
1119 return false;
1122 /* Type for passing arg must be consistent with that declared
1123 for the arg. */
1124 else if (!comptypes (oldargtype, newargtype))
1126 error ("prototype for %q+D declares argument %d"
1127 " with incompatible type",
1128 newdecl, i);
1129 return false;
1132 oldargs = TREE_CHAIN (oldargs);
1133 newargs = TREE_CHAIN (newargs);
1134 i++;
1137 /* If we get here, no errors were found, but do issue a warning
1138 for this poor-style construct. */
1139 warning (0, "prototype for %q+D follows non-prototype definition",
1140 newdecl);
1141 return true;
1142 #undef END_OF_ARGLIST
1145 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1146 first in a pair of mismatched declarations, using the diagnostic
1147 function DIAG. */
1148 static void
1149 locate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2))
1151 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1153 else if (DECL_INITIAL (decl))
1154 diag (G_("previous definition of %q+D was here"), decl);
1155 else if (C_DECL_IMPLICIT (decl))
1156 diag (G_("previous implicit declaration of %q+D was here"), decl);
1157 else
1158 diag (G_("previous declaration of %q+D was here"), decl);
1161 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1162 Returns true if the caller should proceed to merge the two, false
1163 if OLDDECL should simply be discarded. As a side effect, issues
1164 all necessary diagnostics for invalid or poor-style combinations.
1165 If it returns true, writes the types of NEWDECL and OLDDECL to
1166 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1167 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1169 static bool
1170 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1171 tree *newtypep, tree *oldtypep)
1173 tree newtype, oldtype;
1174 bool pedwarned = false;
1175 bool warned = false;
1176 bool retval = true;
1178 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1179 && DECL_EXTERNAL (DECL))
1181 /* If we have error_mark_node for either decl or type, just discard
1182 the previous decl - we're in an error cascade already. */
1183 if (olddecl == error_mark_node || newdecl == error_mark_node)
1184 return false;
1185 *oldtypep = oldtype = TREE_TYPE (olddecl);
1186 *newtypep = newtype = TREE_TYPE (newdecl);
1187 if (oldtype == error_mark_node || newtype == error_mark_node)
1188 return false;
1190 /* Two different categories of symbol altogether. This is an error
1191 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1192 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1194 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1195 && DECL_BUILT_IN (olddecl)
1196 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1198 error ("%q+D redeclared as different kind of symbol", newdecl);
1199 locate_old_decl (olddecl, error);
1201 else if (TREE_PUBLIC (newdecl))
1202 warning (0, "built-in function %q+D declared as non-function",
1203 newdecl);
1204 else
1205 warning (OPT_Wshadow, "declaration of %q+D shadows "
1206 "a built-in function", newdecl);
1207 return false;
1210 /* Enumerators have no linkage, so may only be declared once in a
1211 given scope. */
1212 if (TREE_CODE (olddecl) == CONST_DECL)
1214 error ("redeclaration of enumerator %q+D", newdecl);
1215 locate_old_decl (olddecl, error);
1216 return false;
1219 if (!comptypes (oldtype, newtype))
1221 if (TREE_CODE (olddecl) == FUNCTION_DECL
1222 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1224 /* Accept harmless mismatch in function types.
1225 This is for the ffs and fprintf builtins. */
1226 tree trytype = match_builtin_function_types (newtype, oldtype);
1228 if (trytype && comptypes (newtype, trytype))
1229 *oldtypep = oldtype = trytype;
1230 else
1232 /* If types don't match for a built-in, throw away the
1233 built-in. No point in calling locate_old_decl here, it
1234 won't print anything. */
1235 warning (0, "conflicting types for built-in function %q+D",
1236 newdecl);
1237 return false;
1240 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1241 && DECL_IS_BUILTIN (olddecl))
1243 /* A conflicting function declaration for a predeclared
1244 function that isn't actually built in. Objective C uses
1245 these. The new declaration silently overrides everything
1246 but the volatility (i.e. noreturn) indication. See also
1247 below. FIXME: Make Objective C use normal builtins. */
1248 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1249 return false;
1251 /* Permit void foo (...) to match int foo (...) if the latter is
1252 the definition and implicit int was used. See
1253 c-torture/compile/920625-2.c. */
1254 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1255 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1256 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1257 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1259 pedwarn ("conflicting types for %q+D", newdecl);
1260 /* Make sure we keep void as the return type. */
1261 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1262 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1263 pedwarned = true;
1265 /* Permit void foo (...) to match an earlier call to foo (...) with
1266 no declared type (thus, implicitly int). */
1267 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1268 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1269 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1270 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1272 pedwarn ("conflicting types for %q+D", newdecl);
1273 /* Make sure we keep void as the return type. */
1274 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1275 pedwarned = true;
1277 else
1279 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1280 error ("conflicting type qualifiers for %q+D", newdecl);
1281 else
1282 error ("conflicting types for %q+D", newdecl);
1283 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1284 locate_old_decl (olddecl, error);
1285 return false;
1289 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1290 but silently ignore the redeclaration if either is in a system
1291 header. (Conflicting redeclarations were handled above.) */
1292 if (TREE_CODE (newdecl) == TYPE_DECL)
1294 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1295 return true; /* Allow OLDDECL to continue in use. */
1297 error ("redefinition of typedef %q+D", newdecl);
1298 locate_old_decl (olddecl, error);
1299 return false;
1302 /* Function declarations can either be 'static' or 'extern' (no
1303 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1304 can never conflict with each other on account of linkage
1305 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1306 gnu89 mode permits two definitions if one is 'extern inline' and
1307 one is not. The non- extern-inline definition supersedes the
1308 extern-inline definition. */
1310 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1312 /* If you declare a built-in function name as static, or
1313 define the built-in with an old-style definition (so we
1314 can't validate the argument list) the built-in definition is
1315 overridden, but optionally warn this was a bad choice of name. */
1316 if (DECL_BUILT_IN (olddecl)
1317 && !C_DECL_DECLARED_BUILTIN (olddecl)
1318 && (!TREE_PUBLIC (newdecl)
1319 || (DECL_INITIAL (newdecl)
1320 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1322 warning (OPT_Wshadow, "declaration of %q+D shadows "
1323 "a built-in function", newdecl);
1324 /* Discard the old built-in function. */
1325 return false;
1328 if (DECL_INITIAL (newdecl))
1330 if (DECL_INITIAL (olddecl))
1332 /* If both decls are in the same TU and the new declaration
1333 isn't overriding an extern inline reject the new decl.
1334 In c99, no overriding is allowed in the same translation
1335 unit. */
1336 if ((!DECL_EXTERN_INLINE (olddecl)
1337 || DECL_EXTERN_INLINE (newdecl)
1338 #if WANT_C99_INLINE_SEMANTICS
1339 || (flag_isoc99
1340 && (!DECL_DECLARED_INLINE_P (olddecl)
1341 || !lookup_attribute ("gnu_inline",
1342 DECL_ATTRIBUTES (olddecl)))
1343 && (!DECL_DECLARED_INLINE_P (newdecl)
1344 || !lookup_attribute ("gnu_inline",
1345 DECL_ATTRIBUTES (newdecl))))
1346 #endif /* WANT_C99_INLINE_SEMANTICS */
1348 && same_translation_unit_p (newdecl, olddecl))
1350 error ("redefinition of %q+D", newdecl);
1351 locate_old_decl (olddecl, error);
1352 return false;
1356 /* If we have a prototype after an old-style function definition,
1357 the argument types must be checked specially. */
1358 else if (DECL_INITIAL (olddecl)
1359 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1360 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1361 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1363 locate_old_decl (olddecl, error);
1364 return false;
1366 /* A non-static declaration (even an "extern") followed by a
1367 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1368 The same is true for a static forward declaration at block
1369 scope followed by a non-static declaration/definition at file
1370 scope. Static followed by non-static at the same scope is
1371 not undefined behavior, and is the most convenient way to get
1372 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1373 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1374 we do diagnose it if -Wtraditional. */
1375 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1377 /* Two exceptions to the rule. If olddecl is an extern
1378 inline, or a predeclared function that isn't actually
1379 built in, newdecl silently overrides olddecl. The latter
1380 occur only in Objective C; see also above. (FIXME: Make
1381 Objective C use normal builtins.) */
1382 if (!DECL_IS_BUILTIN (olddecl)
1383 && !DECL_EXTERN_INLINE (olddecl))
1385 error ("static declaration of %q+D follows "
1386 "non-static declaration", newdecl);
1387 locate_old_decl (olddecl, error);
1389 return false;
1391 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1393 if (DECL_CONTEXT (olddecl))
1395 error ("non-static declaration of %q+D follows "
1396 "static declaration", newdecl);
1397 locate_old_decl (olddecl, error);
1398 return false;
1400 else if (warn_traditional)
1402 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1403 "follows static declaration", newdecl);
1404 warned = true;
1408 /* Make sure gnu_inline attribute is either not present, or
1409 present on all inline decls. */
1410 if (DECL_DECLARED_INLINE_P (olddecl)
1411 && DECL_DECLARED_INLINE_P (newdecl))
1413 bool newa = lookup_attribute ("gnu_inline",
1414 DECL_ATTRIBUTES (newdecl)) != NULL;
1415 bool olda = lookup_attribute ("gnu_inline",
1416 DECL_ATTRIBUTES (olddecl)) != NULL;
1417 if (newa != olda)
1419 error ("%<gnu_inline%> attribute present on %q+D",
1420 newa ? newdecl : olddecl);
1421 error ("%Jbut not here", newa ? olddecl : newdecl);
1425 else if (TREE_CODE (newdecl) == VAR_DECL)
1427 /* Only variables can be thread-local, and all declarations must
1428 agree on this property. */
1429 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1431 /* Nothing to check. Since OLDDECL is marked threadprivate
1432 and NEWDECL does not have a thread-local attribute, we
1433 will merge the threadprivate attribute into NEWDECL. */
1436 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1438 if (DECL_THREAD_LOCAL_P (newdecl))
1439 error ("thread-local declaration of %q+D follows "
1440 "non-thread-local declaration", newdecl);
1441 else
1442 error ("non-thread-local declaration of %q+D follows "
1443 "thread-local declaration", newdecl);
1445 locate_old_decl (olddecl, error);
1446 return false;
1449 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1450 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1452 error ("redefinition of %q+D", newdecl);
1453 locate_old_decl (olddecl, error);
1454 return false;
1457 /* Objects declared at file scope: if the first declaration had
1458 external linkage (even if it was an external reference) the
1459 second must have external linkage as well, or the behavior is
1460 undefined. If the first declaration had internal linkage, then
1461 the second must too, or else be an external reference (in which
1462 case the composite declaration still has internal linkage).
1463 As for function declarations, we warn about the static-then-
1464 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1465 if (DECL_FILE_SCOPE_P (newdecl)
1466 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1468 if (DECL_EXTERNAL (newdecl))
1470 if (!DECL_FILE_SCOPE_P (olddecl))
1472 error ("extern declaration of %q+D follows "
1473 "declaration with no linkage", newdecl);
1474 locate_old_decl (olddecl, error);
1475 return false;
1477 else if (warn_traditional)
1479 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1480 "follows static declaration", newdecl);
1481 warned = true;
1484 else
1486 if (TREE_PUBLIC (newdecl))
1487 error ("non-static declaration of %q+D follows "
1488 "static declaration", newdecl);
1489 else
1490 error ("static declaration of %q+D follows "
1491 "non-static declaration", newdecl);
1493 locate_old_decl (olddecl, error);
1494 return false;
1497 /* Two objects with the same name declared at the same block
1498 scope must both be external references (6.7p3). */
1499 else if (!DECL_FILE_SCOPE_P (newdecl))
1501 if (DECL_EXTERNAL (newdecl))
1503 /* Extern with initializer at block scope, which will
1504 already have received an error. */
1506 else if (DECL_EXTERNAL (olddecl))
1508 error ("declaration of %q+D with no linkage follows "
1509 "extern declaration", newdecl);
1510 locate_old_decl (olddecl, error);
1512 else
1514 error ("redeclaration of %q+D with no linkage", newdecl);
1515 locate_old_decl (olddecl, error);
1518 return false;
1522 /* warnings */
1523 /* All decls must agree on a visibility. */
1524 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1525 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1526 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1528 warning (0, "redeclaration of %q+D with different visibility "
1529 "(old visibility preserved)", newdecl);
1530 warned = true;
1533 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1535 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1536 if (DECL_DECLARED_INLINE_P (newdecl)
1537 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1539 warning (OPT_Wattributes, "inline declaration of %qD follows "
1540 "declaration with attribute noinline", newdecl);
1541 warned = true;
1543 else if (DECL_DECLARED_INLINE_P (olddecl)
1544 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1546 warning (OPT_Wattributes, "declaration of %q+D with attribute "
1547 "noinline follows inline declaration ", newdecl);
1548 warned = true;
1551 /* Inline declaration after use or definition.
1552 ??? Should we still warn about this now we have unit-at-a-time
1553 mode and can get it right?
1554 Definitely don't complain if the decls are in different translation
1555 units.
1556 C99 permits this, so don't warn in that case. (The function
1557 may not be inlined everywhere in function-at-a-time mode, but
1558 we still shouldn't warn.) */
1559 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1560 && same_translation_unit_p (olddecl, newdecl)
1561 && ! flag_isoc99)
1563 if (TREE_USED (olddecl))
1565 warning (0, "%q+D declared inline after being called", olddecl);
1566 warned = true;
1568 else if (DECL_INITIAL (olddecl))
1570 warning (0, "%q+D declared inline after its definition", olddecl);
1571 warned = true;
1575 else /* PARM_DECL, VAR_DECL */
1577 /* Redeclaration of a parameter is a constraint violation (this is
1578 not explicitly stated, but follows from C99 6.7p3 [no more than
1579 one declaration of the same identifier with no linkage in the
1580 same scope, except type tags] and 6.2.2p6 [parameters have no
1581 linkage]). We must check for a forward parameter declaration,
1582 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1583 an extension, the mandatory diagnostic for which is handled by
1584 mark_forward_parm_decls. */
1586 if (TREE_CODE (newdecl) == PARM_DECL
1587 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1589 error ("redefinition of parameter %q+D", newdecl);
1590 locate_old_decl (olddecl, error);
1591 return false;
1595 /* Optional warning for completely redundant decls. */
1596 if (!warned && !pedwarned
1597 && warn_redundant_decls
1598 /* Don't warn about a function declaration followed by a
1599 definition. */
1600 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1601 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1602 /* Don't warn about redundant redeclarations of builtins. */
1603 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1604 && !DECL_BUILT_IN (newdecl)
1605 && DECL_BUILT_IN (olddecl)
1606 && !C_DECL_DECLARED_BUILTIN (olddecl))
1607 /* Don't warn about an extern followed by a definition. */
1608 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1609 /* Don't warn about forward parameter decls. */
1610 && !(TREE_CODE (newdecl) == PARM_DECL
1611 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1612 /* Don't warn about a variable definition following a declaration. */
1613 && !(TREE_CODE (newdecl) == VAR_DECL
1614 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1616 warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1617 newdecl);
1618 warned = true;
1621 /* Report location of previous decl/defn in a consistent manner. */
1622 if (warned || pedwarned)
1623 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1625 #undef DECL_EXTERN_INLINE
1627 return retval;
1630 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1631 consistent with OLDDECL, but carries new information. Merge the
1632 new information into OLDDECL. This function issues no
1633 diagnostics. */
1635 static void
1636 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1638 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1639 && DECL_INITIAL (newdecl) != 0);
1640 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1641 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1642 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1643 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1644 bool extern_changed = false;
1646 /* For real parm decl following a forward decl, rechain the old decl
1647 in its new location and clear TREE_ASM_WRITTEN (it's not a
1648 forward decl anymore). */
1649 if (TREE_CODE (newdecl) == PARM_DECL
1650 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1652 struct c_binding *b, **here;
1654 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1655 if ((*here)->decl == olddecl)
1656 goto found;
1657 gcc_unreachable ();
1659 found:
1660 b = *here;
1661 *here = b->prev;
1662 b->prev = current_scope->bindings;
1663 current_scope->bindings = b;
1665 TREE_ASM_WRITTEN (olddecl) = 0;
1668 DECL_ATTRIBUTES (newdecl)
1669 = targetm.merge_decl_attributes (olddecl, newdecl);
1671 /* Merge the data types specified in the two decls. */
1672 TREE_TYPE (newdecl)
1673 = TREE_TYPE (olddecl)
1674 = composite_type (newtype, oldtype);
1676 /* Lay the type out, unless already done. */
1677 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1679 if (TREE_TYPE (newdecl) != error_mark_node)
1680 layout_type (TREE_TYPE (newdecl));
1681 if (TREE_CODE (newdecl) != FUNCTION_DECL
1682 && TREE_CODE (newdecl) != TYPE_DECL
1683 && TREE_CODE (newdecl) != CONST_DECL)
1684 layout_decl (newdecl, 0);
1686 else
1688 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1689 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1690 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1691 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1692 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1693 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1695 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1696 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1701 /* Merge the type qualifiers. */
1702 if (TREE_READONLY (newdecl))
1703 TREE_READONLY (olddecl) = 1;
1705 if (TREE_THIS_VOLATILE (newdecl))
1707 TREE_THIS_VOLATILE (olddecl) = 1;
1708 if (TREE_CODE (newdecl) == VAR_DECL)
1709 make_var_volatile (newdecl);
1712 /* Merge deprecatedness. */
1713 if (TREE_DEPRECATED (newdecl))
1714 TREE_DEPRECATED (olddecl) = 1;
1716 /* Keep source location of definition rather than declaration and of
1717 prototype rather than non-prototype unless that prototype is
1718 built-in. */
1719 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1720 || (old_is_prototype && !new_is_prototype
1721 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1722 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1724 /* Merge the initialization information. */
1725 if (DECL_INITIAL (newdecl) == 0)
1726 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1728 /* Merge the threadprivate attribute. */
1729 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1731 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1732 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1735 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1737 /* Merge the unused-warning information. */
1738 if (DECL_IN_SYSTEM_HEADER (olddecl))
1739 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1740 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1741 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1743 /* Merge the section attribute.
1744 We want to issue an error if the sections conflict but that
1745 must be done later in decl_attributes since we are called
1746 before attributes are assigned. */
1747 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1748 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1750 /* Copy the assembler name.
1751 Currently, it can only be defined in the prototype. */
1752 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1754 /* Use visibility of whichever declaration had it specified */
1755 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1757 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1758 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1761 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1763 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1764 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1765 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1766 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1767 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1768 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1769 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1770 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1771 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1772 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1775 /* Merge the storage class information. */
1776 merge_weak (newdecl, olddecl);
1778 /* For functions, static overrides non-static. */
1779 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1781 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1782 /* This is since we don't automatically
1783 copy the attributes of NEWDECL into OLDDECL. */
1784 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1785 /* If this clears `static', clear it in the identifier too. */
1786 if (!TREE_PUBLIC (olddecl))
1787 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1791 #if WANT_C99_INLINE_SEMANTICS
1792 /* In c99, 'extern' declaration before (or after) 'inline' means this
1793 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1794 is present. */
1795 if (TREE_CODE (newdecl) == FUNCTION_DECL
1796 && flag_isoc99
1797 && (DECL_DECLARED_INLINE_P (newdecl)
1798 || DECL_DECLARED_INLINE_P (olddecl))
1799 && (!DECL_DECLARED_INLINE_P (newdecl)
1800 || !DECL_DECLARED_INLINE_P (olddecl)
1801 || !DECL_EXTERNAL (olddecl))
1802 && DECL_EXTERNAL (newdecl)
1803 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl)))
1804 DECL_EXTERNAL (newdecl) = 0;
1805 #endif /* WANT_C99_INLINE_SEMANTICS */
1807 if (DECL_EXTERNAL (newdecl))
1809 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1810 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1812 /* An extern decl does not override previous storage class. */
1813 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1814 if (!DECL_EXTERNAL (newdecl))
1816 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1817 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1820 else
1822 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1823 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1826 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1828 /* If we're redefining a function previously defined as extern
1829 inline, make sure we emit debug info for the inline before we
1830 throw it away, in case it was inlined into a function that
1831 hasn't been written out yet. */
1832 if (new_is_definition && DECL_INITIAL (olddecl))
1834 if (TREE_USED (olddecl)
1835 /* In unit-at-a-time mode we never inline re-defined extern
1836 inline functions. */
1837 && !flag_unit_at_a_time
1838 && cgraph_function_possibly_inlined_p (olddecl))
1839 (*debug_hooks->outlining_inline_function) (olddecl);
1841 /* The new defn must not be inline. */
1842 DECL_INLINE (newdecl) = 0;
1843 DECL_UNINLINABLE (newdecl) = 1;
1845 else
1847 /* If either decl says `inline', this fn is inline, unless
1848 its definition was passed already. */
1849 if (DECL_DECLARED_INLINE_P (newdecl)
1850 || DECL_DECLARED_INLINE_P (olddecl))
1851 DECL_DECLARED_INLINE_P (newdecl) = 1;
1853 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1854 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1857 if (DECL_BUILT_IN (olddecl))
1859 /* If redeclaring a builtin function, it stays built in.
1860 But it gets tagged as having been declared. */
1861 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1862 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1863 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1864 if (new_is_prototype)
1865 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1866 else
1867 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1868 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1871 /* Also preserve various other info from the definition. */
1872 if (!new_is_definition)
1874 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1875 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1876 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1877 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1878 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1880 /* Set DECL_INLINE on the declaration if we've got a body
1881 from which to instantiate. */
1882 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1884 DECL_INLINE (newdecl) = 1;
1885 DECL_ABSTRACT_ORIGIN (newdecl)
1886 = DECL_ABSTRACT_ORIGIN (olddecl);
1889 else
1891 /* If a previous declaration said inline, mark the
1892 definition as inlinable. */
1893 if (DECL_DECLARED_INLINE_P (newdecl)
1894 && !DECL_UNINLINABLE (newdecl))
1895 DECL_INLINE (newdecl) = 1;
1899 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1901 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1902 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1904 unsigned olddecl_uid = DECL_UID (olddecl);
1905 tree olddecl_context = DECL_CONTEXT (olddecl);
1907 memcpy ((char *) olddecl + sizeof (struct tree_common),
1908 (char *) newdecl + sizeof (struct tree_common),
1909 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1910 switch (TREE_CODE (olddecl))
1912 case FIELD_DECL:
1913 case VAR_DECL:
1914 case PARM_DECL:
1915 case LABEL_DECL:
1916 case RESULT_DECL:
1917 case CONST_DECL:
1918 case TYPE_DECL:
1919 case FUNCTION_DECL:
1920 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1921 (char *) newdecl + sizeof (struct tree_decl_common),
1922 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1923 break;
1925 default:
1927 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1928 (char *) newdecl + sizeof (struct tree_decl_common),
1929 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1931 DECL_UID (olddecl) = olddecl_uid;
1932 DECL_CONTEXT (olddecl) = olddecl_context;
1935 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1936 so that encode_section_info has a chance to look at the new decl
1937 flags and attributes. */
1938 if (DECL_RTL_SET_P (olddecl)
1939 && (TREE_CODE (olddecl) == FUNCTION_DECL
1940 || (TREE_CODE (olddecl) == VAR_DECL
1941 && TREE_STATIC (olddecl))))
1942 make_decl_rtl (olddecl);
1944 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1945 and the definition is coming from the old version, cgraph needs
1946 to be called again. */
1947 if (extern_changed && !new_is_definition
1948 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
1949 cgraph_finalize_function (olddecl, false);
1952 /* Handle when a new declaration NEWDECL has the same name as an old
1953 one OLDDECL in the same binding contour. Prints an error message
1954 if appropriate.
1956 If safely possible, alter OLDDECL to look like NEWDECL, and return
1957 true. Otherwise, return false. */
1959 static bool
1960 duplicate_decls (tree newdecl, tree olddecl)
1962 tree newtype = NULL, oldtype = NULL;
1964 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1966 /* Avoid `unused variable' and other warnings warnings for OLDDECL. */
1967 TREE_NO_WARNING (olddecl) = 1;
1968 return false;
1971 merge_decls (newdecl, olddecl, newtype, oldtype);
1972 return true;
1976 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1977 static void
1978 warn_if_shadowing (tree new_decl)
1980 struct c_binding *b;
1982 /* Shadow warnings wanted? */
1983 if (!warn_shadow
1984 /* No shadow warnings for internally generated vars. */
1985 || DECL_IS_BUILTIN (new_decl)
1986 /* No shadow warnings for vars made for inlining. */
1987 || DECL_FROM_INLINE (new_decl))
1988 return;
1990 /* Is anything being shadowed? Invisible decls do not count. */
1991 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1992 if (b->decl && b->decl != new_decl && !b->invisible)
1994 tree old_decl = b->decl;
1996 if (old_decl == error_mark_node)
1998 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1999 "non-variable", new_decl);
2000 break;
2002 else if (TREE_CODE (old_decl) == PARM_DECL)
2003 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2004 new_decl);
2005 else if (DECL_FILE_SCOPE_P (old_decl))
2006 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2007 "declaration", new_decl);
2008 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2009 && DECL_BUILT_IN (old_decl))
2011 warning (OPT_Wshadow, "declaration of %q+D shadows "
2012 "a built-in function", new_decl);
2013 break;
2015 else
2016 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2017 new_decl);
2019 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
2021 break;
2026 /* Subroutine of pushdecl.
2028 X is a TYPE_DECL for a typedef statement. Create a brand new
2029 ..._TYPE node (which will be just a variant of the existing
2030 ..._TYPE node with identical properties) and then install X
2031 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
2033 The whole point here is to end up with a situation where each
2034 and every ..._TYPE node the compiler creates will be uniquely
2035 associated with AT MOST one node representing a typedef name.
2036 This way, even though the compiler substitutes corresponding
2037 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2038 early on, later parts of the compiler can always do the reverse
2039 translation and get back the corresponding typedef name. For
2040 example, given:
2042 typedef struct S MY_TYPE;
2043 MY_TYPE object;
2045 Later parts of the compiler might only know that `object' was of
2046 type `struct S' if it were not for code just below. With this
2047 code however, later parts of the compiler see something like:
2049 struct S' == struct S
2050 typedef struct S' MY_TYPE;
2051 struct S' object;
2053 And they can then deduce (from the node for type struct S') that
2054 the original object declaration was:
2056 MY_TYPE object;
2058 Being able to do this is important for proper support of protoize,
2059 and also for generating precise symbolic debugging information
2060 which takes full account of the programmer's (typedef) vocabulary.
2062 Obviously, we don't want to generate a duplicate ..._TYPE node if
2063 the TYPE_DECL node that we are now processing really represents a
2064 standard built-in type.
2066 Since all standard types are effectively declared at line zero
2067 in the source file, we can easily check to see if we are working
2068 on a standard type by checking the current value of lineno. */
2070 static void
2071 clone_underlying_type (tree x)
2073 if (DECL_IS_BUILTIN (x))
2075 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2076 TYPE_NAME (TREE_TYPE (x)) = x;
2078 else if (TREE_TYPE (x) != error_mark_node
2079 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2081 tree tt = TREE_TYPE (x);
2082 DECL_ORIGINAL_TYPE (x) = tt;
2083 tt = build_variant_type_copy (tt);
2084 TYPE_NAME (tt) = x;
2085 TREE_USED (tt) = TREE_USED (x);
2086 TREE_TYPE (x) = tt;
2090 /* Record a decl-node X as belonging to the current lexical scope.
2091 Check for errors (such as an incompatible declaration for the same
2092 name already seen in the same scope).
2094 Returns either X or an old decl for the same name.
2095 If an old decl is returned, it may have been smashed
2096 to agree with what X says. */
2098 tree
2099 pushdecl (tree x)
2101 tree name = DECL_NAME (x);
2102 struct c_scope *scope = current_scope;
2103 struct c_binding *b;
2104 bool nested = false;
2106 /* Functions need the lang_decl data. */
2107 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
2108 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2110 /* Must set DECL_CONTEXT for everything not at file scope or
2111 DECL_FILE_SCOPE_P won't work. Local externs don't count
2112 unless they have initializers (which generate code). */
2113 if (current_function_decl
2114 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2115 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2116 DECL_CONTEXT (x) = current_function_decl;
2118 /* If this is of variably modified type, prevent jumping into its
2119 scope. */
2120 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2121 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2122 c_begin_vm_scope (scope->depth);
2124 /* Anonymous decls are just inserted in the scope. */
2125 if (!name)
2127 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2128 return x;
2131 /* First, see if there is another declaration with the same name in
2132 the current scope. If there is, duplicate_decls may do all the
2133 work for us. If duplicate_decls returns false, that indicates
2134 two incompatible decls in the same scope; we are to silently
2135 replace the old one (duplicate_decls has issued all appropriate
2136 diagnostics). In particular, we should not consider possible
2137 duplicates in the external scope, or shadowing. */
2138 b = I_SYMBOL_BINDING (name);
2139 if (b && B_IN_SCOPE (b, scope))
2141 struct c_binding *b_ext, *b_use;
2142 tree type = TREE_TYPE (x);
2143 tree visdecl = b->decl;
2144 tree vistype = TREE_TYPE (visdecl);
2145 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2146 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2147 b->inner_comp = false;
2148 b_use = b;
2149 b_ext = b;
2150 /* If this is an external linkage declaration, we should check
2151 for compatibility with the type in the external scope before
2152 setting the type at this scope based on the visible
2153 information only. */
2154 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2156 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2157 b_ext = b_ext->shadowed;
2158 if (b_ext)
2160 b_use = b_ext;
2161 if (b_use->type)
2162 TREE_TYPE (b_use->decl) = b_use->type;
2165 if (duplicate_decls (x, b_use->decl))
2167 if (b_use != b)
2169 /* Save the updated type in the external scope and
2170 restore the proper type for this scope. */
2171 tree thistype;
2172 if (comptypes (vistype, type))
2173 thistype = composite_type (vistype, type);
2174 else
2175 thistype = TREE_TYPE (b_use->decl);
2176 b_use->type = TREE_TYPE (b_use->decl);
2177 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2178 && DECL_BUILT_IN (b_use->decl))
2179 thistype
2180 = build_type_attribute_variant (thistype,
2181 TYPE_ATTRIBUTES
2182 (b_use->type));
2183 TREE_TYPE (b_use->decl) = thistype;
2185 return b_use->decl;
2187 else
2188 goto skip_external_and_shadow_checks;
2191 /* All declarations with external linkage, and all external
2192 references, go in the external scope, no matter what scope is
2193 current. However, the binding in that scope is ignored for
2194 purposes of normal name lookup. A separate binding structure is
2195 created in the requested scope; this governs the normal
2196 visibility of the symbol.
2198 The binding in the externals scope is used exclusively for
2199 detecting duplicate declarations of the same object, no matter
2200 what scope they are in; this is what we do here. (C99 6.2.7p2:
2201 All declarations that refer to the same object or function shall
2202 have compatible type; otherwise, the behavior is undefined.) */
2203 if (DECL_EXTERNAL (x) || scope == file_scope)
2205 tree type = TREE_TYPE (x);
2206 tree vistype = 0;
2207 tree visdecl = 0;
2208 bool type_saved = false;
2209 if (b && !B_IN_EXTERNAL_SCOPE (b)
2210 && (TREE_CODE (b->decl) == FUNCTION_DECL
2211 || TREE_CODE (b->decl) == VAR_DECL)
2212 && DECL_FILE_SCOPE_P (b->decl))
2214 visdecl = b->decl;
2215 vistype = TREE_TYPE (visdecl);
2217 if (scope != file_scope
2218 && !DECL_IN_SYSTEM_HEADER (x))
2219 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2221 while (b && !B_IN_EXTERNAL_SCOPE (b))
2223 /* If this decl might be modified, save its type. This is
2224 done here rather than when the decl is first bound
2225 because the type may change after first binding, through
2226 being completed or through attributes being added. If we
2227 encounter multiple such decls, only the first should have
2228 its type saved; the others will already have had their
2229 proper types saved and the types will not have changed as
2230 their scopes will not have been re-entered. */
2231 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2233 b->type = TREE_TYPE (b->decl);
2234 type_saved = true;
2236 if (B_IN_FILE_SCOPE (b)
2237 && TREE_CODE (b->decl) == VAR_DECL
2238 && TREE_STATIC (b->decl)
2239 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2240 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2241 && TREE_CODE (type) == ARRAY_TYPE
2242 && TYPE_DOMAIN (type)
2243 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2244 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2246 /* Array type completed in inner scope, which should be
2247 diagnosed if the completion does not have size 1 and
2248 it does not get completed in the file scope. */
2249 b->inner_comp = true;
2251 b = b->shadowed;
2254 /* If a matching external declaration has been found, set its
2255 type to the composite of all the types of that declaration.
2256 After the consistency checks, it will be reset to the
2257 composite of the visible types only. */
2258 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2259 && b->type)
2260 TREE_TYPE (b->decl) = b->type;
2262 /* The point of the same_translation_unit_p check here is,
2263 we want to detect a duplicate decl for a construct like
2264 foo() { extern bar(); } ... static bar(); but not if
2265 they are in different translation units. In any case,
2266 the static does not go in the externals scope. */
2267 if (b
2268 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2269 && duplicate_decls (x, b->decl))
2271 tree thistype;
2272 if (vistype)
2274 if (comptypes (vistype, type))
2275 thistype = composite_type (vistype, type);
2276 else
2277 thistype = TREE_TYPE (b->decl);
2279 else
2280 thistype = type;
2281 b->type = TREE_TYPE (b->decl);
2282 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2283 thistype
2284 = build_type_attribute_variant (thistype,
2285 TYPE_ATTRIBUTES (b->type));
2286 TREE_TYPE (b->decl) = thistype;
2287 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2288 return b->decl;
2290 else if (TREE_PUBLIC (x))
2292 if (visdecl && !b && duplicate_decls (x, visdecl))
2294 /* An external declaration at block scope referring to a
2295 visible entity with internal linkage. The composite
2296 type will already be correct for this scope, so we
2297 just need to fall through to make the declaration in
2298 this scope. */
2299 nested = true;
2300 x = visdecl;
2302 else
2304 bind (name, x, external_scope, /*invisible=*/true,
2305 /*nested=*/false);
2306 nested = true;
2311 if (TREE_CODE (x) != PARM_DECL)
2312 warn_if_shadowing (x);
2314 skip_external_and_shadow_checks:
2315 if (TREE_CODE (x) == TYPE_DECL)
2316 clone_underlying_type (x);
2318 bind (name, x, scope, /*invisible=*/false, nested);
2320 /* If x's type is incomplete because it's based on a
2321 structure or union which has not yet been fully declared,
2322 attach it to that structure or union type, so we can go
2323 back and complete the variable declaration later, if the
2324 structure or union gets fully declared.
2326 If the input is erroneous, we can have error_mark in the type
2327 slot (e.g. "f(void a, ...)") - that doesn't count as an
2328 incomplete type. */
2329 if (TREE_TYPE (x) != error_mark_node
2330 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2332 tree element = TREE_TYPE (x);
2334 while (TREE_CODE (element) == ARRAY_TYPE)
2335 element = TREE_TYPE (element);
2336 element = TYPE_MAIN_VARIANT (element);
2338 if ((TREE_CODE (element) == RECORD_TYPE
2339 || TREE_CODE (element) == UNION_TYPE)
2340 && (TREE_CODE (x) != TYPE_DECL
2341 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2342 && !COMPLETE_TYPE_P (element))
2343 C_TYPE_INCOMPLETE_VARS (element)
2344 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2346 return x;
2349 /* Record X as belonging to file scope.
2350 This is used only internally by the Objective-C front end,
2351 and is limited to its needs. duplicate_decls is not called;
2352 if there is any preexisting decl for this identifier, it is an ICE. */
2354 tree
2355 pushdecl_top_level (tree x)
2357 tree name;
2358 bool nested = false;
2359 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2361 name = DECL_NAME (x);
2363 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2365 if (TREE_PUBLIC (x))
2367 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2368 nested = true;
2370 if (file_scope)
2371 bind (name, x, file_scope, /*invisible=*/false, nested);
2373 return x;
2376 static void
2377 implicit_decl_warning (tree id, tree olddecl)
2379 void (*diag) (const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2);
2380 switch (mesg_implicit_function_declaration)
2382 case 0: return;
2383 case 1: diag = warning0; break;
2384 case 2: diag = error; break;
2385 default: gcc_unreachable ();
2388 diag (G_("implicit declaration of function %qE"), id);
2389 if (olddecl)
2390 locate_old_decl (olddecl, diag);
2393 /* Generate an implicit declaration for identifier FUNCTIONID as a
2394 function of type int (). */
2396 tree
2397 implicitly_declare (tree functionid)
2399 struct c_binding *b;
2400 tree decl = 0;
2401 tree asmspec_tree;
2403 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2405 if (B_IN_SCOPE (b, external_scope))
2407 decl = b->decl;
2408 break;
2412 if (decl)
2414 if (decl == error_mark_node)
2415 return decl;
2417 /* FIXME: Objective-C has weird not-really-builtin functions
2418 which are supposed to be visible automatically. They wind up
2419 in the external scope because they're pushed before the file
2420 scope gets created. Catch this here and rebind them into the
2421 file scope. */
2422 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2424 bind (functionid, decl, file_scope,
2425 /*invisible=*/false, /*nested=*/true);
2426 return decl;
2428 else
2430 tree newtype = default_function_type;
2431 if (b->type)
2432 TREE_TYPE (decl) = b->type;
2433 /* Implicit declaration of a function already declared
2434 (somehow) in a different scope, or as a built-in.
2435 If this is the first time this has happened, warn;
2436 then recycle the old declaration but with the new type. */
2437 if (!C_DECL_IMPLICIT (decl))
2439 implicit_decl_warning (functionid, decl);
2440 C_DECL_IMPLICIT (decl) = 1;
2442 if (DECL_BUILT_IN (decl))
2444 newtype = build_type_attribute_variant (newtype,
2445 TYPE_ATTRIBUTES
2446 (TREE_TYPE (decl)));
2447 if (!comptypes (newtype, TREE_TYPE (decl)))
2449 warning (0, "incompatible implicit declaration of built-in"
2450 " function %qD", decl);
2451 newtype = TREE_TYPE (decl);
2454 else
2456 if (!comptypes (newtype, TREE_TYPE (decl)))
2458 error ("incompatible implicit declaration of function %qD",
2459 decl);
2460 locate_old_decl (decl, error);
2463 b->type = TREE_TYPE (decl);
2464 TREE_TYPE (decl) = newtype;
2465 bind (functionid, decl, current_scope,
2466 /*invisible=*/false, /*nested=*/true);
2467 return decl;
2471 /* Not seen before. */
2472 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2473 DECL_EXTERNAL (decl) = 1;
2474 TREE_PUBLIC (decl) = 1;
2475 C_DECL_IMPLICIT (decl) = 1;
2476 implicit_decl_warning (functionid, 0);
2477 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2478 if (asmspec_tree)
2479 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2481 /* C89 says implicit declarations are in the innermost block.
2482 So we record the decl in the standard fashion. */
2483 decl = pushdecl (decl);
2485 /* No need to call objc_check_decl here - it's a function type. */
2486 rest_of_decl_compilation (decl, 0, 0);
2488 /* Write a record describing this implicit function declaration
2489 to the prototypes file (if requested). */
2490 gen_aux_info_record (decl, 0, 1, 0);
2492 /* Possibly apply some default attributes to this implicit declaration. */
2493 decl_attributes (&decl, NULL_TREE, 0);
2495 return decl;
2498 /* Issue an error message for a reference to an undeclared variable
2499 ID, including a reference to a builtin outside of function-call
2500 context. Establish a binding of the identifier to error_mark_node
2501 in an appropriate scope, which will suppress further errors for the
2502 same identifier. The error message should be given location LOC. */
2503 void
2504 undeclared_variable (tree id, location_t loc)
2506 static bool already = false;
2507 struct c_scope *scope;
2509 if (current_function_decl == 0)
2511 error ("%H%qE undeclared here (not in a function)", &loc, id);
2512 scope = current_scope;
2514 else
2516 error ("%H%qE undeclared (first use in this function)", &loc, id);
2518 if (!already)
2520 error ("%H(Each undeclared identifier is reported only once", &loc);
2521 error ("%Hfor each function it appears in.)", &loc);
2522 already = true;
2525 /* If we are parsing old-style parameter decls, current_function_decl
2526 will be nonnull but current_function_scope will be null. */
2527 scope = current_function_scope ? current_function_scope : current_scope;
2529 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2532 /* Subroutine of lookup_label, declare_label, define_label: construct a
2533 LABEL_DECL with all the proper frills. */
2535 static tree
2536 make_label (tree name, location_t location)
2538 tree label = build_decl (LABEL_DECL, name, void_type_node);
2540 DECL_CONTEXT (label) = current_function_decl;
2541 DECL_MODE (label) = VOIDmode;
2542 DECL_SOURCE_LOCATION (label) = location;
2544 return label;
2547 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2548 Create one if none exists so far for the current function.
2549 This is called when a label is used in a goto expression or
2550 has its address taken. */
2552 tree
2553 lookup_label (tree name)
2555 tree label;
2557 if (current_function_decl == 0)
2559 error ("label %qE referenced outside of any function", name);
2560 return 0;
2563 /* Use a label already defined or ref'd with this name, but not if
2564 it is inherited from a containing function and wasn't declared
2565 using __label__. */
2566 label = I_LABEL_DECL (name);
2567 if (label && (DECL_CONTEXT (label) == current_function_decl
2568 || C_DECLARED_LABEL_FLAG (label)))
2570 /* If the label has only been declared, update its apparent
2571 location to point here, for better diagnostics if it
2572 turns out not to have been defined. */
2573 if (!TREE_USED (label))
2574 DECL_SOURCE_LOCATION (label) = input_location;
2575 return label;
2578 /* No label binding for that identifier; make one. */
2579 label = make_label (name, input_location);
2581 /* Ordinary labels go in the current function scope. */
2582 bind (name, label, current_function_scope,
2583 /*invisible=*/false, /*nested=*/false);
2584 return label;
2587 /* Make a label named NAME in the current function, shadowing silently
2588 any that may be inherited from containing functions or containing
2589 scopes. This is called for __label__ declarations. */
2591 tree
2592 declare_label (tree name)
2594 struct c_binding *b = I_LABEL_BINDING (name);
2595 tree label;
2597 /* Check to make sure that the label hasn't already been declared
2598 at this scope */
2599 if (b && B_IN_CURRENT_SCOPE (b))
2601 error ("duplicate label declaration %qE", name);
2602 locate_old_decl (b->decl, error);
2604 /* Just use the previous declaration. */
2605 return b->decl;
2608 label = make_label (name, input_location);
2609 C_DECLARED_LABEL_FLAG (label) = 1;
2611 /* Declared labels go in the current scope. */
2612 bind (name, label, current_scope,
2613 /*invisible=*/false, /*nested=*/false);
2614 return label;
2617 /* Define a label, specifying the location in the source file.
2618 Return the LABEL_DECL node for the label, if the definition is valid.
2619 Otherwise return 0. */
2621 tree
2622 define_label (location_t location, tree name)
2624 /* Find any preexisting label with this name. It is an error
2625 if that label has already been defined in this function, or
2626 if there is a containing function with a declared label with
2627 the same name. */
2628 tree label = I_LABEL_DECL (name);
2629 struct c_label_list *nlist_se, *nlist_vm;
2631 if (label
2632 && ((DECL_CONTEXT (label) == current_function_decl
2633 && DECL_INITIAL (label) != 0)
2634 || (DECL_CONTEXT (label) != current_function_decl
2635 && C_DECLARED_LABEL_FLAG (label))))
2637 error ("%Hduplicate label %qD", &location, label);
2638 locate_old_decl (label, error);
2639 return 0;
2641 else if (label && DECL_CONTEXT (label) == current_function_decl)
2643 /* The label has been used or declared already in this function,
2644 but not defined. Update its location to point to this
2645 definition. */
2646 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2647 error ("%Jjump into statement expression", label);
2648 if (C_DECL_UNDEFINABLE_VM (label))
2649 error ("%Jjump into scope of identifier with variably modified type",
2650 label);
2651 DECL_SOURCE_LOCATION (label) = location;
2653 else
2655 /* No label binding for that identifier; make one. */
2656 label = make_label (name, location);
2658 /* Ordinary labels go in the current function scope. */
2659 bind (name, label, current_function_scope,
2660 /*invisible=*/false, /*nested=*/false);
2663 if (!in_system_header && lookup_name (name))
2664 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2665 "for labels, identifier %qE conflicts", &location, name);
2667 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2668 nlist_se->next = label_context_stack_se->labels_def;
2669 nlist_se->label = label;
2670 label_context_stack_se->labels_def = nlist_se;
2672 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2673 nlist_vm->next = label_context_stack_vm->labels_def;
2674 nlist_vm->label = label;
2675 label_context_stack_vm->labels_def = nlist_vm;
2677 /* Mark label as having been defined. */
2678 DECL_INITIAL (label) = error_mark_node;
2679 return label;
2682 /* Given NAME, an IDENTIFIER_NODE,
2683 return the structure (or union or enum) definition for that name.
2684 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2685 CODE says which kind of type the caller wants;
2686 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2687 If the wrong kind of type is found, an error is reported. */
2689 static tree
2690 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2692 struct c_binding *b = I_TAG_BINDING (name);
2693 int thislevel = 0;
2695 if (!b || !b->decl)
2696 return 0;
2698 /* We only care about whether it's in this level if
2699 thislevel_only was set or it might be a type clash. */
2700 if (thislevel_only || TREE_CODE (b->decl) != code)
2702 /* For our purposes, a tag in the external scope is the same as
2703 a tag in the file scope. (Primarily relevant to Objective-C
2704 and its builtin structure tags, which get pushed before the
2705 file scope is created.) */
2706 if (B_IN_CURRENT_SCOPE (b)
2707 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2708 thislevel = 1;
2711 if (thislevel_only && !thislevel)
2712 return 0;
2714 if (TREE_CODE (b->decl) != code)
2716 /* Definition isn't the kind we were looking for. */
2717 pending_invalid_xref = name;
2718 pending_invalid_xref_location = input_location;
2720 /* If in the same binding level as a declaration as a tag
2721 of a different type, this must not be allowed to
2722 shadow that tag, so give the error immediately.
2723 (For example, "struct foo; union foo;" is invalid.) */
2724 if (thislevel)
2725 pending_xref_error ();
2727 return b->decl;
2730 /* Print an error message now
2731 for a recent invalid struct, union or enum cross reference.
2732 We don't print them immediately because they are not invalid
2733 when used in the `struct foo;' construct for shadowing. */
2735 void
2736 pending_xref_error (void)
2738 if (pending_invalid_xref != 0)
2739 error ("%H%qE defined as wrong kind of tag",
2740 &pending_invalid_xref_location, pending_invalid_xref);
2741 pending_invalid_xref = 0;
2745 /* Look up NAME in the current scope and its superiors
2746 in the namespace of variables, functions and typedefs.
2747 Return a ..._DECL node of some kind representing its definition,
2748 or return 0 if it is undefined. */
2750 tree
2751 lookup_name (tree name)
2753 struct c_binding *b = I_SYMBOL_BINDING (name);
2754 if (b && !b->invisible)
2755 return b->decl;
2756 return 0;
2759 /* Similar to `lookup_name' but look only at the indicated scope. */
2761 static tree
2762 lookup_name_in_scope (tree name, struct c_scope *scope)
2764 struct c_binding *b;
2766 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2767 if (B_IN_SCOPE (b, scope))
2768 return b->decl;
2769 return 0;
2772 /* Create the predefined scalar types of C,
2773 and some nodes representing standard constants (0, 1, (void *) 0).
2774 Initialize the global scope.
2775 Make definitions for built-in primitive functions. */
2777 void
2778 c_init_decl_processing (void)
2780 location_t save_loc = input_location;
2782 /* Initialize reserved words for parser. */
2783 c_parse_init ();
2785 current_function_decl = 0;
2787 gcc_obstack_init (&parser_obstack);
2789 /* Make the externals scope. */
2790 push_scope ();
2791 external_scope = current_scope;
2793 /* Declarations from c_common_nodes_and_builtins must not be associated
2794 with this input file, lest we get differences between using and not
2795 using preprocessed headers. */
2796 #ifdef USE_MAPPED_LOCATION
2797 input_location = BUILTINS_LOCATION;
2798 #else
2799 input_location.file = "<built-in>";
2800 input_location.line = 0;
2801 #endif
2803 build_common_tree_nodes (flag_signed_char, false);
2805 c_common_nodes_and_builtins ();
2807 /* In C, comparisons and TRUTH_* expressions have type int. */
2808 truthvalue_type_node = integer_type_node;
2809 truthvalue_true_node = integer_one_node;
2810 truthvalue_false_node = integer_zero_node;
2812 /* Even in C99, which has a real boolean type. */
2813 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2814 boolean_type_node));
2816 input_location = save_loc;
2818 pedantic_lvalues = true;
2820 make_fname_decl = c_make_fname_decl;
2821 start_fname_decls ();
2824 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2825 decl, NAME is the initialization string and TYPE_DEP indicates whether
2826 NAME depended on the type of the function. As we don't yet implement
2827 delayed emission of static data, we mark the decl as emitted
2828 so it is not placed in the output. Anything using it must therefore pull
2829 out the STRING_CST initializer directly. FIXME. */
2831 static tree
2832 c_make_fname_decl (tree id, int type_dep)
2834 const char *name = fname_as_string (type_dep);
2835 tree decl, type, init;
2836 size_t length = strlen (name);
2838 type = build_array_type (char_type_node,
2839 build_index_type (size_int (length)));
2840 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2842 decl = build_decl (VAR_DECL, id, type);
2844 TREE_STATIC (decl) = 1;
2845 TREE_READONLY (decl) = 1;
2846 DECL_ARTIFICIAL (decl) = 1;
2848 init = build_string (length + 1, name);
2849 free ((char *) name);
2850 TREE_TYPE (init) = type;
2851 DECL_INITIAL (decl) = init;
2853 TREE_USED (decl) = 1;
2855 if (current_function_decl
2856 /* For invalid programs like this:
2858 void foo()
2859 const char* p = __FUNCTION__;
2861 the __FUNCTION__ is believed to appear in K&R style function
2862 parameter declarator. In that case we still don't have
2863 function_scope. */
2864 && (!errorcount || current_function_scope))
2866 DECL_CONTEXT (decl) = current_function_decl;
2867 bind (id, decl, current_function_scope,
2868 /*invisible=*/false, /*nested=*/false);
2871 finish_decl (decl, init, NULL_TREE);
2873 return decl;
2876 tree
2877 c_builtin_function (tree decl)
2879 tree type = TREE_TYPE (decl);
2880 tree id = DECL_NAME (decl);
2882 const char *name = IDENTIFIER_POINTER (id);
2883 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2884 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2886 /* Should never be called on a symbol with a preexisting meaning. */
2887 gcc_assert (!I_SYMBOL_BINDING (id));
2889 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2891 /* Builtins in the implementation namespace are made visible without
2892 needing to be explicitly declared. See push_file_scope. */
2893 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2895 TREE_CHAIN (decl) = visible_builtins;
2896 visible_builtins = decl;
2899 return decl;
2902 /* Called when a declaration is seen that contains no names to declare.
2903 If its type is a reference to a structure, union or enum inherited
2904 from a containing scope, shadow that tag name for the current scope
2905 with a forward reference.
2906 If its type defines a new named structure or union
2907 or defines an enum, it is valid but we need not do anything here.
2908 Otherwise, it is an error. */
2910 void
2911 shadow_tag (const struct c_declspecs *declspecs)
2913 shadow_tag_warned (declspecs, 0);
2916 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2917 but no pedwarn. */
2918 void
2919 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2921 bool found_tag = false;
2923 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2925 tree value = declspecs->type;
2926 enum tree_code code = TREE_CODE (value);
2928 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2929 /* Used to test also that TYPE_SIZE (value) != 0.
2930 That caused warning for `struct foo;' at top level in the file. */
2932 tree name = TYPE_NAME (value);
2933 tree t;
2935 found_tag = true;
2937 if (name == 0)
2939 if (warned != 1 && code != ENUMERAL_TYPE)
2940 /* Empty unnamed enum OK */
2942 pedwarn ("unnamed struct/union that defines no instances");
2943 warned = 1;
2946 else if (!declspecs->tag_defined_p
2947 && declspecs->storage_class != csc_none)
2949 if (warned != 1)
2950 pedwarn ("empty declaration with storage class specifier "
2951 "does not redeclare tag");
2952 warned = 1;
2953 pending_xref_error ();
2955 else if (!declspecs->tag_defined_p
2956 && (declspecs->const_p
2957 || declspecs->volatile_p
2958 || declspecs->restrict_p))
2960 if (warned != 1)
2961 pedwarn ("empty declaration with type qualifier "
2962 "does not redeclare tag");
2963 warned = 1;
2964 pending_xref_error ();
2966 else
2968 pending_invalid_xref = 0;
2969 t = lookup_tag (code, name, 1);
2971 if (t == 0)
2973 t = make_node (code);
2974 pushtag (name, t);
2978 else
2980 if (warned != 1 && !in_system_header)
2982 pedwarn ("useless type name in empty declaration");
2983 warned = 1;
2987 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2989 pedwarn ("useless type name in empty declaration");
2990 warned = 1;
2993 pending_invalid_xref = 0;
2995 if (declspecs->inline_p)
2997 error ("%<inline%> in empty declaration");
2998 warned = 1;
3001 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3003 error ("%<auto%> in file-scope empty declaration");
3004 warned = 1;
3007 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3009 error ("%<register%> in file-scope empty declaration");
3010 warned = 1;
3013 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3015 warning (0, "useless storage class specifier in empty declaration");
3016 warned = 2;
3019 if (!warned && !in_system_header && declspecs->thread_p)
3021 warning (0, "useless %<__thread%> in empty declaration");
3022 warned = 2;
3025 if (!warned && !in_system_header && (declspecs->const_p
3026 || declspecs->volatile_p
3027 || declspecs->restrict_p))
3029 warning (0, "useless type qualifier in empty declaration");
3030 warned = 2;
3033 if (warned != 1)
3035 if (!found_tag)
3036 pedwarn ("empty declaration");
3041 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3042 bits. SPECS represents declaration specifiers that the grammar
3043 only permits to contain type qualifiers and attributes. */
3046 quals_from_declspecs (const struct c_declspecs *specs)
3048 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3049 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3050 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3051 gcc_assert (!specs->type
3052 && !specs->decl_attr
3053 && specs->typespec_word == cts_none
3054 && specs->storage_class == csc_none
3055 && !specs->typedef_p
3056 && !specs->explicit_signed_p
3057 && !specs->deprecated_p
3058 && !specs->long_p
3059 && !specs->long_long_p
3060 && !specs->short_p
3061 && !specs->signed_p
3062 && !specs->unsigned_p
3063 && !specs->complex_p
3064 && !specs->inline_p
3065 && !specs->thread_p);
3066 return quals;
3069 /* Construct an array declarator. EXPR is the expression inside [],
3070 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3071 applied to the pointer to which a parameter array is converted).
3072 STATIC_P is true if "static" is inside the [], false otherwise.
3073 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3074 length which is nevertheless a complete type, false otherwise. The
3075 field for the contained declarator is left to be filled in by
3076 set_array_declarator_inner. */
3078 struct c_declarator *
3079 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3080 bool vla_unspec_p)
3082 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3083 struct c_declarator);
3084 declarator->kind = cdk_array;
3085 declarator->declarator = 0;
3086 declarator->u.array.dimen = expr;
3087 if (quals)
3089 declarator->u.array.attrs = quals->attrs;
3090 declarator->u.array.quals = quals_from_declspecs (quals);
3092 else
3094 declarator->u.array.attrs = NULL_TREE;
3095 declarator->u.array.quals = 0;
3097 declarator->u.array.static_p = static_p;
3098 declarator->u.array.vla_unspec_p = vla_unspec_p;
3099 if (pedantic && !flag_isoc99)
3101 if (static_p || quals != NULL)
3102 pedwarn ("ISO C90 does not support %<static%> or type "
3103 "qualifiers in parameter array declarators");
3104 if (vla_unspec_p)
3105 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3107 if (vla_unspec_p)
3109 if (!current_scope->parm_flag)
3111 /* C99 6.7.5.2p4 */
3112 error ("%<[*]%> not allowed in other than function prototype scope");
3113 declarator->u.array.vla_unspec_p = false;
3114 return NULL;
3116 current_scope->had_vla_unspec = true;
3118 return declarator;
3121 /* Set the contained declarator of an array declarator. DECL is the
3122 declarator, as constructed by build_array_declarator; INNER is what
3123 appears on the left of the []. ABSTRACT_P is true if it is an
3124 abstract declarator, false otherwise; this is used to reject static
3125 and type qualifiers in abstract declarators, where they are not in
3126 the C99 grammar (subject to possible change in DR#289). */
3128 struct c_declarator *
3129 set_array_declarator_inner (struct c_declarator *decl,
3130 struct c_declarator *inner, bool abstract_p)
3132 decl->declarator = inner;
3133 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3134 || decl->u.array.attrs != NULL_TREE
3135 || decl->u.array.static_p))
3136 error ("static or type qualifiers in abstract declarator");
3137 return decl;
3140 /* INIT is a constructor that forms DECL's initializer. If the final
3141 element initializes a flexible array field, add the size of that
3142 initializer to DECL's size. */
3144 static void
3145 add_flexible_array_elts_to_size (tree decl, tree init)
3147 tree elt, type;
3149 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3150 return;
3152 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3153 type = TREE_TYPE (elt);
3154 if (TREE_CODE (type) == ARRAY_TYPE
3155 && TYPE_SIZE (type) == NULL_TREE
3156 && TYPE_DOMAIN (type) != NULL_TREE
3157 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3159 complete_array_type (&type, elt, false);
3160 DECL_SIZE (decl)
3161 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3162 DECL_SIZE_UNIT (decl)
3163 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3167 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3169 tree
3170 groktypename (struct c_type_name *type_name)
3172 tree type;
3173 tree attrs = type_name->specs->attrs;
3175 type_name->specs->attrs = NULL_TREE;
3177 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3178 false, NULL);
3180 /* Apply attributes. */
3181 decl_attributes (&type, attrs, 0);
3183 return type;
3186 /* Decode a declarator in an ordinary declaration or data definition.
3187 This is called as soon as the type information and variable name
3188 have been parsed, before parsing the initializer if any.
3189 Here we create the ..._DECL node, fill in its type,
3190 and put it on the list of decls for the current context.
3191 The ..._DECL node is returned as the value.
3193 Exception: for arrays where the length is not specified,
3194 the type is left null, to be filled in by `finish_decl'.
3196 Function definitions do not come here; they go to start_function
3197 instead. However, external and forward declarations of functions
3198 do go through here. Structure field declarations are done by
3199 grokfield and not through here. */
3201 tree
3202 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3203 bool initialized, tree attributes)
3205 tree decl;
3206 tree tem;
3208 /* An object declared as __attribute__((deprecated)) suppresses
3209 warnings of uses of other deprecated items. */
3210 if (lookup_attribute ("deprecated", attributes))
3211 deprecated_state = DEPRECATED_SUPPRESS;
3213 decl = grokdeclarator (declarator, declspecs,
3214 NORMAL, initialized, NULL);
3215 if (!decl)
3216 return 0;
3218 deprecated_state = DEPRECATED_NORMAL;
3220 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3221 && MAIN_NAME_P (DECL_NAME (decl)))
3222 warning (OPT_Wmain, "%q+D is usually a function", decl);
3224 if (initialized)
3225 /* Is it valid for this decl to have an initializer at all?
3226 If not, set INITIALIZED to zero, which will indirectly
3227 tell 'finish_decl' to ignore the initializer once it is parsed. */
3228 switch (TREE_CODE (decl))
3230 case TYPE_DECL:
3231 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3232 initialized = 0;
3233 break;
3235 case FUNCTION_DECL:
3236 error ("function %qD is initialized like a variable", decl);
3237 initialized = 0;
3238 break;
3240 case PARM_DECL:
3241 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3242 error ("parameter %qD is initialized", decl);
3243 initialized = 0;
3244 break;
3246 default:
3247 /* Don't allow initializations for incomplete types except for
3248 arrays which might be completed by the initialization. */
3250 /* This can happen if the array size is an undefined macro.
3251 We already gave a warning, so we don't need another one. */
3252 if (TREE_TYPE (decl) == error_mark_node)
3253 initialized = 0;
3254 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3256 /* A complete type is ok if size is fixed. */
3258 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3259 || C_DECL_VARIABLE_SIZE (decl))
3261 error ("variable-sized object may not be initialized");
3262 initialized = 0;
3265 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3267 error ("variable %qD has initializer but incomplete type", decl);
3268 initialized = 0;
3270 else if (C_DECL_VARIABLE_SIZE (decl))
3272 /* Although C99 is unclear about whether incomplete arrays
3273 of VLAs themselves count as VLAs, it does not make
3274 sense to permit them to be initialized given that
3275 ordinary VLAs may not be initialized. */
3276 error ("variable-sized object may not be initialized");
3277 initialized = 0;
3281 if (initialized)
3283 if (current_scope == file_scope)
3284 TREE_STATIC (decl) = 1;
3286 /* Tell 'pushdecl' this is an initialized decl
3287 even though we don't yet have the initializer expression.
3288 Also tell 'finish_decl' it may store the real initializer. */
3289 DECL_INITIAL (decl) = error_mark_node;
3292 /* If this is a function declaration, write a record describing it to the
3293 prototypes file (if requested). */
3295 if (TREE_CODE (decl) == FUNCTION_DECL)
3296 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3298 /* ANSI specifies that a tentative definition which is not merged with
3299 a non-tentative definition behaves exactly like a definition with an
3300 initializer equal to zero. (Section 3.7.2)
3302 -fno-common gives strict ANSI behavior, though this tends to break
3303 a large body of code that grew up without this rule.
3305 Thread-local variables are never common, since there's no entrenched
3306 body of code to break, and it allows more efficient variable references
3307 in the presence of dynamic linking. */
3309 if (TREE_CODE (decl) == VAR_DECL
3310 && !initialized
3311 && TREE_PUBLIC (decl)
3312 && !DECL_THREAD_LOCAL_P (decl)
3313 && !flag_no_common)
3314 DECL_COMMON (decl) = 1;
3316 /* Set attributes here so if duplicate decl, will have proper attributes. */
3317 decl_attributes (&decl, attributes, 0);
3319 #if WANT_C99_INLINE_SEMANTICS
3320 /* Handle gnu_inline attribute. */
3321 if (declspecs->inline_p
3322 && flag_isoc99
3323 && TREE_CODE (decl) == FUNCTION_DECL
3324 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
3326 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3328 else if (declspecs->storage_class != csc_static)
3329 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3331 #endif /* WANT_C99_INLINE_SEMANTICS */
3333 if (TREE_CODE (decl) == FUNCTION_DECL
3334 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3336 struct c_declarator *ce = declarator;
3338 if (ce->kind == cdk_pointer)
3339 ce = declarator->declarator;
3340 if (ce->kind == cdk_function)
3342 tree args = ce->u.arg_info->parms;
3343 for (; args; args = TREE_CHAIN (args))
3345 tree type = TREE_TYPE (args);
3346 if (type && INTEGRAL_TYPE_P (type)
3347 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3348 DECL_ARG_TYPE (args) = integer_type_node;
3353 if (TREE_CODE (decl) == FUNCTION_DECL
3354 && DECL_DECLARED_INLINE_P (decl)
3355 && DECL_UNINLINABLE (decl)
3356 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3357 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3358 decl);
3360 /* C99 6.7.4p3: An inline definition of a function with external
3361 linkage shall not contain a definition of a modifiable object
3362 with static storage duration... */
3363 if (TREE_CODE (decl) == VAR_DECL
3364 && current_scope != file_scope
3365 && TREE_STATIC (decl)
3366 && DECL_DECLARED_INLINE_P (current_function_decl)
3367 && DECL_EXTERNAL (current_function_decl))
3368 pedwarn ("%q+D is static but declared in inline function %qD "
3369 "which is not static", decl, current_function_decl);
3371 /* Add this decl to the current scope.
3372 TEM may equal DECL or it may be a previous decl of the same name. */
3373 tem = pushdecl (decl);
3375 if (initialized && DECL_EXTERNAL (tem))
3377 DECL_EXTERNAL (tem) = 0;
3378 TREE_STATIC (tem) = 1;
3381 return tem;
3384 /* Initialize EH if not initialized yet and exceptions are enabled. */
3386 void
3387 c_maybe_initialize_eh (void)
3389 if (!flag_exceptions || c_eh_initialized_p)
3390 return;
3392 c_eh_initialized_p = true;
3393 eh_personality_libfunc
3394 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3395 ? "__gcc_personality_sj0"
3396 : "__gcc_personality_v0");
3397 default_init_unwind_resume_libfunc ();
3398 using_eh_for_cleanups ();
3401 /* Finish processing of a declaration;
3402 install its initial value.
3403 If the length of an array type is not known before,
3404 it must be determined now, from the initial value, or it is an error. */
3406 void
3407 finish_decl (tree decl, tree init, tree asmspec_tree)
3409 tree type = TREE_TYPE (decl);
3410 int was_incomplete = (DECL_SIZE (decl) == 0);
3411 const char *asmspec = 0;
3413 /* If a name was specified, get the string. */
3414 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3415 && DECL_FILE_SCOPE_P (decl))
3416 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3417 if (asmspec_tree)
3418 asmspec = TREE_STRING_POINTER (asmspec_tree);
3420 /* If `start_decl' didn't like having an initialization, ignore it now. */
3421 if (init != 0 && DECL_INITIAL (decl) == 0)
3422 init = 0;
3424 /* Don't crash if parm is initialized. */
3425 if (TREE_CODE (decl) == PARM_DECL)
3426 init = 0;
3428 if (init)
3429 store_init_value (decl, init);
3431 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3432 || TREE_CODE (decl) == FUNCTION_DECL
3433 || TREE_CODE (decl) == FIELD_DECL))
3434 objc_check_decl (decl);
3436 /* Deduce size of array from initialization, if not already known. */
3437 if (TREE_CODE (type) == ARRAY_TYPE
3438 && TYPE_DOMAIN (type) == 0
3439 && TREE_CODE (decl) != TYPE_DECL)
3441 bool do_default
3442 = (TREE_STATIC (decl)
3443 /* Even if pedantic, an external linkage array
3444 may have incomplete type at first. */
3445 ? pedantic && !TREE_PUBLIC (decl)
3446 : !DECL_EXTERNAL (decl));
3447 int failure
3448 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3449 do_default);
3451 /* Get the completed type made by complete_array_type. */
3452 type = TREE_TYPE (decl);
3454 switch (failure)
3456 case 1:
3457 error ("initializer fails to determine size of %q+D", decl);
3458 break;
3460 case 2:
3461 if (do_default)
3462 error ("array size missing in %q+D", decl);
3463 /* If a `static' var's size isn't known,
3464 make it extern as well as static, so it does not get
3465 allocated.
3466 If it is not `static', then do not mark extern;
3467 finish_incomplete_decl will give it a default size
3468 and it will get allocated. */
3469 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3470 DECL_EXTERNAL (decl) = 1;
3471 break;
3473 case 3:
3474 error ("zero or negative size array %q+D", decl);
3475 break;
3477 case 0:
3478 /* For global variables, update the copy of the type that
3479 exists in the binding. */
3480 if (TREE_PUBLIC (decl))
3482 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3483 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3484 b_ext = b_ext->shadowed;
3485 if (b_ext)
3487 if (b_ext->type)
3488 b_ext->type = composite_type (b_ext->type, type);
3489 else
3490 b_ext->type = type;
3493 break;
3495 default:
3496 gcc_unreachable ();
3499 if (DECL_INITIAL (decl))
3500 TREE_TYPE (DECL_INITIAL (decl)) = type;
3502 layout_decl (decl, 0);
3505 if (TREE_CODE (decl) == VAR_DECL)
3507 if (init && TREE_CODE (init) == CONSTRUCTOR)
3508 add_flexible_array_elts_to_size (decl, init);
3510 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3511 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3512 layout_decl (decl, 0);
3514 if (DECL_SIZE (decl) == 0
3515 /* Don't give an error if we already gave one earlier. */
3516 && TREE_TYPE (decl) != error_mark_node
3517 && (TREE_STATIC (decl)
3518 /* A static variable with an incomplete type
3519 is an error if it is initialized.
3520 Also if it is not file scope.
3521 Otherwise, let it through, but if it is not `extern'
3522 then it may cause an error message later. */
3523 ? (DECL_INITIAL (decl) != 0
3524 || !DECL_FILE_SCOPE_P (decl))
3525 /* An automatic variable with an incomplete type
3526 is an error. */
3527 : !DECL_EXTERNAL (decl)))
3529 error ("storage size of %q+D isn%'t known", decl);
3530 TREE_TYPE (decl) = error_mark_node;
3533 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3534 && DECL_SIZE (decl) != 0)
3536 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3537 constant_expression_warning (DECL_SIZE (decl));
3538 else
3539 error ("storage size of %q+D isn%'t constant", decl);
3542 if (TREE_USED (type))
3543 TREE_USED (decl) = 1;
3546 /* If this is a function and an assembler name is specified, reset DECL_RTL
3547 so we can give it its new name. Also, update built_in_decls if it
3548 was a normal built-in. */
3549 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3551 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3552 set_builtin_user_assembler_name (decl, asmspec);
3553 set_user_assembler_name (decl, asmspec);
3556 /* If #pragma weak was used, mark the decl weak now. */
3557 maybe_apply_pragma_weak (decl);
3559 /* Output the assembler code and/or RTL code for variables and functions,
3560 unless the type is an undefined structure or union.
3561 If not, it will get done when the type is completed. */
3563 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3565 /* Determine the ELF visibility. */
3566 if (TREE_PUBLIC (decl))
3567 c_determine_visibility (decl);
3569 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3570 if (c_dialect_objc ())
3571 objc_check_decl (decl);
3573 if (asmspec)
3575 /* If this is not a static variable, issue a warning.
3576 It doesn't make any sense to give an ASMSPEC for an
3577 ordinary, non-register local variable. Historically,
3578 GCC has accepted -- but ignored -- the ASMSPEC in
3579 this case. */
3580 if (!DECL_FILE_SCOPE_P (decl)
3581 && TREE_CODE (decl) == VAR_DECL
3582 && !C_DECL_REGISTER (decl)
3583 && !TREE_STATIC (decl))
3584 warning (0, "ignoring asm-specifier for non-static local "
3585 "variable %q+D", decl);
3586 else
3587 set_user_assembler_name (decl, asmspec);
3590 if (DECL_FILE_SCOPE_P (decl))
3592 if (DECL_INITIAL (decl) == NULL_TREE
3593 || DECL_INITIAL (decl) == error_mark_node)
3594 /* Don't output anything
3595 when a tentative file-scope definition is seen.
3596 But at end of compilation, do output code for them. */
3597 DECL_DEFER_OUTPUT (decl) = 1;
3598 rest_of_decl_compilation (decl, true, 0);
3600 else
3602 /* In conjunction with an ASMSPEC, the `register'
3603 keyword indicates that we should place the variable
3604 in a particular register. */
3605 if (asmspec && C_DECL_REGISTER (decl))
3607 DECL_HARD_REGISTER (decl) = 1;
3608 /* This cannot be done for a structure with volatile
3609 fields, on which DECL_REGISTER will have been
3610 reset. */
3611 if (!DECL_REGISTER (decl))
3612 error ("cannot put object with volatile field into register");
3615 if (TREE_CODE (decl) != FUNCTION_DECL)
3617 /* If we're building a variable sized type, and we might be
3618 reachable other than via the top of the current binding
3619 level, then create a new BIND_EXPR so that we deallocate
3620 the object at the right time. */
3621 /* Note that DECL_SIZE can be null due to errors. */
3622 if (DECL_SIZE (decl)
3623 && !TREE_CONSTANT (DECL_SIZE (decl))
3624 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3626 tree bind;
3627 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3628 TREE_SIDE_EFFECTS (bind) = 1;
3629 add_stmt (bind);
3630 BIND_EXPR_BODY (bind) = push_stmt_list ();
3632 add_stmt (build_stmt (DECL_EXPR, decl));
3637 if (!DECL_FILE_SCOPE_P (decl))
3639 /* Recompute the RTL of a local array now
3640 if it used to be an incomplete type. */
3641 if (was_incomplete
3642 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3644 /* If we used it already as memory, it must stay in memory. */
3645 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3646 /* If it's still incomplete now, no init will save it. */
3647 if (DECL_SIZE (decl) == 0)
3648 DECL_INITIAL (decl) = 0;
3653 /* If this was marked 'used', be sure it will be output. */
3654 if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3655 mark_decl_referenced (decl);
3657 if (TREE_CODE (decl) == TYPE_DECL)
3659 if (!DECL_FILE_SCOPE_P (decl)
3660 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3661 add_stmt (build_stmt (DECL_EXPR, decl));
3663 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3666 /* At the end of a declaration, throw away any variable type sizes
3667 of types defined inside that declaration. There is no use
3668 computing them in the following function definition. */
3669 if (current_scope == file_scope)
3670 get_pending_sizes ();
3672 /* Install a cleanup (aka destructor) if one was given. */
3673 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3675 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3676 if (attr)
3678 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3679 tree cleanup_decl = lookup_name (cleanup_id);
3680 tree cleanup;
3682 /* Build "cleanup(&decl)" for the destructor. */
3683 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3684 cleanup = build_tree_list (NULL_TREE, cleanup);
3685 cleanup = build_function_call (cleanup_decl, cleanup);
3687 /* Don't warn about decl unused; the cleanup uses it. */
3688 TREE_USED (decl) = 1;
3689 TREE_USED (cleanup_decl) = 1;
3691 /* Initialize EH, if we've been told to do so. */
3692 c_maybe_initialize_eh ();
3694 push_cleanup (decl, cleanup, false);
3699 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3701 tree
3702 grokparm (const struct c_parm *parm)
3704 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3705 NULL);
3707 decl_attributes (&decl, parm->attrs, 0);
3709 return decl;
3712 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3713 and push that on the current scope. */
3715 void
3716 push_parm_decl (const struct c_parm *parm)
3718 tree decl;
3720 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3721 decl_attributes (&decl, parm->attrs, 0);
3723 decl = pushdecl (decl);
3725 finish_decl (decl, NULL_TREE, NULL_TREE);
3728 /* Mark all the parameter declarations to date as forward decls.
3729 Also diagnose use of this extension. */
3731 void
3732 mark_forward_parm_decls (void)
3734 struct c_binding *b;
3736 if (pedantic && !current_scope->warned_forward_parm_decls)
3738 pedwarn ("ISO C forbids forward parameter declarations");
3739 current_scope->warned_forward_parm_decls = true;
3742 for (b = current_scope->bindings; b; b = b->prev)
3743 if (TREE_CODE (b->decl) == PARM_DECL)
3744 TREE_ASM_WRITTEN (b->decl) = 1;
3747 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3748 literal, which may be an incomplete array type completed by the
3749 initializer; INIT is a CONSTRUCTOR that initializes the compound
3750 literal. */
3752 tree
3753 build_compound_literal (tree type, tree init)
3755 /* We do not use start_decl here because we have a type, not a declarator;
3756 and do not use finish_decl because the decl should be stored inside
3757 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3758 tree decl;
3759 tree complit;
3760 tree stmt;
3762 if (type == error_mark_node)
3763 return error_mark_node;
3765 decl = build_decl (VAR_DECL, NULL_TREE, type);
3766 DECL_EXTERNAL (decl) = 0;
3767 TREE_PUBLIC (decl) = 0;
3768 TREE_STATIC (decl) = (current_scope == file_scope);
3769 DECL_CONTEXT (decl) = current_function_decl;
3770 TREE_USED (decl) = 1;
3771 TREE_TYPE (decl) = type;
3772 TREE_READONLY (decl) = TYPE_READONLY (type);
3773 store_init_value (decl, init);
3775 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3777 int failure = complete_array_type (&TREE_TYPE (decl),
3778 DECL_INITIAL (decl), true);
3779 gcc_assert (!failure);
3781 type = TREE_TYPE (decl);
3782 TREE_TYPE (DECL_INITIAL (decl)) = type;
3785 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3786 return error_mark_node;
3788 stmt = build_stmt (DECL_EXPR, decl);
3789 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3790 TREE_SIDE_EFFECTS (complit) = 1;
3792 layout_decl (decl, 0);
3794 if (TREE_STATIC (decl))
3796 /* This decl needs a name for the assembler output. */
3797 set_compound_literal_name (decl);
3798 DECL_DEFER_OUTPUT (decl) = 1;
3799 DECL_COMDAT (decl) = 1;
3800 DECL_ARTIFICIAL (decl) = 1;
3801 DECL_IGNORED_P (decl) = 1;
3802 pushdecl (decl);
3803 rest_of_decl_compilation (decl, 1, 0);
3806 return complit;
3809 /* Determine whether TYPE is a structure with a flexible array member,
3810 or a union containing such a structure (possibly recursively). */
3812 static bool
3813 flexible_array_type_p (tree type)
3815 tree x;
3816 switch (TREE_CODE (type))
3818 case RECORD_TYPE:
3819 x = TYPE_FIELDS (type);
3820 if (x == NULL_TREE)
3821 return false;
3822 while (TREE_CHAIN (x) != NULL_TREE)
3823 x = TREE_CHAIN (x);
3824 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3825 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3826 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3827 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3828 return true;
3829 return false;
3830 case UNION_TYPE:
3831 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3833 if (flexible_array_type_p (TREE_TYPE (x)))
3834 return true;
3836 return false;
3837 default:
3838 return false;
3842 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3843 replacing with appropriate values if they are invalid. */
3844 static void
3845 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3847 tree type_mv;
3848 unsigned int max_width;
3849 unsigned HOST_WIDE_INT w;
3850 const char *name = orig_name ? orig_name: _("<anonymous>");
3852 /* Detect and ignore out of range field width and process valid
3853 field widths. */
3854 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3855 || TREE_CODE (*width) != INTEGER_CST)
3857 error ("bit-field %qs width not an integer constant", name);
3858 *width = integer_one_node;
3860 else
3862 constant_expression_warning (*width);
3863 if (tree_int_cst_sgn (*width) < 0)
3865 error ("negative width in bit-field %qs", name);
3866 *width = integer_one_node;
3868 else if (integer_zerop (*width) && orig_name)
3870 error ("zero width for bit-field %qs", name);
3871 *width = integer_one_node;
3875 /* Detect invalid bit-field type. */
3876 if (TREE_CODE (*type) != INTEGER_TYPE
3877 && TREE_CODE (*type) != BOOLEAN_TYPE
3878 && TREE_CODE (*type) != ENUMERAL_TYPE)
3880 error ("bit-field %qs has invalid type", name);
3881 *type = unsigned_type_node;
3884 type_mv = TYPE_MAIN_VARIANT (*type);
3885 if (pedantic
3886 && !in_system_header
3887 && type_mv != integer_type_node
3888 && type_mv != unsigned_type_node
3889 && type_mv != boolean_type_node)
3890 pedwarn ("type of bit-field %qs is a GCC extension", name);
3892 if (type_mv == boolean_type_node)
3893 max_width = CHAR_TYPE_SIZE;
3894 else
3895 max_width = TYPE_PRECISION (*type);
3897 if (0 < compare_tree_int (*width, max_width))
3899 error ("width of %qs exceeds its type", name);
3900 w = max_width;
3901 *width = build_int_cst (NULL_TREE, w);
3903 else
3904 w = tree_low_cst (*width, 1);
3906 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3908 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3909 if (!lt
3910 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3911 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3912 warning (0, "%qs is narrower than values of its type", name);
3917 /* Given declspecs and a declarator,
3918 determine the name and type of the object declared
3919 and construct a ..._DECL node for it.
3920 (In one case we can return a ..._TYPE node instead.
3921 For invalid input we sometimes return 0.)
3923 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3925 DECL_CONTEXT says which syntactic context this declaration is in:
3926 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3927 FUNCDEF for a function definition. Like NORMAL but a few different
3928 error messages in each case. Return value may be zero meaning
3929 this definition is too screwy to try to parse.
3930 PARM for a parameter declaration (either within a function prototype
3931 or before a function body). Make a PARM_DECL, or return void_type_node.
3932 TYPENAME if for a typename (in a cast or sizeof).
3933 Don't make a DECL node; just return the ..._TYPE node.
3934 FIELD for a struct or union field; make a FIELD_DECL.
3935 INITIALIZED is true if the decl has an initializer.
3936 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3937 representing the width of the bit-field.
3939 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3940 It may also be so in the PARM case, for a prototype where the
3941 argument type is specified but not the name.
3943 This function is where the complicated C meanings of `static'
3944 and `extern' are interpreted. */
3946 static tree
3947 grokdeclarator (const struct c_declarator *declarator,
3948 struct c_declspecs *declspecs,
3949 enum decl_context decl_context, bool initialized, tree *width)
3951 tree type = declspecs->type;
3952 bool threadp = declspecs->thread_p;
3953 enum c_storage_class storage_class = declspecs->storage_class;
3954 int constp;
3955 int restrictp;
3956 int volatilep;
3957 int type_quals = TYPE_UNQUALIFIED;
3958 const char *name, *orig_name;
3959 tree typedef_type = 0;
3960 bool funcdef_flag = false;
3961 bool funcdef_syntax = false;
3962 int size_varies = 0;
3963 tree decl_attr = declspecs->decl_attr;
3964 int array_ptr_quals = TYPE_UNQUALIFIED;
3965 tree array_ptr_attrs = NULL_TREE;
3966 int array_parm_static = 0;
3967 bool array_parm_vla_unspec_p = false;
3968 tree returned_attrs = NULL_TREE;
3969 bool bitfield = width != NULL;
3970 tree element_type;
3971 struct c_arg_info *arg_info = 0;
3973 if (decl_context == FUNCDEF)
3974 funcdef_flag = true, decl_context = NORMAL;
3976 /* Look inside a declarator for the name being declared
3977 and get it as a string, for an error message. */
3979 const struct c_declarator *decl = declarator;
3980 name = 0;
3982 while (decl)
3983 switch (decl->kind)
3985 case cdk_function:
3986 case cdk_array:
3987 case cdk_pointer:
3988 funcdef_syntax = (decl->kind == cdk_function);
3989 decl = decl->declarator;
3990 break;
3992 case cdk_attrs:
3993 decl = decl->declarator;
3994 break;
3996 case cdk_id:
3997 if (decl->u.id)
3998 name = IDENTIFIER_POINTER (decl->u.id);
3999 decl = 0;
4000 break;
4002 default:
4003 gcc_unreachable ();
4005 orig_name = name;
4006 if (name == 0)
4007 name = "type name";
4010 /* A function definition's declarator must have the form of
4011 a function declarator. */
4013 if (funcdef_flag && !funcdef_syntax)
4014 return 0;
4016 /* If this looks like a function definition, make it one,
4017 even if it occurs where parms are expected.
4018 Then store_parm_decls will reject it and not use it as a parm. */
4019 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4020 decl_context = PARM;
4022 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4023 warn_deprecated_use (declspecs->type);
4025 if ((decl_context == NORMAL || decl_context == FIELD)
4026 && current_scope == file_scope
4027 && variably_modified_type_p (type, NULL_TREE))
4029 error ("variably modified %qs at file scope", name);
4030 type = integer_type_node;
4033 typedef_type = type;
4034 size_varies = C_TYPE_VARIABLE_SIZE (type);
4036 /* Diagnose defaulting to "int". */
4038 if (declspecs->default_int_p && !in_system_header)
4040 /* Issue a warning if this is an ISO C 99 program or if
4041 -Wreturn-type and this is a function, or if -Wimplicit;
4042 prefer the former warning since it is more explicit. */
4043 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4044 && funcdef_flag)
4045 warn_about_return_type = 1;
4046 else if (warn_implicit_int || flag_isoc99)
4047 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
4050 /* Adjust the type if a bit-field is being declared,
4051 -funsigned-bitfields applied and the type is not explicitly
4052 "signed". */
4053 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4054 && TREE_CODE (type) == INTEGER_TYPE)
4055 type = c_common_unsigned_type (type);
4057 /* Figure out the type qualifiers for the declaration. There are
4058 two ways a declaration can become qualified. One is something
4059 like `const int i' where the `const' is explicit. Another is
4060 something like `typedef const int CI; CI i' where the type of the
4061 declaration contains the `const'. A third possibility is that
4062 there is a type qualifier on the element type of a typedefed
4063 array type, in which case we should extract that qualifier so
4064 that c_apply_type_quals_to_decls receives the full list of
4065 qualifiers to work with (C90 is not entirely clear about whether
4066 duplicate qualifiers should be diagnosed in this case, but it
4067 seems most appropriate to do so). */
4068 element_type = strip_array_types (type);
4069 constp = declspecs->const_p + TYPE_READONLY (element_type);
4070 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4071 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4072 if (pedantic && !flag_isoc99)
4074 if (constp > 1)
4075 pedwarn ("duplicate %<const%>");
4076 if (restrictp > 1)
4077 pedwarn ("duplicate %<restrict%>");
4078 if (volatilep > 1)
4079 pedwarn ("duplicate %<volatile%>");
4081 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4082 type = TYPE_MAIN_VARIANT (type);
4083 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4084 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4085 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4087 /* Warn about storage classes that are invalid for certain
4088 kinds of declarations (parameters, typenames, etc.). */
4090 if (funcdef_flag
4091 && (threadp
4092 || storage_class == csc_auto
4093 || storage_class == csc_register
4094 || storage_class == csc_typedef))
4096 if (storage_class == csc_auto
4097 && (pedantic || current_scope == file_scope))
4098 pedwarn ("function definition declared %<auto%>");
4099 if (storage_class == csc_register)
4100 error ("function definition declared %<register%>");
4101 if (storage_class == csc_typedef)
4102 error ("function definition declared %<typedef%>");
4103 if (threadp)
4104 error ("function definition declared %<__thread%>");
4105 threadp = false;
4106 if (storage_class == csc_auto
4107 || storage_class == csc_register
4108 || storage_class == csc_typedef)
4109 storage_class = csc_none;
4111 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4113 if (decl_context == PARM && storage_class == csc_register)
4115 else
4117 switch (decl_context)
4119 case FIELD:
4120 error ("storage class specified for structure field %qs",
4121 name);
4122 break;
4123 case PARM:
4124 error ("storage class specified for parameter %qs", name);
4125 break;
4126 default:
4127 error ("storage class specified for typename");
4128 break;
4130 storage_class = csc_none;
4131 threadp = false;
4134 else if (storage_class == csc_extern
4135 && initialized
4136 && !funcdef_flag)
4138 /* 'extern' with initialization is invalid if not at file scope. */
4139 if (current_scope == file_scope)
4141 /* It is fine to have 'extern const' when compiling at C
4142 and C++ intersection. */
4143 if (!(warn_cxx_compat && constp))
4144 warning (0, "%qs initialized and declared %<extern%>", name);
4146 else
4147 error ("%qs has both %<extern%> and initializer", name);
4149 else if (current_scope == file_scope)
4151 if (storage_class == csc_auto)
4152 error ("file-scope declaration of %qs specifies %<auto%>", name);
4153 if (pedantic && storage_class == csc_register)
4154 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4156 else
4158 if (storage_class == csc_extern && funcdef_flag)
4159 error ("nested function %qs declared %<extern%>", name);
4160 else if (threadp && storage_class == csc_none)
4162 error ("function-scope %qs implicitly auto and declared "
4163 "%<__thread%>",
4164 name);
4165 threadp = false;
4169 /* Now figure out the structure of the declarator proper.
4170 Descend through it, creating more complex types, until we reach
4171 the declared identifier (or NULL_TREE, in an absolute declarator).
4172 At each stage we maintain an unqualified version of the type
4173 together with any qualifiers that should be applied to it with
4174 c_build_qualified_type; this way, array types including
4175 multidimensional array types are first built up in unqualified
4176 form and then the qualified form is created with
4177 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4179 while (declarator && declarator->kind != cdk_id)
4181 if (type == error_mark_node)
4183 declarator = declarator->declarator;
4184 continue;
4187 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4188 a cdk_pointer (for *...),
4189 a cdk_function (for ...(...)),
4190 a cdk_attrs (for nested attributes),
4191 or a cdk_id (for the name being declared
4192 or the place in an absolute declarator
4193 where the name was omitted).
4194 For the last case, we have just exited the loop.
4196 At this point, TYPE is the type of elements of an array,
4197 or for a function to return, or for a pointer to point to.
4198 After this sequence of ifs, TYPE is the type of the
4199 array or function or pointer, and DECLARATOR has had its
4200 outermost layer removed. */
4202 if (array_ptr_quals != TYPE_UNQUALIFIED
4203 || array_ptr_attrs != NULL_TREE
4204 || array_parm_static)
4206 /* Only the innermost declarator (making a parameter be of
4207 array type which is converted to pointer type)
4208 may have static or type qualifiers. */
4209 error ("static or type qualifiers in non-parameter array declarator");
4210 array_ptr_quals = TYPE_UNQUALIFIED;
4211 array_ptr_attrs = NULL_TREE;
4212 array_parm_static = 0;
4215 switch (declarator->kind)
4217 case cdk_attrs:
4219 /* A declarator with embedded attributes. */
4220 tree attrs = declarator->u.attrs;
4221 const struct c_declarator *inner_decl;
4222 int attr_flags = 0;
4223 declarator = declarator->declarator;
4224 inner_decl = declarator;
4225 while (inner_decl->kind == cdk_attrs)
4226 inner_decl = inner_decl->declarator;
4227 if (inner_decl->kind == cdk_id)
4228 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4229 else if (inner_decl->kind == cdk_function)
4230 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4231 else if (inner_decl->kind == cdk_array)
4232 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4233 returned_attrs = decl_attributes (&type,
4234 chainon (returned_attrs, attrs),
4235 attr_flags);
4236 break;
4238 case cdk_array:
4240 tree itype = NULL_TREE;
4241 tree size = declarator->u.array.dimen;
4242 /* The index is a signed object `sizetype' bits wide. */
4243 tree index_type = c_common_signed_type (sizetype);
4245 array_ptr_quals = declarator->u.array.quals;
4246 array_ptr_attrs = declarator->u.array.attrs;
4247 array_parm_static = declarator->u.array.static_p;
4248 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4250 declarator = declarator->declarator;
4252 /* Check for some types that there cannot be arrays of. */
4254 if (VOID_TYPE_P (type))
4256 error ("declaration of %qs as array of voids", name);
4257 type = error_mark_node;
4260 if (TREE_CODE (type) == FUNCTION_TYPE)
4262 error ("declaration of %qs as array of functions", name);
4263 type = error_mark_node;
4266 if (pedantic && !in_system_header && flexible_array_type_p (type))
4267 pedwarn ("invalid use of structure with flexible array member");
4269 if (size == error_mark_node)
4270 type = error_mark_node;
4272 if (type == error_mark_node)
4273 continue;
4275 /* If size was specified, set ITYPE to a range-type for
4276 that size. Otherwise, ITYPE remains null. finish_decl
4277 may figure it out from an initial value. */
4279 if (size)
4281 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4282 lvalue. */
4283 STRIP_TYPE_NOPS (size);
4285 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4287 error ("size of array %qs has non-integer type", name);
4288 size = integer_one_node;
4291 if (pedantic && integer_zerop (size))
4292 pedwarn ("ISO C forbids zero-size array %qs", name);
4294 if (TREE_CODE (size) == INTEGER_CST)
4296 constant_expression_warning (size);
4297 if (tree_int_cst_sgn (size) < 0)
4299 error ("size of array %qs is negative", name);
4300 size = integer_one_node;
4303 else if ((decl_context == NORMAL || decl_context == FIELD)
4304 && current_scope == file_scope)
4306 error ("variably modified %qs at file scope", name);
4307 size = integer_one_node;
4309 else
4311 /* Make sure the array size remains visibly
4312 nonconstant even if it is (eg) a const variable
4313 with known value. */
4314 size_varies = 1;
4316 if (!flag_isoc99 && pedantic)
4318 if (TREE_CONSTANT (size))
4319 pedwarn ("ISO C90 forbids array %qs whose size "
4320 "can%'t be evaluated",
4321 name);
4322 else
4323 pedwarn ("ISO C90 forbids variable-size array %qs",
4324 name);
4328 if (integer_zerop (size))
4330 /* A zero-length array cannot be represented with
4331 an unsigned index type, which is what we'll
4332 get with build_index_type. Create an
4333 open-ended range instead. */
4334 itype = build_range_type (sizetype, size, NULL_TREE);
4336 else
4338 /* Arrange for the SAVE_EXPR on the inside of the
4339 MINUS_EXPR, which allows the -1 to get folded
4340 with the +1 that happens when building TYPE_SIZE. */
4341 if (size_varies)
4342 size = variable_size (size);
4344 /* Compute the maximum valid index, that is, size
4345 - 1. Do the calculation in index_type, so that
4346 if it is a variable the computations will be
4347 done in the proper mode. */
4348 itype = fold_build2 (MINUS_EXPR, index_type,
4349 convert (index_type, size),
4350 convert (index_type,
4351 size_one_node));
4353 /* If that overflowed, the array is too big. ???
4354 While a size of INT_MAX+1 technically shouldn't
4355 cause an overflow (because we subtract 1), the
4356 overflow is recorded during the conversion to
4357 index_type, before the subtraction. Handling
4358 this case seems like an unnecessary
4359 complication. */
4360 if (TREE_CODE (itype) == INTEGER_CST
4361 && TREE_OVERFLOW (itype))
4363 error ("size of array %qs is too large", name);
4364 type = error_mark_node;
4365 continue;
4368 itype = build_index_type (itype);
4371 else if (decl_context == FIELD)
4373 if (pedantic && !flag_isoc99 && !in_system_header)
4374 pedwarn ("ISO C90 does not support flexible array members");
4376 /* ISO C99 Flexible array members are effectively
4377 identical to GCC's zero-length array extension. */
4378 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4380 else if (decl_context == PARM)
4382 if (array_parm_vla_unspec_p)
4384 if (! orig_name)
4386 /* C99 6.7.5.2p4 */
4387 error ("%<[*]%> not allowed in other than a declaration");
4390 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4391 size_varies = 1;
4394 else if (decl_context == TYPENAME)
4396 if (array_parm_vla_unspec_p)
4398 /* The error is printed elsewhere. We use this to
4399 avoid messing up with incomplete array types of
4400 the same type, that would otherwise be modified
4401 below. */
4402 itype = build_range_type (sizetype, size_zero_node,
4403 NULL_TREE);
4407 /* Complain about arrays of incomplete types. */
4408 if (!COMPLETE_TYPE_P (type))
4410 error ("array type has incomplete element type");
4411 type = error_mark_node;
4413 else
4414 /* When itype is NULL, a shared incomplete array type is
4415 returned for all array of a given type. Elsewhere we
4416 make sure we don't complete that type before copying
4417 it, but here we want to make sure we don't ever
4418 modify the shared type, so we gcc_assert (itype)
4419 below. */
4420 type = build_array_type (type, itype);
4422 if (type != error_mark_node)
4424 if (size_varies)
4426 /* It is ok to modify type here even if itype is
4427 NULL: if size_varies, we're in a
4428 multi-dimensional array and the inner type has
4429 variable size, so the enclosing shared array type
4430 must too. */
4431 if (size && TREE_CODE (size) == INTEGER_CST)
4432 type
4433 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4434 C_TYPE_VARIABLE_SIZE (type) = 1;
4437 /* The GCC extension for zero-length arrays differs from
4438 ISO flexible array members in that sizeof yields
4439 zero. */
4440 if (size && integer_zerop (size))
4442 gcc_assert (itype);
4443 TYPE_SIZE (type) = bitsize_zero_node;
4444 TYPE_SIZE_UNIT (type) = size_zero_node;
4446 if (array_parm_vla_unspec_p)
4448 gcc_assert (itype);
4449 /* The type is complete. C99 6.7.5.2p4 */
4450 TYPE_SIZE (type) = bitsize_zero_node;
4451 TYPE_SIZE_UNIT (type) = size_zero_node;
4455 if (decl_context != PARM
4456 && (array_ptr_quals != TYPE_UNQUALIFIED
4457 || array_ptr_attrs != NULL_TREE
4458 || array_parm_static))
4460 error ("static or type qualifiers in non-parameter array declarator");
4461 array_ptr_quals = TYPE_UNQUALIFIED;
4462 array_ptr_attrs = NULL_TREE;
4463 array_parm_static = 0;
4465 break;
4467 case cdk_function:
4469 /* Say it's a definition only for the declarator closest
4470 to the identifier, apart possibly from some
4471 attributes. */
4472 bool really_funcdef = false;
4473 tree arg_types;
4474 if (funcdef_flag)
4476 const struct c_declarator *t = declarator->declarator;
4477 while (t->kind == cdk_attrs)
4478 t = t->declarator;
4479 really_funcdef = (t->kind == cdk_id);
4482 /* Declaring a function type. Make sure we have a valid
4483 type for the function to return. */
4484 if (type == error_mark_node)
4485 continue;
4487 size_varies = 0;
4489 /* Warn about some types functions can't return. */
4490 if (TREE_CODE (type) == FUNCTION_TYPE)
4492 error ("%qs declared as function returning a function", name);
4493 type = integer_type_node;
4495 if (TREE_CODE (type) == ARRAY_TYPE)
4497 error ("%qs declared as function returning an array", name);
4498 type = integer_type_node;
4501 /* Construct the function type and go to the next
4502 inner layer of declarator. */
4503 arg_info = declarator->u.arg_info;
4504 arg_types = grokparms (arg_info, really_funcdef);
4505 if (really_funcdef)
4506 put_pending_sizes (arg_info->pending_sizes);
4508 /* Type qualifiers before the return type of the function
4509 qualify the return type, not the function type. */
4510 if (type_quals)
4512 /* Type qualifiers on a function return type are
4513 normally permitted by the standard but have no
4514 effect, so give a warning at -Wreturn-type.
4515 Qualifiers on a void return type are banned on
4516 function definitions in ISO C; GCC used to used
4517 them for noreturn functions. */
4518 if (VOID_TYPE_P (type) && really_funcdef)
4519 pedwarn ("function definition has qualified void return type");
4520 else
4521 warning (OPT_Wreturn_type,
4522 "type qualifiers ignored on function return type");
4524 type = c_build_qualified_type (type, type_quals);
4526 type_quals = TYPE_UNQUALIFIED;
4528 type = build_function_type (type, arg_types);
4529 declarator = declarator->declarator;
4531 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4532 the formal parameter list of this FUNCTION_TYPE to point to
4533 the FUNCTION_TYPE node itself. */
4535 tree link;
4537 for (link = arg_info->tags;
4538 link;
4539 link = TREE_CHAIN (link))
4540 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4542 break;
4544 case cdk_pointer:
4546 /* Merge any constancy or volatility into the target type
4547 for the pointer. */
4549 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4550 && type_quals)
4551 pedwarn ("ISO C forbids qualified function types");
4552 if (type_quals)
4553 type = c_build_qualified_type (type, type_quals);
4554 size_varies = 0;
4556 /* When the pointed-to type involves components of variable size,
4557 care must be taken to ensure that the size evaluation code is
4558 emitted early enough to dominate all the possible later uses
4559 and late enough for the variables on which it depends to have
4560 been assigned.
4562 This is expected to happen automatically when the pointed-to
4563 type has a name/declaration of it's own, but special attention
4564 is required if the type is anonymous.
4566 We handle the NORMAL and FIELD contexts here by attaching an
4567 artificial TYPE_DECL to such pointed-to type. This forces the
4568 sizes evaluation at a safe point and ensures it is not deferred
4569 until e.g. within a deeper conditional context.
4571 We expect nothing to be needed here for PARM or TYPENAME.
4572 Pushing a TYPE_DECL at this point for TYPENAME would actually
4573 be incorrect, as we might be in the middle of an expression
4574 with side effects on the pointed-to type size "arguments" prior
4575 to the pointer declaration point and the fake TYPE_DECL in the
4576 enclosing context would force the size evaluation prior to the
4577 side effects. */
4579 if (!TYPE_NAME (type)
4580 && (decl_context == NORMAL || decl_context == FIELD)
4581 && variably_modified_type_p (type, NULL_TREE))
4583 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4584 DECL_ARTIFICIAL (decl) = 1;
4585 pushdecl (decl);
4586 finish_decl (decl, NULL_TREE, NULL_TREE);
4587 TYPE_NAME (type) = decl;
4590 type = build_pointer_type (type);
4592 /* Process type qualifiers (such as const or volatile)
4593 that were given inside the `*'. */
4594 type_quals = declarator->u.pointer_quals;
4596 declarator = declarator->declarator;
4597 break;
4599 default:
4600 gcc_unreachable ();
4604 /* Now TYPE has the actual type, apart from any qualifiers in
4605 TYPE_QUALS. */
4607 /* Check the type and width of a bit-field. */
4608 if (bitfield)
4609 check_bitfield_type_and_width (&type, width, orig_name);
4611 /* Did array size calculations overflow? */
4613 if (TREE_CODE (type) == ARRAY_TYPE
4614 && COMPLETE_TYPE_P (type)
4615 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4616 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4618 error ("size of array %qs is too large", name);
4619 /* If we proceed with the array type as it is, we'll eventually
4620 crash in tree_low_cst(). */
4621 type = error_mark_node;
4624 /* If this is declaring a typedef name, return a TYPE_DECL. */
4626 if (storage_class == csc_typedef)
4628 tree decl;
4629 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4630 && type_quals)
4631 pedwarn ("ISO C forbids qualified function types");
4632 if (type_quals)
4633 type = c_build_qualified_type (type, type_quals);
4634 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4635 if (declspecs->explicit_signed_p)
4636 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4637 decl_attributes (&decl, returned_attrs, 0);
4638 if (declspecs->inline_p)
4639 pedwarn ("typedef %q+D declared %<inline%>", decl);
4640 return decl;
4643 /* If this is a type name (such as, in a cast or sizeof),
4644 compute the type and return it now. */
4646 if (decl_context == TYPENAME)
4648 /* Note that the grammar rejects storage classes in typenames
4649 and fields. */
4650 gcc_assert (storage_class == csc_none && !threadp
4651 && !declspecs->inline_p);
4652 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4653 && type_quals)
4654 pedwarn ("ISO C forbids const or volatile function types");
4655 if (type_quals)
4656 type = c_build_qualified_type (type, type_quals);
4657 decl_attributes (&type, returned_attrs, 0);
4658 return type;
4661 if (pedantic && decl_context == FIELD
4662 && variably_modified_type_p (type, NULL_TREE))
4664 /* C99 6.7.2.1p8 */
4665 pedwarn ("a member of a structure or union cannot have a variably modified type");
4668 /* Aside from typedefs and type names (handle above),
4669 `void' at top level (not within pointer)
4670 is allowed only in public variables.
4671 We don't complain about parms either, but that is because
4672 a better error message can be made later. */
4674 if (VOID_TYPE_P (type) && decl_context != PARM
4675 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4676 && (storage_class == csc_extern
4677 || (current_scope == file_scope
4678 && !(storage_class == csc_static
4679 || storage_class == csc_register)))))
4681 error ("variable or field %qs declared void", name);
4682 type = integer_type_node;
4685 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4686 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4689 tree decl;
4691 if (decl_context == PARM)
4693 tree type_as_written;
4694 tree promoted_type;
4696 /* A parameter declared as an array of T is really a pointer to T.
4697 One declared as a function is really a pointer to a function. */
4699 if (TREE_CODE (type) == ARRAY_TYPE)
4701 /* Transfer const-ness of array into that of type pointed to. */
4702 type = TREE_TYPE (type);
4703 if (type_quals)
4704 type = c_build_qualified_type (type, type_quals);
4705 type = build_pointer_type (type);
4706 type_quals = array_ptr_quals;
4708 /* We don't yet implement attributes in this context. */
4709 if (array_ptr_attrs != NULL_TREE)
4710 warning (OPT_Wattributes,
4711 "attributes in parameter array declarator ignored");
4713 size_varies = 0;
4715 else if (TREE_CODE (type) == FUNCTION_TYPE)
4717 if (pedantic && type_quals)
4718 pedwarn ("ISO C forbids qualified function types");
4719 if (type_quals)
4720 type = c_build_qualified_type (type, type_quals);
4721 type = build_pointer_type (type);
4722 type_quals = TYPE_UNQUALIFIED;
4724 else if (type_quals)
4725 type = c_build_qualified_type (type, type_quals);
4727 type_as_written = type;
4729 decl = build_decl (PARM_DECL, declarator->u.id, type);
4730 if (size_varies)
4731 C_DECL_VARIABLE_SIZE (decl) = 1;
4733 /* Compute the type actually passed in the parmlist,
4734 for the case where there is no prototype.
4735 (For example, shorts and chars are passed as ints.)
4736 When there is a prototype, this is overridden later. */
4738 if (type == error_mark_node)
4739 promoted_type = type;
4740 else
4741 promoted_type = c_type_promotes_to (type);
4743 DECL_ARG_TYPE (decl) = promoted_type;
4744 if (declspecs->inline_p)
4745 pedwarn ("parameter %q+D declared %<inline%>", decl);
4747 else if (decl_context == FIELD)
4749 /* Note that the grammar rejects storage classes in typenames
4750 and fields. */
4751 gcc_assert (storage_class == csc_none && !threadp
4752 && !declspecs->inline_p);
4754 /* Structure field. It may not be a function. */
4756 if (TREE_CODE (type) == FUNCTION_TYPE)
4758 error ("field %qs declared as a function", name);
4759 type = build_pointer_type (type);
4761 else if (TREE_CODE (type) != ERROR_MARK
4762 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4764 error ("field %qs has incomplete type", name);
4765 type = error_mark_node;
4767 type = c_build_qualified_type (type, type_quals);
4768 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4769 DECL_NONADDRESSABLE_P (decl) = bitfield;
4771 if (size_varies)
4772 C_DECL_VARIABLE_SIZE (decl) = 1;
4774 else if (TREE_CODE (type) == FUNCTION_TYPE)
4776 if (storage_class == csc_register || threadp)
4778 error ("invalid storage class for function %qs", name);
4780 else if (current_scope != file_scope)
4782 /* Function declaration not at file scope. Storage
4783 classes other than `extern' are not allowed, C99
4784 6.7.1p5, and `extern' makes no difference. However,
4785 GCC allows 'auto', perhaps with 'inline', to support
4786 nested functions. */
4787 if (storage_class == csc_auto)
4789 if (pedantic)
4790 pedwarn ("invalid storage class for function %qs", name);
4792 else if (storage_class == csc_static)
4794 error ("invalid storage class for function %qs", name);
4795 if (funcdef_flag)
4796 storage_class = declspecs->storage_class = csc_none;
4797 else
4798 return 0;
4802 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4803 decl = build_decl_attribute_variant (decl, decl_attr);
4805 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4807 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4808 pedwarn ("ISO C forbids qualified function types");
4810 /* GNU C interprets a volatile-qualified function type to indicate
4811 that the function does not return. */
4812 if ((type_quals & TYPE_QUAL_VOLATILE)
4813 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4814 warning (0, "%<noreturn%> function returns non-void value");
4816 /* Every function declaration is an external reference
4817 (DECL_EXTERNAL) except for those which are not at file
4818 scope and are explicitly declared "auto". This is
4819 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4820 GCC to signify a forward declaration of a nested function. */
4821 if (storage_class == csc_auto && current_scope != file_scope)
4822 DECL_EXTERNAL (decl) = 0;
4823 /* In C99, a function which is declared 'inline' with 'extern'
4824 is not an external reference (which is confusing). It
4825 means that the later definition of the function must be output
4826 in this file, C99 6.7.4p6. In GNU C89, a function declared
4827 'extern inline' is an external reference. */
4828 else if (declspecs->inline_p && storage_class != csc_static)
4829 #if WANT_C99_INLINE_SEMANTICS
4830 DECL_EXTERNAL (decl) = (storage_class == csc_extern) == !flag_isoc99;
4831 #else
4832 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4833 #endif
4834 else
4835 DECL_EXTERNAL (decl) = !initialized;
4837 /* Record absence of global scope for `static' or `auto'. */
4838 TREE_PUBLIC (decl)
4839 = !(storage_class == csc_static || storage_class == csc_auto);
4841 /* For a function definition, record the argument information
4842 block where store_parm_decls will look for it. */
4843 if (funcdef_flag)
4844 current_function_arg_info = arg_info;
4846 if (declspecs->default_int_p)
4847 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4849 /* Record presence of `inline', if it is reasonable. */
4850 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4852 if (declspecs->inline_p)
4853 pedwarn ("cannot inline function %<main%>");
4855 else if (declspecs->inline_p)
4857 /* Record that the function is declared `inline'. */
4858 DECL_DECLARED_INLINE_P (decl) = 1;
4860 /* Do not mark bare declarations as DECL_INLINE. Doing so
4861 in the presence of multiple declarations can result in
4862 the abstract origin pointing between the declarations,
4863 which will confuse dwarf2out. */
4864 if (initialized)
4865 DECL_INLINE (decl) = 1;
4867 /* If -finline-functions, assume it can be inlined. This does
4868 two things: let the function be deferred until it is actually
4869 needed, and let dwarf2 know that the function is inlinable. */
4870 else if (flag_inline_trees == 2 && initialized)
4871 DECL_INLINE (decl) = 1;
4873 else
4875 /* It's a variable. */
4876 /* An uninitialized decl with `extern' is a reference. */
4877 int extern_ref = !initialized && storage_class == csc_extern;
4879 type = c_build_qualified_type (type, type_quals);
4881 /* C99 6.2.2p7: It is invalid (compile-time undefined
4882 behavior) to create an 'extern' declaration for a
4883 variable if there is a global declaration that is
4884 'static' and the global declaration is not visible.
4885 (If the static declaration _is_ currently visible,
4886 the 'extern' declaration is taken to refer to that decl.) */
4887 if (extern_ref && current_scope != file_scope)
4889 tree global_decl = identifier_global_value (declarator->u.id);
4890 tree visible_decl = lookup_name (declarator->u.id);
4892 if (global_decl
4893 && global_decl != visible_decl
4894 && TREE_CODE (global_decl) == VAR_DECL
4895 && !TREE_PUBLIC (global_decl))
4896 error ("variable previously declared %<static%> redeclared "
4897 "%<extern%>");
4900 decl = build_decl (VAR_DECL, declarator->u.id, type);
4901 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4902 if (size_varies)
4903 C_DECL_VARIABLE_SIZE (decl) = 1;
4905 if (declspecs->inline_p)
4906 pedwarn ("variable %q+D declared %<inline%>", decl);
4908 /* At file scope, an initialized extern declaration may follow
4909 a static declaration. In that case, DECL_EXTERNAL will be
4910 reset later in start_decl. */
4911 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4913 /* At file scope, the presence of a `static' or `register' storage
4914 class specifier, or the absence of all storage class specifiers
4915 makes this declaration a definition (perhaps tentative). Also,
4916 the absence of `static' makes it public. */
4917 if (current_scope == file_scope)
4919 TREE_PUBLIC (decl) = storage_class != csc_static;
4920 TREE_STATIC (decl) = !extern_ref;
4922 /* Not at file scope, only `static' makes a static definition. */
4923 else
4925 TREE_STATIC (decl) = (storage_class == csc_static);
4926 TREE_PUBLIC (decl) = extern_ref;
4929 if (threadp)
4930 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
4933 if (storage_class == csc_extern
4934 && variably_modified_type_p (type, NULL_TREE))
4936 /* C99 6.7.5.2p2 */
4937 error ("object with variably modified type must have no linkage");
4940 /* Record `register' declaration for warnings on &
4941 and in case doing stupid register allocation. */
4943 if (storage_class == csc_register)
4945 C_DECL_REGISTER (decl) = 1;
4946 DECL_REGISTER (decl) = 1;
4949 /* Record constancy and volatility. */
4950 c_apply_type_quals_to_decl (type_quals, decl);
4952 /* If a type has volatile components, it should be stored in memory.
4953 Otherwise, the fact that those components are volatile
4954 will be ignored, and would even crash the compiler.
4955 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
4956 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
4957 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
4958 || TREE_CODE (decl) == RESULT_DECL))
4960 /* It is not an error for a structure with volatile fields to
4961 be declared register, but reset DECL_REGISTER since it
4962 cannot actually go in a register. */
4963 int was_reg = C_DECL_REGISTER (decl);
4964 C_DECL_REGISTER (decl) = 0;
4965 DECL_REGISTER (decl) = 0;
4966 c_mark_addressable (decl);
4967 C_DECL_REGISTER (decl) = was_reg;
4970 /* This is the earliest point at which we might know the assembler
4971 name of a variable. Thus, if it's known before this, die horribly. */
4972 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4974 decl_attributes (&decl, returned_attrs, 0);
4976 return decl;
4980 /* Decode the parameter-list info for a function type or function definition.
4981 The argument is the value returned by `get_parm_info' (or made in c-parse.c
4982 if there is an identifier list instead of a parameter decl list).
4983 These two functions are separate because when a function returns
4984 or receives functions then each is called multiple times but the order
4985 of calls is different. The last call to `grokparms' is always the one
4986 that contains the formal parameter names of a function definition.
4988 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4990 FUNCDEF_FLAG is true for a function definition, false for
4991 a mere declaration. A nonempty identifier-list gets an error message
4992 when FUNCDEF_FLAG is false. */
4994 static tree
4995 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4997 tree arg_types = arg_info->types;
4999 if (funcdef_flag && arg_info->had_vla_unspec)
5001 /* A function definition isn't function prototype scope C99 6.2.1p4. */
5002 /* C99 6.7.5.2p4 */
5003 error ("%<[*]%> not allowed in other than function prototype scope");
5006 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5007 warning (OPT_Wstrict_prototypes,
5008 "function declaration isn%'t a prototype");
5010 if (arg_types == error_mark_node)
5011 return 0; /* don't set TYPE_ARG_TYPES in this case */
5013 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5015 if (!funcdef_flag)
5016 pedwarn ("parameter names (without types) in function declaration");
5018 arg_info->parms = arg_info->types;
5019 arg_info->types = 0;
5020 return 0;
5022 else
5024 tree parm, type, typelt;
5025 unsigned int parmno;
5027 /* If there is a parameter of incomplete type in a definition,
5028 this is an error. In a declaration this is valid, and a
5029 struct or union type may be completed later, before any calls
5030 or definition of the function. In the case where the tag was
5031 first declared within the parameter list, a warning has
5032 already been given. If a parameter has void type, then
5033 however the function cannot be defined or called, so
5034 warn. */
5036 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5037 parm;
5038 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5040 type = TREE_VALUE (typelt);
5041 if (type == error_mark_node)
5042 continue;
5044 if (!COMPLETE_TYPE_P (type))
5046 if (funcdef_flag)
5048 if (DECL_NAME (parm))
5049 error ("parameter %u (%q+D) has incomplete type",
5050 parmno, parm);
5051 else
5052 error ("%Jparameter %u has incomplete type",
5053 parm, parmno);
5055 TREE_VALUE (typelt) = error_mark_node;
5056 TREE_TYPE (parm) = error_mark_node;
5058 else if (VOID_TYPE_P (type))
5060 if (DECL_NAME (parm))
5061 warning (0, "parameter %u (%q+D) has void type",
5062 parmno, parm);
5063 else
5064 warning (0, "%Jparameter %u has void type",
5065 parm, parmno);
5069 if (DECL_NAME (parm) && TREE_USED (parm))
5070 warn_if_shadowing (parm);
5072 return arg_types;
5076 /* Take apart the current scope and return a c_arg_info structure with
5077 info on a parameter list just parsed.
5079 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5081 ELLIPSIS being true means the argument list ended in '...' so don't
5082 append a sentinel (void_list_node) to the end of the type-list. */
5084 struct c_arg_info *
5085 get_parm_info (bool ellipsis)
5087 struct c_binding *b = current_scope->bindings;
5088 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5089 struct c_arg_info);
5090 tree parms = 0;
5091 tree tags = 0;
5092 tree types = 0;
5093 tree others = 0;
5095 static bool explained_incomplete_types = false;
5096 bool gave_void_only_once_err = false;
5098 arg_info->parms = 0;
5099 arg_info->tags = 0;
5100 arg_info->types = 0;
5101 arg_info->others = 0;
5102 arg_info->pending_sizes = 0;
5103 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5105 /* The bindings in this scope must not get put into a block.
5106 We will take care of deleting the binding nodes. */
5107 current_scope->bindings = 0;
5109 /* This function is only called if there was *something* on the
5110 parameter list. */
5111 gcc_assert (b);
5113 /* A parameter list consisting solely of 'void' indicates that the
5114 function takes no arguments. But if the 'void' is qualified
5115 (by 'const' or 'volatile'), or has a storage class specifier
5116 ('register'), then the behavior is undefined; issue an error.
5117 Typedefs for 'void' are OK (see DR#157). */
5118 if (b->prev == 0 /* one binding */
5119 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5120 && !DECL_NAME (b->decl) /* anonymous */
5121 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5123 if (TREE_THIS_VOLATILE (b->decl)
5124 || TREE_READONLY (b->decl)
5125 || C_DECL_REGISTER (b->decl))
5126 error ("%<void%> as only parameter may not be qualified");
5128 /* There cannot be an ellipsis. */
5129 if (ellipsis)
5130 error ("%<void%> must be the only parameter");
5132 arg_info->types = void_list_node;
5133 return arg_info;
5136 if (!ellipsis)
5137 types = void_list_node;
5139 /* Break up the bindings list into parms, tags, types, and others;
5140 apply sanity checks; purge the name-to-decl bindings. */
5141 while (b)
5143 tree decl = b->decl;
5144 tree type = TREE_TYPE (decl);
5145 const char *keyword;
5147 switch (TREE_CODE (decl))
5149 case PARM_DECL:
5150 if (b->id)
5152 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5153 I_SYMBOL_BINDING (b->id) = b->shadowed;
5156 /* Check for forward decls that never got their actual decl. */
5157 if (TREE_ASM_WRITTEN (decl))
5158 error ("parameter %q+D has just a forward declaration", decl);
5159 /* Check for (..., void, ...) and issue an error. */
5160 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5162 if (!gave_void_only_once_err)
5164 error ("%<void%> must be the only parameter");
5165 gave_void_only_once_err = true;
5168 else
5170 /* Valid parameter, add it to the list. */
5171 TREE_CHAIN (decl) = parms;
5172 parms = decl;
5174 /* Since there is a prototype, args are passed in their
5175 declared types. The back end may override this later. */
5176 DECL_ARG_TYPE (decl) = type;
5177 types = tree_cons (0, type, types);
5179 break;
5181 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5182 case UNION_TYPE: keyword = "union"; goto tag;
5183 case RECORD_TYPE: keyword = "struct"; goto tag;
5184 tag:
5185 /* Types may not have tag-names, in which case the type
5186 appears in the bindings list with b->id NULL. */
5187 if (b->id)
5189 gcc_assert (I_TAG_BINDING (b->id) == b);
5190 I_TAG_BINDING (b->id) = b->shadowed;
5193 /* Warn about any struct, union or enum tags defined in a
5194 parameter list. The scope of such types is limited to
5195 the parameter list, which is rarely if ever desirable
5196 (it's impossible to call such a function with type-
5197 correct arguments). An anonymous union parm type is
5198 meaningful as a GNU extension, so don't warn for that. */
5199 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5201 if (b->id)
5202 /* The %s will be one of 'struct', 'union', or 'enum'. */
5203 warning (0, "%<%s %E%> declared inside parameter list",
5204 keyword, b->id);
5205 else
5206 /* The %s will be one of 'struct', 'union', or 'enum'. */
5207 warning (0, "anonymous %s declared inside parameter list",
5208 keyword);
5210 if (!explained_incomplete_types)
5212 warning (0, "its scope is only this definition or declaration,"
5213 " which is probably not what you want");
5214 explained_incomplete_types = true;
5218 tags = tree_cons (b->id, decl, tags);
5219 break;
5221 case CONST_DECL:
5222 case TYPE_DECL:
5223 case FUNCTION_DECL:
5224 /* CONST_DECLs appear here when we have an embedded enum,
5225 and TYPE_DECLs appear here when we have an embedded struct
5226 or union. No warnings for this - we already warned about the
5227 type itself. FUNCTION_DECLs appear when there is an implicit
5228 function declaration in the parameter list. */
5230 TREE_CHAIN (decl) = others;
5231 others = decl;
5232 /* fall through */
5234 case ERROR_MARK:
5235 /* error_mark_node appears here when we have an undeclared
5236 variable. Just throw it away. */
5237 if (b->id)
5239 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5240 I_SYMBOL_BINDING (b->id) = b->shadowed;
5242 break;
5244 /* Other things that might be encountered. */
5245 case LABEL_DECL:
5246 case VAR_DECL:
5247 default:
5248 gcc_unreachable ();
5251 b = free_binding_and_advance (b);
5254 arg_info->parms = parms;
5255 arg_info->tags = tags;
5256 arg_info->types = types;
5257 arg_info->others = others;
5258 arg_info->pending_sizes = get_pending_sizes ();
5259 return arg_info;
5262 /* Get the struct, enum or union (CODE says which) with tag NAME.
5263 Define the tag as a forward-reference if it is not defined.
5264 Return a c_typespec structure for the type specifier. */
5266 struct c_typespec
5267 parser_xref_tag (enum tree_code code, tree name)
5269 struct c_typespec ret;
5270 /* If a cross reference is requested, look up the type
5271 already defined for this tag and return it. */
5273 tree ref = lookup_tag (code, name, 0);
5274 /* If this is the right type of tag, return what we found.
5275 (This reference will be shadowed by shadow_tag later if appropriate.)
5276 If this is the wrong type of tag, do not return it. If it was the
5277 wrong type in the same scope, we will have had an error
5278 message already; if in a different scope and declaring
5279 a name, pending_xref_error will give an error message; but if in a
5280 different scope and not declaring a name, this tag should
5281 shadow the previous declaration of a different type of tag, and
5282 this would not work properly if we return the reference found.
5283 (For example, with "struct foo" in an outer scope, "union foo;"
5284 must shadow that tag with a new one of union type.) */
5285 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5286 if (ref && TREE_CODE (ref) == code)
5288 ret.spec = ref;
5289 return ret;
5292 /* If no such tag is yet defined, create a forward-reference node
5293 and record it as the "definition".
5294 When a real declaration of this type is found,
5295 the forward-reference will be altered into a real type. */
5297 ref = make_node (code);
5298 if (code == ENUMERAL_TYPE)
5300 /* Give the type a default layout like unsigned int
5301 to avoid crashing if it does not get defined. */
5302 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5303 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5304 TYPE_USER_ALIGN (ref) = 0;
5305 TYPE_UNSIGNED (ref) = 1;
5306 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5307 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5308 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5311 pushtag (name, ref);
5313 ret.spec = ref;
5314 return ret;
5317 /* Get the struct, enum or union (CODE says which) with tag NAME.
5318 Define the tag as a forward-reference if it is not defined.
5319 Return a tree for the type. */
5321 tree
5322 xref_tag (enum tree_code code, tree name)
5324 return parser_xref_tag (code, name).spec;
5327 /* Make sure that the tag NAME is defined *in the current scope*
5328 at least as a forward reference.
5329 CODE says which kind of tag NAME ought to be. */
5331 tree
5332 start_struct (enum tree_code code, tree name)
5334 /* If there is already a tag defined at this scope
5335 (as a forward reference), just return it. */
5337 tree ref = 0;
5339 if (name != 0)
5340 ref = lookup_tag (code, name, 1);
5341 if (ref && TREE_CODE (ref) == code)
5343 if (TYPE_SIZE (ref))
5345 if (code == UNION_TYPE)
5346 error ("redefinition of %<union %E%>", name);
5347 else
5348 error ("redefinition of %<struct %E%>", name);
5350 else if (C_TYPE_BEING_DEFINED (ref))
5352 if (code == UNION_TYPE)
5353 error ("nested redefinition of %<union %E%>", name);
5354 else
5355 error ("nested redefinition of %<struct %E%>", name);
5356 /* Don't create structures that contain themselves. */
5357 ref = NULL_TREE;
5361 /* Otherwise create a forward-reference just so the tag is in scope. */
5363 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5365 ref = make_node (code);
5366 pushtag (name, ref);
5369 C_TYPE_BEING_DEFINED (ref) = 1;
5370 TYPE_PACKED (ref) = flag_pack_struct;
5371 return ref;
5374 /* Process the specs, declarator and width (NULL if omitted)
5375 of a structure component, returning a FIELD_DECL node.
5376 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5378 This is done during the parsing of the struct declaration.
5379 The FIELD_DECL nodes are chained together and the lot of them
5380 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5382 tree
5383 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5384 tree width)
5386 tree value;
5388 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5389 && width == NULL_TREE)
5391 /* This is an unnamed decl.
5393 If we have something of the form "union { list } ;" then this
5394 is the anonymous union extension. Similarly for struct.
5396 If this is something of the form "struct foo;", then
5397 If MS extensions are enabled, this is handled as an
5398 anonymous struct.
5399 Otherwise this is a forward declaration of a structure tag.
5401 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5402 If MS extensions are enabled and foo names a structure, then
5403 again this is an anonymous struct.
5404 Otherwise this is an error.
5406 Oh what a horrid tangled web we weave. I wonder if MS consciously
5407 took this from Plan 9 or if it was an accident of implementation
5408 that took root before someone noticed the bug... */
5410 tree type = declspecs->type;
5411 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5412 || TREE_CODE (type) == UNION_TYPE);
5413 bool ok = false;
5415 if (type_ok
5416 && (flag_ms_extensions || !declspecs->typedef_p))
5418 if (flag_ms_extensions)
5419 ok = true;
5420 else if (flag_iso)
5421 ok = false;
5422 else if (TYPE_NAME (type) == NULL)
5423 ok = true;
5424 else
5425 ok = false;
5427 if (!ok)
5429 pedwarn ("declaration does not declare anything");
5430 return NULL_TREE;
5432 if (pedantic)
5433 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5436 value = grokdeclarator (declarator, declspecs, FIELD, false,
5437 width ? &width : NULL);
5439 finish_decl (value, NULL_TREE, NULL_TREE);
5440 DECL_INITIAL (value) = width;
5442 return value;
5445 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5446 the list such that this does not present a problem later. */
5448 static void
5449 detect_field_duplicates (tree fieldlist)
5451 tree x, y;
5452 int timeout = 10;
5454 /* First, see if there are more than "a few" fields.
5455 This is trivially true if there are zero or one fields. */
5456 if (!fieldlist)
5457 return;
5458 x = TREE_CHAIN (fieldlist);
5459 if (!x)
5460 return;
5461 do {
5462 timeout--;
5463 x = TREE_CHAIN (x);
5464 } while (timeout > 0 && x);
5466 /* If there were "few" fields, avoid the overhead of allocating
5467 a hash table. Instead just do the nested traversal thing. */
5468 if (timeout > 0)
5470 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5471 if (DECL_NAME (x))
5473 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5474 if (DECL_NAME (y) == DECL_NAME (x))
5476 error ("duplicate member %q+D", x);
5477 DECL_NAME (x) = NULL_TREE;
5481 else
5483 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5484 void **slot;
5486 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5487 if ((y = DECL_NAME (x)) != 0)
5489 slot = htab_find_slot (htab, y, INSERT);
5490 if (*slot)
5492 error ("duplicate member %q+D", x);
5493 DECL_NAME (x) = NULL_TREE;
5495 *slot = y;
5498 htab_delete (htab);
5502 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5503 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5504 ATTRIBUTES are attributes to be applied to the structure. */
5506 tree
5507 finish_struct (tree t, tree fieldlist, tree attributes)
5509 tree x;
5510 bool toplevel = file_scope == current_scope;
5511 int saw_named_field;
5513 /* If this type was previously laid out as a forward reference,
5514 make sure we lay it out again. */
5516 TYPE_SIZE (t) = 0;
5518 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5520 if (pedantic)
5522 for (x = fieldlist; x; x = TREE_CHAIN (x))
5523 if (DECL_NAME (x) != 0)
5524 break;
5526 if (x == 0)
5528 if (TREE_CODE (t) == UNION_TYPE)
5530 if (fieldlist)
5531 pedwarn ("union has no named members");
5532 else
5533 pedwarn ("union has no members");
5535 else
5537 if (fieldlist)
5538 pedwarn ("struct has no named members");
5539 else
5540 pedwarn ("struct has no members");
5545 /* Install struct as DECL_CONTEXT of each field decl.
5546 Also process specified field sizes, found in the DECL_INITIAL,
5547 storing 0 there after the type has been changed to precision equal
5548 to its width, rather than the precision of the specified standard
5549 type. (Correct layout requires the original type to have been preserved
5550 until now.) */
5552 saw_named_field = 0;
5553 for (x = fieldlist; x; x = TREE_CHAIN (x))
5555 if (TREE_TYPE (x) == error_mark_node)
5556 continue;
5558 DECL_CONTEXT (x) = t;
5560 if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
5561 DECL_PACKED (x) = 1;
5563 /* If any field is const, the structure type is pseudo-const. */
5564 if (TREE_READONLY (x))
5565 C_TYPE_FIELDS_READONLY (t) = 1;
5566 else
5568 /* A field that is pseudo-const makes the structure likewise. */
5569 tree t1 = TREE_TYPE (x);
5570 while (TREE_CODE (t1) == ARRAY_TYPE)
5571 t1 = TREE_TYPE (t1);
5572 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5573 && C_TYPE_FIELDS_READONLY (t1))
5574 C_TYPE_FIELDS_READONLY (t) = 1;
5577 /* Any field that is volatile means variables of this type must be
5578 treated in some ways as volatile. */
5579 if (TREE_THIS_VOLATILE (x))
5580 C_TYPE_FIELDS_VOLATILE (t) = 1;
5582 /* Any field of nominal variable size implies structure is too. */
5583 if (C_DECL_VARIABLE_SIZE (x))
5584 C_TYPE_VARIABLE_SIZE (t) = 1;
5586 if (DECL_INITIAL (x))
5588 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5589 DECL_SIZE (x) = bitsize_int (width);
5590 DECL_BIT_FIELD (x) = 1;
5591 SET_DECL_C_BIT_FIELD (x);
5594 /* Detect flexible array member in an invalid context. */
5595 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5596 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5597 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5598 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5600 if (TREE_CODE (t) == UNION_TYPE)
5602 error ("%Jflexible array member in union", x);
5603 TREE_TYPE (x) = error_mark_node;
5605 else if (TREE_CHAIN (x) != NULL_TREE)
5607 error ("%Jflexible array member not at end of struct", x);
5608 TREE_TYPE (x) = error_mark_node;
5610 else if (!saw_named_field)
5612 error ("%Jflexible array member in otherwise empty struct", x);
5613 TREE_TYPE (x) = error_mark_node;
5617 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5618 && flexible_array_type_p (TREE_TYPE (x)))
5619 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5621 if (DECL_NAME (x))
5622 saw_named_field = 1;
5625 detect_field_duplicates (fieldlist);
5627 /* Now we have the nearly final fieldlist. Record it,
5628 then lay out the structure or union (including the fields). */
5630 TYPE_FIELDS (t) = fieldlist;
5632 layout_type (t);
5634 /* Give bit-fields their proper types. */
5636 tree *fieldlistp = &fieldlist;
5637 while (*fieldlistp)
5638 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5639 && TREE_TYPE (*fieldlistp) != error_mark_node)
5641 unsigned HOST_WIDE_INT width
5642 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5643 tree type = TREE_TYPE (*fieldlistp);
5644 if (width != TYPE_PRECISION (type))
5646 TREE_TYPE (*fieldlistp)
5647 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5648 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5650 DECL_INITIAL (*fieldlistp) = 0;
5652 else
5653 fieldlistp = &TREE_CHAIN (*fieldlistp);
5656 /* Now we have the truly final field list.
5657 Store it in this type and in the variants. */
5659 TYPE_FIELDS (t) = fieldlist;
5661 /* If there are lots of fields, sort so we can look through them fast.
5662 We arbitrarily consider 16 or more elts to be "a lot". */
5665 int len = 0;
5667 for (x = fieldlist; x; x = TREE_CHAIN (x))
5669 if (len > 15 || DECL_NAME (x) == NULL)
5670 break;
5671 len += 1;
5674 if (len > 15)
5676 tree *field_array;
5677 struct lang_type *space;
5678 struct sorted_fields_type *space2;
5680 len += list_length (x);
5682 /* Use the same allocation policy here that make_node uses, to
5683 ensure that this lives as long as the rest of the struct decl.
5684 All decls in an inline function need to be saved. */
5686 space = GGC_CNEW (struct lang_type);
5687 space2 = GGC_NEWVAR (struct sorted_fields_type,
5688 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5690 len = 0;
5691 space->s = space2;
5692 field_array = &space2->elts[0];
5693 for (x = fieldlist; x; x = TREE_CHAIN (x))
5695 field_array[len++] = x;
5697 /* If there is anonymous struct or union, break out of the loop. */
5698 if (DECL_NAME (x) == NULL)
5699 break;
5701 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5702 if (x == NULL)
5704 TYPE_LANG_SPECIFIC (t) = space;
5705 TYPE_LANG_SPECIFIC (t)->s->len = len;
5706 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5707 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5712 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5714 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5715 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5716 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5717 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5718 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5721 /* If this was supposed to be a transparent union, but we can't
5722 make it one, warn and turn off the flag. */
5723 if (TREE_CODE (t) == UNION_TYPE
5724 && TYPE_TRANSPARENT_UNION (t)
5725 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5727 TYPE_TRANSPARENT_UNION (t) = 0;
5728 warning (0, "union cannot be made transparent");
5731 /* If this structure or union completes the type of any previous
5732 variable declaration, lay it out and output its rtl. */
5733 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5735 x = TREE_CHAIN (x))
5737 tree decl = TREE_VALUE (x);
5738 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5739 layout_array_type (TREE_TYPE (decl));
5740 if (TREE_CODE (decl) != TYPE_DECL)
5742 layout_decl (decl, 0);
5743 if (c_dialect_objc ())
5744 objc_check_decl (decl);
5745 rest_of_decl_compilation (decl, toplevel, 0);
5746 if (!toplevel)
5747 expand_decl (decl);
5750 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5752 /* Finish debugging output for this type. */
5753 rest_of_type_compilation (t, toplevel);
5755 /* If we're inside a function proper, i.e. not file-scope and not still
5756 parsing parameters, then arrange for the size of a variable sized type
5757 to be bound now. */
5758 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5759 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5761 return t;
5764 /* Lay out the type T, and its element type, and so on. */
5766 static void
5767 layout_array_type (tree t)
5769 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5770 layout_array_type (TREE_TYPE (t));
5771 layout_type (t);
5774 /* Begin compiling the definition of an enumeration type.
5775 NAME is its name (or null if anonymous).
5776 Returns the type object, as yet incomplete.
5777 Also records info about it so that build_enumerator
5778 may be used to declare the individual values as they are read. */
5780 tree
5781 start_enum (tree name)
5783 tree enumtype = 0;
5785 /* If this is the real definition for a previous forward reference,
5786 fill in the contents in the same object that used to be the
5787 forward reference. */
5789 if (name != 0)
5790 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5792 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5794 enumtype = make_node (ENUMERAL_TYPE);
5795 pushtag (name, enumtype);
5798 if (C_TYPE_BEING_DEFINED (enumtype))
5799 error ("nested redefinition of %<enum %E%>", name);
5801 C_TYPE_BEING_DEFINED (enumtype) = 1;
5803 if (TYPE_VALUES (enumtype) != 0)
5805 /* This enum is a named one that has been declared already. */
5806 error ("redeclaration of %<enum %E%>", name);
5808 /* Completely replace its old definition.
5809 The old enumerators remain defined, however. */
5810 TYPE_VALUES (enumtype) = 0;
5813 enum_next_value = integer_zero_node;
5814 enum_overflow = 0;
5816 if (flag_short_enums)
5817 TYPE_PACKED (enumtype) = 1;
5819 return enumtype;
5822 /* After processing and defining all the values of an enumeration type,
5823 install their decls in the enumeration type and finish it off.
5824 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5825 and ATTRIBUTES are the specified attributes.
5826 Returns ENUMTYPE. */
5828 tree
5829 finish_enum (tree enumtype, tree values, tree attributes)
5831 tree pair, tem;
5832 tree minnode = 0, maxnode = 0;
5833 int precision, unsign;
5834 bool toplevel = (file_scope == current_scope);
5835 struct lang_type *lt;
5837 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5839 /* Calculate the maximum value of any enumerator in this type. */
5841 if (values == error_mark_node)
5842 minnode = maxnode = integer_zero_node;
5843 else
5845 minnode = maxnode = TREE_VALUE (values);
5846 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5848 tree value = TREE_VALUE (pair);
5849 if (tree_int_cst_lt (maxnode, value))
5850 maxnode = value;
5851 if (tree_int_cst_lt (value, minnode))
5852 minnode = value;
5856 /* Construct the final type of this enumeration. It is the same
5857 as one of the integral types - the narrowest one that fits, except
5858 that normally we only go as narrow as int - and signed iff any of
5859 the values are negative. */
5860 unsign = (tree_int_cst_sgn (minnode) >= 0);
5861 precision = MAX (min_precision (minnode, unsign),
5862 min_precision (maxnode, unsign));
5864 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5866 tem = c_common_type_for_size (precision, unsign);
5867 if (tem == NULL)
5869 warning (0, "enumeration values exceed range of largest integer");
5870 tem = long_long_integer_type_node;
5873 else
5874 tem = unsign ? unsigned_type_node : integer_type_node;
5876 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5877 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5878 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5879 TYPE_SIZE (enumtype) = 0;
5881 /* If the precision of the type was specific with an attribute and it
5882 was too small, give an error. Otherwise, use it. */
5883 if (TYPE_PRECISION (enumtype))
5885 if (precision > TYPE_PRECISION (enumtype))
5886 error ("specified mode too small for enumeral values");
5888 else
5889 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5891 layout_type (enumtype);
5893 if (values != error_mark_node)
5895 /* Change the type of the enumerators to be the enum type. We
5896 need to do this irrespective of the size of the enum, for
5897 proper type checking. Replace the DECL_INITIALs of the
5898 enumerators, and the value slots of the list, with copies
5899 that have the enum type; they cannot be modified in place
5900 because they may be shared (e.g. integer_zero_node) Finally,
5901 change the purpose slots to point to the names of the decls. */
5902 for (pair = values; pair; pair = TREE_CHAIN (pair))
5904 tree enu = TREE_PURPOSE (pair);
5905 tree ini = DECL_INITIAL (enu);
5907 TREE_TYPE (enu) = enumtype;
5909 /* The ISO C Standard mandates enumerators to have type int,
5910 even though the underlying type of an enum type is
5911 unspecified. Here we convert any enumerators that fit in
5912 an int to type int, to avoid promotions to unsigned types
5913 when comparing integers with enumerators that fit in the
5914 int range. When -pedantic is given, build_enumerator()
5915 would have already taken care of those that don't fit. */
5916 if (int_fits_type_p (ini, integer_type_node))
5917 tem = integer_type_node;
5918 else
5919 tem = enumtype;
5920 ini = convert (tem, ini);
5922 DECL_INITIAL (enu) = ini;
5923 TREE_PURPOSE (pair) = DECL_NAME (enu);
5924 TREE_VALUE (pair) = ini;
5927 TYPE_VALUES (enumtype) = values;
5930 /* Record the min/max values so that we can warn about bit-field
5931 enumerations that are too small for the values. */
5932 lt = GGC_CNEW (struct lang_type);
5933 lt->enum_min = minnode;
5934 lt->enum_max = maxnode;
5935 TYPE_LANG_SPECIFIC (enumtype) = lt;
5937 /* Fix up all variant types of this enum type. */
5938 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5940 if (tem == enumtype)
5941 continue;
5942 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5943 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5944 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5945 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5946 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5947 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5948 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5949 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5950 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5951 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5952 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5955 /* Finish debugging output for this type. */
5956 rest_of_type_compilation (enumtype, toplevel);
5958 return enumtype;
5961 /* Build and install a CONST_DECL for one value of the
5962 current enumeration type (one that was begun with start_enum).
5963 Return a tree-list containing the CONST_DECL and its value.
5964 Assignment of sequential values by default is handled here. */
5966 tree
5967 build_enumerator (tree name, tree value)
5969 tree decl, type;
5971 /* Validate and default VALUE. */
5973 if (value != 0)
5975 /* Don't issue more errors for error_mark_node (i.e. an
5976 undeclared identifier) - just ignore the value expression. */
5977 if (value == error_mark_node)
5978 value = 0;
5979 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5980 || TREE_CODE (value) != INTEGER_CST)
5982 error ("enumerator value for %qE is not an integer constant", name);
5983 value = 0;
5985 else
5987 value = default_conversion (value);
5988 constant_expression_warning (value);
5992 /* Default based on previous value. */
5993 /* It should no longer be possible to have NON_LVALUE_EXPR
5994 in the default. */
5995 if (value == 0)
5997 value = enum_next_value;
5998 if (enum_overflow)
5999 error ("overflow in enumeration values");
6002 if (pedantic && !int_fits_type_p (value, integer_type_node))
6004 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
6005 /* XXX This causes -pedantic to change the meaning of the program.
6006 Remove? -zw 2004-03-15 */
6007 value = convert (integer_type_node, value);
6010 /* Set basis for default for next value. */
6011 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
6012 enum_overflow = tree_int_cst_lt (enum_next_value, value);
6014 /* Now create a declaration for the enum value name. */
6016 type = TREE_TYPE (value);
6017 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6018 TYPE_PRECISION (integer_type_node)),
6019 (TYPE_PRECISION (type)
6020 >= TYPE_PRECISION (integer_type_node)
6021 && TYPE_UNSIGNED (type)));
6023 decl = build_decl (CONST_DECL, name, type);
6024 DECL_INITIAL (decl) = convert (type, value);
6025 pushdecl (decl);
6027 return tree_cons (decl, value, NULL_TREE);
6031 /* Create the FUNCTION_DECL for a function definition.
6032 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6033 the declaration; they describe the function's name and the type it returns,
6034 but twisted together in a fashion that parallels the syntax of C.
6036 This function creates a binding context for the function body
6037 as well as setting up the FUNCTION_DECL in current_function_decl.
6039 Returns 1 on success. If the DECLARATOR is not suitable for a function
6040 (it defines a datum instead), we return 0, which tells
6041 yyparse to report a parse error. */
6044 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6045 tree attributes)
6047 tree decl1, old_decl;
6048 tree restype, resdecl;
6049 struct c_label_context_se *nstack_se;
6050 struct c_label_context_vm *nstack_vm;
6052 current_function_returns_value = 0; /* Assume, until we see it does. */
6053 current_function_returns_null = 0;
6054 current_function_returns_abnormally = 0;
6055 warn_about_return_type = 0;
6056 c_switch_stack = NULL;
6058 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6059 nstack_se->labels_def = NULL;
6060 nstack_se->labels_used = NULL;
6061 nstack_se->next = label_context_stack_se;
6062 label_context_stack_se = nstack_se;
6064 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6065 nstack_vm->labels_def = NULL;
6066 nstack_vm->labels_used = NULL;
6067 nstack_vm->scope = 0;
6068 nstack_vm->next = label_context_stack_vm;
6069 label_context_stack_vm = nstack_vm;
6071 /* Indicate no valid break/continue context by setting these variables
6072 to some non-null, non-label value. We'll notice and emit the proper
6073 error message in c_finish_bc_stmt. */
6074 c_break_label = c_cont_label = size_zero_node;
6076 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
6078 /* If the declarator is not suitable for a function definition,
6079 cause a syntax error. */
6080 if (decl1 == 0)
6082 label_context_stack_se = label_context_stack_se->next;
6083 label_context_stack_vm = label_context_stack_vm->next;
6084 return 0;
6087 decl_attributes (&decl1, attributes, 0);
6089 if (DECL_DECLARED_INLINE_P (decl1)
6090 && DECL_UNINLINABLE (decl1)
6091 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6092 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6093 decl1);
6095 #if WANT_C99_INLINE_SEMANTICS
6096 /* Handle gnu_inline attribute. */
6097 if (declspecs->inline_p
6098 && flag_isoc99
6099 && TREE_CODE (decl1) == FUNCTION_DECL
6100 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1)))
6102 if (declspecs->storage_class != csc_static)
6103 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6105 #endif /* WANT_C99_INLINE_SEMANTICS */
6107 announce_function (decl1);
6109 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6111 error ("return type is an incomplete type");
6112 /* Make it return void instead. */
6113 TREE_TYPE (decl1)
6114 = build_function_type (void_type_node,
6115 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6118 if (warn_about_return_type)
6119 pedwarn_c99 ("return type defaults to %<int%>");
6121 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6122 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6123 DECL_INITIAL (decl1) = error_mark_node;
6125 /* If this definition isn't a prototype and we had a prototype declaration
6126 before, copy the arg type info from that prototype. */
6127 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6128 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6129 old_decl = 0;
6130 current_function_prototype_locus = UNKNOWN_LOCATION;
6131 current_function_prototype_built_in = false;
6132 current_function_prototype_arg_types = NULL_TREE;
6133 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6135 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6136 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6137 TREE_TYPE (TREE_TYPE (old_decl))))
6139 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6140 TREE_TYPE (decl1));
6141 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6142 current_function_prototype_built_in
6143 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6144 current_function_prototype_arg_types
6145 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6147 if (TREE_PUBLIC (decl1))
6149 /* If there is an external prototype declaration of this
6150 function, record its location but do not copy information
6151 to this decl. This may be an invisible declaration
6152 (built-in or in a scope which has finished) or simply
6153 have more refined argument types than any declaration
6154 found above. */
6155 struct c_binding *b;
6156 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6157 if (B_IN_SCOPE (b, external_scope))
6158 break;
6159 if (b)
6161 tree ext_decl, ext_type;
6162 ext_decl = b->decl;
6163 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6164 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6165 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6166 TREE_TYPE (ext_type)))
6168 current_function_prototype_locus
6169 = DECL_SOURCE_LOCATION (ext_decl);
6170 current_function_prototype_built_in
6171 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6172 current_function_prototype_arg_types
6173 = TYPE_ARG_TYPES (ext_type);
6179 /* Optionally warn of old-fashioned def with no previous prototype. */
6180 if (warn_strict_prototypes
6181 && old_decl != error_mark_node
6182 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6183 && C_DECL_ISNT_PROTOTYPE (old_decl))
6184 warning (OPT_Wstrict_prototypes,
6185 "function declaration isn%'t a prototype");
6186 /* Optionally warn of any global def with no previous prototype. */
6187 else if (warn_missing_prototypes
6188 && old_decl != error_mark_node
6189 && TREE_PUBLIC (decl1)
6190 && !MAIN_NAME_P (DECL_NAME (decl1))
6191 && C_DECL_ISNT_PROTOTYPE (old_decl))
6192 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6193 /* Optionally warn of any def with no previous prototype
6194 if the function has already been used. */
6195 else if (warn_missing_prototypes
6196 && old_decl != 0
6197 && old_decl != error_mark_node
6198 && TREE_USED (old_decl)
6199 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6200 warning (OPT_Wmissing_prototypes,
6201 "%q+D was used with no prototype before its definition", decl1);
6202 /* Optionally warn of any global def with no previous declaration. */
6203 else if (warn_missing_declarations
6204 && TREE_PUBLIC (decl1)
6205 && old_decl == 0
6206 && !MAIN_NAME_P (DECL_NAME (decl1)))
6207 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6208 decl1);
6209 /* Optionally warn of any def with no previous declaration
6210 if the function has already been used. */
6211 else if (warn_missing_declarations
6212 && old_decl != 0
6213 && old_decl != error_mark_node
6214 && TREE_USED (old_decl)
6215 && C_DECL_IMPLICIT (old_decl))
6216 warning (OPT_Wmissing_declarations,
6217 "%q+D was used with no declaration before its definition", decl1);
6219 /* This function exists in static storage.
6220 (This does not mean `static' in the C sense!) */
6221 TREE_STATIC (decl1) = 1;
6223 /* A nested function is not global. */
6224 if (current_function_decl != 0)
6225 TREE_PUBLIC (decl1) = 0;
6227 /* This is the earliest point at which we might know the assembler
6228 name of the function. Thus, if it's set before this, die horribly. */
6229 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6231 /* If #pragma weak was used, mark the decl weak now. */
6232 if (current_scope == file_scope)
6233 maybe_apply_pragma_weak (decl1);
6235 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6236 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6238 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6239 != integer_type_node)
6240 pedwarn ("return type of %q+D is not %<int%>", decl1);
6242 check_main_parameter_types(decl1);
6244 if (!TREE_PUBLIC (decl1))
6245 pedwarn ("%q+D is normally a non-static function", decl1);
6248 /* Record the decl so that the function name is defined.
6249 If we already have a decl for this name, and it is a FUNCTION_DECL,
6250 use the old decl. */
6252 current_function_decl = pushdecl (decl1);
6254 push_scope ();
6255 declare_parm_level ();
6257 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6258 /* Promote the value to int before returning it. */
6259 if (c_promoting_integer_type_p (restype))
6261 /* It retains unsignedness if not really getting wider. */
6262 if (TYPE_UNSIGNED (restype)
6263 && (TYPE_PRECISION (restype)
6264 == TYPE_PRECISION (integer_type_node)))
6265 restype = unsigned_type_node;
6266 else
6267 restype = integer_type_node;
6270 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6271 DECL_ARTIFICIAL (resdecl) = 1;
6272 DECL_IGNORED_P (resdecl) = 1;
6273 DECL_RESULT (current_function_decl) = resdecl;
6275 start_fname_decls ();
6277 return 1;
6280 /* Subroutine of store_parm_decls which handles new-style function
6281 definitions (prototype format). The parms already have decls, so we
6282 need only record them as in effect and complain if any redundant
6283 old-style parm decls were written. */
6284 static void
6285 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6287 tree decl;
6289 if (current_scope->bindings)
6291 error ("%Jold-style parameter declarations in prototyped "
6292 "function definition", fndecl);
6294 /* Get rid of the old-style declarations. */
6295 pop_scope ();
6296 push_scope ();
6298 /* Don't issue this warning for nested functions, and don't issue this
6299 warning if we got here because ARG_INFO_TYPES was error_mark_node
6300 (this happens when a function definition has just an ellipsis in
6301 its parameter list). */
6302 else if (!in_system_header && !current_function_scope
6303 && arg_info->types != error_mark_node)
6304 warning (OPT_Wtraditional,
6305 "%Jtraditional C rejects ISO C style function definitions",
6306 fndecl);
6308 /* Now make all the parameter declarations visible in the function body.
6309 We can bypass most of the grunt work of pushdecl. */
6310 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6312 DECL_CONTEXT (decl) = current_function_decl;
6313 if (DECL_NAME (decl))
6315 bind (DECL_NAME (decl), decl, current_scope,
6316 /*invisible=*/false, /*nested=*/false);
6317 if (!TREE_USED (decl))
6318 warn_if_shadowing (decl);
6320 else
6321 error ("%Jparameter name omitted", decl);
6324 /* Record the parameter list in the function declaration. */
6325 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6327 /* Now make all the ancillary declarations visible, likewise. */
6328 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6330 DECL_CONTEXT (decl) = current_function_decl;
6331 if (DECL_NAME (decl))
6332 bind (DECL_NAME (decl), decl, current_scope,
6333 /*invisible=*/false, /*nested=*/false);
6336 /* And all the tag declarations. */
6337 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6338 if (TREE_PURPOSE (decl))
6339 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6340 /*invisible=*/false, /*nested=*/false);
6343 /* Subroutine of store_parm_decls which handles old-style function
6344 definitions (separate parameter list and declarations). */
6346 static void
6347 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6349 struct c_binding *b;
6350 tree parm, decl, last;
6351 tree parmids = arg_info->parms;
6352 struct pointer_set_t *seen_args = pointer_set_create ();
6354 if (!in_system_header)
6355 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6356 fndecl);
6358 /* Match each formal parameter name with its declaration. Save each
6359 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6360 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6362 if (TREE_VALUE (parm) == 0)
6364 error ("%Jparameter name missing from parameter list", fndecl);
6365 TREE_PURPOSE (parm) = 0;
6366 continue;
6369 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6370 if (b && B_IN_CURRENT_SCOPE (b))
6372 decl = b->decl;
6373 /* If we got something other than a PARM_DECL it is an error. */
6374 if (TREE_CODE (decl) != PARM_DECL)
6375 error ("%q+D declared as a non-parameter", decl);
6376 /* If the declaration is already marked, we have a duplicate
6377 name. Complain and ignore the duplicate. */
6378 else if (pointer_set_contains (seen_args, decl))
6380 error ("multiple parameters named %q+D", decl);
6381 TREE_PURPOSE (parm) = 0;
6382 continue;
6384 /* If the declaration says "void", complain and turn it into
6385 an int. */
6386 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6388 error ("parameter %q+D declared with void type", decl);
6389 TREE_TYPE (decl) = integer_type_node;
6390 DECL_ARG_TYPE (decl) = integer_type_node;
6391 layout_decl (decl, 0);
6393 warn_if_shadowing (decl);
6395 /* If no declaration found, default to int. */
6396 else
6398 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6399 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6400 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6401 pushdecl (decl);
6402 warn_if_shadowing (decl);
6404 if (flag_isoc99)
6405 pedwarn ("type of %q+D defaults to %<int%>", decl);
6406 else
6407 warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
6410 TREE_PURPOSE (parm) = decl;
6411 pointer_set_insert (seen_args, decl);
6414 /* Now examine the parms chain for incomplete declarations
6415 and declarations with no corresponding names. */
6417 for (b = current_scope->bindings; b; b = b->prev)
6419 parm = b->decl;
6420 if (TREE_CODE (parm) != PARM_DECL)
6421 continue;
6423 if (TREE_TYPE (parm) != error_mark_node
6424 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6426 error ("parameter %q+D has incomplete type", parm);
6427 TREE_TYPE (parm) = error_mark_node;
6430 if (!pointer_set_contains (seen_args, parm))
6432 error ("declaration for parameter %q+D but no such parameter", parm);
6434 /* Pretend the parameter was not missing.
6435 This gets us to a standard state and minimizes
6436 further error messages. */
6437 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6441 /* Chain the declarations together in the order of the list of
6442 names. Store that chain in the function decl, replacing the
6443 list of names. Update the current scope to match. */
6444 DECL_ARGUMENTS (fndecl) = 0;
6446 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6447 if (TREE_PURPOSE (parm))
6448 break;
6449 if (parm && TREE_PURPOSE (parm))
6451 last = TREE_PURPOSE (parm);
6452 DECL_ARGUMENTS (fndecl) = last;
6454 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6455 if (TREE_PURPOSE (parm))
6457 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6458 last = TREE_PURPOSE (parm);
6460 TREE_CHAIN (last) = 0;
6463 pointer_set_destroy (seen_args);
6465 /* If there was a previous prototype,
6466 set the DECL_ARG_TYPE of each argument according to
6467 the type previously specified, and report any mismatches. */
6469 if (current_function_prototype_arg_types)
6471 tree type;
6472 for (parm = DECL_ARGUMENTS (fndecl),
6473 type = current_function_prototype_arg_types;
6474 parm || (type && TREE_VALUE (type) != error_mark_node
6475 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
6476 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6478 if (parm == 0 || type == 0
6479 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6481 if (current_function_prototype_built_in)
6482 warning (0, "number of arguments doesn%'t match "
6483 "built-in prototype");
6484 else
6486 error ("number of arguments doesn%'t match prototype");
6487 error ("%Hprototype declaration",
6488 &current_function_prototype_locus);
6490 break;
6492 /* Type for passing arg must be consistent with that
6493 declared for the arg. ISO C says we take the unqualified
6494 type for parameters declared with qualified type. */
6495 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6496 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6498 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6499 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6501 /* Adjust argument to match prototype. E.g. a previous
6502 `int foo(float);' prototype causes
6503 `int foo(x) float x; {...}' to be treated like
6504 `int foo(float x) {...}'. This is particularly
6505 useful for argument types like uid_t. */
6506 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6508 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6509 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6510 && TYPE_PRECISION (TREE_TYPE (parm))
6511 < TYPE_PRECISION (integer_type_node))
6512 DECL_ARG_TYPE (parm) = integer_type_node;
6514 if (pedantic)
6516 /* ??? Is it possible to get here with a
6517 built-in prototype or will it always have
6518 been diagnosed as conflicting with an
6519 old-style definition and discarded? */
6520 if (current_function_prototype_built_in)
6521 warning (0, "promoted argument %qD "
6522 "doesn%'t match built-in prototype", parm);
6523 else
6525 pedwarn ("promoted argument %qD "
6526 "doesn%'t match prototype", parm);
6527 pedwarn ("%Hprototype declaration",
6528 &current_function_prototype_locus);
6532 else
6534 if (current_function_prototype_built_in)
6535 warning (0, "argument %qD doesn%'t match "
6536 "built-in prototype", parm);
6537 else
6539 error ("argument %qD doesn%'t match prototype", parm);
6540 error ("%Hprototype declaration",
6541 &current_function_prototype_locus);
6546 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6549 /* Otherwise, create a prototype that would match. */
6551 else
6553 tree actual = 0, last = 0, type;
6555 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6557 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6558 if (last)
6559 TREE_CHAIN (last) = type;
6560 else
6561 actual = type;
6562 last = type;
6564 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6565 if (last)
6566 TREE_CHAIN (last) = type;
6567 else
6568 actual = type;
6570 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6571 of the type of this function, but we need to avoid having this
6572 affect the types of other similarly-typed functions, so we must
6573 first force the generation of an identical (but separate) type
6574 node for the relevant function type. The new node we create
6575 will be a variant of the main variant of the original function
6576 type. */
6578 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6580 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6584 /* Store parameter declarations passed in ARG_INFO into the current
6585 function declaration. */
6587 void
6588 store_parm_decls_from (struct c_arg_info *arg_info)
6590 current_function_arg_info = arg_info;
6591 store_parm_decls ();
6594 /* Store the parameter declarations into the current function declaration.
6595 This is called after parsing the parameter declarations, before
6596 digesting the body of the function.
6598 For an old-style definition, construct a prototype out of the old-style
6599 parameter declarations and inject it into the function's type. */
6601 void
6602 store_parm_decls (void)
6604 tree fndecl = current_function_decl;
6605 bool proto;
6607 /* The argument information block for FNDECL. */
6608 struct c_arg_info *arg_info = current_function_arg_info;
6609 current_function_arg_info = 0;
6611 /* True if this definition is written with a prototype. Note:
6612 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6613 list in a function definition as equivalent to (void) -- an
6614 empty argument list specifies the function has no parameters,
6615 but only (void) sets up a prototype for future calls. */
6616 proto = arg_info->types != 0;
6618 if (proto)
6619 store_parm_decls_newstyle (fndecl, arg_info);
6620 else
6621 store_parm_decls_oldstyle (fndecl, arg_info);
6623 /* The next call to push_scope will be a function body. */
6625 next_is_function_body = true;
6627 /* Write a record describing this function definition to the prototypes
6628 file (if requested). */
6630 gen_aux_info_record (fndecl, 1, 0, proto);
6632 /* Initialize the RTL code for the function. */
6633 allocate_struct_function (fndecl);
6635 /* Begin the statement tree for this function. */
6636 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6638 /* ??? Insert the contents of the pending sizes list into the function
6639 to be evaluated. The only reason left to have this is
6640 void foo(int n, int array[n++])
6641 because we throw away the array type in favor of a pointer type, and
6642 thus won't naturally see the SAVE_EXPR containing the increment. All
6643 other pending sizes would be handled by gimplify_parameters. */
6645 tree t;
6646 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6647 add_stmt (TREE_VALUE (t));
6650 /* Even though we're inside a function body, we still don't want to
6651 call expand_expr to calculate the size of a variable-sized array.
6652 We haven't necessarily assigned RTL to all variables yet, so it's
6653 not safe to try to expand expressions involving them. */
6654 cfun->x_dont_save_pending_sizes_p = 1;
6657 /* Emit diagnostics that require gimple input for detection. Operate on
6658 FNDECL and all its nested functions. */
6660 static void
6661 c_gimple_diagnostics_recursively (tree fndecl)
6663 struct cgraph_node *cgn;
6665 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6666 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6668 /* Notice when OpenMP structured block constraints are violated. */
6669 if (flag_openmp)
6670 diagnose_omp_structured_block_errors (fndecl);
6672 /* Finalize all nested functions now. */
6673 cgn = cgraph_node (fndecl);
6674 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6675 c_gimple_diagnostics_recursively (cgn->decl);
6678 /* Finish up a function declaration and compile that function
6679 all the way to assembler language output. The free the storage
6680 for the function definition.
6682 This is called after parsing the body of the function definition. */
6684 void
6685 finish_function (void)
6687 tree fndecl = current_function_decl;
6689 label_context_stack_se = label_context_stack_se->next;
6690 label_context_stack_vm = label_context_stack_vm->next;
6692 if (TREE_CODE (fndecl) == FUNCTION_DECL
6693 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6695 tree args = DECL_ARGUMENTS (fndecl);
6696 for (; args; args = TREE_CHAIN (args))
6698 tree type = TREE_TYPE (args);
6699 if (INTEGRAL_TYPE_P (type)
6700 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6701 DECL_ARG_TYPE (args) = integer_type_node;
6705 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6706 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6708 /* Must mark the RESULT_DECL as being in this function. */
6710 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6711 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6713 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6715 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6716 != integer_type_node)
6718 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6719 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6720 if (!warn_main)
6721 pedwarn ("return type of %q+D is not %<int%>", fndecl);
6723 else
6725 if (flag_isoc99)
6727 tree stmt = c_finish_return (integer_zero_node);
6728 #ifdef USE_MAPPED_LOCATION
6729 /* Hack. We don't want the middle-end to warn that this return
6730 is unreachable, so we mark its location as special. Using
6731 UNKNOWN_LOCATION has the problem that it gets clobbered in
6732 annotate_one_with_locus. A cleaner solution might be to
6733 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6735 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6736 #else
6737 /* Hack. We don't want the middle-end to warn that this
6738 return is unreachable, so put the statement on the
6739 special line 0. */
6740 annotate_with_file_line (stmt, input_filename, 0);
6741 #endif
6746 /* Tie off the statement tree for this function. */
6747 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6749 finish_fname_decls ();
6751 /* Complain if there's just no return statement. */
6752 if (warn_return_type
6753 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6754 && !current_function_returns_value && !current_function_returns_null
6755 /* Don't complain if we are no-return. */
6756 && !current_function_returns_abnormally
6757 /* Don't warn for main(). */
6758 && !MAIN_NAME_P (DECL_NAME (fndecl))
6759 /* Or if they didn't actually specify a return type. */
6760 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6761 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6762 inline function, as we might never be compiled separately. */
6763 && DECL_INLINE (fndecl))
6765 warning (OPT_Wreturn_type,
6766 "no return statement in function returning non-void");
6767 TREE_NO_WARNING (fndecl) = 1;
6770 /* With just -Wextra, complain only if function returns both with
6771 and without a value. */
6772 if (extra_warnings
6773 && current_function_returns_value
6774 && current_function_returns_null)
6775 warning (OPT_Wextra, "this function may return with or without a value");
6777 /* Store the end of the function, so that we get good line number
6778 info for the epilogue. */
6779 cfun->function_end_locus = input_location;
6781 /* If we don't have ctors/dtors sections, and this is a static
6782 constructor or destructor, it must be recorded now. */
6783 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6784 && !targetm.have_ctors_dtors)
6785 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6786 if (DECL_STATIC_DESTRUCTOR (fndecl)
6787 && !targetm.have_ctors_dtors)
6788 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6790 /* Finalize the ELF visibility for the function. */
6791 c_determine_visibility (fndecl);
6793 /* Genericize before inlining. Delay genericizing nested functions
6794 until their parent function is genericized. Since finalizing
6795 requires GENERIC, delay that as well. */
6797 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6798 && !undef_nested_function)
6800 if (!decl_function_context (fndecl))
6802 c_genericize (fndecl);
6803 c_gimple_diagnostics_recursively (fndecl);
6805 /* ??? Objc emits functions after finalizing the compilation unit.
6806 This should be cleaned up later and this conditional removed. */
6807 if (cgraph_global_info_ready)
6809 c_expand_body (fndecl);
6810 return;
6813 cgraph_finalize_function (fndecl, false);
6815 else
6817 /* Register this function with cgraph just far enough to get it
6818 added to our parent's nested function list. Handy, since the
6819 C front end doesn't have such a list. */
6820 (void) cgraph_node (fndecl);
6824 if (!decl_function_context (fndecl))
6825 undef_nested_function = false;
6827 /* We're leaving the context of this function, so zap cfun.
6828 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6829 tree_rest_of_compilation. */
6830 cfun = NULL;
6831 current_function_decl = NULL;
6834 /* Generate the RTL for the body of FNDECL. */
6836 void
6837 c_expand_body (tree fndecl)
6840 if (!DECL_INITIAL (fndecl)
6841 || DECL_INITIAL (fndecl) == error_mark_node)
6842 return;
6844 tree_rest_of_compilation (fndecl);
6846 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6847 && targetm.have_ctors_dtors)
6848 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6849 DEFAULT_INIT_PRIORITY);
6850 if (DECL_STATIC_DESTRUCTOR (fndecl)
6851 && targetm.have_ctors_dtors)
6852 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6853 DEFAULT_INIT_PRIORITY);
6856 /* Check the declarations given in a for-loop for satisfying the C99
6857 constraints. If exactly one such decl is found, return it. */
6859 tree
6860 check_for_loop_decls (void)
6862 struct c_binding *b;
6863 tree one_decl = NULL_TREE;
6864 int n_decls = 0;
6867 if (!flag_isoc99)
6869 /* If we get here, declarations have been used in a for loop without
6870 the C99 for loop scope. This doesn't make much sense, so don't
6871 allow it. */
6872 error ("%<for%> loop initial declaration used outside C99 mode");
6873 return NULL_TREE;
6875 /* C99 subclause 6.8.5 paragraph 3:
6877 [#3] The declaration part of a for statement shall only
6878 declare identifiers for objects having storage class auto or
6879 register.
6881 It isn't clear whether, in this sentence, "identifiers" binds to
6882 "shall only declare" or to "objects" - that is, whether all identifiers
6883 declared must be identifiers for objects, or whether the restriction
6884 only applies to those that are. (A question on this in comp.std.c
6885 in November 2000 received no answer.) We implement the strictest
6886 interpretation, to avoid creating an extension which later causes
6887 problems. */
6889 for (b = current_scope->bindings; b; b = b->prev)
6891 tree id = b->id;
6892 tree decl = b->decl;
6894 if (!id)
6895 continue;
6897 switch (TREE_CODE (decl))
6899 case VAR_DECL:
6900 if (TREE_STATIC (decl))
6901 error ("declaration of static variable %q+D in %<for%> loop "
6902 "initial declaration", decl);
6903 else if (DECL_EXTERNAL (decl))
6904 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6905 "initial declaration", decl);
6906 break;
6908 case RECORD_TYPE:
6909 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6910 id);
6911 break;
6912 case UNION_TYPE:
6913 error ("%<union %E%> declared in %<for%> loop initial declaration",
6914 id);
6915 break;
6916 case ENUMERAL_TYPE:
6917 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6918 id);
6919 break;
6920 default:
6921 error ("declaration of non-variable %q+D in %<for%> loop "
6922 "initial declaration", decl);
6925 n_decls++;
6926 one_decl = decl;
6929 return n_decls == 1 ? one_decl : NULL_TREE;
6932 /* Save and reinitialize the variables
6933 used during compilation of a C function. */
6935 void
6936 c_push_function_context (struct function *f)
6938 struct language_function *p;
6939 p = GGC_NEW (struct language_function);
6940 f->language = p;
6942 p->base.x_stmt_tree = c_stmt_tree;
6943 p->x_break_label = c_break_label;
6944 p->x_cont_label = c_cont_label;
6945 p->x_switch_stack = c_switch_stack;
6946 p->arg_info = current_function_arg_info;
6947 p->returns_value = current_function_returns_value;
6948 p->returns_null = current_function_returns_null;
6949 p->returns_abnormally = current_function_returns_abnormally;
6950 p->warn_about_return_type = warn_about_return_type;
6953 /* Restore the variables used during compilation of a C function. */
6955 void
6956 c_pop_function_context (struct function *f)
6958 struct language_function *p = f->language;
6960 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6961 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6963 /* Stop pointing to the local nodes about to be freed. */
6964 /* But DECL_INITIAL must remain nonzero so we know this
6965 was an actual function definition. */
6966 DECL_INITIAL (current_function_decl) = error_mark_node;
6967 DECL_ARGUMENTS (current_function_decl) = 0;
6970 c_stmt_tree = p->base.x_stmt_tree;
6971 c_break_label = p->x_break_label;
6972 c_cont_label = p->x_cont_label;
6973 c_switch_stack = p->x_switch_stack;
6974 current_function_arg_info = p->arg_info;
6975 current_function_returns_value = p->returns_value;
6976 current_function_returns_null = p->returns_null;
6977 current_function_returns_abnormally = p->returns_abnormally;
6978 warn_about_return_type = p->warn_about_return_type;
6980 f->language = NULL;
6983 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6985 void
6986 c_dup_lang_specific_decl (tree decl)
6988 struct lang_decl *ld;
6990 if (!DECL_LANG_SPECIFIC (decl))
6991 return;
6993 ld = GGC_NEW (struct lang_decl);
6994 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6995 DECL_LANG_SPECIFIC (decl) = ld;
6998 /* The functions below are required for functionality of doing
6999 function at once processing in the C front end. Currently these
7000 functions are not called from anywhere in the C front end, but as
7001 these changes continue, that will change. */
7003 /* Returns the stmt_tree (if any) to which statements are currently
7004 being added. If there is no active statement-tree, NULL is
7005 returned. */
7007 stmt_tree
7008 current_stmt_tree (void)
7010 return &c_stmt_tree;
7013 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
7014 C. */
7017 anon_aggr_type_p (tree ARG_UNUSED (node))
7019 return 0;
7022 /* Return the global value of T as a symbol. */
7024 tree
7025 identifier_global_value (tree t)
7027 struct c_binding *b;
7029 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7030 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7031 return b->decl;
7033 return 0;
7036 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7037 otherwise the name is found in ridpointers from RID_INDEX. */
7039 void
7040 record_builtin_type (enum rid rid_index, const char *name, tree type)
7042 tree id, decl;
7043 if (name == 0)
7044 id = ridpointers[(int) rid_index];
7045 else
7046 id = get_identifier (name);
7047 decl = build_decl (TYPE_DECL, id, type);
7048 pushdecl (decl);
7049 if (debug_hooks->type_decl)
7050 debug_hooks->type_decl (decl, false);
7053 /* Build the void_list_node (void_type_node having been created). */
7054 tree
7055 build_void_list_node (void)
7057 tree t = build_tree_list (NULL_TREE, void_type_node);
7058 return t;
7061 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7063 struct c_parm *
7064 build_c_parm (struct c_declspecs *specs, tree attrs,
7065 struct c_declarator *declarator)
7067 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7068 ret->specs = specs;
7069 ret->attrs = attrs;
7070 ret->declarator = declarator;
7071 return ret;
7074 /* Return a declarator with nested attributes. TARGET is the inner
7075 declarator to which these attributes apply. ATTRS are the
7076 attributes. */
7078 struct c_declarator *
7079 build_attrs_declarator (tree attrs, struct c_declarator *target)
7081 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7082 ret->kind = cdk_attrs;
7083 ret->declarator = target;
7084 ret->u.attrs = attrs;
7085 return ret;
7088 /* Return a declarator for a function with arguments specified by ARGS
7089 and return type specified by TARGET. */
7091 struct c_declarator *
7092 build_function_declarator (struct c_arg_info *args,
7093 struct c_declarator *target)
7095 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7096 ret->kind = cdk_function;
7097 ret->declarator = target;
7098 ret->u.arg_info = args;
7099 return ret;
7102 /* Return a declarator for the identifier IDENT (which may be
7103 NULL_TREE for an abstract declarator). */
7105 struct c_declarator *
7106 build_id_declarator (tree ident)
7108 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7109 ret->kind = cdk_id;
7110 ret->declarator = 0;
7111 ret->u.id = ident;
7112 /* Default value - may get reset to a more precise location. */
7113 ret->id_loc = input_location;
7114 return ret;
7117 /* Return something to represent absolute declarators containing a *.
7118 TARGET is the absolute declarator that the * contains.
7119 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7120 to apply to the pointer type. */
7122 struct c_declarator *
7123 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7124 struct c_declarator *target)
7126 tree attrs;
7127 int quals = 0;
7128 struct c_declarator *itarget = target;
7129 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7130 if (type_quals_attrs)
7132 attrs = type_quals_attrs->attrs;
7133 quals = quals_from_declspecs (type_quals_attrs);
7134 if (attrs != NULL_TREE)
7135 itarget = build_attrs_declarator (attrs, target);
7137 ret->kind = cdk_pointer;
7138 ret->declarator = itarget;
7139 ret->u.pointer_quals = quals;
7140 return ret;
7143 /* Return a pointer to a structure for an empty list of declaration
7144 specifiers. */
7146 struct c_declspecs *
7147 build_null_declspecs (void)
7149 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7150 ret->type = 0;
7151 ret->decl_attr = 0;
7152 ret->attrs = 0;
7153 ret->typespec_word = cts_none;
7154 ret->storage_class = csc_none;
7155 ret->declspecs_seen_p = false;
7156 ret->type_seen_p = false;
7157 ret->non_sc_seen_p = false;
7158 ret->typedef_p = false;
7159 ret->tag_defined_p = false;
7160 ret->explicit_signed_p = false;
7161 ret->deprecated_p = false;
7162 ret->default_int_p = false;
7163 ret->long_p = false;
7164 ret->long_long_p = false;
7165 ret->short_p = false;
7166 ret->signed_p = false;
7167 ret->unsigned_p = false;
7168 ret->complex_p = false;
7169 ret->inline_p = false;
7170 ret->thread_p = false;
7171 ret->const_p = false;
7172 ret->volatile_p = false;
7173 ret->restrict_p = false;
7174 return ret;
7177 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7178 returning SPECS. */
7180 struct c_declspecs *
7181 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7183 enum rid i;
7184 bool dupe = false;
7185 specs->non_sc_seen_p = true;
7186 specs->declspecs_seen_p = true;
7187 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7188 && C_IS_RESERVED_WORD (qual));
7189 i = C_RID_CODE (qual);
7190 switch (i)
7192 case RID_CONST:
7193 dupe = specs->const_p;
7194 specs->const_p = true;
7195 break;
7196 case RID_VOLATILE:
7197 dupe = specs->volatile_p;
7198 specs->volatile_p = true;
7199 break;
7200 case RID_RESTRICT:
7201 dupe = specs->restrict_p;
7202 specs->restrict_p = true;
7203 break;
7204 default:
7205 gcc_unreachable ();
7207 if (dupe && pedantic && !flag_isoc99)
7208 pedwarn ("duplicate %qE", qual);
7209 return specs;
7212 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7213 returning SPECS. */
7215 struct c_declspecs *
7216 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7218 tree type = spec.spec;
7219 specs->non_sc_seen_p = true;
7220 specs->declspecs_seen_p = true;
7221 specs->type_seen_p = true;
7222 if (TREE_DEPRECATED (type))
7223 specs->deprecated_p = true;
7225 /* Handle type specifier keywords. */
7226 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
7228 enum rid i = C_RID_CODE (type);
7229 if (specs->type)
7231 error ("two or more data types in declaration specifiers");
7232 return specs;
7234 if ((int) i <= (int) RID_LAST_MODIFIER)
7236 /* "long", "short", "signed", "unsigned" or "_Complex". */
7237 bool dupe = false;
7238 switch (i)
7240 case RID_LONG:
7241 if (specs->long_long_p)
7243 error ("%<long long long%> is too long for GCC");
7244 break;
7246 if (specs->long_p)
7248 if (specs->typespec_word == cts_double)
7250 error ("both %<long long%> and %<double%> in "
7251 "declaration specifiers");
7252 break;
7254 if (pedantic && !flag_isoc99 && !in_system_header
7255 && warn_long_long)
7256 pedwarn ("ISO C90 does not support %<long long%>");
7257 specs->long_long_p = 1;
7258 break;
7260 if (specs->short_p)
7261 error ("both %<long%> and %<short%> in "
7262 "declaration specifiers");
7263 else if (specs->typespec_word == cts_void)
7264 error ("both %<long%> and %<void%> in "
7265 "declaration specifiers");
7266 else if (specs->typespec_word == cts_bool)
7267 error ("both %<long%> and %<_Bool%> in "
7268 "declaration specifiers");
7269 else if (specs->typespec_word == cts_char)
7270 error ("both %<long%> and %<char%> in "
7271 "declaration specifiers");
7272 else if (specs->typespec_word == cts_float)
7273 error ("both %<long%> and %<float%> in "
7274 "declaration specifiers");
7275 else if (specs->typespec_word == cts_dfloat32)
7276 error ("both %<long%> and %<_Decimal32%> in "
7277 "declaration specifiers");
7278 else if (specs->typespec_word == cts_dfloat64)
7279 error ("both %<long%> and %<_Decimal64%> in "
7280 "declaration specifiers");
7281 else if (specs->typespec_word == cts_dfloat128)
7282 error ("both %<long%> and %<_Decimal128%> in "
7283 "declaration specifiers");
7284 else
7285 specs->long_p = true;
7286 break;
7287 case RID_SHORT:
7288 dupe = specs->short_p;
7289 if (specs->long_p)
7290 error ("both %<long%> and %<short%> in "
7291 "declaration specifiers");
7292 else if (specs->typespec_word == cts_void)
7293 error ("both %<short%> and %<void%> in "
7294 "declaration specifiers");
7295 else if (specs->typespec_word == cts_bool)
7296 error ("both %<short%> and %<_Bool%> in "
7297 "declaration specifiers");
7298 else if (specs->typespec_word == cts_char)
7299 error ("both %<short%> and %<char%> in "
7300 "declaration specifiers");
7301 else if (specs->typespec_word == cts_float)
7302 error ("both %<short%> and %<float%> in "
7303 "declaration specifiers");
7304 else if (specs->typespec_word == cts_double)
7305 error ("both %<short%> and %<double%> in "
7306 "declaration specifiers");
7307 else if (specs->typespec_word == cts_dfloat32)
7308 error ("both %<short%> and %<_Decimal32%> in "
7309 "declaration specifiers");
7310 else if (specs->typespec_word == cts_dfloat64)
7311 error ("both %<short%> and %<_Decimal64%> in "
7312 "declaration specifiers");
7313 else if (specs->typespec_word == cts_dfloat128)
7314 error ("both %<short%> and %<_Decimal128%> in "
7315 "declaration specifiers");
7316 else
7317 specs->short_p = true;
7318 break;
7319 case RID_SIGNED:
7320 dupe = specs->signed_p;
7321 if (specs->unsigned_p)
7322 error ("both %<signed%> and %<unsigned%> in "
7323 "declaration specifiers");
7324 else if (specs->typespec_word == cts_void)
7325 error ("both %<signed%> and %<void%> in "
7326 "declaration specifiers");
7327 else if (specs->typespec_word == cts_bool)
7328 error ("both %<signed%> and %<_Bool%> in "
7329 "declaration specifiers");
7330 else if (specs->typespec_word == cts_float)
7331 error ("both %<signed%> and %<float%> in "
7332 "declaration specifiers");
7333 else if (specs->typespec_word == cts_double)
7334 error ("both %<signed%> and %<double%> in "
7335 "declaration specifiers");
7336 else if (specs->typespec_word == cts_dfloat32)
7337 error ("both %<signed%> and %<_Decimal32%> in "
7338 "declaration specifiers");
7339 else if (specs->typespec_word == cts_dfloat64)
7340 error ("both %<signed%> and %<_Decimal64%> in "
7341 "declaration specifiers");
7342 else if (specs->typespec_word == cts_dfloat128)
7343 error ("both %<signed%> and %<_Decimal128%> in "
7344 "declaration specifiers");
7345 else
7346 specs->signed_p = true;
7347 break;
7348 case RID_UNSIGNED:
7349 dupe = specs->unsigned_p;
7350 if (specs->signed_p)
7351 error ("both %<signed%> and %<unsigned%> in "
7352 "declaration specifiers");
7353 else if (specs->typespec_word == cts_void)
7354 error ("both %<unsigned%> and %<void%> in "
7355 "declaration specifiers");
7356 else if (specs->typespec_word == cts_bool)
7357 error ("both %<unsigned%> and %<_Bool%> in "
7358 "declaration specifiers");
7359 else if (specs->typespec_word == cts_float)
7360 error ("both %<unsigned%> and %<float%> in "
7361 "declaration specifiers");
7362 else if (specs->typespec_word == cts_double)
7363 error ("both %<unsigned%> and %<double%> in "
7364 "declaration specifiers");
7365 else if (specs->typespec_word == cts_dfloat32)
7366 error ("both %<unsigned%> and %<_Decimal32%> in "
7367 "declaration specifiers");
7368 else if (specs->typespec_word == cts_dfloat64)
7369 error ("both %<unsigned%> and %<_Decimal64%> in "
7370 "declaration specifiers");
7371 else if (specs->typespec_word == cts_dfloat128)
7372 error ("both %<unsigned%> and %<_Decimal128%> in "
7373 "declaration specifiers");
7374 else
7375 specs->unsigned_p = true;
7376 break;
7377 case RID_COMPLEX:
7378 dupe = specs->complex_p;
7379 if (pedantic && !flag_isoc99 && !in_system_header)
7380 pedwarn ("ISO C90 does not support complex types");
7381 if (specs->typespec_word == cts_void)
7382 error ("both %<complex%> and %<void%> in "
7383 "declaration specifiers");
7384 else if (specs->typespec_word == cts_bool)
7385 error ("both %<complex%> and %<_Bool%> in "
7386 "declaration specifiers");
7387 else if (specs->typespec_word == cts_dfloat32)
7388 error ("both %<complex%> and %<_Decimal32%> in "
7389 "declaration specifiers");
7390 else if (specs->typespec_word == cts_dfloat64)
7391 error ("both %<complex%> and %<_Decimal64%> in "
7392 "declaration specifiers");
7393 else if (specs->typespec_word == cts_dfloat128)
7394 error ("both %<complex%> and %<_Decimal128%> in "
7395 "declaration specifiers");
7396 else
7397 specs->complex_p = true;
7398 break;
7399 default:
7400 gcc_unreachable ();
7403 if (dupe)
7404 error ("duplicate %qE", type);
7406 return specs;
7408 else
7410 /* "void", "_Bool", "char", "int", "float" or "double". */
7411 if (specs->typespec_word != cts_none)
7413 error ("two or more data types in declaration specifiers");
7414 return specs;
7416 switch (i)
7418 case RID_VOID:
7419 if (specs->long_p)
7420 error ("both %<long%> and %<void%> in "
7421 "declaration specifiers");
7422 else if (specs->short_p)
7423 error ("both %<short%> and %<void%> in "
7424 "declaration specifiers");
7425 else if (specs->signed_p)
7426 error ("both %<signed%> and %<void%> in "
7427 "declaration specifiers");
7428 else if (specs->unsigned_p)
7429 error ("both %<unsigned%> and %<void%> in "
7430 "declaration specifiers");
7431 else if (specs->complex_p)
7432 error ("both %<complex%> and %<void%> in "
7433 "declaration specifiers");
7434 else
7435 specs->typespec_word = cts_void;
7436 return specs;
7437 case RID_BOOL:
7438 if (specs->long_p)
7439 error ("both %<long%> and %<_Bool%> in "
7440 "declaration specifiers");
7441 else if (specs->short_p)
7442 error ("both %<short%> and %<_Bool%> in "
7443 "declaration specifiers");
7444 else if (specs->signed_p)
7445 error ("both %<signed%> and %<_Bool%> in "
7446 "declaration specifiers");
7447 else if (specs->unsigned_p)
7448 error ("both %<unsigned%> and %<_Bool%> in "
7449 "declaration specifiers");
7450 else if (specs->complex_p)
7451 error ("both %<complex%> and %<_Bool%> in "
7452 "declaration specifiers");
7453 else
7454 specs->typespec_word = cts_bool;
7455 return specs;
7456 case RID_CHAR:
7457 if (specs->long_p)
7458 error ("both %<long%> and %<char%> in "
7459 "declaration specifiers");
7460 else if (specs->short_p)
7461 error ("both %<short%> and %<char%> in "
7462 "declaration specifiers");
7463 else
7464 specs->typespec_word = cts_char;
7465 return specs;
7466 case RID_INT:
7467 specs->typespec_word = cts_int;
7468 return specs;
7469 case RID_FLOAT:
7470 if (specs->long_p)
7471 error ("both %<long%> and %<float%> in "
7472 "declaration specifiers");
7473 else if (specs->short_p)
7474 error ("both %<short%> and %<float%> in "
7475 "declaration specifiers");
7476 else if (specs->signed_p)
7477 error ("both %<signed%> and %<float%> in "
7478 "declaration specifiers");
7479 else if (specs->unsigned_p)
7480 error ("both %<unsigned%> and %<float%> in "
7481 "declaration specifiers");
7482 else
7483 specs->typespec_word = cts_float;
7484 return specs;
7485 case RID_DOUBLE:
7486 if (specs->long_long_p)
7487 error ("both %<long long%> and %<double%> in "
7488 "declaration specifiers");
7489 else if (specs->short_p)
7490 error ("both %<short%> and %<double%> in "
7491 "declaration specifiers");
7492 else if (specs->signed_p)
7493 error ("both %<signed%> and %<double%> in "
7494 "declaration specifiers");
7495 else if (specs->unsigned_p)
7496 error ("both %<unsigned%> and %<double%> in "
7497 "declaration specifiers");
7498 else
7499 specs->typespec_word = cts_double;
7500 return specs;
7501 case RID_DFLOAT32:
7502 case RID_DFLOAT64:
7503 case RID_DFLOAT128:
7505 const char *str;
7506 if (i == RID_DFLOAT32)
7507 str = "_Decimal32";
7508 else if (i == RID_DFLOAT64)
7509 str = "_Decimal64";
7510 else
7511 str = "_Decimal128";
7512 if (specs->long_long_p)
7513 error ("both %<long long%> and %<%s%> in "
7514 "declaration specifiers", str);
7515 if (specs->long_p)
7516 error ("both %<long%> and %<%s%> in "
7517 "declaration specifiers", str);
7518 else if (specs->short_p)
7519 error ("both %<short%> and %<%s%> in "
7520 "declaration specifiers", str);
7521 else if (specs->signed_p)
7522 error ("both %<signed%> and %<%s%> in "
7523 "declaration specifiers", str);
7524 else if (specs->unsigned_p)
7525 error ("both %<unsigned%> and %<%s%> in "
7526 "declaration specifiers", str);
7527 else if (specs->complex_p)
7528 error ("both %<complex%> and %<%s%> in "
7529 "declaration specifiers", str);
7530 else if (i == RID_DFLOAT32)
7531 specs->typespec_word = cts_dfloat32;
7532 else if (i == RID_DFLOAT64)
7533 specs->typespec_word = cts_dfloat64;
7534 else
7535 specs->typespec_word = cts_dfloat128;
7537 if (!targetm.decimal_float_supported_p ())
7538 error ("decimal floating point not supported for this target");
7539 if (pedantic)
7540 pedwarn ("ISO C does not support decimal floating point");
7541 return specs;
7542 default:
7543 /* ObjC reserved word "id", handled below. */
7544 break;
7549 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7550 form of ObjC type, cases such as "int" and "long" being handled
7551 above), a TYPE (struct, union, enum and typeof specifiers) or an
7552 ERROR_MARK. In none of these cases may there have previously
7553 been any type specifiers. */
7554 if (specs->type || specs->typespec_word != cts_none
7555 || specs->long_p || specs->short_p || specs->signed_p
7556 || specs->unsigned_p || specs->complex_p)
7557 error ("two or more data types in declaration specifiers");
7558 else if (TREE_CODE (type) == TYPE_DECL)
7560 if (TREE_TYPE (type) == error_mark_node)
7561 ; /* Allow the type to default to int to avoid cascading errors. */
7562 else
7564 specs->type = TREE_TYPE (type);
7565 specs->decl_attr = DECL_ATTRIBUTES (type);
7566 specs->typedef_p = true;
7567 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7570 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7572 tree t = lookup_name (type);
7573 if (!t || TREE_CODE (t) != TYPE_DECL)
7574 error ("%qE fails to be a typedef or built in type", type);
7575 else if (TREE_TYPE (t) == error_mark_node)
7577 else
7578 specs->type = TREE_TYPE (t);
7580 else if (TREE_CODE (type) != ERROR_MARK)
7582 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7583 specs->tag_defined_p = true;
7584 if (spec.kind == ctsk_typeof)
7585 specs->typedef_p = true;
7586 specs->type = type;
7589 return specs;
7592 /* Add the storage class specifier or function specifier SCSPEC to the
7593 declaration specifiers SPECS, returning SPECS. */
7595 struct c_declspecs *
7596 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7598 enum rid i;
7599 enum c_storage_class n = csc_none;
7600 bool dupe = false;
7601 specs->declspecs_seen_p = true;
7602 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7603 && C_IS_RESERVED_WORD (scspec));
7604 i = C_RID_CODE (scspec);
7605 if (specs->non_sc_seen_p)
7606 warning (OPT_Wold_style_declaration,
7607 "%qE is not at beginning of declaration", scspec);
7608 switch (i)
7610 case RID_INLINE:
7611 /* C99 permits duplicate inline. Although of doubtful utility,
7612 it seems simplest to permit it in gnu89 mode as well, as
7613 there is also little utility in maintaining this as a
7614 difference between gnu89 and C99 inline. */
7615 dupe = false;
7616 specs->inline_p = true;
7617 break;
7618 case RID_THREAD:
7619 dupe = specs->thread_p;
7620 if (specs->storage_class == csc_auto)
7621 error ("%<__thread%> used with %<auto%>");
7622 else if (specs->storage_class == csc_register)
7623 error ("%<__thread%> used with %<register%>");
7624 else if (specs->storage_class == csc_typedef)
7625 error ("%<__thread%> used with %<typedef%>");
7626 else
7627 specs->thread_p = true;
7628 break;
7629 case RID_AUTO:
7630 n = csc_auto;
7631 break;
7632 case RID_EXTERN:
7633 n = csc_extern;
7634 /* Diagnose "__thread extern". */
7635 if (specs->thread_p)
7636 error ("%<__thread%> before %<extern%>");
7637 break;
7638 case RID_REGISTER:
7639 n = csc_register;
7640 break;
7641 case RID_STATIC:
7642 n = csc_static;
7643 /* Diagnose "__thread static". */
7644 if (specs->thread_p)
7645 error ("%<__thread%> before %<static%>");
7646 break;
7647 case RID_TYPEDEF:
7648 n = csc_typedef;
7649 break;
7650 default:
7651 gcc_unreachable ();
7653 if (n != csc_none && n == specs->storage_class)
7654 dupe = true;
7655 if (dupe)
7656 error ("duplicate %qE", scspec);
7657 if (n != csc_none)
7659 if (specs->storage_class != csc_none && n != specs->storage_class)
7661 error ("multiple storage classes in declaration specifiers");
7663 else
7665 specs->storage_class = n;
7666 if (n != csc_extern && n != csc_static && specs->thread_p)
7668 error ("%<__thread%> used with %qE", scspec);
7669 specs->thread_p = false;
7673 return specs;
7676 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7677 returning SPECS. */
7679 struct c_declspecs *
7680 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7682 specs->attrs = chainon (attrs, specs->attrs);
7683 specs->declspecs_seen_p = true;
7684 return specs;
7687 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7688 specifiers with any other type specifier to determine the resulting
7689 type. This is where ISO C checks on complex types are made, since
7690 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7691 double". */
7693 struct c_declspecs *
7694 finish_declspecs (struct c_declspecs *specs)
7696 /* If a type was specified as a whole, we have no modifiers and are
7697 done. */
7698 if (specs->type != NULL_TREE)
7700 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7701 && !specs->signed_p && !specs->unsigned_p
7702 && !specs->complex_p);
7703 return specs;
7706 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7707 has been specified, treat it as "int" unless "_Complex" is
7708 present and there are no other specifiers. If we just have
7709 "_Complex", it is equivalent to "_Complex double", but e.g.
7710 "_Complex short" is equivalent to "_Complex short int". */
7711 if (specs->typespec_word == cts_none)
7713 if (specs->long_p || specs->short_p
7714 || specs->signed_p || specs->unsigned_p)
7716 specs->typespec_word = cts_int;
7718 else if (specs->complex_p)
7720 specs->typespec_word = cts_double;
7721 if (pedantic)
7722 pedwarn ("ISO C does not support plain %<complex%> meaning "
7723 "%<double complex%>");
7725 else
7727 specs->typespec_word = cts_int;
7728 specs->default_int_p = true;
7729 /* We don't diagnose this here because grokdeclarator will
7730 give more specific diagnostics according to whether it is
7731 a function definition. */
7735 /* If "signed" was specified, record this to distinguish "int" and
7736 "signed int" in the case of a bit-field with
7737 -funsigned-bitfields. */
7738 specs->explicit_signed_p = specs->signed_p;
7740 /* Now compute the actual type. */
7741 switch (specs->typespec_word)
7743 case cts_void:
7744 gcc_assert (!specs->long_p && !specs->short_p
7745 && !specs->signed_p && !specs->unsigned_p
7746 && !specs->complex_p);
7747 specs->type = void_type_node;
7748 break;
7749 case cts_bool:
7750 gcc_assert (!specs->long_p && !specs->short_p
7751 && !specs->signed_p && !specs->unsigned_p
7752 && !specs->complex_p);
7753 specs->type = boolean_type_node;
7754 break;
7755 case cts_char:
7756 gcc_assert (!specs->long_p && !specs->short_p);
7757 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7758 if (specs->signed_p)
7759 specs->type = signed_char_type_node;
7760 else if (specs->unsigned_p)
7761 specs->type = unsigned_char_type_node;
7762 else
7763 specs->type = char_type_node;
7764 if (specs->complex_p)
7766 if (pedantic)
7767 pedwarn ("ISO C does not support complex integer types");
7768 specs->type = build_complex_type (specs->type);
7770 break;
7771 case cts_int:
7772 gcc_assert (!(specs->long_p && specs->short_p));
7773 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7774 if (specs->long_long_p)
7775 specs->type = (specs->unsigned_p
7776 ? long_long_unsigned_type_node
7777 : long_long_integer_type_node);
7778 else if (specs->long_p)
7779 specs->type = (specs->unsigned_p
7780 ? long_unsigned_type_node
7781 : long_integer_type_node);
7782 else if (specs->short_p)
7783 specs->type = (specs->unsigned_p
7784 ? short_unsigned_type_node
7785 : short_integer_type_node);
7786 else
7787 specs->type = (specs->unsigned_p
7788 ? unsigned_type_node
7789 : integer_type_node);
7790 if (specs->complex_p)
7792 if (pedantic)
7793 pedwarn ("ISO C does not support complex integer types");
7794 specs->type = build_complex_type (specs->type);
7796 break;
7797 case cts_float:
7798 gcc_assert (!specs->long_p && !specs->short_p
7799 && !specs->signed_p && !specs->unsigned_p);
7800 specs->type = (specs->complex_p
7801 ? complex_float_type_node
7802 : float_type_node);
7803 break;
7804 case cts_double:
7805 gcc_assert (!specs->long_long_p && !specs->short_p
7806 && !specs->signed_p && !specs->unsigned_p);
7807 if (specs->long_p)
7809 specs->type = (specs->complex_p
7810 ? complex_long_double_type_node
7811 : long_double_type_node);
7813 else
7815 specs->type = (specs->complex_p
7816 ? complex_double_type_node
7817 : double_type_node);
7819 break;
7820 case cts_dfloat32:
7821 case cts_dfloat64:
7822 case cts_dfloat128:
7823 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7824 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7825 if (specs->typespec_word == cts_dfloat32)
7826 specs->type = dfloat32_type_node;
7827 else if (specs->typespec_word == cts_dfloat64)
7828 specs->type = dfloat64_type_node;
7829 else
7830 specs->type = dfloat128_type_node;
7831 break;
7832 default:
7833 gcc_unreachable ();
7836 return specs;
7839 /* Synthesize a function which calls all the global ctors or global
7840 dtors in this file. This is only used for targets which do not
7841 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7842 static void
7843 build_cdtor (int method_type, tree cdtors)
7845 tree body = 0;
7847 if (!cdtors)
7848 return;
7850 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7851 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7852 &body);
7854 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7857 /* A subroutine of c_write_global_declarations. Perform final processing
7858 on one file scope's declarations (or the external scope's declarations),
7859 GLOBALS. */
7861 static void
7862 c_write_global_declarations_1 (tree globals)
7864 tree decl;
7865 bool reconsider;
7867 /* Process the decls in the order they were written. */
7868 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7870 /* Check for used but undefined static functions using the C
7871 standard's definition of "used", and set TREE_NO_WARNING so
7872 that check_global_declarations doesn't repeat the check. */
7873 if (TREE_CODE (decl) == FUNCTION_DECL
7874 && DECL_INITIAL (decl) == 0
7875 && DECL_EXTERNAL (decl)
7876 && !TREE_PUBLIC (decl)
7877 && C_DECL_USED (decl))
7879 pedwarn ("%q+F used but never defined", decl);
7880 TREE_NO_WARNING (decl) = 1;
7883 wrapup_global_declaration_1 (decl);
7888 reconsider = false;
7889 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7890 reconsider |= wrapup_global_declaration_2 (decl);
7892 while (reconsider);
7894 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7895 check_global_declaration_1 (decl);
7898 /* A subroutine of c_write_global_declarations Emit debug information for each
7899 of the declarations in GLOBALS. */
7901 static void
7902 c_write_global_declarations_2 (tree globals)
7904 tree decl;
7906 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
7907 debug_hooks->global_decl (decl);
7910 /* Preserve the external declarations scope across a garbage collect. */
7911 static GTY(()) tree ext_block;
7913 void
7914 c_write_global_declarations (void)
7916 tree t;
7918 /* We don't want to do this if generating a PCH. */
7919 if (pch_file)
7920 return;
7922 /* Don't waste time on further processing if -fsyntax-only or we've
7923 encountered errors. */
7924 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7925 return;
7927 /* Close the external scope. */
7928 ext_block = pop_scope ();
7929 external_scope = 0;
7930 gcc_assert (!current_scope);
7932 if (ext_block)
7934 tree tmp = BLOCK_VARS (ext_block);
7935 int flags;
7936 FILE * stream = dump_begin (TDI_tu, &flags);
7937 if (stream && tmp)
7939 dump_node (tmp, flags & ~TDF_SLIM, stream);
7940 dump_end (TDI_tu, stream);
7944 /* Process all file scopes in this compilation, and the external_scope,
7945 through wrapup_global_declarations and check_global_declarations. */
7946 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7947 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7948 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7950 /* Generate functions to call static constructors and destructors
7951 for targets that do not support .ctors/.dtors sections. These
7952 functions have magic names which are detected by collect2. */
7953 build_cdtor ('I', static_ctors); static_ctors = 0;
7954 build_cdtor ('D', static_dtors); static_dtors = 0;
7956 /* We're done parsing; proceed to optimize and emit assembly.
7957 FIXME: shouldn't be the front end's responsibility to call this. */
7958 cgraph_optimize ();
7960 /* After cgraph has had a chance to emit everything that's going to
7961 be emitted, output debug information for globals. */
7962 if (errorcount == 0 && sorrycount == 0)
7964 timevar_push (TV_SYMOUT);
7965 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7966 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
7967 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
7968 timevar_pop (TV_SYMOUT);
7971 ext_block = NULL;
7974 #include "gt-c-decl.h"