* common.opt (-Wattributes): New. Default true.
[official-gcc.git] / gcc / c-decl.c
blobd5e13abca878df88290857ae7c07b3a2df86447a
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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, 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"
64 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
65 enum decl_context
66 { NORMAL, /* Ordinary declaration */
67 FUNCDEF, /* Function definition */
68 PARM, /* Declaration of parm before function body */
69 FIELD, /* Declaration inside struct or union */
70 TYPENAME}; /* Typename (inside cast or sizeof) */
73 /* Nonzero if we have seen an invalid cross reference
74 to a struct, union, or enum, but not yet printed the message. */
75 tree pending_invalid_xref;
77 /* File and line to appear in the eventual error message. */
78 location_t pending_invalid_xref_location;
80 /* True means we've initialized exception handling. */
81 bool c_eh_initialized_p;
83 /* While defining an enum type, this is 1 plus the last enumerator
84 constant value. Note that will do not have to save this or `enum_overflow'
85 around nested function definition since such a definition could only
86 occur in an enum value expression and we don't use these variables in
87 that case. */
89 static tree enum_next_value;
91 /* Nonzero means that there was overflow computing enum_next_value. */
93 static int enum_overflow;
95 /* The file and line that the prototype came from if this is an
96 old-style definition; used for diagnostics in
97 store_parm_decls_oldstyle. */
99 static location_t current_function_prototype_locus;
101 /* Whether this prototype was built-in. */
103 static bool current_function_prototype_built_in;
105 /* The argument type information of this prototype. */
107 static tree current_function_prototype_arg_types;
109 /* The argument information structure for the function currently being
110 defined. */
112 static struct c_arg_info *current_function_arg_info;
114 /* The obstack on which parser and related data structures, which are
115 not live beyond their top-level declaration or definition, are
116 allocated. */
117 struct obstack parser_obstack;
119 /* The current statement tree. */
121 static GTY(()) struct stmt_tree_s c_stmt_tree;
123 /* State saving variables. */
124 tree c_break_label;
125 tree c_cont_label;
127 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
128 included in this invocation. Note that the current translation
129 unit is not included in this list. */
131 static GTY(()) tree all_translation_units;
133 /* A list of decls to be made automatically visible in each file scope. */
134 static GTY(()) tree visible_builtins;
136 /* Set to 0 at beginning of a function definition, set to 1 if
137 a return statement that specifies a return value is seen. */
139 int current_function_returns_value;
141 /* Set to 0 at beginning of a function definition, set to 1 if
142 a return statement with no argument is seen. */
144 int current_function_returns_null;
146 /* Set to 0 at beginning of a function definition, set to 1 if
147 a call to a noreturn function is seen. */
149 int current_function_returns_abnormally;
151 /* Set to nonzero by `grokdeclarator' for a function
152 whose return type is defaulted, if warnings for this are desired. */
154 static int warn_about_return_type;
156 /* Nonzero when starting a function declared `extern inline'. */
158 static int current_extern_inline;
160 /* Nonzero when the current toplevel function contains a declaration
161 of a nested function which is never defined. */
163 static bool undef_nested_function;
165 /* True means global_bindings_p should return false even if the scope stack
166 says we are in file scope. */
167 bool c_override_global_bindings_to_false;
170 /* Each c_binding structure describes one binding of an identifier to
171 a decl. All the decls in a scope - irrespective of namespace - are
172 chained together by the ->prev field, which (as the name implies)
173 runs in reverse order. All the decls in a given namespace bound to
174 a given identifier are chained by the ->shadowed field, which runs
175 from inner to outer scopes.
177 The ->decl field usually points to a DECL node, but there are two
178 exceptions. In the namespace of type tags, the bound entity is a
179 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
180 identifier is encountered, it is bound to error_mark_node to
181 suppress further errors about that identifier in the current
182 function.
184 The ->type field stores the type of the declaration in this scope;
185 if NULL, the type is the type of the ->decl field. This is only of
186 relevance for objects with external or internal linkage which may
187 be redeclared in inner scopes, forming composite types that only
188 persist for the duration of those scopes. In the external scope,
189 this stores the composite of all the types declared for this
190 object, visible or not. The ->inner_comp field (used only at file
191 scope) stores whether an incomplete array type at file scope was
192 completed at an inner scope to an array size other than 1.
194 The depth field is copied from the scope structure that holds this
195 decl. It is used to preserve the proper ordering of the ->shadowed
196 field (see bind()) and also for a handful of special-case checks.
197 Finally, the invisible bit is true for a decl which should be
198 ignored for purposes of normal name lookup, and the nested bit is
199 true for a decl that's been bound a second time in an inner scope;
200 in all such cases, the binding in the outer scope will have its
201 invisible bit true. */
203 struct c_binding GTY((chain_next ("%h.prev")))
205 tree decl; /* the decl bound */
206 tree type; /* the type in this scope */
207 tree id; /* the identifier it's bound to */
208 struct c_binding *prev; /* the previous decl in this scope */
209 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
210 unsigned int depth : 28; /* depth of this scope */
211 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
212 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
213 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
214 /* one free bit */
216 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
217 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
218 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
219 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
221 #define I_SYMBOL_BINDING(node) \
222 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
223 #define I_SYMBOL_DECL(node) \
224 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
226 #define I_TAG_BINDING(node) \
227 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
228 #define I_TAG_DECL(node) \
229 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
231 #define I_LABEL_BINDING(node) \
232 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
233 #define I_LABEL_DECL(node) \
234 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
236 /* Each C symbol points to three linked lists of c_binding structures.
237 These describe the values of the identifier in the three different
238 namespaces defined by the language. */
240 struct lang_identifier GTY(())
242 struct c_common_identifier common_id;
243 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
244 struct c_binding *tag_binding; /* struct/union/enum tags */
245 struct c_binding *label_binding; /* labels */
248 /* Validate c-lang.c's assumptions. */
249 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
250 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
252 /* The resulting tree type. */
254 union lang_tree_node
255 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
256 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : (union lang_tree_node *) TREE_CHAIN (&%h.generic)")))
258 union tree_node GTY ((tag ("0"),
259 desc ("tree_node_structure (&%h)")))
260 generic;
261 struct lang_identifier GTY ((tag ("1"))) identifier;
264 /* Each c_scope structure describes the complete contents of one
265 scope. Four scopes are distinguished specially: the innermost or
266 current scope, the innermost function scope, the file scope (always
267 the second to outermost) and the outermost or external scope.
269 Most declarations are recorded in the current scope.
271 All normal label declarations are recorded in the innermost
272 function scope, as are bindings of undeclared identifiers to
273 error_mark_node. (GCC permits nested functions as an extension,
274 hence the 'innermost' qualifier.) Explicitly declared labels
275 (using the __label__ extension) appear in the current scope.
277 Being in the file scope (current_scope == file_scope) causes
278 special behavior in several places below. Also, under some
279 conditions the Objective-C front end records declarations in the
280 file scope even though that isn't the current scope.
282 All declarations with external linkage are recorded in the external
283 scope, even if they aren't visible there; this models the fact that
284 such declarations are visible to the entire program, and (with a
285 bit of cleverness, see pushdecl) allows diagnosis of some violations
286 of C99 6.2.2p7 and 6.2.7p2:
288 If, within the same translation unit, the same identifier appears
289 with both internal and external linkage, the behavior is
290 undefined.
292 All declarations that refer to the same object or function shall
293 have compatible type; otherwise, the behavior is undefined.
295 Initially only the built-in declarations, which describe compiler
296 intrinsic functions plus a subset of the standard library, are in
297 this scope.
299 The order of the blocks list matters, and it is frequently appended
300 to. To avoid having to walk all the way to the end of the list on
301 each insertion, or reverse the list later, we maintain a pointer to
302 the last list entry. (FIXME: It should be feasible to use a reversed
303 list here.)
305 The bindings list is strictly in reverse order of declarations;
306 pop_scope relies on this. */
309 struct c_scope GTY((chain_next ("%h.outer")))
311 /* The scope containing this one. */
312 struct c_scope *outer;
314 /* The next outermost function scope. */
315 struct c_scope *outer_function;
317 /* All bindings in this scope. */
318 struct c_binding *bindings;
320 /* For each scope (except the global one), a chain of BLOCK nodes
321 for all the scopes that were entered and exited one level down. */
322 tree blocks;
323 tree blocks_last;
325 /* The depth of this scope. Used to keep the ->shadowed chain of
326 bindings sorted innermost to outermost. */
327 unsigned int depth : 28;
329 /* True if we are currently filling this scope with parameter
330 declarations. */
331 BOOL_BITFIELD parm_flag : 1;
333 /* True if we already complained about forward parameter decls
334 in this scope. This prevents double warnings on
335 foo (int a; int b; ...) */
336 BOOL_BITFIELD warned_forward_parm_decls : 1;
338 /* True if this is the outermost block scope of a function body.
339 This scope contains the parameters, the local variables declared
340 in the outermost block, and all the labels (except those in
341 nested functions, or declared at block scope with __label__). */
342 BOOL_BITFIELD function_body : 1;
344 /* True means make a BLOCK for this scope no matter what. */
345 BOOL_BITFIELD keep : 1;
348 /* The scope currently in effect. */
350 static GTY(()) struct c_scope *current_scope;
352 /* The innermost function scope. Ordinary (not explicitly declared)
353 labels, bindings to error_mark_node, and the lazily-created
354 bindings of __func__ and its friends get this scope. */
356 static GTY(()) struct c_scope *current_function_scope;
358 /* The C file scope. This is reset for each input translation unit. */
360 static GTY(()) struct c_scope *file_scope;
362 /* The outermost scope. This is used for all declarations with
363 external linkage, and only these, hence the name. */
365 static GTY(()) struct c_scope *external_scope;
367 /* A chain of c_scope structures awaiting reuse. */
369 static GTY((deletable)) struct c_scope *scope_freelist;
371 /* A chain of c_binding structures awaiting reuse. */
373 static GTY((deletable)) struct c_binding *binding_freelist;
375 /* Append VAR to LIST in scope SCOPE. */
376 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
377 struct c_scope *s_ = (scope); \
378 tree d_ = (decl); \
379 if (s_->list##_last) \
380 TREE_CHAIN (s_->list##_last) = d_; \
381 else \
382 s_->list = d_; \
383 s_->list##_last = d_; \
384 } while (0)
386 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
387 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
388 struct c_scope *t_ = (tscope); \
389 struct c_scope *f_ = (fscope); \
390 if (t_->to##_last) \
391 TREE_CHAIN (t_->to##_last) = f_->from; \
392 else \
393 t_->to = f_->from; \
394 t_->to##_last = f_->from##_last; \
395 } while (0)
397 /* True means unconditionally make a BLOCK for the next scope pushed. */
399 static bool keep_next_level_flag;
401 /* True means the next call to push_scope will be the outermost scope
402 of a function body, so do not push a new scope, merely cease
403 expecting parameter decls. */
405 static bool next_is_function_body;
407 /* Functions called automatically at the beginning and end of execution. */
409 static GTY(()) tree static_ctors;
410 static GTY(()) tree static_dtors;
412 /* Forward declarations. */
413 static tree lookup_name_in_scope (tree, struct c_scope *);
414 static tree c_make_fname_decl (tree, int);
415 static tree grokdeclarator (const struct c_declarator *,
416 struct c_declspecs *,
417 enum decl_context, bool, tree *);
418 static tree grokparms (struct c_arg_info *, bool);
419 static void layout_array_type (tree);
421 /* States indicating how grokdeclarator() should handle declspecs marked
422 with __attribute__((deprecated)). An object declared as
423 __attribute__((deprecated)) suppresses warnings of uses of other
424 deprecated items. */
426 enum deprecated_states {
427 DEPRECATED_NORMAL,
428 DEPRECATED_SUPPRESS
431 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
433 void
434 c_print_identifier (FILE *file, tree node, int indent)
436 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
437 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
438 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
439 if (C_IS_RESERVED_WORD (node))
441 tree rid = ridpointers[C_RID_CODE (node)];
442 indent_to (file, indent + 4);
443 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
444 (void *) rid, IDENTIFIER_POINTER (rid));
448 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
449 which may be any of several kinds of DECL or TYPE or error_mark_node,
450 in the scope SCOPE. */
451 static void
452 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
454 struct c_binding *b, **here;
456 if (binding_freelist)
458 b = binding_freelist;
459 binding_freelist = b->prev;
461 else
462 b = GGC_NEW (struct c_binding);
464 b->shadowed = 0;
465 b->decl = decl;
466 b->id = name;
467 b->depth = scope->depth;
468 b->invisible = invisible;
469 b->nested = nested;
470 b->inner_comp = 0;
472 b->type = 0;
474 b->prev = scope->bindings;
475 scope->bindings = b;
477 if (!name)
478 return;
480 switch (TREE_CODE (decl))
482 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
483 case ENUMERAL_TYPE:
484 case UNION_TYPE:
485 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
486 case VAR_DECL:
487 case FUNCTION_DECL:
488 case TYPE_DECL:
489 case CONST_DECL:
490 case PARM_DECL:
491 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
493 default:
494 gcc_unreachable ();
497 /* Locate the appropriate place in the chain of shadowed decls
498 to insert this binding. Normally, scope == current_scope and
499 this does nothing. */
500 while (*here && (*here)->depth > scope->depth)
501 here = &(*here)->shadowed;
503 b->shadowed = *here;
504 *here = b;
507 /* Clear the binding structure B, stick it on the binding_freelist,
508 and return the former value of b->prev. This is used by pop_scope
509 and get_parm_info to iterate destructively over all the bindings
510 from a given scope. */
511 static struct c_binding *
512 free_binding_and_advance (struct c_binding *b)
514 struct c_binding *prev = b->prev;
516 memset (b, 0, sizeof (struct c_binding));
517 b->prev = binding_freelist;
518 binding_freelist = b;
520 return prev;
524 /* Hook called at end of compilation to assume 1 elt
525 for a file-scope tentative array defn that wasn't complete before. */
527 void
528 c_finish_incomplete_decl (tree decl)
530 if (TREE_CODE (decl) == VAR_DECL)
532 tree type = TREE_TYPE (decl);
533 if (type != error_mark_node
534 && TREE_CODE (type) == ARRAY_TYPE
535 && !DECL_EXTERNAL (decl)
536 && TYPE_DOMAIN (type) == 0)
538 warning (0, "%Jarray %qD assumed to have one element", decl, decl);
540 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
542 layout_decl (decl, 0);
547 /* The Objective-C front-end often needs to determine the current scope. */
549 void *
550 objc_get_current_scope (void)
552 return current_scope;
555 /* The following function is used only by Objective-C. It needs to live here
556 because it accesses the innards of c_scope. */
558 void
559 objc_mark_locals_volatile (void *enclosing_blk)
561 struct c_scope *scope;
562 struct c_binding *b;
564 for (scope = current_scope;
565 scope && scope != enclosing_blk;
566 scope = scope->outer)
568 for (b = scope->bindings; b; b = b->prev)
569 objc_volatilize_decl (b->decl);
571 /* Do not climb up past the current function. */
572 if (scope->function_body)
573 break;
577 /* Nonzero if we are currently in file scope. */
580 global_bindings_p (void)
582 return current_scope == file_scope && !c_override_global_bindings_to_false;
585 void
586 keep_next_level (void)
588 keep_next_level_flag = true;
591 /* Identify this scope as currently being filled with parameters. */
593 void
594 declare_parm_level (void)
596 current_scope->parm_flag = true;
599 void
600 push_scope (void)
602 if (next_is_function_body)
604 /* This is the transition from the parameters to the top level
605 of the function body. These are the same scope
606 (C99 6.2.1p4,6) so we do not push another scope structure.
607 next_is_function_body is set only by store_parm_decls, which
608 in turn is called when and only when we are about to
609 encounter the opening curly brace for the function body.
611 The outermost block of a function always gets a BLOCK node,
612 because the debugging output routines expect that each
613 function has at least one BLOCK. */
614 current_scope->parm_flag = false;
615 current_scope->function_body = true;
616 current_scope->keep = true;
617 current_scope->outer_function = current_function_scope;
618 current_function_scope = current_scope;
620 keep_next_level_flag = false;
621 next_is_function_body = false;
623 else
625 struct c_scope *scope;
626 if (scope_freelist)
628 scope = scope_freelist;
629 scope_freelist = scope->outer;
631 else
632 scope = GGC_CNEW (struct c_scope);
634 scope->keep = keep_next_level_flag;
635 scope->outer = current_scope;
636 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
638 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
639 possible. */
640 if (current_scope && scope->depth == 0)
642 scope->depth--;
643 sorry ("GCC supports only %u nested scopes", scope->depth);
646 current_scope = scope;
647 keep_next_level_flag = false;
651 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
653 static void
654 set_type_context (tree type, tree context)
656 for (type = TYPE_MAIN_VARIANT (type); type;
657 type = TYPE_NEXT_VARIANT (type))
658 TYPE_CONTEXT (type) = context;
661 /* Exit a scope. Restore the state of the identifier-decl mappings
662 that were in effect when this scope was entered. Return a BLOCK
663 node containing all the DECLs in this scope that are of interest
664 to debug info generation. */
666 tree
667 pop_scope (void)
669 struct c_scope *scope = current_scope;
670 tree block, context, p;
671 struct c_binding *b;
673 bool functionbody = scope->function_body;
674 bool keep = functionbody || scope->keep || scope->bindings;
676 c_end_vm_scope (scope->depth);
678 /* If appropriate, create a BLOCK to record the decls for the life
679 of this function. */
680 block = 0;
681 if (keep)
683 block = make_node (BLOCK);
684 BLOCK_SUBBLOCKS (block) = scope->blocks;
685 TREE_USED (block) = 1;
687 /* In each subblock, record that this is its superior. */
688 for (p = scope->blocks; p; p = TREE_CHAIN (p))
689 BLOCK_SUPERCONTEXT (p) = block;
691 BLOCK_VARS (block) = 0;
694 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
695 scope must be set so that they point to the appropriate
696 construct, i.e. either to the current FUNCTION_DECL node, or
697 else to the BLOCK node we just constructed.
699 Note that for tagged types whose scope is just the formal
700 parameter list for some function type specification, we can't
701 properly set their TYPE_CONTEXTs here, because we don't have a
702 pointer to the appropriate FUNCTION_TYPE node readily available
703 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
704 type nodes get set in `grokdeclarator' as soon as we have created
705 the FUNCTION_TYPE node which will represent the "scope" for these
706 "parameter list local" tagged types. */
707 if (scope->function_body)
708 context = current_function_decl;
709 else if (scope == file_scope)
711 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
712 TREE_CHAIN (file_decl) = all_translation_units;
713 all_translation_units = file_decl;
714 context = file_decl;
716 else
717 context = block;
719 /* Clear all bindings in this scope. */
720 for (b = scope->bindings; b; b = free_binding_and_advance (b))
722 p = b->decl;
723 switch (TREE_CODE (p))
725 case LABEL_DECL:
726 /* Warnings for unused labels, errors for undefined labels. */
727 if (TREE_USED (p) && !DECL_INITIAL (p))
729 error ("%Jlabel %qD used but not defined", p, p);
730 DECL_INITIAL (p) = error_mark_node;
732 else if (!TREE_USED (p) && warn_unused_label)
734 if (DECL_INITIAL (p))
735 warning (0, "%Jlabel %qD defined but not used", p, p);
736 else
737 warning (0, "%Jlabel %qD declared but not defined", p, p);
739 /* Labels go in BLOCK_VARS. */
740 TREE_CHAIN (p) = BLOCK_VARS (block);
741 BLOCK_VARS (block) = p;
742 gcc_assert (I_LABEL_BINDING (b->id) == b);
743 I_LABEL_BINDING (b->id) = b->shadowed;
744 break;
746 case ENUMERAL_TYPE:
747 case UNION_TYPE:
748 case RECORD_TYPE:
749 set_type_context (p, context);
751 /* Types may not have tag-names, in which case the type
752 appears in the bindings list with b->id NULL. */
753 if (b->id)
755 gcc_assert (I_TAG_BINDING (b->id) == b);
756 I_TAG_BINDING (b->id) = b->shadowed;
758 break;
760 case FUNCTION_DECL:
761 /* Propagate TREE_ADDRESSABLE from nested functions to their
762 containing functions. */
763 if (!TREE_ASM_WRITTEN (p)
764 && DECL_INITIAL (p) != 0
765 && TREE_ADDRESSABLE (p)
766 && DECL_ABSTRACT_ORIGIN (p) != 0
767 && DECL_ABSTRACT_ORIGIN (p) != p)
768 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
769 if (!DECL_EXTERNAL (p)
770 && DECL_INITIAL (p) == 0)
772 error ("%Jnested function %qD declared but never defined", p, p);
773 undef_nested_function = true;
775 goto common_symbol;
777 case VAR_DECL:
778 /* Warnings for unused variables. */
779 if (warn_unused_variable
780 && !TREE_USED (p)
781 && !DECL_IN_SYSTEM_HEADER (p)
782 && DECL_NAME (p)
783 && !DECL_ARTIFICIAL (p)
784 && scope != file_scope
785 && scope != external_scope)
786 warning (0, "%Junused variable %qD", p, p);
788 if (b->inner_comp)
790 error ("%Jtype of array %qD completed incompatibly with"
791 " implicit initialization", p, p);
794 /* Fall through. */
795 case TYPE_DECL:
796 case CONST_DECL:
797 common_symbol:
798 /* All of these go in BLOCK_VARS, but only if this is the
799 binding in the home scope. */
800 if (!b->nested)
802 TREE_CHAIN (p) = BLOCK_VARS (block);
803 BLOCK_VARS (block) = p;
805 /* If this is the file scope, and we are processing more
806 than one translation unit in this compilation, set
807 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
808 This makes same_translation_unit_p work, and causes
809 static declarations to be given disambiguating suffixes. */
810 if (scope == file_scope && num_in_fnames > 1)
812 DECL_CONTEXT (p) = context;
813 if (TREE_CODE (p) == TYPE_DECL)
814 set_type_context (TREE_TYPE (p), context);
817 /* Fall through. */
818 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
819 already been put there by store_parm_decls. Unused-
820 parameter warnings are handled by function.c.
821 error_mark_node obviously does not go in BLOCK_VARS and
822 does not get unused-variable warnings. */
823 case PARM_DECL:
824 case ERROR_MARK:
825 /* It is possible for a decl not to have a name. We get
826 here with b->id NULL in this case. */
827 if (b->id)
829 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
830 I_SYMBOL_BINDING (b->id) = b->shadowed;
831 if (b->shadowed && b->shadowed->type)
832 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
834 break;
836 default:
837 gcc_unreachable ();
842 /* Dispose of the block that we just made inside some higher level. */
843 if ((scope->function_body || scope == file_scope) && context)
845 DECL_INITIAL (context) = block;
846 BLOCK_SUPERCONTEXT (block) = context;
848 else if (scope->outer)
850 if (block)
851 SCOPE_LIST_APPEND (scope->outer, blocks, block);
852 /* If we did not make a block for the scope just exited, any
853 blocks made for inner scopes must be carried forward so they
854 will later become subblocks of something else. */
855 else if (scope->blocks)
856 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
859 /* Pop the current scope, and free the structure for reuse. */
860 current_scope = scope->outer;
861 if (scope->function_body)
862 current_function_scope = scope->outer_function;
864 memset (scope, 0, sizeof (struct c_scope));
865 scope->outer = scope_freelist;
866 scope_freelist = scope;
868 return block;
871 void
872 push_file_scope (void)
874 tree decl;
876 if (file_scope)
877 return;
879 push_scope ();
880 file_scope = current_scope;
882 start_fname_decls ();
884 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
885 bind (DECL_NAME (decl), decl, file_scope,
886 /*invisible=*/false, /*nested=*/true);
889 void
890 pop_file_scope (void)
892 /* In case there were missing closebraces, get us back to the global
893 binding level. */
894 while (current_scope != file_scope)
895 pop_scope ();
897 /* __FUNCTION__ is defined at file scope (""). This
898 call may not be necessary as my tests indicate it
899 still works without it. */
900 finish_fname_decls ();
902 /* This is the point to write out a PCH if we're doing that.
903 In that case we do not want to do anything else. */
904 if (pch_file)
906 c_common_write_pch ();
907 return;
910 /* Pop off the file scope and close this translation unit. */
911 pop_scope ();
912 file_scope = 0;
914 maybe_apply_pending_pragma_weaks ();
915 cgraph_finalize_compilation_unit ();
918 /* Insert BLOCK at the end of the list of subblocks of the current
919 scope. This is used when a BIND_EXPR is expanded, to handle the
920 BLOCK node inside the BIND_EXPR. */
922 void
923 insert_block (tree block)
925 TREE_USED (block) = 1;
926 SCOPE_LIST_APPEND (current_scope, blocks, block);
929 /* Push a definition or a declaration of struct, union or enum tag "name".
930 "type" should be the type node.
931 We assume that the tag "name" is not already defined.
933 Note that the definition may really be just a forward reference.
934 In that case, the TYPE_SIZE will be zero. */
936 static void
937 pushtag (tree name, tree type)
939 /* Record the identifier as the type's name if it has none. */
940 if (name && !TYPE_NAME (type))
941 TYPE_NAME (type) = name;
942 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
944 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
945 tagged type we just added to the current scope. This fake
946 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
947 to output a representation of a tagged type, and it also gives
948 us a convenient place to record the "scope start" address for the
949 tagged type. */
951 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
953 /* An approximation for now, so we can tell this is a function-scope tag.
954 This will be updated in pop_scope. */
955 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
958 /* Subroutine of compare_decls. Allow harmless mismatches in return
959 and argument types provided that the type modes match. This function
960 return a unified type given a suitable match, and 0 otherwise. */
962 static tree
963 match_builtin_function_types (tree newtype, tree oldtype)
965 tree newrettype, oldrettype;
966 tree newargs, oldargs;
967 tree trytype, tryargs;
969 /* Accept the return type of the new declaration if same modes. */
970 oldrettype = TREE_TYPE (oldtype);
971 newrettype = TREE_TYPE (newtype);
973 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
974 return 0;
976 oldargs = TYPE_ARG_TYPES (oldtype);
977 newargs = TYPE_ARG_TYPES (newtype);
978 tryargs = newargs;
980 while (oldargs || newargs)
982 if (!oldargs
983 || !newargs
984 || !TREE_VALUE (oldargs)
985 || !TREE_VALUE (newargs)
986 || TYPE_MODE (TREE_VALUE (oldargs))
987 != TYPE_MODE (TREE_VALUE (newargs)))
988 return 0;
990 oldargs = TREE_CHAIN (oldargs);
991 newargs = TREE_CHAIN (newargs);
994 trytype = build_function_type (newrettype, tryargs);
995 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
998 /* Subroutine of diagnose_mismatched_decls. Check for function type
999 mismatch involving an empty arglist vs a nonempty one and give clearer
1000 diagnostics. */
1001 static void
1002 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1003 tree newtype, tree oldtype)
1005 tree t;
1007 if (TREE_CODE (olddecl) != FUNCTION_DECL
1008 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1009 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1011 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1012 return;
1014 t = TYPE_ARG_TYPES (oldtype);
1015 if (t == 0)
1016 t = TYPE_ARG_TYPES (newtype);
1017 for (; t; t = TREE_CHAIN (t))
1019 tree type = TREE_VALUE (t);
1021 if (TREE_CHAIN (t) == 0
1022 && TYPE_MAIN_VARIANT (type) != void_type_node)
1024 inform ("a parameter list with an ellipsis can%'t match "
1025 "an empty parameter name list declaration");
1026 break;
1029 if (c_type_promotes_to (type) != type)
1031 inform ("an argument type that has a default promotion can%'t match "
1032 "an empty parameter name list declaration");
1033 break;
1038 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1039 old-style function definition, NEWDECL is a prototype declaration.
1040 Diagnose inconsistencies in the argument list. Returns TRUE if
1041 the prototype is compatible, FALSE if not. */
1042 static bool
1043 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1045 tree newargs, oldargs;
1046 int i;
1048 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1050 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1051 newargs = TYPE_ARG_TYPES (newtype);
1052 i = 1;
1054 for (;;)
1056 tree oldargtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1057 tree newargtype = TYPE_MAIN_VARIANT (TREE_VALUE (newargs));
1059 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1060 break;
1062 /* Reaching the end of just one list means the two decls don't
1063 agree on the number of arguments. */
1064 if (END_OF_ARGLIST (oldargtype))
1066 error ("%Jprototype for %qD declares more arguments "
1067 "than previous old-style definition", newdecl, newdecl);
1068 return false;
1070 else if (END_OF_ARGLIST (newargtype))
1072 error ("%Jprototype for %qD declares fewer arguments "
1073 "than previous old-style definition", newdecl, newdecl);
1074 return false;
1077 /* Type for passing arg must be consistent with that declared
1078 for the arg. */
1079 else if (!comptypes (oldargtype, newargtype))
1081 error ("%Jprototype for %qD declares argument %d"
1082 " with incompatible type",
1083 newdecl, newdecl, i);
1084 return false;
1087 oldargs = TREE_CHAIN (oldargs);
1088 newargs = TREE_CHAIN (newargs);
1089 i++;
1092 /* If we get here, no errors were found, but do issue a warning
1093 for this poor-style construct. */
1094 warning (0, "%Jprototype for %qD follows non-prototype definition",
1095 newdecl, newdecl);
1096 return true;
1097 #undef END_OF_ARGLIST
1100 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1101 first in a pair of mismatched declarations, using the diagnostic
1102 function DIAG. */
1103 static void
1104 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1106 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1108 else if (DECL_INITIAL (decl))
1109 diag (N_("%Jprevious definition of %qD was here"), decl, decl);
1110 else if (C_DECL_IMPLICIT (decl))
1111 diag (N_("%Jprevious implicit declaration of %qD was here"), decl, decl);
1112 else
1113 diag (N_("%Jprevious declaration of %qD was here"), decl, decl);
1116 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1117 Returns true if the caller should proceed to merge the two, false
1118 if OLDDECL should simply be discarded. As a side effect, issues
1119 all necessary diagnostics for invalid or poor-style combinations.
1120 If it returns true, writes the types of NEWDECL and OLDDECL to
1121 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1122 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1124 static bool
1125 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1126 tree *newtypep, tree *oldtypep)
1128 tree newtype, oldtype;
1129 bool pedwarned = false;
1130 bool warned = false;
1131 bool retval = true;
1133 /* If we have error_mark_node for either decl or type, just discard
1134 the previous decl - we're in an error cascade already. */
1135 if (olddecl == error_mark_node || newdecl == error_mark_node)
1136 return false;
1137 *oldtypep = oldtype = TREE_TYPE (olddecl);
1138 *newtypep = newtype = TREE_TYPE (newdecl);
1139 if (oldtype == error_mark_node || newtype == error_mark_node)
1140 return false;
1142 /* Two different categories of symbol altogether. This is an error
1143 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1144 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1146 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1147 && DECL_BUILT_IN (olddecl)
1148 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1150 error ("%J%qD redeclared as different kind of symbol",
1151 newdecl, newdecl);
1152 locate_old_decl (olddecl, error);
1154 else if (TREE_PUBLIC (newdecl))
1155 warning (0, "%Jbuilt-in function %qD declared as non-function",
1156 newdecl, newdecl);
1157 else
1158 warning (OPT_Wshadow, "%Jdeclaration of %qD shadows "
1159 "a built-in function", newdecl, newdecl);
1160 return false;
1163 /* Enumerators have no linkage, so may only be declared once in a
1164 given scope. */
1165 if (TREE_CODE (olddecl) == CONST_DECL)
1167 error ("%Jredeclaration of enumerator %qD", newdecl, newdecl);
1168 locate_old_decl (olddecl, error);
1169 return false;
1172 if (!comptypes (oldtype, newtype))
1174 if (TREE_CODE (olddecl) == FUNCTION_DECL
1175 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1177 /* Accept harmless mismatch in function types.
1178 This is for the ffs and fprintf builtins. */
1179 tree trytype = match_builtin_function_types (newtype, oldtype);
1181 if (trytype && comptypes (newtype, trytype))
1182 *oldtypep = oldtype = trytype;
1183 else
1185 /* If types don't match for a built-in, throw away the
1186 built-in. No point in calling locate_old_decl here, it
1187 won't print anything. */
1188 warning (0, "%Jconflicting types for built-in function %qD",
1189 newdecl, newdecl);
1190 return false;
1193 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1194 && DECL_IS_BUILTIN (olddecl))
1196 /* A conflicting function declaration for a predeclared
1197 function that isn't actually built in. Objective C uses
1198 these. The new declaration silently overrides everything
1199 but the volatility (i.e. noreturn) indication. See also
1200 below. FIXME: Make Objective C use normal builtins. */
1201 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1202 return false;
1204 /* Permit void foo (...) to match int foo (...) if the latter is
1205 the definition and implicit int was used. See
1206 c-torture/compile/920625-2.c. */
1207 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1208 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1209 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1210 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1212 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1213 /* Make sure we keep void as the return type. */
1214 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1215 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1216 pedwarned = true;
1218 /* Permit void foo (...) to match an earlier call to foo (...) with
1219 no declared type (thus, implicitly int). */
1220 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1221 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1222 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1223 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1225 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1226 /* Make sure we keep void as the return type. */
1227 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1228 pedwarned = true;
1230 else
1232 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1233 error ("%J conflicting type qualifiers for %qD", newdecl, newdecl);
1234 else
1235 error ("%Jconflicting types for %qD", newdecl, newdecl);
1236 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1237 locate_old_decl (olddecl, error);
1238 return false;
1242 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1243 but silently ignore the redeclaration if either is in a system
1244 header. (Conflicting redeclarations were handled above.) */
1245 if (TREE_CODE (newdecl) == TYPE_DECL)
1247 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1248 return true; /* Allow OLDDECL to continue in use. */
1250 error ("%Jredefinition of typedef %qD", newdecl, newdecl);
1251 locate_old_decl (olddecl, error);
1252 return false;
1255 /* Function declarations can either be 'static' or 'extern' (no
1256 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1257 can never conflict with each other on account of linkage (6.2.2p4).
1258 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1259 two definitions if one is 'extern inline' and one is not. The non-
1260 extern-inline definition supersedes the extern-inline definition. */
1261 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1263 /* If you declare a built-in function name as static, or
1264 define the built-in with an old-style definition (so we
1265 can't validate the argument list) the built-in definition is
1266 overridden, but optionally warn this was a bad choice of name. */
1267 if (DECL_BUILT_IN (olddecl)
1268 && !C_DECL_DECLARED_BUILTIN (olddecl)
1269 && (!TREE_PUBLIC (newdecl)
1270 || (DECL_INITIAL (newdecl)
1271 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1273 warning (OPT_Wshadow, "%Jdeclaration of %qD shadows "
1274 "a built-in function", newdecl, newdecl);
1275 /* Discard the old built-in function. */
1276 return false;
1279 if (DECL_INITIAL (newdecl))
1281 if (DECL_INITIAL (olddecl))
1283 /* If both decls have extern inline and are in the same TU,
1284 reject the new decl. */
1285 if (DECL_DECLARED_INLINE_P (olddecl)
1286 && DECL_EXTERNAL (olddecl)
1287 && DECL_DECLARED_INLINE_P (newdecl)
1288 && DECL_EXTERNAL (newdecl)
1289 && same_translation_unit_p (newdecl, olddecl))
1291 error ("%Jredefinition of %qD", newdecl, newdecl);
1292 locate_old_decl (olddecl, error);
1293 return false;
1295 /* If both decls have not extern inline, reject the new decl. */
1296 if (!DECL_DECLARED_INLINE_P (olddecl)
1297 && !DECL_EXTERNAL (olddecl)
1298 && !DECL_DECLARED_INLINE_P (newdecl)
1299 && !DECL_EXTERNAL (newdecl))
1301 error ("%Jredefinition of %qD", newdecl, newdecl);
1302 locate_old_decl (olddecl, error);
1303 return false;
1305 /* If the new decl is declared as extern inline, error if they are
1306 in the same TU, otherwise retain the old decl. */
1307 if (!DECL_DECLARED_INLINE_P (olddecl)
1308 && !DECL_EXTERNAL (olddecl)
1309 && DECL_DECLARED_INLINE_P (newdecl)
1310 && DECL_EXTERNAL (newdecl))
1312 if (same_translation_unit_p (newdecl, olddecl))
1314 error ("%Jredefinition of %qD", newdecl, newdecl);
1315 locate_old_decl (olddecl, error);
1316 return false;
1318 else
1319 retval = false;
1323 /* If we have a prototype after an old-style function definition,
1324 the argument types must be checked specially. */
1325 else if (DECL_INITIAL (olddecl)
1326 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1327 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1328 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1330 locate_old_decl (olddecl, error);
1331 return false;
1333 /* A non-static declaration (even an "extern") followed by a
1334 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1335 The same is true for a static forward declaration at block
1336 scope followed by a non-static declaration/definition at file
1337 scope. Static followed by non-static at the same scope is
1338 not undefined behavior, and is the most convenient way to get
1339 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1340 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1341 we do diagnose it if -Wtraditional. */
1342 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1344 /* Two exceptions to the rule. If olddecl is an extern
1345 inline, or a predeclared function that isn't actually
1346 built in, newdecl silently overrides olddecl. The latter
1347 occur only in Objective C; see also above. (FIXME: Make
1348 Objective C use normal builtins.) */
1349 if (!DECL_IS_BUILTIN (olddecl)
1350 && !(DECL_EXTERNAL (olddecl)
1351 && DECL_DECLARED_INLINE_P (olddecl)))
1353 error ("%Jstatic declaration of %qD follows "
1354 "non-static declaration", newdecl, newdecl);
1355 locate_old_decl (olddecl, error);
1357 return false;
1359 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1361 if (DECL_CONTEXT (olddecl))
1363 error ("%Jnon-static declaration of %qD follows "
1364 "static declaration", newdecl, newdecl);
1365 locate_old_decl (olddecl, error);
1366 return false;
1368 else if (warn_traditional)
1370 warning (0, "%Jnon-static declaration of %qD follows "
1371 "static declaration", newdecl, newdecl);
1372 warned = true;
1376 else if (TREE_CODE (newdecl) == VAR_DECL)
1378 /* Only variables can be thread-local, and all declarations must
1379 agree on this property. */
1380 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1382 if (DECL_THREAD_LOCAL (newdecl))
1383 error ("%Jthread-local declaration of %qD follows "
1384 "non-thread-local declaration", newdecl, newdecl);
1385 else
1386 error ("%Jnon-thread-local declaration of %qD follows "
1387 "thread-local declaration", newdecl, newdecl);
1389 locate_old_decl (olddecl, error);
1390 return false;
1393 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1394 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1396 error ("%Jredefinition of %qD", newdecl, newdecl);
1397 locate_old_decl (olddecl, error);
1398 return false;
1401 /* Objects declared at file scope: if the first declaration had
1402 external linkage (even if it was an external reference) the
1403 second must have external linkage as well, or the behavior is
1404 undefined. If the first declaration had internal linkage, then
1405 the second must too, or else be an external reference (in which
1406 case the composite declaration still has internal linkage).
1407 As for function declarations, we warn about the static-then-
1408 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1409 if (DECL_FILE_SCOPE_P (newdecl)
1410 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1412 if (DECL_EXTERNAL (newdecl))
1414 if (!DECL_FILE_SCOPE_P (olddecl))
1416 error ("%Jextern declaration of %qD follows "
1417 "declaration with no linkage", newdecl, newdecl);
1418 locate_old_decl (olddecl, error);
1419 return false;
1421 else if (warn_traditional)
1423 warning (0, "%Jnon-static declaration of %qD follows "
1424 "static declaration", newdecl, newdecl);
1425 warned = true;
1428 else
1430 if (TREE_PUBLIC (newdecl))
1431 error ("%Jnon-static declaration of %qD follows "
1432 "static declaration", newdecl, newdecl);
1433 else
1434 error ("%Jstatic declaration of %qD follows "
1435 "non-static declaration", newdecl, newdecl);
1437 locate_old_decl (olddecl, error);
1438 return false;
1441 /* Two objects with the same name declared at the same block
1442 scope must both be external references (6.7p3). */
1443 else if (!DECL_FILE_SCOPE_P (newdecl))
1445 if (DECL_EXTERNAL (newdecl))
1447 /* Extern with initializer at block scope, which will
1448 already have received an error. */
1450 else if (DECL_EXTERNAL (olddecl))
1452 error ("%Jdeclaration of %qD with no linkage follows "
1453 "extern declaration", newdecl, newdecl);
1454 locate_old_decl (olddecl, error);
1456 else
1458 error ("%Jredeclaration of %qD with no linkage",
1459 newdecl, newdecl);
1460 locate_old_decl (olddecl, error);
1463 return false;
1467 /* warnings */
1468 /* All decls must agree on a visibility. */
1469 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1470 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1472 warning (0, "%Jredeclaration of %qD with different visibility "
1473 "(old visibility preserved)", newdecl, newdecl);
1474 warned = true;
1477 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1479 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1480 if (DECL_DECLARED_INLINE_P (newdecl)
1481 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1483 warning (OPT_Wattributes, "%Jinline declaration of %qD follows "
1484 "declaration with attribute noinline", newdecl, newdecl);
1485 warned = true;
1487 else if (DECL_DECLARED_INLINE_P (olddecl)
1488 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1490 warning (OPT_Wattributes, "%Jdeclaration of %qD with attribute "
1491 "noinline follows inline declaration ", newdecl, newdecl);
1492 warned = true;
1495 /* Inline declaration after use or definition.
1496 ??? Should we still warn about this now we have unit-at-a-time
1497 mode and can get it right?
1498 Definitely don't complain if the decls are in different translation
1499 units. */
1500 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1501 && same_translation_unit_p (olddecl, newdecl))
1503 if (TREE_USED (olddecl))
1505 warning (0, "%J%qD declared inline after being called",
1506 olddecl, olddecl);
1507 warned = true;
1509 else if (DECL_INITIAL (olddecl))
1511 warning (0, "%J%qD declared inline after its definition",
1512 olddecl, olddecl);
1513 warned = true;
1517 else /* PARM_DECL, VAR_DECL */
1519 /* Redeclaration of a parameter is a constraint violation (this is
1520 not explicitly stated, but follows from C99 6.7p3 [no more than
1521 one declaration of the same identifier with no linkage in the
1522 same scope, except type tags] and 6.2.2p6 [parameters have no
1523 linkage]). We must check for a forward parameter declaration,
1524 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1525 an extension, the mandatory diagnostic for which is handled by
1526 mark_forward_parm_decls. */
1528 if (TREE_CODE (newdecl) == PARM_DECL
1529 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1531 error ("%Jredefinition of parameter %qD", newdecl, newdecl);
1532 locate_old_decl (olddecl, error);
1533 return false;
1537 /* Optional warning for completely redundant decls. */
1538 if (!warned && !pedwarned
1539 && warn_redundant_decls
1540 /* Don't warn about a function declaration followed by a
1541 definition. */
1542 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1543 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1544 /* Don't warn about redundant redeclarations of builtins. */
1545 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1546 && !DECL_BUILT_IN (newdecl)
1547 && DECL_BUILT_IN (olddecl)
1548 && !C_DECL_DECLARED_BUILTIN (olddecl))
1549 /* Don't warn about an extern followed by a definition. */
1550 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1551 /* Don't warn about forward parameter decls. */
1552 && !(TREE_CODE (newdecl) == PARM_DECL
1553 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1555 warning (0, "%Jredundant redeclaration of %qD", newdecl, newdecl);
1556 warned = true;
1559 /* Report location of previous decl/defn in a consistent manner. */
1560 if (warned || pedwarned)
1561 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1563 return retval;
1566 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1567 consistent with OLDDECL, but carries new information. Merge the
1568 new information into OLDDECL. This function issues no
1569 diagnostics. */
1571 static void
1572 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1574 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1575 && DECL_INITIAL (newdecl) != 0);
1576 int new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1577 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1578 int old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1579 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1581 /* For real parm decl following a forward decl, rechain the old decl
1582 in its new location and clear TREE_ASM_WRITTEN (it's not a
1583 forward decl anymore). */
1584 if (TREE_CODE (newdecl) == PARM_DECL
1585 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1587 struct c_binding *b, **here;
1589 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1590 if ((*here)->decl == olddecl)
1591 goto found;
1592 gcc_unreachable ();
1594 found:
1595 b = *here;
1596 *here = b->prev;
1597 b->prev = current_scope->bindings;
1598 current_scope->bindings = b;
1600 TREE_ASM_WRITTEN (olddecl) = 0;
1603 DECL_ATTRIBUTES (newdecl)
1604 = targetm.merge_decl_attributes (olddecl, newdecl);
1606 /* Merge the data types specified in the two decls. */
1607 TREE_TYPE (newdecl)
1608 = TREE_TYPE (olddecl)
1609 = composite_type (newtype, oldtype);
1611 /* Lay the type out, unless already done. */
1612 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1614 if (TREE_TYPE (newdecl) != error_mark_node)
1615 layout_type (TREE_TYPE (newdecl));
1616 if (TREE_CODE (newdecl) != FUNCTION_DECL
1617 && TREE_CODE (newdecl) != TYPE_DECL
1618 && TREE_CODE (newdecl) != CONST_DECL)
1619 layout_decl (newdecl, 0);
1621 else
1623 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1624 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1625 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1626 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1627 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1628 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1630 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1631 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1635 /* Keep the old rtl since we can safely use it. */
1636 COPY_DECL_RTL (olddecl, newdecl);
1638 /* Merge the type qualifiers. */
1639 if (TREE_READONLY (newdecl))
1640 TREE_READONLY (olddecl) = 1;
1642 if (TREE_THIS_VOLATILE (newdecl))
1644 TREE_THIS_VOLATILE (olddecl) = 1;
1645 if (TREE_CODE (newdecl) == VAR_DECL)
1646 make_var_volatile (newdecl);
1649 /* Merge deprecatedness. */
1650 if (TREE_DEPRECATED (newdecl))
1651 TREE_DEPRECATED (olddecl) = 1;
1653 /* Keep source location of definition rather than declaration and of
1654 prototype rather than non-prototype unless that prototype is
1655 built-in. */
1656 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1657 || (old_is_prototype && !new_is_prototype
1658 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1659 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1661 /* Merge the unused-warning information. */
1662 if (DECL_IN_SYSTEM_HEADER (olddecl))
1663 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1664 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1665 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1667 /* Merge the initialization information. */
1668 if (DECL_INITIAL (newdecl) == 0)
1669 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1671 /* Merge the section attribute.
1672 We want to issue an error if the sections conflict but that must be
1673 done later in decl_attributes since we are called before attributes
1674 are assigned. */
1675 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1676 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1678 /* Copy the assembler name.
1679 Currently, it can only be defined in the prototype. */
1680 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1682 /* Use visibility of whichever declaration had it specified */
1683 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1685 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1686 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1689 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1691 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1692 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1693 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1694 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1695 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1696 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1697 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1698 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1699 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1700 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1703 /* Merge the storage class information. */
1704 merge_weak (newdecl, olddecl);
1706 /* For functions, static overrides non-static. */
1707 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1709 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1710 /* This is since we don't automatically
1711 copy the attributes of NEWDECL into OLDDECL. */
1712 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1713 /* If this clears `static', clear it in the identifier too. */
1714 if (!TREE_PUBLIC (olddecl))
1715 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1717 if (DECL_EXTERNAL (newdecl))
1719 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1720 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1722 /* An extern decl does not override previous storage class. */
1723 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1724 if (!DECL_EXTERNAL (newdecl))
1726 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1727 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1730 else
1732 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1733 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1736 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1738 /* If we're redefining a function previously defined as extern
1739 inline, make sure we emit debug info for the inline before we
1740 throw it away, in case it was inlined into a function that hasn't
1741 been written out yet. */
1742 if (new_is_definition && DECL_INITIAL (olddecl))
1744 if (TREE_USED (olddecl)
1745 /* In unit-at-a-time mode we never inline re-defined extern
1746 inline functions. */
1747 && !flag_unit_at_a_time
1748 && cgraph_function_possibly_inlined_p (olddecl))
1749 (*debug_hooks->outlining_inline_function) (olddecl);
1751 /* The new defn must not be inline. */
1752 DECL_INLINE (newdecl) = 0;
1753 DECL_UNINLINABLE (newdecl) = 1;
1755 else
1757 /* If either decl says `inline', this fn is inline,
1758 unless its definition was passed already. */
1759 if (DECL_DECLARED_INLINE_P (newdecl)
1760 || DECL_DECLARED_INLINE_P (olddecl))
1761 DECL_DECLARED_INLINE_P (newdecl) = 1;
1763 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1764 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1767 if (DECL_BUILT_IN (olddecl))
1769 /* If redeclaring a builtin function, it stays built in.
1770 But it gets tagged as having been declared. */
1771 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1772 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1773 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1774 if (new_is_prototype)
1775 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1776 else
1777 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1778 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1781 /* Also preserve various other info from the definition. */
1782 if (!new_is_definition)
1784 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1785 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1786 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1787 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1788 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1790 /* Set DECL_INLINE on the declaration if we've got a body
1791 from which to instantiate. */
1792 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1794 DECL_INLINE (newdecl) = 1;
1795 DECL_ABSTRACT_ORIGIN (newdecl)
1796 = DECL_ABSTRACT_ORIGIN (olddecl);
1799 else
1801 /* If a previous declaration said inline, mark the
1802 definition as inlinable. */
1803 if (DECL_DECLARED_INLINE_P (newdecl)
1804 && !DECL_UNINLINABLE (newdecl))
1805 DECL_INLINE (newdecl) = 1;
1809 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1810 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1812 unsigned olddecl_uid = DECL_UID (olddecl);
1813 tree olddecl_context = DECL_CONTEXT (olddecl);
1815 memcpy ((char *) olddecl + sizeof (struct tree_common),
1816 (char *) newdecl + sizeof (struct tree_common),
1817 sizeof (struct tree_decl) - sizeof (struct tree_common));
1818 DECL_UID (olddecl) = olddecl_uid;
1819 DECL_CONTEXT (olddecl) = olddecl_context;
1822 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1823 so that encode_section_info has a chance to look at the new decl
1824 flags and attributes. */
1825 if (DECL_RTL_SET_P (olddecl)
1826 && (TREE_CODE (olddecl) == FUNCTION_DECL
1827 || (TREE_CODE (olddecl) == VAR_DECL
1828 && TREE_STATIC (olddecl))))
1829 make_decl_rtl (olddecl);
1832 /* Handle when a new declaration NEWDECL has the same name as an old
1833 one OLDDECL in the same binding contour. Prints an error message
1834 if appropriate.
1836 If safely possible, alter OLDDECL to look like NEWDECL, and return
1837 true. Otherwise, return false. */
1839 static bool
1840 duplicate_decls (tree newdecl, tree olddecl)
1842 tree newtype = NULL, oldtype = NULL;
1844 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1845 return false;
1847 merge_decls (newdecl, olddecl, newtype, oldtype);
1848 return true;
1852 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1853 static void
1854 warn_if_shadowing (tree new_decl)
1856 struct c_binding *b;
1858 /* Shadow warnings wanted? */
1859 if (!warn_shadow
1860 /* No shadow warnings for internally generated vars. */
1861 || DECL_IS_BUILTIN (new_decl)
1862 /* No shadow warnings for vars made for inlining. */
1863 || DECL_FROM_INLINE (new_decl)
1864 /* Don't warn about the parm names in function declarator
1865 within a function declarator. It would be nice to avoid
1866 warning in any function declarator in a declaration, as
1867 opposed to a definition, but there is no way to tell
1868 it's not a definition at this point. */
1869 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1870 return;
1872 /* Is anything being shadowed? Invisible decls do not count. */
1873 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1874 if (b->decl && b->decl != new_decl && !b->invisible)
1876 tree old_decl = b->decl;
1878 if (old_decl == error_mark_node)
1880 warning (0, "%Jdeclaration of %qD shadows previous non-variable",
1881 new_decl, new_decl);
1882 break;
1884 else if (TREE_CODE (old_decl) == PARM_DECL)
1885 warning (0, "%Jdeclaration of %qD shadows a parameter",
1886 new_decl, new_decl);
1887 else if (DECL_FILE_SCOPE_P (old_decl))
1888 warning (0, "%Jdeclaration of %qD shadows a global declaration",
1889 new_decl, new_decl);
1890 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1891 && DECL_BUILT_IN (old_decl))
1893 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1894 new_decl, new_decl);
1895 break;
1897 else
1898 warning (0, "%Jdeclaration of %qD shadows a previous local",
1899 new_decl, new_decl);
1901 warning (0, "%Jshadowed declaration is here", old_decl);
1903 break;
1908 /* Subroutine of pushdecl.
1910 X is a TYPE_DECL for a typedef statement. Create a brand new
1911 ..._TYPE node (which will be just a variant of the existing
1912 ..._TYPE node with identical properties) and then install X
1913 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1915 The whole point here is to end up with a situation where each
1916 and every ..._TYPE node the compiler creates will be uniquely
1917 associated with AT MOST one node representing a typedef name.
1918 This way, even though the compiler substitutes corresponding
1919 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1920 early on, later parts of the compiler can always do the reverse
1921 translation and get back the corresponding typedef name. For
1922 example, given:
1924 typedef struct S MY_TYPE;
1925 MY_TYPE object;
1927 Later parts of the compiler might only know that `object' was of
1928 type `struct S' if it were not for code just below. With this
1929 code however, later parts of the compiler see something like:
1931 struct S' == struct S
1932 typedef struct S' MY_TYPE;
1933 struct S' object;
1935 And they can then deduce (from the node for type struct S') that
1936 the original object declaration was:
1938 MY_TYPE object;
1940 Being able to do this is important for proper support of protoize,
1941 and also for generating precise symbolic debugging information
1942 which takes full account of the programmer's (typedef) vocabulary.
1944 Obviously, we don't want to generate a duplicate ..._TYPE node if
1945 the TYPE_DECL node that we are now processing really represents a
1946 standard built-in type.
1948 Since all standard types are effectively declared at line zero
1949 in the source file, we can easily check to see if we are working
1950 on a standard type by checking the current value of lineno. */
1952 static void
1953 clone_underlying_type (tree x)
1955 if (DECL_IS_BUILTIN (x))
1957 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1958 TYPE_NAME (TREE_TYPE (x)) = x;
1960 else if (TREE_TYPE (x) != error_mark_node
1961 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1963 tree tt = TREE_TYPE (x);
1964 DECL_ORIGINAL_TYPE (x) = tt;
1965 tt = build_variant_type_copy (tt);
1966 TYPE_NAME (tt) = x;
1967 TREE_USED (tt) = TREE_USED (x);
1968 TREE_TYPE (x) = tt;
1972 /* Record a decl-node X as belonging to the current lexical scope.
1973 Check for errors (such as an incompatible declaration for the same
1974 name already seen in the same scope).
1976 Returns either X or an old decl for the same name.
1977 If an old decl is returned, it may have been smashed
1978 to agree with what X says. */
1980 tree
1981 pushdecl (tree x)
1983 tree name = DECL_NAME (x);
1984 struct c_scope *scope = current_scope;
1985 struct c_binding *b;
1986 bool nested = false;
1988 /* Functions need the lang_decl data. */
1989 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
1990 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
1992 /* Must set DECL_CONTEXT for everything not at file scope or
1993 DECL_FILE_SCOPE_P won't work. Local externs don't count
1994 unless they have initializers (which generate code). */
1995 if (current_function_decl
1996 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1997 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1998 DECL_CONTEXT (x) = current_function_decl;
2000 /* If this is of variably modified type, prevent jumping into its
2001 scope. */
2002 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2003 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2004 c_begin_vm_scope (scope->depth);
2006 /* Anonymous decls are just inserted in the scope. */
2007 if (!name)
2009 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2010 return x;
2013 /* First, see if there is another declaration with the same name in
2014 the current scope. If there is, duplicate_decls may do all the
2015 work for us. If duplicate_decls returns false, that indicates
2016 two incompatible decls in the same scope; we are to silently
2017 replace the old one (duplicate_decls has issued all appropriate
2018 diagnostics). In particular, we should not consider possible
2019 duplicates in the external scope, or shadowing. */
2020 b = I_SYMBOL_BINDING (name);
2021 if (b && B_IN_SCOPE (b, scope))
2023 struct c_binding *b_ext, *b_use;
2024 tree type = TREE_TYPE (x);
2025 tree visdecl = b->decl;
2026 tree vistype = TREE_TYPE (visdecl);
2027 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2028 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2029 b->inner_comp = false;
2030 b_use = b;
2031 b_ext = b;
2032 /* If this is an external linkage declaration, we should check
2033 for compatibility with the type in the external scope before
2034 setting the type at this scope based on the visible
2035 information only. */
2036 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2038 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2039 b_ext = b_ext->shadowed;
2040 if (b_ext)
2042 b_use = b_ext;
2043 if (b_use->type)
2044 TREE_TYPE (b_use->decl) = b_use->type;
2047 if (duplicate_decls (x, b_use->decl))
2049 if (b_use != b)
2051 /* Save the updated type in the external scope and
2052 restore the proper type for this scope. */
2053 tree thistype;
2054 if (comptypes (vistype, type))
2055 thistype = composite_type (vistype, type);
2056 else
2057 thistype = TREE_TYPE (b_use->decl);
2058 b_use->type = TREE_TYPE (b_use->decl);
2059 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2060 && DECL_BUILT_IN (b_use->decl))
2061 thistype
2062 = build_type_attribute_variant (thistype,
2063 TYPE_ATTRIBUTES
2064 (b_use->type));
2065 TREE_TYPE (b_use->decl) = thistype;
2067 return b_use->decl;
2069 else
2070 goto skip_external_and_shadow_checks;
2073 /* All declarations with external linkage, and all external
2074 references, go in the external scope, no matter what scope is
2075 current. However, the binding in that scope is ignored for
2076 purposes of normal name lookup. A separate binding structure is
2077 created in the requested scope; this governs the normal
2078 visibility of the symbol.
2080 The binding in the externals scope is used exclusively for
2081 detecting duplicate declarations of the same object, no matter
2082 what scope they are in; this is what we do here. (C99 6.2.7p2:
2083 All declarations that refer to the same object or function shall
2084 have compatible type; otherwise, the behavior is undefined.) */
2085 if (DECL_EXTERNAL (x) || scope == file_scope)
2087 tree type = TREE_TYPE (x);
2088 tree vistype = 0;
2089 tree visdecl = 0;
2090 bool type_saved = false;
2091 if (b && !B_IN_EXTERNAL_SCOPE (b)
2092 && (TREE_CODE (b->decl) == FUNCTION_DECL
2093 || TREE_CODE (b->decl) == VAR_DECL)
2094 && DECL_FILE_SCOPE_P (b->decl))
2096 visdecl = b->decl;
2097 vistype = TREE_TYPE (visdecl);
2099 if (warn_nested_externs
2100 && scope != file_scope
2101 && !DECL_IN_SYSTEM_HEADER (x))
2102 warning (0, "nested extern declaration of %qD", x);
2104 while (b && !B_IN_EXTERNAL_SCOPE (b))
2106 /* If this decl might be modified, save its type. This is
2107 done here rather than when the decl is first bound
2108 because the type may change after first binding, through
2109 being completed or through attributes being added. If we
2110 encounter multiple such decls, only the first should have
2111 its type saved; the others will already have had their
2112 proper types saved and the types will not have changed as
2113 their scopes will not have been re-entered. */
2114 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2116 b->type = TREE_TYPE (b->decl);
2117 type_saved = true;
2119 if (B_IN_FILE_SCOPE (b)
2120 && TREE_CODE (b->decl) == VAR_DECL
2121 && TREE_STATIC (b->decl)
2122 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2123 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2124 && TREE_CODE (type) == ARRAY_TYPE
2125 && TYPE_DOMAIN (type)
2126 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2127 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2129 /* Array type completed in inner scope, which should be
2130 diagnosed if the completion does not have size 1 and
2131 it does not get completed in the file scope. */
2132 b->inner_comp = true;
2134 b = b->shadowed;
2137 /* If a matching external declaration has been found, set its
2138 type to the composite of all the types of that declaration.
2139 After the consistency checks, it will be reset to the
2140 composite of the visible types only. */
2141 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2142 && b->type)
2143 TREE_TYPE (b->decl) = b->type;
2145 /* The point of the same_translation_unit_p check here is,
2146 we want to detect a duplicate decl for a construct like
2147 foo() { extern bar(); } ... static bar(); but not if
2148 they are in different translation units. In any case,
2149 the static does not go in the externals scope. */
2150 if (b
2151 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2152 && duplicate_decls (x, b->decl))
2154 tree thistype;
2155 if (vistype)
2157 if (comptypes (vistype, type))
2158 thistype = composite_type (vistype, type);
2159 else
2160 thistype = TREE_TYPE (b->decl);
2162 else
2163 thistype = type;
2164 b->type = TREE_TYPE (b->decl);
2165 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2166 thistype
2167 = build_type_attribute_variant (thistype,
2168 TYPE_ATTRIBUTES (b->type));
2169 TREE_TYPE (b->decl) = thistype;
2170 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2171 return b->decl;
2173 else if (TREE_PUBLIC (x))
2175 if (visdecl && !b && duplicate_decls (x, visdecl))
2177 /* An external declaration at block scope referring to a
2178 visible entity with internal linkage. The composite
2179 type will already be correct for this scope, so we
2180 just need to fall through to make the declaration in
2181 this scope. */
2182 nested = true;
2183 x = visdecl;
2185 else
2187 bind (name, x, external_scope, /*invisible=*/true,
2188 /*nested=*/false);
2189 nested = true;
2194 warn_if_shadowing (x);
2196 skip_external_and_shadow_checks:
2197 if (TREE_CODE (x) == TYPE_DECL)
2198 clone_underlying_type (x);
2200 bind (name, x, scope, /*invisible=*/false, nested);
2202 /* If x's type is incomplete because it's based on a
2203 structure or union which has not yet been fully declared,
2204 attach it to that structure or union type, so we can go
2205 back and complete the variable declaration later, if the
2206 structure or union gets fully declared.
2208 If the input is erroneous, we can have error_mark in the type
2209 slot (e.g. "f(void a, ...)") - that doesn't count as an
2210 incomplete type. */
2211 if (TREE_TYPE (x) != error_mark_node
2212 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2214 tree element = TREE_TYPE (x);
2216 while (TREE_CODE (element) == ARRAY_TYPE)
2217 element = TREE_TYPE (element);
2218 element = TYPE_MAIN_VARIANT (element);
2220 if ((TREE_CODE (element) == RECORD_TYPE
2221 || TREE_CODE (element) == UNION_TYPE)
2222 && (TREE_CODE (x) != TYPE_DECL
2223 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2224 && !COMPLETE_TYPE_P (element))
2225 C_TYPE_INCOMPLETE_VARS (element)
2226 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2228 return x;
2231 /* Record X as belonging to file scope.
2232 This is used only internally by the Objective-C front end,
2233 and is limited to its needs. duplicate_decls is not called;
2234 if there is any preexisting decl for this identifier, it is an ICE. */
2236 tree
2237 pushdecl_top_level (tree x)
2239 tree name;
2240 bool nested = false;
2241 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2243 name = DECL_NAME (x);
2245 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2247 if (TREE_PUBLIC (x))
2249 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2250 nested = true;
2252 if (file_scope)
2253 bind (name, x, file_scope, /*invisible=*/false, nested);
2255 return x;
2258 static void
2259 implicit_decl_warning (tree id, tree olddecl)
2261 void (*diag) (const char *, ...);
2262 switch (mesg_implicit_function_declaration)
2264 case 0: return;
2265 case 1: diag = warning0; break;
2266 case 2: diag = error; break;
2267 default: gcc_unreachable ();
2270 diag (N_("implicit declaration of function %qE"), id);
2271 if (olddecl)
2272 locate_old_decl (olddecl, diag);
2275 /* Generate an implicit declaration for identifier FUNCTIONID as a
2276 function of type int (). */
2278 tree
2279 implicitly_declare (tree functionid)
2281 struct c_binding *b;
2282 tree decl = 0;
2283 tree asmspec_tree;
2285 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2287 if (B_IN_SCOPE (b, external_scope))
2289 decl = b->decl;
2290 break;
2294 if (decl)
2296 if (decl == error_mark_node)
2297 return decl;
2299 /* FIXME: Objective-C has weird not-really-builtin functions
2300 which are supposed to be visible automatically. They wind up
2301 in the external scope because they're pushed before the file
2302 scope gets created. Catch this here and rebind them into the
2303 file scope. */
2304 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2306 bind (functionid, decl, file_scope,
2307 /*invisible=*/false, /*nested=*/true);
2308 return decl;
2310 else
2312 tree newtype = default_function_type;
2313 if (b->type)
2314 TREE_TYPE (decl) = b->type;
2315 /* Implicit declaration of a function already declared
2316 (somehow) in a different scope, or as a built-in.
2317 If this is the first time this has happened, warn;
2318 then recycle the old declaration but with the new type. */
2319 if (!C_DECL_IMPLICIT (decl))
2321 implicit_decl_warning (functionid, decl);
2322 C_DECL_IMPLICIT (decl) = 1;
2324 if (DECL_BUILT_IN (decl))
2326 newtype = build_type_attribute_variant (newtype,
2327 TYPE_ATTRIBUTES
2328 (TREE_TYPE (decl)));
2329 if (!comptypes (newtype, TREE_TYPE (decl)))
2331 warning (0, "incompatible implicit declaration of built-in"
2332 " function %qD", decl);
2333 newtype = TREE_TYPE (decl);
2336 else
2338 if (!comptypes (newtype, TREE_TYPE (decl)))
2340 error ("incompatible implicit declaration of function %qD",
2341 decl);
2342 locate_old_decl (decl, error);
2345 b->type = TREE_TYPE (decl);
2346 TREE_TYPE (decl) = newtype;
2347 bind (functionid, decl, current_scope,
2348 /*invisible=*/false, /*nested=*/true);
2349 return decl;
2353 /* Not seen before. */
2354 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2355 DECL_EXTERNAL (decl) = 1;
2356 TREE_PUBLIC (decl) = 1;
2357 C_DECL_IMPLICIT (decl) = 1;
2358 implicit_decl_warning (functionid, 0);
2359 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2360 if (asmspec_tree)
2361 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2363 /* C89 says implicit declarations are in the innermost block.
2364 So we record the decl in the standard fashion. */
2365 decl = pushdecl (decl);
2367 /* No need to call objc_check_decl here - it's a function type. */
2368 rest_of_decl_compilation (decl, 0, 0);
2370 /* Write a record describing this implicit function declaration
2371 to the prototypes file (if requested). */
2372 gen_aux_info_record (decl, 0, 1, 0);
2374 /* Possibly apply some default attributes to this implicit declaration. */
2375 decl_attributes (&decl, NULL_TREE, 0);
2377 return decl;
2380 /* Issue an error message for a reference to an undeclared variable
2381 ID, including a reference to a builtin outside of function-call
2382 context. Establish a binding of the identifier to error_mark_node
2383 in an appropriate scope, which will suppress further errors for the
2384 same identifier. The error message should be given location LOC. */
2385 void
2386 undeclared_variable (tree id, location_t loc)
2388 static bool already = false;
2389 struct c_scope *scope;
2391 if (current_function_decl == 0)
2393 error ("%H%qE undeclared here (not in a function)", &loc, id);
2394 scope = current_scope;
2396 else
2398 error ("%H%qE undeclared (first use in this function)", &loc, id);
2400 if (!already)
2402 error ("%H(Each undeclared identifier is reported only once", &loc);
2403 error ("%Hfor each function it appears in.)", &loc);
2404 already = true;
2407 /* If we are parsing old-style parameter decls, current_function_decl
2408 will be nonnull but current_function_scope will be null. */
2409 scope = current_function_scope ? current_function_scope : current_scope;
2411 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2414 /* Subroutine of lookup_label, declare_label, define_label: construct a
2415 LABEL_DECL with all the proper frills. */
2417 static tree
2418 make_label (tree name, location_t location)
2420 tree label = build_decl (LABEL_DECL, name, void_type_node);
2422 DECL_CONTEXT (label) = current_function_decl;
2423 DECL_MODE (label) = VOIDmode;
2424 DECL_SOURCE_LOCATION (label) = location;
2426 return label;
2429 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2430 Create one if none exists so far for the current function.
2431 This is called when a label is used in a goto expression or
2432 has its address taken. */
2434 tree
2435 lookup_label (tree name)
2437 tree label;
2439 if (current_function_decl == 0)
2441 error ("label %qE referenced outside of any function", name);
2442 return 0;
2445 /* Use a label already defined or ref'd with this name, but not if
2446 it is inherited from a containing function and wasn't declared
2447 using __label__. */
2448 label = I_LABEL_DECL (name);
2449 if (label && (DECL_CONTEXT (label) == current_function_decl
2450 || C_DECLARED_LABEL_FLAG (label)))
2452 /* If the label has only been declared, update its apparent
2453 location to point here, for better diagnostics if it
2454 turns out not to have been defined. */
2455 if (!TREE_USED (label))
2456 DECL_SOURCE_LOCATION (label) = input_location;
2457 return label;
2460 /* No label binding for that identifier; make one. */
2461 label = make_label (name, input_location);
2463 /* Ordinary labels go in the current function scope. */
2464 bind (name, label, current_function_scope,
2465 /*invisible=*/false, /*nested=*/false);
2466 return label;
2469 /* Make a label named NAME in the current function, shadowing silently
2470 any that may be inherited from containing functions or containing
2471 scopes. This is called for __label__ declarations. */
2473 tree
2474 declare_label (tree name)
2476 struct c_binding *b = I_LABEL_BINDING (name);
2477 tree label;
2479 /* Check to make sure that the label hasn't already been declared
2480 at this scope */
2481 if (b && B_IN_CURRENT_SCOPE (b))
2483 error ("duplicate label declaration %qE", name);
2484 locate_old_decl (b->decl, error);
2486 /* Just use the previous declaration. */
2487 return b->decl;
2490 label = make_label (name, input_location);
2491 C_DECLARED_LABEL_FLAG (label) = 1;
2493 /* Declared labels go in the current scope. */
2494 bind (name, label, current_scope,
2495 /*invisible=*/false, /*nested=*/false);
2496 return label;
2499 /* Define a label, specifying the location in the source file.
2500 Return the LABEL_DECL node for the label, if the definition is valid.
2501 Otherwise return 0. */
2503 tree
2504 define_label (location_t location, tree name)
2506 /* Find any preexisting label with this name. It is an error
2507 if that label has already been defined in this function, or
2508 if there is a containing function with a declared label with
2509 the same name. */
2510 tree label = I_LABEL_DECL (name);
2511 struct c_label_list *nlist_se, *nlist_vm;
2513 if (label
2514 && ((DECL_CONTEXT (label) == current_function_decl
2515 && DECL_INITIAL (label) != 0)
2516 || (DECL_CONTEXT (label) != current_function_decl
2517 && C_DECLARED_LABEL_FLAG (label))))
2519 error ("%Hduplicate label %qD", &location, label);
2520 locate_old_decl (label, error);
2521 return 0;
2523 else if (label && DECL_CONTEXT (label) == current_function_decl)
2525 /* The label has been used or declared already in this function,
2526 but not defined. Update its location to point to this
2527 definition. */
2528 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2529 error ("%Jjump into statement expression", label);
2530 if (C_DECL_UNDEFINABLE_VM (label))
2531 error ("%Jjump into scope of identifier with variably modified type",
2532 label);
2533 DECL_SOURCE_LOCATION (label) = location;
2535 else
2537 /* No label binding for that identifier; make one. */
2538 label = make_label (name, location);
2540 /* Ordinary labels go in the current function scope. */
2541 bind (name, label, current_function_scope,
2542 /*invisible=*/false, /*nested=*/false);
2545 if (!in_system_header && lookup_name (name))
2546 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2547 "for labels, identifier %qE conflicts", &location, name);
2549 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2550 nlist_se->next = label_context_stack_se->labels_def;
2551 nlist_se->label = label;
2552 label_context_stack_se->labels_def = nlist_se;
2554 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2555 nlist_vm->next = label_context_stack_vm->labels_def;
2556 nlist_vm->label = label;
2557 label_context_stack_vm->labels_def = nlist_vm;
2559 /* Mark label as having been defined. */
2560 DECL_INITIAL (label) = error_mark_node;
2561 return label;
2564 /* Given NAME, an IDENTIFIER_NODE,
2565 return the structure (or union or enum) definition for that name.
2566 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2567 CODE says which kind of type the caller wants;
2568 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2569 If the wrong kind of type is found, an error is reported. */
2571 static tree
2572 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2574 struct c_binding *b = I_TAG_BINDING (name);
2575 int thislevel = 0;
2577 if (!b || !b->decl)
2578 return 0;
2580 /* We only care about whether it's in this level if
2581 thislevel_only was set or it might be a type clash. */
2582 if (thislevel_only || TREE_CODE (b->decl) != code)
2584 /* For our purposes, a tag in the external scope is the same as
2585 a tag in the file scope. (Primarily relevant to Objective-C
2586 and its builtin structure tags, which get pushed before the
2587 file scope is created.) */
2588 if (B_IN_CURRENT_SCOPE (b)
2589 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2590 thislevel = 1;
2593 if (thislevel_only && !thislevel)
2594 return 0;
2596 if (TREE_CODE (b->decl) != code)
2598 /* Definition isn't the kind we were looking for. */
2599 pending_invalid_xref = name;
2600 pending_invalid_xref_location = input_location;
2602 /* If in the same binding level as a declaration as a tag
2603 of a different type, this must not be allowed to
2604 shadow that tag, so give the error immediately.
2605 (For example, "struct foo; union foo;" is invalid.) */
2606 if (thislevel)
2607 pending_xref_error ();
2609 return b->decl;
2612 /* Print an error message now
2613 for a recent invalid struct, union or enum cross reference.
2614 We don't print them immediately because they are not invalid
2615 when used in the `struct foo;' construct for shadowing. */
2617 void
2618 pending_xref_error (void)
2620 if (pending_invalid_xref != 0)
2621 error ("%H%qE defined as wrong kind of tag",
2622 &pending_invalid_xref_location, pending_invalid_xref);
2623 pending_invalid_xref = 0;
2627 /* Look up NAME in the current scope and its superiors
2628 in the namespace of variables, functions and typedefs.
2629 Return a ..._DECL node of some kind representing its definition,
2630 or return 0 if it is undefined. */
2632 tree
2633 lookup_name (tree name)
2635 struct c_binding *b = I_SYMBOL_BINDING (name);
2636 if (b && !b->invisible)
2637 return b->decl;
2638 return 0;
2641 /* Similar to `lookup_name' but look only at the indicated scope. */
2643 static tree
2644 lookup_name_in_scope (tree name, struct c_scope *scope)
2646 struct c_binding *b;
2648 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2649 if (B_IN_SCOPE (b, scope))
2650 return b->decl;
2651 return 0;
2654 /* Create the predefined scalar types of C,
2655 and some nodes representing standard constants (0, 1, (void *) 0).
2656 Initialize the global scope.
2657 Make definitions for built-in primitive functions. */
2659 void
2660 c_init_decl_processing (void)
2662 location_t save_loc = input_location;
2664 /* Initialize reserved words for parser. */
2665 c_parse_init ();
2667 current_function_decl = 0;
2669 gcc_obstack_init (&parser_obstack);
2671 /* Make the externals scope. */
2672 push_scope ();
2673 external_scope = current_scope;
2675 /* Declarations from c_common_nodes_and_builtins must not be associated
2676 with this input file, lest we get differences between using and not
2677 using preprocessed headers. */
2678 #ifdef USE_MAPPED_LOCATION
2679 input_location = BUILTINS_LOCATION;
2680 #else
2681 input_location.file = "<built-in>";
2682 input_location.line = 0;
2683 #endif
2685 build_common_tree_nodes (flag_signed_char, false);
2687 c_common_nodes_and_builtins ();
2689 /* In C, comparisons and TRUTH_* expressions have type int. */
2690 truthvalue_type_node = integer_type_node;
2691 truthvalue_true_node = integer_one_node;
2692 truthvalue_false_node = integer_zero_node;
2694 /* Even in C99, which has a real boolean type. */
2695 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2696 boolean_type_node));
2698 input_location = save_loc;
2700 pedantic_lvalues = true;
2702 make_fname_decl = c_make_fname_decl;
2703 start_fname_decls ();
2706 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2707 decl, NAME is the initialization string and TYPE_DEP indicates whether
2708 NAME depended on the type of the function. As we don't yet implement
2709 delayed emission of static data, we mark the decl as emitted
2710 so it is not placed in the output. Anything using it must therefore pull
2711 out the STRING_CST initializer directly. FIXME. */
2713 static tree
2714 c_make_fname_decl (tree id, int type_dep)
2716 const char *name = fname_as_string (type_dep);
2717 tree decl, type, init;
2718 size_t length = strlen (name);
2720 type = build_array_type (char_type_node,
2721 build_index_type (size_int (length)));
2722 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2724 decl = build_decl (VAR_DECL, id, type);
2726 TREE_STATIC (decl) = 1;
2727 TREE_READONLY (decl) = 1;
2728 DECL_ARTIFICIAL (decl) = 1;
2730 init = build_string (length + 1, name);
2731 free ((char *) name);
2732 TREE_TYPE (init) = type;
2733 DECL_INITIAL (decl) = init;
2735 TREE_USED (decl) = 1;
2737 if (current_function_decl)
2739 DECL_CONTEXT (decl) = current_function_decl;
2740 bind (id, decl, current_function_scope,
2741 /*invisible=*/false, /*nested=*/false);
2744 finish_decl (decl, init, NULL_TREE);
2746 return decl;
2749 /* Return a definition for a builtin function named NAME and whose data type
2750 is TYPE. TYPE should be a function type with argument types.
2751 FUNCTION_CODE tells later passes how to compile calls to this function.
2752 See tree.h for its possible values.
2754 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2755 the name to be called if we can't opencode the function. If
2756 ATTRS is nonzero, use that for the function's attribute list. */
2758 tree
2759 builtin_function (const char *name, tree type, int function_code,
2760 enum built_in_class cl, const char *library_name,
2761 tree attrs)
2763 tree id = get_identifier (name);
2764 tree decl = build_decl (FUNCTION_DECL, id, type);
2765 TREE_PUBLIC (decl) = 1;
2766 DECL_EXTERNAL (decl) = 1;
2767 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2768 DECL_BUILT_IN_CLASS (decl) = cl;
2769 DECL_FUNCTION_CODE (decl) = function_code;
2770 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2771 if (library_name)
2772 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2774 /* Should never be called on a symbol with a preexisting meaning. */
2775 gcc_assert (!I_SYMBOL_BINDING (id));
2777 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2779 /* Builtins in the implementation namespace are made visible without
2780 needing to be explicitly declared. See push_file_scope. */
2781 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2783 TREE_CHAIN (decl) = visible_builtins;
2784 visible_builtins = decl;
2787 /* Possibly apply some default attributes to this built-in function. */
2788 if (attrs)
2789 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2790 else
2791 decl_attributes (&decl, NULL_TREE, 0);
2793 return decl;
2796 /* Called when a declaration is seen that contains no names to declare.
2797 If its type is a reference to a structure, union or enum inherited
2798 from a containing scope, shadow that tag name for the current scope
2799 with a forward reference.
2800 If its type defines a new named structure or union
2801 or defines an enum, it is valid but we need not do anything here.
2802 Otherwise, it is an error. */
2804 void
2805 shadow_tag (const struct c_declspecs *declspecs)
2807 shadow_tag_warned (declspecs, 0);
2810 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2811 but no pedwarn. */
2812 void
2813 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2815 bool found_tag = false;
2817 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2819 tree value = declspecs->type;
2820 enum tree_code code = TREE_CODE (value);
2822 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2823 /* Used to test also that TYPE_SIZE (value) != 0.
2824 That caused warning for `struct foo;' at top level in the file. */
2826 tree name = TYPE_NAME (value);
2827 tree t;
2829 found_tag = true;
2831 if (name == 0)
2833 if (warned != 1 && code != ENUMERAL_TYPE)
2834 /* Empty unnamed enum OK */
2836 pedwarn ("unnamed struct/union that defines no instances");
2837 warned = 1;
2840 else if (!declspecs->tag_defined_p
2841 && declspecs->storage_class != csc_none)
2843 if (warned != 1)
2844 pedwarn ("empty declaration with storage class specifier "
2845 "does not redeclare tag");
2846 warned = 1;
2847 pending_xref_error ();
2849 else if (!declspecs->tag_defined_p
2850 && (declspecs->const_p
2851 || declspecs->volatile_p
2852 || declspecs->restrict_p))
2854 if (warned != 1)
2855 pedwarn ("empty declaration with type qualifier "
2856 "does not redeclare tag");
2857 warned = 1;
2858 pending_xref_error ();
2860 else
2862 pending_invalid_xref = 0;
2863 t = lookup_tag (code, name, 1);
2865 if (t == 0)
2867 t = make_node (code);
2868 pushtag (name, t);
2872 else
2874 if (warned != 1 && !in_system_header)
2876 pedwarn ("useless type name in empty declaration");
2877 warned = 1;
2881 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2883 pedwarn ("useless type name in empty declaration");
2884 warned = 1;
2887 pending_invalid_xref = 0;
2889 if (declspecs->inline_p)
2891 error ("%<inline%> in empty declaration");
2892 warned = 1;
2895 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2897 error ("%<auto%> in file-scope empty declaration");
2898 warned = 1;
2901 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2903 error ("%<register%> in file-scope empty declaration");
2904 warned = 1;
2907 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2909 warning (0, "useless storage class specifier in empty declaration");
2910 warned = 2;
2913 if (!warned && !in_system_header && declspecs->thread_p)
2915 warning (0, "useless %<__thread%> in empty declaration");
2916 warned = 2;
2919 if (!warned && !in_system_header && (declspecs->const_p
2920 || declspecs->volatile_p
2921 || declspecs->restrict_p))
2923 warning (0, "useless type qualifier in empty declaration");
2924 warned = 2;
2927 if (warned != 1)
2929 if (!found_tag)
2930 pedwarn ("empty declaration");
2935 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2936 bits. SPECS represents declaration specifiers that the grammar
2937 only permits to contain type qualifiers and attributes. */
2940 quals_from_declspecs (const struct c_declspecs *specs)
2942 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2943 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2944 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2945 gcc_assert (!specs->type
2946 && !specs->decl_attr
2947 && specs->typespec_word == cts_none
2948 && specs->storage_class == csc_none
2949 && !specs->typedef_p
2950 && !specs->explicit_signed_p
2951 && !specs->deprecated_p
2952 && !specs->long_p
2953 && !specs->long_long_p
2954 && !specs->short_p
2955 && !specs->signed_p
2956 && !specs->unsigned_p
2957 && !specs->complex_p
2958 && !specs->inline_p
2959 && !specs->thread_p);
2960 return quals;
2963 /* Construct an array declarator. EXPR is the expression inside [], or
2964 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2965 to the pointer to which a parameter array is converted). STATIC_P is
2966 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2967 is true if the array is [*], a VLA of unspecified length which is
2968 nevertheless a complete type (not currently implemented by GCC),
2969 false otherwise. The field for the contained declarator is left to be
2970 filled in by set_array_declarator_inner. */
2972 struct c_declarator *
2973 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
2974 bool vla_unspec_p)
2976 struct c_declarator *declarator = XOBNEW (&parser_obstack,
2977 struct c_declarator);
2978 declarator->kind = cdk_array;
2979 declarator->declarator = 0;
2980 declarator->u.array.dimen = expr;
2981 if (quals)
2983 declarator->u.array.attrs = quals->attrs;
2984 declarator->u.array.quals = quals_from_declspecs (quals);
2986 else
2988 declarator->u.array.attrs = NULL_TREE;
2989 declarator->u.array.quals = 0;
2991 declarator->u.array.static_p = static_p;
2992 declarator->u.array.vla_unspec_p = vla_unspec_p;
2993 if (pedantic && !flag_isoc99)
2995 if (static_p || quals != NULL)
2996 pedwarn ("ISO C90 does not support %<static%> or type "
2997 "qualifiers in parameter array declarators");
2998 if (vla_unspec_p)
2999 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3001 if (vla_unspec_p)
3002 warning (0, "GCC does not yet properly implement %<[*]%> array declarators");
3003 return declarator;
3006 /* Set the contained declarator of an array declarator. DECL is the
3007 declarator, as constructed by build_array_declarator; INNER is what
3008 appears on the left of the []. ABSTRACT_P is true if it is an
3009 abstract declarator, false otherwise; this is used to reject static
3010 and type qualifiers in abstract declarators, where they are not in
3011 the C99 grammar (subject to possible change in DR#289). */
3013 struct c_declarator *
3014 set_array_declarator_inner (struct c_declarator *decl,
3015 struct c_declarator *inner, bool abstract_p)
3017 decl->declarator = inner;
3018 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3019 || decl->u.array.attrs != NULL_TREE
3020 || decl->u.array.static_p))
3021 error ("static or type qualifiers in abstract declarator");
3022 return decl;
3025 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3027 tree
3028 groktypename (struct c_type_name *type_name)
3030 tree type;
3031 tree attrs = type_name->specs->attrs;
3033 type_name->specs->attrs = NULL_TREE;
3035 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3036 false, NULL);
3038 /* Apply attributes. */
3039 decl_attributes (&type, attrs, 0);
3041 return type;
3044 /* Decode a declarator in an ordinary declaration or data definition.
3045 This is called as soon as the type information and variable name
3046 have been parsed, before parsing the initializer if any.
3047 Here we create the ..._DECL node, fill in its type,
3048 and put it on the list of decls for the current context.
3049 The ..._DECL node is returned as the value.
3051 Exception: for arrays where the length is not specified,
3052 the type is left null, to be filled in by `finish_decl'.
3054 Function definitions do not come here; they go to start_function
3055 instead. However, external and forward declarations of functions
3056 do go through here. Structure field declarations are done by
3057 grokfield and not through here. */
3059 tree
3060 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3061 bool initialized, tree attributes)
3063 tree decl;
3064 tree tem;
3066 /* An object declared as __attribute__((deprecated)) suppresses
3067 warnings of uses of other deprecated items. */
3068 if (lookup_attribute ("deprecated", attributes))
3069 deprecated_state = DEPRECATED_SUPPRESS;
3071 decl = grokdeclarator (declarator, declspecs,
3072 NORMAL, initialized, NULL);
3073 if (!decl)
3074 return 0;
3076 deprecated_state = DEPRECATED_NORMAL;
3078 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3079 && MAIN_NAME_P (DECL_NAME (decl)))
3080 warning (0, "%J%qD is usually a function", decl, decl);
3082 if (initialized)
3083 /* Is it valid for this decl to have an initializer at all?
3084 If not, set INITIALIZED to zero, which will indirectly
3085 tell 'finish_decl' to ignore the initializer once it is parsed. */
3086 switch (TREE_CODE (decl))
3088 case TYPE_DECL:
3089 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3090 initialized = 0;
3091 break;
3093 case FUNCTION_DECL:
3094 error ("function %qD is initialized like a variable", decl);
3095 initialized = 0;
3096 break;
3098 case PARM_DECL:
3099 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3100 error ("parameter %qD is initialized", decl);
3101 initialized = 0;
3102 break;
3104 default:
3105 /* Don't allow initializations for incomplete types except for
3106 arrays which might be completed by the initialization. */
3108 /* This can happen if the array size is an undefined macro.
3109 We already gave a warning, so we don't need another one. */
3110 if (TREE_TYPE (decl) == error_mark_node)
3111 initialized = 0;
3112 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3114 /* A complete type is ok if size is fixed. */
3116 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3117 || C_DECL_VARIABLE_SIZE (decl))
3119 error ("variable-sized object may not be initialized");
3120 initialized = 0;
3123 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3125 error ("variable %qD has initializer but incomplete type", decl);
3126 initialized = 0;
3128 else if (C_DECL_VARIABLE_SIZE (decl))
3130 /* Although C99 is unclear about whether incomplete arrays
3131 of VLAs themselves count as VLAs, it does not make
3132 sense to permit them to be initialized given that
3133 ordinary VLAs may not be initialized. */
3134 error ("variable-sized object may not be initialized");
3135 initialized = 0;
3139 if (initialized)
3141 if (current_scope == file_scope)
3142 TREE_STATIC (decl) = 1;
3144 /* Tell 'pushdecl' this is an initialized decl
3145 even though we don't yet have the initializer expression.
3146 Also tell 'finish_decl' it may store the real initializer. */
3147 DECL_INITIAL (decl) = error_mark_node;
3150 /* If this is a function declaration, write a record describing it to the
3151 prototypes file (if requested). */
3153 if (TREE_CODE (decl) == FUNCTION_DECL)
3154 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3156 /* ANSI specifies that a tentative definition which is not merged with
3157 a non-tentative definition behaves exactly like a definition with an
3158 initializer equal to zero. (Section 3.7.2)
3160 -fno-common gives strict ANSI behavior, though this tends to break
3161 a large body of code that grew up without this rule.
3163 Thread-local variables are never common, since there's no entrenched
3164 body of code to break, and it allows more efficient variable references
3165 in the presence of dynamic linking. */
3167 if (TREE_CODE (decl) == VAR_DECL
3168 && !initialized
3169 && TREE_PUBLIC (decl)
3170 && !DECL_THREAD_LOCAL (decl)
3171 && !flag_no_common)
3172 DECL_COMMON (decl) = 1;
3174 /* Set attributes here so if duplicate decl, will have proper attributes. */
3175 decl_attributes (&decl, attributes, 0);
3177 if (TREE_CODE (decl) == FUNCTION_DECL
3178 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3180 struct c_declarator *ce = declarator;
3182 if (ce->kind == cdk_pointer)
3183 ce = declarator->declarator;
3184 if (ce->kind == cdk_function)
3186 tree args = ce->u.arg_info->parms;
3187 for (; args; args = TREE_CHAIN (args))
3189 tree type = TREE_TYPE (args);
3190 if (type && INTEGRAL_TYPE_P (type)
3191 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3192 DECL_ARG_TYPE (args) = integer_type_node;
3197 if (TREE_CODE (decl) == FUNCTION_DECL
3198 && DECL_DECLARED_INLINE_P (decl)
3199 && DECL_UNINLINABLE (decl)
3200 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3201 warning (OPT_Wattributes, "%Jinline function %qD given attribute noinline",
3202 decl, decl);
3204 /* Add this decl to the current scope.
3205 TEM may equal DECL or it may be a previous decl of the same name. */
3206 tem = pushdecl (decl);
3208 if (initialized && DECL_EXTERNAL (tem))
3210 DECL_EXTERNAL (tem) = 0;
3211 TREE_STATIC (tem) = 1;
3214 return tem;
3217 /* Finish processing of a declaration;
3218 install its initial value.
3219 If the length of an array type is not known before,
3220 it must be determined now, from the initial value, or it is an error. */
3222 void
3223 finish_decl (tree decl, tree init, tree asmspec_tree)
3225 tree type = TREE_TYPE (decl);
3226 int was_incomplete = (DECL_SIZE (decl) == 0);
3227 const char *asmspec = 0;
3229 /* If a name was specified, get the string. */
3230 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3231 && DECL_FILE_SCOPE_P (decl))
3232 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3233 if (asmspec_tree)
3234 asmspec = TREE_STRING_POINTER (asmspec_tree);
3236 /* If `start_decl' didn't like having an initialization, ignore it now. */
3237 if (init != 0 && DECL_INITIAL (decl) == 0)
3238 init = 0;
3240 /* Don't crash if parm is initialized. */
3241 if (TREE_CODE (decl) == PARM_DECL)
3242 init = 0;
3244 if (init)
3245 store_init_value (decl, init);
3247 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3248 || TREE_CODE (decl) == FUNCTION_DECL
3249 || TREE_CODE (decl) == FIELD_DECL))
3250 objc_check_decl (decl);
3252 /* Deduce size of array from initialization, if not already known. */
3253 if (TREE_CODE (type) == ARRAY_TYPE
3254 && TYPE_DOMAIN (type) == 0
3255 && TREE_CODE (decl) != TYPE_DECL)
3257 bool do_default
3258 = (TREE_STATIC (decl)
3259 /* Even if pedantic, an external linkage array
3260 may have incomplete type at first. */
3261 ? pedantic && !TREE_PUBLIC (decl)
3262 : !DECL_EXTERNAL (decl));
3263 int failure
3264 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3265 do_default);
3267 /* Get the completed type made by complete_array_type. */
3268 type = TREE_TYPE (decl);
3270 switch (failure)
3272 case 1:
3273 error ("%Jinitializer fails to determine size of %qD", decl, decl);
3274 break;
3276 case 2:
3277 if (do_default)
3278 error ("%Jarray size missing in %qD", decl, decl);
3279 /* If a `static' var's size isn't known,
3280 make it extern as well as static, so it does not get
3281 allocated.
3282 If it is not `static', then do not mark extern;
3283 finish_incomplete_decl will give it a default size
3284 and it will get allocated. */
3285 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3286 DECL_EXTERNAL (decl) = 1;
3287 break;
3289 case 3:
3290 error ("%Jzero or negative size array %qD", decl, decl);
3291 break;
3293 case 0:
3294 /* For global variables, update the copy of the type that
3295 exists in the binding. */
3296 if (TREE_PUBLIC (decl))
3298 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3299 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3300 b_ext = b_ext->shadowed;
3301 if (b_ext)
3303 if (b_ext->type)
3304 b_ext->type = composite_type (b_ext->type, type);
3305 else
3306 b_ext->type = type;
3309 break;
3311 default:
3312 gcc_unreachable ();
3315 if (DECL_INITIAL (decl))
3316 TREE_TYPE (DECL_INITIAL (decl)) = type;
3318 layout_decl (decl, 0);
3321 if (TREE_CODE (decl) == VAR_DECL)
3323 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3324 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3325 layout_decl (decl, 0);
3327 if (DECL_SIZE (decl) == 0
3328 /* Don't give an error if we already gave one earlier. */
3329 && TREE_TYPE (decl) != error_mark_node
3330 && (TREE_STATIC (decl)
3331 /* A static variable with an incomplete type
3332 is an error if it is initialized.
3333 Also if it is not file scope.
3334 Otherwise, let it through, but if it is not `extern'
3335 then it may cause an error message later. */
3336 ? (DECL_INITIAL (decl) != 0
3337 || !DECL_FILE_SCOPE_P (decl))
3338 /* An automatic variable with an incomplete type
3339 is an error. */
3340 : !DECL_EXTERNAL (decl)))
3342 error ("%Jstorage size of %qD isn%'t known", decl, decl);
3343 TREE_TYPE (decl) = error_mark_node;
3346 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3347 && DECL_SIZE (decl) != 0)
3349 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3350 constant_expression_warning (DECL_SIZE (decl));
3351 else
3352 error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3355 if (TREE_USED (type))
3356 TREE_USED (decl) = 1;
3359 /* If this is a function and an assembler name is specified, reset DECL_RTL
3360 so we can give it its new name. Also, update built_in_decls if it
3361 was a normal built-in. */
3362 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3364 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3365 set_builtin_user_assembler_name (decl, asmspec);
3366 set_user_assembler_name (decl, asmspec);
3369 /* If #pragma weak was used, mark the decl weak now. */
3370 maybe_apply_pragma_weak (decl);
3372 /* If this is a variable definition, determine its ELF visibility. */
3373 if (TREE_CODE (decl) == VAR_DECL
3374 && TREE_STATIC (decl)
3375 && !DECL_EXTERNAL (decl))
3376 c_determine_visibility (decl);
3378 /* Output the assembler code and/or RTL code for variables and functions,
3379 unless the type is an undefined structure or union.
3380 If not, it will get done when the type is completed. */
3382 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3384 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3385 if (c_dialect_objc ())
3386 objc_check_decl (decl);
3388 if (asmspec)
3390 /* If this is not a static variable, issue a warning.
3391 It doesn't make any sense to give an ASMSPEC for an
3392 ordinary, non-register local variable. Historically,
3393 GCC has accepted -- but ignored -- the ASMSPEC in
3394 this case. */
3395 if (!DECL_FILE_SCOPE_P (decl)
3396 && TREE_CODE (decl) == VAR_DECL
3397 && !C_DECL_REGISTER (decl)
3398 && !TREE_STATIC (decl))
3399 warning (0, "%Jignoring asm-specifier for non-static local "
3400 "variable %qD", decl, decl);
3401 else if (C_DECL_REGISTER (decl))
3402 change_decl_assembler_name (decl, get_identifier (asmspec));
3403 else
3404 set_user_assembler_name (decl, asmspec);
3407 if (DECL_FILE_SCOPE_P (decl))
3409 if (DECL_INITIAL (decl) == NULL_TREE
3410 || DECL_INITIAL (decl) == error_mark_node)
3411 /* Don't output anything
3412 when a tentative file-scope definition is seen.
3413 But at end of compilation, do output code for them. */
3414 DECL_DEFER_OUTPUT (decl) = 1;
3415 rest_of_decl_compilation (decl, true, 0);
3417 else
3419 /* In conjunction with an ASMSPEC, the `register'
3420 keyword indicates that we should place the variable
3421 in a particular register. */
3422 if (asmspec && C_DECL_REGISTER (decl))
3424 DECL_HARD_REGISTER (decl) = 1;
3425 /* This cannot be done for a structure with volatile
3426 fields, on which DECL_REGISTER will have been
3427 reset. */
3428 if (!DECL_REGISTER (decl))
3429 error ("cannot put object with volatile field into register");
3432 if (TREE_CODE (decl) != FUNCTION_DECL)
3434 /* If we're building a variable sized type, and we might be
3435 reachable other than via the top of the current binding
3436 level, then create a new BIND_EXPR so that we deallocate
3437 the object at the right time. */
3438 /* Note that DECL_SIZE can be null due to errors. */
3439 if (DECL_SIZE (decl)
3440 && !TREE_CONSTANT (DECL_SIZE (decl))
3441 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3443 tree bind;
3444 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3445 TREE_SIDE_EFFECTS (bind) = 1;
3446 add_stmt (bind);
3447 BIND_EXPR_BODY (bind) = push_stmt_list ();
3449 add_stmt (build_stmt (DECL_EXPR, decl));
3454 if (!DECL_FILE_SCOPE_P (decl))
3456 /* Recompute the RTL of a local array now
3457 if it used to be an incomplete type. */
3458 if (was_incomplete
3459 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3461 /* If we used it already as memory, it must stay in memory. */
3462 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3463 /* If it's still incomplete now, no init will save it. */
3464 if (DECL_SIZE (decl) == 0)
3465 DECL_INITIAL (decl) = 0;
3470 /* If this was marked 'used', be sure it will be output. */
3471 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3472 mark_decl_referenced (decl);
3474 if (TREE_CODE (decl) == TYPE_DECL)
3476 if (!DECL_FILE_SCOPE_P (decl)
3477 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3478 add_stmt (build_stmt (DECL_EXPR, decl));
3480 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3483 /* At the end of a declaration, throw away any variable type sizes
3484 of types defined inside that declaration. There is no use
3485 computing them in the following function definition. */
3486 if (current_scope == file_scope)
3487 get_pending_sizes ();
3489 /* Install a cleanup (aka destructor) if one was given. */
3490 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3492 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3493 if (attr)
3495 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3496 tree cleanup_decl = lookup_name (cleanup_id);
3497 tree cleanup;
3499 /* Build "cleanup(&decl)" for the destructor. */
3500 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3501 cleanup = build_tree_list (NULL_TREE, cleanup);
3502 cleanup = build_function_call (cleanup_decl, cleanup);
3504 /* Don't warn about decl unused; the cleanup uses it. */
3505 TREE_USED (decl) = 1;
3506 TREE_USED (cleanup_decl) = 1;
3508 /* Initialize EH, if we've been told to do so. */
3509 if (flag_exceptions && !c_eh_initialized_p)
3511 c_eh_initialized_p = true;
3512 eh_personality_libfunc
3513 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3514 ? "__gcc_personality_sj0"
3515 : "__gcc_personality_v0");
3516 using_eh_for_cleanups ();
3519 push_cleanup (decl, cleanup, false);
3524 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3526 tree
3527 grokparm (const struct c_parm *parm)
3529 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3530 NULL);
3532 decl_attributes (&decl, parm->attrs, 0);
3534 return decl;
3537 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3538 and push that on the current scope. */
3540 void
3541 push_parm_decl (const struct c_parm *parm)
3543 tree decl;
3545 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3546 decl_attributes (&decl, parm->attrs, 0);
3548 decl = pushdecl (decl);
3550 finish_decl (decl, NULL_TREE, NULL_TREE);
3553 /* Mark all the parameter declarations to date as forward decls.
3554 Also diagnose use of this extension. */
3556 void
3557 mark_forward_parm_decls (void)
3559 struct c_binding *b;
3561 if (pedantic && !current_scope->warned_forward_parm_decls)
3563 pedwarn ("ISO C forbids forward parameter declarations");
3564 current_scope->warned_forward_parm_decls = true;
3567 for (b = current_scope->bindings; b; b = b->prev)
3568 if (TREE_CODE (b->decl) == PARM_DECL)
3569 TREE_ASM_WRITTEN (b->decl) = 1;
3572 static GTY(()) int compound_literal_number;
3574 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3575 literal, which may be an incomplete array type completed by the
3576 initializer; INIT is a CONSTRUCTOR that initializes the compound
3577 literal. */
3579 tree
3580 build_compound_literal (tree type, tree init)
3582 /* We do not use start_decl here because we have a type, not a declarator;
3583 and do not use finish_decl because the decl should be stored inside
3584 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3585 tree decl;
3586 tree complit;
3587 tree stmt;
3589 if (type == error_mark_node)
3590 return error_mark_node;
3592 decl = build_decl (VAR_DECL, NULL_TREE, type);
3593 DECL_EXTERNAL (decl) = 0;
3594 TREE_PUBLIC (decl) = 0;
3595 TREE_STATIC (decl) = (current_scope == file_scope);
3596 DECL_CONTEXT (decl) = current_function_decl;
3597 TREE_USED (decl) = 1;
3598 TREE_TYPE (decl) = type;
3599 TREE_READONLY (decl) = TYPE_READONLY (type);
3600 store_init_value (decl, init);
3602 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3604 int failure = complete_array_type (&TREE_TYPE (decl),
3605 DECL_INITIAL (decl), true);
3606 gcc_assert (!failure);
3608 type = TREE_TYPE (decl);
3609 TREE_TYPE (DECL_INITIAL (decl)) = type;
3612 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3613 return error_mark_node;
3615 stmt = build_stmt (DECL_EXPR, decl);
3616 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3617 TREE_SIDE_EFFECTS (complit) = 1;
3619 layout_decl (decl, 0);
3621 if (TREE_STATIC (decl))
3623 /* This decl needs a name for the assembler output. We also need
3624 a unique suffix to be added to the name. */
3625 char *name;
3627 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3628 compound_literal_number);
3629 compound_literal_number++;
3630 DECL_NAME (decl) = get_identifier (name);
3631 DECL_DEFER_OUTPUT (decl) = 1;
3632 DECL_COMDAT (decl) = 1;
3633 DECL_ARTIFICIAL (decl) = 1;
3634 DECL_IGNORED_P (decl) = 1;
3635 pushdecl (decl);
3636 rest_of_decl_compilation (decl, 1, 0);
3639 return complit;
3642 /* Determine whether TYPE is a structure with a flexible array member,
3643 or a union containing such a structure (possibly recursively). */
3645 static bool
3646 flexible_array_type_p (tree type)
3648 tree x;
3649 switch (TREE_CODE (type))
3651 case RECORD_TYPE:
3652 x = TYPE_FIELDS (type);
3653 if (x == NULL_TREE)
3654 return false;
3655 while (TREE_CHAIN (x) != NULL_TREE)
3656 x = TREE_CHAIN (x);
3657 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3658 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3659 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3660 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3661 return true;
3662 return false;
3663 case UNION_TYPE:
3664 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3666 if (flexible_array_type_p (TREE_TYPE (x)))
3667 return true;
3669 return false;
3670 default:
3671 return false;
3675 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3676 replacing with appropriate values if they are invalid. */
3677 static void
3678 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3680 tree type_mv;
3681 unsigned int max_width;
3682 unsigned HOST_WIDE_INT w;
3683 const char *name = orig_name ? orig_name: _("<anonymous>");
3685 /* Detect and ignore out of range field width and process valid
3686 field widths. */
3687 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3688 || TREE_CODE (*width) != INTEGER_CST)
3690 error ("bit-field %qs width not an integer constant", name);
3691 *width = integer_one_node;
3693 else
3695 constant_expression_warning (*width);
3696 if (tree_int_cst_sgn (*width) < 0)
3698 error ("negative width in bit-field %qs", name);
3699 *width = integer_one_node;
3701 else if (integer_zerop (*width) && orig_name)
3703 error ("zero width for bit-field %qs", name);
3704 *width = integer_one_node;
3708 /* Detect invalid bit-field type. */
3709 if (TREE_CODE (*type) != INTEGER_TYPE
3710 && TREE_CODE (*type) != BOOLEAN_TYPE
3711 && TREE_CODE (*type) != ENUMERAL_TYPE)
3713 error ("bit-field %qs has invalid type", name);
3714 *type = unsigned_type_node;
3717 type_mv = TYPE_MAIN_VARIANT (*type);
3718 if (pedantic
3719 && type_mv != integer_type_node
3720 && type_mv != unsigned_type_node
3721 && type_mv != boolean_type_node)
3722 pedwarn ("type of bit-field %qs is a GCC extension", name);
3724 if (type_mv == boolean_type_node)
3725 max_width = CHAR_TYPE_SIZE;
3726 else
3727 max_width = TYPE_PRECISION (*type);
3729 if (0 < compare_tree_int (*width, max_width))
3731 error ("width of %qs exceeds its type", name);
3732 w = max_width;
3733 *width = build_int_cst (NULL_TREE, w);
3735 else
3736 w = tree_low_cst (*width, 1);
3738 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3740 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3741 if (!lt
3742 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3743 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3744 warning (0, "%qs is narrower than values of its type", name);
3748 /* Given declspecs and a declarator,
3749 determine the name and type of the object declared
3750 and construct a ..._DECL node for it.
3751 (In one case we can return a ..._TYPE node instead.
3752 For invalid input we sometimes return 0.)
3754 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3756 DECL_CONTEXT says which syntactic context this declaration is in:
3757 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3758 FUNCDEF for a function definition. Like NORMAL but a few different
3759 error messages in each case. Return value may be zero meaning
3760 this definition is too screwy to try to parse.
3761 PARM for a parameter declaration (either within a function prototype
3762 or before a function body). Make a PARM_DECL, or return void_type_node.
3763 TYPENAME if for a typename (in a cast or sizeof).
3764 Don't make a DECL node; just return the ..._TYPE node.
3765 FIELD for a struct or union field; make a FIELD_DECL.
3766 INITIALIZED is true if the decl has an initializer.
3767 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3768 representing the width of the bit-field.
3770 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3771 It may also be so in the PARM case, for a prototype where the
3772 argument type is specified but not the name.
3774 This function is where the complicated C meanings of `static'
3775 and `extern' are interpreted. */
3777 static tree
3778 grokdeclarator (const struct c_declarator *declarator,
3779 struct c_declspecs *declspecs,
3780 enum decl_context decl_context, bool initialized, tree *width)
3782 tree type = declspecs->type;
3783 bool threadp = declspecs->thread_p;
3784 enum c_storage_class storage_class = declspecs->storage_class;
3785 int constp;
3786 int restrictp;
3787 int volatilep;
3788 int type_quals = TYPE_UNQUALIFIED;
3789 const char *name, *orig_name;
3790 tree typedef_type = 0;
3791 int funcdef_flag = 0;
3792 bool funcdef_syntax = false;
3793 int size_varies = 0;
3794 tree decl_attr = declspecs->decl_attr;
3795 int array_ptr_quals = TYPE_UNQUALIFIED;
3796 tree array_ptr_attrs = NULL_TREE;
3797 int array_parm_static = 0;
3798 tree returned_attrs = NULL_TREE;
3799 bool bitfield = width != NULL;
3800 tree element_type;
3801 struct c_arg_info *arg_info = 0;
3803 if (decl_context == FUNCDEF)
3804 funcdef_flag = 1, decl_context = NORMAL;
3806 /* Look inside a declarator for the name being declared
3807 and get it as a string, for an error message. */
3809 const struct c_declarator *decl = declarator;
3810 name = 0;
3812 while (decl)
3813 switch (decl->kind)
3815 case cdk_function:
3816 case cdk_array:
3817 case cdk_pointer:
3818 funcdef_syntax = (decl->kind == cdk_function);
3819 decl = decl->declarator;
3820 break;
3822 case cdk_attrs:
3823 decl = decl->declarator;
3824 break;
3826 case cdk_id:
3827 if (decl->u.id)
3828 name = IDENTIFIER_POINTER (decl->u.id);
3829 decl = 0;
3830 break;
3832 default:
3833 gcc_unreachable ();
3835 orig_name = name;
3836 if (name == 0)
3837 name = "type name";
3840 /* A function definition's declarator must have the form of
3841 a function declarator. */
3843 if (funcdef_flag && !funcdef_syntax)
3844 return 0;
3846 /* If this looks like a function definition, make it one,
3847 even if it occurs where parms are expected.
3848 Then store_parm_decls will reject it and not use it as a parm. */
3849 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3850 decl_context = PARM;
3852 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3853 warn_deprecated_use (declspecs->type);
3855 typedef_type = type;
3856 size_varies = C_TYPE_VARIABLE_SIZE (type);
3858 /* Diagnose defaulting to "int". */
3860 if (declspecs->default_int_p && !in_system_header)
3862 /* Issue a warning if this is an ISO C 99 program or if
3863 -Wreturn-type and this is a function, or if -Wimplicit;
3864 prefer the former warning since it is more explicit. */
3865 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3866 && funcdef_flag)
3867 warn_about_return_type = 1;
3868 else if (warn_implicit_int || flag_isoc99)
3869 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3872 /* Adjust the type if a bit-field is being declared,
3873 -funsigned-bitfields applied and the type is not explicitly
3874 "signed". */
3875 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3876 && TREE_CODE (type) == INTEGER_TYPE)
3877 type = c_common_unsigned_type (type);
3879 /* Figure out the type qualifiers for the declaration. There are
3880 two ways a declaration can become qualified. One is something
3881 like `const int i' where the `const' is explicit. Another is
3882 something like `typedef const int CI; CI i' where the type of the
3883 declaration contains the `const'. A third possibility is that
3884 there is a type qualifier on the element type of a typedefed
3885 array type, in which case we should extract that qualifier so
3886 that c_apply_type_quals_to_decls receives the full list of
3887 qualifiers to work with (C90 is not entirely clear about whether
3888 duplicate qualifiers should be diagnosed in this case, but it
3889 seems most appropriate to do so). */
3890 element_type = strip_array_types (type);
3891 constp = declspecs->const_p + TYPE_READONLY (element_type);
3892 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3893 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3894 if (pedantic && !flag_isoc99)
3896 if (constp > 1)
3897 pedwarn ("duplicate %<const%>");
3898 if (restrictp > 1)
3899 pedwarn ("duplicate %<restrict%>");
3900 if (volatilep > 1)
3901 pedwarn ("duplicate %<volatile%>");
3903 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3904 type = TYPE_MAIN_VARIANT (type);
3905 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3906 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3907 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3909 /* Warn about storage classes that are invalid for certain
3910 kinds of declarations (parameters, typenames, etc.). */
3912 if (funcdef_flag
3913 && (threadp
3914 || storage_class == csc_auto
3915 || storage_class == csc_register
3916 || storage_class == csc_typedef))
3918 if (storage_class == csc_auto
3919 && (pedantic || current_scope == file_scope))
3920 pedwarn ("function definition declared %<auto%>");
3921 if (storage_class == csc_register)
3922 error ("function definition declared %<register%>");
3923 if (storage_class == csc_typedef)
3924 error ("function definition declared %<typedef%>");
3925 if (threadp)
3926 error ("function definition declared %<__thread%>");
3927 threadp = false;
3928 if (storage_class == csc_auto
3929 || storage_class == csc_register
3930 || storage_class == csc_typedef)
3931 storage_class = csc_none;
3933 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3935 if (decl_context == PARM && storage_class == csc_register)
3937 else
3939 switch (decl_context)
3941 case FIELD:
3942 error ("storage class specified for structure field %qs",
3943 name);
3944 break;
3945 case PARM:
3946 error ("storage class specified for parameter %qs", name);
3947 break;
3948 default:
3949 error ("storage class specified for typename");
3950 break;
3952 storage_class = csc_none;
3953 threadp = false;
3956 else if (storage_class == csc_extern
3957 && initialized
3958 && !funcdef_flag)
3960 /* 'extern' with initialization is invalid if not at file scope. */
3961 if (current_scope == file_scope)
3962 warning (0, "%qs initialized and declared %<extern%>", name);
3963 else
3964 error ("%qs has both %<extern%> and initializer", name);
3966 else if (current_scope == file_scope)
3968 if (storage_class == csc_auto)
3969 error ("file-scope declaration of %qs specifies %<auto%>", name);
3970 if (pedantic && storage_class == csc_register)
3971 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3973 else
3975 if (storage_class == csc_extern && funcdef_flag)
3976 error ("nested function %qs declared %<extern%>", name);
3977 else if (threadp && storage_class == csc_none)
3979 error ("function-scope %qs implicitly auto and declared "
3980 "%<__thread%>",
3981 name);
3982 threadp = false;
3986 /* Now figure out the structure of the declarator proper.
3987 Descend through it, creating more complex types, until we reach
3988 the declared identifier (or NULL_TREE, in an absolute declarator).
3989 At each stage we maintain an unqualified version of the type
3990 together with any qualifiers that should be applied to it with
3991 c_build_qualified_type; this way, array types including
3992 multidimensional array types are first built up in unqualified
3993 form and then the qualified form is created with
3994 TYPE_MAIN_VARIANT pointing to the unqualified form. */
3996 while (declarator && declarator->kind != cdk_id)
3998 if (type == error_mark_node)
4000 declarator = declarator->declarator;
4001 continue;
4004 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4005 a cdk_pointer (for *...),
4006 a cdk_function (for ...(...)),
4007 a cdk_attrs (for nested attributes),
4008 or a cdk_id (for the name being declared
4009 or the place in an absolute declarator
4010 where the name was omitted).
4011 For the last case, we have just exited the loop.
4013 At this point, TYPE is the type of elements of an array,
4014 or for a function to return, or for a pointer to point to.
4015 After this sequence of ifs, TYPE is the type of the
4016 array or function or pointer, and DECLARATOR has had its
4017 outermost layer removed. */
4019 if (array_ptr_quals != TYPE_UNQUALIFIED
4020 || array_ptr_attrs != NULL_TREE
4021 || array_parm_static)
4023 /* Only the innermost declarator (making a parameter be of
4024 array type which is converted to pointer type)
4025 may have static or type qualifiers. */
4026 error ("static or type qualifiers in non-parameter array declarator");
4027 array_ptr_quals = TYPE_UNQUALIFIED;
4028 array_ptr_attrs = NULL_TREE;
4029 array_parm_static = 0;
4032 switch (declarator->kind)
4034 case cdk_attrs:
4036 /* A declarator with embedded attributes. */
4037 tree attrs = declarator->u.attrs;
4038 const struct c_declarator *inner_decl;
4039 int attr_flags = 0;
4040 declarator = declarator->declarator;
4041 inner_decl = declarator;
4042 while (inner_decl->kind == cdk_attrs)
4043 inner_decl = inner_decl->declarator;
4044 if (inner_decl->kind == cdk_id)
4045 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4046 else if (inner_decl->kind == cdk_function)
4047 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4048 else if (inner_decl->kind == cdk_array)
4049 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4050 returned_attrs = decl_attributes (&type,
4051 chainon (returned_attrs, attrs),
4052 attr_flags);
4053 break;
4055 case cdk_array:
4057 tree itype = NULL_TREE;
4058 tree size = declarator->u.array.dimen;
4059 /* The index is a signed object `sizetype' bits wide. */
4060 tree index_type = c_common_signed_type (sizetype);
4062 array_ptr_quals = declarator->u.array.quals;
4063 array_ptr_attrs = declarator->u.array.attrs;
4064 array_parm_static = declarator->u.array.static_p;
4066 declarator = declarator->declarator;
4068 /* Check for some types that there cannot be arrays of. */
4070 if (VOID_TYPE_P (type))
4072 error ("declaration of %qs as array of voids", name);
4073 type = error_mark_node;
4076 if (TREE_CODE (type) == FUNCTION_TYPE)
4078 error ("declaration of %qs as array of functions", name);
4079 type = error_mark_node;
4082 if (pedantic && !in_system_header && flexible_array_type_p (type))
4083 pedwarn ("invalid use of structure with flexible array member");
4085 if (size == error_mark_node)
4086 type = error_mark_node;
4088 if (type == error_mark_node)
4089 continue;
4091 /* If size was specified, set ITYPE to a range-type for
4092 that size. Otherwise, ITYPE remains null. finish_decl
4093 may figure it out from an initial value. */
4095 if (size)
4097 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4098 lvalue. */
4099 STRIP_TYPE_NOPS (size);
4101 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4103 error ("size of array %qs has non-integer type", name);
4104 size = integer_one_node;
4107 if (pedantic && integer_zerop (size))
4108 pedwarn ("ISO C forbids zero-size array %qs", name);
4110 if (TREE_CODE (size) == INTEGER_CST)
4112 constant_expression_warning (size);
4113 if (tree_int_cst_sgn (size) < 0)
4115 error ("size of array %qs is negative", name);
4116 size = integer_one_node;
4119 else
4121 /* Make sure the array size remains visibly
4122 nonconstant even if it is (eg) a const variable
4123 with known value. */
4124 size_varies = 1;
4126 if (!flag_isoc99 && pedantic)
4128 if (TREE_CONSTANT (size))
4129 pedwarn ("ISO C90 forbids array %qs whose size "
4130 "can%'t be evaluated",
4131 name);
4132 else
4133 pedwarn ("ISO C90 forbids variable-size array %qs",
4134 name);
4138 if (integer_zerop (size))
4140 /* A zero-length array cannot be represented with
4141 an unsigned index type, which is what we'll
4142 get with build_index_type. Create an
4143 open-ended range instead. */
4144 itype = build_range_type (sizetype, size, NULL_TREE);
4146 else
4148 /* Arrange for the SAVE_EXPR on the inside of the
4149 MINUS_EXPR, which allows the -1 to get folded
4150 with the +1 that happens when building TYPE_SIZE. */
4151 if (size_varies)
4152 size = variable_size (size);
4154 /* Compute the maximum valid index, that is, size
4155 - 1. Do the calculation in index_type, so that
4156 if it is a variable the computations will be
4157 done in the proper mode. */
4158 itype = fold (build2 (MINUS_EXPR, index_type,
4159 convert (index_type, size),
4160 convert (index_type,
4161 size_one_node)));
4163 /* If that overflowed, the array is too big. ???
4164 While a size of INT_MAX+1 technically shouldn't
4165 cause an overflow (because we subtract 1), the
4166 overflow is recorded during the conversion to
4167 index_type, before the subtraction. Handling
4168 this case seems like an unnecessary
4169 complication. */
4170 if (TREE_OVERFLOW (itype))
4172 error ("size of array %qs is too large", name);
4173 type = error_mark_node;
4174 continue;
4177 itype = build_index_type (itype);
4180 else if (decl_context == FIELD)
4182 if (pedantic && !flag_isoc99 && !in_system_header)
4183 pedwarn ("ISO C90 does not support flexible array members");
4185 /* ISO C99 Flexible array members are effectively
4186 identical to GCC's zero-length array extension. */
4187 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4190 /* Complain about arrays of incomplete types. */
4191 if (!COMPLETE_TYPE_P (type))
4193 error ("array type has incomplete element type");
4194 type = error_mark_node;
4196 else
4197 type = build_array_type (type, itype);
4199 if (size_varies)
4200 C_TYPE_VARIABLE_SIZE (type) = 1;
4202 /* The GCC extension for zero-length arrays differs from
4203 ISO flexible array members in that sizeof yields
4204 zero. */
4205 if (size && integer_zerop (size))
4207 TYPE_SIZE (type) = bitsize_zero_node;
4208 TYPE_SIZE_UNIT (type) = size_zero_node;
4211 if (decl_context != PARM
4212 && (array_ptr_quals != TYPE_UNQUALIFIED
4213 || array_ptr_attrs != NULL_TREE
4214 || array_parm_static))
4216 error ("static or type qualifiers in non-parameter array declarator");
4217 array_ptr_quals = TYPE_UNQUALIFIED;
4218 array_ptr_attrs = NULL_TREE;
4219 array_parm_static = 0;
4221 break;
4223 case cdk_function:
4225 /* Say it's a definition only for the declarator closest
4226 to the identifier, apart possibly from some
4227 attributes. */
4228 bool really_funcdef = false;
4229 tree arg_types;
4230 if (funcdef_flag)
4232 const struct c_declarator *t = declarator->declarator;
4233 while (t->kind == cdk_attrs)
4234 t = t->declarator;
4235 really_funcdef = (t->kind == cdk_id);
4238 /* Declaring a function type. Make sure we have a valid
4239 type for the function to return. */
4240 if (type == error_mark_node)
4241 continue;
4243 size_varies = 0;
4245 /* Warn about some types functions can't return. */
4246 if (TREE_CODE (type) == FUNCTION_TYPE)
4248 error ("%qs declared as function returning a function", name);
4249 type = integer_type_node;
4251 if (TREE_CODE (type) == ARRAY_TYPE)
4253 error ("%qs declared as function returning an array", name);
4254 type = integer_type_node;
4257 /* Construct the function type and go to the next
4258 inner layer of declarator. */
4259 arg_info = declarator->u.arg_info;
4260 arg_types = grokparms (arg_info, really_funcdef);
4262 /* Type qualifiers before the return type of the function
4263 qualify the return type, not the function type. */
4264 if (type_quals)
4266 /* Type qualifiers on a function return type are
4267 normally permitted by the standard but have no
4268 effect, so give a warning at -Wreturn-type.
4269 Qualifiers on a void return type are banned on
4270 function definitions in ISO C; GCC used to used
4271 them for noreturn functions. */
4272 if (VOID_TYPE_P (type) && really_funcdef)
4273 pedwarn ("function definition has qualified void return type");
4274 else
4275 warning (OPT_Wreturn_type,
4276 "type qualifiers ignored on function return type");
4278 type = c_build_qualified_type (type, type_quals);
4280 type_quals = TYPE_UNQUALIFIED;
4282 type = build_function_type (type, arg_types);
4283 declarator = declarator->declarator;
4285 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4286 the formal parameter list of this FUNCTION_TYPE to point to
4287 the FUNCTION_TYPE node itself. */
4289 tree link;
4291 for (link = arg_info->tags;
4292 link;
4293 link = TREE_CHAIN (link))
4294 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4296 break;
4298 case cdk_pointer:
4300 /* Merge any constancy or volatility into the target type
4301 for the pointer. */
4303 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4304 && type_quals)
4305 pedwarn ("ISO C forbids qualified function types");
4306 if (type_quals)
4307 type = c_build_qualified_type (type, type_quals);
4308 size_varies = 0;
4310 type = build_pointer_type (type);
4312 /* Process type qualifiers (such as const or volatile)
4313 that were given inside the `*'. */
4314 type_quals = declarator->u.pointer_quals;
4316 declarator = declarator->declarator;
4317 break;
4319 default:
4320 gcc_unreachable ();
4324 /* Now TYPE has the actual type, apart from any qualifiers in
4325 TYPE_QUALS. */
4327 /* Check the type and width of a bit-field. */
4328 if (bitfield)
4329 check_bitfield_type_and_width (&type, width, orig_name);
4331 /* Did array size calculations overflow? */
4333 if (TREE_CODE (type) == ARRAY_TYPE
4334 && COMPLETE_TYPE_P (type)
4335 && TREE_OVERFLOW (TYPE_SIZE (type)))
4337 error ("size of array %qs is too large", name);
4338 /* If we proceed with the array type as it is, we'll eventually
4339 crash in tree_low_cst(). */
4340 type = error_mark_node;
4343 /* If this is declaring a typedef name, return a TYPE_DECL. */
4345 if (storage_class == csc_typedef)
4347 tree decl;
4348 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4349 && type_quals)
4350 pedwarn ("ISO C forbids qualified function types");
4351 if (type_quals)
4352 type = c_build_qualified_type (type, type_quals);
4353 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4354 if (declspecs->explicit_signed_p)
4355 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4356 decl_attributes (&decl, returned_attrs, 0);
4357 if (declspecs->inline_p)
4358 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4359 return decl;
4362 /* Detect the case of an array type of unspecified size
4363 which came, as such, direct from a typedef name.
4364 We must copy the type, so that each identifier gets
4365 a distinct type, so that each identifier's size can be
4366 controlled separately by its own initializer. */
4368 if (type != 0 && typedef_type != 0
4369 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4370 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4372 type = build_array_type (TREE_TYPE (type), 0);
4373 if (size_varies)
4374 C_TYPE_VARIABLE_SIZE (type) = 1;
4377 /* If this is a type name (such as, in a cast or sizeof),
4378 compute the type and return it now. */
4380 if (decl_context == TYPENAME)
4382 /* Note that the grammar rejects storage classes in typenames
4383 and fields. */
4384 gcc_assert (storage_class == csc_none && !threadp
4385 && !declspecs->inline_p);
4386 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4387 && type_quals)
4388 pedwarn ("ISO C forbids const or volatile function types");
4389 if (type_quals)
4390 type = c_build_qualified_type (type, type_quals);
4391 decl_attributes (&type, returned_attrs, 0);
4392 return type;
4395 /* Aside from typedefs and type names (handle above),
4396 `void' at top level (not within pointer)
4397 is allowed only in public variables.
4398 We don't complain about parms either, but that is because
4399 a better error message can be made later. */
4401 if (VOID_TYPE_P (type) && decl_context != PARM
4402 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4403 && (storage_class == csc_extern
4404 || (current_scope == file_scope
4405 && !(storage_class == csc_static
4406 || storage_class == csc_register)))))
4408 error ("variable or field %qs declared void", name);
4409 type = integer_type_node;
4412 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4413 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4416 tree decl;
4418 if (decl_context == PARM)
4420 tree type_as_written;
4421 tree promoted_type;
4423 /* A parameter declared as an array of T is really a pointer to T.
4424 One declared as a function is really a pointer to a function. */
4426 if (TREE_CODE (type) == ARRAY_TYPE)
4428 /* Transfer const-ness of array into that of type pointed to. */
4429 type = TREE_TYPE (type);
4430 if (type_quals)
4431 type = c_build_qualified_type (type, type_quals);
4432 type = build_pointer_type (type);
4433 type_quals = array_ptr_quals;
4435 /* We don't yet implement attributes in this context. */
4436 if (array_ptr_attrs != NULL_TREE)
4437 warning (OPT_Wattributes,
4438 "attributes in parameter array declarator ignored");
4440 size_varies = 0;
4442 else if (TREE_CODE (type) == FUNCTION_TYPE)
4444 if (pedantic && type_quals)
4445 pedwarn ("ISO C forbids qualified function types");
4446 if (type_quals)
4447 type = c_build_qualified_type (type, type_quals);
4448 type = build_pointer_type (type);
4449 type_quals = TYPE_UNQUALIFIED;
4451 else if (type_quals)
4452 type = c_build_qualified_type (type, type_quals);
4454 type_as_written = type;
4456 decl = build_decl (PARM_DECL, declarator->u.id, type);
4457 if (size_varies)
4458 C_DECL_VARIABLE_SIZE (decl) = 1;
4460 /* Compute the type actually passed in the parmlist,
4461 for the case where there is no prototype.
4462 (For example, shorts and chars are passed as ints.)
4463 When there is a prototype, this is overridden later. */
4465 if (type == error_mark_node)
4466 promoted_type = type;
4467 else
4468 promoted_type = c_type_promotes_to (type);
4470 DECL_ARG_TYPE (decl) = promoted_type;
4471 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4472 if (declspecs->inline_p)
4473 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4475 else if (decl_context == FIELD)
4477 /* Note that the grammar rejects storage classes in typenames
4478 and fields. */
4479 gcc_assert (storage_class == csc_none && !threadp
4480 && !declspecs->inline_p);
4482 /* Structure field. It may not be a function. */
4484 if (TREE_CODE (type) == FUNCTION_TYPE)
4486 error ("field %qs declared as a function", name);
4487 type = build_pointer_type (type);
4489 else if (TREE_CODE (type) != ERROR_MARK
4490 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4492 error ("field %qs has incomplete type", name);
4493 type = error_mark_node;
4495 type = c_build_qualified_type (type, type_quals);
4496 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4497 DECL_NONADDRESSABLE_P (decl) = bitfield;
4499 if (size_varies)
4500 C_DECL_VARIABLE_SIZE (decl) = 1;
4502 else if (TREE_CODE (type) == FUNCTION_TYPE)
4504 if (storage_class == csc_register || threadp)
4506 error ("invalid storage class for function %qs", name);
4508 else if (current_scope != file_scope)
4510 /* Function declaration not at file scope. Storage
4511 classes other than `extern' are not allowed, C99
4512 6.7.1p5, and `extern' makes no difference. However,
4513 GCC allows 'auto', perhaps with 'inline', to support
4514 nested functions. */
4515 if (storage_class == csc_auto)
4517 if (pedantic)
4518 pedwarn ("invalid storage class for function %qs", name);
4520 else if (storage_class == csc_static)
4522 error ("invalid storage class for function %qs", name);
4523 if (funcdef_flag)
4524 storage_class = declspecs->storage_class = csc_none;
4525 else
4526 return 0;
4530 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4531 decl = build_decl_attribute_variant (decl, decl_attr);
4533 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4535 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4536 pedwarn ("ISO C forbids qualified function types");
4538 /* GNU C interprets a volatile-qualified function type to indicate
4539 that the function does not return. */
4540 if ((type_quals & TYPE_QUAL_VOLATILE)
4541 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4542 warning (0, "%<noreturn%> function returns non-void value");
4544 /* Every function declaration is an external reference
4545 (DECL_EXTERNAL) except for those which are not at file
4546 scope and are explicitly declared "auto". This is
4547 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4548 GCC to signify a forward declaration of a nested function. */
4549 if (storage_class == csc_auto && current_scope != file_scope)
4550 DECL_EXTERNAL (decl) = 0;
4551 else
4552 DECL_EXTERNAL (decl) = 1;
4554 /* Record absence of global scope for `static' or `auto'. */
4555 TREE_PUBLIC (decl)
4556 = !(storage_class == csc_static || storage_class == csc_auto);
4558 /* For a function definition, record the argument information
4559 block where store_parm_decls will look for it. */
4560 if (funcdef_flag)
4561 current_function_arg_info = arg_info;
4563 if (declspecs->default_int_p)
4564 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4566 /* Record presence of `inline', if it is reasonable. */
4567 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4569 if (declspecs->inline_p)
4570 pedwarn ("cannot inline function %<main%>");
4572 else if (declspecs->inline_p)
4574 /* Record that the function is declared `inline'. */
4575 DECL_DECLARED_INLINE_P (decl) = 1;
4577 /* Do not mark bare declarations as DECL_INLINE. Doing so
4578 in the presence of multiple declarations can result in
4579 the abstract origin pointing between the declarations,
4580 which will confuse dwarf2out. */
4581 if (initialized)
4583 DECL_INLINE (decl) = 1;
4584 if (storage_class == csc_extern)
4585 current_extern_inline = 1;
4588 /* If -finline-functions, assume it can be inlined. This does
4589 two things: let the function be deferred until it is actually
4590 needed, and let dwarf2 know that the function is inlinable. */
4591 else if (flag_inline_trees == 2 && initialized)
4592 DECL_INLINE (decl) = 1;
4594 else
4596 /* It's a variable. */
4597 /* An uninitialized decl with `extern' is a reference. */
4598 int extern_ref = !initialized && storage_class == csc_extern;
4600 type = c_build_qualified_type (type, type_quals);
4602 /* C99 6.2.2p7: It is invalid (compile-time undefined
4603 behavior) to create an 'extern' declaration for a
4604 variable if there is a global declaration that is
4605 'static' and the global declaration is not visible.
4606 (If the static declaration _is_ currently visible,
4607 the 'extern' declaration is taken to refer to that decl.) */
4608 if (extern_ref && current_scope != file_scope)
4610 tree global_decl = identifier_global_value (declarator->u.id);
4611 tree visible_decl = lookup_name (declarator->u.id);
4613 if (global_decl
4614 && global_decl != visible_decl
4615 && TREE_CODE (global_decl) == VAR_DECL
4616 && !TREE_PUBLIC (global_decl))
4617 error ("variable previously declared %<static%> redeclared "
4618 "%<extern%>");
4621 decl = build_decl (VAR_DECL, declarator->u.id, type);
4622 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4623 if (size_varies)
4624 C_DECL_VARIABLE_SIZE (decl) = 1;
4626 if (declspecs->inline_p)
4627 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4629 /* At file scope, an initialized extern declaration may follow
4630 a static declaration. In that case, DECL_EXTERNAL will be
4631 reset later in start_decl. */
4632 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4634 /* At file scope, the presence of a `static' or `register' storage
4635 class specifier, or the absence of all storage class specifiers
4636 makes this declaration a definition (perhaps tentative). Also,
4637 the absence of both `static' and `register' makes it public. */
4638 if (current_scope == file_scope)
4640 TREE_PUBLIC (decl) = !(storage_class == csc_static
4641 || storage_class == csc_register);
4642 TREE_STATIC (decl) = !extern_ref;
4644 /* Not at file scope, only `static' makes a static definition. */
4645 else
4647 TREE_STATIC (decl) = (storage_class == csc_static);
4648 TREE_PUBLIC (decl) = extern_ref;
4651 if (threadp)
4653 if (targetm.have_tls)
4654 DECL_THREAD_LOCAL (decl) = 1;
4655 else
4656 /* A mere warning is sure to result in improper semantics
4657 at runtime. Don't bother to allow this to compile. */
4658 error ("thread-local storage not supported for this target");
4662 /* Record `register' declaration for warnings on &
4663 and in case doing stupid register allocation. */
4665 if (storage_class == csc_register)
4667 C_DECL_REGISTER (decl) = 1;
4668 DECL_REGISTER (decl) = 1;
4671 /* Record constancy and volatility. */
4672 c_apply_type_quals_to_decl (type_quals, decl);
4674 /* If a type has volatile components, it should be stored in memory.
4675 Otherwise, the fact that those components are volatile
4676 will be ignored, and would even crash the compiler. */
4677 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4679 /* It is not an error for a structure with volatile fields to
4680 be declared register, but reset DECL_REGISTER since it
4681 cannot actually go in a register. */
4682 int was_reg = C_DECL_REGISTER (decl);
4683 C_DECL_REGISTER (decl) = 0;
4684 DECL_REGISTER (decl) = 0;
4685 c_mark_addressable (decl);
4686 C_DECL_REGISTER (decl) = was_reg;
4689 /* This is the earliest point at which we might know the assembler
4690 name of a variable. Thus, if it's known before this, die horribly. */
4691 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4693 decl_attributes (&decl, returned_attrs, 0);
4695 return decl;
4699 /* Decode the parameter-list info for a function type or function definition.
4700 The argument is the value returned by `get_parm_info' (or made in parse.y
4701 if there is an identifier list instead of a parameter decl list).
4702 These two functions are separate because when a function returns
4703 or receives functions then each is called multiple times but the order
4704 of calls is different. The last call to `grokparms' is always the one
4705 that contains the formal parameter names of a function definition.
4707 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4709 FUNCDEF_FLAG is true for a function definition, false for
4710 a mere declaration. A nonempty identifier-list gets an error message
4711 when FUNCDEF_FLAG is false. */
4713 static tree
4714 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4716 tree arg_types = arg_info->types;
4718 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4719 && !in_system_header)
4720 warning (0, "function declaration isn%'t a prototype");
4722 if (arg_types == error_mark_node)
4723 return 0; /* don't set TYPE_ARG_TYPES in this case */
4725 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4727 if (!funcdef_flag)
4728 pedwarn ("parameter names (without types) in function declaration");
4730 arg_info->parms = arg_info->types;
4731 arg_info->types = 0;
4732 return 0;
4734 else
4736 tree parm, type, typelt;
4737 unsigned int parmno;
4739 /* If there is a parameter of incomplete type in a definition,
4740 this is an error. In a declaration this is valid, and a
4741 struct or union type may be completed later, before any calls
4742 or definition of the function. In the case where the tag was
4743 first declared within the parameter list, a warning has
4744 already been given. If a parameter has void type, then
4745 however the function cannot be defined or called, so
4746 warn. */
4748 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4749 parm;
4750 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4752 type = TREE_VALUE (typelt);
4753 if (type == error_mark_node)
4754 continue;
4756 if (!COMPLETE_TYPE_P (type))
4758 if (funcdef_flag)
4760 if (DECL_NAME (parm))
4761 error ("%Jparameter %u (%qD) has incomplete type",
4762 parm, parmno, parm);
4763 else
4764 error ("%Jparameter %u has incomplete type",
4765 parm, parmno);
4767 TREE_VALUE (typelt) = error_mark_node;
4768 TREE_TYPE (parm) = error_mark_node;
4770 else if (VOID_TYPE_P (type))
4772 if (DECL_NAME (parm))
4773 warning (0, "%Jparameter %u (%qD) has void type",
4774 parm, parmno, parm);
4775 else
4776 warning (0, "%Jparameter %u has void type",
4777 parm, parmno);
4781 return arg_types;
4785 /* Take apart the current scope and return a c_arg_info structure with
4786 info on a parameter list just parsed.
4788 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4790 ELLIPSIS being true means the argument list ended in '...' so don't
4791 append a sentinel (void_list_node) to the end of the type-list. */
4793 struct c_arg_info *
4794 get_parm_info (bool ellipsis)
4796 struct c_binding *b = current_scope->bindings;
4797 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4798 struct c_arg_info);
4799 tree parms = 0;
4800 tree tags = 0;
4801 tree types = 0;
4802 tree others = 0;
4804 static bool explained_incomplete_types = false;
4805 bool gave_void_only_once_err = false;
4807 arg_info->parms = 0;
4808 arg_info->tags = 0;
4809 arg_info->types = 0;
4810 arg_info->others = 0;
4812 /* The bindings in this scope must not get put into a block.
4813 We will take care of deleting the binding nodes. */
4814 current_scope->bindings = 0;
4816 /* This function is only called if there was *something* on the
4817 parameter list. */
4818 gcc_assert (b);
4820 /* A parameter list consisting solely of 'void' indicates that the
4821 function takes no arguments. But if the 'void' is qualified
4822 (by 'const' or 'volatile'), or has a storage class specifier
4823 ('register'), then the behavior is undefined; issue an error.
4824 Typedefs for 'void' are OK (see DR#157). */
4825 if (b->prev == 0 /* one binding */
4826 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4827 && !DECL_NAME (b->decl) /* anonymous */
4828 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4830 if (TREE_THIS_VOLATILE (b->decl)
4831 || TREE_READONLY (b->decl)
4832 || C_DECL_REGISTER (b->decl))
4833 error ("%<void%> as only parameter may not be qualified");
4835 /* There cannot be an ellipsis. */
4836 if (ellipsis)
4837 error ("%<void%> must be the only parameter");
4839 arg_info->types = void_list_node;
4840 return arg_info;
4843 if (!ellipsis)
4844 types = void_list_node;
4846 /* Break up the bindings list into parms, tags, types, and others;
4847 apply sanity checks; purge the name-to-decl bindings. */
4848 while (b)
4850 tree decl = b->decl;
4851 tree type = TREE_TYPE (decl);
4852 const char *keyword;
4854 switch (TREE_CODE (decl))
4856 case PARM_DECL:
4857 if (b->id)
4859 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4860 I_SYMBOL_BINDING (b->id) = b->shadowed;
4863 /* Check for forward decls that never got their actual decl. */
4864 if (TREE_ASM_WRITTEN (decl))
4865 error ("%Jparameter %qD has just a forward declaration",
4866 decl, decl);
4867 /* Check for (..., void, ...) and issue an error. */
4868 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4870 if (!gave_void_only_once_err)
4872 error ("%<void%> must be the only parameter");
4873 gave_void_only_once_err = true;
4876 else
4878 /* Valid parameter, add it to the list. */
4879 TREE_CHAIN (decl) = parms;
4880 parms = decl;
4882 /* Since there is a prototype, args are passed in their
4883 declared types. The back end may override this later. */
4884 DECL_ARG_TYPE (decl) = type;
4885 types = tree_cons (0, type, types);
4887 break;
4889 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4890 case UNION_TYPE: keyword = "union"; goto tag;
4891 case RECORD_TYPE: keyword = "struct"; goto tag;
4892 tag:
4893 /* Types may not have tag-names, in which case the type
4894 appears in the bindings list with b->id NULL. */
4895 if (b->id)
4897 gcc_assert (I_TAG_BINDING (b->id) == b);
4898 I_TAG_BINDING (b->id) = b->shadowed;
4901 /* Warn about any struct, union or enum tags defined in a
4902 parameter list. The scope of such types is limited to
4903 the parameter list, which is rarely if ever desirable
4904 (it's impossible to call such a function with type-
4905 correct arguments). An anonymous union parm type is
4906 meaningful as a GNU extension, so don't warn for that. */
4907 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4909 if (b->id)
4910 /* The %s will be one of 'struct', 'union', or 'enum'. */
4911 warning (0, "%<%s %E%> declared inside parameter list",
4912 keyword, b->id);
4913 else
4914 /* The %s will be one of 'struct', 'union', or 'enum'. */
4915 warning (0, "anonymous %s declared inside parameter list",
4916 keyword);
4918 if (!explained_incomplete_types)
4920 warning (0, "its scope is only this definition or declaration,"
4921 " which is probably not what you want");
4922 explained_incomplete_types = true;
4926 tags = tree_cons (b->id, decl, tags);
4927 break;
4929 case CONST_DECL:
4930 case TYPE_DECL:
4931 case FUNCTION_DECL:
4932 /* CONST_DECLs appear here when we have an embedded enum,
4933 and TYPE_DECLs appear here when we have an embedded struct
4934 or union. No warnings for this - we already warned about the
4935 type itself. FUNCTION_DECLs appear when there is an implicit
4936 function declaration in the parameter list. */
4938 TREE_CHAIN (decl) = others;
4939 others = decl;
4940 /* fall through */
4942 case ERROR_MARK:
4943 /* error_mark_node appears here when we have an undeclared
4944 variable. Just throw it away. */
4945 if (b->id)
4947 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4948 I_SYMBOL_BINDING (b->id) = b->shadowed;
4950 break;
4952 /* Other things that might be encountered. */
4953 case LABEL_DECL:
4954 case VAR_DECL:
4955 default:
4956 gcc_unreachable ();
4959 b = free_binding_and_advance (b);
4962 arg_info->parms = parms;
4963 arg_info->tags = tags;
4964 arg_info->types = types;
4965 arg_info->others = others;
4966 return arg_info;
4969 /* Get the struct, enum or union (CODE says which) with tag NAME.
4970 Define the tag as a forward-reference if it is not defined.
4971 Return a c_typespec structure for the type specifier. */
4973 struct c_typespec
4974 parser_xref_tag (enum tree_code code, tree name)
4976 struct c_typespec ret;
4977 /* If a cross reference is requested, look up the type
4978 already defined for this tag and return it. */
4980 tree ref = lookup_tag (code, name, 0);
4981 /* If this is the right type of tag, return what we found.
4982 (This reference will be shadowed by shadow_tag later if appropriate.)
4983 If this is the wrong type of tag, do not return it. If it was the
4984 wrong type in the same scope, we will have had an error
4985 message already; if in a different scope and declaring
4986 a name, pending_xref_error will give an error message; but if in a
4987 different scope and not declaring a name, this tag should
4988 shadow the previous declaration of a different type of tag, and
4989 this would not work properly if we return the reference found.
4990 (For example, with "struct foo" in an outer scope, "union foo;"
4991 must shadow that tag with a new one of union type.) */
4992 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
4993 if (ref && TREE_CODE (ref) == code)
4995 ret.spec = ref;
4996 return ret;
4999 /* If no such tag is yet defined, create a forward-reference node
5000 and record it as the "definition".
5001 When a real declaration of this type is found,
5002 the forward-reference will be altered into a real type. */
5004 ref = make_node (code);
5005 if (code == ENUMERAL_TYPE)
5007 /* Give the type a default layout like unsigned int
5008 to avoid crashing if it does not get defined. */
5009 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5010 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5011 TYPE_USER_ALIGN (ref) = 0;
5012 TYPE_UNSIGNED (ref) = 1;
5013 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5014 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5015 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5018 pushtag (name, ref);
5020 ret.spec = ref;
5021 return ret;
5024 /* Get the struct, enum or union (CODE says which) with tag NAME.
5025 Define the tag as a forward-reference if it is not defined.
5026 Return a tree for the type. */
5028 tree
5029 xref_tag (enum tree_code code, tree name)
5031 return parser_xref_tag (code, name).spec;
5034 /* Make sure that the tag NAME is defined *in the current scope*
5035 at least as a forward reference.
5036 CODE says which kind of tag NAME ought to be. */
5038 tree
5039 start_struct (enum tree_code code, tree name)
5041 /* If there is already a tag defined at this scope
5042 (as a forward reference), just return it. */
5044 tree ref = 0;
5046 if (name != 0)
5047 ref = lookup_tag (code, name, 1);
5048 if (ref && TREE_CODE (ref) == code)
5050 if (TYPE_SIZE (ref))
5052 if (code == UNION_TYPE)
5053 error ("redefinition of %<union %E%>", name);
5054 else
5055 error ("redefinition of %<struct %E%>", name);
5057 else if (C_TYPE_BEING_DEFINED (ref))
5059 if (code == UNION_TYPE)
5060 error ("nested redefinition of %<union %E%>", name);
5061 else
5062 error ("nested redefinition of %<struct %E%>", name);
5065 else
5067 /* Otherwise create a forward-reference just so the tag is in scope. */
5069 ref = make_node (code);
5070 pushtag (name, ref);
5073 C_TYPE_BEING_DEFINED (ref) = 1;
5074 TYPE_PACKED (ref) = flag_pack_struct;
5075 return ref;
5078 /* Process the specs, declarator and width (NULL if omitted)
5079 of a structure component, returning a FIELD_DECL node.
5080 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5082 This is done during the parsing of the struct declaration.
5083 The FIELD_DECL nodes are chained together and the lot of them
5084 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5086 tree
5087 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5088 tree width)
5090 tree value;
5092 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5093 && width == NULL_TREE)
5095 /* This is an unnamed decl.
5097 If we have something of the form "union { list } ;" then this
5098 is the anonymous union extension. Similarly for struct.
5100 If this is something of the form "struct foo;", then
5101 If MS extensions are enabled, this is handled as an
5102 anonymous struct.
5103 Otherwise this is a forward declaration of a structure tag.
5105 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5106 If MS extensions are enabled and foo names a structure, then
5107 again this is an anonymous struct.
5108 Otherwise this is an error.
5110 Oh what a horrid tangled web we weave. I wonder if MS consciously
5111 took this from Plan 9 or if it was an accident of implementation
5112 that took root before someone noticed the bug... */
5114 tree type = declspecs->type;
5115 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5116 || TREE_CODE (type) == UNION_TYPE);
5117 bool ok = false;
5119 if (type_ok
5120 && (flag_ms_extensions || !declspecs->typedef_p))
5122 if (flag_ms_extensions)
5123 ok = true;
5124 else if (flag_iso)
5125 ok = false;
5126 else if (TYPE_NAME (type) == NULL)
5127 ok = true;
5128 else
5129 ok = false;
5131 if (!ok)
5133 pedwarn ("declaration does not declare anything");
5134 return NULL_TREE;
5136 if (pedantic)
5137 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5140 value = grokdeclarator (declarator, declspecs, FIELD, false,
5141 width ? &width : NULL);
5143 finish_decl (value, NULL_TREE, NULL_TREE);
5144 DECL_INITIAL (value) = width;
5146 return value;
5149 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5150 the list such that this does not present a problem later. */
5152 static void
5153 detect_field_duplicates (tree fieldlist)
5155 tree x, y;
5156 int timeout = 10;
5158 /* First, see if there are more than "a few" fields.
5159 This is trivially true if there are zero or one fields. */
5160 if (!fieldlist)
5161 return;
5162 x = TREE_CHAIN (fieldlist);
5163 if (!x)
5164 return;
5165 do {
5166 timeout--;
5167 x = TREE_CHAIN (x);
5168 } while (timeout > 0 && x);
5170 /* If there were "few" fields, avoid the overhead of allocating
5171 a hash table. Instead just do the nested traversal thing. */
5172 if (timeout > 0)
5174 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5175 if (DECL_NAME (x))
5177 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5178 if (DECL_NAME (y) == DECL_NAME (x))
5180 error ("%Jduplicate member %qD", x, x);
5181 DECL_NAME (x) = NULL_TREE;
5185 else
5187 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5188 void **slot;
5190 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5191 if ((y = DECL_NAME (x)) != 0)
5193 slot = htab_find_slot (htab, y, INSERT);
5194 if (*slot)
5196 error ("%Jduplicate member %qD", x, x);
5197 DECL_NAME (x) = NULL_TREE;
5199 *slot = y;
5202 htab_delete (htab);
5206 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5207 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5208 ATTRIBUTES are attributes to be applied to the structure. */
5210 tree
5211 finish_struct (tree t, tree fieldlist, tree attributes)
5213 tree x;
5214 bool toplevel = file_scope == current_scope;
5215 int saw_named_field;
5217 /* If this type was previously laid out as a forward reference,
5218 make sure we lay it out again. */
5220 TYPE_SIZE (t) = 0;
5222 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5224 if (pedantic)
5226 for (x = fieldlist; x; x = TREE_CHAIN (x))
5227 if (DECL_NAME (x) != 0)
5228 break;
5230 if (x == 0)
5232 if (TREE_CODE (t) == UNION_TYPE)
5234 if (fieldlist)
5235 pedwarn ("union has no named members");
5236 else
5237 pedwarn ("union has no members");
5239 else
5241 if (fieldlist)
5242 pedwarn ("struct has no named members");
5243 else
5244 pedwarn ("struct has no members");
5249 /* Install struct as DECL_CONTEXT of each field decl.
5250 Also process specified field sizes, found in the DECL_INITIAL,
5251 storing 0 there after the type has been changed to precision equal
5252 to its width, rather than the precision of the specified standard
5253 type. (Correct layout requires the original type to have been preserved
5254 until now.) */
5256 saw_named_field = 0;
5257 for (x = fieldlist; x; x = TREE_CHAIN (x))
5259 DECL_CONTEXT (x) = t;
5260 DECL_PACKED (x) |= TYPE_PACKED (t);
5262 /* If any field is const, the structure type is pseudo-const. */
5263 if (TREE_READONLY (x))
5264 C_TYPE_FIELDS_READONLY (t) = 1;
5265 else
5267 /* A field that is pseudo-const makes the structure likewise. */
5268 tree t1 = TREE_TYPE (x);
5269 while (TREE_CODE (t1) == ARRAY_TYPE)
5270 t1 = TREE_TYPE (t1);
5271 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5272 && C_TYPE_FIELDS_READONLY (t1))
5273 C_TYPE_FIELDS_READONLY (t) = 1;
5276 /* Any field that is volatile means variables of this type must be
5277 treated in some ways as volatile. */
5278 if (TREE_THIS_VOLATILE (x))
5279 C_TYPE_FIELDS_VOLATILE (t) = 1;
5281 /* Any field of nominal variable size implies structure is too. */
5282 if (C_DECL_VARIABLE_SIZE (x))
5283 C_TYPE_VARIABLE_SIZE (t) = 1;
5285 if (DECL_INITIAL (x))
5287 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5288 DECL_SIZE (x) = bitsize_int (width);
5289 DECL_BIT_FIELD (x) = 1;
5290 SET_DECL_C_BIT_FIELD (x);
5293 /* Detect flexible array member in an invalid context. */
5294 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5295 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5296 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5297 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5299 if (TREE_CODE (t) == UNION_TYPE)
5301 error ("%Jflexible array member in union", x);
5302 TREE_TYPE (x) = error_mark_node;
5304 else if (TREE_CHAIN (x) != NULL_TREE)
5306 error ("%Jflexible array member not at end of struct", x);
5307 TREE_TYPE (x) = error_mark_node;
5309 else if (!saw_named_field)
5311 error ("%Jflexible array member in otherwise empty struct", x);
5312 TREE_TYPE (x) = error_mark_node;
5316 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5317 && flexible_array_type_p (TREE_TYPE (x)))
5318 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5320 if (DECL_NAME (x))
5321 saw_named_field = 1;
5324 detect_field_duplicates (fieldlist);
5326 /* Now we have the nearly final fieldlist. Record it,
5327 then lay out the structure or union (including the fields). */
5329 TYPE_FIELDS (t) = fieldlist;
5331 layout_type (t);
5333 /* Give bit-fields their proper types. */
5335 tree *fieldlistp = &fieldlist;
5336 while (*fieldlistp)
5337 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5338 && TREE_TYPE (*fieldlistp) != error_mark_node)
5340 unsigned HOST_WIDE_INT width
5341 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5342 tree type = TREE_TYPE (*fieldlistp);
5343 if (width != TYPE_PRECISION (type))
5345 TREE_TYPE (*fieldlistp)
5346 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5347 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5349 DECL_INITIAL (*fieldlistp) = 0;
5351 else
5352 fieldlistp = &TREE_CHAIN (*fieldlistp);
5355 /* Now we have the truly final field list.
5356 Store it in this type and in the variants. */
5358 TYPE_FIELDS (t) = fieldlist;
5360 /* If there are lots of fields, sort so we can look through them fast.
5361 We arbitrarily consider 16 or more elts to be "a lot". */
5364 int len = 0;
5366 for (x = fieldlist; x; x = TREE_CHAIN (x))
5368 if (len > 15 || DECL_NAME (x) == NULL)
5369 break;
5370 len += 1;
5373 if (len > 15)
5375 tree *field_array;
5376 struct lang_type *space;
5377 struct sorted_fields_type *space2;
5379 len += list_length (x);
5381 /* Use the same allocation policy here that make_node uses, to
5382 ensure that this lives as long as the rest of the struct decl.
5383 All decls in an inline function need to be saved. */
5385 space = GGC_CNEW (struct lang_type);
5386 space2 = GGC_NEWVAR (struct sorted_fields_type,
5387 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5389 len = 0;
5390 space->s = space2;
5391 field_array = &space2->elts[0];
5392 for (x = fieldlist; x; x = TREE_CHAIN (x))
5394 field_array[len++] = x;
5396 /* If there is anonymous struct or union, break out of the loop. */
5397 if (DECL_NAME (x) == NULL)
5398 break;
5400 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5401 if (x == NULL)
5403 TYPE_LANG_SPECIFIC (t) = space;
5404 TYPE_LANG_SPECIFIC (t)->s->len = len;
5405 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5406 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5411 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5413 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5414 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5415 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5416 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5419 /* If this was supposed to be a transparent union, but we can't
5420 make it one, warn and turn off the flag. */
5421 if (TREE_CODE (t) == UNION_TYPE
5422 && TYPE_TRANSPARENT_UNION (t)
5423 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5425 TYPE_TRANSPARENT_UNION (t) = 0;
5426 warning (0, "union cannot be made transparent");
5429 /* If this structure or union completes the type of any previous
5430 variable declaration, lay it out and output its rtl. */
5431 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5433 x = TREE_CHAIN (x))
5435 tree decl = TREE_VALUE (x);
5436 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5437 layout_array_type (TREE_TYPE (decl));
5438 if (TREE_CODE (decl) != TYPE_DECL)
5440 layout_decl (decl, 0);
5441 if (c_dialect_objc ())
5442 objc_check_decl (decl);
5443 rest_of_decl_compilation (decl, toplevel, 0);
5444 if (!toplevel)
5445 expand_decl (decl);
5448 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5450 /* Finish debugging output for this type. */
5451 rest_of_type_compilation (t, toplevel);
5453 /* If we're inside a function proper, i.e. not file-scope and not still
5454 parsing parameters, then arrange for the size of a variable sized type
5455 to be bound now. */
5456 if (cur_stmt_list && variably_modified_type_p (t, NULL))
5457 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5459 return t;
5462 /* Lay out the type T, and its element type, and so on. */
5464 static void
5465 layout_array_type (tree t)
5467 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5468 layout_array_type (TREE_TYPE (t));
5469 layout_type (t);
5472 /* Begin compiling the definition of an enumeration type.
5473 NAME is its name (or null if anonymous).
5474 Returns the type object, as yet incomplete.
5475 Also records info about it so that build_enumerator
5476 may be used to declare the individual values as they are read. */
5478 tree
5479 start_enum (tree name)
5481 tree enumtype = 0;
5483 /* If this is the real definition for a previous forward reference,
5484 fill in the contents in the same object that used to be the
5485 forward reference. */
5487 if (name != 0)
5488 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5490 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5492 enumtype = make_node (ENUMERAL_TYPE);
5493 pushtag (name, enumtype);
5496 if (C_TYPE_BEING_DEFINED (enumtype))
5497 error ("nested redefinition of %<enum %E%>", name);
5499 C_TYPE_BEING_DEFINED (enumtype) = 1;
5501 if (TYPE_VALUES (enumtype) != 0)
5503 /* This enum is a named one that has been declared already. */
5504 error ("redeclaration of %<enum %E%>", name);
5506 /* Completely replace its old definition.
5507 The old enumerators remain defined, however. */
5508 TYPE_VALUES (enumtype) = 0;
5511 enum_next_value = integer_zero_node;
5512 enum_overflow = 0;
5514 if (flag_short_enums)
5515 TYPE_PACKED (enumtype) = 1;
5517 return enumtype;
5520 /* After processing and defining all the values of an enumeration type,
5521 install their decls in the enumeration type and finish it off.
5522 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5523 and ATTRIBUTES are the specified attributes.
5524 Returns ENUMTYPE. */
5526 tree
5527 finish_enum (tree enumtype, tree values, tree attributes)
5529 tree pair, tem;
5530 tree minnode = 0, maxnode = 0;
5531 int precision, unsign;
5532 bool toplevel = (file_scope == current_scope);
5533 struct lang_type *lt;
5535 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5537 /* Calculate the maximum value of any enumerator in this type. */
5539 if (values == error_mark_node)
5540 minnode = maxnode = integer_zero_node;
5541 else
5543 minnode = maxnode = TREE_VALUE (values);
5544 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5546 tree value = TREE_VALUE (pair);
5547 if (tree_int_cst_lt (maxnode, value))
5548 maxnode = value;
5549 if (tree_int_cst_lt (value, minnode))
5550 minnode = value;
5554 /* Construct the final type of this enumeration. It is the same
5555 as one of the integral types - the narrowest one that fits, except
5556 that normally we only go as narrow as int - and signed iff any of
5557 the values are negative. */
5558 unsign = (tree_int_cst_sgn (minnode) >= 0);
5559 precision = MAX (min_precision (minnode, unsign),
5560 min_precision (maxnode, unsign));
5562 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5564 tem = c_common_type_for_size (precision, unsign);
5565 if (tem == NULL)
5567 warning (0, "enumeration values exceed range of largest integer");
5568 tem = long_long_integer_type_node;
5571 else
5572 tem = unsign ? unsigned_type_node : integer_type_node;
5574 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5575 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5576 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5577 TYPE_SIZE (enumtype) = 0;
5579 /* If the precision of the type was specific with an attribute and it
5580 was too small, give an error. Otherwise, use it. */
5581 if (TYPE_PRECISION (enumtype))
5583 if (precision > TYPE_PRECISION (enumtype))
5584 error ("specified mode too small for enumeral values");
5586 else
5587 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5589 layout_type (enumtype);
5591 if (values != error_mark_node)
5593 /* Change the type of the enumerators to be the enum type. We
5594 need to do this irrespective of the size of the enum, for
5595 proper type checking. Replace the DECL_INITIALs of the
5596 enumerators, and the value slots of the list, with copies
5597 that have the enum type; they cannot be modified in place
5598 because they may be shared (e.g. integer_zero_node) Finally,
5599 change the purpose slots to point to the names of the decls. */
5600 for (pair = values; pair; pair = TREE_CHAIN (pair))
5602 tree enu = TREE_PURPOSE (pair);
5603 tree ini = DECL_INITIAL (enu);
5605 TREE_TYPE (enu) = enumtype;
5607 /* The ISO C Standard mandates enumerators to have type int,
5608 even though the underlying type of an enum type is
5609 unspecified. Here we convert any enumerators that fit in
5610 an int to type int, to avoid promotions to unsigned types
5611 when comparing integers with enumerators that fit in the
5612 int range. When -pedantic is given, build_enumerator()
5613 would have already taken care of those that don't fit. */
5614 if (int_fits_type_p (ini, integer_type_node))
5615 tem = integer_type_node;
5616 else
5617 tem = enumtype;
5618 ini = convert (tem, ini);
5620 DECL_INITIAL (enu) = ini;
5621 TREE_PURPOSE (pair) = DECL_NAME (enu);
5622 TREE_VALUE (pair) = ini;
5625 TYPE_VALUES (enumtype) = values;
5628 /* Record the min/max values so that we can warn about bit-field
5629 enumerations that are too small for the values. */
5630 lt = GGC_CNEW (struct lang_type);
5631 lt->enum_min = minnode;
5632 lt->enum_max = maxnode;
5633 TYPE_LANG_SPECIFIC (enumtype) = lt;
5635 /* Fix up all variant types of this enum type. */
5636 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5638 if (tem == enumtype)
5639 continue;
5640 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5641 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5642 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5643 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5644 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5645 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5646 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5647 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5648 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5649 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5650 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5653 /* Finish debugging output for this type. */
5654 rest_of_type_compilation (enumtype, toplevel);
5656 return enumtype;
5659 /* Build and install a CONST_DECL for one value of the
5660 current enumeration type (one that was begun with start_enum).
5661 Return a tree-list containing the CONST_DECL and its value.
5662 Assignment of sequential values by default is handled here. */
5664 tree
5665 build_enumerator (tree name, tree value)
5667 tree decl, type;
5669 /* Validate and default VALUE. */
5671 if (value != 0)
5673 /* Don't issue more errors for error_mark_node (i.e. an
5674 undeclared identifier) - just ignore the value expression. */
5675 if (value == error_mark_node)
5676 value = 0;
5677 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5678 || TREE_CODE (value) != INTEGER_CST)
5680 error ("enumerator value for %qE is not an integer constant", name);
5681 value = 0;
5683 else
5685 value = default_conversion (value);
5686 constant_expression_warning (value);
5690 /* Default based on previous value. */
5691 /* It should no longer be possible to have NON_LVALUE_EXPR
5692 in the default. */
5693 if (value == 0)
5695 value = enum_next_value;
5696 if (enum_overflow)
5697 error ("overflow in enumeration values");
5700 if (pedantic && !int_fits_type_p (value, integer_type_node))
5702 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5703 /* XXX This causes -pedantic to change the meaning of the program.
5704 Remove? -zw 2004-03-15 */
5705 value = convert (integer_type_node, value);
5708 /* Set basis for default for next value. */
5709 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5710 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5712 /* Now create a declaration for the enum value name. */
5714 type = TREE_TYPE (value);
5715 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5716 TYPE_PRECISION (integer_type_node)),
5717 (TYPE_PRECISION (type)
5718 >= TYPE_PRECISION (integer_type_node)
5719 && TYPE_UNSIGNED (type)));
5721 decl = build_decl (CONST_DECL, name, type);
5722 DECL_INITIAL (decl) = convert (type, value);
5723 pushdecl (decl);
5725 return tree_cons (decl, value, NULL_TREE);
5729 /* Create the FUNCTION_DECL for a function definition.
5730 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5731 the declaration; they describe the function's name and the type it returns,
5732 but twisted together in a fashion that parallels the syntax of C.
5734 This function creates a binding context for the function body
5735 as well as setting up the FUNCTION_DECL in current_function_decl.
5737 Returns 1 on success. If the DECLARATOR is not suitable for a function
5738 (it defines a datum instead), we return 0, which tells
5739 yyparse to report a parse error. */
5742 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5743 tree attributes)
5745 tree decl1, old_decl;
5746 tree restype, resdecl;
5747 struct c_label_context_se *nstack_se;
5748 struct c_label_context_vm *nstack_vm;
5750 current_function_returns_value = 0; /* Assume, until we see it does. */
5751 current_function_returns_null = 0;
5752 current_function_returns_abnormally = 0;
5753 warn_about_return_type = 0;
5754 current_extern_inline = 0;
5755 c_switch_stack = NULL;
5757 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
5758 nstack_se->labels_def = NULL;
5759 nstack_se->labels_used = NULL;
5760 nstack_se->next = label_context_stack_se;
5761 label_context_stack_se = nstack_se;
5763 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
5764 nstack_vm->labels_def = NULL;
5765 nstack_vm->labels_used = NULL;
5766 nstack_vm->scope = 0;
5767 nstack_vm->next = label_context_stack_vm;
5768 label_context_stack_vm = nstack_vm;
5770 /* Indicate no valid break/continue context by setting these variables
5771 to some non-null, non-label value. We'll notice and emit the proper
5772 error message in c_finish_bc_stmt. */
5773 c_break_label = c_cont_label = size_zero_node;
5775 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5777 /* If the declarator is not suitable for a function definition,
5778 cause a syntax error. */
5779 if (decl1 == 0)
5780 return 0;
5782 decl_attributes (&decl1, attributes, 0);
5784 if (DECL_DECLARED_INLINE_P (decl1)
5785 && DECL_UNINLINABLE (decl1)
5786 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5787 warning (OPT_Wattributes, "%Jinline function %qD given attribute noinline",
5788 decl1, decl1);
5790 announce_function (decl1);
5792 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5794 error ("return type is an incomplete type");
5795 /* Make it return void instead. */
5796 TREE_TYPE (decl1)
5797 = build_function_type (void_type_node,
5798 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5801 if (warn_about_return_type)
5802 pedwarn_c99 ("return type defaults to %<int%>");
5804 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5805 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5806 DECL_INITIAL (decl1) = error_mark_node;
5808 /* If this definition isn't a prototype and we had a prototype declaration
5809 before, copy the arg type info from that prototype. */
5810 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5811 current_function_prototype_locus = UNKNOWN_LOCATION;
5812 current_function_prototype_built_in = false;
5813 current_function_prototype_arg_types = NULL_TREE;
5814 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5816 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5817 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5818 TREE_TYPE (TREE_TYPE (old_decl))))
5820 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5821 TREE_TYPE (decl1));
5822 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5823 current_function_prototype_built_in
5824 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
5825 current_function_prototype_arg_types
5826 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
5828 if (TREE_PUBLIC (decl1))
5830 /* If there is an external prototype declaration of this
5831 function, record its location but do not copy information
5832 to this decl. This may be an invisible declaration
5833 (built-in or in a scope which has finished) or simply
5834 have more refined argument types than any declaration
5835 found above. */
5836 struct c_binding *b;
5837 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
5838 if (B_IN_SCOPE (b, external_scope))
5839 break;
5840 if (b)
5842 tree ext_decl, ext_type;
5843 ext_decl = b->decl;
5844 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
5845 if (TREE_CODE (ext_type) == FUNCTION_TYPE
5846 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5847 TREE_TYPE (ext_type)))
5849 current_function_prototype_locus
5850 = DECL_SOURCE_LOCATION (ext_decl);
5851 current_function_prototype_built_in
5852 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
5853 current_function_prototype_arg_types
5854 = TYPE_ARG_TYPES (ext_type);
5860 /* Optionally warn of old-fashioned def with no previous prototype. */
5861 if (warn_strict_prototypes
5862 && old_decl != error_mark_node
5863 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5864 && C_DECL_ISNT_PROTOTYPE (old_decl))
5865 warning (0, "function declaration isn%'t a prototype");
5866 /* Optionally warn of any global def with no previous prototype. */
5867 else if (warn_missing_prototypes
5868 && old_decl != error_mark_node
5869 && TREE_PUBLIC (decl1)
5870 && !MAIN_NAME_P (DECL_NAME (decl1))
5871 && C_DECL_ISNT_PROTOTYPE (old_decl))
5872 warning (0, "%Jno previous prototype for %qD", decl1, decl1);
5873 /* Optionally warn of any def with no previous prototype
5874 if the function has already been used. */
5875 else if (warn_missing_prototypes
5876 && old_decl != 0
5877 && old_decl != error_mark_node
5878 && TREE_USED (old_decl)
5879 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5880 warning (0, "%J%qD was used with no prototype before its definition",
5881 decl1, decl1);
5882 /* Optionally warn of any global def with no previous declaration. */
5883 else if (warn_missing_declarations
5884 && TREE_PUBLIC (decl1)
5885 && old_decl == 0
5886 && !MAIN_NAME_P (DECL_NAME (decl1)))
5887 warning (0, "%Jno previous declaration for %qD", decl1, decl1);
5888 /* Optionally warn of any def with no previous declaration
5889 if the function has already been used. */
5890 else if (warn_missing_declarations
5891 && old_decl != 0
5892 && old_decl != error_mark_node
5893 && TREE_USED (old_decl)
5894 && C_DECL_IMPLICIT (old_decl))
5895 warning (0, "%J%qD was used with no declaration before its definition",
5896 decl1, decl1);
5898 /* This is a definition, not a reference.
5899 So normally clear DECL_EXTERNAL.
5900 However, `extern inline' acts like a declaration
5901 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5902 DECL_EXTERNAL (decl1) = current_extern_inline;
5904 /* This function exists in static storage.
5905 (This does not mean `static' in the C sense!) */
5906 TREE_STATIC (decl1) = 1;
5908 /* A nested function is not global. */
5909 if (current_function_decl != 0)
5910 TREE_PUBLIC (decl1) = 0;
5912 /* This is the earliest point at which we might know the assembler
5913 name of the function. Thus, if it's set before this, die horribly. */
5914 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5916 /* If #pragma weak was used, mark the decl weak now. */
5917 if (current_scope == file_scope)
5918 maybe_apply_pragma_weak (decl1);
5920 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5921 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5923 tree args;
5924 int argct = 0;
5926 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5927 != integer_type_node)
5928 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5930 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5931 args = TREE_CHAIN (args))
5933 tree type = args ? TREE_VALUE (args) : 0;
5935 if (type == void_type_node)
5936 break;
5938 ++argct;
5939 switch (argct)
5941 case 1:
5942 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5943 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5944 decl1, decl1);
5945 break;
5947 case 2:
5948 if (TREE_CODE (type) != POINTER_TYPE
5949 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5950 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5951 != char_type_node))
5952 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5953 decl1, decl1);
5954 break;
5956 case 3:
5957 if (TREE_CODE (type) != POINTER_TYPE
5958 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5959 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5960 != char_type_node))
5961 pedwarn ("%Jthird argument of %qD should probably be "
5962 "%<char **%>", decl1, decl1);
5963 break;
5967 /* It is intentional that this message does not mention the third
5968 argument because it's only mentioned in an appendix of the
5969 standard. */
5970 if (argct > 0 && (argct < 2 || argct > 3))
5971 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
5973 if (!TREE_PUBLIC (decl1))
5974 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
5977 /* Record the decl so that the function name is defined.
5978 If we already have a decl for this name, and it is a FUNCTION_DECL,
5979 use the old decl. */
5981 current_function_decl = pushdecl (decl1);
5983 push_scope ();
5984 declare_parm_level ();
5986 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5987 /* Promote the value to int before returning it. */
5988 if (c_promoting_integer_type_p (restype))
5990 /* It retains unsignedness if not really getting wider. */
5991 if (TYPE_UNSIGNED (restype)
5992 && (TYPE_PRECISION (restype)
5993 == TYPE_PRECISION (integer_type_node)))
5994 restype = unsigned_type_node;
5995 else
5996 restype = integer_type_node;
5999 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6000 DECL_ARTIFICIAL (resdecl) = 1;
6001 DECL_IGNORED_P (resdecl) = 1;
6002 DECL_RESULT (current_function_decl) = resdecl;
6004 start_fname_decls ();
6006 return 1;
6009 /* Subroutine of store_parm_decls which handles new-style function
6010 definitions (prototype format). The parms already have decls, so we
6011 need only record them as in effect and complain if any redundant
6012 old-style parm decls were written. */
6013 static void
6014 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6016 tree decl;
6018 if (current_scope->bindings)
6020 error ("%Jold-style parameter declarations in prototyped "
6021 "function definition", fndecl);
6023 /* Get rid of the old-style declarations. */
6024 pop_scope ();
6025 push_scope ();
6027 /* Don't issue this warning for nested functions, and don't issue this
6028 warning if we got here because ARG_INFO_TYPES was error_mark_node
6029 (this happens when a function definition has just an ellipsis in
6030 its parameter list). */
6031 else if (warn_traditional && !in_system_header && !current_function_scope
6032 && arg_info->types != error_mark_node)
6033 warning (0, "%Jtraditional C rejects ISO C style function definitions",
6034 fndecl);
6036 /* Now make all the parameter declarations visible in the function body.
6037 We can bypass most of the grunt work of pushdecl. */
6038 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6040 DECL_CONTEXT (decl) = current_function_decl;
6041 if (DECL_NAME (decl))
6042 bind (DECL_NAME (decl), decl, current_scope,
6043 /*invisible=*/false, /*nested=*/false);
6044 else
6045 error ("%Jparameter name omitted", decl);
6048 /* Record the parameter list in the function declaration. */
6049 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6051 /* Now make all the ancillary declarations visible, likewise. */
6052 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6054 DECL_CONTEXT (decl) = current_function_decl;
6055 if (DECL_NAME (decl))
6056 bind (DECL_NAME (decl), decl, current_scope,
6057 /*invisible=*/false, /*nested=*/false);
6060 /* And all the tag declarations. */
6061 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6062 if (TREE_PURPOSE (decl))
6063 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6064 /*invisible=*/false, /*nested=*/false);
6067 /* Subroutine of store_parm_decls which handles old-style function
6068 definitions (separate parameter list and declarations). */
6070 static void
6071 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6073 struct c_binding *b;
6074 tree parm, decl, last;
6075 tree parmids = arg_info->parms;
6077 /* We use DECL_WEAK as a flag to show which parameters have been
6078 seen already, since it is not used on PARM_DECL. */
6079 #ifdef ENABLE_CHECKING
6080 for (b = current_scope->bindings; b; b = b->prev)
6081 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
6082 #endif
6084 if (!in_system_header)
6085 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6086 fndecl);
6088 /* Match each formal parameter name with its declaration. Save each
6089 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6090 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6092 if (TREE_VALUE (parm) == 0)
6094 error ("%Jparameter name missing from parameter list", fndecl);
6095 TREE_PURPOSE (parm) = 0;
6096 continue;
6099 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6100 if (b && B_IN_CURRENT_SCOPE (b))
6102 decl = b->decl;
6103 /* If we got something other than a PARM_DECL it is an error. */
6104 if (TREE_CODE (decl) != PARM_DECL)
6105 error ("%J%qD declared as a non-parameter", decl, decl);
6106 /* If the declaration is already marked, we have a duplicate
6107 name. Complain and ignore the duplicate. */
6108 else if (DECL_WEAK (decl))
6110 error ("%Jmultiple parameters named %qD", decl, decl);
6111 TREE_PURPOSE (parm) = 0;
6112 continue;
6114 /* If the declaration says "void", complain and turn it into
6115 an int. */
6116 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6118 error ("%Jparameter %qD declared with void type", decl, decl);
6119 TREE_TYPE (decl) = integer_type_node;
6120 DECL_ARG_TYPE (decl) = integer_type_node;
6121 layout_decl (decl, 0);
6124 /* If no declaration found, default to int. */
6125 else
6127 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6128 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6129 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6130 pushdecl (decl);
6132 if (flag_isoc99)
6133 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6134 else if (extra_warnings)
6135 warning (0, "%Jtype of %qD defaults to %<int%>", decl, decl);
6138 TREE_PURPOSE (parm) = decl;
6139 DECL_WEAK (decl) = 1;
6142 /* Now examine the parms chain for incomplete declarations
6143 and declarations with no corresponding names. */
6145 for (b = current_scope->bindings; b; b = b->prev)
6147 parm = b->decl;
6148 if (TREE_CODE (parm) != PARM_DECL)
6149 continue;
6151 if (TREE_TYPE (parm) != error_mark_node
6152 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6154 error ("%Jparameter %qD has incomplete type", parm, parm);
6155 TREE_TYPE (parm) = error_mark_node;
6158 if (!DECL_WEAK (parm))
6160 error ("%Jdeclaration for parameter %qD but no such parameter",
6161 parm, parm);
6163 /* Pretend the parameter was not missing.
6164 This gets us to a standard state and minimizes
6165 further error messages. */
6166 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6170 /* Chain the declarations together in the order of the list of
6171 names. Store that chain in the function decl, replacing the
6172 list of names. Update the current scope to match. */
6173 DECL_ARGUMENTS (fndecl) = 0;
6175 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6176 if (TREE_PURPOSE (parm))
6177 break;
6178 if (parm && TREE_PURPOSE (parm))
6180 last = TREE_PURPOSE (parm);
6181 DECL_ARGUMENTS (fndecl) = last;
6182 DECL_WEAK (last) = 0;
6184 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6185 if (TREE_PURPOSE (parm))
6187 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6188 last = TREE_PURPOSE (parm);
6189 DECL_WEAK (last) = 0;
6191 TREE_CHAIN (last) = 0;
6194 /* If there was a previous prototype,
6195 set the DECL_ARG_TYPE of each argument according to
6196 the type previously specified, and report any mismatches. */
6198 if (current_function_prototype_arg_types)
6200 tree type;
6201 for (parm = DECL_ARGUMENTS (fndecl),
6202 type = current_function_prototype_arg_types;
6203 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6204 != void_type_node));
6205 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6207 if (parm == 0 || type == 0
6208 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6210 if (current_function_prototype_built_in)
6211 warning (0, "number of arguments doesn%'t match "
6212 "built-in prototype");
6213 else
6215 error ("number of arguments doesn%'t match prototype");
6216 error ("%Hprototype declaration",
6217 &current_function_prototype_locus);
6219 break;
6221 /* Type for passing arg must be consistent with that
6222 declared for the arg. ISO C says we take the unqualified
6223 type for parameters declared with qualified type. */
6224 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6225 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6227 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6228 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6230 /* Adjust argument to match prototype. E.g. a previous
6231 `int foo(float);' prototype causes
6232 `int foo(x) float x; {...}' to be treated like
6233 `int foo(float x) {...}'. This is particularly
6234 useful for argument types like uid_t. */
6235 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6237 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6238 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6239 && TYPE_PRECISION (TREE_TYPE (parm))
6240 < TYPE_PRECISION (integer_type_node))
6241 DECL_ARG_TYPE (parm) = integer_type_node;
6243 if (pedantic)
6245 /* ??? Is it possible to get here with a
6246 built-in prototype or will it always have
6247 been diagnosed as conflicting with an
6248 old-style definition and discarded? */
6249 if (current_function_prototype_built_in)
6250 warning (0, "promoted argument %qD "
6251 "doesn%'t match built-in prototype", parm);
6252 else
6254 pedwarn ("promoted argument %qD "
6255 "doesn%'t match prototype", parm);
6256 pedwarn ("%Hprototype declaration",
6257 &current_function_prototype_locus);
6261 else
6263 if (current_function_prototype_built_in)
6264 warning (0, "argument %qD doesn%'t match "
6265 "built-in prototype", parm);
6266 else
6268 error ("argument %qD doesn%'t match prototype", parm);
6269 error ("%Hprototype declaration",
6270 &current_function_prototype_locus);
6275 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6278 /* Otherwise, create a prototype that would match. */
6280 else
6282 tree actual = 0, last = 0, type;
6284 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6286 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6287 if (last)
6288 TREE_CHAIN (last) = type;
6289 else
6290 actual = type;
6291 last = type;
6293 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6294 if (last)
6295 TREE_CHAIN (last) = type;
6296 else
6297 actual = type;
6299 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6300 of the type of this function, but we need to avoid having this
6301 affect the types of other similarly-typed functions, so we must
6302 first force the generation of an identical (but separate) type
6303 node for the relevant function type. The new node we create
6304 will be a variant of the main variant of the original function
6305 type. */
6307 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6309 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6313 /* Store parameter declarations passed in ARG_INFO into the current
6314 function declaration. */
6316 void
6317 store_parm_decls_from (struct c_arg_info *arg_info)
6319 current_function_arg_info = arg_info;
6320 store_parm_decls ();
6323 /* Store the parameter declarations into the current function declaration.
6324 This is called after parsing the parameter declarations, before
6325 digesting the body of the function.
6327 For an old-style definition, construct a prototype out of the old-style
6328 parameter declarations and inject it into the function's type. */
6330 void
6331 store_parm_decls (void)
6333 tree fndecl = current_function_decl;
6334 bool proto;
6336 /* The argument information block for FNDECL. */
6337 struct c_arg_info *arg_info = current_function_arg_info;
6338 current_function_arg_info = 0;
6340 /* True if this definition is written with a prototype. Note:
6341 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6342 list in a function definition as equivalent to (void) -- an
6343 empty argument list specifies the function has no parameters,
6344 but only (void) sets up a prototype for future calls. */
6345 proto = arg_info->types != 0;
6347 if (proto)
6348 store_parm_decls_newstyle (fndecl, arg_info);
6349 else
6350 store_parm_decls_oldstyle (fndecl, arg_info);
6352 /* The next call to push_scope will be a function body. */
6354 next_is_function_body = true;
6356 /* Write a record describing this function definition to the prototypes
6357 file (if requested). */
6359 gen_aux_info_record (fndecl, 1, 0, proto);
6361 /* Initialize the RTL code for the function. */
6362 allocate_struct_function (fndecl);
6364 /* Begin the statement tree for this function. */
6365 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6367 /* ??? Insert the contents of the pending sizes list into the function
6368 to be evaluated. The only reason left to have this is
6369 void foo(int n, int array[n++])
6370 because we throw away the array type in favor of a pointer type, and
6371 thus won't naturally see the SAVE_EXPR containing the increment. All
6372 other pending sizes would be handled by gimplify_parameters. */
6374 tree t;
6375 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6376 add_stmt (TREE_VALUE (t));
6379 /* Even though we're inside a function body, we still don't want to
6380 call expand_expr to calculate the size of a variable-sized array.
6381 We haven't necessarily assigned RTL to all variables yet, so it's
6382 not safe to try to expand expressions involving them. */
6383 cfun->x_dont_save_pending_sizes_p = 1;
6386 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6387 functions. */
6389 static void
6390 c_warn_unused_result_recursively (tree fndecl)
6392 struct cgraph_node *cgn;
6394 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6395 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6397 /* Finalize all nested functions now. */
6398 cgn = cgraph_node (fndecl);
6399 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6400 c_warn_unused_result_recursively (cgn->decl);
6403 /* Finish up a function declaration and compile that function
6404 all the way to assembler language output. The free the storage
6405 for the function definition.
6407 This is called after parsing the body of the function definition. */
6409 void
6410 finish_function (void)
6412 tree fndecl = current_function_decl;
6414 label_context_stack_se = label_context_stack_se->next;
6415 label_context_stack_vm = label_context_stack_vm->next;
6417 if (TREE_CODE (fndecl) == FUNCTION_DECL
6418 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6420 tree args = DECL_ARGUMENTS (fndecl);
6421 for (; args; args = TREE_CHAIN (args))
6423 tree type = TREE_TYPE (args);
6424 if (INTEGRAL_TYPE_P (type)
6425 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6426 DECL_ARG_TYPE (args) = integer_type_node;
6430 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6431 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6433 /* Must mark the RESULT_DECL as being in this function. */
6435 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6436 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6438 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6440 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6441 != integer_type_node)
6443 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6444 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6445 if (!warn_main)
6446 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6448 else
6450 if (flag_isoc99)
6452 tree stmt = c_finish_return (integer_zero_node);
6453 #ifdef USE_MAPPED_LOCATION
6454 /* Hack. We don't want the middle-end to warn that this return
6455 is unreachable, so we mark its location as special. Using
6456 UNKNOWN_LOCATION has the problem that it gets clobbered in
6457 annotate_one_with_locus. A cleaner solution might be to
6458 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6460 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6461 #else
6462 /* Hack. We don't want the middle-end to warn that this
6463 return is unreachable, so put the statement on the
6464 special line 0. */
6465 annotate_with_file_line (stmt, input_filename, 0);
6466 #endif
6471 /* Tie off the statement tree for this function. */
6472 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6474 finish_fname_decls ();
6476 /* Complain if there's just no return statement. */
6477 if (warn_return_type
6478 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6479 && !current_function_returns_value && !current_function_returns_null
6480 /* Don't complain if we are no-return. */
6481 && !current_function_returns_abnormally
6482 /* Don't warn for main(). */
6483 && !MAIN_NAME_P (DECL_NAME (fndecl))
6484 /* Or if they didn't actually specify a return type. */
6485 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6486 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6487 inline function, as we might never be compiled separately. */
6488 && DECL_INLINE (fndecl))
6490 warning (0, "no return statement in function returning non-void");
6491 TREE_NO_WARNING (fndecl) = 1;
6494 /* With just -Wextra, complain only if function returns both with
6495 and without a value. */
6496 if (extra_warnings
6497 && current_function_returns_value
6498 && current_function_returns_null)
6499 warning (0, "this function may return with or without a value");
6501 /* Store the end of the function, so that we get good line number
6502 info for the epilogue. */
6503 cfun->function_end_locus = input_location;
6505 /* If we don't have ctors/dtors sections, and this is a static
6506 constructor or destructor, it must be recorded now. */
6507 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6508 && !targetm.have_ctors_dtors)
6509 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6510 if (DECL_STATIC_DESTRUCTOR (fndecl)
6511 && !targetm.have_ctors_dtors)
6512 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6514 /* Finalize the ELF visibility for the function. */
6515 c_determine_visibility (fndecl);
6517 /* Genericize before inlining. Delay genericizing nested functions
6518 until their parent function is genericized. Since finalizing
6519 requires GENERIC, delay that as well. */
6521 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6522 && !undef_nested_function)
6524 if (!decl_function_context (fndecl))
6526 c_genericize (fndecl);
6527 c_warn_unused_result_recursively (fndecl);
6529 /* ??? Objc emits functions after finalizing the compilation unit.
6530 This should be cleaned up later and this conditional removed. */
6531 if (cgraph_global_info_ready)
6533 c_expand_body (fndecl);
6534 return;
6537 cgraph_finalize_function (fndecl, false);
6539 else
6541 /* Register this function with cgraph just far enough to get it
6542 added to our parent's nested function list. Handy, since the
6543 C front end doesn't have such a list. */
6544 (void) cgraph_node (fndecl);
6548 if (!decl_function_context (fndecl))
6549 undef_nested_function = false;
6551 /* We're leaving the context of this function, so zap cfun.
6552 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6553 tree_rest_of_compilation. */
6554 cfun = NULL;
6555 current_function_decl = NULL;
6558 /* Generate the RTL for the body of FNDECL. */
6560 void
6561 c_expand_body (tree fndecl)
6564 if (!DECL_INITIAL (fndecl)
6565 || DECL_INITIAL (fndecl) == error_mark_node)
6566 return;
6568 tree_rest_of_compilation (fndecl);
6570 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6571 && targetm.have_ctors_dtors)
6572 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6573 DEFAULT_INIT_PRIORITY);
6574 if (DECL_STATIC_DESTRUCTOR (fndecl)
6575 && targetm.have_ctors_dtors)
6576 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6577 DEFAULT_INIT_PRIORITY);
6580 /* Check the declarations given in a for-loop for satisfying the C99
6581 constraints. */
6582 void
6583 check_for_loop_decls (void)
6585 struct c_binding *b;
6587 if (!flag_isoc99)
6589 /* If we get here, declarations have been used in a for loop without
6590 the C99 for loop scope. This doesn't make much sense, so don't
6591 allow it. */
6592 error ("%<for%> loop initial declaration used outside C99 mode");
6593 return;
6595 /* C99 subclause 6.8.5 paragraph 3:
6597 [#3] The declaration part of a for statement shall only
6598 declare identifiers for objects having storage class auto or
6599 register.
6601 It isn't clear whether, in this sentence, "identifiers" binds to
6602 "shall only declare" or to "objects" - that is, whether all identifiers
6603 declared must be identifiers for objects, or whether the restriction
6604 only applies to those that are. (A question on this in comp.std.c
6605 in November 2000 received no answer.) We implement the strictest
6606 interpretation, to avoid creating an extension which later causes
6607 problems. */
6609 for (b = current_scope->bindings; b; b = b->prev)
6611 tree id = b->id;
6612 tree decl = b->decl;
6614 if (!id)
6615 continue;
6617 switch (TREE_CODE (decl))
6619 case VAR_DECL:
6620 if (TREE_STATIC (decl))
6621 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6622 "initial declaration", decl, decl);
6623 else if (DECL_EXTERNAL (decl))
6624 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6625 "initial declaration", decl, decl);
6626 break;
6628 case RECORD_TYPE:
6629 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6630 id);
6631 break;
6632 case UNION_TYPE:
6633 error ("%<union %E%> declared in %<for%> loop initial declaration",
6634 id);
6635 break;
6636 case ENUMERAL_TYPE:
6637 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6638 id);
6639 break;
6640 default:
6641 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6642 "initial declaration", decl, decl);
6647 /* Save and reinitialize the variables
6648 used during compilation of a C function. */
6650 void
6651 c_push_function_context (struct function *f)
6653 struct language_function *p;
6654 p = GGC_NEW (struct language_function);
6655 f->language = p;
6657 p->base.x_stmt_tree = c_stmt_tree;
6658 p->x_break_label = c_break_label;
6659 p->x_cont_label = c_cont_label;
6660 p->x_switch_stack = c_switch_stack;
6661 p->arg_info = current_function_arg_info;
6662 p->returns_value = current_function_returns_value;
6663 p->returns_null = current_function_returns_null;
6664 p->returns_abnormally = current_function_returns_abnormally;
6665 p->warn_about_return_type = warn_about_return_type;
6666 p->extern_inline = current_extern_inline;
6669 /* Restore the variables used during compilation of a C function. */
6671 void
6672 c_pop_function_context (struct function *f)
6674 struct language_function *p = f->language;
6676 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6677 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6679 /* Stop pointing to the local nodes about to be freed. */
6680 /* But DECL_INITIAL must remain nonzero so we know this
6681 was an actual function definition. */
6682 DECL_INITIAL (current_function_decl) = error_mark_node;
6683 DECL_ARGUMENTS (current_function_decl) = 0;
6686 c_stmt_tree = p->base.x_stmt_tree;
6687 c_break_label = p->x_break_label;
6688 c_cont_label = p->x_cont_label;
6689 c_switch_stack = p->x_switch_stack;
6690 current_function_arg_info = p->arg_info;
6691 current_function_returns_value = p->returns_value;
6692 current_function_returns_null = p->returns_null;
6693 current_function_returns_abnormally = p->returns_abnormally;
6694 warn_about_return_type = p->warn_about_return_type;
6695 current_extern_inline = p->extern_inline;
6697 f->language = NULL;
6700 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6702 void
6703 c_dup_lang_specific_decl (tree decl)
6705 struct lang_decl *ld;
6707 if (!DECL_LANG_SPECIFIC (decl))
6708 return;
6710 ld = GGC_NEW (struct lang_decl);
6711 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6712 DECL_LANG_SPECIFIC (decl) = ld;
6715 /* The functions below are required for functionality of doing
6716 function at once processing in the C front end. Currently these
6717 functions are not called from anywhere in the C front end, but as
6718 these changes continue, that will change. */
6720 /* Returns nonzero if the current statement is a full expression,
6721 i.e. temporaries created during that statement should be destroyed
6722 at the end of the statement. */
6725 stmts_are_full_exprs_p (void)
6727 return 0;
6730 /* Returns the stmt_tree (if any) to which statements are currently
6731 being added. If there is no active statement-tree, NULL is
6732 returned. */
6734 stmt_tree
6735 current_stmt_tree (void)
6737 return &c_stmt_tree;
6740 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6741 C. */
6744 anon_aggr_type_p (tree ARG_UNUSED (node))
6746 return 0;
6749 /* Return the global value of T as a symbol. */
6751 tree
6752 identifier_global_value (tree t)
6754 struct c_binding *b;
6756 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6757 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6758 return b->decl;
6760 return 0;
6763 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6764 otherwise the name is found in ridpointers from RID_INDEX. */
6766 void
6767 record_builtin_type (enum rid rid_index, const char *name, tree type)
6769 tree id, decl;
6770 if (name == 0)
6771 id = ridpointers[(int) rid_index];
6772 else
6773 id = get_identifier (name);
6774 decl = build_decl (TYPE_DECL, id, type);
6775 pushdecl (decl);
6776 if (debug_hooks->type_decl)
6777 debug_hooks->type_decl (decl, false);
6780 /* Build the void_list_node (void_type_node having been created). */
6781 tree
6782 build_void_list_node (void)
6784 tree t = build_tree_list (NULL_TREE, void_type_node);
6785 return t;
6788 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6790 struct c_parm *
6791 build_c_parm (struct c_declspecs *specs, tree attrs,
6792 struct c_declarator *declarator)
6794 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6795 ret->specs = specs;
6796 ret->attrs = attrs;
6797 ret->declarator = declarator;
6798 return ret;
6801 /* Return a declarator with nested attributes. TARGET is the inner
6802 declarator to which these attributes apply. ATTRS are the
6803 attributes. */
6805 struct c_declarator *
6806 build_attrs_declarator (tree attrs, struct c_declarator *target)
6808 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6809 ret->kind = cdk_attrs;
6810 ret->declarator = target;
6811 ret->u.attrs = attrs;
6812 return ret;
6815 /* Return a declarator for a function with arguments specified by ARGS
6816 and return type specified by TARGET. */
6818 struct c_declarator *
6819 build_function_declarator (struct c_arg_info *args,
6820 struct c_declarator *target)
6822 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6823 ret->kind = cdk_function;
6824 ret->declarator = target;
6825 ret->u.arg_info = args;
6826 return ret;
6829 /* Return a declarator for the identifier IDENT (which may be
6830 NULL_TREE for an abstract declarator). */
6832 struct c_declarator *
6833 build_id_declarator (tree ident)
6835 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6836 ret->kind = cdk_id;
6837 ret->declarator = 0;
6838 ret->u.id = ident;
6839 /* Default value - may get reset to a more precise location. */
6840 ret->id_loc = input_location;
6841 return ret;
6844 /* Return something to represent absolute declarators containing a *.
6845 TARGET is the absolute declarator that the * contains.
6846 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6847 to apply to the pointer type. */
6849 struct c_declarator *
6850 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6851 struct c_declarator *target)
6853 tree attrs;
6854 int quals = 0;
6855 struct c_declarator *itarget = target;
6856 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6857 if (type_quals_attrs)
6859 attrs = type_quals_attrs->attrs;
6860 quals = quals_from_declspecs (type_quals_attrs);
6861 if (attrs != NULL_TREE)
6862 itarget = build_attrs_declarator (attrs, target);
6864 ret->kind = cdk_pointer;
6865 ret->declarator = itarget;
6866 ret->u.pointer_quals = quals;
6867 return ret;
6870 /* Return a pointer to a structure for an empty list of declaration
6871 specifiers. */
6873 struct c_declspecs *
6874 build_null_declspecs (void)
6876 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6877 ret->type = 0;
6878 ret->decl_attr = 0;
6879 ret->attrs = 0;
6880 ret->typespec_word = cts_none;
6881 ret->storage_class = csc_none;
6882 ret->declspecs_seen_p = false;
6883 ret->type_seen_p = false;
6884 ret->non_sc_seen_p = false;
6885 ret->typedef_p = false;
6886 ret->tag_defined_p = false;
6887 ret->explicit_signed_p = false;
6888 ret->deprecated_p = false;
6889 ret->default_int_p = false;
6890 ret->long_p = false;
6891 ret->long_long_p = false;
6892 ret->short_p = false;
6893 ret->signed_p = false;
6894 ret->unsigned_p = false;
6895 ret->complex_p = false;
6896 ret->inline_p = false;
6897 ret->thread_p = false;
6898 ret->const_p = false;
6899 ret->volatile_p = false;
6900 ret->restrict_p = false;
6901 return ret;
6904 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6905 returning SPECS. */
6907 struct c_declspecs *
6908 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6910 enum rid i;
6911 bool dupe = false;
6912 specs->non_sc_seen_p = true;
6913 specs->declspecs_seen_p = true;
6914 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6915 && C_IS_RESERVED_WORD (qual));
6916 i = C_RID_CODE (qual);
6917 switch (i)
6919 case RID_CONST:
6920 dupe = specs->const_p;
6921 specs->const_p = true;
6922 break;
6923 case RID_VOLATILE:
6924 dupe = specs->volatile_p;
6925 specs->volatile_p = true;
6926 break;
6927 case RID_RESTRICT:
6928 dupe = specs->restrict_p;
6929 specs->restrict_p = true;
6930 break;
6931 default:
6932 gcc_unreachable ();
6934 if (dupe && pedantic && !flag_isoc99)
6935 pedwarn ("duplicate %qE", qual);
6936 return specs;
6939 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6940 returning SPECS. */
6942 struct c_declspecs *
6943 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6945 tree type = spec.spec;
6946 specs->non_sc_seen_p = true;
6947 specs->declspecs_seen_p = true;
6948 specs->type_seen_p = true;
6949 if (TREE_DEPRECATED (type))
6950 specs->deprecated_p = true;
6952 /* Handle type specifier keywords. */
6953 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6955 enum rid i = C_RID_CODE (type);
6956 if (specs->type)
6958 error ("two or more data types in declaration specifiers");
6959 return specs;
6961 if ((int) i <= (int) RID_LAST_MODIFIER)
6963 /* "long", "short", "signed", "unsigned" or "_Complex". */
6964 bool dupe = false;
6965 switch (i)
6967 case RID_LONG:
6968 if (specs->long_long_p)
6970 error ("%<long long long%> is too long for GCC");
6971 break;
6973 if (specs->long_p)
6975 if (specs->typespec_word == cts_double)
6977 error ("both %<long long%> and %<double%> in "
6978 "declaration specifiers");
6979 break;
6981 if (pedantic && !flag_isoc99 && !in_system_header
6982 && warn_long_long)
6983 pedwarn ("ISO C90 does not support %<long long%>");
6984 specs->long_long_p = 1;
6985 break;
6987 if (specs->short_p)
6988 error ("both %<long%> and %<short%> in "
6989 "declaration specifiers");
6990 else if (specs->typespec_word == cts_void)
6991 error ("both %<long%> and %<void%> in "
6992 "declaration specifiers");
6993 else if (specs->typespec_word == cts_bool)
6994 error ("both %<long%> and %<_Bool%> in "
6995 "declaration specifiers");
6996 else if (specs->typespec_word == cts_char)
6997 error ("both %<long%> and %<char%> in "
6998 "declaration specifiers");
6999 else if (specs->typespec_word == cts_float)
7000 error ("both %<long%> and %<float%> in "
7001 "declaration specifiers");
7002 else
7003 specs->long_p = true;
7004 break;
7005 case RID_SHORT:
7006 dupe = specs->short_p;
7007 if (specs->long_p)
7008 error ("both %<long%> and %<short%> in "
7009 "declaration specifiers");
7010 else if (specs->typespec_word == cts_void)
7011 error ("both %<short%> and %<void%> in "
7012 "declaration specifiers");
7013 else if (specs->typespec_word == cts_bool)
7014 error ("both %<short%> and %<_Bool%> in "
7015 "declaration specifiers");
7016 else if (specs->typespec_word == cts_char)
7017 error ("both %<short%> and %<char%> in "
7018 "declaration specifiers");
7019 else if (specs->typespec_word == cts_float)
7020 error ("both %<short%> and %<float%> in "
7021 "declaration specifiers");
7022 else if (specs->typespec_word == cts_double)
7023 error ("both %<short%> and %<double%> in "
7024 "declaration specifiers");
7025 else
7026 specs->short_p = true;
7027 break;
7028 case RID_SIGNED:
7029 dupe = specs->signed_p;
7030 if (specs->unsigned_p)
7031 error ("both %<signed%> and %<unsigned%> in "
7032 "declaration specifiers");
7033 else if (specs->typespec_word == cts_void)
7034 error ("both %<signed%> and %<void%> in "
7035 "declaration specifiers");
7036 else if (specs->typespec_word == cts_bool)
7037 error ("both %<signed%> and %<_Bool%> in "
7038 "declaration specifiers");
7039 else if (specs->typespec_word == cts_float)
7040 error ("both %<signed%> and %<float%> in "
7041 "declaration specifiers");
7042 else if (specs->typespec_word == cts_double)
7043 error ("both %<signed%> and %<double%> in "
7044 "declaration specifiers");
7045 else
7046 specs->signed_p = true;
7047 break;
7048 case RID_UNSIGNED:
7049 dupe = specs->unsigned_p;
7050 if (specs->signed_p)
7051 error ("both %<signed%> and %<unsigned%> in "
7052 "declaration specifiers");
7053 else if (specs->typespec_word == cts_void)
7054 error ("both %<unsigned%> and %<void%> in "
7055 "declaration specifiers");
7056 else if (specs->typespec_word == cts_bool)
7057 error ("both %<unsigned%> and %<_Bool%> in "
7058 "declaration specifiers");
7059 else if (specs->typespec_word == cts_float)
7060 error ("both %<unsigned%> and %<float%> in "
7061 "declaration specifiers");
7062 else if (specs->typespec_word == cts_double)
7063 error ("both %<unsigned%> and %<double%> in "
7064 "declaration specifiers");
7065 else
7066 specs->unsigned_p = true;
7067 break;
7068 case RID_COMPLEX:
7069 dupe = specs->complex_p;
7070 if (pedantic && !flag_isoc99 && !in_system_header)
7071 pedwarn ("ISO C90 does not support complex types");
7072 if (specs->typespec_word == cts_void)
7073 error ("both %<complex%> and %<void%> in "
7074 "declaration specifiers");
7075 else if (specs->typespec_word == cts_bool)
7076 error ("both %<complex%> and %<_Bool%> in "
7077 "declaration specifiers");
7078 else
7079 specs->complex_p = true;
7080 break;
7081 default:
7082 gcc_unreachable ();
7085 if (dupe)
7086 error ("duplicate %qE", type);
7088 return specs;
7090 else
7092 /* "void", "_Bool", "char", "int", "float" or "double". */
7093 if (specs->typespec_word != cts_none)
7095 error ("two or more data types in declaration specifiers");
7096 return specs;
7098 switch (i)
7100 case RID_VOID:
7101 if (specs->long_p)
7102 error ("both %<long%> and %<void%> in "
7103 "declaration specifiers");
7104 else if (specs->short_p)
7105 error ("both %<short%> and %<void%> in "
7106 "declaration specifiers");
7107 else if (specs->signed_p)
7108 error ("both %<signed%> and %<void%> in "
7109 "declaration specifiers");
7110 else if (specs->unsigned_p)
7111 error ("both %<unsigned%> and %<void%> in "
7112 "declaration specifiers");
7113 else if (specs->complex_p)
7114 error ("both %<complex%> and %<void%> in "
7115 "declaration specifiers");
7116 else
7117 specs->typespec_word = cts_void;
7118 return specs;
7119 case RID_BOOL:
7120 if (specs->long_p)
7121 error ("both %<long%> and %<_Bool%> in "
7122 "declaration specifiers");
7123 else if (specs->short_p)
7124 error ("both %<short%> and %<_Bool%> in "
7125 "declaration specifiers");
7126 else if (specs->signed_p)
7127 error ("both %<signed%> and %<_Bool%> in "
7128 "declaration specifiers");
7129 else if (specs->unsigned_p)
7130 error ("both %<unsigned%> and %<_Bool%> in "
7131 "declaration specifiers");
7132 else if (specs->complex_p)
7133 error ("both %<complex%> and %<_Bool%> in "
7134 "declaration specifiers");
7135 else
7136 specs->typespec_word = cts_bool;
7137 return specs;
7138 case RID_CHAR:
7139 if (specs->long_p)
7140 error ("both %<long%> and %<char%> in "
7141 "declaration specifiers");
7142 else if (specs->short_p)
7143 error ("both %<short%> and %<char%> in "
7144 "declaration specifiers");
7145 else
7146 specs->typespec_word = cts_char;
7147 return specs;
7148 case RID_INT:
7149 specs->typespec_word = cts_int;
7150 return specs;
7151 case RID_FLOAT:
7152 if (specs->long_p)
7153 error ("both %<long%> and %<float%> in "
7154 "declaration specifiers");
7155 else if (specs->short_p)
7156 error ("both %<short%> and %<float%> in "
7157 "declaration specifiers");
7158 else if (specs->signed_p)
7159 error ("both %<signed%> and %<float%> in "
7160 "declaration specifiers");
7161 else if (specs->unsigned_p)
7162 error ("both %<unsigned%> and %<float%> in "
7163 "declaration specifiers");
7164 else
7165 specs->typespec_word = cts_float;
7166 return specs;
7167 case RID_DOUBLE:
7168 if (specs->long_long_p)
7169 error ("both %<long long%> and %<double%> in "
7170 "declaration specifiers");
7171 else if (specs->short_p)
7172 error ("both %<short%> and %<double%> in "
7173 "declaration specifiers");
7174 else if (specs->signed_p)
7175 error ("both %<signed%> and %<double%> in "
7176 "declaration specifiers");
7177 else if (specs->unsigned_p)
7178 error ("both %<unsigned%> and %<double%> in "
7179 "declaration specifiers");
7180 else
7181 specs->typespec_word = cts_double;
7182 return specs;
7183 default:
7184 /* ObjC reserved word "id", handled below. */
7185 break;
7190 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7191 form of ObjC type, cases such as "int" and "long" being handled
7192 above), a TYPE (struct, union, enum and typeof specifiers) or an
7193 ERROR_MARK. In none of these cases may there have previously
7194 been any type specifiers. */
7195 if (specs->type || specs->typespec_word != cts_none
7196 || specs->long_p || specs->short_p || specs->signed_p
7197 || specs->unsigned_p || specs->complex_p)
7198 error ("two or more data types in declaration specifiers");
7199 else if (TREE_CODE (type) == TYPE_DECL)
7201 if (TREE_TYPE (type) == error_mark_node)
7202 ; /* Allow the type to default to int to avoid cascading errors. */
7203 else
7205 specs->type = TREE_TYPE (type);
7206 specs->decl_attr = DECL_ATTRIBUTES (type);
7207 specs->typedef_p = true;
7208 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7211 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7213 tree t = lookup_name (type);
7214 if (!t || TREE_CODE (t) != TYPE_DECL)
7215 error ("%qE fails to be a typedef or built in type", type);
7216 else if (TREE_TYPE (t) == error_mark_node)
7218 else
7219 specs->type = TREE_TYPE (t);
7221 else if (TREE_CODE (type) != ERROR_MARK)
7223 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7224 specs->tag_defined_p = true;
7225 if (spec.kind == ctsk_typeof)
7226 specs->typedef_p = true;
7227 specs->type = type;
7230 return specs;
7233 /* Add the storage class specifier or function specifier SCSPEC to the
7234 declaration specifiers SPECS, returning SPECS. */
7236 struct c_declspecs *
7237 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7239 enum rid i;
7240 enum c_storage_class n = csc_none;
7241 bool dupe = false;
7242 specs->declspecs_seen_p = true;
7243 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7244 && C_IS_RESERVED_WORD (scspec));
7245 i = C_RID_CODE (scspec);
7246 if (extra_warnings && specs->non_sc_seen_p)
7247 warning (0, "%qE is not at beginning of declaration", scspec);
7248 switch (i)
7250 case RID_INLINE:
7251 /* C99 permits duplicate inline. Although of doubtful utility,
7252 it seems simplest to permit it in gnu89 mode as well, as
7253 there is also little utility in maintaining this as a
7254 difference between gnu89 and C99 inline. */
7255 dupe = false;
7256 specs->inline_p = true;
7257 break;
7258 case RID_THREAD:
7259 dupe = specs->thread_p;
7260 if (specs->storage_class == csc_auto)
7261 error ("%<__thread%> used with %<auto%>");
7262 else if (specs->storage_class == csc_register)
7263 error ("%<__thread%> used with %<register%>");
7264 else if (specs->storage_class == csc_typedef)
7265 error ("%<__thread%> used with %<typedef%>");
7266 else
7267 specs->thread_p = true;
7268 break;
7269 case RID_AUTO:
7270 n = csc_auto;
7271 break;
7272 case RID_EXTERN:
7273 n = csc_extern;
7274 /* Diagnose "__thread extern". */
7275 if (specs->thread_p)
7276 error ("%<__thread%> before %<extern%>");
7277 break;
7278 case RID_REGISTER:
7279 n = csc_register;
7280 break;
7281 case RID_STATIC:
7282 n = csc_static;
7283 /* Diagnose "__thread static". */
7284 if (specs->thread_p)
7285 error ("%<__thread%> before %<static%>");
7286 break;
7287 case RID_TYPEDEF:
7288 n = csc_typedef;
7289 break;
7290 default:
7291 gcc_unreachable ();
7293 if (n != csc_none && n == specs->storage_class)
7294 dupe = true;
7295 if (dupe)
7296 error ("duplicate %qE", scspec);
7297 if (n != csc_none)
7299 if (specs->storage_class != csc_none && n != specs->storage_class)
7301 error ("multiple storage classes in declaration specifiers");
7303 else
7305 specs->storage_class = n;
7306 if (n != csc_extern && n != csc_static && specs->thread_p)
7308 error ("%<__thread%> used with %qE", scspec);
7309 specs->thread_p = false;
7313 return specs;
7316 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7317 returning SPECS. */
7319 struct c_declspecs *
7320 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7322 specs->attrs = chainon (attrs, specs->attrs);
7323 specs->declspecs_seen_p = true;
7324 return specs;
7327 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7328 specifiers with any other type specifier to determine the resulting
7329 type. This is where ISO C checks on complex types are made, since
7330 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7331 double". */
7333 struct c_declspecs *
7334 finish_declspecs (struct c_declspecs *specs)
7336 /* If a type was specified as a whole, we have no modifiers and are
7337 done. */
7338 if (specs->type != NULL_TREE)
7340 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7341 && !specs->signed_p && !specs->unsigned_p
7342 && !specs->complex_p);
7343 return specs;
7346 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7347 has been specified, treat it as "int" unless "_Complex" is
7348 present and there are no other specifiers. If we just have
7349 "_Complex", it is equivalent to "_Complex double", but e.g.
7350 "_Complex short" is equivalent to "_Complex short int". */
7351 if (specs->typespec_word == cts_none)
7353 if (specs->long_p || specs->short_p
7354 || specs->signed_p || specs->unsigned_p)
7356 specs->typespec_word = cts_int;
7358 else if (specs->complex_p)
7360 specs->typespec_word = cts_double;
7361 if (pedantic)
7362 pedwarn ("ISO C does not support plain %<complex%> meaning "
7363 "%<double complex%>");
7365 else
7367 specs->typespec_word = cts_int;
7368 specs->default_int_p = true;
7369 /* We don't diagnose this here because grokdeclarator will
7370 give more specific diagnostics according to whether it is
7371 a function definition. */
7375 /* If "signed" was specified, record this to distinguish "int" and
7376 "signed int" in the case of a bit-field with
7377 -funsigned-bitfields. */
7378 specs->explicit_signed_p = specs->signed_p;
7380 /* Now compute the actual type. */
7381 switch (specs->typespec_word)
7383 case cts_void:
7384 gcc_assert (!specs->long_p && !specs->short_p
7385 && !specs->signed_p && !specs->unsigned_p
7386 && !specs->complex_p);
7387 specs->type = void_type_node;
7388 break;
7389 case cts_bool:
7390 gcc_assert (!specs->long_p && !specs->short_p
7391 && !specs->signed_p && !specs->unsigned_p
7392 && !specs->complex_p);
7393 specs->type = boolean_type_node;
7394 break;
7395 case cts_char:
7396 gcc_assert (!specs->long_p && !specs->short_p);
7397 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7398 if (specs->signed_p)
7399 specs->type = signed_char_type_node;
7400 else if (specs->unsigned_p)
7401 specs->type = unsigned_char_type_node;
7402 else
7403 specs->type = char_type_node;
7404 if (specs->complex_p)
7406 if (pedantic)
7407 pedwarn ("ISO C does not support complex integer types");
7408 specs->type = build_complex_type (specs->type);
7410 break;
7411 case cts_int:
7412 gcc_assert (!(specs->long_p && specs->short_p));
7413 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7414 if (specs->long_long_p)
7415 specs->type = (specs->unsigned_p
7416 ? long_long_unsigned_type_node
7417 : long_long_integer_type_node);
7418 else if (specs->long_p)
7419 specs->type = (specs->unsigned_p
7420 ? long_unsigned_type_node
7421 : long_integer_type_node);
7422 else if (specs->short_p)
7423 specs->type = (specs->unsigned_p
7424 ? short_unsigned_type_node
7425 : short_integer_type_node);
7426 else
7427 specs->type = (specs->unsigned_p
7428 ? unsigned_type_node
7429 : integer_type_node);
7430 if (specs->complex_p)
7432 if (pedantic)
7433 pedwarn ("ISO C does not support complex integer types");
7434 specs->type = build_complex_type (specs->type);
7436 break;
7437 case cts_float:
7438 gcc_assert (!specs->long_p && !specs->short_p
7439 && !specs->signed_p && !specs->unsigned_p);
7440 specs->type = (specs->complex_p
7441 ? complex_float_type_node
7442 : float_type_node);
7443 break;
7444 case cts_double:
7445 gcc_assert (!specs->long_long_p && !specs->short_p
7446 && !specs->signed_p && !specs->unsigned_p);
7447 if (specs->long_p)
7449 specs->type = (specs->complex_p
7450 ? complex_long_double_type_node
7451 : long_double_type_node);
7453 else
7455 specs->type = (specs->complex_p
7456 ? complex_double_type_node
7457 : double_type_node);
7459 break;
7460 default:
7461 gcc_unreachable ();
7464 return specs;
7467 /* Synthesize a function which calls all the global ctors or global
7468 dtors in this file. This is only used for targets which do not
7469 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7470 static void
7471 build_cdtor (int method_type, tree cdtors)
7473 tree body = 0;
7475 if (!cdtors)
7476 return;
7478 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7479 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7480 &body);
7482 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7485 /* Perform final processing on one file scope's declarations (or the
7486 external scope's declarations), GLOBALS. */
7487 static void
7488 c_write_global_declarations_1 (tree globals)
7490 size_t len = list_length (globals);
7491 tree *vec = XNEWVEC (tree, len);
7492 size_t i;
7493 tree decl;
7495 /* Process the decls in the order they were written. */
7496 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7498 vec[i] = decl;
7499 /* Check for used but undefined static functions using the C
7500 standard's definition of "used", and set TREE_NO_WARNING so
7501 that check_global_declarations doesn't repeat the check. */
7502 if (TREE_CODE (decl) == FUNCTION_DECL
7503 && DECL_INITIAL (decl) == 0
7504 && DECL_EXTERNAL (decl)
7505 && !TREE_PUBLIC (decl)
7506 && C_DECL_USED (decl))
7508 pedwarn ("%J%qF used but never defined", decl, decl);
7509 TREE_NO_WARNING (decl) = 1;
7513 wrapup_global_declarations (vec, len);
7514 check_global_declarations (vec, len);
7516 free (vec);
7519 void
7520 c_write_global_declarations (void)
7522 tree ext_block, t;
7524 /* We don't want to do this if generating a PCH. */
7525 if (pch_file)
7526 return;
7528 /* Don't waste time on further processing if -fsyntax-only or we've
7529 encountered errors. */
7530 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7531 return;
7533 /* Close the external scope. */
7534 ext_block = pop_scope ();
7535 external_scope = 0;
7536 gcc_assert (!current_scope);
7538 /* Process all file scopes in this compilation, and the external_scope,
7539 through wrapup_global_declarations and check_global_declarations. */
7540 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7541 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7542 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7544 /* Generate functions to call static constructors and destructors
7545 for targets that do not support .ctors/.dtors sections. These
7546 functions have magic names which are detected by collect2. */
7547 build_cdtor ('I', static_ctors); static_ctors = 0;
7548 build_cdtor ('D', static_dtors); static_dtors = 0;
7550 /* We're done parsing; proceed to optimize and emit assembly.
7551 FIXME: shouldn't be the front end's responsibility to call this. */
7552 cgraph_optimize ();
7555 #include "gt-c-decl.h"