jawt.c: Remove malloc.h, covered by stdlib.h.
[official-gcc.git] / gcc / c-decl.c
blobd36ceb563d2a706779794405493a886e460d58b7
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 /* T is a statement. Add it to the statement-tree. This is the
422 C/ObjC version--C++ has a slightly different version of this
423 function. */
425 tree
426 add_stmt (tree t)
428 enum tree_code code = TREE_CODE (t);
430 if (EXPR_P (t) && code != LABEL_EXPR)
432 if (!EXPR_HAS_LOCATION (t))
433 SET_EXPR_LOCATION (t, input_location);
436 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
437 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
439 /* Add T to the statement-tree. Non-side-effect statements need to be
440 recorded during statement expressions. */
441 append_to_statement_list_force (t, &cur_stmt_list);
443 return t;
446 /* States indicating how grokdeclarator() should handle declspecs marked
447 with __attribute__((deprecated)). An object declared as
448 __attribute__((deprecated)) suppresses warnings of uses of other
449 deprecated items. */
451 enum deprecated_states {
452 DEPRECATED_NORMAL,
453 DEPRECATED_SUPPRESS
456 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
458 void
459 c_print_identifier (FILE *file, tree node, int indent)
461 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
462 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
463 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
464 if (C_IS_RESERVED_WORD (node))
466 tree rid = ridpointers[C_RID_CODE (node)];
467 indent_to (file, indent + 4);
468 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
469 (void *) rid, IDENTIFIER_POINTER (rid));
473 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
474 which may be any of several kinds of DECL or TYPE or error_mark_node,
475 in the scope SCOPE. */
476 static void
477 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
479 struct c_binding *b, **here;
481 if (binding_freelist)
483 b = binding_freelist;
484 binding_freelist = b->prev;
486 else
487 b = GGC_NEW (struct c_binding);
489 b->shadowed = 0;
490 b->decl = decl;
491 b->id = name;
492 b->depth = scope->depth;
493 b->invisible = invisible;
494 b->nested = nested;
495 b->inner_comp = 0;
497 b->type = 0;
499 b->prev = scope->bindings;
500 scope->bindings = b;
502 if (!name)
503 return;
505 switch (TREE_CODE (decl))
507 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
508 case ENUMERAL_TYPE:
509 case UNION_TYPE:
510 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
511 case VAR_DECL:
512 case FUNCTION_DECL:
513 case TYPE_DECL:
514 case CONST_DECL:
515 case PARM_DECL:
516 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
518 default:
519 gcc_unreachable ();
522 /* Locate the appropriate place in the chain of shadowed decls
523 to insert this binding. Normally, scope == current_scope and
524 this does nothing. */
525 while (*here && (*here)->depth > scope->depth)
526 here = &(*here)->shadowed;
528 b->shadowed = *here;
529 *here = b;
532 /* Clear the binding structure B, stick it on the binding_freelist,
533 and return the former value of b->prev. This is used by pop_scope
534 and get_parm_info to iterate destructively over all the bindings
535 from a given scope. */
536 static struct c_binding *
537 free_binding_and_advance (struct c_binding *b)
539 struct c_binding *prev = b->prev;
541 memset (b, 0, sizeof (struct c_binding));
542 b->prev = binding_freelist;
543 binding_freelist = b;
545 return prev;
549 /* Hook called at end of compilation to assume 1 elt
550 for a file-scope tentative array defn that wasn't complete before. */
552 void
553 c_finish_incomplete_decl (tree decl)
555 if (TREE_CODE (decl) == VAR_DECL)
557 tree type = TREE_TYPE (decl);
558 if (type != error_mark_node
559 && TREE_CODE (type) == ARRAY_TYPE
560 && !DECL_EXTERNAL (decl)
561 && TYPE_DOMAIN (type) == 0)
563 warning (0, "%Jarray %qD assumed to have one element", decl, decl);
565 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
567 layout_decl (decl, 0);
572 /* The Objective-C front-end often needs to determine the current scope. */
574 void *
575 objc_get_current_scope (void)
577 return current_scope;
580 /* The following function is used only by Objective-C. It needs to live here
581 because it accesses the innards of c_scope. */
583 void
584 objc_mark_locals_volatile (void *enclosing_blk)
586 struct c_scope *scope;
587 struct c_binding *b;
589 for (scope = current_scope;
590 scope && scope != enclosing_blk;
591 scope = scope->outer)
593 for (b = scope->bindings; b; b = b->prev)
594 objc_volatilize_decl (b->decl);
596 /* Do not climb up past the current function. */
597 if (scope->function_body)
598 break;
602 /* Nonzero if we are currently in file scope. */
605 global_bindings_p (void)
607 return current_scope == file_scope && !c_override_global_bindings_to_false;
610 void
611 keep_next_level (void)
613 keep_next_level_flag = true;
616 /* Identify this scope as currently being filled with parameters. */
618 void
619 declare_parm_level (void)
621 current_scope->parm_flag = true;
624 void
625 push_scope (void)
627 if (next_is_function_body)
629 /* This is the transition from the parameters to the top level
630 of the function body. These are the same scope
631 (C99 6.2.1p4,6) so we do not push another scope structure.
632 next_is_function_body is set only by store_parm_decls, which
633 in turn is called when and only when we are about to
634 encounter the opening curly brace for the function body.
636 The outermost block of a function always gets a BLOCK node,
637 because the debugging output routines expect that each
638 function has at least one BLOCK. */
639 current_scope->parm_flag = false;
640 current_scope->function_body = true;
641 current_scope->keep = true;
642 current_scope->outer_function = current_function_scope;
643 current_function_scope = current_scope;
645 keep_next_level_flag = false;
646 next_is_function_body = false;
648 else
650 struct c_scope *scope;
651 if (scope_freelist)
653 scope = scope_freelist;
654 scope_freelist = scope->outer;
656 else
657 scope = GGC_CNEW (struct c_scope);
659 scope->keep = keep_next_level_flag;
660 scope->outer = current_scope;
661 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
663 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
664 possible. */
665 if (current_scope && scope->depth == 0)
667 scope->depth--;
668 sorry ("GCC supports only %u nested scopes", scope->depth);
671 current_scope = scope;
672 keep_next_level_flag = false;
676 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
678 static void
679 set_type_context (tree type, tree context)
681 for (type = TYPE_MAIN_VARIANT (type); type;
682 type = TYPE_NEXT_VARIANT (type))
683 TYPE_CONTEXT (type) = context;
686 /* Exit a scope. Restore the state of the identifier-decl mappings
687 that were in effect when this scope was entered. Return a BLOCK
688 node containing all the DECLs in this scope that are of interest
689 to debug info generation. */
691 tree
692 pop_scope (void)
694 struct c_scope *scope = current_scope;
695 tree block, context, p;
696 struct c_binding *b;
698 bool functionbody = scope->function_body;
699 bool keep = functionbody || scope->keep || scope->bindings;
701 c_end_vm_scope (scope->depth);
703 /* If appropriate, create a BLOCK to record the decls for the life
704 of this function. */
705 block = 0;
706 if (keep)
708 block = make_node (BLOCK);
709 BLOCK_SUBBLOCKS (block) = scope->blocks;
710 TREE_USED (block) = 1;
712 /* In each subblock, record that this is its superior. */
713 for (p = scope->blocks; p; p = TREE_CHAIN (p))
714 BLOCK_SUPERCONTEXT (p) = block;
716 BLOCK_VARS (block) = 0;
719 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
720 scope must be set so that they point to the appropriate
721 construct, i.e. either to the current FUNCTION_DECL node, or
722 else to the BLOCK node we just constructed.
724 Note that for tagged types whose scope is just the formal
725 parameter list for some function type specification, we can't
726 properly set their TYPE_CONTEXTs here, because we don't have a
727 pointer to the appropriate FUNCTION_TYPE node readily available
728 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
729 type nodes get set in `grokdeclarator' as soon as we have created
730 the FUNCTION_TYPE node which will represent the "scope" for these
731 "parameter list local" tagged types. */
732 if (scope->function_body)
733 context = current_function_decl;
734 else if (scope == file_scope)
736 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
737 TREE_CHAIN (file_decl) = all_translation_units;
738 all_translation_units = file_decl;
739 context = file_decl;
741 else
742 context = block;
744 /* Clear all bindings in this scope. */
745 for (b = scope->bindings; b; b = free_binding_and_advance (b))
747 p = b->decl;
748 switch (TREE_CODE (p))
750 case LABEL_DECL:
751 /* Warnings for unused labels, errors for undefined labels. */
752 if (TREE_USED (p) && !DECL_INITIAL (p))
754 error ("%Jlabel %qD used but not defined", p, p);
755 DECL_INITIAL (p) = error_mark_node;
757 else if (!TREE_USED (p) && warn_unused_label)
759 if (DECL_INITIAL (p))
760 warning (0, "%Jlabel %qD defined but not used", p, p);
761 else
762 warning (0, "%Jlabel %qD declared but not defined", p, p);
764 /* Labels go in BLOCK_VARS. */
765 TREE_CHAIN (p) = BLOCK_VARS (block);
766 BLOCK_VARS (block) = p;
767 gcc_assert (I_LABEL_BINDING (b->id) == b);
768 I_LABEL_BINDING (b->id) = b->shadowed;
769 break;
771 case ENUMERAL_TYPE:
772 case UNION_TYPE:
773 case RECORD_TYPE:
774 set_type_context (p, context);
776 /* Types may not have tag-names, in which case the type
777 appears in the bindings list with b->id NULL. */
778 if (b->id)
780 gcc_assert (I_TAG_BINDING (b->id) == b);
781 I_TAG_BINDING (b->id) = b->shadowed;
783 break;
785 case FUNCTION_DECL:
786 /* Propagate TREE_ADDRESSABLE from nested functions to their
787 containing functions. */
788 if (!TREE_ASM_WRITTEN (p)
789 && DECL_INITIAL (p) != 0
790 && TREE_ADDRESSABLE (p)
791 && DECL_ABSTRACT_ORIGIN (p) != 0
792 && DECL_ABSTRACT_ORIGIN (p) != p)
793 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
794 if (!DECL_EXTERNAL (p)
795 && DECL_INITIAL (p) == 0)
797 error ("%Jnested function %qD declared but never defined", p, p);
798 undef_nested_function = true;
800 goto common_symbol;
802 case VAR_DECL:
803 /* Warnings for unused variables. */
804 if (warn_unused_variable
805 && !TREE_USED (p)
806 && !DECL_IN_SYSTEM_HEADER (p)
807 && DECL_NAME (p)
808 && !DECL_ARTIFICIAL (p)
809 && scope != file_scope
810 && scope != external_scope)
811 warning (0, "%Junused variable %qD", p, p);
813 if (b->inner_comp)
815 error ("%Jtype of array %qD completed incompatibly with"
816 " implicit initialization", p, p);
819 /* Fall through. */
820 case TYPE_DECL:
821 case CONST_DECL:
822 common_symbol:
823 /* All of these go in BLOCK_VARS, but only if this is the
824 binding in the home scope. */
825 if (!b->nested)
827 TREE_CHAIN (p) = BLOCK_VARS (block);
828 BLOCK_VARS (block) = p;
830 /* If this is the file scope, and we are processing more
831 than one translation unit in this compilation, set
832 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
833 This makes same_translation_unit_p work, and causes
834 static declarations to be given disambiguating suffixes. */
835 if (scope == file_scope && num_in_fnames > 1)
837 DECL_CONTEXT (p) = context;
838 if (TREE_CODE (p) == TYPE_DECL)
839 set_type_context (TREE_TYPE (p), context);
842 /* Fall through. */
843 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
844 already been put there by store_parm_decls. Unused-
845 parameter warnings are handled by function.c.
846 error_mark_node obviously does not go in BLOCK_VARS and
847 does not get unused-variable warnings. */
848 case PARM_DECL:
849 case ERROR_MARK:
850 /* It is possible for a decl not to have a name. We get
851 here with b->id NULL in this case. */
852 if (b->id)
854 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
855 I_SYMBOL_BINDING (b->id) = b->shadowed;
856 if (b->shadowed && b->shadowed->type)
857 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
859 break;
861 default:
862 gcc_unreachable ();
867 /* Dispose of the block that we just made inside some higher level. */
868 if ((scope->function_body || scope == file_scope) && context)
870 DECL_INITIAL (context) = block;
871 BLOCK_SUPERCONTEXT (block) = context;
873 else if (scope->outer)
875 if (block)
876 SCOPE_LIST_APPEND (scope->outer, blocks, block);
877 /* If we did not make a block for the scope just exited, any
878 blocks made for inner scopes must be carried forward so they
879 will later become subblocks of something else. */
880 else if (scope->blocks)
881 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
884 /* Pop the current scope, and free the structure for reuse. */
885 current_scope = scope->outer;
886 if (scope->function_body)
887 current_function_scope = scope->outer_function;
889 memset (scope, 0, sizeof (struct c_scope));
890 scope->outer = scope_freelist;
891 scope_freelist = scope;
893 return block;
896 void
897 push_file_scope (void)
899 tree decl;
901 if (file_scope)
902 return;
904 push_scope ();
905 file_scope = current_scope;
907 start_fname_decls ();
909 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
910 bind (DECL_NAME (decl), decl, file_scope,
911 /*invisible=*/false, /*nested=*/true);
914 void
915 pop_file_scope (void)
917 /* In case there were missing closebraces, get us back to the global
918 binding level. */
919 while (current_scope != file_scope)
920 pop_scope ();
922 /* __FUNCTION__ is defined at file scope (""). This
923 call may not be necessary as my tests indicate it
924 still works without it. */
925 finish_fname_decls ();
927 /* This is the point to write out a PCH if we're doing that.
928 In that case we do not want to do anything else. */
929 if (pch_file)
931 c_common_write_pch ();
932 return;
935 /* Pop off the file scope and close this translation unit. */
936 pop_scope ();
937 file_scope = 0;
939 maybe_apply_pending_pragma_weaks ();
940 cgraph_finalize_compilation_unit ();
943 /* Insert BLOCK at the end of the list of subblocks of the current
944 scope. This is used when a BIND_EXPR is expanded, to handle the
945 BLOCK node inside the BIND_EXPR. */
947 void
948 insert_block (tree block)
950 TREE_USED (block) = 1;
951 SCOPE_LIST_APPEND (current_scope, blocks, block);
954 /* Push a definition or a declaration of struct, union or enum tag "name".
955 "type" should be the type node.
956 We assume that the tag "name" is not already defined.
958 Note that the definition may really be just a forward reference.
959 In that case, the TYPE_SIZE will be zero. */
961 static void
962 pushtag (tree name, tree type)
964 /* Record the identifier as the type's name if it has none. */
965 if (name && !TYPE_NAME (type))
966 TYPE_NAME (type) = name;
967 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
969 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
970 tagged type we just added to the current scope. This fake
971 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
972 to output a representation of a tagged type, and it also gives
973 us a convenient place to record the "scope start" address for the
974 tagged type. */
976 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
978 /* An approximation for now, so we can tell this is a function-scope tag.
979 This will be updated in pop_scope. */
980 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
983 /* Subroutine of compare_decls. Allow harmless mismatches in return
984 and argument types provided that the type modes match. This function
985 return a unified type given a suitable match, and 0 otherwise. */
987 static tree
988 match_builtin_function_types (tree newtype, tree oldtype)
990 tree newrettype, oldrettype;
991 tree newargs, oldargs;
992 tree trytype, tryargs;
994 /* Accept the return type of the new declaration if same modes. */
995 oldrettype = TREE_TYPE (oldtype);
996 newrettype = TREE_TYPE (newtype);
998 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
999 return 0;
1001 oldargs = TYPE_ARG_TYPES (oldtype);
1002 newargs = TYPE_ARG_TYPES (newtype);
1003 tryargs = newargs;
1005 while (oldargs || newargs)
1007 if (!oldargs
1008 || !newargs
1009 || !TREE_VALUE (oldargs)
1010 || !TREE_VALUE (newargs)
1011 || TYPE_MODE (TREE_VALUE (oldargs))
1012 != TYPE_MODE (TREE_VALUE (newargs)))
1013 return 0;
1015 oldargs = TREE_CHAIN (oldargs);
1016 newargs = TREE_CHAIN (newargs);
1019 trytype = build_function_type (newrettype, tryargs);
1020 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1023 /* Subroutine of diagnose_mismatched_decls. Check for function type
1024 mismatch involving an empty arglist vs a nonempty one and give clearer
1025 diagnostics. */
1026 static void
1027 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1028 tree newtype, tree oldtype)
1030 tree t;
1032 if (TREE_CODE (olddecl) != FUNCTION_DECL
1033 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1034 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1036 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1037 return;
1039 t = TYPE_ARG_TYPES (oldtype);
1040 if (t == 0)
1041 t = TYPE_ARG_TYPES (newtype);
1042 for (; t; t = TREE_CHAIN (t))
1044 tree type = TREE_VALUE (t);
1046 if (TREE_CHAIN (t) == 0
1047 && TYPE_MAIN_VARIANT (type) != void_type_node)
1049 inform ("a parameter list with an ellipsis can%'t match "
1050 "an empty parameter name list declaration");
1051 break;
1054 if (c_type_promotes_to (type) != type)
1056 inform ("an argument type that has a default promotion can%'t match "
1057 "an empty parameter name list declaration");
1058 break;
1063 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1064 old-style function definition, NEWDECL is a prototype declaration.
1065 Diagnose inconsistencies in the argument list. Returns TRUE if
1066 the prototype is compatible, FALSE if not. */
1067 static bool
1068 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1070 tree newargs, oldargs;
1071 int i;
1073 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1075 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1076 newargs = TYPE_ARG_TYPES (newtype);
1077 i = 1;
1079 for (;;)
1081 tree oldargtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1082 tree newargtype = TYPE_MAIN_VARIANT (TREE_VALUE (newargs));
1084 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1085 break;
1087 /* Reaching the end of just one list means the two decls don't
1088 agree on the number of arguments. */
1089 if (END_OF_ARGLIST (oldargtype))
1091 error ("%Jprototype for %qD declares more arguments "
1092 "than previous old-style definition", newdecl, newdecl);
1093 return false;
1095 else if (END_OF_ARGLIST (newargtype))
1097 error ("%Jprototype for %qD declares fewer arguments "
1098 "than previous old-style definition", newdecl, newdecl);
1099 return false;
1102 /* Type for passing arg must be consistent with that declared
1103 for the arg. */
1104 else if (!comptypes (oldargtype, newargtype))
1106 error ("%Jprototype for %qD declares argument %d"
1107 " with incompatible type",
1108 newdecl, newdecl, i);
1109 return false;
1112 oldargs = TREE_CHAIN (oldargs);
1113 newargs = TREE_CHAIN (newargs);
1114 i++;
1117 /* If we get here, no errors were found, but do issue a warning
1118 for this poor-style construct. */
1119 warning (0, "%Jprototype for %qD follows non-prototype definition",
1120 newdecl, newdecl);
1121 return true;
1122 #undef END_OF_ARGLIST
1125 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1126 first in a pair of mismatched declarations, using the diagnostic
1127 function DIAG. */
1128 static void
1129 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1131 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1133 else if (DECL_INITIAL (decl))
1134 diag (N_("%Jprevious definition of %qD was here"), decl, decl);
1135 else if (C_DECL_IMPLICIT (decl))
1136 diag (N_("%Jprevious implicit declaration of %qD was here"), decl, decl);
1137 else
1138 diag (N_("%Jprevious declaration of %qD was here"), decl, decl);
1141 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1142 Returns true if the caller should proceed to merge the two, false
1143 if OLDDECL should simply be discarded. As a side effect, issues
1144 all necessary diagnostics for invalid or poor-style combinations.
1145 If it returns true, writes the types of NEWDECL and OLDDECL to
1146 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1147 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1149 static bool
1150 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1151 tree *newtypep, tree *oldtypep)
1153 tree newtype, oldtype;
1154 bool pedwarned = false;
1155 bool warned = false;
1156 bool retval = true;
1158 /* If we have error_mark_node for either decl or type, just discard
1159 the previous decl - we're in an error cascade already. */
1160 if (olddecl == error_mark_node || newdecl == error_mark_node)
1161 return false;
1162 *oldtypep = oldtype = TREE_TYPE (olddecl);
1163 *newtypep = newtype = TREE_TYPE (newdecl);
1164 if (oldtype == error_mark_node || newtype == error_mark_node)
1165 return false;
1167 /* Two different categories of symbol altogether. This is an error
1168 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1169 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1171 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1172 && DECL_BUILT_IN (olddecl)
1173 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1175 error ("%J%qD redeclared as different kind of symbol",
1176 newdecl, newdecl);
1177 locate_old_decl (olddecl, error);
1179 else if (TREE_PUBLIC (newdecl))
1180 warning (0, "%Jbuilt-in function %qD declared as non-function",
1181 newdecl, newdecl);
1182 else
1183 warning (OPT_Wshadow, "%Jdeclaration of %qD shadows "
1184 "a built-in function", newdecl, newdecl);
1185 return false;
1188 /* Enumerators have no linkage, so may only be declared once in a
1189 given scope. */
1190 if (TREE_CODE (olddecl) == CONST_DECL)
1192 error ("%Jredeclaration of enumerator %qD", newdecl, newdecl);
1193 locate_old_decl (olddecl, error);
1194 return false;
1197 if (!comptypes (oldtype, newtype))
1199 if (TREE_CODE (olddecl) == FUNCTION_DECL
1200 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1202 /* Accept harmless mismatch in function types.
1203 This is for the ffs and fprintf builtins. */
1204 tree trytype = match_builtin_function_types (newtype, oldtype);
1206 if (trytype && comptypes (newtype, trytype))
1207 *oldtypep = oldtype = trytype;
1208 else
1210 /* If types don't match for a built-in, throw away the
1211 built-in. No point in calling locate_old_decl here, it
1212 won't print anything. */
1213 warning (0, "%Jconflicting types for built-in function %qD",
1214 newdecl, newdecl);
1215 return false;
1218 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1219 && DECL_IS_BUILTIN (olddecl))
1221 /* A conflicting function declaration for a predeclared
1222 function that isn't actually built in. Objective C uses
1223 these. The new declaration silently overrides everything
1224 but the volatility (i.e. noreturn) indication. See also
1225 below. FIXME: Make Objective C use normal builtins. */
1226 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1227 return false;
1229 /* Permit void foo (...) to match int foo (...) if the latter is
1230 the definition and implicit int was used. See
1231 c-torture/compile/920625-2.c. */
1232 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1233 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1234 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1235 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1237 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1238 /* Make sure we keep void as the return type. */
1239 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1240 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1241 pedwarned = true;
1243 /* Permit void foo (...) to match an earlier call to foo (...) with
1244 no declared type (thus, implicitly int). */
1245 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1246 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1247 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1248 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1250 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1251 /* Make sure we keep void as the return type. */
1252 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1253 pedwarned = true;
1255 else
1257 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1258 error ("%J conflicting type qualifiers for %qD", newdecl, newdecl);
1259 else
1260 error ("%Jconflicting types for %qD", newdecl, newdecl);
1261 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1262 locate_old_decl (olddecl, error);
1263 return false;
1267 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1268 but silently ignore the redeclaration if either is in a system
1269 header. (Conflicting redeclarations were handled above.) */
1270 if (TREE_CODE (newdecl) == TYPE_DECL)
1272 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1273 return true; /* Allow OLDDECL to continue in use. */
1275 error ("%Jredefinition of typedef %qD", newdecl, newdecl);
1276 locate_old_decl (olddecl, error);
1277 return false;
1280 /* Function declarations can either be 'static' or 'extern' (no
1281 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1282 can never conflict with each other on account of linkage (6.2.2p4).
1283 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1284 two definitions if one is 'extern inline' and one is not. The non-
1285 extern-inline definition supersedes the extern-inline definition. */
1286 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1288 /* If you declare a built-in function name as static, or
1289 define the built-in with an old-style definition (so we
1290 can't validate the argument list) the built-in definition is
1291 overridden, but optionally warn this was a bad choice of name. */
1292 if (DECL_BUILT_IN (olddecl)
1293 && !C_DECL_DECLARED_BUILTIN (olddecl)
1294 && (!TREE_PUBLIC (newdecl)
1295 || (DECL_INITIAL (newdecl)
1296 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1298 warning (OPT_Wshadow, "%Jdeclaration of %qD shadows "
1299 "a built-in function", newdecl, newdecl);
1300 /* Discard the old built-in function. */
1301 return false;
1304 if (DECL_INITIAL (newdecl))
1306 if (DECL_INITIAL (olddecl))
1308 /* If both decls have extern inline and are in the same TU,
1309 reject the new decl. */
1310 if (DECL_DECLARED_INLINE_P (olddecl)
1311 && DECL_EXTERNAL (olddecl)
1312 && DECL_DECLARED_INLINE_P (newdecl)
1313 && DECL_EXTERNAL (newdecl)
1314 && same_translation_unit_p (newdecl, olddecl))
1316 error ("%Jredefinition of %qD", newdecl, newdecl);
1317 locate_old_decl (olddecl, error);
1318 return false;
1320 /* If both decls have not extern inline, reject the new decl. */
1321 if (!DECL_DECLARED_INLINE_P (olddecl)
1322 && !DECL_EXTERNAL (olddecl)
1323 && !DECL_DECLARED_INLINE_P (newdecl)
1324 && !DECL_EXTERNAL (newdecl))
1326 error ("%Jredefinition of %qD", newdecl, newdecl);
1327 locate_old_decl (olddecl, error);
1328 return false;
1330 /* If the new decl is declared as extern inline, error if they are
1331 in the same TU, otherwise retain the old decl. */
1332 if (!DECL_DECLARED_INLINE_P (olddecl)
1333 && !DECL_EXTERNAL (olddecl)
1334 && DECL_DECLARED_INLINE_P (newdecl)
1335 && DECL_EXTERNAL (newdecl))
1337 if (same_translation_unit_p (newdecl, olddecl))
1339 error ("%Jredefinition of %qD", newdecl, newdecl);
1340 locate_old_decl (olddecl, error);
1341 return false;
1343 else
1344 retval = false;
1348 /* If we have a prototype after an old-style function definition,
1349 the argument types must be checked specially. */
1350 else if (DECL_INITIAL (olddecl)
1351 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1352 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1353 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1355 locate_old_decl (olddecl, error);
1356 return false;
1358 /* A non-static declaration (even an "extern") followed by a
1359 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1360 The same is true for a static forward declaration at block
1361 scope followed by a non-static declaration/definition at file
1362 scope. Static followed by non-static at the same scope is
1363 not undefined behavior, and is the most convenient way to get
1364 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1365 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1366 we do diagnose it if -Wtraditional. */
1367 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1369 /* Two exceptions to the rule. If olddecl is an extern
1370 inline, or a predeclared function that isn't actually
1371 built in, newdecl silently overrides olddecl. The latter
1372 occur only in Objective C; see also above. (FIXME: Make
1373 Objective C use normal builtins.) */
1374 if (!DECL_IS_BUILTIN (olddecl)
1375 && !(DECL_EXTERNAL (olddecl)
1376 && DECL_DECLARED_INLINE_P (olddecl)))
1378 error ("%Jstatic declaration of %qD follows "
1379 "non-static declaration", newdecl, newdecl);
1380 locate_old_decl (olddecl, error);
1382 return false;
1384 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1386 if (DECL_CONTEXT (olddecl))
1388 error ("%Jnon-static declaration of %qD follows "
1389 "static declaration", newdecl, newdecl);
1390 locate_old_decl (olddecl, error);
1391 return false;
1393 else if (warn_traditional)
1395 warning (0, "%Jnon-static declaration of %qD follows "
1396 "static declaration", newdecl, newdecl);
1397 warned = true;
1401 else if (TREE_CODE (newdecl) == VAR_DECL)
1403 /* Only variables can be thread-local, and all declarations must
1404 agree on this property. */
1405 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1407 if (DECL_THREAD_LOCAL (newdecl))
1408 error ("%Jthread-local declaration of %qD follows "
1409 "non-thread-local declaration", newdecl, newdecl);
1410 else
1411 error ("%Jnon-thread-local declaration of %qD follows "
1412 "thread-local declaration", newdecl, newdecl);
1414 locate_old_decl (olddecl, error);
1415 return false;
1418 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1419 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1421 error ("%Jredefinition of %qD", newdecl, newdecl);
1422 locate_old_decl (olddecl, error);
1423 return false;
1426 /* Objects declared at file scope: if the first declaration had
1427 external linkage (even if it was an external reference) the
1428 second must have external linkage as well, or the behavior is
1429 undefined. If the first declaration had internal linkage, then
1430 the second must too, or else be an external reference (in which
1431 case the composite declaration still has internal linkage).
1432 As for function declarations, we warn about the static-then-
1433 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1434 if (DECL_FILE_SCOPE_P (newdecl)
1435 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1437 if (DECL_EXTERNAL (newdecl))
1439 if (!DECL_FILE_SCOPE_P (olddecl))
1441 error ("%Jextern declaration of %qD follows "
1442 "declaration with no linkage", newdecl, newdecl);
1443 locate_old_decl (olddecl, error);
1444 return false;
1446 else if (warn_traditional)
1448 warning (0, "%Jnon-static declaration of %qD follows "
1449 "static declaration", newdecl, newdecl);
1450 warned = true;
1453 else
1455 if (TREE_PUBLIC (newdecl))
1456 error ("%Jnon-static declaration of %qD follows "
1457 "static declaration", newdecl, newdecl);
1458 else
1459 error ("%Jstatic declaration of %qD follows "
1460 "non-static declaration", newdecl, newdecl);
1462 locate_old_decl (olddecl, error);
1463 return false;
1466 /* Two objects with the same name declared at the same block
1467 scope must both be external references (6.7p3). */
1468 else if (!DECL_FILE_SCOPE_P (newdecl))
1470 if (DECL_EXTERNAL (newdecl))
1472 /* Extern with initializer at block scope, which will
1473 already have received an error. */
1475 else if (DECL_EXTERNAL (olddecl))
1477 error ("%Jdeclaration of %qD with no linkage follows "
1478 "extern declaration", newdecl, newdecl);
1479 locate_old_decl (olddecl, error);
1481 else
1483 error ("%Jredeclaration of %qD with no linkage",
1484 newdecl, newdecl);
1485 locate_old_decl (olddecl, error);
1488 return false;
1492 /* warnings */
1493 /* All decls must agree on a visibility. */
1494 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1495 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1497 warning (0, "%Jredeclaration of %qD with different visibility "
1498 "(old visibility preserved)", newdecl, newdecl);
1499 warned = true;
1502 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1504 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1505 if (DECL_DECLARED_INLINE_P (newdecl)
1506 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1508 warning (OPT_Wattributes, "%Jinline declaration of %qD follows "
1509 "declaration with attribute noinline", newdecl, newdecl);
1510 warned = true;
1512 else if (DECL_DECLARED_INLINE_P (olddecl)
1513 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1515 warning (OPT_Wattributes, "%Jdeclaration of %qD with attribute "
1516 "noinline follows inline declaration ", newdecl, newdecl);
1517 warned = true;
1520 /* Inline declaration after use or definition.
1521 ??? Should we still warn about this now we have unit-at-a-time
1522 mode and can get it right?
1523 Definitely don't complain if the decls are in different translation
1524 units. */
1525 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1526 && same_translation_unit_p (olddecl, newdecl))
1528 if (TREE_USED (olddecl))
1530 warning (0, "%J%qD declared inline after being called",
1531 olddecl, olddecl);
1532 warned = true;
1534 else if (DECL_INITIAL (olddecl))
1536 warning (0, "%J%qD declared inline after its definition",
1537 olddecl, olddecl);
1538 warned = true;
1542 else /* PARM_DECL, VAR_DECL */
1544 /* Redeclaration of a parameter is a constraint violation (this is
1545 not explicitly stated, but follows from C99 6.7p3 [no more than
1546 one declaration of the same identifier with no linkage in the
1547 same scope, except type tags] and 6.2.2p6 [parameters have no
1548 linkage]). We must check for a forward parameter declaration,
1549 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1550 an extension, the mandatory diagnostic for which is handled by
1551 mark_forward_parm_decls. */
1553 if (TREE_CODE (newdecl) == PARM_DECL
1554 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1556 error ("%Jredefinition of parameter %qD", newdecl, newdecl);
1557 locate_old_decl (olddecl, error);
1558 return false;
1562 /* Optional warning for completely redundant decls. */
1563 if (!warned && !pedwarned
1564 && warn_redundant_decls
1565 /* Don't warn about a function declaration followed by a
1566 definition. */
1567 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1568 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1569 /* Don't warn about redundant redeclarations of builtins. */
1570 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1571 && !DECL_BUILT_IN (newdecl)
1572 && DECL_BUILT_IN (olddecl)
1573 && !C_DECL_DECLARED_BUILTIN (olddecl))
1574 /* Don't warn about an extern followed by a definition. */
1575 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1576 /* Don't warn about forward parameter decls. */
1577 && !(TREE_CODE (newdecl) == PARM_DECL
1578 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1580 warning (0, "%Jredundant redeclaration of %qD", newdecl, newdecl);
1581 warned = true;
1584 /* Report location of previous decl/defn in a consistent manner. */
1585 if (warned || pedwarned)
1586 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1588 return retval;
1591 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1592 consistent with OLDDECL, but carries new information. Merge the
1593 new information into OLDDECL. This function issues no
1594 diagnostics. */
1596 static void
1597 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1599 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1600 && DECL_INITIAL (newdecl) != 0);
1601 int new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1602 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1603 int old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1604 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1606 /* For real parm decl following a forward decl, rechain the old decl
1607 in its new location and clear TREE_ASM_WRITTEN (it's not a
1608 forward decl anymore). */
1609 if (TREE_CODE (newdecl) == PARM_DECL
1610 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1612 struct c_binding *b, **here;
1614 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1615 if ((*here)->decl == olddecl)
1616 goto found;
1617 gcc_unreachable ();
1619 found:
1620 b = *here;
1621 *here = b->prev;
1622 b->prev = current_scope->bindings;
1623 current_scope->bindings = b;
1625 TREE_ASM_WRITTEN (olddecl) = 0;
1628 DECL_ATTRIBUTES (newdecl)
1629 = targetm.merge_decl_attributes (olddecl, newdecl);
1631 /* Merge the data types specified in the two decls. */
1632 TREE_TYPE (newdecl)
1633 = TREE_TYPE (olddecl)
1634 = composite_type (newtype, oldtype);
1636 /* Lay the type out, unless already done. */
1637 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1639 if (TREE_TYPE (newdecl) != error_mark_node)
1640 layout_type (TREE_TYPE (newdecl));
1641 if (TREE_CODE (newdecl) != FUNCTION_DECL
1642 && TREE_CODE (newdecl) != TYPE_DECL
1643 && TREE_CODE (newdecl) != CONST_DECL)
1644 layout_decl (newdecl, 0);
1646 else
1648 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1649 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1650 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1651 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1652 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1653 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1655 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1656 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1660 /* Keep the old rtl since we can safely use it. */
1661 COPY_DECL_RTL (olddecl, newdecl);
1663 /* Merge the type qualifiers. */
1664 if (TREE_READONLY (newdecl))
1665 TREE_READONLY (olddecl) = 1;
1667 if (TREE_THIS_VOLATILE (newdecl))
1669 TREE_THIS_VOLATILE (olddecl) = 1;
1670 if (TREE_CODE (newdecl) == VAR_DECL)
1671 make_var_volatile (newdecl);
1674 /* Merge deprecatedness. */
1675 if (TREE_DEPRECATED (newdecl))
1676 TREE_DEPRECATED (olddecl) = 1;
1678 /* Keep source location of definition rather than declaration and of
1679 prototype rather than non-prototype unless that prototype is
1680 built-in. */
1681 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1682 || (old_is_prototype && !new_is_prototype
1683 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1684 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1686 /* Merge the unused-warning information. */
1687 if (DECL_IN_SYSTEM_HEADER (olddecl))
1688 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1689 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1690 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1692 /* Merge the initialization information. */
1693 if (DECL_INITIAL (newdecl) == 0)
1694 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1696 /* Merge the section attribute.
1697 We want to issue an error if the sections conflict but that must be
1698 done later in decl_attributes since we are called before attributes
1699 are assigned. */
1700 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1701 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1703 /* Copy the assembler name.
1704 Currently, it can only be defined in the prototype. */
1705 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1707 /* Use visibility of whichever declaration had it specified */
1708 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1710 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1711 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1714 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1716 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1717 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1718 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1719 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1720 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1721 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1722 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1723 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1724 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1725 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1728 /* Merge the storage class information. */
1729 merge_weak (newdecl, olddecl);
1731 /* For functions, static overrides non-static. */
1732 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1734 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1735 /* This is since we don't automatically
1736 copy the attributes of NEWDECL into OLDDECL. */
1737 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1738 /* If this clears `static', clear it in the identifier too. */
1739 if (!TREE_PUBLIC (olddecl))
1740 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1742 if (DECL_EXTERNAL (newdecl))
1744 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1745 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1747 /* An extern decl does not override previous storage class. */
1748 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1749 if (!DECL_EXTERNAL (newdecl))
1751 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1752 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1755 else
1757 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1758 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1761 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1763 /* If we're redefining a function previously defined as extern
1764 inline, make sure we emit debug info for the inline before we
1765 throw it away, in case it was inlined into a function that hasn't
1766 been written out yet. */
1767 if (new_is_definition && DECL_INITIAL (olddecl))
1769 if (TREE_USED (olddecl)
1770 /* In unit-at-a-time mode we never inline re-defined extern
1771 inline functions. */
1772 && !flag_unit_at_a_time
1773 && cgraph_function_possibly_inlined_p (olddecl))
1774 (*debug_hooks->outlining_inline_function) (olddecl);
1776 /* The new defn must not be inline. */
1777 DECL_INLINE (newdecl) = 0;
1778 DECL_UNINLINABLE (newdecl) = 1;
1780 else
1782 /* If either decl says `inline', this fn is inline,
1783 unless its definition was passed already. */
1784 if (DECL_DECLARED_INLINE_P (newdecl)
1785 || DECL_DECLARED_INLINE_P (olddecl))
1786 DECL_DECLARED_INLINE_P (newdecl) = 1;
1788 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1789 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1792 if (DECL_BUILT_IN (olddecl))
1794 /* If redeclaring a builtin function, it stays built in.
1795 But it gets tagged as having been declared. */
1796 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1797 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1798 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1799 if (new_is_prototype)
1800 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1801 else
1802 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1803 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1806 /* Also preserve various other info from the definition. */
1807 if (!new_is_definition)
1809 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1810 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1811 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1812 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1813 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1815 /* Set DECL_INLINE on the declaration if we've got a body
1816 from which to instantiate. */
1817 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1819 DECL_INLINE (newdecl) = 1;
1820 DECL_ABSTRACT_ORIGIN (newdecl)
1821 = DECL_ABSTRACT_ORIGIN (olddecl);
1824 else
1826 /* If a previous declaration said inline, mark the
1827 definition as inlinable. */
1828 if (DECL_DECLARED_INLINE_P (newdecl)
1829 && !DECL_UNINLINABLE (newdecl))
1830 DECL_INLINE (newdecl) = 1;
1834 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1835 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1837 unsigned olddecl_uid = DECL_UID (olddecl);
1838 tree olddecl_context = DECL_CONTEXT (olddecl);
1840 memcpy ((char *) olddecl + sizeof (struct tree_common),
1841 (char *) newdecl + sizeof (struct tree_common),
1842 sizeof (struct tree_decl) - sizeof (struct tree_common));
1843 DECL_UID (olddecl) = olddecl_uid;
1844 DECL_CONTEXT (olddecl) = olddecl_context;
1847 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1848 so that encode_section_info has a chance to look at the new decl
1849 flags and attributes. */
1850 if (DECL_RTL_SET_P (olddecl)
1851 && (TREE_CODE (olddecl) == FUNCTION_DECL
1852 || (TREE_CODE (olddecl) == VAR_DECL
1853 && TREE_STATIC (olddecl))))
1854 make_decl_rtl (olddecl);
1857 /* Handle when a new declaration NEWDECL has the same name as an old
1858 one OLDDECL in the same binding contour. Prints an error message
1859 if appropriate.
1861 If safely possible, alter OLDDECL to look like NEWDECL, and return
1862 true. Otherwise, return false. */
1864 static bool
1865 duplicate_decls (tree newdecl, tree olddecl)
1867 tree newtype = NULL, oldtype = NULL;
1869 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1870 return false;
1872 merge_decls (newdecl, olddecl, newtype, oldtype);
1873 return true;
1877 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1878 static void
1879 warn_if_shadowing (tree new_decl)
1881 struct c_binding *b;
1883 /* Shadow warnings wanted? */
1884 if (!warn_shadow
1885 /* No shadow warnings for internally generated vars. */
1886 || DECL_IS_BUILTIN (new_decl)
1887 /* No shadow warnings for vars made for inlining. */
1888 || DECL_FROM_INLINE (new_decl)
1889 /* Don't warn about the parm names in function declarator
1890 within a function declarator. It would be nice to avoid
1891 warning in any function declarator in a declaration, as
1892 opposed to a definition, but there is no way to tell
1893 it's not a definition at this point. */
1894 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1895 return;
1897 /* Is anything being shadowed? Invisible decls do not count. */
1898 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1899 if (b->decl && b->decl != new_decl && !b->invisible)
1901 tree old_decl = b->decl;
1903 if (old_decl == error_mark_node)
1905 warning (0, "%Jdeclaration of %qD shadows previous non-variable",
1906 new_decl, new_decl);
1907 break;
1909 else if (TREE_CODE (old_decl) == PARM_DECL)
1910 warning (0, "%Jdeclaration of %qD shadows a parameter",
1911 new_decl, new_decl);
1912 else if (DECL_FILE_SCOPE_P (old_decl))
1913 warning (0, "%Jdeclaration of %qD shadows a global declaration",
1914 new_decl, new_decl);
1915 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1916 && DECL_BUILT_IN (old_decl))
1918 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1919 new_decl, new_decl);
1920 break;
1922 else
1923 warning (0, "%Jdeclaration of %qD shadows a previous local",
1924 new_decl, new_decl);
1926 warning (0, "%Jshadowed declaration is here", old_decl);
1928 break;
1933 /* Subroutine of pushdecl.
1935 X is a TYPE_DECL for a typedef statement. Create a brand new
1936 ..._TYPE node (which will be just a variant of the existing
1937 ..._TYPE node with identical properties) and then install X
1938 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1940 The whole point here is to end up with a situation where each
1941 and every ..._TYPE node the compiler creates will be uniquely
1942 associated with AT MOST one node representing a typedef name.
1943 This way, even though the compiler substitutes corresponding
1944 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1945 early on, later parts of the compiler can always do the reverse
1946 translation and get back the corresponding typedef name. For
1947 example, given:
1949 typedef struct S MY_TYPE;
1950 MY_TYPE object;
1952 Later parts of the compiler might only know that `object' was of
1953 type `struct S' if it were not for code just below. With this
1954 code however, later parts of the compiler see something like:
1956 struct S' == struct S
1957 typedef struct S' MY_TYPE;
1958 struct S' object;
1960 And they can then deduce (from the node for type struct S') that
1961 the original object declaration was:
1963 MY_TYPE object;
1965 Being able to do this is important for proper support of protoize,
1966 and also for generating precise symbolic debugging information
1967 which takes full account of the programmer's (typedef) vocabulary.
1969 Obviously, we don't want to generate a duplicate ..._TYPE node if
1970 the TYPE_DECL node that we are now processing really represents a
1971 standard built-in type.
1973 Since all standard types are effectively declared at line zero
1974 in the source file, we can easily check to see if we are working
1975 on a standard type by checking the current value of lineno. */
1977 static void
1978 clone_underlying_type (tree x)
1980 if (DECL_IS_BUILTIN (x))
1982 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1983 TYPE_NAME (TREE_TYPE (x)) = x;
1985 else if (TREE_TYPE (x) != error_mark_node
1986 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1988 tree tt = TREE_TYPE (x);
1989 DECL_ORIGINAL_TYPE (x) = tt;
1990 tt = build_variant_type_copy (tt);
1991 TYPE_NAME (tt) = x;
1992 TREE_USED (tt) = TREE_USED (x);
1993 TREE_TYPE (x) = tt;
1997 /* Record a decl-node X as belonging to the current lexical scope.
1998 Check for errors (such as an incompatible declaration for the same
1999 name already seen in the same scope).
2001 Returns either X or an old decl for the same name.
2002 If an old decl is returned, it may have been smashed
2003 to agree with what X says. */
2005 tree
2006 pushdecl (tree x)
2008 tree name = DECL_NAME (x);
2009 struct c_scope *scope = current_scope;
2010 struct c_binding *b;
2011 bool nested = false;
2013 /* Functions need the lang_decl data. */
2014 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
2015 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2017 /* Must set DECL_CONTEXT for everything not at file scope or
2018 DECL_FILE_SCOPE_P won't work. Local externs don't count
2019 unless they have initializers (which generate code). */
2020 if (current_function_decl
2021 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2022 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2023 DECL_CONTEXT (x) = current_function_decl;
2025 /* If this is of variably modified type, prevent jumping into its
2026 scope. */
2027 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2028 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2029 c_begin_vm_scope (scope->depth);
2031 /* Anonymous decls are just inserted in the scope. */
2032 if (!name)
2034 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2035 return x;
2038 /* First, see if there is another declaration with the same name in
2039 the current scope. If there is, duplicate_decls may do all the
2040 work for us. If duplicate_decls returns false, that indicates
2041 two incompatible decls in the same scope; we are to silently
2042 replace the old one (duplicate_decls has issued all appropriate
2043 diagnostics). In particular, we should not consider possible
2044 duplicates in the external scope, or shadowing. */
2045 b = I_SYMBOL_BINDING (name);
2046 if (b && B_IN_SCOPE (b, scope))
2048 struct c_binding *b_ext, *b_use;
2049 tree type = TREE_TYPE (x);
2050 tree visdecl = b->decl;
2051 tree vistype = TREE_TYPE (visdecl);
2052 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2053 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2054 b->inner_comp = false;
2055 b_use = b;
2056 b_ext = b;
2057 /* If this is an external linkage declaration, we should check
2058 for compatibility with the type in the external scope before
2059 setting the type at this scope based on the visible
2060 information only. */
2061 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2063 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2064 b_ext = b_ext->shadowed;
2065 if (b_ext)
2067 b_use = b_ext;
2068 if (b_use->type)
2069 TREE_TYPE (b_use->decl) = b_use->type;
2072 if (duplicate_decls (x, b_use->decl))
2074 if (b_use != b)
2076 /* Save the updated type in the external scope and
2077 restore the proper type for this scope. */
2078 tree thistype;
2079 if (comptypes (vistype, type))
2080 thistype = composite_type (vistype, type);
2081 else
2082 thistype = TREE_TYPE (b_use->decl);
2083 b_use->type = TREE_TYPE (b_use->decl);
2084 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2085 && DECL_BUILT_IN (b_use->decl))
2086 thistype
2087 = build_type_attribute_variant (thistype,
2088 TYPE_ATTRIBUTES
2089 (b_use->type));
2090 TREE_TYPE (b_use->decl) = thistype;
2092 return b_use->decl;
2094 else
2095 goto skip_external_and_shadow_checks;
2098 /* All declarations with external linkage, and all external
2099 references, go in the external scope, no matter what scope is
2100 current. However, the binding in that scope is ignored for
2101 purposes of normal name lookup. A separate binding structure is
2102 created in the requested scope; this governs the normal
2103 visibility of the symbol.
2105 The binding in the externals scope is used exclusively for
2106 detecting duplicate declarations of the same object, no matter
2107 what scope they are in; this is what we do here. (C99 6.2.7p2:
2108 All declarations that refer to the same object or function shall
2109 have compatible type; otherwise, the behavior is undefined.) */
2110 if (DECL_EXTERNAL (x) || scope == file_scope)
2112 tree type = TREE_TYPE (x);
2113 tree vistype = 0;
2114 tree visdecl = 0;
2115 bool type_saved = false;
2116 if (b && !B_IN_EXTERNAL_SCOPE (b)
2117 && (TREE_CODE (b->decl) == FUNCTION_DECL
2118 || TREE_CODE (b->decl) == VAR_DECL)
2119 && DECL_FILE_SCOPE_P (b->decl))
2121 visdecl = b->decl;
2122 vistype = TREE_TYPE (visdecl);
2124 if (warn_nested_externs
2125 && scope != file_scope
2126 && !DECL_IN_SYSTEM_HEADER (x))
2127 warning (0, "nested extern declaration of %qD", x);
2129 while (b && !B_IN_EXTERNAL_SCOPE (b))
2131 /* If this decl might be modified, save its type. This is
2132 done here rather than when the decl is first bound
2133 because the type may change after first binding, through
2134 being completed or through attributes being added. If we
2135 encounter multiple such decls, only the first should have
2136 its type saved; the others will already have had their
2137 proper types saved and the types will not have changed as
2138 their scopes will not have been re-entered. */
2139 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2141 b->type = TREE_TYPE (b->decl);
2142 type_saved = true;
2144 if (B_IN_FILE_SCOPE (b)
2145 && TREE_CODE (b->decl) == VAR_DECL
2146 && TREE_STATIC (b->decl)
2147 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2148 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2149 && TREE_CODE (type) == ARRAY_TYPE
2150 && TYPE_DOMAIN (type)
2151 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2152 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2154 /* Array type completed in inner scope, which should be
2155 diagnosed if the completion does not have size 1 and
2156 it does not get completed in the file scope. */
2157 b->inner_comp = true;
2159 b = b->shadowed;
2162 /* If a matching external declaration has been found, set its
2163 type to the composite of all the types of that declaration.
2164 After the consistency checks, it will be reset to the
2165 composite of the visible types only. */
2166 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2167 && b->type)
2168 TREE_TYPE (b->decl) = b->type;
2170 /* The point of the same_translation_unit_p check here is,
2171 we want to detect a duplicate decl for a construct like
2172 foo() { extern bar(); } ... static bar(); but not if
2173 they are in different translation units. In any case,
2174 the static does not go in the externals scope. */
2175 if (b
2176 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2177 && duplicate_decls (x, b->decl))
2179 tree thistype;
2180 if (vistype)
2182 if (comptypes (vistype, type))
2183 thistype = composite_type (vistype, type);
2184 else
2185 thistype = TREE_TYPE (b->decl);
2187 else
2188 thistype = type;
2189 b->type = TREE_TYPE (b->decl);
2190 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2191 thistype
2192 = build_type_attribute_variant (thistype,
2193 TYPE_ATTRIBUTES (b->type));
2194 TREE_TYPE (b->decl) = thistype;
2195 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2196 return b->decl;
2198 else if (TREE_PUBLIC (x))
2200 if (visdecl && !b && duplicate_decls (x, visdecl))
2202 /* An external declaration at block scope referring to a
2203 visible entity with internal linkage. The composite
2204 type will already be correct for this scope, so we
2205 just need to fall through to make the declaration in
2206 this scope. */
2207 nested = true;
2208 x = visdecl;
2210 else
2212 bind (name, x, external_scope, /*invisible=*/true,
2213 /*nested=*/false);
2214 nested = true;
2219 warn_if_shadowing (x);
2221 skip_external_and_shadow_checks:
2222 if (TREE_CODE (x) == TYPE_DECL)
2223 clone_underlying_type (x);
2225 bind (name, x, scope, /*invisible=*/false, nested);
2227 /* If x's type is incomplete because it's based on a
2228 structure or union which has not yet been fully declared,
2229 attach it to that structure or union type, so we can go
2230 back and complete the variable declaration later, if the
2231 structure or union gets fully declared.
2233 If the input is erroneous, we can have error_mark in the type
2234 slot (e.g. "f(void a, ...)") - that doesn't count as an
2235 incomplete type. */
2236 if (TREE_TYPE (x) != error_mark_node
2237 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2239 tree element = TREE_TYPE (x);
2241 while (TREE_CODE (element) == ARRAY_TYPE)
2242 element = TREE_TYPE (element);
2243 element = TYPE_MAIN_VARIANT (element);
2245 if ((TREE_CODE (element) == RECORD_TYPE
2246 || TREE_CODE (element) == UNION_TYPE)
2247 && (TREE_CODE (x) != TYPE_DECL
2248 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2249 && !COMPLETE_TYPE_P (element))
2250 C_TYPE_INCOMPLETE_VARS (element)
2251 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2253 return x;
2256 /* Record X as belonging to file scope.
2257 This is used only internally by the Objective-C front end,
2258 and is limited to its needs. duplicate_decls is not called;
2259 if there is any preexisting decl for this identifier, it is an ICE. */
2261 tree
2262 pushdecl_top_level (tree x)
2264 tree name;
2265 bool nested = false;
2266 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2268 name = DECL_NAME (x);
2270 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2272 if (TREE_PUBLIC (x))
2274 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2275 nested = true;
2277 if (file_scope)
2278 bind (name, x, file_scope, /*invisible=*/false, nested);
2280 return x;
2283 static void
2284 implicit_decl_warning (tree id, tree olddecl)
2286 void (*diag) (const char *, ...);
2287 switch (mesg_implicit_function_declaration)
2289 case 0: return;
2290 case 1: diag = warning0; break;
2291 case 2: diag = error; break;
2292 default: gcc_unreachable ();
2295 diag (N_("implicit declaration of function %qE"), id);
2296 if (olddecl)
2297 locate_old_decl (olddecl, diag);
2300 /* Generate an implicit declaration for identifier FUNCTIONID as a
2301 function of type int (). */
2303 tree
2304 implicitly_declare (tree functionid)
2306 struct c_binding *b;
2307 tree decl = 0;
2308 tree asmspec_tree;
2310 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2312 if (B_IN_SCOPE (b, external_scope))
2314 decl = b->decl;
2315 break;
2319 if (decl)
2321 if (decl == error_mark_node)
2322 return decl;
2324 /* FIXME: Objective-C has weird not-really-builtin functions
2325 which are supposed to be visible automatically. They wind up
2326 in the external scope because they're pushed before the file
2327 scope gets created. Catch this here and rebind them into the
2328 file scope. */
2329 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2331 bind (functionid, decl, file_scope,
2332 /*invisible=*/false, /*nested=*/true);
2333 return decl;
2335 else
2337 tree newtype = default_function_type;
2338 if (b->type)
2339 TREE_TYPE (decl) = b->type;
2340 /* Implicit declaration of a function already declared
2341 (somehow) in a different scope, or as a built-in.
2342 If this is the first time this has happened, warn;
2343 then recycle the old declaration but with the new type. */
2344 if (!C_DECL_IMPLICIT (decl))
2346 implicit_decl_warning (functionid, decl);
2347 C_DECL_IMPLICIT (decl) = 1;
2349 if (DECL_BUILT_IN (decl))
2351 newtype = build_type_attribute_variant (newtype,
2352 TYPE_ATTRIBUTES
2353 (TREE_TYPE (decl)));
2354 if (!comptypes (newtype, TREE_TYPE (decl)))
2356 warning (0, "incompatible implicit declaration of built-in"
2357 " function %qD", decl);
2358 newtype = TREE_TYPE (decl);
2361 else
2363 if (!comptypes (newtype, TREE_TYPE (decl)))
2365 error ("incompatible implicit declaration of function %qD",
2366 decl);
2367 locate_old_decl (decl, error);
2370 b->type = TREE_TYPE (decl);
2371 TREE_TYPE (decl) = newtype;
2372 bind (functionid, decl, current_scope,
2373 /*invisible=*/false, /*nested=*/true);
2374 return decl;
2378 /* Not seen before. */
2379 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2380 DECL_EXTERNAL (decl) = 1;
2381 TREE_PUBLIC (decl) = 1;
2382 C_DECL_IMPLICIT (decl) = 1;
2383 implicit_decl_warning (functionid, 0);
2384 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2385 if (asmspec_tree)
2386 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2388 /* C89 says implicit declarations are in the innermost block.
2389 So we record the decl in the standard fashion. */
2390 decl = pushdecl (decl);
2392 /* No need to call objc_check_decl here - it's a function type. */
2393 rest_of_decl_compilation (decl, 0, 0);
2395 /* Write a record describing this implicit function declaration
2396 to the prototypes file (if requested). */
2397 gen_aux_info_record (decl, 0, 1, 0);
2399 /* Possibly apply some default attributes to this implicit declaration. */
2400 decl_attributes (&decl, NULL_TREE, 0);
2402 return decl;
2405 /* Issue an error message for a reference to an undeclared variable
2406 ID, including a reference to a builtin outside of function-call
2407 context. Establish a binding of the identifier to error_mark_node
2408 in an appropriate scope, which will suppress further errors for the
2409 same identifier. The error message should be given location LOC. */
2410 void
2411 undeclared_variable (tree id, location_t loc)
2413 static bool already = false;
2414 struct c_scope *scope;
2416 if (current_function_decl == 0)
2418 error ("%H%qE undeclared here (not in a function)", &loc, id);
2419 scope = current_scope;
2421 else
2423 error ("%H%qE undeclared (first use in this function)", &loc, id);
2425 if (!already)
2427 error ("%H(Each undeclared identifier is reported only once", &loc);
2428 error ("%Hfor each function it appears in.)", &loc);
2429 already = true;
2432 /* If we are parsing old-style parameter decls, current_function_decl
2433 will be nonnull but current_function_scope will be null. */
2434 scope = current_function_scope ? current_function_scope : current_scope;
2436 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2439 /* Subroutine of lookup_label, declare_label, define_label: construct a
2440 LABEL_DECL with all the proper frills. */
2442 static tree
2443 make_label (tree name, location_t location)
2445 tree label = build_decl (LABEL_DECL, name, void_type_node);
2447 DECL_CONTEXT (label) = current_function_decl;
2448 DECL_MODE (label) = VOIDmode;
2449 DECL_SOURCE_LOCATION (label) = location;
2451 return label;
2454 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2455 Create one if none exists so far for the current function.
2456 This is called when a label is used in a goto expression or
2457 has its address taken. */
2459 tree
2460 lookup_label (tree name)
2462 tree label;
2464 if (current_function_decl == 0)
2466 error ("label %qE referenced outside of any function", name);
2467 return 0;
2470 /* Use a label already defined or ref'd with this name, but not if
2471 it is inherited from a containing function and wasn't declared
2472 using __label__. */
2473 label = I_LABEL_DECL (name);
2474 if (label && (DECL_CONTEXT (label) == current_function_decl
2475 || C_DECLARED_LABEL_FLAG (label)))
2477 /* If the label has only been declared, update its apparent
2478 location to point here, for better diagnostics if it
2479 turns out not to have been defined. */
2480 if (!TREE_USED (label))
2481 DECL_SOURCE_LOCATION (label) = input_location;
2482 return label;
2485 /* No label binding for that identifier; make one. */
2486 label = make_label (name, input_location);
2488 /* Ordinary labels go in the current function scope. */
2489 bind (name, label, current_function_scope,
2490 /*invisible=*/false, /*nested=*/false);
2491 return label;
2494 /* Make a label named NAME in the current function, shadowing silently
2495 any that may be inherited from containing functions or containing
2496 scopes. This is called for __label__ declarations. */
2498 tree
2499 declare_label (tree name)
2501 struct c_binding *b = I_LABEL_BINDING (name);
2502 tree label;
2504 /* Check to make sure that the label hasn't already been declared
2505 at this scope */
2506 if (b && B_IN_CURRENT_SCOPE (b))
2508 error ("duplicate label declaration %qE", name);
2509 locate_old_decl (b->decl, error);
2511 /* Just use the previous declaration. */
2512 return b->decl;
2515 label = make_label (name, input_location);
2516 C_DECLARED_LABEL_FLAG (label) = 1;
2518 /* Declared labels go in the current scope. */
2519 bind (name, label, current_scope,
2520 /*invisible=*/false, /*nested=*/false);
2521 return label;
2524 /* Define a label, specifying the location in the source file.
2525 Return the LABEL_DECL node for the label, if the definition is valid.
2526 Otherwise return 0. */
2528 tree
2529 define_label (location_t location, tree name)
2531 /* Find any preexisting label with this name. It is an error
2532 if that label has already been defined in this function, or
2533 if there is a containing function with a declared label with
2534 the same name. */
2535 tree label = I_LABEL_DECL (name);
2536 struct c_label_list *nlist_se, *nlist_vm;
2538 if (label
2539 && ((DECL_CONTEXT (label) == current_function_decl
2540 && DECL_INITIAL (label) != 0)
2541 || (DECL_CONTEXT (label) != current_function_decl
2542 && C_DECLARED_LABEL_FLAG (label))))
2544 error ("%Hduplicate label %qD", &location, label);
2545 locate_old_decl (label, error);
2546 return 0;
2548 else if (label && DECL_CONTEXT (label) == current_function_decl)
2550 /* The label has been used or declared already in this function,
2551 but not defined. Update its location to point to this
2552 definition. */
2553 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2554 error ("%Jjump into statement expression", label);
2555 if (C_DECL_UNDEFINABLE_VM (label))
2556 error ("%Jjump into scope of identifier with variably modified type",
2557 label);
2558 DECL_SOURCE_LOCATION (label) = location;
2560 else
2562 /* No label binding for that identifier; make one. */
2563 label = make_label (name, location);
2565 /* Ordinary labels go in the current function scope. */
2566 bind (name, label, current_function_scope,
2567 /*invisible=*/false, /*nested=*/false);
2570 if (!in_system_header && lookup_name (name))
2571 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2572 "for labels, identifier %qE conflicts", &location, name);
2574 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2575 nlist_se->next = label_context_stack_se->labels_def;
2576 nlist_se->label = label;
2577 label_context_stack_se->labels_def = nlist_se;
2579 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2580 nlist_vm->next = label_context_stack_vm->labels_def;
2581 nlist_vm->label = label;
2582 label_context_stack_vm->labels_def = nlist_vm;
2584 /* Mark label as having been defined. */
2585 DECL_INITIAL (label) = error_mark_node;
2586 return label;
2589 /* Given NAME, an IDENTIFIER_NODE,
2590 return the structure (or union or enum) definition for that name.
2591 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2592 CODE says which kind of type the caller wants;
2593 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2594 If the wrong kind of type is found, an error is reported. */
2596 static tree
2597 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2599 struct c_binding *b = I_TAG_BINDING (name);
2600 int thislevel = 0;
2602 if (!b || !b->decl)
2603 return 0;
2605 /* We only care about whether it's in this level if
2606 thislevel_only was set or it might be a type clash. */
2607 if (thislevel_only || TREE_CODE (b->decl) != code)
2609 /* For our purposes, a tag in the external scope is the same as
2610 a tag in the file scope. (Primarily relevant to Objective-C
2611 and its builtin structure tags, which get pushed before the
2612 file scope is created.) */
2613 if (B_IN_CURRENT_SCOPE (b)
2614 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2615 thislevel = 1;
2618 if (thislevel_only && !thislevel)
2619 return 0;
2621 if (TREE_CODE (b->decl) != code)
2623 /* Definition isn't the kind we were looking for. */
2624 pending_invalid_xref = name;
2625 pending_invalid_xref_location = input_location;
2627 /* If in the same binding level as a declaration as a tag
2628 of a different type, this must not be allowed to
2629 shadow that tag, so give the error immediately.
2630 (For example, "struct foo; union foo;" is invalid.) */
2631 if (thislevel)
2632 pending_xref_error ();
2634 return b->decl;
2637 /* Print an error message now
2638 for a recent invalid struct, union or enum cross reference.
2639 We don't print them immediately because they are not invalid
2640 when used in the `struct foo;' construct for shadowing. */
2642 void
2643 pending_xref_error (void)
2645 if (pending_invalid_xref != 0)
2646 error ("%H%qE defined as wrong kind of tag",
2647 &pending_invalid_xref_location, pending_invalid_xref);
2648 pending_invalid_xref = 0;
2652 /* Look up NAME in the current scope and its superiors
2653 in the namespace of variables, functions and typedefs.
2654 Return a ..._DECL node of some kind representing its definition,
2655 or return 0 if it is undefined. */
2657 tree
2658 lookup_name (tree name)
2660 struct c_binding *b = I_SYMBOL_BINDING (name);
2661 if (b && !b->invisible)
2662 return b->decl;
2663 return 0;
2666 /* Similar to `lookup_name' but look only at the indicated scope. */
2668 static tree
2669 lookup_name_in_scope (tree name, struct c_scope *scope)
2671 struct c_binding *b;
2673 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2674 if (B_IN_SCOPE (b, scope))
2675 return b->decl;
2676 return 0;
2679 /* Create the predefined scalar types of C,
2680 and some nodes representing standard constants (0, 1, (void *) 0).
2681 Initialize the global scope.
2682 Make definitions for built-in primitive functions. */
2684 void
2685 c_init_decl_processing (void)
2687 location_t save_loc = input_location;
2689 /* Initialize reserved words for parser. */
2690 c_parse_init ();
2692 current_function_decl = 0;
2694 gcc_obstack_init (&parser_obstack);
2696 /* Make the externals scope. */
2697 push_scope ();
2698 external_scope = current_scope;
2700 /* Declarations from c_common_nodes_and_builtins must not be associated
2701 with this input file, lest we get differences between using and not
2702 using preprocessed headers. */
2703 #ifdef USE_MAPPED_LOCATION
2704 input_location = BUILTINS_LOCATION;
2705 #else
2706 input_location.file = "<built-in>";
2707 input_location.line = 0;
2708 #endif
2710 build_common_tree_nodes (flag_signed_char, false);
2712 c_common_nodes_and_builtins ();
2714 /* In C, comparisons and TRUTH_* expressions have type int. */
2715 truthvalue_type_node = integer_type_node;
2716 truthvalue_true_node = integer_one_node;
2717 truthvalue_false_node = integer_zero_node;
2719 /* Even in C99, which has a real boolean type. */
2720 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2721 boolean_type_node));
2723 input_location = save_loc;
2725 pedantic_lvalues = true;
2727 make_fname_decl = c_make_fname_decl;
2728 start_fname_decls ();
2731 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2732 decl, NAME is the initialization string and TYPE_DEP indicates whether
2733 NAME depended on the type of the function. As we don't yet implement
2734 delayed emission of static data, we mark the decl as emitted
2735 so it is not placed in the output. Anything using it must therefore pull
2736 out the STRING_CST initializer directly. FIXME. */
2738 static tree
2739 c_make_fname_decl (tree id, int type_dep)
2741 const char *name = fname_as_string (type_dep);
2742 tree decl, type, init;
2743 size_t length = strlen (name);
2745 type = build_array_type (char_type_node,
2746 build_index_type (size_int (length)));
2747 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2749 decl = build_decl (VAR_DECL, id, type);
2751 TREE_STATIC (decl) = 1;
2752 TREE_READONLY (decl) = 1;
2753 DECL_ARTIFICIAL (decl) = 1;
2755 init = build_string (length + 1, name);
2756 free ((char *) name);
2757 TREE_TYPE (init) = type;
2758 DECL_INITIAL (decl) = init;
2760 TREE_USED (decl) = 1;
2762 if (current_function_decl)
2764 DECL_CONTEXT (decl) = current_function_decl;
2765 bind (id, decl, current_function_scope,
2766 /*invisible=*/false, /*nested=*/false);
2769 finish_decl (decl, init, NULL_TREE);
2771 return decl;
2774 /* Return a definition for a builtin function named NAME and whose data type
2775 is TYPE. TYPE should be a function type with argument types.
2776 FUNCTION_CODE tells later passes how to compile calls to this function.
2777 See tree.h for its possible values.
2779 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2780 the name to be called if we can't opencode the function. If
2781 ATTRS is nonzero, use that for the function's attribute list. */
2783 tree
2784 builtin_function (const char *name, tree type, int function_code,
2785 enum built_in_class cl, const char *library_name,
2786 tree attrs)
2788 tree id = get_identifier (name);
2789 tree decl = build_decl (FUNCTION_DECL, id, type);
2790 TREE_PUBLIC (decl) = 1;
2791 DECL_EXTERNAL (decl) = 1;
2792 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2793 DECL_BUILT_IN_CLASS (decl) = cl;
2794 DECL_FUNCTION_CODE (decl) = function_code;
2795 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2796 if (library_name)
2797 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2799 /* Should never be called on a symbol with a preexisting meaning. */
2800 gcc_assert (!I_SYMBOL_BINDING (id));
2802 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2804 /* Builtins in the implementation namespace are made visible without
2805 needing to be explicitly declared. See push_file_scope. */
2806 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2808 TREE_CHAIN (decl) = visible_builtins;
2809 visible_builtins = decl;
2812 /* Possibly apply some default attributes to this built-in function. */
2813 if (attrs)
2814 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2815 else
2816 decl_attributes (&decl, NULL_TREE, 0);
2818 return decl;
2821 /* Called when a declaration is seen that contains no names to declare.
2822 If its type is a reference to a structure, union or enum inherited
2823 from a containing scope, shadow that tag name for the current scope
2824 with a forward reference.
2825 If its type defines a new named structure or union
2826 or defines an enum, it is valid but we need not do anything here.
2827 Otherwise, it is an error. */
2829 void
2830 shadow_tag (const struct c_declspecs *declspecs)
2832 shadow_tag_warned (declspecs, 0);
2835 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2836 but no pedwarn. */
2837 void
2838 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2840 bool found_tag = false;
2842 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2844 tree value = declspecs->type;
2845 enum tree_code code = TREE_CODE (value);
2847 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2848 /* Used to test also that TYPE_SIZE (value) != 0.
2849 That caused warning for `struct foo;' at top level in the file. */
2851 tree name = TYPE_NAME (value);
2852 tree t;
2854 found_tag = true;
2856 if (name == 0)
2858 if (warned != 1 && code != ENUMERAL_TYPE)
2859 /* Empty unnamed enum OK */
2861 pedwarn ("unnamed struct/union that defines no instances");
2862 warned = 1;
2865 else if (!declspecs->tag_defined_p
2866 && declspecs->storage_class != csc_none)
2868 if (warned != 1)
2869 pedwarn ("empty declaration with storage class specifier "
2870 "does not redeclare tag");
2871 warned = 1;
2872 pending_xref_error ();
2874 else if (!declspecs->tag_defined_p
2875 && (declspecs->const_p
2876 || declspecs->volatile_p
2877 || declspecs->restrict_p))
2879 if (warned != 1)
2880 pedwarn ("empty declaration with type qualifier "
2881 "does not redeclare tag");
2882 warned = 1;
2883 pending_xref_error ();
2885 else
2887 pending_invalid_xref = 0;
2888 t = lookup_tag (code, name, 1);
2890 if (t == 0)
2892 t = make_node (code);
2893 pushtag (name, t);
2897 else
2899 if (warned != 1 && !in_system_header)
2901 pedwarn ("useless type name in empty declaration");
2902 warned = 1;
2906 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2908 pedwarn ("useless type name in empty declaration");
2909 warned = 1;
2912 pending_invalid_xref = 0;
2914 if (declspecs->inline_p)
2916 error ("%<inline%> in empty declaration");
2917 warned = 1;
2920 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2922 error ("%<auto%> in file-scope empty declaration");
2923 warned = 1;
2926 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2928 error ("%<register%> in file-scope empty declaration");
2929 warned = 1;
2932 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2934 warning (0, "useless storage class specifier in empty declaration");
2935 warned = 2;
2938 if (!warned && !in_system_header && declspecs->thread_p)
2940 warning (0, "useless %<__thread%> in empty declaration");
2941 warned = 2;
2944 if (!warned && !in_system_header && (declspecs->const_p
2945 || declspecs->volatile_p
2946 || declspecs->restrict_p))
2948 warning (0, "useless type qualifier in empty declaration");
2949 warned = 2;
2952 if (warned != 1)
2954 if (!found_tag)
2955 pedwarn ("empty declaration");
2960 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2961 bits. SPECS represents declaration specifiers that the grammar
2962 only permits to contain type qualifiers and attributes. */
2965 quals_from_declspecs (const struct c_declspecs *specs)
2967 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2968 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2969 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2970 gcc_assert (!specs->type
2971 && !specs->decl_attr
2972 && specs->typespec_word == cts_none
2973 && specs->storage_class == csc_none
2974 && !specs->typedef_p
2975 && !specs->explicit_signed_p
2976 && !specs->deprecated_p
2977 && !specs->long_p
2978 && !specs->long_long_p
2979 && !specs->short_p
2980 && !specs->signed_p
2981 && !specs->unsigned_p
2982 && !specs->complex_p
2983 && !specs->inline_p
2984 && !specs->thread_p);
2985 return quals;
2988 /* Construct an array declarator. EXPR is the expression inside [], or
2989 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2990 to the pointer to which a parameter array is converted). STATIC_P is
2991 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2992 is true if the array is [*], a VLA of unspecified length which is
2993 nevertheless a complete type (not currently implemented by GCC),
2994 false otherwise. The field for the contained declarator is left to be
2995 filled in by set_array_declarator_inner. */
2997 struct c_declarator *
2998 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
2999 bool vla_unspec_p)
3001 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3002 struct c_declarator);
3003 declarator->kind = cdk_array;
3004 declarator->declarator = 0;
3005 declarator->u.array.dimen = expr;
3006 if (quals)
3008 declarator->u.array.attrs = quals->attrs;
3009 declarator->u.array.quals = quals_from_declspecs (quals);
3011 else
3013 declarator->u.array.attrs = NULL_TREE;
3014 declarator->u.array.quals = 0;
3016 declarator->u.array.static_p = static_p;
3017 declarator->u.array.vla_unspec_p = vla_unspec_p;
3018 if (pedantic && !flag_isoc99)
3020 if (static_p || quals != NULL)
3021 pedwarn ("ISO C90 does not support %<static%> or type "
3022 "qualifiers in parameter array declarators");
3023 if (vla_unspec_p)
3024 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3026 if (vla_unspec_p)
3027 warning (0, "GCC does not yet properly implement %<[*]%> array declarators");
3028 return declarator;
3031 /* Set the contained declarator of an array declarator. DECL is the
3032 declarator, as constructed by build_array_declarator; INNER is what
3033 appears on the left of the []. ABSTRACT_P is true if it is an
3034 abstract declarator, false otherwise; this is used to reject static
3035 and type qualifiers in abstract declarators, where they are not in
3036 the C99 grammar (subject to possible change in DR#289). */
3038 struct c_declarator *
3039 set_array_declarator_inner (struct c_declarator *decl,
3040 struct c_declarator *inner, bool abstract_p)
3042 decl->declarator = inner;
3043 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3044 || decl->u.array.attrs != NULL_TREE
3045 || decl->u.array.static_p))
3046 error ("static or type qualifiers in abstract declarator");
3047 return decl;
3050 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3052 tree
3053 groktypename (struct c_type_name *type_name)
3055 tree type;
3056 tree attrs = type_name->specs->attrs;
3058 type_name->specs->attrs = NULL_TREE;
3060 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3061 false, NULL);
3063 /* Apply attributes. */
3064 decl_attributes (&type, attrs, 0);
3066 return type;
3069 /* Decode a declarator in an ordinary declaration or data definition.
3070 This is called as soon as the type information and variable name
3071 have been parsed, before parsing the initializer if any.
3072 Here we create the ..._DECL node, fill in its type,
3073 and put it on the list of decls for the current context.
3074 The ..._DECL node is returned as the value.
3076 Exception: for arrays where the length is not specified,
3077 the type is left null, to be filled in by `finish_decl'.
3079 Function definitions do not come here; they go to start_function
3080 instead. However, external and forward declarations of functions
3081 do go through here. Structure field declarations are done by
3082 grokfield and not through here. */
3084 tree
3085 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3086 bool initialized, tree attributes)
3088 tree decl;
3089 tree tem;
3091 /* An object declared as __attribute__((deprecated)) suppresses
3092 warnings of uses of other deprecated items. */
3093 if (lookup_attribute ("deprecated", attributes))
3094 deprecated_state = DEPRECATED_SUPPRESS;
3096 decl = grokdeclarator (declarator, declspecs,
3097 NORMAL, initialized, NULL);
3098 if (!decl)
3099 return 0;
3101 deprecated_state = DEPRECATED_NORMAL;
3103 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3104 && MAIN_NAME_P (DECL_NAME (decl)))
3105 warning (0, "%J%qD is usually a function", decl, decl);
3107 if (initialized)
3108 /* Is it valid for this decl to have an initializer at all?
3109 If not, set INITIALIZED to zero, which will indirectly
3110 tell 'finish_decl' to ignore the initializer once it is parsed. */
3111 switch (TREE_CODE (decl))
3113 case TYPE_DECL:
3114 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3115 initialized = 0;
3116 break;
3118 case FUNCTION_DECL:
3119 error ("function %qD is initialized like a variable", decl);
3120 initialized = 0;
3121 break;
3123 case PARM_DECL:
3124 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3125 error ("parameter %qD is initialized", decl);
3126 initialized = 0;
3127 break;
3129 default:
3130 /* Don't allow initializations for incomplete types except for
3131 arrays which might be completed by the initialization. */
3133 /* This can happen if the array size is an undefined macro.
3134 We already gave a warning, so we don't need another one. */
3135 if (TREE_TYPE (decl) == error_mark_node)
3136 initialized = 0;
3137 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3139 /* A complete type is ok if size is fixed. */
3141 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3142 || C_DECL_VARIABLE_SIZE (decl))
3144 error ("variable-sized object may not be initialized");
3145 initialized = 0;
3148 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3150 error ("variable %qD has initializer but incomplete type", decl);
3151 initialized = 0;
3153 else if (C_DECL_VARIABLE_SIZE (decl))
3155 /* Although C99 is unclear about whether incomplete arrays
3156 of VLAs themselves count as VLAs, it does not make
3157 sense to permit them to be initialized given that
3158 ordinary VLAs may not be initialized. */
3159 error ("variable-sized object may not be initialized");
3160 initialized = 0;
3164 if (initialized)
3166 if (current_scope == file_scope)
3167 TREE_STATIC (decl) = 1;
3169 /* Tell 'pushdecl' this is an initialized decl
3170 even though we don't yet have the initializer expression.
3171 Also tell 'finish_decl' it may store the real initializer. */
3172 DECL_INITIAL (decl) = error_mark_node;
3175 /* If this is a function declaration, write a record describing it to the
3176 prototypes file (if requested). */
3178 if (TREE_CODE (decl) == FUNCTION_DECL)
3179 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3181 /* ANSI specifies that a tentative definition which is not merged with
3182 a non-tentative definition behaves exactly like a definition with an
3183 initializer equal to zero. (Section 3.7.2)
3185 -fno-common gives strict ANSI behavior, though this tends to break
3186 a large body of code that grew up without this rule.
3188 Thread-local variables are never common, since there's no entrenched
3189 body of code to break, and it allows more efficient variable references
3190 in the presence of dynamic linking. */
3192 if (TREE_CODE (decl) == VAR_DECL
3193 && !initialized
3194 && TREE_PUBLIC (decl)
3195 && !DECL_THREAD_LOCAL (decl)
3196 && !flag_no_common)
3197 DECL_COMMON (decl) = 1;
3199 /* Set attributes here so if duplicate decl, will have proper attributes. */
3200 decl_attributes (&decl, attributes, 0);
3202 if (TREE_CODE (decl) == FUNCTION_DECL
3203 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3205 struct c_declarator *ce = declarator;
3207 if (ce->kind == cdk_pointer)
3208 ce = declarator->declarator;
3209 if (ce->kind == cdk_function)
3211 tree args = ce->u.arg_info->parms;
3212 for (; args; args = TREE_CHAIN (args))
3214 tree type = TREE_TYPE (args);
3215 if (type && INTEGRAL_TYPE_P (type)
3216 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3217 DECL_ARG_TYPE (args) = integer_type_node;
3222 if (TREE_CODE (decl) == FUNCTION_DECL
3223 && DECL_DECLARED_INLINE_P (decl)
3224 && DECL_UNINLINABLE (decl)
3225 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3226 warning (OPT_Wattributes, "%Jinline function %qD given attribute noinline",
3227 decl, decl);
3229 /* Add this decl to the current scope.
3230 TEM may equal DECL or it may be a previous decl of the same name. */
3231 tem = pushdecl (decl);
3233 if (initialized && DECL_EXTERNAL (tem))
3235 DECL_EXTERNAL (tem) = 0;
3236 TREE_STATIC (tem) = 1;
3239 return tem;
3242 /* Finish processing of a declaration;
3243 install its initial value.
3244 If the length of an array type is not known before,
3245 it must be determined now, from the initial value, or it is an error. */
3247 void
3248 finish_decl (tree decl, tree init, tree asmspec_tree)
3250 tree type = TREE_TYPE (decl);
3251 int was_incomplete = (DECL_SIZE (decl) == 0);
3252 const char *asmspec = 0;
3254 /* If a name was specified, get the string. */
3255 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3256 && DECL_FILE_SCOPE_P (decl))
3257 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3258 if (asmspec_tree)
3259 asmspec = TREE_STRING_POINTER (asmspec_tree);
3261 /* If `start_decl' didn't like having an initialization, ignore it now. */
3262 if (init != 0 && DECL_INITIAL (decl) == 0)
3263 init = 0;
3265 /* Don't crash if parm is initialized. */
3266 if (TREE_CODE (decl) == PARM_DECL)
3267 init = 0;
3269 if (init)
3270 store_init_value (decl, init);
3272 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3273 || TREE_CODE (decl) == FUNCTION_DECL
3274 || TREE_CODE (decl) == FIELD_DECL))
3275 objc_check_decl (decl);
3277 /* Deduce size of array from initialization, if not already known. */
3278 if (TREE_CODE (type) == ARRAY_TYPE
3279 && TYPE_DOMAIN (type) == 0
3280 && TREE_CODE (decl) != TYPE_DECL)
3282 bool do_default
3283 = (TREE_STATIC (decl)
3284 /* Even if pedantic, an external linkage array
3285 may have incomplete type at first. */
3286 ? pedantic && !TREE_PUBLIC (decl)
3287 : !DECL_EXTERNAL (decl));
3288 int failure
3289 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3290 do_default);
3292 /* Get the completed type made by complete_array_type. */
3293 type = TREE_TYPE (decl);
3295 switch (failure)
3297 case 1:
3298 error ("%Jinitializer fails to determine size of %qD", decl, decl);
3299 break;
3301 case 2:
3302 if (do_default)
3303 error ("%Jarray size missing in %qD", decl, decl);
3304 /* If a `static' var's size isn't known,
3305 make it extern as well as static, so it does not get
3306 allocated.
3307 If it is not `static', then do not mark extern;
3308 finish_incomplete_decl will give it a default size
3309 and it will get allocated. */
3310 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3311 DECL_EXTERNAL (decl) = 1;
3312 break;
3314 case 3:
3315 error ("%Jzero or negative size array %qD", decl, decl);
3316 break;
3318 case 0:
3319 /* For global variables, update the copy of the type that
3320 exists in the binding. */
3321 if (TREE_PUBLIC (decl))
3323 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3324 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3325 b_ext = b_ext->shadowed;
3326 if (b_ext)
3328 if (b_ext->type)
3329 b_ext->type = composite_type (b_ext->type, type);
3330 else
3331 b_ext->type = type;
3334 break;
3336 default:
3337 gcc_unreachable ();
3340 if (DECL_INITIAL (decl))
3341 TREE_TYPE (DECL_INITIAL (decl)) = type;
3343 layout_decl (decl, 0);
3346 if (TREE_CODE (decl) == VAR_DECL)
3348 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3349 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3350 layout_decl (decl, 0);
3352 if (DECL_SIZE (decl) == 0
3353 /* Don't give an error if we already gave one earlier. */
3354 && TREE_TYPE (decl) != error_mark_node
3355 && (TREE_STATIC (decl)
3356 /* A static variable with an incomplete type
3357 is an error if it is initialized.
3358 Also if it is not file scope.
3359 Otherwise, let it through, but if it is not `extern'
3360 then it may cause an error message later. */
3361 ? (DECL_INITIAL (decl) != 0
3362 || !DECL_FILE_SCOPE_P (decl))
3363 /* An automatic variable with an incomplete type
3364 is an error. */
3365 : !DECL_EXTERNAL (decl)))
3367 error ("%Jstorage size of %qD isn%'t known", decl, decl);
3368 TREE_TYPE (decl) = error_mark_node;
3371 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3372 && DECL_SIZE (decl) != 0)
3374 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3375 constant_expression_warning (DECL_SIZE (decl));
3376 else
3377 error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3380 if (TREE_USED (type))
3381 TREE_USED (decl) = 1;
3384 /* If this is a function and an assembler name is specified, reset DECL_RTL
3385 so we can give it its new name. Also, update built_in_decls if it
3386 was a normal built-in. */
3387 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3389 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3390 set_builtin_user_assembler_name (decl, asmspec);
3391 set_user_assembler_name (decl, asmspec);
3394 /* If #pragma weak was used, mark the decl weak now. */
3395 maybe_apply_pragma_weak (decl);
3397 /* If this is a variable definition, determine its ELF visibility. */
3398 if (TREE_CODE (decl) == VAR_DECL
3399 && TREE_STATIC (decl)
3400 && !DECL_EXTERNAL (decl))
3401 c_determine_visibility (decl);
3403 /* Output the assembler code and/or RTL code for variables and functions,
3404 unless the type is an undefined structure or union.
3405 If not, it will get done when the type is completed. */
3407 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3409 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3410 if (c_dialect_objc ())
3411 objc_check_decl (decl);
3413 if (asmspec)
3415 /* If this is not a static variable, issue a warning.
3416 It doesn't make any sense to give an ASMSPEC for an
3417 ordinary, non-register local variable. Historically,
3418 GCC has accepted -- but ignored -- the ASMSPEC in
3419 this case. */
3420 if (!DECL_FILE_SCOPE_P (decl)
3421 && TREE_CODE (decl) == VAR_DECL
3422 && !C_DECL_REGISTER (decl)
3423 && !TREE_STATIC (decl))
3424 warning (0, "%Jignoring asm-specifier for non-static local "
3425 "variable %qD", decl, decl);
3426 else if (C_DECL_REGISTER (decl))
3427 change_decl_assembler_name (decl, get_identifier (asmspec));
3428 else
3429 set_user_assembler_name (decl, asmspec);
3432 if (DECL_FILE_SCOPE_P (decl))
3434 if (DECL_INITIAL (decl) == NULL_TREE
3435 || DECL_INITIAL (decl) == error_mark_node)
3436 /* Don't output anything
3437 when a tentative file-scope definition is seen.
3438 But at end of compilation, do output code for them. */
3439 DECL_DEFER_OUTPUT (decl) = 1;
3440 rest_of_decl_compilation (decl, true, 0);
3442 else
3444 /* In conjunction with an ASMSPEC, the `register'
3445 keyword indicates that we should place the variable
3446 in a particular register. */
3447 if (asmspec && C_DECL_REGISTER (decl))
3449 DECL_HARD_REGISTER (decl) = 1;
3450 /* This cannot be done for a structure with volatile
3451 fields, on which DECL_REGISTER will have been
3452 reset. */
3453 if (!DECL_REGISTER (decl))
3454 error ("cannot put object with volatile field into register");
3457 if (TREE_CODE (decl) != FUNCTION_DECL)
3459 /* If we're building a variable sized type, and we might be
3460 reachable other than via the top of the current binding
3461 level, then create a new BIND_EXPR so that we deallocate
3462 the object at the right time. */
3463 /* Note that DECL_SIZE can be null due to errors. */
3464 if (DECL_SIZE (decl)
3465 && !TREE_CONSTANT (DECL_SIZE (decl))
3466 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3468 tree bind;
3469 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3470 TREE_SIDE_EFFECTS (bind) = 1;
3471 add_stmt (bind);
3472 BIND_EXPR_BODY (bind) = push_stmt_list ();
3474 add_stmt (build_stmt (DECL_EXPR, decl));
3479 if (!DECL_FILE_SCOPE_P (decl))
3481 /* Recompute the RTL of a local array now
3482 if it used to be an incomplete type. */
3483 if (was_incomplete
3484 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3486 /* If we used it already as memory, it must stay in memory. */
3487 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3488 /* If it's still incomplete now, no init will save it. */
3489 if (DECL_SIZE (decl) == 0)
3490 DECL_INITIAL (decl) = 0;
3495 /* If this was marked 'used', be sure it will be output. */
3496 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3497 mark_decl_referenced (decl);
3499 if (TREE_CODE (decl) == TYPE_DECL)
3501 if (!DECL_FILE_SCOPE_P (decl)
3502 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3503 add_stmt (build_stmt (DECL_EXPR, decl));
3505 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3508 /* At the end of a declaration, throw away any variable type sizes
3509 of types defined inside that declaration. There is no use
3510 computing them in the following function definition. */
3511 if (current_scope == file_scope)
3512 get_pending_sizes ();
3514 /* Install a cleanup (aka destructor) if one was given. */
3515 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3517 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3518 if (attr)
3520 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3521 tree cleanup_decl = lookup_name (cleanup_id);
3522 tree cleanup;
3524 /* Build "cleanup(&decl)" for the destructor. */
3525 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3526 cleanup = build_tree_list (NULL_TREE, cleanup);
3527 cleanup = build_function_call (cleanup_decl, cleanup);
3529 /* Don't warn about decl unused; the cleanup uses it. */
3530 TREE_USED (decl) = 1;
3531 TREE_USED (cleanup_decl) = 1;
3533 /* Initialize EH, if we've been told to do so. */
3534 if (flag_exceptions && !c_eh_initialized_p)
3536 c_eh_initialized_p = true;
3537 eh_personality_libfunc
3538 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3539 ? "__gcc_personality_sj0"
3540 : "__gcc_personality_v0");
3541 using_eh_for_cleanups ();
3544 push_cleanup (decl, cleanup, false);
3549 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3551 tree
3552 grokparm (const struct c_parm *parm)
3554 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3555 NULL);
3557 decl_attributes (&decl, parm->attrs, 0);
3559 return decl;
3562 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3563 and push that on the current scope. */
3565 void
3566 push_parm_decl (const struct c_parm *parm)
3568 tree decl;
3570 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3571 decl_attributes (&decl, parm->attrs, 0);
3573 decl = pushdecl (decl);
3575 finish_decl (decl, NULL_TREE, NULL_TREE);
3578 /* Mark all the parameter declarations to date as forward decls.
3579 Also diagnose use of this extension. */
3581 void
3582 mark_forward_parm_decls (void)
3584 struct c_binding *b;
3586 if (pedantic && !current_scope->warned_forward_parm_decls)
3588 pedwarn ("ISO C forbids forward parameter declarations");
3589 current_scope->warned_forward_parm_decls = true;
3592 for (b = current_scope->bindings; b; b = b->prev)
3593 if (TREE_CODE (b->decl) == PARM_DECL)
3594 TREE_ASM_WRITTEN (b->decl) = 1;
3597 static GTY(()) int compound_literal_number;
3599 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3600 literal, which may be an incomplete array type completed by the
3601 initializer; INIT is a CONSTRUCTOR that initializes the compound
3602 literal. */
3604 tree
3605 build_compound_literal (tree type, tree init)
3607 /* We do not use start_decl here because we have a type, not a declarator;
3608 and do not use finish_decl because the decl should be stored inside
3609 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3610 tree decl;
3611 tree complit;
3612 tree stmt;
3614 if (type == error_mark_node)
3615 return error_mark_node;
3617 decl = build_decl (VAR_DECL, NULL_TREE, type);
3618 DECL_EXTERNAL (decl) = 0;
3619 TREE_PUBLIC (decl) = 0;
3620 TREE_STATIC (decl) = (current_scope == file_scope);
3621 DECL_CONTEXT (decl) = current_function_decl;
3622 TREE_USED (decl) = 1;
3623 TREE_TYPE (decl) = type;
3624 TREE_READONLY (decl) = TYPE_READONLY (type);
3625 store_init_value (decl, init);
3627 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3629 int failure = complete_array_type (&TREE_TYPE (decl),
3630 DECL_INITIAL (decl), true);
3631 gcc_assert (!failure);
3633 type = TREE_TYPE (decl);
3634 TREE_TYPE (DECL_INITIAL (decl)) = type;
3637 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3638 return error_mark_node;
3640 stmt = build_stmt (DECL_EXPR, decl);
3641 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3642 TREE_SIDE_EFFECTS (complit) = 1;
3644 layout_decl (decl, 0);
3646 if (TREE_STATIC (decl))
3648 /* This decl needs a name for the assembler output. We also need
3649 a unique suffix to be added to the name. */
3650 char *name;
3652 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3653 compound_literal_number);
3654 compound_literal_number++;
3655 DECL_NAME (decl) = get_identifier (name);
3656 DECL_DEFER_OUTPUT (decl) = 1;
3657 DECL_COMDAT (decl) = 1;
3658 DECL_ARTIFICIAL (decl) = 1;
3659 DECL_IGNORED_P (decl) = 1;
3660 pushdecl (decl);
3661 rest_of_decl_compilation (decl, 1, 0);
3664 return complit;
3667 /* Determine whether TYPE is a structure with a flexible array member,
3668 or a union containing such a structure (possibly recursively). */
3670 static bool
3671 flexible_array_type_p (tree type)
3673 tree x;
3674 switch (TREE_CODE (type))
3676 case RECORD_TYPE:
3677 x = TYPE_FIELDS (type);
3678 if (x == NULL_TREE)
3679 return false;
3680 while (TREE_CHAIN (x) != NULL_TREE)
3681 x = TREE_CHAIN (x);
3682 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3683 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3684 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3685 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3686 return true;
3687 return false;
3688 case UNION_TYPE:
3689 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3691 if (flexible_array_type_p (TREE_TYPE (x)))
3692 return true;
3694 return false;
3695 default:
3696 return false;
3700 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3701 replacing with appropriate values if they are invalid. */
3702 static void
3703 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3705 tree type_mv;
3706 unsigned int max_width;
3707 unsigned HOST_WIDE_INT w;
3708 const char *name = orig_name ? orig_name: _("<anonymous>");
3710 /* Detect and ignore out of range field width and process valid
3711 field widths. */
3712 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3713 || TREE_CODE (*width) != INTEGER_CST)
3715 error ("bit-field %qs width not an integer constant", name);
3716 *width = integer_one_node;
3718 else
3720 constant_expression_warning (*width);
3721 if (tree_int_cst_sgn (*width) < 0)
3723 error ("negative width in bit-field %qs", name);
3724 *width = integer_one_node;
3726 else if (integer_zerop (*width) && orig_name)
3728 error ("zero width for bit-field %qs", name);
3729 *width = integer_one_node;
3733 /* Detect invalid bit-field type. */
3734 if (TREE_CODE (*type) != INTEGER_TYPE
3735 && TREE_CODE (*type) != BOOLEAN_TYPE
3736 && TREE_CODE (*type) != ENUMERAL_TYPE)
3738 error ("bit-field %qs has invalid type", name);
3739 *type = unsigned_type_node;
3742 type_mv = TYPE_MAIN_VARIANT (*type);
3743 if (pedantic
3744 && type_mv != integer_type_node
3745 && type_mv != unsigned_type_node
3746 && type_mv != boolean_type_node)
3747 pedwarn ("type of bit-field %qs is a GCC extension", name);
3749 if (type_mv == boolean_type_node)
3750 max_width = CHAR_TYPE_SIZE;
3751 else
3752 max_width = TYPE_PRECISION (*type);
3754 if (0 < compare_tree_int (*width, max_width))
3756 error ("width of %qs exceeds its type", name);
3757 w = max_width;
3758 *width = build_int_cst (NULL_TREE, w);
3760 else
3761 w = tree_low_cst (*width, 1);
3763 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3765 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3766 if (!lt
3767 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3768 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3769 warning (0, "%qs is narrower than values of its type", name);
3773 /* Given declspecs and a declarator,
3774 determine the name and type of the object declared
3775 and construct a ..._DECL node for it.
3776 (In one case we can return a ..._TYPE node instead.
3777 For invalid input we sometimes return 0.)
3779 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3781 DECL_CONTEXT says which syntactic context this declaration is in:
3782 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3783 FUNCDEF for a function definition. Like NORMAL but a few different
3784 error messages in each case. Return value may be zero meaning
3785 this definition is too screwy to try to parse.
3786 PARM for a parameter declaration (either within a function prototype
3787 or before a function body). Make a PARM_DECL, or return void_type_node.
3788 TYPENAME if for a typename (in a cast or sizeof).
3789 Don't make a DECL node; just return the ..._TYPE node.
3790 FIELD for a struct or union field; make a FIELD_DECL.
3791 INITIALIZED is true if the decl has an initializer.
3792 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3793 representing the width of the bit-field.
3795 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3796 It may also be so in the PARM case, for a prototype where the
3797 argument type is specified but not the name.
3799 This function is where the complicated C meanings of `static'
3800 and `extern' are interpreted. */
3802 static tree
3803 grokdeclarator (const struct c_declarator *declarator,
3804 struct c_declspecs *declspecs,
3805 enum decl_context decl_context, bool initialized, tree *width)
3807 tree type = declspecs->type;
3808 bool threadp = declspecs->thread_p;
3809 enum c_storage_class storage_class = declspecs->storage_class;
3810 int constp;
3811 int restrictp;
3812 int volatilep;
3813 int type_quals = TYPE_UNQUALIFIED;
3814 const char *name, *orig_name;
3815 tree typedef_type = 0;
3816 int funcdef_flag = 0;
3817 bool funcdef_syntax = false;
3818 int size_varies = 0;
3819 tree decl_attr = declspecs->decl_attr;
3820 int array_ptr_quals = TYPE_UNQUALIFIED;
3821 tree array_ptr_attrs = NULL_TREE;
3822 int array_parm_static = 0;
3823 tree returned_attrs = NULL_TREE;
3824 bool bitfield = width != NULL;
3825 tree element_type;
3826 struct c_arg_info *arg_info = 0;
3828 if (decl_context == FUNCDEF)
3829 funcdef_flag = 1, decl_context = NORMAL;
3831 /* Look inside a declarator for the name being declared
3832 and get it as a string, for an error message. */
3834 const struct c_declarator *decl = declarator;
3835 name = 0;
3837 while (decl)
3838 switch (decl->kind)
3840 case cdk_function:
3841 case cdk_array:
3842 case cdk_pointer:
3843 funcdef_syntax = (decl->kind == cdk_function);
3844 decl = decl->declarator;
3845 break;
3847 case cdk_attrs:
3848 decl = decl->declarator;
3849 break;
3851 case cdk_id:
3852 if (decl->u.id)
3853 name = IDENTIFIER_POINTER (decl->u.id);
3854 decl = 0;
3855 break;
3857 default:
3858 gcc_unreachable ();
3860 orig_name = name;
3861 if (name == 0)
3862 name = "type name";
3865 /* A function definition's declarator must have the form of
3866 a function declarator. */
3868 if (funcdef_flag && !funcdef_syntax)
3869 return 0;
3871 /* If this looks like a function definition, make it one,
3872 even if it occurs where parms are expected.
3873 Then store_parm_decls will reject it and not use it as a parm. */
3874 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3875 decl_context = PARM;
3877 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3878 warn_deprecated_use (declspecs->type);
3880 typedef_type = type;
3881 size_varies = C_TYPE_VARIABLE_SIZE (type);
3883 /* Diagnose defaulting to "int". */
3885 if (declspecs->default_int_p && !in_system_header)
3887 /* Issue a warning if this is an ISO C 99 program or if
3888 -Wreturn-type and this is a function, or if -Wimplicit;
3889 prefer the former warning since it is more explicit. */
3890 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3891 && funcdef_flag)
3892 warn_about_return_type = 1;
3893 else if (warn_implicit_int || flag_isoc99)
3894 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3897 /* Adjust the type if a bit-field is being declared,
3898 -funsigned-bitfields applied and the type is not explicitly
3899 "signed". */
3900 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3901 && TREE_CODE (type) == INTEGER_TYPE)
3902 type = c_common_unsigned_type (type);
3904 /* Figure out the type qualifiers for the declaration. There are
3905 two ways a declaration can become qualified. One is something
3906 like `const int i' where the `const' is explicit. Another is
3907 something like `typedef const int CI; CI i' where the type of the
3908 declaration contains the `const'. A third possibility is that
3909 there is a type qualifier on the element type of a typedefed
3910 array type, in which case we should extract that qualifier so
3911 that c_apply_type_quals_to_decls receives the full list of
3912 qualifiers to work with (C90 is not entirely clear about whether
3913 duplicate qualifiers should be diagnosed in this case, but it
3914 seems most appropriate to do so). */
3915 element_type = strip_array_types (type);
3916 constp = declspecs->const_p + TYPE_READONLY (element_type);
3917 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3918 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3919 if (pedantic && !flag_isoc99)
3921 if (constp > 1)
3922 pedwarn ("duplicate %<const%>");
3923 if (restrictp > 1)
3924 pedwarn ("duplicate %<restrict%>");
3925 if (volatilep > 1)
3926 pedwarn ("duplicate %<volatile%>");
3928 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3929 type = TYPE_MAIN_VARIANT (type);
3930 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3931 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3932 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3934 /* Warn about storage classes that are invalid for certain
3935 kinds of declarations (parameters, typenames, etc.). */
3937 if (funcdef_flag
3938 && (threadp
3939 || storage_class == csc_auto
3940 || storage_class == csc_register
3941 || storage_class == csc_typedef))
3943 if (storage_class == csc_auto
3944 && (pedantic || current_scope == file_scope))
3945 pedwarn ("function definition declared %<auto%>");
3946 if (storage_class == csc_register)
3947 error ("function definition declared %<register%>");
3948 if (storage_class == csc_typedef)
3949 error ("function definition declared %<typedef%>");
3950 if (threadp)
3951 error ("function definition declared %<__thread%>");
3952 threadp = false;
3953 if (storage_class == csc_auto
3954 || storage_class == csc_register
3955 || storage_class == csc_typedef)
3956 storage_class = csc_none;
3958 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3960 if (decl_context == PARM && storage_class == csc_register)
3962 else
3964 switch (decl_context)
3966 case FIELD:
3967 error ("storage class specified for structure field %qs",
3968 name);
3969 break;
3970 case PARM:
3971 error ("storage class specified for parameter %qs", name);
3972 break;
3973 default:
3974 error ("storage class specified for typename");
3975 break;
3977 storage_class = csc_none;
3978 threadp = false;
3981 else if (storage_class == csc_extern
3982 && initialized
3983 && !funcdef_flag)
3985 /* 'extern' with initialization is invalid if not at file scope. */
3986 if (current_scope == file_scope)
3987 warning (0, "%qs initialized and declared %<extern%>", name);
3988 else
3989 error ("%qs has both %<extern%> and initializer", name);
3991 else if (current_scope == file_scope)
3993 if (storage_class == csc_auto)
3994 error ("file-scope declaration of %qs specifies %<auto%>", name);
3995 if (pedantic && storage_class == csc_register)
3996 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3998 else
4000 if (storage_class == csc_extern && funcdef_flag)
4001 error ("nested function %qs declared %<extern%>", name);
4002 else if (threadp && storage_class == csc_none)
4004 error ("function-scope %qs implicitly auto and declared "
4005 "%<__thread%>",
4006 name);
4007 threadp = false;
4011 /* Now figure out the structure of the declarator proper.
4012 Descend through it, creating more complex types, until we reach
4013 the declared identifier (or NULL_TREE, in an absolute declarator).
4014 At each stage we maintain an unqualified version of the type
4015 together with any qualifiers that should be applied to it with
4016 c_build_qualified_type; this way, array types including
4017 multidimensional array types are first built up in unqualified
4018 form and then the qualified form is created with
4019 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4021 while (declarator && declarator->kind != cdk_id)
4023 if (type == error_mark_node)
4025 declarator = declarator->declarator;
4026 continue;
4029 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4030 a cdk_pointer (for *...),
4031 a cdk_function (for ...(...)),
4032 a cdk_attrs (for nested attributes),
4033 or a cdk_id (for the name being declared
4034 or the place in an absolute declarator
4035 where the name was omitted).
4036 For the last case, we have just exited the loop.
4038 At this point, TYPE is the type of elements of an array,
4039 or for a function to return, or for a pointer to point to.
4040 After this sequence of ifs, TYPE is the type of the
4041 array or function or pointer, and DECLARATOR has had its
4042 outermost layer removed. */
4044 if (array_ptr_quals != TYPE_UNQUALIFIED
4045 || array_ptr_attrs != NULL_TREE
4046 || array_parm_static)
4048 /* Only the innermost declarator (making a parameter be of
4049 array type which is converted to pointer type)
4050 may have static or type qualifiers. */
4051 error ("static or type qualifiers in non-parameter array declarator");
4052 array_ptr_quals = TYPE_UNQUALIFIED;
4053 array_ptr_attrs = NULL_TREE;
4054 array_parm_static = 0;
4057 switch (declarator->kind)
4059 case cdk_attrs:
4061 /* A declarator with embedded attributes. */
4062 tree attrs = declarator->u.attrs;
4063 const struct c_declarator *inner_decl;
4064 int attr_flags = 0;
4065 declarator = declarator->declarator;
4066 inner_decl = declarator;
4067 while (inner_decl->kind == cdk_attrs)
4068 inner_decl = inner_decl->declarator;
4069 if (inner_decl->kind == cdk_id)
4070 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4071 else if (inner_decl->kind == cdk_function)
4072 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4073 else if (inner_decl->kind == cdk_array)
4074 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4075 returned_attrs = decl_attributes (&type,
4076 chainon (returned_attrs, attrs),
4077 attr_flags);
4078 break;
4080 case cdk_array:
4082 tree itype = NULL_TREE;
4083 tree size = declarator->u.array.dimen;
4084 /* The index is a signed object `sizetype' bits wide. */
4085 tree index_type = c_common_signed_type (sizetype);
4087 array_ptr_quals = declarator->u.array.quals;
4088 array_ptr_attrs = declarator->u.array.attrs;
4089 array_parm_static = declarator->u.array.static_p;
4091 declarator = declarator->declarator;
4093 /* Check for some types that there cannot be arrays of. */
4095 if (VOID_TYPE_P (type))
4097 error ("declaration of %qs as array of voids", name);
4098 type = error_mark_node;
4101 if (TREE_CODE (type) == FUNCTION_TYPE)
4103 error ("declaration of %qs as array of functions", name);
4104 type = error_mark_node;
4107 if (pedantic && !in_system_header && flexible_array_type_p (type))
4108 pedwarn ("invalid use of structure with flexible array member");
4110 if (size == error_mark_node)
4111 type = error_mark_node;
4113 if (type == error_mark_node)
4114 continue;
4116 /* If size was specified, set ITYPE to a range-type for
4117 that size. Otherwise, ITYPE remains null. finish_decl
4118 may figure it out from an initial value. */
4120 if (size)
4122 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4123 lvalue. */
4124 STRIP_TYPE_NOPS (size);
4126 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4128 error ("size of array %qs has non-integer type", name);
4129 size = integer_one_node;
4132 if (pedantic && integer_zerop (size))
4133 pedwarn ("ISO C forbids zero-size array %qs", name);
4135 if (TREE_CODE (size) == INTEGER_CST)
4137 constant_expression_warning (size);
4138 if (tree_int_cst_sgn (size) < 0)
4140 error ("size of array %qs is negative", name);
4141 size = integer_one_node;
4144 else
4146 /* Make sure the array size remains visibly
4147 nonconstant even if it is (eg) a const variable
4148 with known value. */
4149 size_varies = 1;
4151 if (!flag_isoc99 && pedantic)
4153 if (TREE_CONSTANT (size))
4154 pedwarn ("ISO C90 forbids array %qs whose size "
4155 "can%'t be evaluated",
4156 name);
4157 else
4158 pedwarn ("ISO C90 forbids variable-size array %qs",
4159 name);
4163 if (integer_zerop (size))
4165 /* A zero-length array cannot be represented with
4166 an unsigned index type, which is what we'll
4167 get with build_index_type. Create an
4168 open-ended range instead. */
4169 itype = build_range_type (sizetype, size, NULL_TREE);
4171 else
4173 /* Arrange for the SAVE_EXPR on the inside of the
4174 MINUS_EXPR, which allows the -1 to get folded
4175 with the +1 that happens when building TYPE_SIZE. */
4176 if (size_varies)
4177 size = variable_size (size);
4179 /* Compute the maximum valid index, that is, size
4180 - 1. Do the calculation in index_type, so that
4181 if it is a variable the computations will be
4182 done in the proper mode. */
4183 itype = fold (build2 (MINUS_EXPR, index_type,
4184 convert (index_type, size),
4185 convert (index_type,
4186 size_one_node)));
4188 /* If that overflowed, the array is too big. ???
4189 While a size of INT_MAX+1 technically shouldn't
4190 cause an overflow (because we subtract 1), the
4191 overflow is recorded during the conversion to
4192 index_type, before the subtraction. Handling
4193 this case seems like an unnecessary
4194 complication. */
4195 if (TREE_OVERFLOW (itype))
4197 error ("size of array %qs is too large", name);
4198 type = error_mark_node;
4199 continue;
4202 itype = build_index_type (itype);
4205 else if (decl_context == FIELD)
4207 if (pedantic && !flag_isoc99 && !in_system_header)
4208 pedwarn ("ISO C90 does not support flexible array members");
4210 /* ISO C99 Flexible array members are effectively
4211 identical to GCC's zero-length array extension. */
4212 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4215 /* Complain about arrays of incomplete types. */
4216 if (!COMPLETE_TYPE_P (type))
4218 error ("array type has incomplete element type");
4219 type = error_mark_node;
4221 else
4222 type = build_array_type (type, itype);
4224 if (size_varies)
4225 C_TYPE_VARIABLE_SIZE (type) = 1;
4227 /* The GCC extension for zero-length arrays differs from
4228 ISO flexible array members in that sizeof yields
4229 zero. */
4230 if (size && integer_zerop (size))
4232 TYPE_SIZE (type) = bitsize_zero_node;
4233 TYPE_SIZE_UNIT (type) = size_zero_node;
4236 if (decl_context != PARM
4237 && (array_ptr_quals != TYPE_UNQUALIFIED
4238 || array_ptr_attrs != NULL_TREE
4239 || array_parm_static))
4241 error ("static or type qualifiers in non-parameter array declarator");
4242 array_ptr_quals = TYPE_UNQUALIFIED;
4243 array_ptr_attrs = NULL_TREE;
4244 array_parm_static = 0;
4246 break;
4248 case cdk_function:
4250 /* Say it's a definition only for the declarator closest
4251 to the identifier, apart possibly from some
4252 attributes. */
4253 bool really_funcdef = false;
4254 tree arg_types;
4255 if (funcdef_flag)
4257 const struct c_declarator *t = declarator->declarator;
4258 while (t->kind == cdk_attrs)
4259 t = t->declarator;
4260 really_funcdef = (t->kind == cdk_id);
4263 /* Declaring a function type. Make sure we have a valid
4264 type for the function to return. */
4265 if (type == error_mark_node)
4266 continue;
4268 size_varies = 0;
4270 /* Warn about some types functions can't return. */
4271 if (TREE_CODE (type) == FUNCTION_TYPE)
4273 error ("%qs declared as function returning a function", name);
4274 type = integer_type_node;
4276 if (TREE_CODE (type) == ARRAY_TYPE)
4278 error ("%qs declared as function returning an array", name);
4279 type = integer_type_node;
4282 /* Construct the function type and go to the next
4283 inner layer of declarator. */
4284 arg_info = declarator->u.arg_info;
4285 arg_types = grokparms (arg_info, really_funcdef);
4287 /* Type qualifiers before the return type of the function
4288 qualify the return type, not the function type. */
4289 if (type_quals)
4291 /* Type qualifiers on a function return type are
4292 normally permitted by the standard but have no
4293 effect, so give a warning at -Wreturn-type.
4294 Qualifiers on a void return type are banned on
4295 function definitions in ISO C; GCC used to used
4296 them for noreturn functions. */
4297 if (VOID_TYPE_P (type) && really_funcdef)
4298 pedwarn ("function definition has qualified void return type");
4299 else
4300 warning (OPT_Wreturn_type,
4301 "type qualifiers ignored on function return type");
4303 type = c_build_qualified_type (type, type_quals);
4305 type_quals = TYPE_UNQUALIFIED;
4307 type = build_function_type (type, arg_types);
4308 declarator = declarator->declarator;
4310 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4311 the formal parameter list of this FUNCTION_TYPE to point to
4312 the FUNCTION_TYPE node itself. */
4314 tree link;
4316 for (link = arg_info->tags;
4317 link;
4318 link = TREE_CHAIN (link))
4319 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4321 break;
4323 case cdk_pointer:
4325 /* Merge any constancy or volatility into the target type
4326 for the pointer. */
4328 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4329 && type_quals)
4330 pedwarn ("ISO C forbids qualified function types");
4331 if (type_quals)
4332 type = c_build_qualified_type (type, type_quals);
4333 size_varies = 0;
4335 type = build_pointer_type (type);
4337 /* Process type qualifiers (such as const or volatile)
4338 that were given inside the `*'. */
4339 type_quals = declarator->u.pointer_quals;
4341 declarator = declarator->declarator;
4342 break;
4344 default:
4345 gcc_unreachable ();
4349 /* Now TYPE has the actual type, apart from any qualifiers in
4350 TYPE_QUALS. */
4352 /* Check the type and width of a bit-field. */
4353 if (bitfield)
4354 check_bitfield_type_and_width (&type, width, orig_name);
4356 /* Did array size calculations overflow? */
4358 if (TREE_CODE (type) == ARRAY_TYPE
4359 && COMPLETE_TYPE_P (type)
4360 && TREE_OVERFLOW (TYPE_SIZE (type)))
4362 error ("size of array %qs is too large", name);
4363 /* If we proceed with the array type as it is, we'll eventually
4364 crash in tree_low_cst(). */
4365 type = error_mark_node;
4368 /* If this is declaring a typedef name, return a TYPE_DECL. */
4370 if (storage_class == csc_typedef)
4372 tree decl;
4373 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4374 && type_quals)
4375 pedwarn ("ISO C forbids qualified function types");
4376 if (type_quals)
4377 type = c_build_qualified_type (type, type_quals);
4378 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4379 if (declspecs->explicit_signed_p)
4380 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4381 decl_attributes (&decl, returned_attrs, 0);
4382 if (declspecs->inline_p)
4383 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4384 return decl;
4387 /* Detect the case of an array type of unspecified size
4388 which came, as such, direct from a typedef name.
4389 We must copy the type, so that each identifier gets
4390 a distinct type, so that each identifier's size can be
4391 controlled separately by its own initializer. */
4393 if (type != 0 && typedef_type != 0
4394 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4395 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4397 type = build_array_type (TREE_TYPE (type), 0);
4398 if (size_varies)
4399 C_TYPE_VARIABLE_SIZE (type) = 1;
4402 /* If this is a type name (such as, in a cast or sizeof),
4403 compute the type and return it now. */
4405 if (decl_context == TYPENAME)
4407 /* Note that the grammar rejects storage classes in typenames
4408 and fields. */
4409 gcc_assert (storage_class == csc_none && !threadp
4410 && !declspecs->inline_p);
4411 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4412 && type_quals)
4413 pedwarn ("ISO C forbids const or volatile function types");
4414 if (type_quals)
4415 type = c_build_qualified_type (type, type_quals);
4416 decl_attributes (&type, returned_attrs, 0);
4417 return type;
4420 /* Aside from typedefs and type names (handle above),
4421 `void' at top level (not within pointer)
4422 is allowed only in public variables.
4423 We don't complain about parms either, but that is because
4424 a better error message can be made later. */
4426 if (VOID_TYPE_P (type) && decl_context != PARM
4427 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4428 && (storage_class == csc_extern
4429 || (current_scope == file_scope
4430 && !(storage_class == csc_static
4431 || storage_class == csc_register)))))
4433 error ("variable or field %qs declared void", name);
4434 type = integer_type_node;
4437 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4438 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4441 tree decl;
4443 if (decl_context == PARM)
4445 tree type_as_written;
4446 tree promoted_type;
4448 /* A parameter declared as an array of T is really a pointer to T.
4449 One declared as a function is really a pointer to a function. */
4451 if (TREE_CODE (type) == ARRAY_TYPE)
4453 /* Transfer const-ness of array into that of type pointed to. */
4454 type = TREE_TYPE (type);
4455 if (type_quals)
4456 type = c_build_qualified_type (type, type_quals);
4457 type = build_pointer_type (type);
4458 type_quals = array_ptr_quals;
4460 /* We don't yet implement attributes in this context. */
4461 if (array_ptr_attrs != NULL_TREE)
4462 warning (OPT_Wattributes,
4463 "attributes in parameter array declarator ignored");
4465 size_varies = 0;
4467 else if (TREE_CODE (type) == FUNCTION_TYPE)
4469 if (pedantic && type_quals)
4470 pedwarn ("ISO C forbids qualified function types");
4471 if (type_quals)
4472 type = c_build_qualified_type (type, type_quals);
4473 type = build_pointer_type (type);
4474 type_quals = TYPE_UNQUALIFIED;
4476 else if (type_quals)
4477 type = c_build_qualified_type (type, type_quals);
4479 type_as_written = type;
4481 decl = build_decl (PARM_DECL, declarator->u.id, type);
4482 if (size_varies)
4483 C_DECL_VARIABLE_SIZE (decl) = 1;
4485 /* Compute the type actually passed in the parmlist,
4486 for the case where there is no prototype.
4487 (For example, shorts and chars are passed as ints.)
4488 When there is a prototype, this is overridden later. */
4490 if (type == error_mark_node)
4491 promoted_type = type;
4492 else
4493 promoted_type = c_type_promotes_to (type);
4495 DECL_ARG_TYPE (decl) = promoted_type;
4496 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4497 if (declspecs->inline_p)
4498 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4500 else if (decl_context == FIELD)
4502 /* Note that the grammar rejects storage classes in typenames
4503 and fields. */
4504 gcc_assert (storage_class == csc_none && !threadp
4505 && !declspecs->inline_p);
4507 /* Structure field. It may not be a function. */
4509 if (TREE_CODE (type) == FUNCTION_TYPE)
4511 error ("field %qs declared as a function", name);
4512 type = build_pointer_type (type);
4514 else if (TREE_CODE (type) != ERROR_MARK
4515 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4517 error ("field %qs has incomplete type", name);
4518 type = error_mark_node;
4520 type = c_build_qualified_type (type, type_quals);
4521 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4522 DECL_NONADDRESSABLE_P (decl) = bitfield;
4524 if (size_varies)
4525 C_DECL_VARIABLE_SIZE (decl) = 1;
4527 else if (TREE_CODE (type) == FUNCTION_TYPE)
4529 if (storage_class == csc_register || threadp)
4531 error ("invalid storage class for function %qs", name);
4533 else if (current_scope != file_scope)
4535 /* Function declaration not at file scope. Storage
4536 classes other than `extern' are not allowed, C99
4537 6.7.1p5, and `extern' makes no difference. However,
4538 GCC allows 'auto', perhaps with 'inline', to support
4539 nested functions. */
4540 if (storage_class == csc_auto)
4542 if (pedantic)
4543 pedwarn ("invalid storage class for function %qs", name);
4545 else if (storage_class == csc_static)
4547 error ("invalid storage class for function %qs", name);
4548 if (funcdef_flag)
4549 storage_class = declspecs->storage_class = csc_none;
4550 else
4551 return 0;
4555 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4556 decl = build_decl_attribute_variant (decl, decl_attr);
4558 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4560 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4561 pedwarn ("ISO C forbids qualified function types");
4563 /* GNU C interprets a volatile-qualified function type to indicate
4564 that the function does not return. */
4565 if ((type_quals & TYPE_QUAL_VOLATILE)
4566 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4567 warning (0, "%<noreturn%> function returns non-void value");
4569 /* Every function declaration is an external reference
4570 (DECL_EXTERNAL) except for those which are not at file
4571 scope and are explicitly declared "auto". This is
4572 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4573 GCC to signify a forward declaration of a nested function. */
4574 if (storage_class == csc_auto && current_scope != file_scope)
4575 DECL_EXTERNAL (decl) = 0;
4576 else
4577 DECL_EXTERNAL (decl) = 1;
4579 /* Record absence of global scope for `static' or `auto'. */
4580 TREE_PUBLIC (decl)
4581 = !(storage_class == csc_static || storage_class == csc_auto);
4583 /* For a function definition, record the argument information
4584 block where store_parm_decls will look for it. */
4585 if (funcdef_flag)
4586 current_function_arg_info = arg_info;
4588 if (declspecs->default_int_p)
4589 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4591 /* Record presence of `inline', if it is reasonable. */
4592 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4594 if (declspecs->inline_p)
4595 pedwarn ("cannot inline function %<main%>");
4597 else if (declspecs->inline_p)
4599 /* Record that the function is declared `inline'. */
4600 DECL_DECLARED_INLINE_P (decl) = 1;
4602 /* Do not mark bare declarations as DECL_INLINE. Doing so
4603 in the presence of multiple declarations can result in
4604 the abstract origin pointing between the declarations,
4605 which will confuse dwarf2out. */
4606 if (initialized)
4608 DECL_INLINE (decl) = 1;
4609 if (storage_class == csc_extern)
4610 current_extern_inline = 1;
4613 /* If -finline-functions, assume it can be inlined. This does
4614 two things: let the function be deferred until it is actually
4615 needed, and let dwarf2 know that the function is inlinable. */
4616 else if (flag_inline_trees == 2 && initialized)
4617 DECL_INLINE (decl) = 1;
4619 else
4621 /* It's a variable. */
4622 /* An uninitialized decl with `extern' is a reference. */
4623 int extern_ref = !initialized && storage_class == csc_extern;
4625 type = c_build_qualified_type (type, type_quals);
4627 /* C99 6.2.2p7: It is invalid (compile-time undefined
4628 behavior) to create an 'extern' declaration for a
4629 variable if there is a global declaration that is
4630 'static' and the global declaration is not visible.
4631 (If the static declaration _is_ currently visible,
4632 the 'extern' declaration is taken to refer to that decl.) */
4633 if (extern_ref && current_scope != file_scope)
4635 tree global_decl = identifier_global_value (declarator->u.id);
4636 tree visible_decl = lookup_name (declarator->u.id);
4638 if (global_decl
4639 && global_decl != visible_decl
4640 && TREE_CODE (global_decl) == VAR_DECL
4641 && !TREE_PUBLIC (global_decl))
4642 error ("variable previously declared %<static%> redeclared "
4643 "%<extern%>");
4646 decl = build_decl (VAR_DECL, declarator->u.id, type);
4647 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4648 if (size_varies)
4649 C_DECL_VARIABLE_SIZE (decl) = 1;
4651 if (declspecs->inline_p)
4652 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4654 /* At file scope, an initialized extern declaration may follow
4655 a static declaration. In that case, DECL_EXTERNAL will be
4656 reset later in start_decl. */
4657 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4659 /* At file scope, the presence of a `static' or `register' storage
4660 class specifier, or the absence of all storage class specifiers
4661 makes this declaration a definition (perhaps tentative). Also,
4662 the absence of both `static' and `register' makes it public. */
4663 if (current_scope == file_scope)
4665 TREE_PUBLIC (decl) = !(storage_class == csc_static
4666 || storage_class == csc_register);
4667 TREE_STATIC (decl) = !extern_ref;
4669 /* Not at file scope, only `static' makes a static definition. */
4670 else
4672 TREE_STATIC (decl) = (storage_class == csc_static);
4673 TREE_PUBLIC (decl) = extern_ref;
4676 if (threadp)
4678 if (targetm.have_tls)
4679 DECL_THREAD_LOCAL (decl) = 1;
4680 else
4681 /* A mere warning is sure to result in improper semantics
4682 at runtime. Don't bother to allow this to compile. */
4683 error ("thread-local storage not supported for this target");
4687 /* Record `register' declaration for warnings on &
4688 and in case doing stupid register allocation. */
4690 if (storage_class == csc_register)
4692 C_DECL_REGISTER (decl) = 1;
4693 DECL_REGISTER (decl) = 1;
4696 /* Record constancy and volatility. */
4697 c_apply_type_quals_to_decl (type_quals, decl);
4699 /* If a type has volatile components, it should be stored in memory.
4700 Otherwise, the fact that those components are volatile
4701 will be ignored, and would even crash the compiler. */
4702 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4704 /* It is not an error for a structure with volatile fields to
4705 be declared register, but reset DECL_REGISTER since it
4706 cannot actually go in a register. */
4707 int was_reg = C_DECL_REGISTER (decl);
4708 C_DECL_REGISTER (decl) = 0;
4709 DECL_REGISTER (decl) = 0;
4710 c_mark_addressable (decl);
4711 C_DECL_REGISTER (decl) = was_reg;
4714 /* This is the earliest point at which we might know the assembler
4715 name of a variable. Thus, if it's known before this, die horribly. */
4716 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4718 decl_attributes (&decl, returned_attrs, 0);
4720 return decl;
4724 /* Decode the parameter-list info for a function type or function definition.
4725 The argument is the value returned by `get_parm_info' (or made in parse.y
4726 if there is an identifier list instead of a parameter decl list).
4727 These two functions are separate because when a function returns
4728 or receives functions then each is called multiple times but the order
4729 of calls is different. The last call to `grokparms' is always the one
4730 that contains the formal parameter names of a function definition.
4732 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4734 FUNCDEF_FLAG is true for a function definition, false for
4735 a mere declaration. A nonempty identifier-list gets an error message
4736 when FUNCDEF_FLAG is false. */
4738 static tree
4739 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4741 tree arg_types = arg_info->types;
4743 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4744 && !in_system_header)
4745 warning (0, "function declaration isn%'t a prototype");
4747 if (arg_types == error_mark_node)
4748 return 0; /* don't set TYPE_ARG_TYPES in this case */
4750 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4752 if (!funcdef_flag)
4753 pedwarn ("parameter names (without types) in function declaration");
4755 arg_info->parms = arg_info->types;
4756 arg_info->types = 0;
4757 return 0;
4759 else
4761 tree parm, type, typelt;
4762 unsigned int parmno;
4764 /* If there is a parameter of incomplete type in a definition,
4765 this is an error. In a declaration this is valid, and a
4766 struct or union type may be completed later, before any calls
4767 or definition of the function. In the case where the tag was
4768 first declared within the parameter list, a warning has
4769 already been given. If a parameter has void type, then
4770 however the function cannot be defined or called, so
4771 warn. */
4773 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4774 parm;
4775 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4777 type = TREE_VALUE (typelt);
4778 if (type == error_mark_node)
4779 continue;
4781 if (!COMPLETE_TYPE_P (type))
4783 if (funcdef_flag)
4785 if (DECL_NAME (parm))
4786 error ("%Jparameter %u (%qD) has incomplete type",
4787 parm, parmno, parm);
4788 else
4789 error ("%Jparameter %u has incomplete type",
4790 parm, parmno);
4792 TREE_VALUE (typelt) = error_mark_node;
4793 TREE_TYPE (parm) = error_mark_node;
4795 else if (VOID_TYPE_P (type))
4797 if (DECL_NAME (parm))
4798 warning (0, "%Jparameter %u (%qD) has void type",
4799 parm, parmno, parm);
4800 else
4801 warning (0, "%Jparameter %u has void type",
4802 parm, parmno);
4806 return arg_types;
4810 /* Take apart the current scope and return a c_arg_info structure with
4811 info on a parameter list just parsed.
4813 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4815 ELLIPSIS being true means the argument list ended in '...' so don't
4816 append a sentinel (void_list_node) to the end of the type-list. */
4818 struct c_arg_info *
4819 get_parm_info (bool ellipsis)
4821 struct c_binding *b = current_scope->bindings;
4822 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4823 struct c_arg_info);
4824 tree parms = 0;
4825 tree tags = 0;
4826 tree types = 0;
4827 tree others = 0;
4829 static bool explained_incomplete_types = false;
4830 bool gave_void_only_once_err = false;
4832 arg_info->parms = 0;
4833 arg_info->tags = 0;
4834 arg_info->types = 0;
4835 arg_info->others = 0;
4837 /* The bindings in this scope must not get put into a block.
4838 We will take care of deleting the binding nodes. */
4839 current_scope->bindings = 0;
4841 /* This function is only called if there was *something* on the
4842 parameter list. */
4843 gcc_assert (b);
4845 /* A parameter list consisting solely of 'void' indicates that the
4846 function takes no arguments. But if the 'void' is qualified
4847 (by 'const' or 'volatile'), or has a storage class specifier
4848 ('register'), then the behavior is undefined; issue an error.
4849 Typedefs for 'void' are OK (see DR#157). */
4850 if (b->prev == 0 /* one binding */
4851 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4852 && !DECL_NAME (b->decl) /* anonymous */
4853 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4855 if (TREE_THIS_VOLATILE (b->decl)
4856 || TREE_READONLY (b->decl)
4857 || C_DECL_REGISTER (b->decl))
4858 error ("%<void%> as only parameter may not be qualified");
4860 /* There cannot be an ellipsis. */
4861 if (ellipsis)
4862 error ("%<void%> must be the only parameter");
4864 arg_info->types = void_list_node;
4865 return arg_info;
4868 if (!ellipsis)
4869 types = void_list_node;
4871 /* Break up the bindings list into parms, tags, types, and others;
4872 apply sanity checks; purge the name-to-decl bindings. */
4873 while (b)
4875 tree decl = b->decl;
4876 tree type = TREE_TYPE (decl);
4877 const char *keyword;
4879 switch (TREE_CODE (decl))
4881 case PARM_DECL:
4882 if (b->id)
4884 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4885 I_SYMBOL_BINDING (b->id) = b->shadowed;
4888 /* Check for forward decls that never got their actual decl. */
4889 if (TREE_ASM_WRITTEN (decl))
4890 error ("%Jparameter %qD has just a forward declaration",
4891 decl, decl);
4892 /* Check for (..., void, ...) and issue an error. */
4893 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4895 if (!gave_void_only_once_err)
4897 error ("%<void%> must be the only parameter");
4898 gave_void_only_once_err = true;
4901 else
4903 /* Valid parameter, add it to the list. */
4904 TREE_CHAIN (decl) = parms;
4905 parms = decl;
4907 /* Since there is a prototype, args are passed in their
4908 declared types. The back end may override this later. */
4909 DECL_ARG_TYPE (decl) = type;
4910 types = tree_cons (0, type, types);
4912 break;
4914 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4915 case UNION_TYPE: keyword = "union"; goto tag;
4916 case RECORD_TYPE: keyword = "struct"; goto tag;
4917 tag:
4918 /* Types may not have tag-names, in which case the type
4919 appears in the bindings list with b->id NULL. */
4920 if (b->id)
4922 gcc_assert (I_TAG_BINDING (b->id) == b);
4923 I_TAG_BINDING (b->id) = b->shadowed;
4926 /* Warn about any struct, union or enum tags defined in a
4927 parameter list. The scope of such types is limited to
4928 the parameter list, which is rarely if ever desirable
4929 (it's impossible to call such a function with type-
4930 correct arguments). An anonymous union parm type is
4931 meaningful as a GNU extension, so don't warn for that. */
4932 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4934 if (b->id)
4935 /* The %s will be one of 'struct', 'union', or 'enum'. */
4936 warning (0, "%<%s %E%> declared inside parameter list",
4937 keyword, b->id);
4938 else
4939 /* The %s will be one of 'struct', 'union', or 'enum'. */
4940 warning (0, "anonymous %s declared inside parameter list",
4941 keyword);
4943 if (!explained_incomplete_types)
4945 warning (0, "its scope is only this definition or declaration,"
4946 " which is probably not what you want");
4947 explained_incomplete_types = true;
4951 tags = tree_cons (b->id, decl, tags);
4952 break;
4954 case CONST_DECL:
4955 case TYPE_DECL:
4956 case FUNCTION_DECL:
4957 /* CONST_DECLs appear here when we have an embedded enum,
4958 and TYPE_DECLs appear here when we have an embedded struct
4959 or union. No warnings for this - we already warned about the
4960 type itself. FUNCTION_DECLs appear when there is an implicit
4961 function declaration in the parameter list. */
4963 TREE_CHAIN (decl) = others;
4964 others = decl;
4965 /* fall through */
4967 case ERROR_MARK:
4968 /* error_mark_node appears here when we have an undeclared
4969 variable. Just throw it away. */
4970 if (b->id)
4972 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4973 I_SYMBOL_BINDING (b->id) = b->shadowed;
4975 break;
4977 /* Other things that might be encountered. */
4978 case LABEL_DECL:
4979 case VAR_DECL:
4980 default:
4981 gcc_unreachable ();
4984 b = free_binding_and_advance (b);
4987 arg_info->parms = parms;
4988 arg_info->tags = tags;
4989 arg_info->types = types;
4990 arg_info->others = others;
4991 return arg_info;
4994 /* Get the struct, enum or union (CODE says which) with tag NAME.
4995 Define the tag as a forward-reference if it is not defined.
4996 Return a c_typespec structure for the type specifier. */
4998 struct c_typespec
4999 parser_xref_tag (enum tree_code code, tree name)
5001 struct c_typespec ret;
5002 /* If a cross reference is requested, look up the type
5003 already defined for this tag and return it. */
5005 tree ref = lookup_tag (code, name, 0);
5006 /* If this is the right type of tag, return what we found.
5007 (This reference will be shadowed by shadow_tag later if appropriate.)
5008 If this is the wrong type of tag, do not return it. If it was the
5009 wrong type in the same scope, we will have had an error
5010 message already; if in a different scope and declaring
5011 a name, pending_xref_error will give an error message; but if in a
5012 different scope and not declaring a name, this tag should
5013 shadow the previous declaration of a different type of tag, and
5014 this would not work properly if we return the reference found.
5015 (For example, with "struct foo" in an outer scope, "union foo;"
5016 must shadow that tag with a new one of union type.) */
5017 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5018 if (ref && TREE_CODE (ref) == code)
5020 ret.spec = ref;
5021 return ret;
5024 /* If no such tag is yet defined, create a forward-reference node
5025 and record it as the "definition".
5026 When a real declaration of this type is found,
5027 the forward-reference will be altered into a real type. */
5029 ref = make_node (code);
5030 if (code == ENUMERAL_TYPE)
5032 /* Give the type a default layout like unsigned int
5033 to avoid crashing if it does not get defined. */
5034 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5035 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5036 TYPE_USER_ALIGN (ref) = 0;
5037 TYPE_UNSIGNED (ref) = 1;
5038 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5039 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5040 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5043 pushtag (name, ref);
5045 ret.spec = ref;
5046 return ret;
5049 /* Get the struct, enum or union (CODE says which) with tag NAME.
5050 Define the tag as a forward-reference if it is not defined.
5051 Return a tree for the type. */
5053 tree
5054 xref_tag (enum tree_code code, tree name)
5056 return parser_xref_tag (code, name).spec;
5059 /* Make sure that the tag NAME is defined *in the current scope*
5060 at least as a forward reference.
5061 CODE says which kind of tag NAME ought to be. */
5063 tree
5064 start_struct (enum tree_code code, tree name)
5066 /* If there is already a tag defined at this scope
5067 (as a forward reference), just return it. */
5069 tree ref = 0;
5071 if (name != 0)
5072 ref = lookup_tag (code, name, 1);
5073 if (ref && TREE_CODE (ref) == code)
5075 if (TYPE_SIZE (ref))
5077 if (code == UNION_TYPE)
5078 error ("redefinition of %<union %E%>", name);
5079 else
5080 error ("redefinition of %<struct %E%>", name);
5082 else if (C_TYPE_BEING_DEFINED (ref))
5084 if (code == UNION_TYPE)
5085 error ("nested redefinition of %<union %E%>", name);
5086 else
5087 error ("nested redefinition of %<struct %E%>", name);
5090 else
5092 /* Otherwise create a forward-reference just so the tag is in scope. */
5094 ref = make_node (code);
5095 pushtag (name, ref);
5098 C_TYPE_BEING_DEFINED (ref) = 1;
5099 TYPE_PACKED (ref) = flag_pack_struct;
5100 return ref;
5103 /* Process the specs, declarator and width (NULL if omitted)
5104 of a structure component, returning a FIELD_DECL node.
5105 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5107 This is done during the parsing of the struct declaration.
5108 The FIELD_DECL nodes are chained together and the lot of them
5109 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5111 tree
5112 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5113 tree width)
5115 tree value;
5117 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5118 && width == NULL_TREE)
5120 /* This is an unnamed decl.
5122 If we have something of the form "union { list } ;" then this
5123 is the anonymous union extension. Similarly for struct.
5125 If this is something of the form "struct foo;", then
5126 If MS extensions are enabled, this is handled as an
5127 anonymous struct.
5128 Otherwise this is a forward declaration of a structure tag.
5130 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5131 If MS extensions are enabled and foo names a structure, then
5132 again this is an anonymous struct.
5133 Otherwise this is an error.
5135 Oh what a horrid tangled web we weave. I wonder if MS consciously
5136 took this from Plan 9 or if it was an accident of implementation
5137 that took root before someone noticed the bug... */
5139 tree type = declspecs->type;
5140 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5141 || TREE_CODE (type) == UNION_TYPE);
5142 bool ok = false;
5144 if (type_ok
5145 && (flag_ms_extensions || !declspecs->typedef_p))
5147 if (flag_ms_extensions)
5148 ok = true;
5149 else if (flag_iso)
5150 ok = false;
5151 else if (TYPE_NAME (type) == NULL)
5152 ok = true;
5153 else
5154 ok = false;
5156 if (!ok)
5158 pedwarn ("declaration does not declare anything");
5159 return NULL_TREE;
5161 if (pedantic)
5162 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5165 value = grokdeclarator (declarator, declspecs, FIELD, false,
5166 width ? &width : NULL);
5168 finish_decl (value, NULL_TREE, NULL_TREE);
5169 DECL_INITIAL (value) = width;
5171 return value;
5174 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5175 the list such that this does not present a problem later. */
5177 static void
5178 detect_field_duplicates (tree fieldlist)
5180 tree x, y;
5181 int timeout = 10;
5183 /* First, see if there are more than "a few" fields.
5184 This is trivially true if there are zero or one fields. */
5185 if (!fieldlist)
5186 return;
5187 x = TREE_CHAIN (fieldlist);
5188 if (!x)
5189 return;
5190 do {
5191 timeout--;
5192 x = TREE_CHAIN (x);
5193 } while (timeout > 0 && x);
5195 /* If there were "few" fields, avoid the overhead of allocating
5196 a hash table. Instead just do the nested traversal thing. */
5197 if (timeout > 0)
5199 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5200 if (DECL_NAME (x))
5202 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5203 if (DECL_NAME (y) == DECL_NAME (x))
5205 error ("%Jduplicate member %qD", x, x);
5206 DECL_NAME (x) = NULL_TREE;
5210 else
5212 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5213 void **slot;
5215 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5216 if ((y = DECL_NAME (x)) != 0)
5218 slot = htab_find_slot (htab, y, INSERT);
5219 if (*slot)
5221 error ("%Jduplicate member %qD", x, x);
5222 DECL_NAME (x) = NULL_TREE;
5224 *slot = y;
5227 htab_delete (htab);
5231 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5232 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5233 ATTRIBUTES are attributes to be applied to the structure. */
5235 tree
5236 finish_struct (tree t, tree fieldlist, tree attributes)
5238 tree x;
5239 bool toplevel = file_scope == current_scope;
5240 int saw_named_field;
5242 /* If this type was previously laid out as a forward reference,
5243 make sure we lay it out again. */
5245 TYPE_SIZE (t) = 0;
5247 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5249 if (pedantic)
5251 for (x = fieldlist; x; x = TREE_CHAIN (x))
5252 if (DECL_NAME (x) != 0)
5253 break;
5255 if (x == 0)
5257 if (TREE_CODE (t) == UNION_TYPE)
5259 if (fieldlist)
5260 pedwarn ("union has no named members");
5261 else
5262 pedwarn ("union has no members");
5264 else
5266 if (fieldlist)
5267 pedwarn ("struct has no named members");
5268 else
5269 pedwarn ("struct has no members");
5274 /* Install struct as DECL_CONTEXT of each field decl.
5275 Also process specified field sizes, found in the DECL_INITIAL,
5276 storing 0 there after the type has been changed to precision equal
5277 to its width, rather than the precision of the specified standard
5278 type. (Correct layout requires the original type to have been preserved
5279 until now.) */
5281 saw_named_field = 0;
5282 for (x = fieldlist; x; x = TREE_CHAIN (x))
5284 DECL_CONTEXT (x) = t;
5285 DECL_PACKED (x) |= TYPE_PACKED (t);
5287 /* If any field is const, the structure type is pseudo-const. */
5288 if (TREE_READONLY (x))
5289 C_TYPE_FIELDS_READONLY (t) = 1;
5290 else
5292 /* A field that is pseudo-const makes the structure likewise. */
5293 tree t1 = TREE_TYPE (x);
5294 while (TREE_CODE (t1) == ARRAY_TYPE)
5295 t1 = TREE_TYPE (t1);
5296 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5297 && C_TYPE_FIELDS_READONLY (t1))
5298 C_TYPE_FIELDS_READONLY (t) = 1;
5301 /* Any field that is volatile means variables of this type must be
5302 treated in some ways as volatile. */
5303 if (TREE_THIS_VOLATILE (x))
5304 C_TYPE_FIELDS_VOLATILE (t) = 1;
5306 /* Any field of nominal variable size implies structure is too. */
5307 if (C_DECL_VARIABLE_SIZE (x))
5308 C_TYPE_VARIABLE_SIZE (t) = 1;
5310 if (DECL_INITIAL (x))
5312 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5313 DECL_SIZE (x) = bitsize_int (width);
5314 DECL_BIT_FIELD (x) = 1;
5315 SET_DECL_C_BIT_FIELD (x);
5318 /* Detect flexible array member in an invalid context. */
5319 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5320 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5321 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5322 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5324 if (TREE_CODE (t) == UNION_TYPE)
5326 error ("%Jflexible array member in union", x);
5327 TREE_TYPE (x) = error_mark_node;
5329 else if (TREE_CHAIN (x) != NULL_TREE)
5331 error ("%Jflexible array member not at end of struct", x);
5332 TREE_TYPE (x) = error_mark_node;
5334 else if (!saw_named_field)
5336 error ("%Jflexible array member in otherwise empty struct", x);
5337 TREE_TYPE (x) = error_mark_node;
5341 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5342 && flexible_array_type_p (TREE_TYPE (x)))
5343 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5345 if (DECL_NAME (x))
5346 saw_named_field = 1;
5349 detect_field_duplicates (fieldlist);
5351 /* Now we have the nearly final fieldlist. Record it,
5352 then lay out the structure or union (including the fields). */
5354 TYPE_FIELDS (t) = fieldlist;
5356 layout_type (t);
5358 /* Give bit-fields their proper types. */
5360 tree *fieldlistp = &fieldlist;
5361 while (*fieldlistp)
5362 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5363 && TREE_TYPE (*fieldlistp) != error_mark_node)
5365 unsigned HOST_WIDE_INT width
5366 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5367 tree type = TREE_TYPE (*fieldlistp);
5368 if (width != TYPE_PRECISION (type))
5370 TREE_TYPE (*fieldlistp)
5371 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5372 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5374 DECL_INITIAL (*fieldlistp) = 0;
5376 else
5377 fieldlistp = &TREE_CHAIN (*fieldlistp);
5380 /* Now we have the truly final field list.
5381 Store it in this type and in the variants. */
5383 TYPE_FIELDS (t) = fieldlist;
5385 /* If there are lots of fields, sort so we can look through them fast.
5386 We arbitrarily consider 16 or more elts to be "a lot". */
5389 int len = 0;
5391 for (x = fieldlist; x; x = TREE_CHAIN (x))
5393 if (len > 15 || DECL_NAME (x) == NULL)
5394 break;
5395 len += 1;
5398 if (len > 15)
5400 tree *field_array;
5401 struct lang_type *space;
5402 struct sorted_fields_type *space2;
5404 len += list_length (x);
5406 /* Use the same allocation policy here that make_node uses, to
5407 ensure that this lives as long as the rest of the struct decl.
5408 All decls in an inline function need to be saved. */
5410 space = GGC_CNEW (struct lang_type);
5411 space2 = GGC_NEWVAR (struct sorted_fields_type,
5412 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5414 len = 0;
5415 space->s = space2;
5416 field_array = &space2->elts[0];
5417 for (x = fieldlist; x; x = TREE_CHAIN (x))
5419 field_array[len++] = x;
5421 /* If there is anonymous struct or union, break out of the loop. */
5422 if (DECL_NAME (x) == NULL)
5423 break;
5425 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5426 if (x == NULL)
5428 TYPE_LANG_SPECIFIC (t) = space;
5429 TYPE_LANG_SPECIFIC (t)->s->len = len;
5430 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5431 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5436 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5438 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5439 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5440 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5441 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5444 /* If this was supposed to be a transparent union, but we can't
5445 make it one, warn and turn off the flag. */
5446 if (TREE_CODE (t) == UNION_TYPE
5447 && TYPE_TRANSPARENT_UNION (t)
5448 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5450 TYPE_TRANSPARENT_UNION (t) = 0;
5451 warning (0, "union cannot be made transparent");
5454 /* If this structure or union completes the type of any previous
5455 variable declaration, lay it out and output its rtl. */
5456 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5458 x = TREE_CHAIN (x))
5460 tree decl = TREE_VALUE (x);
5461 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5462 layout_array_type (TREE_TYPE (decl));
5463 if (TREE_CODE (decl) != TYPE_DECL)
5465 layout_decl (decl, 0);
5466 if (c_dialect_objc ())
5467 objc_check_decl (decl);
5468 rest_of_decl_compilation (decl, toplevel, 0);
5469 if (!toplevel)
5470 expand_decl (decl);
5473 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5475 /* Finish debugging output for this type. */
5476 rest_of_type_compilation (t, toplevel);
5478 /* If we're inside a function proper, i.e. not file-scope and not still
5479 parsing parameters, then arrange for the size of a variable sized type
5480 to be bound now. */
5481 if (cur_stmt_list && variably_modified_type_p (t, NULL))
5482 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5484 return t;
5487 /* Lay out the type T, and its element type, and so on. */
5489 static void
5490 layout_array_type (tree t)
5492 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5493 layout_array_type (TREE_TYPE (t));
5494 layout_type (t);
5497 /* Begin compiling the definition of an enumeration type.
5498 NAME is its name (or null if anonymous).
5499 Returns the type object, as yet incomplete.
5500 Also records info about it so that build_enumerator
5501 may be used to declare the individual values as they are read. */
5503 tree
5504 start_enum (tree name)
5506 tree enumtype = 0;
5508 /* If this is the real definition for a previous forward reference,
5509 fill in the contents in the same object that used to be the
5510 forward reference. */
5512 if (name != 0)
5513 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5515 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5517 enumtype = make_node (ENUMERAL_TYPE);
5518 pushtag (name, enumtype);
5521 if (C_TYPE_BEING_DEFINED (enumtype))
5522 error ("nested redefinition of %<enum %E%>", name);
5524 C_TYPE_BEING_DEFINED (enumtype) = 1;
5526 if (TYPE_VALUES (enumtype) != 0)
5528 /* This enum is a named one that has been declared already. */
5529 error ("redeclaration of %<enum %E%>", name);
5531 /* Completely replace its old definition.
5532 The old enumerators remain defined, however. */
5533 TYPE_VALUES (enumtype) = 0;
5536 enum_next_value = integer_zero_node;
5537 enum_overflow = 0;
5539 if (flag_short_enums)
5540 TYPE_PACKED (enumtype) = 1;
5542 return enumtype;
5545 /* After processing and defining all the values of an enumeration type,
5546 install their decls in the enumeration type and finish it off.
5547 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5548 and ATTRIBUTES are the specified attributes.
5549 Returns ENUMTYPE. */
5551 tree
5552 finish_enum (tree enumtype, tree values, tree attributes)
5554 tree pair, tem;
5555 tree minnode = 0, maxnode = 0;
5556 int precision, unsign;
5557 bool toplevel = (file_scope == current_scope);
5558 struct lang_type *lt;
5560 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5562 /* Calculate the maximum value of any enumerator in this type. */
5564 if (values == error_mark_node)
5565 minnode = maxnode = integer_zero_node;
5566 else
5568 minnode = maxnode = TREE_VALUE (values);
5569 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5571 tree value = TREE_VALUE (pair);
5572 if (tree_int_cst_lt (maxnode, value))
5573 maxnode = value;
5574 if (tree_int_cst_lt (value, minnode))
5575 minnode = value;
5579 /* Construct the final type of this enumeration. It is the same
5580 as one of the integral types - the narrowest one that fits, except
5581 that normally we only go as narrow as int - and signed iff any of
5582 the values are negative. */
5583 unsign = (tree_int_cst_sgn (minnode) >= 0);
5584 precision = MAX (min_precision (minnode, unsign),
5585 min_precision (maxnode, unsign));
5587 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5589 tem = c_common_type_for_size (precision, unsign);
5590 if (tem == NULL)
5592 warning (0, "enumeration values exceed range of largest integer");
5593 tem = long_long_integer_type_node;
5596 else
5597 tem = unsign ? unsigned_type_node : integer_type_node;
5599 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5600 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5601 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5602 TYPE_SIZE (enumtype) = 0;
5604 /* If the precision of the type was specific with an attribute and it
5605 was too small, give an error. Otherwise, use it. */
5606 if (TYPE_PRECISION (enumtype))
5608 if (precision > TYPE_PRECISION (enumtype))
5609 error ("specified mode too small for enumeral values");
5611 else
5612 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5614 layout_type (enumtype);
5616 if (values != error_mark_node)
5618 /* Change the type of the enumerators to be the enum type. We
5619 need to do this irrespective of the size of the enum, for
5620 proper type checking. Replace the DECL_INITIALs of the
5621 enumerators, and the value slots of the list, with copies
5622 that have the enum type; they cannot be modified in place
5623 because they may be shared (e.g. integer_zero_node) Finally,
5624 change the purpose slots to point to the names of the decls. */
5625 for (pair = values; pair; pair = TREE_CHAIN (pair))
5627 tree enu = TREE_PURPOSE (pair);
5628 tree ini = DECL_INITIAL (enu);
5630 TREE_TYPE (enu) = enumtype;
5632 /* The ISO C Standard mandates enumerators to have type int,
5633 even though the underlying type of an enum type is
5634 unspecified. Here we convert any enumerators that fit in
5635 an int to type int, to avoid promotions to unsigned types
5636 when comparing integers with enumerators that fit in the
5637 int range. When -pedantic is given, build_enumerator()
5638 would have already taken care of those that don't fit. */
5639 if (int_fits_type_p (ini, integer_type_node))
5640 tem = integer_type_node;
5641 else
5642 tem = enumtype;
5643 ini = convert (tem, ini);
5645 DECL_INITIAL (enu) = ini;
5646 TREE_PURPOSE (pair) = DECL_NAME (enu);
5647 TREE_VALUE (pair) = ini;
5650 TYPE_VALUES (enumtype) = values;
5653 /* Record the min/max values so that we can warn about bit-field
5654 enumerations that are too small for the values. */
5655 lt = GGC_CNEW (struct lang_type);
5656 lt->enum_min = minnode;
5657 lt->enum_max = maxnode;
5658 TYPE_LANG_SPECIFIC (enumtype) = lt;
5660 /* Fix up all variant types of this enum type. */
5661 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5663 if (tem == enumtype)
5664 continue;
5665 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5666 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5667 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5668 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5669 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5670 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5671 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5672 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5673 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5674 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5675 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5678 /* Finish debugging output for this type. */
5679 rest_of_type_compilation (enumtype, toplevel);
5681 return enumtype;
5684 /* Build and install a CONST_DECL for one value of the
5685 current enumeration type (one that was begun with start_enum).
5686 Return a tree-list containing the CONST_DECL and its value.
5687 Assignment of sequential values by default is handled here. */
5689 tree
5690 build_enumerator (tree name, tree value)
5692 tree decl, type;
5694 /* Validate and default VALUE. */
5696 if (value != 0)
5698 /* Don't issue more errors for error_mark_node (i.e. an
5699 undeclared identifier) - just ignore the value expression. */
5700 if (value == error_mark_node)
5701 value = 0;
5702 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5703 || TREE_CODE (value) != INTEGER_CST)
5705 error ("enumerator value for %qE is not an integer constant", name);
5706 value = 0;
5708 else
5710 value = default_conversion (value);
5711 constant_expression_warning (value);
5715 /* Default based on previous value. */
5716 /* It should no longer be possible to have NON_LVALUE_EXPR
5717 in the default. */
5718 if (value == 0)
5720 value = enum_next_value;
5721 if (enum_overflow)
5722 error ("overflow in enumeration values");
5725 if (pedantic && !int_fits_type_p (value, integer_type_node))
5727 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5728 /* XXX This causes -pedantic to change the meaning of the program.
5729 Remove? -zw 2004-03-15 */
5730 value = convert (integer_type_node, value);
5733 /* Set basis for default for next value. */
5734 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5735 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5737 /* Now create a declaration for the enum value name. */
5739 type = TREE_TYPE (value);
5740 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5741 TYPE_PRECISION (integer_type_node)),
5742 (TYPE_PRECISION (type)
5743 >= TYPE_PRECISION (integer_type_node)
5744 && TYPE_UNSIGNED (type)));
5746 decl = build_decl (CONST_DECL, name, type);
5747 DECL_INITIAL (decl) = convert (type, value);
5748 pushdecl (decl);
5750 return tree_cons (decl, value, NULL_TREE);
5754 /* Create the FUNCTION_DECL for a function definition.
5755 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5756 the declaration; they describe the function's name and the type it returns,
5757 but twisted together in a fashion that parallels the syntax of C.
5759 This function creates a binding context for the function body
5760 as well as setting up the FUNCTION_DECL in current_function_decl.
5762 Returns 1 on success. If the DECLARATOR is not suitable for a function
5763 (it defines a datum instead), we return 0, which tells
5764 yyparse to report a parse error. */
5767 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5768 tree attributes)
5770 tree decl1, old_decl;
5771 tree restype, resdecl;
5772 struct c_label_context_se *nstack_se;
5773 struct c_label_context_vm *nstack_vm;
5775 current_function_returns_value = 0; /* Assume, until we see it does. */
5776 current_function_returns_null = 0;
5777 current_function_returns_abnormally = 0;
5778 warn_about_return_type = 0;
5779 current_extern_inline = 0;
5780 c_switch_stack = NULL;
5782 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
5783 nstack_se->labels_def = NULL;
5784 nstack_se->labels_used = NULL;
5785 nstack_se->next = label_context_stack_se;
5786 label_context_stack_se = nstack_se;
5788 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
5789 nstack_vm->labels_def = NULL;
5790 nstack_vm->labels_used = NULL;
5791 nstack_vm->scope = 0;
5792 nstack_vm->next = label_context_stack_vm;
5793 label_context_stack_vm = nstack_vm;
5795 /* Indicate no valid break/continue context by setting these variables
5796 to some non-null, non-label value. We'll notice and emit the proper
5797 error message in c_finish_bc_stmt. */
5798 c_break_label = c_cont_label = size_zero_node;
5800 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5802 /* If the declarator is not suitable for a function definition,
5803 cause a syntax error. */
5804 if (decl1 == 0)
5805 return 0;
5807 decl_attributes (&decl1, attributes, 0);
5809 if (DECL_DECLARED_INLINE_P (decl1)
5810 && DECL_UNINLINABLE (decl1)
5811 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5812 warning (OPT_Wattributes, "%Jinline function %qD given attribute noinline",
5813 decl1, decl1);
5815 announce_function (decl1);
5817 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5819 error ("return type is an incomplete type");
5820 /* Make it return void instead. */
5821 TREE_TYPE (decl1)
5822 = build_function_type (void_type_node,
5823 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5826 if (warn_about_return_type)
5827 pedwarn_c99 ("return type defaults to %<int%>");
5829 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5830 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5831 DECL_INITIAL (decl1) = error_mark_node;
5833 /* If this definition isn't a prototype and we had a prototype declaration
5834 before, copy the arg type info from that prototype. */
5835 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5836 current_function_prototype_locus = UNKNOWN_LOCATION;
5837 current_function_prototype_built_in = false;
5838 current_function_prototype_arg_types = NULL_TREE;
5839 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5841 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5842 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5843 TREE_TYPE (TREE_TYPE (old_decl))))
5845 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5846 TREE_TYPE (decl1));
5847 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5848 current_function_prototype_built_in
5849 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
5850 current_function_prototype_arg_types
5851 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
5853 if (TREE_PUBLIC (decl1))
5855 /* If there is an external prototype declaration of this
5856 function, record its location but do not copy information
5857 to this decl. This may be an invisible declaration
5858 (built-in or in a scope which has finished) or simply
5859 have more refined argument types than any declaration
5860 found above. */
5861 struct c_binding *b;
5862 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
5863 if (B_IN_SCOPE (b, external_scope))
5864 break;
5865 if (b)
5867 tree ext_decl, ext_type;
5868 ext_decl = b->decl;
5869 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
5870 if (TREE_CODE (ext_type) == FUNCTION_TYPE
5871 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5872 TREE_TYPE (ext_type)))
5874 current_function_prototype_locus
5875 = DECL_SOURCE_LOCATION (ext_decl);
5876 current_function_prototype_built_in
5877 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
5878 current_function_prototype_arg_types
5879 = TYPE_ARG_TYPES (ext_type);
5885 /* Optionally warn of old-fashioned def with no previous prototype. */
5886 if (warn_strict_prototypes
5887 && old_decl != error_mark_node
5888 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5889 && C_DECL_ISNT_PROTOTYPE (old_decl))
5890 warning (0, "function declaration isn%'t a prototype");
5891 /* Optionally warn of any global def with no previous prototype. */
5892 else if (warn_missing_prototypes
5893 && old_decl != error_mark_node
5894 && TREE_PUBLIC (decl1)
5895 && !MAIN_NAME_P (DECL_NAME (decl1))
5896 && C_DECL_ISNT_PROTOTYPE (old_decl))
5897 warning (0, "%Jno previous prototype for %qD", decl1, decl1);
5898 /* Optionally warn of any def with no previous prototype
5899 if the function has already been used. */
5900 else if (warn_missing_prototypes
5901 && old_decl != 0
5902 && old_decl != error_mark_node
5903 && TREE_USED (old_decl)
5904 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5905 warning (0, "%J%qD was used with no prototype before its definition",
5906 decl1, decl1);
5907 /* Optionally warn of any global def with no previous declaration. */
5908 else if (warn_missing_declarations
5909 && TREE_PUBLIC (decl1)
5910 && old_decl == 0
5911 && !MAIN_NAME_P (DECL_NAME (decl1)))
5912 warning (0, "%Jno previous declaration for %qD", decl1, decl1);
5913 /* Optionally warn of any def with no previous declaration
5914 if the function has already been used. */
5915 else if (warn_missing_declarations
5916 && old_decl != 0
5917 && old_decl != error_mark_node
5918 && TREE_USED (old_decl)
5919 && C_DECL_IMPLICIT (old_decl))
5920 warning (0, "%J%qD was used with no declaration before its definition",
5921 decl1, decl1);
5923 /* This is a definition, not a reference.
5924 So normally clear DECL_EXTERNAL.
5925 However, `extern inline' acts like a declaration
5926 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5927 DECL_EXTERNAL (decl1) = current_extern_inline;
5929 /* This function exists in static storage.
5930 (This does not mean `static' in the C sense!) */
5931 TREE_STATIC (decl1) = 1;
5933 /* A nested function is not global. */
5934 if (current_function_decl != 0)
5935 TREE_PUBLIC (decl1) = 0;
5937 /* This is the earliest point at which we might know the assembler
5938 name of the function. Thus, if it's set before this, die horribly. */
5939 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5941 /* If #pragma weak was used, mark the decl weak now. */
5942 if (current_scope == file_scope)
5943 maybe_apply_pragma_weak (decl1);
5945 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5946 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5948 tree args;
5949 int argct = 0;
5951 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5952 != integer_type_node)
5953 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5955 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5956 args = TREE_CHAIN (args))
5958 tree type = args ? TREE_VALUE (args) : 0;
5960 if (type == void_type_node)
5961 break;
5963 ++argct;
5964 switch (argct)
5966 case 1:
5967 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5968 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5969 decl1, decl1);
5970 break;
5972 case 2:
5973 if (TREE_CODE (type) != POINTER_TYPE
5974 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5975 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5976 != char_type_node))
5977 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5978 decl1, decl1);
5979 break;
5981 case 3:
5982 if (TREE_CODE (type) != POINTER_TYPE
5983 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5984 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5985 != char_type_node))
5986 pedwarn ("%Jthird argument of %qD should probably be "
5987 "%<char **%>", decl1, decl1);
5988 break;
5992 /* It is intentional that this message does not mention the third
5993 argument because it's only mentioned in an appendix of the
5994 standard. */
5995 if (argct > 0 && (argct < 2 || argct > 3))
5996 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
5998 if (!TREE_PUBLIC (decl1))
5999 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
6002 /* Record the decl so that the function name is defined.
6003 If we already have a decl for this name, and it is a FUNCTION_DECL,
6004 use the old decl. */
6006 current_function_decl = pushdecl (decl1);
6008 push_scope ();
6009 declare_parm_level ();
6011 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6012 /* Promote the value to int before returning it. */
6013 if (c_promoting_integer_type_p (restype))
6015 /* It retains unsignedness if not really getting wider. */
6016 if (TYPE_UNSIGNED (restype)
6017 && (TYPE_PRECISION (restype)
6018 == TYPE_PRECISION (integer_type_node)))
6019 restype = unsigned_type_node;
6020 else
6021 restype = integer_type_node;
6024 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6025 DECL_ARTIFICIAL (resdecl) = 1;
6026 DECL_IGNORED_P (resdecl) = 1;
6027 DECL_RESULT (current_function_decl) = resdecl;
6029 start_fname_decls ();
6031 return 1;
6034 /* Subroutine of store_parm_decls which handles new-style function
6035 definitions (prototype format). The parms already have decls, so we
6036 need only record them as in effect and complain if any redundant
6037 old-style parm decls were written. */
6038 static void
6039 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6041 tree decl;
6043 if (current_scope->bindings)
6045 error ("%Jold-style parameter declarations in prototyped "
6046 "function definition", fndecl);
6048 /* Get rid of the old-style declarations. */
6049 pop_scope ();
6050 push_scope ();
6052 /* Don't issue this warning for nested functions, and don't issue this
6053 warning if we got here because ARG_INFO_TYPES was error_mark_node
6054 (this happens when a function definition has just an ellipsis in
6055 its parameter list). */
6056 else if (warn_traditional && !in_system_header && !current_function_scope
6057 && arg_info->types != error_mark_node)
6058 warning (0, "%Jtraditional C rejects ISO C style function definitions",
6059 fndecl);
6061 /* Now make all the parameter declarations visible in the function body.
6062 We can bypass most of the grunt work of pushdecl. */
6063 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6065 DECL_CONTEXT (decl) = current_function_decl;
6066 if (DECL_NAME (decl))
6067 bind (DECL_NAME (decl), decl, current_scope,
6068 /*invisible=*/false, /*nested=*/false);
6069 else
6070 error ("%Jparameter name omitted", decl);
6073 /* Record the parameter list in the function declaration. */
6074 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6076 /* Now make all the ancillary declarations visible, likewise. */
6077 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6079 DECL_CONTEXT (decl) = current_function_decl;
6080 if (DECL_NAME (decl))
6081 bind (DECL_NAME (decl), decl, current_scope,
6082 /*invisible=*/false, /*nested=*/false);
6085 /* And all the tag declarations. */
6086 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6087 if (TREE_PURPOSE (decl))
6088 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6089 /*invisible=*/false, /*nested=*/false);
6092 /* Subroutine of store_parm_decls which handles old-style function
6093 definitions (separate parameter list and declarations). */
6095 static void
6096 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6098 struct c_binding *b;
6099 tree parm, decl, last;
6100 tree parmids = arg_info->parms;
6102 /* We use DECL_WEAK as a flag to show which parameters have been
6103 seen already, since it is not used on PARM_DECL. */
6104 #ifdef ENABLE_CHECKING
6105 for (b = current_scope->bindings; b; b = b->prev)
6106 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
6107 #endif
6109 if (!in_system_header)
6110 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6111 fndecl);
6113 /* Match each formal parameter name with its declaration. Save each
6114 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6115 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6117 if (TREE_VALUE (parm) == 0)
6119 error ("%Jparameter name missing from parameter list", fndecl);
6120 TREE_PURPOSE (parm) = 0;
6121 continue;
6124 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6125 if (b && B_IN_CURRENT_SCOPE (b))
6127 decl = b->decl;
6128 /* If we got something other than a PARM_DECL it is an error. */
6129 if (TREE_CODE (decl) != PARM_DECL)
6130 error ("%J%qD declared as a non-parameter", decl, decl);
6131 /* If the declaration is already marked, we have a duplicate
6132 name. Complain and ignore the duplicate. */
6133 else if (DECL_WEAK (decl))
6135 error ("%Jmultiple parameters named %qD", decl, decl);
6136 TREE_PURPOSE (parm) = 0;
6137 continue;
6139 /* If the declaration says "void", complain and turn it into
6140 an int. */
6141 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6143 error ("%Jparameter %qD declared with void type", decl, decl);
6144 TREE_TYPE (decl) = integer_type_node;
6145 DECL_ARG_TYPE (decl) = integer_type_node;
6146 layout_decl (decl, 0);
6149 /* If no declaration found, default to int. */
6150 else
6152 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6153 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6154 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6155 pushdecl (decl);
6157 if (flag_isoc99)
6158 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6159 else if (extra_warnings)
6160 warning (0, "%Jtype of %qD defaults to %<int%>", decl, decl);
6163 TREE_PURPOSE (parm) = decl;
6164 DECL_WEAK (decl) = 1;
6167 /* Now examine the parms chain for incomplete declarations
6168 and declarations with no corresponding names. */
6170 for (b = current_scope->bindings; b; b = b->prev)
6172 parm = b->decl;
6173 if (TREE_CODE (parm) != PARM_DECL)
6174 continue;
6176 if (TREE_TYPE (parm) != error_mark_node
6177 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6179 error ("%Jparameter %qD has incomplete type", parm, parm);
6180 TREE_TYPE (parm) = error_mark_node;
6183 if (!DECL_WEAK (parm))
6185 error ("%Jdeclaration for parameter %qD but no such parameter",
6186 parm, parm);
6188 /* Pretend the parameter was not missing.
6189 This gets us to a standard state and minimizes
6190 further error messages. */
6191 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6195 /* Chain the declarations together in the order of the list of
6196 names. Store that chain in the function decl, replacing the
6197 list of names. Update the current scope to match. */
6198 DECL_ARGUMENTS (fndecl) = 0;
6200 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6201 if (TREE_PURPOSE (parm))
6202 break;
6203 if (parm && TREE_PURPOSE (parm))
6205 last = TREE_PURPOSE (parm);
6206 DECL_ARGUMENTS (fndecl) = last;
6207 DECL_WEAK (last) = 0;
6209 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6210 if (TREE_PURPOSE (parm))
6212 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6213 last = TREE_PURPOSE (parm);
6214 DECL_WEAK (last) = 0;
6216 TREE_CHAIN (last) = 0;
6219 /* If there was a previous prototype,
6220 set the DECL_ARG_TYPE of each argument according to
6221 the type previously specified, and report any mismatches. */
6223 if (current_function_prototype_arg_types)
6225 tree type;
6226 for (parm = DECL_ARGUMENTS (fndecl),
6227 type = current_function_prototype_arg_types;
6228 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6229 != void_type_node));
6230 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6232 if (parm == 0 || type == 0
6233 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6235 if (current_function_prototype_built_in)
6236 warning (0, "number of arguments doesn%'t match "
6237 "built-in prototype");
6238 else
6240 error ("number of arguments doesn%'t match prototype");
6241 error ("%Hprototype declaration",
6242 &current_function_prototype_locus);
6244 break;
6246 /* Type for passing arg must be consistent with that
6247 declared for the arg. ISO C says we take the unqualified
6248 type for parameters declared with qualified type. */
6249 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6250 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6252 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6253 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6255 /* Adjust argument to match prototype. E.g. a previous
6256 `int foo(float);' prototype causes
6257 `int foo(x) float x; {...}' to be treated like
6258 `int foo(float x) {...}'. This is particularly
6259 useful for argument types like uid_t. */
6260 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6262 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6263 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6264 && TYPE_PRECISION (TREE_TYPE (parm))
6265 < TYPE_PRECISION (integer_type_node))
6266 DECL_ARG_TYPE (parm) = integer_type_node;
6268 if (pedantic)
6270 /* ??? Is it possible to get here with a
6271 built-in prototype or will it always have
6272 been diagnosed as conflicting with an
6273 old-style definition and discarded? */
6274 if (current_function_prototype_built_in)
6275 warning (0, "promoted argument %qD "
6276 "doesn%'t match built-in prototype", parm);
6277 else
6279 pedwarn ("promoted argument %qD "
6280 "doesn%'t match prototype", parm);
6281 pedwarn ("%Hprototype declaration",
6282 &current_function_prototype_locus);
6286 else
6288 if (current_function_prototype_built_in)
6289 warning (0, "argument %qD doesn%'t match "
6290 "built-in prototype", parm);
6291 else
6293 error ("argument %qD doesn%'t match prototype", parm);
6294 error ("%Hprototype declaration",
6295 &current_function_prototype_locus);
6300 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6303 /* Otherwise, create a prototype that would match. */
6305 else
6307 tree actual = 0, last = 0, type;
6309 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6311 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6312 if (last)
6313 TREE_CHAIN (last) = type;
6314 else
6315 actual = type;
6316 last = type;
6318 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6319 if (last)
6320 TREE_CHAIN (last) = type;
6321 else
6322 actual = type;
6324 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6325 of the type of this function, but we need to avoid having this
6326 affect the types of other similarly-typed functions, so we must
6327 first force the generation of an identical (but separate) type
6328 node for the relevant function type. The new node we create
6329 will be a variant of the main variant of the original function
6330 type. */
6332 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6334 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6338 /* Store parameter declarations passed in ARG_INFO into the current
6339 function declaration. */
6341 void
6342 store_parm_decls_from (struct c_arg_info *arg_info)
6344 current_function_arg_info = arg_info;
6345 store_parm_decls ();
6348 /* Store the parameter declarations into the current function declaration.
6349 This is called after parsing the parameter declarations, before
6350 digesting the body of the function.
6352 For an old-style definition, construct a prototype out of the old-style
6353 parameter declarations and inject it into the function's type. */
6355 void
6356 store_parm_decls (void)
6358 tree fndecl = current_function_decl;
6359 bool proto;
6361 /* The argument information block for FNDECL. */
6362 struct c_arg_info *arg_info = current_function_arg_info;
6363 current_function_arg_info = 0;
6365 /* True if this definition is written with a prototype. Note:
6366 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6367 list in a function definition as equivalent to (void) -- an
6368 empty argument list specifies the function has no parameters,
6369 but only (void) sets up a prototype for future calls. */
6370 proto = arg_info->types != 0;
6372 if (proto)
6373 store_parm_decls_newstyle (fndecl, arg_info);
6374 else
6375 store_parm_decls_oldstyle (fndecl, arg_info);
6377 /* The next call to push_scope will be a function body. */
6379 next_is_function_body = true;
6381 /* Write a record describing this function definition to the prototypes
6382 file (if requested). */
6384 gen_aux_info_record (fndecl, 1, 0, proto);
6386 /* Initialize the RTL code for the function. */
6387 allocate_struct_function (fndecl);
6389 /* Begin the statement tree for this function. */
6390 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6392 /* ??? Insert the contents of the pending sizes list into the function
6393 to be evaluated. The only reason left to have this is
6394 void foo(int n, int array[n++])
6395 because we throw away the array type in favor of a pointer type, and
6396 thus won't naturally see the SAVE_EXPR containing the increment. All
6397 other pending sizes would be handled by gimplify_parameters. */
6399 tree t;
6400 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6401 add_stmt (TREE_VALUE (t));
6404 /* Even though we're inside a function body, we still don't want to
6405 call expand_expr to calculate the size of a variable-sized array.
6406 We haven't necessarily assigned RTL to all variables yet, so it's
6407 not safe to try to expand expressions involving them. */
6408 cfun->x_dont_save_pending_sizes_p = 1;
6411 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6412 functions. */
6414 static void
6415 c_warn_unused_result_recursively (tree fndecl)
6417 struct cgraph_node *cgn;
6419 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6420 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6422 /* Finalize all nested functions now. */
6423 cgn = cgraph_node (fndecl);
6424 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6425 c_warn_unused_result_recursively (cgn->decl);
6428 /* Finish up a function declaration and compile that function
6429 all the way to assembler language output. The free the storage
6430 for the function definition.
6432 This is called after parsing the body of the function definition. */
6434 void
6435 finish_function (void)
6437 tree fndecl = current_function_decl;
6439 label_context_stack_se = label_context_stack_se->next;
6440 label_context_stack_vm = label_context_stack_vm->next;
6442 if (TREE_CODE (fndecl) == FUNCTION_DECL
6443 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6445 tree args = DECL_ARGUMENTS (fndecl);
6446 for (; args; args = TREE_CHAIN (args))
6448 tree type = TREE_TYPE (args);
6449 if (INTEGRAL_TYPE_P (type)
6450 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6451 DECL_ARG_TYPE (args) = integer_type_node;
6455 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6456 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6458 /* Must mark the RESULT_DECL as being in this function. */
6460 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6461 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6463 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6465 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6466 != integer_type_node)
6468 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6469 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6470 if (!warn_main)
6471 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6473 else
6475 if (flag_isoc99)
6477 tree stmt = c_finish_return (integer_zero_node);
6478 #ifdef USE_MAPPED_LOCATION
6479 /* Hack. We don't want the middle-end to warn that this return
6480 is unreachable, so we mark its location as special. Using
6481 UNKNOWN_LOCATION has the problem that it gets clobbered in
6482 annotate_one_with_locus. A cleaner solution might be to
6483 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6485 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6486 #else
6487 /* Hack. We don't want the middle-end to warn that this
6488 return is unreachable, so put the statement on the
6489 special line 0. */
6490 annotate_with_file_line (stmt, input_filename, 0);
6491 #endif
6496 /* Tie off the statement tree for this function. */
6497 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6499 finish_fname_decls ();
6501 /* Complain if there's just no return statement. */
6502 if (warn_return_type
6503 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6504 && !current_function_returns_value && !current_function_returns_null
6505 /* Don't complain if we are no-return. */
6506 && !current_function_returns_abnormally
6507 /* Don't warn for main(). */
6508 && !MAIN_NAME_P (DECL_NAME (fndecl))
6509 /* Or if they didn't actually specify a return type. */
6510 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6511 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6512 inline function, as we might never be compiled separately. */
6513 && DECL_INLINE (fndecl))
6515 warning (0, "no return statement in function returning non-void");
6516 TREE_NO_WARNING (fndecl) = 1;
6519 /* With just -Wextra, complain only if function returns both with
6520 and without a value. */
6521 if (extra_warnings
6522 && current_function_returns_value
6523 && current_function_returns_null)
6524 warning (0, "this function may return with or without a value");
6526 /* Store the end of the function, so that we get good line number
6527 info for the epilogue. */
6528 cfun->function_end_locus = input_location;
6530 /* If we don't have ctors/dtors sections, and this is a static
6531 constructor or destructor, it must be recorded now. */
6532 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6533 && !targetm.have_ctors_dtors)
6534 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6535 if (DECL_STATIC_DESTRUCTOR (fndecl)
6536 && !targetm.have_ctors_dtors)
6537 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6539 /* Finalize the ELF visibility for the function. */
6540 c_determine_visibility (fndecl);
6542 /* Genericize before inlining. Delay genericizing nested functions
6543 until their parent function is genericized. Since finalizing
6544 requires GENERIC, delay that as well. */
6546 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6547 && !undef_nested_function)
6549 if (!decl_function_context (fndecl))
6551 c_genericize (fndecl);
6552 c_warn_unused_result_recursively (fndecl);
6554 /* ??? Objc emits functions after finalizing the compilation unit.
6555 This should be cleaned up later and this conditional removed. */
6556 if (cgraph_global_info_ready)
6558 c_expand_body (fndecl);
6559 return;
6562 cgraph_finalize_function (fndecl, false);
6564 else
6566 /* Register this function with cgraph just far enough to get it
6567 added to our parent's nested function list. Handy, since the
6568 C front end doesn't have such a list. */
6569 (void) cgraph_node (fndecl);
6573 if (!decl_function_context (fndecl))
6574 undef_nested_function = false;
6576 /* We're leaving the context of this function, so zap cfun.
6577 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6578 tree_rest_of_compilation. */
6579 cfun = NULL;
6580 current_function_decl = NULL;
6583 /* Generate the RTL for the body of FNDECL. */
6585 void
6586 c_expand_body (tree fndecl)
6589 if (!DECL_INITIAL (fndecl)
6590 || DECL_INITIAL (fndecl) == error_mark_node)
6591 return;
6593 tree_rest_of_compilation (fndecl);
6595 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6596 && targetm.have_ctors_dtors)
6597 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6598 DEFAULT_INIT_PRIORITY);
6599 if (DECL_STATIC_DESTRUCTOR (fndecl)
6600 && targetm.have_ctors_dtors)
6601 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6602 DEFAULT_INIT_PRIORITY);
6605 /* Check the declarations given in a for-loop for satisfying the C99
6606 constraints. */
6607 void
6608 check_for_loop_decls (void)
6610 struct c_binding *b;
6612 if (!flag_isoc99)
6614 /* If we get here, declarations have been used in a for loop without
6615 the C99 for loop scope. This doesn't make much sense, so don't
6616 allow it. */
6617 error ("%<for%> loop initial declaration used outside C99 mode");
6618 return;
6620 /* C99 subclause 6.8.5 paragraph 3:
6622 [#3] The declaration part of a for statement shall only
6623 declare identifiers for objects having storage class auto or
6624 register.
6626 It isn't clear whether, in this sentence, "identifiers" binds to
6627 "shall only declare" or to "objects" - that is, whether all identifiers
6628 declared must be identifiers for objects, or whether the restriction
6629 only applies to those that are. (A question on this in comp.std.c
6630 in November 2000 received no answer.) We implement the strictest
6631 interpretation, to avoid creating an extension which later causes
6632 problems. */
6634 for (b = current_scope->bindings; b; b = b->prev)
6636 tree id = b->id;
6637 tree decl = b->decl;
6639 if (!id)
6640 continue;
6642 switch (TREE_CODE (decl))
6644 case VAR_DECL:
6645 if (TREE_STATIC (decl))
6646 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6647 "initial declaration", decl, decl);
6648 else if (DECL_EXTERNAL (decl))
6649 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6650 "initial declaration", decl, decl);
6651 break;
6653 case RECORD_TYPE:
6654 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6655 id);
6656 break;
6657 case UNION_TYPE:
6658 error ("%<union %E%> declared in %<for%> loop initial declaration",
6659 id);
6660 break;
6661 case ENUMERAL_TYPE:
6662 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6663 id);
6664 break;
6665 default:
6666 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6667 "initial declaration", decl, decl);
6672 /* Save and reinitialize the variables
6673 used during compilation of a C function. */
6675 void
6676 c_push_function_context (struct function *f)
6678 struct language_function *p;
6679 p = GGC_NEW (struct language_function);
6680 f->language = p;
6682 p->base.x_stmt_tree = c_stmt_tree;
6683 p->x_break_label = c_break_label;
6684 p->x_cont_label = c_cont_label;
6685 p->x_switch_stack = c_switch_stack;
6686 p->arg_info = current_function_arg_info;
6687 p->returns_value = current_function_returns_value;
6688 p->returns_null = current_function_returns_null;
6689 p->returns_abnormally = current_function_returns_abnormally;
6690 p->warn_about_return_type = warn_about_return_type;
6691 p->extern_inline = current_extern_inline;
6694 /* Restore the variables used during compilation of a C function. */
6696 void
6697 c_pop_function_context (struct function *f)
6699 struct language_function *p = f->language;
6701 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6702 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6704 /* Stop pointing to the local nodes about to be freed. */
6705 /* But DECL_INITIAL must remain nonzero so we know this
6706 was an actual function definition. */
6707 DECL_INITIAL (current_function_decl) = error_mark_node;
6708 DECL_ARGUMENTS (current_function_decl) = 0;
6711 c_stmt_tree = p->base.x_stmt_tree;
6712 c_break_label = p->x_break_label;
6713 c_cont_label = p->x_cont_label;
6714 c_switch_stack = p->x_switch_stack;
6715 current_function_arg_info = p->arg_info;
6716 current_function_returns_value = p->returns_value;
6717 current_function_returns_null = p->returns_null;
6718 current_function_returns_abnormally = p->returns_abnormally;
6719 warn_about_return_type = p->warn_about_return_type;
6720 current_extern_inline = p->extern_inline;
6722 f->language = NULL;
6725 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6727 void
6728 c_dup_lang_specific_decl (tree decl)
6730 struct lang_decl *ld;
6732 if (!DECL_LANG_SPECIFIC (decl))
6733 return;
6735 ld = GGC_NEW (struct lang_decl);
6736 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6737 DECL_LANG_SPECIFIC (decl) = ld;
6740 /* The functions below are required for functionality of doing
6741 function at once processing in the C front end. Currently these
6742 functions are not called from anywhere in the C front end, but as
6743 these changes continue, that will change. */
6745 /* Returns the stmt_tree (if any) to which statements are currently
6746 being added. If there is no active statement-tree, NULL is
6747 returned. */
6749 stmt_tree
6750 current_stmt_tree (void)
6752 return &c_stmt_tree;
6755 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6756 C. */
6759 anon_aggr_type_p (tree ARG_UNUSED (node))
6761 return 0;
6764 /* Return the global value of T as a symbol. */
6766 tree
6767 identifier_global_value (tree t)
6769 struct c_binding *b;
6771 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6772 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6773 return b->decl;
6775 return 0;
6778 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6779 otherwise the name is found in ridpointers from RID_INDEX. */
6781 void
6782 record_builtin_type (enum rid rid_index, const char *name, tree type)
6784 tree id, decl;
6785 if (name == 0)
6786 id = ridpointers[(int) rid_index];
6787 else
6788 id = get_identifier (name);
6789 decl = build_decl (TYPE_DECL, id, type);
6790 pushdecl (decl);
6791 if (debug_hooks->type_decl)
6792 debug_hooks->type_decl (decl, false);
6795 /* Build the void_list_node (void_type_node having been created). */
6796 tree
6797 build_void_list_node (void)
6799 tree t = build_tree_list (NULL_TREE, void_type_node);
6800 return t;
6803 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6805 struct c_parm *
6806 build_c_parm (struct c_declspecs *specs, tree attrs,
6807 struct c_declarator *declarator)
6809 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6810 ret->specs = specs;
6811 ret->attrs = attrs;
6812 ret->declarator = declarator;
6813 return ret;
6816 /* Return a declarator with nested attributes. TARGET is the inner
6817 declarator to which these attributes apply. ATTRS are the
6818 attributes. */
6820 struct c_declarator *
6821 build_attrs_declarator (tree attrs, struct c_declarator *target)
6823 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6824 ret->kind = cdk_attrs;
6825 ret->declarator = target;
6826 ret->u.attrs = attrs;
6827 return ret;
6830 /* Return a declarator for a function with arguments specified by ARGS
6831 and return type specified by TARGET. */
6833 struct c_declarator *
6834 build_function_declarator (struct c_arg_info *args,
6835 struct c_declarator *target)
6837 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6838 ret->kind = cdk_function;
6839 ret->declarator = target;
6840 ret->u.arg_info = args;
6841 return ret;
6844 /* Return a declarator for the identifier IDENT (which may be
6845 NULL_TREE for an abstract declarator). */
6847 struct c_declarator *
6848 build_id_declarator (tree ident)
6850 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6851 ret->kind = cdk_id;
6852 ret->declarator = 0;
6853 ret->u.id = ident;
6854 /* Default value - may get reset to a more precise location. */
6855 ret->id_loc = input_location;
6856 return ret;
6859 /* Return something to represent absolute declarators containing a *.
6860 TARGET is the absolute declarator that the * contains.
6861 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6862 to apply to the pointer type. */
6864 struct c_declarator *
6865 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6866 struct c_declarator *target)
6868 tree attrs;
6869 int quals = 0;
6870 struct c_declarator *itarget = target;
6871 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6872 if (type_quals_attrs)
6874 attrs = type_quals_attrs->attrs;
6875 quals = quals_from_declspecs (type_quals_attrs);
6876 if (attrs != NULL_TREE)
6877 itarget = build_attrs_declarator (attrs, target);
6879 ret->kind = cdk_pointer;
6880 ret->declarator = itarget;
6881 ret->u.pointer_quals = quals;
6882 return ret;
6885 /* Return a pointer to a structure for an empty list of declaration
6886 specifiers. */
6888 struct c_declspecs *
6889 build_null_declspecs (void)
6891 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6892 ret->type = 0;
6893 ret->decl_attr = 0;
6894 ret->attrs = 0;
6895 ret->typespec_word = cts_none;
6896 ret->storage_class = csc_none;
6897 ret->declspecs_seen_p = false;
6898 ret->type_seen_p = false;
6899 ret->non_sc_seen_p = false;
6900 ret->typedef_p = false;
6901 ret->tag_defined_p = false;
6902 ret->explicit_signed_p = false;
6903 ret->deprecated_p = false;
6904 ret->default_int_p = false;
6905 ret->long_p = false;
6906 ret->long_long_p = false;
6907 ret->short_p = false;
6908 ret->signed_p = false;
6909 ret->unsigned_p = false;
6910 ret->complex_p = false;
6911 ret->inline_p = false;
6912 ret->thread_p = false;
6913 ret->const_p = false;
6914 ret->volatile_p = false;
6915 ret->restrict_p = false;
6916 return ret;
6919 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6920 returning SPECS. */
6922 struct c_declspecs *
6923 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6925 enum rid i;
6926 bool dupe = false;
6927 specs->non_sc_seen_p = true;
6928 specs->declspecs_seen_p = true;
6929 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6930 && C_IS_RESERVED_WORD (qual));
6931 i = C_RID_CODE (qual);
6932 switch (i)
6934 case RID_CONST:
6935 dupe = specs->const_p;
6936 specs->const_p = true;
6937 break;
6938 case RID_VOLATILE:
6939 dupe = specs->volatile_p;
6940 specs->volatile_p = true;
6941 break;
6942 case RID_RESTRICT:
6943 dupe = specs->restrict_p;
6944 specs->restrict_p = true;
6945 break;
6946 default:
6947 gcc_unreachable ();
6949 if (dupe && pedantic && !flag_isoc99)
6950 pedwarn ("duplicate %qE", qual);
6951 return specs;
6954 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6955 returning SPECS. */
6957 struct c_declspecs *
6958 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6960 tree type = spec.spec;
6961 specs->non_sc_seen_p = true;
6962 specs->declspecs_seen_p = true;
6963 specs->type_seen_p = true;
6964 if (TREE_DEPRECATED (type))
6965 specs->deprecated_p = true;
6967 /* Handle type specifier keywords. */
6968 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6970 enum rid i = C_RID_CODE (type);
6971 if (specs->type)
6973 error ("two or more data types in declaration specifiers");
6974 return specs;
6976 if ((int) i <= (int) RID_LAST_MODIFIER)
6978 /* "long", "short", "signed", "unsigned" or "_Complex". */
6979 bool dupe = false;
6980 switch (i)
6982 case RID_LONG:
6983 if (specs->long_long_p)
6985 error ("%<long long long%> is too long for GCC");
6986 break;
6988 if (specs->long_p)
6990 if (specs->typespec_word == cts_double)
6992 error ("both %<long long%> and %<double%> in "
6993 "declaration specifiers");
6994 break;
6996 if (pedantic && !flag_isoc99 && !in_system_header
6997 && warn_long_long)
6998 pedwarn ("ISO C90 does not support %<long long%>");
6999 specs->long_long_p = 1;
7000 break;
7002 if (specs->short_p)
7003 error ("both %<long%> and %<short%> in "
7004 "declaration specifiers");
7005 else if (specs->typespec_word == cts_void)
7006 error ("both %<long%> and %<void%> in "
7007 "declaration specifiers");
7008 else if (specs->typespec_word == cts_bool)
7009 error ("both %<long%> and %<_Bool%> in "
7010 "declaration specifiers");
7011 else if (specs->typespec_word == cts_char)
7012 error ("both %<long%> and %<char%> in "
7013 "declaration specifiers");
7014 else if (specs->typespec_word == cts_float)
7015 error ("both %<long%> and %<float%> in "
7016 "declaration specifiers");
7017 else
7018 specs->long_p = true;
7019 break;
7020 case RID_SHORT:
7021 dupe = specs->short_p;
7022 if (specs->long_p)
7023 error ("both %<long%> and %<short%> in "
7024 "declaration specifiers");
7025 else if (specs->typespec_word == cts_void)
7026 error ("both %<short%> and %<void%> in "
7027 "declaration specifiers");
7028 else if (specs->typespec_word == cts_bool)
7029 error ("both %<short%> and %<_Bool%> in "
7030 "declaration specifiers");
7031 else if (specs->typespec_word == cts_char)
7032 error ("both %<short%> and %<char%> in "
7033 "declaration specifiers");
7034 else if (specs->typespec_word == cts_float)
7035 error ("both %<short%> and %<float%> in "
7036 "declaration specifiers");
7037 else if (specs->typespec_word == cts_double)
7038 error ("both %<short%> and %<double%> in "
7039 "declaration specifiers");
7040 else
7041 specs->short_p = true;
7042 break;
7043 case RID_SIGNED:
7044 dupe = specs->signed_p;
7045 if (specs->unsigned_p)
7046 error ("both %<signed%> and %<unsigned%> in "
7047 "declaration specifiers");
7048 else if (specs->typespec_word == cts_void)
7049 error ("both %<signed%> and %<void%> in "
7050 "declaration specifiers");
7051 else if (specs->typespec_word == cts_bool)
7052 error ("both %<signed%> and %<_Bool%> in "
7053 "declaration specifiers");
7054 else if (specs->typespec_word == cts_float)
7055 error ("both %<signed%> and %<float%> in "
7056 "declaration specifiers");
7057 else if (specs->typespec_word == cts_double)
7058 error ("both %<signed%> and %<double%> in "
7059 "declaration specifiers");
7060 else
7061 specs->signed_p = true;
7062 break;
7063 case RID_UNSIGNED:
7064 dupe = specs->unsigned_p;
7065 if (specs->signed_p)
7066 error ("both %<signed%> and %<unsigned%> in "
7067 "declaration specifiers");
7068 else if (specs->typespec_word == cts_void)
7069 error ("both %<unsigned%> and %<void%> in "
7070 "declaration specifiers");
7071 else if (specs->typespec_word == cts_bool)
7072 error ("both %<unsigned%> and %<_Bool%> in "
7073 "declaration specifiers");
7074 else if (specs->typespec_word == cts_float)
7075 error ("both %<unsigned%> and %<float%> in "
7076 "declaration specifiers");
7077 else if (specs->typespec_word == cts_double)
7078 error ("both %<unsigned%> and %<double%> in "
7079 "declaration specifiers");
7080 else
7081 specs->unsigned_p = true;
7082 break;
7083 case RID_COMPLEX:
7084 dupe = specs->complex_p;
7085 if (pedantic && !flag_isoc99 && !in_system_header)
7086 pedwarn ("ISO C90 does not support complex types");
7087 if (specs->typespec_word == cts_void)
7088 error ("both %<complex%> and %<void%> in "
7089 "declaration specifiers");
7090 else if (specs->typespec_word == cts_bool)
7091 error ("both %<complex%> and %<_Bool%> in "
7092 "declaration specifiers");
7093 else
7094 specs->complex_p = true;
7095 break;
7096 default:
7097 gcc_unreachable ();
7100 if (dupe)
7101 error ("duplicate %qE", type);
7103 return specs;
7105 else
7107 /* "void", "_Bool", "char", "int", "float" or "double". */
7108 if (specs->typespec_word != cts_none)
7110 error ("two or more data types in declaration specifiers");
7111 return specs;
7113 switch (i)
7115 case RID_VOID:
7116 if (specs->long_p)
7117 error ("both %<long%> and %<void%> in "
7118 "declaration specifiers");
7119 else if (specs->short_p)
7120 error ("both %<short%> and %<void%> in "
7121 "declaration specifiers");
7122 else if (specs->signed_p)
7123 error ("both %<signed%> and %<void%> in "
7124 "declaration specifiers");
7125 else if (specs->unsigned_p)
7126 error ("both %<unsigned%> and %<void%> in "
7127 "declaration specifiers");
7128 else if (specs->complex_p)
7129 error ("both %<complex%> and %<void%> in "
7130 "declaration specifiers");
7131 else
7132 specs->typespec_word = cts_void;
7133 return specs;
7134 case RID_BOOL:
7135 if (specs->long_p)
7136 error ("both %<long%> and %<_Bool%> in "
7137 "declaration specifiers");
7138 else if (specs->short_p)
7139 error ("both %<short%> and %<_Bool%> in "
7140 "declaration specifiers");
7141 else if (specs->signed_p)
7142 error ("both %<signed%> and %<_Bool%> in "
7143 "declaration specifiers");
7144 else if (specs->unsigned_p)
7145 error ("both %<unsigned%> and %<_Bool%> in "
7146 "declaration specifiers");
7147 else if (specs->complex_p)
7148 error ("both %<complex%> and %<_Bool%> in "
7149 "declaration specifiers");
7150 else
7151 specs->typespec_word = cts_bool;
7152 return specs;
7153 case RID_CHAR:
7154 if (specs->long_p)
7155 error ("both %<long%> and %<char%> in "
7156 "declaration specifiers");
7157 else if (specs->short_p)
7158 error ("both %<short%> and %<char%> in "
7159 "declaration specifiers");
7160 else
7161 specs->typespec_word = cts_char;
7162 return specs;
7163 case RID_INT:
7164 specs->typespec_word = cts_int;
7165 return specs;
7166 case RID_FLOAT:
7167 if (specs->long_p)
7168 error ("both %<long%> and %<float%> in "
7169 "declaration specifiers");
7170 else if (specs->short_p)
7171 error ("both %<short%> and %<float%> in "
7172 "declaration specifiers");
7173 else if (specs->signed_p)
7174 error ("both %<signed%> and %<float%> in "
7175 "declaration specifiers");
7176 else if (specs->unsigned_p)
7177 error ("both %<unsigned%> and %<float%> in "
7178 "declaration specifiers");
7179 else
7180 specs->typespec_word = cts_float;
7181 return specs;
7182 case RID_DOUBLE:
7183 if (specs->long_long_p)
7184 error ("both %<long long%> and %<double%> in "
7185 "declaration specifiers");
7186 else if (specs->short_p)
7187 error ("both %<short%> and %<double%> in "
7188 "declaration specifiers");
7189 else if (specs->signed_p)
7190 error ("both %<signed%> and %<double%> in "
7191 "declaration specifiers");
7192 else if (specs->unsigned_p)
7193 error ("both %<unsigned%> and %<double%> in "
7194 "declaration specifiers");
7195 else
7196 specs->typespec_word = cts_double;
7197 return specs;
7198 default:
7199 /* ObjC reserved word "id", handled below. */
7200 break;
7205 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7206 form of ObjC type, cases such as "int" and "long" being handled
7207 above), a TYPE (struct, union, enum and typeof specifiers) or an
7208 ERROR_MARK. In none of these cases may there have previously
7209 been any type specifiers. */
7210 if (specs->type || specs->typespec_word != cts_none
7211 || specs->long_p || specs->short_p || specs->signed_p
7212 || specs->unsigned_p || specs->complex_p)
7213 error ("two or more data types in declaration specifiers");
7214 else if (TREE_CODE (type) == TYPE_DECL)
7216 if (TREE_TYPE (type) == error_mark_node)
7217 ; /* Allow the type to default to int to avoid cascading errors. */
7218 else
7220 specs->type = TREE_TYPE (type);
7221 specs->decl_attr = DECL_ATTRIBUTES (type);
7222 specs->typedef_p = true;
7223 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7226 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7228 tree t = lookup_name (type);
7229 if (!t || TREE_CODE (t) != TYPE_DECL)
7230 error ("%qE fails to be a typedef or built in type", type);
7231 else if (TREE_TYPE (t) == error_mark_node)
7233 else
7234 specs->type = TREE_TYPE (t);
7236 else if (TREE_CODE (type) != ERROR_MARK)
7238 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7239 specs->tag_defined_p = true;
7240 if (spec.kind == ctsk_typeof)
7241 specs->typedef_p = true;
7242 specs->type = type;
7245 return specs;
7248 /* Add the storage class specifier or function specifier SCSPEC to the
7249 declaration specifiers SPECS, returning SPECS. */
7251 struct c_declspecs *
7252 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7254 enum rid i;
7255 enum c_storage_class n = csc_none;
7256 bool dupe = false;
7257 specs->declspecs_seen_p = true;
7258 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7259 && C_IS_RESERVED_WORD (scspec));
7260 i = C_RID_CODE (scspec);
7261 if (extra_warnings && specs->non_sc_seen_p)
7262 warning (0, "%qE is not at beginning of declaration", scspec);
7263 switch (i)
7265 case RID_INLINE:
7266 /* C99 permits duplicate inline. Although of doubtful utility,
7267 it seems simplest to permit it in gnu89 mode as well, as
7268 there is also little utility in maintaining this as a
7269 difference between gnu89 and C99 inline. */
7270 dupe = false;
7271 specs->inline_p = true;
7272 break;
7273 case RID_THREAD:
7274 dupe = specs->thread_p;
7275 if (specs->storage_class == csc_auto)
7276 error ("%<__thread%> used with %<auto%>");
7277 else if (specs->storage_class == csc_register)
7278 error ("%<__thread%> used with %<register%>");
7279 else if (specs->storage_class == csc_typedef)
7280 error ("%<__thread%> used with %<typedef%>");
7281 else
7282 specs->thread_p = true;
7283 break;
7284 case RID_AUTO:
7285 n = csc_auto;
7286 break;
7287 case RID_EXTERN:
7288 n = csc_extern;
7289 /* Diagnose "__thread extern". */
7290 if (specs->thread_p)
7291 error ("%<__thread%> before %<extern%>");
7292 break;
7293 case RID_REGISTER:
7294 n = csc_register;
7295 break;
7296 case RID_STATIC:
7297 n = csc_static;
7298 /* Diagnose "__thread static". */
7299 if (specs->thread_p)
7300 error ("%<__thread%> before %<static%>");
7301 break;
7302 case RID_TYPEDEF:
7303 n = csc_typedef;
7304 break;
7305 default:
7306 gcc_unreachable ();
7308 if (n != csc_none && n == specs->storage_class)
7309 dupe = true;
7310 if (dupe)
7311 error ("duplicate %qE", scspec);
7312 if (n != csc_none)
7314 if (specs->storage_class != csc_none && n != specs->storage_class)
7316 error ("multiple storage classes in declaration specifiers");
7318 else
7320 specs->storage_class = n;
7321 if (n != csc_extern && n != csc_static && specs->thread_p)
7323 error ("%<__thread%> used with %qE", scspec);
7324 specs->thread_p = false;
7328 return specs;
7331 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7332 returning SPECS. */
7334 struct c_declspecs *
7335 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7337 specs->attrs = chainon (attrs, specs->attrs);
7338 specs->declspecs_seen_p = true;
7339 return specs;
7342 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7343 specifiers with any other type specifier to determine the resulting
7344 type. This is where ISO C checks on complex types are made, since
7345 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7346 double". */
7348 struct c_declspecs *
7349 finish_declspecs (struct c_declspecs *specs)
7351 /* If a type was specified as a whole, we have no modifiers and are
7352 done. */
7353 if (specs->type != NULL_TREE)
7355 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7356 && !specs->signed_p && !specs->unsigned_p
7357 && !specs->complex_p);
7358 return specs;
7361 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7362 has been specified, treat it as "int" unless "_Complex" is
7363 present and there are no other specifiers. If we just have
7364 "_Complex", it is equivalent to "_Complex double", but e.g.
7365 "_Complex short" is equivalent to "_Complex short int". */
7366 if (specs->typespec_word == cts_none)
7368 if (specs->long_p || specs->short_p
7369 || specs->signed_p || specs->unsigned_p)
7371 specs->typespec_word = cts_int;
7373 else if (specs->complex_p)
7375 specs->typespec_word = cts_double;
7376 if (pedantic)
7377 pedwarn ("ISO C does not support plain %<complex%> meaning "
7378 "%<double complex%>");
7380 else
7382 specs->typespec_word = cts_int;
7383 specs->default_int_p = true;
7384 /* We don't diagnose this here because grokdeclarator will
7385 give more specific diagnostics according to whether it is
7386 a function definition. */
7390 /* If "signed" was specified, record this to distinguish "int" and
7391 "signed int" in the case of a bit-field with
7392 -funsigned-bitfields. */
7393 specs->explicit_signed_p = specs->signed_p;
7395 /* Now compute the actual type. */
7396 switch (specs->typespec_word)
7398 case cts_void:
7399 gcc_assert (!specs->long_p && !specs->short_p
7400 && !specs->signed_p && !specs->unsigned_p
7401 && !specs->complex_p);
7402 specs->type = void_type_node;
7403 break;
7404 case cts_bool:
7405 gcc_assert (!specs->long_p && !specs->short_p
7406 && !specs->signed_p && !specs->unsigned_p
7407 && !specs->complex_p);
7408 specs->type = boolean_type_node;
7409 break;
7410 case cts_char:
7411 gcc_assert (!specs->long_p && !specs->short_p);
7412 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7413 if (specs->signed_p)
7414 specs->type = signed_char_type_node;
7415 else if (specs->unsigned_p)
7416 specs->type = unsigned_char_type_node;
7417 else
7418 specs->type = char_type_node;
7419 if (specs->complex_p)
7421 if (pedantic)
7422 pedwarn ("ISO C does not support complex integer types");
7423 specs->type = build_complex_type (specs->type);
7425 break;
7426 case cts_int:
7427 gcc_assert (!(specs->long_p && specs->short_p));
7428 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7429 if (specs->long_long_p)
7430 specs->type = (specs->unsigned_p
7431 ? long_long_unsigned_type_node
7432 : long_long_integer_type_node);
7433 else if (specs->long_p)
7434 specs->type = (specs->unsigned_p
7435 ? long_unsigned_type_node
7436 : long_integer_type_node);
7437 else if (specs->short_p)
7438 specs->type = (specs->unsigned_p
7439 ? short_unsigned_type_node
7440 : short_integer_type_node);
7441 else
7442 specs->type = (specs->unsigned_p
7443 ? unsigned_type_node
7444 : integer_type_node);
7445 if (specs->complex_p)
7447 if (pedantic)
7448 pedwarn ("ISO C does not support complex integer types");
7449 specs->type = build_complex_type (specs->type);
7451 break;
7452 case cts_float:
7453 gcc_assert (!specs->long_p && !specs->short_p
7454 && !specs->signed_p && !specs->unsigned_p);
7455 specs->type = (specs->complex_p
7456 ? complex_float_type_node
7457 : float_type_node);
7458 break;
7459 case cts_double:
7460 gcc_assert (!specs->long_long_p && !specs->short_p
7461 && !specs->signed_p && !specs->unsigned_p);
7462 if (specs->long_p)
7464 specs->type = (specs->complex_p
7465 ? complex_long_double_type_node
7466 : long_double_type_node);
7468 else
7470 specs->type = (specs->complex_p
7471 ? complex_double_type_node
7472 : double_type_node);
7474 break;
7475 default:
7476 gcc_unreachable ();
7479 return specs;
7482 /* Synthesize a function which calls all the global ctors or global
7483 dtors in this file. This is only used for targets which do not
7484 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7485 static void
7486 build_cdtor (int method_type, tree cdtors)
7488 tree body = 0;
7490 if (!cdtors)
7491 return;
7493 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7494 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7495 &body);
7497 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7500 /* Perform final processing on one file scope's declarations (or the
7501 external scope's declarations), GLOBALS. */
7502 static void
7503 c_write_global_declarations_1 (tree globals)
7505 size_t len = list_length (globals);
7506 tree *vec = XNEWVEC (tree, len);
7507 size_t i;
7508 tree decl;
7510 /* Process the decls in the order they were written. */
7511 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7513 vec[i] = decl;
7514 /* Check for used but undefined static functions using the C
7515 standard's definition of "used", and set TREE_NO_WARNING so
7516 that check_global_declarations doesn't repeat the check. */
7517 if (TREE_CODE (decl) == FUNCTION_DECL
7518 && DECL_INITIAL (decl) == 0
7519 && DECL_EXTERNAL (decl)
7520 && !TREE_PUBLIC (decl)
7521 && C_DECL_USED (decl))
7523 pedwarn ("%J%qF used but never defined", decl, decl);
7524 TREE_NO_WARNING (decl) = 1;
7528 wrapup_global_declarations (vec, len);
7529 check_global_declarations (vec, len);
7531 free (vec);
7534 void
7535 c_write_global_declarations (void)
7537 tree ext_block, t;
7539 /* We don't want to do this if generating a PCH. */
7540 if (pch_file)
7541 return;
7543 /* Don't waste time on further processing if -fsyntax-only or we've
7544 encountered errors. */
7545 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7546 return;
7548 /* Close the external scope. */
7549 ext_block = pop_scope ();
7550 external_scope = 0;
7551 gcc_assert (!current_scope);
7553 /* Process all file scopes in this compilation, and the external_scope,
7554 through wrapup_global_declarations and check_global_declarations. */
7555 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7556 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7557 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7559 /* Generate functions to call static constructors and destructors
7560 for targets that do not support .ctors/.dtors sections. These
7561 functions have magic names which are detected by collect2. */
7562 build_cdtor ('I', static_ctors); static_ctors = 0;
7563 build_cdtor ('D', static_dtors); static_dtors = 0;
7565 /* We're done parsing; proceed to optimize and emit assembly.
7566 FIXME: shouldn't be the front end's responsibility to call this. */
7567 cgraph_optimize ();
7570 #include "gt-c-decl.h"