* defaults.h (FRAME_GROWS_DOWNWARD): Define to 0 if not defined.
[official-gcc.git] / gcc / c-decl.c
blob9ed4ade09b5cbfdf620c3c94d5d53adfaa3bd39c
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "input.h"
33 #include "tm.h"
34 #include "intl.h"
35 #include "tree.h"
36 #include "tree-inline.h"
37 #include "rtl.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "output.h"
41 #include "expr.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tm_p.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "debug.h"
49 #include "opts.h"
50 #include "timevar.h"
51 #include "c-common.h"
52 #include "c-pragma.h"
53 #include "langhooks.h"
54 #include "tree-mudflap.h"
55 #include "tree-gimple.h"
56 #include "diagnostic.h"
57 #include "tree-dump.h"
58 #include "cgraph.h"
59 #include "hashtab.h"
60 #include "libfuncs.h"
61 #include "except.h"
62 #include "langhooks-def.h"
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 *, ...) ATTRIBUTE_GCC_CDIAG(1,2))
1131 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1133 else if (DECL_INITIAL (decl))
1134 diag (G_("%Jprevious definition of %qD was here"), decl, decl);
1135 else if (C_DECL_IMPLICIT (decl))
1136 diag (G_("%Jprevious implicit declaration of %qD was here"), decl, decl);
1137 else
1138 diag (G_("%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 *, ...) ATTRIBUTE_GCC_CDIAG(1,2);
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 (G_("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_CODE (itype) == INTEGER_CST
4196 && TREE_OVERFLOW (itype))
4198 error ("size of array %qs is too large", name);
4199 type = error_mark_node;
4200 continue;
4203 itype = build_index_type (itype);
4206 else if (decl_context == FIELD)
4208 if (pedantic && !flag_isoc99 && !in_system_header)
4209 pedwarn ("ISO C90 does not support flexible array members");
4211 /* ISO C99 Flexible array members are effectively
4212 identical to GCC's zero-length array extension. */
4213 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4216 /* Complain about arrays of incomplete types. */
4217 if (!COMPLETE_TYPE_P (type))
4219 error ("array type has incomplete element type");
4220 type = error_mark_node;
4222 else
4223 type = build_array_type (type, itype);
4225 if (size_varies)
4226 C_TYPE_VARIABLE_SIZE (type) = 1;
4228 /* The GCC extension for zero-length arrays differs from
4229 ISO flexible array members in that sizeof yields
4230 zero. */
4231 if (size && integer_zerop (size))
4233 TYPE_SIZE (type) = bitsize_zero_node;
4234 TYPE_SIZE_UNIT (type) = size_zero_node;
4237 if (decl_context != PARM
4238 && (array_ptr_quals != TYPE_UNQUALIFIED
4239 || array_ptr_attrs != NULL_TREE
4240 || array_parm_static))
4242 error ("static or type qualifiers in non-parameter array declarator");
4243 array_ptr_quals = TYPE_UNQUALIFIED;
4244 array_ptr_attrs = NULL_TREE;
4245 array_parm_static = 0;
4247 break;
4249 case cdk_function:
4251 /* Say it's a definition only for the declarator closest
4252 to the identifier, apart possibly from some
4253 attributes. */
4254 bool really_funcdef = false;
4255 tree arg_types;
4256 if (funcdef_flag)
4258 const struct c_declarator *t = declarator->declarator;
4259 while (t->kind == cdk_attrs)
4260 t = t->declarator;
4261 really_funcdef = (t->kind == cdk_id);
4264 /* Declaring a function type. Make sure we have a valid
4265 type for the function to return. */
4266 if (type == error_mark_node)
4267 continue;
4269 size_varies = 0;
4271 /* Warn about some types functions can't return. */
4272 if (TREE_CODE (type) == FUNCTION_TYPE)
4274 error ("%qs declared as function returning a function", name);
4275 type = integer_type_node;
4277 if (TREE_CODE (type) == ARRAY_TYPE)
4279 error ("%qs declared as function returning an array", name);
4280 type = integer_type_node;
4283 /* Construct the function type and go to the next
4284 inner layer of declarator. */
4285 arg_info = declarator->u.arg_info;
4286 arg_types = grokparms (arg_info, really_funcdef);
4288 /* Type qualifiers before the return type of the function
4289 qualify the return type, not the function type. */
4290 if (type_quals)
4292 /* Type qualifiers on a function return type are
4293 normally permitted by the standard but have no
4294 effect, so give a warning at -Wreturn-type.
4295 Qualifiers on a void return type are banned on
4296 function definitions in ISO C; GCC used to used
4297 them for noreturn functions. */
4298 if (VOID_TYPE_P (type) && really_funcdef)
4299 pedwarn ("function definition has qualified void return type");
4300 else
4301 warning (OPT_Wreturn_type,
4302 "type qualifiers ignored on function return type");
4304 type = c_build_qualified_type (type, type_quals);
4306 type_quals = TYPE_UNQUALIFIED;
4308 type = build_function_type (type, arg_types);
4309 declarator = declarator->declarator;
4311 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4312 the formal parameter list of this FUNCTION_TYPE to point to
4313 the FUNCTION_TYPE node itself. */
4315 tree link;
4317 for (link = arg_info->tags;
4318 link;
4319 link = TREE_CHAIN (link))
4320 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4322 break;
4324 case cdk_pointer:
4326 /* Merge any constancy or volatility into the target type
4327 for the pointer. */
4329 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4330 && type_quals)
4331 pedwarn ("ISO C forbids qualified function types");
4332 if (type_quals)
4333 type = c_build_qualified_type (type, type_quals);
4334 size_varies = 0;
4336 type = build_pointer_type (type);
4338 /* Process type qualifiers (such as const or volatile)
4339 that were given inside the `*'. */
4340 type_quals = declarator->u.pointer_quals;
4342 declarator = declarator->declarator;
4343 break;
4345 default:
4346 gcc_unreachable ();
4350 /* Now TYPE has the actual type, apart from any qualifiers in
4351 TYPE_QUALS. */
4353 /* Check the type and width of a bit-field. */
4354 if (bitfield)
4355 check_bitfield_type_and_width (&type, width, orig_name);
4357 /* Did array size calculations overflow? */
4359 if (TREE_CODE (type) == ARRAY_TYPE
4360 && COMPLETE_TYPE_P (type)
4361 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4362 && TREE_OVERFLOW (TYPE_SIZE (type)))
4364 error ("size of array %qs is too large", name);
4365 /* If we proceed with the array type as it is, we'll eventually
4366 crash in tree_low_cst(). */
4367 type = error_mark_node;
4370 /* If this is declaring a typedef name, return a TYPE_DECL. */
4372 if (storage_class == csc_typedef)
4374 tree decl;
4375 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4376 && type_quals)
4377 pedwarn ("ISO C forbids qualified function types");
4378 if (type_quals)
4379 type = c_build_qualified_type (type, type_quals);
4380 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4381 if (declspecs->explicit_signed_p)
4382 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4383 decl_attributes (&decl, returned_attrs, 0);
4384 if (declspecs->inline_p)
4385 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4386 return decl;
4389 /* Detect the case of an array type of unspecified size
4390 which came, as such, direct from a typedef name.
4391 We must copy the type, so that each identifier gets
4392 a distinct type, so that each identifier's size can be
4393 controlled separately by its own initializer. */
4395 if (type != 0 && typedef_type != 0
4396 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4397 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4399 type = build_array_type (TREE_TYPE (type), 0);
4400 if (size_varies)
4401 C_TYPE_VARIABLE_SIZE (type) = 1;
4404 /* If this is a type name (such as, in a cast or sizeof),
4405 compute the type and return it now. */
4407 if (decl_context == TYPENAME)
4409 /* Note that the grammar rejects storage classes in typenames
4410 and fields. */
4411 gcc_assert (storage_class == csc_none && !threadp
4412 && !declspecs->inline_p);
4413 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4414 && type_quals)
4415 pedwarn ("ISO C forbids const or volatile function types");
4416 if (type_quals)
4417 type = c_build_qualified_type (type, type_quals);
4418 decl_attributes (&type, returned_attrs, 0);
4419 return type;
4422 /* Aside from typedefs and type names (handle above),
4423 `void' at top level (not within pointer)
4424 is allowed only in public variables.
4425 We don't complain about parms either, but that is because
4426 a better error message can be made later. */
4428 if (VOID_TYPE_P (type) && decl_context != PARM
4429 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4430 && (storage_class == csc_extern
4431 || (current_scope == file_scope
4432 && !(storage_class == csc_static
4433 || storage_class == csc_register)))))
4435 error ("variable or field %qs declared void", name);
4436 type = integer_type_node;
4439 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4440 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4443 tree decl;
4445 if (decl_context == PARM)
4447 tree type_as_written;
4448 tree promoted_type;
4450 /* A parameter declared as an array of T is really a pointer to T.
4451 One declared as a function is really a pointer to a function. */
4453 if (TREE_CODE (type) == ARRAY_TYPE)
4455 /* Transfer const-ness of array into that of type pointed to. */
4456 type = TREE_TYPE (type);
4457 if (type_quals)
4458 type = c_build_qualified_type (type, type_quals);
4459 type = build_pointer_type (type);
4460 type_quals = array_ptr_quals;
4462 /* We don't yet implement attributes in this context. */
4463 if (array_ptr_attrs != NULL_TREE)
4464 warning (OPT_Wattributes,
4465 "attributes in parameter array declarator ignored");
4467 size_varies = 0;
4469 else if (TREE_CODE (type) == FUNCTION_TYPE)
4471 if (pedantic && type_quals)
4472 pedwarn ("ISO C forbids qualified function types");
4473 if (type_quals)
4474 type = c_build_qualified_type (type, type_quals);
4475 type = build_pointer_type (type);
4476 type_quals = TYPE_UNQUALIFIED;
4478 else if (type_quals)
4479 type = c_build_qualified_type (type, type_quals);
4481 type_as_written = type;
4483 decl = build_decl (PARM_DECL, declarator->u.id, type);
4484 if (size_varies)
4485 C_DECL_VARIABLE_SIZE (decl) = 1;
4487 /* Compute the type actually passed in the parmlist,
4488 for the case where there is no prototype.
4489 (For example, shorts and chars are passed as ints.)
4490 When there is a prototype, this is overridden later. */
4492 if (type == error_mark_node)
4493 promoted_type = type;
4494 else
4495 promoted_type = c_type_promotes_to (type);
4497 DECL_ARG_TYPE (decl) = promoted_type;
4498 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4499 if (declspecs->inline_p)
4500 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4502 else if (decl_context == FIELD)
4504 /* Note that the grammar rejects storage classes in typenames
4505 and fields. */
4506 gcc_assert (storage_class == csc_none && !threadp
4507 && !declspecs->inline_p);
4509 /* Structure field. It may not be a function. */
4511 if (TREE_CODE (type) == FUNCTION_TYPE)
4513 error ("field %qs declared as a function", name);
4514 type = build_pointer_type (type);
4516 else if (TREE_CODE (type) != ERROR_MARK
4517 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4519 error ("field %qs has incomplete type", name);
4520 type = error_mark_node;
4522 type = c_build_qualified_type (type, type_quals);
4523 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4524 DECL_NONADDRESSABLE_P (decl) = bitfield;
4526 if (size_varies)
4527 C_DECL_VARIABLE_SIZE (decl) = 1;
4529 else if (TREE_CODE (type) == FUNCTION_TYPE)
4531 if (storage_class == csc_register || threadp)
4533 error ("invalid storage class for function %qs", name);
4535 else if (current_scope != file_scope)
4537 /* Function declaration not at file scope. Storage
4538 classes other than `extern' are not allowed, C99
4539 6.7.1p5, and `extern' makes no difference. However,
4540 GCC allows 'auto', perhaps with 'inline', to support
4541 nested functions. */
4542 if (storage_class == csc_auto)
4544 if (pedantic)
4545 pedwarn ("invalid storage class for function %qs", name);
4547 else if (storage_class == csc_static)
4549 error ("invalid storage class for function %qs", name);
4550 if (funcdef_flag)
4551 storage_class = declspecs->storage_class = csc_none;
4552 else
4553 return 0;
4557 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4558 decl = build_decl_attribute_variant (decl, decl_attr);
4560 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4562 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4563 pedwarn ("ISO C forbids qualified function types");
4565 /* GNU C interprets a volatile-qualified function type to indicate
4566 that the function does not return. */
4567 if ((type_quals & TYPE_QUAL_VOLATILE)
4568 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4569 warning (0, "%<noreturn%> function returns non-void value");
4571 /* Every function declaration is an external reference
4572 (DECL_EXTERNAL) except for those which are not at file
4573 scope and are explicitly declared "auto". This is
4574 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4575 GCC to signify a forward declaration of a nested function. */
4576 if (storage_class == csc_auto && current_scope != file_scope)
4577 DECL_EXTERNAL (decl) = 0;
4578 else
4579 DECL_EXTERNAL (decl) = 1;
4581 /* Record absence of global scope for `static' or `auto'. */
4582 TREE_PUBLIC (decl)
4583 = !(storage_class == csc_static || storage_class == csc_auto);
4585 /* For a function definition, record the argument information
4586 block where store_parm_decls will look for it. */
4587 if (funcdef_flag)
4588 current_function_arg_info = arg_info;
4590 if (declspecs->default_int_p)
4591 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4593 /* Record presence of `inline', if it is reasonable. */
4594 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4596 if (declspecs->inline_p)
4597 pedwarn ("cannot inline function %<main%>");
4599 else if (declspecs->inline_p)
4601 /* Record that the function is declared `inline'. */
4602 DECL_DECLARED_INLINE_P (decl) = 1;
4604 /* Do not mark bare declarations as DECL_INLINE. Doing so
4605 in the presence of multiple declarations can result in
4606 the abstract origin pointing between the declarations,
4607 which will confuse dwarf2out. */
4608 if (initialized)
4610 DECL_INLINE (decl) = 1;
4611 if (storage_class == csc_extern)
4612 current_extern_inline = 1;
4615 /* If -finline-functions, assume it can be inlined. This does
4616 two things: let the function be deferred until it is actually
4617 needed, and let dwarf2 know that the function is inlinable. */
4618 else if (flag_inline_trees == 2 && initialized)
4619 DECL_INLINE (decl) = 1;
4621 else
4623 /* It's a variable. */
4624 /* An uninitialized decl with `extern' is a reference. */
4625 int extern_ref = !initialized && storage_class == csc_extern;
4627 type = c_build_qualified_type (type, type_quals);
4629 /* C99 6.2.2p7: It is invalid (compile-time undefined
4630 behavior) to create an 'extern' declaration for a
4631 variable if there is a global declaration that is
4632 'static' and the global declaration is not visible.
4633 (If the static declaration _is_ currently visible,
4634 the 'extern' declaration is taken to refer to that decl.) */
4635 if (extern_ref && current_scope != file_scope)
4637 tree global_decl = identifier_global_value (declarator->u.id);
4638 tree visible_decl = lookup_name (declarator->u.id);
4640 if (global_decl
4641 && global_decl != visible_decl
4642 && TREE_CODE (global_decl) == VAR_DECL
4643 && !TREE_PUBLIC (global_decl))
4644 error ("variable previously declared %<static%> redeclared "
4645 "%<extern%>");
4648 decl = build_decl (VAR_DECL, declarator->u.id, type);
4649 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4650 if (size_varies)
4651 C_DECL_VARIABLE_SIZE (decl) = 1;
4653 if (declspecs->inline_p)
4654 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4656 /* At file scope, an initialized extern declaration may follow
4657 a static declaration. In that case, DECL_EXTERNAL will be
4658 reset later in start_decl. */
4659 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4661 /* At file scope, the presence of a `static' or `register' storage
4662 class specifier, or the absence of all storage class specifiers
4663 makes this declaration a definition (perhaps tentative). Also,
4664 the absence of both `static' and `register' makes it public. */
4665 if (current_scope == file_scope)
4667 TREE_PUBLIC (decl) = !(storage_class == csc_static
4668 || storage_class == csc_register);
4669 TREE_STATIC (decl) = !extern_ref;
4671 /* Not at file scope, only `static' makes a static definition. */
4672 else
4674 TREE_STATIC (decl) = (storage_class == csc_static);
4675 TREE_PUBLIC (decl) = extern_ref;
4678 if (threadp)
4680 if (targetm.have_tls)
4681 DECL_THREAD_LOCAL (decl) = 1;
4682 else
4683 /* A mere warning is sure to result in improper semantics
4684 at runtime. Don't bother to allow this to compile. */
4685 error ("thread-local storage not supported for this target");
4689 /* Record `register' declaration for warnings on &
4690 and in case doing stupid register allocation. */
4692 if (storage_class == csc_register)
4694 C_DECL_REGISTER (decl) = 1;
4695 DECL_REGISTER (decl) = 1;
4698 /* Record constancy and volatility. */
4699 c_apply_type_quals_to_decl (type_quals, decl);
4701 /* If a type has volatile components, it should be stored in memory.
4702 Otherwise, the fact that those components are volatile
4703 will be ignored, and would even crash the compiler. */
4704 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4706 /* It is not an error for a structure with volatile fields to
4707 be declared register, but reset DECL_REGISTER since it
4708 cannot actually go in a register. */
4709 int was_reg = C_DECL_REGISTER (decl);
4710 C_DECL_REGISTER (decl) = 0;
4711 DECL_REGISTER (decl) = 0;
4712 c_mark_addressable (decl);
4713 C_DECL_REGISTER (decl) = was_reg;
4716 /* This is the earliest point at which we might know the assembler
4717 name of a variable. Thus, if it's known before this, die horribly. */
4718 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4720 decl_attributes (&decl, returned_attrs, 0);
4722 return decl;
4726 /* Decode the parameter-list info for a function type or function definition.
4727 The argument is the value returned by `get_parm_info' (or made in parse.y
4728 if there is an identifier list instead of a parameter decl list).
4729 These two functions are separate because when a function returns
4730 or receives functions then each is called multiple times but the order
4731 of calls is different. The last call to `grokparms' is always the one
4732 that contains the formal parameter names of a function definition.
4734 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4736 FUNCDEF_FLAG is true for a function definition, false for
4737 a mere declaration. A nonempty identifier-list gets an error message
4738 when FUNCDEF_FLAG is false. */
4740 static tree
4741 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4743 tree arg_types = arg_info->types;
4745 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4746 && !in_system_header)
4747 warning (0, "function declaration isn%'t a prototype");
4749 if (arg_types == error_mark_node)
4750 return 0; /* don't set TYPE_ARG_TYPES in this case */
4752 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4754 if (!funcdef_flag)
4755 pedwarn ("parameter names (without types) in function declaration");
4757 arg_info->parms = arg_info->types;
4758 arg_info->types = 0;
4759 return 0;
4761 else
4763 tree parm, type, typelt;
4764 unsigned int parmno;
4766 /* If there is a parameter of incomplete type in a definition,
4767 this is an error. In a declaration this is valid, and a
4768 struct or union type may be completed later, before any calls
4769 or definition of the function. In the case where the tag was
4770 first declared within the parameter list, a warning has
4771 already been given. If a parameter has void type, then
4772 however the function cannot be defined or called, so
4773 warn. */
4775 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4776 parm;
4777 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4779 type = TREE_VALUE (typelt);
4780 if (type == error_mark_node)
4781 continue;
4783 if (!COMPLETE_TYPE_P (type))
4785 if (funcdef_flag)
4787 if (DECL_NAME (parm))
4788 error ("%Jparameter %u (%qD) has incomplete type",
4789 parm, parmno, parm);
4790 else
4791 error ("%Jparameter %u has incomplete type",
4792 parm, parmno);
4794 TREE_VALUE (typelt) = error_mark_node;
4795 TREE_TYPE (parm) = error_mark_node;
4797 else if (VOID_TYPE_P (type))
4799 if (DECL_NAME (parm))
4800 warning (0, "%Jparameter %u (%qD) has void type",
4801 parm, parmno, parm);
4802 else
4803 warning (0, "%Jparameter %u has void type",
4804 parm, parmno);
4808 return arg_types;
4812 /* Take apart the current scope and return a c_arg_info structure with
4813 info on a parameter list just parsed.
4815 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4817 ELLIPSIS being true means the argument list ended in '...' so don't
4818 append a sentinel (void_list_node) to the end of the type-list. */
4820 struct c_arg_info *
4821 get_parm_info (bool ellipsis)
4823 struct c_binding *b = current_scope->bindings;
4824 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4825 struct c_arg_info);
4826 tree parms = 0;
4827 tree tags = 0;
4828 tree types = 0;
4829 tree others = 0;
4831 static bool explained_incomplete_types = false;
4832 bool gave_void_only_once_err = false;
4834 arg_info->parms = 0;
4835 arg_info->tags = 0;
4836 arg_info->types = 0;
4837 arg_info->others = 0;
4839 /* The bindings in this scope must not get put into a block.
4840 We will take care of deleting the binding nodes. */
4841 current_scope->bindings = 0;
4843 /* This function is only called if there was *something* on the
4844 parameter list. */
4845 gcc_assert (b);
4847 /* A parameter list consisting solely of 'void' indicates that the
4848 function takes no arguments. But if the 'void' is qualified
4849 (by 'const' or 'volatile'), or has a storage class specifier
4850 ('register'), then the behavior is undefined; issue an error.
4851 Typedefs for 'void' are OK (see DR#157). */
4852 if (b->prev == 0 /* one binding */
4853 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4854 && !DECL_NAME (b->decl) /* anonymous */
4855 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4857 if (TREE_THIS_VOLATILE (b->decl)
4858 || TREE_READONLY (b->decl)
4859 || C_DECL_REGISTER (b->decl))
4860 error ("%<void%> as only parameter may not be qualified");
4862 /* There cannot be an ellipsis. */
4863 if (ellipsis)
4864 error ("%<void%> must be the only parameter");
4866 arg_info->types = void_list_node;
4867 return arg_info;
4870 if (!ellipsis)
4871 types = void_list_node;
4873 /* Break up the bindings list into parms, tags, types, and others;
4874 apply sanity checks; purge the name-to-decl bindings. */
4875 while (b)
4877 tree decl = b->decl;
4878 tree type = TREE_TYPE (decl);
4879 const char *keyword;
4881 switch (TREE_CODE (decl))
4883 case PARM_DECL:
4884 if (b->id)
4886 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4887 I_SYMBOL_BINDING (b->id) = b->shadowed;
4890 /* Check for forward decls that never got their actual decl. */
4891 if (TREE_ASM_WRITTEN (decl))
4892 error ("%Jparameter %qD has just a forward declaration",
4893 decl, decl);
4894 /* Check for (..., void, ...) and issue an error. */
4895 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4897 if (!gave_void_only_once_err)
4899 error ("%<void%> must be the only parameter");
4900 gave_void_only_once_err = true;
4903 else
4905 /* Valid parameter, add it to the list. */
4906 TREE_CHAIN (decl) = parms;
4907 parms = decl;
4909 /* Since there is a prototype, args are passed in their
4910 declared types. The back end may override this later. */
4911 DECL_ARG_TYPE (decl) = type;
4912 types = tree_cons (0, type, types);
4914 break;
4916 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4917 case UNION_TYPE: keyword = "union"; goto tag;
4918 case RECORD_TYPE: keyword = "struct"; goto tag;
4919 tag:
4920 /* Types may not have tag-names, in which case the type
4921 appears in the bindings list with b->id NULL. */
4922 if (b->id)
4924 gcc_assert (I_TAG_BINDING (b->id) == b);
4925 I_TAG_BINDING (b->id) = b->shadowed;
4928 /* Warn about any struct, union or enum tags defined in a
4929 parameter list. The scope of such types is limited to
4930 the parameter list, which is rarely if ever desirable
4931 (it's impossible to call such a function with type-
4932 correct arguments). An anonymous union parm type is
4933 meaningful as a GNU extension, so don't warn for that. */
4934 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4936 if (b->id)
4937 /* The %s will be one of 'struct', 'union', or 'enum'. */
4938 warning (0, "%<%s %E%> declared inside parameter list",
4939 keyword, b->id);
4940 else
4941 /* The %s will be one of 'struct', 'union', or 'enum'. */
4942 warning (0, "anonymous %s declared inside parameter list",
4943 keyword);
4945 if (!explained_incomplete_types)
4947 warning (0, "its scope is only this definition or declaration,"
4948 " which is probably not what you want");
4949 explained_incomplete_types = true;
4953 tags = tree_cons (b->id, decl, tags);
4954 break;
4956 case CONST_DECL:
4957 case TYPE_DECL:
4958 case FUNCTION_DECL:
4959 /* CONST_DECLs appear here when we have an embedded enum,
4960 and TYPE_DECLs appear here when we have an embedded struct
4961 or union. No warnings for this - we already warned about the
4962 type itself. FUNCTION_DECLs appear when there is an implicit
4963 function declaration in the parameter list. */
4965 TREE_CHAIN (decl) = others;
4966 others = decl;
4967 /* fall through */
4969 case ERROR_MARK:
4970 /* error_mark_node appears here when we have an undeclared
4971 variable. Just throw it away. */
4972 if (b->id)
4974 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4975 I_SYMBOL_BINDING (b->id) = b->shadowed;
4977 break;
4979 /* Other things that might be encountered. */
4980 case LABEL_DECL:
4981 case VAR_DECL:
4982 default:
4983 gcc_unreachable ();
4986 b = free_binding_and_advance (b);
4989 arg_info->parms = parms;
4990 arg_info->tags = tags;
4991 arg_info->types = types;
4992 arg_info->others = others;
4993 return arg_info;
4996 /* Get the struct, enum or union (CODE says which) with tag NAME.
4997 Define the tag as a forward-reference if it is not defined.
4998 Return a c_typespec structure for the type specifier. */
5000 struct c_typespec
5001 parser_xref_tag (enum tree_code code, tree name)
5003 struct c_typespec ret;
5004 /* If a cross reference is requested, look up the type
5005 already defined for this tag and return it. */
5007 tree ref = lookup_tag (code, name, 0);
5008 /* If this is the right type of tag, return what we found.
5009 (This reference will be shadowed by shadow_tag later if appropriate.)
5010 If this is the wrong type of tag, do not return it. If it was the
5011 wrong type in the same scope, we will have had an error
5012 message already; if in a different scope and declaring
5013 a name, pending_xref_error will give an error message; but if in a
5014 different scope and not declaring a name, this tag should
5015 shadow the previous declaration of a different type of tag, and
5016 this would not work properly if we return the reference found.
5017 (For example, with "struct foo" in an outer scope, "union foo;"
5018 must shadow that tag with a new one of union type.) */
5019 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5020 if (ref && TREE_CODE (ref) == code)
5022 ret.spec = ref;
5023 return ret;
5026 /* If no such tag is yet defined, create a forward-reference node
5027 and record it as the "definition".
5028 When a real declaration of this type is found,
5029 the forward-reference will be altered into a real type. */
5031 ref = make_node (code);
5032 if (code == ENUMERAL_TYPE)
5034 /* Give the type a default layout like unsigned int
5035 to avoid crashing if it does not get defined. */
5036 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5037 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5038 TYPE_USER_ALIGN (ref) = 0;
5039 TYPE_UNSIGNED (ref) = 1;
5040 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5041 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5042 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5045 pushtag (name, ref);
5047 ret.spec = ref;
5048 return ret;
5051 /* Get the struct, enum or union (CODE says which) with tag NAME.
5052 Define the tag as a forward-reference if it is not defined.
5053 Return a tree for the type. */
5055 tree
5056 xref_tag (enum tree_code code, tree name)
5058 return parser_xref_tag (code, name).spec;
5061 /* Make sure that the tag NAME is defined *in the current scope*
5062 at least as a forward reference.
5063 CODE says which kind of tag NAME ought to be. */
5065 tree
5066 start_struct (enum tree_code code, tree name)
5068 /* If there is already a tag defined at this scope
5069 (as a forward reference), just return it. */
5071 tree ref = 0;
5073 if (name != 0)
5074 ref = lookup_tag (code, name, 1);
5075 if (ref && TREE_CODE (ref) == code)
5077 if (TYPE_SIZE (ref))
5079 if (code == UNION_TYPE)
5080 error ("redefinition of %<union %E%>", name);
5081 else
5082 error ("redefinition of %<struct %E%>", name);
5084 else if (C_TYPE_BEING_DEFINED (ref))
5086 if (code == UNION_TYPE)
5087 error ("nested redefinition of %<union %E%>", name);
5088 else
5089 error ("nested redefinition of %<struct %E%>", name);
5092 else
5094 /* Otherwise create a forward-reference just so the tag is in scope. */
5096 ref = make_node (code);
5097 pushtag (name, ref);
5100 C_TYPE_BEING_DEFINED (ref) = 1;
5101 TYPE_PACKED (ref) = flag_pack_struct;
5102 return ref;
5105 /* Process the specs, declarator and width (NULL if omitted)
5106 of a structure component, returning a FIELD_DECL node.
5107 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5109 This is done during the parsing of the struct declaration.
5110 The FIELD_DECL nodes are chained together and the lot of them
5111 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5113 tree
5114 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5115 tree width)
5117 tree value;
5119 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5120 && width == NULL_TREE)
5122 /* This is an unnamed decl.
5124 If we have something of the form "union { list } ;" then this
5125 is the anonymous union extension. Similarly for struct.
5127 If this is something of the form "struct foo;", then
5128 If MS extensions are enabled, this is handled as an
5129 anonymous struct.
5130 Otherwise this is a forward declaration of a structure tag.
5132 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5133 If MS extensions are enabled and foo names a structure, then
5134 again this is an anonymous struct.
5135 Otherwise this is an error.
5137 Oh what a horrid tangled web we weave. I wonder if MS consciously
5138 took this from Plan 9 or if it was an accident of implementation
5139 that took root before someone noticed the bug... */
5141 tree type = declspecs->type;
5142 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5143 || TREE_CODE (type) == UNION_TYPE);
5144 bool ok = false;
5146 if (type_ok
5147 && (flag_ms_extensions || !declspecs->typedef_p))
5149 if (flag_ms_extensions)
5150 ok = true;
5151 else if (flag_iso)
5152 ok = false;
5153 else if (TYPE_NAME (type) == NULL)
5154 ok = true;
5155 else
5156 ok = false;
5158 if (!ok)
5160 pedwarn ("declaration does not declare anything");
5161 return NULL_TREE;
5163 if (pedantic)
5164 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5167 value = grokdeclarator (declarator, declspecs, FIELD, false,
5168 width ? &width : NULL);
5170 finish_decl (value, NULL_TREE, NULL_TREE);
5171 DECL_INITIAL (value) = width;
5173 return value;
5176 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5177 the list such that this does not present a problem later. */
5179 static void
5180 detect_field_duplicates (tree fieldlist)
5182 tree x, y;
5183 int timeout = 10;
5185 /* First, see if there are more than "a few" fields.
5186 This is trivially true if there are zero or one fields. */
5187 if (!fieldlist)
5188 return;
5189 x = TREE_CHAIN (fieldlist);
5190 if (!x)
5191 return;
5192 do {
5193 timeout--;
5194 x = TREE_CHAIN (x);
5195 } while (timeout > 0 && x);
5197 /* If there were "few" fields, avoid the overhead of allocating
5198 a hash table. Instead just do the nested traversal thing. */
5199 if (timeout > 0)
5201 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5202 if (DECL_NAME (x))
5204 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5205 if (DECL_NAME (y) == DECL_NAME (x))
5207 error ("%Jduplicate member %qD", x, x);
5208 DECL_NAME (x) = NULL_TREE;
5212 else
5214 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5215 void **slot;
5217 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5218 if ((y = DECL_NAME (x)) != 0)
5220 slot = htab_find_slot (htab, y, INSERT);
5221 if (*slot)
5223 error ("%Jduplicate member %qD", x, x);
5224 DECL_NAME (x) = NULL_TREE;
5226 *slot = y;
5229 htab_delete (htab);
5233 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5234 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5235 ATTRIBUTES are attributes to be applied to the structure. */
5237 tree
5238 finish_struct (tree t, tree fieldlist, tree attributes)
5240 tree x;
5241 bool toplevel = file_scope == current_scope;
5242 int saw_named_field;
5244 /* If this type was previously laid out as a forward reference,
5245 make sure we lay it out again. */
5247 TYPE_SIZE (t) = 0;
5249 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5251 if (pedantic)
5253 for (x = fieldlist; x; x = TREE_CHAIN (x))
5254 if (DECL_NAME (x) != 0)
5255 break;
5257 if (x == 0)
5259 if (TREE_CODE (t) == UNION_TYPE)
5261 if (fieldlist)
5262 pedwarn ("union has no named members");
5263 else
5264 pedwarn ("union has no members");
5266 else
5268 if (fieldlist)
5269 pedwarn ("struct has no named members");
5270 else
5271 pedwarn ("struct has no members");
5276 /* Install struct as DECL_CONTEXT of each field decl.
5277 Also process specified field sizes, found in the DECL_INITIAL,
5278 storing 0 there after the type has been changed to precision equal
5279 to its width, rather than the precision of the specified standard
5280 type. (Correct layout requires the original type to have been preserved
5281 until now.) */
5283 saw_named_field = 0;
5284 for (x = fieldlist; x; x = TREE_CHAIN (x))
5286 DECL_CONTEXT (x) = t;
5287 DECL_PACKED (x) |= TYPE_PACKED (t);
5289 /* If any field is const, the structure type is pseudo-const. */
5290 if (TREE_READONLY (x))
5291 C_TYPE_FIELDS_READONLY (t) = 1;
5292 else
5294 /* A field that is pseudo-const makes the structure likewise. */
5295 tree t1 = TREE_TYPE (x);
5296 while (TREE_CODE (t1) == ARRAY_TYPE)
5297 t1 = TREE_TYPE (t1);
5298 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5299 && C_TYPE_FIELDS_READONLY (t1))
5300 C_TYPE_FIELDS_READONLY (t) = 1;
5303 /* Any field that is volatile means variables of this type must be
5304 treated in some ways as volatile. */
5305 if (TREE_THIS_VOLATILE (x))
5306 C_TYPE_FIELDS_VOLATILE (t) = 1;
5308 /* Any field of nominal variable size implies structure is too. */
5309 if (C_DECL_VARIABLE_SIZE (x))
5310 C_TYPE_VARIABLE_SIZE (t) = 1;
5312 if (DECL_INITIAL (x))
5314 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5315 DECL_SIZE (x) = bitsize_int (width);
5316 DECL_BIT_FIELD (x) = 1;
5317 SET_DECL_C_BIT_FIELD (x);
5320 /* Detect flexible array member in an invalid context. */
5321 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5322 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5323 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5324 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5326 if (TREE_CODE (t) == UNION_TYPE)
5328 error ("%Jflexible array member in union", x);
5329 TREE_TYPE (x) = error_mark_node;
5331 else if (TREE_CHAIN (x) != NULL_TREE)
5333 error ("%Jflexible array member not at end of struct", x);
5334 TREE_TYPE (x) = error_mark_node;
5336 else if (!saw_named_field)
5338 error ("%Jflexible array member in otherwise empty struct", x);
5339 TREE_TYPE (x) = error_mark_node;
5343 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5344 && flexible_array_type_p (TREE_TYPE (x)))
5345 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5347 if (DECL_NAME (x))
5348 saw_named_field = 1;
5351 detect_field_duplicates (fieldlist);
5353 /* Now we have the nearly final fieldlist. Record it,
5354 then lay out the structure or union (including the fields). */
5356 TYPE_FIELDS (t) = fieldlist;
5358 layout_type (t);
5360 /* Give bit-fields their proper types. */
5362 tree *fieldlistp = &fieldlist;
5363 while (*fieldlistp)
5364 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5365 && TREE_TYPE (*fieldlistp) != error_mark_node)
5367 unsigned HOST_WIDE_INT width
5368 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5369 tree type = TREE_TYPE (*fieldlistp);
5370 if (width != TYPE_PRECISION (type))
5372 TREE_TYPE (*fieldlistp)
5373 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5374 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5376 DECL_INITIAL (*fieldlistp) = 0;
5378 else
5379 fieldlistp = &TREE_CHAIN (*fieldlistp);
5382 /* Now we have the truly final field list.
5383 Store it in this type and in the variants. */
5385 TYPE_FIELDS (t) = fieldlist;
5387 /* If there are lots of fields, sort so we can look through them fast.
5388 We arbitrarily consider 16 or more elts to be "a lot". */
5391 int len = 0;
5393 for (x = fieldlist; x; x = TREE_CHAIN (x))
5395 if (len > 15 || DECL_NAME (x) == NULL)
5396 break;
5397 len += 1;
5400 if (len > 15)
5402 tree *field_array;
5403 struct lang_type *space;
5404 struct sorted_fields_type *space2;
5406 len += list_length (x);
5408 /* Use the same allocation policy here that make_node uses, to
5409 ensure that this lives as long as the rest of the struct decl.
5410 All decls in an inline function need to be saved. */
5412 space = GGC_CNEW (struct lang_type);
5413 space2 = GGC_NEWVAR (struct sorted_fields_type,
5414 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5416 len = 0;
5417 space->s = space2;
5418 field_array = &space2->elts[0];
5419 for (x = fieldlist; x; x = TREE_CHAIN (x))
5421 field_array[len++] = x;
5423 /* If there is anonymous struct or union, break out of the loop. */
5424 if (DECL_NAME (x) == NULL)
5425 break;
5427 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5428 if (x == NULL)
5430 TYPE_LANG_SPECIFIC (t) = space;
5431 TYPE_LANG_SPECIFIC (t)->s->len = len;
5432 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5433 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5438 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5440 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5441 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5442 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5443 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5446 /* If this was supposed to be a transparent union, but we can't
5447 make it one, warn and turn off the flag. */
5448 if (TREE_CODE (t) == UNION_TYPE
5449 && TYPE_TRANSPARENT_UNION (t)
5450 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5452 TYPE_TRANSPARENT_UNION (t) = 0;
5453 warning (0, "union cannot be made transparent");
5456 /* If this structure or union completes the type of any previous
5457 variable declaration, lay it out and output its rtl. */
5458 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5460 x = TREE_CHAIN (x))
5462 tree decl = TREE_VALUE (x);
5463 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5464 layout_array_type (TREE_TYPE (decl));
5465 if (TREE_CODE (decl) != TYPE_DECL)
5467 layout_decl (decl, 0);
5468 if (c_dialect_objc ())
5469 objc_check_decl (decl);
5470 rest_of_decl_compilation (decl, toplevel, 0);
5471 if (!toplevel)
5472 expand_decl (decl);
5475 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5477 /* Finish debugging output for this type. */
5478 rest_of_type_compilation (t, toplevel);
5480 /* If we're inside a function proper, i.e. not file-scope and not still
5481 parsing parameters, then arrange for the size of a variable sized type
5482 to be bound now. */
5483 if (cur_stmt_list && variably_modified_type_p (t, NULL))
5484 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5486 return t;
5489 /* Lay out the type T, and its element type, and so on. */
5491 static void
5492 layout_array_type (tree t)
5494 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5495 layout_array_type (TREE_TYPE (t));
5496 layout_type (t);
5499 /* Begin compiling the definition of an enumeration type.
5500 NAME is its name (or null if anonymous).
5501 Returns the type object, as yet incomplete.
5502 Also records info about it so that build_enumerator
5503 may be used to declare the individual values as they are read. */
5505 tree
5506 start_enum (tree name)
5508 tree enumtype = 0;
5510 /* If this is the real definition for a previous forward reference,
5511 fill in the contents in the same object that used to be the
5512 forward reference. */
5514 if (name != 0)
5515 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5517 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5519 enumtype = make_node (ENUMERAL_TYPE);
5520 pushtag (name, enumtype);
5523 if (C_TYPE_BEING_DEFINED (enumtype))
5524 error ("nested redefinition of %<enum %E%>", name);
5526 C_TYPE_BEING_DEFINED (enumtype) = 1;
5528 if (TYPE_VALUES (enumtype) != 0)
5530 /* This enum is a named one that has been declared already. */
5531 error ("redeclaration of %<enum %E%>", name);
5533 /* Completely replace its old definition.
5534 The old enumerators remain defined, however. */
5535 TYPE_VALUES (enumtype) = 0;
5538 enum_next_value = integer_zero_node;
5539 enum_overflow = 0;
5541 if (flag_short_enums)
5542 TYPE_PACKED (enumtype) = 1;
5544 return enumtype;
5547 /* After processing and defining all the values of an enumeration type,
5548 install their decls in the enumeration type and finish it off.
5549 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5550 and ATTRIBUTES are the specified attributes.
5551 Returns ENUMTYPE. */
5553 tree
5554 finish_enum (tree enumtype, tree values, tree attributes)
5556 tree pair, tem;
5557 tree minnode = 0, maxnode = 0;
5558 int precision, unsign;
5559 bool toplevel = (file_scope == current_scope);
5560 struct lang_type *lt;
5562 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5564 /* Calculate the maximum value of any enumerator in this type. */
5566 if (values == error_mark_node)
5567 minnode = maxnode = integer_zero_node;
5568 else
5570 minnode = maxnode = TREE_VALUE (values);
5571 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5573 tree value = TREE_VALUE (pair);
5574 if (tree_int_cst_lt (maxnode, value))
5575 maxnode = value;
5576 if (tree_int_cst_lt (value, minnode))
5577 minnode = value;
5581 /* Construct the final type of this enumeration. It is the same
5582 as one of the integral types - the narrowest one that fits, except
5583 that normally we only go as narrow as int - and signed iff any of
5584 the values are negative. */
5585 unsign = (tree_int_cst_sgn (minnode) >= 0);
5586 precision = MAX (min_precision (minnode, unsign),
5587 min_precision (maxnode, unsign));
5589 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5591 tem = c_common_type_for_size (precision, unsign);
5592 if (tem == NULL)
5594 warning (0, "enumeration values exceed range of largest integer");
5595 tem = long_long_integer_type_node;
5598 else
5599 tem = unsign ? unsigned_type_node : integer_type_node;
5601 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5602 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5603 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5604 TYPE_SIZE (enumtype) = 0;
5606 /* If the precision of the type was specific with an attribute and it
5607 was too small, give an error. Otherwise, use it. */
5608 if (TYPE_PRECISION (enumtype))
5610 if (precision > TYPE_PRECISION (enumtype))
5611 error ("specified mode too small for enumeral values");
5613 else
5614 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5616 layout_type (enumtype);
5618 if (values != error_mark_node)
5620 /* Change the type of the enumerators to be the enum type. We
5621 need to do this irrespective of the size of the enum, for
5622 proper type checking. Replace the DECL_INITIALs of the
5623 enumerators, and the value slots of the list, with copies
5624 that have the enum type; they cannot be modified in place
5625 because they may be shared (e.g. integer_zero_node) Finally,
5626 change the purpose slots to point to the names of the decls. */
5627 for (pair = values; pair; pair = TREE_CHAIN (pair))
5629 tree enu = TREE_PURPOSE (pair);
5630 tree ini = DECL_INITIAL (enu);
5632 TREE_TYPE (enu) = enumtype;
5634 /* The ISO C Standard mandates enumerators to have type int,
5635 even though the underlying type of an enum type is
5636 unspecified. Here we convert any enumerators that fit in
5637 an int to type int, to avoid promotions to unsigned types
5638 when comparing integers with enumerators that fit in the
5639 int range. When -pedantic is given, build_enumerator()
5640 would have already taken care of those that don't fit. */
5641 if (int_fits_type_p (ini, integer_type_node))
5642 tem = integer_type_node;
5643 else
5644 tem = enumtype;
5645 ini = convert (tem, ini);
5647 DECL_INITIAL (enu) = ini;
5648 TREE_PURPOSE (pair) = DECL_NAME (enu);
5649 TREE_VALUE (pair) = ini;
5652 TYPE_VALUES (enumtype) = values;
5655 /* Record the min/max values so that we can warn about bit-field
5656 enumerations that are too small for the values. */
5657 lt = GGC_CNEW (struct lang_type);
5658 lt->enum_min = minnode;
5659 lt->enum_max = maxnode;
5660 TYPE_LANG_SPECIFIC (enumtype) = lt;
5662 /* Fix up all variant types of this enum type. */
5663 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5665 if (tem == enumtype)
5666 continue;
5667 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5668 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5669 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5670 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5671 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5672 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5673 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5674 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5675 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5676 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5677 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5680 /* Finish debugging output for this type. */
5681 rest_of_type_compilation (enumtype, toplevel);
5683 return enumtype;
5686 /* Build and install a CONST_DECL for one value of the
5687 current enumeration type (one that was begun with start_enum).
5688 Return a tree-list containing the CONST_DECL and its value.
5689 Assignment of sequential values by default is handled here. */
5691 tree
5692 build_enumerator (tree name, tree value)
5694 tree decl, type;
5696 /* Validate and default VALUE. */
5698 if (value != 0)
5700 /* Don't issue more errors for error_mark_node (i.e. an
5701 undeclared identifier) - just ignore the value expression. */
5702 if (value == error_mark_node)
5703 value = 0;
5704 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5705 || TREE_CODE (value) != INTEGER_CST)
5707 error ("enumerator value for %qE is not an integer constant", name);
5708 value = 0;
5710 else
5712 value = default_conversion (value);
5713 constant_expression_warning (value);
5717 /* Default based on previous value. */
5718 /* It should no longer be possible to have NON_LVALUE_EXPR
5719 in the default. */
5720 if (value == 0)
5722 value = enum_next_value;
5723 if (enum_overflow)
5724 error ("overflow in enumeration values");
5727 if (pedantic && !int_fits_type_p (value, integer_type_node))
5729 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5730 /* XXX This causes -pedantic to change the meaning of the program.
5731 Remove? -zw 2004-03-15 */
5732 value = convert (integer_type_node, value);
5735 /* Set basis for default for next value. */
5736 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5737 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5739 /* Now create a declaration for the enum value name. */
5741 type = TREE_TYPE (value);
5742 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5743 TYPE_PRECISION (integer_type_node)),
5744 (TYPE_PRECISION (type)
5745 >= TYPE_PRECISION (integer_type_node)
5746 && TYPE_UNSIGNED (type)));
5748 decl = build_decl (CONST_DECL, name, type);
5749 DECL_INITIAL (decl) = convert (type, value);
5750 pushdecl (decl);
5752 return tree_cons (decl, value, NULL_TREE);
5756 /* Create the FUNCTION_DECL for a function definition.
5757 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5758 the declaration; they describe the function's name and the type it returns,
5759 but twisted together in a fashion that parallels the syntax of C.
5761 This function creates a binding context for the function body
5762 as well as setting up the FUNCTION_DECL in current_function_decl.
5764 Returns 1 on success. If the DECLARATOR is not suitable for a function
5765 (it defines a datum instead), we return 0, which tells
5766 yyparse to report a parse error. */
5769 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5770 tree attributes)
5772 tree decl1, old_decl;
5773 tree restype, resdecl;
5774 struct c_label_context_se *nstack_se;
5775 struct c_label_context_vm *nstack_vm;
5777 current_function_returns_value = 0; /* Assume, until we see it does. */
5778 current_function_returns_null = 0;
5779 current_function_returns_abnormally = 0;
5780 warn_about_return_type = 0;
5781 current_extern_inline = 0;
5782 c_switch_stack = NULL;
5784 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
5785 nstack_se->labels_def = NULL;
5786 nstack_se->labels_used = NULL;
5787 nstack_se->next = label_context_stack_se;
5788 label_context_stack_se = nstack_se;
5790 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
5791 nstack_vm->labels_def = NULL;
5792 nstack_vm->labels_used = NULL;
5793 nstack_vm->scope = 0;
5794 nstack_vm->next = label_context_stack_vm;
5795 label_context_stack_vm = nstack_vm;
5797 /* Indicate no valid break/continue context by setting these variables
5798 to some non-null, non-label value. We'll notice and emit the proper
5799 error message in c_finish_bc_stmt. */
5800 c_break_label = c_cont_label = size_zero_node;
5802 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5804 /* If the declarator is not suitable for a function definition,
5805 cause a syntax error. */
5806 if (decl1 == 0)
5808 label_context_stack_se = label_context_stack_se->next;
5809 label_context_stack_vm = label_context_stack_vm->next;
5810 return 0;
5813 decl_attributes (&decl1, attributes, 0);
5815 if (DECL_DECLARED_INLINE_P (decl1)
5816 && DECL_UNINLINABLE (decl1)
5817 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5818 warning (OPT_Wattributes, "%Jinline function %qD given attribute noinline",
5819 decl1, decl1);
5821 announce_function (decl1);
5823 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5825 error ("return type is an incomplete type");
5826 /* Make it return void instead. */
5827 TREE_TYPE (decl1)
5828 = build_function_type (void_type_node,
5829 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5832 if (warn_about_return_type)
5833 pedwarn_c99 ("return type defaults to %<int%>");
5835 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5836 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5837 DECL_INITIAL (decl1) = error_mark_node;
5839 /* If this definition isn't a prototype and we had a prototype declaration
5840 before, copy the arg type info from that prototype. */
5841 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5842 current_function_prototype_locus = UNKNOWN_LOCATION;
5843 current_function_prototype_built_in = false;
5844 current_function_prototype_arg_types = NULL_TREE;
5845 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5847 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5848 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5849 TREE_TYPE (TREE_TYPE (old_decl))))
5851 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5852 TREE_TYPE (decl1));
5853 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5854 current_function_prototype_built_in
5855 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
5856 current_function_prototype_arg_types
5857 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
5859 if (TREE_PUBLIC (decl1))
5861 /* If there is an external prototype declaration of this
5862 function, record its location but do not copy information
5863 to this decl. This may be an invisible declaration
5864 (built-in or in a scope which has finished) or simply
5865 have more refined argument types than any declaration
5866 found above. */
5867 struct c_binding *b;
5868 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
5869 if (B_IN_SCOPE (b, external_scope))
5870 break;
5871 if (b)
5873 tree ext_decl, ext_type;
5874 ext_decl = b->decl;
5875 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
5876 if (TREE_CODE (ext_type) == FUNCTION_TYPE
5877 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5878 TREE_TYPE (ext_type)))
5880 current_function_prototype_locus
5881 = DECL_SOURCE_LOCATION (ext_decl);
5882 current_function_prototype_built_in
5883 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
5884 current_function_prototype_arg_types
5885 = TYPE_ARG_TYPES (ext_type);
5891 /* Optionally warn of old-fashioned def with no previous prototype. */
5892 if (warn_strict_prototypes
5893 && old_decl != error_mark_node
5894 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5895 && C_DECL_ISNT_PROTOTYPE (old_decl))
5896 warning (0, "function declaration isn%'t a prototype");
5897 /* Optionally warn of any global def with no previous prototype. */
5898 else if (warn_missing_prototypes
5899 && old_decl != error_mark_node
5900 && TREE_PUBLIC (decl1)
5901 && !MAIN_NAME_P (DECL_NAME (decl1))
5902 && C_DECL_ISNT_PROTOTYPE (old_decl))
5903 warning (0, "%Jno previous prototype for %qD", decl1, decl1);
5904 /* Optionally warn of any def with no previous prototype
5905 if the function has already been used. */
5906 else if (warn_missing_prototypes
5907 && old_decl != 0
5908 && old_decl != error_mark_node
5909 && TREE_USED (old_decl)
5910 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5911 warning (0, "%J%qD was used with no prototype before its definition",
5912 decl1, decl1);
5913 /* Optionally warn of any global def with no previous declaration. */
5914 else if (warn_missing_declarations
5915 && TREE_PUBLIC (decl1)
5916 && old_decl == 0
5917 && !MAIN_NAME_P (DECL_NAME (decl1)))
5918 warning (0, "%Jno previous declaration for %qD", decl1, decl1);
5919 /* Optionally warn of any def with no previous declaration
5920 if the function has already been used. */
5921 else if (warn_missing_declarations
5922 && old_decl != 0
5923 && old_decl != error_mark_node
5924 && TREE_USED (old_decl)
5925 && C_DECL_IMPLICIT (old_decl))
5926 warning (0, "%J%qD was used with no declaration before its definition",
5927 decl1, decl1);
5929 /* This is a definition, not a reference.
5930 So normally clear DECL_EXTERNAL.
5931 However, `extern inline' acts like a declaration
5932 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5933 DECL_EXTERNAL (decl1) = current_extern_inline;
5935 /* This function exists in static storage.
5936 (This does not mean `static' in the C sense!) */
5937 TREE_STATIC (decl1) = 1;
5939 /* A nested function is not global. */
5940 if (current_function_decl != 0)
5941 TREE_PUBLIC (decl1) = 0;
5943 /* This is the earliest point at which we might know the assembler
5944 name of the function. Thus, if it's set before this, die horribly. */
5945 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5947 /* If #pragma weak was used, mark the decl weak now. */
5948 if (current_scope == file_scope)
5949 maybe_apply_pragma_weak (decl1);
5951 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5952 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5954 tree args;
5955 int argct = 0;
5957 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5958 != integer_type_node)
5959 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5961 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5962 args = TREE_CHAIN (args))
5964 tree type = args ? TREE_VALUE (args) : 0;
5966 if (type == void_type_node)
5967 break;
5969 ++argct;
5970 switch (argct)
5972 case 1:
5973 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5974 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5975 decl1, decl1);
5976 break;
5978 case 2:
5979 if (TREE_CODE (type) != POINTER_TYPE
5980 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5981 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5982 != char_type_node))
5983 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5984 decl1, decl1);
5985 break;
5987 case 3:
5988 if (TREE_CODE (type) != POINTER_TYPE
5989 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5990 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5991 != char_type_node))
5992 pedwarn ("%Jthird argument of %qD should probably be "
5993 "%<char **%>", decl1, decl1);
5994 break;
5998 /* It is intentional that this message does not mention the third
5999 argument because it's only mentioned in an appendix of the
6000 standard. */
6001 if (argct > 0 && (argct < 2 || argct > 3))
6002 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
6004 if (!TREE_PUBLIC (decl1))
6005 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
6008 /* Record the decl so that the function name is defined.
6009 If we already have a decl for this name, and it is a FUNCTION_DECL,
6010 use the old decl. */
6012 current_function_decl = pushdecl (decl1);
6014 push_scope ();
6015 declare_parm_level ();
6017 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6018 /* Promote the value to int before returning it. */
6019 if (c_promoting_integer_type_p (restype))
6021 /* It retains unsignedness if not really getting wider. */
6022 if (TYPE_UNSIGNED (restype)
6023 && (TYPE_PRECISION (restype)
6024 == TYPE_PRECISION (integer_type_node)))
6025 restype = unsigned_type_node;
6026 else
6027 restype = integer_type_node;
6030 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6031 DECL_ARTIFICIAL (resdecl) = 1;
6032 DECL_IGNORED_P (resdecl) = 1;
6033 DECL_RESULT (current_function_decl) = resdecl;
6035 start_fname_decls ();
6037 return 1;
6040 /* Subroutine of store_parm_decls which handles new-style function
6041 definitions (prototype format). The parms already have decls, so we
6042 need only record them as in effect and complain if any redundant
6043 old-style parm decls were written. */
6044 static void
6045 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6047 tree decl;
6049 if (current_scope->bindings)
6051 error ("%Jold-style parameter declarations in prototyped "
6052 "function definition", fndecl);
6054 /* Get rid of the old-style declarations. */
6055 pop_scope ();
6056 push_scope ();
6058 /* Don't issue this warning for nested functions, and don't issue this
6059 warning if we got here because ARG_INFO_TYPES was error_mark_node
6060 (this happens when a function definition has just an ellipsis in
6061 its parameter list). */
6062 else if (warn_traditional && !in_system_header && !current_function_scope
6063 && arg_info->types != error_mark_node)
6064 warning (0, "%Jtraditional C rejects ISO C style function definitions",
6065 fndecl);
6067 /* Now make all the parameter declarations visible in the function body.
6068 We can bypass most of the grunt work of pushdecl. */
6069 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6071 DECL_CONTEXT (decl) = current_function_decl;
6072 if (DECL_NAME (decl))
6073 bind (DECL_NAME (decl), decl, current_scope,
6074 /*invisible=*/false, /*nested=*/false);
6075 else
6076 error ("%Jparameter name omitted", decl);
6079 /* Record the parameter list in the function declaration. */
6080 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6082 /* Now make all the ancillary declarations visible, likewise. */
6083 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6085 DECL_CONTEXT (decl) = current_function_decl;
6086 if (DECL_NAME (decl))
6087 bind (DECL_NAME (decl), decl, current_scope,
6088 /*invisible=*/false, /*nested=*/false);
6091 /* And all the tag declarations. */
6092 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6093 if (TREE_PURPOSE (decl))
6094 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6095 /*invisible=*/false, /*nested=*/false);
6098 /* Subroutine of store_parm_decls which handles old-style function
6099 definitions (separate parameter list and declarations). */
6101 static void
6102 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6104 struct c_binding *b;
6105 tree parm, decl, last;
6106 tree parmids = arg_info->parms;
6108 /* We use DECL_WEAK as a flag to show which parameters have been
6109 seen already, since it is not used on PARM_DECL. */
6110 #ifdef ENABLE_CHECKING
6111 for (b = current_scope->bindings; b; b = b->prev)
6112 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
6113 #endif
6115 if (!in_system_header)
6116 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6117 fndecl);
6119 /* Match each formal parameter name with its declaration. Save each
6120 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6121 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6123 if (TREE_VALUE (parm) == 0)
6125 error ("%Jparameter name missing from parameter list", fndecl);
6126 TREE_PURPOSE (parm) = 0;
6127 continue;
6130 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6131 if (b && B_IN_CURRENT_SCOPE (b))
6133 decl = b->decl;
6134 /* If we got something other than a PARM_DECL it is an error. */
6135 if (TREE_CODE (decl) != PARM_DECL)
6136 error ("%J%qD declared as a non-parameter", decl, decl);
6137 /* If the declaration is already marked, we have a duplicate
6138 name. Complain and ignore the duplicate. */
6139 else if (DECL_WEAK (decl))
6141 error ("%Jmultiple parameters named %qD", decl, decl);
6142 TREE_PURPOSE (parm) = 0;
6143 continue;
6145 /* If the declaration says "void", complain and turn it into
6146 an int. */
6147 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6149 error ("%Jparameter %qD declared with void type", decl, decl);
6150 TREE_TYPE (decl) = integer_type_node;
6151 DECL_ARG_TYPE (decl) = integer_type_node;
6152 layout_decl (decl, 0);
6155 /* If no declaration found, default to int. */
6156 else
6158 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6159 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6160 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6161 pushdecl (decl);
6163 if (flag_isoc99)
6164 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6165 else if (extra_warnings)
6166 warning (0, "%Jtype of %qD defaults to %<int%>", decl, decl);
6169 TREE_PURPOSE (parm) = decl;
6170 DECL_WEAK (decl) = 1;
6173 /* Now examine the parms chain for incomplete declarations
6174 and declarations with no corresponding names. */
6176 for (b = current_scope->bindings; b; b = b->prev)
6178 parm = b->decl;
6179 if (TREE_CODE (parm) != PARM_DECL)
6180 continue;
6182 if (TREE_TYPE (parm) != error_mark_node
6183 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6185 error ("%Jparameter %qD has incomplete type", parm, parm);
6186 TREE_TYPE (parm) = error_mark_node;
6189 if (!DECL_WEAK (parm))
6191 error ("%Jdeclaration for parameter %qD but no such parameter",
6192 parm, parm);
6194 /* Pretend the parameter was not missing.
6195 This gets us to a standard state and minimizes
6196 further error messages. */
6197 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6201 /* Chain the declarations together in the order of the list of
6202 names. Store that chain in the function decl, replacing the
6203 list of names. Update the current scope to match. */
6204 DECL_ARGUMENTS (fndecl) = 0;
6206 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6207 if (TREE_PURPOSE (parm))
6208 break;
6209 if (parm && TREE_PURPOSE (parm))
6211 last = TREE_PURPOSE (parm);
6212 DECL_ARGUMENTS (fndecl) = last;
6213 DECL_WEAK (last) = 0;
6215 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6216 if (TREE_PURPOSE (parm))
6218 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6219 last = TREE_PURPOSE (parm);
6220 DECL_WEAK (last) = 0;
6222 TREE_CHAIN (last) = 0;
6225 /* If there was a previous prototype,
6226 set the DECL_ARG_TYPE of each argument according to
6227 the type previously specified, and report any mismatches. */
6229 if (current_function_prototype_arg_types)
6231 tree type;
6232 for (parm = DECL_ARGUMENTS (fndecl),
6233 type = current_function_prototype_arg_types;
6234 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6235 != void_type_node));
6236 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6238 if (parm == 0 || type == 0
6239 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6241 if (current_function_prototype_built_in)
6242 warning (0, "number of arguments doesn%'t match "
6243 "built-in prototype");
6244 else
6246 error ("number of arguments doesn%'t match prototype");
6247 error ("%Hprototype declaration",
6248 &current_function_prototype_locus);
6250 break;
6252 /* Type for passing arg must be consistent with that
6253 declared for the arg. ISO C says we take the unqualified
6254 type for parameters declared with qualified type. */
6255 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6256 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6258 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6259 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6261 /* Adjust argument to match prototype. E.g. a previous
6262 `int foo(float);' prototype causes
6263 `int foo(x) float x; {...}' to be treated like
6264 `int foo(float x) {...}'. This is particularly
6265 useful for argument types like uid_t. */
6266 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6268 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6269 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6270 && TYPE_PRECISION (TREE_TYPE (parm))
6271 < TYPE_PRECISION (integer_type_node))
6272 DECL_ARG_TYPE (parm) = integer_type_node;
6274 if (pedantic)
6276 /* ??? Is it possible to get here with a
6277 built-in prototype or will it always have
6278 been diagnosed as conflicting with an
6279 old-style definition and discarded? */
6280 if (current_function_prototype_built_in)
6281 warning (0, "promoted argument %qD "
6282 "doesn%'t match built-in prototype", parm);
6283 else
6285 pedwarn ("promoted argument %qD "
6286 "doesn%'t match prototype", parm);
6287 pedwarn ("%Hprototype declaration",
6288 &current_function_prototype_locus);
6292 else
6294 if (current_function_prototype_built_in)
6295 warning (0, "argument %qD doesn%'t match "
6296 "built-in prototype", parm);
6297 else
6299 error ("argument %qD doesn%'t match prototype", parm);
6300 error ("%Hprototype declaration",
6301 &current_function_prototype_locus);
6306 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6309 /* Otherwise, create a prototype that would match. */
6311 else
6313 tree actual = 0, last = 0, type;
6315 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6317 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6318 if (last)
6319 TREE_CHAIN (last) = type;
6320 else
6321 actual = type;
6322 last = type;
6324 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6325 if (last)
6326 TREE_CHAIN (last) = type;
6327 else
6328 actual = type;
6330 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6331 of the type of this function, but we need to avoid having this
6332 affect the types of other similarly-typed functions, so we must
6333 first force the generation of an identical (but separate) type
6334 node for the relevant function type. The new node we create
6335 will be a variant of the main variant of the original function
6336 type. */
6338 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6340 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6344 /* Store parameter declarations passed in ARG_INFO into the current
6345 function declaration. */
6347 void
6348 store_parm_decls_from (struct c_arg_info *arg_info)
6350 current_function_arg_info = arg_info;
6351 store_parm_decls ();
6354 /* Store the parameter declarations into the current function declaration.
6355 This is called after parsing the parameter declarations, before
6356 digesting the body of the function.
6358 For an old-style definition, construct a prototype out of the old-style
6359 parameter declarations and inject it into the function's type. */
6361 void
6362 store_parm_decls (void)
6364 tree fndecl = current_function_decl;
6365 bool proto;
6367 /* The argument information block for FNDECL. */
6368 struct c_arg_info *arg_info = current_function_arg_info;
6369 current_function_arg_info = 0;
6371 /* True if this definition is written with a prototype. Note:
6372 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6373 list in a function definition as equivalent to (void) -- an
6374 empty argument list specifies the function has no parameters,
6375 but only (void) sets up a prototype for future calls. */
6376 proto = arg_info->types != 0;
6378 if (proto)
6379 store_parm_decls_newstyle (fndecl, arg_info);
6380 else
6381 store_parm_decls_oldstyle (fndecl, arg_info);
6383 /* The next call to push_scope will be a function body. */
6385 next_is_function_body = true;
6387 /* Write a record describing this function definition to the prototypes
6388 file (if requested). */
6390 gen_aux_info_record (fndecl, 1, 0, proto);
6392 /* Initialize the RTL code for the function. */
6393 allocate_struct_function (fndecl);
6395 /* Begin the statement tree for this function. */
6396 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6398 /* ??? Insert the contents of the pending sizes list into the function
6399 to be evaluated. The only reason left to have this is
6400 void foo(int n, int array[n++])
6401 because we throw away the array type in favor of a pointer type, and
6402 thus won't naturally see the SAVE_EXPR containing the increment. All
6403 other pending sizes would be handled by gimplify_parameters. */
6405 tree t;
6406 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6407 add_stmt (TREE_VALUE (t));
6410 /* Even though we're inside a function body, we still don't want to
6411 call expand_expr to calculate the size of a variable-sized array.
6412 We haven't necessarily assigned RTL to all variables yet, so it's
6413 not safe to try to expand expressions involving them. */
6414 cfun->x_dont_save_pending_sizes_p = 1;
6417 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6418 functions. */
6420 static void
6421 c_warn_unused_result_recursively (tree fndecl)
6423 struct cgraph_node *cgn;
6425 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6426 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6428 /* Finalize all nested functions now. */
6429 cgn = cgraph_node (fndecl);
6430 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6431 c_warn_unused_result_recursively (cgn->decl);
6434 /* Finish up a function declaration and compile that function
6435 all the way to assembler language output. The free the storage
6436 for the function definition.
6438 This is called after parsing the body of the function definition. */
6440 void
6441 finish_function (void)
6443 tree fndecl = current_function_decl;
6445 label_context_stack_se = label_context_stack_se->next;
6446 label_context_stack_vm = label_context_stack_vm->next;
6448 if (TREE_CODE (fndecl) == FUNCTION_DECL
6449 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6451 tree args = DECL_ARGUMENTS (fndecl);
6452 for (; args; args = TREE_CHAIN (args))
6454 tree type = TREE_TYPE (args);
6455 if (INTEGRAL_TYPE_P (type)
6456 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6457 DECL_ARG_TYPE (args) = integer_type_node;
6461 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6462 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6464 /* Must mark the RESULT_DECL as being in this function. */
6466 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6467 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6469 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6471 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6472 != integer_type_node)
6474 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6475 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6476 if (!warn_main)
6477 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6479 else
6481 if (flag_isoc99)
6483 tree stmt = c_finish_return (integer_zero_node);
6484 #ifdef USE_MAPPED_LOCATION
6485 /* Hack. We don't want the middle-end to warn that this return
6486 is unreachable, so we mark its location as special. Using
6487 UNKNOWN_LOCATION has the problem that it gets clobbered in
6488 annotate_one_with_locus. A cleaner solution might be to
6489 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6491 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6492 #else
6493 /* Hack. We don't want the middle-end to warn that this
6494 return is unreachable, so put the statement on the
6495 special line 0. */
6496 annotate_with_file_line (stmt, input_filename, 0);
6497 #endif
6502 /* Tie off the statement tree for this function. */
6503 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6505 finish_fname_decls ();
6507 /* Complain if there's just no return statement. */
6508 if (warn_return_type
6509 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6510 && !current_function_returns_value && !current_function_returns_null
6511 /* Don't complain if we are no-return. */
6512 && !current_function_returns_abnormally
6513 /* Don't warn for main(). */
6514 && !MAIN_NAME_P (DECL_NAME (fndecl))
6515 /* Or if they didn't actually specify a return type. */
6516 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6517 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6518 inline function, as we might never be compiled separately. */
6519 && DECL_INLINE (fndecl))
6521 warning (0, "no return statement in function returning non-void");
6522 TREE_NO_WARNING (fndecl) = 1;
6525 /* With just -Wextra, complain only if function returns both with
6526 and without a value. */
6527 if (extra_warnings
6528 && current_function_returns_value
6529 && current_function_returns_null)
6530 warning (0, "this function may return with or without a value");
6532 /* Store the end of the function, so that we get good line number
6533 info for the epilogue. */
6534 cfun->function_end_locus = input_location;
6536 /* If we don't have ctors/dtors sections, and this is a static
6537 constructor or destructor, it must be recorded now. */
6538 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6539 && !targetm.have_ctors_dtors)
6540 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6541 if (DECL_STATIC_DESTRUCTOR (fndecl)
6542 && !targetm.have_ctors_dtors)
6543 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6545 /* Finalize the ELF visibility for the function. */
6546 c_determine_visibility (fndecl);
6548 /* Genericize before inlining. Delay genericizing nested functions
6549 until their parent function is genericized. Since finalizing
6550 requires GENERIC, delay that as well. */
6552 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6553 && !undef_nested_function)
6555 if (!decl_function_context (fndecl))
6557 c_genericize (fndecl);
6558 c_warn_unused_result_recursively (fndecl);
6560 /* ??? Objc emits functions after finalizing the compilation unit.
6561 This should be cleaned up later and this conditional removed. */
6562 if (cgraph_global_info_ready)
6564 c_expand_body (fndecl);
6565 return;
6568 cgraph_finalize_function (fndecl, false);
6570 else
6572 /* Register this function with cgraph just far enough to get it
6573 added to our parent's nested function list. Handy, since the
6574 C front end doesn't have such a list. */
6575 (void) cgraph_node (fndecl);
6579 if (!decl_function_context (fndecl))
6580 undef_nested_function = false;
6582 /* We're leaving the context of this function, so zap cfun.
6583 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6584 tree_rest_of_compilation. */
6585 cfun = NULL;
6586 current_function_decl = NULL;
6589 /* Generate the RTL for the body of FNDECL. */
6591 void
6592 c_expand_body (tree fndecl)
6595 if (!DECL_INITIAL (fndecl)
6596 || DECL_INITIAL (fndecl) == error_mark_node)
6597 return;
6599 tree_rest_of_compilation (fndecl);
6601 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6602 && targetm.have_ctors_dtors)
6603 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6604 DEFAULT_INIT_PRIORITY);
6605 if (DECL_STATIC_DESTRUCTOR (fndecl)
6606 && targetm.have_ctors_dtors)
6607 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6608 DEFAULT_INIT_PRIORITY);
6611 /* Check the declarations given in a for-loop for satisfying the C99
6612 constraints. */
6613 void
6614 check_for_loop_decls (void)
6616 struct c_binding *b;
6618 if (!flag_isoc99)
6620 /* If we get here, declarations have been used in a for loop without
6621 the C99 for loop scope. This doesn't make much sense, so don't
6622 allow it. */
6623 error ("%<for%> loop initial declaration used outside C99 mode");
6624 return;
6626 /* C99 subclause 6.8.5 paragraph 3:
6628 [#3] The declaration part of a for statement shall only
6629 declare identifiers for objects having storage class auto or
6630 register.
6632 It isn't clear whether, in this sentence, "identifiers" binds to
6633 "shall only declare" or to "objects" - that is, whether all identifiers
6634 declared must be identifiers for objects, or whether the restriction
6635 only applies to those that are. (A question on this in comp.std.c
6636 in November 2000 received no answer.) We implement the strictest
6637 interpretation, to avoid creating an extension which later causes
6638 problems. */
6640 for (b = current_scope->bindings; b; b = b->prev)
6642 tree id = b->id;
6643 tree decl = b->decl;
6645 if (!id)
6646 continue;
6648 switch (TREE_CODE (decl))
6650 case VAR_DECL:
6651 if (TREE_STATIC (decl))
6652 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6653 "initial declaration", decl, decl);
6654 else if (DECL_EXTERNAL (decl))
6655 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6656 "initial declaration", decl, decl);
6657 break;
6659 case RECORD_TYPE:
6660 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6661 id);
6662 break;
6663 case UNION_TYPE:
6664 error ("%<union %E%> declared in %<for%> loop initial declaration",
6665 id);
6666 break;
6667 case ENUMERAL_TYPE:
6668 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6669 id);
6670 break;
6671 default:
6672 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6673 "initial declaration", decl, decl);
6678 /* Save and reinitialize the variables
6679 used during compilation of a C function. */
6681 void
6682 c_push_function_context (struct function *f)
6684 struct language_function *p;
6685 p = GGC_NEW (struct language_function);
6686 f->language = p;
6688 p->base.x_stmt_tree = c_stmt_tree;
6689 p->x_break_label = c_break_label;
6690 p->x_cont_label = c_cont_label;
6691 p->x_switch_stack = c_switch_stack;
6692 p->arg_info = current_function_arg_info;
6693 p->returns_value = current_function_returns_value;
6694 p->returns_null = current_function_returns_null;
6695 p->returns_abnormally = current_function_returns_abnormally;
6696 p->warn_about_return_type = warn_about_return_type;
6697 p->extern_inline = current_extern_inline;
6700 /* Restore the variables used during compilation of a C function. */
6702 void
6703 c_pop_function_context (struct function *f)
6705 struct language_function *p = f->language;
6707 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6708 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6710 /* Stop pointing to the local nodes about to be freed. */
6711 /* But DECL_INITIAL must remain nonzero so we know this
6712 was an actual function definition. */
6713 DECL_INITIAL (current_function_decl) = error_mark_node;
6714 DECL_ARGUMENTS (current_function_decl) = 0;
6717 c_stmt_tree = p->base.x_stmt_tree;
6718 c_break_label = p->x_break_label;
6719 c_cont_label = p->x_cont_label;
6720 c_switch_stack = p->x_switch_stack;
6721 current_function_arg_info = p->arg_info;
6722 current_function_returns_value = p->returns_value;
6723 current_function_returns_null = p->returns_null;
6724 current_function_returns_abnormally = p->returns_abnormally;
6725 warn_about_return_type = p->warn_about_return_type;
6726 current_extern_inline = p->extern_inline;
6728 f->language = NULL;
6731 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6733 void
6734 c_dup_lang_specific_decl (tree decl)
6736 struct lang_decl *ld;
6738 if (!DECL_LANG_SPECIFIC (decl))
6739 return;
6741 ld = GGC_NEW (struct lang_decl);
6742 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6743 DECL_LANG_SPECIFIC (decl) = ld;
6746 /* The functions below are required for functionality of doing
6747 function at once processing in the C front end. Currently these
6748 functions are not called from anywhere in the C front end, but as
6749 these changes continue, that will change. */
6751 /* Returns the stmt_tree (if any) to which statements are currently
6752 being added. If there is no active statement-tree, NULL is
6753 returned. */
6755 stmt_tree
6756 current_stmt_tree (void)
6758 return &c_stmt_tree;
6761 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6762 C. */
6765 anon_aggr_type_p (tree ARG_UNUSED (node))
6767 return 0;
6770 /* Return the global value of T as a symbol. */
6772 tree
6773 identifier_global_value (tree t)
6775 struct c_binding *b;
6777 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6778 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6779 return b->decl;
6781 return 0;
6784 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6785 otherwise the name is found in ridpointers from RID_INDEX. */
6787 void
6788 record_builtin_type (enum rid rid_index, const char *name, tree type)
6790 tree id, decl;
6791 if (name == 0)
6792 id = ridpointers[(int) rid_index];
6793 else
6794 id = get_identifier (name);
6795 decl = build_decl (TYPE_DECL, id, type);
6796 pushdecl (decl);
6797 if (debug_hooks->type_decl)
6798 debug_hooks->type_decl (decl, false);
6801 /* Build the void_list_node (void_type_node having been created). */
6802 tree
6803 build_void_list_node (void)
6805 tree t = build_tree_list (NULL_TREE, void_type_node);
6806 return t;
6809 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6811 struct c_parm *
6812 build_c_parm (struct c_declspecs *specs, tree attrs,
6813 struct c_declarator *declarator)
6815 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6816 ret->specs = specs;
6817 ret->attrs = attrs;
6818 ret->declarator = declarator;
6819 return ret;
6822 /* Return a declarator with nested attributes. TARGET is the inner
6823 declarator to which these attributes apply. ATTRS are the
6824 attributes. */
6826 struct c_declarator *
6827 build_attrs_declarator (tree attrs, struct c_declarator *target)
6829 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6830 ret->kind = cdk_attrs;
6831 ret->declarator = target;
6832 ret->u.attrs = attrs;
6833 return ret;
6836 /* Return a declarator for a function with arguments specified by ARGS
6837 and return type specified by TARGET. */
6839 struct c_declarator *
6840 build_function_declarator (struct c_arg_info *args,
6841 struct c_declarator *target)
6843 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6844 ret->kind = cdk_function;
6845 ret->declarator = target;
6846 ret->u.arg_info = args;
6847 return ret;
6850 /* Return a declarator for the identifier IDENT (which may be
6851 NULL_TREE for an abstract declarator). */
6853 struct c_declarator *
6854 build_id_declarator (tree ident)
6856 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6857 ret->kind = cdk_id;
6858 ret->declarator = 0;
6859 ret->u.id = ident;
6860 /* Default value - may get reset to a more precise location. */
6861 ret->id_loc = input_location;
6862 return ret;
6865 /* Return something to represent absolute declarators containing a *.
6866 TARGET is the absolute declarator that the * contains.
6867 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6868 to apply to the pointer type. */
6870 struct c_declarator *
6871 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6872 struct c_declarator *target)
6874 tree attrs;
6875 int quals = 0;
6876 struct c_declarator *itarget = target;
6877 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6878 if (type_quals_attrs)
6880 attrs = type_quals_attrs->attrs;
6881 quals = quals_from_declspecs (type_quals_attrs);
6882 if (attrs != NULL_TREE)
6883 itarget = build_attrs_declarator (attrs, target);
6885 ret->kind = cdk_pointer;
6886 ret->declarator = itarget;
6887 ret->u.pointer_quals = quals;
6888 return ret;
6891 /* Return a pointer to a structure for an empty list of declaration
6892 specifiers. */
6894 struct c_declspecs *
6895 build_null_declspecs (void)
6897 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6898 ret->type = 0;
6899 ret->decl_attr = 0;
6900 ret->attrs = 0;
6901 ret->typespec_word = cts_none;
6902 ret->storage_class = csc_none;
6903 ret->declspecs_seen_p = false;
6904 ret->type_seen_p = false;
6905 ret->non_sc_seen_p = false;
6906 ret->typedef_p = false;
6907 ret->tag_defined_p = false;
6908 ret->explicit_signed_p = false;
6909 ret->deprecated_p = false;
6910 ret->default_int_p = false;
6911 ret->long_p = false;
6912 ret->long_long_p = false;
6913 ret->short_p = false;
6914 ret->signed_p = false;
6915 ret->unsigned_p = false;
6916 ret->complex_p = false;
6917 ret->inline_p = false;
6918 ret->thread_p = false;
6919 ret->const_p = false;
6920 ret->volatile_p = false;
6921 ret->restrict_p = false;
6922 return ret;
6925 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6926 returning SPECS. */
6928 struct c_declspecs *
6929 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6931 enum rid i;
6932 bool dupe = false;
6933 specs->non_sc_seen_p = true;
6934 specs->declspecs_seen_p = true;
6935 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6936 && C_IS_RESERVED_WORD (qual));
6937 i = C_RID_CODE (qual);
6938 switch (i)
6940 case RID_CONST:
6941 dupe = specs->const_p;
6942 specs->const_p = true;
6943 break;
6944 case RID_VOLATILE:
6945 dupe = specs->volatile_p;
6946 specs->volatile_p = true;
6947 break;
6948 case RID_RESTRICT:
6949 dupe = specs->restrict_p;
6950 specs->restrict_p = true;
6951 break;
6952 default:
6953 gcc_unreachable ();
6955 if (dupe && pedantic && !flag_isoc99)
6956 pedwarn ("duplicate %qE", qual);
6957 return specs;
6960 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6961 returning SPECS. */
6963 struct c_declspecs *
6964 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6966 tree type = spec.spec;
6967 specs->non_sc_seen_p = true;
6968 specs->declspecs_seen_p = true;
6969 specs->type_seen_p = true;
6970 if (TREE_DEPRECATED (type))
6971 specs->deprecated_p = true;
6973 /* Handle type specifier keywords. */
6974 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6976 enum rid i = C_RID_CODE (type);
6977 if (specs->type)
6979 error ("two or more data types in declaration specifiers");
6980 return specs;
6982 if ((int) i <= (int) RID_LAST_MODIFIER)
6984 /* "long", "short", "signed", "unsigned" or "_Complex". */
6985 bool dupe = false;
6986 switch (i)
6988 case RID_LONG:
6989 if (specs->long_long_p)
6991 error ("%<long long long%> is too long for GCC");
6992 break;
6994 if (specs->long_p)
6996 if (specs->typespec_word == cts_double)
6998 error ("both %<long long%> and %<double%> in "
6999 "declaration specifiers");
7000 break;
7002 if (pedantic && !flag_isoc99 && !in_system_header
7003 && warn_long_long)
7004 pedwarn ("ISO C90 does not support %<long long%>");
7005 specs->long_long_p = 1;
7006 break;
7008 if (specs->short_p)
7009 error ("both %<long%> and %<short%> in "
7010 "declaration specifiers");
7011 else if (specs->typespec_word == cts_void)
7012 error ("both %<long%> and %<void%> in "
7013 "declaration specifiers");
7014 else if (specs->typespec_word == cts_bool)
7015 error ("both %<long%> and %<_Bool%> in "
7016 "declaration specifiers");
7017 else if (specs->typespec_word == cts_char)
7018 error ("both %<long%> and %<char%> in "
7019 "declaration specifiers");
7020 else if (specs->typespec_word == cts_float)
7021 error ("both %<long%> and %<float%> in "
7022 "declaration specifiers");
7023 else
7024 specs->long_p = true;
7025 break;
7026 case RID_SHORT:
7027 dupe = specs->short_p;
7028 if (specs->long_p)
7029 error ("both %<long%> and %<short%> in "
7030 "declaration specifiers");
7031 else if (specs->typespec_word == cts_void)
7032 error ("both %<short%> and %<void%> in "
7033 "declaration specifiers");
7034 else if (specs->typespec_word == cts_bool)
7035 error ("both %<short%> and %<_Bool%> in "
7036 "declaration specifiers");
7037 else if (specs->typespec_word == cts_char)
7038 error ("both %<short%> and %<char%> in "
7039 "declaration specifiers");
7040 else if (specs->typespec_word == cts_float)
7041 error ("both %<short%> and %<float%> in "
7042 "declaration specifiers");
7043 else if (specs->typespec_word == cts_double)
7044 error ("both %<short%> and %<double%> in "
7045 "declaration specifiers");
7046 else
7047 specs->short_p = true;
7048 break;
7049 case RID_SIGNED:
7050 dupe = specs->signed_p;
7051 if (specs->unsigned_p)
7052 error ("both %<signed%> and %<unsigned%> in "
7053 "declaration specifiers");
7054 else if (specs->typespec_word == cts_void)
7055 error ("both %<signed%> and %<void%> in "
7056 "declaration specifiers");
7057 else if (specs->typespec_word == cts_bool)
7058 error ("both %<signed%> and %<_Bool%> in "
7059 "declaration specifiers");
7060 else if (specs->typespec_word == cts_float)
7061 error ("both %<signed%> and %<float%> in "
7062 "declaration specifiers");
7063 else if (specs->typespec_word == cts_double)
7064 error ("both %<signed%> and %<double%> in "
7065 "declaration specifiers");
7066 else
7067 specs->signed_p = true;
7068 break;
7069 case RID_UNSIGNED:
7070 dupe = specs->unsigned_p;
7071 if (specs->signed_p)
7072 error ("both %<signed%> and %<unsigned%> in "
7073 "declaration specifiers");
7074 else if (specs->typespec_word == cts_void)
7075 error ("both %<unsigned%> and %<void%> in "
7076 "declaration specifiers");
7077 else if (specs->typespec_word == cts_bool)
7078 error ("both %<unsigned%> and %<_Bool%> in "
7079 "declaration specifiers");
7080 else if (specs->typespec_word == cts_float)
7081 error ("both %<unsigned%> and %<float%> in "
7082 "declaration specifiers");
7083 else if (specs->typespec_word == cts_double)
7084 error ("both %<unsigned%> and %<double%> in "
7085 "declaration specifiers");
7086 else
7087 specs->unsigned_p = true;
7088 break;
7089 case RID_COMPLEX:
7090 dupe = specs->complex_p;
7091 if (pedantic && !flag_isoc99 && !in_system_header)
7092 pedwarn ("ISO C90 does not support complex types");
7093 if (specs->typespec_word == cts_void)
7094 error ("both %<complex%> and %<void%> in "
7095 "declaration specifiers");
7096 else if (specs->typespec_word == cts_bool)
7097 error ("both %<complex%> and %<_Bool%> in "
7098 "declaration specifiers");
7099 else
7100 specs->complex_p = true;
7101 break;
7102 default:
7103 gcc_unreachable ();
7106 if (dupe)
7107 error ("duplicate %qE", type);
7109 return specs;
7111 else
7113 /* "void", "_Bool", "char", "int", "float" or "double". */
7114 if (specs->typespec_word != cts_none)
7116 error ("two or more data types in declaration specifiers");
7117 return specs;
7119 switch (i)
7121 case RID_VOID:
7122 if (specs->long_p)
7123 error ("both %<long%> and %<void%> in "
7124 "declaration specifiers");
7125 else if (specs->short_p)
7126 error ("both %<short%> and %<void%> in "
7127 "declaration specifiers");
7128 else if (specs->signed_p)
7129 error ("both %<signed%> and %<void%> in "
7130 "declaration specifiers");
7131 else if (specs->unsigned_p)
7132 error ("both %<unsigned%> and %<void%> in "
7133 "declaration specifiers");
7134 else if (specs->complex_p)
7135 error ("both %<complex%> and %<void%> in "
7136 "declaration specifiers");
7137 else
7138 specs->typespec_word = cts_void;
7139 return specs;
7140 case RID_BOOL:
7141 if (specs->long_p)
7142 error ("both %<long%> and %<_Bool%> in "
7143 "declaration specifiers");
7144 else if (specs->short_p)
7145 error ("both %<short%> and %<_Bool%> in "
7146 "declaration specifiers");
7147 else if (specs->signed_p)
7148 error ("both %<signed%> and %<_Bool%> in "
7149 "declaration specifiers");
7150 else if (specs->unsigned_p)
7151 error ("both %<unsigned%> and %<_Bool%> in "
7152 "declaration specifiers");
7153 else if (specs->complex_p)
7154 error ("both %<complex%> and %<_Bool%> in "
7155 "declaration specifiers");
7156 else
7157 specs->typespec_word = cts_bool;
7158 return specs;
7159 case RID_CHAR:
7160 if (specs->long_p)
7161 error ("both %<long%> and %<char%> in "
7162 "declaration specifiers");
7163 else if (specs->short_p)
7164 error ("both %<short%> and %<char%> in "
7165 "declaration specifiers");
7166 else
7167 specs->typespec_word = cts_char;
7168 return specs;
7169 case RID_INT:
7170 specs->typespec_word = cts_int;
7171 return specs;
7172 case RID_FLOAT:
7173 if (specs->long_p)
7174 error ("both %<long%> and %<float%> in "
7175 "declaration specifiers");
7176 else if (specs->short_p)
7177 error ("both %<short%> and %<float%> in "
7178 "declaration specifiers");
7179 else if (specs->signed_p)
7180 error ("both %<signed%> and %<float%> in "
7181 "declaration specifiers");
7182 else if (specs->unsigned_p)
7183 error ("both %<unsigned%> and %<float%> in "
7184 "declaration specifiers");
7185 else
7186 specs->typespec_word = cts_float;
7187 return specs;
7188 case RID_DOUBLE:
7189 if (specs->long_long_p)
7190 error ("both %<long long%> and %<double%> in "
7191 "declaration specifiers");
7192 else if (specs->short_p)
7193 error ("both %<short%> and %<double%> in "
7194 "declaration specifiers");
7195 else if (specs->signed_p)
7196 error ("both %<signed%> and %<double%> in "
7197 "declaration specifiers");
7198 else if (specs->unsigned_p)
7199 error ("both %<unsigned%> and %<double%> in "
7200 "declaration specifiers");
7201 else
7202 specs->typespec_word = cts_double;
7203 return specs;
7204 default:
7205 /* ObjC reserved word "id", handled below. */
7206 break;
7211 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7212 form of ObjC type, cases such as "int" and "long" being handled
7213 above), a TYPE (struct, union, enum and typeof specifiers) or an
7214 ERROR_MARK. In none of these cases may there have previously
7215 been any type specifiers. */
7216 if (specs->type || specs->typespec_word != cts_none
7217 || specs->long_p || specs->short_p || specs->signed_p
7218 || specs->unsigned_p || specs->complex_p)
7219 error ("two or more data types in declaration specifiers");
7220 else if (TREE_CODE (type) == TYPE_DECL)
7222 if (TREE_TYPE (type) == error_mark_node)
7223 ; /* Allow the type to default to int to avoid cascading errors. */
7224 else
7226 specs->type = TREE_TYPE (type);
7227 specs->decl_attr = DECL_ATTRIBUTES (type);
7228 specs->typedef_p = true;
7229 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7232 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7234 tree t = lookup_name (type);
7235 if (!t || TREE_CODE (t) != TYPE_DECL)
7236 error ("%qE fails to be a typedef or built in type", type);
7237 else if (TREE_TYPE (t) == error_mark_node)
7239 else
7240 specs->type = TREE_TYPE (t);
7242 else if (TREE_CODE (type) != ERROR_MARK)
7244 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7245 specs->tag_defined_p = true;
7246 if (spec.kind == ctsk_typeof)
7247 specs->typedef_p = true;
7248 specs->type = type;
7251 return specs;
7254 /* Add the storage class specifier or function specifier SCSPEC to the
7255 declaration specifiers SPECS, returning SPECS. */
7257 struct c_declspecs *
7258 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7260 enum rid i;
7261 enum c_storage_class n = csc_none;
7262 bool dupe = false;
7263 specs->declspecs_seen_p = true;
7264 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7265 && C_IS_RESERVED_WORD (scspec));
7266 i = C_RID_CODE (scspec);
7267 if (extra_warnings && specs->non_sc_seen_p)
7268 warning (0, "%qE is not at beginning of declaration", scspec);
7269 switch (i)
7271 case RID_INLINE:
7272 /* C99 permits duplicate inline. Although of doubtful utility,
7273 it seems simplest to permit it in gnu89 mode as well, as
7274 there is also little utility in maintaining this as a
7275 difference between gnu89 and C99 inline. */
7276 dupe = false;
7277 specs->inline_p = true;
7278 break;
7279 case RID_THREAD:
7280 dupe = specs->thread_p;
7281 if (specs->storage_class == csc_auto)
7282 error ("%<__thread%> used with %<auto%>");
7283 else if (specs->storage_class == csc_register)
7284 error ("%<__thread%> used with %<register%>");
7285 else if (specs->storage_class == csc_typedef)
7286 error ("%<__thread%> used with %<typedef%>");
7287 else
7288 specs->thread_p = true;
7289 break;
7290 case RID_AUTO:
7291 n = csc_auto;
7292 break;
7293 case RID_EXTERN:
7294 n = csc_extern;
7295 /* Diagnose "__thread extern". */
7296 if (specs->thread_p)
7297 error ("%<__thread%> before %<extern%>");
7298 break;
7299 case RID_REGISTER:
7300 n = csc_register;
7301 break;
7302 case RID_STATIC:
7303 n = csc_static;
7304 /* Diagnose "__thread static". */
7305 if (specs->thread_p)
7306 error ("%<__thread%> before %<static%>");
7307 break;
7308 case RID_TYPEDEF:
7309 n = csc_typedef;
7310 break;
7311 default:
7312 gcc_unreachable ();
7314 if (n != csc_none && n == specs->storage_class)
7315 dupe = true;
7316 if (dupe)
7317 error ("duplicate %qE", scspec);
7318 if (n != csc_none)
7320 if (specs->storage_class != csc_none && n != specs->storage_class)
7322 error ("multiple storage classes in declaration specifiers");
7324 else
7326 specs->storage_class = n;
7327 if (n != csc_extern && n != csc_static && specs->thread_p)
7329 error ("%<__thread%> used with %qE", scspec);
7330 specs->thread_p = false;
7334 return specs;
7337 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7338 returning SPECS. */
7340 struct c_declspecs *
7341 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7343 specs->attrs = chainon (attrs, specs->attrs);
7344 specs->declspecs_seen_p = true;
7345 return specs;
7348 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7349 specifiers with any other type specifier to determine the resulting
7350 type. This is where ISO C checks on complex types are made, since
7351 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7352 double". */
7354 struct c_declspecs *
7355 finish_declspecs (struct c_declspecs *specs)
7357 /* If a type was specified as a whole, we have no modifiers and are
7358 done. */
7359 if (specs->type != NULL_TREE)
7361 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7362 && !specs->signed_p && !specs->unsigned_p
7363 && !specs->complex_p);
7364 return specs;
7367 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7368 has been specified, treat it as "int" unless "_Complex" is
7369 present and there are no other specifiers. If we just have
7370 "_Complex", it is equivalent to "_Complex double", but e.g.
7371 "_Complex short" is equivalent to "_Complex short int". */
7372 if (specs->typespec_word == cts_none)
7374 if (specs->long_p || specs->short_p
7375 || specs->signed_p || specs->unsigned_p)
7377 specs->typespec_word = cts_int;
7379 else if (specs->complex_p)
7381 specs->typespec_word = cts_double;
7382 if (pedantic)
7383 pedwarn ("ISO C does not support plain %<complex%> meaning "
7384 "%<double complex%>");
7386 else
7388 specs->typespec_word = cts_int;
7389 specs->default_int_p = true;
7390 /* We don't diagnose this here because grokdeclarator will
7391 give more specific diagnostics according to whether it is
7392 a function definition. */
7396 /* If "signed" was specified, record this to distinguish "int" and
7397 "signed int" in the case of a bit-field with
7398 -funsigned-bitfields. */
7399 specs->explicit_signed_p = specs->signed_p;
7401 /* Now compute the actual type. */
7402 switch (specs->typespec_word)
7404 case cts_void:
7405 gcc_assert (!specs->long_p && !specs->short_p
7406 && !specs->signed_p && !specs->unsigned_p
7407 && !specs->complex_p);
7408 specs->type = void_type_node;
7409 break;
7410 case cts_bool:
7411 gcc_assert (!specs->long_p && !specs->short_p
7412 && !specs->signed_p && !specs->unsigned_p
7413 && !specs->complex_p);
7414 specs->type = boolean_type_node;
7415 break;
7416 case cts_char:
7417 gcc_assert (!specs->long_p && !specs->short_p);
7418 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7419 if (specs->signed_p)
7420 specs->type = signed_char_type_node;
7421 else if (specs->unsigned_p)
7422 specs->type = unsigned_char_type_node;
7423 else
7424 specs->type = char_type_node;
7425 if (specs->complex_p)
7427 if (pedantic)
7428 pedwarn ("ISO C does not support complex integer types");
7429 specs->type = build_complex_type (specs->type);
7431 break;
7432 case cts_int:
7433 gcc_assert (!(specs->long_p && specs->short_p));
7434 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7435 if (specs->long_long_p)
7436 specs->type = (specs->unsigned_p
7437 ? long_long_unsigned_type_node
7438 : long_long_integer_type_node);
7439 else if (specs->long_p)
7440 specs->type = (specs->unsigned_p
7441 ? long_unsigned_type_node
7442 : long_integer_type_node);
7443 else if (specs->short_p)
7444 specs->type = (specs->unsigned_p
7445 ? short_unsigned_type_node
7446 : short_integer_type_node);
7447 else
7448 specs->type = (specs->unsigned_p
7449 ? unsigned_type_node
7450 : integer_type_node);
7451 if (specs->complex_p)
7453 if (pedantic)
7454 pedwarn ("ISO C does not support complex integer types");
7455 specs->type = build_complex_type (specs->type);
7457 break;
7458 case cts_float:
7459 gcc_assert (!specs->long_p && !specs->short_p
7460 && !specs->signed_p && !specs->unsigned_p);
7461 specs->type = (specs->complex_p
7462 ? complex_float_type_node
7463 : float_type_node);
7464 break;
7465 case cts_double:
7466 gcc_assert (!specs->long_long_p && !specs->short_p
7467 && !specs->signed_p && !specs->unsigned_p);
7468 if (specs->long_p)
7470 specs->type = (specs->complex_p
7471 ? complex_long_double_type_node
7472 : long_double_type_node);
7474 else
7476 specs->type = (specs->complex_p
7477 ? complex_double_type_node
7478 : double_type_node);
7480 break;
7481 default:
7482 gcc_unreachable ();
7485 return specs;
7488 /* Synthesize a function which calls all the global ctors or global
7489 dtors in this file. This is only used for targets which do not
7490 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7491 static void
7492 build_cdtor (int method_type, tree cdtors)
7494 tree body = 0;
7496 if (!cdtors)
7497 return;
7499 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7500 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7501 &body);
7503 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7506 /* Perform final processing on one file scope's declarations (or the
7507 external scope's declarations), GLOBALS. */
7508 static void
7509 c_write_global_declarations_1 (tree globals)
7511 size_t len = list_length (globals);
7512 tree *vec = XNEWVEC (tree, len);
7513 size_t i;
7514 tree decl;
7516 /* Process the decls in the order they were written. */
7517 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7519 vec[i] = decl;
7520 /* Check for used but undefined static functions using the C
7521 standard's definition of "used", and set TREE_NO_WARNING so
7522 that check_global_declarations doesn't repeat the check. */
7523 if (TREE_CODE (decl) == FUNCTION_DECL
7524 && DECL_INITIAL (decl) == 0
7525 && DECL_EXTERNAL (decl)
7526 && !TREE_PUBLIC (decl)
7527 && C_DECL_USED (decl))
7529 pedwarn ("%J%qF used but never defined", decl, decl);
7530 TREE_NO_WARNING (decl) = 1;
7534 wrapup_global_declarations (vec, len);
7535 check_global_declarations (vec, len);
7537 free (vec);
7540 void
7541 c_write_global_declarations (void)
7543 tree ext_block, t;
7545 /* We don't want to do this if generating a PCH. */
7546 if (pch_file)
7547 return;
7549 /* Don't waste time on further processing if -fsyntax-only or we've
7550 encountered errors. */
7551 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7552 return;
7554 /* Close the external scope. */
7555 ext_block = pop_scope ();
7556 external_scope = 0;
7557 gcc_assert (!current_scope);
7559 /* Process all file scopes in this compilation, and the external_scope,
7560 through wrapup_global_declarations and check_global_declarations. */
7561 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7562 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7563 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7565 /* Generate functions to call static constructors and destructors
7566 for targets that do not support .ctors/.dtors sections. These
7567 functions have magic names which are detected by collect2. */
7568 build_cdtor ('I', static_ctors); static_ctors = 0;
7569 build_cdtor ('D', static_dtors); static_dtors = 0;
7571 /* We're done parsing; proceed to optimize and emit assembly.
7572 FIXME: shouldn't be the front end's responsibility to call this. */
7573 cgraph_optimize ();
7576 #include "gt-c-decl.h"