* ca.po, rw.po: Remove.
[official-gcc.git] / gcc / c-decl.c
blob7cd79206dad0b2dd5f26d97fced288c1d61fdc3e
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, 2007 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* Process declarations and symbol lookup for C front end.
22 Also constructs types; the standard scalar types at initialization,
23 and structure, union, array and enum types when they are declared. */
25 /* ??? not all decl nodes are given the most useful possible
26 line numbers. For example, the CONST_DECLs for enum values. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "input.h"
32 #include "tm.h"
33 #include "intl.h"
34 #include "tree.h"
35 #include "tree-inline.h"
36 #include "rtl.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "output.h"
40 #include "expr.h"
41 #include "c-tree.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "tm_p.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "debug.h"
48 #include "opts.h"
49 #include "timevar.h"
50 #include "c-common.h"
51 #include "c-pragma.h"
52 #include "langhooks.h"
53 #include "tree-mudflap.h"
54 #include "tree-gimple.h"
55 #include "diagnostic.h"
56 #include "tree-dump.h"
57 #include "cgraph.h"
58 #include "hashtab.h"
59 #include "libfuncs.h"
60 #include "except.h"
61 #include "langhooks-def.h"
62 #include "pointer-set.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 saw [*] in this scope. Used to give an error messages
334 if these appears in a function definition. */
335 BOOL_BITFIELD had_vla_unspec : 1;
337 /* True if we already complained about forward parameter decls
338 in this scope. This prevents double warnings on
339 foo (int a; int b; ...) */
340 BOOL_BITFIELD warned_forward_parm_decls : 1;
342 /* True if this is the outermost block scope of a function body.
343 This scope contains the parameters, the local variables declared
344 in the outermost block, and all the labels (except those in
345 nested functions, or declared at block scope with __label__). */
346 BOOL_BITFIELD function_body : 1;
348 /* True means make a BLOCK for this scope no matter what. */
349 BOOL_BITFIELD keep : 1;
352 /* The scope currently in effect. */
354 static GTY(()) struct c_scope *current_scope;
356 /* The innermost function scope. Ordinary (not explicitly declared)
357 labels, bindings to error_mark_node, and the lazily-created
358 bindings of __func__ and its friends get this scope. */
360 static GTY(()) struct c_scope *current_function_scope;
362 /* The C file scope. This is reset for each input translation unit. */
364 static GTY(()) struct c_scope *file_scope;
366 /* The outermost scope. This is used for all declarations with
367 external linkage, and only these, hence the name. */
369 static GTY(()) struct c_scope *external_scope;
371 /* A chain of c_scope structures awaiting reuse. */
373 static GTY((deletable)) struct c_scope *scope_freelist;
375 /* A chain of c_binding structures awaiting reuse. */
377 static GTY((deletable)) struct c_binding *binding_freelist;
379 /* Append VAR to LIST in scope SCOPE. */
380 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
381 struct c_scope *s_ = (scope); \
382 tree d_ = (decl); \
383 if (s_->list##_last) \
384 TREE_CHAIN (s_->list##_last) = d_; \
385 else \
386 s_->list = d_; \
387 s_->list##_last = d_; \
388 } while (0)
390 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
391 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
392 struct c_scope *t_ = (tscope); \
393 struct c_scope *f_ = (fscope); \
394 if (t_->to##_last) \
395 TREE_CHAIN (t_->to##_last) = f_->from; \
396 else \
397 t_->to = f_->from; \
398 t_->to##_last = f_->from##_last; \
399 } while (0)
401 /* True means unconditionally make a BLOCK for the next scope pushed. */
403 static bool keep_next_level_flag;
405 /* True means the next call to push_scope will be the outermost scope
406 of a function body, so do not push a new scope, merely cease
407 expecting parameter decls. */
409 static bool next_is_function_body;
411 /* Functions called automatically at the beginning and end of execution. */
413 static GTY(()) tree static_ctors;
414 static GTY(()) tree static_dtors;
416 /* Forward declarations. */
417 static tree lookup_name_in_scope (tree, struct c_scope *);
418 static tree c_make_fname_decl (tree, int);
419 static tree grokdeclarator (const struct c_declarator *,
420 struct c_declspecs *,
421 enum decl_context, bool, tree *);
422 static tree grokparms (struct c_arg_info *, bool);
423 static void layout_array_type (tree);
425 /* T is a statement. Add it to the statement-tree. This is the
426 C/ObjC version--C++ has a slightly different version of this
427 function. */
429 tree
430 add_stmt (tree t)
432 enum tree_code code = TREE_CODE (t);
434 if (EXPR_P (t) && code != LABEL_EXPR)
436 if (!EXPR_HAS_LOCATION (t))
437 SET_EXPR_LOCATION (t, input_location);
440 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
441 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
443 /* Add T to the statement-tree. Non-side-effect statements need to be
444 recorded during statement expressions. */
445 append_to_statement_list_force (t, &cur_stmt_list);
447 return t;
450 /* States indicating how grokdeclarator() should handle declspecs marked
451 with __attribute__((deprecated)). An object declared as
452 __attribute__((deprecated)) suppresses warnings of uses of other
453 deprecated items. */
455 enum deprecated_states {
456 DEPRECATED_NORMAL,
457 DEPRECATED_SUPPRESS
460 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
462 void
463 c_print_identifier (FILE *file, tree node, int indent)
465 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
466 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
467 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
468 if (C_IS_RESERVED_WORD (node))
470 tree rid = ridpointers[C_RID_CODE (node)];
471 indent_to (file, indent + 4);
472 fprintf (file, "rid %p \"%s\"",
473 (void *) rid, IDENTIFIER_POINTER (rid));
477 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
478 which may be any of several kinds of DECL or TYPE or error_mark_node,
479 in the scope SCOPE. */
480 static void
481 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
483 struct c_binding *b, **here;
485 if (binding_freelist)
487 b = binding_freelist;
488 binding_freelist = b->prev;
490 else
491 b = GGC_NEW (struct c_binding);
493 b->shadowed = 0;
494 b->decl = decl;
495 b->id = name;
496 b->depth = scope->depth;
497 b->invisible = invisible;
498 b->nested = nested;
499 b->inner_comp = 0;
501 b->type = 0;
503 b->prev = scope->bindings;
504 scope->bindings = b;
506 if (!name)
507 return;
509 switch (TREE_CODE (decl))
511 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
512 case ENUMERAL_TYPE:
513 case UNION_TYPE:
514 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
515 case VAR_DECL:
516 case FUNCTION_DECL:
517 case TYPE_DECL:
518 case CONST_DECL:
519 case PARM_DECL:
520 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
522 default:
523 gcc_unreachable ();
526 /* Locate the appropriate place in the chain of shadowed decls
527 to insert this binding. Normally, scope == current_scope and
528 this does nothing. */
529 while (*here && (*here)->depth > scope->depth)
530 here = &(*here)->shadowed;
532 b->shadowed = *here;
533 *here = b;
536 /* Clear the binding structure B, stick it on the binding_freelist,
537 and return the former value of b->prev. This is used by pop_scope
538 and get_parm_info to iterate destructively over all the bindings
539 from a given scope. */
540 static struct c_binding *
541 free_binding_and_advance (struct c_binding *b)
543 struct c_binding *prev = b->prev;
545 memset (b, 0, sizeof (struct c_binding));
546 b->prev = binding_freelist;
547 binding_freelist = b;
549 return prev;
553 /* Hook called at end of compilation to assume 1 elt
554 for a file-scope tentative array defn that wasn't complete before. */
556 void
557 c_finish_incomplete_decl (tree decl)
559 if (TREE_CODE (decl) == VAR_DECL)
561 tree type = TREE_TYPE (decl);
562 if (type != error_mark_node
563 && TREE_CODE (type) == ARRAY_TYPE
564 && !DECL_EXTERNAL (decl)
565 && TYPE_DOMAIN (type) == 0)
567 warning (0, "array %q+D assumed to have one element", decl);
569 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
571 layout_decl (decl, 0);
576 /* The Objective-C front-end often needs to determine the current scope. */
578 void *
579 objc_get_current_scope (void)
581 return current_scope;
584 /* The following function is used only by Objective-C. It needs to live here
585 because it accesses the innards of c_scope. */
587 void
588 objc_mark_locals_volatile (void *enclosing_blk)
590 struct c_scope *scope;
591 struct c_binding *b;
593 for (scope = current_scope;
594 scope && scope != enclosing_blk;
595 scope = scope->outer)
597 for (b = scope->bindings; b; b = b->prev)
598 objc_volatilize_decl (b->decl);
600 /* Do not climb up past the current function. */
601 if (scope->function_body)
602 break;
606 /* Nonzero if we are currently in file scope. */
609 global_bindings_p (void)
611 return current_scope == file_scope && !c_override_global_bindings_to_false;
614 void
615 keep_next_level (void)
617 keep_next_level_flag = true;
620 /* Identify this scope as currently being filled with parameters. */
622 void
623 declare_parm_level (void)
625 current_scope->parm_flag = true;
628 void
629 push_scope (void)
631 if (next_is_function_body)
633 /* This is the transition from the parameters to the top level
634 of the function body. These are the same scope
635 (C99 6.2.1p4,6) so we do not push another scope structure.
636 next_is_function_body is set only by store_parm_decls, which
637 in turn is called when and only when we are about to
638 encounter the opening curly brace for the function body.
640 The outermost block of a function always gets a BLOCK node,
641 because the debugging output routines expect that each
642 function has at least one BLOCK. */
643 current_scope->parm_flag = false;
644 current_scope->function_body = true;
645 current_scope->keep = true;
646 current_scope->outer_function = current_function_scope;
647 current_function_scope = current_scope;
649 keep_next_level_flag = false;
650 next_is_function_body = false;
652 else
654 struct c_scope *scope;
655 if (scope_freelist)
657 scope = scope_freelist;
658 scope_freelist = scope->outer;
660 else
661 scope = GGC_CNEW (struct c_scope);
663 scope->keep = keep_next_level_flag;
664 scope->outer = current_scope;
665 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
667 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
668 possible. */
669 if (current_scope && scope->depth == 0)
671 scope->depth--;
672 sorry ("GCC supports only %u nested scopes", scope->depth);
675 current_scope = scope;
676 keep_next_level_flag = false;
680 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
682 static void
683 set_type_context (tree type, tree context)
685 for (type = TYPE_MAIN_VARIANT (type); type;
686 type = TYPE_NEXT_VARIANT (type))
687 TYPE_CONTEXT (type) = context;
690 /* Exit a scope. Restore the state of the identifier-decl mappings
691 that were in effect when this scope was entered. Return a BLOCK
692 node containing all the DECLs in this scope that are of interest
693 to debug info generation. */
695 tree
696 pop_scope (void)
698 struct c_scope *scope = current_scope;
699 tree block, context, p;
700 struct c_binding *b;
702 bool functionbody = scope->function_body;
703 bool keep = functionbody || scope->keep || scope->bindings;
705 c_end_vm_scope (scope->depth);
707 /* If appropriate, create a BLOCK to record the decls for the life
708 of this function. */
709 block = 0;
710 if (keep)
712 block = make_node (BLOCK);
713 BLOCK_SUBBLOCKS (block) = scope->blocks;
714 TREE_USED (block) = 1;
716 /* In each subblock, record that this is its superior. */
717 for (p = scope->blocks; p; p = TREE_CHAIN (p))
718 BLOCK_SUPERCONTEXT (p) = block;
720 BLOCK_VARS (block) = 0;
723 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
724 scope must be set so that they point to the appropriate
725 construct, i.e. either to the current FUNCTION_DECL node, or
726 else to the BLOCK node we just constructed.
728 Note that for tagged types whose scope is just the formal
729 parameter list for some function type specification, we can't
730 properly set their TYPE_CONTEXTs here, because we don't have a
731 pointer to the appropriate FUNCTION_TYPE node readily available
732 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
733 type nodes get set in `grokdeclarator' as soon as we have created
734 the FUNCTION_TYPE node which will represent the "scope" for these
735 "parameter list local" tagged types. */
736 if (scope->function_body)
737 context = current_function_decl;
738 else if (scope == file_scope)
740 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
741 TREE_CHAIN (file_decl) = all_translation_units;
742 all_translation_units = file_decl;
743 context = file_decl;
745 else
746 context = block;
748 /* Clear all bindings in this scope. */
749 for (b = scope->bindings; b; b = free_binding_and_advance (b))
751 p = b->decl;
752 switch (TREE_CODE (p))
754 case LABEL_DECL:
755 /* Warnings for unused labels, errors for undefined labels. */
756 if (TREE_USED (p) && !DECL_INITIAL (p))
758 error ("label %q+D used but not defined", p);
759 DECL_INITIAL (p) = error_mark_node;
761 else if (!TREE_USED (p) && warn_unused_label)
763 if (DECL_INITIAL (p))
764 warning (0, "label %q+D defined but not used", p);
765 else
766 warning (0, "label %q+D declared but not defined", p);
768 /* Labels go in BLOCK_VARS. */
769 TREE_CHAIN (p) = BLOCK_VARS (block);
770 BLOCK_VARS (block) = p;
771 gcc_assert (I_LABEL_BINDING (b->id) == b);
772 I_LABEL_BINDING (b->id) = b->shadowed;
773 break;
775 case ENUMERAL_TYPE:
776 case UNION_TYPE:
777 case RECORD_TYPE:
778 set_type_context (p, context);
780 /* Types may not have tag-names, in which case the type
781 appears in the bindings list with b->id NULL. */
782 if (b->id)
784 gcc_assert (I_TAG_BINDING (b->id) == b);
785 I_TAG_BINDING (b->id) = b->shadowed;
787 break;
789 case FUNCTION_DECL:
790 /* Propagate TREE_ADDRESSABLE from nested functions to their
791 containing functions. */
792 if (!TREE_ASM_WRITTEN (p)
793 && DECL_INITIAL (p) != 0
794 && TREE_ADDRESSABLE (p)
795 && DECL_ABSTRACT_ORIGIN (p) != 0
796 && DECL_ABSTRACT_ORIGIN (p) != p)
797 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
798 if (!DECL_EXTERNAL (p)
799 && DECL_INITIAL (p) == 0)
801 error ("nested function %q+D declared but never defined", p);
802 undef_nested_function = true;
804 goto common_symbol;
806 case VAR_DECL:
807 /* Warnings for unused variables. */
808 if (!TREE_USED (p)
809 && !TREE_NO_WARNING (p)
810 && !DECL_IN_SYSTEM_HEADER (p)
811 && DECL_NAME (p)
812 && !DECL_ARTIFICIAL (p)
813 && scope != file_scope
814 && scope != external_scope)
815 warning (OPT_Wunused_variable, "unused variable %q+D", p);
817 if (b->inner_comp)
819 error ("type of array %q+D completed incompatibly with"
820 " implicit initialization", p);
823 /* Fall through. */
824 case TYPE_DECL:
825 case CONST_DECL:
826 common_symbol:
827 /* All of these go in BLOCK_VARS, but only if this is the
828 binding in the home scope. */
829 if (!b->nested)
831 TREE_CHAIN (p) = BLOCK_VARS (block);
832 BLOCK_VARS (block) = p;
834 /* If this is the file scope, and we are processing more
835 than one translation unit in this compilation, set
836 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
837 This makes same_translation_unit_p work, and causes
838 static declarations to be given disambiguating suffixes. */
839 if (scope == file_scope && num_in_fnames > 1)
841 DECL_CONTEXT (p) = context;
842 if (TREE_CODE (p) == TYPE_DECL)
843 set_type_context (TREE_TYPE (p), context);
846 /* Fall through. */
847 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
848 already been put there by store_parm_decls. Unused-
849 parameter warnings are handled by function.c.
850 error_mark_node obviously does not go in BLOCK_VARS and
851 does not get unused-variable warnings. */
852 case PARM_DECL:
853 case ERROR_MARK:
854 /* It is possible for a decl not to have a name. We get
855 here with b->id NULL in this case. */
856 if (b->id)
858 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
859 I_SYMBOL_BINDING (b->id) = b->shadowed;
860 if (b->shadowed && b->shadowed->type)
861 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
863 break;
865 default:
866 gcc_unreachable ();
871 /* Dispose of the block that we just made inside some higher level. */
872 if ((scope->function_body || scope == file_scope) && context)
874 DECL_INITIAL (context) = block;
875 BLOCK_SUPERCONTEXT (block) = context;
877 else if (scope->outer)
879 if (block)
880 SCOPE_LIST_APPEND (scope->outer, blocks, block);
881 /* If we did not make a block for the scope just exited, any
882 blocks made for inner scopes must be carried forward so they
883 will later become subblocks of something else. */
884 else if (scope->blocks)
885 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
888 /* Pop the current scope, and free the structure for reuse. */
889 current_scope = scope->outer;
890 if (scope->function_body)
891 current_function_scope = scope->outer_function;
893 memset (scope, 0, sizeof (struct c_scope));
894 scope->outer = scope_freelist;
895 scope_freelist = scope;
897 return block;
900 void
901 push_file_scope (void)
903 tree decl;
905 if (file_scope)
906 return;
908 push_scope ();
909 file_scope = current_scope;
911 start_fname_decls ();
913 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
914 bind (DECL_NAME (decl), decl, file_scope,
915 /*invisible=*/false, /*nested=*/true);
918 void
919 pop_file_scope (void)
921 /* In case there were missing closebraces, get us back to the global
922 binding level. */
923 while (current_scope != file_scope)
924 pop_scope ();
926 /* __FUNCTION__ is defined at file scope (""). This
927 call may not be necessary as my tests indicate it
928 still works without it. */
929 finish_fname_decls ();
931 /* This is the point to write out a PCH if we're doing that.
932 In that case we do not want to do anything else. */
933 if (pch_file)
935 c_common_write_pch ();
936 return;
939 /* Pop off the file scope and close this translation unit. */
940 pop_scope ();
941 file_scope = 0;
943 maybe_apply_pending_pragma_weaks ();
944 cgraph_finalize_compilation_unit ();
947 /* Insert BLOCK at the end of the list of subblocks of the current
948 scope. This is used when a BIND_EXPR is expanded, to handle the
949 BLOCK node inside the BIND_EXPR. */
951 void
952 insert_block (tree block)
954 TREE_USED (block) = 1;
955 SCOPE_LIST_APPEND (current_scope, blocks, block);
958 /* Push a definition or a declaration of struct, union or enum tag "name".
959 "type" should be the type node.
960 We assume that the tag "name" is not already defined.
962 Note that the definition may really be just a forward reference.
963 In that case, the TYPE_SIZE will be zero. */
965 static void
966 pushtag (tree name, tree type)
968 /* Record the identifier as the type's name if it has none. */
969 if (name && !TYPE_NAME (type))
970 TYPE_NAME (type) = name;
971 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
973 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
974 tagged type we just added to the current scope. This fake
975 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
976 to output a representation of a tagged type, and it also gives
977 us a convenient place to record the "scope start" address for the
978 tagged type. */
980 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
982 /* An approximation for now, so we can tell this is a function-scope tag.
983 This will be updated in pop_scope. */
984 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
987 /* Subroutine of compare_decls. Allow harmless mismatches in return
988 and argument types provided that the type modes match. This function
989 return a unified type given a suitable match, and 0 otherwise. */
991 static tree
992 match_builtin_function_types (tree newtype, tree oldtype)
994 tree newrettype, oldrettype;
995 tree newargs, oldargs;
996 tree trytype, tryargs;
998 /* Accept the return type of the new declaration if same modes. */
999 oldrettype = TREE_TYPE (oldtype);
1000 newrettype = TREE_TYPE (newtype);
1002 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1003 return 0;
1005 oldargs = TYPE_ARG_TYPES (oldtype);
1006 newargs = TYPE_ARG_TYPES (newtype);
1007 tryargs = newargs;
1009 while (oldargs || newargs)
1011 if (!oldargs
1012 || !newargs
1013 || !TREE_VALUE (oldargs)
1014 || !TREE_VALUE (newargs)
1015 || TYPE_MODE (TREE_VALUE (oldargs))
1016 != TYPE_MODE (TREE_VALUE (newargs)))
1017 return 0;
1019 oldargs = TREE_CHAIN (oldargs);
1020 newargs = TREE_CHAIN (newargs);
1023 trytype = build_function_type (newrettype, tryargs);
1024 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1027 /* Subroutine of diagnose_mismatched_decls. Check for function type
1028 mismatch involving an empty arglist vs a nonempty one and give clearer
1029 diagnostics. */
1030 static void
1031 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1032 tree newtype, tree oldtype)
1034 tree t;
1036 if (TREE_CODE (olddecl) != FUNCTION_DECL
1037 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1038 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1040 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1041 return;
1043 t = TYPE_ARG_TYPES (oldtype);
1044 if (t == 0)
1045 t = TYPE_ARG_TYPES (newtype);
1046 for (; t; t = TREE_CHAIN (t))
1048 tree type = TREE_VALUE (t);
1050 if (TREE_CHAIN (t) == 0
1051 && TYPE_MAIN_VARIANT (type) != void_type_node)
1053 inform ("a parameter list with an ellipsis can%'t match "
1054 "an empty parameter name list declaration");
1055 break;
1058 if (c_type_promotes_to (type) != type)
1060 inform ("an argument type that has a default promotion can%'t match "
1061 "an empty parameter name list declaration");
1062 break;
1067 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1068 old-style function definition, NEWDECL is a prototype declaration.
1069 Diagnose inconsistencies in the argument list. Returns TRUE if
1070 the prototype is compatible, FALSE if not. */
1071 static bool
1072 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1074 tree newargs, oldargs;
1075 int i;
1077 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1079 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1080 newargs = TYPE_ARG_TYPES (newtype);
1081 i = 1;
1083 for (;;)
1085 tree oldargtype = TREE_VALUE (oldargs);
1086 tree newargtype = TREE_VALUE (newargs);
1088 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1089 return false;
1091 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1092 newargtype = TYPE_MAIN_VARIANT (newargtype);
1094 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1095 break;
1097 /* Reaching the end of just one list means the two decls don't
1098 agree on the number of arguments. */
1099 if (END_OF_ARGLIST (oldargtype))
1101 error ("prototype for %q+D declares more arguments "
1102 "than previous old-style definition", newdecl);
1103 return false;
1105 else if (END_OF_ARGLIST (newargtype))
1107 error ("prototype for %q+D declares fewer arguments "
1108 "than previous old-style definition", newdecl);
1109 return false;
1112 /* Type for passing arg must be consistent with that declared
1113 for the arg. */
1114 else if (!comptypes (oldargtype, newargtype))
1116 error ("prototype for %q+D declares argument %d"
1117 " with incompatible type",
1118 newdecl, i);
1119 return false;
1122 oldargs = TREE_CHAIN (oldargs);
1123 newargs = TREE_CHAIN (newargs);
1124 i++;
1127 /* If we get here, no errors were found, but do issue a warning
1128 for this poor-style construct. */
1129 warning (0, "prototype for %q+D follows non-prototype definition",
1130 newdecl);
1131 return true;
1132 #undef END_OF_ARGLIST
1135 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1136 first in a pair of mismatched declarations, using the diagnostic
1137 function DIAG. */
1138 static void
1139 locate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2))
1141 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1143 else if (DECL_INITIAL (decl))
1144 diag (G_("previous definition of %q+D was here"), decl);
1145 else if (C_DECL_IMPLICIT (decl))
1146 diag (G_("previous implicit declaration of %q+D was here"), decl);
1147 else
1148 diag (G_("previous declaration of %q+D was here"), decl);
1151 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1152 Returns true if the caller should proceed to merge the two, false
1153 if OLDDECL should simply be discarded. As a side effect, issues
1154 all necessary diagnostics for invalid or poor-style combinations.
1155 If it returns true, writes the types of NEWDECL and OLDDECL to
1156 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1157 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1159 static bool
1160 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1161 tree *newtypep, tree *oldtypep)
1163 tree newtype, oldtype;
1164 bool pedwarned = false;
1165 bool warned = false;
1166 bool retval = true;
1168 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1169 && DECL_EXTERNAL (DECL))
1171 /* If we have error_mark_node for either decl or type, just discard
1172 the previous decl - we're in an error cascade already. */
1173 if (olddecl == error_mark_node || newdecl == error_mark_node)
1174 return false;
1175 *oldtypep = oldtype = TREE_TYPE (olddecl);
1176 *newtypep = newtype = TREE_TYPE (newdecl);
1177 if (oldtype == error_mark_node || newtype == error_mark_node)
1178 return false;
1180 /* Two different categories of symbol altogether. This is an error
1181 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1182 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1184 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1185 && DECL_BUILT_IN (olddecl)
1186 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1188 error ("%q+D redeclared as different kind of symbol", newdecl);
1189 locate_old_decl (olddecl, error);
1191 else if (TREE_PUBLIC (newdecl))
1192 warning (0, "built-in function %q+D declared as non-function",
1193 newdecl);
1194 else
1195 warning (OPT_Wshadow, "declaration of %q+D shadows "
1196 "a built-in function", newdecl);
1197 return false;
1200 /* Enumerators have no linkage, so may only be declared once in a
1201 given scope. */
1202 if (TREE_CODE (olddecl) == CONST_DECL)
1204 error ("redeclaration of enumerator %q+D", newdecl);
1205 locate_old_decl (olddecl, error);
1206 return false;
1209 if (!comptypes (oldtype, newtype))
1211 if (TREE_CODE (olddecl) == FUNCTION_DECL
1212 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1214 /* Accept harmless mismatch in function types.
1215 This is for the ffs and fprintf builtins. */
1216 tree trytype = match_builtin_function_types (newtype, oldtype);
1218 if (trytype && comptypes (newtype, trytype))
1219 *oldtypep = oldtype = trytype;
1220 else
1222 /* If types don't match for a built-in, throw away the
1223 built-in. No point in calling locate_old_decl here, it
1224 won't print anything. */
1225 warning (0, "conflicting types for built-in function %q+D",
1226 newdecl);
1227 return false;
1230 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1231 && DECL_IS_BUILTIN (olddecl))
1233 /* A conflicting function declaration for a predeclared
1234 function that isn't actually built in. Objective C uses
1235 these. The new declaration silently overrides everything
1236 but the volatility (i.e. noreturn) indication. See also
1237 below. FIXME: Make Objective C use normal builtins. */
1238 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1239 return false;
1241 /* Permit void foo (...) to match int foo (...) if the latter is
1242 the definition and implicit int was used. See
1243 c-torture/compile/920625-2.c. */
1244 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1245 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1246 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1247 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1249 pedwarn ("conflicting types for %q+D", newdecl);
1250 /* Make sure we keep void as the return type. */
1251 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1252 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1253 pedwarned = true;
1255 /* Permit void foo (...) to match an earlier call to foo (...) with
1256 no declared type (thus, implicitly int). */
1257 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1258 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1259 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1260 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1262 pedwarn ("conflicting types for %q+D", newdecl);
1263 /* Make sure we keep void as the return type. */
1264 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1265 pedwarned = true;
1267 else
1269 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1270 error ("conflicting type qualifiers for %q+D", newdecl);
1271 else
1272 error ("conflicting types for %q+D", newdecl);
1273 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1274 locate_old_decl (olddecl, error);
1275 return false;
1279 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1280 but silently ignore the redeclaration if either is in a system
1281 header. (Conflicting redeclarations were handled above.) */
1282 if (TREE_CODE (newdecl) == TYPE_DECL)
1284 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1285 return true; /* Allow OLDDECL to continue in use. */
1287 error ("redefinition of typedef %q+D", newdecl);
1288 locate_old_decl (olddecl, error);
1289 return false;
1292 /* Function declarations can either be 'static' or 'extern' (no
1293 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1294 can never conflict with each other on account of linkage (6.2.2p4).
1295 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1296 two definitions if one is 'extern inline' and one is not. The non-
1297 extern-inline definition supersedes the extern-inline definition. */
1299 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1301 /* If you declare a built-in function name as static, or
1302 define the built-in with an old-style definition (so we
1303 can't validate the argument list) the built-in definition is
1304 overridden, but optionally warn this was a bad choice of name. */
1305 if (DECL_BUILT_IN (olddecl)
1306 && !C_DECL_DECLARED_BUILTIN (olddecl)
1307 && (!TREE_PUBLIC (newdecl)
1308 || (DECL_INITIAL (newdecl)
1309 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1311 warning (OPT_Wshadow, "declaration of %q+D shadows "
1312 "a built-in function", newdecl);
1313 /* Discard the old built-in function. */
1314 return false;
1317 if (DECL_INITIAL (newdecl))
1319 if (DECL_INITIAL (olddecl))
1321 /* If both decls are in the same TU and the new declaration
1322 isn't overriding an extern inline reject the new decl.
1323 When we handle c99 style inline rules we'll want to reject
1324 the following:
1326 DECL_EXTERN_INLINE (olddecl)
1327 && !DECL_EXTERN_INLINE (newdecl)
1329 if they're in the same translation unit. Until we implement
1330 the full semantics we accept the construct. */
1331 if (!(DECL_EXTERN_INLINE (olddecl)
1332 && !DECL_EXTERN_INLINE (newdecl))
1333 && same_translation_unit_p (newdecl, olddecl))
1335 error ("redefinition of %q+D", newdecl);
1336 locate_old_decl (olddecl, error);
1337 return false;
1341 /* If we have a prototype after an old-style function definition,
1342 the argument types must be checked specially. */
1343 else if (DECL_INITIAL (olddecl)
1344 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1345 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1346 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1348 locate_old_decl (olddecl, error);
1349 return false;
1351 /* A non-static declaration (even an "extern") followed by a
1352 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1353 The same is true for a static forward declaration at block
1354 scope followed by a non-static declaration/definition at file
1355 scope. Static followed by non-static at the same scope is
1356 not undefined behavior, and is the most convenient way to get
1357 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1358 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1359 we do diagnose it if -Wtraditional. */
1360 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1362 /* Two exceptions to the rule. If olddecl is an extern
1363 inline, or a predeclared function that isn't actually
1364 built in, newdecl silently overrides olddecl. The latter
1365 occur only in Objective C; see also above. (FIXME: Make
1366 Objective C use normal builtins.) */
1367 if (!DECL_IS_BUILTIN (olddecl)
1368 && !DECL_EXTERN_INLINE (olddecl))
1370 error ("static declaration of %q+D follows "
1371 "non-static declaration", newdecl);
1372 locate_old_decl (olddecl, error);
1374 return false;
1376 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1378 if (DECL_CONTEXT (olddecl))
1380 error ("non-static declaration of %q+D follows "
1381 "static declaration", newdecl);
1382 locate_old_decl (olddecl, error);
1383 return false;
1385 else if (warn_traditional)
1387 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1388 "follows static declaration", newdecl);
1389 warned = true;
1393 else if (TREE_CODE (newdecl) == VAR_DECL)
1395 /* Only variables can be thread-local, and all declarations must
1396 agree on this property. */
1397 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1399 /* Nothing to check. Since OLDDECL is marked threadprivate
1400 and NEWDECL does not have a thread-local attribute, we
1401 will merge the threadprivate attribute into NEWDECL. */
1404 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1406 if (DECL_THREAD_LOCAL_P (newdecl))
1407 error ("thread-local declaration of %q+D follows "
1408 "non-thread-local declaration", newdecl);
1409 else
1410 error ("non-thread-local declaration of %q+D follows "
1411 "thread-local declaration", newdecl);
1413 locate_old_decl (olddecl, error);
1414 return false;
1417 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1418 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1420 error ("redefinition of %q+D", newdecl);
1421 locate_old_decl (olddecl, error);
1422 return false;
1425 /* Objects declared at file scope: if the first declaration had
1426 external linkage (even if it was an external reference) the
1427 second must have external linkage as well, or the behavior is
1428 undefined. If the first declaration had internal linkage, then
1429 the second must too, or else be an external reference (in which
1430 case the composite declaration still has internal linkage).
1431 As for function declarations, we warn about the static-then-
1432 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1433 if (DECL_FILE_SCOPE_P (newdecl)
1434 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1436 if (DECL_EXTERNAL (newdecl))
1438 if (!DECL_FILE_SCOPE_P (olddecl))
1440 error ("extern declaration of %q+D follows "
1441 "declaration with no linkage", newdecl);
1442 locate_old_decl (olddecl, error);
1443 return false;
1445 else if (warn_traditional)
1447 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1448 "follows static declaration", newdecl);
1449 warned = true;
1452 else
1454 if (TREE_PUBLIC (newdecl))
1455 error ("non-static declaration of %q+D follows "
1456 "static declaration", newdecl);
1457 else
1458 error ("static declaration of %q+D follows "
1459 "non-static declaration", newdecl);
1461 locate_old_decl (olddecl, error);
1462 return false;
1465 /* Two objects with the same name declared at the same block
1466 scope must both be external references (6.7p3). */
1467 else if (!DECL_FILE_SCOPE_P (newdecl))
1469 if (DECL_EXTERNAL (newdecl))
1471 /* Extern with initializer at block scope, which will
1472 already have received an error. */
1474 else if (DECL_EXTERNAL (olddecl))
1476 error ("declaration of %q+D with no linkage follows "
1477 "extern declaration", newdecl);
1478 locate_old_decl (olddecl, error);
1480 else
1482 error ("redeclaration of %q+D with no linkage", newdecl);
1483 locate_old_decl (olddecl, error);
1486 return false;
1490 /* warnings */
1491 /* All decls must agree on a visibility. */
1492 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1493 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1494 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1496 warning (0, "redeclaration of %q+D with different visibility "
1497 "(old visibility preserved)", newdecl);
1498 warned = true;
1501 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1503 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1504 if (DECL_DECLARED_INLINE_P (newdecl)
1505 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1507 warning (OPT_Wattributes, "inline declaration of %qD follows "
1508 "declaration with attribute noinline", newdecl);
1509 warned = true;
1511 else if (DECL_DECLARED_INLINE_P (olddecl)
1512 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1514 warning (OPT_Wattributes, "declaration of %q+D with attribute "
1515 "noinline follows inline declaration ", newdecl);
1516 warned = true;
1519 /* Inline declaration after use or definition.
1520 ??? Should we still warn about this now we have unit-at-a-time
1521 mode and can get it right?
1522 Definitely don't complain if the decls are in different translation
1523 units. */
1524 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1525 && same_translation_unit_p (olddecl, newdecl))
1527 if (TREE_USED (olddecl))
1529 warning (0, "%q+D declared inline after being called", olddecl);
1530 warned = true;
1532 else if (DECL_INITIAL (olddecl))
1534 warning (0, "%q+D declared inline after its definition", olddecl);
1535 warned = true;
1539 else /* PARM_DECL, VAR_DECL */
1541 /* Redeclaration of a parameter is a constraint violation (this is
1542 not explicitly stated, but follows from C99 6.7p3 [no more than
1543 one declaration of the same identifier with no linkage in the
1544 same scope, except type tags] and 6.2.2p6 [parameters have no
1545 linkage]). We must check for a forward parameter declaration,
1546 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1547 an extension, the mandatory diagnostic for which is handled by
1548 mark_forward_parm_decls. */
1550 if (TREE_CODE (newdecl) == PARM_DECL
1551 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1553 error ("redefinition of parameter %q+D", newdecl);
1554 locate_old_decl (olddecl, error);
1555 return false;
1559 /* Optional warning for completely redundant decls. */
1560 if (!warned && !pedwarned
1561 && warn_redundant_decls
1562 /* Don't warn about a function declaration followed by a
1563 definition. */
1564 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1565 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1566 /* Don't warn about redundant redeclarations of builtins. */
1567 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1568 && !DECL_BUILT_IN (newdecl)
1569 && DECL_BUILT_IN (olddecl)
1570 && !C_DECL_DECLARED_BUILTIN (olddecl))
1571 /* Don't warn about an extern followed by a definition. */
1572 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1573 /* Don't warn about forward parameter decls. */
1574 && !(TREE_CODE (newdecl) == PARM_DECL
1575 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1576 /* Don't warn about a variable definition following a declaration. */
1577 && !(TREE_CODE (newdecl) == VAR_DECL
1578 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1580 warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1581 newdecl);
1582 warned = true;
1585 /* Report location of previous decl/defn in a consistent manner. */
1586 if (warned || pedwarned)
1587 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1589 #undef DECL_EXTERN_INLINE
1591 return retval;
1594 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1595 consistent with OLDDECL, but carries new information. Merge the
1596 new information into OLDDECL. This function issues no
1597 diagnostics. */
1599 static void
1600 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1602 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1603 && DECL_INITIAL (newdecl) != 0);
1604 int new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1605 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1606 int old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1607 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1609 /* For real parm decl following a forward decl, rechain the old decl
1610 in its new location and clear TREE_ASM_WRITTEN (it's not a
1611 forward decl anymore). */
1612 if (TREE_CODE (newdecl) == PARM_DECL
1613 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1615 struct c_binding *b, **here;
1617 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1618 if ((*here)->decl == olddecl)
1619 goto found;
1620 gcc_unreachable ();
1622 found:
1623 b = *here;
1624 *here = b->prev;
1625 b->prev = current_scope->bindings;
1626 current_scope->bindings = b;
1628 TREE_ASM_WRITTEN (olddecl) = 0;
1631 DECL_ATTRIBUTES (newdecl)
1632 = targetm.merge_decl_attributes (olddecl, newdecl);
1634 /* Merge the data types specified in the two decls. */
1635 TREE_TYPE (newdecl)
1636 = TREE_TYPE (olddecl)
1637 = composite_type (newtype, oldtype);
1639 /* Lay the type out, unless already done. */
1640 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1642 if (TREE_TYPE (newdecl) != error_mark_node)
1643 layout_type (TREE_TYPE (newdecl));
1644 if (TREE_CODE (newdecl) != FUNCTION_DECL
1645 && TREE_CODE (newdecl) != TYPE_DECL
1646 && TREE_CODE (newdecl) != CONST_DECL)
1647 layout_decl (newdecl, 0);
1649 else
1651 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1652 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1653 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1654 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1655 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1656 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1658 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1659 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1663 /* Keep the old rtl since we can safely use it. */
1664 if (HAS_RTL_P (olddecl))
1665 COPY_DECL_RTL (olddecl, newdecl);
1667 /* Merge the type qualifiers. */
1668 if (TREE_READONLY (newdecl))
1669 TREE_READONLY (olddecl) = 1;
1671 if (TREE_THIS_VOLATILE (newdecl))
1672 TREE_THIS_VOLATILE (olddecl) = 1;
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 initialization information. */
1687 if (DECL_INITIAL (newdecl) == 0)
1688 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1690 /* Merge the threadprivate attribute. */
1691 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1693 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1694 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1697 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1699 /* Merge the unused-warning information. */
1700 if (DECL_IN_SYSTEM_HEADER (olddecl))
1701 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1702 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1703 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1705 /* Merge the section attribute.
1706 We want to issue an error if the sections conflict but that
1707 must be done later in decl_attributes since we are called
1708 before attributes are assigned. */
1709 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1710 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1712 /* Copy the assembler name.
1713 Currently, it can only be defined in the prototype. */
1714 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1716 /* Use visibility of whichever declaration had it specified */
1717 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1719 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1720 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1723 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1725 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1726 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1727 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1728 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1729 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1730 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1731 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1732 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1733 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1734 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1737 /* Merge the storage class information. */
1738 merge_weak (newdecl, olddecl);
1740 /* For functions, static overrides non-static. */
1741 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1743 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1744 /* This is since we don't automatically
1745 copy the attributes of NEWDECL into OLDDECL. */
1746 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1747 /* If this clears `static', clear it in the identifier too. */
1748 if (!TREE_PUBLIC (olddecl))
1749 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1753 if (DECL_EXTERNAL (newdecl))
1755 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1756 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1758 /* An extern decl does not override previous storage class. */
1759 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1760 if (!DECL_EXTERNAL (newdecl))
1762 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1763 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1766 else
1768 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1769 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1772 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1774 /* If we're redefining a function previously defined as extern
1775 inline, make sure we emit debug info for the inline before we
1776 throw it away, in case it was inlined into a function that
1777 hasn't been written out yet. */
1778 if (new_is_definition && DECL_INITIAL (olddecl))
1780 if (TREE_USED (olddecl)
1781 /* In unit-at-a-time mode we never inline re-defined extern
1782 inline functions. */
1783 && !flag_unit_at_a_time
1784 && cgraph_function_possibly_inlined_p (olddecl))
1785 (*debug_hooks->outlining_inline_function) (olddecl);
1787 /* The new defn must not be inline. */
1788 DECL_INLINE (newdecl) = 0;
1789 DECL_UNINLINABLE (newdecl) = 1;
1791 else
1793 /* If either decl says `inline', this fn is inline, unless
1794 its definition was passed already. */
1795 if (DECL_DECLARED_INLINE_P (newdecl)
1796 || DECL_DECLARED_INLINE_P (olddecl))
1797 DECL_DECLARED_INLINE_P (newdecl) = 1;
1799 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1800 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1803 if (DECL_BUILT_IN (olddecl))
1805 /* If redeclaring a builtin function, it stays built in.
1806 But it gets tagged as having been declared. */
1807 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1808 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1809 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1810 if (new_is_prototype)
1811 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1812 else
1813 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1814 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1817 /* Also preserve various other info from the definition. */
1818 if (!new_is_definition)
1820 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1821 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1822 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1823 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1824 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1826 /* Set DECL_INLINE on the declaration if we've got a body
1827 from which to instantiate. */
1828 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1830 DECL_INLINE (newdecl) = 1;
1831 DECL_ABSTRACT_ORIGIN (newdecl)
1832 = DECL_ABSTRACT_ORIGIN (olddecl);
1835 else
1837 /* If a previous declaration said inline, mark the
1838 definition as inlinable. */
1839 if (DECL_DECLARED_INLINE_P (newdecl)
1840 && !DECL_UNINLINABLE (newdecl))
1841 DECL_INLINE (newdecl) = 1;
1845 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1846 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1848 unsigned olddecl_uid = DECL_UID (olddecl);
1849 tree olddecl_context = DECL_CONTEXT (olddecl);
1851 memcpy ((char *) olddecl + sizeof (struct tree_common),
1852 (char *) newdecl + sizeof (struct tree_common),
1853 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1854 switch (TREE_CODE (olddecl))
1856 case FIELD_DECL:
1857 case VAR_DECL:
1858 case PARM_DECL:
1859 case LABEL_DECL:
1860 case RESULT_DECL:
1861 case CONST_DECL:
1862 case TYPE_DECL:
1863 case FUNCTION_DECL:
1864 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1865 (char *) newdecl + sizeof (struct tree_decl_common),
1866 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1867 break;
1869 default:
1871 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1872 (char *) newdecl + sizeof (struct tree_decl_common),
1873 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1875 DECL_UID (olddecl) = olddecl_uid;
1876 DECL_CONTEXT (olddecl) = olddecl_context;
1879 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1880 so that encode_section_info has a chance to look at the new decl
1881 flags and attributes. */
1882 if (DECL_RTL_SET_P (olddecl)
1883 && (TREE_CODE (olddecl) == FUNCTION_DECL
1884 || (TREE_CODE (olddecl) == VAR_DECL
1885 && TREE_STATIC (olddecl))))
1886 make_decl_rtl (olddecl);
1889 /* Handle when a new declaration NEWDECL has the same name as an old
1890 one OLDDECL in the same binding contour. Prints an error message
1891 if appropriate.
1893 If safely possible, alter OLDDECL to look like NEWDECL, and return
1894 true. Otherwise, return false. */
1896 static bool
1897 duplicate_decls (tree newdecl, tree olddecl)
1899 tree newtype = NULL, oldtype = NULL;
1901 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1903 /* Avoid `unused variable' and other warnings warnings for OLDDECL. */
1904 TREE_NO_WARNING (olddecl) = 1;
1905 return false;
1908 merge_decls (newdecl, olddecl, newtype, oldtype);
1909 return true;
1913 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1914 static void
1915 warn_if_shadowing (tree new_decl)
1917 struct c_binding *b;
1919 /* Shadow warnings wanted? */
1920 if (!warn_shadow
1921 /* No shadow warnings for internally generated vars. */
1922 || DECL_IS_BUILTIN (new_decl)
1923 /* No shadow warnings for vars made for inlining. */
1924 || DECL_FROM_INLINE (new_decl))
1925 return;
1927 /* Is anything being shadowed? Invisible decls do not count. */
1928 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1929 if (b->decl && b->decl != new_decl && !b->invisible)
1931 tree old_decl = b->decl;
1933 if (old_decl == error_mark_node)
1935 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1936 "non-variable", new_decl);
1937 break;
1939 else if (TREE_CODE (old_decl) == PARM_DECL)
1940 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
1941 new_decl);
1942 else if (DECL_FILE_SCOPE_P (old_decl))
1943 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
1944 "declaration", new_decl);
1945 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1946 && DECL_BUILT_IN (old_decl))
1948 warning (OPT_Wshadow, "declaration of %q+D shadows "
1949 "a built-in function", new_decl);
1950 break;
1952 else
1953 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
1954 new_decl);
1956 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
1958 break;
1963 /* Subroutine of pushdecl.
1965 X is a TYPE_DECL for a typedef statement. Create a brand new
1966 ..._TYPE node (which will be just a variant of the existing
1967 ..._TYPE node with identical properties) and then install X
1968 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1970 The whole point here is to end up with a situation where each
1971 and every ..._TYPE node the compiler creates will be uniquely
1972 associated with AT MOST one node representing a typedef name.
1973 This way, even though the compiler substitutes corresponding
1974 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1975 early on, later parts of the compiler can always do the reverse
1976 translation and get back the corresponding typedef name. For
1977 example, given:
1979 typedef struct S MY_TYPE;
1980 MY_TYPE object;
1982 Later parts of the compiler might only know that `object' was of
1983 type `struct S' if it were not for code just below. With this
1984 code however, later parts of the compiler see something like:
1986 struct S' == struct S
1987 typedef struct S' MY_TYPE;
1988 struct S' object;
1990 And they can then deduce (from the node for type struct S') that
1991 the original object declaration was:
1993 MY_TYPE object;
1995 Being able to do this is important for proper support of protoize,
1996 and also for generating precise symbolic debugging information
1997 which takes full account of the programmer's (typedef) vocabulary.
1999 Obviously, we don't want to generate a duplicate ..._TYPE node if
2000 the TYPE_DECL node that we are now processing really represents a
2001 standard built-in type.
2003 Since all standard types are effectively declared at line zero
2004 in the source file, we can easily check to see if we are working
2005 on a standard type by checking the current value of lineno. */
2007 static void
2008 clone_underlying_type (tree x)
2010 if (DECL_IS_BUILTIN (x))
2012 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2013 TYPE_NAME (TREE_TYPE (x)) = x;
2015 else if (TREE_TYPE (x) != error_mark_node
2016 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2018 tree tt = TREE_TYPE (x);
2019 DECL_ORIGINAL_TYPE (x) = tt;
2020 tt = build_variant_type_copy (tt);
2021 TYPE_NAME (tt) = x;
2022 TREE_USED (tt) = TREE_USED (x);
2023 TREE_TYPE (x) = tt;
2027 /* Record a decl-node X as belonging to the current lexical scope.
2028 Check for errors (such as an incompatible declaration for the same
2029 name already seen in the same scope).
2031 Returns either X or an old decl for the same name.
2032 If an old decl is returned, it may have been smashed
2033 to agree with what X says. */
2035 tree
2036 pushdecl (tree x)
2038 tree name = DECL_NAME (x);
2039 struct c_scope *scope = current_scope;
2040 struct c_binding *b;
2041 bool nested = false;
2043 /* Functions need the lang_decl data. */
2044 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
2045 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2047 /* Must set DECL_CONTEXT for everything not at file scope or
2048 DECL_FILE_SCOPE_P won't work. Local externs don't count
2049 unless they have initializers (which generate code). */
2050 if (current_function_decl
2051 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2052 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2053 DECL_CONTEXT (x) = current_function_decl;
2055 /* If this is of variably modified type, prevent jumping into its
2056 scope. */
2057 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2058 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2059 c_begin_vm_scope (scope->depth);
2061 /* Anonymous decls are just inserted in the scope. */
2062 if (!name)
2064 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2065 return x;
2068 /* First, see if there is another declaration with the same name in
2069 the current scope. If there is, duplicate_decls may do all the
2070 work for us. If duplicate_decls returns false, that indicates
2071 two incompatible decls in the same scope; we are to silently
2072 replace the old one (duplicate_decls has issued all appropriate
2073 diagnostics). In particular, we should not consider possible
2074 duplicates in the external scope, or shadowing. */
2075 b = I_SYMBOL_BINDING (name);
2076 if (b && B_IN_SCOPE (b, scope))
2078 struct c_binding *b_ext, *b_use;
2079 tree type = TREE_TYPE (x);
2080 tree visdecl = b->decl;
2081 tree vistype = TREE_TYPE (visdecl);
2082 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2083 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2084 b->inner_comp = false;
2085 b_use = b;
2086 b_ext = b;
2087 /* If this is an external linkage declaration, we should check
2088 for compatibility with the type in the external scope before
2089 setting the type at this scope based on the visible
2090 information only. */
2091 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2093 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2094 b_ext = b_ext->shadowed;
2095 if (b_ext)
2097 b_use = b_ext;
2098 if (b_use->type)
2099 TREE_TYPE (b_use->decl) = b_use->type;
2102 if (duplicate_decls (x, b_use->decl))
2104 if (b_use != b)
2106 /* Save the updated type in the external scope and
2107 restore the proper type for this scope. */
2108 tree thistype;
2109 if (comptypes (vistype, type))
2110 thistype = composite_type (vistype, type);
2111 else
2112 thistype = TREE_TYPE (b_use->decl);
2113 b_use->type = TREE_TYPE (b_use->decl);
2114 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2115 && DECL_BUILT_IN (b_use->decl))
2116 thistype
2117 = build_type_attribute_variant (thistype,
2118 TYPE_ATTRIBUTES
2119 (b_use->type));
2120 TREE_TYPE (b_use->decl) = thistype;
2122 return b_use->decl;
2124 else
2125 goto skip_external_and_shadow_checks;
2128 /* All declarations with external linkage, and all external
2129 references, go in the external scope, no matter what scope is
2130 current. However, the binding in that scope is ignored for
2131 purposes of normal name lookup. A separate binding structure is
2132 created in the requested scope; this governs the normal
2133 visibility of the symbol.
2135 The binding in the externals scope is used exclusively for
2136 detecting duplicate declarations of the same object, no matter
2137 what scope they are in; this is what we do here. (C99 6.2.7p2:
2138 All declarations that refer to the same object or function shall
2139 have compatible type; otherwise, the behavior is undefined.) */
2140 if (DECL_EXTERNAL (x) || scope == file_scope)
2142 tree type = TREE_TYPE (x);
2143 tree vistype = 0;
2144 tree visdecl = 0;
2145 bool type_saved = false;
2146 if (b && !B_IN_EXTERNAL_SCOPE (b)
2147 && (TREE_CODE (b->decl) == FUNCTION_DECL
2148 || TREE_CODE (b->decl) == VAR_DECL)
2149 && DECL_FILE_SCOPE_P (b->decl))
2151 visdecl = b->decl;
2152 vistype = TREE_TYPE (visdecl);
2154 if (scope != file_scope
2155 && !DECL_IN_SYSTEM_HEADER (x))
2156 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2158 while (b && !B_IN_EXTERNAL_SCOPE (b))
2160 /* If this decl might be modified, save its type. This is
2161 done here rather than when the decl is first bound
2162 because the type may change after first binding, through
2163 being completed or through attributes being added. If we
2164 encounter multiple such decls, only the first should have
2165 its type saved; the others will already have had their
2166 proper types saved and the types will not have changed as
2167 their scopes will not have been re-entered. */
2168 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2170 b->type = TREE_TYPE (b->decl);
2171 type_saved = true;
2173 if (B_IN_FILE_SCOPE (b)
2174 && TREE_CODE (b->decl) == VAR_DECL
2175 && TREE_STATIC (b->decl)
2176 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2177 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2178 && TREE_CODE (type) == ARRAY_TYPE
2179 && TYPE_DOMAIN (type)
2180 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2181 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2183 /* Array type completed in inner scope, which should be
2184 diagnosed if the completion does not have size 1 and
2185 it does not get completed in the file scope. */
2186 b->inner_comp = true;
2188 b = b->shadowed;
2191 /* If a matching external declaration has been found, set its
2192 type to the composite of all the types of that declaration.
2193 After the consistency checks, it will be reset to the
2194 composite of the visible types only. */
2195 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2196 && b->type)
2197 TREE_TYPE (b->decl) = b->type;
2199 /* The point of the same_translation_unit_p check here is,
2200 we want to detect a duplicate decl for a construct like
2201 foo() { extern bar(); } ... static bar(); but not if
2202 they are in different translation units. In any case,
2203 the static does not go in the externals scope. */
2204 if (b
2205 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2206 && duplicate_decls (x, b->decl))
2208 tree thistype;
2209 if (vistype)
2211 if (comptypes (vistype, type))
2212 thistype = composite_type (vistype, type);
2213 else
2214 thistype = TREE_TYPE (b->decl);
2216 else
2217 thistype = type;
2218 b->type = TREE_TYPE (b->decl);
2219 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2220 thistype
2221 = build_type_attribute_variant (thistype,
2222 TYPE_ATTRIBUTES (b->type));
2223 TREE_TYPE (b->decl) = thistype;
2224 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2225 return b->decl;
2227 else if (TREE_PUBLIC (x))
2229 if (visdecl && !b && duplicate_decls (x, visdecl))
2231 /* An external declaration at block scope referring to a
2232 visible entity with internal linkage. The composite
2233 type will already be correct for this scope, so we
2234 just need to fall through to make the declaration in
2235 this scope. */
2236 nested = true;
2237 x = visdecl;
2239 else
2241 bind (name, x, external_scope, /*invisible=*/true,
2242 /*nested=*/false);
2243 nested = true;
2248 if (TREE_CODE (x) != PARM_DECL)
2249 warn_if_shadowing (x);
2251 skip_external_and_shadow_checks:
2252 if (TREE_CODE (x) == TYPE_DECL)
2253 clone_underlying_type (x);
2255 bind (name, x, scope, /*invisible=*/false, nested);
2257 /* If x's type is incomplete because it's based on a
2258 structure or union which has not yet been fully declared,
2259 attach it to that structure or union type, so we can go
2260 back and complete the variable declaration later, if the
2261 structure or union gets fully declared.
2263 If the input is erroneous, we can have error_mark in the type
2264 slot (e.g. "f(void a, ...)") - that doesn't count as an
2265 incomplete type. */
2266 if (TREE_TYPE (x) != error_mark_node
2267 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2269 tree element = TREE_TYPE (x);
2271 while (TREE_CODE (element) == ARRAY_TYPE)
2272 element = TREE_TYPE (element);
2273 element = TYPE_MAIN_VARIANT (element);
2275 if ((TREE_CODE (element) == RECORD_TYPE
2276 || TREE_CODE (element) == UNION_TYPE)
2277 && (TREE_CODE (x) != TYPE_DECL
2278 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2279 && !COMPLETE_TYPE_P (element))
2280 C_TYPE_INCOMPLETE_VARS (element)
2281 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2283 return x;
2286 /* Record X as belonging to file scope.
2287 This is used only internally by the Objective-C front end,
2288 and is limited to its needs. duplicate_decls is not called;
2289 if there is any preexisting decl for this identifier, it is an ICE. */
2291 tree
2292 pushdecl_top_level (tree x)
2294 tree name;
2295 bool nested = false;
2296 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2298 name = DECL_NAME (x);
2300 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2302 if (TREE_PUBLIC (x))
2304 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2305 nested = true;
2307 if (file_scope)
2308 bind (name, x, file_scope, /*invisible=*/false, nested);
2310 return x;
2313 static void
2314 implicit_decl_warning (tree id, tree olddecl)
2316 void (*diag) (const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2);
2317 switch (mesg_implicit_function_declaration)
2319 case 0: return;
2320 case 1: diag = warning0; break;
2321 case 2: diag = error; break;
2322 default: gcc_unreachable ();
2325 diag (G_("implicit declaration of function %qE"), id);
2326 if (olddecl)
2327 locate_old_decl (olddecl, diag);
2330 /* Generate an implicit declaration for identifier FUNCTIONID as a
2331 function of type int (). */
2333 tree
2334 implicitly_declare (tree functionid)
2336 struct c_binding *b;
2337 tree decl = 0;
2338 tree asmspec_tree;
2340 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2342 if (B_IN_SCOPE (b, external_scope))
2344 decl = b->decl;
2345 break;
2349 if (decl)
2351 if (decl == error_mark_node)
2352 return decl;
2354 /* FIXME: Objective-C has weird not-really-builtin functions
2355 which are supposed to be visible automatically. They wind up
2356 in the external scope because they're pushed before the file
2357 scope gets created. Catch this here and rebind them into the
2358 file scope. */
2359 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2361 bind (functionid, decl, file_scope,
2362 /*invisible=*/false, /*nested=*/true);
2363 return decl;
2365 else
2367 tree newtype = default_function_type;
2368 if (b->type)
2369 TREE_TYPE (decl) = b->type;
2370 /* Implicit declaration of a function already declared
2371 (somehow) in a different scope, or as a built-in.
2372 If this is the first time this has happened, warn;
2373 then recycle the old declaration but with the new type. */
2374 if (!C_DECL_IMPLICIT (decl))
2376 implicit_decl_warning (functionid, decl);
2377 C_DECL_IMPLICIT (decl) = 1;
2379 if (DECL_BUILT_IN (decl))
2381 newtype = build_type_attribute_variant (newtype,
2382 TYPE_ATTRIBUTES
2383 (TREE_TYPE (decl)));
2384 if (!comptypes (newtype, TREE_TYPE (decl)))
2386 warning (0, "incompatible implicit declaration of built-in"
2387 " function %qD", decl);
2388 newtype = TREE_TYPE (decl);
2391 else
2393 if (!comptypes (newtype, TREE_TYPE (decl)))
2395 error ("incompatible implicit declaration of function %qD",
2396 decl);
2397 locate_old_decl (decl, error);
2400 b->type = TREE_TYPE (decl);
2401 TREE_TYPE (decl) = newtype;
2402 bind (functionid, decl, current_scope,
2403 /*invisible=*/false, /*nested=*/true);
2404 return decl;
2408 /* Not seen before. */
2409 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2410 DECL_EXTERNAL (decl) = 1;
2411 TREE_PUBLIC (decl) = 1;
2412 C_DECL_IMPLICIT (decl) = 1;
2413 implicit_decl_warning (functionid, 0);
2414 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2415 if (asmspec_tree)
2416 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2418 /* C89 says implicit declarations are in the innermost block.
2419 So we record the decl in the standard fashion. */
2420 decl = pushdecl (decl);
2422 /* No need to call objc_check_decl here - it's a function type. */
2423 rest_of_decl_compilation (decl, 0, 0);
2425 /* Write a record describing this implicit function declaration
2426 to the prototypes file (if requested). */
2427 gen_aux_info_record (decl, 0, 1, 0);
2429 /* Possibly apply some default attributes to this implicit declaration. */
2430 decl_attributes (&decl, NULL_TREE, 0);
2432 return decl;
2435 /* Issue an error message for a reference to an undeclared variable
2436 ID, including a reference to a builtin outside of function-call
2437 context. Establish a binding of the identifier to error_mark_node
2438 in an appropriate scope, which will suppress further errors for the
2439 same identifier. The error message should be given location LOC. */
2440 void
2441 undeclared_variable (tree id, location_t loc)
2443 static bool already = false;
2444 struct c_scope *scope;
2446 if (current_function_decl == 0)
2448 error ("%H%qE undeclared here (not in a function)", &loc, id);
2449 scope = current_scope;
2451 else
2453 error ("%H%qE undeclared (first use in this function)", &loc, id);
2455 if (!already)
2457 error ("%H(Each undeclared identifier is reported only once", &loc);
2458 error ("%Hfor each function it appears in.)", &loc);
2459 already = true;
2462 /* If we are parsing old-style parameter decls, current_function_decl
2463 will be nonnull but current_function_scope will be null. */
2464 scope = current_function_scope ? current_function_scope : current_scope;
2466 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2469 /* Subroutine of lookup_label, declare_label, define_label: construct a
2470 LABEL_DECL with all the proper frills. */
2472 static tree
2473 make_label (tree name, location_t location)
2475 tree label = build_decl (LABEL_DECL, name, void_type_node);
2477 DECL_CONTEXT (label) = current_function_decl;
2478 DECL_MODE (label) = VOIDmode;
2479 DECL_SOURCE_LOCATION (label) = location;
2481 return label;
2484 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2485 Create one if none exists so far for the current function.
2486 This is called when a label is used in a goto expression or
2487 has its address taken. */
2489 tree
2490 lookup_label (tree name)
2492 tree label;
2494 if (current_function_decl == 0)
2496 error ("label %qE referenced outside of any function", name);
2497 return 0;
2500 /* Use a label already defined or ref'd with this name, but not if
2501 it is inherited from a containing function and wasn't declared
2502 using __label__. */
2503 label = I_LABEL_DECL (name);
2504 if (label && (DECL_CONTEXT (label) == current_function_decl
2505 || C_DECLARED_LABEL_FLAG (label)))
2507 /* If the label has only been declared, update its apparent
2508 location to point here, for better diagnostics if it
2509 turns out not to have been defined. */
2510 if (!TREE_USED (label))
2511 DECL_SOURCE_LOCATION (label) = input_location;
2512 return label;
2515 /* No label binding for that identifier; make one. */
2516 label = make_label (name, input_location);
2518 /* Ordinary labels go in the current function scope. */
2519 bind (name, label, current_function_scope,
2520 /*invisible=*/false, /*nested=*/false);
2521 return label;
2524 /* Make a label named NAME in the current function, shadowing silently
2525 any that may be inherited from containing functions or containing
2526 scopes. This is called for __label__ declarations. */
2528 tree
2529 declare_label (tree name)
2531 struct c_binding *b = I_LABEL_BINDING (name);
2532 tree label;
2534 /* Check to make sure that the label hasn't already been declared
2535 at this scope */
2536 if (b && B_IN_CURRENT_SCOPE (b))
2538 error ("duplicate label declaration %qE", name);
2539 locate_old_decl (b->decl, error);
2541 /* Just use the previous declaration. */
2542 return b->decl;
2545 label = make_label (name, input_location);
2546 C_DECLARED_LABEL_FLAG (label) = 1;
2548 /* Declared labels go in the current scope. */
2549 bind (name, label, current_scope,
2550 /*invisible=*/false, /*nested=*/false);
2551 return label;
2554 /* Define a label, specifying the location in the source file.
2555 Return the LABEL_DECL node for the label, if the definition is valid.
2556 Otherwise return 0. */
2558 tree
2559 define_label (location_t location, tree name)
2561 /* Find any preexisting label with this name. It is an error
2562 if that label has already been defined in this function, or
2563 if there is a containing function with a declared label with
2564 the same name. */
2565 tree label = I_LABEL_DECL (name);
2566 struct c_label_list *nlist_se, *nlist_vm;
2568 if (label
2569 && ((DECL_CONTEXT (label) == current_function_decl
2570 && DECL_INITIAL (label) != 0)
2571 || (DECL_CONTEXT (label) != current_function_decl
2572 && C_DECLARED_LABEL_FLAG (label))))
2574 error ("%Hduplicate label %qD", &location, label);
2575 locate_old_decl (label, error);
2576 return 0;
2578 else if (label && DECL_CONTEXT (label) == current_function_decl)
2580 /* The label has been used or declared already in this function,
2581 but not defined. Update its location to point to this
2582 definition. */
2583 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2584 error ("%Jjump into statement expression", label);
2585 if (C_DECL_UNDEFINABLE_VM (label))
2586 error ("%Jjump into scope of identifier with variably modified type",
2587 label);
2588 DECL_SOURCE_LOCATION (label) = location;
2590 else
2592 /* No label binding for that identifier; make one. */
2593 label = make_label (name, location);
2595 /* Ordinary labels go in the current function scope. */
2596 bind (name, label, current_function_scope,
2597 /*invisible=*/false, /*nested=*/false);
2600 if (!in_system_header && lookup_name (name))
2601 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2602 "for labels, identifier %qE conflicts", &location, name);
2604 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2605 nlist_se->next = label_context_stack_se->labels_def;
2606 nlist_se->label = label;
2607 label_context_stack_se->labels_def = nlist_se;
2609 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2610 nlist_vm->next = label_context_stack_vm->labels_def;
2611 nlist_vm->label = label;
2612 label_context_stack_vm->labels_def = nlist_vm;
2614 /* Mark label as having been defined. */
2615 DECL_INITIAL (label) = error_mark_node;
2616 return label;
2619 /* Given NAME, an IDENTIFIER_NODE,
2620 return the structure (or union or enum) definition for that name.
2621 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2622 CODE says which kind of type the caller wants;
2623 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2624 If the wrong kind of type is found, an error is reported. */
2626 static tree
2627 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2629 struct c_binding *b = I_TAG_BINDING (name);
2630 int thislevel = 0;
2632 if (!b || !b->decl)
2633 return 0;
2635 /* We only care about whether it's in this level if
2636 thislevel_only was set or it might be a type clash. */
2637 if (thislevel_only || TREE_CODE (b->decl) != code)
2639 /* For our purposes, a tag in the external scope is the same as
2640 a tag in the file scope. (Primarily relevant to Objective-C
2641 and its builtin structure tags, which get pushed before the
2642 file scope is created.) */
2643 if (B_IN_CURRENT_SCOPE (b)
2644 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2645 thislevel = 1;
2648 if (thislevel_only && !thislevel)
2649 return 0;
2651 if (TREE_CODE (b->decl) != code)
2653 /* Definition isn't the kind we were looking for. */
2654 pending_invalid_xref = name;
2655 pending_invalid_xref_location = input_location;
2657 /* If in the same binding level as a declaration as a tag
2658 of a different type, this must not be allowed to
2659 shadow that tag, so give the error immediately.
2660 (For example, "struct foo; union foo;" is invalid.) */
2661 if (thislevel)
2662 pending_xref_error ();
2664 return b->decl;
2667 /* Print an error message now
2668 for a recent invalid struct, union or enum cross reference.
2669 We don't print them immediately because they are not invalid
2670 when used in the `struct foo;' construct for shadowing. */
2672 void
2673 pending_xref_error (void)
2675 if (pending_invalid_xref != 0)
2676 error ("%H%qE defined as wrong kind of tag",
2677 &pending_invalid_xref_location, pending_invalid_xref);
2678 pending_invalid_xref = 0;
2682 /* Look up NAME in the current scope and its superiors
2683 in the namespace of variables, functions and typedefs.
2684 Return a ..._DECL node of some kind representing its definition,
2685 or return 0 if it is undefined. */
2687 tree
2688 lookup_name (tree name)
2690 struct c_binding *b = I_SYMBOL_BINDING (name);
2691 if (b && !b->invisible)
2692 return b->decl;
2693 return 0;
2696 /* Similar to `lookup_name' but look only at the indicated scope. */
2698 static tree
2699 lookup_name_in_scope (tree name, struct c_scope *scope)
2701 struct c_binding *b;
2703 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2704 if (B_IN_SCOPE (b, scope))
2705 return b->decl;
2706 return 0;
2709 /* Create the predefined scalar types of C,
2710 and some nodes representing standard constants (0, 1, (void *) 0).
2711 Initialize the global scope.
2712 Make definitions for built-in primitive functions. */
2714 void
2715 c_init_decl_processing (void)
2717 location_t save_loc = input_location;
2719 /* Initialize reserved words for parser. */
2720 c_parse_init ();
2722 current_function_decl = 0;
2724 gcc_obstack_init (&parser_obstack);
2726 /* Make the externals scope. */
2727 push_scope ();
2728 external_scope = current_scope;
2730 /* Declarations from c_common_nodes_and_builtins must not be associated
2731 with this input file, lest we get differences between using and not
2732 using preprocessed headers. */
2733 #ifdef USE_MAPPED_LOCATION
2734 input_location = BUILTINS_LOCATION;
2735 #else
2736 input_location.file = "<built-in>";
2737 input_location.line = 0;
2738 #endif
2740 build_common_tree_nodes (flag_signed_char, false);
2742 c_common_nodes_and_builtins ();
2744 /* In C, comparisons and TRUTH_* expressions have type int. */
2745 truthvalue_type_node = integer_type_node;
2746 truthvalue_true_node = integer_one_node;
2747 truthvalue_false_node = integer_zero_node;
2749 /* Even in C99, which has a real boolean type. */
2750 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2751 boolean_type_node));
2753 input_location = save_loc;
2755 pedantic_lvalues = true;
2757 make_fname_decl = c_make_fname_decl;
2758 start_fname_decls ();
2761 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2762 decl, NAME is the initialization string and TYPE_DEP indicates whether
2763 NAME depended on the type of the function. As we don't yet implement
2764 delayed emission of static data, we mark the decl as emitted
2765 so it is not placed in the output. Anything using it must therefore pull
2766 out the STRING_CST initializer directly. FIXME. */
2768 static tree
2769 c_make_fname_decl (tree id, int type_dep)
2771 const char *name = fname_as_string (type_dep);
2772 tree decl, type, init;
2773 size_t length = strlen (name);
2775 type = build_array_type (char_type_node,
2776 build_index_type (size_int (length)));
2777 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2779 decl = build_decl (VAR_DECL, id, type);
2781 TREE_STATIC (decl) = 1;
2782 TREE_READONLY (decl) = 1;
2783 DECL_ARTIFICIAL (decl) = 1;
2785 init = build_string (length + 1, name);
2786 free ((char *) name);
2787 TREE_TYPE (init) = type;
2788 DECL_INITIAL (decl) = init;
2790 TREE_USED (decl) = 1;
2792 if (current_function_decl
2793 /* For invalid programs like this:
2795 void foo()
2796 const char* p = __FUNCTION__;
2798 the __FUNCTION__ is believed to appear in K&R style function
2799 parameter declarator. In that case we still don't have
2800 function_scope. */
2801 && (!errorcount || current_function_scope))
2803 DECL_CONTEXT (decl) = current_function_decl;
2804 bind (id, decl, current_function_scope,
2805 /*invisible=*/false, /*nested=*/false);
2808 finish_decl (decl, init, NULL_TREE);
2810 return decl;
2813 /* Return a definition for a builtin function named NAME and whose data type
2814 is TYPE. TYPE should be a function type with argument types.
2815 FUNCTION_CODE tells later passes how to compile calls to this function.
2816 See tree.h for its possible values.
2818 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2819 the name to be called if we can't opencode the function. If
2820 ATTRS is nonzero, use that for the function's attribute list. */
2822 tree
2823 builtin_function (const char *name, tree type, int function_code,
2824 enum built_in_class cl, const char *library_name,
2825 tree attrs)
2827 tree id = get_identifier (name);
2828 tree decl = build_decl (FUNCTION_DECL, id, type);
2829 TREE_PUBLIC (decl) = 1;
2830 DECL_EXTERNAL (decl) = 1;
2831 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2832 DECL_BUILT_IN_CLASS (decl) = cl;
2833 DECL_FUNCTION_CODE (decl) = function_code;
2834 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2835 if (library_name)
2836 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2838 /* Should never be called on a symbol with a preexisting meaning. */
2839 gcc_assert (!I_SYMBOL_BINDING (id));
2841 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2843 /* Builtins in the implementation namespace are made visible without
2844 needing to be explicitly declared. See push_file_scope. */
2845 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2847 TREE_CHAIN (decl) = visible_builtins;
2848 visible_builtins = decl;
2851 /* Possibly apply some default attributes to this built-in function. */
2852 if (attrs)
2853 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2854 else
2855 decl_attributes (&decl, NULL_TREE, 0);
2857 return decl;
2860 /* Called when a declaration is seen that contains no names to declare.
2861 If its type is a reference to a structure, union or enum inherited
2862 from a containing scope, shadow that tag name for the current scope
2863 with a forward reference.
2864 If its type defines a new named structure or union
2865 or defines an enum, it is valid but we need not do anything here.
2866 Otherwise, it is an error. */
2868 void
2869 shadow_tag (const struct c_declspecs *declspecs)
2871 shadow_tag_warned (declspecs, 0);
2874 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2875 but no pedwarn. */
2876 void
2877 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2879 bool found_tag = false;
2881 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2883 tree value = declspecs->type;
2884 enum tree_code code = TREE_CODE (value);
2886 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2887 /* Used to test also that TYPE_SIZE (value) != 0.
2888 That caused warning for `struct foo;' at top level in the file. */
2890 tree name = TYPE_NAME (value);
2891 tree t;
2893 found_tag = true;
2895 if (name == 0)
2897 if (warned != 1 && code != ENUMERAL_TYPE)
2898 /* Empty unnamed enum OK */
2900 pedwarn ("unnamed struct/union that defines no instances");
2901 warned = 1;
2904 else if (!declspecs->tag_defined_p
2905 && declspecs->storage_class != csc_none)
2907 if (warned != 1)
2908 pedwarn ("empty declaration with storage class specifier "
2909 "does not redeclare tag");
2910 warned = 1;
2911 pending_xref_error ();
2913 else if (!declspecs->tag_defined_p
2914 && (declspecs->const_p
2915 || declspecs->volatile_p
2916 || declspecs->restrict_p))
2918 if (warned != 1)
2919 pedwarn ("empty declaration with type qualifier "
2920 "does not redeclare tag");
2921 warned = 1;
2922 pending_xref_error ();
2924 else
2926 pending_invalid_xref = 0;
2927 t = lookup_tag (code, name, 1);
2929 if (t == 0)
2931 t = make_node (code);
2932 pushtag (name, t);
2936 else
2938 if (warned != 1 && !in_system_header)
2940 pedwarn ("useless type name in empty declaration");
2941 warned = 1;
2945 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2947 pedwarn ("useless type name in empty declaration");
2948 warned = 1;
2951 pending_invalid_xref = 0;
2953 if (declspecs->inline_p)
2955 error ("%<inline%> in empty declaration");
2956 warned = 1;
2959 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2961 error ("%<auto%> in file-scope empty declaration");
2962 warned = 1;
2965 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2967 error ("%<register%> in file-scope empty declaration");
2968 warned = 1;
2971 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2973 warning (0, "useless storage class specifier in empty declaration");
2974 warned = 2;
2977 if (!warned && !in_system_header && declspecs->thread_p)
2979 warning (0, "useless %<__thread%> in empty declaration");
2980 warned = 2;
2983 if (!warned && !in_system_header && (declspecs->const_p
2984 || declspecs->volatile_p
2985 || declspecs->restrict_p))
2987 warning (0, "useless type qualifier in empty declaration");
2988 warned = 2;
2991 if (warned != 1)
2993 if (!found_tag)
2994 pedwarn ("empty declaration");
2999 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3000 bits. SPECS represents declaration specifiers that the grammar
3001 only permits to contain type qualifiers and attributes. */
3004 quals_from_declspecs (const struct c_declspecs *specs)
3006 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3007 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3008 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3009 gcc_assert (!specs->type
3010 && !specs->decl_attr
3011 && specs->typespec_word == cts_none
3012 && specs->storage_class == csc_none
3013 && !specs->typedef_p
3014 && !specs->explicit_signed_p
3015 && !specs->deprecated_p
3016 && !specs->long_p
3017 && !specs->long_long_p
3018 && !specs->short_p
3019 && !specs->signed_p
3020 && !specs->unsigned_p
3021 && !specs->complex_p
3022 && !specs->inline_p
3023 && !specs->thread_p);
3024 return quals;
3027 /* Construct an array declarator. EXPR is the expression inside [],
3028 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3029 applied to the pointer to which a parameter array is converted).
3030 STATIC_P is true if "static" is inside the [], false otherwise.
3031 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3032 length which is nevertheless a complete type, false otherwise. The
3033 field for the contained declarator is left to be filled in by
3034 set_array_declarator_inner. */
3036 struct c_declarator *
3037 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3038 bool vla_unspec_p)
3040 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3041 struct c_declarator);
3042 declarator->kind = cdk_array;
3043 declarator->declarator = 0;
3044 declarator->u.array.dimen = expr;
3045 if (quals)
3047 declarator->u.array.attrs = quals->attrs;
3048 declarator->u.array.quals = quals_from_declspecs (quals);
3050 else
3052 declarator->u.array.attrs = NULL_TREE;
3053 declarator->u.array.quals = 0;
3055 declarator->u.array.static_p = static_p;
3056 declarator->u.array.vla_unspec_p = vla_unspec_p;
3057 if (pedantic && !flag_isoc99)
3059 if (static_p || quals != NULL)
3060 pedwarn ("ISO C90 does not support %<static%> or type "
3061 "qualifiers in parameter array declarators");
3062 if (vla_unspec_p)
3063 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3065 if (vla_unspec_p)
3067 if (!current_scope->parm_flag)
3069 /* C99 6.7.5.2p4 */
3070 error ("%<[*]%> not allowed in other than function prototype scope");
3071 declarator->u.array.vla_unspec_p = false;
3072 return NULL;
3074 current_scope->had_vla_unspec = true;
3076 return declarator;
3079 /* Set the contained declarator of an array declarator. DECL is the
3080 declarator, as constructed by build_array_declarator; INNER is what
3081 appears on the left of the []. ABSTRACT_P is true if it is an
3082 abstract declarator, false otherwise; this is used to reject static
3083 and type qualifiers in abstract declarators, where they are not in
3084 the C99 grammar (subject to possible change in DR#289). */
3086 struct c_declarator *
3087 set_array_declarator_inner (struct c_declarator *decl,
3088 struct c_declarator *inner, bool abstract_p)
3090 decl->declarator = inner;
3091 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3092 || decl->u.array.attrs != NULL_TREE
3093 || decl->u.array.static_p))
3094 error ("static or type qualifiers in abstract declarator");
3095 return decl;
3098 /* INIT is a constructor that forms DECL's initializer. If the final
3099 element initializes a flexible array field, add the size of that
3100 initializer to DECL's size. */
3102 static void
3103 add_flexible_array_elts_to_size (tree decl, tree init)
3105 tree elt, type;
3107 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3108 return;
3110 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3111 type = TREE_TYPE (elt);
3112 if (TREE_CODE (type) == ARRAY_TYPE
3113 && TYPE_SIZE (type) == NULL_TREE
3114 && TYPE_DOMAIN (type) != NULL_TREE
3115 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3117 complete_array_type (&type, elt, false);
3118 DECL_SIZE (decl)
3119 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3120 DECL_SIZE_UNIT (decl)
3121 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3125 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3127 tree
3128 groktypename (struct c_type_name *type_name)
3130 tree type;
3131 tree attrs = type_name->specs->attrs;
3133 type_name->specs->attrs = NULL_TREE;
3135 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3136 false, NULL);
3138 /* Apply attributes. */
3139 decl_attributes (&type, attrs, 0);
3141 return type;
3144 /* Decode a declarator in an ordinary declaration or data definition.
3145 This is called as soon as the type information and variable name
3146 have been parsed, before parsing the initializer if any.
3147 Here we create the ..._DECL node, fill in its type,
3148 and put it on the list of decls for the current context.
3149 The ..._DECL node is returned as the value.
3151 Exception: for arrays where the length is not specified,
3152 the type is left null, to be filled in by `finish_decl'.
3154 Function definitions do not come here; they go to start_function
3155 instead. However, external and forward declarations of functions
3156 do go through here. Structure field declarations are done by
3157 grokfield and not through here. */
3159 tree
3160 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3161 bool initialized, tree attributes)
3163 tree decl;
3164 tree tem;
3166 /* An object declared as __attribute__((deprecated)) suppresses
3167 warnings of uses of other deprecated items. */
3168 if (lookup_attribute ("deprecated", attributes))
3169 deprecated_state = DEPRECATED_SUPPRESS;
3171 decl = grokdeclarator (declarator, declspecs,
3172 NORMAL, initialized, NULL);
3173 if (!decl)
3174 return 0;
3176 deprecated_state = DEPRECATED_NORMAL;
3178 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3179 && MAIN_NAME_P (DECL_NAME (decl)))
3180 warning (OPT_Wmain, "%q+D is usually a function", decl);
3182 if (initialized)
3183 /* Is it valid for this decl to have an initializer at all?
3184 If not, set INITIALIZED to zero, which will indirectly
3185 tell 'finish_decl' to ignore the initializer once it is parsed. */
3186 switch (TREE_CODE (decl))
3188 case TYPE_DECL:
3189 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3190 initialized = 0;
3191 break;
3193 case FUNCTION_DECL:
3194 error ("function %qD is initialized like a variable", decl);
3195 initialized = 0;
3196 break;
3198 case PARM_DECL:
3199 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3200 error ("parameter %qD is initialized", decl);
3201 initialized = 0;
3202 break;
3204 default:
3205 /* Don't allow initializations for incomplete types except for
3206 arrays which might be completed by the initialization. */
3208 /* This can happen if the array size is an undefined macro.
3209 We already gave a warning, so we don't need another one. */
3210 if (TREE_TYPE (decl) == error_mark_node)
3211 initialized = 0;
3212 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3214 /* A complete type is ok if size is fixed. */
3216 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3217 || C_DECL_VARIABLE_SIZE (decl))
3219 error ("variable-sized object may not be initialized");
3220 initialized = 0;
3223 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3225 error ("variable %qD has initializer but incomplete type", decl);
3226 initialized = 0;
3228 else if (C_DECL_VARIABLE_SIZE (decl))
3230 /* Although C99 is unclear about whether incomplete arrays
3231 of VLAs themselves count as VLAs, it does not make
3232 sense to permit them to be initialized given that
3233 ordinary VLAs may not be initialized. */
3234 error ("variable-sized object may not be initialized");
3235 initialized = 0;
3239 if (initialized)
3241 if (current_scope == file_scope)
3242 TREE_STATIC (decl) = 1;
3244 /* Tell 'pushdecl' this is an initialized decl
3245 even though we don't yet have the initializer expression.
3246 Also tell 'finish_decl' it may store the real initializer. */
3247 DECL_INITIAL (decl) = error_mark_node;
3250 /* If this is a function declaration, write a record describing it to the
3251 prototypes file (if requested). */
3253 if (TREE_CODE (decl) == FUNCTION_DECL)
3254 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3256 /* ANSI specifies that a tentative definition which is not merged with
3257 a non-tentative definition behaves exactly like a definition with an
3258 initializer equal to zero. (Section 3.7.2)
3260 -fno-common gives strict ANSI behavior, though this tends to break
3261 a large body of code that grew up without this rule.
3263 Thread-local variables are never common, since there's no entrenched
3264 body of code to break, and it allows more efficient variable references
3265 in the presence of dynamic linking. */
3267 if (TREE_CODE (decl) == VAR_DECL
3268 && !initialized
3269 && TREE_PUBLIC (decl)
3270 && !DECL_THREAD_LOCAL_P (decl)
3271 && !flag_no_common)
3272 DECL_COMMON (decl) = 1;
3274 /* Set attributes here so if duplicate decl, will have proper attributes. */
3275 decl_attributes (&decl, attributes, 0);
3277 if (TREE_CODE (decl) == FUNCTION_DECL
3278 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3280 struct c_declarator *ce = declarator;
3282 if (ce->kind == cdk_pointer)
3283 ce = declarator->declarator;
3284 if (ce->kind == cdk_function)
3286 tree args = ce->u.arg_info->parms;
3287 for (; args; args = TREE_CHAIN (args))
3289 tree type = TREE_TYPE (args);
3290 if (type && INTEGRAL_TYPE_P (type)
3291 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3292 DECL_ARG_TYPE (args) = integer_type_node;
3297 if (TREE_CODE (decl) == FUNCTION_DECL
3298 && DECL_DECLARED_INLINE_P (decl)
3299 && DECL_UNINLINABLE (decl)
3300 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3301 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3302 decl);
3304 /* Add this decl to the current scope.
3305 TEM may equal DECL or it may be a previous decl of the same name. */
3306 tem = pushdecl (decl);
3308 if (initialized && DECL_EXTERNAL (tem))
3310 DECL_EXTERNAL (tem) = 0;
3311 TREE_STATIC (tem) = 1;
3314 return tem;
3317 /* Initialize EH if not initialized yet and exceptions are enabled. */
3319 void
3320 c_maybe_initialize_eh (void)
3322 if (!flag_exceptions || c_eh_initialized_p)
3323 return;
3325 c_eh_initialized_p = true;
3326 eh_personality_libfunc
3327 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3328 ? "__gcc_personality_sj0"
3329 : "__gcc_personality_v0");
3330 default_init_unwind_resume_libfunc ();
3331 using_eh_for_cleanups ();
3334 /* Finish processing of a declaration;
3335 install its initial value.
3336 If the length of an array type is not known before,
3337 it must be determined now, from the initial value, or it is an error. */
3339 void
3340 finish_decl (tree decl, tree init, tree asmspec_tree)
3342 tree type;
3343 int was_incomplete = (DECL_SIZE (decl) == 0);
3344 const char *asmspec = 0;
3346 /* If a name was specified, get the string. */
3347 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3348 && DECL_FILE_SCOPE_P (decl))
3349 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3350 if (asmspec_tree)
3351 asmspec = TREE_STRING_POINTER (asmspec_tree);
3353 /* If `start_decl' didn't like having an initialization, ignore it now. */
3354 if (init != 0 && DECL_INITIAL (decl) == 0)
3355 init = 0;
3357 /* Don't crash if parm is initialized. */
3358 if (TREE_CODE (decl) == PARM_DECL)
3359 init = 0;
3361 if (init)
3362 store_init_value (decl, init);
3364 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3365 || TREE_CODE (decl) == FUNCTION_DECL
3366 || TREE_CODE (decl) == FIELD_DECL))
3367 objc_check_decl (decl);
3369 type = TREE_TYPE (decl);
3371 /* Deduce size of array from initialization, if not already known. */
3372 if (TREE_CODE (type) == ARRAY_TYPE
3373 && TYPE_DOMAIN (type) == 0
3374 && TREE_CODE (decl) != TYPE_DECL)
3376 bool do_default
3377 = (TREE_STATIC (decl)
3378 /* Even if pedantic, an external linkage array
3379 may have incomplete type at first. */
3380 ? pedantic && !TREE_PUBLIC (decl)
3381 : !DECL_EXTERNAL (decl));
3382 int failure
3383 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3384 do_default);
3386 /* Get the completed type made by complete_array_type. */
3387 type = TREE_TYPE (decl);
3389 switch (failure)
3391 case 1:
3392 error ("initializer fails to determine size of %q+D", decl);
3393 break;
3395 case 2:
3396 if (do_default)
3397 error ("array size missing in %q+D", decl);
3398 /* If a `static' var's size isn't known,
3399 make it extern as well as static, so it does not get
3400 allocated.
3401 If it is not `static', then do not mark extern;
3402 finish_incomplete_decl will give it a default size
3403 and it will get allocated. */
3404 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3405 DECL_EXTERNAL (decl) = 1;
3406 break;
3408 case 3:
3409 error ("zero or negative size array %q+D", decl);
3410 break;
3412 case 0:
3413 /* For global variables, update the copy of the type that
3414 exists in the binding. */
3415 if (TREE_PUBLIC (decl))
3417 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3418 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3419 b_ext = b_ext->shadowed;
3420 if (b_ext)
3422 if (b_ext->type)
3423 b_ext->type = composite_type (b_ext->type, type);
3424 else
3425 b_ext->type = type;
3428 break;
3430 default:
3431 gcc_unreachable ();
3434 if (DECL_INITIAL (decl))
3435 TREE_TYPE (DECL_INITIAL (decl)) = type;
3437 layout_decl (decl, 0);
3440 if (TREE_CODE (decl) == VAR_DECL)
3442 if (init && TREE_CODE (init) == CONSTRUCTOR)
3443 add_flexible_array_elts_to_size (decl, init);
3445 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3446 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3447 layout_decl (decl, 0);
3449 if (DECL_SIZE (decl) == 0
3450 /* Don't give an error if we already gave one earlier. */
3451 && TREE_TYPE (decl) != error_mark_node
3452 && (TREE_STATIC (decl)
3453 /* A static variable with an incomplete type
3454 is an error if it is initialized.
3455 Also if it is not file scope.
3456 Otherwise, let it through, but if it is not `extern'
3457 then it may cause an error message later. */
3458 ? (DECL_INITIAL (decl) != 0
3459 || !DECL_FILE_SCOPE_P (decl))
3460 /* An automatic variable with an incomplete type
3461 is an error. */
3462 : !DECL_EXTERNAL (decl)))
3464 error ("storage size of %q+D isn%'t known", decl);
3465 TREE_TYPE (decl) = error_mark_node;
3468 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3469 && DECL_SIZE (decl) != 0)
3471 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3472 constant_expression_warning (DECL_SIZE (decl));
3473 else
3474 error ("storage size of %q+D isn%'t constant", decl);
3477 if (TREE_USED (type))
3478 TREE_USED (decl) = 1;
3481 /* If this is a function and an assembler name is specified, reset DECL_RTL
3482 so we can give it its new name. Also, update built_in_decls if it
3483 was a normal built-in. */
3484 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3486 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3487 set_builtin_user_assembler_name (decl, asmspec);
3488 set_user_assembler_name (decl, asmspec);
3491 /* If #pragma weak was used, mark the decl weak now. */
3492 maybe_apply_pragma_weak (decl);
3494 /* Output the assembler code and/or RTL code for variables and functions,
3495 unless the type is an undefined structure or union.
3496 If not, it will get done when the type is completed. */
3498 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3500 /* Determine the ELF visibility. */
3501 if (TREE_PUBLIC (decl))
3502 c_determine_visibility (decl);
3504 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3505 if (c_dialect_objc ())
3506 objc_check_decl (decl);
3508 if (asmspec)
3510 /* If this is not a static variable, issue a warning.
3511 It doesn't make any sense to give an ASMSPEC for an
3512 ordinary, non-register local variable. Historically,
3513 GCC has accepted -- but ignored -- the ASMSPEC in
3514 this case. */
3515 if (!DECL_FILE_SCOPE_P (decl)
3516 && TREE_CODE (decl) == VAR_DECL
3517 && !C_DECL_REGISTER (decl)
3518 && !TREE_STATIC (decl))
3519 warning (0, "ignoring asm-specifier for non-static local "
3520 "variable %q+D", decl);
3521 else
3522 set_user_assembler_name (decl, asmspec);
3525 if (DECL_FILE_SCOPE_P (decl))
3527 if (DECL_INITIAL (decl) == NULL_TREE
3528 || DECL_INITIAL (decl) == error_mark_node)
3529 /* Don't output anything
3530 when a tentative file-scope definition is seen.
3531 But at end of compilation, do output code for them. */
3532 DECL_DEFER_OUTPUT (decl) = 1;
3533 rest_of_decl_compilation (decl, true, 0);
3535 else
3537 /* In conjunction with an ASMSPEC, the `register'
3538 keyword indicates that we should place the variable
3539 in a particular register. */
3540 if (asmspec && C_DECL_REGISTER (decl))
3542 DECL_HARD_REGISTER (decl) = 1;
3543 /* This cannot be done for a structure with volatile
3544 fields, on which DECL_REGISTER will have been
3545 reset. */
3546 if (!DECL_REGISTER (decl))
3547 error ("cannot put object with volatile field into register");
3550 if (TREE_CODE (decl) != FUNCTION_DECL)
3552 /* If we're building a variable sized type, and we might be
3553 reachable other than via the top of the current binding
3554 level, then create a new BIND_EXPR so that we deallocate
3555 the object at the right time. */
3556 /* Note that DECL_SIZE can be null due to errors. */
3557 if (DECL_SIZE (decl)
3558 && !TREE_CONSTANT (DECL_SIZE (decl))
3559 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3561 tree bind;
3562 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3563 TREE_SIDE_EFFECTS (bind) = 1;
3564 add_stmt (bind);
3565 BIND_EXPR_BODY (bind) = push_stmt_list ();
3567 add_stmt (build_stmt (DECL_EXPR, decl));
3572 if (!DECL_FILE_SCOPE_P (decl))
3574 /* Recompute the RTL of a local array now
3575 if it used to be an incomplete type. */
3576 if (was_incomplete
3577 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3579 /* If we used it already as memory, it must stay in memory. */
3580 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3581 /* If it's still incomplete now, no init will save it. */
3582 if (DECL_SIZE (decl) == 0)
3583 DECL_INITIAL (decl) = 0;
3588 /* If this was marked 'used', be sure it will be output. */
3589 if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3590 mark_decl_referenced (decl);
3592 if (TREE_CODE (decl) == TYPE_DECL)
3594 if (!DECL_FILE_SCOPE_P (decl)
3595 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3596 add_stmt (build_stmt (DECL_EXPR, decl));
3598 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3601 /* At the end of a declaration, throw away any variable type sizes
3602 of types defined inside that declaration. There is no use
3603 computing them in the following function definition. */
3604 if (current_scope == file_scope)
3605 get_pending_sizes ();
3607 /* Install a cleanup (aka destructor) if one was given. */
3608 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3610 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3611 if (attr)
3613 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3614 tree cleanup_decl = lookup_name (cleanup_id);
3615 tree cleanup;
3617 /* Build "cleanup(&decl)" for the destructor. */
3618 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3619 cleanup = build_tree_list (NULL_TREE, cleanup);
3620 cleanup = build_function_call (cleanup_decl, cleanup);
3622 /* Don't warn about decl unused; the cleanup uses it. */
3623 TREE_USED (decl) = 1;
3624 TREE_USED (cleanup_decl) = 1;
3626 /* Initialize EH, if we've been told to do so. */
3627 c_maybe_initialize_eh ();
3629 push_cleanup (decl, cleanup, false);
3634 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3636 tree
3637 grokparm (const struct c_parm *parm)
3639 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3640 NULL);
3642 decl_attributes (&decl, parm->attrs, 0);
3644 return decl;
3647 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3648 and push that on the current scope. */
3650 void
3651 push_parm_decl (const struct c_parm *parm)
3653 tree decl;
3655 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3656 decl_attributes (&decl, parm->attrs, 0);
3658 decl = pushdecl (decl);
3660 finish_decl (decl, NULL_TREE, NULL_TREE);
3663 /* Mark all the parameter declarations to date as forward decls.
3664 Also diagnose use of this extension. */
3666 void
3667 mark_forward_parm_decls (void)
3669 struct c_binding *b;
3671 if (pedantic && !current_scope->warned_forward_parm_decls)
3673 pedwarn ("ISO C forbids forward parameter declarations");
3674 current_scope->warned_forward_parm_decls = true;
3677 for (b = current_scope->bindings; b; b = b->prev)
3678 if (TREE_CODE (b->decl) == PARM_DECL)
3679 TREE_ASM_WRITTEN (b->decl) = 1;
3682 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3683 literal, which may be an incomplete array type completed by the
3684 initializer; INIT is a CONSTRUCTOR that initializes the compound
3685 literal. */
3687 tree
3688 build_compound_literal (tree type, tree init)
3690 /* We do not use start_decl here because we have a type, not a declarator;
3691 and do not use finish_decl because the decl should be stored inside
3692 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3693 tree decl;
3694 tree complit;
3695 tree stmt;
3697 if (type == error_mark_node)
3698 return error_mark_node;
3700 decl = build_decl (VAR_DECL, NULL_TREE, type);
3701 DECL_EXTERNAL (decl) = 0;
3702 TREE_PUBLIC (decl) = 0;
3703 TREE_STATIC (decl) = (current_scope == file_scope);
3704 DECL_CONTEXT (decl) = current_function_decl;
3705 TREE_USED (decl) = 1;
3706 TREE_TYPE (decl) = type;
3707 TREE_READONLY (decl) = TYPE_READONLY (type);
3708 store_init_value (decl, init);
3710 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3712 int failure = complete_array_type (&TREE_TYPE (decl),
3713 DECL_INITIAL (decl), true);
3714 gcc_assert (!failure);
3716 type = TREE_TYPE (decl);
3717 TREE_TYPE (DECL_INITIAL (decl)) = type;
3720 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3721 return error_mark_node;
3723 stmt = build_stmt (DECL_EXPR, decl);
3724 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3725 TREE_SIDE_EFFECTS (complit) = 1;
3727 layout_decl (decl, 0);
3729 if (TREE_STATIC (decl))
3731 /* This decl needs a name for the assembler output. */
3732 set_compound_literal_name (decl);
3733 DECL_DEFER_OUTPUT (decl) = 1;
3734 DECL_COMDAT (decl) = 1;
3735 DECL_ARTIFICIAL (decl) = 1;
3736 DECL_IGNORED_P (decl) = 1;
3737 pushdecl (decl);
3738 rest_of_decl_compilation (decl, 1, 0);
3741 return complit;
3744 /* Determine whether TYPE is a structure with a flexible array member,
3745 or a union containing such a structure (possibly recursively). */
3747 static bool
3748 flexible_array_type_p (tree type)
3750 tree x;
3751 switch (TREE_CODE (type))
3753 case RECORD_TYPE:
3754 x = TYPE_FIELDS (type);
3755 if (x == NULL_TREE)
3756 return false;
3757 while (TREE_CHAIN (x) != NULL_TREE)
3758 x = TREE_CHAIN (x);
3759 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3760 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3761 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3762 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3763 return true;
3764 return false;
3765 case UNION_TYPE:
3766 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3768 if (flexible_array_type_p (TREE_TYPE (x)))
3769 return true;
3771 return false;
3772 default:
3773 return false;
3777 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3778 replacing with appropriate values if they are invalid. */
3779 static void
3780 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3782 tree type_mv;
3783 unsigned int max_width;
3784 unsigned HOST_WIDE_INT w;
3785 const char *name = orig_name ? orig_name: _("<anonymous>");
3787 /* Detect and ignore out of range field width and process valid
3788 field widths. */
3789 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3790 || TREE_CODE (*width) != INTEGER_CST)
3792 error ("bit-field %qs width not an integer constant", name);
3793 *width = integer_one_node;
3795 else
3797 constant_expression_warning (*width);
3798 if (tree_int_cst_sgn (*width) < 0)
3800 error ("negative width in bit-field %qs", name);
3801 *width = integer_one_node;
3803 else if (integer_zerop (*width) && orig_name)
3805 error ("zero width for bit-field %qs", name);
3806 *width = integer_one_node;
3810 /* Detect invalid bit-field type. */
3811 if (TREE_CODE (*type) != INTEGER_TYPE
3812 && TREE_CODE (*type) != BOOLEAN_TYPE
3813 && TREE_CODE (*type) != ENUMERAL_TYPE)
3815 error ("bit-field %qs has invalid type", name);
3816 *type = unsigned_type_node;
3819 type_mv = TYPE_MAIN_VARIANT (*type);
3820 if (pedantic
3821 && !in_system_header
3822 && type_mv != integer_type_node
3823 && type_mv != unsigned_type_node
3824 && type_mv != boolean_type_node)
3825 pedwarn ("type of bit-field %qs is a GCC extension", name);
3827 if (type_mv == boolean_type_node)
3828 max_width = CHAR_TYPE_SIZE;
3829 else
3830 max_width = TYPE_PRECISION (*type);
3832 if (0 < compare_tree_int (*width, max_width))
3834 error ("width of %qs exceeds its type", name);
3835 w = max_width;
3836 *width = build_int_cst (NULL_TREE, w);
3838 else
3839 w = tree_low_cst (*width, 1);
3841 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3843 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3844 if (!lt
3845 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3846 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3847 warning (0, "%qs is narrower than values of its type", name);
3852 /* Given declspecs and a declarator,
3853 determine the name and type of the object declared
3854 and construct a ..._DECL node for it.
3855 (In one case we can return a ..._TYPE node instead.
3856 For invalid input we sometimes return 0.)
3858 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3860 DECL_CONTEXT says which syntactic context this declaration is in:
3861 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3862 FUNCDEF for a function definition. Like NORMAL but a few different
3863 error messages in each case. Return value may be zero meaning
3864 this definition is too screwy to try to parse.
3865 PARM for a parameter declaration (either within a function prototype
3866 or before a function body). Make a PARM_DECL, or return void_type_node.
3867 TYPENAME if for a typename (in a cast or sizeof).
3868 Don't make a DECL node; just return the ..._TYPE node.
3869 FIELD for a struct or union field; make a FIELD_DECL.
3870 INITIALIZED is true if the decl has an initializer.
3871 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3872 representing the width of the bit-field.
3874 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3875 It may also be so in the PARM case, for a prototype where the
3876 argument type is specified but not the name.
3878 This function is where the complicated C meanings of `static'
3879 and `extern' are interpreted. */
3881 static tree
3882 grokdeclarator (const struct c_declarator *declarator,
3883 struct c_declspecs *declspecs,
3884 enum decl_context decl_context, bool initialized, tree *width)
3886 tree type = declspecs->type;
3887 bool threadp = declspecs->thread_p;
3888 enum c_storage_class storage_class = declspecs->storage_class;
3889 int constp;
3890 int restrictp;
3891 int volatilep;
3892 int type_quals = TYPE_UNQUALIFIED;
3893 const char *name, *orig_name;
3894 tree typedef_type = 0;
3895 bool funcdef_flag = false;
3896 bool funcdef_syntax = false;
3897 int size_varies = 0;
3898 tree decl_attr = declspecs->decl_attr;
3899 int array_ptr_quals = TYPE_UNQUALIFIED;
3900 tree array_ptr_attrs = NULL_TREE;
3901 int array_parm_static = 0;
3902 bool array_parm_vla_unspec_p = false;
3903 tree returned_attrs = NULL_TREE;
3904 bool bitfield = width != NULL;
3905 tree element_type;
3906 struct c_arg_info *arg_info = 0;
3908 if (decl_context == FUNCDEF)
3909 funcdef_flag = true, decl_context = NORMAL;
3911 /* Look inside a declarator for the name being declared
3912 and get it as a string, for an error message. */
3914 const struct c_declarator *decl = declarator;
3915 name = 0;
3917 while (decl)
3918 switch (decl->kind)
3920 case cdk_function:
3921 case cdk_array:
3922 case cdk_pointer:
3923 funcdef_syntax = (decl->kind == cdk_function);
3924 decl = decl->declarator;
3925 break;
3927 case cdk_attrs:
3928 decl = decl->declarator;
3929 break;
3931 case cdk_id:
3932 if (decl->u.id)
3933 name = IDENTIFIER_POINTER (decl->u.id);
3934 decl = 0;
3935 break;
3937 default:
3938 gcc_unreachable ();
3940 orig_name = name;
3941 if (name == 0)
3942 name = "type name";
3945 /* A function definition's declarator must have the form of
3946 a function declarator. */
3948 if (funcdef_flag && !funcdef_syntax)
3949 return 0;
3951 /* If this looks like a function definition, make it one,
3952 even if it occurs where parms are expected.
3953 Then store_parm_decls will reject it and not use it as a parm. */
3954 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3955 decl_context = PARM;
3957 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3958 warn_deprecated_use (declspecs->type);
3960 if ((decl_context == NORMAL || decl_context == FIELD)
3961 && current_scope == file_scope
3962 && variably_modified_type_p (type, NULL_TREE))
3964 error ("variably modified %qs at file scope", name);
3965 type = integer_type_node;
3968 typedef_type = type;
3969 size_varies = C_TYPE_VARIABLE_SIZE (type);
3971 /* Diagnose defaulting to "int". */
3973 if (declspecs->default_int_p && !in_system_header)
3975 /* Issue a warning if this is an ISO C 99 program or if
3976 -Wreturn-type and this is a function, or if -Wimplicit;
3977 prefer the former warning since it is more explicit. */
3978 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3979 && funcdef_flag)
3980 warn_about_return_type = 1;
3981 else if (warn_implicit_int || flag_isoc99)
3982 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3985 /* Adjust the type if a bit-field is being declared,
3986 -funsigned-bitfields applied and the type is not explicitly
3987 "signed". */
3988 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3989 && TREE_CODE (type) == INTEGER_TYPE)
3990 type = c_common_unsigned_type (type);
3992 /* Figure out the type qualifiers for the declaration. There are
3993 two ways a declaration can become qualified. One is something
3994 like `const int i' where the `const' is explicit. Another is
3995 something like `typedef const int CI; CI i' where the type of the
3996 declaration contains the `const'. A third possibility is that
3997 there is a type qualifier on the element type of a typedefed
3998 array type, in which case we should extract that qualifier so
3999 that c_apply_type_quals_to_decls receives the full list of
4000 qualifiers to work with (C90 is not entirely clear about whether
4001 duplicate qualifiers should be diagnosed in this case, but it
4002 seems most appropriate to do so). */
4003 element_type = strip_array_types (type);
4004 constp = declspecs->const_p + TYPE_READONLY (element_type);
4005 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4006 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4007 if (pedantic && !flag_isoc99)
4009 if (constp > 1)
4010 pedwarn ("duplicate %<const%>");
4011 if (restrictp > 1)
4012 pedwarn ("duplicate %<restrict%>");
4013 if (volatilep > 1)
4014 pedwarn ("duplicate %<volatile%>");
4016 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4017 type = TYPE_MAIN_VARIANT (type);
4018 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4019 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4020 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4022 /* Warn about storage classes that are invalid for certain
4023 kinds of declarations (parameters, typenames, etc.). */
4025 if (funcdef_flag
4026 && (threadp
4027 || storage_class == csc_auto
4028 || storage_class == csc_register
4029 || storage_class == csc_typedef))
4031 if (storage_class == csc_auto
4032 && (pedantic || current_scope == file_scope))
4033 pedwarn ("function definition declared %<auto%>");
4034 if (storage_class == csc_register)
4035 error ("function definition declared %<register%>");
4036 if (storage_class == csc_typedef)
4037 error ("function definition declared %<typedef%>");
4038 if (threadp)
4039 error ("function definition declared %<__thread%>");
4040 threadp = false;
4041 if (storage_class == csc_auto
4042 || storage_class == csc_register
4043 || storage_class == csc_typedef)
4044 storage_class = csc_none;
4046 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4048 if (decl_context == PARM && storage_class == csc_register)
4050 else
4052 switch (decl_context)
4054 case FIELD:
4055 error ("storage class specified for structure field %qs",
4056 name);
4057 break;
4058 case PARM:
4059 error ("storage class specified for parameter %qs", name);
4060 break;
4061 default:
4062 error ("storage class specified for typename");
4063 break;
4065 storage_class = csc_none;
4066 threadp = false;
4069 else if (storage_class == csc_extern
4070 && initialized
4071 && !funcdef_flag)
4073 /* 'extern' with initialization is invalid if not at file scope. */
4074 if (current_scope == file_scope)
4076 /* It is fine to have 'extern const' when compiling at C
4077 and C++ intersection. */
4078 if (!(warn_cxx_compat && constp))
4079 warning (0, "%qs initialized and declared %<extern%>", name);
4081 else
4082 error ("%qs has both %<extern%> and initializer", name);
4084 else if (current_scope == file_scope)
4086 if (storage_class == csc_auto)
4087 error ("file-scope declaration of %qs specifies %<auto%>", name);
4088 if (pedantic && storage_class == csc_register)
4089 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4091 else
4093 if (storage_class == csc_extern && funcdef_flag)
4094 error ("nested function %qs declared %<extern%>", name);
4095 else if (threadp && storage_class == csc_none)
4097 error ("function-scope %qs implicitly auto and declared "
4098 "%<__thread%>",
4099 name);
4100 threadp = false;
4104 /* Now figure out the structure of the declarator proper.
4105 Descend through it, creating more complex types, until we reach
4106 the declared identifier (or NULL_TREE, in an absolute declarator).
4107 At each stage we maintain an unqualified version of the type
4108 together with any qualifiers that should be applied to it with
4109 c_build_qualified_type; this way, array types including
4110 multidimensional array types are first built up in unqualified
4111 form and then the qualified form is created with
4112 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4114 while (declarator && declarator->kind != cdk_id)
4116 if (type == error_mark_node)
4118 declarator = declarator->declarator;
4119 continue;
4122 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4123 a cdk_pointer (for *...),
4124 a cdk_function (for ...(...)),
4125 a cdk_attrs (for nested attributes),
4126 or a cdk_id (for the name being declared
4127 or the place in an absolute declarator
4128 where the name was omitted).
4129 For the last case, we have just exited the loop.
4131 At this point, TYPE is the type of elements of an array,
4132 or for a function to return, or for a pointer to point to.
4133 After this sequence of ifs, TYPE is the type of the
4134 array or function or pointer, and DECLARATOR has had its
4135 outermost layer removed. */
4137 if (array_ptr_quals != TYPE_UNQUALIFIED
4138 || array_ptr_attrs != NULL_TREE
4139 || array_parm_static)
4141 /* Only the innermost declarator (making a parameter be of
4142 array type which is converted to pointer type)
4143 may have static or type qualifiers. */
4144 error ("static or type qualifiers in non-parameter array declarator");
4145 array_ptr_quals = TYPE_UNQUALIFIED;
4146 array_ptr_attrs = NULL_TREE;
4147 array_parm_static = 0;
4150 switch (declarator->kind)
4152 case cdk_attrs:
4154 /* A declarator with embedded attributes. */
4155 tree attrs = declarator->u.attrs;
4156 const struct c_declarator *inner_decl;
4157 int attr_flags = 0;
4158 declarator = declarator->declarator;
4159 inner_decl = declarator;
4160 while (inner_decl->kind == cdk_attrs)
4161 inner_decl = inner_decl->declarator;
4162 if (inner_decl->kind == cdk_id)
4163 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4164 else if (inner_decl->kind == cdk_function)
4165 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4166 else if (inner_decl->kind == cdk_array)
4167 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4168 returned_attrs = decl_attributes (&type,
4169 chainon (returned_attrs, attrs),
4170 attr_flags);
4171 break;
4173 case cdk_array:
4175 tree itype = NULL_TREE;
4176 tree size = declarator->u.array.dimen;
4177 /* The index is a signed object `sizetype' bits wide. */
4178 tree index_type = c_common_signed_type (sizetype);
4180 array_ptr_quals = declarator->u.array.quals;
4181 array_ptr_attrs = declarator->u.array.attrs;
4182 array_parm_static = declarator->u.array.static_p;
4183 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4185 declarator = declarator->declarator;
4187 /* Check for some types that there cannot be arrays of. */
4189 if (VOID_TYPE_P (type))
4191 error ("declaration of %qs as array of voids", name);
4192 type = error_mark_node;
4195 if (TREE_CODE (type) == FUNCTION_TYPE)
4197 error ("declaration of %qs as array of functions", name);
4198 type = error_mark_node;
4201 if (pedantic && !in_system_header && flexible_array_type_p (type))
4202 pedwarn ("invalid use of structure with flexible array member");
4204 if (size == error_mark_node)
4205 type = error_mark_node;
4207 if (type == error_mark_node)
4208 continue;
4210 /* If size was specified, set ITYPE to a range-type for
4211 that size. Otherwise, ITYPE remains null. finish_decl
4212 may figure it out from an initial value. */
4214 if (size)
4216 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4217 lvalue. */
4218 STRIP_TYPE_NOPS (size);
4220 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4222 error ("size of array %qs has non-integer type", name);
4223 size = integer_one_node;
4226 if (pedantic && integer_zerop (size))
4227 pedwarn ("ISO C forbids zero-size array %qs", name);
4229 if (TREE_CODE (size) == INTEGER_CST)
4231 constant_expression_warning (size);
4232 if (tree_int_cst_sgn (size) < 0)
4234 error ("size of array %qs is negative", name);
4235 size = integer_one_node;
4238 else if ((decl_context == NORMAL || decl_context == FIELD)
4239 && current_scope == file_scope)
4241 error ("variably modified %qs at file scope", name);
4242 size = integer_one_node;
4244 else
4246 /* Make sure the array size remains visibly
4247 nonconstant even if it is (eg) a const variable
4248 with known value. */
4249 size_varies = 1;
4251 if (!flag_isoc99 && pedantic)
4253 if (TREE_CONSTANT (size))
4254 pedwarn ("ISO C90 forbids array %qs whose size "
4255 "can%'t be evaluated",
4256 name);
4257 else
4258 pedwarn ("ISO C90 forbids variable-size array %qs",
4259 name);
4263 if (integer_zerop (size))
4265 /* A zero-length array cannot be represented with
4266 an unsigned index type, which is what we'll
4267 get with build_index_type. Create an
4268 open-ended range instead. */
4269 itype = build_range_type (sizetype, size, NULL_TREE);
4271 else
4273 /* Arrange for the SAVE_EXPR on the inside of the
4274 MINUS_EXPR, which allows the -1 to get folded
4275 with the +1 that happens when building TYPE_SIZE. */
4276 if (size_varies)
4277 size = variable_size (size);
4279 /* Compute the maximum valid index, that is, size
4280 - 1. Do the calculation in index_type, so that
4281 if it is a variable the computations will be
4282 done in the proper mode. */
4283 itype = fold_build2 (MINUS_EXPR, index_type,
4284 convert (index_type, size),
4285 convert (index_type,
4286 size_one_node));
4288 /* If that overflowed, the array is too big. ???
4289 While a size of INT_MAX+1 technically shouldn't
4290 cause an overflow (because we subtract 1), the
4291 overflow is recorded during the conversion to
4292 index_type, before the subtraction. Handling
4293 this case seems like an unnecessary
4294 complication. */
4295 if (TREE_CODE (itype) == INTEGER_CST
4296 && TREE_OVERFLOW (itype))
4298 error ("size of array %qs is too large", name);
4299 type = error_mark_node;
4300 continue;
4303 itype = build_index_type (itype);
4306 else if (decl_context == FIELD)
4308 if (pedantic && !flag_isoc99 && !in_system_header)
4309 pedwarn ("ISO C90 does not support flexible array members");
4311 /* ISO C99 Flexible array members are effectively
4312 identical to GCC's zero-length array extension. */
4313 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4315 else if (decl_context == PARM)
4317 if (array_parm_vla_unspec_p)
4319 if (! orig_name)
4321 /* C99 6.7.5.2p4 */
4322 error ("%<[*]%> not allowed in other than a declaration");
4325 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4326 size_varies = 1;
4329 else if (decl_context == TYPENAME)
4331 if (array_parm_vla_unspec_p)
4333 /* The error is printed elsewhere. We use this to
4334 avoid messing up with incomplete array types of
4335 the same type, that would otherwise be modified
4336 below. */
4337 itype = build_range_type (sizetype, size_zero_node,
4338 NULL_TREE);
4342 /* Complain about arrays of incomplete types. */
4343 if (!COMPLETE_TYPE_P (type))
4345 error ("array type has incomplete element type");
4346 type = error_mark_node;
4348 else
4349 /* When itype is NULL, a shared incomplete array type is
4350 returned for all array of a given type. Elsewhere we
4351 make sure we don't complete that type before copying
4352 it, but here we want to make sure we don't ever
4353 modify the shared type, so we gcc_assert (itype)
4354 below. */
4355 type = build_array_type (type, itype);
4357 if (type != error_mark_node)
4359 if (size_varies)
4361 /* It is ok to modify type here even if itype is
4362 NULL: if size_varies, we're in a
4363 multi-dimensional array and the inner type has
4364 variable size, so the enclosing shared array type
4365 must too. */
4366 if (size && TREE_CODE (size) == INTEGER_CST)
4367 type
4368 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4369 C_TYPE_VARIABLE_SIZE (type) = 1;
4372 /* The GCC extension for zero-length arrays differs from
4373 ISO flexible array members in that sizeof yields
4374 zero. */
4375 if (size && integer_zerop (size))
4377 gcc_assert (itype);
4378 TYPE_SIZE (type) = bitsize_zero_node;
4379 TYPE_SIZE_UNIT (type) = size_zero_node;
4381 if (array_parm_vla_unspec_p)
4383 gcc_assert (itype);
4384 /* The type is complete. C99 6.7.5.2p4 */
4385 TYPE_SIZE (type) = bitsize_zero_node;
4386 TYPE_SIZE_UNIT (type) = size_zero_node;
4390 if (decl_context != PARM
4391 && (array_ptr_quals != TYPE_UNQUALIFIED
4392 || array_ptr_attrs != NULL_TREE
4393 || array_parm_static))
4395 error ("static or type qualifiers in non-parameter array declarator");
4396 array_ptr_quals = TYPE_UNQUALIFIED;
4397 array_ptr_attrs = NULL_TREE;
4398 array_parm_static = 0;
4400 break;
4402 case cdk_function:
4404 /* Say it's a definition only for the declarator closest
4405 to the identifier, apart possibly from some
4406 attributes. */
4407 bool really_funcdef = false;
4408 tree arg_types;
4409 if (funcdef_flag)
4411 const struct c_declarator *t = declarator->declarator;
4412 while (t->kind == cdk_attrs)
4413 t = t->declarator;
4414 really_funcdef = (t->kind == cdk_id);
4417 /* Declaring a function type. Make sure we have a valid
4418 type for the function to return. */
4419 if (type == error_mark_node)
4420 continue;
4422 size_varies = 0;
4424 /* Warn about some types functions can't return. */
4425 if (TREE_CODE (type) == FUNCTION_TYPE)
4427 error ("%qs declared as function returning a function", name);
4428 type = integer_type_node;
4430 if (TREE_CODE (type) == ARRAY_TYPE)
4432 error ("%qs declared as function returning an array", name);
4433 type = integer_type_node;
4436 /* Construct the function type and go to the next
4437 inner layer of declarator. */
4438 arg_info = declarator->u.arg_info;
4439 arg_types = grokparms (arg_info, really_funcdef);
4440 if (really_funcdef)
4441 put_pending_sizes (arg_info->pending_sizes);
4443 /* Type qualifiers before the return type of the function
4444 qualify the return type, not the function type. */
4445 if (type_quals)
4447 /* Type qualifiers on a function return type are
4448 normally permitted by the standard but have no
4449 effect, so give a warning at -Wreturn-type.
4450 Qualifiers on a void return type are banned on
4451 function definitions in ISO C; GCC used to used
4452 them for noreturn functions. */
4453 if (VOID_TYPE_P (type) && really_funcdef)
4454 pedwarn ("function definition has qualified void return type");
4455 else
4456 warning (OPT_Wreturn_type,
4457 "type qualifiers ignored on function return type");
4459 type = c_build_qualified_type (type, type_quals);
4461 type_quals = TYPE_UNQUALIFIED;
4463 type = build_function_type (type, arg_types);
4464 declarator = declarator->declarator;
4466 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4467 the formal parameter list of this FUNCTION_TYPE to point to
4468 the FUNCTION_TYPE node itself. */
4470 tree link;
4472 for (link = arg_info->tags;
4473 link;
4474 link = TREE_CHAIN (link))
4475 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4477 break;
4479 case cdk_pointer:
4481 /* Merge any constancy or volatility into the target type
4482 for the pointer. */
4484 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4485 && type_quals)
4486 pedwarn ("ISO C forbids qualified function types");
4487 if (type_quals)
4488 type = c_build_qualified_type (type, type_quals);
4489 size_varies = 0;
4491 /* When the pointed-to type involves components of variable size,
4492 care must be taken to ensure that the size evaluation code is
4493 emitted early enough to dominate all the possible later uses
4494 and late enough for the variables on which it depends to have
4495 been assigned.
4497 This is expected to happen automatically when the pointed-to
4498 type has a name/declaration of it's own, but special attention
4499 is required if the type is anonymous.
4501 We handle the NORMAL and FIELD contexts here by attaching an
4502 artificial TYPE_DECL to such pointed-to type. This forces the
4503 sizes evaluation at a safe point and ensures it is not deferred
4504 until e.g. within a deeper conditional context.
4506 We expect nothing to be needed here for PARM or TYPENAME.
4507 Pushing a TYPE_DECL at this point for TYPENAME would actually
4508 be incorrect, as we might be in the middle of an expression
4509 with side effects on the pointed-to type size "arguments" prior
4510 to the pointer declaration point and the fake TYPE_DECL in the
4511 enclosing context would force the size evaluation prior to the
4512 side effects. */
4514 if (!TYPE_NAME (type)
4515 && (decl_context == NORMAL || decl_context == FIELD)
4516 && variably_modified_type_p (type, NULL_TREE))
4518 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4519 DECL_ARTIFICIAL (decl) = 1;
4520 pushdecl (decl);
4521 finish_decl (decl, NULL_TREE, NULL_TREE);
4522 TYPE_NAME (type) = decl;
4525 type = build_pointer_type (type);
4527 /* Process type qualifiers (such as const or volatile)
4528 that were given inside the `*'. */
4529 type_quals = declarator->u.pointer_quals;
4531 declarator = declarator->declarator;
4532 break;
4534 default:
4535 gcc_unreachable ();
4539 /* Now TYPE has the actual type, apart from any qualifiers in
4540 TYPE_QUALS. */
4542 /* Check the type and width of a bit-field. */
4543 if (bitfield)
4544 check_bitfield_type_and_width (&type, width, orig_name);
4546 /* Did array size calculations overflow? */
4548 if (TREE_CODE (type) == ARRAY_TYPE
4549 && COMPLETE_TYPE_P (type)
4550 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4551 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4553 error ("size of array %qs is too large", name);
4554 /* If we proceed with the array type as it is, we'll eventually
4555 crash in tree_low_cst(). */
4556 type = error_mark_node;
4559 /* If this is declaring a typedef name, return a TYPE_DECL. */
4561 if (storage_class == csc_typedef)
4563 tree decl;
4564 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4565 && type_quals)
4566 pedwarn ("ISO C forbids qualified function types");
4567 if (type_quals)
4568 type = c_build_qualified_type (type, type_quals);
4569 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4570 if (declspecs->explicit_signed_p)
4571 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4572 decl_attributes (&decl, returned_attrs, 0);
4573 if (declspecs->inline_p)
4574 pedwarn ("typedef %q+D declared %<inline%>", decl);
4575 return decl;
4578 /* If this is a type name (such as, in a cast or sizeof),
4579 compute the type and return it now. */
4581 if (decl_context == TYPENAME)
4583 /* Note that the grammar rejects storage classes in typenames
4584 and fields. */
4585 gcc_assert (storage_class == csc_none && !threadp
4586 && !declspecs->inline_p);
4587 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4588 && type_quals)
4589 pedwarn ("ISO C forbids const or volatile function types");
4590 if (type_quals)
4591 type = c_build_qualified_type (type, type_quals);
4592 decl_attributes (&type, returned_attrs, 0);
4593 return type;
4596 if (pedantic && decl_context == FIELD
4597 && variably_modified_type_p (type, NULL_TREE))
4599 /* C99 6.7.2.1p8 */
4600 pedwarn ("a member of a structure or union cannot have a variably modified type");
4603 /* Aside from typedefs and type names (handle above),
4604 `void' at top level (not within pointer)
4605 is allowed only in public variables.
4606 We don't complain about parms either, but that is because
4607 a better error message can be made later. */
4609 if (VOID_TYPE_P (type) && decl_context != PARM
4610 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4611 && (storage_class == csc_extern
4612 || (current_scope == file_scope
4613 && !(storage_class == csc_static
4614 || storage_class == csc_register)))))
4616 error ("variable or field %qs declared void", name);
4617 type = integer_type_node;
4620 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4621 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4624 tree decl;
4626 if (decl_context == PARM)
4628 tree type_as_written;
4629 tree promoted_type;
4631 /* A parameter declared as an array of T is really a pointer to T.
4632 One declared as a function is really a pointer to a function. */
4634 if (TREE_CODE (type) == ARRAY_TYPE)
4636 /* Transfer const-ness of array into that of type pointed to. */
4637 type = TREE_TYPE (type);
4638 if (type_quals)
4639 type = c_build_qualified_type (type, type_quals);
4640 type = build_pointer_type (type);
4641 type_quals = array_ptr_quals;
4643 /* We don't yet implement attributes in this context. */
4644 if (array_ptr_attrs != NULL_TREE)
4645 warning (OPT_Wattributes,
4646 "attributes in parameter array declarator ignored");
4648 size_varies = 0;
4650 else if (TREE_CODE (type) == FUNCTION_TYPE)
4652 if (pedantic && type_quals)
4653 pedwarn ("ISO C forbids qualified function types");
4654 if (type_quals)
4655 type = c_build_qualified_type (type, type_quals);
4656 type = build_pointer_type (type);
4657 type_quals = TYPE_UNQUALIFIED;
4659 else if (type_quals)
4660 type = c_build_qualified_type (type, type_quals);
4662 type_as_written = type;
4664 decl = build_decl (PARM_DECL, declarator->u.id, type);
4665 if (size_varies)
4666 C_DECL_VARIABLE_SIZE (decl) = 1;
4668 /* Compute the type actually passed in the parmlist,
4669 for the case where there is no prototype.
4670 (For example, shorts and chars are passed as ints.)
4671 When there is a prototype, this is overridden later. */
4673 if (type == error_mark_node)
4674 promoted_type = type;
4675 else
4676 promoted_type = c_type_promotes_to (type);
4678 DECL_ARG_TYPE (decl) = promoted_type;
4679 if (declspecs->inline_p)
4680 pedwarn ("parameter %q+D declared %<inline%>", decl);
4682 else if (decl_context == FIELD)
4684 /* Note that the grammar rejects storage classes in typenames
4685 and fields. */
4686 gcc_assert (storage_class == csc_none && !threadp
4687 && !declspecs->inline_p);
4689 /* Structure field. It may not be a function. */
4691 if (TREE_CODE (type) == FUNCTION_TYPE)
4693 error ("field %qs declared as a function", name);
4694 type = build_pointer_type (type);
4696 else if (TREE_CODE (type) != ERROR_MARK
4697 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4699 error ("field %qs has incomplete type", name);
4700 type = error_mark_node;
4702 type = c_build_qualified_type (type, type_quals);
4703 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4704 DECL_NONADDRESSABLE_P (decl) = bitfield;
4706 if (size_varies)
4707 C_DECL_VARIABLE_SIZE (decl) = 1;
4709 else if (TREE_CODE (type) == FUNCTION_TYPE)
4711 if (storage_class == csc_register || threadp)
4713 error ("invalid storage class for function %qs", name);
4715 else if (current_scope != file_scope)
4717 /* Function declaration not at file scope. Storage
4718 classes other than `extern' are not allowed, C99
4719 6.7.1p5, and `extern' makes no difference. However,
4720 GCC allows 'auto', perhaps with 'inline', to support
4721 nested functions. */
4722 if (storage_class == csc_auto)
4724 if (pedantic)
4725 pedwarn ("invalid storage class for function %qs", name);
4727 else if (storage_class == csc_static)
4729 error ("invalid storage class for function %qs", name);
4730 if (funcdef_flag)
4731 storage_class = declspecs->storage_class = csc_none;
4732 else
4733 return 0;
4737 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4738 decl = build_decl_attribute_variant (decl, decl_attr);
4740 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4742 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4743 pedwarn ("ISO C forbids qualified function types");
4745 /* GNU C interprets a volatile-qualified function type to indicate
4746 that the function does not return. */
4747 if ((type_quals & TYPE_QUAL_VOLATILE)
4748 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4749 warning (0, "%<noreturn%> function returns non-void value");
4751 /* Every function declaration is an external reference
4752 (DECL_EXTERNAL) except for those which are not at file
4753 scope and are explicitly declared "auto". This is
4754 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4755 GCC to signify a forward declaration of a nested function. */
4756 if (storage_class == csc_auto && current_scope != file_scope)
4757 DECL_EXTERNAL (decl) = 0;
4758 else
4759 DECL_EXTERNAL (decl) = 1;
4761 /* Record absence of global scope for `static' or `auto'. */
4762 TREE_PUBLIC (decl)
4763 = !(storage_class == csc_static || storage_class == csc_auto);
4765 /* For a function definition, record the argument information
4766 block where store_parm_decls will look for it. */
4767 if (funcdef_flag)
4768 current_function_arg_info = arg_info;
4770 if (declspecs->default_int_p)
4771 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4773 /* Record presence of `inline', if it is reasonable. */
4774 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4776 if (declspecs->inline_p)
4777 pedwarn ("cannot inline function %<main%>");
4779 else if (declspecs->inline_p)
4781 /* Record that the function is declared `inline'. */
4782 DECL_DECLARED_INLINE_P (decl) = 1;
4784 /* Do not mark bare declarations as DECL_INLINE. Doing so
4785 in the presence of multiple declarations can result in
4786 the abstract origin pointing between the declarations,
4787 which will confuse dwarf2out. */
4788 if (initialized)
4790 DECL_INLINE (decl) = 1;
4791 if (storage_class == csc_extern)
4792 current_extern_inline = 1;
4795 /* If -finline-functions, assume it can be inlined. This does
4796 two things: let the function be deferred until it is actually
4797 needed, and let dwarf2 know that the function is inlinable. */
4798 else if (flag_inline_trees == 2 && initialized)
4799 DECL_INLINE (decl) = 1;
4801 else
4803 /* It's a variable. */
4804 /* An uninitialized decl with `extern' is a reference. */
4805 int extern_ref = !initialized && storage_class == csc_extern;
4807 type = c_build_qualified_type (type, type_quals);
4809 /* C99 6.2.2p7: It is invalid (compile-time undefined
4810 behavior) to create an 'extern' declaration for a
4811 variable if there is a global declaration that is
4812 'static' and the global declaration is not visible.
4813 (If the static declaration _is_ currently visible,
4814 the 'extern' declaration is taken to refer to that decl.) */
4815 if (extern_ref && current_scope != file_scope)
4817 tree global_decl = identifier_global_value (declarator->u.id);
4818 tree visible_decl = lookup_name (declarator->u.id);
4820 if (global_decl
4821 && global_decl != visible_decl
4822 && TREE_CODE (global_decl) == VAR_DECL
4823 && !TREE_PUBLIC (global_decl))
4824 error ("variable previously declared %<static%> redeclared "
4825 "%<extern%>");
4828 decl = build_decl (VAR_DECL, declarator->u.id, type);
4829 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4830 if (size_varies)
4831 C_DECL_VARIABLE_SIZE (decl) = 1;
4833 if (declspecs->inline_p)
4834 pedwarn ("variable %q+D declared %<inline%>", decl);
4836 /* At file scope, an initialized extern declaration may follow
4837 a static declaration. In that case, DECL_EXTERNAL will be
4838 reset later in start_decl. */
4839 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4841 /* At file scope, the presence of a `static' or `register' storage
4842 class specifier, or the absence of all storage class specifiers
4843 makes this declaration a definition (perhaps tentative). Also,
4844 the absence of `static' makes it public. */
4845 if (current_scope == file_scope)
4847 TREE_PUBLIC (decl) = storage_class != csc_static;
4848 TREE_STATIC (decl) = !extern_ref;
4850 /* Not at file scope, only `static' makes a static definition. */
4851 else
4853 TREE_STATIC (decl) = (storage_class == csc_static);
4854 TREE_PUBLIC (decl) = extern_ref;
4857 if (threadp)
4859 if (targetm.have_tls)
4860 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
4861 else
4862 /* A mere warning is sure to result in improper semantics
4863 at runtime. Don't bother to allow this to compile. */
4864 error ("thread-local storage not supported for this target");
4868 if (storage_class == csc_extern
4869 && variably_modified_type_p (type, NULL_TREE))
4871 /* C99 6.7.5.2p2 */
4872 error ("object with variably modified type must have no linkage");
4875 /* Record `register' declaration for warnings on &
4876 and in case doing stupid register allocation. */
4878 if (storage_class == csc_register)
4880 C_DECL_REGISTER (decl) = 1;
4881 DECL_REGISTER (decl) = 1;
4884 /* Record constancy and volatility. */
4885 c_apply_type_quals_to_decl (type_quals, decl);
4887 /* If a type has volatile components, it should be stored in memory.
4888 Otherwise, the fact that those components are volatile
4889 will be ignored, and would even crash the compiler.
4890 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
4891 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
4892 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
4893 || TREE_CODE (decl) == RESULT_DECL))
4895 /* It is not an error for a structure with volatile fields to
4896 be declared register, but reset DECL_REGISTER since it
4897 cannot actually go in a register. */
4898 int was_reg = C_DECL_REGISTER (decl);
4899 C_DECL_REGISTER (decl) = 0;
4900 DECL_REGISTER (decl) = 0;
4901 c_mark_addressable (decl);
4902 C_DECL_REGISTER (decl) = was_reg;
4905 /* This is the earliest point at which we might know the assembler
4906 name of a variable. Thus, if it's known before this, die horribly. */
4907 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4909 decl_attributes (&decl, returned_attrs, 0);
4911 return decl;
4915 /* Decode the parameter-list info for a function type or function definition.
4916 The argument is the value returned by `get_parm_info' (or made in c-parse.c
4917 if there is an identifier list instead of a parameter decl list).
4918 These two functions are separate because when a function returns
4919 or receives functions then each is called multiple times but the order
4920 of calls is different. The last call to `grokparms' is always the one
4921 that contains the formal parameter names of a function definition.
4923 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4925 FUNCDEF_FLAG is true for a function definition, false for
4926 a mere declaration. A nonempty identifier-list gets an error message
4927 when FUNCDEF_FLAG is false. */
4929 static tree
4930 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4932 tree arg_types = arg_info->types;
4934 if (funcdef_flag && arg_info->had_vla_unspec)
4936 /* A function definition isn't function prototype scope C99 6.2.1p4. */
4937 /* C99 6.7.5.2p4 */
4938 error ("%<[*]%> not allowed in other than function prototype scope");
4941 if (arg_types == 0 && !funcdef_flag && !in_system_header)
4942 warning (OPT_Wstrict_prototypes,
4943 "function declaration isn%'t a prototype");
4945 if (arg_types == error_mark_node)
4946 return 0; /* don't set TYPE_ARG_TYPES in this case */
4948 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4950 if (!funcdef_flag)
4951 pedwarn ("parameter names (without types) in function declaration");
4953 arg_info->parms = arg_info->types;
4954 arg_info->types = 0;
4955 return 0;
4957 else
4959 tree parm, type, typelt;
4960 unsigned int parmno;
4962 /* If there is a parameter of incomplete type in a definition,
4963 this is an error. In a declaration this is valid, and a
4964 struct or union type may be completed later, before any calls
4965 or definition of the function. In the case where the tag was
4966 first declared within the parameter list, a warning has
4967 already been given. If a parameter has void type, then
4968 however the function cannot be defined or called, so
4969 warn. */
4971 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4972 parm;
4973 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4975 type = TREE_VALUE (typelt);
4976 if (type == error_mark_node)
4977 continue;
4979 if (!COMPLETE_TYPE_P (type))
4981 if (funcdef_flag)
4983 if (DECL_NAME (parm))
4984 error ("parameter %u (%q+D) has incomplete type",
4985 parmno, parm);
4986 else
4987 error ("%Jparameter %u has incomplete type",
4988 parm, parmno);
4990 TREE_VALUE (typelt) = error_mark_node;
4991 TREE_TYPE (parm) = error_mark_node;
4993 else if (VOID_TYPE_P (type))
4995 if (DECL_NAME (parm))
4996 warning (0, "parameter %u (%q+D) has void type",
4997 parmno, parm);
4998 else
4999 warning (0, "%Jparameter %u has void type",
5000 parm, parmno);
5004 if (DECL_NAME (parm) && TREE_USED (parm))
5005 warn_if_shadowing (parm);
5007 return arg_types;
5011 /* Take apart the current scope and return a c_arg_info structure with
5012 info on a parameter list just parsed.
5014 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5016 ELLIPSIS being true means the argument list ended in '...' so don't
5017 append a sentinel (void_list_node) to the end of the type-list. */
5019 struct c_arg_info *
5020 get_parm_info (bool ellipsis)
5022 struct c_binding *b = current_scope->bindings;
5023 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5024 struct c_arg_info);
5025 tree parms = 0;
5026 tree tags = 0;
5027 tree types = 0;
5028 tree others = 0;
5030 static bool explained_incomplete_types = false;
5031 bool gave_void_only_once_err = false;
5033 arg_info->parms = 0;
5034 arg_info->tags = 0;
5035 arg_info->types = 0;
5036 arg_info->others = 0;
5037 arg_info->pending_sizes = 0;
5038 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5040 /* The bindings in this scope must not get put into a block.
5041 We will take care of deleting the binding nodes. */
5042 current_scope->bindings = 0;
5044 /* This function is only called if there was *something* on the
5045 parameter list. */
5046 gcc_assert (b);
5048 /* A parameter list consisting solely of 'void' indicates that the
5049 function takes no arguments. But if the 'void' is qualified
5050 (by 'const' or 'volatile'), or has a storage class specifier
5051 ('register'), then the behavior is undefined; issue an error.
5052 Typedefs for 'void' are OK (see DR#157). */
5053 if (b->prev == 0 /* one binding */
5054 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5055 && !DECL_NAME (b->decl) /* anonymous */
5056 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5058 if (TREE_THIS_VOLATILE (b->decl)
5059 || TREE_READONLY (b->decl)
5060 || C_DECL_REGISTER (b->decl))
5061 error ("%<void%> as only parameter may not be qualified");
5063 /* There cannot be an ellipsis. */
5064 if (ellipsis)
5065 error ("%<void%> must be the only parameter");
5067 arg_info->types = void_list_node;
5068 return arg_info;
5071 if (!ellipsis)
5072 types = void_list_node;
5074 /* Break up the bindings list into parms, tags, types, and others;
5075 apply sanity checks; purge the name-to-decl bindings. */
5076 while (b)
5078 tree decl = b->decl;
5079 tree type = TREE_TYPE (decl);
5080 const char *keyword;
5082 switch (TREE_CODE (decl))
5084 case PARM_DECL:
5085 if (b->id)
5087 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5088 I_SYMBOL_BINDING (b->id) = b->shadowed;
5091 /* Check for forward decls that never got their actual decl. */
5092 if (TREE_ASM_WRITTEN (decl))
5093 error ("parameter %q+D has just a forward declaration", decl);
5094 /* Check for (..., void, ...) and issue an error. */
5095 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5097 if (!gave_void_only_once_err)
5099 error ("%<void%> must be the only parameter");
5100 gave_void_only_once_err = true;
5103 else
5105 /* Valid parameter, add it to the list. */
5106 TREE_CHAIN (decl) = parms;
5107 parms = decl;
5109 /* Since there is a prototype, args are passed in their
5110 declared types. The back end may override this later. */
5111 DECL_ARG_TYPE (decl) = type;
5112 types = tree_cons (0, type, types);
5114 break;
5116 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5117 case UNION_TYPE: keyword = "union"; goto tag;
5118 case RECORD_TYPE: keyword = "struct"; goto tag;
5119 tag:
5120 /* Types may not have tag-names, in which case the type
5121 appears in the bindings list with b->id NULL. */
5122 if (b->id)
5124 gcc_assert (I_TAG_BINDING (b->id) == b);
5125 I_TAG_BINDING (b->id) = b->shadowed;
5128 /* Warn about any struct, union or enum tags defined in a
5129 parameter list. The scope of such types is limited to
5130 the parameter list, which is rarely if ever desirable
5131 (it's impossible to call such a function with type-
5132 correct arguments). An anonymous union parm type is
5133 meaningful as a GNU extension, so don't warn for that. */
5134 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5136 if (b->id)
5137 /* The %s will be one of 'struct', 'union', or 'enum'. */
5138 warning (0, "%<%s %E%> declared inside parameter list",
5139 keyword, b->id);
5140 else
5141 /* The %s will be one of 'struct', 'union', or 'enum'. */
5142 warning (0, "anonymous %s declared inside parameter list",
5143 keyword);
5145 if (!explained_incomplete_types)
5147 warning (0, "its scope is only this definition or declaration,"
5148 " which is probably not what you want");
5149 explained_incomplete_types = true;
5153 tags = tree_cons (b->id, decl, tags);
5154 break;
5156 case CONST_DECL:
5157 case TYPE_DECL:
5158 case FUNCTION_DECL:
5159 /* CONST_DECLs appear here when we have an embedded enum,
5160 and TYPE_DECLs appear here when we have an embedded struct
5161 or union. No warnings for this - we already warned about the
5162 type itself. FUNCTION_DECLs appear when there is an implicit
5163 function declaration in the parameter list. */
5165 TREE_CHAIN (decl) = others;
5166 others = decl;
5167 /* fall through */
5169 case ERROR_MARK:
5170 /* error_mark_node appears here when we have an undeclared
5171 variable. Just throw it away. */
5172 if (b->id)
5174 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5175 I_SYMBOL_BINDING (b->id) = b->shadowed;
5177 break;
5179 /* Other things that might be encountered. */
5180 case LABEL_DECL:
5181 case VAR_DECL:
5182 default:
5183 gcc_unreachable ();
5186 b = free_binding_and_advance (b);
5189 arg_info->parms = parms;
5190 arg_info->tags = tags;
5191 arg_info->types = types;
5192 arg_info->others = others;
5193 arg_info->pending_sizes = get_pending_sizes ();
5194 return arg_info;
5197 /* Get the struct, enum or union (CODE says which) with tag NAME.
5198 Define the tag as a forward-reference if it is not defined.
5199 Return a c_typespec structure for the type specifier. */
5201 struct c_typespec
5202 parser_xref_tag (enum tree_code code, tree name)
5204 struct c_typespec ret;
5205 /* If a cross reference is requested, look up the type
5206 already defined for this tag and return it. */
5208 tree ref = lookup_tag (code, name, 0);
5209 /* If this is the right type of tag, return what we found.
5210 (This reference will be shadowed by shadow_tag later if appropriate.)
5211 If this is the wrong type of tag, do not return it. If it was the
5212 wrong type in the same scope, we will have had an error
5213 message already; if in a different scope and declaring
5214 a name, pending_xref_error will give an error message; but if in a
5215 different scope and not declaring a name, this tag should
5216 shadow the previous declaration of a different type of tag, and
5217 this would not work properly if we return the reference found.
5218 (For example, with "struct foo" in an outer scope, "union foo;"
5219 must shadow that tag with a new one of union type.) */
5220 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5221 if (ref && TREE_CODE (ref) == code)
5223 ret.spec = ref;
5224 return ret;
5227 /* If no such tag is yet defined, create a forward-reference node
5228 and record it as the "definition".
5229 When a real declaration of this type is found,
5230 the forward-reference will be altered into a real type. */
5232 ref = make_node (code);
5233 if (code == ENUMERAL_TYPE)
5235 /* Give the type a default layout like unsigned int
5236 to avoid crashing if it does not get defined. */
5237 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5238 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5239 TYPE_USER_ALIGN (ref) = 0;
5240 TYPE_UNSIGNED (ref) = 1;
5241 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5242 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5243 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5246 pushtag (name, ref);
5248 ret.spec = ref;
5249 return ret;
5252 /* Get the struct, enum or union (CODE says which) with tag NAME.
5253 Define the tag as a forward-reference if it is not defined.
5254 Return a tree for the type. */
5256 tree
5257 xref_tag (enum tree_code code, tree name)
5259 return parser_xref_tag (code, name).spec;
5262 /* Make sure that the tag NAME is defined *in the current scope*
5263 at least as a forward reference.
5264 CODE says which kind of tag NAME ought to be. */
5266 tree
5267 start_struct (enum tree_code code, tree name)
5269 /* If there is already a tag defined at this scope
5270 (as a forward reference), just return it. */
5272 tree ref = 0;
5274 if (name != 0)
5275 ref = lookup_tag (code, name, 1);
5276 if (ref && TREE_CODE (ref) == code)
5278 if (TYPE_SIZE (ref))
5280 if (code == UNION_TYPE)
5281 error ("redefinition of %<union %E%>", name);
5282 else
5283 error ("redefinition of %<struct %E%>", name);
5285 else if (C_TYPE_BEING_DEFINED (ref))
5287 if (code == UNION_TYPE)
5288 error ("nested redefinition of %<union %E%>", name);
5289 else
5290 error ("nested redefinition of %<struct %E%>", name);
5293 else
5295 /* Otherwise create a forward-reference just so the tag is in scope. */
5297 ref = make_node (code);
5298 pushtag (name, ref);
5301 C_TYPE_BEING_DEFINED (ref) = 1;
5302 TYPE_PACKED (ref) = flag_pack_struct;
5303 return ref;
5306 /* Process the specs, declarator and width (NULL if omitted)
5307 of a structure component, returning a FIELD_DECL node.
5308 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5310 This is done during the parsing of the struct declaration.
5311 The FIELD_DECL nodes are chained together and the lot of them
5312 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5314 tree
5315 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5316 tree width)
5318 tree value;
5320 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5321 && width == NULL_TREE)
5323 /* This is an unnamed decl.
5325 If we have something of the form "union { list } ;" then this
5326 is the anonymous union extension. Similarly for struct.
5328 If this is something of the form "struct foo;", then
5329 If MS extensions are enabled, this is handled as an
5330 anonymous struct.
5331 Otherwise this is a forward declaration of a structure tag.
5333 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5334 If MS extensions are enabled and foo names a structure, then
5335 again this is an anonymous struct.
5336 Otherwise this is an error.
5338 Oh what a horrid tangled web we weave. I wonder if MS consciously
5339 took this from Plan 9 or if it was an accident of implementation
5340 that took root before someone noticed the bug... */
5342 tree type = declspecs->type;
5343 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5344 || TREE_CODE (type) == UNION_TYPE);
5345 bool ok = false;
5347 if (type_ok
5348 && (flag_ms_extensions || !declspecs->typedef_p))
5350 if (flag_ms_extensions)
5351 ok = true;
5352 else if (flag_iso)
5353 ok = false;
5354 else if (TYPE_NAME (type) == NULL)
5355 ok = true;
5356 else
5357 ok = false;
5359 if (!ok)
5361 pedwarn ("declaration does not declare anything");
5362 return NULL_TREE;
5364 if (pedantic)
5365 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5368 value = grokdeclarator (declarator, declspecs, FIELD, false,
5369 width ? &width : NULL);
5371 finish_decl (value, NULL_TREE, NULL_TREE);
5372 DECL_INITIAL (value) = width;
5374 return value;
5377 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5378 the list such that this does not present a problem later. */
5380 static void
5381 detect_field_duplicates (tree fieldlist)
5383 tree x, y;
5384 int timeout = 10;
5386 /* First, see if there are more than "a few" fields.
5387 This is trivially true if there are zero or one fields. */
5388 if (!fieldlist)
5389 return;
5390 x = TREE_CHAIN (fieldlist);
5391 if (!x)
5392 return;
5393 do {
5394 timeout--;
5395 x = TREE_CHAIN (x);
5396 } while (timeout > 0 && x);
5398 /* If there were "few" fields, avoid the overhead of allocating
5399 a hash table. Instead just do the nested traversal thing. */
5400 if (timeout > 0)
5402 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5403 if (DECL_NAME (x))
5405 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5406 if (DECL_NAME (y) == DECL_NAME (x))
5408 error ("duplicate member %q+D", x);
5409 DECL_NAME (x) = NULL_TREE;
5413 else
5415 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5416 void **slot;
5418 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5419 if ((y = DECL_NAME (x)) != 0)
5421 slot = htab_find_slot (htab, y, INSERT);
5422 if (*slot)
5424 error ("duplicate member %q+D", x);
5425 DECL_NAME (x) = NULL_TREE;
5427 *slot = y;
5430 htab_delete (htab);
5434 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5435 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5436 ATTRIBUTES are attributes to be applied to the structure. */
5438 tree
5439 finish_struct (tree t, tree fieldlist, tree attributes)
5441 tree x;
5442 bool toplevel = file_scope == current_scope;
5443 int saw_named_field;
5445 /* If this type was previously laid out as a forward reference,
5446 make sure we lay it out again. */
5448 TYPE_SIZE (t) = 0;
5450 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5452 if (pedantic)
5454 for (x = fieldlist; x; x = TREE_CHAIN (x))
5455 if (DECL_NAME (x) != 0)
5456 break;
5458 if (x == 0)
5460 if (TREE_CODE (t) == UNION_TYPE)
5462 if (fieldlist)
5463 pedwarn ("union has no named members");
5464 else
5465 pedwarn ("union has no members");
5467 else
5469 if (fieldlist)
5470 pedwarn ("struct has no named members");
5471 else
5472 pedwarn ("struct has no members");
5477 /* Install struct as DECL_CONTEXT of each field decl.
5478 Also process specified field sizes, found in the DECL_INITIAL,
5479 storing 0 there after the type has been changed to precision equal
5480 to its width, rather than the precision of the specified standard
5481 type. (Correct layout requires the original type to have been preserved
5482 until now.) */
5484 saw_named_field = 0;
5485 for (x = fieldlist; x; x = TREE_CHAIN (x))
5487 if (TREE_TYPE (x) == error_mark_node)
5488 continue;
5490 DECL_CONTEXT (x) = t;
5492 if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
5493 DECL_PACKED (x) = 1;
5495 /* If any field is const, the structure type is pseudo-const. */
5496 if (TREE_READONLY (x))
5497 C_TYPE_FIELDS_READONLY (t) = 1;
5498 else
5500 /* A field that is pseudo-const makes the structure likewise. */
5501 tree t1 = TREE_TYPE (x);
5502 while (TREE_CODE (t1) == ARRAY_TYPE)
5503 t1 = TREE_TYPE (t1);
5504 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5505 && C_TYPE_FIELDS_READONLY (t1))
5506 C_TYPE_FIELDS_READONLY (t) = 1;
5509 /* Any field that is volatile means variables of this type must be
5510 treated in some ways as volatile. */
5511 if (TREE_THIS_VOLATILE (x))
5512 C_TYPE_FIELDS_VOLATILE (t) = 1;
5514 /* Any field of nominal variable size implies structure is too. */
5515 if (C_DECL_VARIABLE_SIZE (x))
5516 C_TYPE_VARIABLE_SIZE (t) = 1;
5518 if (DECL_INITIAL (x))
5520 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5521 DECL_SIZE (x) = bitsize_int (width);
5522 DECL_BIT_FIELD (x) = 1;
5523 SET_DECL_C_BIT_FIELD (x);
5526 /* Detect flexible array member in an invalid context. */
5527 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5528 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5529 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5530 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5532 if (TREE_CODE (t) == UNION_TYPE)
5534 error ("%Jflexible array member in union", x);
5535 TREE_TYPE (x) = error_mark_node;
5537 else if (TREE_CHAIN (x) != NULL_TREE)
5539 error ("%Jflexible array member not at end of struct", x);
5540 TREE_TYPE (x) = error_mark_node;
5542 else if (!saw_named_field)
5544 error ("%Jflexible array member in otherwise empty struct", x);
5545 TREE_TYPE (x) = error_mark_node;
5549 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5550 && flexible_array_type_p (TREE_TYPE (x)))
5551 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5553 if (DECL_NAME (x))
5554 saw_named_field = 1;
5557 detect_field_duplicates (fieldlist);
5559 /* Now we have the nearly final fieldlist. Record it,
5560 then lay out the structure or union (including the fields). */
5562 TYPE_FIELDS (t) = fieldlist;
5564 layout_type (t);
5566 /* Give bit-fields their proper types. */
5568 tree *fieldlistp = &fieldlist;
5569 while (*fieldlistp)
5570 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5571 && TREE_TYPE (*fieldlistp) != error_mark_node)
5573 unsigned HOST_WIDE_INT width
5574 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5575 tree type = TREE_TYPE (*fieldlistp);
5576 if (width != TYPE_PRECISION (type))
5578 TREE_TYPE (*fieldlistp)
5579 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5580 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5582 DECL_INITIAL (*fieldlistp) = 0;
5584 else
5585 fieldlistp = &TREE_CHAIN (*fieldlistp);
5588 /* Now we have the truly final field list.
5589 Store it in this type and in the variants. */
5591 TYPE_FIELDS (t) = fieldlist;
5593 /* If there are lots of fields, sort so we can look through them fast.
5594 We arbitrarily consider 16 or more elts to be "a lot". */
5597 int len = 0;
5599 for (x = fieldlist; x; x = TREE_CHAIN (x))
5601 if (len > 15 || DECL_NAME (x) == NULL)
5602 break;
5603 len += 1;
5606 if (len > 15)
5608 tree *field_array;
5609 struct lang_type *space;
5610 struct sorted_fields_type *space2;
5612 len += list_length (x);
5614 /* Use the same allocation policy here that make_node uses, to
5615 ensure that this lives as long as the rest of the struct decl.
5616 All decls in an inline function need to be saved. */
5618 space = GGC_CNEW (struct lang_type);
5619 space2 = GGC_NEWVAR (struct sorted_fields_type,
5620 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5622 len = 0;
5623 space->s = space2;
5624 field_array = &space2->elts[0];
5625 for (x = fieldlist; x; x = TREE_CHAIN (x))
5627 field_array[len++] = x;
5629 /* If there is anonymous struct or union, break out of the loop. */
5630 if (DECL_NAME (x) == NULL)
5631 break;
5633 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5634 if (x == NULL)
5636 TYPE_LANG_SPECIFIC (t) = space;
5637 TYPE_LANG_SPECIFIC (t)->s->len = len;
5638 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5639 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5644 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5646 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5647 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5648 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5649 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5650 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5653 /* If this was supposed to be a transparent union, but we can't
5654 make it one, warn and turn off the flag. */
5655 if (TREE_CODE (t) == UNION_TYPE
5656 && TYPE_TRANSPARENT_UNION (t)
5657 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5659 TYPE_TRANSPARENT_UNION (t) = 0;
5660 warning (0, "union cannot be made transparent");
5663 /* If this structure or union completes the type of any previous
5664 variable declaration, lay it out and output its rtl. */
5665 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5667 x = TREE_CHAIN (x))
5669 tree decl = TREE_VALUE (x);
5670 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5671 layout_array_type (TREE_TYPE (decl));
5672 if (TREE_CODE (decl) != TYPE_DECL)
5674 layout_decl (decl, 0);
5675 if (c_dialect_objc ())
5676 objc_check_decl (decl);
5677 rest_of_decl_compilation (decl, toplevel, 0);
5678 if (!toplevel)
5679 expand_decl (decl);
5682 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5684 /* Finish debugging output for this type. */
5685 rest_of_type_compilation (t, toplevel);
5687 /* If we're inside a function proper, i.e. not file-scope and not still
5688 parsing parameters, then arrange for the size of a variable sized type
5689 to be bound now. */
5690 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5691 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5693 return t;
5696 /* Lay out the type T, and its element type, and so on. */
5698 static void
5699 layout_array_type (tree t)
5701 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5702 layout_array_type (TREE_TYPE (t));
5703 layout_type (t);
5706 /* Begin compiling the definition of an enumeration type.
5707 NAME is its name (or null if anonymous).
5708 Returns the type object, as yet incomplete.
5709 Also records info about it so that build_enumerator
5710 may be used to declare the individual values as they are read. */
5712 tree
5713 start_enum (tree name)
5715 tree enumtype = 0;
5717 /* If this is the real definition for a previous forward reference,
5718 fill in the contents in the same object that used to be the
5719 forward reference. */
5721 if (name != 0)
5722 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5724 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5726 enumtype = make_node (ENUMERAL_TYPE);
5727 pushtag (name, enumtype);
5730 if (C_TYPE_BEING_DEFINED (enumtype))
5731 error ("nested redefinition of %<enum %E%>", name);
5733 C_TYPE_BEING_DEFINED (enumtype) = 1;
5735 if (TYPE_VALUES (enumtype) != 0)
5737 /* This enum is a named one that has been declared already. */
5738 error ("redeclaration of %<enum %E%>", name);
5740 /* Completely replace its old definition.
5741 The old enumerators remain defined, however. */
5742 TYPE_VALUES (enumtype) = 0;
5745 enum_next_value = integer_zero_node;
5746 enum_overflow = 0;
5748 if (flag_short_enums)
5749 TYPE_PACKED (enumtype) = 1;
5751 return enumtype;
5754 /* After processing and defining all the values of an enumeration type,
5755 install their decls in the enumeration type and finish it off.
5756 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5757 and ATTRIBUTES are the specified attributes.
5758 Returns ENUMTYPE. */
5760 tree
5761 finish_enum (tree enumtype, tree values, tree attributes)
5763 tree pair, tem;
5764 tree minnode = 0, maxnode = 0;
5765 int precision, unsign;
5766 bool toplevel = (file_scope == current_scope);
5767 struct lang_type *lt;
5769 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5771 /* Calculate the maximum value of any enumerator in this type. */
5773 if (values == error_mark_node)
5774 minnode = maxnode = integer_zero_node;
5775 else
5777 minnode = maxnode = TREE_VALUE (values);
5778 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5780 tree value = TREE_VALUE (pair);
5781 if (tree_int_cst_lt (maxnode, value))
5782 maxnode = value;
5783 if (tree_int_cst_lt (value, minnode))
5784 minnode = value;
5788 /* Construct the final type of this enumeration. It is the same
5789 as one of the integral types - the narrowest one that fits, except
5790 that normally we only go as narrow as int - and signed iff any of
5791 the values are negative. */
5792 unsign = (tree_int_cst_sgn (minnode) >= 0);
5793 precision = MAX (min_precision (minnode, unsign),
5794 min_precision (maxnode, unsign));
5796 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5798 tem = c_common_type_for_size (precision, unsign);
5799 if (tem == NULL)
5801 warning (0, "enumeration values exceed range of largest integer");
5802 tem = long_long_integer_type_node;
5805 else
5806 tem = unsign ? unsigned_type_node : integer_type_node;
5808 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5809 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5810 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5811 TYPE_SIZE (enumtype) = 0;
5813 /* If the precision of the type was specific with an attribute and it
5814 was too small, give an error. Otherwise, use it. */
5815 if (TYPE_PRECISION (enumtype))
5817 if (precision > TYPE_PRECISION (enumtype))
5818 error ("specified mode too small for enumeral values");
5820 else
5821 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5823 layout_type (enumtype);
5825 if (values != error_mark_node)
5827 /* Change the type of the enumerators to be the enum type. We
5828 need to do this irrespective of the size of the enum, for
5829 proper type checking. Replace the DECL_INITIALs of the
5830 enumerators, and the value slots of the list, with copies
5831 that have the enum type; they cannot be modified in place
5832 because they may be shared (e.g. integer_zero_node) Finally,
5833 change the purpose slots to point to the names of the decls. */
5834 for (pair = values; pair; pair = TREE_CHAIN (pair))
5836 tree enu = TREE_PURPOSE (pair);
5837 tree ini = DECL_INITIAL (enu);
5839 TREE_TYPE (enu) = enumtype;
5841 /* The ISO C Standard mandates enumerators to have type int,
5842 even though the underlying type of an enum type is
5843 unspecified. Here we convert any enumerators that fit in
5844 an int to type int, to avoid promotions to unsigned types
5845 when comparing integers with enumerators that fit in the
5846 int range. When -pedantic is given, build_enumerator()
5847 would have already taken care of those that don't fit. */
5848 if (int_fits_type_p (ini, integer_type_node))
5849 tem = integer_type_node;
5850 else
5851 tem = enumtype;
5852 ini = convert (tem, ini);
5854 DECL_INITIAL (enu) = ini;
5855 TREE_PURPOSE (pair) = DECL_NAME (enu);
5856 TREE_VALUE (pair) = ini;
5859 TYPE_VALUES (enumtype) = values;
5862 /* Record the min/max values so that we can warn about bit-field
5863 enumerations that are too small for the values. */
5864 lt = GGC_CNEW (struct lang_type);
5865 lt->enum_min = minnode;
5866 lt->enum_max = maxnode;
5867 TYPE_LANG_SPECIFIC (enumtype) = lt;
5869 /* Fix up all variant types of this enum type. */
5870 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5872 if (tem == enumtype)
5873 continue;
5874 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5875 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5876 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5877 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5878 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5879 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5880 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5881 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5882 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5883 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5884 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5887 /* Finish debugging output for this type. */
5888 rest_of_type_compilation (enumtype, toplevel);
5890 return enumtype;
5893 /* Build and install a CONST_DECL for one value of the
5894 current enumeration type (one that was begun with start_enum).
5895 Return a tree-list containing the CONST_DECL and its value.
5896 Assignment of sequential values by default is handled here. */
5898 tree
5899 build_enumerator (tree name, tree value)
5901 tree decl, type;
5903 /* Validate and default VALUE. */
5905 if (value != 0)
5907 /* Don't issue more errors for error_mark_node (i.e. an
5908 undeclared identifier) - just ignore the value expression. */
5909 if (value == error_mark_node)
5910 value = 0;
5911 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5912 || TREE_CODE (value) != INTEGER_CST)
5914 error ("enumerator value for %qE is not an integer constant", name);
5915 value = 0;
5917 else
5919 value = default_conversion (value);
5920 constant_expression_warning (value);
5924 /* Default based on previous value. */
5925 /* It should no longer be possible to have NON_LVALUE_EXPR
5926 in the default. */
5927 if (value == 0)
5929 value = enum_next_value;
5930 if (enum_overflow)
5931 error ("overflow in enumeration values");
5934 if (pedantic && !int_fits_type_p (value, integer_type_node))
5936 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5937 /* XXX This causes -pedantic to change the meaning of the program.
5938 Remove? -zw 2004-03-15 */
5939 value = convert (integer_type_node, value);
5942 /* Set basis for default for next value. */
5943 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5944 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5946 /* Now create a declaration for the enum value name. */
5948 type = TREE_TYPE (value);
5949 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5950 TYPE_PRECISION (integer_type_node)),
5951 (TYPE_PRECISION (type)
5952 >= TYPE_PRECISION (integer_type_node)
5953 && TYPE_UNSIGNED (type)));
5955 decl = build_decl (CONST_DECL, name, type);
5956 DECL_INITIAL (decl) = convert (type, value);
5957 pushdecl (decl);
5959 return tree_cons (decl, value, NULL_TREE);
5963 /* Create the FUNCTION_DECL for a function definition.
5964 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5965 the declaration; they describe the function's name and the type it returns,
5966 but twisted together in a fashion that parallels the syntax of C.
5968 This function creates a binding context for the function body
5969 as well as setting up the FUNCTION_DECL in current_function_decl.
5971 Returns 1 on success. If the DECLARATOR is not suitable for a function
5972 (it defines a datum instead), we return 0, which tells
5973 yyparse to report a parse error. */
5976 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5977 tree attributes)
5979 tree decl1, old_decl;
5980 tree restype, resdecl;
5981 struct c_label_context_se *nstack_se;
5982 struct c_label_context_vm *nstack_vm;
5984 current_function_returns_value = 0; /* Assume, until we see it does. */
5985 current_function_returns_null = 0;
5986 current_function_returns_abnormally = 0;
5987 warn_about_return_type = 0;
5988 current_extern_inline = 0;
5989 c_switch_stack = NULL;
5991 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
5992 nstack_se->labels_def = NULL;
5993 nstack_se->labels_used = NULL;
5994 nstack_se->next = label_context_stack_se;
5995 label_context_stack_se = nstack_se;
5997 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
5998 nstack_vm->labels_def = NULL;
5999 nstack_vm->labels_used = NULL;
6000 nstack_vm->scope = 0;
6001 nstack_vm->next = label_context_stack_vm;
6002 label_context_stack_vm = nstack_vm;
6004 /* Indicate no valid break/continue context by setting these variables
6005 to some non-null, non-label value. We'll notice and emit the proper
6006 error message in c_finish_bc_stmt. */
6007 c_break_label = c_cont_label = size_zero_node;
6009 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
6011 /* If the declarator is not suitable for a function definition,
6012 cause a syntax error. */
6013 if (decl1 == 0)
6015 label_context_stack_se = label_context_stack_se->next;
6016 label_context_stack_vm = label_context_stack_vm->next;
6017 return 0;
6020 decl_attributes (&decl1, attributes, 0);
6022 if (DECL_DECLARED_INLINE_P (decl1)
6023 && DECL_UNINLINABLE (decl1)
6024 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6025 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6026 decl1);
6028 announce_function (decl1);
6030 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6032 error ("return type is an incomplete type");
6033 /* Make it return void instead. */
6034 TREE_TYPE (decl1)
6035 = build_function_type (void_type_node,
6036 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6039 if (warn_about_return_type)
6040 pedwarn_c99 ("return type defaults to %<int%>");
6042 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6043 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6044 DECL_INITIAL (decl1) = error_mark_node;
6046 /* If this definition isn't a prototype and we had a prototype declaration
6047 before, copy the arg type info from that prototype. */
6048 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6049 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6050 old_decl = 0;
6051 current_function_prototype_locus = UNKNOWN_LOCATION;
6052 current_function_prototype_built_in = false;
6053 current_function_prototype_arg_types = NULL_TREE;
6054 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6056 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6057 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6058 TREE_TYPE (TREE_TYPE (old_decl))))
6060 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6061 TREE_TYPE (decl1));
6062 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6063 current_function_prototype_built_in
6064 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6065 current_function_prototype_arg_types
6066 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6068 if (TREE_PUBLIC (decl1))
6070 /* If there is an external prototype declaration of this
6071 function, record its location but do not copy information
6072 to this decl. This may be an invisible declaration
6073 (built-in or in a scope which has finished) or simply
6074 have more refined argument types than any declaration
6075 found above. */
6076 struct c_binding *b;
6077 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6078 if (B_IN_SCOPE (b, external_scope))
6079 break;
6080 if (b)
6082 tree ext_decl, ext_type;
6083 ext_decl = b->decl;
6084 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6085 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6086 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6087 TREE_TYPE (ext_type)))
6089 current_function_prototype_locus
6090 = DECL_SOURCE_LOCATION (ext_decl);
6091 current_function_prototype_built_in
6092 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6093 current_function_prototype_arg_types
6094 = TYPE_ARG_TYPES (ext_type);
6100 /* Optionally warn of old-fashioned def with no previous prototype. */
6101 if (warn_strict_prototypes
6102 && old_decl != error_mark_node
6103 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6104 && C_DECL_ISNT_PROTOTYPE (old_decl))
6105 warning (OPT_Wstrict_prototypes,
6106 "function declaration isn%'t a prototype");
6107 /* Optionally warn of any global def with no previous prototype. */
6108 else if (warn_missing_prototypes
6109 && old_decl != error_mark_node
6110 && TREE_PUBLIC (decl1)
6111 && !MAIN_NAME_P (DECL_NAME (decl1))
6112 && C_DECL_ISNT_PROTOTYPE (old_decl))
6113 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6114 /* Optionally warn of any def with no previous prototype
6115 if the function has already been used. */
6116 else if (warn_missing_prototypes
6117 && old_decl != 0
6118 && old_decl != error_mark_node
6119 && TREE_USED (old_decl)
6120 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6121 warning (OPT_Wmissing_prototypes,
6122 "%q+D was used with no prototype before its definition", decl1);
6123 /* Optionally warn of any global def with no previous declaration. */
6124 else if (warn_missing_declarations
6125 && TREE_PUBLIC (decl1)
6126 && old_decl == 0
6127 && !MAIN_NAME_P (DECL_NAME (decl1)))
6128 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6129 decl1);
6130 /* Optionally warn of any def with no previous declaration
6131 if the function has already been used. */
6132 else if (warn_missing_declarations
6133 && old_decl != 0
6134 && old_decl != error_mark_node
6135 && TREE_USED (old_decl)
6136 && C_DECL_IMPLICIT (old_decl))
6137 warning (OPT_Wmissing_declarations,
6138 "%q+D was used with no declaration before its definition", decl1);
6140 /* This is a definition, not a reference.
6141 So normally clear DECL_EXTERNAL.
6142 However, `extern inline' acts like a declaration
6143 except for defining how to inline. So set DECL_EXTERNAL in that case. */
6144 DECL_EXTERNAL (decl1) = current_extern_inline;
6146 /* C99 specified different behaviour for non-static inline
6147 functions, compared with the traditional GNU behaviour. We don't
6148 support the C99 behaviour, but we do warn about non-static inline
6149 functions here. The warning can be disabled via an explicit use
6150 of -fgnu89-inline, or by using the gnu_inline attribute. */
6151 if (DECL_DECLARED_INLINE_P (decl1)
6152 && TREE_PUBLIC (decl1)
6153 && flag_isoc99
6154 && flag_gnu89_inline != 1
6155 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
6156 && diagnostic_report_warnings_p ())
6158 static bool info = false;
6160 warning (0, "C99 inline functions are not supported; using GNU89");
6161 if (!info)
6163 warning (0,
6164 "to disable this warning use -fgnu89-inline or "
6165 "the gnu_inline function attribute");
6166 info = true;
6170 /* This function exists in static storage.
6171 (This does not mean `static' in the C sense!) */
6172 TREE_STATIC (decl1) = 1;
6174 /* A nested function is not global. */
6175 if (current_function_decl != 0)
6176 TREE_PUBLIC (decl1) = 0;
6178 /* This is the earliest point at which we might know the assembler
6179 name of the function. Thus, if it's set before this, die horribly. */
6180 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6182 /* If #pragma weak was used, mark the decl weak now. */
6183 if (current_scope == file_scope)
6184 maybe_apply_pragma_weak (decl1);
6186 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6187 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6189 tree args;
6190 int argct = 0;
6192 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6193 != integer_type_node)
6194 pedwarn ("return type of %q+D is not %<int%>", decl1);
6196 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
6197 args = TREE_CHAIN (args))
6199 tree type = args ? TREE_VALUE (args) : 0;
6201 if (type == void_type_node)
6202 break;
6204 ++argct;
6205 switch (argct)
6207 case 1:
6208 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
6209 pedwarn ("first argument of %q+D should be %<int%>", decl1);
6210 break;
6212 case 2:
6213 if (TREE_CODE (type) != POINTER_TYPE
6214 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6215 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6216 != char_type_node))
6217 pedwarn ("second argument of %q+D should be %<char **%>",
6218 decl1);
6219 break;
6221 case 3:
6222 if (TREE_CODE (type) != POINTER_TYPE
6223 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6224 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6225 != char_type_node))
6226 pedwarn ("third argument of %q+D should probably be "
6227 "%<char **%>", decl1);
6228 break;
6232 /* It is intentional that this message does not mention the third
6233 argument because it's only mentioned in an appendix of the
6234 standard. */
6235 if (argct > 0 && (argct < 2 || argct > 3))
6236 pedwarn ("%q+D takes only zero or two arguments", decl1);
6238 if (!TREE_PUBLIC (decl1))
6239 pedwarn ("%q+D is normally a non-static function", decl1);
6242 /* Record the decl so that the function name is defined.
6243 If we already have a decl for this name, and it is a FUNCTION_DECL,
6244 use the old decl. */
6246 current_function_decl = pushdecl (decl1);
6248 push_scope ();
6249 declare_parm_level ();
6251 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6252 /* Promote the value to int before returning it. */
6253 if (c_promoting_integer_type_p (restype))
6255 /* It retains unsignedness if not really getting wider. */
6256 if (TYPE_UNSIGNED (restype)
6257 && (TYPE_PRECISION (restype)
6258 == TYPE_PRECISION (integer_type_node)))
6259 restype = unsigned_type_node;
6260 else
6261 restype = integer_type_node;
6264 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6265 DECL_ARTIFICIAL (resdecl) = 1;
6266 DECL_IGNORED_P (resdecl) = 1;
6267 DECL_RESULT (current_function_decl) = resdecl;
6269 start_fname_decls ();
6271 return 1;
6274 /* Subroutine of store_parm_decls which handles new-style function
6275 definitions (prototype format). The parms already have decls, so we
6276 need only record them as in effect and complain if any redundant
6277 old-style parm decls were written. */
6278 static void
6279 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6281 tree decl;
6283 if (current_scope->bindings)
6285 error ("%Jold-style parameter declarations in prototyped "
6286 "function definition", fndecl);
6288 /* Get rid of the old-style declarations. */
6289 pop_scope ();
6290 push_scope ();
6292 /* Don't issue this warning for nested functions, and don't issue this
6293 warning if we got here because ARG_INFO_TYPES was error_mark_node
6294 (this happens when a function definition has just an ellipsis in
6295 its parameter list). */
6296 else if (!in_system_header && !current_function_scope
6297 && arg_info->types != error_mark_node)
6298 warning (OPT_Wtraditional,
6299 "%Jtraditional C rejects ISO C style function definitions",
6300 fndecl);
6302 /* Now make all the parameter declarations visible in the function body.
6303 We can bypass most of the grunt work of pushdecl. */
6304 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6306 DECL_CONTEXT (decl) = current_function_decl;
6307 if (DECL_NAME (decl))
6309 bind (DECL_NAME (decl), decl, current_scope,
6310 /*invisible=*/false, /*nested=*/false);
6311 if (!TREE_USED (decl))
6312 warn_if_shadowing (decl);
6314 else
6315 error ("%Jparameter name omitted", decl);
6318 /* Record the parameter list in the function declaration. */
6319 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6321 /* Now make all the ancillary declarations visible, likewise. */
6322 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6324 DECL_CONTEXT (decl) = current_function_decl;
6325 if (DECL_NAME (decl))
6326 bind (DECL_NAME (decl), decl, current_scope,
6327 /*invisible=*/false, /*nested=*/false);
6330 /* And all the tag declarations. */
6331 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6332 if (TREE_PURPOSE (decl))
6333 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6334 /*invisible=*/false, /*nested=*/false);
6337 /* Subroutine of store_parm_decls which handles old-style function
6338 definitions (separate parameter list and declarations). */
6340 static void
6341 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6343 struct c_binding *b;
6344 tree parm, decl, last;
6345 tree parmids = arg_info->parms;
6346 struct pointer_set_t *seen_args = pointer_set_create ();
6348 if (!in_system_header)
6349 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6350 fndecl);
6352 /* Match each formal parameter name with its declaration. Save each
6353 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6354 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6356 if (TREE_VALUE (parm) == 0)
6358 error ("%Jparameter name missing from parameter list", fndecl);
6359 TREE_PURPOSE (parm) = 0;
6360 continue;
6363 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6364 if (b && B_IN_CURRENT_SCOPE (b))
6366 decl = b->decl;
6367 /* If we got something other than a PARM_DECL it is an error. */
6368 if (TREE_CODE (decl) != PARM_DECL)
6369 error ("%q+D declared as a non-parameter", decl);
6370 /* If the declaration is already marked, we have a duplicate
6371 name. Complain and ignore the duplicate. */
6372 else if (pointer_set_contains (seen_args, decl))
6374 error ("multiple parameters named %q+D", decl);
6375 TREE_PURPOSE (parm) = 0;
6376 continue;
6378 /* If the declaration says "void", complain and turn it into
6379 an int. */
6380 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6382 error ("parameter %q+D declared with void type", decl);
6383 TREE_TYPE (decl) = integer_type_node;
6384 DECL_ARG_TYPE (decl) = integer_type_node;
6385 layout_decl (decl, 0);
6387 warn_if_shadowing (decl);
6389 /* If no declaration found, default to int. */
6390 else
6392 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6393 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6394 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6395 pushdecl (decl);
6396 warn_if_shadowing (decl);
6398 if (flag_isoc99)
6399 pedwarn ("type of %q+D defaults to %<int%>", decl);
6400 else if (extra_warnings)
6401 warning (OPT_Wextra, "type of %q+D defaults to %<int%>", decl);
6404 TREE_PURPOSE (parm) = decl;
6405 pointer_set_insert (seen_args, decl);
6408 /* Now examine the parms chain for incomplete declarations
6409 and declarations with no corresponding names. */
6411 for (b = current_scope->bindings; b; b = b->prev)
6413 parm = b->decl;
6414 if (TREE_CODE (parm) != PARM_DECL)
6415 continue;
6417 if (TREE_TYPE (parm) != error_mark_node
6418 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6420 error ("parameter %q+D has incomplete type", parm);
6421 TREE_TYPE (parm) = error_mark_node;
6424 if (!pointer_set_contains (seen_args, parm))
6426 error ("declaration for parameter %q+D but no such parameter", parm);
6428 /* Pretend the parameter was not missing.
6429 This gets us to a standard state and minimizes
6430 further error messages. */
6431 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6435 /* Chain the declarations together in the order of the list of
6436 names. Store that chain in the function decl, replacing the
6437 list of names. Update the current scope to match. */
6438 DECL_ARGUMENTS (fndecl) = 0;
6440 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6441 if (TREE_PURPOSE (parm))
6442 break;
6443 if (parm && TREE_PURPOSE (parm))
6445 last = TREE_PURPOSE (parm);
6446 DECL_ARGUMENTS (fndecl) = last;
6448 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6449 if (TREE_PURPOSE (parm))
6451 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6452 last = TREE_PURPOSE (parm);
6454 TREE_CHAIN (last) = 0;
6457 pointer_set_destroy (seen_args);
6459 /* If there was a previous prototype,
6460 set the DECL_ARG_TYPE of each argument according to
6461 the type previously specified, and report any mismatches. */
6463 if (current_function_prototype_arg_types)
6465 tree type;
6466 for (parm = DECL_ARGUMENTS (fndecl),
6467 type = current_function_prototype_arg_types;
6468 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6469 != void_type_node));
6470 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6472 if (parm == 0 || type == 0
6473 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6475 if (current_function_prototype_built_in)
6476 warning (0, "number of arguments doesn%'t match "
6477 "built-in prototype");
6478 else
6480 error ("number of arguments doesn%'t match prototype");
6481 error ("%Hprototype declaration",
6482 &current_function_prototype_locus);
6484 break;
6486 /* Type for passing arg must be consistent with that
6487 declared for the arg. ISO C says we take the unqualified
6488 type for parameters declared with qualified type. */
6489 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6490 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6492 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6493 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6495 /* Adjust argument to match prototype. E.g. a previous
6496 `int foo(float);' prototype causes
6497 `int foo(x) float x; {...}' to be treated like
6498 `int foo(float x) {...}'. This is particularly
6499 useful for argument types like uid_t. */
6500 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6502 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6503 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6504 && TYPE_PRECISION (TREE_TYPE (parm))
6505 < TYPE_PRECISION (integer_type_node))
6506 DECL_ARG_TYPE (parm) = integer_type_node;
6508 if (pedantic)
6510 /* ??? Is it possible to get here with a
6511 built-in prototype or will it always have
6512 been diagnosed as conflicting with an
6513 old-style definition and discarded? */
6514 if (current_function_prototype_built_in)
6515 warning (0, "promoted argument %qD "
6516 "doesn%'t match built-in prototype", parm);
6517 else
6519 pedwarn ("promoted argument %qD "
6520 "doesn%'t match prototype", parm);
6521 pedwarn ("%Hprototype declaration",
6522 &current_function_prototype_locus);
6526 else
6528 if (current_function_prototype_built_in)
6529 warning (0, "argument %qD doesn%'t match "
6530 "built-in prototype", parm);
6531 else
6533 error ("argument %qD doesn%'t match prototype", parm);
6534 error ("%Hprototype declaration",
6535 &current_function_prototype_locus);
6540 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6543 /* Otherwise, create a prototype that would match. */
6545 else
6547 tree actual = 0, last = 0, type;
6549 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6551 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6552 if (last)
6553 TREE_CHAIN (last) = type;
6554 else
6555 actual = type;
6556 last = type;
6558 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6559 if (last)
6560 TREE_CHAIN (last) = type;
6561 else
6562 actual = type;
6564 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6565 of the type of this function, but we need to avoid having this
6566 affect the types of other similarly-typed functions, so we must
6567 first force the generation of an identical (but separate) type
6568 node for the relevant function type. The new node we create
6569 will be a variant of the main variant of the original function
6570 type. */
6572 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6574 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6578 /* Store parameter declarations passed in ARG_INFO into the current
6579 function declaration. */
6581 void
6582 store_parm_decls_from (struct c_arg_info *arg_info)
6584 current_function_arg_info = arg_info;
6585 store_parm_decls ();
6588 /* Store the parameter declarations into the current function declaration.
6589 This is called after parsing the parameter declarations, before
6590 digesting the body of the function.
6592 For an old-style definition, construct a prototype out of the old-style
6593 parameter declarations and inject it into the function's type. */
6595 void
6596 store_parm_decls (void)
6598 tree fndecl = current_function_decl;
6599 bool proto;
6601 /* The argument information block for FNDECL. */
6602 struct c_arg_info *arg_info = current_function_arg_info;
6603 current_function_arg_info = 0;
6605 /* True if this definition is written with a prototype. Note:
6606 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6607 list in a function definition as equivalent to (void) -- an
6608 empty argument list specifies the function has no parameters,
6609 but only (void) sets up a prototype for future calls. */
6610 proto = arg_info->types != 0;
6612 if (proto)
6613 store_parm_decls_newstyle (fndecl, arg_info);
6614 else
6615 store_parm_decls_oldstyle (fndecl, arg_info);
6617 /* The next call to push_scope will be a function body. */
6619 next_is_function_body = true;
6621 /* Write a record describing this function definition to the prototypes
6622 file (if requested). */
6624 gen_aux_info_record (fndecl, 1, 0, proto);
6626 /* Initialize the RTL code for the function. */
6627 allocate_struct_function (fndecl);
6629 /* Begin the statement tree for this function. */
6630 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6632 /* ??? Insert the contents of the pending sizes list into the function
6633 to be evaluated. The only reason left to have this is
6634 void foo(int n, int array[n++])
6635 because we throw away the array type in favor of a pointer type, and
6636 thus won't naturally see the SAVE_EXPR containing the increment. All
6637 other pending sizes would be handled by gimplify_parameters. */
6639 tree t;
6640 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6641 add_stmt (TREE_VALUE (t));
6644 /* Even though we're inside a function body, we still don't want to
6645 call expand_expr to calculate the size of a variable-sized array.
6646 We haven't necessarily assigned RTL to all variables yet, so it's
6647 not safe to try to expand expressions involving them. */
6648 cfun->x_dont_save_pending_sizes_p = 1;
6651 /* Emit diagnostics that require gimple input for detection. Operate on
6652 FNDECL and all its nested functions. */
6654 static void
6655 c_gimple_diagnostics_recursively (tree fndecl)
6657 struct cgraph_node *cgn;
6659 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6660 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6662 /* Notice when OpenMP structured block constraints are violated. */
6663 if (flag_openmp)
6664 diagnose_omp_structured_block_errors (fndecl);
6666 /* Finalize all nested functions now. */
6667 cgn = cgraph_node (fndecl);
6668 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6669 c_gimple_diagnostics_recursively (cgn->decl);
6672 /* Finish up a function declaration and compile that function
6673 all the way to assembler language output. The free the storage
6674 for the function definition.
6676 This is called after parsing the body of the function definition. */
6678 void
6679 finish_function (void)
6681 tree fndecl = current_function_decl;
6683 label_context_stack_se = label_context_stack_se->next;
6684 label_context_stack_vm = label_context_stack_vm->next;
6686 if (TREE_CODE (fndecl) == FUNCTION_DECL
6687 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6689 tree args = DECL_ARGUMENTS (fndecl);
6690 for (; args; args = TREE_CHAIN (args))
6692 tree type = TREE_TYPE (args);
6693 if (INTEGRAL_TYPE_P (type)
6694 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6695 DECL_ARG_TYPE (args) = integer_type_node;
6699 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6700 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6702 /* Must mark the RESULT_DECL as being in this function. */
6704 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6705 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6707 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6709 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6710 != integer_type_node)
6712 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6713 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6714 if (!warn_main)
6715 pedwarn ("return type of %q+D is not %<int%>", fndecl);
6717 else
6719 if (flag_isoc99)
6721 tree stmt = c_finish_return (integer_zero_node);
6722 #ifdef USE_MAPPED_LOCATION
6723 /* Hack. We don't want the middle-end to warn that this return
6724 is unreachable, so we mark its location as special. Using
6725 UNKNOWN_LOCATION has the problem that it gets clobbered in
6726 annotate_one_with_locus. A cleaner solution might be to
6727 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6729 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6730 #else
6731 /* Hack. We don't want the middle-end to warn that this
6732 return is unreachable, so put the statement on the
6733 special line 0. */
6734 annotate_with_file_line (stmt, input_filename, 0);
6735 #endif
6740 /* Tie off the statement tree for this function. */
6741 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6743 finish_fname_decls ();
6745 /* Complain if there's just no return statement. */
6746 if (warn_return_type
6747 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6748 && !current_function_returns_value && !current_function_returns_null
6749 /* Don't complain if we are no-return. */
6750 && !current_function_returns_abnormally
6751 /* Don't warn for main(). */
6752 && !MAIN_NAME_P (DECL_NAME (fndecl))
6753 /* Or if they didn't actually specify a return type. */
6754 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6755 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6756 inline function, as we might never be compiled separately. */
6757 && DECL_INLINE (fndecl))
6759 warning (OPT_Wreturn_type,
6760 "no return statement in function returning non-void");
6761 TREE_NO_WARNING (fndecl) = 1;
6764 /* With just -Wextra, complain only if function returns both with
6765 and without a value. */
6766 if (extra_warnings
6767 && current_function_returns_value
6768 && current_function_returns_null)
6769 warning (OPT_Wextra, "this function may return with or without a value");
6771 /* Store the end of the function, so that we get good line number
6772 info for the epilogue. */
6773 cfun->function_end_locus = input_location;
6775 /* If we don't have ctors/dtors sections, and this is a static
6776 constructor or destructor, it must be recorded now. */
6777 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6778 && !targetm.have_ctors_dtors)
6779 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6780 if (DECL_STATIC_DESTRUCTOR (fndecl)
6781 && !targetm.have_ctors_dtors)
6782 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6784 /* Finalize the ELF visibility for the function. */
6785 c_determine_visibility (fndecl);
6787 /* Genericize before inlining. Delay genericizing nested functions
6788 until their parent function is genericized. Since finalizing
6789 requires GENERIC, delay that as well. */
6791 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6792 && !undef_nested_function)
6794 if (!decl_function_context (fndecl))
6796 c_genericize (fndecl);
6797 c_gimple_diagnostics_recursively (fndecl);
6799 /* ??? Objc emits functions after finalizing the compilation unit.
6800 This should be cleaned up later and this conditional removed. */
6801 if (cgraph_global_info_ready)
6803 c_expand_body (fndecl);
6804 return;
6807 cgraph_finalize_function (fndecl, false);
6809 else
6811 /* Register this function with cgraph just far enough to get it
6812 added to our parent's nested function list. Handy, since the
6813 C front end doesn't have such a list. */
6814 (void) cgraph_node (fndecl);
6818 if (!decl_function_context (fndecl))
6819 undef_nested_function = false;
6821 /* We're leaving the context of this function, so zap cfun.
6822 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6823 tree_rest_of_compilation. */
6824 cfun = NULL;
6825 current_function_decl = NULL;
6828 /* Generate the RTL for the body of FNDECL. */
6830 void
6831 c_expand_body (tree fndecl)
6834 if (!DECL_INITIAL (fndecl)
6835 || DECL_INITIAL (fndecl) == error_mark_node)
6836 return;
6838 tree_rest_of_compilation (fndecl);
6840 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6841 && targetm.have_ctors_dtors)
6842 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6843 DEFAULT_INIT_PRIORITY);
6844 if (DECL_STATIC_DESTRUCTOR (fndecl)
6845 && targetm.have_ctors_dtors)
6846 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6847 DEFAULT_INIT_PRIORITY);
6850 /* Check the declarations given in a for-loop for satisfying the C99
6851 constraints. If exactly one such decl is found, return it. */
6853 tree
6854 check_for_loop_decls (void)
6856 struct c_binding *b;
6857 tree one_decl = NULL_TREE;
6858 int n_decls = 0;
6861 if (!flag_isoc99)
6863 /* If we get here, declarations have been used in a for loop without
6864 the C99 for loop scope. This doesn't make much sense, so don't
6865 allow it. */
6866 error ("%<for%> loop initial declaration used outside C99 mode");
6867 return NULL_TREE;
6869 /* C99 subclause 6.8.5 paragraph 3:
6871 [#3] The declaration part of a for statement shall only
6872 declare identifiers for objects having storage class auto or
6873 register.
6875 It isn't clear whether, in this sentence, "identifiers" binds to
6876 "shall only declare" or to "objects" - that is, whether all identifiers
6877 declared must be identifiers for objects, or whether the restriction
6878 only applies to those that are. (A question on this in comp.std.c
6879 in November 2000 received no answer.) We implement the strictest
6880 interpretation, to avoid creating an extension which later causes
6881 problems. */
6883 for (b = current_scope->bindings; b; b = b->prev)
6885 tree id = b->id;
6886 tree decl = b->decl;
6888 if (!id)
6889 continue;
6891 switch (TREE_CODE (decl))
6893 case VAR_DECL:
6894 if (TREE_STATIC (decl))
6895 error ("declaration of static variable %q+D in %<for%> loop "
6896 "initial declaration", decl);
6897 else if (DECL_EXTERNAL (decl))
6898 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6899 "initial declaration", decl);
6900 break;
6902 case RECORD_TYPE:
6903 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6904 id);
6905 break;
6906 case UNION_TYPE:
6907 error ("%<union %E%> declared in %<for%> loop initial declaration",
6908 id);
6909 break;
6910 case ENUMERAL_TYPE:
6911 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6912 id);
6913 break;
6914 default:
6915 error ("declaration of non-variable %q+D in %<for%> loop "
6916 "initial declaration", decl);
6919 n_decls++;
6920 one_decl = decl;
6923 return n_decls == 1 ? one_decl : NULL_TREE;
6926 /* Save and reinitialize the variables
6927 used during compilation of a C function. */
6929 void
6930 c_push_function_context (struct function *f)
6932 struct language_function *p;
6933 p = GGC_NEW (struct language_function);
6934 f->language = p;
6936 p->base.x_stmt_tree = c_stmt_tree;
6937 p->x_break_label = c_break_label;
6938 p->x_cont_label = c_cont_label;
6939 p->x_switch_stack = c_switch_stack;
6940 p->arg_info = current_function_arg_info;
6941 p->returns_value = current_function_returns_value;
6942 p->returns_null = current_function_returns_null;
6943 p->returns_abnormally = current_function_returns_abnormally;
6944 p->warn_about_return_type = warn_about_return_type;
6945 p->extern_inline = current_extern_inline;
6948 /* Restore the variables used during compilation of a C function. */
6950 void
6951 c_pop_function_context (struct function *f)
6953 struct language_function *p = f->language;
6955 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6956 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6958 /* Stop pointing to the local nodes about to be freed. */
6959 /* But DECL_INITIAL must remain nonzero so we know this
6960 was an actual function definition. */
6961 DECL_INITIAL (current_function_decl) = error_mark_node;
6962 DECL_ARGUMENTS (current_function_decl) = 0;
6965 c_stmt_tree = p->base.x_stmt_tree;
6966 c_break_label = p->x_break_label;
6967 c_cont_label = p->x_cont_label;
6968 c_switch_stack = p->x_switch_stack;
6969 current_function_arg_info = p->arg_info;
6970 current_function_returns_value = p->returns_value;
6971 current_function_returns_null = p->returns_null;
6972 current_function_returns_abnormally = p->returns_abnormally;
6973 warn_about_return_type = p->warn_about_return_type;
6974 current_extern_inline = p->extern_inline;
6976 f->language = NULL;
6979 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6981 void
6982 c_dup_lang_specific_decl (tree decl)
6984 struct lang_decl *ld;
6986 if (!DECL_LANG_SPECIFIC (decl))
6987 return;
6989 ld = GGC_NEW (struct lang_decl);
6990 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6991 DECL_LANG_SPECIFIC (decl) = ld;
6994 /* The functions below are required for functionality of doing
6995 function at once processing in the C front end. Currently these
6996 functions are not called from anywhere in the C front end, but as
6997 these changes continue, that will change. */
6999 /* Returns the stmt_tree (if any) to which statements are currently
7000 being added. If there is no active statement-tree, NULL is
7001 returned. */
7003 stmt_tree
7004 current_stmt_tree (void)
7006 return &c_stmt_tree;
7009 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
7010 C. */
7013 anon_aggr_type_p (tree ARG_UNUSED (node))
7015 return 0;
7018 /* Return the global value of T as a symbol. */
7020 tree
7021 identifier_global_value (tree t)
7023 struct c_binding *b;
7025 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7026 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7027 return b->decl;
7029 return 0;
7032 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7033 otherwise the name is found in ridpointers from RID_INDEX. */
7035 void
7036 record_builtin_type (enum rid rid_index, const char *name, tree type)
7038 tree id, decl;
7039 if (name == 0)
7040 id = ridpointers[(int) rid_index];
7041 else
7042 id = get_identifier (name);
7043 decl = build_decl (TYPE_DECL, id, type);
7044 pushdecl (decl);
7045 if (debug_hooks->type_decl)
7046 debug_hooks->type_decl (decl, false);
7049 /* Build the void_list_node (void_type_node having been created). */
7050 tree
7051 build_void_list_node (void)
7053 tree t = build_tree_list (NULL_TREE, void_type_node);
7054 return t;
7057 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7059 struct c_parm *
7060 build_c_parm (struct c_declspecs *specs, tree attrs,
7061 struct c_declarator *declarator)
7063 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7064 ret->specs = specs;
7065 ret->attrs = attrs;
7066 ret->declarator = declarator;
7067 return ret;
7070 /* Return a declarator with nested attributes. TARGET is the inner
7071 declarator to which these attributes apply. ATTRS are the
7072 attributes. */
7074 struct c_declarator *
7075 build_attrs_declarator (tree attrs, struct c_declarator *target)
7077 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7078 ret->kind = cdk_attrs;
7079 ret->declarator = target;
7080 ret->u.attrs = attrs;
7081 return ret;
7084 /* Return a declarator for a function with arguments specified by ARGS
7085 and return type specified by TARGET. */
7087 struct c_declarator *
7088 build_function_declarator (struct c_arg_info *args,
7089 struct c_declarator *target)
7091 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7092 ret->kind = cdk_function;
7093 ret->declarator = target;
7094 ret->u.arg_info = args;
7095 return ret;
7098 /* Return a declarator for the identifier IDENT (which may be
7099 NULL_TREE for an abstract declarator). */
7101 struct c_declarator *
7102 build_id_declarator (tree ident)
7104 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7105 ret->kind = cdk_id;
7106 ret->declarator = 0;
7107 ret->u.id = ident;
7108 /* Default value - may get reset to a more precise location. */
7109 ret->id_loc = input_location;
7110 return ret;
7113 /* Return something to represent absolute declarators containing a *.
7114 TARGET is the absolute declarator that the * contains.
7115 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7116 to apply to the pointer type. */
7118 struct c_declarator *
7119 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7120 struct c_declarator *target)
7122 tree attrs;
7123 int quals = 0;
7124 struct c_declarator *itarget = target;
7125 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7126 if (type_quals_attrs)
7128 attrs = type_quals_attrs->attrs;
7129 quals = quals_from_declspecs (type_quals_attrs);
7130 if (attrs != NULL_TREE)
7131 itarget = build_attrs_declarator (attrs, target);
7133 ret->kind = cdk_pointer;
7134 ret->declarator = itarget;
7135 ret->u.pointer_quals = quals;
7136 return ret;
7139 /* Return a pointer to a structure for an empty list of declaration
7140 specifiers. */
7142 struct c_declspecs *
7143 build_null_declspecs (void)
7145 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7146 ret->type = 0;
7147 ret->decl_attr = 0;
7148 ret->attrs = 0;
7149 ret->typespec_word = cts_none;
7150 ret->storage_class = csc_none;
7151 ret->declspecs_seen_p = false;
7152 ret->type_seen_p = false;
7153 ret->non_sc_seen_p = false;
7154 ret->typedef_p = false;
7155 ret->tag_defined_p = false;
7156 ret->explicit_signed_p = false;
7157 ret->deprecated_p = false;
7158 ret->default_int_p = false;
7159 ret->long_p = false;
7160 ret->long_long_p = false;
7161 ret->short_p = false;
7162 ret->signed_p = false;
7163 ret->unsigned_p = false;
7164 ret->complex_p = false;
7165 ret->inline_p = false;
7166 ret->thread_p = false;
7167 ret->const_p = false;
7168 ret->volatile_p = false;
7169 ret->restrict_p = false;
7170 return ret;
7173 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7174 returning SPECS. */
7176 struct c_declspecs *
7177 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7179 enum rid i;
7180 bool dupe = false;
7181 specs->non_sc_seen_p = true;
7182 specs->declspecs_seen_p = true;
7183 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7184 && C_IS_RESERVED_WORD (qual));
7185 i = C_RID_CODE (qual);
7186 switch (i)
7188 case RID_CONST:
7189 dupe = specs->const_p;
7190 specs->const_p = true;
7191 break;
7192 case RID_VOLATILE:
7193 dupe = specs->volatile_p;
7194 specs->volatile_p = true;
7195 break;
7196 case RID_RESTRICT:
7197 dupe = specs->restrict_p;
7198 specs->restrict_p = true;
7199 break;
7200 default:
7201 gcc_unreachable ();
7203 if (dupe && pedantic && !flag_isoc99)
7204 pedwarn ("duplicate %qE", qual);
7205 return specs;
7208 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7209 returning SPECS. */
7211 struct c_declspecs *
7212 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7214 tree type = spec.spec;
7215 specs->non_sc_seen_p = true;
7216 specs->declspecs_seen_p = true;
7217 specs->type_seen_p = true;
7218 if (TREE_DEPRECATED (type))
7219 specs->deprecated_p = true;
7221 /* Handle type specifier keywords. */
7222 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
7224 enum rid i = C_RID_CODE (type);
7225 if (specs->type)
7227 error ("two or more data types in declaration specifiers");
7228 return specs;
7230 if ((int) i <= (int) RID_LAST_MODIFIER)
7232 /* "long", "short", "signed", "unsigned" or "_Complex". */
7233 bool dupe = false;
7234 switch (i)
7236 case RID_LONG:
7237 if (specs->long_long_p)
7239 error ("%<long long long%> is too long for GCC");
7240 break;
7242 if (specs->long_p)
7244 if (specs->typespec_word == cts_double)
7246 error ("both %<long long%> and %<double%> in "
7247 "declaration specifiers");
7248 break;
7250 if (pedantic && !flag_isoc99 && !in_system_header
7251 && warn_long_long)
7252 pedwarn ("ISO C90 does not support %<long long%>");
7253 specs->long_long_p = 1;
7254 break;
7256 if (specs->short_p)
7257 error ("both %<long%> and %<short%> in "
7258 "declaration specifiers");
7259 else if (specs->typespec_word == cts_void)
7260 error ("both %<long%> and %<void%> in "
7261 "declaration specifiers");
7262 else if (specs->typespec_word == cts_bool)
7263 error ("both %<long%> and %<_Bool%> in "
7264 "declaration specifiers");
7265 else if (specs->typespec_word == cts_char)
7266 error ("both %<long%> and %<char%> in "
7267 "declaration specifiers");
7268 else if (specs->typespec_word == cts_float)
7269 error ("both %<long%> and %<float%> in "
7270 "declaration specifiers");
7271 else if (specs->typespec_word == cts_dfloat32)
7272 error ("both %<long%> and %<_Decimal32%> in "
7273 "declaration specifiers");
7274 else if (specs->typespec_word == cts_dfloat64)
7275 error ("both %<long%> and %<_Decimal64%> in "
7276 "declaration specifiers");
7277 else if (specs->typespec_word == cts_dfloat128)
7278 error ("both %<long%> and %<_Decimal128%> in "
7279 "declaration specifiers");
7280 else
7281 specs->long_p = true;
7282 break;
7283 case RID_SHORT:
7284 dupe = specs->short_p;
7285 if (specs->long_p)
7286 error ("both %<long%> and %<short%> in "
7287 "declaration specifiers");
7288 else if (specs->typespec_word == cts_void)
7289 error ("both %<short%> and %<void%> in "
7290 "declaration specifiers");
7291 else if (specs->typespec_word == cts_bool)
7292 error ("both %<short%> and %<_Bool%> in "
7293 "declaration specifiers");
7294 else if (specs->typespec_word == cts_char)
7295 error ("both %<short%> and %<char%> in "
7296 "declaration specifiers");
7297 else if (specs->typespec_word == cts_float)
7298 error ("both %<short%> and %<float%> in "
7299 "declaration specifiers");
7300 else if (specs->typespec_word == cts_double)
7301 error ("both %<short%> and %<double%> in "
7302 "declaration specifiers");
7303 else if (specs->typespec_word == cts_dfloat32)
7304 error ("both %<short%> and %<_Decimal32%> in "
7305 "declaration specifiers");
7306 else if (specs->typespec_word == cts_dfloat64)
7307 error ("both %<short%> and %<_Decimal64%> in "
7308 "declaration specifiers");
7309 else if (specs->typespec_word == cts_dfloat128)
7310 error ("both %<short%> and %<_Decimal128%> in "
7311 "declaration specifiers");
7312 else
7313 specs->short_p = true;
7314 break;
7315 case RID_SIGNED:
7316 dupe = specs->signed_p;
7317 if (specs->unsigned_p)
7318 error ("both %<signed%> and %<unsigned%> in "
7319 "declaration specifiers");
7320 else if (specs->typespec_word == cts_void)
7321 error ("both %<signed%> and %<void%> in "
7322 "declaration specifiers");
7323 else if (specs->typespec_word == cts_bool)
7324 error ("both %<signed%> and %<_Bool%> in "
7325 "declaration specifiers");
7326 else if (specs->typespec_word == cts_float)
7327 error ("both %<signed%> and %<float%> in "
7328 "declaration specifiers");
7329 else if (specs->typespec_word == cts_double)
7330 error ("both %<signed%> and %<double%> in "
7331 "declaration specifiers");
7332 else if (specs->typespec_word == cts_dfloat32)
7333 error ("both %<signed%> and %<_Decimal32%> in "
7334 "declaration specifiers");
7335 else if (specs->typespec_word == cts_dfloat64)
7336 error ("both %<signed%> and %<_Decimal64%> in "
7337 "declaration specifiers");
7338 else if (specs->typespec_word == cts_dfloat128)
7339 error ("both %<signed%> and %<_Decimal128%> in "
7340 "declaration specifiers");
7341 else
7342 specs->signed_p = true;
7343 break;
7344 case RID_UNSIGNED:
7345 dupe = specs->unsigned_p;
7346 if (specs->signed_p)
7347 error ("both %<signed%> and %<unsigned%> in "
7348 "declaration specifiers");
7349 else if (specs->typespec_word == cts_void)
7350 error ("both %<unsigned%> and %<void%> in "
7351 "declaration specifiers");
7352 else if (specs->typespec_word == cts_bool)
7353 error ("both %<unsigned%> and %<_Bool%> in "
7354 "declaration specifiers");
7355 else if (specs->typespec_word == cts_float)
7356 error ("both %<unsigned%> and %<float%> in "
7357 "declaration specifiers");
7358 else if (specs->typespec_word == cts_double)
7359 error ("both %<unsigned%> and %<double%> in "
7360 "declaration specifiers");
7361 else if (specs->typespec_word == cts_dfloat32)
7362 error ("both %<unsigned%> and %<_Decimal32%> in "
7363 "declaration specifiers");
7364 else if (specs->typespec_word == cts_dfloat64)
7365 error ("both %<unsigned%> and %<_Decimal64%> in "
7366 "declaration specifiers");
7367 else if (specs->typespec_word == cts_dfloat128)
7368 error ("both %<unsigned%> and %<_Decimal128%> in "
7369 "declaration specifiers");
7370 else
7371 specs->unsigned_p = true;
7372 break;
7373 case RID_COMPLEX:
7374 dupe = specs->complex_p;
7375 if (pedantic && !flag_isoc99 && !in_system_header)
7376 pedwarn ("ISO C90 does not support complex types");
7377 if (specs->typespec_word == cts_void)
7378 error ("both %<complex%> and %<void%> in "
7379 "declaration specifiers");
7380 else if (specs->typespec_word == cts_bool)
7381 error ("both %<complex%> and %<_Bool%> in "
7382 "declaration specifiers");
7383 else if (specs->typespec_word == cts_dfloat32)
7384 error ("both %<complex%> and %<_Decimal32%> in "
7385 "declaration specifiers");
7386 else if (specs->typespec_word == cts_dfloat64)
7387 error ("both %<complex%> and %<_Decimal64%> in "
7388 "declaration specifiers");
7389 else if (specs->typespec_word == cts_dfloat128)
7390 error ("both %<complex%> and %<_Decimal128%> in "
7391 "declaration specifiers");
7392 else
7393 specs->complex_p = true;
7394 break;
7395 default:
7396 gcc_unreachable ();
7399 if (dupe)
7400 error ("duplicate %qE", type);
7402 return specs;
7404 else
7406 /* "void", "_Bool", "char", "int", "float" or "double". */
7407 if (specs->typespec_word != cts_none)
7409 error ("two or more data types in declaration specifiers");
7410 return specs;
7412 switch (i)
7414 case RID_VOID:
7415 if (specs->long_p)
7416 error ("both %<long%> and %<void%> in "
7417 "declaration specifiers");
7418 else if (specs->short_p)
7419 error ("both %<short%> and %<void%> in "
7420 "declaration specifiers");
7421 else if (specs->signed_p)
7422 error ("both %<signed%> and %<void%> in "
7423 "declaration specifiers");
7424 else if (specs->unsigned_p)
7425 error ("both %<unsigned%> and %<void%> in "
7426 "declaration specifiers");
7427 else if (specs->complex_p)
7428 error ("both %<complex%> and %<void%> in "
7429 "declaration specifiers");
7430 else
7431 specs->typespec_word = cts_void;
7432 return specs;
7433 case RID_BOOL:
7434 if (specs->long_p)
7435 error ("both %<long%> and %<_Bool%> in "
7436 "declaration specifiers");
7437 else if (specs->short_p)
7438 error ("both %<short%> and %<_Bool%> in "
7439 "declaration specifiers");
7440 else if (specs->signed_p)
7441 error ("both %<signed%> and %<_Bool%> in "
7442 "declaration specifiers");
7443 else if (specs->unsigned_p)
7444 error ("both %<unsigned%> and %<_Bool%> in "
7445 "declaration specifiers");
7446 else if (specs->complex_p)
7447 error ("both %<complex%> and %<_Bool%> in "
7448 "declaration specifiers");
7449 else
7450 specs->typespec_word = cts_bool;
7451 return specs;
7452 case RID_CHAR:
7453 if (specs->long_p)
7454 error ("both %<long%> and %<char%> in "
7455 "declaration specifiers");
7456 else if (specs->short_p)
7457 error ("both %<short%> and %<char%> in "
7458 "declaration specifiers");
7459 else
7460 specs->typespec_word = cts_char;
7461 return specs;
7462 case RID_INT:
7463 specs->typespec_word = cts_int;
7464 return specs;
7465 case RID_FLOAT:
7466 if (specs->long_p)
7467 error ("both %<long%> and %<float%> in "
7468 "declaration specifiers");
7469 else if (specs->short_p)
7470 error ("both %<short%> and %<float%> in "
7471 "declaration specifiers");
7472 else if (specs->signed_p)
7473 error ("both %<signed%> and %<float%> in "
7474 "declaration specifiers");
7475 else if (specs->unsigned_p)
7476 error ("both %<unsigned%> and %<float%> in "
7477 "declaration specifiers");
7478 else
7479 specs->typespec_word = cts_float;
7480 return specs;
7481 case RID_DOUBLE:
7482 if (specs->long_long_p)
7483 error ("both %<long long%> and %<double%> in "
7484 "declaration specifiers");
7485 else if (specs->short_p)
7486 error ("both %<short%> and %<double%> in "
7487 "declaration specifiers");
7488 else if (specs->signed_p)
7489 error ("both %<signed%> and %<double%> in "
7490 "declaration specifiers");
7491 else if (specs->unsigned_p)
7492 error ("both %<unsigned%> and %<double%> in "
7493 "declaration specifiers");
7494 else
7495 specs->typespec_word = cts_double;
7496 return specs;
7497 case RID_DFLOAT32:
7498 case RID_DFLOAT64:
7499 case RID_DFLOAT128:
7501 const char *str;
7502 if (i == RID_DFLOAT32)
7503 str = "_Decimal32";
7504 else if (i == RID_DFLOAT64)
7505 str = "_Decimal64";
7506 else
7507 str = "_Decimal128";
7508 if (specs->long_long_p)
7509 error ("both %<long long%> and %<%s%> in "
7510 "declaration specifiers", str);
7511 if (specs->long_p)
7512 error ("both %<long%> and %<%s%> in "
7513 "declaration specifiers", str);
7514 else if (specs->short_p)
7515 error ("both %<short%> and %<%s%> in "
7516 "declaration specifiers", str);
7517 else if (specs->signed_p)
7518 error ("both %<signed%> and %<%s%> in "
7519 "declaration specifiers", str);
7520 else if (specs->unsigned_p)
7521 error ("both %<unsigned%> and %<%s%> in "
7522 "declaration specifiers", str);
7523 else if (specs->complex_p)
7524 error ("both %<complex%> and %<%s%> in "
7525 "declaration specifiers", str);
7526 else if (i == RID_DFLOAT32)
7527 specs->typespec_word = cts_dfloat32;
7528 else if (i == RID_DFLOAT64)
7529 specs->typespec_word = cts_dfloat64;
7530 else
7531 specs->typespec_word = cts_dfloat128;
7533 if (!targetm.decimal_float_supported_p ())
7534 error ("decimal floating point not supported for this target");
7535 if (pedantic)
7536 pedwarn ("ISO C does not support decimal floating point");
7537 return specs;
7538 default:
7539 /* ObjC reserved word "id", handled below. */
7540 break;
7545 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7546 form of ObjC type, cases such as "int" and "long" being handled
7547 above), a TYPE (struct, union, enum and typeof specifiers) or an
7548 ERROR_MARK. In none of these cases may there have previously
7549 been any type specifiers. */
7550 if (specs->type || specs->typespec_word != cts_none
7551 || specs->long_p || specs->short_p || specs->signed_p
7552 || specs->unsigned_p || specs->complex_p)
7553 error ("two or more data types in declaration specifiers");
7554 else if (TREE_CODE (type) == TYPE_DECL)
7556 if (TREE_TYPE (type) == error_mark_node)
7557 ; /* Allow the type to default to int to avoid cascading errors. */
7558 else
7560 specs->type = TREE_TYPE (type);
7561 specs->decl_attr = DECL_ATTRIBUTES (type);
7562 specs->typedef_p = true;
7563 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7566 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7568 tree t = lookup_name (type);
7569 if (!t || TREE_CODE (t) != TYPE_DECL)
7570 error ("%qE fails to be a typedef or built in type", type);
7571 else if (TREE_TYPE (t) == error_mark_node)
7573 else
7574 specs->type = TREE_TYPE (t);
7576 else if (TREE_CODE (type) != ERROR_MARK)
7578 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7579 specs->tag_defined_p = true;
7580 if (spec.kind == ctsk_typeof)
7581 specs->typedef_p = true;
7582 specs->type = type;
7585 return specs;
7588 /* Add the storage class specifier or function specifier SCSPEC to the
7589 declaration specifiers SPECS, returning SPECS. */
7591 struct c_declspecs *
7592 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7594 enum rid i;
7595 enum c_storage_class n = csc_none;
7596 bool dupe = false;
7597 specs->declspecs_seen_p = true;
7598 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7599 && C_IS_RESERVED_WORD (scspec));
7600 i = C_RID_CODE (scspec);
7601 if (extra_warnings && specs->non_sc_seen_p)
7602 warning (OPT_Wextra, "%qE is not at beginning of declaration", scspec);
7603 switch (i)
7605 case RID_INLINE:
7606 /* C99 permits duplicate inline. Although of doubtful utility,
7607 it seems simplest to permit it in gnu89 mode as well, as
7608 there is also little utility in maintaining this as a
7609 difference between gnu89 and C99 inline. */
7610 dupe = false;
7611 specs->inline_p = true;
7612 break;
7613 case RID_THREAD:
7614 dupe = specs->thread_p;
7615 if (specs->storage_class == csc_auto)
7616 error ("%<__thread%> used with %<auto%>");
7617 else if (specs->storage_class == csc_register)
7618 error ("%<__thread%> used with %<register%>");
7619 else if (specs->storage_class == csc_typedef)
7620 error ("%<__thread%> used with %<typedef%>");
7621 else
7622 specs->thread_p = true;
7623 break;
7624 case RID_AUTO:
7625 n = csc_auto;
7626 break;
7627 case RID_EXTERN:
7628 n = csc_extern;
7629 /* Diagnose "__thread extern". */
7630 if (specs->thread_p)
7631 error ("%<__thread%> before %<extern%>");
7632 break;
7633 case RID_REGISTER:
7634 n = csc_register;
7635 break;
7636 case RID_STATIC:
7637 n = csc_static;
7638 /* Diagnose "__thread static". */
7639 if (specs->thread_p)
7640 error ("%<__thread%> before %<static%>");
7641 break;
7642 case RID_TYPEDEF:
7643 n = csc_typedef;
7644 break;
7645 default:
7646 gcc_unreachable ();
7648 if (n != csc_none && n == specs->storage_class)
7649 dupe = true;
7650 if (dupe)
7651 error ("duplicate %qE", scspec);
7652 if (n != csc_none)
7654 if (specs->storage_class != csc_none && n != specs->storage_class)
7656 error ("multiple storage classes in declaration specifiers");
7658 else
7660 specs->storage_class = n;
7661 if (n != csc_extern && n != csc_static && specs->thread_p)
7663 error ("%<__thread%> used with %qE", scspec);
7664 specs->thread_p = false;
7668 return specs;
7671 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7672 returning SPECS. */
7674 struct c_declspecs *
7675 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7677 specs->attrs = chainon (attrs, specs->attrs);
7678 specs->declspecs_seen_p = true;
7679 return specs;
7682 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7683 specifiers with any other type specifier to determine the resulting
7684 type. This is where ISO C checks on complex types are made, since
7685 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7686 double". */
7688 struct c_declspecs *
7689 finish_declspecs (struct c_declspecs *specs)
7691 /* If a type was specified as a whole, we have no modifiers and are
7692 done. */
7693 if (specs->type != NULL_TREE)
7695 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7696 && !specs->signed_p && !specs->unsigned_p
7697 && !specs->complex_p);
7698 return specs;
7701 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7702 has been specified, treat it as "int" unless "_Complex" is
7703 present and there are no other specifiers. If we just have
7704 "_Complex", it is equivalent to "_Complex double", but e.g.
7705 "_Complex short" is equivalent to "_Complex short int". */
7706 if (specs->typespec_word == cts_none)
7708 if (specs->long_p || specs->short_p
7709 || specs->signed_p || specs->unsigned_p)
7711 specs->typespec_word = cts_int;
7713 else if (specs->complex_p)
7715 specs->typespec_word = cts_double;
7716 if (pedantic)
7717 pedwarn ("ISO C does not support plain %<complex%> meaning "
7718 "%<double complex%>");
7720 else
7722 specs->typespec_word = cts_int;
7723 specs->default_int_p = true;
7724 /* We don't diagnose this here because grokdeclarator will
7725 give more specific diagnostics according to whether it is
7726 a function definition. */
7730 /* If "signed" was specified, record this to distinguish "int" and
7731 "signed int" in the case of a bit-field with
7732 -funsigned-bitfields. */
7733 specs->explicit_signed_p = specs->signed_p;
7735 /* Now compute the actual type. */
7736 switch (specs->typespec_word)
7738 case cts_void:
7739 gcc_assert (!specs->long_p && !specs->short_p
7740 && !specs->signed_p && !specs->unsigned_p
7741 && !specs->complex_p);
7742 specs->type = void_type_node;
7743 break;
7744 case cts_bool:
7745 gcc_assert (!specs->long_p && !specs->short_p
7746 && !specs->signed_p && !specs->unsigned_p
7747 && !specs->complex_p);
7748 specs->type = boolean_type_node;
7749 break;
7750 case cts_char:
7751 gcc_assert (!specs->long_p && !specs->short_p);
7752 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7753 if (specs->signed_p)
7754 specs->type = signed_char_type_node;
7755 else if (specs->unsigned_p)
7756 specs->type = unsigned_char_type_node;
7757 else
7758 specs->type = char_type_node;
7759 if (specs->complex_p)
7761 if (pedantic)
7762 pedwarn ("ISO C does not support complex integer types");
7763 specs->type = build_complex_type (specs->type);
7765 break;
7766 case cts_int:
7767 gcc_assert (!(specs->long_p && specs->short_p));
7768 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7769 if (specs->long_long_p)
7770 specs->type = (specs->unsigned_p
7771 ? long_long_unsigned_type_node
7772 : long_long_integer_type_node);
7773 else if (specs->long_p)
7774 specs->type = (specs->unsigned_p
7775 ? long_unsigned_type_node
7776 : long_integer_type_node);
7777 else if (specs->short_p)
7778 specs->type = (specs->unsigned_p
7779 ? short_unsigned_type_node
7780 : short_integer_type_node);
7781 else
7782 specs->type = (specs->unsigned_p
7783 ? unsigned_type_node
7784 : integer_type_node);
7785 if (specs->complex_p)
7787 if (pedantic)
7788 pedwarn ("ISO C does not support complex integer types");
7789 specs->type = build_complex_type (specs->type);
7791 break;
7792 case cts_float:
7793 gcc_assert (!specs->long_p && !specs->short_p
7794 && !specs->signed_p && !specs->unsigned_p);
7795 specs->type = (specs->complex_p
7796 ? complex_float_type_node
7797 : float_type_node);
7798 break;
7799 case cts_double:
7800 gcc_assert (!specs->long_long_p && !specs->short_p
7801 && !specs->signed_p && !specs->unsigned_p);
7802 if (specs->long_p)
7804 specs->type = (specs->complex_p
7805 ? complex_long_double_type_node
7806 : long_double_type_node);
7808 else
7810 specs->type = (specs->complex_p
7811 ? complex_double_type_node
7812 : double_type_node);
7814 break;
7815 case cts_dfloat32:
7816 case cts_dfloat64:
7817 case cts_dfloat128:
7818 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7819 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7820 if (specs->typespec_word == cts_dfloat32)
7821 specs->type = dfloat32_type_node;
7822 else if (specs->typespec_word == cts_dfloat64)
7823 specs->type = dfloat64_type_node;
7824 else
7825 specs->type = dfloat128_type_node;
7826 break;
7827 default:
7828 gcc_unreachable ();
7831 return specs;
7834 /* Synthesize a function which calls all the global ctors or global
7835 dtors in this file. This is only used for targets which do not
7836 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7837 static void
7838 build_cdtor (int method_type, tree cdtors)
7840 tree body = 0;
7842 if (!cdtors)
7843 return;
7845 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7846 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7847 &body);
7849 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7852 /* A subroutine of c_write_global_declarations. Perform final processing
7853 on one file scope's declarations (or the external scope's declarations),
7854 GLOBALS. */
7856 static void
7857 c_write_global_declarations_1 (tree globals)
7859 tree decl;
7860 bool reconsider;
7862 /* Process the decls in the order they were written. */
7863 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7865 /* Check for used but undefined static functions using the C
7866 standard's definition of "used", and set TREE_NO_WARNING so
7867 that check_global_declarations doesn't repeat the check. */
7868 if (TREE_CODE (decl) == FUNCTION_DECL
7869 && DECL_INITIAL (decl) == 0
7870 && DECL_EXTERNAL (decl)
7871 && !TREE_PUBLIC (decl)
7872 && C_DECL_USED (decl))
7874 pedwarn ("%q+F used but never defined", decl);
7875 TREE_NO_WARNING (decl) = 1;
7878 wrapup_global_declaration_1 (decl);
7883 reconsider = false;
7884 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7885 reconsider |= wrapup_global_declaration_2 (decl);
7887 while (reconsider);
7889 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7890 check_global_declaration_1 (decl);
7893 /* A subroutine of c_write_global_declarations Emit debug information for each
7894 of the declarations in GLOBALS. */
7896 static void
7897 c_write_global_declarations_2 (tree globals)
7899 tree decl;
7901 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
7902 debug_hooks->global_decl (decl);
7905 /* Preserve the external declarations scope across a garbage collect. */
7906 static GTY(()) tree ext_block;
7908 void
7909 c_write_global_declarations (void)
7911 tree t;
7913 /* We don't want to do this if generating a PCH. */
7914 if (pch_file)
7915 return;
7917 /* Don't waste time on further processing if -fsyntax-only or we've
7918 encountered errors. */
7919 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7920 return;
7922 /* Close the external scope. */
7923 ext_block = pop_scope ();
7924 external_scope = 0;
7925 gcc_assert (!current_scope);
7927 if (ext_block)
7929 tree tmp = BLOCK_VARS (ext_block);
7930 int flags;
7931 FILE * stream = dump_begin (TDI_tu, &flags);
7932 if (stream && tmp)
7934 dump_node (tmp, flags & ~TDF_SLIM, stream);
7935 dump_end (TDI_tu, stream);
7939 /* Process all file scopes in this compilation, and the external_scope,
7940 through wrapup_global_declarations and check_global_declarations. */
7941 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7942 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7943 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7945 /* Generate functions to call static constructors and destructors
7946 for targets that do not support .ctors/.dtors sections. These
7947 functions have magic names which are detected by collect2. */
7948 build_cdtor ('I', static_ctors); static_ctors = 0;
7949 build_cdtor ('D', static_dtors); static_dtors = 0;
7951 /* We're done parsing; proceed to optimize and emit assembly.
7952 FIXME: shouldn't be the front end's responsibility to call this. */
7953 cgraph_optimize ();
7955 /* After cgraph has had a chance to emit everything that's going to
7956 be emitted, output debug information for globals. */
7957 if (errorcount == 0 && sorrycount == 0)
7959 timevar_push (TV_SYMOUT);
7960 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7961 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
7962 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
7963 timevar_pop (TV_SYMOUT);
7966 ext_block = NULL;
7969 #include "gt-c-decl.h"