* tree-ssa-loop-prefetch.c (determine_unroll_factor): Bound the unroll
[official-gcc.git] / gcc / c-decl.c
bloba84dea67f30e1995c300d4c818f9157f08d8c93b
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "input.h"
33 #include "tm.h"
34 #include "intl.h"
35 #include "tree.h"
36 #include "tree-inline.h"
37 #include "rtl.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "output.h"
41 #include "expr.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tm_p.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "debug.h"
49 #include "opts.h"
50 #include "timevar.h"
51 #include "c-common.h"
52 #include "c-pragma.h"
53 #include "langhooks.h"
54 #include "tree-mudflap.h"
55 #include "tree-gimple.h"
56 #include "diagnostic.h"
57 #include "tree-dump.h"
58 #include "cgraph.h"
59 #include "hashtab.h"
60 #include "libfuncs.h"
61 #include "except.h"
62 #include "langhooks-def.h"
63 #include "pointer-set.h"
65 /* Set this to 1 if you want the standard ISO C99 semantics of 'inline'
66 when you specify -std=c99 or -std=gnu99, and to 0 if you want
67 behavior compatible with the nonstandard semantics implemented by
68 GCC 2.95 through 4.2. */
69 #define WANT_C99_INLINE_SEMANTICS 1
71 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
72 enum decl_context
73 { NORMAL, /* Ordinary declaration */
74 FUNCDEF, /* Function definition */
75 PARM, /* Declaration of parm before function body */
76 FIELD, /* Declaration inside struct or union */
77 TYPENAME}; /* Typename (inside cast or sizeof) */
80 /* Nonzero if we have seen an invalid cross reference
81 to a struct, union, or enum, but not yet printed the message. */
82 tree pending_invalid_xref;
84 /* File and line to appear in the eventual error message. */
85 location_t pending_invalid_xref_location;
87 /* True means we've initialized exception handling. */
88 bool c_eh_initialized_p;
90 /* While defining an enum type, this is 1 plus the last enumerator
91 constant value. Note that will do not have to save this or `enum_overflow'
92 around nested function definition since such a definition could only
93 occur in an enum value expression and we don't use these variables in
94 that case. */
96 static tree enum_next_value;
98 /* Nonzero means that there was overflow computing enum_next_value. */
100 static int enum_overflow;
102 /* The file and line that the prototype came from if this is an
103 old-style definition; used for diagnostics in
104 store_parm_decls_oldstyle. */
106 static location_t current_function_prototype_locus;
108 /* Whether this prototype was built-in. */
110 static bool current_function_prototype_built_in;
112 /* The argument type information of this prototype. */
114 static tree current_function_prototype_arg_types;
116 /* The argument information structure for the function currently being
117 defined. */
119 static struct c_arg_info *current_function_arg_info;
121 /* The obstack on which parser and related data structures, which are
122 not live beyond their top-level declaration or definition, are
123 allocated. */
124 struct obstack parser_obstack;
126 /* The current statement tree. */
128 static GTY(()) struct stmt_tree_s c_stmt_tree;
130 /* State saving variables. */
131 tree c_break_label;
132 tree c_cont_label;
134 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
135 included in this invocation. Note that the current translation
136 unit is not included in this list. */
138 static GTY(()) tree all_translation_units;
140 /* A list of decls to be made automatically visible in each file scope. */
141 static GTY(()) tree visible_builtins;
143 /* Set to 0 at beginning of a function definition, set to 1 if
144 a return statement that specifies a return value is seen. */
146 int current_function_returns_value;
148 /* Set to 0 at beginning of a function definition, set to 1 if
149 a return statement with no argument is seen. */
151 int current_function_returns_null;
153 /* Set to 0 at beginning of a function definition, set to 1 if
154 a call to a noreturn function is seen. */
156 int current_function_returns_abnormally;
158 /* Set to nonzero by `grokdeclarator' for a function
159 whose return type is defaulted, if warnings for this are desired. */
161 static int warn_about_return_type;
163 /* Nonzero when the current toplevel function contains a declaration
164 of a nested function which is never defined. */
166 static bool undef_nested_function;
168 /* True means global_bindings_p should return false even if the scope stack
169 says we are in file scope. */
170 bool c_override_global_bindings_to_false;
173 /* Each c_binding structure describes one binding of an identifier to
174 a decl. All the decls in a scope - irrespective of namespace - are
175 chained together by the ->prev field, which (as the name implies)
176 runs in reverse order. All the decls in a given namespace bound to
177 a given identifier are chained by the ->shadowed field, which runs
178 from inner to outer scopes.
180 The ->decl field usually points to a DECL node, but there are two
181 exceptions. In the namespace of type tags, the bound entity is a
182 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
183 identifier is encountered, it is bound to error_mark_node to
184 suppress further errors about that identifier in the current
185 function.
187 The ->type field stores the type of the declaration in this scope;
188 if NULL, the type is the type of the ->decl field. This is only of
189 relevance for objects with external or internal linkage which may
190 be redeclared in inner scopes, forming composite types that only
191 persist for the duration of those scopes. In the external scope,
192 this stores the composite of all the types declared for this
193 object, visible or not. The ->inner_comp field (used only at file
194 scope) stores whether an incomplete array type at file scope was
195 completed at an inner scope to an array size other than 1.
197 The depth field is copied from the scope structure that holds this
198 decl. It is used to preserve the proper ordering of the ->shadowed
199 field (see bind()) and also for a handful of special-case checks.
200 Finally, the invisible bit is true for a decl which should be
201 ignored for purposes of normal name lookup, and the nested bit is
202 true for a decl that's been bound a second time in an inner scope;
203 in all such cases, the binding in the outer scope will have its
204 invisible bit true. */
206 struct c_binding GTY((chain_next ("%h.prev")))
208 tree decl; /* the decl bound */
209 tree type; /* the type in this scope */
210 tree id; /* the identifier it's bound to */
211 struct c_binding *prev; /* the previous decl in this scope */
212 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
213 unsigned int depth : 28; /* depth of this scope */
214 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
215 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
216 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
217 /* one free bit */
219 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
220 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
221 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
222 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
224 #define I_SYMBOL_BINDING(node) \
225 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
226 #define I_SYMBOL_DECL(node) \
227 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
229 #define I_TAG_BINDING(node) \
230 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
231 #define I_TAG_DECL(node) \
232 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
234 #define I_LABEL_BINDING(node) \
235 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
236 #define I_LABEL_DECL(node) \
237 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
239 /* Each C symbol points to three linked lists of c_binding structures.
240 These describe the values of the identifier in the three different
241 namespaces defined by the language. */
243 struct lang_identifier GTY(())
245 struct c_common_identifier common_id;
246 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
247 struct c_binding *tag_binding; /* struct/union/enum tags */
248 struct c_binding *label_binding; /* labels */
251 /* Validate c-lang.c's assumptions. */
252 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
253 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
255 /* The resulting tree type. */
257 union lang_tree_node
258 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
259 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : (GIMPLE_TUPLE_P (&%h.generic) ? (union lang_tree_node *) 0 : (union lang_tree_node *) TREE_CHAIN (&%h.generic))")))
261 union tree_node GTY ((tag ("0"),
262 desc ("tree_node_structure (&%h)")))
263 generic;
264 struct lang_identifier GTY ((tag ("1"))) identifier;
267 /* Each c_scope structure describes the complete contents of one
268 scope. Four scopes are distinguished specially: the innermost or
269 current scope, the innermost function scope, the file scope (always
270 the second to outermost) and the outermost or external scope.
272 Most declarations are recorded in the current scope.
274 All normal label declarations are recorded in the innermost
275 function scope, as are bindings of undeclared identifiers to
276 error_mark_node. (GCC permits nested functions as an extension,
277 hence the 'innermost' qualifier.) Explicitly declared labels
278 (using the __label__ extension) appear in the current scope.
280 Being in the file scope (current_scope == file_scope) causes
281 special behavior in several places below. Also, under some
282 conditions the Objective-C front end records declarations in the
283 file scope even though that isn't the current scope.
285 All declarations with external linkage are recorded in the external
286 scope, even if they aren't visible there; this models the fact that
287 such declarations are visible to the entire program, and (with a
288 bit of cleverness, see pushdecl) allows diagnosis of some violations
289 of C99 6.2.2p7 and 6.2.7p2:
291 If, within the same translation unit, the same identifier appears
292 with both internal and external linkage, the behavior is
293 undefined.
295 All declarations that refer to the same object or function shall
296 have compatible type; otherwise, the behavior is undefined.
298 Initially only the built-in declarations, which describe compiler
299 intrinsic functions plus a subset of the standard library, are in
300 this scope.
302 The order of the blocks list matters, and it is frequently appended
303 to. To avoid having to walk all the way to the end of the list on
304 each insertion, or reverse the list later, we maintain a pointer to
305 the last list entry. (FIXME: It should be feasible to use a reversed
306 list here.)
308 The bindings list is strictly in reverse order of declarations;
309 pop_scope relies on this. */
312 struct c_scope GTY((chain_next ("%h.outer")))
314 /* The scope containing this one. */
315 struct c_scope *outer;
317 /* The next outermost function scope. */
318 struct c_scope *outer_function;
320 /* All bindings in this scope. */
321 struct c_binding *bindings;
323 /* For each scope (except the global one), a chain of BLOCK nodes
324 for all the scopes that were entered and exited one level down. */
325 tree blocks;
326 tree blocks_last;
328 /* The depth of this scope. Used to keep the ->shadowed chain of
329 bindings sorted innermost to outermost. */
330 unsigned int depth : 28;
332 /* True if we are currently filling this scope with parameter
333 declarations. */
334 BOOL_BITFIELD parm_flag : 1;
336 /* True if we saw [*] in this scope. Used to give an error messages
337 if these appears in a function definition. */
338 BOOL_BITFIELD had_vla_unspec : 1;
340 /* True if we already complained about forward parameter decls
341 in this scope. This prevents double warnings on
342 foo (int a; int b; ...) */
343 BOOL_BITFIELD warned_forward_parm_decls : 1;
345 /* True if this is the outermost block scope of a function body.
346 This scope contains the parameters, the local variables declared
347 in the outermost block, and all the labels (except those in
348 nested functions, or declared at block scope with __label__). */
349 BOOL_BITFIELD function_body : 1;
351 /* True means make a BLOCK for this scope no matter what. */
352 BOOL_BITFIELD keep : 1;
355 /* The scope currently in effect. */
357 static GTY(()) struct c_scope *current_scope;
359 /* The innermost function scope. Ordinary (not explicitly declared)
360 labels, bindings to error_mark_node, and the lazily-created
361 bindings of __func__ and its friends get this scope. */
363 static GTY(()) struct c_scope *current_function_scope;
365 /* The C file scope. This is reset for each input translation unit. */
367 static GTY(()) struct c_scope *file_scope;
369 /* The outermost scope. This is used for all declarations with
370 external linkage, and only these, hence the name. */
372 static GTY(()) struct c_scope *external_scope;
374 /* A chain of c_scope structures awaiting reuse. */
376 static GTY((deletable)) struct c_scope *scope_freelist;
378 /* A chain of c_binding structures awaiting reuse. */
380 static GTY((deletable)) struct c_binding *binding_freelist;
382 /* Append VAR to LIST in scope SCOPE. */
383 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
384 struct c_scope *s_ = (scope); \
385 tree d_ = (decl); \
386 if (s_->list##_last) \
387 TREE_CHAIN (s_->list##_last) = d_; \
388 else \
389 s_->list = d_; \
390 s_->list##_last = d_; \
391 } while (0)
393 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
394 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
395 struct c_scope *t_ = (tscope); \
396 struct c_scope *f_ = (fscope); \
397 if (t_->to##_last) \
398 TREE_CHAIN (t_->to##_last) = f_->from; \
399 else \
400 t_->to = f_->from; \
401 t_->to##_last = f_->from##_last; \
402 } while (0)
404 /* True means unconditionally make a BLOCK for the next scope pushed. */
406 static bool keep_next_level_flag;
408 /* True means the next call to push_scope will be the outermost scope
409 of a function body, so do not push a new scope, merely cease
410 expecting parameter decls. */
412 static bool next_is_function_body;
414 /* Forward declarations. */
415 static tree lookup_name_in_scope (tree, struct c_scope *);
416 static tree c_make_fname_decl (tree, int);
417 static tree grokdeclarator (const struct c_declarator *,
418 struct c_declspecs *,
419 enum decl_context, bool, tree *);
420 static tree grokparms (struct c_arg_info *, bool);
421 static void layout_array_type (tree);
423 /* T is a statement. Add it to the statement-tree. This is the
424 C/ObjC version--C++ has a slightly different version of this
425 function. */
427 tree
428 add_stmt (tree t)
430 enum tree_code code = TREE_CODE (t);
432 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
434 if (!EXPR_HAS_LOCATION (t))
435 SET_EXPR_LOCATION (t, input_location);
438 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
439 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
441 /* Add T to the statement-tree. Non-side-effect statements need to be
442 recorded during statement expressions. */
443 append_to_statement_list_force (t, &cur_stmt_list);
445 return t;
448 /* States indicating how grokdeclarator() should handle declspecs marked
449 with __attribute__((deprecated)). An object declared as
450 __attribute__((deprecated)) suppresses warnings of uses of other
451 deprecated items. */
453 enum deprecated_states {
454 DEPRECATED_NORMAL,
455 DEPRECATED_SUPPRESS
458 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
460 void
461 c_print_identifier (FILE *file, tree node, int indent)
463 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
464 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
465 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
466 if (C_IS_RESERVED_WORD (node))
468 tree rid = ridpointers[C_RID_CODE (node)];
469 indent_to (file, indent + 4);
470 fprintf (file, "rid %p \"%s\"",
471 (void *) rid, IDENTIFIER_POINTER (rid));
475 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
476 which may be any of several kinds of DECL or TYPE or error_mark_node,
477 in the scope SCOPE. */
478 static void
479 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
481 struct c_binding *b, **here;
483 if (binding_freelist)
485 b = binding_freelist;
486 binding_freelist = b->prev;
488 else
489 b = GGC_NEW (struct c_binding);
491 b->shadowed = 0;
492 b->decl = decl;
493 b->id = name;
494 b->depth = scope->depth;
495 b->invisible = invisible;
496 b->nested = nested;
497 b->inner_comp = 0;
499 b->type = 0;
501 b->prev = scope->bindings;
502 scope->bindings = b;
504 if (!name)
505 return;
507 switch (TREE_CODE (decl))
509 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
510 case ENUMERAL_TYPE:
511 case UNION_TYPE:
512 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
513 case VAR_DECL:
514 case FUNCTION_DECL:
515 case TYPE_DECL:
516 case CONST_DECL:
517 case PARM_DECL:
518 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
520 default:
521 gcc_unreachable ();
524 /* Locate the appropriate place in the chain of shadowed decls
525 to insert this binding. Normally, scope == current_scope and
526 this does nothing. */
527 while (*here && (*here)->depth > scope->depth)
528 here = &(*here)->shadowed;
530 b->shadowed = *here;
531 *here = b;
534 /* Clear the binding structure B, stick it on the binding_freelist,
535 and return the former value of b->prev. This is used by pop_scope
536 and get_parm_info to iterate destructively over all the bindings
537 from a given scope. */
538 static struct c_binding *
539 free_binding_and_advance (struct c_binding *b)
541 struct c_binding *prev = b->prev;
543 memset (b, 0, sizeof (struct c_binding));
544 b->prev = binding_freelist;
545 binding_freelist = b;
547 return prev;
551 /* Hook called at end of compilation to assume 1 elt
552 for a file-scope tentative array defn that wasn't complete before. */
554 void
555 c_finish_incomplete_decl (tree decl)
557 if (TREE_CODE (decl) == VAR_DECL)
559 tree type = TREE_TYPE (decl);
560 if (type != error_mark_node
561 && TREE_CODE (type) == ARRAY_TYPE
562 && !DECL_EXTERNAL (decl)
563 && TYPE_DOMAIN (type) == 0)
565 warning (0, "array %q+D assumed to have one element", decl);
567 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
569 layout_decl (decl, 0);
574 /* The Objective-C front-end often needs to determine the current scope. */
576 void *
577 objc_get_current_scope (void)
579 return current_scope;
582 /* The following function is used only by Objective-C. It needs to live here
583 because it accesses the innards of c_scope. */
585 void
586 objc_mark_locals_volatile (void *enclosing_blk)
588 struct c_scope *scope;
589 struct c_binding *b;
591 for (scope = current_scope;
592 scope && scope != enclosing_blk;
593 scope = scope->outer)
595 for (b = scope->bindings; b; b = b->prev)
596 objc_volatilize_decl (b->decl);
598 /* Do not climb up past the current function. */
599 if (scope->function_body)
600 break;
604 /* Nonzero if we are currently in file scope. */
607 global_bindings_p (void)
609 return current_scope == file_scope && !c_override_global_bindings_to_false;
612 void
613 keep_next_level (void)
615 keep_next_level_flag = true;
618 /* Identify this scope as currently being filled with parameters. */
620 void
621 declare_parm_level (void)
623 current_scope->parm_flag = true;
626 void
627 push_scope (void)
629 if (next_is_function_body)
631 /* This is the transition from the parameters to the top level
632 of the function body. These are the same scope
633 (C99 6.2.1p4,6) so we do not push another scope structure.
634 next_is_function_body is set only by store_parm_decls, which
635 in turn is called when and only when we are about to
636 encounter the opening curly brace for the function body.
638 The outermost block of a function always gets a BLOCK node,
639 because the debugging output routines expect that each
640 function has at least one BLOCK. */
641 current_scope->parm_flag = false;
642 current_scope->function_body = true;
643 current_scope->keep = true;
644 current_scope->outer_function = current_function_scope;
645 current_function_scope = current_scope;
647 keep_next_level_flag = false;
648 next_is_function_body = false;
650 else
652 struct c_scope *scope;
653 if (scope_freelist)
655 scope = scope_freelist;
656 scope_freelist = scope->outer;
658 else
659 scope = GGC_CNEW (struct c_scope);
661 scope->keep = keep_next_level_flag;
662 scope->outer = current_scope;
663 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
665 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
666 possible. */
667 if (current_scope && scope->depth == 0)
669 scope->depth--;
670 sorry ("GCC supports only %u nested scopes", scope->depth);
673 current_scope = scope;
674 keep_next_level_flag = false;
678 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
680 static void
681 set_type_context (tree type, tree context)
683 for (type = TYPE_MAIN_VARIANT (type); type;
684 type = TYPE_NEXT_VARIANT (type))
685 TYPE_CONTEXT (type) = context;
688 /* Exit a scope. Restore the state of the identifier-decl mappings
689 that were in effect when this scope was entered. Return a BLOCK
690 node containing all the DECLs in this scope that are of interest
691 to debug info generation. */
693 tree
694 pop_scope (void)
696 struct c_scope *scope = current_scope;
697 tree block, context, p;
698 struct c_binding *b;
700 bool functionbody = scope->function_body;
701 bool keep = functionbody || scope->keep || scope->bindings;
703 c_end_vm_scope (scope->depth);
705 /* If appropriate, create a BLOCK to record the decls for the life
706 of this function. */
707 block = 0;
708 if (keep)
710 block = make_node (BLOCK);
711 BLOCK_SUBBLOCKS (block) = scope->blocks;
712 TREE_USED (block) = 1;
714 /* In each subblock, record that this is its superior. */
715 for (p = scope->blocks; p; p = TREE_CHAIN (p))
716 BLOCK_SUPERCONTEXT (p) = block;
718 BLOCK_VARS (block) = 0;
721 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
722 scope must be set so that they point to the appropriate
723 construct, i.e. either to the current FUNCTION_DECL node, or
724 else to the BLOCK node we just constructed.
726 Note that for tagged types whose scope is just the formal
727 parameter list for some function type specification, we can't
728 properly set their TYPE_CONTEXTs here, because we don't have a
729 pointer to the appropriate FUNCTION_TYPE node readily available
730 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
731 type nodes get set in `grokdeclarator' as soon as we have created
732 the FUNCTION_TYPE node which will represent the "scope" for these
733 "parameter list local" tagged types. */
734 if (scope->function_body)
735 context = current_function_decl;
736 else if (scope == file_scope)
738 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
739 TREE_CHAIN (file_decl) = all_translation_units;
740 all_translation_units = file_decl;
741 context = file_decl;
743 else
744 context = block;
746 /* Clear all bindings in this scope. */
747 for (b = scope->bindings; b; b = free_binding_and_advance (b))
749 p = b->decl;
750 switch (TREE_CODE (p))
752 case LABEL_DECL:
753 /* Warnings for unused labels, errors for undefined labels. */
754 if (TREE_USED (p) && !DECL_INITIAL (p))
756 error ("label %q+D used but not defined", p);
757 DECL_INITIAL (p) = error_mark_node;
759 else
760 warn_for_unused_label (p);
762 /* Labels go in BLOCK_VARS. */
763 TREE_CHAIN (p) = BLOCK_VARS (block);
764 BLOCK_VARS (block) = p;
765 gcc_assert (I_LABEL_BINDING (b->id) == b);
766 I_LABEL_BINDING (b->id) = b->shadowed;
767 break;
769 case ENUMERAL_TYPE:
770 case UNION_TYPE:
771 case RECORD_TYPE:
772 set_type_context (p, context);
774 /* Types may not have tag-names, in which case the type
775 appears in the bindings list with b->id NULL. */
776 if (b->id)
778 gcc_assert (I_TAG_BINDING (b->id) == b);
779 I_TAG_BINDING (b->id) = b->shadowed;
781 break;
783 case FUNCTION_DECL:
784 /* Propagate TREE_ADDRESSABLE from nested functions to their
785 containing functions. */
786 if (!TREE_ASM_WRITTEN (p)
787 && DECL_INITIAL (p) != 0
788 && TREE_ADDRESSABLE (p)
789 && DECL_ABSTRACT_ORIGIN (p) != 0
790 && DECL_ABSTRACT_ORIGIN (p) != p)
791 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
792 if (!DECL_EXTERNAL (p)
793 && !DECL_INITIAL (p)
794 && scope != file_scope
795 && scope != external_scope)
797 error ("nested function %q+D declared but never defined", p);
798 undef_nested_function = true;
800 /* C99 6.7.4p6: "a function with external linkage... declared
801 with an inline function specifier ... shall also be defined in the
802 same translation unit." */
803 else if (DECL_DECLARED_INLINE_P (p)
804 && TREE_PUBLIC (p)
805 && !DECL_INITIAL (p)
806 && flag_isoc99)
807 pedwarn ("inline function %q+D declared but never defined", p);
809 goto common_symbol;
811 case VAR_DECL:
812 /* Warnings for unused variables. */
813 if (!TREE_USED (p)
814 && !TREE_NO_WARNING (p)
815 && !DECL_IN_SYSTEM_HEADER (p)
816 && DECL_NAME (p)
817 && !DECL_ARTIFICIAL (p)
818 && scope != file_scope
819 && scope != external_scope)
820 warning (OPT_Wunused_variable, "unused variable %q+D", p);
822 if (b->inner_comp)
824 error ("type of array %q+D completed incompatibly with"
825 " implicit initialization", p);
828 /* Fall through. */
829 case TYPE_DECL:
830 case CONST_DECL:
831 common_symbol:
832 /* All of these go in BLOCK_VARS, but only if this is the
833 binding in the home scope. */
834 if (!b->nested)
836 TREE_CHAIN (p) = BLOCK_VARS (block);
837 BLOCK_VARS (block) = p;
839 /* If this is the file scope, and we are processing more
840 than one translation unit in this compilation, set
841 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
842 This makes same_translation_unit_p work, and causes
843 static declarations to be given disambiguating suffixes. */
844 if (scope == file_scope && num_in_fnames > 1)
846 DECL_CONTEXT (p) = context;
847 if (TREE_CODE (p) == TYPE_DECL)
848 set_type_context (TREE_TYPE (p), context);
851 /* Fall through. */
852 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
853 already been put there by store_parm_decls. Unused-
854 parameter warnings are handled by function.c.
855 error_mark_node obviously does not go in BLOCK_VARS and
856 does not get unused-variable warnings. */
857 case PARM_DECL:
858 case ERROR_MARK:
859 /* It is possible for a decl not to have a name. We get
860 here with b->id NULL in this case. */
861 if (b->id)
863 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
864 I_SYMBOL_BINDING (b->id) = b->shadowed;
865 if (b->shadowed && b->shadowed->type)
866 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
868 break;
870 default:
871 gcc_unreachable ();
876 /* Dispose of the block that we just made inside some higher level. */
877 if ((scope->function_body || scope == file_scope) && context)
879 DECL_INITIAL (context) = block;
880 BLOCK_SUPERCONTEXT (block) = context;
882 else if (scope->outer)
884 if (block)
885 SCOPE_LIST_APPEND (scope->outer, blocks, block);
886 /* If we did not make a block for the scope just exited, any
887 blocks made for inner scopes must be carried forward so they
888 will later become subblocks of something else. */
889 else if (scope->blocks)
890 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
893 /* Pop the current scope, and free the structure for reuse. */
894 current_scope = scope->outer;
895 if (scope->function_body)
896 current_function_scope = scope->outer_function;
898 memset (scope, 0, sizeof (struct c_scope));
899 scope->outer = scope_freelist;
900 scope_freelist = scope;
902 return block;
905 void
906 push_file_scope (void)
908 tree decl;
910 if (file_scope)
911 return;
913 push_scope ();
914 file_scope = current_scope;
916 start_fname_decls ();
918 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
919 bind (DECL_NAME (decl), decl, file_scope,
920 /*invisible=*/false, /*nested=*/true);
923 void
924 pop_file_scope (void)
926 /* In case there were missing closebraces, get us back to the global
927 binding level. */
928 while (current_scope != file_scope)
929 pop_scope ();
931 /* __FUNCTION__ is defined at file scope (""). This
932 call may not be necessary as my tests indicate it
933 still works without it. */
934 finish_fname_decls ();
936 /* This is the point to write out a PCH if we're doing that.
937 In that case we do not want to do anything else. */
938 if (pch_file)
940 c_common_write_pch ();
941 return;
944 /* Pop off the file scope and close this translation unit. */
945 pop_scope ();
946 file_scope = 0;
948 maybe_apply_pending_pragma_weaks ();
949 cgraph_finalize_compilation_unit ();
952 /* Insert BLOCK at the end of the list of subblocks of the current
953 scope. This is used when a BIND_EXPR is expanded, to handle the
954 BLOCK node inside the BIND_EXPR. */
956 void
957 insert_block (tree block)
959 TREE_USED (block) = 1;
960 SCOPE_LIST_APPEND (current_scope, blocks, block);
963 /* Push a definition or a declaration of struct, union or enum tag "name".
964 "type" should be the type node.
965 We assume that the tag "name" is not already defined.
967 Note that the definition may really be just a forward reference.
968 In that case, the TYPE_SIZE will be zero. */
970 static void
971 pushtag (tree name, tree type)
973 /* Record the identifier as the type's name if it has none. */
974 if (name && !TYPE_NAME (type))
975 TYPE_NAME (type) = name;
976 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
978 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
979 tagged type we just added to the current scope. This fake
980 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
981 to output a representation of a tagged type, and it also gives
982 us a convenient place to record the "scope start" address for the
983 tagged type. */
985 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
987 /* An approximation for now, so we can tell this is a function-scope tag.
988 This will be updated in pop_scope. */
989 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
992 /* Subroutine of compare_decls. Allow harmless mismatches in return
993 and argument types provided that the type modes match. This function
994 return a unified type given a suitable match, and 0 otherwise. */
996 static tree
997 match_builtin_function_types (tree newtype, tree oldtype)
999 tree newrettype, oldrettype;
1000 tree newargs, oldargs;
1001 tree trytype, tryargs;
1003 /* Accept the return type of the new declaration if same modes. */
1004 oldrettype = TREE_TYPE (oldtype);
1005 newrettype = TREE_TYPE (newtype);
1007 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1008 return 0;
1010 oldargs = TYPE_ARG_TYPES (oldtype);
1011 newargs = TYPE_ARG_TYPES (newtype);
1012 tryargs = newargs;
1014 while (oldargs || newargs)
1016 if (!oldargs
1017 || !newargs
1018 || !TREE_VALUE (oldargs)
1019 || !TREE_VALUE (newargs)
1020 || TYPE_MODE (TREE_VALUE (oldargs))
1021 != TYPE_MODE (TREE_VALUE (newargs)))
1022 return 0;
1024 oldargs = TREE_CHAIN (oldargs);
1025 newargs = TREE_CHAIN (newargs);
1028 trytype = build_function_type (newrettype, tryargs);
1029 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1032 /* Subroutine of diagnose_mismatched_decls. Check for function type
1033 mismatch involving an empty arglist vs a nonempty one and give clearer
1034 diagnostics. */
1035 static void
1036 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1037 tree newtype, tree oldtype)
1039 tree t;
1041 if (TREE_CODE (olddecl) != FUNCTION_DECL
1042 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1043 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1045 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1046 return;
1048 t = TYPE_ARG_TYPES (oldtype);
1049 if (t == 0)
1050 t = TYPE_ARG_TYPES (newtype);
1051 for (; t; t = TREE_CHAIN (t))
1053 tree type = TREE_VALUE (t);
1055 if (TREE_CHAIN (t) == 0
1056 && TYPE_MAIN_VARIANT (type) != void_type_node)
1058 inform ("a parameter list with an ellipsis can%'t match "
1059 "an empty parameter name list declaration");
1060 break;
1063 if (c_type_promotes_to (type) != type)
1065 inform ("an argument type that has a default promotion can%'t match "
1066 "an empty parameter name list declaration");
1067 break;
1072 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1073 old-style function definition, NEWDECL is a prototype declaration.
1074 Diagnose inconsistencies in the argument list. Returns TRUE if
1075 the prototype is compatible, FALSE if not. */
1076 static bool
1077 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1079 tree newargs, oldargs;
1080 int i;
1082 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1084 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1085 newargs = TYPE_ARG_TYPES (newtype);
1086 i = 1;
1088 for (;;)
1090 tree oldargtype = TREE_VALUE (oldargs);
1091 tree newargtype = TREE_VALUE (newargs);
1093 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1094 return false;
1096 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1097 newargtype = TYPE_MAIN_VARIANT (newargtype);
1099 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1100 break;
1102 /* Reaching the end of just one list means the two decls don't
1103 agree on the number of arguments. */
1104 if (END_OF_ARGLIST (oldargtype))
1106 error ("prototype for %q+D declares more arguments "
1107 "than previous old-style definition", newdecl);
1108 return false;
1110 else if (END_OF_ARGLIST (newargtype))
1112 error ("prototype for %q+D declares fewer arguments "
1113 "than previous old-style definition", newdecl);
1114 return false;
1117 /* Type for passing arg must be consistent with that declared
1118 for the arg. */
1119 else if (!comptypes (oldargtype, newargtype))
1121 error ("prototype for %q+D declares argument %d"
1122 " with incompatible type",
1123 newdecl, i);
1124 return false;
1127 oldargs = TREE_CHAIN (oldargs);
1128 newargs = TREE_CHAIN (newargs);
1129 i++;
1132 /* If we get here, no errors were found, but do issue a warning
1133 for this poor-style construct. */
1134 warning (0, "prototype for %q+D follows non-prototype definition",
1135 newdecl);
1136 return true;
1137 #undef END_OF_ARGLIST
1140 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1141 first in a pair of mismatched declarations, using the diagnostic
1142 function DIAG. */
1143 static void
1144 locate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2))
1146 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1148 else if (DECL_INITIAL (decl))
1149 diag (G_("previous definition of %q+D was here"), decl);
1150 else if (C_DECL_IMPLICIT (decl))
1151 diag (G_("previous implicit declaration of %q+D was here"), decl);
1152 else
1153 diag (G_("previous declaration of %q+D was here"), decl);
1156 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1157 Returns true if the caller should proceed to merge the two, false
1158 if OLDDECL should simply be discarded. As a side effect, issues
1159 all necessary diagnostics for invalid or poor-style combinations.
1160 If it returns true, writes the types of NEWDECL and OLDDECL to
1161 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1162 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1164 static bool
1165 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1166 tree *newtypep, tree *oldtypep)
1168 tree newtype, oldtype;
1169 bool pedwarned = false;
1170 bool warned = false;
1171 bool retval = true;
1173 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1174 && DECL_EXTERNAL (DECL))
1176 /* If we have error_mark_node for either decl or type, just discard
1177 the previous decl - we're in an error cascade already. */
1178 if (olddecl == error_mark_node || newdecl == error_mark_node)
1179 return false;
1180 *oldtypep = oldtype = TREE_TYPE (olddecl);
1181 *newtypep = newtype = TREE_TYPE (newdecl);
1182 if (oldtype == error_mark_node || newtype == error_mark_node)
1183 return false;
1185 /* Two different categories of symbol altogether. This is an error
1186 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1187 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1189 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1190 && DECL_BUILT_IN (olddecl)
1191 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1193 error ("%q+D redeclared as different kind of symbol", newdecl);
1194 locate_old_decl (olddecl, error);
1196 else if (TREE_PUBLIC (newdecl))
1197 warning (0, "built-in function %q+D declared as non-function",
1198 newdecl);
1199 else
1200 warning (OPT_Wshadow, "declaration of %q+D shadows "
1201 "a built-in function", newdecl);
1202 return false;
1205 /* Enumerators have no linkage, so may only be declared once in a
1206 given scope. */
1207 if (TREE_CODE (olddecl) == CONST_DECL)
1209 error ("redeclaration of enumerator %q+D", newdecl);
1210 locate_old_decl (olddecl, error);
1211 return false;
1214 if (!comptypes (oldtype, newtype))
1216 if (TREE_CODE (olddecl) == FUNCTION_DECL
1217 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1219 /* Accept harmless mismatch in function types.
1220 This is for the ffs and fprintf builtins. */
1221 tree trytype = match_builtin_function_types (newtype, oldtype);
1223 if (trytype && comptypes (newtype, trytype))
1224 *oldtypep = oldtype = trytype;
1225 else
1227 /* If types don't match for a built-in, throw away the
1228 built-in. No point in calling locate_old_decl here, it
1229 won't print anything. */
1230 warning (0, "conflicting types for built-in function %q+D",
1231 newdecl);
1232 return false;
1235 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1236 && DECL_IS_BUILTIN (olddecl))
1238 /* A conflicting function declaration for a predeclared
1239 function that isn't actually built in. Objective C uses
1240 these. The new declaration silently overrides everything
1241 but the volatility (i.e. noreturn) indication. See also
1242 below. FIXME: Make Objective C use normal builtins. */
1243 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1244 return false;
1246 /* Permit void foo (...) to match int foo (...) if the latter is
1247 the definition and implicit int was used. See
1248 c-torture/compile/920625-2.c. */
1249 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1250 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1251 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1252 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1254 pedwarn ("conflicting types for %q+D", newdecl);
1255 /* Make sure we keep void as the return type. */
1256 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1257 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1258 pedwarned = true;
1260 /* Permit void foo (...) to match an earlier call to foo (...) with
1261 no declared type (thus, implicitly int). */
1262 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1263 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1264 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1265 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1267 pedwarn ("conflicting types for %q+D", newdecl);
1268 /* Make sure we keep void as the return type. */
1269 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1270 pedwarned = true;
1272 else
1274 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1275 error ("conflicting type qualifiers for %q+D", newdecl);
1276 else
1277 error ("conflicting types for %q+D", newdecl);
1278 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1279 locate_old_decl (olddecl, error);
1280 return false;
1284 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1285 but silently ignore the redeclaration if either is in a system
1286 header. (Conflicting redeclarations were handled above.) */
1287 if (TREE_CODE (newdecl) == TYPE_DECL)
1289 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1290 return true; /* Allow OLDDECL to continue in use. */
1292 error ("redefinition of typedef %q+D", newdecl);
1293 locate_old_decl (olddecl, error);
1294 return false;
1297 /* Function declarations can either be 'static' or 'extern' (no
1298 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1299 can never conflict with each other on account of linkage
1300 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1301 gnu89 mode permits two definitions if one is 'extern inline' and
1302 one is not. The non- extern-inline definition supersedes the
1303 extern-inline definition. */
1305 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1307 /* If you declare a built-in function name as static, or
1308 define the built-in with an old-style definition (so we
1309 can't validate the argument list) the built-in definition is
1310 overridden, but optionally warn this was a bad choice of name. */
1311 if (DECL_BUILT_IN (olddecl)
1312 && !C_DECL_DECLARED_BUILTIN (olddecl)
1313 && (!TREE_PUBLIC (newdecl)
1314 || (DECL_INITIAL (newdecl)
1315 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1317 warning (OPT_Wshadow, "declaration of %q+D shadows "
1318 "a built-in function", newdecl);
1319 /* Discard the old built-in function. */
1320 return false;
1323 if (DECL_INITIAL (newdecl))
1325 if (DECL_INITIAL (olddecl))
1327 /* If both decls are in the same TU and the new declaration
1328 isn't overriding an extern inline reject the new decl.
1329 In c99, no overriding is allowed in the same translation
1330 unit. */
1331 if ((!DECL_EXTERN_INLINE (olddecl)
1332 || DECL_EXTERN_INLINE (newdecl)
1333 #if WANT_C99_INLINE_SEMANTICS
1334 || (flag_isoc99
1335 && (!DECL_DECLARED_INLINE_P (olddecl)
1336 || !lookup_attribute ("gnu_inline",
1337 DECL_ATTRIBUTES (olddecl)))
1338 && (!DECL_DECLARED_INLINE_P (newdecl)
1339 || !lookup_attribute ("gnu_inline",
1340 DECL_ATTRIBUTES (newdecl))))
1341 #endif /* WANT_C99_INLINE_SEMANTICS */
1343 && same_translation_unit_p (newdecl, olddecl))
1345 error ("redefinition of %q+D", newdecl);
1346 locate_old_decl (olddecl, error);
1347 return false;
1351 /* If we have a prototype after an old-style function definition,
1352 the argument types must be checked specially. */
1353 else if (DECL_INITIAL (olddecl)
1354 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1355 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1356 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1358 locate_old_decl (olddecl, error);
1359 return false;
1361 /* A non-static declaration (even an "extern") followed by a
1362 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1363 The same is true for a static forward declaration at block
1364 scope followed by a non-static declaration/definition at file
1365 scope. Static followed by non-static at the same scope is
1366 not undefined behavior, and is the most convenient way to get
1367 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1368 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1369 we do diagnose it if -Wtraditional. */
1370 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1372 /* Two exceptions to the rule. If olddecl is an extern
1373 inline, or a predeclared function that isn't actually
1374 built in, newdecl silently overrides olddecl. The latter
1375 occur only in Objective C; see also above. (FIXME: Make
1376 Objective C use normal builtins.) */
1377 if (!DECL_IS_BUILTIN (olddecl)
1378 && !DECL_EXTERN_INLINE (olddecl))
1380 error ("static declaration of %q+D follows "
1381 "non-static declaration", newdecl);
1382 locate_old_decl (olddecl, error);
1384 return false;
1386 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1388 if (DECL_CONTEXT (olddecl))
1390 error ("non-static declaration of %q+D follows "
1391 "static declaration", newdecl);
1392 locate_old_decl (olddecl, error);
1393 return false;
1395 else if (warn_traditional)
1397 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1398 "follows static declaration", newdecl);
1399 warned = true;
1403 /* Make sure gnu_inline attribute is either not present, or
1404 present on all inline decls. */
1405 if (DECL_DECLARED_INLINE_P (olddecl)
1406 && DECL_DECLARED_INLINE_P (newdecl))
1408 bool newa = lookup_attribute ("gnu_inline",
1409 DECL_ATTRIBUTES (newdecl)) != NULL;
1410 bool olda = lookup_attribute ("gnu_inline",
1411 DECL_ATTRIBUTES (olddecl)) != NULL;
1412 if (newa != olda)
1414 error ("%<gnu_inline%> attribute present on %q+D",
1415 newa ? newdecl : olddecl);
1416 error ("%Jbut not here", newa ? olddecl : newdecl);
1420 else if (TREE_CODE (newdecl) == VAR_DECL)
1422 /* Only variables can be thread-local, and all declarations must
1423 agree on this property. */
1424 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1426 /* Nothing to check. Since OLDDECL is marked threadprivate
1427 and NEWDECL does not have a thread-local attribute, we
1428 will merge the threadprivate attribute into NEWDECL. */
1431 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1433 if (DECL_THREAD_LOCAL_P (newdecl))
1434 error ("thread-local declaration of %q+D follows "
1435 "non-thread-local declaration", newdecl);
1436 else
1437 error ("non-thread-local declaration of %q+D follows "
1438 "thread-local declaration", newdecl);
1440 locate_old_decl (olddecl, error);
1441 return false;
1444 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1445 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1447 error ("redefinition of %q+D", newdecl);
1448 locate_old_decl (olddecl, error);
1449 return false;
1452 /* Objects declared at file scope: if the first declaration had
1453 external linkage (even if it was an external reference) the
1454 second must have external linkage as well, or the behavior is
1455 undefined. If the first declaration had internal linkage, then
1456 the second must too, or else be an external reference (in which
1457 case the composite declaration still has internal linkage).
1458 As for function declarations, we warn about the static-then-
1459 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1460 if (DECL_FILE_SCOPE_P (newdecl)
1461 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1463 if (DECL_EXTERNAL (newdecl))
1465 if (!DECL_FILE_SCOPE_P (olddecl))
1467 error ("extern declaration of %q+D follows "
1468 "declaration with no linkage", newdecl);
1469 locate_old_decl (olddecl, error);
1470 return false;
1472 else if (warn_traditional)
1474 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1475 "follows static declaration", newdecl);
1476 warned = true;
1479 else
1481 if (TREE_PUBLIC (newdecl))
1482 error ("non-static declaration of %q+D follows "
1483 "static declaration", newdecl);
1484 else
1485 error ("static declaration of %q+D follows "
1486 "non-static declaration", newdecl);
1488 locate_old_decl (olddecl, error);
1489 return false;
1492 /* Two objects with the same name declared at the same block
1493 scope must both be external references (6.7p3). */
1494 else if (!DECL_FILE_SCOPE_P (newdecl))
1496 if (DECL_EXTERNAL (newdecl))
1498 /* Extern with initializer at block scope, which will
1499 already have received an error. */
1501 else if (DECL_EXTERNAL (olddecl))
1503 error ("declaration of %q+D with no linkage follows "
1504 "extern declaration", newdecl);
1505 locate_old_decl (olddecl, error);
1507 else
1509 error ("redeclaration of %q+D with no linkage", newdecl);
1510 locate_old_decl (olddecl, error);
1513 return false;
1517 /* warnings */
1518 /* All decls must agree on a visibility. */
1519 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1520 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1521 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1523 warning (0, "redeclaration of %q+D with different visibility "
1524 "(old visibility preserved)", newdecl);
1525 warned = true;
1528 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1530 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1531 if (DECL_DECLARED_INLINE_P (newdecl)
1532 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1534 warning (OPT_Wattributes, "inline declaration of %qD follows "
1535 "declaration with attribute noinline", newdecl);
1536 warned = true;
1538 else if (DECL_DECLARED_INLINE_P (olddecl)
1539 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1541 warning (OPT_Wattributes, "declaration of %q+D with attribute "
1542 "noinline follows inline declaration ", newdecl);
1543 warned = true;
1546 /* Inline declaration after use or definition.
1547 ??? Should we still warn about this now we have unit-at-a-time
1548 mode and can get it right?
1549 Definitely don't complain if the decls are in different translation
1550 units.
1551 C99 permits this, so don't warn in that case. (The function
1552 may not be inlined everywhere in function-at-a-time mode, but
1553 we still shouldn't warn.) */
1554 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1555 && same_translation_unit_p (olddecl, newdecl)
1556 && ! flag_isoc99)
1558 if (TREE_USED (olddecl))
1560 warning (0, "%q+D declared inline after being called", olddecl);
1561 warned = true;
1563 else if (DECL_INITIAL (olddecl))
1565 warning (0, "%q+D declared inline after its definition", olddecl);
1566 warned = true;
1570 else /* PARM_DECL, VAR_DECL */
1572 /* Redeclaration of a parameter is a constraint violation (this is
1573 not explicitly stated, but follows from C99 6.7p3 [no more than
1574 one declaration of the same identifier with no linkage in the
1575 same scope, except type tags] and 6.2.2p6 [parameters have no
1576 linkage]). We must check for a forward parameter declaration,
1577 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1578 an extension, the mandatory diagnostic for which is handled by
1579 mark_forward_parm_decls. */
1581 if (TREE_CODE (newdecl) == PARM_DECL
1582 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1584 error ("redefinition of parameter %q+D", newdecl);
1585 locate_old_decl (olddecl, error);
1586 return false;
1590 /* Optional warning for completely redundant decls. */
1591 if (!warned && !pedwarned
1592 && warn_redundant_decls
1593 /* Don't warn about a function declaration followed by a
1594 definition. */
1595 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1596 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1597 /* Don't warn about redundant redeclarations of builtins. */
1598 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1599 && !DECL_BUILT_IN (newdecl)
1600 && DECL_BUILT_IN (olddecl)
1601 && !C_DECL_DECLARED_BUILTIN (olddecl))
1602 /* Don't warn about an extern followed by a definition. */
1603 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1604 /* Don't warn about forward parameter decls. */
1605 && !(TREE_CODE (newdecl) == PARM_DECL
1606 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1607 /* Don't warn about a variable definition following a declaration. */
1608 && !(TREE_CODE (newdecl) == VAR_DECL
1609 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1611 warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1612 newdecl);
1613 warned = true;
1616 /* Report location of previous decl/defn in a consistent manner. */
1617 if (warned || pedwarned)
1618 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1620 #undef DECL_EXTERN_INLINE
1622 return retval;
1625 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1626 consistent with OLDDECL, but carries new information. Merge the
1627 new information into OLDDECL. This function issues no
1628 diagnostics. */
1630 static void
1631 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1633 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1634 && DECL_INITIAL (newdecl) != 0);
1635 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1636 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1637 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1638 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1639 bool extern_changed = false;
1641 /* For real parm decl following a forward decl, rechain the old decl
1642 in its new location and clear TREE_ASM_WRITTEN (it's not a
1643 forward decl anymore). */
1644 if (TREE_CODE (newdecl) == PARM_DECL
1645 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1647 struct c_binding *b, **here;
1649 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1650 if ((*here)->decl == olddecl)
1651 goto found;
1652 gcc_unreachable ();
1654 found:
1655 b = *here;
1656 *here = b->prev;
1657 b->prev = current_scope->bindings;
1658 current_scope->bindings = b;
1660 TREE_ASM_WRITTEN (olddecl) = 0;
1663 DECL_ATTRIBUTES (newdecl)
1664 = targetm.merge_decl_attributes (olddecl, newdecl);
1666 /* Merge the data types specified in the two decls. */
1667 TREE_TYPE (newdecl)
1668 = TREE_TYPE (olddecl)
1669 = composite_type (newtype, oldtype);
1671 /* Lay the type out, unless already done. */
1672 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1674 if (TREE_TYPE (newdecl) != error_mark_node)
1675 layout_type (TREE_TYPE (newdecl));
1676 if (TREE_CODE (newdecl) != FUNCTION_DECL
1677 && TREE_CODE (newdecl) != TYPE_DECL
1678 && TREE_CODE (newdecl) != CONST_DECL)
1679 layout_decl (newdecl, 0);
1681 else
1683 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1684 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1685 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1686 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1687 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1688 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1690 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1691 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1696 /* Merge the type qualifiers. */
1697 if (TREE_READONLY (newdecl))
1698 TREE_READONLY (olddecl) = 1;
1700 if (TREE_THIS_VOLATILE (newdecl))
1702 TREE_THIS_VOLATILE (olddecl) = 1;
1703 if (TREE_CODE (newdecl) == VAR_DECL)
1704 make_var_volatile (newdecl);
1707 /* Merge deprecatedness. */
1708 if (TREE_DEPRECATED (newdecl))
1709 TREE_DEPRECATED (olddecl) = 1;
1711 /* Keep source location of definition rather than declaration and of
1712 prototype rather than non-prototype unless that prototype is
1713 built-in. */
1714 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1715 || (old_is_prototype && !new_is_prototype
1716 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1717 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1719 /* Merge the initialization information. */
1720 if (DECL_INITIAL (newdecl) == 0)
1721 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1723 /* Merge the threadprivate attribute. */
1724 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1726 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1727 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1730 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1732 /* Merge the unused-warning information. */
1733 if (DECL_IN_SYSTEM_HEADER (olddecl))
1734 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1735 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1736 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1738 /* Merge the section attribute.
1739 We want to issue an error if the sections conflict but that
1740 must be done later in decl_attributes since we are called
1741 before attributes are assigned. */
1742 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1743 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1745 /* Copy the assembler name.
1746 Currently, it can only be defined in the prototype. */
1747 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1749 /* Use visibility of whichever declaration had it specified */
1750 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1752 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1753 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1756 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1758 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1759 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1760 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1761 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1762 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1763 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1764 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1765 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1766 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1767 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1770 /* Merge the storage class information. */
1771 merge_weak (newdecl, olddecl);
1773 /* For functions, static overrides non-static. */
1774 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1776 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1777 /* This is since we don't automatically
1778 copy the attributes of NEWDECL into OLDDECL. */
1779 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1780 /* If this clears `static', clear it in the identifier too. */
1781 if (!TREE_PUBLIC (olddecl))
1782 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1786 #if WANT_C99_INLINE_SEMANTICS
1787 /* In c99, 'extern' declaration before (or after) 'inline' means this
1788 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1789 is present. */
1790 if (TREE_CODE (newdecl) == FUNCTION_DECL
1791 && flag_isoc99
1792 && (DECL_DECLARED_INLINE_P (newdecl)
1793 || DECL_DECLARED_INLINE_P (olddecl))
1794 && (!DECL_DECLARED_INLINE_P (newdecl)
1795 || !DECL_DECLARED_INLINE_P (olddecl)
1796 || !DECL_EXTERNAL (olddecl))
1797 && DECL_EXTERNAL (newdecl)
1798 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl)))
1799 DECL_EXTERNAL (newdecl) = 0;
1800 #endif /* WANT_C99_INLINE_SEMANTICS */
1802 if (DECL_EXTERNAL (newdecl))
1804 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1805 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1807 /* An extern decl does not override previous storage class. */
1808 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1809 if (!DECL_EXTERNAL (newdecl))
1811 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1812 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1815 else
1817 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1818 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1821 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1823 /* If we're redefining a function previously defined as extern
1824 inline, make sure we emit debug info for the inline before we
1825 throw it away, in case it was inlined into a function that
1826 hasn't been written out yet. */
1827 if (new_is_definition && DECL_INITIAL (olddecl))
1829 if (TREE_USED (olddecl)
1830 /* In unit-at-a-time mode we never inline re-defined extern
1831 inline functions. */
1832 && !flag_unit_at_a_time
1833 && cgraph_function_possibly_inlined_p (olddecl))
1834 (*debug_hooks->outlining_inline_function) (olddecl);
1836 /* The new defn must not be inline. */
1837 DECL_INLINE (newdecl) = 0;
1838 DECL_UNINLINABLE (newdecl) = 1;
1840 else
1842 /* If either decl says `inline', this fn is inline, unless
1843 its definition was passed already. */
1844 if (DECL_DECLARED_INLINE_P (newdecl)
1845 || DECL_DECLARED_INLINE_P (olddecl))
1846 DECL_DECLARED_INLINE_P (newdecl) = 1;
1848 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1849 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1852 if (DECL_BUILT_IN (olddecl))
1854 /* If redeclaring a builtin function, it stays built in.
1855 But it gets tagged as having been declared. */
1856 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1857 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1858 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1859 if (new_is_prototype)
1860 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1861 else
1862 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1863 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1866 /* Also preserve various other info from the definition. */
1867 if (!new_is_definition)
1869 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1870 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1871 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1872 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1873 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1875 /* Set DECL_INLINE on the declaration if we've got a body
1876 from which to instantiate. */
1877 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1879 DECL_INLINE (newdecl) = 1;
1880 DECL_ABSTRACT_ORIGIN (newdecl)
1881 = DECL_ABSTRACT_ORIGIN (olddecl);
1884 else
1886 /* If a previous declaration said inline, mark the
1887 definition as inlinable. */
1888 if (DECL_DECLARED_INLINE_P (newdecl)
1889 && !DECL_UNINLINABLE (newdecl))
1890 DECL_INLINE (newdecl) = 1;
1894 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1896 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1897 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1899 unsigned olddecl_uid = DECL_UID (olddecl);
1900 tree olddecl_context = DECL_CONTEXT (olddecl);
1902 memcpy ((char *) olddecl + sizeof (struct tree_common),
1903 (char *) newdecl + sizeof (struct tree_common),
1904 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1905 switch (TREE_CODE (olddecl))
1907 case FIELD_DECL:
1908 case VAR_DECL:
1909 case PARM_DECL:
1910 case LABEL_DECL:
1911 case RESULT_DECL:
1912 case CONST_DECL:
1913 case TYPE_DECL:
1914 case FUNCTION_DECL:
1915 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1916 (char *) newdecl + sizeof (struct tree_decl_common),
1917 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1918 break;
1920 default:
1922 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1923 (char *) newdecl + sizeof (struct tree_decl_common),
1924 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1926 DECL_UID (olddecl) = olddecl_uid;
1927 DECL_CONTEXT (olddecl) = olddecl_context;
1930 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1931 so that encode_section_info has a chance to look at the new decl
1932 flags and attributes. */
1933 if (DECL_RTL_SET_P (olddecl)
1934 && (TREE_CODE (olddecl) == FUNCTION_DECL
1935 || (TREE_CODE (olddecl) == VAR_DECL
1936 && TREE_STATIC (olddecl))))
1937 make_decl_rtl (olddecl);
1939 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1940 and the definition is coming from the old version, cgraph needs
1941 to be called again. */
1942 if (extern_changed && !new_is_definition
1943 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
1944 cgraph_finalize_function (olddecl, false);
1947 /* Handle when a new declaration NEWDECL has the same name as an old
1948 one OLDDECL in the same binding contour. Prints an error message
1949 if appropriate.
1951 If safely possible, alter OLDDECL to look like NEWDECL, and return
1952 true. Otherwise, return false. */
1954 static bool
1955 duplicate_decls (tree newdecl, tree olddecl)
1957 tree newtype = NULL, oldtype = NULL;
1959 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1961 /* Avoid `unused variable' and other warnings warnings for OLDDECL. */
1962 TREE_NO_WARNING (olddecl) = 1;
1963 return false;
1966 merge_decls (newdecl, olddecl, newtype, oldtype);
1967 return true;
1971 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1972 static void
1973 warn_if_shadowing (tree new_decl)
1975 struct c_binding *b;
1977 /* Shadow warnings wanted? */
1978 if (!warn_shadow
1979 /* No shadow warnings for internally generated vars. */
1980 || DECL_IS_BUILTIN (new_decl)
1981 /* No shadow warnings for vars made for inlining. */
1982 || DECL_FROM_INLINE (new_decl))
1983 return;
1985 /* Is anything being shadowed? Invisible decls do not count. */
1986 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1987 if (b->decl && b->decl != new_decl && !b->invisible)
1989 tree old_decl = b->decl;
1991 if (old_decl == error_mark_node)
1993 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1994 "non-variable", new_decl);
1995 break;
1997 else if (TREE_CODE (old_decl) == PARM_DECL)
1998 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
1999 new_decl);
2000 else if (DECL_FILE_SCOPE_P (old_decl))
2001 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2002 "declaration", new_decl);
2003 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2004 && DECL_BUILT_IN (old_decl))
2006 warning (OPT_Wshadow, "declaration of %q+D shadows "
2007 "a built-in function", new_decl);
2008 break;
2010 else
2011 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2012 new_decl);
2014 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
2016 break;
2021 /* Subroutine of pushdecl.
2023 X is a TYPE_DECL for a typedef statement. Create a brand new
2024 ..._TYPE node (which will be just a variant of the existing
2025 ..._TYPE node with identical properties) and then install X
2026 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
2028 The whole point here is to end up with a situation where each
2029 and every ..._TYPE node the compiler creates will be uniquely
2030 associated with AT MOST one node representing a typedef name.
2031 This way, even though the compiler substitutes corresponding
2032 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2033 early on, later parts of the compiler can always do the reverse
2034 translation and get back the corresponding typedef name. For
2035 example, given:
2037 typedef struct S MY_TYPE;
2038 MY_TYPE object;
2040 Later parts of the compiler might only know that `object' was of
2041 type `struct S' if it were not for code just below. With this
2042 code however, later parts of the compiler see something like:
2044 struct S' == struct S
2045 typedef struct S' MY_TYPE;
2046 struct S' object;
2048 And they can then deduce (from the node for type struct S') that
2049 the original object declaration was:
2051 MY_TYPE object;
2053 Being able to do this is important for proper support of protoize,
2054 and also for generating precise symbolic debugging information
2055 which takes full account of the programmer's (typedef) vocabulary.
2057 Obviously, we don't want to generate a duplicate ..._TYPE node if
2058 the TYPE_DECL node that we are now processing really represents a
2059 standard built-in type.
2061 Since all standard types are effectively declared at line zero
2062 in the source file, we can easily check to see if we are working
2063 on a standard type by checking the current value of lineno. */
2065 static void
2066 clone_underlying_type (tree x)
2068 if (DECL_IS_BUILTIN (x))
2070 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2071 TYPE_NAME (TREE_TYPE (x)) = x;
2073 else if (TREE_TYPE (x) != error_mark_node
2074 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2076 tree tt = TREE_TYPE (x);
2077 DECL_ORIGINAL_TYPE (x) = tt;
2078 tt = build_variant_type_copy (tt);
2079 TYPE_NAME (tt) = x;
2080 TREE_USED (tt) = TREE_USED (x);
2081 TREE_TYPE (x) = tt;
2085 /* Record a decl-node X as belonging to the current lexical scope.
2086 Check for errors (such as an incompatible declaration for the same
2087 name already seen in the same scope).
2089 Returns either X or an old decl for the same name.
2090 If an old decl is returned, it may have been smashed
2091 to agree with what X says. */
2093 tree
2094 pushdecl (tree x)
2096 tree name = DECL_NAME (x);
2097 struct c_scope *scope = current_scope;
2098 struct c_binding *b;
2099 bool nested = false;
2101 /* Functions need the lang_decl data. */
2102 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
2103 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2105 /* Must set DECL_CONTEXT for everything not at file scope or
2106 DECL_FILE_SCOPE_P won't work. Local externs don't count
2107 unless they have initializers (which generate code). */
2108 if (current_function_decl
2109 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2110 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2111 DECL_CONTEXT (x) = current_function_decl;
2113 /* If this is of variably modified type, prevent jumping into its
2114 scope. */
2115 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2116 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2117 c_begin_vm_scope (scope->depth);
2119 /* Anonymous decls are just inserted in the scope. */
2120 if (!name)
2122 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2123 return x;
2126 /* First, see if there is another declaration with the same name in
2127 the current scope. If there is, duplicate_decls may do all the
2128 work for us. If duplicate_decls returns false, that indicates
2129 two incompatible decls in the same scope; we are to silently
2130 replace the old one (duplicate_decls has issued all appropriate
2131 diagnostics). In particular, we should not consider possible
2132 duplicates in the external scope, or shadowing. */
2133 b = I_SYMBOL_BINDING (name);
2134 if (b && B_IN_SCOPE (b, scope))
2136 struct c_binding *b_ext, *b_use;
2137 tree type = TREE_TYPE (x);
2138 tree visdecl = b->decl;
2139 tree vistype = TREE_TYPE (visdecl);
2140 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2141 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2142 b->inner_comp = false;
2143 b_use = b;
2144 b_ext = b;
2145 /* If this is an external linkage declaration, we should check
2146 for compatibility with the type in the external scope before
2147 setting the type at this scope based on the visible
2148 information only. */
2149 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2151 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2152 b_ext = b_ext->shadowed;
2153 if (b_ext)
2155 b_use = b_ext;
2156 if (b_use->type)
2157 TREE_TYPE (b_use->decl) = b_use->type;
2160 if (duplicate_decls (x, b_use->decl))
2162 if (b_use != b)
2164 /* Save the updated type in the external scope and
2165 restore the proper type for this scope. */
2166 tree thistype;
2167 if (comptypes (vistype, type))
2168 thistype = composite_type (vistype, type);
2169 else
2170 thistype = TREE_TYPE (b_use->decl);
2171 b_use->type = TREE_TYPE (b_use->decl);
2172 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2173 && DECL_BUILT_IN (b_use->decl))
2174 thistype
2175 = build_type_attribute_variant (thistype,
2176 TYPE_ATTRIBUTES
2177 (b_use->type));
2178 TREE_TYPE (b_use->decl) = thistype;
2180 return b_use->decl;
2182 else
2183 goto skip_external_and_shadow_checks;
2186 /* All declarations with external linkage, and all external
2187 references, go in the external scope, no matter what scope is
2188 current. However, the binding in that scope is ignored for
2189 purposes of normal name lookup. A separate binding structure is
2190 created in the requested scope; this governs the normal
2191 visibility of the symbol.
2193 The binding in the externals scope is used exclusively for
2194 detecting duplicate declarations of the same object, no matter
2195 what scope they are in; this is what we do here. (C99 6.2.7p2:
2196 All declarations that refer to the same object or function shall
2197 have compatible type; otherwise, the behavior is undefined.) */
2198 if (DECL_EXTERNAL (x) || scope == file_scope)
2200 tree type = TREE_TYPE (x);
2201 tree vistype = 0;
2202 tree visdecl = 0;
2203 bool type_saved = false;
2204 if (b && !B_IN_EXTERNAL_SCOPE (b)
2205 && (TREE_CODE (b->decl) == FUNCTION_DECL
2206 || TREE_CODE (b->decl) == VAR_DECL)
2207 && DECL_FILE_SCOPE_P (b->decl))
2209 visdecl = b->decl;
2210 vistype = TREE_TYPE (visdecl);
2212 if (scope != file_scope
2213 && !DECL_IN_SYSTEM_HEADER (x))
2214 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2216 while (b && !B_IN_EXTERNAL_SCOPE (b))
2218 /* If this decl might be modified, save its type. This is
2219 done here rather than when the decl is first bound
2220 because the type may change after first binding, through
2221 being completed or through attributes being added. If we
2222 encounter multiple such decls, only the first should have
2223 its type saved; the others will already have had their
2224 proper types saved and the types will not have changed as
2225 their scopes will not have been re-entered. */
2226 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2228 b->type = TREE_TYPE (b->decl);
2229 type_saved = true;
2231 if (B_IN_FILE_SCOPE (b)
2232 && TREE_CODE (b->decl) == VAR_DECL
2233 && TREE_STATIC (b->decl)
2234 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2235 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2236 && TREE_CODE (type) == ARRAY_TYPE
2237 && TYPE_DOMAIN (type)
2238 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2239 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2241 /* Array type completed in inner scope, which should be
2242 diagnosed if the completion does not have size 1 and
2243 it does not get completed in the file scope. */
2244 b->inner_comp = true;
2246 b = b->shadowed;
2249 /* If a matching external declaration has been found, set its
2250 type to the composite of all the types of that declaration.
2251 After the consistency checks, it will be reset to the
2252 composite of the visible types only. */
2253 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2254 && b->type)
2255 TREE_TYPE (b->decl) = b->type;
2257 /* The point of the same_translation_unit_p check here is,
2258 we want to detect a duplicate decl for a construct like
2259 foo() { extern bar(); } ... static bar(); but not if
2260 they are in different translation units. In any case,
2261 the static does not go in the externals scope. */
2262 if (b
2263 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2264 && duplicate_decls (x, b->decl))
2266 tree thistype;
2267 if (vistype)
2269 if (comptypes (vistype, type))
2270 thistype = composite_type (vistype, type);
2271 else
2272 thistype = TREE_TYPE (b->decl);
2274 else
2275 thistype = type;
2276 b->type = TREE_TYPE (b->decl);
2277 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2278 thistype
2279 = build_type_attribute_variant (thistype,
2280 TYPE_ATTRIBUTES (b->type));
2281 TREE_TYPE (b->decl) = thistype;
2282 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2283 return b->decl;
2285 else if (TREE_PUBLIC (x))
2287 if (visdecl && !b && duplicate_decls (x, visdecl))
2289 /* An external declaration at block scope referring to a
2290 visible entity with internal linkage. The composite
2291 type will already be correct for this scope, so we
2292 just need to fall through to make the declaration in
2293 this scope. */
2294 nested = true;
2295 x = visdecl;
2297 else
2299 bind (name, x, external_scope, /*invisible=*/true,
2300 /*nested=*/false);
2301 nested = true;
2306 if (TREE_CODE (x) != PARM_DECL)
2307 warn_if_shadowing (x);
2309 skip_external_and_shadow_checks:
2310 if (TREE_CODE (x) == TYPE_DECL)
2311 clone_underlying_type (x);
2313 bind (name, x, scope, /*invisible=*/false, nested);
2315 /* If x's type is incomplete because it's based on a
2316 structure or union which has not yet been fully declared,
2317 attach it to that structure or union type, so we can go
2318 back and complete the variable declaration later, if the
2319 structure or union gets fully declared.
2321 If the input is erroneous, we can have error_mark in the type
2322 slot (e.g. "f(void a, ...)") - that doesn't count as an
2323 incomplete type. */
2324 if (TREE_TYPE (x) != error_mark_node
2325 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2327 tree element = TREE_TYPE (x);
2329 while (TREE_CODE (element) == ARRAY_TYPE)
2330 element = TREE_TYPE (element);
2331 element = TYPE_MAIN_VARIANT (element);
2333 if ((TREE_CODE (element) == RECORD_TYPE
2334 || TREE_CODE (element) == UNION_TYPE)
2335 && (TREE_CODE (x) != TYPE_DECL
2336 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2337 && !COMPLETE_TYPE_P (element))
2338 C_TYPE_INCOMPLETE_VARS (element)
2339 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2341 return x;
2344 /* Record X as belonging to file scope.
2345 This is used only internally by the Objective-C front end,
2346 and is limited to its needs. duplicate_decls is not called;
2347 if there is any preexisting decl for this identifier, it is an ICE. */
2349 tree
2350 pushdecl_top_level (tree x)
2352 tree name;
2353 bool nested = false;
2354 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2356 name = DECL_NAME (x);
2358 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2360 if (TREE_PUBLIC (x))
2362 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2363 nested = true;
2365 if (file_scope)
2366 bind (name, x, file_scope, /*invisible=*/false, nested);
2368 return x;
2371 static void
2372 implicit_decl_warning (tree id, tree olddecl)
2374 if (warn_implicit_function_declaration)
2376 if (flag_isoc99)
2377 pedwarn (G_("implicit declaration of function %qE"), id);
2378 else
2379 warning (OPT_Wimplicit_function_declaration,
2380 G_("implicit declaration of function %qE"), id);
2381 if (olddecl)
2382 locate_old_decl (olddecl, inform);
2386 /* Generate an implicit declaration for identifier FUNCTIONID as a
2387 function of type int (). */
2389 tree
2390 implicitly_declare (tree functionid)
2392 struct c_binding *b;
2393 tree decl = 0;
2394 tree asmspec_tree;
2396 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2398 if (B_IN_SCOPE (b, external_scope))
2400 decl = b->decl;
2401 break;
2405 if (decl)
2407 if (decl == error_mark_node)
2408 return decl;
2410 /* FIXME: Objective-C has weird not-really-builtin functions
2411 which are supposed to be visible automatically. They wind up
2412 in the external scope because they're pushed before the file
2413 scope gets created. Catch this here and rebind them into the
2414 file scope. */
2415 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2417 bind (functionid, decl, file_scope,
2418 /*invisible=*/false, /*nested=*/true);
2419 return decl;
2421 else
2423 tree newtype = default_function_type;
2424 if (b->type)
2425 TREE_TYPE (decl) = b->type;
2426 /* Implicit declaration of a function already declared
2427 (somehow) in a different scope, or as a built-in.
2428 If this is the first time this has happened, warn;
2429 then recycle the old declaration but with the new type. */
2430 if (!C_DECL_IMPLICIT (decl))
2432 implicit_decl_warning (functionid, decl);
2433 C_DECL_IMPLICIT (decl) = 1;
2435 if (DECL_BUILT_IN (decl))
2437 newtype = build_type_attribute_variant (newtype,
2438 TYPE_ATTRIBUTES
2439 (TREE_TYPE (decl)));
2440 if (!comptypes (newtype, TREE_TYPE (decl)))
2442 warning (0, "incompatible implicit declaration of built-in"
2443 " function %qD", decl);
2444 newtype = TREE_TYPE (decl);
2447 else
2449 if (!comptypes (newtype, TREE_TYPE (decl)))
2451 error ("incompatible implicit declaration of function %qD",
2452 decl);
2453 locate_old_decl (decl, error);
2456 b->type = TREE_TYPE (decl);
2457 TREE_TYPE (decl) = newtype;
2458 bind (functionid, decl, current_scope,
2459 /*invisible=*/false, /*nested=*/true);
2460 return decl;
2464 /* Not seen before. */
2465 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2466 DECL_EXTERNAL (decl) = 1;
2467 TREE_PUBLIC (decl) = 1;
2468 C_DECL_IMPLICIT (decl) = 1;
2469 implicit_decl_warning (functionid, 0);
2470 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2471 if (asmspec_tree)
2472 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2474 /* C89 says implicit declarations are in the innermost block.
2475 So we record the decl in the standard fashion. */
2476 decl = pushdecl (decl);
2478 /* No need to call objc_check_decl here - it's a function type. */
2479 rest_of_decl_compilation (decl, 0, 0);
2481 /* Write a record describing this implicit function declaration
2482 to the prototypes file (if requested). */
2483 gen_aux_info_record (decl, 0, 1, 0);
2485 /* Possibly apply some default attributes to this implicit declaration. */
2486 decl_attributes (&decl, NULL_TREE, 0);
2488 return decl;
2491 /* Issue an error message for a reference to an undeclared variable
2492 ID, including a reference to a builtin outside of function-call
2493 context. Establish a binding of the identifier to error_mark_node
2494 in an appropriate scope, which will suppress further errors for the
2495 same identifier. The error message should be given location LOC. */
2496 void
2497 undeclared_variable (tree id, location_t loc)
2499 static bool already = false;
2500 struct c_scope *scope;
2502 if (current_function_decl == 0)
2504 error ("%H%qE undeclared here (not in a function)", &loc, id);
2505 scope = current_scope;
2507 else
2509 error ("%H%qE undeclared (first use in this function)", &loc, id);
2511 if (!already)
2513 error ("%H(Each undeclared identifier is reported only once", &loc);
2514 error ("%Hfor each function it appears in.)", &loc);
2515 already = true;
2518 /* If we are parsing old-style parameter decls, current_function_decl
2519 will be nonnull but current_function_scope will be null. */
2520 scope = current_function_scope ? current_function_scope : current_scope;
2522 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2525 /* Subroutine of lookup_label, declare_label, define_label: construct a
2526 LABEL_DECL with all the proper frills. */
2528 static tree
2529 make_label (tree name, location_t location)
2531 tree label = build_decl (LABEL_DECL, name, void_type_node);
2533 DECL_CONTEXT (label) = current_function_decl;
2534 DECL_MODE (label) = VOIDmode;
2535 DECL_SOURCE_LOCATION (label) = location;
2537 return label;
2540 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2541 Create one if none exists so far for the current function.
2542 This is called when a label is used in a goto expression or
2543 has its address taken. */
2545 tree
2546 lookup_label (tree name)
2548 tree label;
2550 if (current_function_decl == 0)
2552 error ("label %qE referenced outside of any function", name);
2553 return 0;
2556 /* Use a label already defined or ref'd with this name, but not if
2557 it is inherited from a containing function and wasn't declared
2558 using __label__. */
2559 label = I_LABEL_DECL (name);
2560 if (label && (DECL_CONTEXT (label) == current_function_decl
2561 || C_DECLARED_LABEL_FLAG (label)))
2563 /* If the label has only been declared, update its apparent
2564 location to point here, for better diagnostics if it
2565 turns out not to have been defined. */
2566 if (!TREE_USED (label))
2567 DECL_SOURCE_LOCATION (label) = input_location;
2568 return label;
2571 /* No label binding for that identifier; make one. */
2572 label = make_label (name, input_location);
2574 /* Ordinary labels go in the current function scope. */
2575 bind (name, label, current_function_scope,
2576 /*invisible=*/false, /*nested=*/false);
2577 return label;
2580 /* Make a label named NAME in the current function, shadowing silently
2581 any that may be inherited from containing functions or containing
2582 scopes. This is called for __label__ declarations. */
2584 tree
2585 declare_label (tree name)
2587 struct c_binding *b = I_LABEL_BINDING (name);
2588 tree label;
2590 /* Check to make sure that the label hasn't already been declared
2591 at this scope */
2592 if (b && B_IN_CURRENT_SCOPE (b))
2594 error ("duplicate label declaration %qE", name);
2595 locate_old_decl (b->decl, error);
2597 /* Just use the previous declaration. */
2598 return b->decl;
2601 label = make_label (name, input_location);
2602 C_DECLARED_LABEL_FLAG (label) = 1;
2604 /* Declared labels go in the current scope. */
2605 bind (name, label, current_scope,
2606 /*invisible=*/false, /*nested=*/false);
2607 return label;
2610 /* Define a label, specifying the location in the source file.
2611 Return the LABEL_DECL node for the label, if the definition is valid.
2612 Otherwise return 0. */
2614 tree
2615 define_label (location_t location, tree name)
2617 /* Find any preexisting label with this name. It is an error
2618 if that label has already been defined in this function, or
2619 if there is a containing function with a declared label with
2620 the same name. */
2621 tree label = I_LABEL_DECL (name);
2622 struct c_label_list *nlist_se, *nlist_vm;
2624 if (label
2625 && ((DECL_CONTEXT (label) == current_function_decl
2626 && DECL_INITIAL (label) != 0)
2627 || (DECL_CONTEXT (label) != current_function_decl
2628 && C_DECLARED_LABEL_FLAG (label))))
2630 error ("%Hduplicate label %qD", &location, label);
2631 locate_old_decl (label, error);
2632 return 0;
2634 else if (label && DECL_CONTEXT (label) == current_function_decl)
2636 /* The label has been used or declared already in this function,
2637 but not defined. Update its location to point to this
2638 definition. */
2639 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2640 error ("%Jjump into statement expression", label);
2641 if (C_DECL_UNDEFINABLE_VM (label))
2642 error ("%Jjump into scope of identifier with variably modified type",
2643 label);
2644 DECL_SOURCE_LOCATION (label) = location;
2646 else
2648 /* No label binding for that identifier; make one. */
2649 label = make_label (name, location);
2651 /* Ordinary labels go in the current function scope. */
2652 bind (name, label, current_function_scope,
2653 /*invisible=*/false, /*nested=*/false);
2656 if (!in_system_header && lookup_name (name))
2657 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2658 "for labels, identifier %qE conflicts", &location, name);
2660 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2661 nlist_se->next = label_context_stack_se->labels_def;
2662 nlist_se->label = label;
2663 label_context_stack_se->labels_def = nlist_se;
2665 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2666 nlist_vm->next = label_context_stack_vm->labels_def;
2667 nlist_vm->label = label;
2668 label_context_stack_vm->labels_def = nlist_vm;
2670 /* Mark label as having been defined. */
2671 DECL_INITIAL (label) = error_mark_node;
2672 return label;
2675 /* Given NAME, an IDENTIFIER_NODE,
2676 return the structure (or union or enum) definition for that name.
2677 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2678 CODE says which kind of type the caller wants;
2679 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2680 If the wrong kind of type is found, an error is reported. */
2682 static tree
2683 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2685 struct c_binding *b = I_TAG_BINDING (name);
2686 int thislevel = 0;
2688 if (!b || !b->decl)
2689 return 0;
2691 /* We only care about whether it's in this level if
2692 thislevel_only was set or it might be a type clash. */
2693 if (thislevel_only || TREE_CODE (b->decl) != code)
2695 /* For our purposes, a tag in the external scope is the same as
2696 a tag in the file scope. (Primarily relevant to Objective-C
2697 and its builtin structure tags, which get pushed before the
2698 file scope is created.) */
2699 if (B_IN_CURRENT_SCOPE (b)
2700 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2701 thislevel = 1;
2704 if (thislevel_only && !thislevel)
2705 return 0;
2707 if (TREE_CODE (b->decl) != code)
2709 /* Definition isn't the kind we were looking for. */
2710 pending_invalid_xref = name;
2711 pending_invalid_xref_location = input_location;
2713 /* If in the same binding level as a declaration as a tag
2714 of a different type, this must not be allowed to
2715 shadow that tag, so give the error immediately.
2716 (For example, "struct foo; union foo;" is invalid.) */
2717 if (thislevel)
2718 pending_xref_error ();
2720 return b->decl;
2723 /* Print an error message now
2724 for a recent invalid struct, union or enum cross reference.
2725 We don't print them immediately because they are not invalid
2726 when used in the `struct foo;' construct for shadowing. */
2728 void
2729 pending_xref_error (void)
2731 if (pending_invalid_xref != 0)
2732 error ("%H%qE defined as wrong kind of tag",
2733 &pending_invalid_xref_location, pending_invalid_xref);
2734 pending_invalid_xref = 0;
2738 /* Look up NAME in the current scope and its superiors
2739 in the namespace of variables, functions and typedefs.
2740 Return a ..._DECL node of some kind representing its definition,
2741 or return 0 if it is undefined. */
2743 tree
2744 lookup_name (tree name)
2746 struct c_binding *b = I_SYMBOL_BINDING (name);
2747 if (b && !b->invisible)
2748 return b->decl;
2749 return 0;
2752 /* Similar to `lookup_name' but look only at the indicated scope. */
2754 static tree
2755 lookup_name_in_scope (tree name, struct c_scope *scope)
2757 struct c_binding *b;
2759 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2760 if (B_IN_SCOPE (b, scope))
2761 return b->decl;
2762 return 0;
2765 /* Create the predefined scalar types of C,
2766 and some nodes representing standard constants (0, 1, (void *) 0).
2767 Initialize the global scope.
2768 Make definitions for built-in primitive functions. */
2770 void
2771 c_init_decl_processing (void)
2773 location_t save_loc = input_location;
2775 /* Initialize reserved words for parser. */
2776 c_parse_init ();
2778 current_function_decl = 0;
2780 gcc_obstack_init (&parser_obstack);
2782 /* Make the externals scope. */
2783 push_scope ();
2784 external_scope = current_scope;
2786 /* Declarations from c_common_nodes_and_builtins must not be associated
2787 with this input file, lest we get differences between using and not
2788 using preprocessed headers. */
2789 #ifdef USE_MAPPED_LOCATION
2790 input_location = BUILTINS_LOCATION;
2791 #else
2792 input_location.file = "<built-in>";
2793 input_location.line = 0;
2794 #endif
2796 build_common_tree_nodes (flag_signed_char, false);
2798 c_common_nodes_and_builtins ();
2800 /* In C, comparisons and TRUTH_* expressions have type int. */
2801 truthvalue_type_node = integer_type_node;
2802 truthvalue_true_node = integer_one_node;
2803 truthvalue_false_node = integer_zero_node;
2805 /* Even in C99, which has a real boolean type. */
2806 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2807 boolean_type_node));
2809 input_location = save_loc;
2811 pedantic_lvalues = true;
2813 make_fname_decl = c_make_fname_decl;
2814 start_fname_decls ();
2817 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2818 decl, NAME is the initialization string and TYPE_DEP indicates whether
2819 NAME depended on the type of the function. As we don't yet implement
2820 delayed emission of static data, we mark the decl as emitted
2821 so it is not placed in the output. Anything using it must therefore pull
2822 out the STRING_CST initializer directly. FIXME. */
2824 static tree
2825 c_make_fname_decl (tree id, int type_dep)
2827 const char *name = fname_as_string (type_dep);
2828 tree decl, type, init;
2829 size_t length = strlen (name);
2831 type = build_array_type (char_type_node,
2832 build_index_type (size_int (length)));
2833 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2835 decl = build_decl (VAR_DECL, id, type);
2837 TREE_STATIC (decl) = 1;
2838 TREE_READONLY (decl) = 1;
2839 DECL_ARTIFICIAL (decl) = 1;
2841 init = build_string (length + 1, name);
2842 free ((char *) name);
2843 TREE_TYPE (init) = type;
2844 DECL_INITIAL (decl) = init;
2846 TREE_USED (decl) = 1;
2848 if (current_function_decl
2849 /* For invalid programs like this:
2851 void foo()
2852 const char* p = __FUNCTION__;
2854 the __FUNCTION__ is believed to appear in K&R style function
2855 parameter declarator. In that case we still don't have
2856 function_scope. */
2857 && (!errorcount || current_function_scope))
2859 DECL_CONTEXT (decl) = current_function_decl;
2860 bind (id, decl, current_function_scope,
2861 /*invisible=*/false, /*nested=*/false);
2864 finish_decl (decl, init, NULL_TREE);
2866 return decl;
2869 tree
2870 c_builtin_function (tree decl)
2872 tree type = TREE_TYPE (decl);
2873 tree id = DECL_NAME (decl);
2875 const char *name = IDENTIFIER_POINTER (id);
2876 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2877 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2879 /* Should never be called on a symbol with a preexisting meaning. */
2880 gcc_assert (!I_SYMBOL_BINDING (id));
2882 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2884 /* Builtins in the implementation namespace are made visible without
2885 needing to be explicitly declared. See push_file_scope. */
2886 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2888 TREE_CHAIN (decl) = visible_builtins;
2889 visible_builtins = decl;
2892 return decl;
2895 /* Called when a declaration is seen that contains no names to declare.
2896 If its type is a reference to a structure, union or enum inherited
2897 from a containing scope, shadow that tag name for the current scope
2898 with a forward reference.
2899 If its type defines a new named structure or union
2900 or defines an enum, it is valid but we need not do anything here.
2901 Otherwise, it is an error. */
2903 void
2904 shadow_tag (const struct c_declspecs *declspecs)
2906 shadow_tag_warned (declspecs, 0);
2909 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2910 but no pedwarn. */
2911 void
2912 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2914 bool found_tag = false;
2916 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2918 tree value = declspecs->type;
2919 enum tree_code code = TREE_CODE (value);
2921 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2922 /* Used to test also that TYPE_SIZE (value) != 0.
2923 That caused warning for `struct foo;' at top level in the file. */
2925 tree name = TYPE_NAME (value);
2926 tree t;
2928 found_tag = true;
2930 if (name == 0)
2932 if (warned != 1 && code != ENUMERAL_TYPE)
2933 /* Empty unnamed enum OK */
2935 pedwarn ("unnamed struct/union that defines no instances");
2936 warned = 1;
2939 else if (!declspecs->tag_defined_p
2940 && declspecs->storage_class != csc_none)
2942 if (warned != 1)
2943 pedwarn ("empty declaration with storage class specifier "
2944 "does not redeclare tag");
2945 warned = 1;
2946 pending_xref_error ();
2948 else if (!declspecs->tag_defined_p
2949 && (declspecs->const_p
2950 || declspecs->volatile_p
2951 || declspecs->restrict_p))
2953 if (warned != 1)
2954 pedwarn ("empty declaration with type qualifier "
2955 "does not redeclare tag");
2956 warned = 1;
2957 pending_xref_error ();
2959 else
2961 pending_invalid_xref = 0;
2962 t = lookup_tag (code, name, 1);
2964 if (t == 0)
2966 t = make_node (code);
2967 pushtag (name, t);
2971 else
2973 if (warned != 1 && !in_system_header)
2975 pedwarn ("useless type name in empty declaration");
2976 warned = 1;
2980 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2982 pedwarn ("useless type name in empty declaration");
2983 warned = 1;
2986 pending_invalid_xref = 0;
2988 if (declspecs->inline_p)
2990 error ("%<inline%> in empty declaration");
2991 warned = 1;
2994 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2996 error ("%<auto%> in file-scope empty declaration");
2997 warned = 1;
3000 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3002 error ("%<register%> in file-scope empty declaration");
3003 warned = 1;
3006 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3008 warning (0, "useless storage class specifier in empty declaration");
3009 warned = 2;
3012 if (!warned && !in_system_header && declspecs->thread_p)
3014 warning (0, "useless %<__thread%> in empty declaration");
3015 warned = 2;
3018 if (!warned && !in_system_header && (declspecs->const_p
3019 || declspecs->volatile_p
3020 || declspecs->restrict_p))
3022 warning (0, "useless type qualifier in empty declaration");
3023 warned = 2;
3026 if (warned != 1)
3028 if (!found_tag)
3029 pedwarn ("empty declaration");
3034 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3035 bits. SPECS represents declaration specifiers that the grammar
3036 only permits to contain type qualifiers and attributes. */
3039 quals_from_declspecs (const struct c_declspecs *specs)
3041 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3042 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3043 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3044 gcc_assert (!specs->type
3045 && !specs->decl_attr
3046 && specs->typespec_word == cts_none
3047 && specs->storage_class == csc_none
3048 && !specs->typedef_p
3049 && !specs->explicit_signed_p
3050 && !specs->deprecated_p
3051 && !specs->long_p
3052 && !specs->long_long_p
3053 && !specs->short_p
3054 && !specs->signed_p
3055 && !specs->unsigned_p
3056 && !specs->complex_p
3057 && !specs->inline_p
3058 && !specs->thread_p);
3059 return quals;
3062 /* Construct an array declarator. EXPR is the expression inside [],
3063 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3064 applied to the pointer to which a parameter array is converted).
3065 STATIC_P is true if "static" is inside the [], false otherwise.
3066 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3067 length which is nevertheless a complete type, false otherwise. The
3068 field for the contained declarator is left to be filled in by
3069 set_array_declarator_inner. */
3071 struct c_declarator *
3072 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3073 bool vla_unspec_p)
3075 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3076 struct c_declarator);
3077 declarator->kind = cdk_array;
3078 declarator->declarator = 0;
3079 declarator->u.array.dimen = expr;
3080 if (quals)
3082 declarator->u.array.attrs = quals->attrs;
3083 declarator->u.array.quals = quals_from_declspecs (quals);
3085 else
3087 declarator->u.array.attrs = NULL_TREE;
3088 declarator->u.array.quals = 0;
3090 declarator->u.array.static_p = static_p;
3091 declarator->u.array.vla_unspec_p = vla_unspec_p;
3092 if (pedantic && !flag_isoc99)
3094 if (static_p || quals != NULL)
3095 pedwarn ("ISO C90 does not support %<static%> or type "
3096 "qualifiers in parameter array declarators");
3097 if (vla_unspec_p)
3098 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3100 if (vla_unspec_p)
3102 if (!current_scope->parm_flag)
3104 /* C99 6.7.5.2p4 */
3105 error ("%<[*]%> not allowed in other than function prototype scope");
3106 declarator->u.array.vla_unspec_p = false;
3107 return NULL;
3109 current_scope->had_vla_unspec = true;
3111 return declarator;
3114 /* Set the contained declarator of an array declarator. DECL is the
3115 declarator, as constructed by build_array_declarator; INNER is what
3116 appears on the left of the []. ABSTRACT_P is true if it is an
3117 abstract declarator, false otherwise; this is used to reject static
3118 and type qualifiers in abstract declarators, where they are not in
3119 the C99 grammar (subject to possible change in DR#289). */
3121 struct c_declarator *
3122 set_array_declarator_inner (struct c_declarator *decl,
3123 struct c_declarator *inner, bool abstract_p)
3125 decl->declarator = inner;
3126 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3127 || decl->u.array.attrs != NULL_TREE
3128 || decl->u.array.static_p))
3129 error ("static or type qualifiers in abstract declarator");
3130 return decl;
3133 /* INIT is a constructor that forms DECL's initializer. If the final
3134 element initializes a flexible array field, add the size of that
3135 initializer to DECL's size. */
3137 static void
3138 add_flexible_array_elts_to_size (tree decl, tree init)
3140 tree elt, type;
3142 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3143 return;
3145 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3146 type = TREE_TYPE (elt);
3147 if (TREE_CODE (type) == ARRAY_TYPE
3148 && TYPE_SIZE (type) == NULL_TREE
3149 && TYPE_DOMAIN (type) != NULL_TREE
3150 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3152 complete_array_type (&type, elt, false);
3153 DECL_SIZE (decl)
3154 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3155 DECL_SIZE_UNIT (decl)
3156 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3160 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3162 tree
3163 groktypename (struct c_type_name *type_name)
3165 tree type;
3166 tree attrs = type_name->specs->attrs;
3168 type_name->specs->attrs = NULL_TREE;
3170 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3171 false, NULL);
3173 /* Apply attributes. */
3174 decl_attributes (&type, attrs, 0);
3176 return type;
3179 /* Decode a declarator in an ordinary declaration or data definition.
3180 This is called as soon as the type information and variable name
3181 have been parsed, before parsing the initializer if any.
3182 Here we create the ..._DECL node, fill in its type,
3183 and put it on the list of decls for the current context.
3184 The ..._DECL node is returned as the value.
3186 Exception: for arrays where the length is not specified,
3187 the type is left null, to be filled in by `finish_decl'.
3189 Function definitions do not come here; they go to start_function
3190 instead. However, external and forward declarations of functions
3191 do go through here. Structure field declarations are done by
3192 grokfield and not through here. */
3194 tree
3195 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3196 bool initialized, tree attributes)
3198 tree decl;
3199 tree tem;
3201 /* An object declared as __attribute__((deprecated)) suppresses
3202 warnings of uses of other deprecated items. */
3203 if (lookup_attribute ("deprecated", attributes))
3204 deprecated_state = DEPRECATED_SUPPRESS;
3206 decl = grokdeclarator (declarator, declspecs,
3207 NORMAL, initialized, NULL);
3208 if (!decl)
3209 return 0;
3211 deprecated_state = DEPRECATED_NORMAL;
3213 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3214 && MAIN_NAME_P (DECL_NAME (decl)))
3215 warning (OPT_Wmain, "%q+D is usually a function", decl);
3217 if (initialized)
3218 /* Is it valid for this decl to have an initializer at all?
3219 If not, set INITIALIZED to zero, which will indirectly
3220 tell 'finish_decl' to ignore the initializer once it is parsed. */
3221 switch (TREE_CODE (decl))
3223 case TYPE_DECL:
3224 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3225 initialized = 0;
3226 break;
3228 case FUNCTION_DECL:
3229 error ("function %qD is initialized like a variable", decl);
3230 initialized = 0;
3231 break;
3233 case PARM_DECL:
3234 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3235 error ("parameter %qD is initialized", decl);
3236 initialized = 0;
3237 break;
3239 default:
3240 /* Don't allow initializations for incomplete types except for
3241 arrays which might be completed by the initialization. */
3243 /* This can happen if the array size is an undefined macro.
3244 We already gave a warning, so we don't need another one. */
3245 if (TREE_TYPE (decl) == error_mark_node)
3246 initialized = 0;
3247 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3249 /* A complete type is ok if size is fixed. */
3251 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3252 || C_DECL_VARIABLE_SIZE (decl))
3254 error ("variable-sized object may not be initialized");
3255 initialized = 0;
3258 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3260 error ("variable %qD has initializer but incomplete type", decl);
3261 initialized = 0;
3263 else if (C_DECL_VARIABLE_SIZE (decl))
3265 /* Although C99 is unclear about whether incomplete arrays
3266 of VLAs themselves count as VLAs, it does not make
3267 sense to permit them to be initialized given that
3268 ordinary VLAs may not be initialized. */
3269 error ("variable-sized object may not be initialized");
3270 initialized = 0;
3274 if (initialized)
3276 if (current_scope == file_scope)
3277 TREE_STATIC (decl) = 1;
3279 /* Tell 'pushdecl' this is an initialized decl
3280 even though we don't yet have the initializer expression.
3281 Also tell 'finish_decl' it may store the real initializer. */
3282 DECL_INITIAL (decl) = error_mark_node;
3285 /* If this is a function declaration, write a record describing it to the
3286 prototypes file (if requested). */
3288 if (TREE_CODE (decl) == FUNCTION_DECL)
3289 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3291 /* ANSI specifies that a tentative definition which is not merged with
3292 a non-tentative definition behaves exactly like a definition with an
3293 initializer equal to zero. (Section 3.7.2)
3295 -fno-common gives strict ANSI behavior, though this tends to break
3296 a large body of code that grew up without this rule.
3298 Thread-local variables are never common, since there's no entrenched
3299 body of code to break, and it allows more efficient variable references
3300 in the presence of dynamic linking. */
3302 if (TREE_CODE (decl) == VAR_DECL
3303 && !initialized
3304 && TREE_PUBLIC (decl)
3305 && !DECL_THREAD_LOCAL_P (decl)
3306 && !flag_no_common)
3307 DECL_COMMON (decl) = 1;
3309 /* Set attributes here so if duplicate decl, will have proper attributes. */
3310 decl_attributes (&decl, attributes, 0);
3312 #if WANT_C99_INLINE_SEMANTICS
3313 /* Handle gnu_inline attribute. */
3314 if (declspecs->inline_p
3315 && flag_isoc99
3316 && TREE_CODE (decl) == FUNCTION_DECL
3317 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
3319 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3321 else if (declspecs->storage_class != csc_static)
3322 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3324 #endif /* WANT_C99_INLINE_SEMANTICS */
3326 if (TREE_CODE (decl) == FUNCTION_DECL
3327 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3329 struct c_declarator *ce = declarator;
3331 if (ce->kind == cdk_pointer)
3332 ce = declarator->declarator;
3333 if (ce->kind == cdk_function)
3335 tree args = ce->u.arg_info->parms;
3336 for (; args; args = TREE_CHAIN (args))
3338 tree type = TREE_TYPE (args);
3339 if (type && INTEGRAL_TYPE_P (type)
3340 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3341 DECL_ARG_TYPE (args) = integer_type_node;
3346 if (TREE_CODE (decl) == FUNCTION_DECL
3347 && DECL_DECLARED_INLINE_P (decl)
3348 && DECL_UNINLINABLE (decl)
3349 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3350 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3351 decl);
3353 /* C99 6.7.4p3: An inline definition of a function with external
3354 linkage shall not contain a definition of a modifiable object
3355 with static storage duration... */
3356 if (TREE_CODE (decl) == VAR_DECL
3357 && current_scope != file_scope
3358 && TREE_STATIC (decl)
3359 && DECL_DECLARED_INLINE_P (current_function_decl)
3360 && DECL_EXTERNAL (current_function_decl))
3361 pedwarn ("%q+D is static but declared in inline function %qD "
3362 "which is not static", decl, current_function_decl);
3364 /* Add this decl to the current scope.
3365 TEM may equal DECL or it may be a previous decl of the same name. */
3366 tem = pushdecl (decl);
3368 if (initialized && DECL_EXTERNAL (tem))
3370 DECL_EXTERNAL (tem) = 0;
3371 TREE_STATIC (tem) = 1;
3374 return tem;
3377 /* Initialize EH if not initialized yet and exceptions are enabled. */
3379 void
3380 c_maybe_initialize_eh (void)
3382 if (!flag_exceptions || c_eh_initialized_p)
3383 return;
3385 c_eh_initialized_p = true;
3386 eh_personality_libfunc
3387 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3388 ? "__gcc_personality_sj0"
3389 : "__gcc_personality_v0");
3390 default_init_unwind_resume_libfunc ();
3391 using_eh_for_cleanups ();
3394 /* Finish processing of a declaration;
3395 install its initial value.
3396 If the length of an array type is not known before,
3397 it must be determined now, from the initial value, or it is an error. */
3399 void
3400 finish_decl (tree decl, tree init, tree asmspec_tree)
3402 tree type = TREE_TYPE (decl);
3403 int was_incomplete = (DECL_SIZE (decl) == 0);
3404 const char *asmspec = 0;
3406 /* If a name was specified, get the string. */
3407 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3408 && DECL_FILE_SCOPE_P (decl))
3409 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3410 if (asmspec_tree)
3411 asmspec = TREE_STRING_POINTER (asmspec_tree);
3413 /* If `start_decl' didn't like having an initialization, ignore it now. */
3414 if (init != 0 && DECL_INITIAL (decl) == 0)
3415 init = 0;
3417 /* Don't crash if parm is initialized. */
3418 if (TREE_CODE (decl) == PARM_DECL)
3419 init = 0;
3421 if (init)
3422 store_init_value (decl, init);
3424 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3425 || TREE_CODE (decl) == FUNCTION_DECL
3426 || TREE_CODE (decl) == FIELD_DECL))
3427 objc_check_decl (decl);
3429 /* Deduce size of array from initialization, if not already known. */
3430 if (TREE_CODE (type) == ARRAY_TYPE
3431 && TYPE_DOMAIN (type) == 0
3432 && TREE_CODE (decl) != TYPE_DECL)
3434 bool do_default
3435 = (TREE_STATIC (decl)
3436 /* Even if pedantic, an external linkage array
3437 may have incomplete type at first. */
3438 ? pedantic && !TREE_PUBLIC (decl)
3439 : !DECL_EXTERNAL (decl));
3440 int failure
3441 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3442 do_default);
3444 /* Get the completed type made by complete_array_type. */
3445 type = TREE_TYPE (decl);
3447 switch (failure)
3449 case 1:
3450 error ("initializer fails to determine size of %q+D", decl);
3451 break;
3453 case 2:
3454 if (do_default)
3455 error ("array size missing in %q+D", decl);
3456 /* If a `static' var's size isn't known,
3457 make it extern as well as static, so it does not get
3458 allocated.
3459 If it is not `static', then do not mark extern;
3460 finish_incomplete_decl will give it a default size
3461 and it will get allocated. */
3462 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3463 DECL_EXTERNAL (decl) = 1;
3464 break;
3466 case 3:
3467 error ("zero or negative size array %q+D", decl);
3468 break;
3470 case 0:
3471 /* For global variables, update the copy of the type that
3472 exists in the binding. */
3473 if (TREE_PUBLIC (decl))
3475 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3476 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3477 b_ext = b_ext->shadowed;
3478 if (b_ext)
3480 if (b_ext->type)
3481 b_ext->type = composite_type (b_ext->type, type);
3482 else
3483 b_ext->type = type;
3486 break;
3488 default:
3489 gcc_unreachable ();
3492 if (DECL_INITIAL (decl))
3493 TREE_TYPE (DECL_INITIAL (decl)) = type;
3495 layout_decl (decl, 0);
3498 if (TREE_CODE (decl) == VAR_DECL)
3500 if (init && TREE_CODE (init) == CONSTRUCTOR)
3501 add_flexible_array_elts_to_size (decl, init);
3503 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3504 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3505 layout_decl (decl, 0);
3507 if (DECL_SIZE (decl) == 0
3508 /* Don't give an error if we already gave one earlier. */
3509 && TREE_TYPE (decl) != error_mark_node
3510 && (TREE_STATIC (decl)
3511 /* A static variable with an incomplete type
3512 is an error if it is initialized.
3513 Also if it is not file scope.
3514 Otherwise, let it through, but if it is not `extern'
3515 then it may cause an error message later. */
3516 ? (DECL_INITIAL (decl) != 0
3517 || !DECL_FILE_SCOPE_P (decl))
3518 /* An automatic variable with an incomplete type
3519 is an error. */
3520 : !DECL_EXTERNAL (decl)))
3522 error ("storage size of %q+D isn%'t known", decl);
3523 TREE_TYPE (decl) = error_mark_node;
3526 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3527 && DECL_SIZE (decl) != 0)
3529 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3530 constant_expression_warning (DECL_SIZE (decl));
3531 else
3532 error ("storage size of %q+D isn%'t constant", decl);
3535 if (TREE_USED (type))
3536 TREE_USED (decl) = 1;
3539 /* If this is a function and an assembler name is specified, reset DECL_RTL
3540 so we can give it its new name. Also, update built_in_decls if it
3541 was a normal built-in. */
3542 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3544 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3545 set_builtin_user_assembler_name (decl, asmspec);
3546 set_user_assembler_name (decl, asmspec);
3549 /* If #pragma weak was used, mark the decl weak now. */
3550 maybe_apply_pragma_weak (decl);
3552 /* Output the assembler code and/or RTL code for variables and functions,
3553 unless the type is an undefined structure or union.
3554 If not, it will get done when the type is completed. */
3556 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3558 /* Determine the ELF visibility. */
3559 if (TREE_PUBLIC (decl))
3560 c_determine_visibility (decl);
3562 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3563 if (c_dialect_objc ())
3564 objc_check_decl (decl);
3566 if (asmspec)
3568 /* If this is not a static variable, issue a warning.
3569 It doesn't make any sense to give an ASMSPEC for an
3570 ordinary, non-register local variable. Historically,
3571 GCC has accepted -- but ignored -- the ASMSPEC in
3572 this case. */
3573 if (!DECL_FILE_SCOPE_P (decl)
3574 && TREE_CODE (decl) == VAR_DECL
3575 && !C_DECL_REGISTER (decl)
3576 && !TREE_STATIC (decl))
3577 warning (0, "ignoring asm-specifier for non-static local "
3578 "variable %q+D", decl);
3579 else
3580 set_user_assembler_name (decl, asmspec);
3583 if (DECL_FILE_SCOPE_P (decl))
3585 if (DECL_INITIAL (decl) == NULL_TREE
3586 || DECL_INITIAL (decl) == error_mark_node)
3587 /* Don't output anything
3588 when a tentative file-scope definition is seen.
3589 But at end of compilation, do output code for them. */
3590 DECL_DEFER_OUTPUT (decl) = 1;
3591 rest_of_decl_compilation (decl, true, 0);
3593 else
3595 /* In conjunction with an ASMSPEC, the `register'
3596 keyword indicates that we should place the variable
3597 in a particular register. */
3598 if (asmspec && C_DECL_REGISTER (decl))
3600 DECL_HARD_REGISTER (decl) = 1;
3601 /* This cannot be done for a structure with volatile
3602 fields, on which DECL_REGISTER will have been
3603 reset. */
3604 if (!DECL_REGISTER (decl))
3605 error ("cannot put object with volatile field into register");
3608 if (TREE_CODE (decl) != FUNCTION_DECL)
3610 /* If we're building a variable sized type, and we might be
3611 reachable other than via the top of the current binding
3612 level, then create a new BIND_EXPR so that we deallocate
3613 the object at the right time. */
3614 /* Note that DECL_SIZE can be null due to errors. */
3615 if (DECL_SIZE (decl)
3616 && !TREE_CONSTANT (DECL_SIZE (decl))
3617 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3619 tree bind;
3620 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3621 TREE_SIDE_EFFECTS (bind) = 1;
3622 add_stmt (bind);
3623 BIND_EXPR_BODY (bind) = push_stmt_list ();
3625 add_stmt (build_stmt (DECL_EXPR, decl));
3630 if (!DECL_FILE_SCOPE_P (decl))
3632 /* Recompute the RTL of a local array now
3633 if it used to be an incomplete type. */
3634 if (was_incomplete
3635 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3637 /* If we used it already as memory, it must stay in memory. */
3638 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3639 /* If it's still incomplete now, no init will save it. */
3640 if (DECL_SIZE (decl) == 0)
3641 DECL_INITIAL (decl) = 0;
3646 /* If this was marked 'used', be sure it will be output. */
3647 if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3648 mark_decl_referenced (decl);
3650 if (TREE_CODE (decl) == TYPE_DECL)
3652 if (!DECL_FILE_SCOPE_P (decl)
3653 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3654 add_stmt (build_stmt (DECL_EXPR, decl));
3656 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3659 /* At the end of a declaration, throw away any variable type sizes
3660 of types defined inside that declaration. There is no use
3661 computing them in the following function definition. */
3662 if (current_scope == file_scope)
3663 get_pending_sizes ();
3665 /* Install a cleanup (aka destructor) if one was given. */
3666 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3668 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3669 if (attr)
3671 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3672 tree cleanup_decl = lookup_name (cleanup_id);
3673 tree cleanup;
3675 /* Build "cleanup(&decl)" for the destructor. */
3676 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3677 cleanup = build_tree_list (NULL_TREE, cleanup);
3678 cleanup = build_function_call (cleanup_decl, cleanup);
3680 /* Don't warn about decl unused; the cleanup uses it. */
3681 TREE_USED (decl) = 1;
3682 TREE_USED (cleanup_decl) = 1;
3684 /* Initialize EH, if we've been told to do so. */
3685 c_maybe_initialize_eh ();
3687 push_cleanup (decl, cleanup, false);
3692 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3694 tree
3695 grokparm (const struct c_parm *parm)
3697 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3698 NULL);
3700 decl_attributes (&decl, parm->attrs, 0);
3702 return decl;
3705 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3706 and push that on the current scope. */
3708 void
3709 push_parm_decl (const struct c_parm *parm)
3711 tree decl;
3713 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3714 decl_attributes (&decl, parm->attrs, 0);
3716 decl = pushdecl (decl);
3718 finish_decl (decl, NULL_TREE, NULL_TREE);
3721 /* Mark all the parameter declarations to date as forward decls.
3722 Also diagnose use of this extension. */
3724 void
3725 mark_forward_parm_decls (void)
3727 struct c_binding *b;
3729 if (pedantic && !current_scope->warned_forward_parm_decls)
3731 pedwarn ("ISO C forbids forward parameter declarations");
3732 current_scope->warned_forward_parm_decls = true;
3735 for (b = current_scope->bindings; b; b = b->prev)
3736 if (TREE_CODE (b->decl) == PARM_DECL)
3737 TREE_ASM_WRITTEN (b->decl) = 1;
3740 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3741 literal, which may be an incomplete array type completed by the
3742 initializer; INIT is a CONSTRUCTOR that initializes the compound
3743 literal. */
3745 tree
3746 build_compound_literal (tree type, tree init)
3748 /* We do not use start_decl here because we have a type, not a declarator;
3749 and do not use finish_decl because the decl should be stored inside
3750 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3751 tree decl;
3752 tree complit;
3753 tree stmt;
3755 if (type == error_mark_node)
3756 return error_mark_node;
3758 decl = build_decl (VAR_DECL, NULL_TREE, type);
3759 DECL_EXTERNAL (decl) = 0;
3760 TREE_PUBLIC (decl) = 0;
3761 TREE_STATIC (decl) = (current_scope == file_scope);
3762 DECL_CONTEXT (decl) = current_function_decl;
3763 TREE_USED (decl) = 1;
3764 TREE_TYPE (decl) = type;
3765 TREE_READONLY (decl) = TYPE_READONLY (type);
3766 store_init_value (decl, init);
3768 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3770 int failure = complete_array_type (&TREE_TYPE (decl),
3771 DECL_INITIAL (decl), true);
3772 gcc_assert (!failure);
3774 type = TREE_TYPE (decl);
3775 TREE_TYPE (DECL_INITIAL (decl)) = type;
3778 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3779 return error_mark_node;
3781 stmt = build_stmt (DECL_EXPR, decl);
3782 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3783 TREE_SIDE_EFFECTS (complit) = 1;
3785 layout_decl (decl, 0);
3787 if (TREE_STATIC (decl))
3789 /* This decl needs a name for the assembler output. */
3790 set_compound_literal_name (decl);
3791 DECL_DEFER_OUTPUT (decl) = 1;
3792 DECL_COMDAT (decl) = 1;
3793 DECL_ARTIFICIAL (decl) = 1;
3794 DECL_IGNORED_P (decl) = 1;
3795 pushdecl (decl);
3796 rest_of_decl_compilation (decl, 1, 0);
3799 return complit;
3802 /* Determine whether TYPE is a structure with a flexible array member,
3803 or a union containing such a structure (possibly recursively). */
3805 static bool
3806 flexible_array_type_p (tree type)
3808 tree x;
3809 switch (TREE_CODE (type))
3811 case RECORD_TYPE:
3812 x = TYPE_FIELDS (type);
3813 if (x == NULL_TREE)
3814 return false;
3815 while (TREE_CHAIN (x) != NULL_TREE)
3816 x = TREE_CHAIN (x);
3817 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3818 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3819 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3820 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3821 return true;
3822 return false;
3823 case UNION_TYPE:
3824 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3826 if (flexible_array_type_p (TREE_TYPE (x)))
3827 return true;
3829 return false;
3830 default:
3831 return false;
3835 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3836 replacing with appropriate values if they are invalid. */
3837 static void
3838 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3840 tree type_mv;
3841 unsigned int max_width;
3842 unsigned HOST_WIDE_INT w;
3843 const char *name = orig_name ? orig_name: _("<anonymous>");
3845 /* Detect and ignore out of range field width and process valid
3846 field widths. */
3847 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3848 || TREE_CODE (*width) != INTEGER_CST)
3850 error ("bit-field %qs width not an integer constant", name);
3851 *width = integer_one_node;
3853 else
3855 constant_expression_warning (*width);
3856 if (tree_int_cst_sgn (*width) < 0)
3858 error ("negative width in bit-field %qs", name);
3859 *width = integer_one_node;
3861 else if (integer_zerop (*width) && orig_name)
3863 error ("zero width for bit-field %qs", name);
3864 *width = integer_one_node;
3868 /* Detect invalid bit-field type. */
3869 if (TREE_CODE (*type) != INTEGER_TYPE
3870 && TREE_CODE (*type) != BOOLEAN_TYPE
3871 && TREE_CODE (*type) != ENUMERAL_TYPE)
3873 error ("bit-field %qs has invalid type", name);
3874 *type = unsigned_type_node;
3877 type_mv = TYPE_MAIN_VARIANT (*type);
3878 if (pedantic
3879 && !in_system_header
3880 && type_mv != integer_type_node
3881 && type_mv != unsigned_type_node
3882 && type_mv != boolean_type_node)
3883 pedwarn ("type of bit-field %qs is a GCC extension", name);
3885 if (type_mv == boolean_type_node)
3886 max_width = CHAR_TYPE_SIZE;
3887 else
3888 max_width = TYPE_PRECISION (*type);
3890 if (0 < compare_tree_int (*width, max_width))
3892 error ("width of %qs exceeds its type", name);
3893 w = max_width;
3894 *width = build_int_cst (NULL_TREE, w);
3896 else
3897 w = tree_low_cst (*width, 1);
3899 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3901 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3902 if (!lt
3903 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3904 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3905 warning (0, "%qs is narrower than values of its type", name);
3910 /* Given declspecs and a declarator,
3911 determine the name and type of the object declared
3912 and construct a ..._DECL node for it.
3913 (In one case we can return a ..._TYPE node instead.
3914 For invalid input we sometimes return 0.)
3916 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3918 DECL_CONTEXT says which syntactic context this declaration is in:
3919 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3920 FUNCDEF for a function definition. Like NORMAL but a few different
3921 error messages in each case. Return value may be zero meaning
3922 this definition is too screwy to try to parse.
3923 PARM for a parameter declaration (either within a function prototype
3924 or before a function body). Make a PARM_DECL, or return void_type_node.
3925 TYPENAME if for a typename (in a cast or sizeof).
3926 Don't make a DECL node; just return the ..._TYPE node.
3927 FIELD for a struct or union field; make a FIELD_DECL.
3928 INITIALIZED is true if the decl has an initializer.
3929 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3930 representing the width of the bit-field.
3932 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3933 It may also be so in the PARM case, for a prototype where the
3934 argument type is specified but not the name.
3936 This function is where the complicated C meanings of `static'
3937 and `extern' are interpreted. */
3939 static tree
3940 grokdeclarator (const struct c_declarator *declarator,
3941 struct c_declspecs *declspecs,
3942 enum decl_context decl_context, bool initialized, tree *width)
3944 tree type = declspecs->type;
3945 bool threadp = declspecs->thread_p;
3946 enum c_storage_class storage_class = declspecs->storage_class;
3947 int constp;
3948 int restrictp;
3949 int volatilep;
3950 int type_quals = TYPE_UNQUALIFIED;
3951 const char *name, *orig_name;
3952 tree typedef_type = 0;
3953 bool funcdef_flag = false;
3954 bool funcdef_syntax = false;
3955 int size_varies = 0;
3956 tree decl_attr = declspecs->decl_attr;
3957 int array_ptr_quals = TYPE_UNQUALIFIED;
3958 tree array_ptr_attrs = NULL_TREE;
3959 int array_parm_static = 0;
3960 bool array_parm_vla_unspec_p = false;
3961 tree returned_attrs = NULL_TREE;
3962 bool bitfield = width != NULL;
3963 tree element_type;
3964 struct c_arg_info *arg_info = 0;
3966 if (decl_context == FUNCDEF)
3967 funcdef_flag = true, decl_context = NORMAL;
3969 /* Look inside a declarator for the name being declared
3970 and get it as a string, for an error message. */
3972 const struct c_declarator *decl = declarator;
3973 name = 0;
3975 while (decl)
3976 switch (decl->kind)
3978 case cdk_function:
3979 case cdk_array:
3980 case cdk_pointer:
3981 funcdef_syntax = (decl->kind == cdk_function);
3982 decl = decl->declarator;
3983 break;
3985 case cdk_attrs:
3986 decl = decl->declarator;
3987 break;
3989 case cdk_id:
3990 if (decl->u.id)
3991 name = IDENTIFIER_POINTER (decl->u.id);
3992 decl = 0;
3993 break;
3995 default:
3996 gcc_unreachable ();
3998 orig_name = name;
3999 if (name == 0)
4000 name = "type name";
4003 /* A function definition's declarator must have the form of
4004 a function declarator. */
4006 if (funcdef_flag && !funcdef_syntax)
4007 return 0;
4009 /* If this looks like a function definition, make it one,
4010 even if it occurs where parms are expected.
4011 Then store_parm_decls will reject it and not use it as a parm. */
4012 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4013 decl_context = PARM;
4015 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4016 warn_deprecated_use (declspecs->type);
4018 if ((decl_context == NORMAL || decl_context == FIELD)
4019 && current_scope == file_scope
4020 && variably_modified_type_p (type, NULL_TREE))
4022 error ("variably modified %qs at file scope", name);
4023 type = integer_type_node;
4026 typedef_type = type;
4027 size_varies = C_TYPE_VARIABLE_SIZE (type);
4029 /* Diagnose defaulting to "int". */
4031 if (declspecs->default_int_p && !in_system_header)
4033 /* Issue a warning if this is an ISO C 99 program or if
4034 -Wreturn-type and this is a function, or if -Wimplicit;
4035 prefer the former warning since it is more explicit. */
4036 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4037 && funcdef_flag)
4038 warn_about_return_type = 1;
4039 else if (warn_implicit_int || flag_isoc99)
4040 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
4043 /* Adjust the type if a bit-field is being declared,
4044 -funsigned-bitfields applied and the type is not explicitly
4045 "signed". */
4046 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4047 && TREE_CODE (type) == INTEGER_TYPE)
4048 type = c_common_unsigned_type (type);
4050 /* Figure out the type qualifiers for the declaration. There are
4051 two ways a declaration can become qualified. One is something
4052 like `const int i' where the `const' is explicit. Another is
4053 something like `typedef const int CI; CI i' where the type of the
4054 declaration contains the `const'. A third possibility is that
4055 there is a type qualifier on the element type of a typedefed
4056 array type, in which case we should extract that qualifier so
4057 that c_apply_type_quals_to_decls receives the full list of
4058 qualifiers to work with (C90 is not entirely clear about whether
4059 duplicate qualifiers should be diagnosed in this case, but it
4060 seems most appropriate to do so). */
4061 element_type = strip_array_types (type);
4062 constp = declspecs->const_p + TYPE_READONLY (element_type);
4063 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4064 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4065 if (pedantic && !flag_isoc99)
4067 if (constp > 1)
4068 pedwarn ("duplicate %<const%>");
4069 if (restrictp > 1)
4070 pedwarn ("duplicate %<restrict%>");
4071 if (volatilep > 1)
4072 pedwarn ("duplicate %<volatile%>");
4074 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4075 type = TYPE_MAIN_VARIANT (type);
4076 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4077 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4078 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4080 /* Warn about storage classes that are invalid for certain
4081 kinds of declarations (parameters, typenames, etc.). */
4083 if (funcdef_flag
4084 && (threadp
4085 || storage_class == csc_auto
4086 || storage_class == csc_register
4087 || storage_class == csc_typedef))
4089 if (storage_class == csc_auto
4090 && (pedantic || current_scope == file_scope))
4091 pedwarn ("function definition declared %<auto%>");
4092 if (storage_class == csc_register)
4093 error ("function definition declared %<register%>");
4094 if (storage_class == csc_typedef)
4095 error ("function definition declared %<typedef%>");
4096 if (threadp)
4097 error ("function definition declared %<__thread%>");
4098 threadp = false;
4099 if (storage_class == csc_auto
4100 || storage_class == csc_register
4101 || storage_class == csc_typedef)
4102 storage_class = csc_none;
4104 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4106 if (decl_context == PARM && storage_class == csc_register)
4108 else
4110 switch (decl_context)
4112 case FIELD:
4113 error ("storage class specified for structure field %qs",
4114 name);
4115 break;
4116 case PARM:
4117 error ("storage class specified for parameter %qs", name);
4118 break;
4119 default:
4120 error ("storage class specified for typename");
4121 break;
4123 storage_class = csc_none;
4124 threadp = false;
4127 else if (storage_class == csc_extern
4128 && initialized
4129 && !funcdef_flag)
4131 /* 'extern' with initialization is invalid if not at file scope. */
4132 if (current_scope == file_scope)
4134 /* It is fine to have 'extern const' when compiling at C
4135 and C++ intersection. */
4136 if (!(warn_cxx_compat && constp))
4137 warning (0, "%qs initialized and declared %<extern%>", name);
4139 else
4140 error ("%qs has both %<extern%> and initializer", name);
4142 else if (current_scope == file_scope)
4144 if (storage_class == csc_auto)
4145 error ("file-scope declaration of %qs specifies %<auto%>", name);
4146 if (pedantic && storage_class == csc_register)
4147 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4149 else
4151 if (storage_class == csc_extern && funcdef_flag)
4152 error ("nested function %qs declared %<extern%>", name);
4153 else if (threadp && storage_class == csc_none)
4155 error ("function-scope %qs implicitly auto and declared "
4156 "%<__thread%>",
4157 name);
4158 threadp = false;
4162 /* Now figure out the structure of the declarator proper.
4163 Descend through it, creating more complex types, until we reach
4164 the declared identifier (or NULL_TREE, in an absolute declarator).
4165 At each stage we maintain an unqualified version of the type
4166 together with any qualifiers that should be applied to it with
4167 c_build_qualified_type; this way, array types including
4168 multidimensional array types are first built up in unqualified
4169 form and then the qualified form is created with
4170 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4172 while (declarator && declarator->kind != cdk_id)
4174 if (type == error_mark_node)
4176 declarator = declarator->declarator;
4177 continue;
4180 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4181 a cdk_pointer (for *...),
4182 a cdk_function (for ...(...)),
4183 a cdk_attrs (for nested attributes),
4184 or a cdk_id (for the name being declared
4185 or the place in an absolute declarator
4186 where the name was omitted).
4187 For the last case, we have just exited the loop.
4189 At this point, TYPE is the type of elements of an array,
4190 or for a function to return, or for a pointer to point to.
4191 After this sequence of ifs, TYPE is the type of the
4192 array or function or pointer, and DECLARATOR has had its
4193 outermost layer removed. */
4195 if (array_ptr_quals != TYPE_UNQUALIFIED
4196 || array_ptr_attrs != NULL_TREE
4197 || array_parm_static)
4199 /* Only the innermost declarator (making a parameter be of
4200 array type which is converted to pointer type)
4201 may have static or type qualifiers. */
4202 error ("static or type qualifiers in non-parameter array declarator");
4203 array_ptr_quals = TYPE_UNQUALIFIED;
4204 array_ptr_attrs = NULL_TREE;
4205 array_parm_static = 0;
4208 switch (declarator->kind)
4210 case cdk_attrs:
4212 /* A declarator with embedded attributes. */
4213 tree attrs = declarator->u.attrs;
4214 const struct c_declarator *inner_decl;
4215 int attr_flags = 0;
4216 declarator = declarator->declarator;
4217 inner_decl = declarator;
4218 while (inner_decl->kind == cdk_attrs)
4219 inner_decl = inner_decl->declarator;
4220 if (inner_decl->kind == cdk_id)
4221 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4222 else if (inner_decl->kind == cdk_function)
4223 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4224 else if (inner_decl->kind == cdk_array)
4225 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4226 returned_attrs = decl_attributes (&type,
4227 chainon (returned_attrs, attrs),
4228 attr_flags);
4229 break;
4231 case cdk_array:
4233 tree itype = NULL_TREE;
4234 tree size = declarator->u.array.dimen;
4235 /* The index is a signed object `sizetype' bits wide. */
4236 tree index_type = c_common_signed_type (sizetype);
4238 array_ptr_quals = declarator->u.array.quals;
4239 array_ptr_attrs = declarator->u.array.attrs;
4240 array_parm_static = declarator->u.array.static_p;
4241 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4243 declarator = declarator->declarator;
4245 /* Check for some types that there cannot be arrays of. */
4247 if (VOID_TYPE_P (type))
4249 error ("declaration of %qs as array of voids", name);
4250 type = error_mark_node;
4253 if (TREE_CODE (type) == FUNCTION_TYPE)
4255 error ("declaration of %qs as array of functions", name);
4256 type = error_mark_node;
4259 if (pedantic && !in_system_header && flexible_array_type_p (type))
4260 pedwarn ("invalid use of structure with flexible array member");
4262 if (size == error_mark_node)
4263 type = error_mark_node;
4265 if (type == error_mark_node)
4266 continue;
4268 /* If size was specified, set ITYPE to a range-type for
4269 that size. Otherwise, ITYPE remains null. finish_decl
4270 may figure it out from an initial value. */
4272 if (size)
4274 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4275 lvalue. */
4276 STRIP_TYPE_NOPS (size);
4278 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4280 error ("size of array %qs has non-integer type", name);
4281 size = integer_one_node;
4284 if (pedantic && integer_zerop (size))
4285 pedwarn ("ISO C forbids zero-size array %qs", name);
4287 if (TREE_CODE (size) == INTEGER_CST)
4289 constant_expression_warning (size);
4290 if (tree_int_cst_sgn (size) < 0)
4292 error ("size of array %qs is negative", name);
4293 size = integer_one_node;
4296 else if ((decl_context == NORMAL || decl_context == FIELD)
4297 && current_scope == file_scope)
4299 error ("variably modified %qs at file scope", name);
4300 size = integer_one_node;
4302 else
4304 /* Make sure the array size remains visibly
4305 nonconstant even if it is (eg) a const variable
4306 with known value. */
4307 size_varies = 1;
4309 if (!flag_isoc99 && pedantic)
4311 if (TREE_CONSTANT (size))
4312 pedwarn ("ISO C90 forbids array %qs whose size "
4313 "can%'t be evaluated",
4314 name);
4315 else
4316 pedwarn ("ISO C90 forbids variable-size array %qs",
4317 name);
4321 if (integer_zerop (size))
4323 /* A zero-length array cannot be represented with
4324 an unsigned index type, which is what we'll
4325 get with build_index_type. Create an
4326 open-ended range instead. */
4327 itype = build_range_type (sizetype, size, NULL_TREE);
4329 else
4331 /* Arrange for the SAVE_EXPR on the inside of the
4332 MINUS_EXPR, which allows the -1 to get folded
4333 with the +1 that happens when building TYPE_SIZE. */
4334 if (size_varies)
4335 size = variable_size (size);
4337 /* Compute the maximum valid index, that is, size
4338 - 1. Do the calculation in index_type, so that
4339 if it is a variable the computations will be
4340 done in the proper mode. */
4341 itype = fold_build2 (MINUS_EXPR, index_type,
4342 convert (index_type, size),
4343 convert (index_type,
4344 size_one_node));
4346 /* If that overflowed, the array is too big. ???
4347 While a size of INT_MAX+1 technically shouldn't
4348 cause an overflow (because we subtract 1), the
4349 overflow is recorded during the conversion to
4350 index_type, before the subtraction. Handling
4351 this case seems like an unnecessary
4352 complication. */
4353 if (TREE_CODE (itype) == INTEGER_CST
4354 && TREE_OVERFLOW (itype))
4356 error ("size of array %qs is too large", name);
4357 type = error_mark_node;
4358 continue;
4361 itype = build_index_type (itype);
4364 else if (decl_context == FIELD)
4366 if (pedantic && !flag_isoc99 && !in_system_header)
4367 pedwarn ("ISO C90 does not support flexible array members");
4369 /* ISO C99 Flexible array members are effectively
4370 identical to GCC's zero-length array extension. */
4371 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4373 else if (decl_context == PARM)
4375 if (array_parm_vla_unspec_p)
4377 if (! orig_name)
4379 /* C99 6.7.5.2p4 */
4380 error ("%<[*]%> not allowed in other than a declaration");
4383 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4384 size_varies = 1;
4387 else if (decl_context == TYPENAME)
4389 if (array_parm_vla_unspec_p)
4391 /* The error is printed elsewhere. We use this to
4392 avoid messing up with incomplete array types of
4393 the same type, that would otherwise be modified
4394 below. */
4395 itype = build_range_type (sizetype, size_zero_node,
4396 NULL_TREE);
4400 /* Complain about arrays of incomplete types. */
4401 if (!COMPLETE_TYPE_P (type))
4403 error ("array type has incomplete element type");
4404 type = error_mark_node;
4406 else
4407 /* When itype is NULL, a shared incomplete array type is
4408 returned for all array of a given type. Elsewhere we
4409 make sure we don't complete that type before copying
4410 it, but here we want to make sure we don't ever
4411 modify the shared type, so we gcc_assert (itype)
4412 below. */
4413 type = build_array_type (type, itype);
4415 if (type != error_mark_node)
4417 if (size_varies)
4419 /* It is ok to modify type here even if itype is
4420 NULL: if size_varies, we're in a
4421 multi-dimensional array and the inner type has
4422 variable size, so the enclosing shared array type
4423 must too. */
4424 if (size && TREE_CODE (size) == INTEGER_CST)
4425 type
4426 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4427 C_TYPE_VARIABLE_SIZE (type) = 1;
4430 /* The GCC extension for zero-length arrays differs from
4431 ISO flexible array members in that sizeof yields
4432 zero. */
4433 if (size && integer_zerop (size))
4435 gcc_assert (itype);
4436 TYPE_SIZE (type) = bitsize_zero_node;
4437 TYPE_SIZE_UNIT (type) = size_zero_node;
4439 if (array_parm_vla_unspec_p)
4441 gcc_assert (itype);
4442 /* The type is complete. C99 6.7.5.2p4 */
4443 TYPE_SIZE (type) = bitsize_zero_node;
4444 TYPE_SIZE_UNIT (type) = size_zero_node;
4448 if (decl_context != PARM
4449 && (array_ptr_quals != TYPE_UNQUALIFIED
4450 || array_ptr_attrs != NULL_TREE
4451 || array_parm_static))
4453 error ("static or type qualifiers in non-parameter array declarator");
4454 array_ptr_quals = TYPE_UNQUALIFIED;
4455 array_ptr_attrs = NULL_TREE;
4456 array_parm_static = 0;
4458 break;
4460 case cdk_function:
4462 /* Say it's a definition only for the declarator closest
4463 to the identifier, apart possibly from some
4464 attributes. */
4465 bool really_funcdef = false;
4466 tree arg_types;
4467 if (funcdef_flag)
4469 const struct c_declarator *t = declarator->declarator;
4470 while (t->kind == cdk_attrs)
4471 t = t->declarator;
4472 really_funcdef = (t->kind == cdk_id);
4475 /* Declaring a function type. Make sure we have a valid
4476 type for the function to return. */
4477 if (type == error_mark_node)
4478 continue;
4480 size_varies = 0;
4482 /* Warn about some types functions can't return. */
4483 if (TREE_CODE (type) == FUNCTION_TYPE)
4485 error ("%qs declared as function returning a function", name);
4486 type = integer_type_node;
4488 if (TREE_CODE (type) == ARRAY_TYPE)
4490 error ("%qs declared as function returning an array", name);
4491 type = integer_type_node;
4494 /* Construct the function type and go to the next
4495 inner layer of declarator. */
4496 arg_info = declarator->u.arg_info;
4497 arg_types = grokparms (arg_info, really_funcdef);
4498 if (really_funcdef)
4499 put_pending_sizes (arg_info->pending_sizes);
4501 /* Type qualifiers before the return type of the function
4502 qualify the return type, not the function type. */
4503 if (type_quals)
4505 /* Type qualifiers on a function return type are
4506 normally permitted by the standard but have no
4507 effect, so give a warning at -Wreturn-type.
4508 Qualifiers on a void return type are banned on
4509 function definitions in ISO C; GCC used to used
4510 them for noreturn functions. */
4511 if (VOID_TYPE_P (type) && really_funcdef)
4512 pedwarn ("function definition has qualified void return type");
4513 else
4514 warning (OPT_Wreturn_type,
4515 "type qualifiers ignored on function return type");
4517 type = c_build_qualified_type (type, type_quals);
4519 type_quals = TYPE_UNQUALIFIED;
4521 type = build_function_type (type, arg_types);
4522 declarator = declarator->declarator;
4524 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4525 the formal parameter list of this FUNCTION_TYPE to point to
4526 the FUNCTION_TYPE node itself. */
4528 tree link;
4530 for (link = arg_info->tags;
4531 link;
4532 link = TREE_CHAIN (link))
4533 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4535 break;
4537 case cdk_pointer:
4539 /* Merge any constancy or volatility into the target type
4540 for the pointer. */
4542 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4543 && type_quals)
4544 pedwarn ("ISO C forbids qualified function types");
4545 if (type_quals)
4546 type = c_build_qualified_type (type, type_quals);
4547 size_varies = 0;
4549 /* When the pointed-to type involves components of variable size,
4550 care must be taken to ensure that the size evaluation code is
4551 emitted early enough to dominate all the possible later uses
4552 and late enough for the variables on which it depends to have
4553 been assigned.
4555 This is expected to happen automatically when the pointed-to
4556 type has a name/declaration of it's own, but special attention
4557 is required if the type is anonymous.
4559 We handle the NORMAL and FIELD contexts here by attaching an
4560 artificial TYPE_DECL to such pointed-to type. This forces the
4561 sizes evaluation at a safe point and ensures it is not deferred
4562 until e.g. within a deeper conditional context.
4564 We expect nothing to be needed here for PARM or TYPENAME.
4565 Pushing a TYPE_DECL at this point for TYPENAME would actually
4566 be incorrect, as we might be in the middle of an expression
4567 with side effects on the pointed-to type size "arguments" prior
4568 to the pointer declaration point and the fake TYPE_DECL in the
4569 enclosing context would force the size evaluation prior to the
4570 side effects. */
4572 if (!TYPE_NAME (type)
4573 && (decl_context == NORMAL || decl_context == FIELD)
4574 && variably_modified_type_p (type, NULL_TREE))
4576 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4577 DECL_ARTIFICIAL (decl) = 1;
4578 pushdecl (decl);
4579 finish_decl (decl, NULL_TREE, NULL_TREE);
4580 TYPE_NAME (type) = decl;
4583 type = build_pointer_type (type);
4585 /* Process type qualifiers (such as const or volatile)
4586 that were given inside the `*'. */
4587 type_quals = declarator->u.pointer_quals;
4589 declarator = declarator->declarator;
4590 break;
4592 default:
4593 gcc_unreachable ();
4597 /* Now TYPE has the actual type, apart from any qualifiers in
4598 TYPE_QUALS. */
4600 /* Check the type and width of a bit-field. */
4601 if (bitfield)
4602 check_bitfield_type_and_width (&type, width, orig_name);
4604 /* Did array size calculations overflow? */
4606 if (TREE_CODE (type) == ARRAY_TYPE
4607 && COMPLETE_TYPE_P (type)
4608 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4609 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4611 error ("size of array %qs is too large", name);
4612 /* If we proceed with the array type as it is, we'll eventually
4613 crash in tree_low_cst(). */
4614 type = error_mark_node;
4617 /* If this is declaring a typedef name, return a TYPE_DECL. */
4619 if (storage_class == csc_typedef)
4621 tree decl;
4622 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4623 && type_quals)
4624 pedwarn ("ISO C forbids qualified function types");
4625 if (type_quals)
4626 type = c_build_qualified_type (type, type_quals);
4627 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4628 if (declspecs->explicit_signed_p)
4629 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4630 decl_attributes (&decl, returned_attrs, 0);
4631 if (declspecs->inline_p)
4632 pedwarn ("typedef %q+D declared %<inline%>", decl);
4633 return decl;
4636 /* If this is a type name (such as, in a cast or sizeof),
4637 compute the type and return it now. */
4639 if (decl_context == TYPENAME)
4641 /* Note that the grammar rejects storage classes in typenames
4642 and fields. */
4643 gcc_assert (storage_class == csc_none && !threadp
4644 && !declspecs->inline_p);
4645 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4646 && type_quals)
4647 pedwarn ("ISO C forbids const or volatile function types");
4648 if (type_quals)
4649 type = c_build_qualified_type (type, type_quals);
4650 decl_attributes (&type, returned_attrs, 0);
4651 return type;
4654 if (pedantic && decl_context == FIELD
4655 && variably_modified_type_p (type, NULL_TREE))
4657 /* C99 6.7.2.1p8 */
4658 pedwarn ("a member of a structure or union cannot have a variably modified type");
4661 /* Aside from typedefs and type names (handle above),
4662 `void' at top level (not within pointer)
4663 is allowed only in public variables.
4664 We don't complain about parms either, but that is because
4665 a better error message can be made later. */
4667 if (VOID_TYPE_P (type) && decl_context != PARM
4668 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4669 && (storage_class == csc_extern
4670 || (current_scope == file_scope
4671 && !(storage_class == csc_static
4672 || storage_class == csc_register)))))
4674 error ("variable or field %qs declared void", name);
4675 type = integer_type_node;
4678 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4679 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4682 tree decl;
4684 if (decl_context == PARM)
4686 tree type_as_written;
4687 tree promoted_type;
4689 /* A parameter declared as an array of T is really a pointer to T.
4690 One declared as a function is really a pointer to a function. */
4692 if (TREE_CODE (type) == ARRAY_TYPE)
4694 /* Transfer const-ness of array into that of type pointed to. */
4695 type = TREE_TYPE (type);
4696 if (type_quals)
4697 type = c_build_qualified_type (type, type_quals);
4698 type = build_pointer_type (type);
4699 type_quals = array_ptr_quals;
4701 /* We don't yet implement attributes in this context. */
4702 if (array_ptr_attrs != NULL_TREE)
4703 warning (OPT_Wattributes,
4704 "attributes in parameter array declarator ignored");
4706 size_varies = 0;
4708 else if (TREE_CODE (type) == FUNCTION_TYPE)
4710 if (pedantic && type_quals)
4711 pedwarn ("ISO C forbids qualified function types");
4712 if (type_quals)
4713 type = c_build_qualified_type (type, type_quals);
4714 type = build_pointer_type (type);
4715 type_quals = TYPE_UNQUALIFIED;
4717 else if (type_quals)
4718 type = c_build_qualified_type (type, type_quals);
4720 type_as_written = type;
4722 decl = build_decl (PARM_DECL, declarator->u.id, type);
4723 if (size_varies)
4724 C_DECL_VARIABLE_SIZE (decl) = 1;
4726 /* Compute the type actually passed in the parmlist,
4727 for the case where there is no prototype.
4728 (For example, shorts and chars are passed as ints.)
4729 When there is a prototype, this is overridden later. */
4731 if (type == error_mark_node)
4732 promoted_type = type;
4733 else
4734 promoted_type = c_type_promotes_to (type);
4736 DECL_ARG_TYPE (decl) = promoted_type;
4737 if (declspecs->inline_p)
4738 pedwarn ("parameter %q+D declared %<inline%>", decl);
4740 else if (decl_context == FIELD)
4742 /* Note that the grammar rejects storage classes in typenames
4743 and fields. */
4744 gcc_assert (storage_class == csc_none && !threadp
4745 && !declspecs->inline_p);
4747 /* Structure field. It may not be a function. */
4749 if (TREE_CODE (type) == FUNCTION_TYPE)
4751 error ("field %qs declared as a function", name);
4752 type = build_pointer_type (type);
4754 else if (TREE_CODE (type) != ERROR_MARK
4755 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4757 error ("field %qs has incomplete type", name);
4758 type = error_mark_node;
4760 type = c_build_qualified_type (type, type_quals);
4761 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4762 DECL_NONADDRESSABLE_P (decl) = bitfield;
4764 if (size_varies)
4765 C_DECL_VARIABLE_SIZE (decl) = 1;
4767 else if (TREE_CODE (type) == FUNCTION_TYPE)
4769 if (storage_class == csc_register || threadp)
4771 error ("invalid storage class for function %qs", name);
4773 else if (current_scope != file_scope)
4775 /* Function declaration not at file scope. Storage
4776 classes other than `extern' are not allowed, C99
4777 6.7.1p5, and `extern' makes no difference. However,
4778 GCC allows 'auto', perhaps with 'inline', to support
4779 nested functions. */
4780 if (storage_class == csc_auto)
4782 if (pedantic)
4783 pedwarn ("invalid storage class for function %qs", name);
4785 else if (storage_class == csc_static)
4787 error ("invalid storage class for function %qs", name);
4788 if (funcdef_flag)
4789 storage_class = declspecs->storage_class = csc_none;
4790 else
4791 return 0;
4795 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4796 decl = build_decl_attribute_variant (decl, decl_attr);
4798 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4800 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4801 pedwarn ("ISO C forbids qualified function types");
4803 /* GNU C interprets a volatile-qualified function type to indicate
4804 that the function does not return. */
4805 if ((type_quals & TYPE_QUAL_VOLATILE)
4806 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4807 warning (0, "%<noreturn%> function returns non-void value");
4809 /* Every function declaration is an external reference
4810 (DECL_EXTERNAL) except for those which are not at file
4811 scope and are explicitly declared "auto". This is
4812 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4813 GCC to signify a forward declaration of a nested function. */
4814 if (storage_class == csc_auto && current_scope != file_scope)
4815 DECL_EXTERNAL (decl) = 0;
4816 /* In C99, a function which is declared 'inline' with 'extern'
4817 is not an external reference (which is confusing). It
4818 means that the later definition of the function must be output
4819 in this file, C99 6.7.4p6. In GNU C89, a function declared
4820 'extern inline' is an external reference. */
4821 else if (declspecs->inline_p && storage_class != csc_static)
4822 #if WANT_C99_INLINE_SEMANTICS
4823 DECL_EXTERNAL (decl) = (storage_class == csc_extern) == !flag_isoc99;
4824 #else
4825 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4826 #endif
4827 else
4828 DECL_EXTERNAL (decl) = !initialized;
4830 /* Record absence of global scope for `static' or `auto'. */
4831 TREE_PUBLIC (decl)
4832 = !(storage_class == csc_static || storage_class == csc_auto);
4834 /* For a function definition, record the argument information
4835 block where store_parm_decls will look for it. */
4836 if (funcdef_flag)
4837 current_function_arg_info = arg_info;
4839 if (declspecs->default_int_p)
4840 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4842 /* Record presence of `inline', if it is reasonable. */
4843 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4845 if (declspecs->inline_p)
4846 pedwarn ("cannot inline function %<main%>");
4848 else if (declspecs->inline_p)
4850 /* Record that the function is declared `inline'. */
4851 DECL_DECLARED_INLINE_P (decl) = 1;
4853 /* Do not mark bare declarations as DECL_INLINE. Doing so
4854 in the presence of multiple declarations can result in
4855 the abstract origin pointing between the declarations,
4856 which will confuse dwarf2out. */
4857 if (initialized)
4858 DECL_INLINE (decl) = 1;
4860 /* If -finline-functions, assume it can be inlined. This does
4861 two things: let the function be deferred until it is actually
4862 needed, and let dwarf2 know that the function is inlinable. */
4863 else if (flag_inline_trees == 2 && initialized)
4864 DECL_INLINE (decl) = 1;
4866 else
4868 /* It's a variable. */
4869 /* An uninitialized decl with `extern' is a reference. */
4870 int extern_ref = !initialized && storage_class == csc_extern;
4872 type = c_build_qualified_type (type, type_quals);
4874 /* C99 6.2.2p7: It is invalid (compile-time undefined
4875 behavior) to create an 'extern' declaration for a
4876 variable if there is a global declaration that is
4877 'static' and the global declaration is not visible.
4878 (If the static declaration _is_ currently visible,
4879 the 'extern' declaration is taken to refer to that decl.) */
4880 if (extern_ref && current_scope != file_scope)
4882 tree global_decl = identifier_global_value (declarator->u.id);
4883 tree visible_decl = lookup_name (declarator->u.id);
4885 if (global_decl
4886 && global_decl != visible_decl
4887 && TREE_CODE (global_decl) == VAR_DECL
4888 && !TREE_PUBLIC (global_decl))
4889 error ("variable previously declared %<static%> redeclared "
4890 "%<extern%>");
4893 decl = build_decl (VAR_DECL, declarator->u.id, type);
4894 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4895 if (size_varies)
4896 C_DECL_VARIABLE_SIZE (decl) = 1;
4898 if (declspecs->inline_p)
4899 pedwarn ("variable %q+D declared %<inline%>", decl);
4901 /* At file scope, an initialized extern declaration may follow
4902 a static declaration. In that case, DECL_EXTERNAL will be
4903 reset later in start_decl. */
4904 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4906 /* At file scope, the presence of a `static' or `register' storage
4907 class specifier, or the absence of all storage class specifiers
4908 makes this declaration a definition (perhaps tentative). Also,
4909 the absence of `static' makes it public. */
4910 if (current_scope == file_scope)
4912 TREE_PUBLIC (decl) = storage_class != csc_static;
4913 TREE_STATIC (decl) = !extern_ref;
4915 /* Not at file scope, only `static' makes a static definition. */
4916 else
4918 TREE_STATIC (decl) = (storage_class == csc_static);
4919 TREE_PUBLIC (decl) = extern_ref;
4922 if (threadp)
4923 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
4926 if (storage_class == csc_extern
4927 && variably_modified_type_p (type, NULL_TREE))
4929 /* C99 6.7.5.2p2 */
4930 error ("object with variably modified type must have no linkage");
4933 /* Record `register' declaration for warnings on &
4934 and in case doing stupid register allocation. */
4936 if (storage_class == csc_register)
4938 C_DECL_REGISTER (decl) = 1;
4939 DECL_REGISTER (decl) = 1;
4942 /* Record constancy and volatility. */
4943 c_apply_type_quals_to_decl (type_quals, decl);
4945 /* If a type has volatile components, it should be stored in memory.
4946 Otherwise, the fact that those components are volatile
4947 will be ignored, and would even crash the compiler.
4948 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
4949 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
4950 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
4951 || TREE_CODE (decl) == RESULT_DECL))
4953 /* It is not an error for a structure with volatile fields to
4954 be declared register, but reset DECL_REGISTER since it
4955 cannot actually go in a register. */
4956 int was_reg = C_DECL_REGISTER (decl);
4957 C_DECL_REGISTER (decl) = 0;
4958 DECL_REGISTER (decl) = 0;
4959 c_mark_addressable (decl);
4960 C_DECL_REGISTER (decl) = was_reg;
4963 /* This is the earliest point at which we might know the assembler
4964 name of a variable. Thus, if it's known before this, die horribly. */
4965 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4967 decl_attributes (&decl, returned_attrs, 0);
4969 return decl;
4973 /* Decode the parameter-list info for a function type or function definition.
4974 The argument is the value returned by `get_parm_info' (or made in c-parse.c
4975 if there is an identifier list instead of a parameter decl list).
4976 These two functions are separate because when a function returns
4977 or receives functions then each is called multiple times but the order
4978 of calls is different. The last call to `grokparms' is always the one
4979 that contains the formal parameter names of a function definition.
4981 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4983 FUNCDEF_FLAG is true for a function definition, false for
4984 a mere declaration. A nonempty identifier-list gets an error message
4985 when FUNCDEF_FLAG is false. */
4987 static tree
4988 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4990 tree arg_types = arg_info->types;
4992 if (funcdef_flag && arg_info->had_vla_unspec)
4994 /* A function definition isn't function prototype scope C99 6.2.1p4. */
4995 /* C99 6.7.5.2p4 */
4996 error ("%<[*]%> not allowed in other than function prototype scope");
4999 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5000 warning (OPT_Wstrict_prototypes,
5001 "function declaration isn%'t a prototype");
5003 if (arg_types == error_mark_node)
5004 return 0; /* don't set TYPE_ARG_TYPES in this case */
5006 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5008 if (!funcdef_flag)
5009 pedwarn ("parameter names (without types) in function declaration");
5011 arg_info->parms = arg_info->types;
5012 arg_info->types = 0;
5013 return 0;
5015 else
5017 tree parm, type, typelt;
5018 unsigned int parmno;
5020 /* If there is a parameter of incomplete type in a definition,
5021 this is an error. In a declaration this is valid, and a
5022 struct or union type may be completed later, before any calls
5023 or definition of the function. In the case where the tag was
5024 first declared within the parameter list, a warning has
5025 already been given. If a parameter has void type, then
5026 however the function cannot be defined or called, so
5027 warn. */
5029 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5030 parm;
5031 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5033 type = TREE_VALUE (typelt);
5034 if (type == error_mark_node)
5035 continue;
5037 if (!COMPLETE_TYPE_P (type))
5039 if (funcdef_flag)
5041 if (DECL_NAME (parm))
5042 error ("parameter %u (%q+D) has incomplete type",
5043 parmno, parm);
5044 else
5045 error ("%Jparameter %u has incomplete type",
5046 parm, parmno);
5048 TREE_VALUE (typelt) = error_mark_node;
5049 TREE_TYPE (parm) = error_mark_node;
5051 else if (VOID_TYPE_P (type))
5053 if (DECL_NAME (parm))
5054 warning (0, "parameter %u (%q+D) has void type",
5055 parmno, parm);
5056 else
5057 warning (0, "%Jparameter %u has void type",
5058 parm, parmno);
5062 if (DECL_NAME (parm) && TREE_USED (parm))
5063 warn_if_shadowing (parm);
5065 return arg_types;
5069 /* Take apart the current scope and return a c_arg_info structure with
5070 info on a parameter list just parsed.
5072 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5074 ELLIPSIS being true means the argument list ended in '...' so don't
5075 append a sentinel (void_list_node) to the end of the type-list. */
5077 struct c_arg_info *
5078 get_parm_info (bool ellipsis)
5080 struct c_binding *b = current_scope->bindings;
5081 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5082 struct c_arg_info);
5083 tree parms = 0;
5084 tree tags = 0;
5085 tree types = 0;
5086 tree others = 0;
5088 static bool explained_incomplete_types = false;
5089 bool gave_void_only_once_err = false;
5091 arg_info->parms = 0;
5092 arg_info->tags = 0;
5093 arg_info->types = 0;
5094 arg_info->others = 0;
5095 arg_info->pending_sizes = 0;
5096 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5098 /* The bindings in this scope must not get put into a block.
5099 We will take care of deleting the binding nodes. */
5100 current_scope->bindings = 0;
5102 /* This function is only called if there was *something* on the
5103 parameter list. */
5104 gcc_assert (b);
5106 /* A parameter list consisting solely of 'void' indicates that the
5107 function takes no arguments. But if the 'void' is qualified
5108 (by 'const' or 'volatile'), or has a storage class specifier
5109 ('register'), then the behavior is undefined; issue an error.
5110 Typedefs for 'void' are OK (see DR#157). */
5111 if (b->prev == 0 /* one binding */
5112 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5113 && !DECL_NAME (b->decl) /* anonymous */
5114 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5116 if (TREE_THIS_VOLATILE (b->decl)
5117 || TREE_READONLY (b->decl)
5118 || C_DECL_REGISTER (b->decl))
5119 error ("%<void%> as only parameter may not be qualified");
5121 /* There cannot be an ellipsis. */
5122 if (ellipsis)
5123 error ("%<void%> must be the only parameter");
5125 arg_info->types = void_list_node;
5126 return arg_info;
5129 if (!ellipsis)
5130 types = void_list_node;
5132 /* Break up the bindings list into parms, tags, types, and others;
5133 apply sanity checks; purge the name-to-decl bindings. */
5134 while (b)
5136 tree decl = b->decl;
5137 tree type = TREE_TYPE (decl);
5138 const char *keyword;
5140 switch (TREE_CODE (decl))
5142 case PARM_DECL:
5143 if (b->id)
5145 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5146 I_SYMBOL_BINDING (b->id) = b->shadowed;
5149 /* Check for forward decls that never got their actual decl. */
5150 if (TREE_ASM_WRITTEN (decl))
5151 error ("parameter %q+D has just a forward declaration", decl);
5152 /* Check for (..., void, ...) and issue an error. */
5153 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5155 if (!gave_void_only_once_err)
5157 error ("%<void%> must be the only parameter");
5158 gave_void_only_once_err = true;
5161 else
5163 /* Valid parameter, add it to the list. */
5164 TREE_CHAIN (decl) = parms;
5165 parms = decl;
5167 /* Since there is a prototype, args are passed in their
5168 declared types. The back end may override this later. */
5169 DECL_ARG_TYPE (decl) = type;
5170 types = tree_cons (0, type, types);
5172 break;
5174 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5175 case UNION_TYPE: keyword = "union"; goto tag;
5176 case RECORD_TYPE: keyword = "struct"; goto tag;
5177 tag:
5178 /* Types may not have tag-names, in which case the type
5179 appears in the bindings list with b->id NULL. */
5180 if (b->id)
5182 gcc_assert (I_TAG_BINDING (b->id) == b);
5183 I_TAG_BINDING (b->id) = b->shadowed;
5186 /* Warn about any struct, union or enum tags defined in a
5187 parameter list. The scope of such types is limited to
5188 the parameter list, which is rarely if ever desirable
5189 (it's impossible to call such a function with type-
5190 correct arguments). An anonymous union parm type is
5191 meaningful as a GNU extension, so don't warn for that. */
5192 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5194 if (b->id)
5195 /* The %s will be one of 'struct', 'union', or 'enum'. */
5196 warning (0, "%<%s %E%> declared inside parameter list",
5197 keyword, b->id);
5198 else
5199 /* The %s will be one of 'struct', 'union', or 'enum'. */
5200 warning (0, "anonymous %s declared inside parameter list",
5201 keyword);
5203 if (!explained_incomplete_types)
5205 warning (0, "its scope is only this definition or declaration,"
5206 " which is probably not what you want");
5207 explained_incomplete_types = true;
5211 tags = tree_cons (b->id, decl, tags);
5212 break;
5214 case CONST_DECL:
5215 case TYPE_DECL:
5216 case FUNCTION_DECL:
5217 /* CONST_DECLs appear here when we have an embedded enum,
5218 and TYPE_DECLs appear here when we have an embedded struct
5219 or union. No warnings for this - we already warned about the
5220 type itself. FUNCTION_DECLs appear when there is an implicit
5221 function declaration in the parameter list. */
5223 TREE_CHAIN (decl) = others;
5224 others = decl;
5225 /* fall through */
5227 case ERROR_MARK:
5228 /* error_mark_node appears here when we have an undeclared
5229 variable. Just throw it away. */
5230 if (b->id)
5232 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5233 I_SYMBOL_BINDING (b->id) = b->shadowed;
5235 break;
5237 /* Other things that might be encountered. */
5238 case LABEL_DECL:
5239 case VAR_DECL:
5240 default:
5241 gcc_unreachable ();
5244 b = free_binding_and_advance (b);
5247 arg_info->parms = parms;
5248 arg_info->tags = tags;
5249 arg_info->types = types;
5250 arg_info->others = others;
5251 arg_info->pending_sizes = get_pending_sizes ();
5252 return arg_info;
5255 /* Get the struct, enum or union (CODE says which) with tag NAME.
5256 Define the tag as a forward-reference if it is not defined.
5257 Return a c_typespec structure for the type specifier. */
5259 struct c_typespec
5260 parser_xref_tag (enum tree_code code, tree name)
5262 struct c_typespec ret;
5263 /* If a cross reference is requested, look up the type
5264 already defined for this tag and return it. */
5266 tree ref = lookup_tag (code, name, 0);
5267 /* If this is the right type of tag, return what we found.
5268 (This reference will be shadowed by shadow_tag later if appropriate.)
5269 If this is the wrong type of tag, do not return it. If it was the
5270 wrong type in the same scope, we will have had an error
5271 message already; if in a different scope and declaring
5272 a name, pending_xref_error will give an error message; but if in a
5273 different scope and not declaring a name, this tag should
5274 shadow the previous declaration of a different type of tag, and
5275 this would not work properly if we return the reference found.
5276 (For example, with "struct foo" in an outer scope, "union foo;"
5277 must shadow that tag with a new one of union type.) */
5278 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5279 if (ref && TREE_CODE (ref) == code)
5281 ret.spec = ref;
5282 return ret;
5285 /* If no such tag is yet defined, create a forward-reference node
5286 and record it as the "definition".
5287 When a real declaration of this type is found,
5288 the forward-reference will be altered into a real type. */
5290 ref = make_node (code);
5291 if (code == ENUMERAL_TYPE)
5293 /* Give the type a default layout like unsigned int
5294 to avoid crashing if it does not get defined. */
5295 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5296 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5297 TYPE_USER_ALIGN (ref) = 0;
5298 TYPE_UNSIGNED (ref) = 1;
5299 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5300 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5301 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5304 pushtag (name, ref);
5306 ret.spec = ref;
5307 return ret;
5310 /* Get the struct, enum or union (CODE says which) with tag NAME.
5311 Define the tag as a forward-reference if it is not defined.
5312 Return a tree for the type. */
5314 tree
5315 xref_tag (enum tree_code code, tree name)
5317 return parser_xref_tag (code, name).spec;
5320 /* Make sure that the tag NAME is defined *in the current scope*
5321 at least as a forward reference.
5322 CODE says which kind of tag NAME ought to be. */
5324 tree
5325 start_struct (enum tree_code code, tree name)
5327 /* If there is already a tag defined at this scope
5328 (as a forward reference), just return it. */
5330 tree ref = 0;
5332 if (name != 0)
5333 ref = lookup_tag (code, name, 1);
5334 if (ref && TREE_CODE (ref) == code)
5336 if (TYPE_SIZE (ref))
5338 if (code == UNION_TYPE)
5339 error ("redefinition of %<union %E%>", name);
5340 else
5341 error ("redefinition of %<struct %E%>", name);
5343 else if (C_TYPE_BEING_DEFINED (ref))
5345 if (code == UNION_TYPE)
5346 error ("nested redefinition of %<union %E%>", name);
5347 else
5348 error ("nested redefinition of %<struct %E%>", name);
5349 /* Don't create structures that contain themselves. */
5350 ref = NULL_TREE;
5354 /* Otherwise create a forward-reference just so the tag is in scope. */
5356 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5358 ref = make_node (code);
5359 pushtag (name, ref);
5362 C_TYPE_BEING_DEFINED (ref) = 1;
5363 TYPE_PACKED (ref) = flag_pack_struct;
5364 return ref;
5367 /* Process the specs, declarator and width (NULL if omitted)
5368 of a structure component, returning a FIELD_DECL node.
5369 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5371 This is done during the parsing of the struct declaration.
5372 The FIELD_DECL nodes are chained together and the lot of them
5373 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5375 tree
5376 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5377 tree width)
5379 tree value;
5381 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5382 && width == NULL_TREE)
5384 /* This is an unnamed decl.
5386 If we have something of the form "union { list } ;" then this
5387 is the anonymous union extension. Similarly for struct.
5389 If this is something of the form "struct foo;", then
5390 If MS extensions are enabled, this is handled as an
5391 anonymous struct.
5392 Otherwise this is a forward declaration of a structure tag.
5394 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5395 If MS extensions are enabled and foo names a structure, then
5396 again this is an anonymous struct.
5397 Otherwise this is an error.
5399 Oh what a horrid tangled web we weave. I wonder if MS consciously
5400 took this from Plan 9 or if it was an accident of implementation
5401 that took root before someone noticed the bug... */
5403 tree type = declspecs->type;
5404 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5405 || TREE_CODE (type) == UNION_TYPE);
5406 bool ok = false;
5408 if (type_ok
5409 && (flag_ms_extensions || !declspecs->typedef_p))
5411 if (flag_ms_extensions)
5412 ok = true;
5413 else if (flag_iso)
5414 ok = false;
5415 else if (TYPE_NAME (type) == NULL)
5416 ok = true;
5417 else
5418 ok = false;
5420 if (!ok)
5422 pedwarn ("declaration does not declare anything");
5423 return NULL_TREE;
5425 if (pedantic)
5426 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5429 value = grokdeclarator (declarator, declspecs, FIELD, false,
5430 width ? &width : NULL);
5432 finish_decl (value, NULL_TREE, NULL_TREE);
5433 DECL_INITIAL (value) = width;
5435 return value;
5438 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5439 the list such that this does not present a problem later. */
5441 static void
5442 detect_field_duplicates (tree fieldlist)
5444 tree x, y;
5445 int timeout = 10;
5447 /* First, see if there are more than "a few" fields.
5448 This is trivially true if there are zero or one fields. */
5449 if (!fieldlist)
5450 return;
5451 x = TREE_CHAIN (fieldlist);
5452 if (!x)
5453 return;
5454 do {
5455 timeout--;
5456 x = TREE_CHAIN (x);
5457 } while (timeout > 0 && x);
5459 /* If there were "few" fields, avoid the overhead of allocating
5460 a hash table. Instead just do the nested traversal thing. */
5461 if (timeout > 0)
5463 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5464 if (DECL_NAME (x))
5466 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5467 if (DECL_NAME (y) == DECL_NAME (x))
5469 error ("duplicate member %q+D", x);
5470 DECL_NAME (x) = NULL_TREE;
5474 else
5476 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5477 void **slot;
5479 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5480 if ((y = DECL_NAME (x)) != 0)
5482 slot = htab_find_slot (htab, y, INSERT);
5483 if (*slot)
5485 error ("duplicate member %q+D", x);
5486 DECL_NAME (x) = NULL_TREE;
5488 *slot = y;
5491 htab_delete (htab);
5495 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5496 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5497 ATTRIBUTES are attributes to be applied to the structure. */
5499 tree
5500 finish_struct (tree t, tree fieldlist, tree attributes)
5502 tree x;
5503 bool toplevel = file_scope == current_scope;
5504 int saw_named_field;
5506 /* If this type was previously laid out as a forward reference,
5507 make sure we lay it out again. */
5509 TYPE_SIZE (t) = 0;
5511 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5513 if (pedantic)
5515 for (x = fieldlist; x; x = TREE_CHAIN (x))
5516 if (DECL_NAME (x) != 0)
5517 break;
5519 if (x == 0)
5521 if (TREE_CODE (t) == UNION_TYPE)
5523 if (fieldlist)
5524 pedwarn ("union has no named members");
5525 else
5526 pedwarn ("union has no members");
5528 else
5530 if (fieldlist)
5531 pedwarn ("struct has no named members");
5532 else
5533 pedwarn ("struct has no members");
5538 /* Install struct as DECL_CONTEXT of each field decl.
5539 Also process specified field sizes, found in the DECL_INITIAL,
5540 storing 0 there after the type has been changed to precision equal
5541 to its width, rather than the precision of the specified standard
5542 type. (Correct layout requires the original type to have been preserved
5543 until now.) */
5545 saw_named_field = 0;
5546 for (x = fieldlist; x; x = TREE_CHAIN (x))
5548 if (TREE_TYPE (x) == error_mark_node)
5549 continue;
5551 DECL_CONTEXT (x) = t;
5553 if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
5554 DECL_PACKED (x) = 1;
5556 /* If any field is const, the structure type is pseudo-const. */
5557 if (TREE_READONLY (x))
5558 C_TYPE_FIELDS_READONLY (t) = 1;
5559 else
5561 /* A field that is pseudo-const makes the structure likewise. */
5562 tree t1 = TREE_TYPE (x);
5563 while (TREE_CODE (t1) == ARRAY_TYPE)
5564 t1 = TREE_TYPE (t1);
5565 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5566 && C_TYPE_FIELDS_READONLY (t1))
5567 C_TYPE_FIELDS_READONLY (t) = 1;
5570 /* Any field that is volatile means variables of this type must be
5571 treated in some ways as volatile. */
5572 if (TREE_THIS_VOLATILE (x))
5573 C_TYPE_FIELDS_VOLATILE (t) = 1;
5575 /* Any field of nominal variable size implies structure is too. */
5576 if (C_DECL_VARIABLE_SIZE (x))
5577 C_TYPE_VARIABLE_SIZE (t) = 1;
5579 if (DECL_INITIAL (x))
5581 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5582 DECL_SIZE (x) = bitsize_int (width);
5583 DECL_BIT_FIELD (x) = 1;
5584 SET_DECL_C_BIT_FIELD (x);
5587 /* Detect flexible array member in an invalid context. */
5588 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5589 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5590 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5591 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5593 if (TREE_CODE (t) == UNION_TYPE)
5595 error ("%Jflexible array member in union", x);
5596 TREE_TYPE (x) = error_mark_node;
5598 else if (TREE_CHAIN (x) != NULL_TREE)
5600 error ("%Jflexible array member not at end of struct", x);
5601 TREE_TYPE (x) = error_mark_node;
5603 else if (!saw_named_field)
5605 error ("%Jflexible array member in otherwise empty struct", x);
5606 TREE_TYPE (x) = error_mark_node;
5610 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5611 && flexible_array_type_p (TREE_TYPE (x)))
5612 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5614 if (DECL_NAME (x))
5615 saw_named_field = 1;
5618 detect_field_duplicates (fieldlist);
5620 /* Now we have the nearly final fieldlist. Record it,
5621 then lay out the structure or union (including the fields). */
5623 TYPE_FIELDS (t) = fieldlist;
5625 layout_type (t);
5627 /* Give bit-fields their proper types. */
5629 tree *fieldlistp = &fieldlist;
5630 while (*fieldlistp)
5631 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5632 && TREE_TYPE (*fieldlistp) != error_mark_node)
5634 unsigned HOST_WIDE_INT width
5635 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5636 tree type = TREE_TYPE (*fieldlistp);
5637 if (width != TYPE_PRECISION (type))
5639 TREE_TYPE (*fieldlistp)
5640 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5641 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5643 DECL_INITIAL (*fieldlistp) = 0;
5645 else
5646 fieldlistp = &TREE_CHAIN (*fieldlistp);
5649 /* Now we have the truly final field list.
5650 Store it in this type and in the variants. */
5652 TYPE_FIELDS (t) = fieldlist;
5654 /* If there are lots of fields, sort so we can look through them fast.
5655 We arbitrarily consider 16 or more elts to be "a lot". */
5658 int len = 0;
5660 for (x = fieldlist; x; x = TREE_CHAIN (x))
5662 if (len > 15 || DECL_NAME (x) == NULL)
5663 break;
5664 len += 1;
5667 if (len > 15)
5669 tree *field_array;
5670 struct lang_type *space;
5671 struct sorted_fields_type *space2;
5673 len += list_length (x);
5675 /* Use the same allocation policy here that make_node uses, to
5676 ensure that this lives as long as the rest of the struct decl.
5677 All decls in an inline function need to be saved. */
5679 space = GGC_CNEW (struct lang_type);
5680 space2 = GGC_NEWVAR (struct sorted_fields_type,
5681 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5683 len = 0;
5684 space->s = space2;
5685 field_array = &space2->elts[0];
5686 for (x = fieldlist; x; x = TREE_CHAIN (x))
5688 field_array[len++] = x;
5690 /* If there is anonymous struct or union, break out of the loop. */
5691 if (DECL_NAME (x) == NULL)
5692 break;
5694 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5695 if (x == NULL)
5697 TYPE_LANG_SPECIFIC (t) = space;
5698 TYPE_LANG_SPECIFIC (t)->s->len = len;
5699 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5700 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5705 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5707 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5708 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5709 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5710 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5711 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5714 /* If this was supposed to be a transparent union, but we can't
5715 make it one, warn and turn off the flag. */
5716 if (TREE_CODE (t) == UNION_TYPE
5717 && TYPE_TRANSPARENT_UNION (t)
5718 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5720 TYPE_TRANSPARENT_UNION (t) = 0;
5721 warning (0, "union cannot be made transparent");
5724 /* If this structure or union completes the type of any previous
5725 variable declaration, lay it out and output its rtl. */
5726 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5728 x = TREE_CHAIN (x))
5730 tree decl = TREE_VALUE (x);
5731 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5732 layout_array_type (TREE_TYPE (decl));
5733 if (TREE_CODE (decl) != TYPE_DECL)
5735 layout_decl (decl, 0);
5736 if (c_dialect_objc ())
5737 objc_check_decl (decl);
5738 rest_of_decl_compilation (decl, toplevel, 0);
5739 if (!toplevel)
5740 expand_decl (decl);
5743 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5745 /* Finish debugging output for this type. */
5746 rest_of_type_compilation (t, toplevel);
5748 /* If we're inside a function proper, i.e. not file-scope and not still
5749 parsing parameters, then arrange for the size of a variable sized type
5750 to be bound now. */
5751 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5752 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5754 return t;
5757 /* Lay out the type T, and its element type, and so on. */
5759 static void
5760 layout_array_type (tree t)
5762 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5763 layout_array_type (TREE_TYPE (t));
5764 layout_type (t);
5767 /* Begin compiling the definition of an enumeration type.
5768 NAME is its name (or null if anonymous).
5769 Returns the type object, as yet incomplete.
5770 Also records info about it so that build_enumerator
5771 may be used to declare the individual values as they are read. */
5773 tree
5774 start_enum (tree name)
5776 tree enumtype = 0;
5778 /* If this is the real definition for a previous forward reference,
5779 fill in the contents in the same object that used to be the
5780 forward reference. */
5782 if (name != 0)
5783 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5785 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5787 enumtype = make_node (ENUMERAL_TYPE);
5788 pushtag (name, enumtype);
5791 if (C_TYPE_BEING_DEFINED (enumtype))
5792 error ("nested redefinition of %<enum %E%>", name);
5794 C_TYPE_BEING_DEFINED (enumtype) = 1;
5796 if (TYPE_VALUES (enumtype) != 0)
5798 /* This enum is a named one that has been declared already. */
5799 error ("redeclaration of %<enum %E%>", name);
5801 /* Completely replace its old definition.
5802 The old enumerators remain defined, however. */
5803 TYPE_VALUES (enumtype) = 0;
5806 enum_next_value = integer_zero_node;
5807 enum_overflow = 0;
5809 if (flag_short_enums)
5810 TYPE_PACKED (enumtype) = 1;
5812 return enumtype;
5815 /* After processing and defining all the values of an enumeration type,
5816 install their decls in the enumeration type and finish it off.
5817 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5818 and ATTRIBUTES are the specified attributes.
5819 Returns ENUMTYPE. */
5821 tree
5822 finish_enum (tree enumtype, tree values, tree attributes)
5824 tree pair, tem;
5825 tree minnode = 0, maxnode = 0;
5826 int precision, unsign;
5827 bool toplevel = (file_scope == current_scope);
5828 struct lang_type *lt;
5830 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5832 /* Calculate the maximum value of any enumerator in this type. */
5834 if (values == error_mark_node)
5835 minnode = maxnode = integer_zero_node;
5836 else
5838 minnode = maxnode = TREE_VALUE (values);
5839 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5841 tree value = TREE_VALUE (pair);
5842 if (tree_int_cst_lt (maxnode, value))
5843 maxnode = value;
5844 if (tree_int_cst_lt (value, minnode))
5845 minnode = value;
5849 /* Construct the final type of this enumeration. It is the same
5850 as one of the integral types - the narrowest one that fits, except
5851 that normally we only go as narrow as int - and signed iff any of
5852 the values are negative. */
5853 unsign = (tree_int_cst_sgn (minnode) >= 0);
5854 precision = MAX (min_precision (minnode, unsign),
5855 min_precision (maxnode, unsign));
5857 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5859 tem = c_common_type_for_size (precision, unsign);
5860 if (tem == NULL)
5862 warning (0, "enumeration values exceed range of largest integer");
5863 tem = long_long_integer_type_node;
5866 else
5867 tem = unsign ? unsigned_type_node : integer_type_node;
5869 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5870 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5871 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5872 TYPE_SIZE (enumtype) = 0;
5874 /* If the precision of the type was specific with an attribute and it
5875 was too small, give an error. Otherwise, use it. */
5876 if (TYPE_PRECISION (enumtype))
5878 if (precision > TYPE_PRECISION (enumtype))
5879 error ("specified mode too small for enumeral values");
5881 else
5882 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5884 layout_type (enumtype);
5886 if (values != error_mark_node)
5888 /* Change the type of the enumerators to be the enum type. We
5889 need to do this irrespective of the size of the enum, for
5890 proper type checking. Replace the DECL_INITIALs of the
5891 enumerators, and the value slots of the list, with copies
5892 that have the enum type; they cannot be modified in place
5893 because they may be shared (e.g. integer_zero_node) Finally,
5894 change the purpose slots to point to the names of the decls. */
5895 for (pair = values; pair; pair = TREE_CHAIN (pair))
5897 tree enu = TREE_PURPOSE (pair);
5898 tree ini = DECL_INITIAL (enu);
5900 TREE_TYPE (enu) = enumtype;
5902 /* The ISO C Standard mandates enumerators to have type int,
5903 even though the underlying type of an enum type is
5904 unspecified. Here we convert any enumerators that fit in
5905 an int to type int, to avoid promotions to unsigned types
5906 when comparing integers with enumerators that fit in the
5907 int range. When -pedantic is given, build_enumerator()
5908 would have already taken care of those that don't fit. */
5909 if (int_fits_type_p (ini, integer_type_node))
5910 tem = integer_type_node;
5911 else
5912 tem = enumtype;
5913 ini = convert (tem, ini);
5915 DECL_INITIAL (enu) = ini;
5916 TREE_PURPOSE (pair) = DECL_NAME (enu);
5917 TREE_VALUE (pair) = ini;
5920 TYPE_VALUES (enumtype) = values;
5923 /* Record the min/max values so that we can warn about bit-field
5924 enumerations that are too small for the values. */
5925 lt = GGC_CNEW (struct lang_type);
5926 lt->enum_min = minnode;
5927 lt->enum_max = maxnode;
5928 TYPE_LANG_SPECIFIC (enumtype) = lt;
5930 /* Fix up all variant types of this enum type. */
5931 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5933 if (tem == enumtype)
5934 continue;
5935 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5936 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5937 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5938 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5939 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5940 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5941 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5942 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5943 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5944 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5945 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5948 /* Finish debugging output for this type. */
5949 rest_of_type_compilation (enumtype, toplevel);
5951 return enumtype;
5954 /* Build and install a CONST_DECL for one value of the
5955 current enumeration type (one that was begun with start_enum).
5956 Return a tree-list containing the CONST_DECL and its value.
5957 Assignment of sequential values by default is handled here. */
5959 tree
5960 build_enumerator (tree name, tree value)
5962 tree decl, type;
5964 /* Validate and default VALUE. */
5966 if (value != 0)
5968 /* Don't issue more errors for error_mark_node (i.e. an
5969 undeclared identifier) - just ignore the value expression. */
5970 if (value == error_mark_node)
5971 value = 0;
5972 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5973 || TREE_CODE (value) != INTEGER_CST)
5975 error ("enumerator value for %qE is not an integer constant", name);
5976 value = 0;
5978 else
5980 value = default_conversion (value);
5981 constant_expression_warning (value);
5985 /* Default based on previous value. */
5986 /* It should no longer be possible to have NON_LVALUE_EXPR
5987 in the default. */
5988 if (value == 0)
5990 value = enum_next_value;
5991 if (enum_overflow)
5992 error ("overflow in enumeration values");
5995 if (pedantic && !int_fits_type_p (value, integer_type_node))
5997 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5998 /* XXX This causes -pedantic to change the meaning of the program.
5999 Remove? -zw 2004-03-15 */
6000 value = convert (integer_type_node, value);
6003 /* Set basis for default for next value. */
6004 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
6005 enum_overflow = tree_int_cst_lt (enum_next_value, value);
6007 /* Now create a declaration for the enum value name. */
6009 type = TREE_TYPE (value);
6010 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6011 TYPE_PRECISION (integer_type_node)),
6012 (TYPE_PRECISION (type)
6013 >= TYPE_PRECISION (integer_type_node)
6014 && TYPE_UNSIGNED (type)));
6016 decl = build_decl (CONST_DECL, name, type);
6017 DECL_INITIAL (decl) = convert (type, value);
6018 pushdecl (decl);
6020 return tree_cons (decl, value, NULL_TREE);
6024 /* Create the FUNCTION_DECL for a function definition.
6025 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6026 the declaration; they describe the function's name and the type it returns,
6027 but twisted together in a fashion that parallels the syntax of C.
6029 This function creates a binding context for the function body
6030 as well as setting up the FUNCTION_DECL in current_function_decl.
6032 Returns 1 on success. If the DECLARATOR is not suitable for a function
6033 (it defines a datum instead), we return 0, which tells
6034 yyparse to report a parse error. */
6037 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6038 tree attributes)
6040 tree decl1, old_decl;
6041 tree restype, resdecl;
6042 struct c_label_context_se *nstack_se;
6043 struct c_label_context_vm *nstack_vm;
6045 current_function_returns_value = 0; /* Assume, until we see it does. */
6046 current_function_returns_null = 0;
6047 current_function_returns_abnormally = 0;
6048 warn_about_return_type = 0;
6049 c_switch_stack = NULL;
6051 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6052 nstack_se->labels_def = NULL;
6053 nstack_se->labels_used = NULL;
6054 nstack_se->next = label_context_stack_se;
6055 label_context_stack_se = nstack_se;
6057 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6058 nstack_vm->labels_def = NULL;
6059 nstack_vm->labels_used = NULL;
6060 nstack_vm->scope = 0;
6061 nstack_vm->next = label_context_stack_vm;
6062 label_context_stack_vm = nstack_vm;
6064 /* Indicate no valid break/continue context by setting these variables
6065 to some non-null, non-label value. We'll notice and emit the proper
6066 error message in c_finish_bc_stmt. */
6067 c_break_label = c_cont_label = size_zero_node;
6069 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
6071 /* If the declarator is not suitable for a function definition,
6072 cause a syntax error. */
6073 if (decl1 == 0)
6075 label_context_stack_se = label_context_stack_se->next;
6076 label_context_stack_vm = label_context_stack_vm->next;
6077 return 0;
6080 decl_attributes (&decl1, attributes, 0);
6082 if (DECL_DECLARED_INLINE_P (decl1)
6083 && DECL_UNINLINABLE (decl1)
6084 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6085 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6086 decl1);
6088 #if WANT_C99_INLINE_SEMANTICS
6089 /* Handle gnu_inline attribute. */
6090 if (declspecs->inline_p
6091 && flag_isoc99
6092 && TREE_CODE (decl1) == FUNCTION_DECL
6093 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1)))
6095 if (declspecs->storage_class != csc_static)
6096 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6098 #endif /* WANT_C99_INLINE_SEMANTICS */
6100 announce_function (decl1);
6102 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6104 error ("return type is an incomplete type");
6105 /* Make it return void instead. */
6106 TREE_TYPE (decl1)
6107 = build_function_type (void_type_node,
6108 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6111 if (warn_about_return_type)
6112 pedwarn_c99 ("return type defaults to %<int%>");
6114 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6115 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6116 DECL_INITIAL (decl1) = error_mark_node;
6118 /* If this definition isn't a prototype and we had a prototype declaration
6119 before, copy the arg type info from that prototype. */
6120 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6121 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6122 old_decl = 0;
6123 current_function_prototype_locus = UNKNOWN_LOCATION;
6124 current_function_prototype_built_in = false;
6125 current_function_prototype_arg_types = NULL_TREE;
6126 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6128 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6129 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6130 TREE_TYPE (TREE_TYPE (old_decl))))
6132 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6133 TREE_TYPE (decl1));
6134 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6135 current_function_prototype_built_in
6136 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6137 current_function_prototype_arg_types
6138 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6140 if (TREE_PUBLIC (decl1))
6142 /* If there is an external prototype declaration of this
6143 function, record its location but do not copy information
6144 to this decl. This may be an invisible declaration
6145 (built-in or in a scope which has finished) or simply
6146 have more refined argument types than any declaration
6147 found above. */
6148 struct c_binding *b;
6149 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6150 if (B_IN_SCOPE (b, external_scope))
6151 break;
6152 if (b)
6154 tree ext_decl, ext_type;
6155 ext_decl = b->decl;
6156 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6157 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6158 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6159 TREE_TYPE (ext_type)))
6161 current_function_prototype_locus
6162 = DECL_SOURCE_LOCATION (ext_decl);
6163 current_function_prototype_built_in
6164 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6165 current_function_prototype_arg_types
6166 = TYPE_ARG_TYPES (ext_type);
6172 /* Optionally warn of old-fashioned def with no previous prototype. */
6173 if (warn_strict_prototypes
6174 && old_decl != error_mark_node
6175 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6176 && C_DECL_ISNT_PROTOTYPE (old_decl))
6177 warning (OPT_Wstrict_prototypes,
6178 "function declaration isn%'t a prototype");
6179 /* Optionally warn of any global def with no previous prototype. */
6180 else if (warn_missing_prototypes
6181 && old_decl != error_mark_node
6182 && TREE_PUBLIC (decl1)
6183 && !MAIN_NAME_P (DECL_NAME (decl1))
6184 && C_DECL_ISNT_PROTOTYPE (old_decl))
6185 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6186 /* Optionally warn of any def with no previous prototype
6187 if the function has already been used. */
6188 else if (warn_missing_prototypes
6189 && old_decl != 0
6190 && old_decl != error_mark_node
6191 && TREE_USED (old_decl)
6192 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6193 warning (OPT_Wmissing_prototypes,
6194 "%q+D was used with no prototype before its definition", decl1);
6195 /* Optionally warn of any global def with no previous declaration. */
6196 else if (warn_missing_declarations
6197 && TREE_PUBLIC (decl1)
6198 && old_decl == 0
6199 && !MAIN_NAME_P (DECL_NAME (decl1)))
6200 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6201 decl1);
6202 /* Optionally warn of any def with no previous declaration
6203 if the function has already been used. */
6204 else if (warn_missing_declarations
6205 && old_decl != 0
6206 && old_decl != error_mark_node
6207 && TREE_USED (old_decl)
6208 && C_DECL_IMPLICIT (old_decl))
6209 warning (OPT_Wmissing_declarations,
6210 "%q+D was used with no declaration before its definition", decl1);
6212 /* This function exists in static storage.
6213 (This does not mean `static' in the C sense!) */
6214 TREE_STATIC (decl1) = 1;
6216 /* A nested function is not global. */
6217 if (current_function_decl != 0)
6218 TREE_PUBLIC (decl1) = 0;
6220 /* This is the earliest point at which we might know the assembler
6221 name of the function. Thus, if it's set before this, die horribly. */
6222 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6224 /* If #pragma weak was used, mark the decl weak now. */
6225 if (current_scope == file_scope)
6226 maybe_apply_pragma_weak (decl1);
6228 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6229 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6231 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6232 != integer_type_node)
6233 pedwarn ("return type of %q+D is not %<int%>", decl1);
6235 check_main_parameter_types(decl1);
6237 if (!TREE_PUBLIC (decl1))
6238 pedwarn ("%q+D is normally a non-static function", decl1);
6241 /* Record the decl so that the function name is defined.
6242 If we already have a decl for this name, and it is a FUNCTION_DECL,
6243 use the old decl. */
6245 current_function_decl = pushdecl (decl1);
6247 push_scope ();
6248 declare_parm_level ();
6250 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6251 /* Promote the value to int before returning it. */
6252 if (c_promoting_integer_type_p (restype))
6254 /* It retains unsignedness if not really getting wider. */
6255 if (TYPE_UNSIGNED (restype)
6256 && (TYPE_PRECISION (restype)
6257 == TYPE_PRECISION (integer_type_node)))
6258 restype = unsigned_type_node;
6259 else
6260 restype = integer_type_node;
6263 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6264 DECL_ARTIFICIAL (resdecl) = 1;
6265 DECL_IGNORED_P (resdecl) = 1;
6266 DECL_RESULT (current_function_decl) = resdecl;
6268 start_fname_decls ();
6270 return 1;
6273 /* Subroutine of store_parm_decls which handles new-style function
6274 definitions (prototype format). The parms already have decls, so we
6275 need only record them as in effect and complain if any redundant
6276 old-style parm decls were written. */
6277 static void
6278 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6280 tree decl;
6282 if (current_scope->bindings)
6284 error ("%Jold-style parameter declarations in prototyped "
6285 "function definition", fndecl);
6287 /* Get rid of the old-style declarations. */
6288 pop_scope ();
6289 push_scope ();
6291 /* Don't issue this warning for nested functions, and don't issue this
6292 warning if we got here because ARG_INFO_TYPES was error_mark_node
6293 (this happens when a function definition has just an ellipsis in
6294 its parameter list). */
6295 else if (!in_system_header && !current_function_scope
6296 && arg_info->types != error_mark_node)
6297 warning (OPT_Wtraditional,
6298 "%Jtraditional C rejects ISO C style function definitions",
6299 fndecl);
6301 /* Now make all the parameter declarations visible in the function body.
6302 We can bypass most of the grunt work of pushdecl. */
6303 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6305 DECL_CONTEXT (decl) = current_function_decl;
6306 if (DECL_NAME (decl))
6308 bind (DECL_NAME (decl), decl, current_scope,
6309 /*invisible=*/false, /*nested=*/false);
6310 if (!TREE_USED (decl))
6311 warn_if_shadowing (decl);
6313 else
6314 error ("%Jparameter name omitted", decl);
6317 /* Record the parameter list in the function declaration. */
6318 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6320 /* Now make all the ancillary declarations visible, likewise. */
6321 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6323 DECL_CONTEXT (decl) = current_function_decl;
6324 if (DECL_NAME (decl))
6325 bind (DECL_NAME (decl), decl, current_scope,
6326 /*invisible=*/false, /*nested=*/false);
6329 /* And all the tag declarations. */
6330 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6331 if (TREE_PURPOSE (decl))
6332 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6333 /*invisible=*/false, /*nested=*/false);
6336 /* Subroutine of store_parm_decls which handles old-style function
6337 definitions (separate parameter list and declarations). */
6339 static void
6340 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6342 struct c_binding *b;
6343 tree parm, decl, last;
6344 tree parmids = arg_info->parms;
6345 struct pointer_set_t *seen_args = pointer_set_create ();
6347 if (!in_system_header)
6348 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6349 fndecl);
6351 /* Match each formal parameter name with its declaration. Save each
6352 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6353 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6355 if (TREE_VALUE (parm) == 0)
6357 error ("%Jparameter name missing from parameter list", fndecl);
6358 TREE_PURPOSE (parm) = 0;
6359 continue;
6362 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6363 if (b && B_IN_CURRENT_SCOPE (b))
6365 decl = b->decl;
6366 /* If we got something other than a PARM_DECL it is an error. */
6367 if (TREE_CODE (decl) != PARM_DECL)
6368 error ("%q+D declared as a non-parameter", decl);
6369 /* If the declaration is already marked, we have a duplicate
6370 name. Complain and ignore the duplicate. */
6371 else if (pointer_set_contains (seen_args, decl))
6373 error ("multiple parameters named %q+D", decl);
6374 TREE_PURPOSE (parm) = 0;
6375 continue;
6377 /* If the declaration says "void", complain and turn it into
6378 an int. */
6379 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6381 error ("parameter %q+D declared with void type", decl);
6382 TREE_TYPE (decl) = integer_type_node;
6383 DECL_ARG_TYPE (decl) = integer_type_node;
6384 layout_decl (decl, 0);
6386 warn_if_shadowing (decl);
6388 /* If no declaration found, default to int. */
6389 else
6391 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6392 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6393 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6394 pushdecl (decl);
6395 warn_if_shadowing (decl);
6397 if (flag_isoc99)
6398 pedwarn ("type of %q+D defaults to %<int%>", decl);
6399 else
6400 warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
6403 TREE_PURPOSE (parm) = decl;
6404 pointer_set_insert (seen_args, decl);
6407 /* Now examine the parms chain for incomplete declarations
6408 and declarations with no corresponding names. */
6410 for (b = current_scope->bindings; b; b = b->prev)
6412 parm = b->decl;
6413 if (TREE_CODE (parm) != PARM_DECL)
6414 continue;
6416 if (TREE_TYPE (parm) != error_mark_node
6417 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6419 error ("parameter %q+D has incomplete type", parm);
6420 TREE_TYPE (parm) = error_mark_node;
6423 if (!pointer_set_contains (seen_args, parm))
6425 error ("declaration for parameter %q+D but no such parameter", parm);
6427 /* Pretend the parameter was not missing.
6428 This gets us to a standard state and minimizes
6429 further error messages. */
6430 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6434 /* Chain the declarations together in the order of the list of
6435 names. Store that chain in the function decl, replacing the
6436 list of names. Update the current scope to match. */
6437 DECL_ARGUMENTS (fndecl) = 0;
6439 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6440 if (TREE_PURPOSE (parm))
6441 break;
6442 if (parm && TREE_PURPOSE (parm))
6444 last = TREE_PURPOSE (parm);
6445 DECL_ARGUMENTS (fndecl) = last;
6447 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6448 if (TREE_PURPOSE (parm))
6450 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6451 last = TREE_PURPOSE (parm);
6453 TREE_CHAIN (last) = 0;
6456 pointer_set_destroy (seen_args);
6458 /* If there was a previous prototype,
6459 set the DECL_ARG_TYPE of each argument according to
6460 the type previously specified, and report any mismatches. */
6462 if (current_function_prototype_arg_types)
6464 tree type;
6465 for (parm = DECL_ARGUMENTS (fndecl),
6466 type = current_function_prototype_arg_types;
6467 parm || (type && TREE_VALUE (type) != error_mark_node
6468 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
6469 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6471 if (parm == 0 || type == 0
6472 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6474 if (current_function_prototype_built_in)
6475 warning (0, "number of arguments doesn%'t match "
6476 "built-in prototype");
6477 else
6479 error ("number of arguments doesn%'t match prototype");
6480 error ("%Hprototype declaration",
6481 &current_function_prototype_locus);
6483 break;
6485 /* Type for passing arg must be consistent with that
6486 declared for the arg. ISO C says we take the unqualified
6487 type for parameters declared with qualified type. */
6488 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6489 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6491 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6492 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6494 /* Adjust argument to match prototype. E.g. a previous
6495 `int foo(float);' prototype causes
6496 `int foo(x) float x; {...}' to be treated like
6497 `int foo(float x) {...}'. This is particularly
6498 useful for argument types like uid_t. */
6499 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6501 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6502 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6503 && TYPE_PRECISION (TREE_TYPE (parm))
6504 < TYPE_PRECISION (integer_type_node))
6505 DECL_ARG_TYPE (parm) = integer_type_node;
6507 if (pedantic)
6509 /* ??? Is it possible to get here with a
6510 built-in prototype or will it always have
6511 been diagnosed as conflicting with an
6512 old-style definition and discarded? */
6513 if (current_function_prototype_built_in)
6514 warning (0, "promoted argument %qD "
6515 "doesn%'t match built-in prototype", parm);
6516 else
6518 pedwarn ("promoted argument %qD "
6519 "doesn%'t match prototype", parm);
6520 pedwarn ("%Hprototype declaration",
6521 &current_function_prototype_locus);
6525 else
6527 if (current_function_prototype_built_in)
6528 warning (0, "argument %qD doesn%'t match "
6529 "built-in prototype", parm);
6530 else
6532 error ("argument %qD doesn%'t match prototype", parm);
6533 error ("%Hprototype declaration",
6534 &current_function_prototype_locus);
6539 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6542 /* Otherwise, create a prototype that would match. */
6544 else
6546 tree actual = 0, last = 0, type;
6548 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6550 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6551 if (last)
6552 TREE_CHAIN (last) = type;
6553 else
6554 actual = type;
6555 last = type;
6557 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6558 if (last)
6559 TREE_CHAIN (last) = type;
6560 else
6561 actual = type;
6563 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6564 of the type of this function, but we need to avoid having this
6565 affect the types of other similarly-typed functions, so we must
6566 first force the generation of an identical (but separate) type
6567 node for the relevant function type. The new node we create
6568 will be a variant of the main variant of the original function
6569 type. */
6571 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6573 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6577 /* Store parameter declarations passed in ARG_INFO into the current
6578 function declaration. */
6580 void
6581 store_parm_decls_from (struct c_arg_info *arg_info)
6583 current_function_arg_info = arg_info;
6584 store_parm_decls ();
6587 /* Store the parameter declarations into the current function declaration.
6588 This is called after parsing the parameter declarations, before
6589 digesting the body of the function.
6591 For an old-style definition, construct a prototype out of the old-style
6592 parameter declarations and inject it into the function's type. */
6594 void
6595 store_parm_decls (void)
6597 tree fndecl = current_function_decl;
6598 bool proto;
6600 /* The argument information block for FNDECL. */
6601 struct c_arg_info *arg_info = current_function_arg_info;
6602 current_function_arg_info = 0;
6604 /* True if this definition is written with a prototype. Note:
6605 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6606 list in a function definition as equivalent to (void) -- an
6607 empty argument list specifies the function has no parameters,
6608 but only (void) sets up a prototype for future calls. */
6609 proto = arg_info->types != 0;
6611 if (proto)
6612 store_parm_decls_newstyle (fndecl, arg_info);
6613 else
6614 store_parm_decls_oldstyle (fndecl, arg_info);
6616 /* The next call to push_scope will be a function body. */
6618 next_is_function_body = true;
6620 /* Write a record describing this function definition to the prototypes
6621 file (if requested). */
6623 gen_aux_info_record (fndecl, 1, 0, proto);
6625 /* Initialize the RTL code for the function. */
6626 allocate_struct_function (fndecl);
6628 /* Begin the statement tree for this function. */
6629 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6631 /* ??? Insert the contents of the pending sizes list into the function
6632 to be evaluated. The only reason left to have this is
6633 void foo(int n, int array[n++])
6634 because we throw away the array type in favor of a pointer type, and
6635 thus won't naturally see the SAVE_EXPR containing the increment. All
6636 other pending sizes would be handled by gimplify_parameters. */
6638 tree t;
6639 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6640 add_stmt (TREE_VALUE (t));
6643 /* Even though we're inside a function body, we still don't want to
6644 call expand_expr to calculate the size of a variable-sized array.
6645 We haven't necessarily assigned RTL to all variables yet, so it's
6646 not safe to try to expand expressions involving them. */
6647 cfun->x_dont_save_pending_sizes_p = 1;
6650 /* Emit diagnostics that require gimple input for detection. Operate on
6651 FNDECL and all its nested functions. */
6653 static void
6654 c_gimple_diagnostics_recursively (tree fndecl)
6656 struct cgraph_node *cgn;
6658 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6659 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6661 /* Notice when OpenMP structured block constraints are violated. */
6662 if (flag_openmp)
6663 diagnose_omp_structured_block_errors (fndecl);
6665 /* Finalize all nested functions now. */
6666 cgn = cgraph_node (fndecl);
6667 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6668 c_gimple_diagnostics_recursively (cgn->decl);
6671 /* Finish up a function declaration and compile that function
6672 all the way to assembler language output. The free the storage
6673 for the function definition.
6675 This is called after parsing the body of the function definition. */
6677 void
6678 finish_function (void)
6680 tree fndecl = current_function_decl;
6682 label_context_stack_se = label_context_stack_se->next;
6683 label_context_stack_vm = label_context_stack_vm->next;
6685 if (TREE_CODE (fndecl) == FUNCTION_DECL
6686 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6688 tree args = DECL_ARGUMENTS (fndecl);
6689 for (; args; args = TREE_CHAIN (args))
6691 tree type = TREE_TYPE (args);
6692 if (INTEGRAL_TYPE_P (type)
6693 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6694 DECL_ARG_TYPE (args) = integer_type_node;
6698 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6699 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6701 /* Must mark the RESULT_DECL as being in this function. */
6703 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6704 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6706 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6708 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6709 != integer_type_node)
6711 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6712 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6713 if (!warn_main)
6714 pedwarn ("return type of %q+D is not %<int%>", fndecl);
6716 else
6718 if (flag_isoc99)
6720 tree stmt = c_finish_return (integer_zero_node);
6721 #ifdef USE_MAPPED_LOCATION
6722 /* Hack. We don't want the middle-end to warn that this return
6723 is unreachable, so we mark its location as special. Using
6724 UNKNOWN_LOCATION has the problem that it gets clobbered in
6725 annotate_one_with_locus. A cleaner solution might be to
6726 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6728 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6729 #else
6730 /* Hack. We don't want the middle-end to warn that this
6731 return is unreachable, so put the statement on the
6732 special line 0. */
6733 annotate_with_file_line (stmt, input_filename, 0);
6734 #endif
6739 /* Tie off the statement tree for this function. */
6740 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6742 finish_fname_decls ();
6744 /* Complain if there's just no return statement. */
6745 if (warn_return_type
6746 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6747 && !current_function_returns_value && !current_function_returns_null
6748 /* Don't complain if we are no-return. */
6749 && !current_function_returns_abnormally
6750 /* Don't warn for main(). */
6751 && !MAIN_NAME_P (DECL_NAME (fndecl))
6752 /* Or if they didn't actually specify a return type. */
6753 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6754 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6755 inline function, as we might never be compiled separately. */
6756 && DECL_INLINE (fndecl))
6758 warning (OPT_Wreturn_type,
6759 "no return statement in function returning non-void");
6760 TREE_NO_WARNING (fndecl) = 1;
6763 /* With just -Wextra, complain only if function returns both with
6764 and without a value. */
6765 if (extra_warnings
6766 && current_function_returns_value
6767 && current_function_returns_null)
6768 warning (OPT_Wextra, "this function may return with or without a value");
6770 /* Store the end of the function, so that we get good line number
6771 info for the epilogue. */
6772 cfun->function_end_locus = input_location;
6774 /* Keep track of functions declared with the "constructor" and
6775 "destructor" attribute. */
6776 c_record_cdtor_fn (fndecl);
6778 /* Finalize the ELF visibility for the function. */
6779 c_determine_visibility (fndecl);
6781 /* Genericize before inlining. Delay genericizing nested functions
6782 until their parent function is genericized. Since finalizing
6783 requires GENERIC, delay that as well. */
6785 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6786 && !undef_nested_function)
6788 if (!decl_function_context (fndecl))
6790 c_genericize (fndecl);
6791 c_gimple_diagnostics_recursively (fndecl);
6793 /* ??? Objc emits functions after finalizing the compilation unit.
6794 This should be cleaned up later and this conditional removed. */
6795 if (cgraph_global_info_ready)
6797 c_expand_body (fndecl);
6798 return;
6801 cgraph_finalize_function (fndecl, false);
6803 else
6805 /* Register this function with cgraph just far enough to get it
6806 added to our parent's nested function list. Handy, since the
6807 C front end doesn't have such a list. */
6808 (void) cgraph_node (fndecl);
6812 if (!decl_function_context (fndecl))
6813 undef_nested_function = false;
6815 /* We're leaving the context of this function, so zap cfun.
6816 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6817 tree_rest_of_compilation. */
6818 cfun = NULL;
6819 current_function_decl = NULL;
6822 /* Check the declarations given in a for-loop for satisfying the C99
6823 constraints. If exactly one such decl is found, return it. */
6825 tree
6826 check_for_loop_decls (void)
6828 struct c_binding *b;
6829 tree one_decl = NULL_TREE;
6830 int n_decls = 0;
6833 if (!flag_isoc99)
6835 /* If we get here, declarations have been used in a for loop without
6836 the C99 for loop scope. This doesn't make much sense, so don't
6837 allow it. */
6838 error ("%<for%> loop initial declaration used outside C99 mode");
6839 return NULL_TREE;
6841 /* C99 subclause 6.8.5 paragraph 3:
6843 [#3] The declaration part of a for statement shall only
6844 declare identifiers for objects having storage class auto or
6845 register.
6847 It isn't clear whether, in this sentence, "identifiers" binds to
6848 "shall only declare" or to "objects" - that is, whether all identifiers
6849 declared must be identifiers for objects, or whether the restriction
6850 only applies to those that are. (A question on this in comp.std.c
6851 in November 2000 received no answer.) We implement the strictest
6852 interpretation, to avoid creating an extension which later causes
6853 problems. */
6855 for (b = current_scope->bindings; b; b = b->prev)
6857 tree id = b->id;
6858 tree decl = b->decl;
6860 if (!id)
6861 continue;
6863 switch (TREE_CODE (decl))
6865 case VAR_DECL:
6866 if (TREE_STATIC (decl))
6867 error ("declaration of static variable %q+D in %<for%> loop "
6868 "initial declaration", decl);
6869 else if (DECL_EXTERNAL (decl))
6870 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6871 "initial declaration", decl);
6872 break;
6874 case RECORD_TYPE:
6875 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6876 id);
6877 break;
6878 case UNION_TYPE:
6879 error ("%<union %E%> declared in %<for%> loop initial declaration",
6880 id);
6881 break;
6882 case ENUMERAL_TYPE:
6883 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6884 id);
6885 break;
6886 default:
6887 error ("declaration of non-variable %q+D in %<for%> loop "
6888 "initial declaration", decl);
6891 n_decls++;
6892 one_decl = decl;
6895 return n_decls == 1 ? one_decl : NULL_TREE;
6898 /* Save and reinitialize the variables
6899 used during compilation of a C function. */
6901 void
6902 c_push_function_context (struct function *f)
6904 struct language_function *p;
6905 p = GGC_NEW (struct language_function);
6906 f->language = p;
6908 p->base.x_stmt_tree = c_stmt_tree;
6909 p->x_break_label = c_break_label;
6910 p->x_cont_label = c_cont_label;
6911 p->x_switch_stack = c_switch_stack;
6912 p->arg_info = current_function_arg_info;
6913 p->returns_value = current_function_returns_value;
6914 p->returns_null = current_function_returns_null;
6915 p->returns_abnormally = current_function_returns_abnormally;
6916 p->warn_about_return_type = warn_about_return_type;
6919 /* Restore the variables used during compilation of a C function. */
6921 void
6922 c_pop_function_context (struct function *f)
6924 struct language_function *p = f->language;
6926 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6927 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6929 /* Stop pointing to the local nodes about to be freed. */
6930 /* But DECL_INITIAL must remain nonzero so we know this
6931 was an actual function definition. */
6932 DECL_INITIAL (current_function_decl) = error_mark_node;
6933 DECL_ARGUMENTS (current_function_decl) = 0;
6936 c_stmt_tree = p->base.x_stmt_tree;
6937 c_break_label = p->x_break_label;
6938 c_cont_label = p->x_cont_label;
6939 c_switch_stack = p->x_switch_stack;
6940 current_function_arg_info = p->arg_info;
6941 current_function_returns_value = p->returns_value;
6942 current_function_returns_null = p->returns_null;
6943 current_function_returns_abnormally = p->returns_abnormally;
6944 warn_about_return_type = p->warn_about_return_type;
6946 f->language = NULL;
6949 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6951 void
6952 c_dup_lang_specific_decl (tree decl)
6954 struct lang_decl *ld;
6956 if (!DECL_LANG_SPECIFIC (decl))
6957 return;
6959 ld = GGC_NEW (struct lang_decl);
6960 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6961 DECL_LANG_SPECIFIC (decl) = ld;
6964 /* The functions below are required for functionality of doing
6965 function at once processing in the C front end. Currently these
6966 functions are not called from anywhere in the C front end, but as
6967 these changes continue, that will change. */
6969 /* Returns the stmt_tree (if any) to which statements are currently
6970 being added. If there is no active statement-tree, NULL is
6971 returned. */
6973 stmt_tree
6974 current_stmt_tree (void)
6976 return &c_stmt_tree;
6979 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6980 C. */
6983 anon_aggr_type_p (tree ARG_UNUSED (node))
6985 return 0;
6988 /* Return the global value of T as a symbol. */
6990 tree
6991 identifier_global_value (tree t)
6993 struct c_binding *b;
6995 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6996 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6997 return b->decl;
6999 return 0;
7002 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7003 otherwise the name is found in ridpointers from RID_INDEX. */
7005 void
7006 record_builtin_type (enum rid rid_index, const char *name, tree type)
7008 tree id, decl;
7009 if (name == 0)
7010 id = ridpointers[(int) rid_index];
7011 else
7012 id = get_identifier (name);
7013 decl = build_decl (TYPE_DECL, id, type);
7014 pushdecl (decl);
7015 if (debug_hooks->type_decl)
7016 debug_hooks->type_decl (decl, false);
7019 /* Build the void_list_node (void_type_node having been created). */
7020 tree
7021 build_void_list_node (void)
7023 tree t = build_tree_list (NULL_TREE, void_type_node);
7024 return t;
7027 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7029 struct c_parm *
7030 build_c_parm (struct c_declspecs *specs, tree attrs,
7031 struct c_declarator *declarator)
7033 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7034 ret->specs = specs;
7035 ret->attrs = attrs;
7036 ret->declarator = declarator;
7037 return ret;
7040 /* Return a declarator with nested attributes. TARGET is the inner
7041 declarator to which these attributes apply. ATTRS are the
7042 attributes. */
7044 struct c_declarator *
7045 build_attrs_declarator (tree attrs, struct c_declarator *target)
7047 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7048 ret->kind = cdk_attrs;
7049 ret->declarator = target;
7050 ret->u.attrs = attrs;
7051 return ret;
7054 /* Return a declarator for a function with arguments specified by ARGS
7055 and return type specified by TARGET. */
7057 struct c_declarator *
7058 build_function_declarator (struct c_arg_info *args,
7059 struct c_declarator *target)
7061 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7062 ret->kind = cdk_function;
7063 ret->declarator = target;
7064 ret->u.arg_info = args;
7065 return ret;
7068 /* Return a declarator for the identifier IDENT (which may be
7069 NULL_TREE for an abstract declarator). */
7071 struct c_declarator *
7072 build_id_declarator (tree ident)
7074 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7075 ret->kind = cdk_id;
7076 ret->declarator = 0;
7077 ret->u.id = ident;
7078 /* Default value - may get reset to a more precise location. */
7079 ret->id_loc = input_location;
7080 return ret;
7083 /* Return something to represent absolute declarators containing a *.
7084 TARGET is the absolute declarator that the * contains.
7085 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7086 to apply to the pointer type. */
7088 struct c_declarator *
7089 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7090 struct c_declarator *target)
7092 tree attrs;
7093 int quals = 0;
7094 struct c_declarator *itarget = target;
7095 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7096 if (type_quals_attrs)
7098 attrs = type_quals_attrs->attrs;
7099 quals = quals_from_declspecs (type_quals_attrs);
7100 if (attrs != NULL_TREE)
7101 itarget = build_attrs_declarator (attrs, target);
7103 ret->kind = cdk_pointer;
7104 ret->declarator = itarget;
7105 ret->u.pointer_quals = quals;
7106 return ret;
7109 /* Return a pointer to a structure for an empty list of declaration
7110 specifiers. */
7112 struct c_declspecs *
7113 build_null_declspecs (void)
7115 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7116 ret->type = 0;
7117 ret->decl_attr = 0;
7118 ret->attrs = 0;
7119 ret->typespec_word = cts_none;
7120 ret->storage_class = csc_none;
7121 ret->declspecs_seen_p = false;
7122 ret->type_seen_p = false;
7123 ret->non_sc_seen_p = false;
7124 ret->typedef_p = false;
7125 ret->tag_defined_p = false;
7126 ret->explicit_signed_p = false;
7127 ret->deprecated_p = false;
7128 ret->default_int_p = false;
7129 ret->long_p = false;
7130 ret->long_long_p = false;
7131 ret->short_p = false;
7132 ret->signed_p = false;
7133 ret->unsigned_p = false;
7134 ret->complex_p = false;
7135 ret->inline_p = false;
7136 ret->thread_p = false;
7137 ret->const_p = false;
7138 ret->volatile_p = false;
7139 ret->restrict_p = false;
7140 return ret;
7143 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7144 returning SPECS. */
7146 struct c_declspecs *
7147 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7149 enum rid i;
7150 bool dupe = false;
7151 specs->non_sc_seen_p = true;
7152 specs->declspecs_seen_p = true;
7153 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7154 && C_IS_RESERVED_WORD (qual));
7155 i = C_RID_CODE (qual);
7156 switch (i)
7158 case RID_CONST:
7159 dupe = specs->const_p;
7160 specs->const_p = true;
7161 break;
7162 case RID_VOLATILE:
7163 dupe = specs->volatile_p;
7164 specs->volatile_p = true;
7165 break;
7166 case RID_RESTRICT:
7167 dupe = specs->restrict_p;
7168 specs->restrict_p = true;
7169 break;
7170 default:
7171 gcc_unreachable ();
7173 if (dupe && pedantic && !flag_isoc99)
7174 pedwarn ("duplicate %qE", qual);
7175 return specs;
7178 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7179 returning SPECS. */
7181 struct c_declspecs *
7182 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7184 tree type = spec.spec;
7185 specs->non_sc_seen_p = true;
7186 specs->declspecs_seen_p = true;
7187 specs->type_seen_p = true;
7188 if (TREE_DEPRECATED (type))
7189 specs->deprecated_p = true;
7191 /* Handle type specifier keywords. */
7192 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
7194 enum rid i = C_RID_CODE (type);
7195 if (specs->type)
7197 error ("two or more data types in declaration specifiers");
7198 return specs;
7200 if ((int) i <= (int) RID_LAST_MODIFIER)
7202 /* "long", "short", "signed", "unsigned" or "_Complex". */
7203 bool dupe = false;
7204 switch (i)
7206 case RID_LONG:
7207 if (specs->long_long_p)
7209 error ("%<long long long%> is too long for GCC");
7210 break;
7212 if (specs->long_p)
7214 if (specs->typespec_word == cts_double)
7216 error ("both %<long long%> and %<double%> in "
7217 "declaration specifiers");
7218 break;
7220 if (pedantic && !flag_isoc99 && !in_system_header
7221 && warn_long_long)
7222 pedwarn ("ISO C90 does not support %<long long%>");
7223 specs->long_long_p = 1;
7224 break;
7226 if (specs->short_p)
7227 error ("both %<long%> and %<short%> in "
7228 "declaration specifiers");
7229 else if (specs->typespec_word == cts_void)
7230 error ("both %<long%> and %<void%> in "
7231 "declaration specifiers");
7232 else if (specs->typespec_word == cts_bool)
7233 error ("both %<long%> and %<_Bool%> in "
7234 "declaration specifiers");
7235 else if (specs->typespec_word == cts_char)
7236 error ("both %<long%> and %<char%> in "
7237 "declaration specifiers");
7238 else if (specs->typespec_word == cts_float)
7239 error ("both %<long%> and %<float%> in "
7240 "declaration specifiers");
7241 else if (specs->typespec_word == cts_dfloat32)
7242 error ("both %<long%> and %<_Decimal32%> in "
7243 "declaration specifiers");
7244 else if (specs->typespec_word == cts_dfloat64)
7245 error ("both %<long%> and %<_Decimal64%> in "
7246 "declaration specifiers");
7247 else if (specs->typespec_word == cts_dfloat128)
7248 error ("both %<long%> and %<_Decimal128%> in "
7249 "declaration specifiers");
7250 else
7251 specs->long_p = true;
7252 break;
7253 case RID_SHORT:
7254 dupe = specs->short_p;
7255 if (specs->long_p)
7256 error ("both %<long%> and %<short%> in "
7257 "declaration specifiers");
7258 else if (specs->typespec_word == cts_void)
7259 error ("both %<short%> and %<void%> in "
7260 "declaration specifiers");
7261 else if (specs->typespec_word == cts_bool)
7262 error ("both %<short%> and %<_Bool%> in "
7263 "declaration specifiers");
7264 else if (specs->typespec_word == cts_char)
7265 error ("both %<short%> and %<char%> in "
7266 "declaration specifiers");
7267 else if (specs->typespec_word == cts_float)
7268 error ("both %<short%> and %<float%> in "
7269 "declaration specifiers");
7270 else if (specs->typespec_word == cts_double)
7271 error ("both %<short%> and %<double%> in "
7272 "declaration specifiers");
7273 else if (specs->typespec_word == cts_dfloat32)
7274 error ("both %<short%> and %<_Decimal32%> in "
7275 "declaration specifiers");
7276 else if (specs->typespec_word == cts_dfloat64)
7277 error ("both %<short%> and %<_Decimal64%> in "
7278 "declaration specifiers");
7279 else if (specs->typespec_word == cts_dfloat128)
7280 error ("both %<short%> and %<_Decimal128%> in "
7281 "declaration specifiers");
7282 else
7283 specs->short_p = true;
7284 break;
7285 case RID_SIGNED:
7286 dupe = specs->signed_p;
7287 if (specs->unsigned_p)
7288 error ("both %<signed%> and %<unsigned%> in "
7289 "declaration specifiers");
7290 else if (specs->typespec_word == cts_void)
7291 error ("both %<signed%> and %<void%> in "
7292 "declaration specifiers");
7293 else if (specs->typespec_word == cts_bool)
7294 error ("both %<signed%> and %<_Bool%> in "
7295 "declaration specifiers");
7296 else if (specs->typespec_word == cts_float)
7297 error ("both %<signed%> and %<float%> in "
7298 "declaration specifiers");
7299 else if (specs->typespec_word == cts_double)
7300 error ("both %<signed%> and %<double%> in "
7301 "declaration specifiers");
7302 else if (specs->typespec_word == cts_dfloat32)
7303 error ("both %<signed%> and %<_Decimal32%> in "
7304 "declaration specifiers");
7305 else if (specs->typespec_word == cts_dfloat64)
7306 error ("both %<signed%> and %<_Decimal64%> in "
7307 "declaration specifiers");
7308 else if (specs->typespec_word == cts_dfloat128)
7309 error ("both %<signed%> and %<_Decimal128%> in "
7310 "declaration specifiers");
7311 else
7312 specs->signed_p = true;
7313 break;
7314 case RID_UNSIGNED:
7315 dupe = specs->unsigned_p;
7316 if (specs->signed_p)
7317 error ("both %<signed%> and %<unsigned%> in "
7318 "declaration specifiers");
7319 else if (specs->typespec_word == cts_void)
7320 error ("both %<unsigned%> and %<void%> in "
7321 "declaration specifiers");
7322 else if (specs->typespec_word == cts_bool)
7323 error ("both %<unsigned%> and %<_Bool%> in "
7324 "declaration specifiers");
7325 else if (specs->typespec_word == cts_float)
7326 error ("both %<unsigned%> and %<float%> in "
7327 "declaration specifiers");
7328 else if (specs->typespec_word == cts_double)
7329 error ("both %<unsigned%> and %<double%> in "
7330 "declaration specifiers");
7331 else if (specs->typespec_word == cts_dfloat32)
7332 error ("both %<unsigned%> and %<_Decimal32%> in "
7333 "declaration specifiers");
7334 else if (specs->typespec_word == cts_dfloat64)
7335 error ("both %<unsigned%> and %<_Decimal64%> in "
7336 "declaration specifiers");
7337 else if (specs->typespec_word == cts_dfloat128)
7338 error ("both %<unsigned%> and %<_Decimal128%> in "
7339 "declaration specifiers");
7340 else
7341 specs->unsigned_p = true;
7342 break;
7343 case RID_COMPLEX:
7344 dupe = specs->complex_p;
7345 if (pedantic && !flag_isoc99 && !in_system_header)
7346 pedwarn ("ISO C90 does not support complex types");
7347 if (specs->typespec_word == cts_void)
7348 error ("both %<complex%> and %<void%> in "
7349 "declaration specifiers");
7350 else if (specs->typespec_word == cts_bool)
7351 error ("both %<complex%> and %<_Bool%> in "
7352 "declaration specifiers");
7353 else if (specs->typespec_word == cts_dfloat32)
7354 error ("both %<complex%> and %<_Decimal32%> in "
7355 "declaration specifiers");
7356 else if (specs->typespec_word == cts_dfloat64)
7357 error ("both %<complex%> and %<_Decimal64%> in "
7358 "declaration specifiers");
7359 else if (specs->typespec_word == cts_dfloat128)
7360 error ("both %<complex%> and %<_Decimal128%> in "
7361 "declaration specifiers");
7362 else
7363 specs->complex_p = true;
7364 break;
7365 default:
7366 gcc_unreachable ();
7369 if (dupe)
7370 error ("duplicate %qE", type);
7372 return specs;
7374 else
7376 /* "void", "_Bool", "char", "int", "float" or "double". */
7377 if (specs->typespec_word != cts_none)
7379 error ("two or more data types in declaration specifiers");
7380 return specs;
7382 switch (i)
7384 case RID_VOID:
7385 if (specs->long_p)
7386 error ("both %<long%> and %<void%> in "
7387 "declaration specifiers");
7388 else if (specs->short_p)
7389 error ("both %<short%> and %<void%> in "
7390 "declaration specifiers");
7391 else if (specs->signed_p)
7392 error ("both %<signed%> and %<void%> in "
7393 "declaration specifiers");
7394 else if (specs->unsigned_p)
7395 error ("both %<unsigned%> and %<void%> in "
7396 "declaration specifiers");
7397 else if (specs->complex_p)
7398 error ("both %<complex%> and %<void%> in "
7399 "declaration specifiers");
7400 else
7401 specs->typespec_word = cts_void;
7402 return specs;
7403 case RID_BOOL:
7404 if (specs->long_p)
7405 error ("both %<long%> and %<_Bool%> in "
7406 "declaration specifiers");
7407 else if (specs->short_p)
7408 error ("both %<short%> and %<_Bool%> in "
7409 "declaration specifiers");
7410 else if (specs->signed_p)
7411 error ("both %<signed%> and %<_Bool%> in "
7412 "declaration specifiers");
7413 else if (specs->unsigned_p)
7414 error ("both %<unsigned%> and %<_Bool%> in "
7415 "declaration specifiers");
7416 else if (specs->complex_p)
7417 error ("both %<complex%> and %<_Bool%> in "
7418 "declaration specifiers");
7419 else
7420 specs->typespec_word = cts_bool;
7421 return specs;
7422 case RID_CHAR:
7423 if (specs->long_p)
7424 error ("both %<long%> and %<char%> in "
7425 "declaration specifiers");
7426 else if (specs->short_p)
7427 error ("both %<short%> and %<char%> in "
7428 "declaration specifiers");
7429 else
7430 specs->typespec_word = cts_char;
7431 return specs;
7432 case RID_INT:
7433 specs->typespec_word = cts_int;
7434 return specs;
7435 case RID_FLOAT:
7436 if (specs->long_p)
7437 error ("both %<long%> and %<float%> in "
7438 "declaration specifiers");
7439 else if (specs->short_p)
7440 error ("both %<short%> and %<float%> in "
7441 "declaration specifiers");
7442 else if (specs->signed_p)
7443 error ("both %<signed%> and %<float%> in "
7444 "declaration specifiers");
7445 else if (specs->unsigned_p)
7446 error ("both %<unsigned%> and %<float%> in "
7447 "declaration specifiers");
7448 else
7449 specs->typespec_word = cts_float;
7450 return specs;
7451 case RID_DOUBLE:
7452 if (specs->long_long_p)
7453 error ("both %<long long%> and %<double%> in "
7454 "declaration specifiers");
7455 else if (specs->short_p)
7456 error ("both %<short%> and %<double%> in "
7457 "declaration specifiers");
7458 else if (specs->signed_p)
7459 error ("both %<signed%> and %<double%> in "
7460 "declaration specifiers");
7461 else if (specs->unsigned_p)
7462 error ("both %<unsigned%> and %<double%> in "
7463 "declaration specifiers");
7464 else
7465 specs->typespec_word = cts_double;
7466 return specs;
7467 case RID_DFLOAT32:
7468 case RID_DFLOAT64:
7469 case RID_DFLOAT128:
7471 const char *str;
7472 if (i == RID_DFLOAT32)
7473 str = "_Decimal32";
7474 else if (i == RID_DFLOAT64)
7475 str = "_Decimal64";
7476 else
7477 str = "_Decimal128";
7478 if (specs->long_long_p)
7479 error ("both %<long long%> and %<%s%> in "
7480 "declaration specifiers", str);
7481 if (specs->long_p)
7482 error ("both %<long%> and %<%s%> in "
7483 "declaration specifiers", str);
7484 else if (specs->short_p)
7485 error ("both %<short%> and %<%s%> in "
7486 "declaration specifiers", str);
7487 else if (specs->signed_p)
7488 error ("both %<signed%> and %<%s%> in "
7489 "declaration specifiers", str);
7490 else if (specs->unsigned_p)
7491 error ("both %<unsigned%> and %<%s%> in "
7492 "declaration specifiers", str);
7493 else if (specs->complex_p)
7494 error ("both %<complex%> and %<%s%> in "
7495 "declaration specifiers", str);
7496 else if (i == RID_DFLOAT32)
7497 specs->typespec_word = cts_dfloat32;
7498 else if (i == RID_DFLOAT64)
7499 specs->typespec_word = cts_dfloat64;
7500 else
7501 specs->typespec_word = cts_dfloat128;
7503 if (!targetm.decimal_float_supported_p ())
7504 error ("decimal floating point not supported for this target");
7505 if (pedantic)
7506 pedwarn ("ISO C does not support decimal floating point");
7507 return specs;
7508 default:
7509 /* ObjC reserved word "id", handled below. */
7510 break;
7515 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7516 form of ObjC type, cases such as "int" and "long" being handled
7517 above), a TYPE (struct, union, enum and typeof specifiers) or an
7518 ERROR_MARK. In none of these cases may there have previously
7519 been any type specifiers. */
7520 if (specs->type || specs->typespec_word != cts_none
7521 || specs->long_p || specs->short_p || specs->signed_p
7522 || specs->unsigned_p || specs->complex_p)
7523 error ("two or more data types in declaration specifiers");
7524 else if (TREE_CODE (type) == TYPE_DECL)
7526 if (TREE_TYPE (type) == error_mark_node)
7527 ; /* Allow the type to default to int to avoid cascading errors. */
7528 else
7530 specs->type = TREE_TYPE (type);
7531 specs->decl_attr = DECL_ATTRIBUTES (type);
7532 specs->typedef_p = true;
7533 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7536 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7538 tree t = lookup_name (type);
7539 if (!t || TREE_CODE (t) != TYPE_DECL)
7540 error ("%qE fails to be a typedef or built in type", type);
7541 else if (TREE_TYPE (t) == error_mark_node)
7543 else
7544 specs->type = TREE_TYPE (t);
7546 else if (TREE_CODE (type) != ERROR_MARK)
7548 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7549 specs->tag_defined_p = true;
7550 if (spec.kind == ctsk_typeof)
7551 specs->typedef_p = true;
7552 specs->type = type;
7555 return specs;
7558 /* Add the storage class specifier or function specifier SCSPEC to the
7559 declaration specifiers SPECS, returning SPECS. */
7561 struct c_declspecs *
7562 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7564 enum rid i;
7565 enum c_storage_class n = csc_none;
7566 bool dupe = false;
7567 specs->declspecs_seen_p = true;
7568 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7569 && C_IS_RESERVED_WORD (scspec));
7570 i = C_RID_CODE (scspec);
7571 if (specs->non_sc_seen_p)
7572 warning (OPT_Wold_style_declaration,
7573 "%qE is not at beginning of declaration", scspec);
7574 switch (i)
7576 case RID_INLINE:
7577 /* C99 permits duplicate inline. Although of doubtful utility,
7578 it seems simplest to permit it in gnu89 mode as well, as
7579 there is also little utility in maintaining this as a
7580 difference between gnu89 and C99 inline. */
7581 dupe = false;
7582 specs->inline_p = true;
7583 break;
7584 case RID_THREAD:
7585 dupe = specs->thread_p;
7586 if (specs->storage_class == csc_auto)
7587 error ("%<__thread%> used with %<auto%>");
7588 else if (specs->storage_class == csc_register)
7589 error ("%<__thread%> used with %<register%>");
7590 else if (specs->storage_class == csc_typedef)
7591 error ("%<__thread%> used with %<typedef%>");
7592 else
7593 specs->thread_p = true;
7594 break;
7595 case RID_AUTO:
7596 n = csc_auto;
7597 break;
7598 case RID_EXTERN:
7599 n = csc_extern;
7600 /* Diagnose "__thread extern". */
7601 if (specs->thread_p)
7602 error ("%<__thread%> before %<extern%>");
7603 break;
7604 case RID_REGISTER:
7605 n = csc_register;
7606 break;
7607 case RID_STATIC:
7608 n = csc_static;
7609 /* Diagnose "__thread static". */
7610 if (specs->thread_p)
7611 error ("%<__thread%> before %<static%>");
7612 break;
7613 case RID_TYPEDEF:
7614 n = csc_typedef;
7615 break;
7616 default:
7617 gcc_unreachable ();
7619 if (n != csc_none && n == specs->storage_class)
7620 dupe = true;
7621 if (dupe)
7622 error ("duplicate %qE", scspec);
7623 if (n != csc_none)
7625 if (specs->storage_class != csc_none && n != specs->storage_class)
7627 error ("multiple storage classes in declaration specifiers");
7629 else
7631 specs->storage_class = n;
7632 if (n != csc_extern && n != csc_static && specs->thread_p)
7634 error ("%<__thread%> used with %qE", scspec);
7635 specs->thread_p = false;
7639 return specs;
7642 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7643 returning SPECS. */
7645 struct c_declspecs *
7646 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7648 specs->attrs = chainon (attrs, specs->attrs);
7649 specs->declspecs_seen_p = true;
7650 return specs;
7653 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7654 specifiers with any other type specifier to determine the resulting
7655 type. This is where ISO C checks on complex types are made, since
7656 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7657 double". */
7659 struct c_declspecs *
7660 finish_declspecs (struct c_declspecs *specs)
7662 /* If a type was specified as a whole, we have no modifiers and are
7663 done. */
7664 if (specs->type != NULL_TREE)
7666 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7667 && !specs->signed_p && !specs->unsigned_p
7668 && !specs->complex_p);
7669 return specs;
7672 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7673 has been specified, treat it as "int" unless "_Complex" is
7674 present and there are no other specifiers. If we just have
7675 "_Complex", it is equivalent to "_Complex double", but e.g.
7676 "_Complex short" is equivalent to "_Complex short int". */
7677 if (specs->typespec_word == cts_none)
7679 if (specs->long_p || specs->short_p
7680 || specs->signed_p || specs->unsigned_p)
7682 specs->typespec_word = cts_int;
7684 else if (specs->complex_p)
7686 specs->typespec_word = cts_double;
7687 if (pedantic)
7688 pedwarn ("ISO C does not support plain %<complex%> meaning "
7689 "%<double complex%>");
7691 else
7693 specs->typespec_word = cts_int;
7694 specs->default_int_p = true;
7695 /* We don't diagnose this here because grokdeclarator will
7696 give more specific diagnostics according to whether it is
7697 a function definition. */
7701 /* If "signed" was specified, record this to distinguish "int" and
7702 "signed int" in the case of a bit-field with
7703 -funsigned-bitfields. */
7704 specs->explicit_signed_p = specs->signed_p;
7706 /* Now compute the actual type. */
7707 switch (specs->typespec_word)
7709 case cts_void:
7710 gcc_assert (!specs->long_p && !specs->short_p
7711 && !specs->signed_p && !specs->unsigned_p
7712 && !specs->complex_p);
7713 specs->type = void_type_node;
7714 break;
7715 case cts_bool:
7716 gcc_assert (!specs->long_p && !specs->short_p
7717 && !specs->signed_p && !specs->unsigned_p
7718 && !specs->complex_p);
7719 specs->type = boolean_type_node;
7720 break;
7721 case cts_char:
7722 gcc_assert (!specs->long_p && !specs->short_p);
7723 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7724 if (specs->signed_p)
7725 specs->type = signed_char_type_node;
7726 else if (specs->unsigned_p)
7727 specs->type = unsigned_char_type_node;
7728 else
7729 specs->type = char_type_node;
7730 if (specs->complex_p)
7732 if (pedantic)
7733 pedwarn ("ISO C does not support complex integer types");
7734 specs->type = build_complex_type (specs->type);
7736 break;
7737 case cts_int:
7738 gcc_assert (!(specs->long_p && specs->short_p));
7739 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7740 if (specs->long_long_p)
7741 specs->type = (specs->unsigned_p
7742 ? long_long_unsigned_type_node
7743 : long_long_integer_type_node);
7744 else if (specs->long_p)
7745 specs->type = (specs->unsigned_p
7746 ? long_unsigned_type_node
7747 : long_integer_type_node);
7748 else if (specs->short_p)
7749 specs->type = (specs->unsigned_p
7750 ? short_unsigned_type_node
7751 : short_integer_type_node);
7752 else
7753 specs->type = (specs->unsigned_p
7754 ? unsigned_type_node
7755 : integer_type_node);
7756 if (specs->complex_p)
7758 if (pedantic)
7759 pedwarn ("ISO C does not support complex integer types");
7760 specs->type = build_complex_type (specs->type);
7762 break;
7763 case cts_float:
7764 gcc_assert (!specs->long_p && !specs->short_p
7765 && !specs->signed_p && !specs->unsigned_p);
7766 specs->type = (specs->complex_p
7767 ? complex_float_type_node
7768 : float_type_node);
7769 break;
7770 case cts_double:
7771 gcc_assert (!specs->long_long_p && !specs->short_p
7772 && !specs->signed_p && !specs->unsigned_p);
7773 if (specs->long_p)
7775 specs->type = (specs->complex_p
7776 ? complex_long_double_type_node
7777 : long_double_type_node);
7779 else
7781 specs->type = (specs->complex_p
7782 ? complex_double_type_node
7783 : double_type_node);
7785 break;
7786 case cts_dfloat32:
7787 case cts_dfloat64:
7788 case cts_dfloat128:
7789 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7790 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7791 if (specs->typespec_word == cts_dfloat32)
7792 specs->type = dfloat32_type_node;
7793 else if (specs->typespec_word == cts_dfloat64)
7794 specs->type = dfloat64_type_node;
7795 else
7796 specs->type = dfloat128_type_node;
7797 break;
7798 default:
7799 gcc_unreachable ();
7802 return specs;
7805 /* A subroutine of c_write_global_declarations. Perform final processing
7806 on one file scope's declarations (or the external scope's declarations),
7807 GLOBALS. */
7809 static void
7810 c_write_global_declarations_1 (tree globals)
7812 tree decl;
7813 bool reconsider;
7815 /* Process the decls in the order they were written. */
7816 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7818 /* Check for used but undefined static functions using the C
7819 standard's definition of "used", and set TREE_NO_WARNING so
7820 that check_global_declarations doesn't repeat the check. */
7821 if (TREE_CODE (decl) == FUNCTION_DECL
7822 && DECL_INITIAL (decl) == 0
7823 && DECL_EXTERNAL (decl)
7824 && !TREE_PUBLIC (decl)
7825 && C_DECL_USED (decl))
7827 pedwarn ("%q+F used but never defined", decl);
7828 TREE_NO_WARNING (decl) = 1;
7831 wrapup_global_declaration_1 (decl);
7836 reconsider = false;
7837 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7838 reconsider |= wrapup_global_declaration_2 (decl);
7840 while (reconsider);
7842 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7843 check_global_declaration_1 (decl);
7846 /* A subroutine of c_write_global_declarations Emit debug information for each
7847 of the declarations in GLOBALS. */
7849 static void
7850 c_write_global_declarations_2 (tree globals)
7852 tree decl;
7854 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
7855 debug_hooks->global_decl (decl);
7858 /* Preserve the external declarations scope across a garbage collect. */
7859 static GTY(()) tree ext_block;
7861 void
7862 c_write_global_declarations (void)
7864 tree t;
7866 /* We don't want to do this if generating a PCH. */
7867 if (pch_file)
7868 return;
7870 /* Don't waste time on further processing if -fsyntax-only or we've
7871 encountered errors. */
7872 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7873 return;
7875 /* Close the external scope. */
7876 ext_block = pop_scope ();
7877 external_scope = 0;
7878 gcc_assert (!current_scope);
7880 if (ext_block)
7882 tree tmp = BLOCK_VARS (ext_block);
7883 int flags;
7884 FILE * stream = dump_begin (TDI_tu, &flags);
7885 if (stream && tmp)
7887 dump_node (tmp, flags & ~TDF_SLIM, stream);
7888 dump_end (TDI_tu, stream);
7892 /* Process all file scopes in this compilation, and the external_scope,
7893 through wrapup_global_declarations and check_global_declarations. */
7894 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7895 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7896 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7898 /* Call functions declared with the "constructor" or "destructor"
7899 attribute. */
7900 c_build_cdtor_fns ();
7902 /* We're done parsing; proceed to optimize and emit assembly.
7903 FIXME: shouldn't be the front end's responsibility to call this. */
7904 cgraph_optimize ();
7906 /* After cgraph has had a chance to emit everything that's going to
7907 be emitted, output debug information for globals. */
7908 if (errorcount == 0 && sorrycount == 0)
7910 timevar_push (TV_SYMOUT);
7911 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7912 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
7913 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
7914 timevar_pop (TV_SYMOUT);
7917 ext_block = NULL;
7920 #include "gt-c-decl.h"