2005-05-19 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / c-decl.c
blobb71838755f8f2dadb737c1582b876eb57785cc5d
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)
570 tree decl = b->decl;
572 /* Do not mess with variables that are 'static' or (already)
573 'volatile'. */
574 if (!TREE_THIS_VOLATILE (decl) && !TREE_STATIC (decl)
575 && (TREE_CODE (decl) == VAR_DECL
576 || TREE_CODE (decl) == PARM_DECL))
578 TREE_TYPE (decl)
579 = build_qualified_type (TREE_TYPE (decl),
580 (TYPE_QUALS (TREE_TYPE (decl))
581 | TYPE_QUAL_VOLATILE));
582 TREE_THIS_VOLATILE (decl) = 1;
583 TREE_SIDE_EFFECTS (decl) = 1;
584 DECL_REGISTER (decl) = 0;
585 C_DECL_REGISTER (decl) = 0;
589 /* Do not climb up past the current function. */
590 if (scope->function_body)
591 break;
595 /* Nonzero if we are currently in file scope. */
598 global_bindings_p (void)
600 return current_scope == file_scope && !c_override_global_bindings_to_false;
603 void
604 keep_next_level (void)
606 keep_next_level_flag = true;
609 /* Identify this scope as currently being filled with parameters. */
611 void
612 declare_parm_level (void)
614 current_scope->parm_flag = true;
617 void
618 push_scope (void)
620 if (next_is_function_body)
622 /* This is the transition from the parameters to the top level
623 of the function body. These are the same scope
624 (C99 6.2.1p4,6) so we do not push another scope structure.
625 next_is_function_body is set only by store_parm_decls, which
626 in turn is called when and only when we are about to
627 encounter the opening curly brace for the function body.
629 The outermost block of a function always gets a BLOCK node,
630 because the debugging output routines expect that each
631 function has at least one BLOCK. */
632 current_scope->parm_flag = false;
633 current_scope->function_body = true;
634 current_scope->keep = true;
635 current_scope->outer_function = current_function_scope;
636 current_function_scope = current_scope;
638 keep_next_level_flag = false;
639 next_is_function_body = false;
641 else
643 struct c_scope *scope;
644 if (scope_freelist)
646 scope = scope_freelist;
647 scope_freelist = scope->outer;
649 else
650 scope = GGC_CNEW (struct c_scope);
652 scope->keep = keep_next_level_flag;
653 scope->outer = current_scope;
654 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
656 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
657 possible. */
658 if (current_scope && scope->depth == 0)
660 scope->depth--;
661 sorry ("GCC supports only %u nested scopes", scope->depth);
664 current_scope = scope;
665 keep_next_level_flag = false;
669 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
671 static void
672 set_type_context (tree type, tree context)
674 for (type = TYPE_MAIN_VARIANT (type); type;
675 type = TYPE_NEXT_VARIANT (type))
676 TYPE_CONTEXT (type) = context;
679 /* Exit a scope. Restore the state of the identifier-decl mappings
680 that were in effect when this scope was entered. Return a BLOCK
681 node containing all the DECLs in this scope that are of interest
682 to debug info generation. */
684 tree
685 pop_scope (void)
687 struct c_scope *scope = current_scope;
688 tree block, context, p;
689 struct c_binding *b;
691 bool functionbody = scope->function_body;
692 bool keep = functionbody || scope->keep || scope->bindings;
694 c_end_vm_scope (scope->depth);
696 /* If appropriate, create a BLOCK to record the decls for the life
697 of this function. */
698 block = 0;
699 if (keep)
701 block = make_node (BLOCK);
702 BLOCK_SUBBLOCKS (block) = scope->blocks;
703 TREE_USED (block) = 1;
705 /* In each subblock, record that this is its superior. */
706 for (p = scope->blocks; p; p = TREE_CHAIN (p))
707 BLOCK_SUPERCONTEXT (p) = block;
709 BLOCK_VARS (block) = 0;
712 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
713 scope must be set so that they point to the appropriate
714 construct, i.e. either to the current FUNCTION_DECL node, or
715 else to the BLOCK node we just constructed.
717 Note that for tagged types whose scope is just the formal
718 parameter list for some function type specification, we can't
719 properly set their TYPE_CONTEXTs here, because we don't have a
720 pointer to the appropriate FUNCTION_TYPE node readily available
721 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
722 type nodes get set in `grokdeclarator' as soon as we have created
723 the FUNCTION_TYPE node which will represent the "scope" for these
724 "parameter list local" tagged types. */
725 if (scope->function_body)
726 context = current_function_decl;
727 else if (scope == file_scope)
729 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
730 TREE_CHAIN (file_decl) = all_translation_units;
731 all_translation_units = file_decl;
732 context = file_decl;
734 else
735 context = block;
737 /* Clear all bindings in this scope. */
738 for (b = scope->bindings; b; b = free_binding_and_advance (b))
740 p = b->decl;
741 switch (TREE_CODE (p))
743 case LABEL_DECL:
744 /* Warnings for unused labels, errors for undefined labels. */
745 if (TREE_USED (p) && !DECL_INITIAL (p))
747 error ("%Jlabel %qD used but not defined", p, p);
748 DECL_INITIAL (p) = error_mark_node;
750 else if (!TREE_USED (p) && warn_unused_label)
752 if (DECL_INITIAL (p))
753 warning (0, "%Jlabel %qD defined but not used", p, p);
754 else
755 warning (0, "%Jlabel %qD declared but not defined", p, p);
757 /* Labels go in BLOCK_VARS. */
758 TREE_CHAIN (p) = BLOCK_VARS (block);
759 BLOCK_VARS (block) = p;
760 gcc_assert (I_LABEL_BINDING (b->id) == b);
761 I_LABEL_BINDING (b->id) = b->shadowed;
762 break;
764 case ENUMERAL_TYPE:
765 case UNION_TYPE:
766 case RECORD_TYPE:
767 set_type_context (p, context);
769 /* Types may not have tag-names, in which case the type
770 appears in the bindings list with b->id NULL. */
771 if (b->id)
773 gcc_assert (I_TAG_BINDING (b->id) == b);
774 I_TAG_BINDING (b->id) = b->shadowed;
776 break;
778 case FUNCTION_DECL:
779 /* Propagate TREE_ADDRESSABLE from nested functions to their
780 containing functions. */
781 if (!TREE_ASM_WRITTEN (p)
782 && DECL_INITIAL (p) != 0
783 && TREE_ADDRESSABLE (p)
784 && DECL_ABSTRACT_ORIGIN (p) != 0
785 && DECL_ABSTRACT_ORIGIN (p) != p)
786 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
787 if (!DECL_EXTERNAL (p)
788 && DECL_INITIAL (p) == 0)
790 error ("%Jnested function %qD declared but never defined", p, p);
791 undef_nested_function = true;
793 goto common_symbol;
795 case VAR_DECL:
796 /* Warnings for unused variables. */
797 if (warn_unused_variable
798 && !TREE_USED (p)
799 && !DECL_IN_SYSTEM_HEADER (p)
800 && DECL_NAME (p)
801 && !DECL_ARTIFICIAL (p)
802 && scope != file_scope
803 && scope != external_scope)
804 warning (0, "%Junused variable %qD", p, p);
806 if (b->inner_comp)
808 error ("%Jtype of array %qD completed incompatibly with"
809 " implicit initialization", p, p);
812 /* Fall through. */
813 case TYPE_DECL:
814 case CONST_DECL:
815 common_symbol:
816 /* All of these go in BLOCK_VARS, but only if this is the
817 binding in the home scope. */
818 if (!b->nested)
820 TREE_CHAIN (p) = BLOCK_VARS (block);
821 BLOCK_VARS (block) = p;
823 /* If this is the file scope, and we are processing more
824 than one translation unit in this compilation, set
825 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
826 This makes same_translation_unit_p work, and causes
827 static declarations to be given disambiguating suffixes. */
828 if (scope == file_scope && num_in_fnames > 1)
830 DECL_CONTEXT (p) = context;
831 if (TREE_CODE (p) == TYPE_DECL)
832 set_type_context (TREE_TYPE (p), context);
835 /* Fall through. */
836 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
837 already been put there by store_parm_decls. Unused-
838 parameter warnings are handled by function.c.
839 error_mark_node obviously does not go in BLOCK_VARS and
840 does not get unused-variable warnings. */
841 case PARM_DECL:
842 case ERROR_MARK:
843 /* It is possible for a decl not to have a name. We get
844 here with b->id NULL in this case. */
845 if (b->id)
847 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
848 I_SYMBOL_BINDING (b->id) = b->shadowed;
849 if (b->shadowed && b->shadowed->type)
850 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
852 break;
854 default:
855 gcc_unreachable ();
860 /* Dispose of the block that we just made inside some higher level. */
861 if ((scope->function_body || scope == file_scope) && context)
863 DECL_INITIAL (context) = block;
864 BLOCK_SUPERCONTEXT (block) = context;
866 else if (scope->outer)
868 if (block)
869 SCOPE_LIST_APPEND (scope->outer, blocks, block);
870 /* If we did not make a block for the scope just exited, any
871 blocks made for inner scopes must be carried forward so they
872 will later become subblocks of something else. */
873 else if (scope->blocks)
874 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
877 /* Pop the current scope, and free the structure for reuse. */
878 current_scope = scope->outer;
879 if (scope->function_body)
880 current_function_scope = scope->outer_function;
882 memset (scope, 0, sizeof (struct c_scope));
883 scope->outer = scope_freelist;
884 scope_freelist = scope;
886 return block;
889 void
890 push_file_scope (void)
892 tree decl;
894 if (file_scope)
895 return;
897 push_scope ();
898 file_scope = current_scope;
900 start_fname_decls ();
902 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
903 bind (DECL_NAME (decl), decl, file_scope,
904 /*invisible=*/false, /*nested=*/true);
907 void
908 pop_file_scope (void)
910 /* In case there were missing closebraces, get us back to the global
911 binding level. */
912 while (current_scope != file_scope)
913 pop_scope ();
915 /* __FUNCTION__ is defined at file scope (""). This
916 call may not be necessary as my tests indicate it
917 still works without it. */
918 finish_fname_decls ();
920 /* This is the point to write out a PCH if we're doing that.
921 In that case we do not want to do anything else. */
922 if (pch_file)
924 c_common_write_pch ();
925 return;
928 /* Pop off the file scope and close this translation unit. */
929 pop_scope ();
930 file_scope = 0;
932 maybe_apply_pending_pragma_weaks ();
933 cgraph_finalize_compilation_unit ();
936 /* Insert BLOCK at the end of the list of subblocks of the current
937 scope. This is used when a BIND_EXPR is expanded, to handle the
938 BLOCK node inside the BIND_EXPR. */
940 void
941 insert_block (tree block)
943 TREE_USED (block) = 1;
944 SCOPE_LIST_APPEND (current_scope, blocks, block);
947 /* Push a definition or a declaration of struct, union or enum tag "name".
948 "type" should be the type node.
949 We assume that the tag "name" is not already defined.
951 Note that the definition may really be just a forward reference.
952 In that case, the TYPE_SIZE will be zero. */
954 static void
955 pushtag (tree name, tree type)
957 /* Record the identifier as the type's name if it has none. */
958 if (name && !TYPE_NAME (type))
959 TYPE_NAME (type) = name;
960 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
962 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
963 tagged type we just added to the current scope. This fake
964 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
965 to output a representation of a tagged type, and it also gives
966 us a convenient place to record the "scope start" address for the
967 tagged type. */
969 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
971 /* An approximation for now, so we can tell this is a function-scope tag.
972 This will be updated in pop_scope. */
973 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
976 /* Subroutine of compare_decls. Allow harmless mismatches in return
977 and argument types provided that the type modes match. This function
978 return a unified type given a suitable match, and 0 otherwise. */
980 static tree
981 match_builtin_function_types (tree newtype, tree oldtype)
983 tree newrettype, oldrettype;
984 tree newargs, oldargs;
985 tree trytype, tryargs;
987 /* Accept the return type of the new declaration if same modes. */
988 oldrettype = TREE_TYPE (oldtype);
989 newrettype = TREE_TYPE (newtype);
991 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
992 return 0;
994 oldargs = TYPE_ARG_TYPES (oldtype);
995 newargs = TYPE_ARG_TYPES (newtype);
996 tryargs = newargs;
998 while (oldargs || newargs)
1000 if (!oldargs
1001 || !newargs
1002 || !TREE_VALUE (oldargs)
1003 || !TREE_VALUE (newargs)
1004 || TYPE_MODE (TREE_VALUE (oldargs))
1005 != TYPE_MODE (TREE_VALUE (newargs)))
1006 return 0;
1008 oldargs = TREE_CHAIN (oldargs);
1009 newargs = TREE_CHAIN (newargs);
1012 trytype = build_function_type (newrettype, tryargs);
1013 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1016 /* Subroutine of diagnose_mismatched_decls. Check for function type
1017 mismatch involving an empty arglist vs a nonempty one and give clearer
1018 diagnostics. */
1019 static void
1020 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1021 tree newtype, tree oldtype)
1023 tree t;
1025 if (TREE_CODE (olddecl) != FUNCTION_DECL
1026 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1027 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1029 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1030 return;
1032 t = TYPE_ARG_TYPES (oldtype);
1033 if (t == 0)
1034 t = TYPE_ARG_TYPES (newtype);
1035 for (; t; t = TREE_CHAIN (t))
1037 tree type = TREE_VALUE (t);
1039 if (TREE_CHAIN (t) == 0
1040 && TYPE_MAIN_VARIANT (type) != void_type_node)
1042 inform ("a parameter list with an ellipsis can%'t match "
1043 "an empty parameter name list declaration");
1044 break;
1047 if (c_type_promotes_to (type) != type)
1049 inform ("an argument type that has a default promotion can%'t match "
1050 "an empty parameter name list declaration");
1051 break;
1056 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1057 old-style function definition, NEWDECL is a prototype declaration.
1058 Diagnose inconsistencies in the argument list. Returns TRUE if
1059 the prototype is compatible, FALSE if not. */
1060 static bool
1061 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1063 tree newargs, oldargs;
1064 int i;
1066 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1068 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1069 newargs = TYPE_ARG_TYPES (newtype);
1070 i = 1;
1072 for (;;)
1074 tree oldargtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1075 tree newargtype = TYPE_MAIN_VARIANT (TREE_VALUE (newargs));
1077 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1078 break;
1080 /* Reaching the end of just one list means the two decls don't
1081 agree on the number of arguments. */
1082 if (END_OF_ARGLIST (oldargtype))
1084 error ("%Jprototype for %qD declares more arguments "
1085 "than previous old-style definition", newdecl, newdecl);
1086 return false;
1088 else if (END_OF_ARGLIST (newargtype))
1090 error ("%Jprototype for %qD declares fewer arguments "
1091 "than previous old-style definition", newdecl, newdecl);
1092 return false;
1095 /* Type for passing arg must be consistent with that declared
1096 for the arg. */
1097 else if (!comptypes (oldargtype, newargtype))
1099 error ("%Jprototype for %qD declares argument %d"
1100 " with incompatible type",
1101 newdecl, newdecl, i);
1102 return false;
1105 oldargs = TREE_CHAIN (oldargs);
1106 newargs = TREE_CHAIN (newargs);
1107 i++;
1110 /* If we get here, no errors were found, but do issue a warning
1111 for this poor-style construct. */
1112 warning (0, "%Jprototype for %qD follows non-prototype definition",
1113 newdecl, newdecl);
1114 return true;
1115 #undef END_OF_ARGLIST
1118 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1119 first in a pair of mismatched declarations, using the diagnostic
1120 function DIAG. */
1121 static void
1122 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1124 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1126 else if (DECL_INITIAL (decl))
1127 diag (N_("%Jprevious definition of %qD was here"), decl, decl);
1128 else if (C_DECL_IMPLICIT (decl))
1129 diag (N_("%Jprevious implicit declaration of %qD was here"), decl, decl);
1130 else
1131 diag (N_("%Jprevious declaration of %qD was here"), decl, decl);
1134 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1135 Returns true if the caller should proceed to merge the two, false
1136 if OLDDECL should simply be discarded. As a side effect, issues
1137 all necessary diagnostics for invalid or poor-style combinations.
1138 If it returns true, writes the types of NEWDECL and OLDDECL to
1139 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1140 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1142 static bool
1143 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1144 tree *newtypep, tree *oldtypep)
1146 tree newtype, oldtype;
1147 bool pedwarned = false;
1148 bool warned = false;
1149 bool retval = true;
1151 /* If we have error_mark_node for either decl or type, just discard
1152 the previous decl - we're in an error cascade already. */
1153 if (olddecl == error_mark_node || newdecl == error_mark_node)
1154 return false;
1155 *oldtypep = oldtype = TREE_TYPE (olddecl);
1156 *newtypep = newtype = TREE_TYPE (newdecl);
1157 if (oldtype == error_mark_node || newtype == error_mark_node)
1158 return false;
1160 /* Two different categories of symbol altogether. This is an error
1161 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1162 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1164 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1165 && DECL_BUILT_IN (olddecl)
1166 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1168 error ("%J%qD redeclared as different kind of symbol",
1169 newdecl, newdecl);
1170 locate_old_decl (olddecl, error);
1172 else if (TREE_PUBLIC (newdecl))
1173 warning (0, "%Jbuilt-in function %qD declared as non-function",
1174 newdecl, newdecl);
1175 else if (warn_shadow)
1176 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1177 newdecl, newdecl);
1178 return false;
1181 /* Enumerators have no linkage, so may only be declared once in a
1182 given scope. */
1183 if (TREE_CODE (olddecl) == CONST_DECL)
1185 error ("%Jredeclaration of enumerator %qD", newdecl, newdecl);
1186 locate_old_decl (olddecl, error);
1187 return false;
1190 if (!comptypes (oldtype, newtype))
1192 if (TREE_CODE (olddecl) == FUNCTION_DECL
1193 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1195 /* Accept harmless mismatch in function types.
1196 This is for the ffs and fprintf builtins. */
1197 tree trytype = match_builtin_function_types (newtype, oldtype);
1199 if (trytype && comptypes (newtype, trytype))
1200 *oldtypep = oldtype = trytype;
1201 else
1203 /* If types don't match for a built-in, throw away the
1204 built-in. No point in calling locate_old_decl here, it
1205 won't print anything. */
1206 warning (0, "%Jconflicting types for built-in function %qD",
1207 newdecl, newdecl);
1208 return false;
1211 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1212 && DECL_IS_BUILTIN (olddecl))
1214 /* A conflicting function declaration for a predeclared
1215 function that isn't actually built in. Objective C uses
1216 these. The new declaration silently overrides everything
1217 but the volatility (i.e. noreturn) indication. See also
1218 below. FIXME: Make Objective C use normal builtins. */
1219 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1220 return false;
1222 /* Permit void foo (...) to match int foo (...) if the latter is
1223 the definition and implicit int was used. See
1224 c-torture/compile/920625-2.c. */
1225 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1226 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1227 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1228 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1230 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1231 /* Make sure we keep void as the return type. */
1232 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1233 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1234 pedwarned = true;
1236 /* Permit void foo (...) to match an earlier call to foo (...) with
1237 no declared type (thus, implicitly int). */
1238 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1239 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1240 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1241 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1243 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1244 /* Make sure we keep void as the return type. */
1245 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1246 pedwarned = true;
1248 else
1250 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1251 error ("%J conflicting type qualifiers for %qD", newdecl, newdecl);
1252 else
1253 error ("%Jconflicting types for %qD", newdecl, newdecl);
1254 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1255 locate_old_decl (olddecl, error);
1256 return false;
1260 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1261 but silently ignore the redeclaration if either is in a system
1262 header. (Conflicting redeclarations were handled above.) */
1263 if (TREE_CODE (newdecl) == TYPE_DECL)
1265 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1266 return true; /* Allow OLDDECL to continue in use. */
1268 error ("%Jredefinition of typedef %qD", newdecl, newdecl);
1269 locate_old_decl (olddecl, error);
1270 return false;
1273 /* Function declarations can either be 'static' or 'extern' (no
1274 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1275 can never conflict with each other on account of linkage (6.2.2p4).
1276 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1277 two definitions if one is 'extern inline' and one is not. The non-
1278 extern-inline definition supersedes the extern-inline definition. */
1279 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1281 /* If you declare a built-in function name as static, or
1282 define the built-in with an old-style definition (so we
1283 can't validate the argument list) the built-in definition is
1284 overridden, but optionally warn this was a bad choice of name. */
1285 if (DECL_BUILT_IN (olddecl)
1286 && !C_DECL_DECLARED_BUILTIN (olddecl)
1287 && (!TREE_PUBLIC (newdecl)
1288 || (DECL_INITIAL (newdecl)
1289 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1291 if (warn_shadow)
1292 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1293 newdecl, newdecl);
1294 /* Discard the old built-in function. */
1295 return false;
1298 if (DECL_INITIAL (newdecl))
1300 if (DECL_INITIAL (olddecl))
1302 /* If both decls have extern inline and are in the same TU,
1303 reject the new decl. */
1304 if (DECL_DECLARED_INLINE_P (olddecl)
1305 && DECL_EXTERNAL (olddecl)
1306 && DECL_DECLARED_INLINE_P (newdecl)
1307 && DECL_EXTERNAL (newdecl)
1308 && same_translation_unit_p (newdecl, olddecl))
1310 error ("%Jredefinition of %qD", newdecl, newdecl);
1311 locate_old_decl (olddecl, error);
1312 return false;
1314 /* If both decls have not extern inline, reject the new decl. */
1315 if (!DECL_DECLARED_INLINE_P (olddecl)
1316 && !DECL_EXTERNAL (olddecl)
1317 && !DECL_DECLARED_INLINE_P (newdecl)
1318 && !DECL_EXTERNAL (newdecl))
1320 error ("%Jredefinition of %qD", newdecl, newdecl);
1321 locate_old_decl (olddecl, error);
1322 return false;
1324 /* If the new decl is declared as extern inline, error if they are
1325 in the same TU, otherwise retain the old decl. */
1326 if (!DECL_DECLARED_INLINE_P (olddecl)
1327 && !DECL_EXTERNAL (olddecl)
1328 && DECL_DECLARED_INLINE_P (newdecl)
1329 && DECL_EXTERNAL (newdecl))
1331 if (same_translation_unit_p (newdecl, olddecl))
1333 error ("%Jredefinition of %qD", newdecl, newdecl);
1334 locate_old_decl (olddecl, error);
1335 return false;
1337 else
1338 retval = false;
1342 /* If we have a prototype after an old-style function definition,
1343 the argument types must be checked specially. */
1344 else if (DECL_INITIAL (olddecl)
1345 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1346 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1347 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1349 locate_old_decl (olddecl, error);
1350 return false;
1352 /* A non-static declaration (even an "extern") followed by a
1353 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1354 The same is true for a static forward declaration at block
1355 scope followed by a non-static declaration/definition at file
1356 scope. Static followed by non-static at the same scope is
1357 not undefined behavior, and is the most convenient way to get
1358 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1359 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1360 we do diagnose it if -Wtraditional. */
1361 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1363 /* Two exceptions to the rule. If olddecl is an extern
1364 inline, or a predeclared function that isn't actually
1365 built in, newdecl silently overrides olddecl. The latter
1366 occur only in Objective C; see also above. (FIXME: Make
1367 Objective C use normal builtins.) */
1368 if (!DECL_IS_BUILTIN (olddecl)
1369 && !(DECL_EXTERNAL (olddecl)
1370 && DECL_DECLARED_INLINE_P (olddecl)))
1372 error ("%Jstatic declaration of %qD follows "
1373 "non-static declaration", newdecl, newdecl);
1374 locate_old_decl (olddecl, error);
1376 return false;
1378 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1380 if (DECL_CONTEXT (olddecl))
1382 error ("%Jnon-static declaration of %qD follows "
1383 "static declaration", newdecl, newdecl);
1384 locate_old_decl (olddecl, error);
1385 return false;
1387 else if (warn_traditional)
1389 warning (0, "%Jnon-static declaration of %qD follows "
1390 "static declaration", newdecl, newdecl);
1391 warned = true;
1395 else if (TREE_CODE (newdecl) == VAR_DECL)
1397 /* Only variables can be thread-local, and all declarations must
1398 agree on this property. */
1399 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1401 if (DECL_THREAD_LOCAL (newdecl))
1402 error ("%Jthread-local declaration of %qD follows "
1403 "non-thread-local declaration", newdecl, newdecl);
1404 else
1405 error ("%Jnon-thread-local declaration of %qD follows "
1406 "thread-local declaration", newdecl, newdecl);
1408 locate_old_decl (olddecl, error);
1409 return false;
1412 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1413 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1415 error ("%Jredefinition of %qD", newdecl, newdecl);
1416 locate_old_decl (olddecl, error);
1417 return false;
1420 /* Objects declared at file scope: if the first declaration had
1421 external linkage (even if it was an external reference) the
1422 second must have external linkage as well, or the behavior is
1423 undefined. If the first declaration had internal linkage, then
1424 the second must too, or else be an external reference (in which
1425 case the composite declaration still has internal linkage).
1426 As for function declarations, we warn about the static-then-
1427 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1428 if (DECL_FILE_SCOPE_P (newdecl)
1429 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1431 if (DECL_EXTERNAL (newdecl))
1433 if (!DECL_FILE_SCOPE_P (olddecl))
1435 error ("%Jextern declaration of %qD follows "
1436 "declaration with no linkage", newdecl, newdecl);
1437 locate_old_decl (olddecl, error);
1438 return false;
1440 else if (warn_traditional)
1442 warning (0, "%Jnon-static declaration of %qD follows "
1443 "static declaration", newdecl, newdecl);
1444 warned = true;
1447 else
1449 if (TREE_PUBLIC (newdecl))
1450 error ("%Jnon-static declaration of %qD follows "
1451 "static declaration", newdecl, newdecl);
1452 else
1453 error ("%Jstatic declaration of %qD follows "
1454 "non-static declaration", newdecl, newdecl);
1456 locate_old_decl (olddecl, error);
1457 return false;
1460 /* Two objects with the same name declared at the same block
1461 scope must both be external references (6.7p3). */
1462 else if (!DECL_FILE_SCOPE_P (newdecl))
1464 if (DECL_EXTERNAL (newdecl))
1466 /* Extern with initializer at block scope, which will
1467 already have received an error. */
1469 else if (DECL_EXTERNAL (olddecl))
1471 error ("%Jdeclaration of %qD with no linkage follows "
1472 "extern declaration", newdecl, newdecl);
1473 locate_old_decl (olddecl, error);
1475 else
1477 error ("%Jredeclaration of %qD with no linkage",
1478 newdecl, newdecl);
1479 locate_old_decl (olddecl, error);
1482 return false;
1486 /* warnings */
1487 /* All decls must agree on a visibility. */
1488 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1489 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1491 warning (0, "%Jredeclaration of %qD with different visibility "
1492 "(old visibility preserved)", newdecl, newdecl);
1493 warned = true;
1496 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1498 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1499 if (DECL_DECLARED_INLINE_P (newdecl)
1500 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1502 warning (0, "%Jinline declaration of %qD follows "
1503 "declaration with attribute noinline", newdecl, newdecl);
1504 warned = true;
1506 else if (DECL_DECLARED_INLINE_P (olddecl)
1507 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1509 warning (0, "%Jdeclaration of %qD with attribute noinline follows "
1510 "inline declaration ", newdecl, newdecl);
1511 warned = true;
1514 /* Inline declaration after use or definition.
1515 ??? Should we still warn about this now we have unit-at-a-time
1516 mode and can get it right?
1517 Definitely don't complain if the decls are in different translation
1518 units. */
1519 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1520 && same_translation_unit_p (olddecl, newdecl))
1522 if (TREE_USED (olddecl))
1524 warning (0, "%J%qD declared inline after being called",
1525 olddecl, olddecl);
1526 warned = true;
1528 else if (DECL_INITIAL (olddecl))
1530 warning (0, "%J%qD declared inline after its definition",
1531 olddecl, olddecl);
1532 warned = true;
1536 else /* PARM_DECL, VAR_DECL */
1538 /* Redeclaration of a parameter is a constraint violation (this is
1539 not explicitly stated, but follows from C99 6.7p3 [no more than
1540 one declaration of the same identifier with no linkage in the
1541 same scope, except type tags] and 6.2.2p6 [parameters have no
1542 linkage]). We must check for a forward parameter declaration,
1543 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1544 an extension, the mandatory diagnostic for which is handled by
1545 mark_forward_parm_decls. */
1547 if (TREE_CODE (newdecl) == PARM_DECL
1548 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1550 error ("%Jredefinition of parameter %qD", newdecl, newdecl);
1551 locate_old_decl (olddecl, error);
1552 return false;
1556 /* Optional warning for completely redundant decls. */
1557 if (!warned && !pedwarned
1558 && warn_redundant_decls
1559 /* Don't warn about a function declaration followed by a
1560 definition. */
1561 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1562 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1563 /* Don't warn about redundant redeclarations of builtins. */
1564 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1565 && !DECL_BUILT_IN (newdecl)
1566 && DECL_BUILT_IN (olddecl)
1567 && !C_DECL_DECLARED_BUILTIN (olddecl))
1568 /* Don't warn about an extern followed by a definition. */
1569 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1570 /* Don't warn about forward parameter decls. */
1571 && !(TREE_CODE (newdecl) == PARM_DECL
1572 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1574 warning (0, "%Jredundant redeclaration of %qD", newdecl, newdecl);
1575 warned = true;
1578 /* Report location of previous decl/defn in a consistent manner. */
1579 if (warned || pedwarned)
1580 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1582 return retval;
1585 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1586 consistent with OLDDECL, but carries new information. Merge the
1587 new information into OLDDECL. This function issues no
1588 diagnostics. */
1590 static void
1591 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1593 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1594 && DECL_INITIAL (newdecl) != 0);
1595 int new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1596 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1597 int old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1598 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1600 /* For real parm decl following a forward decl, rechain the old decl
1601 in its new location and clear TREE_ASM_WRITTEN (it's not a
1602 forward decl anymore). */
1603 if (TREE_CODE (newdecl) == PARM_DECL
1604 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1606 struct c_binding *b, **here;
1608 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1609 if ((*here)->decl == olddecl)
1610 goto found;
1611 gcc_unreachable ();
1613 found:
1614 b = *here;
1615 *here = b->prev;
1616 b->prev = current_scope->bindings;
1617 current_scope->bindings = b;
1619 TREE_ASM_WRITTEN (olddecl) = 0;
1622 DECL_ATTRIBUTES (newdecl)
1623 = targetm.merge_decl_attributes (olddecl, newdecl);
1625 /* Merge the data types specified in the two decls. */
1626 TREE_TYPE (newdecl)
1627 = TREE_TYPE (olddecl)
1628 = composite_type (newtype, oldtype);
1630 /* Lay the type out, unless already done. */
1631 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1633 if (TREE_TYPE (newdecl) != error_mark_node)
1634 layout_type (TREE_TYPE (newdecl));
1635 if (TREE_CODE (newdecl) != FUNCTION_DECL
1636 && TREE_CODE (newdecl) != TYPE_DECL
1637 && TREE_CODE (newdecl) != CONST_DECL)
1638 layout_decl (newdecl, 0);
1640 else
1642 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1643 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1644 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1645 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1646 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1647 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1649 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1650 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1654 /* Keep the old rtl since we can safely use it. */
1655 COPY_DECL_RTL (olddecl, newdecl);
1657 /* Merge the type qualifiers. */
1658 if (TREE_READONLY (newdecl))
1659 TREE_READONLY (olddecl) = 1;
1661 if (TREE_THIS_VOLATILE (newdecl))
1663 TREE_THIS_VOLATILE (olddecl) = 1;
1664 if (TREE_CODE (newdecl) == VAR_DECL)
1665 make_var_volatile (newdecl);
1668 /* Merge deprecatedness. */
1669 if (TREE_DEPRECATED (newdecl))
1670 TREE_DEPRECATED (olddecl) = 1;
1672 /* Keep source location of definition rather than declaration and of
1673 prototype rather than non-prototype unless that prototype is
1674 built-in. */
1675 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1676 || (old_is_prototype && !new_is_prototype
1677 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1678 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1680 /* Merge the unused-warning information. */
1681 if (DECL_IN_SYSTEM_HEADER (olddecl))
1682 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1683 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1684 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1686 /* Merge the initialization information. */
1687 if (DECL_INITIAL (newdecl) == 0)
1688 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1690 /* Merge the section attribute.
1691 We want to issue an error if the sections conflict but that must be
1692 done later in decl_attributes since we are called before attributes
1693 are assigned. */
1694 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1695 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1697 /* Copy the assembler name.
1698 Currently, it can only be defined in the prototype. */
1699 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1701 /* Use visibility of whichever declaration had it specified */
1702 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1704 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1705 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1708 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1710 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1711 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1712 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1713 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1714 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1715 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1716 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1717 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1718 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1719 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1722 /* Merge the storage class information. */
1723 merge_weak (newdecl, olddecl);
1725 /* For functions, static overrides non-static. */
1726 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1728 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1729 /* This is since we don't automatically
1730 copy the attributes of NEWDECL into OLDDECL. */
1731 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1732 /* If this clears `static', clear it in the identifier too. */
1733 if (!TREE_PUBLIC (olddecl))
1734 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1736 if (DECL_EXTERNAL (newdecl))
1738 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1739 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1741 /* An extern decl does not override previous storage class. */
1742 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1743 if (!DECL_EXTERNAL (newdecl))
1745 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1746 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1749 else
1751 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1752 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1755 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1757 /* If we're redefining a function previously defined as extern
1758 inline, make sure we emit debug info for the inline before we
1759 throw it away, in case it was inlined into a function that hasn't
1760 been written out yet. */
1761 if (new_is_definition && DECL_INITIAL (olddecl))
1763 if (TREE_USED (olddecl)
1764 /* In unit-at-a-time mode we never inline re-defined extern
1765 inline functions. */
1766 && !flag_unit_at_a_time
1767 && cgraph_function_possibly_inlined_p (olddecl))
1768 (*debug_hooks->outlining_inline_function) (olddecl);
1770 /* The new defn must not be inline. */
1771 DECL_INLINE (newdecl) = 0;
1772 DECL_UNINLINABLE (newdecl) = 1;
1774 else
1776 /* If either decl says `inline', this fn is inline,
1777 unless its definition was passed already. */
1778 if (DECL_DECLARED_INLINE_P (newdecl)
1779 || DECL_DECLARED_INLINE_P (olddecl))
1780 DECL_DECLARED_INLINE_P (newdecl) = 1;
1782 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1783 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1786 if (DECL_BUILT_IN (olddecl))
1788 /* If redeclaring a builtin function, it stays built in.
1789 But it gets tagged as having been declared. */
1790 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1791 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1792 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1793 if (new_is_prototype)
1794 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1795 else
1796 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1797 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1800 /* Also preserve various other info from the definition. */
1801 if (!new_is_definition)
1803 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1804 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1805 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1806 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1807 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1809 /* Set DECL_INLINE on the declaration if we've got a body
1810 from which to instantiate. */
1811 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1813 DECL_INLINE (newdecl) = 1;
1814 DECL_ABSTRACT_ORIGIN (newdecl)
1815 = DECL_ABSTRACT_ORIGIN (olddecl);
1818 else
1820 /* If a previous declaration said inline, mark the
1821 definition as inlinable. */
1822 if (DECL_DECLARED_INLINE_P (newdecl)
1823 && !DECL_UNINLINABLE (newdecl))
1824 DECL_INLINE (newdecl) = 1;
1828 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1829 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1831 unsigned olddecl_uid = DECL_UID (olddecl);
1832 tree olddecl_context = DECL_CONTEXT (olddecl);
1834 memcpy ((char *) olddecl + sizeof (struct tree_common),
1835 (char *) newdecl + sizeof (struct tree_common),
1836 sizeof (struct tree_decl) - sizeof (struct tree_common));
1837 DECL_UID (olddecl) = olddecl_uid;
1838 DECL_CONTEXT (olddecl) = olddecl_context;
1841 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1842 so that encode_section_info has a chance to look at the new decl
1843 flags and attributes. */
1844 if (DECL_RTL_SET_P (olddecl)
1845 && (TREE_CODE (olddecl) == FUNCTION_DECL
1846 || (TREE_CODE (olddecl) == VAR_DECL
1847 && TREE_STATIC (olddecl))))
1848 make_decl_rtl (olddecl);
1851 /* Handle when a new declaration NEWDECL has the same name as an old
1852 one OLDDECL in the same binding contour. Prints an error message
1853 if appropriate.
1855 If safely possible, alter OLDDECL to look like NEWDECL, and return
1856 true. Otherwise, return false. */
1858 static bool
1859 duplicate_decls (tree newdecl, tree olddecl)
1861 tree newtype = NULL, oldtype = NULL;
1863 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1864 return false;
1866 merge_decls (newdecl, olddecl, newtype, oldtype);
1867 return true;
1871 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1872 static void
1873 warn_if_shadowing (tree new_decl)
1875 struct c_binding *b;
1877 /* Shadow warnings wanted? */
1878 if (!warn_shadow
1879 /* No shadow warnings for internally generated vars. */
1880 || DECL_IS_BUILTIN (new_decl)
1881 /* No shadow warnings for vars made for inlining. */
1882 || DECL_FROM_INLINE (new_decl)
1883 /* Don't warn about the parm names in function declarator
1884 within a function declarator. It would be nice to avoid
1885 warning in any function declarator in a declaration, as
1886 opposed to a definition, but there is no way to tell
1887 it's not a definition at this point. */
1888 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1889 return;
1891 /* Is anything being shadowed? Invisible decls do not count. */
1892 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1893 if (b->decl && b->decl != new_decl && !b->invisible)
1895 tree old_decl = b->decl;
1897 if (old_decl == error_mark_node)
1899 warning (0, "%Jdeclaration of %qD shadows previous non-variable",
1900 new_decl, new_decl);
1901 break;
1903 else if (TREE_CODE (old_decl) == PARM_DECL)
1904 warning (0, "%Jdeclaration of %qD shadows a parameter",
1905 new_decl, new_decl);
1906 else if (DECL_FILE_SCOPE_P (old_decl))
1907 warning (0, "%Jdeclaration of %qD shadows a global declaration",
1908 new_decl, new_decl);
1909 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1910 && DECL_BUILT_IN (old_decl))
1912 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1913 new_decl, new_decl);
1914 break;
1916 else
1917 warning (0, "%Jdeclaration of %qD shadows a previous local",
1918 new_decl, new_decl);
1920 warning (0, "%Jshadowed declaration is here", old_decl);
1922 break;
1927 /* Subroutine of pushdecl.
1929 X is a TYPE_DECL for a typedef statement. Create a brand new
1930 ..._TYPE node (which will be just a variant of the existing
1931 ..._TYPE node with identical properties) and then install X
1932 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1934 The whole point here is to end up with a situation where each
1935 and every ..._TYPE node the compiler creates will be uniquely
1936 associated with AT MOST one node representing a typedef name.
1937 This way, even though the compiler substitutes corresponding
1938 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1939 early on, later parts of the compiler can always do the reverse
1940 translation and get back the corresponding typedef name. For
1941 example, given:
1943 typedef struct S MY_TYPE;
1944 MY_TYPE object;
1946 Later parts of the compiler might only know that `object' was of
1947 type `struct S' if it were not for code just below. With this
1948 code however, later parts of the compiler see something like:
1950 struct S' == struct S
1951 typedef struct S' MY_TYPE;
1952 struct S' object;
1954 And they can then deduce (from the node for type struct S') that
1955 the original object declaration was:
1957 MY_TYPE object;
1959 Being able to do this is important for proper support of protoize,
1960 and also for generating precise symbolic debugging information
1961 which takes full account of the programmer's (typedef) vocabulary.
1963 Obviously, we don't want to generate a duplicate ..._TYPE node if
1964 the TYPE_DECL node that we are now processing really represents a
1965 standard built-in type.
1967 Since all standard types are effectively declared at line zero
1968 in the source file, we can easily check to see if we are working
1969 on a standard type by checking the current value of lineno. */
1971 static void
1972 clone_underlying_type (tree x)
1974 if (DECL_IS_BUILTIN (x))
1976 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1977 TYPE_NAME (TREE_TYPE (x)) = x;
1979 else if (TREE_TYPE (x) != error_mark_node
1980 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1982 tree tt = TREE_TYPE (x);
1983 DECL_ORIGINAL_TYPE (x) = tt;
1984 tt = build_variant_type_copy (tt);
1985 TYPE_NAME (tt) = x;
1986 TREE_USED (tt) = TREE_USED (x);
1987 TREE_TYPE (x) = tt;
1991 /* Record a decl-node X as belonging to the current lexical scope.
1992 Check for errors (such as an incompatible declaration for the same
1993 name already seen in the same scope).
1995 Returns either X or an old decl for the same name.
1996 If an old decl is returned, it may have been smashed
1997 to agree with what X says. */
1999 tree
2000 pushdecl (tree x)
2002 tree name = DECL_NAME (x);
2003 struct c_scope *scope = current_scope;
2004 struct c_binding *b;
2005 bool nested = false;
2007 /* Functions need the lang_decl data. */
2008 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
2009 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2011 /* Must set DECL_CONTEXT for everything not at file scope or
2012 DECL_FILE_SCOPE_P won't work. Local externs don't count
2013 unless they have initializers (which generate code). */
2014 if (current_function_decl
2015 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2016 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2017 DECL_CONTEXT (x) = current_function_decl;
2019 /* If this is of variably modified type, prevent jumping into its
2020 scope. */
2021 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2022 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2023 c_begin_vm_scope (scope->depth);
2025 /* Anonymous decls are just inserted in the scope. */
2026 if (!name)
2028 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2029 return x;
2032 /* First, see if there is another declaration with the same name in
2033 the current scope. If there is, duplicate_decls may do all the
2034 work for us. If duplicate_decls returns false, that indicates
2035 two incompatible decls in the same scope; we are to silently
2036 replace the old one (duplicate_decls has issued all appropriate
2037 diagnostics). In particular, we should not consider possible
2038 duplicates in the external scope, or shadowing. */
2039 b = I_SYMBOL_BINDING (name);
2040 if (b && B_IN_SCOPE (b, scope))
2042 struct c_binding *b_ext, *b_use;
2043 tree type = TREE_TYPE (x);
2044 tree visdecl = b->decl;
2045 tree vistype = TREE_TYPE (visdecl);
2046 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2047 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2048 b->inner_comp = false;
2049 b_use = b;
2050 b_ext = b;
2051 /* If this is an external linkage declaration, we should check
2052 for compatibility with the type in the external scope before
2053 setting the type at this scope based on the visible
2054 information only. */
2055 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2057 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2058 b_ext = b_ext->shadowed;
2059 if (b_ext)
2061 b_use = b_ext;
2062 if (b_use->type)
2063 TREE_TYPE (b_use->decl) = b_use->type;
2066 if (duplicate_decls (x, b_use->decl))
2068 if (b_use != b)
2070 /* Save the updated type in the external scope and
2071 restore the proper type for this scope. */
2072 tree thistype;
2073 if (comptypes (vistype, type))
2074 thistype = composite_type (vistype, type);
2075 else
2076 thistype = TREE_TYPE (b_use->decl);
2077 b_use->type = TREE_TYPE (b_use->decl);
2078 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2079 && DECL_BUILT_IN (b_use->decl))
2080 thistype
2081 = build_type_attribute_variant (thistype,
2082 TYPE_ATTRIBUTES
2083 (b_use->type));
2084 TREE_TYPE (b_use->decl) = thistype;
2086 return b_use->decl;
2088 else
2089 goto skip_external_and_shadow_checks;
2092 /* All declarations with external linkage, and all external
2093 references, go in the external scope, no matter what scope is
2094 current. However, the binding in that scope is ignored for
2095 purposes of normal name lookup. A separate binding structure is
2096 created in the requested scope; this governs the normal
2097 visibility of the symbol.
2099 The binding in the externals scope is used exclusively for
2100 detecting duplicate declarations of the same object, no matter
2101 what scope they are in; this is what we do here. (C99 6.2.7p2:
2102 All declarations that refer to the same object or function shall
2103 have compatible type; otherwise, the behavior is undefined.) */
2104 if (DECL_EXTERNAL (x) || scope == file_scope)
2106 tree type = TREE_TYPE (x);
2107 tree vistype = 0;
2108 tree visdecl = 0;
2109 bool type_saved = false;
2110 if (b && !B_IN_EXTERNAL_SCOPE (b)
2111 && (TREE_CODE (b->decl) == FUNCTION_DECL
2112 || TREE_CODE (b->decl) == VAR_DECL)
2113 && DECL_FILE_SCOPE_P (b->decl))
2115 visdecl = b->decl;
2116 vistype = TREE_TYPE (visdecl);
2118 if (warn_nested_externs
2119 && scope != file_scope
2120 && !DECL_IN_SYSTEM_HEADER (x))
2121 warning (0, "nested extern declaration of %qD", x);
2123 while (b && !B_IN_EXTERNAL_SCOPE (b))
2125 /* If this decl might be modified, save its type. This is
2126 done here rather than when the decl is first bound
2127 because the type may change after first binding, through
2128 being completed or through attributes being added. If we
2129 encounter multiple such decls, only the first should have
2130 its type saved; the others will already have had their
2131 proper types saved and the types will not have changed as
2132 their scopes will not have been re-entered. */
2133 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2135 b->type = TREE_TYPE (b->decl);
2136 type_saved = true;
2138 if (B_IN_FILE_SCOPE (b)
2139 && TREE_CODE (b->decl) == VAR_DECL
2140 && TREE_STATIC (b->decl)
2141 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2142 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2143 && TREE_CODE (type) == ARRAY_TYPE
2144 && TYPE_DOMAIN (type)
2145 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2146 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2148 /* Array type completed in inner scope, which should be
2149 diagnosed if the completion does not have size 1 and
2150 it does not get completed in the file scope. */
2151 b->inner_comp = true;
2153 b = b->shadowed;
2156 /* If a matching external declaration has been found, set its
2157 type to the composite of all the types of that declaration.
2158 After the consistency checks, it will be reset to the
2159 composite of the visible types only. */
2160 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2161 && b->type)
2162 TREE_TYPE (b->decl) = b->type;
2164 /* The point of the same_translation_unit_p check here is,
2165 we want to detect a duplicate decl for a construct like
2166 foo() { extern bar(); } ... static bar(); but not if
2167 they are in different translation units. In any case,
2168 the static does not go in the externals scope. */
2169 if (b
2170 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2171 && duplicate_decls (x, b->decl))
2173 tree thistype;
2174 if (vistype)
2176 if (comptypes (vistype, type))
2177 thistype = composite_type (vistype, type);
2178 else
2179 thistype = TREE_TYPE (b->decl);
2181 else
2182 thistype = type;
2183 b->type = TREE_TYPE (b->decl);
2184 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2185 thistype
2186 = build_type_attribute_variant (thistype,
2187 TYPE_ATTRIBUTES (b->type));
2188 TREE_TYPE (b->decl) = thistype;
2189 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2190 return b->decl;
2192 else if (TREE_PUBLIC (x))
2194 if (visdecl && !b && duplicate_decls (x, visdecl))
2196 /* An external declaration at block scope referring to a
2197 visible entity with internal linkage. The composite
2198 type will already be correct for this scope, so we
2199 just need to fall through to make the declaration in
2200 this scope. */
2201 nested = true;
2202 x = visdecl;
2204 else
2206 bind (name, x, external_scope, /*invisible=*/true,
2207 /*nested=*/false);
2208 nested = true;
2213 warn_if_shadowing (x);
2215 skip_external_and_shadow_checks:
2216 if (TREE_CODE (x) == TYPE_DECL)
2217 clone_underlying_type (x);
2219 bind (name, x, scope, /*invisible=*/false, nested);
2221 /* If x's type is incomplete because it's based on a
2222 structure or union which has not yet been fully declared,
2223 attach it to that structure or union type, so we can go
2224 back and complete the variable declaration later, if the
2225 structure or union gets fully declared.
2227 If the input is erroneous, we can have error_mark in the type
2228 slot (e.g. "f(void a, ...)") - that doesn't count as an
2229 incomplete type. */
2230 if (TREE_TYPE (x) != error_mark_node
2231 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2233 tree element = TREE_TYPE (x);
2235 while (TREE_CODE (element) == ARRAY_TYPE)
2236 element = TREE_TYPE (element);
2237 element = TYPE_MAIN_VARIANT (element);
2239 if ((TREE_CODE (element) == RECORD_TYPE
2240 || TREE_CODE (element) == UNION_TYPE)
2241 && (TREE_CODE (x) != TYPE_DECL
2242 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2243 && !COMPLETE_TYPE_P (element))
2244 C_TYPE_INCOMPLETE_VARS (element)
2245 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2247 return x;
2250 /* Record X as belonging to file scope.
2251 This is used only internally by the Objective-C front end,
2252 and is limited to its needs. duplicate_decls is not called;
2253 if there is any preexisting decl for this identifier, it is an ICE. */
2255 tree
2256 pushdecl_top_level (tree x)
2258 tree name;
2259 bool nested = false;
2260 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2262 name = DECL_NAME (x);
2264 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2266 if (TREE_PUBLIC (x))
2268 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2269 nested = true;
2271 if (file_scope)
2272 bind (name, x, file_scope, /*invisible=*/false, nested);
2274 return x;
2277 static void
2278 implicit_decl_warning (tree id, tree olddecl)
2280 void (*diag) (const char *, ...);
2281 switch (mesg_implicit_function_declaration)
2283 case 0: return;
2284 case 1: diag = warning0; break;
2285 case 2: diag = error; break;
2286 default: gcc_unreachable ();
2289 diag (N_("implicit declaration of function %qE"), id);
2290 if (olddecl)
2291 locate_old_decl (olddecl, diag);
2294 /* Generate an implicit declaration for identifier FUNCTIONID as a
2295 function of type int (). */
2297 tree
2298 implicitly_declare (tree functionid)
2300 struct c_binding *b;
2301 tree decl = 0;
2302 tree asmspec_tree;
2304 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2306 if (B_IN_SCOPE (b, external_scope))
2308 decl = b->decl;
2309 break;
2313 if (decl)
2315 if (decl == error_mark_node)
2316 return decl;
2318 /* FIXME: Objective-C has weird not-really-builtin functions
2319 which are supposed to be visible automatically. They wind up
2320 in the external scope because they're pushed before the file
2321 scope gets created. Catch this here and rebind them into the
2322 file scope. */
2323 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2325 bind (functionid, decl, file_scope,
2326 /*invisible=*/false, /*nested=*/true);
2327 return decl;
2329 else
2331 tree newtype = default_function_type;
2332 if (b->type)
2333 TREE_TYPE (decl) = b->type;
2334 /* Implicit declaration of a function already declared
2335 (somehow) in a different scope, or as a built-in.
2336 If this is the first time this has happened, warn;
2337 then recycle the old declaration but with the new type. */
2338 if (!C_DECL_IMPLICIT (decl))
2340 implicit_decl_warning (functionid, decl);
2341 C_DECL_IMPLICIT (decl) = 1;
2343 if (DECL_BUILT_IN (decl))
2345 newtype = build_type_attribute_variant (newtype,
2346 TYPE_ATTRIBUTES
2347 (TREE_TYPE (decl)));
2348 if (!comptypes (newtype, TREE_TYPE (decl)))
2350 warning (0, "incompatible implicit declaration of built-in"
2351 " function %qD", decl);
2352 newtype = TREE_TYPE (decl);
2355 else
2357 if (!comptypes (newtype, TREE_TYPE (decl)))
2359 error ("incompatible implicit declaration of function %qD",
2360 decl);
2361 locate_old_decl (decl, error);
2364 b->type = TREE_TYPE (decl);
2365 TREE_TYPE (decl) = newtype;
2366 bind (functionid, decl, current_scope,
2367 /*invisible=*/false, /*nested=*/true);
2368 return decl;
2372 /* Not seen before. */
2373 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2374 DECL_EXTERNAL (decl) = 1;
2375 TREE_PUBLIC (decl) = 1;
2376 C_DECL_IMPLICIT (decl) = 1;
2377 implicit_decl_warning (functionid, 0);
2378 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2379 if (asmspec_tree)
2380 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2382 /* C89 says implicit declarations are in the innermost block.
2383 So we record the decl in the standard fashion. */
2384 decl = pushdecl (decl);
2386 /* No need to call objc_check_decl here - it's a function type. */
2387 rest_of_decl_compilation (decl, 0, 0);
2389 /* Write a record describing this implicit function declaration
2390 to the prototypes file (if requested). */
2391 gen_aux_info_record (decl, 0, 1, 0);
2393 /* Possibly apply some default attributes to this implicit declaration. */
2394 decl_attributes (&decl, NULL_TREE, 0);
2396 return decl;
2399 /* Issue an error message for a reference to an undeclared variable
2400 ID, including a reference to a builtin outside of function-call
2401 context. Establish a binding of the identifier to error_mark_node
2402 in an appropriate scope, which will suppress further errors for the
2403 same identifier. The error message should be given location LOC. */
2404 void
2405 undeclared_variable (tree id, location_t loc)
2407 static bool already = false;
2408 struct c_scope *scope;
2410 if (current_function_decl == 0)
2412 error ("%H%qE undeclared here (not in a function)", &loc, id);
2413 scope = current_scope;
2415 else
2417 error ("%H%qE undeclared (first use in this function)", &loc, id);
2419 if (!already)
2421 error ("%H(Each undeclared identifier is reported only once", &loc);
2422 error ("%Hfor each function it appears in.)", &loc);
2423 already = true;
2426 /* If we are parsing old-style parameter decls, current_function_decl
2427 will be nonnull but current_function_scope will be null. */
2428 scope = current_function_scope ? current_function_scope : current_scope;
2430 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2433 /* Subroutine of lookup_label, declare_label, define_label: construct a
2434 LABEL_DECL with all the proper frills. */
2436 static tree
2437 make_label (tree name, location_t location)
2439 tree label = build_decl (LABEL_DECL, name, void_type_node);
2441 DECL_CONTEXT (label) = current_function_decl;
2442 DECL_MODE (label) = VOIDmode;
2443 DECL_SOURCE_LOCATION (label) = location;
2445 return label;
2448 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2449 Create one if none exists so far for the current function.
2450 This is called when a label is used in a goto expression or
2451 has its address taken. */
2453 tree
2454 lookup_label (tree name)
2456 tree label;
2458 if (current_function_decl == 0)
2460 error ("label %qE referenced outside of any function", name);
2461 return 0;
2464 /* Use a label already defined or ref'd with this name, but not if
2465 it is inherited from a containing function and wasn't declared
2466 using __label__. */
2467 label = I_LABEL_DECL (name);
2468 if (label && (DECL_CONTEXT (label) == current_function_decl
2469 || C_DECLARED_LABEL_FLAG (label)))
2471 /* If the label has only been declared, update its apparent
2472 location to point here, for better diagnostics if it
2473 turns out not to have been defined. */
2474 if (!TREE_USED (label))
2475 DECL_SOURCE_LOCATION (label) = input_location;
2476 return label;
2479 /* No label binding for that identifier; make one. */
2480 label = make_label (name, input_location);
2482 /* Ordinary labels go in the current function scope. */
2483 bind (name, label, current_function_scope,
2484 /*invisible=*/false, /*nested=*/false);
2485 return label;
2488 /* Make a label named NAME in the current function, shadowing silently
2489 any that may be inherited from containing functions or containing
2490 scopes. This is called for __label__ declarations. */
2492 tree
2493 declare_label (tree name)
2495 struct c_binding *b = I_LABEL_BINDING (name);
2496 tree label;
2498 /* Check to make sure that the label hasn't already been declared
2499 at this scope */
2500 if (b && B_IN_CURRENT_SCOPE (b))
2502 error ("duplicate label declaration %qE", name);
2503 locate_old_decl (b->decl, error);
2505 /* Just use the previous declaration. */
2506 return b->decl;
2509 label = make_label (name, input_location);
2510 C_DECLARED_LABEL_FLAG (label) = 1;
2512 /* Declared labels go in the current scope. */
2513 bind (name, label, current_scope,
2514 /*invisible=*/false, /*nested=*/false);
2515 return label;
2518 /* Define a label, specifying the location in the source file.
2519 Return the LABEL_DECL node for the label, if the definition is valid.
2520 Otherwise return 0. */
2522 tree
2523 define_label (location_t location, tree name)
2525 /* Find any preexisting label with this name. It is an error
2526 if that label has already been defined in this function, or
2527 if there is a containing function with a declared label with
2528 the same name. */
2529 tree label = I_LABEL_DECL (name);
2530 struct c_label_list *nlist_se, *nlist_vm;
2532 if (label
2533 && ((DECL_CONTEXT (label) == current_function_decl
2534 && DECL_INITIAL (label) != 0)
2535 || (DECL_CONTEXT (label) != current_function_decl
2536 && C_DECLARED_LABEL_FLAG (label))))
2538 error ("%Hduplicate label %qD", &location, label);
2539 locate_old_decl (label, error);
2540 return 0;
2542 else if (label && DECL_CONTEXT (label) == current_function_decl)
2544 /* The label has been used or declared already in this function,
2545 but not defined. Update its location to point to this
2546 definition. */
2547 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2548 error ("%Jjump into statement expression", label);
2549 if (C_DECL_UNDEFINABLE_VM (label))
2550 error ("%Jjump into scope of identifier with variably modified type",
2551 label);
2552 DECL_SOURCE_LOCATION (label) = location;
2554 else
2556 /* No label binding for that identifier; make one. */
2557 label = make_label (name, location);
2559 /* Ordinary labels go in the current function scope. */
2560 bind (name, label, current_function_scope,
2561 /*invisible=*/false, /*nested=*/false);
2564 if (warn_traditional && !in_system_header && lookup_name (name))
2565 warning (0, "%Htraditional C lacks a separate namespace for labels, "
2566 "identifier %qE conflicts", &location, name);
2568 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2569 nlist_se->next = label_context_stack_se->labels_def;
2570 nlist_se->label = label;
2571 label_context_stack_se->labels_def = nlist_se;
2573 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2574 nlist_vm->next = label_context_stack_vm->labels_def;
2575 nlist_vm->label = label;
2576 label_context_stack_vm->labels_def = nlist_vm;
2578 /* Mark label as having been defined. */
2579 DECL_INITIAL (label) = error_mark_node;
2580 return label;
2583 /* Given NAME, an IDENTIFIER_NODE,
2584 return the structure (or union or enum) definition for that name.
2585 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2586 CODE says which kind of type the caller wants;
2587 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2588 If the wrong kind of type is found, an error is reported. */
2590 static tree
2591 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2593 struct c_binding *b = I_TAG_BINDING (name);
2594 int thislevel = 0;
2596 if (!b || !b->decl)
2597 return 0;
2599 /* We only care about whether it's in this level if
2600 thislevel_only was set or it might be a type clash. */
2601 if (thislevel_only || TREE_CODE (b->decl) != code)
2603 /* For our purposes, a tag in the external scope is the same as
2604 a tag in the file scope. (Primarily relevant to Objective-C
2605 and its builtin structure tags, which get pushed before the
2606 file scope is created.) */
2607 if (B_IN_CURRENT_SCOPE (b)
2608 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2609 thislevel = 1;
2612 if (thislevel_only && !thislevel)
2613 return 0;
2615 if (TREE_CODE (b->decl) != code)
2617 /* Definition isn't the kind we were looking for. */
2618 pending_invalid_xref = name;
2619 pending_invalid_xref_location = input_location;
2621 /* If in the same binding level as a declaration as a tag
2622 of a different type, this must not be allowed to
2623 shadow that tag, so give the error immediately.
2624 (For example, "struct foo; union foo;" is invalid.) */
2625 if (thislevel)
2626 pending_xref_error ();
2628 return b->decl;
2631 /* Print an error message now
2632 for a recent invalid struct, union or enum cross reference.
2633 We don't print them immediately because they are not invalid
2634 when used in the `struct foo;' construct for shadowing. */
2636 void
2637 pending_xref_error (void)
2639 if (pending_invalid_xref != 0)
2640 error ("%H%qE defined as wrong kind of tag",
2641 &pending_invalid_xref_location, pending_invalid_xref);
2642 pending_invalid_xref = 0;
2646 /* Look up NAME in the current scope and its superiors
2647 in the namespace of variables, functions and typedefs.
2648 Return a ..._DECL node of some kind representing its definition,
2649 or return 0 if it is undefined. */
2651 tree
2652 lookup_name (tree name)
2654 struct c_binding *b = I_SYMBOL_BINDING (name);
2655 if (b && !b->invisible)
2656 return b->decl;
2657 return 0;
2660 /* Similar to `lookup_name' but look only at the indicated scope. */
2662 static tree
2663 lookup_name_in_scope (tree name, struct c_scope *scope)
2665 struct c_binding *b;
2667 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2668 if (B_IN_SCOPE (b, scope))
2669 return b->decl;
2670 return 0;
2673 /* Create the predefined scalar types of C,
2674 and some nodes representing standard constants (0, 1, (void *) 0).
2675 Initialize the global scope.
2676 Make definitions for built-in primitive functions. */
2678 void
2679 c_init_decl_processing (void)
2681 location_t save_loc = input_location;
2683 /* Initialize reserved words for parser. */
2684 c_parse_init ();
2686 current_function_decl = 0;
2688 gcc_obstack_init (&parser_obstack);
2690 /* Make the externals scope. */
2691 push_scope ();
2692 external_scope = current_scope;
2694 /* Declarations from c_common_nodes_and_builtins must not be associated
2695 with this input file, lest we get differences between using and not
2696 using preprocessed headers. */
2697 #ifdef USE_MAPPED_LOCATION
2698 input_location = BUILTINS_LOCATION;
2699 #else
2700 input_location.file = "<built-in>";
2701 input_location.line = 0;
2702 #endif
2704 build_common_tree_nodes (flag_signed_char, false);
2706 c_common_nodes_and_builtins ();
2708 /* In C, comparisons and TRUTH_* expressions have type int. */
2709 truthvalue_type_node = integer_type_node;
2710 truthvalue_true_node = integer_one_node;
2711 truthvalue_false_node = integer_zero_node;
2713 /* Even in C99, which has a real boolean type. */
2714 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2715 boolean_type_node));
2717 input_location = save_loc;
2719 pedantic_lvalues = true;
2721 make_fname_decl = c_make_fname_decl;
2722 start_fname_decls ();
2725 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2726 decl, NAME is the initialization string and TYPE_DEP indicates whether
2727 NAME depended on the type of the function. As we don't yet implement
2728 delayed emission of static data, we mark the decl as emitted
2729 so it is not placed in the output. Anything using it must therefore pull
2730 out the STRING_CST initializer directly. FIXME. */
2732 static tree
2733 c_make_fname_decl (tree id, int type_dep)
2735 const char *name = fname_as_string (type_dep);
2736 tree decl, type, init;
2737 size_t length = strlen (name);
2739 type = build_array_type (char_type_node,
2740 build_index_type (size_int (length)));
2741 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2743 decl = build_decl (VAR_DECL, id, type);
2745 TREE_STATIC (decl) = 1;
2746 TREE_READONLY (decl) = 1;
2747 DECL_ARTIFICIAL (decl) = 1;
2749 init = build_string (length + 1, name);
2750 free ((char *) name);
2751 TREE_TYPE (init) = type;
2752 DECL_INITIAL (decl) = init;
2754 TREE_USED (decl) = 1;
2756 if (current_function_decl)
2758 DECL_CONTEXT (decl) = current_function_decl;
2759 bind (id, decl, current_function_scope,
2760 /*invisible=*/false, /*nested=*/false);
2763 finish_decl (decl, init, NULL_TREE);
2765 return decl;
2768 /* Return a definition for a builtin function named NAME and whose data type
2769 is TYPE. TYPE should be a function type with argument types.
2770 FUNCTION_CODE tells later passes how to compile calls to this function.
2771 See tree.h for its possible values.
2773 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2774 the name to be called if we can't opencode the function. If
2775 ATTRS is nonzero, use that for the function's attribute list. */
2777 tree
2778 builtin_function (const char *name, tree type, int function_code,
2779 enum built_in_class cl, const char *library_name,
2780 tree attrs)
2782 tree id = get_identifier (name);
2783 tree decl = build_decl (FUNCTION_DECL, id, type);
2784 TREE_PUBLIC (decl) = 1;
2785 DECL_EXTERNAL (decl) = 1;
2786 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2787 DECL_BUILT_IN_CLASS (decl) = cl;
2788 DECL_FUNCTION_CODE (decl) = function_code;
2789 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2790 if (library_name)
2791 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2793 /* Should never be called on a symbol with a preexisting meaning. */
2794 gcc_assert (!I_SYMBOL_BINDING (id));
2796 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2798 /* Builtins in the implementation namespace are made visible without
2799 needing to be explicitly declared. See push_file_scope. */
2800 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2802 TREE_CHAIN (decl) = visible_builtins;
2803 visible_builtins = decl;
2806 /* Possibly apply some default attributes to this built-in function. */
2807 if (attrs)
2808 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2809 else
2810 decl_attributes (&decl, NULL_TREE, 0);
2812 return decl;
2815 /* Called when a declaration is seen that contains no names to declare.
2816 If its type is a reference to a structure, union or enum inherited
2817 from a containing scope, shadow that tag name for the current scope
2818 with a forward reference.
2819 If its type defines a new named structure or union
2820 or defines an enum, it is valid but we need not do anything here.
2821 Otherwise, it is an error. */
2823 void
2824 shadow_tag (const struct c_declspecs *declspecs)
2826 shadow_tag_warned (declspecs, 0);
2829 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2830 but no pedwarn. */
2831 void
2832 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2834 bool found_tag = false;
2836 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2838 tree value = declspecs->type;
2839 enum tree_code code = TREE_CODE (value);
2841 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2842 /* Used to test also that TYPE_SIZE (value) != 0.
2843 That caused warning for `struct foo;' at top level in the file. */
2845 tree name = TYPE_NAME (value);
2846 tree t;
2848 found_tag = true;
2850 if (name == 0)
2852 if (warned != 1 && code != ENUMERAL_TYPE)
2853 /* Empty unnamed enum OK */
2855 pedwarn ("unnamed struct/union that defines no instances");
2856 warned = 1;
2859 else if (!declspecs->tag_defined_p
2860 && declspecs->storage_class != csc_none)
2862 if (warned != 1)
2863 pedwarn ("empty declaration with storage class specifier "
2864 "does not redeclare tag");
2865 warned = 1;
2866 pending_xref_error ();
2868 else if (!declspecs->tag_defined_p
2869 && (declspecs->const_p
2870 || declspecs->volatile_p
2871 || declspecs->restrict_p))
2873 if (warned != 1)
2874 pedwarn ("empty declaration with type qualifier "
2875 "does not redeclare tag");
2876 warned = 1;
2877 pending_xref_error ();
2879 else
2881 pending_invalid_xref = 0;
2882 t = lookup_tag (code, name, 1);
2884 if (t == 0)
2886 t = make_node (code);
2887 pushtag (name, t);
2891 else
2893 if (warned != 1 && !in_system_header)
2895 pedwarn ("useless type name in empty declaration");
2896 warned = 1;
2900 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2902 pedwarn ("useless type name in empty declaration");
2903 warned = 1;
2906 pending_invalid_xref = 0;
2908 if (declspecs->inline_p)
2910 error ("%<inline%> in empty declaration");
2911 warned = 1;
2914 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2916 error ("%<auto%> in file-scope empty declaration");
2917 warned = 1;
2920 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2922 error ("%<register%> in file-scope empty declaration");
2923 warned = 1;
2926 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2928 warning (0, "useless storage class specifier in empty declaration");
2929 warned = 2;
2932 if (!warned && !in_system_header && declspecs->thread_p)
2934 warning (0, "useless %<__thread%> in empty declaration");
2935 warned = 2;
2938 if (!warned && !in_system_header && (declspecs->const_p
2939 || declspecs->volatile_p
2940 || declspecs->restrict_p))
2942 warning (0, "useless type qualifier in empty declaration");
2943 warned = 2;
2946 if (warned != 1)
2948 if (!found_tag)
2949 pedwarn ("empty declaration");
2954 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2955 bits. SPECS represents declaration specifiers that the grammar
2956 only permits to contain type qualifiers and attributes. */
2959 quals_from_declspecs (const struct c_declspecs *specs)
2961 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2962 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2963 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2964 gcc_assert (!specs->type
2965 && !specs->decl_attr
2966 && specs->typespec_word == cts_none
2967 && specs->storage_class == csc_none
2968 && !specs->typedef_p
2969 && !specs->explicit_signed_p
2970 && !specs->deprecated_p
2971 && !specs->long_p
2972 && !specs->long_long_p
2973 && !specs->short_p
2974 && !specs->signed_p
2975 && !specs->unsigned_p
2976 && !specs->complex_p
2977 && !specs->inline_p
2978 && !specs->thread_p);
2979 return quals;
2982 /* Construct an array declarator. EXPR is the expression inside [], or
2983 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2984 to the pointer to which a parameter array is converted). STATIC_P is
2985 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2986 is true if the array is [*], a VLA of unspecified length which is
2987 nevertheless a complete type (not currently implemented by GCC),
2988 false otherwise. The field for the contained declarator is left to be
2989 filled in by set_array_declarator_inner. */
2991 struct c_declarator *
2992 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
2993 bool vla_unspec_p)
2995 struct c_declarator *declarator = XOBNEW (&parser_obstack,
2996 struct c_declarator);
2997 declarator->kind = cdk_array;
2998 declarator->declarator = 0;
2999 declarator->u.array.dimen = expr;
3000 if (quals)
3002 declarator->u.array.attrs = quals->attrs;
3003 declarator->u.array.quals = quals_from_declspecs (quals);
3005 else
3007 declarator->u.array.attrs = NULL_TREE;
3008 declarator->u.array.quals = 0;
3010 declarator->u.array.static_p = static_p;
3011 declarator->u.array.vla_unspec_p = vla_unspec_p;
3012 if (pedantic && !flag_isoc99)
3014 if (static_p || quals != NULL)
3015 pedwarn ("ISO C90 does not support %<static%> or type "
3016 "qualifiers in parameter array declarators");
3017 if (vla_unspec_p)
3018 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3020 if (vla_unspec_p)
3021 warning (0, "GCC does not yet properly implement %<[*]%> array declarators");
3022 return declarator;
3025 /* Set the contained declarator of an array declarator. DECL is the
3026 declarator, as constructed by build_array_declarator; INNER is what
3027 appears on the left of the []. ABSTRACT_P is true if it is an
3028 abstract declarator, false otherwise; this is used to reject static
3029 and type qualifiers in abstract declarators, where they are not in
3030 the C99 grammar (subject to possible change in DR#289). */
3032 struct c_declarator *
3033 set_array_declarator_inner (struct c_declarator *decl,
3034 struct c_declarator *inner, bool abstract_p)
3036 decl->declarator = inner;
3037 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3038 || decl->u.array.attrs != NULL_TREE
3039 || decl->u.array.static_p))
3040 error ("static or type qualifiers in abstract declarator");
3041 return decl;
3044 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3046 tree
3047 groktypename (struct c_type_name *type_name)
3049 tree type;
3050 tree attrs = type_name->specs->attrs;
3052 type_name->specs->attrs = NULL_TREE;
3054 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3055 false, NULL);
3057 /* Apply attributes. */
3058 decl_attributes (&type, attrs, 0);
3060 return type;
3063 /* Decode a declarator in an ordinary declaration or data definition.
3064 This is called as soon as the type information and variable name
3065 have been parsed, before parsing the initializer if any.
3066 Here we create the ..._DECL node, fill in its type,
3067 and put it on the list of decls for the current context.
3068 The ..._DECL node is returned as the value.
3070 Exception: for arrays where the length is not specified,
3071 the type is left null, to be filled in by `finish_decl'.
3073 Function definitions do not come here; they go to start_function
3074 instead. However, external and forward declarations of functions
3075 do go through here. Structure field declarations are done by
3076 grokfield and not through here. */
3078 tree
3079 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3080 bool initialized, tree attributes)
3082 tree decl;
3083 tree tem;
3085 /* An object declared as __attribute__((deprecated)) suppresses
3086 warnings of uses of other deprecated items. */
3087 if (lookup_attribute ("deprecated", attributes))
3088 deprecated_state = DEPRECATED_SUPPRESS;
3090 decl = grokdeclarator (declarator, declspecs,
3091 NORMAL, initialized, NULL);
3092 if (!decl)
3093 return 0;
3095 deprecated_state = DEPRECATED_NORMAL;
3097 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3098 && MAIN_NAME_P (DECL_NAME (decl)))
3099 warning (0, "%J%qD is usually a function", decl, decl);
3101 if (initialized)
3102 /* Is it valid for this decl to have an initializer at all?
3103 If not, set INITIALIZED to zero, which will indirectly
3104 tell 'finish_decl' to ignore the initializer once it is parsed. */
3105 switch (TREE_CODE (decl))
3107 case TYPE_DECL:
3108 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3109 initialized = 0;
3110 break;
3112 case FUNCTION_DECL:
3113 error ("function %qD is initialized like a variable", decl);
3114 initialized = 0;
3115 break;
3117 case PARM_DECL:
3118 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3119 error ("parameter %qD is initialized", decl);
3120 initialized = 0;
3121 break;
3123 default:
3124 /* Don't allow initializations for incomplete types except for
3125 arrays which might be completed by the initialization. */
3127 /* This can happen if the array size is an undefined macro.
3128 We already gave a warning, so we don't need another one. */
3129 if (TREE_TYPE (decl) == error_mark_node)
3130 initialized = 0;
3131 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3133 /* A complete type is ok if size is fixed. */
3135 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3136 || C_DECL_VARIABLE_SIZE (decl))
3138 error ("variable-sized object may not be initialized");
3139 initialized = 0;
3142 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3144 error ("variable %qD has initializer but incomplete type", decl);
3145 initialized = 0;
3147 else if (C_DECL_VARIABLE_SIZE (decl))
3149 /* Although C99 is unclear about whether incomplete arrays
3150 of VLAs themselves count as VLAs, it does not make
3151 sense to permit them to be initialized given that
3152 ordinary VLAs may not be initialized. */
3153 error ("variable-sized object may not be initialized");
3154 initialized = 0;
3158 if (initialized)
3160 if (current_scope == file_scope)
3161 TREE_STATIC (decl) = 1;
3163 /* Tell 'pushdecl' this is an initialized decl
3164 even though we don't yet have the initializer expression.
3165 Also tell 'finish_decl' it may store the real initializer. */
3166 DECL_INITIAL (decl) = error_mark_node;
3169 /* If this is a function declaration, write a record describing it to the
3170 prototypes file (if requested). */
3172 if (TREE_CODE (decl) == FUNCTION_DECL)
3173 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3175 /* ANSI specifies that a tentative definition which is not merged with
3176 a non-tentative definition behaves exactly like a definition with an
3177 initializer equal to zero. (Section 3.7.2)
3179 -fno-common gives strict ANSI behavior, though this tends to break
3180 a large body of code that grew up without this rule.
3182 Thread-local variables are never common, since there's no entrenched
3183 body of code to break, and it allows more efficient variable references
3184 in the presence of dynamic linking. */
3186 if (TREE_CODE (decl) == VAR_DECL
3187 && !initialized
3188 && TREE_PUBLIC (decl)
3189 && !DECL_THREAD_LOCAL (decl)
3190 && !flag_no_common)
3191 DECL_COMMON (decl) = 1;
3193 /* Set attributes here so if duplicate decl, will have proper attributes. */
3194 decl_attributes (&decl, attributes, 0);
3196 if (TREE_CODE (decl) == FUNCTION_DECL
3197 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3199 struct c_declarator *ce = declarator;
3201 if (ce->kind == cdk_pointer)
3202 ce = declarator->declarator;
3203 if (ce->kind == cdk_function)
3205 tree args = ce->u.arg_info->parms;
3206 for (; args; args = TREE_CHAIN (args))
3208 tree type = TREE_TYPE (args);
3209 if (type && INTEGRAL_TYPE_P (type)
3210 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3211 DECL_ARG_TYPE (args) = integer_type_node;
3216 if (TREE_CODE (decl) == FUNCTION_DECL
3217 && DECL_DECLARED_INLINE_P (decl)
3218 && DECL_UNINLINABLE (decl)
3219 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3220 warning (0, "%Jinline function %qD given attribute noinline", decl, decl);
3222 /* Add this decl to the current scope.
3223 TEM may equal DECL or it may be a previous decl of the same name. */
3224 tem = pushdecl (decl);
3226 if (initialized && DECL_EXTERNAL (tem))
3228 DECL_EXTERNAL (tem) = 0;
3229 TREE_STATIC (tem) = 1;
3232 return tem;
3235 /* Finish processing of a declaration;
3236 install its initial value.
3237 If the length of an array type is not known before,
3238 it must be determined now, from the initial value, or it is an error. */
3240 void
3241 finish_decl (tree decl, tree init, tree asmspec_tree)
3243 tree type = TREE_TYPE (decl);
3244 int was_incomplete = (DECL_SIZE (decl) == 0);
3245 const char *asmspec = 0;
3247 /* If a name was specified, get the string. */
3248 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3249 && DECL_FILE_SCOPE_P (decl))
3250 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3251 if (asmspec_tree)
3252 asmspec = TREE_STRING_POINTER (asmspec_tree);
3254 /* If `start_decl' didn't like having an initialization, ignore it now. */
3255 if (init != 0 && DECL_INITIAL (decl) == 0)
3256 init = 0;
3258 /* Don't crash if parm is initialized. */
3259 if (TREE_CODE (decl) == PARM_DECL)
3260 init = 0;
3262 if (init)
3263 store_init_value (decl, init);
3265 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3266 || TREE_CODE (decl) == FUNCTION_DECL
3267 || TREE_CODE (decl) == FIELD_DECL))
3268 objc_check_decl (decl);
3270 /* Deduce size of array from initialization, if not already known. */
3271 if (TREE_CODE (type) == ARRAY_TYPE
3272 && TYPE_DOMAIN (type) == 0
3273 && TREE_CODE (decl) != TYPE_DECL)
3275 bool do_default
3276 = (TREE_STATIC (decl)
3277 /* Even if pedantic, an external linkage array
3278 may have incomplete type at first. */
3279 ? pedantic && !TREE_PUBLIC (decl)
3280 : !DECL_EXTERNAL (decl));
3281 int failure
3282 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3283 do_default);
3285 /* Get the completed type made by complete_array_type. */
3286 type = TREE_TYPE (decl);
3288 switch (failure)
3290 case 1:
3291 error ("%Jinitializer fails to determine size of %qD", decl, decl);
3292 break;
3294 case 2:
3295 if (do_default)
3296 error ("%Jarray size missing in %qD", decl, decl);
3297 /* If a `static' var's size isn't known,
3298 make it extern as well as static, so it does not get
3299 allocated.
3300 If it is not `static', then do not mark extern;
3301 finish_incomplete_decl will give it a default size
3302 and it will get allocated. */
3303 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3304 DECL_EXTERNAL (decl) = 1;
3305 break;
3307 case 3:
3308 error ("%Jzero or negative size array %qD", decl, decl);
3309 break;
3311 case 0:
3312 /* For global variables, update the copy of the type that
3313 exists in the binding. */
3314 if (TREE_PUBLIC (decl))
3316 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3317 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3318 b_ext = b_ext->shadowed;
3319 if (b_ext)
3321 if (b_ext->type)
3322 b_ext->type = composite_type (b_ext->type, type);
3323 else
3324 b_ext->type = type;
3327 break;
3329 default:
3330 gcc_unreachable ();
3333 if (DECL_INITIAL (decl))
3334 TREE_TYPE (DECL_INITIAL (decl)) = type;
3336 layout_decl (decl, 0);
3339 if (TREE_CODE (decl) == VAR_DECL)
3341 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3342 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3343 layout_decl (decl, 0);
3345 if (DECL_SIZE (decl) == 0
3346 /* Don't give an error if we already gave one earlier. */
3347 && TREE_TYPE (decl) != error_mark_node
3348 && (TREE_STATIC (decl)
3349 /* A static variable with an incomplete type
3350 is an error if it is initialized.
3351 Also if it is not file scope.
3352 Otherwise, let it through, but if it is not `extern'
3353 then it may cause an error message later. */
3354 ? (DECL_INITIAL (decl) != 0
3355 || !DECL_FILE_SCOPE_P (decl))
3356 /* An automatic variable with an incomplete type
3357 is an error. */
3358 : !DECL_EXTERNAL (decl)))
3360 error ("%Jstorage size of %qD isn%'t known", decl, decl);
3361 TREE_TYPE (decl) = error_mark_node;
3364 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3365 && DECL_SIZE (decl) != 0)
3367 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3368 constant_expression_warning (DECL_SIZE (decl));
3369 else
3370 error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3373 if (TREE_USED (type))
3374 TREE_USED (decl) = 1;
3377 /* If this is a function and an assembler name is specified, reset DECL_RTL
3378 so we can give it its new name. Also, update built_in_decls if it
3379 was a normal built-in. */
3380 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3382 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3383 set_builtin_user_assembler_name (decl, asmspec);
3384 set_user_assembler_name (decl, asmspec);
3387 /* If #pragma weak was used, mark the decl weak now. */
3388 maybe_apply_pragma_weak (decl);
3390 /* If this is a variable definition, determine its ELF visibility. */
3391 if (TREE_CODE (decl) == VAR_DECL
3392 && TREE_STATIC (decl)
3393 && !DECL_EXTERNAL (decl))
3394 c_determine_visibility (decl);
3396 /* Output the assembler code and/or RTL code for variables and functions,
3397 unless the type is an undefined structure or union.
3398 If not, it will get done when the type is completed. */
3400 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3402 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3403 if (c_dialect_objc ())
3404 objc_check_decl (decl);
3406 if (asmspec)
3408 /* If this is not a static variable, issue a warning.
3409 It doesn't make any sense to give an ASMSPEC for an
3410 ordinary, non-register local variable. Historically,
3411 GCC has accepted -- but ignored -- the ASMSPEC in
3412 this case. */
3413 if (!DECL_FILE_SCOPE_P (decl)
3414 && TREE_CODE (decl) == VAR_DECL
3415 && !C_DECL_REGISTER (decl)
3416 && !TREE_STATIC (decl))
3417 warning (0, "%Jignoring asm-specifier for non-static local "
3418 "variable %qD", decl, decl);
3419 else if (C_DECL_REGISTER (decl))
3420 change_decl_assembler_name (decl, get_identifier (asmspec));
3421 else
3422 set_user_assembler_name (decl, asmspec);
3425 if (DECL_FILE_SCOPE_P (decl))
3427 if (DECL_INITIAL (decl) == NULL_TREE
3428 || DECL_INITIAL (decl) == error_mark_node)
3429 /* Don't output anything
3430 when a tentative file-scope definition is seen.
3431 But at end of compilation, do output code for them. */
3432 DECL_DEFER_OUTPUT (decl) = 1;
3433 rest_of_decl_compilation (decl, true, 0);
3435 else
3437 /* In conjunction with an ASMSPEC, the `register'
3438 keyword indicates that we should place the variable
3439 in a particular register. */
3440 if (asmspec && C_DECL_REGISTER (decl))
3442 DECL_HARD_REGISTER (decl) = 1;
3443 /* This cannot be done for a structure with volatile
3444 fields, on which DECL_REGISTER will have been
3445 reset. */
3446 if (!DECL_REGISTER (decl))
3447 error ("cannot put object with volatile field into register");
3450 if (TREE_CODE (decl) != FUNCTION_DECL)
3452 /* If we're building a variable sized type, and we might be
3453 reachable other than via the top of the current binding
3454 level, then create a new BIND_EXPR so that we deallocate
3455 the object at the right time. */
3456 /* Note that DECL_SIZE can be null due to errors. */
3457 if (DECL_SIZE (decl)
3458 && !TREE_CONSTANT (DECL_SIZE (decl))
3459 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3461 tree bind;
3462 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3463 TREE_SIDE_EFFECTS (bind) = 1;
3464 add_stmt (bind);
3465 BIND_EXPR_BODY (bind) = push_stmt_list ();
3467 add_stmt (build_stmt (DECL_EXPR, decl));
3472 if (!DECL_FILE_SCOPE_P (decl))
3474 /* Recompute the RTL of a local array now
3475 if it used to be an incomplete type. */
3476 if (was_incomplete
3477 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3479 /* If we used it already as memory, it must stay in memory. */
3480 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3481 /* If it's still incomplete now, no init will save it. */
3482 if (DECL_SIZE (decl) == 0)
3483 DECL_INITIAL (decl) = 0;
3488 /* If this was marked 'used', be sure it will be output. */
3489 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3490 mark_decl_referenced (decl);
3492 if (TREE_CODE (decl) == TYPE_DECL)
3494 if (!DECL_FILE_SCOPE_P (decl)
3495 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3496 add_stmt (build_stmt (DECL_EXPR, decl));
3498 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3501 /* At the end of a declaration, throw away any variable type sizes
3502 of types defined inside that declaration. There is no use
3503 computing them in the following function definition. */
3504 if (current_scope == file_scope)
3505 get_pending_sizes ();
3507 /* Install a cleanup (aka destructor) if one was given. */
3508 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3510 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3511 if (attr)
3513 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3514 tree cleanup_decl = lookup_name (cleanup_id);
3515 tree cleanup;
3517 /* Build "cleanup(&decl)" for the destructor. */
3518 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3519 cleanup = build_tree_list (NULL_TREE, cleanup);
3520 cleanup = build_function_call (cleanup_decl, cleanup);
3522 /* Don't warn about decl unused; the cleanup uses it. */
3523 TREE_USED (decl) = 1;
3524 TREE_USED (cleanup_decl) = 1;
3526 /* Initialize EH, if we've been told to do so. */
3527 if (flag_exceptions && !c_eh_initialized_p)
3529 c_eh_initialized_p = true;
3530 eh_personality_libfunc
3531 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3532 ? "__gcc_personality_sj0"
3533 : "__gcc_personality_v0");
3534 using_eh_for_cleanups ();
3537 push_cleanup (decl, cleanup, false);
3542 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3544 tree
3545 grokparm (const struct c_parm *parm)
3547 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3548 NULL);
3550 decl_attributes (&decl, parm->attrs, 0);
3552 return decl;
3555 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3556 and push that on the current scope. */
3558 void
3559 push_parm_decl (const struct c_parm *parm)
3561 tree decl;
3563 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3564 decl_attributes (&decl, parm->attrs, 0);
3566 decl = pushdecl (decl);
3568 finish_decl (decl, NULL_TREE, NULL_TREE);
3571 /* Mark all the parameter declarations to date as forward decls.
3572 Also diagnose use of this extension. */
3574 void
3575 mark_forward_parm_decls (void)
3577 struct c_binding *b;
3579 if (pedantic && !current_scope->warned_forward_parm_decls)
3581 pedwarn ("ISO C forbids forward parameter declarations");
3582 current_scope->warned_forward_parm_decls = true;
3585 for (b = current_scope->bindings; b; b = b->prev)
3586 if (TREE_CODE (b->decl) == PARM_DECL)
3587 TREE_ASM_WRITTEN (b->decl) = 1;
3590 static GTY(()) int compound_literal_number;
3592 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3593 literal, which may be an incomplete array type completed by the
3594 initializer; INIT is a CONSTRUCTOR that initializes the compound
3595 literal. */
3597 tree
3598 build_compound_literal (tree type, tree init)
3600 /* We do not use start_decl here because we have a type, not a declarator;
3601 and do not use finish_decl because the decl should be stored inside
3602 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3603 tree decl;
3604 tree complit;
3605 tree stmt;
3607 if (type == error_mark_node)
3608 return error_mark_node;
3610 decl = build_decl (VAR_DECL, NULL_TREE, type);
3611 DECL_EXTERNAL (decl) = 0;
3612 TREE_PUBLIC (decl) = 0;
3613 TREE_STATIC (decl) = (current_scope == file_scope);
3614 DECL_CONTEXT (decl) = current_function_decl;
3615 TREE_USED (decl) = 1;
3616 TREE_TYPE (decl) = type;
3617 TREE_READONLY (decl) = TYPE_READONLY (type);
3618 store_init_value (decl, init);
3620 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3622 int failure = complete_array_type (&TREE_TYPE (decl),
3623 DECL_INITIAL (decl), true);
3624 gcc_assert (!failure);
3626 type = TREE_TYPE (decl);
3627 TREE_TYPE (DECL_INITIAL (decl)) = type;
3630 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3631 return error_mark_node;
3633 stmt = build_stmt (DECL_EXPR, decl);
3634 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3635 TREE_SIDE_EFFECTS (complit) = 1;
3637 layout_decl (decl, 0);
3639 if (TREE_STATIC (decl))
3641 /* This decl needs a name for the assembler output. We also need
3642 a unique suffix to be added to the name. */
3643 char *name;
3645 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3646 compound_literal_number);
3647 compound_literal_number++;
3648 DECL_NAME (decl) = get_identifier (name);
3649 DECL_DEFER_OUTPUT (decl) = 1;
3650 DECL_COMDAT (decl) = 1;
3651 DECL_ARTIFICIAL (decl) = 1;
3652 DECL_IGNORED_P (decl) = 1;
3653 pushdecl (decl);
3654 rest_of_decl_compilation (decl, 1, 0);
3657 return complit;
3660 /* Determine whether TYPE is a structure with a flexible array member,
3661 or a union containing such a structure (possibly recursively). */
3663 static bool
3664 flexible_array_type_p (tree type)
3666 tree x;
3667 switch (TREE_CODE (type))
3669 case RECORD_TYPE:
3670 x = TYPE_FIELDS (type);
3671 if (x == NULL_TREE)
3672 return false;
3673 while (TREE_CHAIN (x) != NULL_TREE)
3674 x = TREE_CHAIN (x);
3675 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3676 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3677 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3678 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3679 return true;
3680 return false;
3681 case UNION_TYPE:
3682 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3684 if (flexible_array_type_p (TREE_TYPE (x)))
3685 return true;
3687 return false;
3688 default:
3689 return false;
3693 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3694 replacing with appropriate values if they are invalid. */
3695 static void
3696 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3698 tree type_mv;
3699 unsigned int max_width;
3700 unsigned HOST_WIDE_INT w;
3701 const char *name = orig_name ? orig_name: _("<anonymous>");
3703 /* Detect and ignore out of range field width and process valid
3704 field widths. */
3705 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3706 || TREE_CODE (*width) != INTEGER_CST)
3708 error ("bit-field %qs width not an integer constant", name);
3709 *width = integer_one_node;
3711 else
3713 constant_expression_warning (*width);
3714 if (tree_int_cst_sgn (*width) < 0)
3716 error ("negative width in bit-field %qs", name);
3717 *width = integer_one_node;
3719 else if (integer_zerop (*width) && orig_name)
3721 error ("zero width for bit-field %qs", name);
3722 *width = integer_one_node;
3726 /* Detect invalid bit-field type. */
3727 if (TREE_CODE (*type) != INTEGER_TYPE
3728 && TREE_CODE (*type) != BOOLEAN_TYPE
3729 && TREE_CODE (*type) != ENUMERAL_TYPE)
3731 error ("bit-field %qs has invalid type", name);
3732 *type = unsigned_type_node;
3735 type_mv = TYPE_MAIN_VARIANT (*type);
3736 if (pedantic
3737 && type_mv != integer_type_node
3738 && type_mv != unsigned_type_node
3739 && type_mv != boolean_type_node)
3740 pedwarn ("type of bit-field %qs is a GCC extension", name);
3742 if (type_mv == boolean_type_node)
3743 max_width = CHAR_TYPE_SIZE;
3744 else
3745 max_width = TYPE_PRECISION (*type);
3747 if (0 < compare_tree_int (*width, max_width))
3749 error ("width of %qs exceeds its type", name);
3750 w = max_width;
3751 *width = build_int_cst (NULL_TREE, w);
3753 else
3754 w = tree_low_cst (*width, 1);
3756 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3758 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3759 if (!lt
3760 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3761 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3762 warning (0, "%qs is narrower than values of its type", name);
3766 /* Given declspecs and a declarator,
3767 determine the name and type of the object declared
3768 and construct a ..._DECL node for it.
3769 (In one case we can return a ..._TYPE node instead.
3770 For invalid input we sometimes return 0.)
3772 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3774 DECL_CONTEXT says which syntactic context this declaration is in:
3775 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3776 FUNCDEF for a function definition. Like NORMAL but a few different
3777 error messages in each case. Return value may be zero meaning
3778 this definition is too screwy to try to parse.
3779 PARM for a parameter declaration (either within a function prototype
3780 or before a function body). Make a PARM_DECL, or return void_type_node.
3781 TYPENAME if for a typename (in a cast or sizeof).
3782 Don't make a DECL node; just return the ..._TYPE node.
3783 FIELD for a struct or union field; make a FIELD_DECL.
3784 INITIALIZED is true if the decl has an initializer.
3785 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3786 representing the width of the bit-field.
3788 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3789 It may also be so in the PARM case, for a prototype where the
3790 argument type is specified but not the name.
3792 This function is where the complicated C meanings of `static'
3793 and `extern' are interpreted. */
3795 static tree
3796 grokdeclarator (const struct c_declarator *declarator,
3797 struct c_declspecs *declspecs,
3798 enum decl_context decl_context, bool initialized, tree *width)
3800 tree type = declspecs->type;
3801 bool threadp = declspecs->thread_p;
3802 enum c_storage_class storage_class = declspecs->storage_class;
3803 int constp;
3804 int restrictp;
3805 int volatilep;
3806 int type_quals = TYPE_UNQUALIFIED;
3807 const char *name, *orig_name;
3808 tree typedef_type = 0;
3809 int funcdef_flag = 0;
3810 bool funcdef_syntax = false;
3811 int size_varies = 0;
3812 tree decl_attr = declspecs->decl_attr;
3813 int array_ptr_quals = TYPE_UNQUALIFIED;
3814 tree array_ptr_attrs = NULL_TREE;
3815 int array_parm_static = 0;
3816 tree returned_attrs = NULL_TREE;
3817 bool bitfield = width != NULL;
3818 tree element_type;
3819 struct c_arg_info *arg_info = 0;
3821 if (decl_context == FUNCDEF)
3822 funcdef_flag = 1, decl_context = NORMAL;
3824 /* Look inside a declarator for the name being declared
3825 and get it as a string, for an error message. */
3827 const struct c_declarator *decl = declarator;
3828 name = 0;
3830 while (decl)
3831 switch (decl->kind)
3833 case cdk_function:
3834 case cdk_array:
3835 case cdk_pointer:
3836 funcdef_syntax = (decl->kind == cdk_function);
3837 decl = decl->declarator;
3838 break;
3840 case cdk_attrs:
3841 decl = decl->declarator;
3842 break;
3844 case cdk_id:
3845 if (decl->u.id)
3846 name = IDENTIFIER_POINTER (decl->u.id);
3847 decl = 0;
3848 break;
3850 default:
3851 gcc_unreachable ();
3853 orig_name = name;
3854 if (name == 0)
3855 name = "type name";
3858 /* A function definition's declarator must have the form of
3859 a function declarator. */
3861 if (funcdef_flag && !funcdef_syntax)
3862 return 0;
3864 /* If this looks like a function definition, make it one,
3865 even if it occurs where parms are expected.
3866 Then store_parm_decls will reject it and not use it as a parm. */
3867 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3868 decl_context = PARM;
3870 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3871 warn_deprecated_use (declspecs->type);
3873 typedef_type = type;
3874 size_varies = C_TYPE_VARIABLE_SIZE (type);
3876 /* Diagnose defaulting to "int". */
3878 if (declspecs->default_int_p && !in_system_header)
3880 /* Issue a warning if this is an ISO C 99 program or if
3881 -Wreturn-type and this is a function, or if -Wimplicit;
3882 prefer the former warning since it is more explicit. */
3883 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3884 && funcdef_flag)
3885 warn_about_return_type = 1;
3886 else if (warn_implicit_int || flag_isoc99)
3887 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3890 /* Adjust the type if a bit-field is being declared,
3891 -funsigned-bitfields applied and the type is not explicitly
3892 "signed". */
3893 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3894 && TREE_CODE (type) == INTEGER_TYPE)
3895 type = c_common_unsigned_type (type);
3897 /* Figure out the type qualifiers for the declaration. There are
3898 two ways a declaration can become qualified. One is something
3899 like `const int i' where the `const' is explicit. Another is
3900 something like `typedef const int CI; CI i' where the type of the
3901 declaration contains the `const'. A third possibility is that
3902 there is a type qualifier on the element type of a typedefed
3903 array type, in which case we should extract that qualifier so
3904 that c_apply_type_quals_to_decls receives the full list of
3905 qualifiers to work with (C90 is not entirely clear about whether
3906 duplicate qualifiers should be diagnosed in this case, but it
3907 seems most appropriate to do so). */
3908 element_type = strip_array_types (type);
3909 constp = declspecs->const_p + TYPE_READONLY (element_type);
3910 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3911 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3912 if (pedantic && !flag_isoc99)
3914 if (constp > 1)
3915 pedwarn ("duplicate %<const%>");
3916 if (restrictp > 1)
3917 pedwarn ("duplicate %<restrict%>");
3918 if (volatilep > 1)
3919 pedwarn ("duplicate %<volatile%>");
3921 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3922 type = TYPE_MAIN_VARIANT (type);
3923 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3924 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3925 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3927 /* Warn about storage classes that are invalid for certain
3928 kinds of declarations (parameters, typenames, etc.). */
3930 if (funcdef_flag
3931 && (threadp
3932 || storage_class == csc_auto
3933 || storage_class == csc_register
3934 || storage_class == csc_typedef))
3936 if (storage_class == csc_auto
3937 && (pedantic || current_scope == file_scope))
3938 pedwarn ("function definition declared %<auto%>");
3939 if (storage_class == csc_register)
3940 error ("function definition declared %<register%>");
3941 if (storage_class == csc_typedef)
3942 error ("function definition declared %<typedef%>");
3943 if (threadp)
3944 error ("function definition declared %<__thread%>");
3945 threadp = false;
3946 if (storage_class == csc_auto
3947 || storage_class == csc_register
3948 || storage_class == csc_typedef)
3949 storage_class = csc_none;
3951 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3953 if (decl_context == PARM && storage_class == csc_register)
3955 else
3957 switch (decl_context)
3959 case FIELD:
3960 error ("storage class specified for structure field %qs",
3961 name);
3962 break;
3963 case PARM:
3964 error ("storage class specified for parameter %qs", name);
3965 break;
3966 default:
3967 error ("storage class specified for typename");
3968 break;
3970 storage_class = csc_none;
3971 threadp = false;
3974 else if (storage_class == csc_extern
3975 && initialized
3976 && !funcdef_flag)
3978 /* 'extern' with initialization is invalid if not at file scope. */
3979 if (current_scope == file_scope)
3980 warning (0, "%qs initialized and declared %<extern%>", name);
3981 else
3982 error ("%qs has both %<extern%> and initializer", name);
3984 else if (current_scope == file_scope)
3986 if (storage_class == csc_auto)
3987 error ("file-scope declaration of %qs specifies %<auto%>", name);
3988 if (pedantic && storage_class == csc_register)
3989 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3991 else
3993 if (storage_class == csc_extern && funcdef_flag)
3994 error ("nested function %qs declared %<extern%>", name);
3995 else if (threadp && storage_class == csc_none)
3997 error ("function-scope %qs implicitly auto and declared "
3998 "%<__thread%>",
3999 name);
4000 threadp = false;
4004 /* Now figure out the structure of the declarator proper.
4005 Descend through it, creating more complex types, until we reach
4006 the declared identifier (or NULL_TREE, in an absolute declarator).
4007 At each stage we maintain an unqualified version of the type
4008 together with any qualifiers that should be applied to it with
4009 c_build_qualified_type; this way, array types including
4010 multidimensional array types are first built up in unqualified
4011 form and then the qualified form is created with
4012 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4014 while (declarator && declarator->kind != cdk_id)
4016 if (type == error_mark_node)
4018 declarator = declarator->declarator;
4019 continue;
4022 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4023 a cdk_pointer (for *...),
4024 a cdk_function (for ...(...)),
4025 a cdk_attrs (for nested attributes),
4026 or a cdk_id (for the name being declared
4027 or the place in an absolute declarator
4028 where the name was omitted).
4029 For the last case, we have just exited the loop.
4031 At this point, TYPE is the type of elements of an array,
4032 or for a function to return, or for a pointer to point to.
4033 After this sequence of ifs, TYPE is the type of the
4034 array or function or pointer, and DECLARATOR has had its
4035 outermost layer removed. */
4037 if (array_ptr_quals != TYPE_UNQUALIFIED
4038 || array_ptr_attrs != NULL_TREE
4039 || array_parm_static)
4041 /* Only the innermost declarator (making a parameter be of
4042 array type which is converted to pointer type)
4043 may have static or type qualifiers. */
4044 error ("static or type qualifiers in non-parameter array declarator");
4045 array_ptr_quals = TYPE_UNQUALIFIED;
4046 array_ptr_attrs = NULL_TREE;
4047 array_parm_static = 0;
4050 switch (declarator->kind)
4052 case cdk_attrs:
4054 /* A declarator with embedded attributes. */
4055 tree attrs = declarator->u.attrs;
4056 const struct c_declarator *inner_decl;
4057 int attr_flags = 0;
4058 declarator = declarator->declarator;
4059 inner_decl = declarator;
4060 while (inner_decl->kind == cdk_attrs)
4061 inner_decl = inner_decl->declarator;
4062 if (inner_decl->kind == cdk_id)
4063 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4064 else if (inner_decl->kind == cdk_function)
4065 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4066 else if (inner_decl->kind == cdk_array)
4067 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4068 returned_attrs = decl_attributes (&type,
4069 chainon (returned_attrs, attrs),
4070 attr_flags);
4071 break;
4073 case cdk_array:
4075 tree itype = NULL_TREE;
4076 tree size = declarator->u.array.dimen;
4077 /* The index is a signed object `sizetype' bits wide. */
4078 tree index_type = c_common_signed_type (sizetype);
4080 array_ptr_quals = declarator->u.array.quals;
4081 array_ptr_attrs = declarator->u.array.attrs;
4082 array_parm_static = declarator->u.array.static_p;
4084 declarator = declarator->declarator;
4086 /* Check for some types that there cannot be arrays of. */
4088 if (VOID_TYPE_P (type))
4090 error ("declaration of %qs as array of voids", name);
4091 type = error_mark_node;
4094 if (TREE_CODE (type) == FUNCTION_TYPE)
4096 error ("declaration of %qs as array of functions", name);
4097 type = error_mark_node;
4100 if (pedantic && !in_system_header && flexible_array_type_p (type))
4101 pedwarn ("invalid use of structure with flexible array member");
4103 if (size == error_mark_node)
4104 type = error_mark_node;
4106 if (type == error_mark_node)
4107 continue;
4109 /* If size was specified, set ITYPE to a range-type for
4110 that size. Otherwise, ITYPE remains null. finish_decl
4111 may figure it out from an initial value. */
4113 if (size)
4115 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4116 lvalue. */
4117 STRIP_TYPE_NOPS (size);
4119 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4121 error ("size of array %qs has non-integer type", name);
4122 size = integer_one_node;
4125 if (pedantic && integer_zerop (size))
4126 pedwarn ("ISO C forbids zero-size array %qs", name);
4128 if (TREE_CODE (size) == INTEGER_CST)
4130 constant_expression_warning (size);
4131 if (tree_int_cst_sgn (size) < 0)
4133 error ("size of array %qs is negative", name);
4134 size = integer_one_node;
4137 else
4139 /* Make sure the array size remains visibly
4140 nonconstant even if it is (eg) a const variable
4141 with known value. */
4142 size_varies = 1;
4144 if (!flag_isoc99 && pedantic)
4146 if (TREE_CONSTANT (size))
4147 pedwarn ("ISO C90 forbids array %qs whose size "
4148 "can%'t be evaluated",
4149 name);
4150 else
4151 pedwarn ("ISO C90 forbids variable-size array %qs",
4152 name);
4156 if (integer_zerop (size))
4158 /* A zero-length array cannot be represented with
4159 an unsigned index type, which is what we'll
4160 get with build_index_type. Create an
4161 open-ended range instead. */
4162 itype = build_range_type (sizetype, size, NULL_TREE);
4164 else
4166 /* Arrange for the SAVE_EXPR on the inside of the
4167 MINUS_EXPR, which allows the -1 to get folded
4168 with the +1 that happens when building TYPE_SIZE. */
4169 if (size_varies)
4170 size = variable_size (size);
4172 /* Compute the maximum valid index, that is, size
4173 - 1. Do the calculation in index_type, so that
4174 if it is a variable the computations will be
4175 done in the proper mode. */
4176 itype = fold (build2 (MINUS_EXPR, index_type,
4177 convert (index_type, size),
4178 convert (index_type,
4179 size_one_node)));
4181 /* If that overflowed, the array is too big. ???
4182 While a size of INT_MAX+1 technically shouldn't
4183 cause an overflow (because we subtract 1), the
4184 overflow is recorded during the conversion to
4185 index_type, before the subtraction. Handling
4186 this case seems like an unnecessary
4187 complication. */
4188 if (TREE_OVERFLOW (itype))
4190 error ("size of array %qs is too large", name);
4191 type = error_mark_node;
4192 continue;
4195 itype = build_index_type (itype);
4198 else if (decl_context == FIELD)
4200 if (pedantic && !flag_isoc99 && !in_system_header)
4201 pedwarn ("ISO C90 does not support flexible array members");
4203 /* ISO C99 Flexible array members are effectively
4204 identical to GCC's zero-length array extension. */
4205 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4208 /* Complain about arrays of incomplete types. */
4209 if (!COMPLETE_TYPE_P (type))
4211 error ("array type has incomplete element type");
4212 type = error_mark_node;
4214 else
4215 type = build_array_type (type, itype);
4217 if (size_varies)
4218 C_TYPE_VARIABLE_SIZE (type) = 1;
4220 /* The GCC extension for zero-length arrays differs from
4221 ISO flexible array members in that sizeof yields
4222 zero. */
4223 if (size && integer_zerop (size))
4225 TYPE_SIZE (type) = bitsize_zero_node;
4226 TYPE_SIZE_UNIT (type) = size_zero_node;
4229 if (decl_context != PARM
4230 && (array_ptr_quals != TYPE_UNQUALIFIED
4231 || array_ptr_attrs != NULL_TREE
4232 || array_parm_static))
4234 error ("static or type qualifiers in non-parameter array declarator");
4235 array_ptr_quals = TYPE_UNQUALIFIED;
4236 array_ptr_attrs = NULL_TREE;
4237 array_parm_static = 0;
4239 break;
4241 case cdk_function:
4243 /* Say it's a definition only for the declarator closest
4244 to the identifier, apart possibly from some
4245 attributes. */
4246 bool really_funcdef = false;
4247 tree arg_types;
4248 if (funcdef_flag)
4250 const struct c_declarator *t = declarator->declarator;
4251 while (t->kind == cdk_attrs)
4252 t = t->declarator;
4253 really_funcdef = (t->kind == cdk_id);
4256 /* Declaring a function type. Make sure we have a valid
4257 type for the function to return. */
4258 if (type == error_mark_node)
4259 continue;
4261 size_varies = 0;
4263 /* Warn about some types functions can't return. */
4264 if (TREE_CODE (type) == FUNCTION_TYPE)
4266 error ("%qs declared as function returning a function", name);
4267 type = integer_type_node;
4269 if (TREE_CODE (type) == ARRAY_TYPE)
4271 error ("%qs declared as function returning an array", name);
4272 type = integer_type_node;
4275 /* Construct the function type and go to the next
4276 inner layer of declarator. */
4277 arg_info = declarator->u.arg_info;
4278 arg_types = grokparms (arg_info, really_funcdef);
4280 /* Type qualifiers before the return type of the function
4281 qualify the return type, not the function type. */
4282 if (type_quals)
4284 /* Type qualifiers on a function return type are
4285 normally permitted by the standard but have no
4286 effect, so give a warning at -Wreturn-type.
4287 Qualifiers on a void return type are banned on
4288 function definitions in ISO C; GCC used to used
4289 them for noreturn functions. */
4290 if (VOID_TYPE_P (type) && really_funcdef)
4291 pedwarn ("function definition has qualified void return type");
4292 else if (warn_return_type)
4293 warning (0, "type qualifiers ignored on function return type");
4295 type = c_build_qualified_type (type, type_quals);
4297 type_quals = TYPE_UNQUALIFIED;
4299 type = build_function_type (type, arg_types);
4300 declarator = declarator->declarator;
4302 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4303 the formal parameter list of this FUNCTION_TYPE to point to
4304 the FUNCTION_TYPE node itself. */
4306 tree link;
4308 for (link = arg_info->tags;
4309 link;
4310 link = TREE_CHAIN (link))
4311 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4313 break;
4315 case cdk_pointer:
4317 /* Merge any constancy or volatility into the target type
4318 for the pointer. */
4320 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4321 && type_quals)
4322 pedwarn ("ISO C forbids qualified function types");
4323 if (type_quals)
4324 type = c_build_qualified_type (type, type_quals);
4325 size_varies = 0;
4327 type = build_pointer_type (type);
4329 /* Process type qualifiers (such as const or volatile)
4330 that were given inside the `*'. */
4331 type_quals = declarator->u.pointer_quals;
4333 declarator = declarator->declarator;
4334 break;
4336 default:
4337 gcc_unreachable ();
4341 /* Now TYPE has the actual type, apart from any qualifiers in
4342 TYPE_QUALS. */
4344 /* Check the type and width of a bit-field. */
4345 if (bitfield)
4346 check_bitfield_type_and_width (&type, width, orig_name);
4348 /* Did array size calculations overflow? */
4350 if (TREE_CODE (type) == ARRAY_TYPE
4351 && COMPLETE_TYPE_P (type)
4352 && TREE_OVERFLOW (TYPE_SIZE (type)))
4354 error ("size of array %qs is too large", name);
4355 /* If we proceed with the array type as it is, we'll eventually
4356 crash in tree_low_cst(). */
4357 type = error_mark_node;
4360 /* If this is declaring a typedef name, return a TYPE_DECL. */
4362 if (storage_class == csc_typedef)
4364 tree decl;
4365 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4366 && type_quals)
4367 pedwarn ("ISO C forbids qualified function types");
4368 if (type_quals)
4369 type = c_build_qualified_type (type, type_quals);
4370 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4371 if (declspecs->explicit_signed_p)
4372 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4373 decl_attributes (&decl, returned_attrs, 0);
4374 if (declspecs->inline_p)
4375 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4376 return decl;
4379 /* Detect the case of an array type of unspecified size
4380 which came, as such, direct from a typedef name.
4381 We must copy the type, so that each identifier gets
4382 a distinct type, so that each identifier's size can be
4383 controlled separately by its own initializer. */
4385 if (type != 0 && typedef_type != 0
4386 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4387 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4389 type = build_array_type (TREE_TYPE (type), 0);
4390 if (size_varies)
4391 C_TYPE_VARIABLE_SIZE (type) = 1;
4394 /* If this is a type name (such as, in a cast or sizeof),
4395 compute the type and return it now. */
4397 if (decl_context == TYPENAME)
4399 /* Note that the grammar rejects storage classes in typenames
4400 and fields. */
4401 gcc_assert (storage_class == csc_none && !threadp
4402 && !declspecs->inline_p);
4403 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4404 && type_quals)
4405 pedwarn ("ISO C forbids const or volatile function types");
4406 if (type_quals)
4407 type = c_build_qualified_type (type, type_quals);
4408 decl_attributes (&type, returned_attrs, 0);
4409 return type;
4412 /* Aside from typedefs and type names (handle above),
4413 `void' at top level (not within pointer)
4414 is allowed only in public variables.
4415 We don't complain about parms either, but that is because
4416 a better error message can be made later. */
4418 if (VOID_TYPE_P (type) && decl_context != PARM
4419 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4420 && (storage_class == csc_extern
4421 || (current_scope == file_scope
4422 && !(storage_class == csc_static
4423 || storage_class == csc_register)))))
4425 error ("variable or field %qs declared void", name);
4426 type = integer_type_node;
4429 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4430 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4433 tree decl;
4435 if (decl_context == PARM)
4437 tree type_as_written;
4438 tree promoted_type;
4440 /* A parameter declared as an array of T is really a pointer to T.
4441 One declared as a function is really a pointer to a function. */
4443 if (TREE_CODE (type) == ARRAY_TYPE)
4445 /* Transfer const-ness of array into that of type pointed to. */
4446 type = TREE_TYPE (type);
4447 if (type_quals)
4448 type = c_build_qualified_type (type, type_quals);
4449 type = build_pointer_type (type);
4450 type_quals = array_ptr_quals;
4452 /* We don't yet implement attributes in this context. */
4453 if (array_ptr_attrs != NULL_TREE)
4454 warning (0, "attributes in parameter array declarator ignored");
4456 size_varies = 0;
4458 else if (TREE_CODE (type) == FUNCTION_TYPE)
4460 if (pedantic && type_quals)
4461 pedwarn ("ISO C forbids qualified function types");
4462 if (type_quals)
4463 type = c_build_qualified_type (type, type_quals);
4464 type = build_pointer_type (type);
4465 type_quals = TYPE_UNQUALIFIED;
4467 else if (type_quals)
4468 type = c_build_qualified_type (type, type_quals);
4470 type_as_written = type;
4472 decl = build_decl (PARM_DECL, declarator->u.id, type);
4473 if (size_varies)
4474 C_DECL_VARIABLE_SIZE (decl) = 1;
4476 /* Compute the type actually passed in the parmlist,
4477 for the case where there is no prototype.
4478 (For example, shorts and chars are passed as ints.)
4479 When there is a prototype, this is overridden later. */
4481 if (type == error_mark_node)
4482 promoted_type = type;
4483 else
4484 promoted_type = c_type_promotes_to (type);
4486 DECL_ARG_TYPE (decl) = promoted_type;
4487 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4488 if (declspecs->inline_p)
4489 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4491 else if (decl_context == FIELD)
4493 /* Note that the grammar rejects storage classes in typenames
4494 and fields. */
4495 gcc_assert (storage_class == csc_none && !threadp
4496 && !declspecs->inline_p);
4498 /* Structure field. It may not be a function. */
4500 if (TREE_CODE (type) == FUNCTION_TYPE)
4502 error ("field %qs declared as a function", name);
4503 type = build_pointer_type (type);
4505 else if (TREE_CODE (type) != ERROR_MARK
4506 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4508 error ("field %qs has incomplete type", name);
4509 type = error_mark_node;
4511 type = c_build_qualified_type (type, type_quals);
4512 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4513 DECL_NONADDRESSABLE_P (decl) = bitfield;
4515 if (size_varies)
4516 C_DECL_VARIABLE_SIZE (decl) = 1;
4518 else if (TREE_CODE (type) == FUNCTION_TYPE)
4520 if (storage_class == csc_register || threadp)
4522 error ("invalid storage class for function %qs", name);
4524 else if (current_scope != file_scope)
4526 /* Function declaration not at file scope. Storage
4527 classes other than `extern' are not allowed, C99
4528 6.7.1p5, and `extern' makes no difference. However,
4529 GCC allows 'auto', perhaps with 'inline', to support
4530 nested functions. */
4531 if (storage_class == csc_auto)
4533 if (pedantic)
4534 pedwarn ("invalid storage class for function %qs", name);
4536 else if (storage_class == csc_static)
4538 error ("invalid storage class for function %qs", name);
4539 if (funcdef_flag)
4540 storage_class = declspecs->storage_class = csc_none;
4541 else
4542 return 0;
4546 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4547 decl = build_decl_attribute_variant (decl, decl_attr);
4549 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4551 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4552 pedwarn ("ISO C forbids qualified function types");
4554 /* GNU C interprets a volatile-qualified function type to indicate
4555 that the function does not return. */
4556 if ((type_quals & TYPE_QUAL_VOLATILE)
4557 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4558 warning (0, "%<noreturn%> function returns non-void value");
4560 /* Every function declaration is an external reference
4561 (DECL_EXTERNAL) except for those which are not at file
4562 scope and are explicitly declared "auto". This is
4563 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4564 GCC to signify a forward declaration of a nested function. */
4565 if (storage_class == csc_auto && current_scope != file_scope)
4566 DECL_EXTERNAL (decl) = 0;
4567 else
4568 DECL_EXTERNAL (decl) = 1;
4570 /* Record absence of global scope for `static' or `auto'. */
4571 TREE_PUBLIC (decl)
4572 = !(storage_class == csc_static || storage_class == csc_auto);
4574 /* For a function definition, record the argument information
4575 block where store_parm_decls will look for it. */
4576 if (funcdef_flag)
4577 current_function_arg_info = arg_info;
4579 if (declspecs->default_int_p)
4580 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4582 /* Record presence of `inline', if it is reasonable. */
4583 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4585 if (declspecs->inline_p)
4586 pedwarn ("cannot inline function %<main%>");
4588 else if (declspecs->inline_p)
4590 /* Record that the function is declared `inline'. */
4591 DECL_DECLARED_INLINE_P (decl) = 1;
4593 /* Do not mark bare declarations as DECL_INLINE. Doing so
4594 in the presence of multiple declarations can result in
4595 the abstract origin pointing between the declarations,
4596 which will confuse dwarf2out. */
4597 if (initialized)
4599 DECL_INLINE (decl) = 1;
4600 if (storage_class == csc_extern)
4601 current_extern_inline = 1;
4604 /* If -finline-functions, assume it can be inlined. This does
4605 two things: let the function be deferred until it is actually
4606 needed, and let dwarf2 know that the function is inlinable. */
4607 else if (flag_inline_trees == 2 && initialized)
4608 DECL_INLINE (decl) = 1;
4610 else
4612 /* It's a variable. */
4613 /* An uninitialized decl with `extern' is a reference. */
4614 int extern_ref = !initialized && storage_class == csc_extern;
4616 type = c_build_qualified_type (type, type_quals);
4618 /* C99 6.2.2p7: It is invalid (compile-time undefined
4619 behavior) to create an 'extern' declaration for a
4620 variable if there is a global declaration that is
4621 'static' and the global declaration is not visible.
4622 (If the static declaration _is_ currently visible,
4623 the 'extern' declaration is taken to refer to that decl.) */
4624 if (extern_ref && current_scope != file_scope)
4626 tree global_decl = identifier_global_value (declarator->u.id);
4627 tree visible_decl = lookup_name (declarator->u.id);
4629 if (global_decl
4630 && global_decl != visible_decl
4631 && TREE_CODE (global_decl) == VAR_DECL
4632 && !TREE_PUBLIC (global_decl))
4633 error ("variable previously declared %<static%> redeclared "
4634 "%<extern%>");
4637 decl = build_decl (VAR_DECL, declarator->u.id, type);
4638 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4639 if (size_varies)
4640 C_DECL_VARIABLE_SIZE (decl) = 1;
4642 if (declspecs->inline_p)
4643 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4645 /* At file scope, an initialized extern declaration may follow
4646 a static declaration. In that case, DECL_EXTERNAL will be
4647 reset later in start_decl. */
4648 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4650 /* At file scope, the presence of a `static' or `register' storage
4651 class specifier, or the absence of all storage class specifiers
4652 makes this declaration a definition (perhaps tentative). Also,
4653 the absence of both `static' and `register' makes it public. */
4654 if (current_scope == file_scope)
4656 TREE_PUBLIC (decl) = !(storage_class == csc_static
4657 || storage_class == csc_register);
4658 TREE_STATIC (decl) = !extern_ref;
4660 /* Not at file scope, only `static' makes a static definition. */
4661 else
4663 TREE_STATIC (decl) = (storage_class == csc_static);
4664 TREE_PUBLIC (decl) = extern_ref;
4667 if (threadp)
4669 if (targetm.have_tls)
4670 DECL_THREAD_LOCAL (decl) = 1;
4671 else
4672 /* A mere warning is sure to result in improper semantics
4673 at runtime. Don't bother to allow this to compile. */
4674 error ("thread-local storage not supported for this target");
4678 /* Record `register' declaration for warnings on &
4679 and in case doing stupid register allocation. */
4681 if (storage_class == csc_register)
4683 C_DECL_REGISTER (decl) = 1;
4684 DECL_REGISTER (decl) = 1;
4687 /* Record constancy and volatility. */
4688 c_apply_type_quals_to_decl (type_quals, decl);
4690 /* If a type has volatile components, it should be stored in memory.
4691 Otherwise, the fact that those components are volatile
4692 will be ignored, and would even crash the compiler. */
4693 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4695 /* It is not an error for a structure with volatile fields to
4696 be declared register, but reset DECL_REGISTER since it
4697 cannot actually go in a register. */
4698 int was_reg = C_DECL_REGISTER (decl);
4699 C_DECL_REGISTER (decl) = 0;
4700 DECL_REGISTER (decl) = 0;
4701 c_mark_addressable (decl);
4702 C_DECL_REGISTER (decl) = was_reg;
4705 /* This is the earliest point at which we might know the assembler
4706 name of a variable. Thus, if it's known before this, die horribly. */
4707 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4709 decl_attributes (&decl, returned_attrs, 0);
4711 return decl;
4715 /* Decode the parameter-list info for a function type or function definition.
4716 The argument is the value returned by `get_parm_info' (or made in parse.y
4717 if there is an identifier list instead of a parameter decl list).
4718 These two functions are separate because when a function returns
4719 or receives functions then each is called multiple times but the order
4720 of calls is different. The last call to `grokparms' is always the one
4721 that contains the formal parameter names of a function definition.
4723 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4725 FUNCDEF_FLAG is true for a function definition, false for
4726 a mere declaration. A nonempty identifier-list gets an error message
4727 when FUNCDEF_FLAG is false. */
4729 static tree
4730 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4732 tree arg_types = arg_info->types;
4734 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4735 && !in_system_header)
4736 warning (0, "function declaration isn%'t a prototype");
4738 if (arg_types == error_mark_node)
4739 return 0; /* don't set TYPE_ARG_TYPES in this case */
4741 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4743 if (!funcdef_flag)
4744 pedwarn ("parameter names (without types) in function declaration");
4746 arg_info->parms = arg_info->types;
4747 arg_info->types = 0;
4748 return 0;
4750 else
4752 tree parm, type, typelt;
4753 unsigned int parmno;
4755 /* If there is a parameter of incomplete type in a definition,
4756 this is an error. In a declaration this is valid, and a
4757 struct or union type may be completed later, before any calls
4758 or definition of the function. In the case where the tag was
4759 first declared within the parameter list, a warning has
4760 already been given. If a parameter has void type, then
4761 however the function cannot be defined or called, so
4762 warn. */
4764 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4765 parm;
4766 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4768 type = TREE_VALUE (typelt);
4769 if (type == error_mark_node)
4770 continue;
4772 if (!COMPLETE_TYPE_P (type))
4774 if (funcdef_flag)
4776 if (DECL_NAME (parm))
4777 error ("%Jparameter %u (%qD) has incomplete type",
4778 parm, parmno, parm);
4779 else
4780 error ("%Jparameter %u has incomplete type",
4781 parm, parmno);
4783 TREE_VALUE (typelt) = error_mark_node;
4784 TREE_TYPE (parm) = error_mark_node;
4786 else if (VOID_TYPE_P (type))
4788 if (DECL_NAME (parm))
4789 warning (0, "%Jparameter %u (%qD) has void type",
4790 parm, parmno, parm);
4791 else
4792 warning (0, "%Jparameter %u has void type",
4793 parm, parmno);
4797 return arg_types;
4801 /* Take apart the current scope and return a c_arg_info structure with
4802 info on a parameter list just parsed.
4804 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4806 ELLIPSIS being true means the argument list ended in '...' so don't
4807 append a sentinel (void_list_node) to the end of the type-list. */
4809 struct c_arg_info *
4810 get_parm_info (bool ellipsis)
4812 struct c_binding *b = current_scope->bindings;
4813 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4814 struct c_arg_info);
4815 tree parms = 0;
4816 tree tags = 0;
4817 tree types = 0;
4818 tree others = 0;
4820 static bool explained_incomplete_types = false;
4821 bool gave_void_only_once_err = false;
4823 arg_info->parms = 0;
4824 arg_info->tags = 0;
4825 arg_info->types = 0;
4826 arg_info->others = 0;
4828 /* The bindings in this scope must not get put into a block.
4829 We will take care of deleting the binding nodes. */
4830 current_scope->bindings = 0;
4832 /* This function is only called if there was *something* on the
4833 parameter list. */
4834 gcc_assert (b);
4836 /* A parameter list consisting solely of 'void' indicates that the
4837 function takes no arguments. But if the 'void' is qualified
4838 (by 'const' or 'volatile'), or has a storage class specifier
4839 ('register'), then the behavior is undefined; issue an error.
4840 Typedefs for 'void' are OK (see DR#157). */
4841 if (b->prev == 0 /* one binding */
4842 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4843 && !DECL_NAME (b->decl) /* anonymous */
4844 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4846 if (TREE_THIS_VOLATILE (b->decl)
4847 || TREE_READONLY (b->decl)
4848 || C_DECL_REGISTER (b->decl))
4849 error ("%<void%> as only parameter may not be qualified");
4851 /* There cannot be an ellipsis. */
4852 if (ellipsis)
4853 error ("%<void%> must be the only parameter");
4855 arg_info->types = void_list_node;
4856 return arg_info;
4859 if (!ellipsis)
4860 types = void_list_node;
4862 /* Break up the bindings list into parms, tags, types, and others;
4863 apply sanity checks; purge the name-to-decl bindings. */
4864 while (b)
4866 tree decl = b->decl;
4867 tree type = TREE_TYPE (decl);
4868 const char *keyword;
4870 switch (TREE_CODE (decl))
4872 case PARM_DECL:
4873 if (b->id)
4875 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4876 I_SYMBOL_BINDING (b->id) = b->shadowed;
4879 /* Check for forward decls that never got their actual decl. */
4880 if (TREE_ASM_WRITTEN (decl))
4881 error ("%Jparameter %qD has just a forward declaration",
4882 decl, decl);
4883 /* Check for (..., void, ...) and issue an error. */
4884 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4886 if (!gave_void_only_once_err)
4888 error ("%<void%> must be the only parameter");
4889 gave_void_only_once_err = true;
4892 else
4894 /* Valid parameter, add it to the list. */
4895 TREE_CHAIN (decl) = parms;
4896 parms = decl;
4898 /* Since there is a prototype, args are passed in their
4899 declared types. The back end may override this later. */
4900 DECL_ARG_TYPE (decl) = type;
4901 types = tree_cons (0, type, types);
4903 break;
4905 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4906 case UNION_TYPE: keyword = "union"; goto tag;
4907 case RECORD_TYPE: keyword = "struct"; goto tag;
4908 tag:
4909 /* Types may not have tag-names, in which case the type
4910 appears in the bindings list with b->id NULL. */
4911 if (b->id)
4913 gcc_assert (I_TAG_BINDING (b->id) == b);
4914 I_TAG_BINDING (b->id) = b->shadowed;
4917 /* Warn about any struct, union or enum tags defined in a
4918 parameter list. The scope of such types is limited to
4919 the parameter list, which is rarely if ever desirable
4920 (it's impossible to call such a function with type-
4921 correct arguments). An anonymous union parm type is
4922 meaningful as a GNU extension, so don't warn for that. */
4923 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4925 if (b->id)
4926 /* The %s will be one of 'struct', 'union', or 'enum'. */
4927 warning (0, "%<%s %E%> declared inside parameter list",
4928 keyword, b->id);
4929 else
4930 /* The %s will be one of 'struct', 'union', or 'enum'. */
4931 warning (0, "anonymous %s declared inside parameter list",
4932 keyword);
4934 if (!explained_incomplete_types)
4936 warning (0, "its scope is only this definition or declaration,"
4937 " which is probably not what you want");
4938 explained_incomplete_types = true;
4942 tags = tree_cons (b->id, decl, tags);
4943 break;
4945 case CONST_DECL:
4946 case TYPE_DECL:
4947 case FUNCTION_DECL:
4948 /* CONST_DECLs appear here when we have an embedded enum,
4949 and TYPE_DECLs appear here when we have an embedded struct
4950 or union. No warnings for this - we already warned about the
4951 type itself. FUNCTION_DECLs appear when there is an implicit
4952 function declaration in the parameter list. */
4954 TREE_CHAIN (decl) = others;
4955 others = decl;
4956 /* fall through */
4958 case ERROR_MARK:
4959 /* error_mark_node appears here when we have an undeclared
4960 variable. Just throw it away. */
4961 if (b->id)
4963 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4964 I_SYMBOL_BINDING (b->id) = b->shadowed;
4966 break;
4968 /* Other things that might be encountered. */
4969 case LABEL_DECL:
4970 case VAR_DECL:
4971 default:
4972 gcc_unreachable ();
4975 b = free_binding_and_advance (b);
4978 arg_info->parms = parms;
4979 arg_info->tags = tags;
4980 arg_info->types = types;
4981 arg_info->others = others;
4982 return arg_info;
4985 /* Get the struct, enum or union (CODE says which) with tag NAME.
4986 Define the tag as a forward-reference if it is not defined.
4987 Return a c_typespec structure for the type specifier. */
4989 struct c_typespec
4990 parser_xref_tag (enum tree_code code, tree name)
4992 struct c_typespec ret;
4993 /* If a cross reference is requested, look up the type
4994 already defined for this tag and return it. */
4996 tree ref = lookup_tag (code, name, 0);
4997 /* If this is the right type of tag, return what we found.
4998 (This reference will be shadowed by shadow_tag later if appropriate.)
4999 If this is the wrong type of tag, do not return it. If it was the
5000 wrong type in the same scope, we will have had an error
5001 message already; if in a different scope and declaring
5002 a name, pending_xref_error will give an error message; but if in a
5003 different scope and not declaring a name, this tag should
5004 shadow the previous declaration of a different type of tag, and
5005 this would not work properly if we return the reference found.
5006 (For example, with "struct foo" in an outer scope, "union foo;"
5007 must shadow that tag with a new one of union type.) */
5008 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5009 if (ref && TREE_CODE (ref) == code)
5011 ret.spec = ref;
5012 return ret;
5015 /* If no such tag is yet defined, create a forward-reference node
5016 and record it as the "definition".
5017 When a real declaration of this type is found,
5018 the forward-reference will be altered into a real type. */
5020 ref = make_node (code);
5021 if (code == ENUMERAL_TYPE)
5023 /* Give the type a default layout like unsigned int
5024 to avoid crashing if it does not get defined. */
5025 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5026 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5027 TYPE_USER_ALIGN (ref) = 0;
5028 TYPE_UNSIGNED (ref) = 1;
5029 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5030 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5031 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5034 pushtag (name, ref);
5036 ret.spec = ref;
5037 return ret;
5040 /* Get the struct, enum or union (CODE says which) with tag NAME.
5041 Define the tag as a forward-reference if it is not defined.
5042 Return a tree for the type. */
5044 tree
5045 xref_tag (enum tree_code code, tree name)
5047 return parser_xref_tag (code, name).spec;
5050 /* Make sure that the tag NAME is defined *in the current scope*
5051 at least as a forward reference.
5052 CODE says which kind of tag NAME ought to be. */
5054 tree
5055 start_struct (enum tree_code code, tree name)
5057 /* If there is already a tag defined at this scope
5058 (as a forward reference), just return it. */
5060 tree ref = 0;
5062 if (name != 0)
5063 ref = lookup_tag (code, name, 1);
5064 if (ref && TREE_CODE (ref) == code)
5066 if (TYPE_SIZE (ref))
5068 if (code == UNION_TYPE)
5069 error ("redefinition of %<union %E%>", name);
5070 else
5071 error ("redefinition of %<struct %E%>", name);
5073 else if (C_TYPE_BEING_DEFINED (ref))
5075 if (code == UNION_TYPE)
5076 error ("nested redefinition of %<union %E%>", name);
5077 else
5078 error ("nested redefinition of %<struct %E%>", name);
5081 else
5083 /* Otherwise create a forward-reference just so the tag is in scope. */
5085 ref = make_node (code);
5086 pushtag (name, ref);
5089 C_TYPE_BEING_DEFINED (ref) = 1;
5090 TYPE_PACKED (ref) = flag_pack_struct;
5091 return ref;
5094 /* Process the specs, declarator and width (NULL if omitted)
5095 of a structure component, returning a FIELD_DECL node.
5096 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5098 This is done during the parsing of the struct declaration.
5099 The FIELD_DECL nodes are chained together and the lot of them
5100 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5102 tree
5103 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5104 tree width)
5106 tree value;
5108 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5109 && width == NULL_TREE)
5111 /* This is an unnamed decl.
5113 If we have something of the form "union { list } ;" then this
5114 is the anonymous union extension. Similarly for struct.
5116 If this is something of the form "struct foo;", then
5117 If MS extensions are enabled, this is handled as an
5118 anonymous struct.
5119 Otherwise this is a forward declaration of a structure tag.
5121 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5122 If MS extensions are enabled and foo names a structure, then
5123 again this is an anonymous struct.
5124 Otherwise this is an error.
5126 Oh what a horrid tangled web we weave. I wonder if MS consciously
5127 took this from Plan 9 or if it was an accident of implementation
5128 that took root before someone noticed the bug... */
5130 tree type = declspecs->type;
5131 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5132 || TREE_CODE (type) == UNION_TYPE);
5133 bool ok = false;
5135 if (type_ok
5136 && (flag_ms_extensions || !declspecs->typedef_p))
5138 if (flag_ms_extensions)
5139 ok = true;
5140 else if (flag_iso)
5141 ok = false;
5142 else if (TYPE_NAME (type) == NULL)
5143 ok = true;
5144 else
5145 ok = false;
5147 if (!ok)
5149 pedwarn ("declaration does not declare anything");
5150 return NULL_TREE;
5152 if (pedantic)
5153 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5156 value = grokdeclarator (declarator, declspecs, FIELD, false,
5157 width ? &width : NULL);
5159 finish_decl (value, NULL_TREE, NULL_TREE);
5160 DECL_INITIAL (value) = width;
5162 return value;
5165 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5166 the list such that this does not present a problem later. */
5168 static void
5169 detect_field_duplicates (tree fieldlist)
5171 tree x, y;
5172 int timeout = 10;
5174 /* First, see if there are more than "a few" fields.
5175 This is trivially true if there are zero or one fields. */
5176 if (!fieldlist)
5177 return;
5178 x = TREE_CHAIN (fieldlist);
5179 if (!x)
5180 return;
5181 do {
5182 timeout--;
5183 x = TREE_CHAIN (x);
5184 } while (timeout > 0 && x);
5186 /* If there were "few" fields, avoid the overhead of allocating
5187 a hash table. Instead just do the nested traversal thing. */
5188 if (timeout > 0)
5190 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5191 if (DECL_NAME (x))
5193 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5194 if (DECL_NAME (y) == DECL_NAME (x))
5196 error ("%Jduplicate member %qD", x, x);
5197 DECL_NAME (x) = NULL_TREE;
5201 else
5203 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5204 void **slot;
5206 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5207 if ((y = DECL_NAME (x)) != 0)
5209 slot = htab_find_slot (htab, y, INSERT);
5210 if (*slot)
5212 error ("%Jduplicate member %qD", x, x);
5213 DECL_NAME (x) = NULL_TREE;
5215 *slot = y;
5218 htab_delete (htab);
5222 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5223 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5224 ATTRIBUTES are attributes to be applied to the structure. */
5226 tree
5227 finish_struct (tree t, tree fieldlist, tree attributes)
5229 tree x;
5230 bool toplevel = file_scope == current_scope;
5231 int saw_named_field;
5233 /* If this type was previously laid out as a forward reference,
5234 make sure we lay it out again. */
5236 TYPE_SIZE (t) = 0;
5238 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5240 if (pedantic)
5242 for (x = fieldlist; x; x = TREE_CHAIN (x))
5243 if (DECL_NAME (x) != 0)
5244 break;
5246 if (x == 0)
5248 if (TREE_CODE (t) == UNION_TYPE)
5250 if (fieldlist)
5251 pedwarn ("union has no named members");
5252 else
5253 pedwarn ("union has no members");
5255 else
5257 if (fieldlist)
5258 pedwarn ("struct has no named members");
5259 else
5260 pedwarn ("struct has no members");
5265 /* Install struct as DECL_CONTEXT of each field decl.
5266 Also process specified field sizes, found in the DECL_INITIAL,
5267 storing 0 there after the type has been changed to precision equal
5268 to its width, rather than the precision of the specified standard
5269 type. (Correct layout requires the original type to have been preserved
5270 until now.) */
5272 saw_named_field = 0;
5273 for (x = fieldlist; x; x = TREE_CHAIN (x))
5275 DECL_CONTEXT (x) = t;
5276 DECL_PACKED (x) |= TYPE_PACKED (t);
5278 /* If any field is const, the structure type is pseudo-const. */
5279 if (TREE_READONLY (x))
5280 C_TYPE_FIELDS_READONLY (t) = 1;
5281 else
5283 /* A field that is pseudo-const makes the structure likewise. */
5284 tree t1 = TREE_TYPE (x);
5285 while (TREE_CODE (t1) == ARRAY_TYPE)
5286 t1 = TREE_TYPE (t1);
5287 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5288 && C_TYPE_FIELDS_READONLY (t1))
5289 C_TYPE_FIELDS_READONLY (t) = 1;
5292 /* Any field that is volatile means variables of this type must be
5293 treated in some ways as volatile. */
5294 if (TREE_THIS_VOLATILE (x))
5295 C_TYPE_FIELDS_VOLATILE (t) = 1;
5297 /* Any field of nominal variable size implies structure is too. */
5298 if (C_DECL_VARIABLE_SIZE (x))
5299 C_TYPE_VARIABLE_SIZE (t) = 1;
5301 if (DECL_INITIAL (x))
5303 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5304 DECL_SIZE (x) = bitsize_int (width);
5305 DECL_BIT_FIELD (x) = 1;
5306 SET_DECL_C_BIT_FIELD (x);
5309 /* Detect flexible array member in an invalid context. */
5310 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5311 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5312 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5313 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5315 if (TREE_CODE (t) == UNION_TYPE)
5317 error ("%Jflexible array member in union", x);
5318 TREE_TYPE (x) = error_mark_node;
5320 else if (TREE_CHAIN (x) != NULL_TREE)
5322 error ("%Jflexible array member not at end of struct", x);
5323 TREE_TYPE (x) = error_mark_node;
5325 else if (!saw_named_field)
5327 error ("%Jflexible array member in otherwise empty struct", x);
5328 TREE_TYPE (x) = error_mark_node;
5332 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5333 && flexible_array_type_p (TREE_TYPE (x)))
5334 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5336 if (DECL_NAME (x))
5337 saw_named_field = 1;
5340 detect_field_duplicates (fieldlist);
5342 /* Now we have the nearly final fieldlist. Record it,
5343 then lay out the structure or union (including the fields). */
5345 TYPE_FIELDS (t) = fieldlist;
5347 layout_type (t);
5349 /* Give bit-fields their proper types. */
5351 tree *fieldlistp = &fieldlist;
5352 while (*fieldlistp)
5353 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5354 && TREE_TYPE (*fieldlistp) != error_mark_node)
5356 unsigned HOST_WIDE_INT width
5357 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5358 tree type = TREE_TYPE (*fieldlistp);
5359 if (width != TYPE_PRECISION (type))
5361 TREE_TYPE (*fieldlistp)
5362 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5363 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5365 DECL_INITIAL (*fieldlistp) = 0;
5367 else
5368 fieldlistp = &TREE_CHAIN (*fieldlistp);
5371 /* Now we have the truly final field list.
5372 Store it in this type and in the variants. */
5374 TYPE_FIELDS (t) = fieldlist;
5376 /* If there are lots of fields, sort so we can look through them fast.
5377 We arbitrarily consider 16 or more elts to be "a lot". */
5380 int len = 0;
5382 for (x = fieldlist; x; x = TREE_CHAIN (x))
5384 if (len > 15 || DECL_NAME (x) == NULL)
5385 break;
5386 len += 1;
5389 if (len > 15)
5391 tree *field_array;
5392 struct lang_type *space;
5393 struct sorted_fields_type *space2;
5395 len += list_length (x);
5397 /* Use the same allocation policy here that make_node uses, to
5398 ensure that this lives as long as the rest of the struct decl.
5399 All decls in an inline function need to be saved. */
5401 space = GGC_CNEW (struct lang_type);
5402 space2 = GGC_NEWVAR (struct sorted_fields_type,
5403 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5405 len = 0;
5406 space->s = space2;
5407 field_array = &space2->elts[0];
5408 for (x = fieldlist; x; x = TREE_CHAIN (x))
5410 field_array[len++] = x;
5412 /* If there is anonymous struct or union, break out of the loop. */
5413 if (DECL_NAME (x) == NULL)
5414 break;
5416 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5417 if (x == NULL)
5419 TYPE_LANG_SPECIFIC (t) = space;
5420 TYPE_LANG_SPECIFIC (t)->s->len = len;
5421 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5422 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5427 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5429 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5430 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5431 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5432 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5435 /* If this was supposed to be a transparent union, but we can't
5436 make it one, warn and turn off the flag. */
5437 if (TREE_CODE (t) == UNION_TYPE
5438 && TYPE_TRANSPARENT_UNION (t)
5439 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5441 TYPE_TRANSPARENT_UNION (t) = 0;
5442 warning (0, "union cannot be made transparent");
5445 /* If this structure or union completes the type of any previous
5446 variable declaration, lay it out and output its rtl. */
5447 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5449 x = TREE_CHAIN (x))
5451 tree decl = TREE_VALUE (x);
5452 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5453 layout_array_type (TREE_TYPE (decl));
5454 if (TREE_CODE (decl) != TYPE_DECL)
5456 layout_decl (decl, 0);
5457 if (c_dialect_objc ())
5458 objc_check_decl (decl);
5459 rest_of_decl_compilation (decl, toplevel, 0);
5460 if (!toplevel)
5461 expand_decl (decl);
5464 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5466 /* Finish debugging output for this type. */
5467 rest_of_type_compilation (t, toplevel);
5469 /* If we're inside a function proper, i.e. not file-scope and not still
5470 parsing parameters, then arrange for the size of a variable sized type
5471 to be bound now. */
5472 if (cur_stmt_list && variably_modified_type_p (t, NULL))
5473 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5475 return t;
5478 /* Lay out the type T, and its element type, and so on. */
5480 static void
5481 layout_array_type (tree t)
5483 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5484 layout_array_type (TREE_TYPE (t));
5485 layout_type (t);
5488 /* Begin compiling the definition of an enumeration type.
5489 NAME is its name (or null if anonymous).
5490 Returns the type object, as yet incomplete.
5491 Also records info about it so that build_enumerator
5492 may be used to declare the individual values as they are read. */
5494 tree
5495 start_enum (tree name)
5497 tree enumtype = 0;
5499 /* If this is the real definition for a previous forward reference,
5500 fill in the contents in the same object that used to be the
5501 forward reference. */
5503 if (name != 0)
5504 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5506 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5508 enumtype = make_node (ENUMERAL_TYPE);
5509 pushtag (name, enumtype);
5512 if (C_TYPE_BEING_DEFINED (enumtype))
5513 error ("nested redefinition of %<enum %E%>", name);
5515 C_TYPE_BEING_DEFINED (enumtype) = 1;
5517 if (TYPE_VALUES (enumtype) != 0)
5519 /* This enum is a named one that has been declared already. */
5520 error ("redeclaration of %<enum %E%>", name);
5522 /* Completely replace its old definition.
5523 The old enumerators remain defined, however. */
5524 TYPE_VALUES (enumtype) = 0;
5527 enum_next_value = integer_zero_node;
5528 enum_overflow = 0;
5530 if (flag_short_enums)
5531 TYPE_PACKED (enumtype) = 1;
5533 return enumtype;
5536 /* After processing and defining all the values of an enumeration type,
5537 install their decls in the enumeration type and finish it off.
5538 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5539 and ATTRIBUTES are the specified attributes.
5540 Returns ENUMTYPE. */
5542 tree
5543 finish_enum (tree enumtype, tree values, tree attributes)
5545 tree pair, tem;
5546 tree minnode = 0, maxnode = 0;
5547 int precision, unsign;
5548 bool toplevel = (file_scope == current_scope);
5549 struct lang_type *lt;
5551 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5553 /* Calculate the maximum value of any enumerator in this type. */
5555 if (values == error_mark_node)
5556 minnode = maxnode = integer_zero_node;
5557 else
5559 minnode = maxnode = TREE_VALUE (values);
5560 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5562 tree value = TREE_VALUE (pair);
5563 if (tree_int_cst_lt (maxnode, value))
5564 maxnode = value;
5565 if (tree_int_cst_lt (value, minnode))
5566 minnode = value;
5570 /* Construct the final type of this enumeration. It is the same
5571 as one of the integral types - the narrowest one that fits, except
5572 that normally we only go as narrow as int - and signed iff any of
5573 the values are negative. */
5574 unsign = (tree_int_cst_sgn (minnode) >= 0);
5575 precision = MAX (min_precision (minnode, unsign),
5576 min_precision (maxnode, unsign));
5578 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5580 tem = c_common_type_for_size (precision, unsign);
5581 if (tem == NULL)
5583 warning (0, "enumeration values exceed range of largest integer");
5584 tem = long_long_integer_type_node;
5587 else
5588 tem = unsign ? unsigned_type_node : integer_type_node;
5590 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5591 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5592 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5593 TYPE_SIZE (enumtype) = 0;
5595 /* If the precision of the type was specific with an attribute and it
5596 was too small, give an error. Otherwise, use it. */
5597 if (TYPE_PRECISION (enumtype))
5599 if (precision > TYPE_PRECISION (enumtype))
5600 error ("specified mode too small for enumeral values");
5602 else
5603 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5605 layout_type (enumtype);
5607 if (values != error_mark_node)
5609 /* Change the type of the enumerators to be the enum type. We
5610 need to do this irrespective of the size of the enum, for
5611 proper type checking. Replace the DECL_INITIALs of the
5612 enumerators, and the value slots of the list, with copies
5613 that have the enum type; they cannot be modified in place
5614 because they may be shared (e.g. integer_zero_node) Finally,
5615 change the purpose slots to point to the names of the decls. */
5616 for (pair = values; pair; pair = TREE_CHAIN (pair))
5618 tree enu = TREE_PURPOSE (pair);
5619 tree ini = DECL_INITIAL (enu);
5621 TREE_TYPE (enu) = enumtype;
5623 /* The ISO C Standard mandates enumerators to have type int,
5624 even though the underlying type of an enum type is
5625 unspecified. Here we convert any enumerators that fit in
5626 an int to type int, to avoid promotions to unsigned types
5627 when comparing integers with enumerators that fit in the
5628 int range. When -pedantic is given, build_enumerator()
5629 would have already taken care of those that don't fit. */
5630 if (int_fits_type_p (ini, integer_type_node))
5631 tem = integer_type_node;
5632 else
5633 tem = enumtype;
5634 ini = convert (tem, ini);
5636 DECL_INITIAL (enu) = ini;
5637 TREE_PURPOSE (pair) = DECL_NAME (enu);
5638 TREE_VALUE (pair) = ini;
5641 TYPE_VALUES (enumtype) = values;
5644 /* Record the min/max values so that we can warn about bit-field
5645 enumerations that are too small for the values. */
5646 lt = GGC_CNEW (struct lang_type);
5647 lt->enum_min = minnode;
5648 lt->enum_max = maxnode;
5649 TYPE_LANG_SPECIFIC (enumtype) = lt;
5651 /* Fix up all variant types of this enum type. */
5652 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5654 if (tem == enumtype)
5655 continue;
5656 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5657 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5658 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5659 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5660 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5661 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5662 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5663 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5664 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5665 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5666 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5669 /* Finish debugging output for this type. */
5670 rest_of_type_compilation (enumtype, toplevel);
5672 return enumtype;
5675 /* Build and install a CONST_DECL for one value of the
5676 current enumeration type (one that was begun with start_enum).
5677 Return a tree-list containing the CONST_DECL and its value.
5678 Assignment of sequential values by default is handled here. */
5680 tree
5681 build_enumerator (tree name, tree value)
5683 tree decl, type;
5685 /* Validate and default VALUE. */
5687 if (value != 0)
5689 /* Don't issue more errors for error_mark_node (i.e. an
5690 undeclared identifier) - just ignore the value expression. */
5691 if (value == error_mark_node)
5692 value = 0;
5693 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5694 || TREE_CODE (value) != INTEGER_CST)
5696 error ("enumerator value for %qE is not an integer constant", name);
5697 value = 0;
5699 else
5701 value = default_conversion (value);
5702 constant_expression_warning (value);
5706 /* Default based on previous value. */
5707 /* It should no longer be possible to have NON_LVALUE_EXPR
5708 in the default. */
5709 if (value == 0)
5711 value = enum_next_value;
5712 if (enum_overflow)
5713 error ("overflow in enumeration values");
5716 if (pedantic && !int_fits_type_p (value, integer_type_node))
5718 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5719 /* XXX This causes -pedantic to change the meaning of the program.
5720 Remove? -zw 2004-03-15 */
5721 value = convert (integer_type_node, value);
5724 /* Set basis for default for next value. */
5725 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5726 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5728 /* Now create a declaration for the enum value name. */
5730 type = TREE_TYPE (value);
5731 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5732 TYPE_PRECISION (integer_type_node)),
5733 (TYPE_PRECISION (type)
5734 >= TYPE_PRECISION (integer_type_node)
5735 && TYPE_UNSIGNED (type)));
5737 decl = build_decl (CONST_DECL, name, type);
5738 DECL_INITIAL (decl) = convert (type, value);
5739 pushdecl (decl);
5741 return tree_cons (decl, value, NULL_TREE);
5745 /* Create the FUNCTION_DECL for a function definition.
5746 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5747 the declaration; they describe the function's name and the type it returns,
5748 but twisted together in a fashion that parallels the syntax of C.
5750 This function creates a binding context for the function body
5751 as well as setting up the FUNCTION_DECL in current_function_decl.
5753 Returns 1 on success. If the DECLARATOR is not suitable for a function
5754 (it defines a datum instead), we return 0, which tells
5755 yyparse to report a parse error. */
5758 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5759 tree attributes)
5761 tree decl1, old_decl;
5762 tree restype, resdecl;
5763 struct c_label_context_se *nstack_se;
5764 struct c_label_context_vm *nstack_vm;
5766 current_function_returns_value = 0; /* Assume, until we see it does. */
5767 current_function_returns_null = 0;
5768 current_function_returns_abnormally = 0;
5769 warn_about_return_type = 0;
5770 current_extern_inline = 0;
5771 c_switch_stack = NULL;
5773 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
5774 nstack_se->labels_def = NULL;
5775 nstack_se->labels_used = NULL;
5776 nstack_se->next = label_context_stack_se;
5777 label_context_stack_se = nstack_se;
5779 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
5780 nstack_vm->labels_def = NULL;
5781 nstack_vm->labels_used = NULL;
5782 nstack_vm->scope = 0;
5783 nstack_vm->next = label_context_stack_vm;
5784 label_context_stack_vm = nstack_vm;
5786 /* Indicate no valid break/continue context by setting these variables
5787 to some non-null, non-label value. We'll notice and emit the proper
5788 error message in c_finish_bc_stmt. */
5789 c_break_label = c_cont_label = size_zero_node;
5791 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5793 /* If the declarator is not suitable for a function definition,
5794 cause a syntax error. */
5795 if (decl1 == 0)
5796 return 0;
5798 decl_attributes (&decl1, attributes, 0);
5800 if (DECL_DECLARED_INLINE_P (decl1)
5801 && DECL_UNINLINABLE (decl1)
5802 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5803 warning (0, "%Jinline function %qD given attribute noinline", decl1, decl1);
5805 announce_function (decl1);
5807 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5809 error ("return type is an incomplete type");
5810 /* Make it return void instead. */
5811 TREE_TYPE (decl1)
5812 = build_function_type (void_type_node,
5813 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5816 if (warn_about_return_type)
5817 pedwarn_c99 ("return type defaults to %<int%>");
5819 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5820 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5821 DECL_INITIAL (decl1) = error_mark_node;
5823 /* If this definition isn't a prototype and we had a prototype declaration
5824 before, copy the arg type info from that prototype. */
5825 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5826 current_function_prototype_locus = UNKNOWN_LOCATION;
5827 current_function_prototype_built_in = false;
5828 current_function_prototype_arg_types = NULL_TREE;
5829 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5831 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5832 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5833 TREE_TYPE (TREE_TYPE (old_decl))))
5835 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5836 TREE_TYPE (decl1));
5837 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5838 current_function_prototype_built_in
5839 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
5840 current_function_prototype_arg_types
5841 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
5843 if (TREE_PUBLIC (decl1))
5845 /* If there is an external prototype declaration of this
5846 function, record its location but do not copy information
5847 to this decl. This may be an invisible declaration
5848 (built-in or in a scope which has finished) or simply
5849 have more refined argument types than any declaration
5850 found above. */
5851 struct c_binding *b;
5852 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
5853 if (B_IN_SCOPE (b, external_scope))
5854 break;
5855 if (b)
5857 tree ext_decl, ext_type;
5858 ext_decl = b->decl;
5859 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
5860 if (TREE_CODE (ext_type) == FUNCTION_TYPE
5861 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5862 TREE_TYPE (ext_type)))
5864 current_function_prototype_locus
5865 = DECL_SOURCE_LOCATION (ext_decl);
5866 current_function_prototype_built_in
5867 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
5868 current_function_prototype_arg_types
5869 = TYPE_ARG_TYPES (ext_type);
5875 /* Optionally warn of old-fashioned def with no previous prototype. */
5876 if (warn_strict_prototypes
5877 && old_decl != error_mark_node
5878 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5879 && C_DECL_ISNT_PROTOTYPE (old_decl))
5880 warning (0, "function declaration isn%'t a prototype");
5881 /* Optionally warn of any global def with no previous prototype. */
5882 else if (warn_missing_prototypes
5883 && old_decl != error_mark_node
5884 && TREE_PUBLIC (decl1)
5885 && !MAIN_NAME_P (DECL_NAME (decl1))
5886 && C_DECL_ISNT_PROTOTYPE (old_decl))
5887 warning (0, "%Jno previous prototype for %qD", decl1, decl1);
5888 /* Optionally warn of any def with no previous prototype
5889 if the function has already been used. */
5890 else if (warn_missing_prototypes
5891 && old_decl != 0
5892 && old_decl != error_mark_node
5893 && TREE_USED (old_decl)
5894 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5895 warning (0, "%J%qD was used with no prototype before its definition",
5896 decl1, decl1);
5897 /* Optionally warn of any global def with no previous declaration. */
5898 else if (warn_missing_declarations
5899 && TREE_PUBLIC (decl1)
5900 && old_decl == 0
5901 && !MAIN_NAME_P (DECL_NAME (decl1)))
5902 warning (0, "%Jno previous declaration for %qD", decl1, decl1);
5903 /* Optionally warn of any def with no previous declaration
5904 if the function has already been used. */
5905 else if (warn_missing_declarations
5906 && old_decl != 0
5907 && old_decl != error_mark_node
5908 && TREE_USED (old_decl)
5909 && C_DECL_IMPLICIT (old_decl))
5910 warning (0, "%J%qD was used with no declaration before its definition",
5911 decl1, decl1);
5913 /* This is a definition, not a reference.
5914 So normally clear DECL_EXTERNAL.
5915 However, `extern inline' acts like a declaration
5916 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5917 DECL_EXTERNAL (decl1) = current_extern_inline;
5919 /* This function exists in static storage.
5920 (This does not mean `static' in the C sense!) */
5921 TREE_STATIC (decl1) = 1;
5923 /* A nested function is not global. */
5924 if (current_function_decl != 0)
5925 TREE_PUBLIC (decl1) = 0;
5927 /* This is the earliest point at which we might know the assembler
5928 name of the function. Thus, if it's set before this, die horribly. */
5929 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5931 /* If #pragma weak was used, mark the decl weak now. */
5932 if (current_scope == file_scope)
5933 maybe_apply_pragma_weak (decl1);
5935 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5936 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5938 tree args;
5939 int argct = 0;
5941 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5942 != integer_type_node)
5943 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5945 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5946 args = TREE_CHAIN (args))
5948 tree type = args ? TREE_VALUE (args) : 0;
5950 if (type == void_type_node)
5951 break;
5953 ++argct;
5954 switch (argct)
5956 case 1:
5957 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5958 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5959 decl1, decl1);
5960 break;
5962 case 2:
5963 if (TREE_CODE (type) != POINTER_TYPE
5964 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5965 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5966 != char_type_node))
5967 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5968 decl1, decl1);
5969 break;
5971 case 3:
5972 if (TREE_CODE (type) != POINTER_TYPE
5973 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5974 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5975 != char_type_node))
5976 pedwarn ("%Jthird argument of %qD should probably be "
5977 "%<char **%>", decl1, decl1);
5978 break;
5982 /* It is intentional that this message does not mention the third
5983 argument because it's only mentioned in an appendix of the
5984 standard. */
5985 if (argct > 0 && (argct < 2 || argct > 3))
5986 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
5988 if (!TREE_PUBLIC (decl1))
5989 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
5992 /* Record the decl so that the function name is defined.
5993 If we already have a decl for this name, and it is a FUNCTION_DECL,
5994 use the old decl. */
5996 current_function_decl = pushdecl (decl1);
5998 push_scope ();
5999 declare_parm_level ();
6001 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6002 /* Promote the value to int before returning it. */
6003 if (c_promoting_integer_type_p (restype))
6005 /* It retains unsignedness if not really getting wider. */
6006 if (TYPE_UNSIGNED (restype)
6007 && (TYPE_PRECISION (restype)
6008 == TYPE_PRECISION (integer_type_node)))
6009 restype = unsigned_type_node;
6010 else
6011 restype = integer_type_node;
6014 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6015 DECL_ARTIFICIAL (resdecl) = 1;
6016 DECL_IGNORED_P (resdecl) = 1;
6017 DECL_RESULT (current_function_decl) = resdecl;
6019 start_fname_decls ();
6021 return 1;
6024 /* Subroutine of store_parm_decls which handles new-style function
6025 definitions (prototype format). The parms already have decls, so we
6026 need only record them as in effect and complain if any redundant
6027 old-style parm decls were written. */
6028 static void
6029 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6031 tree decl;
6033 if (current_scope->bindings)
6035 error ("%Jold-style parameter declarations in prototyped "
6036 "function definition", fndecl);
6038 /* Get rid of the old-style declarations. */
6039 pop_scope ();
6040 push_scope ();
6042 /* Don't issue this warning for nested functions, and don't issue this
6043 warning if we got here because ARG_INFO_TYPES was error_mark_node
6044 (this happens when a function definition has just an ellipsis in
6045 its parameter list). */
6046 else if (warn_traditional && !in_system_header && !current_function_scope
6047 && arg_info->types != error_mark_node)
6048 warning (0, "%Jtraditional C rejects ISO C style function definitions",
6049 fndecl);
6051 /* Now make all the parameter declarations visible in the function body.
6052 We can bypass most of the grunt work of pushdecl. */
6053 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6055 DECL_CONTEXT (decl) = current_function_decl;
6056 if (DECL_NAME (decl))
6057 bind (DECL_NAME (decl), decl, current_scope,
6058 /*invisible=*/false, /*nested=*/false);
6059 else
6060 error ("%Jparameter name omitted", decl);
6063 /* Record the parameter list in the function declaration. */
6064 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6066 /* Now make all the ancillary declarations visible, likewise. */
6067 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6069 DECL_CONTEXT (decl) = current_function_decl;
6070 if (DECL_NAME (decl))
6071 bind (DECL_NAME (decl), decl, current_scope,
6072 /*invisible=*/false, /*nested=*/false);
6075 /* And all the tag declarations. */
6076 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6077 if (TREE_PURPOSE (decl))
6078 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6079 /*invisible=*/false, /*nested=*/false);
6082 /* Subroutine of store_parm_decls which handles old-style function
6083 definitions (separate parameter list and declarations). */
6085 static void
6086 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6088 struct c_binding *b;
6089 tree parm, decl, last;
6090 tree parmids = arg_info->parms;
6092 /* We use DECL_WEAK as a flag to show which parameters have been
6093 seen already, since it is not used on PARM_DECL. */
6094 #ifdef ENABLE_CHECKING
6095 for (b = current_scope->bindings; b; b = b->prev)
6096 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
6097 #endif
6099 if (!in_system_header)
6100 warning (OPT_Wold_style_definition, "%Jold-style function definition", fndecl);
6102 /* Match each formal parameter name with its declaration. Save each
6103 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6104 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6106 if (TREE_VALUE (parm) == 0)
6108 error ("%Jparameter name missing from parameter list", fndecl);
6109 TREE_PURPOSE (parm) = 0;
6110 continue;
6113 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6114 if (b && B_IN_CURRENT_SCOPE (b))
6116 decl = b->decl;
6117 /* If we got something other than a PARM_DECL it is an error. */
6118 if (TREE_CODE (decl) != PARM_DECL)
6119 error ("%J%qD declared as a non-parameter", decl, decl);
6120 /* If the declaration is already marked, we have a duplicate
6121 name. Complain and ignore the duplicate. */
6122 else if (DECL_WEAK (decl))
6124 error ("%Jmultiple parameters named %qD", decl, decl);
6125 TREE_PURPOSE (parm) = 0;
6126 continue;
6128 /* If the declaration says "void", complain and turn it into
6129 an int. */
6130 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6132 error ("%Jparameter %qD declared with void type", decl, decl);
6133 TREE_TYPE (decl) = integer_type_node;
6134 DECL_ARG_TYPE (decl) = integer_type_node;
6135 layout_decl (decl, 0);
6138 /* If no declaration found, default to int. */
6139 else
6141 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6142 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6143 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6144 pushdecl (decl);
6146 if (flag_isoc99)
6147 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6148 else if (extra_warnings)
6149 warning (0, "%Jtype of %qD defaults to %<int%>", decl, decl);
6152 TREE_PURPOSE (parm) = decl;
6153 DECL_WEAK (decl) = 1;
6156 /* Now examine the parms chain for incomplete declarations
6157 and declarations with no corresponding names. */
6159 for (b = current_scope->bindings; b; b = b->prev)
6161 parm = b->decl;
6162 if (TREE_CODE (parm) != PARM_DECL)
6163 continue;
6165 if (TREE_TYPE (parm) != error_mark_node
6166 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6168 error ("%Jparameter %qD has incomplete type", parm, parm);
6169 TREE_TYPE (parm) = error_mark_node;
6172 if (!DECL_WEAK (parm))
6174 error ("%Jdeclaration for parameter %qD but no such parameter",
6175 parm, parm);
6177 /* Pretend the parameter was not missing.
6178 This gets us to a standard state and minimizes
6179 further error messages. */
6180 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6184 /* Chain the declarations together in the order of the list of
6185 names. Store that chain in the function decl, replacing the
6186 list of names. Update the current scope to match. */
6187 DECL_ARGUMENTS (fndecl) = 0;
6189 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6190 if (TREE_PURPOSE (parm))
6191 break;
6192 if (parm && TREE_PURPOSE (parm))
6194 last = TREE_PURPOSE (parm);
6195 DECL_ARGUMENTS (fndecl) = last;
6196 DECL_WEAK (last) = 0;
6198 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6199 if (TREE_PURPOSE (parm))
6201 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6202 last = TREE_PURPOSE (parm);
6203 DECL_WEAK (last) = 0;
6205 TREE_CHAIN (last) = 0;
6208 /* If there was a previous prototype,
6209 set the DECL_ARG_TYPE of each argument according to
6210 the type previously specified, and report any mismatches. */
6212 if (current_function_prototype_arg_types)
6214 tree type;
6215 for (parm = DECL_ARGUMENTS (fndecl),
6216 type = current_function_prototype_arg_types;
6217 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6218 != void_type_node));
6219 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6221 if (parm == 0 || type == 0
6222 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6224 if (current_function_prototype_built_in)
6225 warning (0, "number of arguments doesn%'t match "
6226 "built-in prototype");
6227 else
6229 error ("number of arguments doesn%'t match prototype");
6230 error ("%Hprototype declaration",
6231 &current_function_prototype_locus);
6233 break;
6235 /* Type for passing arg must be consistent with that
6236 declared for the arg. ISO C says we take the unqualified
6237 type for parameters declared with qualified type. */
6238 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6239 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6241 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6242 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6244 /* Adjust argument to match prototype. E.g. a previous
6245 `int foo(float);' prototype causes
6246 `int foo(x) float x; {...}' to be treated like
6247 `int foo(float x) {...}'. This is particularly
6248 useful for argument types like uid_t. */
6249 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6251 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6252 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6253 && TYPE_PRECISION (TREE_TYPE (parm))
6254 < TYPE_PRECISION (integer_type_node))
6255 DECL_ARG_TYPE (parm) = integer_type_node;
6257 if (pedantic)
6259 /* ??? Is it possible to get here with a
6260 built-in prototype or will it always have
6261 been diagnosed as conflicting with an
6262 old-style definition and discarded? */
6263 if (current_function_prototype_built_in)
6264 warning (0, "promoted argument %qD "
6265 "doesn%'t match built-in prototype", parm);
6266 else
6268 pedwarn ("promoted argument %qD "
6269 "doesn%'t match prototype", parm);
6270 pedwarn ("%Hprototype declaration",
6271 &current_function_prototype_locus);
6275 else
6277 if (current_function_prototype_built_in)
6278 warning (0, "argument %qD doesn%'t match "
6279 "built-in prototype", parm);
6280 else
6282 error ("argument %qD doesn%'t match prototype", parm);
6283 error ("%Hprototype declaration",
6284 &current_function_prototype_locus);
6289 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6292 /* Otherwise, create a prototype that would match. */
6294 else
6296 tree actual = 0, last = 0, type;
6298 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6300 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6301 if (last)
6302 TREE_CHAIN (last) = type;
6303 else
6304 actual = type;
6305 last = type;
6307 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6308 if (last)
6309 TREE_CHAIN (last) = type;
6310 else
6311 actual = type;
6313 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6314 of the type of this function, but we need to avoid having this
6315 affect the types of other similarly-typed functions, so we must
6316 first force the generation of an identical (but separate) type
6317 node for the relevant function type. The new node we create
6318 will be a variant of the main variant of the original function
6319 type. */
6321 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6323 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6327 /* Store parameter declarations passed in ARG_INFO into the current
6328 function declaration. */
6330 void
6331 store_parm_decls_from (struct c_arg_info *arg_info)
6333 current_function_arg_info = arg_info;
6334 store_parm_decls ();
6337 /* Store the parameter declarations into the current function declaration.
6338 This is called after parsing the parameter declarations, before
6339 digesting the body of the function.
6341 For an old-style definition, construct a prototype out of the old-style
6342 parameter declarations and inject it into the function's type. */
6344 void
6345 store_parm_decls (void)
6347 tree fndecl = current_function_decl;
6348 bool proto;
6350 /* The argument information block for FNDECL. */
6351 struct c_arg_info *arg_info = current_function_arg_info;
6352 current_function_arg_info = 0;
6354 /* True if this definition is written with a prototype. Note:
6355 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6356 list in a function definition as equivalent to (void) -- an
6357 empty argument list specifies the function has no parameters,
6358 but only (void) sets up a prototype for future calls. */
6359 proto = arg_info->types != 0;
6361 if (proto)
6362 store_parm_decls_newstyle (fndecl, arg_info);
6363 else
6364 store_parm_decls_oldstyle (fndecl, arg_info);
6366 /* The next call to push_scope will be a function body. */
6368 next_is_function_body = true;
6370 /* Write a record describing this function definition to the prototypes
6371 file (if requested). */
6373 gen_aux_info_record (fndecl, 1, 0, proto);
6375 /* Initialize the RTL code for the function. */
6376 allocate_struct_function (fndecl);
6378 /* Begin the statement tree for this function. */
6379 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6381 /* ??? Insert the contents of the pending sizes list into the function
6382 to be evaluated. The only reason left to have this is
6383 void foo(int n, int array[n++])
6384 because we throw away the array type in favor of a pointer type, and
6385 thus won't naturally see the SAVE_EXPR containing the increment. All
6386 other pending sizes would be handled by gimplify_parameters. */
6388 tree t;
6389 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6390 add_stmt (TREE_VALUE (t));
6393 /* Even though we're inside a function body, we still don't want to
6394 call expand_expr to calculate the size of a variable-sized array.
6395 We haven't necessarily assigned RTL to all variables yet, so it's
6396 not safe to try to expand expressions involving them. */
6397 cfun->x_dont_save_pending_sizes_p = 1;
6400 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6401 functions. */
6403 static void
6404 c_warn_unused_result_recursively (tree fndecl)
6406 struct cgraph_node *cgn;
6408 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6409 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6411 /* Finalize all nested functions now. */
6412 cgn = cgraph_node (fndecl);
6413 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6414 c_warn_unused_result_recursively (cgn->decl);
6417 /* Finish up a function declaration and compile that function
6418 all the way to assembler language output. The free the storage
6419 for the function definition.
6421 This is called after parsing the body of the function definition. */
6423 void
6424 finish_function (void)
6426 tree fndecl = current_function_decl;
6428 label_context_stack_se = label_context_stack_se->next;
6429 label_context_stack_vm = label_context_stack_vm->next;
6431 if (TREE_CODE (fndecl) == FUNCTION_DECL
6432 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6434 tree args = DECL_ARGUMENTS (fndecl);
6435 for (; args; args = TREE_CHAIN (args))
6437 tree type = TREE_TYPE (args);
6438 if (INTEGRAL_TYPE_P (type)
6439 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6440 DECL_ARG_TYPE (args) = integer_type_node;
6444 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6445 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6447 /* Must mark the RESULT_DECL as being in this function. */
6449 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6450 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6452 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6454 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6455 != integer_type_node)
6457 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6458 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6459 if (!warn_main)
6460 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6462 else
6464 if (flag_isoc99)
6466 tree stmt = c_finish_return (integer_zero_node);
6467 #ifdef USE_MAPPED_LOCATION
6468 /* Hack. We don't want the middle-end to warn that this return
6469 is unreachable, so we mark its location as special. Using
6470 UNKNOWN_LOCATION has the problem that it gets clobbered in
6471 annotate_one_with_locus. A cleaner solution might be to
6472 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6474 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6475 #else
6476 /* Hack. We don't want the middle-end to warn that this
6477 return is unreachable, so put the statement on the
6478 special line 0. */
6479 annotate_with_file_line (stmt, input_filename, 0);
6480 #endif
6485 /* Tie off the statement tree for this function. */
6486 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6488 finish_fname_decls ();
6490 /* Complain if there's just no return statement. */
6491 if (warn_return_type
6492 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6493 && !current_function_returns_value && !current_function_returns_null
6494 /* Don't complain if we are no-return. */
6495 && !current_function_returns_abnormally
6496 /* Don't warn for main(). */
6497 && !MAIN_NAME_P (DECL_NAME (fndecl))
6498 /* Or if they didn't actually specify a return type. */
6499 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6500 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6501 inline function, as we might never be compiled separately. */
6502 && DECL_INLINE (fndecl))
6504 warning (0, "no return statement in function returning non-void");
6505 TREE_NO_WARNING (fndecl) = 1;
6508 /* With just -Wextra, complain only if function returns both with
6509 and without a value. */
6510 if (extra_warnings
6511 && current_function_returns_value
6512 && current_function_returns_null)
6513 warning (0, "this function may return with or without a value");
6515 /* Store the end of the function, so that we get good line number
6516 info for the epilogue. */
6517 cfun->function_end_locus = input_location;
6519 /* If we don't have ctors/dtors sections, and this is a static
6520 constructor or destructor, it must be recorded now. */
6521 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6522 && !targetm.have_ctors_dtors)
6523 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6524 if (DECL_STATIC_DESTRUCTOR (fndecl)
6525 && !targetm.have_ctors_dtors)
6526 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6528 /* Finalize the ELF visibility for the function. */
6529 c_determine_visibility (fndecl);
6531 /* Genericize before inlining. Delay genericizing nested functions
6532 until their parent function is genericized. Since finalizing
6533 requires GENERIC, delay that as well. */
6535 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6536 && !undef_nested_function)
6538 if (!decl_function_context (fndecl))
6540 c_genericize (fndecl);
6541 c_warn_unused_result_recursively (fndecl);
6543 /* ??? Objc emits functions after finalizing the compilation unit.
6544 This should be cleaned up later and this conditional removed. */
6545 if (cgraph_global_info_ready)
6547 c_expand_body (fndecl);
6548 return;
6551 cgraph_finalize_function (fndecl, false);
6553 else
6555 /* Register this function with cgraph just far enough to get it
6556 added to our parent's nested function list. Handy, since the
6557 C front end doesn't have such a list. */
6558 (void) cgraph_node (fndecl);
6562 if (!decl_function_context (fndecl))
6563 undef_nested_function = false;
6565 /* We're leaving the context of this function, so zap cfun.
6566 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6567 tree_rest_of_compilation. */
6568 cfun = NULL;
6569 current_function_decl = NULL;
6572 /* Generate the RTL for the body of FNDECL. */
6574 void
6575 c_expand_body (tree fndecl)
6578 if (!DECL_INITIAL (fndecl)
6579 || DECL_INITIAL (fndecl) == error_mark_node)
6580 return;
6582 tree_rest_of_compilation (fndecl);
6584 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6585 && targetm.have_ctors_dtors)
6586 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6587 DEFAULT_INIT_PRIORITY);
6588 if (DECL_STATIC_DESTRUCTOR (fndecl)
6589 && targetm.have_ctors_dtors)
6590 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6591 DEFAULT_INIT_PRIORITY);
6594 /* Check the declarations given in a for-loop for satisfying the C99
6595 constraints. */
6596 void
6597 check_for_loop_decls (void)
6599 struct c_binding *b;
6601 if (!flag_isoc99)
6603 /* If we get here, declarations have been used in a for loop without
6604 the C99 for loop scope. This doesn't make much sense, so don't
6605 allow it. */
6606 error ("%<for%> loop initial declaration used outside C99 mode");
6607 return;
6609 /* C99 subclause 6.8.5 paragraph 3:
6611 [#3] The declaration part of a for statement shall only
6612 declare identifiers for objects having storage class auto or
6613 register.
6615 It isn't clear whether, in this sentence, "identifiers" binds to
6616 "shall only declare" or to "objects" - that is, whether all identifiers
6617 declared must be identifiers for objects, or whether the restriction
6618 only applies to those that are. (A question on this in comp.std.c
6619 in November 2000 received no answer.) We implement the strictest
6620 interpretation, to avoid creating an extension which later causes
6621 problems. */
6623 for (b = current_scope->bindings; b; b = b->prev)
6625 tree id = b->id;
6626 tree decl = b->decl;
6628 if (!id)
6629 continue;
6631 switch (TREE_CODE (decl))
6633 case VAR_DECL:
6634 if (TREE_STATIC (decl))
6635 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6636 "initial declaration", decl, decl);
6637 else if (DECL_EXTERNAL (decl))
6638 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6639 "initial declaration", decl, decl);
6640 break;
6642 case RECORD_TYPE:
6643 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6644 id);
6645 break;
6646 case UNION_TYPE:
6647 error ("%<union %E%> declared in %<for%> loop initial declaration",
6648 id);
6649 break;
6650 case ENUMERAL_TYPE:
6651 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6652 id);
6653 break;
6654 default:
6655 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6656 "initial declaration", decl, decl);
6661 /* Save and reinitialize the variables
6662 used during compilation of a C function. */
6664 void
6665 c_push_function_context (struct function *f)
6667 struct language_function *p;
6668 p = GGC_NEW (struct language_function);
6669 f->language = p;
6671 p->base.x_stmt_tree = c_stmt_tree;
6672 p->x_break_label = c_break_label;
6673 p->x_cont_label = c_cont_label;
6674 p->x_switch_stack = c_switch_stack;
6675 p->arg_info = current_function_arg_info;
6676 p->returns_value = current_function_returns_value;
6677 p->returns_null = current_function_returns_null;
6678 p->returns_abnormally = current_function_returns_abnormally;
6679 p->warn_about_return_type = warn_about_return_type;
6680 p->extern_inline = current_extern_inline;
6683 /* Restore the variables used during compilation of a C function. */
6685 void
6686 c_pop_function_context (struct function *f)
6688 struct language_function *p = f->language;
6690 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6691 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6693 /* Stop pointing to the local nodes about to be freed. */
6694 /* But DECL_INITIAL must remain nonzero so we know this
6695 was an actual function definition. */
6696 DECL_INITIAL (current_function_decl) = error_mark_node;
6697 DECL_ARGUMENTS (current_function_decl) = 0;
6700 c_stmt_tree = p->base.x_stmt_tree;
6701 c_break_label = p->x_break_label;
6702 c_cont_label = p->x_cont_label;
6703 c_switch_stack = p->x_switch_stack;
6704 current_function_arg_info = p->arg_info;
6705 current_function_returns_value = p->returns_value;
6706 current_function_returns_null = p->returns_null;
6707 current_function_returns_abnormally = p->returns_abnormally;
6708 warn_about_return_type = p->warn_about_return_type;
6709 current_extern_inline = p->extern_inline;
6711 f->language = NULL;
6714 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6716 void
6717 c_dup_lang_specific_decl (tree decl)
6719 struct lang_decl *ld;
6721 if (!DECL_LANG_SPECIFIC (decl))
6722 return;
6724 ld = GGC_NEW (struct lang_decl);
6725 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6726 DECL_LANG_SPECIFIC (decl) = ld;
6729 /* The functions below are required for functionality of doing
6730 function at once processing in the C front end. Currently these
6731 functions are not called from anywhere in the C front end, but as
6732 these changes continue, that will change. */
6734 /* Returns nonzero if the current statement is a full expression,
6735 i.e. temporaries created during that statement should be destroyed
6736 at the end of the statement. */
6739 stmts_are_full_exprs_p (void)
6741 return 0;
6744 /* Returns the stmt_tree (if any) to which statements are currently
6745 being added. If there is no active statement-tree, NULL is
6746 returned. */
6748 stmt_tree
6749 current_stmt_tree (void)
6751 return &c_stmt_tree;
6754 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6755 C. */
6758 anon_aggr_type_p (tree ARG_UNUSED (node))
6760 return 0;
6763 /* Return the global value of T as a symbol. */
6765 tree
6766 identifier_global_value (tree t)
6768 struct c_binding *b;
6770 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6771 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6772 return b->decl;
6774 return 0;
6777 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6778 otherwise the name is found in ridpointers from RID_INDEX. */
6780 void
6781 record_builtin_type (enum rid rid_index, const char *name, tree type)
6783 tree id, decl;
6784 if (name == 0)
6785 id = ridpointers[(int) rid_index];
6786 else
6787 id = get_identifier (name);
6788 decl = build_decl (TYPE_DECL, id, type);
6789 pushdecl (decl);
6790 if (debug_hooks->type_decl)
6791 debug_hooks->type_decl (decl, false);
6794 /* Build the void_list_node (void_type_node having been created). */
6795 tree
6796 build_void_list_node (void)
6798 tree t = build_tree_list (NULL_TREE, void_type_node);
6799 return t;
6802 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6804 struct c_parm *
6805 build_c_parm (struct c_declspecs *specs, tree attrs,
6806 struct c_declarator *declarator)
6808 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6809 ret->specs = specs;
6810 ret->attrs = attrs;
6811 ret->declarator = declarator;
6812 return ret;
6815 /* Return a declarator with nested attributes. TARGET is the inner
6816 declarator to which these attributes apply. ATTRS are the
6817 attributes. */
6819 struct c_declarator *
6820 build_attrs_declarator (tree attrs, struct c_declarator *target)
6822 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6823 ret->kind = cdk_attrs;
6824 ret->declarator = target;
6825 ret->u.attrs = attrs;
6826 return ret;
6829 /* Return a declarator for a function with arguments specified by ARGS
6830 and return type specified by TARGET. */
6832 struct c_declarator *
6833 build_function_declarator (struct c_arg_info *args,
6834 struct c_declarator *target)
6836 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6837 ret->kind = cdk_function;
6838 ret->declarator = target;
6839 ret->u.arg_info = args;
6840 return ret;
6843 /* Return a declarator for the identifier IDENT (which may be
6844 NULL_TREE for an abstract declarator). */
6846 struct c_declarator *
6847 build_id_declarator (tree ident)
6849 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6850 ret->kind = cdk_id;
6851 ret->declarator = 0;
6852 ret->u.id = ident;
6853 /* Default value - may get reset to a more precise location. */
6854 ret->id_loc = input_location;
6855 return ret;
6858 /* Return something to represent absolute declarators containing a *.
6859 TARGET is the absolute declarator that the * contains.
6860 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6861 to apply to the pointer type. */
6863 struct c_declarator *
6864 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6865 struct c_declarator *target)
6867 tree attrs;
6868 int quals = 0;
6869 struct c_declarator *itarget = target;
6870 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6871 if (type_quals_attrs)
6873 attrs = type_quals_attrs->attrs;
6874 quals = quals_from_declspecs (type_quals_attrs);
6875 if (attrs != NULL_TREE)
6876 itarget = build_attrs_declarator (attrs, target);
6878 ret->kind = cdk_pointer;
6879 ret->declarator = itarget;
6880 ret->u.pointer_quals = quals;
6881 return ret;
6884 /* Return a pointer to a structure for an empty list of declaration
6885 specifiers. */
6887 struct c_declspecs *
6888 build_null_declspecs (void)
6890 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6891 ret->type = 0;
6892 ret->decl_attr = 0;
6893 ret->attrs = 0;
6894 ret->typespec_word = cts_none;
6895 ret->storage_class = csc_none;
6896 ret->declspecs_seen_p = false;
6897 ret->type_seen_p = false;
6898 ret->non_sc_seen_p = false;
6899 ret->typedef_p = false;
6900 ret->tag_defined_p = false;
6901 ret->explicit_signed_p = false;
6902 ret->deprecated_p = false;
6903 ret->default_int_p = false;
6904 ret->long_p = false;
6905 ret->long_long_p = false;
6906 ret->short_p = false;
6907 ret->signed_p = false;
6908 ret->unsigned_p = false;
6909 ret->complex_p = false;
6910 ret->inline_p = false;
6911 ret->thread_p = false;
6912 ret->const_p = false;
6913 ret->volatile_p = false;
6914 ret->restrict_p = false;
6915 return ret;
6918 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6919 returning SPECS. */
6921 struct c_declspecs *
6922 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6924 enum rid i;
6925 bool dupe = false;
6926 specs->non_sc_seen_p = true;
6927 specs->declspecs_seen_p = true;
6928 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6929 && C_IS_RESERVED_WORD (qual));
6930 i = C_RID_CODE (qual);
6931 switch (i)
6933 case RID_CONST:
6934 dupe = specs->const_p;
6935 specs->const_p = true;
6936 break;
6937 case RID_VOLATILE:
6938 dupe = specs->volatile_p;
6939 specs->volatile_p = true;
6940 break;
6941 case RID_RESTRICT:
6942 dupe = specs->restrict_p;
6943 specs->restrict_p = true;
6944 break;
6945 default:
6946 gcc_unreachable ();
6948 if (dupe && pedantic && !flag_isoc99)
6949 pedwarn ("duplicate %qE", qual);
6950 return specs;
6953 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6954 returning SPECS. */
6956 struct c_declspecs *
6957 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6959 tree type = spec.spec;
6960 specs->non_sc_seen_p = true;
6961 specs->declspecs_seen_p = true;
6962 specs->type_seen_p = true;
6963 if (TREE_DEPRECATED (type))
6964 specs->deprecated_p = true;
6966 /* Handle type specifier keywords. */
6967 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6969 enum rid i = C_RID_CODE (type);
6970 if (specs->type)
6972 error ("two or more data types in declaration specifiers");
6973 return specs;
6975 if ((int) i <= (int) RID_LAST_MODIFIER)
6977 /* "long", "short", "signed", "unsigned" or "_Complex". */
6978 bool dupe = false;
6979 switch (i)
6981 case RID_LONG:
6982 if (specs->long_long_p)
6984 error ("%<long long long%> is too long for GCC");
6985 break;
6987 if (specs->long_p)
6989 if (specs->typespec_word == cts_double)
6991 error ("both %<long long%> and %<double%> in "
6992 "declaration specifiers");
6993 break;
6995 if (pedantic && !flag_isoc99 && !in_system_header
6996 && warn_long_long)
6997 pedwarn ("ISO C90 does not support %<long long%>");
6998 specs->long_long_p = 1;
6999 break;
7001 if (specs->short_p)
7002 error ("both %<long%> and %<short%> in "
7003 "declaration specifiers");
7004 else if (specs->typespec_word == cts_void)
7005 error ("both %<long%> and %<void%> in "
7006 "declaration specifiers");
7007 else if (specs->typespec_word == cts_bool)
7008 error ("both %<long%> and %<_Bool%> in "
7009 "declaration specifiers");
7010 else if (specs->typespec_word == cts_char)
7011 error ("both %<long%> and %<char%> in "
7012 "declaration specifiers");
7013 else if (specs->typespec_word == cts_float)
7014 error ("both %<long%> and %<float%> in "
7015 "declaration specifiers");
7016 else
7017 specs->long_p = true;
7018 break;
7019 case RID_SHORT:
7020 dupe = specs->short_p;
7021 if (specs->long_p)
7022 error ("both %<long%> and %<short%> in "
7023 "declaration specifiers");
7024 else if (specs->typespec_word == cts_void)
7025 error ("both %<short%> and %<void%> in "
7026 "declaration specifiers");
7027 else if (specs->typespec_word == cts_bool)
7028 error ("both %<short%> and %<_Bool%> in "
7029 "declaration specifiers");
7030 else if (specs->typespec_word == cts_char)
7031 error ("both %<short%> and %<char%> in "
7032 "declaration specifiers");
7033 else if (specs->typespec_word == cts_float)
7034 error ("both %<short%> and %<float%> in "
7035 "declaration specifiers");
7036 else if (specs->typespec_word == cts_double)
7037 error ("both %<short%> and %<double%> in "
7038 "declaration specifiers");
7039 else
7040 specs->short_p = true;
7041 break;
7042 case RID_SIGNED:
7043 dupe = specs->signed_p;
7044 if (specs->unsigned_p)
7045 error ("both %<signed%> and %<unsigned%> in "
7046 "declaration specifiers");
7047 else if (specs->typespec_word == cts_void)
7048 error ("both %<signed%> and %<void%> in "
7049 "declaration specifiers");
7050 else if (specs->typespec_word == cts_bool)
7051 error ("both %<signed%> and %<_Bool%> in "
7052 "declaration specifiers");
7053 else if (specs->typespec_word == cts_float)
7054 error ("both %<signed%> and %<float%> in "
7055 "declaration specifiers");
7056 else if (specs->typespec_word == cts_double)
7057 error ("both %<signed%> and %<double%> in "
7058 "declaration specifiers");
7059 else
7060 specs->signed_p = true;
7061 break;
7062 case RID_UNSIGNED:
7063 dupe = specs->unsigned_p;
7064 if (specs->signed_p)
7065 error ("both %<signed%> and %<unsigned%> in "
7066 "declaration specifiers");
7067 else if (specs->typespec_word == cts_void)
7068 error ("both %<unsigned%> and %<void%> in "
7069 "declaration specifiers");
7070 else if (specs->typespec_word == cts_bool)
7071 error ("both %<unsigned%> and %<_Bool%> in "
7072 "declaration specifiers");
7073 else if (specs->typespec_word == cts_float)
7074 error ("both %<unsigned%> and %<float%> in "
7075 "declaration specifiers");
7076 else if (specs->typespec_word == cts_double)
7077 error ("both %<unsigned%> and %<double%> in "
7078 "declaration specifiers");
7079 else
7080 specs->unsigned_p = true;
7081 break;
7082 case RID_COMPLEX:
7083 dupe = specs->complex_p;
7084 if (pedantic && !flag_isoc99 && !in_system_header)
7085 pedwarn ("ISO C90 does not support complex types");
7086 if (specs->typespec_word == cts_void)
7087 error ("both %<complex%> and %<void%> in "
7088 "declaration specifiers");
7089 else if (specs->typespec_word == cts_bool)
7090 error ("both %<complex%> and %<_Bool%> in "
7091 "declaration specifiers");
7092 else
7093 specs->complex_p = true;
7094 break;
7095 default:
7096 gcc_unreachable ();
7099 if (dupe)
7100 error ("duplicate %qE", type);
7102 return specs;
7104 else
7106 /* "void", "_Bool", "char", "int", "float" or "double". */
7107 if (specs->typespec_word != cts_none)
7109 error ("two or more data types in declaration specifiers");
7110 return specs;
7112 switch (i)
7114 case RID_VOID:
7115 if (specs->long_p)
7116 error ("both %<long%> and %<void%> in "
7117 "declaration specifiers");
7118 else if (specs->short_p)
7119 error ("both %<short%> and %<void%> in "
7120 "declaration specifiers");
7121 else if (specs->signed_p)
7122 error ("both %<signed%> and %<void%> in "
7123 "declaration specifiers");
7124 else if (specs->unsigned_p)
7125 error ("both %<unsigned%> and %<void%> in "
7126 "declaration specifiers");
7127 else if (specs->complex_p)
7128 error ("both %<complex%> and %<void%> in "
7129 "declaration specifiers");
7130 else
7131 specs->typespec_word = cts_void;
7132 return specs;
7133 case RID_BOOL:
7134 if (specs->long_p)
7135 error ("both %<long%> and %<_Bool%> in "
7136 "declaration specifiers");
7137 else if (specs->short_p)
7138 error ("both %<short%> and %<_Bool%> in "
7139 "declaration specifiers");
7140 else if (specs->signed_p)
7141 error ("both %<signed%> and %<_Bool%> in "
7142 "declaration specifiers");
7143 else if (specs->unsigned_p)
7144 error ("both %<unsigned%> and %<_Bool%> in "
7145 "declaration specifiers");
7146 else if (specs->complex_p)
7147 error ("both %<complex%> and %<_Bool%> in "
7148 "declaration specifiers");
7149 else
7150 specs->typespec_word = cts_bool;
7151 return specs;
7152 case RID_CHAR:
7153 if (specs->long_p)
7154 error ("both %<long%> and %<char%> in "
7155 "declaration specifiers");
7156 else if (specs->short_p)
7157 error ("both %<short%> and %<char%> in "
7158 "declaration specifiers");
7159 else
7160 specs->typespec_word = cts_char;
7161 return specs;
7162 case RID_INT:
7163 specs->typespec_word = cts_int;
7164 return specs;
7165 case RID_FLOAT:
7166 if (specs->long_p)
7167 error ("both %<long%> and %<float%> in "
7168 "declaration specifiers");
7169 else if (specs->short_p)
7170 error ("both %<short%> and %<float%> in "
7171 "declaration specifiers");
7172 else if (specs->signed_p)
7173 error ("both %<signed%> and %<float%> in "
7174 "declaration specifiers");
7175 else if (specs->unsigned_p)
7176 error ("both %<unsigned%> and %<float%> in "
7177 "declaration specifiers");
7178 else
7179 specs->typespec_word = cts_float;
7180 return specs;
7181 case RID_DOUBLE:
7182 if (specs->long_long_p)
7183 error ("both %<long long%> and %<double%> in "
7184 "declaration specifiers");
7185 else if (specs->short_p)
7186 error ("both %<short%> and %<double%> in "
7187 "declaration specifiers");
7188 else if (specs->signed_p)
7189 error ("both %<signed%> and %<double%> in "
7190 "declaration specifiers");
7191 else if (specs->unsigned_p)
7192 error ("both %<unsigned%> and %<double%> in "
7193 "declaration specifiers");
7194 else
7195 specs->typespec_word = cts_double;
7196 return specs;
7197 default:
7198 /* ObjC reserved word "id", handled below. */
7199 break;
7204 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7205 form of ObjC type, cases such as "int" and "long" being handled
7206 above), a TYPE (struct, union, enum and typeof specifiers) or an
7207 ERROR_MARK. In none of these cases may there have previously
7208 been any type specifiers. */
7209 if (specs->type || specs->typespec_word != cts_none
7210 || specs->long_p || specs->short_p || specs->signed_p
7211 || specs->unsigned_p || specs->complex_p)
7212 error ("two or more data types in declaration specifiers");
7213 else if (TREE_CODE (type) == TYPE_DECL)
7215 if (TREE_TYPE (type) == error_mark_node)
7216 ; /* Allow the type to default to int to avoid cascading errors. */
7217 else
7219 specs->type = TREE_TYPE (type);
7220 specs->decl_attr = DECL_ATTRIBUTES (type);
7221 specs->typedef_p = true;
7222 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7225 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7227 tree t = lookup_name (type);
7228 if (!t || TREE_CODE (t) != TYPE_DECL)
7229 error ("%qE fails to be a typedef or built in type", type);
7230 else if (TREE_TYPE (t) == error_mark_node)
7232 else
7233 specs->type = TREE_TYPE (t);
7235 else if (TREE_CODE (type) != ERROR_MARK)
7237 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7238 specs->tag_defined_p = true;
7239 if (spec.kind == ctsk_typeof)
7240 specs->typedef_p = true;
7241 specs->type = type;
7244 return specs;
7247 /* Add the storage class specifier or function specifier SCSPEC to the
7248 declaration specifiers SPECS, returning SPECS. */
7250 struct c_declspecs *
7251 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7253 enum rid i;
7254 enum c_storage_class n = csc_none;
7255 bool dupe = false;
7256 specs->declspecs_seen_p = true;
7257 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7258 && C_IS_RESERVED_WORD (scspec));
7259 i = C_RID_CODE (scspec);
7260 if (extra_warnings && specs->non_sc_seen_p)
7261 warning (0, "%qE is not at beginning of declaration", scspec);
7262 switch (i)
7264 case RID_INLINE:
7265 /* C99 permits duplicate inline. Although of doubtful utility,
7266 it seems simplest to permit it in gnu89 mode as well, as
7267 there is also little utility in maintaining this as a
7268 difference between gnu89 and C99 inline. */
7269 dupe = false;
7270 specs->inline_p = true;
7271 break;
7272 case RID_THREAD:
7273 dupe = specs->thread_p;
7274 if (specs->storage_class == csc_auto)
7275 error ("%<__thread%> used with %<auto%>");
7276 else if (specs->storage_class == csc_register)
7277 error ("%<__thread%> used with %<register%>");
7278 else if (specs->storage_class == csc_typedef)
7279 error ("%<__thread%> used with %<typedef%>");
7280 else
7281 specs->thread_p = true;
7282 break;
7283 case RID_AUTO:
7284 n = csc_auto;
7285 break;
7286 case RID_EXTERN:
7287 n = csc_extern;
7288 /* Diagnose "__thread extern". */
7289 if (specs->thread_p)
7290 error ("%<__thread%> before %<extern%>");
7291 break;
7292 case RID_REGISTER:
7293 n = csc_register;
7294 break;
7295 case RID_STATIC:
7296 n = csc_static;
7297 /* Diagnose "__thread static". */
7298 if (specs->thread_p)
7299 error ("%<__thread%> before %<static%>");
7300 break;
7301 case RID_TYPEDEF:
7302 n = csc_typedef;
7303 break;
7304 default:
7305 gcc_unreachable ();
7307 if (n != csc_none && n == specs->storage_class)
7308 dupe = true;
7309 if (dupe)
7310 error ("duplicate %qE", scspec);
7311 if (n != csc_none)
7313 if (specs->storage_class != csc_none && n != specs->storage_class)
7315 error ("multiple storage classes in declaration specifiers");
7317 else
7319 specs->storage_class = n;
7320 if (n != csc_extern && n != csc_static && specs->thread_p)
7322 error ("%<__thread%> used with %qE", scspec);
7323 specs->thread_p = false;
7327 return specs;
7330 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7331 returning SPECS. */
7333 struct c_declspecs *
7334 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7336 specs->attrs = chainon (attrs, specs->attrs);
7337 specs->declspecs_seen_p = true;
7338 return specs;
7341 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7342 specifiers with any other type specifier to determine the resulting
7343 type. This is where ISO C checks on complex types are made, since
7344 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7345 double". */
7347 struct c_declspecs *
7348 finish_declspecs (struct c_declspecs *specs)
7350 /* If a type was specified as a whole, we have no modifiers and are
7351 done. */
7352 if (specs->type != NULL_TREE)
7354 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7355 && !specs->signed_p && !specs->unsigned_p
7356 && !specs->complex_p);
7357 return specs;
7360 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7361 has been specified, treat it as "int" unless "_Complex" is
7362 present and there are no other specifiers. If we just have
7363 "_Complex", it is equivalent to "_Complex double", but e.g.
7364 "_Complex short" is equivalent to "_Complex short int". */
7365 if (specs->typespec_word == cts_none)
7367 if (specs->long_p || specs->short_p
7368 || specs->signed_p || specs->unsigned_p)
7370 specs->typespec_word = cts_int;
7372 else if (specs->complex_p)
7374 specs->typespec_word = cts_double;
7375 if (pedantic)
7376 pedwarn ("ISO C does not support plain %<complex%> meaning "
7377 "%<double complex%>");
7379 else
7381 specs->typespec_word = cts_int;
7382 specs->default_int_p = true;
7383 /* We don't diagnose this here because grokdeclarator will
7384 give more specific diagnostics according to whether it is
7385 a function definition. */
7389 /* If "signed" was specified, record this to distinguish "int" and
7390 "signed int" in the case of a bit-field with
7391 -funsigned-bitfields. */
7392 specs->explicit_signed_p = specs->signed_p;
7394 /* Now compute the actual type. */
7395 switch (specs->typespec_word)
7397 case cts_void:
7398 gcc_assert (!specs->long_p && !specs->short_p
7399 && !specs->signed_p && !specs->unsigned_p
7400 && !specs->complex_p);
7401 specs->type = void_type_node;
7402 break;
7403 case cts_bool:
7404 gcc_assert (!specs->long_p && !specs->short_p
7405 && !specs->signed_p && !specs->unsigned_p
7406 && !specs->complex_p);
7407 specs->type = boolean_type_node;
7408 break;
7409 case cts_char:
7410 gcc_assert (!specs->long_p && !specs->short_p);
7411 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7412 if (specs->signed_p)
7413 specs->type = signed_char_type_node;
7414 else if (specs->unsigned_p)
7415 specs->type = unsigned_char_type_node;
7416 else
7417 specs->type = char_type_node;
7418 if (specs->complex_p)
7420 if (pedantic)
7421 pedwarn ("ISO C does not support complex integer types");
7422 specs->type = build_complex_type (specs->type);
7424 break;
7425 case cts_int:
7426 gcc_assert (!(specs->long_p && specs->short_p));
7427 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7428 if (specs->long_long_p)
7429 specs->type = (specs->unsigned_p
7430 ? long_long_unsigned_type_node
7431 : long_long_integer_type_node);
7432 else if (specs->long_p)
7433 specs->type = (specs->unsigned_p
7434 ? long_unsigned_type_node
7435 : long_integer_type_node);
7436 else if (specs->short_p)
7437 specs->type = (specs->unsigned_p
7438 ? short_unsigned_type_node
7439 : short_integer_type_node);
7440 else
7441 specs->type = (specs->unsigned_p
7442 ? unsigned_type_node
7443 : integer_type_node);
7444 if (specs->complex_p)
7446 if (pedantic)
7447 pedwarn ("ISO C does not support complex integer types");
7448 specs->type = build_complex_type (specs->type);
7450 break;
7451 case cts_float:
7452 gcc_assert (!specs->long_p && !specs->short_p
7453 && !specs->signed_p && !specs->unsigned_p);
7454 specs->type = (specs->complex_p
7455 ? complex_float_type_node
7456 : float_type_node);
7457 break;
7458 case cts_double:
7459 gcc_assert (!specs->long_long_p && !specs->short_p
7460 && !specs->signed_p && !specs->unsigned_p);
7461 if (specs->long_p)
7463 specs->type = (specs->complex_p
7464 ? complex_long_double_type_node
7465 : long_double_type_node);
7467 else
7469 specs->type = (specs->complex_p
7470 ? complex_double_type_node
7471 : double_type_node);
7473 break;
7474 default:
7475 gcc_unreachable ();
7478 return specs;
7481 /* Synthesize a function which calls all the global ctors or global
7482 dtors in this file. This is only used for targets which do not
7483 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7484 static void
7485 build_cdtor (int method_type, tree cdtors)
7487 tree body = 0;
7489 if (!cdtors)
7490 return;
7492 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7493 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7494 &body);
7496 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7499 /* Perform final processing on one file scope's declarations (or the
7500 external scope's declarations), GLOBALS. */
7501 static void
7502 c_write_global_declarations_1 (tree globals)
7504 size_t len = list_length (globals);
7505 tree *vec = XNEWVEC (tree, len);
7506 size_t i;
7507 tree decl;
7509 /* Process the decls in the order they were written. */
7510 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7512 vec[i] = decl;
7513 /* Check for used but undefined static functions using the C
7514 standard's definition of "used", and set TREE_NO_WARNING so
7515 that check_global_declarations doesn't repeat the check. */
7516 if (TREE_CODE (decl) == FUNCTION_DECL
7517 && DECL_INITIAL (decl) == 0
7518 && DECL_EXTERNAL (decl)
7519 && !TREE_PUBLIC (decl)
7520 && C_DECL_USED (decl))
7522 pedwarn ("%J%qF used but never defined", decl, decl);
7523 TREE_NO_WARNING (decl) = 1;
7527 wrapup_global_declarations (vec, len);
7528 check_global_declarations (vec, len);
7530 free (vec);
7533 void
7534 c_write_global_declarations (void)
7536 tree ext_block, t;
7538 /* We don't want to do this if generating a PCH. */
7539 if (pch_file)
7540 return;
7542 /* Don't waste time on further processing if -fsyntax-only or we've
7543 encountered errors. */
7544 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7545 return;
7547 /* Close the external scope. */
7548 ext_block = pop_scope ();
7549 external_scope = 0;
7550 gcc_assert (!current_scope);
7552 /* Process all file scopes in this compilation, and the external_scope,
7553 through wrapup_global_declarations and check_global_declarations. */
7554 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7555 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7556 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7558 /* Generate functions to call static constructors and destructors
7559 for targets that do not support .ctors/.dtors sections. These
7560 functions have magic names which are detected by collect2. */
7561 build_cdtor ('I', static_ctors); static_ctors = 0;
7562 build_cdtor ('D', static_dtors); static_dtors = 0;
7564 /* We're done parsing; proceed to optimize and emit assembly.
7565 FIXME: shouldn't be the front end's responsibility to call this. */
7566 cgraph_optimize ();
7569 #include "gt-c-decl.h"