* doc/extend.texi: Document optional priority argument to
[official-gcc.git] / gcc / c-decl.c
blobec567a5451d9f4188b6e835f0f78c5311222a75a
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 if (warn_implicit_function_declaration)
2381 if (flag_isoc99)
2382 pedwarn (G_("implicit declaration of function %qE"), id);
2383 else
2384 warning (OPT_Wimplicit_function_declaration,
2385 G_("implicit declaration of function %qE"), id);
2386 if (olddecl)
2387 locate_old_decl (olddecl, inform);
2391 /* Generate an implicit declaration for identifier FUNCTIONID as a
2392 function of type int (). */
2394 tree
2395 implicitly_declare (tree functionid)
2397 struct c_binding *b;
2398 tree decl = 0;
2399 tree asmspec_tree;
2401 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2403 if (B_IN_SCOPE (b, external_scope))
2405 decl = b->decl;
2406 break;
2410 if (decl)
2412 if (decl == error_mark_node)
2413 return decl;
2415 /* FIXME: Objective-C has weird not-really-builtin functions
2416 which are supposed to be visible automatically. They wind up
2417 in the external scope because they're pushed before the file
2418 scope gets created. Catch this here and rebind them into the
2419 file scope. */
2420 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2422 bind (functionid, decl, file_scope,
2423 /*invisible=*/false, /*nested=*/true);
2424 return decl;
2426 else
2428 tree newtype = default_function_type;
2429 if (b->type)
2430 TREE_TYPE (decl) = b->type;
2431 /* Implicit declaration of a function already declared
2432 (somehow) in a different scope, or as a built-in.
2433 If this is the first time this has happened, warn;
2434 then recycle the old declaration but with the new type. */
2435 if (!C_DECL_IMPLICIT (decl))
2437 implicit_decl_warning (functionid, decl);
2438 C_DECL_IMPLICIT (decl) = 1;
2440 if (DECL_BUILT_IN (decl))
2442 newtype = build_type_attribute_variant (newtype,
2443 TYPE_ATTRIBUTES
2444 (TREE_TYPE (decl)));
2445 if (!comptypes (newtype, TREE_TYPE (decl)))
2447 warning (0, "incompatible implicit declaration of built-in"
2448 " function %qD", decl);
2449 newtype = TREE_TYPE (decl);
2452 else
2454 if (!comptypes (newtype, TREE_TYPE (decl)))
2456 error ("incompatible implicit declaration of function %qD",
2457 decl);
2458 locate_old_decl (decl, error);
2461 b->type = TREE_TYPE (decl);
2462 TREE_TYPE (decl) = newtype;
2463 bind (functionid, decl, current_scope,
2464 /*invisible=*/false, /*nested=*/true);
2465 return decl;
2469 /* Not seen before. */
2470 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2471 DECL_EXTERNAL (decl) = 1;
2472 TREE_PUBLIC (decl) = 1;
2473 C_DECL_IMPLICIT (decl) = 1;
2474 implicit_decl_warning (functionid, 0);
2475 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2476 if (asmspec_tree)
2477 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2479 /* C89 says implicit declarations are in the innermost block.
2480 So we record the decl in the standard fashion. */
2481 decl = pushdecl (decl);
2483 /* No need to call objc_check_decl here - it's a function type. */
2484 rest_of_decl_compilation (decl, 0, 0);
2486 /* Write a record describing this implicit function declaration
2487 to the prototypes file (if requested). */
2488 gen_aux_info_record (decl, 0, 1, 0);
2490 /* Possibly apply some default attributes to this implicit declaration. */
2491 decl_attributes (&decl, NULL_TREE, 0);
2493 return decl;
2496 /* Issue an error message for a reference to an undeclared variable
2497 ID, including a reference to a builtin outside of function-call
2498 context. Establish a binding of the identifier to error_mark_node
2499 in an appropriate scope, which will suppress further errors for the
2500 same identifier. The error message should be given location LOC. */
2501 void
2502 undeclared_variable (tree id, location_t loc)
2504 static bool already = false;
2505 struct c_scope *scope;
2507 if (current_function_decl == 0)
2509 error ("%H%qE undeclared here (not in a function)", &loc, id);
2510 scope = current_scope;
2512 else
2514 error ("%H%qE undeclared (first use in this function)", &loc, id);
2516 if (!already)
2518 error ("%H(Each undeclared identifier is reported only once", &loc);
2519 error ("%Hfor each function it appears in.)", &loc);
2520 already = true;
2523 /* If we are parsing old-style parameter decls, current_function_decl
2524 will be nonnull but current_function_scope will be null. */
2525 scope = current_function_scope ? current_function_scope : current_scope;
2527 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2530 /* Subroutine of lookup_label, declare_label, define_label: construct a
2531 LABEL_DECL with all the proper frills. */
2533 static tree
2534 make_label (tree name, location_t location)
2536 tree label = build_decl (LABEL_DECL, name, void_type_node);
2538 DECL_CONTEXT (label) = current_function_decl;
2539 DECL_MODE (label) = VOIDmode;
2540 DECL_SOURCE_LOCATION (label) = location;
2542 return label;
2545 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2546 Create one if none exists so far for the current function.
2547 This is called when a label is used in a goto expression or
2548 has its address taken. */
2550 tree
2551 lookup_label (tree name)
2553 tree label;
2555 if (current_function_decl == 0)
2557 error ("label %qE referenced outside of any function", name);
2558 return 0;
2561 /* Use a label already defined or ref'd with this name, but not if
2562 it is inherited from a containing function and wasn't declared
2563 using __label__. */
2564 label = I_LABEL_DECL (name);
2565 if (label && (DECL_CONTEXT (label) == current_function_decl
2566 || C_DECLARED_LABEL_FLAG (label)))
2568 /* If the label has only been declared, update its apparent
2569 location to point here, for better diagnostics if it
2570 turns out not to have been defined. */
2571 if (!TREE_USED (label))
2572 DECL_SOURCE_LOCATION (label) = input_location;
2573 return label;
2576 /* No label binding for that identifier; make one. */
2577 label = make_label (name, input_location);
2579 /* Ordinary labels go in the current function scope. */
2580 bind (name, label, current_function_scope,
2581 /*invisible=*/false, /*nested=*/false);
2582 return label;
2585 /* Make a label named NAME in the current function, shadowing silently
2586 any that may be inherited from containing functions or containing
2587 scopes. This is called for __label__ declarations. */
2589 tree
2590 declare_label (tree name)
2592 struct c_binding *b = I_LABEL_BINDING (name);
2593 tree label;
2595 /* Check to make sure that the label hasn't already been declared
2596 at this scope */
2597 if (b && B_IN_CURRENT_SCOPE (b))
2599 error ("duplicate label declaration %qE", name);
2600 locate_old_decl (b->decl, error);
2602 /* Just use the previous declaration. */
2603 return b->decl;
2606 label = make_label (name, input_location);
2607 C_DECLARED_LABEL_FLAG (label) = 1;
2609 /* Declared labels go in the current scope. */
2610 bind (name, label, current_scope,
2611 /*invisible=*/false, /*nested=*/false);
2612 return label;
2615 /* Define a label, specifying the location in the source file.
2616 Return the LABEL_DECL node for the label, if the definition is valid.
2617 Otherwise return 0. */
2619 tree
2620 define_label (location_t location, tree name)
2622 /* Find any preexisting label with this name. It is an error
2623 if that label has already been defined in this function, or
2624 if there is a containing function with a declared label with
2625 the same name. */
2626 tree label = I_LABEL_DECL (name);
2627 struct c_label_list *nlist_se, *nlist_vm;
2629 if (label
2630 && ((DECL_CONTEXT (label) == current_function_decl
2631 && DECL_INITIAL (label) != 0)
2632 || (DECL_CONTEXT (label) != current_function_decl
2633 && C_DECLARED_LABEL_FLAG (label))))
2635 error ("%Hduplicate label %qD", &location, label);
2636 locate_old_decl (label, error);
2637 return 0;
2639 else if (label && DECL_CONTEXT (label) == current_function_decl)
2641 /* The label has been used or declared already in this function,
2642 but not defined. Update its location to point to this
2643 definition. */
2644 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2645 error ("%Jjump into statement expression", label);
2646 if (C_DECL_UNDEFINABLE_VM (label))
2647 error ("%Jjump into scope of identifier with variably modified type",
2648 label);
2649 DECL_SOURCE_LOCATION (label) = location;
2651 else
2653 /* No label binding for that identifier; make one. */
2654 label = make_label (name, location);
2656 /* Ordinary labels go in the current function scope. */
2657 bind (name, label, current_function_scope,
2658 /*invisible=*/false, /*nested=*/false);
2661 if (!in_system_header && lookup_name (name))
2662 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2663 "for labels, identifier %qE conflicts", &location, name);
2665 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2666 nlist_se->next = label_context_stack_se->labels_def;
2667 nlist_se->label = label;
2668 label_context_stack_se->labels_def = nlist_se;
2670 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2671 nlist_vm->next = label_context_stack_vm->labels_def;
2672 nlist_vm->label = label;
2673 label_context_stack_vm->labels_def = nlist_vm;
2675 /* Mark label as having been defined. */
2676 DECL_INITIAL (label) = error_mark_node;
2677 return label;
2680 /* Given NAME, an IDENTIFIER_NODE,
2681 return the structure (or union or enum) definition for that name.
2682 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2683 CODE says which kind of type the caller wants;
2684 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2685 If the wrong kind of type is found, an error is reported. */
2687 static tree
2688 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2690 struct c_binding *b = I_TAG_BINDING (name);
2691 int thislevel = 0;
2693 if (!b || !b->decl)
2694 return 0;
2696 /* We only care about whether it's in this level if
2697 thislevel_only was set or it might be a type clash. */
2698 if (thislevel_only || TREE_CODE (b->decl) != code)
2700 /* For our purposes, a tag in the external scope is the same as
2701 a tag in the file scope. (Primarily relevant to Objective-C
2702 and its builtin structure tags, which get pushed before the
2703 file scope is created.) */
2704 if (B_IN_CURRENT_SCOPE (b)
2705 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2706 thislevel = 1;
2709 if (thislevel_only && !thislevel)
2710 return 0;
2712 if (TREE_CODE (b->decl) != code)
2714 /* Definition isn't the kind we were looking for. */
2715 pending_invalid_xref = name;
2716 pending_invalid_xref_location = input_location;
2718 /* If in the same binding level as a declaration as a tag
2719 of a different type, this must not be allowed to
2720 shadow that tag, so give the error immediately.
2721 (For example, "struct foo; union foo;" is invalid.) */
2722 if (thislevel)
2723 pending_xref_error ();
2725 return b->decl;
2728 /* Print an error message now
2729 for a recent invalid struct, union or enum cross reference.
2730 We don't print them immediately because they are not invalid
2731 when used in the `struct foo;' construct for shadowing. */
2733 void
2734 pending_xref_error (void)
2736 if (pending_invalid_xref != 0)
2737 error ("%H%qE defined as wrong kind of tag",
2738 &pending_invalid_xref_location, pending_invalid_xref);
2739 pending_invalid_xref = 0;
2743 /* Look up NAME in the current scope and its superiors
2744 in the namespace of variables, functions and typedefs.
2745 Return a ..._DECL node of some kind representing its definition,
2746 or return 0 if it is undefined. */
2748 tree
2749 lookup_name (tree name)
2751 struct c_binding *b = I_SYMBOL_BINDING (name);
2752 if (b && !b->invisible)
2753 return b->decl;
2754 return 0;
2757 /* Similar to `lookup_name' but look only at the indicated scope. */
2759 static tree
2760 lookup_name_in_scope (tree name, struct c_scope *scope)
2762 struct c_binding *b;
2764 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2765 if (B_IN_SCOPE (b, scope))
2766 return b->decl;
2767 return 0;
2770 /* Create the predefined scalar types of C,
2771 and some nodes representing standard constants (0, 1, (void *) 0).
2772 Initialize the global scope.
2773 Make definitions for built-in primitive functions. */
2775 void
2776 c_init_decl_processing (void)
2778 location_t save_loc = input_location;
2780 /* Initialize reserved words for parser. */
2781 c_parse_init ();
2783 current_function_decl = 0;
2785 gcc_obstack_init (&parser_obstack);
2787 /* Make the externals scope. */
2788 push_scope ();
2789 external_scope = current_scope;
2791 /* Declarations from c_common_nodes_and_builtins must not be associated
2792 with this input file, lest we get differences between using and not
2793 using preprocessed headers. */
2794 #ifdef USE_MAPPED_LOCATION
2795 input_location = BUILTINS_LOCATION;
2796 #else
2797 input_location.file = "<built-in>";
2798 input_location.line = 0;
2799 #endif
2801 build_common_tree_nodes (flag_signed_char, false);
2803 c_common_nodes_and_builtins ();
2805 /* In C, comparisons and TRUTH_* expressions have type int. */
2806 truthvalue_type_node = integer_type_node;
2807 truthvalue_true_node = integer_one_node;
2808 truthvalue_false_node = integer_zero_node;
2810 /* Even in C99, which has a real boolean type. */
2811 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2812 boolean_type_node));
2814 input_location = save_loc;
2816 pedantic_lvalues = true;
2818 make_fname_decl = c_make_fname_decl;
2819 start_fname_decls ();
2822 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2823 decl, NAME is the initialization string and TYPE_DEP indicates whether
2824 NAME depended on the type of the function. As we don't yet implement
2825 delayed emission of static data, we mark the decl as emitted
2826 so it is not placed in the output. Anything using it must therefore pull
2827 out the STRING_CST initializer directly. FIXME. */
2829 static tree
2830 c_make_fname_decl (tree id, int type_dep)
2832 const char *name = fname_as_string (type_dep);
2833 tree decl, type, init;
2834 size_t length = strlen (name);
2836 type = build_array_type (char_type_node,
2837 build_index_type (size_int (length)));
2838 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2840 decl = build_decl (VAR_DECL, id, type);
2842 TREE_STATIC (decl) = 1;
2843 TREE_READONLY (decl) = 1;
2844 DECL_ARTIFICIAL (decl) = 1;
2846 init = build_string (length + 1, name);
2847 free ((char *) name);
2848 TREE_TYPE (init) = type;
2849 DECL_INITIAL (decl) = init;
2851 TREE_USED (decl) = 1;
2853 if (current_function_decl
2854 /* For invalid programs like this:
2856 void foo()
2857 const char* p = __FUNCTION__;
2859 the __FUNCTION__ is believed to appear in K&R style function
2860 parameter declarator. In that case we still don't have
2861 function_scope. */
2862 && (!errorcount || current_function_scope))
2864 DECL_CONTEXT (decl) = current_function_decl;
2865 bind (id, decl, current_function_scope,
2866 /*invisible=*/false, /*nested=*/false);
2869 finish_decl (decl, init, NULL_TREE);
2871 return decl;
2874 tree
2875 c_builtin_function (tree decl)
2877 tree type = TREE_TYPE (decl);
2878 tree id = DECL_NAME (decl);
2880 const char *name = IDENTIFIER_POINTER (id);
2881 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2882 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2884 /* Should never be called on a symbol with a preexisting meaning. */
2885 gcc_assert (!I_SYMBOL_BINDING (id));
2887 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2889 /* Builtins in the implementation namespace are made visible without
2890 needing to be explicitly declared. See push_file_scope. */
2891 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2893 TREE_CHAIN (decl) = visible_builtins;
2894 visible_builtins = decl;
2897 return decl;
2900 /* Called when a declaration is seen that contains no names to declare.
2901 If its type is a reference to a structure, union or enum inherited
2902 from a containing scope, shadow that tag name for the current scope
2903 with a forward reference.
2904 If its type defines a new named structure or union
2905 or defines an enum, it is valid but we need not do anything here.
2906 Otherwise, it is an error. */
2908 void
2909 shadow_tag (const struct c_declspecs *declspecs)
2911 shadow_tag_warned (declspecs, 0);
2914 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2915 but no pedwarn. */
2916 void
2917 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2919 bool found_tag = false;
2921 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2923 tree value = declspecs->type;
2924 enum tree_code code = TREE_CODE (value);
2926 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2927 /* Used to test also that TYPE_SIZE (value) != 0.
2928 That caused warning for `struct foo;' at top level in the file. */
2930 tree name = TYPE_NAME (value);
2931 tree t;
2933 found_tag = true;
2935 if (name == 0)
2937 if (warned != 1 && code != ENUMERAL_TYPE)
2938 /* Empty unnamed enum OK */
2940 pedwarn ("unnamed struct/union that defines no instances");
2941 warned = 1;
2944 else if (!declspecs->tag_defined_p
2945 && declspecs->storage_class != csc_none)
2947 if (warned != 1)
2948 pedwarn ("empty declaration with storage class specifier "
2949 "does not redeclare tag");
2950 warned = 1;
2951 pending_xref_error ();
2953 else if (!declspecs->tag_defined_p
2954 && (declspecs->const_p
2955 || declspecs->volatile_p
2956 || declspecs->restrict_p))
2958 if (warned != 1)
2959 pedwarn ("empty declaration with type qualifier "
2960 "does not redeclare tag");
2961 warned = 1;
2962 pending_xref_error ();
2964 else
2966 pending_invalid_xref = 0;
2967 t = lookup_tag (code, name, 1);
2969 if (t == 0)
2971 t = make_node (code);
2972 pushtag (name, t);
2976 else
2978 if (warned != 1 && !in_system_header)
2980 pedwarn ("useless type name in empty declaration");
2981 warned = 1;
2985 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2987 pedwarn ("useless type name in empty declaration");
2988 warned = 1;
2991 pending_invalid_xref = 0;
2993 if (declspecs->inline_p)
2995 error ("%<inline%> in empty declaration");
2996 warned = 1;
2999 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3001 error ("%<auto%> in file-scope empty declaration");
3002 warned = 1;
3005 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3007 error ("%<register%> in file-scope empty declaration");
3008 warned = 1;
3011 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3013 warning (0, "useless storage class specifier in empty declaration");
3014 warned = 2;
3017 if (!warned && !in_system_header && declspecs->thread_p)
3019 warning (0, "useless %<__thread%> in empty declaration");
3020 warned = 2;
3023 if (!warned && !in_system_header && (declspecs->const_p
3024 || declspecs->volatile_p
3025 || declspecs->restrict_p))
3027 warning (0, "useless type qualifier in empty declaration");
3028 warned = 2;
3031 if (warned != 1)
3033 if (!found_tag)
3034 pedwarn ("empty declaration");
3039 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3040 bits. SPECS represents declaration specifiers that the grammar
3041 only permits to contain type qualifiers and attributes. */
3044 quals_from_declspecs (const struct c_declspecs *specs)
3046 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3047 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3048 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3049 gcc_assert (!specs->type
3050 && !specs->decl_attr
3051 && specs->typespec_word == cts_none
3052 && specs->storage_class == csc_none
3053 && !specs->typedef_p
3054 && !specs->explicit_signed_p
3055 && !specs->deprecated_p
3056 && !specs->long_p
3057 && !specs->long_long_p
3058 && !specs->short_p
3059 && !specs->signed_p
3060 && !specs->unsigned_p
3061 && !specs->complex_p
3062 && !specs->inline_p
3063 && !specs->thread_p);
3064 return quals;
3067 /* Construct an array declarator. EXPR is the expression inside [],
3068 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3069 applied to the pointer to which a parameter array is converted).
3070 STATIC_P is true if "static" is inside the [], false otherwise.
3071 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3072 length which is nevertheless a complete type, false otherwise. The
3073 field for the contained declarator is left to be filled in by
3074 set_array_declarator_inner. */
3076 struct c_declarator *
3077 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3078 bool vla_unspec_p)
3080 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3081 struct c_declarator);
3082 declarator->kind = cdk_array;
3083 declarator->declarator = 0;
3084 declarator->u.array.dimen = expr;
3085 if (quals)
3087 declarator->u.array.attrs = quals->attrs;
3088 declarator->u.array.quals = quals_from_declspecs (quals);
3090 else
3092 declarator->u.array.attrs = NULL_TREE;
3093 declarator->u.array.quals = 0;
3095 declarator->u.array.static_p = static_p;
3096 declarator->u.array.vla_unspec_p = vla_unspec_p;
3097 if (pedantic && !flag_isoc99)
3099 if (static_p || quals != NULL)
3100 pedwarn ("ISO C90 does not support %<static%> or type "
3101 "qualifiers in parameter array declarators");
3102 if (vla_unspec_p)
3103 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3105 if (vla_unspec_p)
3107 if (!current_scope->parm_flag)
3109 /* C99 6.7.5.2p4 */
3110 error ("%<[*]%> not allowed in other than function prototype scope");
3111 declarator->u.array.vla_unspec_p = false;
3112 return NULL;
3114 current_scope->had_vla_unspec = true;
3116 return declarator;
3119 /* Set the contained declarator of an array declarator. DECL is the
3120 declarator, as constructed by build_array_declarator; INNER is what
3121 appears on the left of the []. ABSTRACT_P is true if it is an
3122 abstract declarator, false otherwise; this is used to reject static
3123 and type qualifiers in abstract declarators, where they are not in
3124 the C99 grammar (subject to possible change in DR#289). */
3126 struct c_declarator *
3127 set_array_declarator_inner (struct c_declarator *decl,
3128 struct c_declarator *inner, bool abstract_p)
3130 decl->declarator = inner;
3131 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3132 || decl->u.array.attrs != NULL_TREE
3133 || decl->u.array.static_p))
3134 error ("static or type qualifiers in abstract declarator");
3135 return decl;
3138 /* INIT is a constructor that forms DECL's initializer. If the final
3139 element initializes a flexible array field, add the size of that
3140 initializer to DECL's size. */
3142 static void
3143 add_flexible_array_elts_to_size (tree decl, tree init)
3145 tree elt, type;
3147 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3148 return;
3150 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3151 type = TREE_TYPE (elt);
3152 if (TREE_CODE (type) == ARRAY_TYPE
3153 && TYPE_SIZE (type) == NULL_TREE
3154 && TYPE_DOMAIN (type) != NULL_TREE
3155 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3157 complete_array_type (&type, elt, false);
3158 DECL_SIZE (decl)
3159 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3160 DECL_SIZE_UNIT (decl)
3161 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3165 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3167 tree
3168 groktypename (struct c_type_name *type_name)
3170 tree type;
3171 tree attrs = type_name->specs->attrs;
3173 type_name->specs->attrs = NULL_TREE;
3175 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3176 false, NULL);
3178 /* Apply attributes. */
3179 decl_attributes (&type, attrs, 0);
3181 return type;
3184 /* Decode a declarator in an ordinary declaration or data definition.
3185 This is called as soon as the type information and variable name
3186 have been parsed, before parsing the initializer if any.
3187 Here we create the ..._DECL node, fill in its type,
3188 and put it on the list of decls for the current context.
3189 The ..._DECL node is returned as the value.
3191 Exception: for arrays where the length is not specified,
3192 the type is left null, to be filled in by `finish_decl'.
3194 Function definitions do not come here; they go to start_function
3195 instead. However, external and forward declarations of functions
3196 do go through here. Structure field declarations are done by
3197 grokfield and not through here. */
3199 tree
3200 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3201 bool initialized, tree attributes)
3203 tree decl;
3204 tree tem;
3206 /* An object declared as __attribute__((deprecated)) suppresses
3207 warnings of uses of other deprecated items. */
3208 if (lookup_attribute ("deprecated", attributes))
3209 deprecated_state = DEPRECATED_SUPPRESS;
3211 decl = grokdeclarator (declarator, declspecs,
3212 NORMAL, initialized, NULL);
3213 if (!decl)
3214 return 0;
3216 deprecated_state = DEPRECATED_NORMAL;
3218 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3219 && MAIN_NAME_P (DECL_NAME (decl)))
3220 warning (OPT_Wmain, "%q+D is usually a function", decl);
3222 if (initialized)
3223 /* Is it valid for this decl to have an initializer at all?
3224 If not, set INITIALIZED to zero, which will indirectly
3225 tell 'finish_decl' to ignore the initializer once it is parsed. */
3226 switch (TREE_CODE (decl))
3228 case TYPE_DECL:
3229 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3230 initialized = 0;
3231 break;
3233 case FUNCTION_DECL:
3234 error ("function %qD is initialized like a variable", decl);
3235 initialized = 0;
3236 break;
3238 case PARM_DECL:
3239 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3240 error ("parameter %qD is initialized", decl);
3241 initialized = 0;
3242 break;
3244 default:
3245 /* Don't allow initializations for incomplete types except for
3246 arrays which might be completed by the initialization. */
3248 /* This can happen if the array size is an undefined macro.
3249 We already gave a warning, so we don't need another one. */
3250 if (TREE_TYPE (decl) == error_mark_node)
3251 initialized = 0;
3252 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3254 /* A complete type is ok if size is fixed. */
3256 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3257 || C_DECL_VARIABLE_SIZE (decl))
3259 error ("variable-sized object may not be initialized");
3260 initialized = 0;
3263 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3265 error ("variable %qD has initializer but incomplete type", decl);
3266 initialized = 0;
3268 else if (C_DECL_VARIABLE_SIZE (decl))
3270 /* Although C99 is unclear about whether incomplete arrays
3271 of VLAs themselves count as VLAs, it does not make
3272 sense to permit them to be initialized given that
3273 ordinary VLAs may not be initialized. */
3274 error ("variable-sized object may not be initialized");
3275 initialized = 0;
3279 if (initialized)
3281 if (current_scope == file_scope)
3282 TREE_STATIC (decl) = 1;
3284 /* Tell 'pushdecl' this is an initialized decl
3285 even though we don't yet have the initializer expression.
3286 Also tell 'finish_decl' it may store the real initializer. */
3287 DECL_INITIAL (decl) = error_mark_node;
3290 /* If this is a function declaration, write a record describing it to the
3291 prototypes file (if requested). */
3293 if (TREE_CODE (decl) == FUNCTION_DECL)
3294 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3296 /* ANSI specifies that a tentative definition which is not merged with
3297 a non-tentative definition behaves exactly like a definition with an
3298 initializer equal to zero. (Section 3.7.2)
3300 -fno-common gives strict ANSI behavior, though this tends to break
3301 a large body of code that grew up without this rule.
3303 Thread-local variables are never common, since there's no entrenched
3304 body of code to break, and it allows more efficient variable references
3305 in the presence of dynamic linking. */
3307 if (TREE_CODE (decl) == VAR_DECL
3308 && !initialized
3309 && TREE_PUBLIC (decl)
3310 && !DECL_THREAD_LOCAL_P (decl)
3311 && !flag_no_common)
3312 DECL_COMMON (decl) = 1;
3314 /* Set attributes here so if duplicate decl, will have proper attributes. */
3315 decl_attributes (&decl, attributes, 0);
3317 #if WANT_C99_INLINE_SEMANTICS
3318 /* Handle gnu_inline attribute. */
3319 if (declspecs->inline_p
3320 && flag_isoc99
3321 && TREE_CODE (decl) == FUNCTION_DECL
3322 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
3324 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3326 else if (declspecs->storage_class != csc_static)
3327 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3329 #endif /* WANT_C99_INLINE_SEMANTICS */
3331 if (TREE_CODE (decl) == FUNCTION_DECL
3332 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3334 struct c_declarator *ce = declarator;
3336 if (ce->kind == cdk_pointer)
3337 ce = declarator->declarator;
3338 if (ce->kind == cdk_function)
3340 tree args = ce->u.arg_info->parms;
3341 for (; args; args = TREE_CHAIN (args))
3343 tree type = TREE_TYPE (args);
3344 if (type && INTEGRAL_TYPE_P (type)
3345 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3346 DECL_ARG_TYPE (args) = integer_type_node;
3351 if (TREE_CODE (decl) == FUNCTION_DECL
3352 && DECL_DECLARED_INLINE_P (decl)
3353 && DECL_UNINLINABLE (decl)
3354 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3355 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3356 decl);
3358 /* C99 6.7.4p3: An inline definition of a function with external
3359 linkage shall not contain a definition of a modifiable object
3360 with static storage duration... */
3361 if (TREE_CODE (decl) == VAR_DECL
3362 && current_scope != file_scope
3363 && TREE_STATIC (decl)
3364 && DECL_DECLARED_INLINE_P (current_function_decl)
3365 && DECL_EXTERNAL (current_function_decl))
3366 pedwarn ("%q+D is static but declared in inline function %qD "
3367 "which is not static", decl, current_function_decl);
3369 /* Add this decl to the current scope.
3370 TEM may equal DECL or it may be a previous decl of the same name. */
3371 tem = pushdecl (decl);
3373 if (initialized && DECL_EXTERNAL (tem))
3375 DECL_EXTERNAL (tem) = 0;
3376 TREE_STATIC (tem) = 1;
3379 return tem;
3382 /* Initialize EH if not initialized yet and exceptions are enabled. */
3384 void
3385 c_maybe_initialize_eh (void)
3387 if (!flag_exceptions || c_eh_initialized_p)
3388 return;
3390 c_eh_initialized_p = true;
3391 eh_personality_libfunc
3392 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3393 ? "__gcc_personality_sj0"
3394 : "__gcc_personality_v0");
3395 default_init_unwind_resume_libfunc ();
3396 using_eh_for_cleanups ();
3399 /* Finish processing of a declaration;
3400 install its initial value.
3401 If the length of an array type is not known before,
3402 it must be determined now, from the initial value, or it is an error. */
3404 void
3405 finish_decl (tree decl, tree init, tree asmspec_tree)
3407 tree type = TREE_TYPE (decl);
3408 int was_incomplete = (DECL_SIZE (decl) == 0);
3409 const char *asmspec = 0;
3411 /* If a name was specified, get the string. */
3412 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3413 && DECL_FILE_SCOPE_P (decl))
3414 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3415 if (asmspec_tree)
3416 asmspec = TREE_STRING_POINTER (asmspec_tree);
3418 /* If `start_decl' didn't like having an initialization, ignore it now. */
3419 if (init != 0 && DECL_INITIAL (decl) == 0)
3420 init = 0;
3422 /* Don't crash if parm is initialized. */
3423 if (TREE_CODE (decl) == PARM_DECL)
3424 init = 0;
3426 if (init)
3427 store_init_value (decl, init);
3429 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3430 || TREE_CODE (decl) == FUNCTION_DECL
3431 || TREE_CODE (decl) == FIELD_DECL))
3432 objc_check_decl (decl);
3434 /* Deduce size of array from initialization, if not already known. */
3435 if (TREE_CODE (type) == ARRAY_TYPE
3436 && TYPE_DOMAIN (type) == 0
3437 && TREE_CODE (decl) != TYPE_DECL)
3439 bool do_default
3440 = (TREE_STATIC (decl)
3441 /* Even if pedantic, an external linkage array
3442 may have incomplete type at first. */
3443 ? pedantic && !TREE_PUBLIC (decl)
3444 : !DECL_EXTERNAL (decl));
3445 int failure
3446 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3447 do_default);
3449 /* Get the completed type made by complete_array_type. */
3450 type = TREE_TYPE (decl);
3452 switch (failure)
3454 case 1:
3455 error ("initializer fails to determine size of %q+D", decl);
3456 break;
3458 case 2:
3459 if (do_default)
3460 error ("array size missing in %q+D", decl);
3461 /* If a `static' var's size isn't known,
3462 make it extern as well as static, so it does not get
3463 allocated.
3464 If it is not `static', then do not mark extern;
3465 finish_incomplete_decl will give it a default size
3466 and it will get allocated. */
3467 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3468 DECL_EXTERNAL (decl) = 1;
3469 break;
3471 case 3:
3472 error ("zero or negative size array %q+D", decl);
3473 break;
3475 case 0:
3476 /* For global variables, update the copy of the type that
3477 exists in the binding. */
3478 if (TREE_PUBLIC (decl))
3480 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3481 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3482 b_ext = b_ext->shadowed;
3483 if (b_ext)
3485 if (b_ext->type)
3486 b_ext->type = composite_type (b_ext->type, type);
3487 else
3488 b_ext->type = type;
3491 break;
3493 default:
3494 gcc_unreachable ();
3497 if (DECL_INITIAL (decl))
3498 TREE_TYPE (DECL_INITIAL (decl)) = type;
3500 layout_decl (decl, 0);
3503 if (TREE_CODE (decl) == VAR_DECL)
3505 if (init && TREE_CODE (init) == CONSTRUCTOR)
3506 add_flexible_array_elts_to_size (decl, init);
3508 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3509 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3510 layout_decl (decl, 0);
3512 if (DECL_SIZE (decl) == 0
3513 /* Don't give an error if we already gave one earlier. */
3514 && TREE_TYPE (decl) != error_mark_node
3515 && (TREE_STATIC (decl)
3516 /* A static variable with an incomplete type
3517 is an error if it is initialized.
3518 Also if it is not file scope.
3519 Otherwise, let it through, but if it is not `extern'
3520 then it may cause an error message later. */
3521 ? (DECL_INITIAL (decl) != 0
3522 || !DECL_FILE_SCOPE_P (decl))
3523 /* An automatic variable with an incomplete type
3524 is an error. */
3525 : !DECL_EXTERNAL (decl)))
3527 error ("storage size of %q+D isn%'t known", decl);
3528 TREE_TYPE (decl) = error_mark_node;
3531 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3532 && DECL_SIZE (decl) != 0)
3534 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3535 constant_expression_warning (DECL_SIZE (decl));
3536 else
3537 error ("storage size of %q+D isn%'t constant", decl);
3540 if (TREE_USED (type))
3541 TREE_USED (decl) = 1;
3544 /* If this is a function and an assembler name is specified, reset DECL_RTL
3545 so we can give it its new name. Also, update built_in_decls if it
3546 was a normal built-in. */
3547 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3549 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3550 set_builtin_user_assembler_name (decl, asmspec);
3551 set_user_assembler_name (decl, asmspec);
3554 /* If #pragma weak was used, mark the decl weak now. */
3555 maybe_apply_pragma_weak (decl);
3557 /* Output the assembler code and/or RTL code for variables and functions,
3558 unless the type is an undefined structure or union.
3559 If not, it will get done when the type is completed. */
3561 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3563 /* Determine the ELF visibility. */
3564 if (TREE_PUBLIC (decl))
3565 c_determine_visibility (decl);
3567 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3568 if (c_dialect_objc ())
3569 objc_check_decl (decl);
3571 if (asmspec)
3573 /* If this is not a static variable, issue a warning.
3574 It doesn't make any sense to give an ASMSPEC for an
3575 ordinary, non-register local variable. Historically,
3576 GCC has accepted -- but ignored -- the ASMSPEC in
3577 this case. */
3578 if (!DECL_FILE_SCOPE_P (decl)
3579 && TREE_CODE (decl) == VAR_DECL
3580 && !C_DECL_REGISTER (decl)
3581 && !TREE_STATIC (decl))
3582 warning (0, "ignoring asm-specifier for non-static local "
3583 "variable %q+D", decl);
3584 else
3585 set_user_assembler_name (decl, asmspec);
3588 if (DECL_FILE_SCOPE_P (decl))
3590 if (DECL_INITIAL (decl) == NULL_TREE
3591 || DECL_INITIAL (decl) == error_mark_node)
3592 /* Don't output anything
3593 when a tentative file-scope definition is seen.
3594 But at end of compilation, do output code for them. */
3595 DECL_DEFER_OUTPUT (decl) = 1;
3596 rest_of_decl_compilation (decl, true, 0);
3598 else
3600 /* In conjunction with an ASMSPEC, the `register'
3601 keyword indicates that we should place the variable
3602 in a particular register. */
3603 if (asmspec && C_DECL_REGISTER (decl))
3605 DECL_HARD_REGISTER (decl) = 1;
3606 /* This cannot be done for a structure with volatile
3607 fields, on which DECL_REGISTER will have been
3608 reset. */
3609 if (!DECL_REGISTER (decl))
3610 error ("cannot put object with volatile field into register");
3613 if (TREE_CODE (decl) != FUNCTION_DECL)
3615 /* If we're building a variable sized type, and we might be
3616 reachable other than via the top of the current binding
3617 level, then create a new BIND_EXPR so that we deallocate
3618 the object at the right time. */
3619 /* Note that DECL_SIZE can be null due to errors. */
3620 if (DECL_SIZE (decl)
3621 && !TREE_CONSTANT (DECL_SIZE (decl))
3622 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3624 tree bind;
3625 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3626 TREE_SIDE_EFFECTS (bind) = 1;
3627 add_stmt (bind);
3628 BIND_EXPR_BODY (bind) = push_stmt_list ();
3630 add_stmt (build_stmt (DECL_EXPR, decl));
3635 if (!DECL_FILE_SCOPE_P (decl))
3637 /* Recompute the RTL of a local array now
3638 if it used to be an incomplete type. */
3639 if (was_incomplete
3640 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3642 /* If we used it already as memory, it must stay in memory. */
3643 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3644 /* If it's still incomplete now, no init will save it. */
3645 if (DECL_SIZE (decl) == 0)
3646 DECL_INITIAL (decl) = 0;
3651 /* If this was marked 'used', be sure it will be output. */
3652 if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3653 mark_decl_referenced (decl);
3655 if (TREE_CODE (decl) == TYPE_DECL)
3657 if (!DECL_FILE_SCOPE_P (decl)
3658 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3659 add_stmt (build_stmt (DECL_EXPR, decl));
3661 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3664 /* At the end of a declaration, throw away any variable type sizes
3665 of types defined inside that declaration. There is no use
3666 computing them in the following function definition. */
3667 if (current_scope == file_scope)
3668 get_pending_sizes ();
3670 /* Install a cleanup (aka destructor) if one was given. */
3671 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3673 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3674 if (attr)
3676 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3677 tree cleanup_decl = lookup_name (cleanup_id);
3678 tree cleanup;
3680 /* Build "cleanup(&decl)" for the destructor. */
3681 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3682 cleanup = build_tree_list (NULL_TREE, cleanup);
3683 cleanup = build_function_call (cleanup_decl, cleanup);
3685 /* Don't warn about decl unused; the cleanup uses it. */
3686 TREE_USED (decl) = 1;
3687 TREE_USED (cleanup_decl) = 1;
3689 /* Initialize EH, if we've been told to do so. */
3690 c_maybe_initialize_eh ();
3692 push_cleanup (decl, cleanup, false);
3697 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3699 tree
3700 grokparm (const struct c_parm *parm)
3702 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3703 NULL);
3705 decl_attributes (&decl, parm->attrs, 0);
3707 return decl;
3710 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3711 and push that on the current scope. */
3713 void
3714 push_parm_decl (const struct c_parm *parm)
3716 tree decl;
3718 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3719 decl_attributes (&decl, parm->attrs, 0);
3721 decl = pushdecl (decl);
3723 finish_decl (decl, NULL_TREE, NULL_TREE);
3726 /* Mark all the parameter declarations to date as forward decls.
3727 Also diagnose use of this extension. */
3729 void
3730 mark_forward_parm_decls (void)
3732 struct c_binding *b;
3734 if (pedantic && !current_scope->warned_forward_parm_decls)
3736 pedwarn ("ISO C forbids forward parameter declarations");
3737 current_scope->warned_forward_parm_decls = true;
3740 for (b = current_scope->bindings; b; b = b->prev)
3741 if (TREE_CODE (b->decl) == PARM_DECL)
3742 TREE_ASM_WRITTEN (b->decl) = 1;
3745 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3746 literal, which may be an incomplete array type completed by the
3747 initializer; INIT is a CONSTRUCTOR that initializes the compound
3748 literal. */
3750 tree
3751 build_compound_literal (tree type, tree init)
3753 /* We do not use start_decl here because we have a type, not a declarator;
3754 and do not use finish_decl because the decl should be stored inside
3755 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3756 tree decl;
3757 tree complit;
3758 tree stmt;
3760 if (type == error_mark_node)
3761 return error_mark_node;
3763 decl = build_decl (VAR_DECL, NULL_TREE, type);
3764 DECL_EXTERNAL (decl) = 0;
3765 TREE_PUBLIC (decl) = 0;
3766 TREE_STATIC (decl) = (current_scope == file_scope);
3767 DECL_CONTEXT (decl) = current_function_decl;
3768 TREE_USED (decl) = 1;
3769 TREE_TYPE (decl) = type;
3770 TREE_READONLY (decl) = TYPE_READONLY (type);
3771 store_init_value (decl, init);
3773 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3775 int failure = complete_array_type (&TREE_TYPE (decl),
3776 DECL_INITIAL (decl), true);
3777 gcc_assert (!failure);
3779 type = TREE_TYPE (decl);
3780 TREE_TYPE (DECL_INITIAL (decl)) = type;
3783 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3784 return error_mark_node;
3786 stmt = build_stmt (DECL_EXPR, decl);
3787 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3788 TREE_SIDE_EFFECTS (complit) = 1;
3790 layout_decl (decl, 0);
3792 if (TREE_STATIC (decl))
3794 /* This decl needs a name for the assembler output. */
3795 set_compound_literal_name (decl);
3796 DECL_DEFER_OUTPUT (decl) = 1;
3797 DECL_COMDAT (decl) = 1;
3798 DECL_ARTIFICIAL (decl) = 1;
3799 DECL_IGNORED_P (decl) = 1;
3800 pushdecl (decl);
3801 rest_of_decl_compilation (decl, 1, 0);
3804 return complit;
3807 /* Determine whether TYPE is a structure with a flexible array member,
3808 or a union containing such a structure (possibly recursively). */
3810 static bool
3811 flexible_array_type_p (tree type)
3813 tree x;
3814 switch (TREE_CODE (type))
3816 case RECORD_TYPE:
3817 x = TYPE_FIELDS (type);
3818 if (x == NULL_TREE)
3819 return false;
3820 while (TREE_CHAIN (x) != NULL_TREE)
3821 x = TREE_CHAIN (x);
3822 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3823 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3824 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3825 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3826 return true;
3827 return false;
3828 case UNION_TYPE:
3829 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3831 if (flexible_array_type_p (TREE_TYPE (x)))
3832 return true;
3834 return false;
3835 default:
3836 return false;
3840 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3841 replacing with appropriate values if they are invalid. */
3842 static void
3843 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3845 tree type_mv;
3846 unsigned int max_width;
3847 unsigned HOST_WIDE_INT w;
3848 const char *name = orig_name ? orig_name: _("<anonymous>");
3850 /* Detect and ignore out of range field width and process valid
3851 field widths. */
3852 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3853 || TREE_CODE (*width) != INTEGER_CST)
3855 error ("bit-field %qs width not an integer constant", name);
3856 *width = integer_one_node;
3858 else
3860 constant_expression_warning (*width);
3861 if (tree_int_cst_sgn (*width) < 0)
3863 error ("negative width in bit-field %qs", name);
3864 *width = integer_one_node;
3866 else if (integer_zerop (*width) && orig_name)
3868 error ("zero width for bit-field %qs", name);
3869 *width = integer_one_node;
3873 /* Detect invalid bit-field type. */
3874 if (TREE_CODE (*type) != INTEGER_TYPE
3875 && TREE_CODE (*type) != BOOLEAN_TYPE
3876 && TREE_CODE (*type) != ENUMERAL_TYPE)
3878 error ("bit-field %qs has invalid type", name);
3879 *type = unsigned_type_node;
3882 type_mv = TYPE_MAIN_VARIANT (*type);
3883 if (pedantic
3884 && !in_system_header
3885 && type_mv != integer_type_node
3886 && type_mv != unsigned_type_node
3887 && type_mv != boolean_type_node)
3888 pedwarn ("type of bit-field %qs is a GCC extension", name);
3890 if (type_mv == boolean_type_node)
3891 max_width = CHAR_TYPE_SIZE;
3892 else
3893 max_width = TYPE_PRECISION (*type);
3895 if (0 < compare_tree_int (*width, max_width))
3897 error ("width of %qs exceeds its type", name);
3898 w = max_width;
3899 *width = build_int_cst (NULL_TREE, w);
3901 else
3902 w = tree_low_cst (*width, 1);
3904 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3906 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3907 if (!lt
3908 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3909 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3910 warning (0, "%qs is narrower than values of its type", name);
3915 /* Given declspecs and a declarator,
3916 determine the name and type of the object declared
3917 and construct a ..._DECL node for it.
3918 (In one case we can return a ..._TYPE node instead.
3919 For invalid input we sometimes return 0.)
3921 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3923 DECL_CONTEXT says which syntactic context this declaration is in:
3924 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3925 FUNCDEF for a function definition. Like NORMAL but a few different
3926 error messages in each case. Return value may be zero meaning
3927 this definition is too screwy to try to parse.
3928 PARM for a parameter declaration (either within a function prototype
3929 or before a function body). Make a PARM_DECL, or return void_type_node.
3930 TYPENAME if for a typename (in a cast or sizeof).
3931 Don't make a DECL node; just return the ..._TYPE node.
3932 FIELD for a struct or union field; make a FIELD_DECL.
3933 INITIALIZED is true if the decl has an initializer.
3934 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3935 representing the width of the bit-field.
3937 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3938 It may also be so in the PARM case, for a prototype where the
3939 argument type is specified but not the name.
3941 This function is where the complicated C meanings of `static'
3942 and `extern' are interpreted. */
3944 static tree
3945 grokdeclarator (const struct c_declarator *declarator,
3946 struct c_declspecs *declspecs,
3947 enum decl_context decl_context, bool initialized, tree *width)
3949 tree type = declspecs->type;
3950 bool threadp = declspecs->thread_p;
3951 enum c_storage_class storage_class = declspecs->storage_class;
3952 int constp;
3953 int restrictp;
3954 int volatilep;
3955 int type_quals = TYPE_UNQUALIFIED;
3956 const char *name, *orig_name;
3957 tree typedef_type = 0;
3958 bool funcdef_flag = false;
3959 bool funcdef_syntax = false;
3960 int size_varies = 0;
3961 tree decl_attr = declspecs->decl_attr;
3962 int array_ptr_quals = TYPE_UNQUALIFIED;
3963 tree array_ptr_attrs = NULL_TREE;
3964 int array_parm_static = 0;
3965 bool array_parm_vla_unspec_p = false;
3966 tree returned_attrs = NULL_TREE;
3967 bool bitfield = width != NULL;
3968 tree element_type;
3969 struct c_arg_info *arg_info = 0;
3971 if (decl_context == FUNCDEF)
3972 funcdef_flag = true, decl_context = NORMAL;
3974 /* Look inside a declarator for the name being declared
3975 and get it as a string, for an error message. */
3977 const struct c_declarator *decl = declarator;
3978 name = 0;
3980 while (decl)
3981 switch (decl->kind)
3983 case cdk_function:
3984 case cdk_array:
3985 case cdk_pointer:
3986 funcdef_syntax = (decl->kind == cdk_function);
3987 decl = decl->declarator;
3988 break;
3990 case cdk_attrs:
3991 decl = decl->declarator;
3992 break;
3994 case cdk_id:
3995 if (decl->u.id)
3996 name = IDENTIFIER_POINTER (decl->u.id);
3997 decl = 0;
3998 break;
4000 default:
4001 gcc_unreachable ();
4003 orig_name = name;
4004 if (name == 0)
4005 name = "type name";
4008 /* A function definition's declarator must have the form of
4009 a function declarator. */
4011 if (funcdef_flag && !funcdef_syntax)
4012 return 0;
4014 /* If this looks like a function definition, make it one,
4015 even if it occurs where parms are expected.
4016 Then store_parm_decls will reject it and not use it as a parm. */
4017 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4018 decl_context = PARM;
4020 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4021 warn_deprecated_use (declspecs->type);
4023 if ((decl_context == NORMAL || decl_context == FIELD)
4024 && current_scope == file_scope
4025 && variably_modified_type_p (type, NULL_TREE))
4027 error ("variably modified %qs at file scope", name);
4028 type = integer_type_node;
4031 typedef_type = type;
4032 size_varies = C_TYPE_VARIABLE_SIZE (type);
4034 /* Diagnose defaulting to "int". */
4036 if (declspecs->default_int_p && !in_system_header)
4038 /* Issue a warning if this is an ISO C 99 program or if
4039 -Wreturn-type and this is a function, or if -Wimplicit;
4040 prefer the former warning since it is more explicit. */
4041 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4042 && funcdef_flag)
4043 warn_about_return_type = 1;
4044 else if (warn_implicit_int || flag_isoc99)
4045 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
4048 /* Adjust the type if a bit-field is being declared,
4049 -funsigned-bitfields applied and the type is not explicitly
4050 "signed". */
4051 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4052 && TREE_CODE (type) == INTEGER_TYPE)
4053 type = c_common_unsigned_type (type);
4055 /* Figure out the type qualifiers for the declaration. There are
4056 two ways a declaration can become qualified. One is something
4057 like `const int i' where the `const' is explicit. Another is
4058 something like `typedef const int CI; CI i' where the type of the
4059 declaration contains the `const'. A third possibility is that
4060 there is a type qualifier on the element type of a typedefed
4061 array type, in which case we should extract that qualifier so
4062 that c_apply_type_quals_to_decls receives the full list of
4063 qualifiers to work with (C90 is not entirely clear about whether
4064 duplicate qualifiers should be diagnosed in this case, but it
4065 seems most appropriate to do so). */
4066 element_type = strip_array_types (type);
4067 constp = declspecs->const_p + TYPE_READONLY (element_type);
4068 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4069 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4070 if (pedantic && !flag_isoc99)
4072 if (constp > 1)
4073 pedwarn ("duplicate %<const%>");
4074 if (restrictp > 1)
4075 pedwarn ("duplicate %<restrict%>");
4076 if (volatilep > 1)
4077 pedwarn ("duplicate %<volatile%>");
4079 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4080 type = TYPE_MAIN_VARIANT (type);
4081 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4082 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4083 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4085 /* Warn about storage classes that are invalid for certain
4086 kinds of declarations (parameters, typenames, etc.). */
4088 if (funcdef_flag
4089 && (threadp
4090 || storage_class == csc_auto
4091 || storage_class == csc_register
4092 || storage_class == csc_typedef))
4094 if (storage_class == csc_auto
4095 && (pedantic || current_scope == file_scope))
4096 pedwarn ("function definition declared %<auto%>");
4097 if (storage_class == csc_register)
4098 error ("function definition declared %<register%>");
4099 if (storage_class == csc_typedef)
4100 error ("function definition declared %<typedef%>");
4101 if (threadp)
4102 error ("function definition declared %<__thread%>");
4103 threadp = false;
4104 if (storage_class == csc_auto
4105 || storage_class == csc_register
4106 || storage_class == csc_typedef)
4107 storage_class = csc_none;
4109 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4111 if (decl_context == PARM && storage_class == csc_register)
4113 else
4115 switch (decl_context)
4117 case FIELD:
4118 error ("storage class specified for structure field %qs",
4119 name);
4120 break;
4121 case PARM:
4122 error ("storage class specified for parameter %qs", name);
4123 break;
4124 default:
4125 error ("storage class specified for typename");
4126 break;
4128 storage_class = csc_none;
4129 threadp = false;
4132 else if (storage_class == csc_extern
4133 && initialized
4134 && !funcdef_flag)
4136 /* 'extern' with initialization is invalid if not at file scope. */
4137 if (current_scope == file_scope)
4139 /* It is fine to have 'extern const' when compiling at C
4140 and C++ intersection. */
4141 if (!(warn_cxx_compat && constp))
4142 warning (0, "%qs initialized and declared %<extern%>", name);
4144 else
4145 error ("%qs has both %<extern%> and initializer", name);
4147 else if (current_scope == file_scope)
4149 if (storage_class == csc_auto)
4150 error ("file-scope declaration of %qs specifies %<auto%>", name);
4151 if (pedantic && storage_class == csc_register)
4152 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4154 else
4156 if (storage_class == csc_extern && funcdef_flag)
4157 error ("nested function %qs declared %<extern%>", name);
4158 else if (threadp && storage_class == csc_none)
4160 error ("function-scope %qs implicitly auto and declared "
4161 "%<__thread%>",
4162 name);
4163 threadp = false;
4167 /* Now figure out the structure of the declarator proper.
4168 Descend through it, creating more complex types, until we reach
4169 the declared identifier (or NULL_TREE, in an absolute declarator).
4170 At each stage we maintain an unqualified version of the type
4171 together with any qualifiers that should be applied to it with
4172 c_build_qualified_type; this way, array types including
4173 multidimensional array types are first built up in unqualified
4174 form and then the qualified form is created with
4175 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4177 while (declarator && declarator->kind != cdk_id)
4179 if (type == error_mark_node)
4181 declarator = declarator->declarator;
4182 continue;
4185 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4186 a cdk_pointer (for *...),
4187 a cdk_function (for ...(...)),
4188 a cdk_attrs (for nested attributes),
4189 or a cdk_id (for the name being declared
4190 or the place in an absolute declarator
4191 where the name was omitted).
4192 For the last case, we have just exited the loop.
4194 At this point, TYPE is the type of elements of an array,
4195 or for a function to return, or for a pointer to point to.
4196 After this sequence of ifs, TYPE is the type of the
4197 array or function or pointer, and DECLARATOR has had its
4198 outermost layer removed. */
4200 if (array_ptr_quals != TYPE_UNQUALIFIED
4201 || array_ptr_attrs != NULL_TREE
4202 || array_parm_static)
4204 /* Only the innermost declarator (making a parameter be of
4205 array type which is converted to pointer type)
4206 may have static or type qualifiers. */
4207 error ("static or type qualifiers in non-parameter array declarator");
4208 array_ptr_quals = TYPE_UNQUALIFIED;
4209 array_ptr_attrs = NULL_TREE;
4210 array_parm_static = 0;
4213 switch (declarator->kind)
4215 case cdk_attrs:
4217 /* A declarator with embedded attributes. */
4218 tree attrs = declarator->u.attrs;
4219 const struct c_declarator *inner_decl;
4220 int attr_flags = 0;
4221 declarator = declarator->declarator;
4222 inner_decl = declarator;
4223 while (inner_decl->kind == cdk_attrs)
4224 inner_decl = inner_decl->declarator;
4225 if (inner_decl->kind == cdk_id)
4226 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4227 else if (inner_decl->kind == cdk_function)
4228 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4229 else if (inner_decl->kind == cdk_array)
4230 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4231 returned_attrs = decl_attributes (&type,
4232 chainon (returned_attrs, attrs),
4233 attr_flags);
4234 break;
4236 case cdk_array:
4238 tree itype = NULL_TREE;
4239 tree size = declarator->u.array.dimen;
4240 /* The index is a signed object `sizetype' bits wide. */
4241 tree index_type = c_common_signed_type (sizetype);
4243 array_ptr_quals = declarator->u.array.quals;
4244 array_ptr_attrs = declarator->u.array.attrs;
4245 array_parm_static = declarator->u.array.static_p;
4246 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4248 declarator = declarator->declarator;
4250 /* Check for some types that there cannot be arrays of. */
4252 if (VOID_TYPE_P (type))
4254 error ("declaration of %qs as array of voids", name);
4255 type = error_mark_node;
4258 if (TREE_CODE (type) == FUNCTION_TYPE)
4260 error ("declaration of %qs as array of functions", name);
4261 type = error_mark_node;
4264 if (pedantic && !in_system_header && flexible_array_type_p (type))
4265 pedwarn ("invalid use of structure with flexible array member");
4267 if (size == error_mark_node)
4268 type = error_mark_node;
4270 if (type == error_mark_node)
4271 continue;
4273 /* If size was specified, set ITYPE to a range-type for
4274 that size. Otherwise, ITYPE remains null. finish_decl
4275 may figure it out from an initial value. */
4277 if (size)
4279 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4280 lvalue. */
4281 STRIP_TYPE_NOPS (size);
4283 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4285 error ("size of array %qs has non-integer type", name);
4286 size = integer_one_node;
4289 if (pedantic && integer_zerop (size))
4290 pedwarn ("ISO C forbids zero-size array %qs", name);
4292 if (TREE_CODE (size) == INTEGER_CST)
4294 constant_expression_warning (size);
4295 if (tree_int_cst_sgn (size) < 0)
4297 error ("size of array %qs is negative", name);
4298 size = integer_one_node;
4301 else if ((decl_context == NORMAL || decl_context == FIELD)
4302 && current_scope == file_scope)
4304 error ("variably modified %qs at file scope", name);
4305 size = integer_one_node;
4307 else
4309 /* Make sure the array size remains visibly
4310 nonconstant even if it is (eg) a const variable
4311 with known value. */
4312 size_varies = 1;
4314 if (!flag_isoc99 && pedantic)
4316 if (TREE_CONSTANT (size))
4317 pedwarn ("ISO C90 forbids array %qs whose size "
4318 "can%'t be evaluated",
4319 name);
4320 else
4321 pedwarn ("ISO C90 forbids variable-size array %qs",
4322 name);
4326 if (integer_zerop (size))
4328 /* A zero-length array cannot be represented with
4329 an unsigned index type, which is what we'll
4330 get with build_index_type. Create an
4331 open-ended range instead. */
4332 itype = build_range_type (sizetype, size, NULL_TREE);
4334 else
4336 /* Arrange for the SAVE_EXPR on the inside of the
4337 MINUS_EXPR, which allows the -1 to get folded
4338 with the +1 that happens when building TYPE_SIZE. */
4339 if (size_varies)
4340 size = variable_size (size);
4342 /* Compute the maximum valid index, that is, size
4343 - 1. Do the calculation in index_type, so that
4344 if it is a variable the computations will be
4345 done in the proper mode. */
4346 itype = fold_build2 (MINUS_EXPR, index_type,
4347 convert (index_type, size),
4348 convert (index_type,
4349 size_one_node));
4351 /* If that overflowed, the array is too big. ???
4352 While a size of INT_MAX+1 technically shouldn't
4353 cause an overflow (because we subtract 1), the
4354 overflow is recorded during the conversion to
4355 index_type, before the subtraction. Handling
4356 this case seems like an unnecessary
4357 complication. */
4358 if (TREE_CODE (itype) == INTEGER_CST
4359 && TREE_OVERFLOW (itype))
4361 error ("size of array %qs is too large", name);
4362 type = error_mark_node;
4363 continue;
4366 itype = build_index_type (itype);
4369 else if (decl_context == FIELD)
4371 if (pedantic && !flag_isoc99 && !in_system_header)
4372 pedwarn ("ISO C90 does not support flexible array members");
4374 /* ISO C99 Flexible array members are effectively
4375 identical to GCC's zero-length array extension. */
4376 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4378 else if (decl_context == PARM)
4380 if (array_parm_vla_unspec_p)
4382 if (! orig_name)
4384 /* C99 6.7.5.2p4 */
4385 error ("%<[*]%> not allowed in other than a declaration");
4388 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4389 size_varies = 1;
4392 else if (decl_context == TYPENAME)
4394 if (array_parm_vla_unspec_p)
4396 /* The error is printed elsewhere. We use this to
4397 avoid messing up with incomplete array types of
4398 the same type, that would otherwise be modified
4399 below. */
4400 itype = build_range_type (sizetype, size_zero_node,
4401 NULL_TREE);
4405 /* Complain about arrays of incomplete types. */
4406 if (!COMPLETE_TYPE_P (type))
4408 error ("array type has incomplete element type");
4409 type = error_mark_node;
4411 else
4412 /* When itype is NULL, a shared incomplete array type is
4413 returned for all array of a given type. Elsewhere we
4414 make sure we don't complete that type before copying
4415 it, but here we want to make sure we don't ever
4416 modify the shared type, so we gcc_assert (itype)
4417 below. */
4418 type = build_array_type (type, itype);
4420 if (type != error_mark_node)
4422 if (size_varies)
4424 /* It is ok to modify type here even if itype is
4425 NULL: if size_varies, we're in a
4426 multi-dimensional array and the inner type has
4427 variable size, so the enclosing shared array type
4428 must too. */
4429 if (size && TREE_CODE (size) == INTEGER_CST)
4430 type
4431 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4432 C_TYPE_VARIABLE_SIZE (type) = 1;
4435 /* The GCC extension for zero-length arrays differs from
4436 ISO flexible array members in that sizeof yields
4437 zero. */
4438 if (size && integer_zerop (size))
4440 gcc_assert (itype);
4441 TYPE_SIZE (type) = bitsize_zero_node;
4442 TYPE_SIZE_UNIT (type) = size_zero_node;
4444 if (array_parm_vla_unspec_p)
4446 gcc_assert (itype);
4447 /* The type is complete. C99 6.7.5.2p4 */
4448 TYPE_SIZE (type) = bitsize_zero_node;
4449 TYPE_SIZE_UNIT (type) = size_zero_node;
4453 if (decl_context != PARM
4454 && (array_ptr_quals != TYPE_UNQUALIFIED
4455 || array_ptr_attrs != NULL_TREE
4456 || array_parm_static))
4458 error ("static or type qualifiers in non-parameter array declarator");
4459 array_ptr_quals = TYPE_UNQUALIFIED;
4460 array_ptr_attrs = NULL_TREE;
4461 array_parm_static = 0;
4463 break;
4465 case cdk_function:
4467 /* Say it's a definition only for the declarator closest
4468 to the identifier, apart possibly from some
4469 attributes. */
4470 bool really_funcdef = false;
4471 tree arg_types;
4472 if (funcdef_flag)
4474 const struct c_declarator *t = declarator->declarator;
4475 while (t->kind == cdk_attrs)
4476 t = t->declarator;
4477 really_funcdef = (t->kind == cdk_id);
4480 /* Declaring a function type. Make sure we have a valid
4481 type for the function to return. */
4482 if (type == error_mark_node)
4483 continue;
4485 size_varies = 0;
4487 /* Warn about some types functions can't return. */
4488 if (TREE_CODE (type) == FUNCTION_TYPE)
4490 error ("%qs declared as function returning a function", name);
4491 type = integer_type_node;
4493 if (TREE_CODE (type) == ARRAY_TYPE)
4495 error ("%qs declared as function returning an array", name);
4496 type = integer_type_node;
4499 /* Construct the function type and go to the next
4500 inner layer of declarator. */
4501 arg_info = declarator->u.arg_info;
4502 arg_types = grokparms (arg_info, really_funcdef);
4503 if (really_funcdef)
4504 put_pending_sizes (arg_info->pending_sizes);
4506 /* Type qualifiers before the return type of the function
4507 qualify the return type, not the function type. */
4508 if (type_quals)
4510 /* Type qualifiers on a function return type are
4511 normally permitted by the standard but have no
4512 effect, so give a warning at -Wreturn-type.
4513 Qualifiers on a void return type are banned on
4514 function definitions in ISO C; GCC used to used
4515 them for noreturn functions. */
4516 if (VOID_TYPE_P (type) && really_funcdef)
4517 pedwarn ("function definition has qualified void return type");
4518 else
4519 warning (OPT_Wreturn_type,
4520 "type qualifiers ignored on function return type");
4522 type = c_build_qualified_type (type, type_quals);
4524 type_quals = TYPE_UNQUALIFIED;
4526 type = build_function_type (type, arg_types);
4527 declarator = declarator->declarator;
4529 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4530 the formal parameter list of this FUNCTION_TYPE to point to
4531 the FUNCTION_TYPE node itself. */
4533 tree link;
4535 for (link = arg_info->tags;
4536 link;
4537 link = TREE_CHAIN (link))
4538 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4540 break;
4542 case cdk_pointer:
4544 /* Merge any constancy or volatility into the target type
4545 for the pointer. */
4547 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4548 && type_quals)
4549 pedwarn ("ISO C forbids qualified function types");
4550 if (type_quals)
4551 type = c_build_qualified_type (type, type_quals);
4552 size_varies = 0;
4554 /* When the pointed-to type involves components of variable size,
4555 care must be taken to ensure that the size evaluation code is
4556 emitted early enough to dominate all the possible later uses
4557 and late enough for the variables on which it depends to have
4558 been assigned.
4560 This is expected to happen automatically when the pointed-to
4561 type has a name/declaration of it's own, but special attention
4562 is required if the type is anonymous.
4564 We handle the NORMAL and FIELD contexts here by attaching an
4565 artificial TYPE_DECL to such pointed-to type. This forces the
4566 sizes evaluation at a safe point and ensures it is not deferred
4567 until e.g. within a deeper conditional context.
4569 We expect nothing to be needed here for PARM or TYPENAME.
4570 Pushing a TYPE_DECL at this point for TYPENAME would actually
4571 be incorrect, as we might be in the middle of an expression
4572 with side effects on the pointed-to type size "arguments" prior
4573 to the pointer declaration point and the fake TYPE_DECL in the
4574 enclosing context would force the size evaluation prior to the
4575 side effects. */
4577 if (!TYPE_NAME (type)
4578 && (decl_context == NORMAL || decl_context == FIELD)
4579 && variably_modified_type_p (type, NULL_TREE))
4581 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4582 DECL_ARTIFICIAL (decl) = 1;
4583 pushdecl (decl);
4584 finish_decl (decl, NULL_TREE, NULL_TREE);
4585 TYPE_NAME (type) = decl;
4588 type = build_pointer_type (type);
4590 /* Process type qualifiers (such as const or volatile)
4591 that were given inside the `*'. */
4592 type_quals = declarator->u.pointer_quals;
4594 declarator = declarator->declarator;
4595 break;
4597 default:
4598 gcc_unreachable ();
4602 /* Now TYPE has the actual type, apart from any qualifiers in
4603 TYPE_QUALS. */
4605 /* Check the type and width of a bit-field. */
4606 if (bitfield)
4607 check_bitfield_type_and_width (&type, width, orig_name);
4609 /* Did array size calculations overflow? */
4611 if (TREE_CODE (type) == ARRAY_TYPE
4612 && COMPLETE_TYPE_P (type)
4613 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4614 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4616 error ("size of array %qs is too large", name);
4617 /* If we proceed with the array type as it is, we'll eventually
4618 crash in tree_low_cst(). */
4619 type = error_mark_node;
4622 /* If this is declaring a typedef name, return a TYPE_DECL. */
4624 if (storage_class == csc_typedef)
4626 tree decl;
4627 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4628 && type_quals)
4629 pedwarn ("ISO C forbids qualified function types");
4630 if (type_quals)
4631 type = c_build_qualified_type (type, type_quals);
4632 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4633 if (declspecs->explicit_signed_p)
4634 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4635 decl_attributes (&decl, returned_attrs, 0);
4636 if (declspecs->inline_p)
4637 pedwarn ("typedef %q+D declared %<inline%>", decl);
4638 return decl;
4641 /* If this is a type name (such as, in a cast or sizeof),
4642 compute the type and return it now. */
4644 if (decl_context == TYPENAME)
4646 /* Note that the grammar rejects storage classes in typenames
4647 and fields. */
4648 gcc_assert (storage_class == csc_none && !threadp
4649 && !declspecs->inline_p);
4650 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4651 && type_quals)
4652 pedwarn ("ISO C forbids const or volatile function types");
4653 if (type_quals)
4654 type = c_build_qualified_type (type, type_quals);
4655 decl_attributes (&type, returned_attrs, 0);
4656 return type;
4659 if (pedantic && decl_context == FIELD
4660 && variably_modified_type_p (type, NULL_TREE))
4662 /* C99 6.7.2.1p8 */
4663 pedwarn ("a member of a structure or union cannot have a variably modified type");
4666 /* Aside from typedefs and type names (handle above),
4667 `void' at top level (not within pointer)
4668 is allowed only in public variables.
4669 We don't complain about parms either, but that is because
4670 a better error message can be made later. */
4672 if (VOID_TYPE_P (type) && decl_context != PARM
4673 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4674 && (storage_class == csc_extern
4675 || (current_scope == file_scope
4676 && !(storage_class == csc_static
4677 || storage_class == csc_register)))))
4679 error ("variable or field %qs declared void", name);
4680 type = integer_type_node;
4683 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4684 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4687 tree decl;
4689 if (decl_context == PARM)
4691 tree type_as_written;
4692 tree promoted_type;
4694 /* A parameter declared as an array of T is really a pointer to T.
4695 One declared as a function is really a pointer to a function. */
4697 if (TREE_CODE (type) == ARRAY_TYPE)
4699 /* Transfer const-ness of array into that of type pointed to. */
4700 type = TREE_TYPE (type);
4701 if (type_quals)
4702 type = c_build_qualified_type (type, type_quals);
4703 type = build_pointer_type (type);
4704 type_quals = array_ptr_quals;
4706 /* We don't yet implement attributes in this context. */
4707 if (array_ptr_attrs != NULL_TREE)
4708 warning (OPT_Wattributes,
4709 "attributes in parameter array declarator ignored");
4711 size_varies = 0;
4713 else if (TREE_CODE (type) == FUNCTION_TYPE)
4715 if (pedantic && type_quals)
4716 pedwarn ("ISO C forbids qualified function types");
4717 if (type_quals)
4718 type = c_build_qualified_type (type, type_quals);
4719 type = build_pointer_type (type);
4720 type_quals = TYPE_UNQUALIFIED;
4722 else if (type_quals)
4723 type = c_build_qualified_type (type, type_quals);
4725 type_as_written = type;
4727 decl = build_decl (PARM_DECL, declarator->u.id, type);
4728 if (size_varies)
4729 C_DECL_VARIABLE_SIZE (decl) = 1;
4731 /* Compute the type actually passed in the parmlist,
4732 for the case where there is no prototype.
4733 (For example, shorts and chars are passed as ints.)
4734 When there is a prototype, this is overridden later. */
4736 if (type == error_mark_node)
4737 promoted_type = type;
4738 else
4739 promoted_type = c_type_promotes_to (type);
4741 DECL_ARG_TYPE (decl) = promoted_type;
4742 if (declspecs->inline_p)
4743 pedwarn ("parameter %q+D declared %<inline%>", decl);
4745 else if (decl_context == FIELD)
4747 /* Note that the grammar rejects storage classes in typenames
4748 and fields. */
4749 gcc_assert (storage_class == csc_none && !threadp
4750 && !declspecs->inline_p);
4752 /* Structure field. It may not be a function. */
4754 if (TREE_CODE (type) == FUNCTION_TYPE)
4756 error ("field %qs declared as a function", name);
4757 type = build_pointer_type (type);
4759 else if (TREE_CODE (type) != ERROR_MARK
4760 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4762 error ("field %qs has incomplete type", name);
4763 type = error_mark_node;
4765 type = c_build_qualified_type (type, type_quals);
4766 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4767 DECL_NONADDRESSABLE_P (decl) = bitfield;
4769 if (size_varies)
4770 C_DECL_VARIABLE_SIZE (decl) = 1;
4772 else if (TREE_CODE (type) == FUNCTION_TYPE)
4774 if (storage_class == csc_register || threadp)
4776 error ("invalid storage class for function %qs", name);
4778 else if (current_scope != file_scope)
4780 /* Function declaration not at file scope. Storage
4781 classes other than `extern' are not allowed, C99
4782 6.7.1p5, and `extern' makes no difference. However,
4783 GCC allows 'auto', perhaps with 'inline', to support
4784 nested functions. */
4785 if (storage_class == csc_auto)
4787 if (pedantic)
4788 pedwarn ("invalid storage class for function %qs", name);
4790 else if (storage_class == csc_static)
4792 error ("invalid storage class for function %qs", name);
4793 if (funcdef_flag)
4794 storage_class = declspecs->storage_class = csc_none;
4795 else
4796 return 0;
4800 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4801 decl = build_decl_attribute_variant (decl, decl_attr);
4803 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4805 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4806 pedwarn ("ISO C forbids qualified function types");
4808 /* GNU C interprets a volatile-qualified function type to indicate
4809 that the function does not return. */
4810 if ((type_quals & TYPE_QUAL_VOLATILE)
4811 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4812 warning (0, "%<noreturn%> function returns non-void value");
4814 /* Every function declaration is an external reference
4815 (DECL_EXTERNAL) except for those which are not at file
4816 scope and are explicitly declared "auto". This is
4817 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4818 GCC to signify a forward declaration of a nested function. */
4819 if (storage_class == csc_auto && current_scope != file_scope)
4820 DECL_EXTERNAL (decl) = 0;
4821 /* In C99, a function which is declared 'inline' with 'extern'
4822 is not an external reference (which is confusing). It
4823 means that the later definition of the function must be output
4824 in this file, C99 6.7.4p6. In GNU C89, a function declared
4825 'extern inline' is an external reference. */
4826 else if (declspecs->inline_p && storage_class != csc_static)
4827 #if WANT_C99_INLINE_SEMANTICS
4828 DECL_EXTERNAL (decl) = (storage_class == csc_extern) == !flag_isoc99;
4829 #else
4830 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4831 #endif
4832 else
4833 DECL_EXTERNAL (decl) = !initialized;
4835 /* Record absence of global scope for `static' or `auto'. */
4836 TREE_PUBLIC (decl)
4837 = !(storage_class == csc_static || storage_class == csc_auto);
4839 /* For a function definition, record the argument information
4840 block where store_parm_decls will look for it. */
4841 if (funcdef_flag)
4842 current_function_arg_info = arg_info;
4844 if (declspecs->default_int_p)
4845 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4847 /* Record presence of `inline', if it is reasonable. */
4848 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4850 if (declspecs->inline_p)
4851 pedwarn ("cannot inline function %<main%>");
4853 else if (declspecs->inline_p)
4855 /* Record that the function is declared `inline'. */
4856 DECL_DECLARED_INLINE_P (decl) = 1;
4858 /* Do not mark bare declarations as DECL_INLINE. Doing so
4859 in the presence of multiple declarations can result in
4860 the abstract origin pointing between the declarations,
4861 which will confuse dwarf2out. */
4862 if (initialized)
4863 DECL_INLINE (decl) = 1;
4865 /* If -finline-functions, assume it can be inlined. This does
4866 two things: let the function be deferred until it is actually
4867 needed, and let dwarf2 know that the function is inlinable. */
4868 else if (flag_inline_trees == 2 && initialized)
4869 DECL_INLINE (decl) = 1;
4871 else
4873 /* It's a variable. */
4874 /* An uninitialized decl with `extern' is a reference. */
4875 int extern_ref = !initialized && storage_class == csc_extern;
4877 type = c_build_qualified_type (type, type_quals);
4879 /* C99 6.2.2p7: It is invalid (compile-time undefined
4880 behavior) to create an 'extern' declaration for a
4881 variable if there is a global declaration that is
4882 'static' and the global declaration is not visible.
4883 (If the static declaration _is_ currently visible,
4884 the 'extern' declaration is taken to refer to that decl.) */
4885 if (extern_ref && current_scope != file_scope)
4887 tree global_decl = identifier_global_value (declarator->u.id);
4888 tree visible_decl = lookup_name (declarator->u.id);
4890 if (global_decl
4891 && global_decl != visible_decl
4892 && TREE_CODE (global_decl) == VAR_DECL
4893 && !TREE_PUBLIC (global_decl))
4894 error ("variable previously declared %<static%> redeclared "
4895 "%<extern%>");
4898 decl = build_decl (VAR_DECL, declarator->u.id, type);
4899 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4900 if (size_varies)
4901 C_DECL_VARIABLE_SIZE (decl) = 1;
4903 if (declspecs->inline_p)
4904 pedwarn ("variable %q+D declared %<inline%>", decl);
4906 /* At file scope, an initialized extern declaration may follow
4907 a static declaration. In that case, DECL_EXTERNAL will be
4908 reset later in start_decl. */
4909 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4911 /* At file scope, the presence of a `static' or `register' storage
4912 class specifier, or the absence of all storage class specifiers
4913 makes this declaration a definition (perhaps tentative). Also,
4914 the absence of `static' makes it public. */
4915 if (current_scope == file_scope)
4917 TREE_PUBLIC (decl) = storage_class != csc_static;
4918 TREE_STATIC (decl) = !extern_ref;
4920 /* Not at file scope, only `static' makes a static definition. */
4921 else
4923 TREE_STATIC (decl) = (storage_class == csc_static);
4924 TREE_PUBLIC (decl) = extern_ref;
4927 if (threadp)
4928 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
4931 if (storage_class == csc_extern
4932 && variably_modified_type_p (type, NULL_TREE))
4934 /* C99 6.7.5.2p2 */
4935 error ("object with variably modified type must have no linkage");
4938 /* Record `register' declaration for warnings on &
4939 and in case doing stupid register allocation. */
4941 if (storage_class == csc_register)
4943 C_DECL_REGISTER (decl) = 1;
4944 DECL_REGISTER (decl) = 1;
4947 /* Record constancy and volatility. */
4948 c_apply_type_quals_to_decl (type_quals, decl);
4950 /* If a type has volatile components, it should be stored in memory.
4951 Otherwise, the fact that those components are volatile
4952 will be ignored, and would even crash the compiler.
4953 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
4954 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
4955 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
4956 || TREE_CODE (decl) == RESULT_DECL))
4958 /* It is not an error for a structure with volatile fields to
4959 be declared register, but reset DECL_REGISTER since it
4960 cannot actually go in a register. */
4961 int was_reg = C_DECL_REGISTER (decl);
4962 C_DECL_REGISTER (decl) = 0;
4963 DECL_REGISTER (decl) = 0;
4964 c_mark_addressable (decl);
4965 C_DECL_REGISTER (decl) = was_reg;
4968 /* This is the earliest point at which we might know the assembler
4969 name of a variable. Thus, if it's known before this, die horribly. */
4970 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4972 decl_attributes (&decl, returned_attrs, 0);
4974 return decl;
4978 /* Decode the parameter-list info for a function type or function definition.
4979 The argument is the value returned by `get_parm_info' (or made in c-parse.c
4980 if there is an identifier list instead of a parameter decl list).
4981 These two functions are separate because when a function returns
4982 or receives functions then each is called multiple times but the order
4983 of calls is different. The last call to `grokparms' is always the one
4984 that contains the formal parameter names of a function definition.
4986 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4988 FUNCDEF_FLAG is true for a function definition, false for
4989 a mere declaration. A nonempty identifier-list gets an error message
4990 when FUNCDEF_FLAG is false. */
4992 static tree
4993 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4995 tree arg_types = arg_info->types;
4997 if (funcdef_flag && arg_info->had_vla_unspec)
4999 /* A function definition isn't function prototype scope C99 6.2.1p4. */
5000 /* C99 6.7.5.2p4 */
5001 error ("%<[*]%> not allowed in other than function prototype scope");
5004 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5005 warning (OPT_Wstrict_prototypes,
5006 "function declaration isn%'t a prototype");
5008 if (arg_types == error_mark_node)
5009 return 0; /* don't set TYPE_ARG_TYPES in this case */
5011 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5013 if (!funcdef_flag)
5014 pedwarn ("parameter names (without types) in function declaration");
5016 arg_info->parms = arg_info->types;
5017 arg_info->types = 0;
5018 return 0;
5020 else
5022 tree parm, type, typelt;
5023 unsigned int parmno;
5025 /* If there is a parameter of incomplete type in a definition,
5026 this is an error. In a declaration this is valid, and a
5027 struct or union type may be completed later, before any calls
5028 or definition of the function. In the case where the tag was
5029 first declared within the parameter list, a warning has
5030 already been given. If a parameter has void type, then
5031 however the function cannot be defined or called, so
5032 warn. */
5034 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5035 parm;
5036 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5038 type = TREE_VALUE (typelt);
5039 if (type == error_mark_node)
5040 continue;
5042 if (!COMPLETE_TYPE_P (type))
5044 if (funcdef_flag)
5046 if (DECL_NAME (parm))
5047 error ("parameter %u (%q+D) has incomplete type",
5048 parmno, parm);
5049 else
5050 error ("%Jparameter %u has incomplete type",
5051 parm, parmno);
5053 TREE_VALUE (typelt) = error_mark_node;
5054 TREE_TYPE (parm) = error_mark_node;
5056 else if (VOID_TYPE_P (type))
5058 if (DECL_NAME (parm))
5059 warning (0, "parameter %u (%q+D) has void type",
5060 parmno, parm);
5061 else
5062 warning (0, "%Jparameter %u has void type",
5063 parm, parmno);
5067 if (DECL_NAME (parm) && TREE_USED (parm))
5068 warn_if_shadowing (parm);
5070 return arg_types;
5074 /* Take apart the current scope and return a c_arg_info structure with
5075 info on a parameter list just parsed.
5077 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5079 ELLIPSIS being true means the argument list ended in '...' so don't
5080 append a sentinel (void_list_node) to the end of the type-list. */
5082 struct c_arg_info *
5083 get_parm_info (bool ellipsis)
5085 struct c_binding *b = current_scope->bindings;
5086 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5087 struct c_arg_info);
5088 tree parms = 0;
5089 tree tags = 0;
5090 tree types = 0;
5091 tree others = 0;
5093 static bool explained_incomplete_types = false;
5094 bool gave_void_only_once_err = false;
5096 arg_info->parms = 0;
5097 arg_info->tags = 0;
5098 arg_info->types = 0;
5099 arg_info->others = 0;
5100 arg_info->pending_sizes = 0;
5101 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5103 /* The bindings in this scope must not get put into a block.
5104 We will take care of deleting the binding nodes. */
5105 current_scope->bindings = 0;
5107 /* This function is only called if there was *something* on the
5108 parameter list. */
5109 gcc_assert (b);
5111 /* A parameter list consisting solely of 'void' indicates that the
5112 function takes no arguments. But if the 'void' is qualified
5113 (by 'const' or 'volatile'), or has a storage class specifier
5114 ('register'), then the behavior is undefined; issue an error.
5115 Typedefs for 'void' are OK (see DR#157). */
5116 if (b->prev == 0 /* one binding */
5117 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5118 && !DECL_NAME (b->decl) /* anonymous */
5119 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5121 if (TREE_THIS_VOLATILE (b->decl)
5122 || TREE_READONLY (b->decl)
5123 || C_DECL_REGISTER (b->decl))
5124 error ("%<void%> as only parameter may not be qualified");
5126 /* There cannot be an ellipsis. */
5127 if (ellipsis)
5128 error ("%<void%> must be the only parameter");
5130 arg_info->types = void_list_node;
5131 return arg_info;
5134 if (!ellipsis)
5135 types = void_list_node;
5137 /* Break up the bindings list into parms, tags, types, and others;
5138 apply sanity checks; purge the name-to-decl bindings. */
5139 while (b)
5141 tree decl = b->decl;
5142 tree type = TREE_TYPE (decl);
5143 const char *keyword;
5145 switch (TREE_CODE (decl))
5147 case PARM_DECL:
5148 if (b->id)
5150 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5151 I_SYMBOL_BINDING (b->id) = b->shadowed;
5154 /* Check for forward decls that never got their actual decl. */
5155 if (TREE_ASM_WRITTEN (decl))
5156 error ("parameter %q+D has just a forward declaration", decl);
5157 /* Check for (..., void, ...) and issue an error. */
5158 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5160 if (!gave_void_only_once_err)
5162 error ("%<void%> must be the only parameter");
5163 gave_void_only_once_err = true;
5166 else
5168 /* Valid parameter, add it to the list. */
5169 TREE_CHAIN (decl) = parms;
5170 parms = decl;
5172 /* Since there is a prototype, args are passed in their
5173 declared types. The back end may override this later. */
5174 DECL_ARG_TYPE (decl) = type;
5175 types = tree_cons (0, type, types);
5177 break;
5179 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5180 case UNION_TYPE: keyword = "union"; goto tag;
5181 case RECORD_TYPE: keyword = "struct"; goto tag;
5182 tag:
5183 /* Types may not have tag-names, in which case the type
5184 appears in the bindings list with b->id NULL. */
5185 if (b->id)
5187 gcc_assert (I_TAG_BINDING (b->id) == b);
5188 I_TAG_BINDING (b->id) = b->shadowed;
5191 /* Warn about any struct, union or enum tags defined in a
5192 parameter list. The scope of such types is limited to
5193 the parameter list, which is rarely if ever desirable
5194 (it's impossible to call such a function with type-
5195 correct arguments). An anonymous union parm type is
5196 meaningful as a GNU extension, so don't warn for that. */
5197 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5199 if (b->id)
5200 /* The %s will be one of 'struct', 'union', or 'enum'. */
5201 warning (0, "%<%s %E%> declared inside parameter list",
5202 keyword, b->id);
5203 else
5204 /* The %s will be one of 'struct', 'union', or 'enum'. */
5205 warning (0, "anonymous %s declared inside parameter list",
5206 keyword);
5208 if (!explained_incomplete_types)
5210 warning (0, "its scope is only this definition or declaration,"
5211 " which is probably not what you want");
5212 explained_incomplete_types = true;
5216 tags = tree_cons (b->id, decl, tags);
5217 break;
5219 case CONST_DECL:
5220 case TYPE_DECL:
5221 case FUNCTION_DECL:
5222 /* CONST_DECLs appear here when we have an embedded enum,
5223 and TYPE_DECLs appear here when we have an embedded struct
5224 or union. No warnings for this - we already warned about the
5225 type itself. FUNCTION_DECLs appear when there is an implicit
5226 function declaration in the parameter list. */
5228 TREE_CHAIN (decl) = others;
5229 others = decl;
5230 /* fall through */
5232 case ERROR_MARK:
5233 /* error_mark_node appears here when we have an undeclared
5234 variable. Just throw it away. */
5235 if (b->id)
5237 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5238 I_SYMBOL_BINDING (b->id) = b->shadowed;
5240 break;
5242 /* Other things that might be encountered. */
5243 case LABEL_DECL:
5244 case VAR_DECL:
5245 default:
5246 gcc_unreachable ();
5249 b = free_binding_and_advance (b);
5252 arg_info->parms = parms;
5253 arg_info->tags = tags;
5254 arg_info->types = types;
5255 arg_info->others = others;
5256 arg_info->pending_sizes = get_pending_sizes ();
5257 return arg_info;
5260 /* Get the struct, enum or union (CODE says which) with tag NAME.
5261 Define the tag as a forward-reference if it is not defined.
5262 Return a c_typespec structure for the type specifier. */
5264 struct c_typespec
5265 parser_xref_tag (enum tree_code code, tree name)
5267 struct c_typespec ret;
5268 /* If a cross reference is requested, look up the type
5269 already defined for this tag and return it. */
5271 tree ref = lookup_tag (code, name, 0);
5272 /* If this is the right type of tag, return what we found.
5273 (This reference will be shadowed by shadow_tag later if appropriate.)
5274 If this is the wrong type of tag, do not return it. If it was the
5275 wrong type in the same scope, we will have had an error
5276 message already; if in a different scope and declaring
5277 a name, pending_xref_error will give an error message; but if in a
5278 different scope and not declaring a name, this tag should
5279 shadow the previous declaration of a different type of tag, and
5280 this would not work properly if we return the reference found.
5281 (For example, with "struct foo" in an outer scope, "union foo;"
5282 must shadow that tag with a new one of union type.) */
5283 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5284 if (ref && TREE_CODE (ref) == code)
5286 ret.spec = ref;
5287 return ret;
5290 /* If no such tag is yet defined, create a forward-reference node
5291 and record it as the "definition".
5292 When a real declaration of this type is found,
5293 the forward-reference will be altered into a real type. */
5295 ref = make_node (code);
5296 if (code == ENUMERAL_TYPE)
5298 /* Give the type a default layout like unsigned int
5299 to avoid crashing if it does not get defined. */
5300 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5301 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5302 TYPE_USER_ALIGN (ref) = 0;
5303 TYPE_UNSIGNED (ref) = 1;
5304 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5305 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5306 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5309 pushtag (name, ref);
5311 ret.spec = ref;
5312 return ret;
5315 /* Get the struct, enum or union (CODE says which) with tag NAME.
5316 Define the tag as a forward-reference if it is not defined.
5317 Return a tree for the type. */
5319 tree
5320 xref_tag (enum tree_code code, tree name)
5322 return parser_xref_tag (code, name).spec;
5325 /* Make sure that the tag NAME is defined *in the current scope*
5326 at least as a forward reference.
5327 CODE says which kind of tag NAME ought to be. */
5329 tree
5330 start_struct (enum tree_code code, tree name)
5332 /* If there is already a tag defined at this scope
5333 (as a forward reference), just return it. */
5335 tree ref = 0;
5337 if (name != 0)
5338 ref = lookup_tag (code, name, 1);
5339 if (ref && TREE_CODE (ref) == code)
5341 if (TYPE_SIZE (ref))
5343 if (code == UNION_TYPE)
5344 error ("redefinition of %<union %E%>", name);
5345 else
5346 error ("redefinition of %<struct %E%>", name);
5348 else if (C_TYPE_BEING_DEFINED (ref))
5350 if (code == UNION_TYPE)
5351 error ("nested redefinition of %<union %E%>", name);
5352 else
5353 error ("nested redefinition of %<struct %E%>", name);
5354 /* Don't create structures that contain themselves. */
5355 ref = NULL_TREE;
5359 /* Otherwise create a forward-reference just so the tag is in scope. */
5361 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5363 ref = make_node (code);
5364 pushtag (name, ref);
5367 C_TYPE_BEING_DEFINED (ref) = 1;
5368 TYPE_PACKED (ref) = flag_pack_struct;
5369 return ref;
5372 /* Process the specs, declarator and width (NULL if omitted)
5373 of a structure component, returning a FIELD_DECL node.
5374 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5376 This is done during the parsing of the struct declaration.
5377 The FIELD_DECL nodes are chained together and the lot of them
5378 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5380 tree
5381 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5382 tree width)
5384 tree value;
5386 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5387 && width == NULL_TREE)
5389 /* This is an unnamed decl.
5391 If we have something of the form "union { list } ;" then this
5392 is the anonymous union extension. Similarly for struct.
5394 If this is something of the form "struct foo;", then
5395 If MS extensions are enabled, this is handled as an
5396 anonymous struct.
5397 Otherwise this is a forward declaration of a structure tag.
5399 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5400 If MS extensions are enabled and foo names a structure, then
5401 again this is an anonymous struct.
5402 Otherwise this is an error.
5404 Oh what a horrid tangled web we weave. I wonder if MS consciously
5405 took this from Plan 9 or if it was an accident of implementation
5406 that took root before someone noticed the bug... */
5408 tree type = declspecs->type;
5409 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5410 || TREE_CODE (type) == UNION_TYPE);
5411 bool ok = false;
5413 if (type_ok
5414 && (flag_ms_extensions || !declspecs->typedef_p))
5416 if (flag_ms_extensions)
5417 ok = true;
5418 else if (flag_iso)
5419 ok = false;
5420 else if (TYPE_NAME (type) == NULL)
5421 ok = true;
5422 else
5423 ok = false;
5425 if (!ok)
5427 pedwarn ("declaration does not declare anything");
5428 return NULL_TREE;
5430 if (pedantic)
5431 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5434 value = grokdeclarator (declarator, declspecs, FIELD, false,
5435 width ? &width : NULL);
5437 finish_decl (value, NULL_TREE, NULL_TREE);
5438 DECL_INITIAL (value) = width;
5440 return value;
5443 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5444 the list such that this does not present a problem later. */
5446 static void
5447 detect_field_duplicates (tree fieldlist)
5449 tree x, y;
5450 int timeout = 10;
5452 /* First, see if there are more than "a few" fields.
5453 This is trivially true if there are zero or one fields. */
5454 if (!fieldlist)
5455 return;
5456 x = TREE_CHAIN (fieldlist);
5457 if (!x)
5458 return;
5459 do {
5460 timeout--;
5461 x = TREE_CHAIN (x);
5462 } while (timeout > 0 && x);
5464 /* If there were "few" fields, avoid the overhead of allocating
5465 a hash table. Instead just do the nested traversal thing. */
5466 if (timeout > 0)
5468 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5469 if (DECL_NAME (x))
5471 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5472 if (DECL_NAME (y) == DECL_NAME (x))
5474 error ("duplicate member %q+D", x);
5475 DECL_NAME (x) = NULL_TREE;
5479 else
5481 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5482 void **slot;
5484 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5485 if ((y = DECL_NAME (x)) != 0)
5487 slot = htab_find_slot (htab, y, INSERT);
5488 if (*slot)
5490 error ("duplicate member %q+D", x);
5491 DECL_NAME (x) = NULL_TREE;
5493 *slot = y;
5496 htab_delete (htab);
5500 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5501 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5502 ATTRIBUTES are attributes to be applied to the structure. */
5504 tree
5505 finish_struct (tree t, tree fieldlist, tree attributes)
5507 tree x;
5508 bool toplevel = file_scope == current_scope;
5509 int saw_named_field;
5511 /* If this type was previously laid out as a forward reference,
5512 make sure we lay it out again. */
5514 TYPE_SIZE (t) = 0;
5516 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5518 if (pedantic)
5520 for (x = fieldlist; x; x = TREE_CHAIN (x))
5521 if (DECL_NAME (x) != 0)
5522 break;
5524 if (x == 0)
5526 if (TREE_CODE (t) == UNION_TYPE)
5528 if (fieldlist)
5529 pedwarn ("union has no named members");
5530 else
5531 pedwarn ("union has no members");
5533 else
5535 if (fieldlist)
5536 pedwarn ("struct has no named members");
5537 else
5538 pedwarn ("struct has no members");
5543 /* Install struct as DECL_CONTEXT of each field decl.
5544 Also process specified field sizes, found in the DECL_INITIAL,
5545 storing 0 there after the type has been changed to precision equal
5546 to its width, rather than the precision of the specified standard
5547 type. (Correct layout requires the original type to have been preserved
5548 until now.) */
5550 saw_named_field = 0;
5551 for (x = fieldlist; x; x = TREE_CHAIN (x))
5553 if (TREE_TYPE (x) == error_mark_node)
5554 continue;
5556 DECL_CONTEXT (x) = t;
5558 if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
5559 DECL_PACKED (x) = 1;
5561 /* If any field is const, the structure type is pseudo-const. */
5562 if (TREE_READONLY (x))
5563 C_TYPE_FIELDS_READONLY (t) = 1;
5564 else
5566 /* A field that is pseudo-const makes the structure likewise. */
5567 tree t1 = TREE_TYPE (x);
5568 while (TREE_CODE (t1) == ARRAY_TYPE)
5569 t1 = TREE_TYPE (t1);
5570 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5571 && C_TYPE_FIELDS_READONLY (t1))
5572 C_TYPE_FIELDS_READONLY (t) = 1;
5575 /* Any field that is volatile means variables of this type must be
5576 treated in some ways as volatile. */
5577 if (TREE_THIS_VOLATILE (x))
5578 C_TYPE_FIELDS_VOLATILE (t) = 1;
5580 /* Any field of nominal variable size implies structure is too. */
5581 if (C_DECL_VARIABLE_SIZE (x))
5582 C_TYPE_VARIABLE_SIZE (t) = 1;
5584 if (DECL_INITIAL (x))
5586 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5587 DECL_SIZE (x) = bitsize_int (width);
5588 DECL_BIT_FIELD (x) = 1;
5589 SET_DECL_C_BIT_FIELD (x);
5592 /* Detect flexible array member in an invalid context. */
5593 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5594 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5595 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5596 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5598 if (TREE_CODE (t) == UNION_TYPE)
5600 error ("%Jflexible array member in union", x);
5601 TREE_TYPE (x) = error_mark_node;
5603 else if (TREE_CHAIN (x) != NULL_TREE)
5605 error ("%Jflexible array member not at end of struct", x);
5606 TREE_TYPE (x) = error_mark_node;
5608 else if (!saw_named_field)
5610 error ("%Jflexible array member in otherwise empty struct", x);
5611 TREE_TYPE (x) = error_mark_node;
5615 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5616 && flexible_array_type_p (TREE_TYPE (x)))
5617 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5619 if (DECL_NAME (x))
5620 saw_named_field = 1;
5623 detect_field_duplicates (fieldlist);
5625 /* Now we have the nearly final fieldlist. Record it,
5626 then lay out the structure or union (including the fields). */
5628 TYPE_FIELDS (t) = fieldlist;
5630 layout_type (t);
5632 /* Give bit-fields their proper types. */
5634 tree *fieldlistp = &fieldlist;
5635 while (*fieldlistp)
5636 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5637 && TREE_TYPE (*fieldlistp) != error_mark_node)
5639 unsigned HOST_WIDE_INT width
5640 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5641 tree type = TREE_TYPE (*fieldlistp);
5642 if (width != TYPE_PRECISION (type))
5644 TREE_TYPE (*fieldlistp)
5645 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5646 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5648 DECL_INITIAL (*fieldlistp) = 0;
5650 else
5651 fieldlistp = &TREE_CHAIN (*fieldlistp);
5654 /* Now we have the truly final field list.
5655 Store it in this type and in the variants. */
5657 TYPE_FIELDS (t) = fieldlist;
5659 /* If there are lots of fields, sort so we can look through them fast.
5660 We arbitrarily consider 16 or more elts to be "a lot". */
5663 int len = 0;
5665 for (x = fieldlist; x; x = TREE_CHAIN (x))
5667 if (len > 15 || DECL_NAME (x) == NULL)
5668 break;
5669 len += 1;
5672 if (len > 15)
5674 tree *field_array;
5675 struct lang_type *space;
5676 struct sorted_fields_type *space2;
5678 len += list_length (x);
5680 /* Use the same allocation policy here that make_node uses, to
5681 ensure that this lives as long as the rest of the struct decl.
5682 All decls in an inline function need to be saved. */
5684 space = GGC_CNEW (struct lang_type);
5685 space2 = GGC_NEWVAR (struct sorted_fields_type,
5686 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5688 len = 0;
5689 space->s = space2;
5690 field_array = &space2->elts[0];
5691 for (x = fieldlist; x; x = TREE_CHAIN (x))
5693 field_array[len++] = x;
5695 /* If there is anonymous struct or union, break out of the loop. */
5696 if (DECL_NAME (x) == NULL)
5697 break;
5699 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5700 if (x == NULL)
5702 TYPE_LANG_SPECIFIC (t) = space;
5703 TYPE_LANG_SPECIFIC (t)->s->len = len;
5704 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5705 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5710 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5712 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5713 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5714 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5715 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5716 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5719 /* If this was supposed to be a transparent union, but we can't
5720 make it one, warn and turn off the flag. */
5721 if (TREE_CODE (t) == UNION_TYPE
5722 && TYPE_TRANSPARENT_UNION (t)
5723 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5725 TYPE_TRANSPARENT_UNION (t) = 0;
5726 warning (0, "union cannot be made transparent");
5729 /* If this structure or union completes the type of any previous
5730 variable declaration, lay it out and output its rtl. */
5731 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5733 x = TREE_CHAIN (x))
5735 tree decl = TREE_VALUE (x);
5736 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5737 layout_array_type (TREE_TYPE (decl));
5738 if (TREE_CODE (decl) != TYPE_DECL)
5740 layout_decl (decl, 0);
5741 if (c_dialect_objc ())
5742 objc_check_decl (decl);
5743 rest_of_decl_compilation (decl, toplevel, 0);
5744 if (!toplevel)
5745 expand_decl (decl);
5748 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5750 /* Finish debugging output for this type. */
5751 rest_of_type_compilation (t, toplevel);
5753 /* If we're inside a function proper, i.e. not file-scope and not still
5754 parsing parameters, then arrange for the size of a variable sized type
5755 to be bound now. */
5756 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5757 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5759 return t;
5762 /* Lay out the type T, and its element type, and so on. */
5764 static void
5765 layout_array_type (tree t)
5767 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5768 layout_array_type (TREE_TYPE (t));
5769 layout_type (t);
5772 /* Begin compiling the definition of an enumeration type.
5773 NAME is its name (or null if anonymous).
5774 Returns the type object, as yet incomplete.
5775 Also records info about it so that build_enumerator
5776 may be used to declare the individual values as they are read. */
5778 tree
5779 start_enum (tree name)
5781 tree enumtype = 0;
5783 /* If this is the real definition for a previous forward reference,
5784 fill in the contents in the same object that used to be the
5785 forward reference. */
5787 if (name != 0)
5788 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5790 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5792 enumtype = make_node (ENUMERAL_TYPE);
5793 pushtag (name, enumtype);
5796 if (C_TYPE_BEING_DEFINED (enumtype))
5797 error ("nested redefinition of %<enum %E%>", name);
5799 C_TYPE_BEING_DEFINED (enumtype) = 1;
5801 if (TYPE_VALUES (enumtype) != 0)
5803 /* This enum is a named one that has been declared already. */
5804 error ("redeclaration of %<enum %E%>", name);
5806 /* Completely replace its old definition.
5807 The old enumerators remain defined, however. */
5808 TYPE_VALUES (enumtype) = 0;
5811 enum_next_value = integer_zero_node;
5812 enum_overflow = 0;
5814 if (flag_short_enums)
5815 TYPE_PACKED (enumtype) = 1;
5817 return enumtype;
5820 /* After processing and defining all the values of an enumeration type,
5821 install their decls in the enumeration type and finish it off.
5822 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5823 and ATTRIBUTES are the specified attributes.
5824 Returns ENUMTYPE. */
5826 tree
5827 finish_enum (tree enumtype, tree values, tree attributes)
5829 tree pair, tem;
5830 tree minnode = 0, maxnode = 0;
5831 int precision, unsign;
5832 bool toplevel = (file_scope == current_scope);
5833 struct lang_type *lt;
5835 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5837 /* Calculate the maximum value of any enumerator in this type. */
5839 if (values == error_mark_node)
5840 minnode = maxnode = integer_zero_node;
5841 else
5843 minnode = maxnode = TREE_VALUE (values);
5844 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5846 tree value = TREE_VALUE (pair);
5847 if (tree_int_cst_lt (maxnode, value))
5848 maxnode = value;
5849 if (tree_int_cst_lt (value, minnode))
5850 minnode = value;
5854 /* Construct the final type of this enumeration. It is the same
5855 as one of the integral types - the narrowest one that fits, except
5856 that normally we only go as narrow as int - and signed iff any of
5857 the values are negative. */
5858 unsign = (tree_int_cst_sgn (minnode) >= 0);
5859 precision = MAX (min_precision (minnode, unsign),
5860 min_precision (maxnode, unsign));
5862 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5864 tem = c_common_type_for_size (precision, unsign);
5865 if (tem == NULL)
5867 warning (0, "enumeration values exceed range of largest integer");
5868 tem = long_long_integer_type_node;
5871 else
5872 tem = unsign ? unsigned_type_node : integer_type_node;
5874 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5875 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5876 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5877 TYPE_SIZE (enumtype) = 0;
5879 /* If the precision of the type was specific with an attribute and it
5880 was too small, give an error. Otherwise, use it. */
5881 if (TYPE_PRECISION (enumtype))
5883 if (precision > TYPE_PRECISION (enumtype))
5884 error ("specified mode too small for enumeral values");
5886 else
5887 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5889 layout_type (enumtype);
5891 if (values != error_mark_node)
5893 /* Change the type of the enumerators to be the enum type. We
5894 need to do this irrespective of the size of the enum, for
5895 proper type checking. Replace the DECL_INITIALs of the
5896 enumerators, and the value slots of the list, with copies
5897 that have the enum type; they cannot be modified in place
5898 because they may be shared (e.g. integer_zero_node) Finally,
5899 change the purpose slots to point to the names of the decls. */
5900 for (pair = values; pair; pair = TREE_CHAIN (pair))
5902 tree enu = TREE_PURPOSE (pair);
5903 tree ini = DECL_INITIAL (enu);
5905 TREE_TYPE (enu) = enumtype;
5907 /* The ISO C Standard mandates enumerators to have type int,
5908 even though the underlying type of an enum type is
5909 unspecified. Here we convert any enumerators that fit in
5910 an int to type int, to avoid promotions to unsigned types
5911 when comparing integers with enumerators that fit in the
5912 int range. When -pedantic is given, build_enumerator()
5913 would have already taken care of those that don't fit. */
5914 if (int_fits_type_p (ini, integer_type_node))
5915 tem = integer_type_node;
5916 else
5917 tem = enumtype;
5918 ini = convert (tem, ini);
5920 DECL_INITIAL (enu) = ini;
5921 TREE_PURPOSE (pair) = DECL_NAME (enu);
5922 TREE_VALUE (pair) = ini;
5925 TYPE_VALUES (enumtype) = values;
5928 /* Record the min/max values so that we can warn about bit-field
5929 enumerations that are too small for the values. */
5930 lt = GGC_CNEW (struct lang_type);
5931 lt->enum_min = minnode;
5932 lt->enum_max = maxnode;
5933 TYPE_LANG_SPECIFIC (enumtype) = lt;
5935 /* Fix up all variant types of this enum type. */
5936 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5938 if (tem == enumtype)
5939 continue;
5940 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5941 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5942 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5943 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5944 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5945 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5946 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5947 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5948 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5949 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5950 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5953 /* Finish debugging output for this type. */
5954 rest_of_type_compilation (enumtype, toplevel);
5956 return enumtype;
5959 /* Build and install a CONST_DECL for one value of the
5960 current enumeration type (one that was begun with start_enum).
5961 Return a tree-list containing the CONST_DECL and its value.
5962 Assignment of sequential values by default is handled here. */
5964 tree
5965 build_enumerator (tree name, tree value)
5967 tree decl, type;
5969 /* Validate and default VALUE. */
5971 if (value != 0)
5973 /* Don't issue more errors for error_mark_node (i.e. an
5974 undeclared identifier) - just ignore the value expression. */
5975 if (value == error_mark_node)
5976 value = 0;
5977 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5978 || TREE_CODE (value) != INTEGER_CST)
5980 error ("enumerator value for %qE is not an integer constant", name);
5981 value = 0;
5983 else
5985 value = default_conversion (value);
5986 constant_expression_warning (value);
5990 /* Default based on previous value. */
5991 /* It should no longer be possible to have NON_LVALUE_EXPR
5992 in the default. */
5993 if (value == 0)
5995 value = enum_next_value;
5996 if (enum_overflow)
5997 error ("overflow in enumeration values");
6000 if (pedantic && !int_fits_type_p (value, integer_type_node))
6002 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
6003 /* XXX This causes -pedantic to change the meaning of the program.
6004 Remove? -zw 2004-03-15 */
6005 value = convert (integer_type_node, value);
6008 /* Set basis for default for next value. */
6009 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
6010 enum_overflow = tree_int_cst_lt (enum_next_value, value);
6012 /* Now create a declaration for the enum value name. */
6014 type = TREE_TYPE (value);
6015 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6016 TYPE_PRECISION (integer_type_node)),
6017 (TYPE_PRECISION (type)
6018 >= TYPE_PRECISION (integer_type_node)
6019 && TYPE_UNSIGNED (type)));
6021 decl = build_decl (CONST_DECL, name, type);
6022 DECL_INITIAL (decl) = convert (type, value);
6023 pushdecl (decl);
6025 return tree_cons (decl, value, NULL_TREE);
6029 /* Create the FUNCTION_DECL for a function definition.
6030 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6031 the declaration; they describe the function's name and the type it returns,
6032 but twisted together in a fashion that parallels the syntax of C.
6034 This function creates a binding context for the function body
6035 as well as setting up the FUNCTION_DECL in current_function_decl.
6037 Returns 1 on success. If the DECLARATOR is not suitable for a function
6038 (it defines a datum instead), we return 0, which tells
6039 yyparse to report a parse error. */
6042 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6043 tree attributes)
6045 tree decl1, old_decl;
6046 tree restype, resdecl;
6047 struct c_label_context_se *nstack_se;
6048 struct c_label_context_vm *nstack_vm;
6050 current_function_returns_value = 0; /* Assume, until we see it does. */
6051 current_function_returns_null = 0;
6052 current_function_returns_abnormally = 0;
6053 warn_about_return_type = 0;
6054 c_switch_stack = NULL;
6056 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6057 nstack_se->labels_def = NULL;
6058 nstack_se->labels_used = NULL;
6059 nstack_se->next = label_context_stack_se;
6060 label_context_stack_se = nstack_se;
6062 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6063 nstack_vm->labels_def = NULL;
6064 nstack_vm->labels_used = NULL;
6065 nstack_vm->scope = 0;
6066 nstack_vm->next = label_context_stack_vm;
6067 label_context_stack_vm = nstack_vm;
6069 /* Indicate no valid break/continue context by setting these variables
6070 to some non-null, non-label value. We'll notice and emit the proper
6071 error message in c_finish_bc_stmt. */
6072 c_break_label = c_cont_label = size_zero_node;
6074 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
6076 /* If the declarator is not suitable for a function definition,
6077 cause a syntax error. */
6078 if (decl1 == 0)
6080 label_context_stack_se = label_context_stack_se->next;
6081 label_context_stack_vm = label_context_stack_vm->next;
6082 return 0;
6085 decl_attributes (&decl1, attributes, 0);
6087 if (DECL_DECLARED_INLINE_P (decl1)
6088 && DECL_UNINLINABLE (decl1)
6089 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6090 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6091 decl1);
6093 #if WANT_C99_INLINE_SEMANTICS
6094 /* Handle gnu_inline attribute. */
6095 if (declspecs->inline_p
6096 && flag_isoc99
6097 && TREE_CODE (decl1) == FUNCTION_DECL
6098 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1)))
6100 if (declspecs->storage_class != csc_static)
6101 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6103 #endif /* WANT_C99_INLINE_SEMANTICS */
6105 announce_function (decl1);
6107 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6109 error ("return type is an incomplete type");
6110 /* Make it return void instead. */
6111 TREE_TYPE (decl1)
6112 = build_function_type (void_type_node,
6113 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6116 if (warn_about_return_type)
6117 pedwarn_c99 ("return type defaults to %<int%>");
6119 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6120 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6121 DECL_INITIAL (decl1) = error_mark_node;
6123 /* If this definition isn't a prototype and we had a prototype declaration
6124 before, copy the arg type info from that prototype. */
6125 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6126 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6127 old_decl = 0;
6128 current_function_prototype_locus = UNKNOWN_LOCATION;
6129 current_function_prototype_built_in = false;
6130 current_function_prototype_arg_types = NULL_TREE;
6131 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6133 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6134 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6135 TREE_TYPE (TREE_TYPE (old_decl))))
6137 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6138 TREE_TYPE (decl1));
6139 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6140 current_function_prototype_built_in
6141 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6142 current_function_prototype_arg_types
6143 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6145 if (TREE_PUBLIC (decl1))
6147 /* If there is an external prototype declaration of this
6148 function, record its location but do not copy information
6149 to this decl. This may be an invisible declaration
6150 (built-in or in a scope which has finished) or simply
6151 have more refined argument types than any declaration
6152 found above. */
6153 struct c_binding *b;
6154 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6155 if (B_IN_SCOPE (b, external_scope))
6156 break;
6157 if (b)
6159 tree ext_decl, ext_type;
6160 ext_decl = b->decl;
6161 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6162 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6163 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6164 TREE_TYPE (ext_type)))
6166 current_function_prototype_locus
6167 = DECL_SOURCE_LOCATION (ext_decl);
6168 current_function_prototype_built_in
6169 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6170 current_function_prototype_arg_types
6171 = TYPE_ARG_TYPES (ext_type);
6177 /* Optionally warn of old-fashioned def with no previous prototype. */
6178 if (warn_strict_prototypes
6179 && old_decl != error_mark_node
6180 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6181 && C_DECL_ISNT_PROTOTYPE (old_decl))
6182 warning (OPT_Wstrict_prototypes,
6183 "function declaration isn%'t a prototype");
6184 /* Optionally warn of any global def with no previous prototype. */
6185 else if (warn_missing_prototypes
6186 && old_decl != error_mark_node
6187 && TREE_PUBLIC (decl1)
6188 && !MAIN_NAME_P (DECL_NAME (decl1))
6189 && C_DECL_ISNT_PROTOTYPE (old_decl))
6190 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6191 /* Optionally warn of any def with no previous prototype
6192 if the function has already been used. */
6193 else if (warn_missing_prototypes
6194 && old_decl != 0
6195 && old_decl != error_mark_node
6196 && TREE_USED (old_decl)
6197 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6198 warning (OPT_Wmissing_prototypes,
6199 "%q+D was used with no prototype before its definition", decl1);
6200 /* Optionally warn of any global def with no previous declaration. */
6201 else if (warn_missing_declarations
6202 && TREE_PUBLIC (decl1)
6203 && old_decl == 0
6204 && !MAIN_NAME_P (DECL_NAME (decl1)))
6205 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6206 decl1);
6207 /* Optionally warn of any def with no previous declaration
6208 if the function has already been used. */
6209 else if (warn_missing_declarations
6210 && old_decl != 0
6211 && old_decl != error_mark_node
6212 && TREE_USED (old_decl)
6213 && C_DECL_IMPLICIT (old_decl))
6214 warning (OPT_Wmissing_declarations,
6215 "%q+D was used with no declaration before its definition", decl1);
6217 /* This function exists in static storage.
6218 (This does not mean `static' in the C sense!) */
6219 TREE_STATIC (decl1) = 1;
6221 /* A nested function is not global. */
6222 if (current_function_decl != 0)
6223 TREE_PUBLIC (decl1) = 0;
6225 /* This is the earliest point at which we might know the assembler
6226 name of the function. Thus, if it's set before this, die horribly. */
6227 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6229 /* If #pragma weak was used, mark the decl weak now. */
6230 if (current_scope == file_scope)
6231 maybe_apply_pragma_weak (decl1);
6233 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6234 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6236 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6237 != integer_type_node)
6238 pedwarn ("return type of %q+D is not %<int%>", decl1);
6240 check_main_parameter_types(decl1);
6242 if (!TREE_PUBLIC (decl1))
6243 pedwarn ("%q+D is normally a non-static function", decl1);
6246 /* Record the decl so that the function name is defined.
6247 If we already have a decl for this name, and it is a FUNCTION_DECL,
6248 use the old decl. */
6250 current_function_decl = pushdecl (decl1);
6252 push_scope ();
6253 declare_parm_level ();
6255 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6256 /* Promote the value to int before returning it. */
6257 if (c_promoting_integer_type_p (restype))
6259 /* It retains unsignedness if not really getting wider. */
6260 if (TYPE_UNSIGNED (restype)
6261 && (TYPE_PRECISION (restype)
6262 == TYPE_PRECISION (integer_type_node)))
6263 restype = unsigned_type_node;
6264 else
6265 restype = integer_type_node;
6268 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6269 DECL_ARTIFICIAL (resdecl) = 1;
6270 DECL_IGNORED_P (resdecl) = 1;
6271 DECL_RESULT (current_function_decl) = resdecl;
6273 start_fname_decls ();
6275 return 1;
6278 /* Subroutine of store_parm_decls which handles new-style function
6279 definitions (prototype format). The parms already have decls, so we
6280 need only record them as in effect and complain if any redundant
6281 old-style parm decls were written. */
6282 static void
6283 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6285 tree decl;
6287 if (current_scope->bindings)
6289 error ("%Jold-style parameter declarations in prototyped "
6290 "function definition", fndecl);
6292 /* Get rid of the old-style declarations. */
6293 pop_scope ();
6294 push_scope ();
6296 /* Don't issue this warning for nested functions, and don't issue this
6297 warning if we got here because ARG_INFO_TYPES was error_mark_node
6298 (this happens when a function definition has just an ellipsis in
6299 its parameter list). */
6300 else if (!in_system_header && !current_function_scope
6301 && arg_info->types != error_mark_node)
6302 warning (OPT_Wtraditional,
6303 "%Jtraditional C rejects ISO C style function definitions",
6304 fndecl);
6306 /* Now make all the parameter declarations visible in the function body.
6307 We can bypass most of the grunt work of pushdecl. */
6308 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6310 DECL_CONTEXT (decl) = current_function_decl;
6311 if (DECL_NAME (decl))
6313 bind (DECL_NAME (decl), decl, current_scope,
6314 /*invisible=*/false, /*nested=*/false);
6315 if (!TREE_USED (decl))
6316 warn_if_shadowing (decl);
6318 else
6319 error ("%Jparameter name omitted", decl);
6322 /* Record the parameter list in the function declaration. */
6323 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6325 /* Now make all the ancillary declarations visible, likewise. */
6326 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6328 DECL_CONTEXT (decl) = current_function_decl;
6329 if (DECL_NAME (decl))
6330 bind (DECL_NAME (decl), decl, current_scope,
6331 /*invisible=*/false, /*nested=*/false);
6334 /* And all the tag declarations. */
6335 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6336 if (TREE_PURPOSE (decl))
6337 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6338 /*invisible=*/false, /*nested=*/false);
6341 /* Subroutine of store_parm_decls which handles old-style function
6342 definitions (separate parameter list and declarations). */
6344 static void
6345 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6347 struct c_binding *b;
6348 tree parm, decl, last;
6349 tree parmids = arg_info->parms;
6350 struct pointer_set_t *seen_args = pointer_set_create ();
6352 if (!in_system_header)
6353 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6354 fndecl);
6356 /* Match each formal parameter name with its declaration. Save each
6357 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6358 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6360 if (TREE_VALUE (parm) == 0)
6362 error ("%Jparameter name missing from parameter list", fndecl);
6363 TREE_PURPOSE (parm) = 0;
6364 continue;
6367 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6368 if (b && B_IN_CURRENT_SCOPE (b))
6370 decl = b->decl;
6371 /* If we got something other than a PARM_DECL it is an error. */
6372 if (TREE_CODE (decl) != PARM_DECL)
6373 error ("%q+D declared as a non-parameter", decl);
6374 /* If the declaration is already marked, we have a duplicate
6375 name. Complain and ignore the duplicate. */
6376 else if (pointer_set_contains (seen_args, decl))
6378 error ("multiple parameters named %q+D", decl);
6379 TREE_PURPOSE (parm) = 0;
6380 continue;
6382 /* If the declaration says "void", complain and turn it into
6383 an int. */
6384 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6386 error ("parameter %q+D declared with void type", decl);
6387 TREE_TYPE (decl) = integer_type_node;
6388 DECL_ARG_TYPE (decl) = integer_type_node;
6389 layout_decl (decl, 0);
6391 warn_if_shadowing (decl);
6393 /* If no declaration found, default to int. */
6394 else
6396 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6397 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6398 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6399 pushdecl (decl);
6400 warn_if_shadowing (decl);
6402 if (flag_isoc99)
6403 pedwarn ("type of %q+D defaults to %<int%>", decl);
6404 else
6405 warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
6408 TREE_PURPOSE (parm) = decl;
6409 pointer_set_insert (seen_args, decl);
6412 /* Now examine the parms chain for incomplete declarations
6413 and declarations with no corresponding names. */
6415 for (b = current_scope->bindings; b; b = b->prev)
6417 parm = b->decl;
6418 if (TREE_CODE (parm) != PARM_DECL)
6419 continue;
6421 if (TREE_TYPE (parm) != error_mark_node
6422 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6424 error ("parameter %q+D has incomplete type", parm);
6425 TREE_TYPE (parm) = error_mark_node;
6428 if (!pointer_set_contains (seen_args, parm))
6430 error ("declaration for parameter %q+D but no such parameter", parm);
6432 /* Pretend the parameter was not missing.
6433 This gets us to a standard state and minimizes
6434 further error messages. */
6435 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6439 /* Chain the declarations together in the order of the list of
6440 names. Store that chain in the function decl, replacing the
6441 list of names. Update the current scope to match. */
6442 DECL_ARGUMENTS (fndecl) = 0;
6444 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6445 if (TREE_PURPOSE (parm))
6446 break;
6447 if (parm && TREE_PURPOSE (parm))
6449 last = TREE_PURPOSE (parm);
6450 DECL_ARGUMENTS (fndecl) = last;
6452 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6453 if (TREE_PURPOSE (parm))
6455 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6456 last = TREE_PURPOSE (parm);
6458 TREE_CHAIN (last) = 0;
6461 pointer_set_destroy (seen_args);
6463 /* If there was a previous prototype,
6464 set the DECL_ARG_TYPE of each argument according to
6465 the type previously specified, and report any mismatches. */
6467 if (current_function_prototype_arg_types)
6469 tree type;
6470 for (parm = DECL_ARGUMENTS (fndecl),
6471 type = current_function_prototype_arg_types;
6472 parm || (type && TREE_VALUE (type) != error_mark_node
6473 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
6474 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6476 if (parm == 0 || type == 0
6477 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6479 if (current_function_prototype_built_in)
6480 warning (0, "number of arguments doesn%'t match "
6481 "built-in prototype");
6482 else
6484 error ("number of arguments doesn%'t match prototype");
6485 error ("%Hprototype declaration",
6486 &current_function_prototype_locus);
6488 break;
6490 /* Type for passing arg must be consistent with that
6491 declared for the arg. ISO C says we take the unqualified
6492 type for parameters declared with qualified type. */
6493 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6494 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6496 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6497 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6499 /* Adjust argument to match prototype. E.g. a previous
6500 `int foo(float);' prototype causes
6501 `int foo(x) float x; {...}' to be treated like
6502 `int foo(float x) {...}'. This is particularly
6503 useful for argument types like uid_t. */
6504 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6506 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6507 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6508 && TYPE_PRECISION (TREE_TYPE (parm))
6509 < TYPE_PRECISION (integer_type_node))
6510 DECL_ARG_TYPE (parm) = integer_type_node;
6512 if (pedantic)
6514 /* ??? Is it possible to get here with a
6515 built-in prototype or will it always have
6516 been diagnosed as conflicting with an
6517 old-style definition and discarded? */
6518 if (current_function_prototype_built_in)
6519 warning (0, "promoted argument %qD "
6520 "doesn%'t match built-in prototype", parm);
6521 else
6523 pedwarn ("promoted argument %qD "
6524 "doesn%'t match prototype", parm);
6525 pedwarn ("%Hprototype declaration",
6526 &current_function_prototype_locus);
6530 else
6532 if (current_function_prototype_built_in)
6533 warning (0, "argument %qD doesn%'t match "
6534 "built-in prototype", parm);
6535 else
6537 error ("argument %qD doesn%'t match prototype", parm);
6538 error ("%Hprototype declaration",
6539 &current_function_prototype_locus);
6544 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6547 /* Otherwise, create a prototype that would match. */
6549 else
6551 tree actual = 0, last = 0, type;
6553 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6555 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6556 if (last)
6557 TREE_CHAIN (last) = type;
6558 else
6559 actual = type;
6560 last = type;
6562 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6563 if (last)
6564 TREE_CHAIN (last) = type;
6565 else
6566 actual = type;
6568 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6569 of the type of this function, but we need to avoid having this
6570 affect the types of other similarly-typed functions, so we must
6571 first force the generation of an identical (but separate) type
6572 node for the relevant function type. The new node we create
6573 will be a variant of the main variant of the original function
6574 type. */
6576 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6578 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6582 /* Store parameter declarations passed in ARG_INFO into the current
6583 function declaration. */
6585 void
6586 store_parm_decls_from (struct c_arg_info *arg_info)
6588 current_function_arg_info = arg_info;
6589 store_parm_decls ();
6592 /* Store the parameter declarations into the current function declaration.
6593 This is called after parsing the parameter declarations, before
6594 digesting the body of the function.
6596 For an old-style definition, construct a prototype out of the old-style
6597 parameter declarations and inject it into the function's type. */
6599 void
6600 store_parm_decls (void)
6602 tree fndecl = current_function_decl;
6603 bool proto;
6605 /* The argument information block for FNDECL. */
6606 struct c_arg_info *arg_info = current_function_arg_info;
6607 current_function_arg_info = 0;
6609 /* True if this definition is written with a prototype. Note:
6610 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6611 list in a function definition as equivalent to (void) -- an
6612 empty argument list specifies the function has no parameters,
6613 but only (void) sets up a prototype for future calls. */
6614 proto = arg_info->types != 0;
6616 if (proto)
6617 store_parm_decls_newstyle (fndecl, arg_info);
6618 else
6619 store_parm_decls_oldstyle (fndecl, arg_info);
6621 /* The next call to push_scope will be a function body. */
6623 next_is_function_body = true;
6625 /* Write a record describing this function definition to the prototypes
6626 file (if requested). */
6628 gen_aux_info_record (fndecl, 1, 0, proto);
6630 /* Initialize the RTL code for the function. */
6631 allocate_struct_function (fndecl);
6633 /* Begin the statement tree for this function. */
6634 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6636 /* ??? Insert the contents of the pending sizes list into the function
6637 to be evaluated. The only reason left to have this is
6638 void foo(int n, int array[n++])
6639 because we throw away the array type in favor of a pointer type, and
6640 thus won't naturally see the SAVE_EXPR containing the increment. All
6641 other pending sizes would be handled by gimplify_parameters. */
6643 tree t;
6644 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6645 add_stmt (TREE_VALUE (t));
6648 /* Even though we're inside a function body, we still don't want to
6649 call expand_expr to calculate the size of a variable-sized array.
6650 We haven't necessarily assigned RTL to all variables yet, so it's
6651 not safe to try to expand expressions involving them. */
6652 cfun->x_dont_save_pending_sizes_p = 1;
6655 /* Emit diagnostics that require gimple input for detection. Operate on
6656 FNDECL and all its nested functions. */
6658 static void
6659 c_gimple_diagnostics_recursively (tree fndecl)
6661 struct cgraph_node *cgn;
6663 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6664 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6666 /* Notice when OpenMP structured block constraints are violated. */
6667 if (flag_openmp)
6668 diagnose_omp_structured_block_errors (fndecl);
6670 /* Finalize all nested functions now. */
6671 cgn = cgraph_node (fndecl);
6672 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6673 c_gimple_diagnostics_recursively (cgn->decl);
6676 /* Finish up a function declaration and compile that function
6677 all the way to assembler language output. The free the storage
6678 for the function definition.
6680 This is called after parsing the body of the function definition. */
6682 void
6683 finish_function (void)
6685 tree fndecl = current_function_decl;
6687 label_context_stack_se = label_context_stack_se->next;
6688 label_context_stack_vm = label_context_stack_vm->next;
6690 if (TREE_CODE (fndecl) == FUNCTION_DECL
6691 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6693 tree args = DECL_ARGUMENTS (fndecl);
6694 for (; args; args = TREE_CHAIN (args))
6696 tree type = TREE_TYPE (args);
6697 if (INTEGRAL_TYPE_P (type)
6698 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6699 DECL_ARG_TYPE (args) = integer_type_node;
6703 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6704 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6706 /* Must mark the RESULT_DECL as being in this function. */
6708 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6709 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6711 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6713 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6714 != integer_type_node)
6716 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6717 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6718 if (!warn_main)
6719 pedwarn ("return type of %q+D is not %<int%>", fndecl);
6721 else
6723 if (flag_isoc99)
6725 tree stmt = c_finish_return (integer_zero_node);
6726 #ifdef USE_MAPPED_LOCATION
6727 /* Hack. We don't want the middle-end to warn that this return
6728 is unreachable, so we mark its location as special. Using
6729 UNKNOWN_LOCATION has the problem that it gets clobbered in
6730 annotate_one_with_locus. A cleaner solution might be to
6731 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6733 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6734 #else
6735 /* Hack. We don't want the middle-end to warn that this
6736 return is unreachable, so put the statement on the
6737 special line 0. */
6738 annotate_with_file_line (stmt, input_filename, 0);
6739 #endif
6744 /* Tie off the statement tree for this function. */
6745 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6747 finish_fname_decls ();
6749 /* Complain if there's just no return statement. */
6750 if (warn_return_type
6751 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6752 && !current_function_returns_value && !current_function_returns_null
6753 /* Don't complain if we are no-return. */
6754 && !current_function_returns_abnormally
6755 /* Don't warn for main(). */
6756 && !MAIN_NAME_P (DECL_NAME (fndecl))
6757 /* Or if they didn't actually specify a return type. */
6758 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6759 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6760 inline function, as we might never be compiled separately. */
6761 && DECL_INLINE (fndecl))
6763 warning (OPT_Wreturn_type,
6764 "no return statement in function returning non-void");
6765 TREE_NO_WARNING (fndecl) = 1;
6768 /* With just -Wextra, complain only if function returns both with
6769 and without a value. */
6770 if (extra_warnings
6771 && current_function_returns_value
6772 && current_function_returns_null)
6773 warning (OPT_Wextra, "this function may return with or without a value");
6775 /* Store the end of the function, so that we get good line number
6776 info for the epilogue. */
6777 cfun->function_end_locus = input_location;
6779 /* If we don't have ctors/dtors sections, and this is a static
6780 constructor or destructor, it must be recorded now. */
6781 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6782 && !targetm.have_ctors_dtors)
6783 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6784 if (DECL_STATIC_DESTRUCTOR (fndecl)
6785 && !targetm.have_ctors_dtors)
6786 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6788 /* Finalize the ELF visibility for the function. */
6789 c_determine_visibility (fndecl);
6791 /* Genericize before inlining. Delay genericizing nested functions
6792 until their parent function is genericized. Since finalizing
6793 requires GENERIC, delay that as well. */
6795 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6796 && !undef_nested_function)
6798 if (!decl_function_context (fndecl))
6800 c_genericize (fndecl);
6801 c_gimple_diagnostics_recursively (fndecl);
6803 /* ??? Objc emits functions after finalizing the compilation unit.
6804 This should be cleaned up later and this conditional removed. */
6805 if (cgraph_global_info_ready)
6807 c_expand_body (fndecl);
6808 return;
6811 cgraph_finalize_function (fndecl, false);
6813 else
6815 /* Register this function with cgraph just far enough to get it
6816 added to our parent's nested function list. Handy, since the
6817 C front end doesn't have such a list. */
6818 (void) cgraph_node (fndecl);
6822 if (!decl_function_context (fndecl))
6823 undef_nested_function = false;
6825 /* We're leaving the context of this function, so zap cfun.
6826 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6827 tree_rest_of_compilation. */
6828 cfun = NULL;
6829 current_function_decl = NULL;
6832 /* Check the declarations given in a for-loop for satisfying the C99
6833 constraints. If exactly one such decl is found, return it. */
6835 tree
6836 check_for_loop_decls (void)
6838 struct c_binding *b;
6839 tree one_decl = NULL_TREE;
6840 int n_decls = 0;
6843 if (!flag_isoc99)
6845 /* If we get here, declarations have been used in a for loop without
6846 the C99 for loop scope. This doesn't make much sense, so don't
6847 allow it. */
6848 error ("%<for%> loop initial declaration used outside C99 mode");
6849 return NULL_TREE;
6851 /* C99 subclause 6.8.5 paragraph 3:
6853 [#3] The declaration part of a for statement shall only
6854 declare identifiers for objects having storage class auto or
6855 register.
6857 It isn't clear whether, in this sentence, "identifiers" binds to
6858 "shall only declare" or to "objects" - that is, whether all identifiers
6859 declared must be identifiers for objects, or whether the restriction
6860 only applies to those that are. (A question on this in comp.std.c
6861 in November 2000 received no answer.) We implement the strictest
6862 interpretation, to avoid creating an extension which later causes
6863 problems. */
6865 for (b = current_scope->bindings; b; b = b->prev)
6867 tree id = b->id;
6868 tree decl = b->decl;
6870 if (!id)
6871 continue;
6873 switch (TREE_CODE (decl))
6875 case VAR_DECL:
6876 if (TREE_STATIC (decl))
6877 error ("declaration of static variable %q+D in %<for%> loop "
6878 "initial declaration", decl);
6879 else if (DECL_EXTERNAL (decl))
6880 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6881 "initial declaration", decl);
6882 break;
6884 case RECORD_TYPE:
6885 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6886 id);
6887 break;
6888 case UNION_TYPE:
6889 error ("%<union %E%> declared in %<for%> loop initial declaration",
6890 id);
6891 break;
6892 case ENUMERAL_TYPE:
6893 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6894 id);
6895 break;
6896 default:
6897 error ("declaration of non-variable %q+D in %<for%> loop "
6898 "initial declaration", decl);
6901 n_decls++;
6902 one_decl = decl;
6905 return n_decls == 1 ? one_decl : NULL_TREE;
6908 /* Save and reinitialize the variables
6909 used during compilation of a C function. */
6911 void
6912 c_push_function_context (struct function *f)
6914 struct language_function *p;
6915 p = GGC_NEW (struct language_function);
6916 f->language = p;
6918 p->base.x_stmt_tree = c_stmt_tree;
6919 p->x_break_label = c_break_label;
6920 p->x_cont_label = c_cont_label;
6921 p->x_switch_stack = c_switch_stack;
6922 p->arg_info = current_function_arg_info;
6923 p->returns_value = current_function_returns_value;
6924 p->returns_null = current_function_returns_null;
6925 p->returns_abnormally = current_function_returns_abnormally;
6926 p->warn_about_return_type = warn_about_return_type;
6929 /* Restore the variables used during compilation of a C function. */
6931 void
6932 c_pop_function_context (struct function *f)
6934 struct language_function *p = f->language;
6936 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6937 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6939 /* Stop pointing to the local nodes about to be freed. */
6940 /* But DECL_INITIAL must remain nonzero so we know this
6941 was an actual function definition. */
6942 DECL_INITIAL (current_function_decl) = error_mark_node;
6943 DECL_ARGUMENTS (current_function_decl) = 0;
6946 c_stmt_tree = p->base.x_stmt_tree;
6947 c_break_label = p->x_break_label;
6948 c_cont_label = p->x_cont_label;
6949 c_switch_stack = p->x_switch_stack;
6950 current_function_arg_info = p->arg_info;
6951 current_function_returns_value = p->returns_value;
6952 current_function_returns_null = p->returns_null;
6953 current_function_returns_abnormally = p->returns_abnormally;
6954 warn_about_return_type = p->warn_about_return_type;
6956 f->language = NULL;
6959 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6961 void
6962 c_dup_lang_specific_decl (tree decl)
6964 struct lang_decl *ld;
6966 if (!DECL_LANG_SPECIFIC (decl))
6967 return;
6969 ld = GGC_NEW (struct lang_decl);
6970 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6971 DECL_LANG_SPECIFIC (decl) = ld;
6974 /* The functions below are required for functionality of doing
6975 function at once processing in the C front end. Currently these
6976 functions are not called from anywhere in the C front end, but as
6977 these changes continue, that will change. */
6979 /* Returns the stmt_tree (if any) to which statements are currently
6980 being added. If there is no active statement-tree, NULL is
6981 returned. */
6983 stmt_tree
6984 current_stmt_tree (void)
6986 return &c_stmt_tree;
6989 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6990 C. */
6993 anon_aggr_type_p (tree ARG_UNUSED (node))
6995 return 0;
6998 /* Return the global value of T as a symbol. */
7000 tree
7001 identifier_global_value (tree t)
7003 struct c_binding *b;
7005 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7006 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7007 return b->decl;
7009 return 0;
7012 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7013 otherwise the name is found in ridpointers from RID_INDEX. */
7015 void
7016 record_builtin_type (enum rid rid_index, const char *name, tree type)
7018 tree id, decl;
7019 if (name == 0)
7020 id = ridpointers[(int) rid_index];
7021 else
7022 id = get_identifier (name);
7023 decl = build_decl (TYPE_DECL, id, type);
7024 pushdecl (decl);
7025 if (debug_hooks->type_decl)
7026 debug_hooks->type_decl (decl, false);
7029 /* Build the void_list_node (void_type_node having been created). */
7030 tree
7031 build_void_list_node (void)
7033 tree t = build_tree_list (NULL_TREE, void_type_node);
7034 return t;
7037 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7039 struct c_parm *
7040 build_c_parm (struct c_declspecs *specs, tree attrs,
7041 struct c_declarator *declarator)
7043 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7044 ret->specs = specs;
7045 ret->attrs = attrs;
7046 ret->declarator = declarator;
7047 return ret;
7050 /* Return a declarator with nested attributes. TARGET is the inner
7051 declarator to which these attributes apply. ATTRS are the
7052 attributes. */
7054 struct c_declarator *
7055 build_attrs_declarator (tree attrs, struct c_declarator *target)
7057 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7058 ret->kind = cdk_attrs;
7059 ret->declarator = target;
7060 ret->u.attrs = attrs;
7061 return ret;
7064 /* Return a declarator for a function with arguments specified by ARGS
7065 and return type specified by TARGET. */
7067 struct c_declarator *
7068 build_function_declarator (struct c_arg_info *args,
7069 struct c_declarator *target)
7071 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7072 ret->kind = cdk_function;
7073 ret->declarator = target;
7074 ret->u.arg_info = args;
7075 return ret;
7078 /* Return a declarator for the identifier IDENT (which may be
7079 NULL_TREE for an abstract declarator). */
7081 struct c_declarator *
7082 build_id_declarator (tree ident)
7084 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7085 ret->kind = cdk_id;
7086 ret->declarator = 0;
7087 ret->u.id = ident;
7088 /* Default value - may get reset to a more precise location. */
7089 ret->id_loc = input_location;
7090 return ret;
7093 /* Return something to represent absolute declarators containing a *.
7094 TARGET is the absolute declarator that the * contains.
7095 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7096 to apply to the pointer type. */
7098 struct c_declarator *
7099 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7100 struct c_declarator *target)
7102 tree attrs;
7103 int quals = 0;
7104 struct c_declarator *itarget = target;
7105 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7106 if (type_quals_attrs)
7108 attrs = type_quals_attrs->attrs;
7109 quals = quals_from_declspecs (type_quals_attrs);
7110 if (attrs != NULL_TREE)
7111 itarget = build_attrs_declarator (attrs, target);
7113 ret->kind = cdk_pointer;
7114 ret->declarator = itarget;
7115 ret->u.pointer_quals = quals;
7116 return ret;
7119 /* Return a pointer to a structure for an empty list of declaration
7120 specifiers. */
7122 struct c_declspecs *
7123 build_null_declspecs (void)
7125 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7126 ret->type = 0;
7127 ret->decl_attr = 0;
7128 ret->attrs = 0;
7129 ret->typespec_word = cts_none;
7130 ret->storage_class = csc_none;
7131 ret->declspecs_seen_p = false;
7132 ret->type_seen_p = false;
7133 ret->non_sc_seen_p = false;
7134 ret->typedef_p = false;
7135 ret->tag_defined_p = false;
7136 ret->explicit_signed_p = false;
7137 ret->deprecated_p = false;
7138 ret->default_int_p = false;
7139 ret->long_p = false;
7140 ret->long_long_p = false;
7141 ret->short_p = false;
7142 ret->signed_p = false;
7143 ret->unsigned_p = false;
7144 ret->complex_p = false;
7145 ret->inline_p = false;
7146 ret->thread_p = false;
7147 ret->const_p = false;
7148 ret->volatile_p = false;
7149 ret->restrict_p = false;
7150 return ret;
7153 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7154 returning SPECS. */
7156 struct c_declspecs *
7157 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7159 enum rid i;
7160 bool dupe = false;
7161 specs->non_sc_seen_p = true;
7162 specs->declspecs_seen_p = true;
7163 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7164 && C_IS_RESERVED_WORD (qual));
7165 i = C_RID_CODE (qual);
7166 switch (i)
7168 case RID_CONST:
7169 dupe = specs->const_p;
7170 specs->const_p = true;
7171 break;
7172 case RID_VOLATILE:
7173 dupe = specs->volatile_p;
7174 specs->volatile_p = true;
7175 break;
7176 case RID_RESTRICT:
7177 dupe = specs->restrict_p;
7178 specs->restrict_p = true;
7179 break;
7180 default:
7181 gcc_unreachable ();
7183 if (dupe && pedantic && !flag_isoc99)
7184 pedwarn ("duplicate %qE", qual);
7185 return specs;
7188 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7189 returning SPECS. */
7191 struct c_declspecs *
7192 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7194 tree type = spec.spec;
7195 specs->non_sc_seen_p = true;
7196 specs->declspecs_seen_p = true;
7197 specs->type_seen_p = true;
7198 if (TREE_DEPRECATED (type))
7199 specs->deprecated_p = true;
7201 /* Handle type specifier keywords. */
7202 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
7204 enum rid i = C_RID_CODE (type);
7205 if (specs->type)
7207 error ("two or more data types in declaration specifiers");
7208 return specs;
7210 if ((int) i <= (int) RID_LAST_MODIFIER)
7212 /* "long", "short", "signed", "unsigned" or "_Complex". */
7213 bool dupe = false;
7214 switch (i)
7216 case RID_LONG:
7217 if (specs->long_long_p)
7219 error ("%<long long long%> is too long for GCC");
7220 break;
7222 if (specs->long_p)
7224 if (specs->typespec_word == cts_double)
7226 error ("both %<long long%> and %<double%> in "
7227 "declaration specifiers");
7228 break;
7230 if (pedantic && !flag_isoc99 && !in_system_header
7231 && warn_long_long)
7232 pedwarn ("ISO C90 does not support %<long long%>");
7233 specs->long_long_p = 1;
7234 break;
7236 if (specs->short_p)
7237 error ("both %<long%> and %<short%> in "
7238 "declaration specifiers");
7239 else if (specs->typespec_word == cts_void)
7240 error ("both %<long%> and %<void%> in "
7241 "declaration specifiers");
7242 else if (specs->typespec_word == cts_bool)
7243 error ("both %<long%> and %<_Bool%> in "
7244 "declaration specifiers");
7245 else if (specs->typespec_word == cts_char)
7246 error ("both %<long%> and %<char%> in "
7247 "declaration specifiers");
7248 else if (specs->typespec_word == cts_float)
7249 error ("both %<long%> and %<float%> in "
7250 "declaration specifiers");
7251 else if (specs->typespec_word == cts_dfloat32)
7252 error ("both %<long%> and %<_Decimal32%> in "
7253 "declaration specifiers");
7254 else if (specs->typespec_word == cts_dfloat64)
7255 error ("both %<long%> and %<_Decimal64%> in "
7256 "declaration specifiers");
7257 else if (specs->typespec_word == cts_dfloat128)
7258 error ("both %<long%> and %<_Decimal128%> in "
7259 "declaration specifiers");
7260 else
7261 specs->long_p = true;
7262 break;
7263 case RID_SHORT:
7264 dupe = specs->short_p;
7265 if (specs->long_p)
7266 error ("both %<long%> and %<short%> in "
7267 "declaration specifiers");
7268 else if (specs->typespec_word == cts_void)
7269 error ("both %<short%> and %<void%> in "
7270 "declaration specifiers");
7271 else if (specs->typespec_word == cts_bool)
7272 error ("both %<short%> and %<_Bool%> in "
7273 "declaration specifiers");
7274 else if (specs->typespec_word == cts_char)
7275 error ("both %<short%> and %<char%> in "
7276 "declaration specifiers");
7277 else if (specs->typespec_word == cts_float)
7278 error ("both %<short%> and %<float%> in "
7279 "declaration specifiers");
7280 else if (specs->typespec_word == cts_double)
7281 error ("both %<short%> and %<double%> in "
7282 "declaration specifiers");
7283 else if (specs->typespec_word == cts_dfloat32)
7284 error ("both %<short%> and %<_Decimal32%> in "
7285 "declaration specifiers");
7286 else if (specs->typespec_word == cts_dfloat64)
7287 error ("both %<short%> and %<_Decimal64%> in "
7288 "declaration specifiers");
7289 else if (specs->typespec_word == cts_dfloat128)
7290 error ("both %<short%> and %<_Decimal128%> in "
7291 "declaration specifiers");
7292 else
7293 specs->short_p = true;
7294 break;
7295 case RID_SIGNED:
7296 dupe = specs->signed_p;
7297 if (specs->unsigned_p)
7298 error ("both %<signed%> and %<unsigned%> in "
7299 "declaration specifiers");
7300 else if (specs->typespec_word == cts_void)
7301 error ("both %<signed%> and %<void%> in "
7302 "declaration specifiers");
7303 else if (specs->typespec_word == cts_bool)
7304 error ("both %<signed%> and %<_Bool%> in "
7305 "declaration specifiers");
7306 else if (specs->typespec_word == cts_float)
7307 error ("both %<signed%> and %<float%> in "
7308 "declaration specifiers");
7309 else if (specs->typespec_word == cts_double)
7310 error ("both %<signed%> and %<double%> in "
7311 "declaration specifiers");
7312 else if (specs->typespec_word == cts_dfloat32)
7313 error ("both %<signed%> and %<_Decimal32%> in "
7314 "declaration specifiers");
7315 else if (specs->typespec_word == cts_dfloat64)
7316 error ("both %<signed%> and %<_Decimal64%> in "
7317 "declaration specifiers");
7318 else if (specs->typespec_word == cts_dfloat128)
7319 error ("both %<signed%> and %<_Decimal128%> in "
7320 "declaration specifiers");
7321 else
7322 specs->signed_p = true;
7323 break;
7324 case RID_UNSIGNED:
7325 dupe = specs->unsigned_p;
7326 if (specs->signed_p)
7327 error ("both %<signed%> and %<unsigned%> in "
7328 "declaration specifiers");
7329 else if (specs->typespec_word == cts_void)
7330 error ("both %<unsigned%> and %<void%> in "
7331 "declaration specifiers");
7332 else if (specs->typespec_word == cts_bool)
7333 error ("both %<unsigned%> and %<_Bool%> in "
7334 "declaration specifiers");
7335 else if (specs->typespec_word == cts_float)
7336 error ("both %<unsigned%> and %<float%> in "
7337 "declaration specifiers");
7338 else if (specs->typespec_word == cts_double)
7339 error ("both %<unsigned%> and %<double%> in "
7340 "declaration specifiers");
7341 else if (specs->typespec_word == cts_dfloat32)
7342 error ("both %<unsigned%> and %<_Decimal32%> in "
7343 "declaration specifiers");
7344 else if (specs->typespec_word == cts_dfloat64)
7345 error ("both %<unsigned%> and %<_Decimal64%> in "
7346 "declaration specifiers");
7347 else if (specs->typespec_word == cts_dfloat128)
7348 error ("both %<unsigned%> and %<_Decimal128%> in "
7349 "declaration specifiers");
7350 else
7351 specs->unsigned_p = true;
7352 break;
7353 case RID_COMPLEX:
7354 dupe = specs->complex_p;
7355 if (pedantic && !flag_isoc99 && !in_system_header)
7356 pedwarn ("ISO C90 does not support complex types");
7357 if (specs->typespec_word == cts_void)
7358 error ("both %<complex%> and %<void%> in "
7359 "declaration specifiers");
7360 else if (specs->typespec_word == cts_bool)
7361 error ("both %<complex%> and %<_Bool%> in "
7362 "declaration specifiers");
7363 else if (specs->typespec_word == cts_dfloat32)
7364 error ("both %<complex%> and %<_Decimal32%> in "
7365 "declaration specifiers");
7366 else if (specs->typespec_word == cts_dfloat64)
7367 error ("both %<complex%> and %<_Decimal64%> in "
7368 "declaration specifiers");
7369 else if (specs->typespec_word == cts_dfloat128)
7370 error ("both %<complex%> and %<_Decimal128%> in "
7371 "declaration specifiers");
7372 else
7373 specs->complex_p = true;
7374 break;
7375 default:
7376 gcc_unreachable ();
7379 if (dupe)
7380 error ("duplicate %qE", type);
7382 return specs;
7384 else
7386 /* "void", "_Bool", "char", "int", "float" or "double". */
7387 if (specs->typespec_word != cts_none)
7389 error ("two or more data types in declaration specifiers");
7390 return specs;
7392 switch (i)
7394 case RID_VOID:
7395 if (specs->long_p)
7396 error ("both %<long%> and %<void%> in "
7397 "declaration specifiers");
7398 else if (specs->short_p)
7399 error ("both %<short%> and %<void%> in "
7400 "declaration specifiers");
7401 else if (specs->signed_p)
7402 error ("both %<signed%> and %<void%> in "
7403 "declaration specifiers");
7404 else if (specs->unsigned_p)
7405 error ("both %<unsigned%> and %<void%> in "
7406 "declaration specifiers");
7407 else if (specs->complex_p)
7408 error ("both %<complex%> and %<void%> in "
7409 "declaration specifiers");
7410 else
7411 specs->typespec_word = cts_void;
7412 return specs;
7413 case RID_BOOL:
7414 if (specs->long_p)
7415 error ("both %<long%> and %<_Bool%> in "
7416 "declaration specifiers");
7417 else if (specs->short_p)
7418 error ("both %<short%> and %<_Bool%> in "
7419 "declaration specifiers");
7420 else if (specs->signed_p)
7421 error ("both %<signed%> and %<_Bool%> in "
7422 "declaration specifiers");
7423 else if (specs->unsigned_p)
7424 error ("both %<unsigned%> and %<_Bool%> in "
7425 "declaration specifiers");
7426 else if (specs->complex_p)
7427 error ("both %<complex%> and %<_Bool%> in "
7428 "declaration specifiers");
7429 else
7430 specs->typespec_word = cts_bool;
7431 return specs;
7432 case RID_CHAR:
7433 if (specs->long_p)
7434 error ("both %<long%> and %<char%> in "
7435 "declaration specifiers");
7436 else if (specs->short_p)
7437 error ("both %<short%> and %<char%> in "
7438 "declaration specifiers");
7439 else
7440 specs->typespec_word = cts_char;
7441 return specs;
7442 case RID_INT:
7443 specs->typespec_word = cts_int;
7444 return specs;
7445 case RID_FLOAT:
7446 if (specs->long_p)
7447 error ("both %<long%> and %<float%> in "
7448 "declaration specifiers");
7449 else if (specs->short_p)
7450 error ("both %<short%> and %<float%> in "
7451 "declaration specifiers");
7452 else if (specs->signed_p)
7453 error ("both %<signed%> and %<float%> in "
7454 "declaration specifiers");
7455 else if (specs->unsigned_p)
7456 error ("both %<unsigned%> and %<float%> in "
7457 "declaration specifiers");
7458 else
7459 specs->typespec_word = cts_float;
7460 return specs;
7461 case RID_DOUBLE:
7462 if (specs->long_long_p)
7463 error ("both %<long long%> and %<double%> in "
7464 "declaration specifiers");
7465 else if (specs->short_p)
7466 error ("both %<short%> and %<double%> in "
7467 "declaration specifiers");
7468 else if (specs->signed_p)
7469 error ("both %<signed%> and %<double%> in "
7470 "declaration specifiers");
7471 else if (specs->unsigned_p)
7472 error ("both %<unsigned%> and %<double%> in "
7473 "declaration specifiers");
7474 else
7475 specs->typespec_word = cts_double;
7476 return specs;
7477 case RID_DFLOAT32:
7478 case RID_DFLOAT64:
7479 case RID_DFLOAT128:
7481 const char *str;
7482 if (i == RID_DFLOAT32)
7483 str = "_Decimal32";
7484 else if (i == RID_DFLOAT64)
7485 str = "_Decimal64";
7486 else
7487 str = "_Decimal128";
7488 if (specs->long_long_p)
7489 error ("both %<long long%> and %<%s%> in "
7490 "declaration specifiers", str);
7491 if (specs->long_p)
7492 error ("both %<long%> and %<%s%> in "
7493 "declaration specifiers", str);
7494 else if (specs->short_p)
7495 error ("both %<short%> and %<%s%> in "
7496 "declaration specifiers", str);
7497 else if (specs->signed_p)
7498 error ("both %<signed%> and %<%s%> in "
7499 "declaration specifiers", str);
7500 else if (specs->unsigned_p)
7501 error ("both %<unsigned%> and %<%s%> in "
7502 "declaration specifiers", str);
7503 else if (specs->complex_p)
7504 error ("both %<complex%> and %<%s%> in "
7505 "declaration specifiers", str);
7506 else if (i == RID_DFLOAT32)
7507 specs->typespec_word = cts_dfloat32;
7508 else if (i == RID_DFLOAT64)
7509 specs->typespec_word = cts_dfloat64;
7510 else
7511 specs->typespec_word = cts_dfloat128;
7513 if (!targetm.decimal_float_supported_p ())
7514 error ("decimal floating point not supported for this target");
7515 if (pedantic)
7516 pedwarn ("ISO C does not support decimal floating point");
7517 return specs;
7518 default:
7519 /* ObjC reserved word "id", handled below. */
7520 break;
7525 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7526 form of ObjC type, cases such as "int" and "long" being handled
7527 above), a TYPE (struct, union, enum and typeof specifiers) or an
7528 ERROR_MARK. In none of these cases may there have previously
7529 been any type specifiers. */
7530 if (specs->type || specs->typespec_word != cts_none
7531 || specs->long_p || specs->short_p || specs->signed_p
7532 || specs->unsigned_p || specs->complex_p)
7533 error ("two or more data types in declaration specifiers");
7534 else if (TREE_CODE (type) == TYPE_DECL)
7536 if (TREE_TYPE (type) == error_mark_node)
7537 ; /* Allow the type to default to int to avoid cascading errors. */
7538 else
7540 specs->type = TREE_TYPE (type);
7541 specs->decl_attr = DECL_ATTRIBUTES (type);
7542 specs->typedef_p = true;
7543 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7546 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7548 tree t = lookup_name (type);
7549 if (!t || TREE_CODE (t) != TYPE_DECL)
7550 error ("%qE fails to be a typedef or built in type", type);
7551 else if (TREE_TYPE (t) == error_mark_node)
7553 else
7554 specs->type = TREE_TYPE (t);
7556 else if (TREE_CODE (type) != ERROR_MARK)
7558 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7559 specs->tag_defined_p = true;
7560 if (spec.kind == ctsk_typeof)
7561 specs->typedef_p = true;
7562 specs->type = type;
7565 return specs;
7568 /* Add the storage class specifier or function specifier SCSPEC to the
7569 declaration specifiers SPECS, returning SPECS. */
7571 struct c_declspecs *
7572 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7574 enum rid i;
7575 enum c_storage_class n = csc_none;
7576 bool dupe = false;
7577 specs->declspecs_seen_p = true;
7578 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7579 && C_IS_RESERVED_WORD (scspec));
7580 i = C_RID_CODE (scspec);
7581 if (specs->non_sc_seen_p)
7582 warning (OPT_Wold_style_declaration,
7583 "%qE is not at beginning of declaration", scspec);
7584 switch (i)
7586 case RID_INLINE:
7587 /* C99 permits duplicate inline. Although of doubtful utility,
7588 it seems simplest to permit it in gnu89 mode as well, as
7589 there is also little utility in maintaining this as a
7590 difference between gnu89 and C99 inline. */
7591 dupe = false;
7592 specs->inline_p = true;
7593 break;
7594 case RID_THREAD:
7595 dupe = specs->thread_p;
7596 if (specs->storage_class == csc_auto)
7597 error ("%<__thread%> used with %<auto%>");
7598 else if (specs->storage_class == csc_register)
7599 error ("%<__thread%> used with %<register%>");
7600 else if (specs->storage_class == csc_typedef)
7601 error ("%<__thread%> used with %<typedef%>");
7602 else
7603 specs->thread_p = true;
7604 break;
7605 case RID_AUTO:
7606 n = csc_auto;
7607 break;
7608 case RID_EXTERN:
7609 n = csc_extern;
7610 /* Diagnose "__thread extern". */
7611 if (specs->thread_p)
7612 error ("%<__thread%> before %<extern%>");
7613 break;
7614 case RID_REGISTER:
7615 n = csc_register;
7616 break;
7617 case RID_STATIC:
7618 n = csc_static;
7619 /* Diagnose "__thread static". */
7620 if (specs->thread_p)
7621 error ("%<__thread%> before %<static%>");
7622 break;
7623 case RID_TYPEDEF:
7624 n = csc_typedef;
7625 break;
7626 default:
7627 gcc_unreachable ();
7629 if (n != csc_none && n == specs->storage_class)
7630 dupe = true;
7631 if (dupe)
7632 error ("duplicate %qE", scspec);
7633 if (n != csc_none)
7635 if (specs->storage_class != csc_none && n != specs->storage_class)
7637 error ("multiple storage classes in declaration specifiers");
7639 else
7641 specs->storage_class = n;
7642 if (n != csc_extern && n != csc_static && specs->thread_p)
7644 error ("%<__thread%> used with %qE", scspec);
7645 specs->thread_p = false;
7649 return specs;
7652 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7653 returning SPECS. */
7655 struct c_declspecs *
7656 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7658 specs->attrs = chainon (attrs, specs->attrs);
7659 specs->declspecs_seen_p = true;
7660 return specs;
7663 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7664 specifiers with any other type specifier to determine the resulting
7665 type. This is where ISO C checks on complex types are made, since
7666 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7667 double". */
7669 struct c_declspecs *
7670 finish_declspecs (struct c_declspecs *specs)
7672 /* If a type was specified as a whole, we have no modifiers and are
7673 done. */
7674 if (specs->type != NULL_TREE)
7676 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7677 && !specs->signed_p && !specs->unsigned_p
7678 && !specs->complex_p);
7679 return specs;
7682 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7683 has been specified, treat it as "int" unless "_Complex" is
7684 present and there are no other specifiers. If we just have
7685 "_Complex", it is equivalent to "_Complex double", but e.g.
7686 "_Complex short" is equivalent to "_Complex short int". */
7687 if (specs->typespec_word == cts_none)
7689 if (specs->long_p || specs->short_p
7690 || specs->signed_p || specs->unsigned_p)
7692 specs->typespec_word = cts_int;
7694 else if (specs->complex_p)
7696 specs->typespec_word = cts_double;
7697 if (pedantic)
7698 pedwarn ("ISO C does not support plain %<complex%> meaning "
7699 "%<double complex%>");
7701 else
7703 specs->typespec_word = cts_int;
7704 specs->default_int_p = true;
7705 /* We don't diagnose this here because grokdeclarator will
7706 give more specific diagnostics according to whether it is
7707 a function definition. */
7711 /* If "signed" was specified, record this to distinguish "int" and
7712 "signed int" in the case of a bit-field with
7713 -funsigned-bitfields. */
7714 specs->explicit_signed_p = specs->signed_p;
7716 /* Now compute the actual type. */
7717 switch (specs->typespec_word)
7719 case cts_void:
7720 gcc_assert (!specs->long_p && !specs->short_p
7721 && !specs->signed_p && !specs->unsigned_p
7722 && !specs->complex_p);
7723 specs->type = void_type_node;
7724 break;
7725 case cts_bool:
7726 gcc_assert (!specs->long_p && !specs->short_p
7727 && !specs->signed_p && !specs->unsigned_p
7728 && !specs->complex_p);
7729 specs->type = boolean_type_node;
7730 break;
7731 case cts_char:
7732 gcc_assert (!specs->long_p && !specs->short_p);
7733 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7734 if (specs->signed_p)
7735 specs->type = signed_char_type_node;
7736 else if (specs->unsigned_p)
7737 specs->type = unsigned_char_type_node;
7738 else
7739 specs->type = char_type_node;
7740 if (specs->complex_p)
7742 if (pedantic)
7743 pedwarn ("ISO C does not support complex integer types");
7744 specs->type = build_complex_type (specs->type);
7746 break;
7747 case cts_int:
7748 gcc_assert (!(specs->long_p && specs->short_p));
7749 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7750 if (specs->long_long_p)
7751 specs->type = (specs->unsigned_p
7752 ? long_long_unsigned_type_node
7753 : long_long_integer_type_node);
7754 else if (specs->long_p)
7755 specs->type = (specs->unsigned_p
7756 ? long_unsigned_type_node
7757 : long_integer_type_node);
7758 else if (specs->short_p)
7759 specs->type = (specs->unsigned_p
7760 ? short_unsigned_type_node
7761 : short_integer_type_node);
7762 else
7763 specs->type = (specs->unsigned_p
7764 ? unsigned_type_node
7765 : integer_type_node);
7766 if (specs->complex_p)
7768 if (pedantic)
7769 pedwarn ("ISO C does not support complex integer types");
7770 specs->type = build_complex_type (specs->type);
7772 break;
7773 case cts_float:
7774 gcc_assert (!specs->long_p && !specs->short_p
7775 && !specs->signed_p && !specs->unsigned_p);
7776 specs->type = (specs->complex_p
7777 ? complex_float_type_node
7778 : float_type_node);
7779 break;
7780 case cts_double:
7781 gcc_assert (!specs->long_long_p && !specs->short_p
7782 && !specs->signed_p && !specs->unsigned_p);
7783 if (specs->long_p)
7785 specs->type = (specs->complex_p
7786 ? complex_long_double_type_node
7787 : long_double_type_node);
7789 else
7791 specs->type = (specs->complex_p
7792 ? complex_double_type_node
7793 : double_type_node);
7795 break;
7796 case cts_dfloat32:
7797 case cts_dfloat64:
7798 case cts_dfloat128:
7799 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7800 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7801 if (specs->typespec_word == cts_dfloat32)
7802 specs->type = dfloat32_type_node;
7803 else if (specs->typespec_word == cts_dfloat64)
7804 specs->type = dfloat64_type_node;
7805 else
7806 specs->type = dfloat128_type_node;
7807 break;
7808 default:
7809 gcc_unreachable ();
7812 return specs;
7815 /* Synthesize a function which calls all the global ctors or global
7816 dtors in this file. This is only used for targets which do not
7817 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7818 static void
7819 build_cdtor (int method_type, tree cdtors)
7821 tree body = 0;
7823 if (!cdtors)
7824 return;
7826 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7827 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7828 &body);
7830 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7833 /* A subroutine of c_write_global_declarations. Perform final processing
7834 on one file scope's declarations (or the external scope's declarations),
7835 GLOBALS. */
7837 static void
7838 c_write_global_declarations_1 (tree globals)
7840 tree decl;
7841 bool reconsider;
7843 /* Process the decls in the order they were written. */
7844 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7846 /* Check for used but undefined static functions using the C
7847 standard's definition of "used", and set TREE_NO_WARNING so
7848 that check_global_declarations doesn't repeat the check. */
7849 if (TREE_CODE (decl) == FUNCTION_DECL
7850 && DECL_INITIAL (decl) == 0
7851 && DECL_EXTERNAL (decl)
7852 && !TREE_PUBLIC (decl)
7853 && C_DECL_USED (decl))
7855 pedwarn ("%q+F used but never defined", decl);
7856 TREE_NO_WARNING (decl) = 1;
7859 wrapup_global_declaration_1 (decl);
7864 reconsider = false;
7865 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7866 reconsider |= wrapup_global_declaration_2 (decl);
7868 while (reconsider);
7870 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7871 check_global_declaration_1 (decl);
7874 /* A subroutine of c_write_global_declarations Emit debug information for each
7875 of the declarations in GLOBALS. */
7877 static void
7878 c_write_global_declarations_2 (tree globals)
7880 tree decl;
7882 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
7883 debug_hooks->global_decl (decl);
7886 /* Preserve the external declarations scope across a garbage collect. */
7887 static GTY(()) tree ext_block;
7889 void
7890 c_write_global_declarations (void)
7892 tree t;
7894 /* We don't want to do this if generating a PCH. */
7895 if (pch_file)
7896 return;
7898 /* Don't waste time on further processing if -fsyntax-only or we've
7899 encountered errors. */
7900 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7901 return;
7903 /* Close the external scope. */
7904 ext_block = pop_scope ();
7905 external_scope = 0;
7906 gcc_assert (!current_scope);
7908 if (ext_block)
7910 tree tmp = BLOCK_VARS (ext_block);
7911 int flags;
7912 FILE * stream = dump_begin (TDI_tu, &flags);
7913 if (stream && tmp)
7915 dump_node (tmp, flags & ~TDF_SLIM, stream);
7916 dump_end (TDI_tu, stream);
7920 /* Process all file scopes in this compilation, and the external_scope,
7921 through wrapup_global_declarations and check_global_declarations. */
7922 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7923 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7924 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7926 /* Generate functions to call static constructors and destructors
7927 for targets that do not support .ctors/.dtors sections. These
7928 functions have magic names which are detected by collect2. */
7929 build_cdtor ('I', static_ctors); static_ctors = 0;
7930 build_cdtor ('D', static_dtors); static_dtors = 0;
7932 /* We're done parsing; proceed to optimize and emit assembly.
7933 FIXME: shouldn't be the front end's responsibility to call this. */
7934 cgraph_optimize ();
7936 /* After cgraph has had a chance to emit everything that's going to
7937 be emitted, output debug information for globals. */
7938 if (errorcount == 0 && sorrycount == 0)
7940 timevar_push (TV_SYMOUT);
7941 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7942 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
7943 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
7944 timevar_pop (TV_SYMOUT);
7947 ext_block = NULL;
7950 #include "gt-c-decl.h"