gcc/
[official-gcc/alias-decl.git] / gcc / c-decl.c
blob88bfa25008e2077eede449ec146a9ffbdaa5dbb4
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, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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 "gimple.h"
56 #include "tree-iterator.h"
57 #include "diagnostic.h"
58 #include "tree-dump.h"
59 #include "cgraph.h"
60 #include "hashtab.h"
61 #include "libfuncs.h"
62 #include "except.h"
63 #include "langhooks-def.h"
64 #include "pointer-set.h"
65 #include "gimple.h"
66 #include "plugin.h"
68 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
69 enum decl_context
70 { NORMAL, /* Ordinary declaration */
71 FUNCDEF, /* Function definition */
72 PARM, /* Declaration of parm before function body */
73 FIELD, /* Declaration inside struct or union */
74 TYPENAME}; /* Typename (inside cast or sizeof) */
76 /* States indicating how grokdeclarator() should handle declspecs marked
77 with __attribute__((deprecated)). An object declared as
78 __attribute__((deprecated)) suppresses warnings of uses of other
79 deprecated items. */
81 enum deprecated_states {
82 DEPRECATED_NORMAL,
83 DEPRECATED_SUPPRESS
87 /* Nonzero if we have seen an invalid cross reference
88 to a struct, union, or enum, but not yet printed the message. */
89 tree pending_invalid_xref;
91 /* File and line to appear in the eventual error message. */
92 location_t pending_invalid_xref_location;
94 /* True means we've initialized exception handling. */
95 bool c_eh_initialized_p;
97 /* The file and line that the prototype came from if this is an
98 old-style definition; used for diagnostics in
99 store_parm_decls_oldstyle. */
101 static location_t current_function_prototype_locus;
103 /* Whether this prototype was built-in. */
105 static bool current_function_prototype_built_in;
107 /* The argument type information of this prototype. */
109 static tree current_function_prototype_arg_types;
111 /* The argument information structure for the function currently being
112 defined. */
114 static struct c_arg_info *current_function_arg_info;
116 /* The obstack on which parser and related data structures, which are
117 not live beyond their top-level declaration or definition, are
118 allocated. */
119 struct obstack parser_obstack;
121 /* The current statement tree. */
123 static GTY(()) struct stmt_tree_s c_stmt_tree;
125 /* State saving variables. */
126 tree c_break_label;
127 tree c_cont_label;
129 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
130 included in this invocation. Note that the current translation
131 unit is not included in this list. */
133 static GTY(()) tree all_translation_units;
135 /* A list of decls to be made automatically visible in each file scope. */
136 static GTY(()) tree visible_builtins;
138 /* Set to 0 at beginning of a function definition, set to 1 if
139 a return statement that specifies a return value is seen. */
141 int current_function_returns_value;
143 /* Set to 0 at beginning of a function definition, set to 1 if
144 a return statement with no argument is seen. */
146 int current_function_returns_null;
148 /* Set to 0 at beginning of a function definition, set to 1 if
149 a call to a noreturn function is seen. */
151 int current_function_returns_abnormally;
153 /* Set to nonzero by `grokdeclarator' for a function
154 whose return type is defaulted, if warnings for this are desired. */
156 static int warn_about_return_type;
158 /* Nonzero when the current toplevel function contains a declaration
159 of a nested function which is never defined. */
161 static bool undef_nested_function;
163 /* True means global_bindings_p should return false even if the scope stack
164 says we are in file scope. */
165 bool c_override_global_bindings_to_false;
168 /* Each c_binding structure describes one binding of an identifier to
169 a decl. All the decls in a scope - irrespective of namespace - are
170 chained together by the ->prev field, which (as the name implies)
171 runs in reverse order. All the decls in a given namespace bound to
172 a given identifier are chained by the ->shadowed field, which runs
173 from inner to outer scopes.
175 The ->decl field usually points to a DECL node, but there are two
176 exceptions. In the namespace of type tags, the bound entity is a
177 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
178 identifier is encountered, it is bound to error_mark_node to
179 suppress further errors about that identifier in the current
180 function.
182 The ->type field stores the type of the declaration in this scope;
183 if NULL, the type is the type of the ->decl field. This is only of
184 relevance for objects with external or internal linkage which may
185 be redeclared in inner scopes, forming composite types that only
186 persist for the duration of those scopes. In the external scope,
187 this stores the composite of all the types declared for this
188 object, visible or not. The ->inner_comp field (used only at file
189 scope) stores whether an incomplete array type at file scope was
190 completed at an inner scope to an array size other than 1.
192 The depth field is copied from the scope structure that holds this
193 decl. It is used to preserve the proper ordering of the ->shadowed
194 field (see bind()) and also for a handful of special-case checks.
195 Finally, the invisible bit is true for a decl which should be
196 ignored for purposes of normal name lookup, and the nested bit is
197 true for a decl that's been bound a second time in an inner scope;
198 in all such cases, the binding in the outer scope will have its
199 invisible bit true. */
201 struct GTY((chain_next ("%h.prev"))) c_binding {
202 tree decl; /* the decl bound */
203 tree type; /* the type in this scope */
204 tree id; /* the identifier it's bound to */
205 struct c_binding *prev; /* the previous decl in this scope */
206 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
207 unsigned int depth : 28; /* depth of this scope */
208 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
209 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
210 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
211 /* one free bit */
212 location_t locus; /* location for nested bindings */
214 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
215 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
216 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
217 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
219 #define I_SYMBOL_BINDING(node) \
220 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
221 #define I_SYMBOL_DECL(node) \
222 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
224 #define I_TAG_BINDING(node) \
225 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
226 #define I_TAG_DECL(node) \
227 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
229 #define I_LABEL_BINDING(node) \
230 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
231 #define I_LABEL_DECL(node) \
232 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
234 /* Each C symbol points to three linked lists of c_binding structures.
235 These describe the values of the identifier in the three different
236 namespaces defined by the language. */
238 struct GTY(()) lang_identifier {
239 struct c_common_identifier common_id;
240 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
241 struct c_binding *tag_binding; /* struct/union/enum tags */
242 struct c_binding *label_binding; /* labels */
245 /* Validate c-lang.c's assumptions. */
246 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
247 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
249 /* The resulting tree type. */
251 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
252 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : ((union lang_tree_node *) TREE_CHAIN (&%h.generic))"))) lang_tree_node
254 union tree_node GTY ((tag ("0"),
255 desc ("tree_node_structure (&%h)")))
256 generic;
257 struct lang_identifier GTY ((tag ("1"))) identifier;
260 /* Each c_scope structure describes the complete contents of one
261 scope. Four scopes are distinguished specially: the innermost or
262 current scope, the innermost function scope, the file scope (always
263 the second to outermost) and the outermost or external scope.
265 Most declarations are recorded in the current scope.
267 All normal label declarations are recorded in the innermost
268 function scope, as are bindings of undeclared identifiers to
269 error_mark_node. (GCC permits nested functions as an extension,
270 hence the 'innermost' qualifier.) Explicitly declared labels
271 (using the __label__ extension) appear in the current scope.
273 Being in the file scope (current_scope == file_scope) causes
274 special behavior in several places below. Also, under some
275 conditions the Objective-C front end records declarations in the
276 file scope even though that isn't the current scope.
278 All declarations with external linkage are recorded in the external
279 scope, even if they aren't visible there; this models the fact that
280 such declarations are visible to the entire program, and (with a
281 bit of cleverness, see pushdecl) allows diagnosis of some violations
282 of C99 6.2.2p7 and 6.2.7p2:
284 If, within the same translation unit, the same identifier appears
285 with both internal and external linkage, the behavior is
286 undefined.
288 All declarations that refer to the same object or function shall
289 have compatible type; otherwise, the behavior is undefined.
291 Initially only the built-in declarations, which describe compiler
292 intrinsic functions plus a subset of the standard library, are in
293 this scope.
295 The order of the blocks list matters, and it is frequently appended
296 to. To avoid having to walk all the way to the end of the list on
297 each insertion, or reverse the list later, we maintain a pointer to
298 the last list entry. (FIXME: It should be feasible to use a reversed
299 list here.)
301 The bindings list is strictly in reverse order of declarations;
302 pop_scope relies on this. */
305 struct GTY((chain_next ("%h.outer"))) c_scope {
306 /* The scope containing this one. */
307 struct c_scope *outer;
309 /* The next outermost function scope. */
310 struct c_scope *outer_function;
312 /* All bindings in this scope. */
313 struct c_binding *bindings;
315 /* For each scope (except the global one), a chain of BLOCK nodes
316 for all the scopes that were entered and exited one level down. */
317 tree blocks;
318 tree blocks_last;
320 /* The depth of this scope. Used to keep the ->shadowed chain of
321 bindings sorted innermost to outermost. */
322 unsigned int depth : 28;
324 /* True if we are currently filling this scope with parameter
325 declarations. */
326 BOOL_BITFIELD parm_flag : 1;
328 /* True if we saw [*] in this scope. Used to give an error messages
329 if these appears in a function definition. */
330 BOOL_BITFIELD had_vla_unspec : 1;
332 /* True if we already complained about forward parameter decls
333 in this scope. This prevents double warnings on
334 foo (int a; int b; ...) */
335 BOOL_BITFIELD warned_forward_parm_decls : 1;
337 /* True if this is the outermost block scope of a function body.
338 This scope contains the parameters, the local variables declared
339 in the outermost block, and all the labels (except those in
340 nested functions, or declared at block scope with __label__). */
341 BOOL_BITFIELD function_body : 1;
343 /* True means make a BLOCK for this scope no matter what. */
344 BOOL_BITFIELD keep : 1;
347 /* The scope currently in effect. */
349 static GTY(()) struct c_scope *current_scope;
351 /* The innermost function scope. Ordinary (not explicitly declared)
352 labels, bindings to error_mark_node, and the lazily-created
353 bindings of __func__ and its friends get this scope. */
355 static GTY(()) struct c_scope *current_function_scope;
357 /* The C file scope. This is reset for each input translation unit. */
359 static GTY(()) struct c_scope *file_scope;
361 /* The outermost scope. This is used for all declarations with
362 external linkage, and only these, hence the name. */
364 static GTY(()) struct c_scope *external_scope;
366 /* A chain of c_scope structures awaiting reuse. */
368 static GTY((deletable)) struct c_scope *scope_freelist;
370 /* A chain of c_binding structures awaiting reuse. */
372 static GTY((deletable)) struct c_binding *binding_freelist;
374 /* Append VAR to LIST in scope SCOPE. */
375 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
376 struct c_scope *s_ = (scope); \
377 tree d_ = (decl); \
378 if (s_->list##_last) \
379 BLOCK_CHAIN (s_->list##_last) = d_; \
380 else \
381 s_->list = d_; \
382 s_->list##_last = d_; \
383 } while (0)
385 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
386 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
387 struct c_scope *t_ = (tscope); \
388 struct c_scope *f_ = (fscope); \
389 if (t_->to##_last) \
390 BLOCK_CHAIN (t_->to##_last) = f_->from; \
391 else \
392 t_->to = f_->from; \
393 t_->to##_last = f_->from##_last; \
394 } while (0)
396 /* A c_inline_static structure stores details of a static identifier
397 referenced in a definition of a function that may be an inline
398 definition if no subsequent declaration of that function uses
399 "extern" or does not use "inline". */
401 struct GTY((chain_next ("%h.next"))) c_inline_static {
402 /* The location for a diagnostic. */
403 location_t location;
405 /* The function that may be an inline definition. */
406 tree function;
408 /* The object or function referenced. */
409 tree static_decl;
411 /* What sort of reference this is. */
412 enum c_inline_static_type type;
414 /* The next such structure or NULL. */
415 struct c_inline_static *next;
418 /* List of static identifiers used or referenced in functions that may
419 be inline definitions. */
420 static GTY(()) struct c_inline_static *c_inline_statics;
422 /* True means unconditionally make a BLOCK for the next scope pushed. */
424 static bool keep_next_level_flag;
426 /* True means the next call to push_scope will be the outermost scope
427 of a function body, so do not push a new scope, merely cease
428 expecting parameter decls. */
430 static bool next_is_function_body;
432 /* Forward declarations. */
433 static tree lookup_name_in_scope (tree, struct c_scope *);
434 static tree c_make_fname_decl (tree, int);
435 static tree grokdeclarator (const struct c_declarator *,
436 struct c_declspecs *,
437 enum decl_context, bool, tree *, tree *, tree *,
438 bool *, enum deprecated_states);
439 static tree grokparms (struct c_arg_info *, bool);
440 static void layout_array_type (tree);
442 /* T is a statement. Add it to the statement-tree. This is the
443 C/ObjC version--C++ has a slightly different version of this
444 function. */
446 tree
447 add_stmt (tree t)
449 enum tree_code code = TREE_CODE (t);
451 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
453 if (!EXPR_HAS_LOCATION (t))
454 SET_EXPR_LOCATION (t, input_location);
457 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
458 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
460 /* Add T to the statement-tree. Non-side-effect statements need to be
461 recorded during statement expressions. */
462 append_to_statement_list_force (t, &cur_stmt_list);
464 return t;
468 void
469 c_print_identifier (FILE *file, tree node, int indent)
471 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
472 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
473 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
474 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
476 tree rid = ridpointers[C_RID_CODE (node)];
477 indent_to (file, indent + 4);
478 fprintf (file, "rid %p \"%s\"",
479 (void *) rid, IDENTIFIER_POINTER (rid));
483 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
484 which may be any of several kinds of DECL or TYPE or error_mark_node,
485 in the scope SCOPE. */
486 static void
487 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
488 bool nested, location_t locus)
490 struct c_binding *b, **here;
492 if (binding_freelist)
494 b = binding_freelist;
495 binding_freelist = b->prev;
497 else
498 b = GGC_NEW (struct c_binding);
500 b->shadowed = 0;
501 b->decl = decl;
502 b->id = name;
503 b->depth = scope->depth;
504 b->invisible = invisible;
505 b->nested = nested;
506 b->inner_comp = 0;
507 b->locus = locus;
509 b->type = 0;
511 b->prev = scope->bindings;
512 scope->bindings = b;
514 if (!name)
515 return;
517 switch (TREE_CODE (decl))
519 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
520 case ENUMERAL_TYPE:
521 case UNION_TYPE:
522 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
523 case VAR_DECL:
524 case FUNCTION_DECL:
525 case TYPE_DECL:
526 case CONST_DECL:
527 case PARM_DECL:
528 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
530 default:
531 gcc_unreachable ();
534 /* Locate the appropriate place in the chain of shadowed decls
535 to insert this binding. Normally, scope == current_scope and
536 this does nothing. */
537 while (*here && (*here)->depth > scope->depth)
538 here = &(*here)->shadowed;
540 b->shadowed = *here;
541 *here = b;
544 /* Clear the binding structure B, stick it on the binding_freelist,
545 and return the former value of b->prev. This is used by pop_scope
546 and get_parm_info to iterate destructively over all the bindings
547 from a given scope. */
548 static struct c_binding *
549 free_binding_and_advance (struct c_binding *b)
551 struct c_binding *prev = b->prev;
553 memset (b, 0, sizeof (struct c_binding));
554 b->prev = binding_freelist;
555 binding_freelist = b;
557 return prev;
561 /* Hook called at end of compilation to assume 1 elt
562 for a file-scope tentative array defn that wasn't complete before. */
564 void
565 c_finish_incomplete_decl (tree decl)
567 if (TREE_CODE (decl) == VAR_DECL)
569 tree type = TREE_TYPE (decl);
570 if (type != error_mark_node
571 && TREE_CODE (type) == ARRAY_TYPE
572 && !DECL_EXTERNAL (decl)
573 && TYPE_DOMAIN (type) == 0)
575 warning (0, "array %q+D assumed to have one element", decl);
577 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
579 layout_decl (decl, 0);
584 /* Record that inline function FUNC contains a reference (location
585 LOC) to static DECL (file-scope or function-local according to
586 TYPE). */
588 void
589 record_inline_static (location_t loc, tree func, tree decl,
590 enum c_inline_static_type type)
592 struct c_inline_static *csi = GGC_NEW (struct c_inline_static);
593 csi->location = loc;
594 csi->function = func;
595 csi->static_decl = decl;
596 csi->type = type;
597 csi->next = c_inline_statics;
598 c_inline_statics = csi;
601 /* Check for references to static declarations in inline functions at
602 the end of the translation unit and diagnose them if the functions
603 are still inline definitions. */
605 static void
606 check_inline_statics (void)
608 struct c_inline_static *csi;
609 for (csi = c_inline_statics; csi; csi = csi->next)
611 if (DECL_EXTERNAL (csi->function))
612 switch (csi->type)
614 case csi_internal:
615 pedwarn (csi->location, 0,
616 "%qD is static but used in inline function %qD "
617 "which is not static", csi->static_decl, csi->function);
618 break;
619 case csi_modifiable:
620 pedwarn (csi->location, 0,
621 "%q+D is static but declared in inline function %qD "
622 "which is not static", csi->static_decl, csi->function);
623 break;
624 default:
625 gcc_unreachable ();
628 c_inline_statics = NULL;
631 /* The Objective-C front-end often needs to determine the current scope. */
633 void *
634 objc_get_current_scope (void)
636 return current_scope;
639 /* The following function is used only by Objective-C. It needs to live here
640 because it accesses the innards of c_scope. */
642 void
643 objc_mark_locals_volatile (void *enclosing_blk)
645 struct c_scope *scope;
646 struct c_binding *b;
648 for (scope = current_scope;
649 scope && scope != enclosing_blk;
650 scope = scope->outer)
652 for (b = scope->bindings; b; b = b->prev)
653 objc_volatilize_decl (b->decl);
655 /* Do not climb up past the current function. */
656 if (scope->function_body)
657 break;
661 /* Nonzero if we are currently in file scope. */
664 global_bindings_p (void)
666 return (current_scope == file_scope && !c_override_global_bindings_to_false
667 ? -1
668 : 0);
671 void
672 keep_next_level (void)
674 keep_next_level_flag = true;
677 /* Identify this scope as currently being filled with parameters. */
679 void
680 declare_parm_level (void)
682 current_scope->parm_flag = true;
685 void
686 push_scope (void)
688 if (next_is_function_body)
690 /* This is the transition from the parameters to the top level
691 of the function body. These are the same scope
692 (C99 6.2.1p4,6) so we do not push another scope structure.
693 next_is_function_body is set only by store_parm_decls, which
694 in turn is called when and only when we are about to
695 encounter the opening curly brace for the function body.
697 The outermost block of a function always gets a BLOCK node,
698 because the debugging output routines expect that each
699 function has at least one BLOCK. */
700 current_scope->parm_flag = false;
701 current_scope->function_body = true;
702 current_scope->keep = true;
703 current_scope->outer_function = current_function_scope;
704 current_function_scope = current_scope;
706 keep_next_level_flag = false;
707 next_is_function_body = false;
709 else
711 struct c_scope *scope;
712 if (scope_freelist)
714 scope = scope_freelist;
715 scope_freelist = scope->outer;
717 else
718 scope = GGC_CNEW (struct c_scope);
720 scope->keep = keep_next_level_flag;
721 scope->outer = current_scope;
722 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
724 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
725 possible. */
726 if (current_scope && scope->depth == 0)
728 scope->depth--;
729 sorry ("GCC supports only %u nested scopes", scope->depth);
732 current_scope = scope;
733 keep_next_level_flag = false;
737 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
739 static void
740 set_type_context (tree type, tree context)
742 for (type = TYPE_MAIN_VARIANT (type); type;
743 type = TYPE_NEXT_VARIANT (type))
744 TYPE_CONTEXT (type) = context;
747 /* Exit a scope. Restore the state of the identifier-decl mappings
748 that were in effect when this scope was entered. Return a BLOCK
749 node containing all the DECLs in this scope that are of interest
750 to debug info generation. */
752 tree
753 pop_scope (void)
755 struct c_scope *scope = current_scope;
756 tree block, context, p;
757 struct c_binding *b;
759 bool functionbody = scope->function_body;
760 bool keep = functionbody || scope->keep || scope->bindings;
762 c_end_vm_scope (scope->depth);
764 /* If appropriate, create a BLOCK to record the decls for the life
765 of this function. */
766 block = 0;
767 if (keep)
769 block = make_node (BLOCK);
770 BLOCK_SUBBLOCKS (block) = scope->blocks;
771 TREE_USED (block) = 1;
773 /* In each subblock, record that this is its superior. */
774 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
775 BLOCK_SUPERCONTEXT (p) = block;
777 BLOCK_VARS (block) = 0;
780 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
781 scope must be set so that they point to the appropriate
782 construct, i.e. either to the current FUNCTION_DECL node, or
783 else to the BLOCK node we just constructed.
785 Note that for tagged types whose scope is just the formal
786 parameter list for some function type specification, we can't
787 properly set their TYPE_CONTEXTs here, because we don't have a
788 pointer to the appropriate FUNCTION_TYPE node readily available
789 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
790 type nodes get set in `grokdeclarator' as soon as we have created
791 the FUNCTION_TYPE node which will represent the "scope" for these
792 "parameter list local" tagged types. */
793 if (scope->function_body)
794 context = current_function_decl;
795 else if (scope == file_scope)
797 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
798 TREE_CHAIN (file_decl) = all_translation_units;
799 all_translation_units = file_decl;
800 context = file_decl;
802 else
803 context = block;
805 /* Clear all bindings in this scope. */
806 for (b = scope->bindings; b; b = free_binding_and_advance (b))
808 p = b->decl;
809 switch (TREE_CODE (p))
811 case LABEL_DECL:
812 /* Warnings for unused labels, errors for undefined labels. */
813 if (TREE_USED (p) && !DECL_INITIAL (p))
815 error ("label %q+D used but not defined", p);
816 DECL_INITIAL (p) = error_mark_node;
818 else
819 warn_for_unused_label (p);
821 /* Labels go in BLOCK_VARS. */
822 TREE_CHAIN (p) = BLOCK_VARS (block);
823 BLOCK_VARS (block) = p;
824 gcc_assert (I_LABEL_BINDING (b->id) == b);
825 I_LABEL_BINDING (b->id) = b->shadowed;
826 break;
828 case ENUMERAL_TYPE:
829 case UNION_TYPE:
830 case RECORD_TYPE:
831 set_type_context (p, context);
833 /* Types may not have tag-names, in which case the type
834 appears in the bindings list with b->id NULL. */
835 if (b->id)
837 gcc_assert (I_TAG_BINDING (b->id) == b);
838 I_TAG_BINDING (b->id) = b->shadowed;
840 break;
842 case FUNCTION_DECL:
843 /* Propagate TREE_ADDRESSABLE from nested functions to their
844 containing functions. */
845 if (!TREE_ASM_WRITTEN (p)
846 && DECL_INITIAL (p) != 0
847 && TREE_ADDRESSABLE (p)
848 && DECL_ABSTRACT_ORIGIN (p) != 0
849 && DECL_ABSTRACT_ORIGIN (p) != p)
850 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
851 if (!DECL_EXTERNAL (p)
852 && !DECL_INITIAL (p)
853 && scope != file_scope
854 && scope != external_scope)
856 error ("nested function %q+D declared but never defined", p);
857 undef_nested_function = true;
859 else if (DECL_DECLARED_INLINE_P (p)
860 && TREE_PUBLIC (p)
861 && !DECL_INITIAL (p))
863 /* C99 6.7.4p6: "a function with external linkage... declared
864 with an inline function specifier ... shall also be defined
865 in the same translation unit." */
866 if (!flag_gnu89_inline)
867 pedwarn (input_location, 0,
868 "inline function %q+D declared but never defined", p);
869 DECL_EXTERNAL (p) = 1;
872 goto common_symbol;
874 case VAR_DECL:
875 /* Warnings for unused variables. */
876 if (!TREE_USED (p)
877 && !TREE_NO_WARNING (p)
878 && !DECL_IN_SYSTEM_HEADER (p)
879 && DECL_NAME (p)
880 && !DECL_ARTIFICIAL (p)
881 && scope != file_scope
882 && scope != external_scope)
883 warning (OPT_Wunused_variable, "unused variable %q+D", p);
885 if (b->inner_comp)
887 error ("type of array %q+D completed incompatibly with"
888 " implicit initialization", p);
891 /* Fall through. */
892 case TYPE_DECL:
893 case CONST_DECL:
894 common_symbol:
895 /* All of these go in BLOCK_VARS, but only if this is the
896 binding in the home scope. */
897 if (!b->nested)
899 TREE_CHAIN (p) = BLOCK_VARS (block);
900 BLOCK_VARS (block) = p;
902 else if (VAR_OR_FUNCTION_DECL_P (p))
904 /* For block local externs add a special
905 DECL_EXTERNAL decl for debug info generation. */
906 tree extp = copy_node (p);
908 DECL_EXTERNAL (extp) = 1;
909 TREE_STATIC (extp) = 0;
910 TREE_PUBLIC (extp) = 1;
911 DECL_INITIAL (extp) = NULL_TREE;
912 DECL_LANG_SPECIFIC (extp) = NULL;
913 DECL_CONTEXT (extp) = current_function_decl;
914 if (TREE_CODE (p) == FUNCTION_DECL)
916 DECL_RESULT (extp) = NULL_TREE;
917 DECL_SAVED_TREE (extp) = NULL_TREE;
918 DECL_STRUCT_FUNCTION (extp) = NULL;
920 if (b->locus != UNKNOWN_LOCATION)
921 DECL_SOURCE_LOCATION (extp) = b->locus;
922 TREE_CHAIN (extp) = BLOCK_VARS (block);
923 BLOCK_VARS (block) = extp;
925 /* If this is the file scope, and we are processing more
926 than one translation unit in this compilation, set
927 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
928 This makes same_translation_unit_p work, and causes
929 static declarations to be given disambiguating suffixes. */
930 if (scope == file_scope && num_in_fnames > 1)
932 DECL_CONTEXT (p) = context;
933 if (TREE_CODE (p) == TYPE_DECL)
934 set_type_context (TREE_TYPE (p), context);
937 /* Fall through. */
938 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
939 already been put there by store_parm_decls. Unused-
940 parameter warnings are handled by function.c.
941 error_mark_node obviously does not go in BLOCK_VARS and
942 does not get unused-variable warnings. */
943 case PARM_DECL:
944 case ERROR_MARK:
945 /* It is possible for a decl not to have a name. We get
946 here with b->id NULL in this case. */
947 if (b->id)
949 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
950 I_SYMBOL_BINDING (b->id) = b->shadowed;
951 if (b->shadowed && b->shadowed->type)
952 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
954 break;
956 default:
957 gcc_unreachable ();
962 /* Dispose of the block that we just made inside some higher level. */
963 if ((scope->function_body || scope == file_scope) && context)
965 DECL_INITIAL (context) = block;
966 BLOCK_SUPERCONTEXT (block) = context;
968 else if (scope->outer)
970 if (block)
971 SCOPE_LIST_APPEND (scope->outer, blocks, block);
972 /* If we did not make a block for the scope just exited, any
973 blocks made for inner scopes must be carried forward so they
974 will later become subblocks of something else. */
975 else if (scope->blocks)
976 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
979 /* Pop the current scope, and free the structure for reuse. */
980 current_scope = scope->outer;
981 if (scope->function_body)
982 current_function_scope = scope->outer_function;
984 memset (scope, 0, sizeof (struct c_scope));
985 scope->outer = scope_freelist;
986 scope_freelist = scope;
988 return block;
991 void
992 push_file_scope (void)
994 tree decl;
996 if (file_scope)
997 return;
999 push_scope ();
1000 file_scope = current_scope;
1002 start_fname_decls ();
1004 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
1005 bind (DECL_NAME (decl), decl, file_scope,
1006 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1009 void
1010 pop_file_scope (void)
1012 /* In case there were missing closebraces, get us back to the global
1013 binding level. */
1014 while (current_scope != file_scope)
1015 pop_scope ();
1017 /* __FUNCTION__ is defined at file scope (""). This
1018 call may not be necessary as my tests indicate it
1019 still works without it. */
1020 finish_fname_decls ();
1022 check_inline_statics ();
1024 /* This is the point to write out a PCH if we're doing that.
1025 In that case we do not want to do anything else. */
1026 if (pch_file)
1028 c_common_write_pch ();
1029 return;
1032 /* Pop off the file scope and close this translation unit. */
1033 pop_scope ();
1034 file_scope = 0;
1036 maybe_apply_pending_pragma_weaks ();
1037 cgraph_finalize_compilation_unit ();
1041 /* Push a definition or a declaration of struct, union or enum tag "name".
1042 "type" should be the type node.
1043 We assume that the tag "name" is not already defined.
1045 Note that the definition may really be just a forward reference.
1046 In that case, the TYPE_SIZE will be zero. */
1048 static void
1049 pushtag (tree name, tree type)
1051 /* Record the identifier as the type's name if it has none. */
1052 if (name && !TYPE_NAME (type))
1053 TYPE_NAME (type) = name;
1054 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false,
1055 UNKNOWN_LOCATION);
1057 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1058 tagged type we just added to the current scope. This fake
1059 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1060 to output a representation of a tagged type, and it also gives
1061 us a convenient place to record the "scope start" address for the
1062 tagged type. */
1064 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
1066 /* An approximation for now, so we can tell this is a function-scope tag.
1067 This will be updated in pop_scope. */
1068 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1071 /* Subroutine of compare_decls. Allow harmless mismatches in return
1072 and argument types provided that the type modes match. This function
1073 return a unified type given a suitable match, and 0 otherwise. */
1075 static tree
1076 match_builtin_function_types (tree newtype, tree oldtype)
1078 tree newrettype, oldrettype;
1079 tree newargs, oldargs;
1080 tree trytype, tryargs;
1082 /* Accept the return type of the new declaration if same modes. */
1083 oldrettype = TREE_TYPE (oldtype);
1084 newrettype = TREE_TYPE (newtype);
1086 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1087 return 0;
1089 oldargs = TYPE_ARG_TYPES (oldtype);
1090 newargs = TYPE_ARG_TYPES (newtype);
1091 tryargs = newargs;
1093 while (oldargs || newargs)
1095 if (!oldargs
1096 || !newargs
1097 || !TREE_VALUE (oldargs)
1098 || !TREE_VALUE (newargs)
1099 || TYPE_MODE (TREE_VALUE (oldargs))
1100 != TYPE_MODE (TREE_VALUE (newargs)))
1101 return 0;
1103 oldargs = TREE_CHAIN (oldargs);
1104 newargs = TREE_CHAIN (newargs);
1107 trytype = build_function_type (newrettype, tryargs);
1108 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1111 /* Subroutine of diagnose_mismatched_decls. Check for function type
1112 mismatch involving an empty arglist vs a nonempty one and give clearer
1113 diagnostics. */
1114 static void
1115 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1116 tree newtype, tree oldtype)
1118 tree t;
1120 if (TREE_CODE (olddecl) != FUNCTION_DECL
1121 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1122 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1124 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1125 return;
1127 t = TYPE_ARG_TYPES (oldtype);
1128 if (t == 0)
1129 t = TYPE_ARG_TYPES (newtype);
1130 for (; t; t = TREE_CHAIN (t))
1132 tree type = TREE_VALUE (t);
1134 if (TREE_CHAIN (t) == 0
1135 && TYPE_MAIN_VARIANT (type) != void_type_node)
1137 inform (input_location, "a parameter list with an ellipsis can%'t match "
1138 "an empty parameter name list declaration");
1139 break;
1142 if (c_type_promotes_to (type) != type)
1144 inform (input_location, "an argument type that has a default promotion can%'t match "
1145 "an empty parameter name list declaration");
1146 break;
1151 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1152 old-style function definition, NEWDECL is a prototype declaration.
1153 Diagnose inconsistencies in the argument list. Returns TRUE if
1154 the prototype is compatible, FALSE if not. */
1155 static bool
1156 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1158 tree newargs, oldargs;
1159 int i;
1161 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1163 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1164 newargs = TYPE_ARG_TYPES (newtype);
1165 i = 1;
1167 for (;;)
1169 tree oldargtype = TREE_VALUE (oldargs);
1170 tree newargtype = TREE_VALUE (newargs);
1172 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1173 return false;
1175 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1176 newargtype = TYPE_MAIN_VARIANT (newargtype);
1178 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1179 break;
1181 /* Reaching the end of just one list means the two decls don't
1182 agree on the number of arguments. */
1183 if (END_OF_ARGLIST (oldargtype))
1185 error ("prototype for %q+D declares more arguments "
1186 "than previous old-style definition", newdecl);
1187 return false;
1189 else if (END_OF_ARGLIST (newargtype))
1191 error ("prototype for %q+D declares fewer arguments "
1192 "than previous old-style definition", newdecl);
1193 return false;
1196 /* Type for passing arg must be consistent with that declared
1197 for the arg. */
1198 else if (!comptypes (oldargtype, newargtype))
1200 error ("prototype for %q+D declares argument %d"
1201 " with incompatible type",
1202 newdecl, i);
1203 return false;
1206 oldargs = TREE_CHAIN (oldargs);
1207 newargs = TREE_CHAIN (newargs);
1208 i++;
1211 /* If we get here, no errors were found, but do issue a warning
1212 for this poor-style construct. */
1213 warning (0, "prototype for %q+D follows non-prototype definition",
1214 newdecl);
1215 return true;
1216 #undef END_OF_ARGLIST
1219 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1220 first in a pair of mismatched declarations, using the diagnostic
1221 function DIAG. */
1222 static void
1223 locate_old_decl (tree decl)
1225 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1227 else if (DECL_INITIAL (decl))
1228 inform (input_location, "previous definition of %q+D was here", decl);
1229 else if (C_DECL_IMPLICIT (decl))
1230 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1231 else
1232 inform (input_location, "previous declaration of %q+D was here", decl);
1235 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1236 Returns true if the caller should proceed to merge the two, false
1237 if OLDDECL should simply be discarded. As a side effect, issues
1238 all necessary diagnostics for invalid or poor-style combinations.
1239 If it returns true, writes the types of NEWDECL and OLDDECL to
1240 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1241 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1243 static bool
1244 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1245 tree *newtypep, tree *oldtypep)
1247 tree newtype, oldtype;
1248 bool pedwarned = false;
1249 bool warned = false;
1250 bool retval = true;
1252 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1253 && DECL_EXTERNAL (DECL))
1255 /* If we have error_mark_node for either decl or type, just discard
1256 the previous decl - we're in an error cascade already. */
1257 if (olddecl == error_mark_node || newdecl == error_mark_node)
1258 return false;
1259 *oldtypep = oldtype = TREE_TYPE (olddecl);
1260 *newtypep = newtype = TREE_TYPE (newdecl);
1261 if (oldtype == error_mark_node || newtype == error_mark_node)
1262 return false;
1264 /* Two different categories of symbol altogether. This is an error
1265 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1266 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1268 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1269 && DECL_BUILT_IN (olddecl)
1270 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1272 error ("%q+D redeclared as different kind of symbol", newdecl);
1273 locate_old_decl (olddecl);
1275 else if (TREE_PUBLIC (newdecl))
1276 warning (0, "built-in function %q+D declared as non-function",
1277 newdecl);
1278 else
1279 warning (OPT_Wshadow, "declaration of %q+D shadows "
1280 "a built-in function", newdecl);
1281 return false;
1284 /* Enumerators have no linkage, so may only be declared once in a
1285 given scope. */
1286 if (TREE_CODE (olddecl) == CONST_DECL)
1288 error ("redeclaration of enumerator %q+D", newdecl);
1289 locate_old_decl (olddecl);
1290 return false;
1293 if (!comptypes (oldtype, newtype))
1295 if (TREE_CODE (olddecl) == FUNCTION_DECL
1296 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1298 /* Accept harmless mismatch in function types.
1299 This is for the ffs and fprintf builtins. */
1300 tree trytype = match_builtin_function_types (newtype, oldtype);
1302 if (trytype && comptypes (newtype, trytype))
1303 *oldtypep = oldtype = trytype;
1304 else
1306 /* If types don't match for a built-in, throw away the
1307 built-in. No point in calling locate_old_decl here, it
1308 won't print anything. */
1309 warning (0, "conflicting types for built-in function %q+D",
1310 newdecl);
1311 return false;
1314 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1315 && DECL_IS_BUILTIN (olddecl))
1317 /* A conflicting function declaration for a predeclared
1318 function that isn't actually built in. Objective C uses
1319 these. The new declaration silently overrides everything
1320 but the volatility (i.e. noreturn) indication. See also
1321 below. FIXME: Make Objective C use normal builtins. */
1322 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1323 return false;
1325 /* Permit void foo (...) to match int foo (...) if the latter is
1326 the definition and implicit int was used. See
1327 c-torture/compile/920625-2.c. */
1328 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1329 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1330 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1331 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1333 pedwarned = pedwarn (input_location, 0,
1334 "conflicting types for %q+D", newdecl);
1335 /* Make sure we keep void as the return type. */
1336 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1337 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1339 /* Permit void foo (...) to match an earlier call to foo (...) with
1340 no declared type (thus, implicitly int). */
1341 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1342 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1343 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1344 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1346 pedwarned = pedwarn (input_location, 0,
1347 "conflicting types for %q+D", newdecl);
1348 /* Make sure we keep void as the return type. */
1349 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1351 else
1353 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1354 error ("conflicting type qualifiers for %q+D", newdecl);
1355 else
1356 error ("conflicting types for %q+D", newdecl);
1357 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1358 locate_old_decl (olddecl);
1359 return false;
1363 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1364 but silently ignore the redeclaration if either is in a system
1365 header. (Conflicting redeclarations were handled above.) */
1366 if (TREE_CODE (newdecl) == TYPE_DECL)
1368 if (DECL_IN_SYSTEM_HEADER (newdecl)
1369 || DECL_IN_SYSTEM_HEADER (olddecl)
1370 || TREE_NO_WARNING (newdecl)
1371 || TREE_NO_WARNING (olddecl))
1372 return true; /* Allow OLDDECL to continue in use. */
1374 error ("redefinition of typedef %q+D", newdecl);
1375 locate_old_decl (olddecl);
1376 return false;
1379 /* Function declarations can either be 'static' or 'extern' (no
1380 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1381 can never conflict with each other on account of linkage
1382 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1383 gnu89 mode permits two definitions if one is 'extern inline' and
1384 one is not. The non- extern-inline definition supersedes the
1385 extern-inline definition. */
1387 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1389 /* If you declare a built-in function name as static, or
1390 define the built-in with an old-style definition (so we
1391 can't validate the argument list) the built-in definition is
1392 overridden, but optionally warn this was a bad choice of name. */
1393 if (DECL_BUILT_IN (olddecl)
1394 && !C_DECL_DECLARED_BUILTIN (olddecl)
1395 && (!TREE_PUBLIC (newdecl)
1396 || (DECL_INITIAL (newdecl)
1397 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1399 warning (OPT_Wshadow, "declaration of %q+D shadows "
1400 "a built-in function", newdecl);
1401 /* Discard the old built-in function. */
1402 return false;
1405 if (DECL_INITIAL (newdecl))
1407 if (DECL_INITIAL (olddecl))
1409 /* If both decls are in the same TU and the new declaration
1410 isn't overriding an extern inline reject the new decl.
1411 In c99, no overriding is allowed in the same translation
1412 unit. */
1413 if ((!DECL_EXTERN_INLINE (olddecl)
1414 || DECL_EXTERN_INLINE (newdecl)
1415 || (!flag_gnu89_inline
1416 && (!DECL_DECLARED_INLINE_P (olddecl)
1417 || !lookup_attribute ("gnu_inline",
1418 DECL_ATTRIBUTES (olddecl)))
1419 && (!DECL_DECLARED_INLINE_P (newdecl)
1420 || !lookup_attribute ("gnu_inline",
1421 DECL_ATTRIBUTES (newdecl))))
1423 && same_translation_unit_p (newdecl, olddecl))
1425 error ("redefinition of %q+D", newdecl);
1426 locate_old_decl (olddecl);
1427 return false;
1431 /* If we have a prototype after an old-style function definition,
1432 the argument types must be checked specially. */
1433 else if (DECL_INITIAL (olddecl)
1434 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1435 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1436 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1438 locate_old_decl (olddecl);
1439 return false;
1441 /* A non-static declaration (even an "extern") followed by a
1442 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1443 The same is true for a static forward declaration at block
1444 scope followed by a non-static declaration/definition at file
1445 scope. Static followed by non-static at the same scope is
1446 not undefined behavior, and is the most convenient way to get
1447 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1448 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1449 we do diagnose it if -Wtraditional. */
1450 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1452 /* Two exceptions to the rule. If olddecl is an extern
1453 inline, or a predeclared function that isn't actually
1454 built in, newdecl silently overrides olddecl. The latter
1455 occur only in Objective C; see also above. (FIXME: Make
1456 Objective C use normal builtins.) */
1457 if (!DECL_IS_BUILTIN (olddecl)
1458 && !DECL_EXTERN_INLINE (olddecl))
1460 error ("static declaration of %q+D follows "
1461 "non-static declaration", newdecl);
1462 locate_old_decl (olddecl);
1464 return false;
1466 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1468 if (DECL_CONTEXT (olddecl))
1470 error ("non-static declaration of %q+D follows "
1471 "static declaration", newdecl);
1472 locate_old_decl (olddecl);
1473 return false;
1475 else if (warn_traditional)
1477 warned |= warning (OPT_Wtraditional,
1478 "non-static declaration of %q+D "
1479 "follows static declaration", newdecl);
1483 /* Make sure gnu_inline attribute is either not present, or
1484 present on all inline decls. */
1485 if (DECL_DECLARED_INLINE_P (olddecl)
1486 && DECL_DECLARED_INLINE_P (newdecl))
1488 bool newa = lookup_attribute ("gnu_inline",
1489 DECL_ATTRIBUTES (newdecl)) != NULL;
1490 bool olda = lookup_attribute ("gnu_inline",
1491 DECL_ATTRIBUTES (olddecl)) != NULL;
1492 if (newa != olda)
1494 error ("%<gnu_inline%> attribute present on %q+D",
1495 newa ? newdecl : olddecl);
1496 error ("%Jbut not here", newa ? olddecl : newdecl);
1500 else if (TREE_CODE (newdecl) == VAR_DECL)
1502 /* Only variables can be thread-local, and all declarations must
1503 agree on this property. */
1504 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1506 /* Nothing to check. Since OLDDECL is marked threadprivate
1507 and NEWDECL does not have a thread-local attribute, we
1508 will merge the threadprivate attribute into NEWDECL. */
1511 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1513 if (DECL_THREAD_LOCAL_P (newdecl))
1514 error ("thread-local declaration of %q+D follows "
1515 "non-thread-local declaration", newdecl);
1516 else
1517 error ("non-thread-local declaration of %q+D follows "
1518 "thread-local declaration", newdecl);
1520 locate_old_decl (olddecl);
1521 return false;
1524 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1525 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1527 error ("redefinition of %q+D", newdecl);
1528 locate_old_decl (olddecl);
1529 return false;
1532 /* Objects declared at file scope: if the first declaration had
1533 external linkage (even if it was an external reference) the
1534 second must have external linkage as well, or the behavior is
1535 undefined. If the first declaration had internal linkage, then
1536 the second must too, or else be an external reference (in which
1537 case the composite declaration still has internal linkage).
1538 As for function declarations, we warn about the static-then-
1539 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1540 if (DECL_FILE_SCOPE_P (newdecl)
1541 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1543 if (DECL_EXTERNAL (newdecl))
1545 if (!DECL_FILE_SCOPE_P (olddecl))
1547 error ("extern declaration of %q+D follows "
1548 "declaration with no linkage", newdecl);
1549 locate_old_decl (olddecl);
1550 return false;
1552 else if (warn_traditional)
1554 warned |= warning (OPT_Wtraditional,
1555 "non-static declaration of %q+D "
1556 "follows static declaration", newdecl);
1559 else
1561 if (TREE_PUBLIC (newdecl))
1562 error ("non-static declaration of %q+D follows "
1563 "static declaration", newdecl);
1564 else
1565 error ("static declaration of %q+D follows "
1566 "non-static declaration", newdecl);
1568 locate_old_decl (olddecl);
1569 return false;
1572 /* Two objects with the same name declared at the same block
1573 scope must both be external references (6.7p3). */
1574 else if (!DECL_FILE_SCOPE_P (newdecl))
1576 if (DECL_EXTERNAL (newdecl))
1578 /* Extern with initializer at block scope, which will
1579 already have received an error. */
1581 else if (DECL_EXTERNAL (olddecl))
1583 error ("declaration of %q+D with no linkage follows "
1584 "extern declaration", newdecl);
1585 locate_old_decl (olddecl);
1587 else
1589 error ("redeclaration of %q+D with no linkage", newdecl);
1590 locate_old_decl (olddecl);
1593 return false;
1597 /* warnings */
1598 /* All decls must agree on a visibility. */
1599 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1600 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1601 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1603 warned |= warning (0, "redeclaration of %q+D with different visibility "
1604 "(old visibility preserved)", newdecl);
1607 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1609 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1610 if (DECL_DECLARED_INLINE_P (newdecl)
1611 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1613 warned |= warning (OPT_Wattributes,
1614 "inline declaration of %qD follows "
1615 "declaration with attribute noinline", newdecl);
1617 else if (DECL_DECLARED_INLINE_P (olddecl)
1618 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1620 warned |= warning (OPT_Wattributes,
1621 "declaration of %q+D with attribute "
1622 "noinline follows inline declaration ", newdecl);
1625 else /* PARM_DECL, VAR_DECL */
1627 /* Redeclaration of a parameter is a constraint violation (this is
1628 not explicitly stated, but follows from C99 6.7p3 [no more than
1629 one declaration of the same identifier with no linkage in the
1630 same scope, except type tags] and 6.2.2p6 [parameters have no
1631 linkage]). We must check for a forward parameter declaration,
1632 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1633 an extension, the mandatory diagnostic for which is handled by
1634 mark_forward_parm_decls. */
1636 if (TREE_CODE (newdecl) == PARM_DECL
1637 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1639 error ("redefinition of parameter %q+D", newdecl);
1640 locate_old_decl (olddecl);
1641 return false;
1645 /* Optional warning for completely redundant decls. */
1646 if (!warned && !pedwarned
1647 && warn_redundant_decls
1648 /* Don't warn about a function declaration followed by a
1649 definition. */
1650 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1651 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1652 /* Don't warn about redundant redeclarations of builtins. */
1653 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1654 && !DECL_BUILT_IN (newdecl)
1655 && DECL_BUILT_IN (olddecl)
1656 && !C_DECL_DECLARED_BUILTIN (olddecl))
1657 /* Don't warn about an extern followed by a definition. */
1658 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1659 /* Don't warn about forward parameter decls. */
1660 && !(TREE_CODE (newdecl) == PARM_DECL
1661 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1662 /* Don't warn about a variable definition following a declaration. */
1663 && !(TREE_CODE (newdecl) == VAR_DECL
1664 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1666 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1667 newdecl);
1670 /* Report location of previous decl/defn. */
1671 if (warned || pedwarned)
1672 locate_old_decl (olddecl);
1674 #undef DECL_EXTERN_INLINE
1676 return retval;
1679 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1680 consistent with OLDDECL, but carries new information. Merge the
1681 new information into OLDDECL. This function issues no
1682 diagnostics. */
1684 static void
1685 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1687 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1688 && DECL_INITIAL (newdecl) != 0);
1689 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1690 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1691 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1692 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1693 bool extern_changed = false;
1695 /* For real parm decl following a forward decl, rechain the old decl
1696 in its new location and clear TREE_ASM_WRITTEN (it's not a
1697 forward decl anymore). */
1698 if (TREE_CODE (newdecl) == PARM_DECL
1699 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1701 struct c_binding *b, **here;
1703 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1704 if ((*here)->decl == olddecl)
1705 goto found;
1706 gcc_unreachable ();
1708 found:
1709 b = *here;
1710 *here = b->prev;
1711 b->prev = current_scope->bindings;
1712 current_scope->bindings = b;
1714 TREE_ASM_WRITTEN (olddecl) = 0;
1717 DECL_ATTRIBUTES (newdecl)
1718 = targetm.merge_decl_attributes (olddecl, newdecl);
1720 /* Merge the data types specified in the two decls. */
1721 TREE_TYPE (newdecl)
1722 = TREE_TYPE (olddecl)
1723 = composite_type (newtype, oldtype);
1725 /* Lay the type out, unless already done. */
1726 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1728 if (TREE_TYPE (newdecl) != error_mark_node)
1729 layout_type (TREE_TYPE (newdecl));
1730 if (TREE_CODE (newdecl) != FUNCTION_DECL
1731 && TREE_CODE (newdecl) != TYPE_DECL
1732 && TREE_CODE (newdecl) != CONST_DECL)
1733 layout_decl (newdecl, 0);
1735 else
1737 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1738 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1739 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1740 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1741 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1743 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1744 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
1748 /* Keep the old rtl since we can safely use it. */
1749 if (HAS_RTL_P (olddecl))
1750 COPY_DECL_RTL (olddecl, newdecl);
1752 /* Merge the type qualifiers. */
1753 if (TREE_READONLY (newdecl))
1754 TREE_READONLY (olddecl) = 1;
1756 if (TREE_THIS_VOLATILE (newdecl))
1757 TREE_THIS_VOLATILE (olddecl) = 1;
1759 /* Merge deprecatedness. */
1760 if (TREE_DEPRECATED (newdecl))
1761 TREE_DEPRECATED (olddecl) = 1;
1763 /* If a decl is in a system header and the other isn't, keep the one on the
1764 system header. Otherwise, keep source location of definition rather than
1765 declaration and of prototype rather than non-prototype unless that
1766 prototype is built-in. */
1767 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1768 && DECL_IN_SYSTEM_HEADER (olddecl)
1769 && !DECL_IN_SYSTEM_HEADER (newdecl) )
1770 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1771 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
1772 && DECL_IN_SYSTEM_HEADER (newdecl)
1773 && !DECL_IN_SYSTEM_HEADER (olddecl))
1774 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
1775 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1776 || (old_is_prototype && !new_is_prototype
1777 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1778 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1780 /* Merge the initialization information. */
1781 if (DECL_INITIAL (newdecl) == 0)
1782 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1784 /* Merge the threadprivate attribute. */
1785 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1787 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1788 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1791 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1793 /* Merge the section attribute.
1794 We want to issue an error if the sections conflict but that
1795 must be done later in decl_attributes since we are called
1796 before attributes are assigned. */
1797 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1798 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1800 /* Copy the assembler name.
1801 Currently, it can only be defined in the prototype. */
1802 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1804 /* Use visibility of whichever declaration had it specified */
1805 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1807 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1808 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1811 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1813 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1814 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1815 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1816 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1817 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1818 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1819 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1820 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1821 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1822 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1823 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1826 /* Merge the storage class information. */
1827 merge_weak (newdecl, olddecl);
1829 /* For functions, static overrides non-static. */
1830 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1832 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1833 /* This is since we don't automatically
1834 copy the attributes of NEWDECL into OLDDECL. */
1835 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1836 /* If this clears `static', clear it in the identifier too. */
1837 if (!TREE_PUBLIC (olddecl))
1838 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1842 /* In c99, 'extern' declaration before (or after) 'inline' means this
1843 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1844 is present. */
1845 if (TREE_CODE (newdecl) == FUNCTION_DECL
1846 && !flag_gnu89_inline
1847 && (DECL_DECLARED_INLINE_P (newdecl)
1848 || DECL_DECLARED_INLINE_P (olddecl))
1849 && (!DECL_DECLARED_INLINE_P (newdecl)
1850 || !DECL_DECLARED_INLINE_P (olddecl)
1851 || !DECL_EXTERNAL (olddecl))
1852 && DECL_EXTERNAL (newdecl)
1853 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
1854 && !current_function_decl)
1855 DECL_EXTERNAL (newdecl) = 0;
1857 if (DECL_EXTERNAL (newdecl))
1859 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1860 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1862 /* An extern decl does not override previous storage class. */
1863 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1864 if (!DECL_EXTERNAL (newdecl))
1866 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1867 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1870 else
1872 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1873 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1876 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1878 /* If we're redefining a function previously defined as extern
1879 inline, make sure we emit debug info for the inline before we
1880 throw it away, in case it was inlined into a function that
1881 hasn't been written out yet. */
1882 if (new_is_definition && DECL_INITIAL (olddecl))
1883 /* The new defn must not be inline. */
1884 DECL_UNINLINABLE (newdecl) = 1;
1885 else
1887 /* If either decl says `inline', this fn is inline, unless
1888 its definition was passed already. */
1889 if (DECL_DECLARED_INLINE_P (newdecl)
1890 || DECL_DECLARED_INLINE_P (olddecl))
1891 DECL_DECLARED_INLINE_P (newdecl) = 1;
1893 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1894 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1896 DECL_DISREGARD_INLINE_LIMITS (newdecl)
1897 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1898 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1899 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
1902 if (DECL_BUILT_IN (olddecl))
1904 /* If redeclaring a builtin function, it stays built in.
1905 But it gets tagged as having been declared. */
1906 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1907 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1908 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1909 if (new_is_prototype)
1910 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1911 else
1912 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1913 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1916 /* Preserve function specific target and optimization options */
1917 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1918 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1919 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1920 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1922 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1923 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1924 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1925 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
1927 /* Also preserve various other info from the definition. */
1928 if (!new_is_definition)
1930 tree t;
1931 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1932 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1933 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1934 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1935 gimple_set_body (newdecl, gimple_body (olddecl));
1936 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
1937 for (t = DECL_ARGUMENTS (newdecl); t ; t = TREE_CHAIN (t))
1938 DECL_CONTEXT (t) = newdecl;
1940 /* See if we've got a function to instantiate from. */
1941 if (DECL_SAVED_TREE (olddecl))
1942 DECL_ABSTRACT_ORIGIN (newdecl)
1943 = DECL_ABSTRACT_ORIGIN (olddecl);
1947 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1949 /* Merge the USED information. */
1950 if (TREE_USED (olddecl))
1951 TREE_USED (newdecl) = 1;
1952 else if (TREE_USED (newdecl))
1953 TREE_USED (olddecl) = 1;
1955 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1956 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1958 unsigned olddecl_uid = DECL_UID (olddecl);
1959 tree olddecl_context = DECL_CONTEXT (olddecl);
1960 tree olddecl_arguments = NULL;
1961 if (TREE_CODE (olddecl) == FUNCTION_DECL)
1962 olddecl_arguments = DECL_ARGUMENTS (olddecl);
1964 memcpy ((char *) olddecl + sizeof (struct tree_common),
1965 (char *) newdecl + sizeof (struct tree_common),
1966 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1967 switch (TREE_CODE (olddecl))
1969 case FUNCTION_DECL:
1970 gimple_set_body (olddecl, gimple_body (newdecl));
1971 /* fall through */
1973 case FIELD_DECL:
1974 case VAR_DECL:
1975 case PARM_DECL:
1976 case LABEL_DECL:
1977 case RESULT_DECL:
1978 case CONST_DECL:
1979 case TYPE_DECL:
1980 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1981 (char *) newdecl + sizeof (struct tree_decl_common),
1982 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1983 break;
1985 default:
1987 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1988 (char *) newdecl + sizeof (struct tree_decl_common),
1989 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1991 DECL_UID (olddecl) = olddecl_uid;
1992 DECL_CONTEXT (olddecl) = olddecl_context;
1993 if (TREE_CODE (olddecl) == FUNCTION_DECL)
1994 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
1997 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1998 so that encode_section_info has a chance to look at the new decl
1999 flags and attributes. */
2000 if (DECL_RTL_SET_P (olddecl)
2001 && (TREE_CODE (olddecl) == FUNCTION_DECL
2002 || (TREE_CODE (olddecl) == VAR_DECL
2003 && TREE_STATIC (olddecl))))
2004 make_decl_rtl (olddecl);
2006 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
2007 and the definition is coming from the old version, cgraph needs
2008 to be called again. */
2009 if (extern_changed && !new_is_definition
2010 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
2011 cgraph_mark_if_needed (olddecl);
2014 /* Handle when a new declaration NEWDECL has the same name as an old
2015 one OLDDECL in the same binding contour. Prints an error message
2016 if appropriate.
2018 If safely possible, alter OLDDECL to look like NEWDECL, and return
2019 true. Otherwise, return false. */
2021 static bool
2022 duplicate_decls (tree newdecl, tree olddecl)
2024 tree newtype = NULL, oldtype = NULL;
2026 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2028 /* Avoid `unused variable' and other warnings for OLDDECL. */
2029 TREE_NO_WARNING (olddecl) = 1;
2030 return false;
2033 merge_decls (newdecl, olddecl, newtype, oldtype);
2034 return true;
2038 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2039 static void
2040 warn_if_shadowing (tree new_decl)
2042 struct c_binding *b;
2044 /* Shadow warnings wanted? */
2045 if (!warn_shadow
2046 /* No shadow warnings for internally generated vars. */
2047 || DECL_IS_BUILTIN (new_decl)
2048 /* No shadow warnings for vars made for inlining. */
2049 || DECL_FROM_INLINE (new_decl))
2050 return;
2052 /* Is anything being shadowed? Invisible decls do not count. */
2053 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2054 if (b->decl && b->decl != new_decl && !b->invisible)
2056 tree old_decl = b->decl;
2058 if (old_decl == error_mark_node)
2060 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2061 "non-variable", new_decl);
2062 break;
2064 else if (TREE_CODE (old_decl) == PARM_DECL)
2065 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2066 new_decl);
2067 else if (DECL_FILE_SCOPE_P (old_decl))
2068 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2069 "declaration", new_decl);
2070 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2071 && DECL_BUILT_IN (old_decl))
2073 warning (OPT_Wshadow, "declaration of %q+D shadows "
2074 "a built-in function", new_decl);
2075 break;
2077 else
2078 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2079 new_decl);
2081 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
2083 break;
2087 /* Record a decl-node X as belonging to the current lexical scope.
2088 Check for errors (such as an incompatible declaration for the same
2089 name already seen in the same scope).
2091 Returns either X or an old decl for the same name.
2092 If an old decl is returned, it may have been smashed
2093 to agree with what X says. */
2095 tree
2096 pushdecl (tree x)
2098 tree name = DECL_NAME (x);
2099 struct c_scope *scope = current_scope;
2100 struct c_binding *b;
2101 bool nested = false;
2102 location_t locus = DECL_SOURCE_LOCATION (x);
2104 /* Must set DECL_CONTEXT for everything not at file scope or
2105 DECL_FILE_SCOPE_P won't work. Local externs don't count
2106 unless they have initializers (which generate code). */
2107 if (current_function_decl
2108 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2109 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2110 DECL_CONTEXT (x) = current_function_decl;
2112 /* If this is of variably modified type, prevent jumping into its
2113 scope. */
2114 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2115 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2116 c_begin_vm_scope (scope->depth);
2118 /* Anonymous decls are just inserted in the scope. */
2119 if (!name)
2121 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2122 locus);
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 locus);
2284 return b->decl;
2286 else if (TREE_PUBLIC (x))
2288 if (visdecl && !b && duplicate_decls (x, visdecl))
2290 /* An external declaration at block scope referring to a
2291 visible entity with internal linkage. The composite
2292 type will already be correct for this scope, so we
2293 just need to fall through to make the declaration in
2294 this scope. */
2295 nested = true;
2296 x = visdecl;
2298 else
2300 bind (name, x, external_scope, /*invisible=*/true,
2301 /*nested=*/false, locus);
2302 nested = true;
2307 if (TREE_CODE (x) != PARM_DECL)
2308 warn_if_shadowing (x);
2310 skip_external_and_shadow_checks:
2311 if (TREE_CODE (x) == TYPE_DECL)
2312 set_underlying_type (x);
2314 bind (name, x, scope, /*invisible=*/false, nested, locus);
2316 /* If x's type is incomplete because it's based on a
2317 structure or union which has not yet been fully declared,
2318 attach it to that structure or union type, so we can go
2319 back and complete the variable declaration later, if the
2320 structure or union gets fully declared.
2322 If the input is erroneous, we can have error_mark in the type
2323 slot (e.g. "f(void a, ...)") - that doesn't count as an
2324 incomplete type. */
2325 if (TREE_TYPE (x) != error_mark_node
2326 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2328 tree element = TREE_TYPE (x);
2330 while (TREE_CODE (element) == ARRAY_TYPE)
2331 element = TREE_TYPE (element);
2332 element = TYPE_MAIN_VARIANT (element);
2334 if ((TREE_CODE (element) == RECORD_TYPE
2335 || TREE_CODE (element) == UNION_TYPE)
2336 && (TREE_CODE (x) != TYPE_DECL
2337 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2338 && !COMPLETE_TYPE_P (element))
2339 C_TYPE_INCOMPLETE_VARS (element)
2340 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2342 return x;
2345 /* Record X as belonging to file scope.
2346 This is used only internally by the Objective-C front end,
2347 and is limited to its needs. duplicate_decls is not called;
2348 if there is any preexisting decl for this identifier, it is an ICE. */
2350 tree
2351 pushdecl_top_level (tree x)
2353 tree name;
2354 bool nested = false;
2355 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2357 name = DECL_NAME (x);
2359 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2361 if (TREE_PUBLIC (x))
2363 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2364 UNKNOWN_LOCATION);
2365 nested = true;
2367 if (file_scope)
2368 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2370 return x;
2373 static void
2374 implicit_decl_warning (tree id, tree olddecl)
2376 if (warn_implicit_function_declaration)
2378 bool warned;
2380 if (flag_isoc99)
2381 warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2382 "implicit declaration of function %qE", id);
2383 else
2384 warned = warning (OPT_Wimplicit_function_declaration,
2385 G_("implicit declaration of function %qE"), id);
2386 if (olddecl && warned)
2387 locate_old_decl (olddecl);
2391 /* Generate an implicit declaration for identifier FUNCTIONID as a
2392 function of type int (). */
2394 tree
2395 implicitly_declare (tree functionid)
2397 struct c_binding *b;
2398 tree decl = 0;
2399 tree asmspec_tree;
2401 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2403 if (B_IN_SCOPE (b, external_scope))
2405 decl = b->decl;
2406 break;
2410 if (decl)
2412 if (decl == error_mark_node)
2413 return decl;
2415 /* FIXME: Objective-C has weird not-really-builtin functions
2416 which are supposed to be visible automatically. They wind up
2417 in the external scope because they're pushed before the file
2418 scope gets created. Catch this here and rebind them into the
2419 file scope. */
2420 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2422 bind (functionid, decl, file_scope,
2423 /*invisible=*/false, /*nested=*/true,
2424 DECL_SOURCE_LOCATION (decl));
2425 return decl;
2427 else
2429 tree newtype = default_function_type;
2430 if (b->type)
2431 TREE_TYPE (decl) = b->type;
2432 /* Implicit declaration of a function already declared
2433 (somehow) in a different scope, or as a built-in.
2434 If this is the first time this has happened, warn;
2435 then recycle the old declaration but with the new type. */
2436 if (!C_DECL_IMPLICIT (decl))
2438 implicit_decl_warning (functionid, decl);
2439 C_DECL_IMPLICIT (decl) = 1;
2441 if (DECL_BUILT_IN (decl))
2443 newtype = build_type_attribute_variant (newtype,
2444 TYPE_ATTRIBUTES
2445 (TREE_TYPE (decl)));
2446 if (!comptypes (newtype, TREE_TYPE (decl)))
2448 warning (0, "incompatible implicit declaration of built-in"
2449 " function %qD", decl);
2450 newtype = TREE_TYPE (decl);
2453 else
2455 if (!comptypes (newtype, TREE_TYPE (decl)))
2457 error ("incompatible implicit declaration of function %qD",
2458 decl);
2459 locate_old_decl (decl);
2462 b->type = TREE_TYPE (decl);
2463 TREE_TYPE (decl) = newtype;
2464 bind (functionid, decl, current_scope,
2465 /*invisible=*/false, /*nested=*/true,
2466 DECL_SOURCE_LOCATION (decl));
2467 return decl;
2471 /* Not seen before. */
2472 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2473 DECL_EXTERNAL (decl) = 1;
2474 TREE_PUBLIC (decl) = 1;
2475 C_DECL_IMPLICIT (decl) = 1;
2476 implicit_decl_warning (functionid, 0);
2477 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2478 if (asmspec_tree)
2479 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2481 /* C89 says implicit declarations are in the innermost block.
2482 So we record the decl in the standard fashion. */
2483 decl = pushdecl (decl);
2485 /* No need to call objc_check_decl here - it's a function type. */
2486 rest_of_decl_compilation (decl, 0, 0);
2488 /* Write a record describing this implicit function declaration
2489 to the prototypes file (if requested). */
2490 gen_aux_info_record (decl, 0, 1, 0);
2492 /* Possibly apply some default attributes to this implicit declaration. */
2493 decl_attributes (&decl, NULL_TREE, 0);
2495 return decl;
2498 /* Issue an error message for a reference to an undeclared variable
2499 ID, including a reference to a builtin outside of function-call
2500 context. Establish a binding of the identifier to error_mark_node
2501 in an appropriate scope, which will suppress further errors for the
2502 same identifier. The error message should be given location LOC. */
2503 void
2504 undeclared_variable (tree id, location_t loc)
2506 static bool already = false;
2507 struct c_scope *scope;
2509 if (current_function_decl == 0)
2511 error ("%H%qE undeclared here (not in a function)", &loc, id);
2512 scope = current_scope;
2514 else
2516 error ("%H%qE undeclared (first use in this function)", &loc, id);
2518 if (!already)
2520 error ("%H(Each undeclared identifier is reported only once", &loc);
2521 error ("%Hfor each function it appears in.)", &loc);
2522 already = true;
2525 /* If we are parsing old-style parameter decls, current_function_decl
2526 will be nonnull but current_function_scope will be null. */
2527 scope = current_function_scope ? current_function_scope : current_scope;
2529 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
2530 UNKNOWN_LOCATION);
2533 /* Subroutine of lookup_label, declare_label, define_label: construct a
2534 LABEL_DECL with all the proper frills. */
2536 static tree
2537 make_label (tree name, location_t location)
2539 tree label = build_decl (LABEL_DECL, name, void_type_node);
2541 DECL_CONTEXT (label) = current_function_decl;
2542 DECL_MODE (label) = VOIDmode;
2543 DECL_SOURCE_LOCATION (label) = location;
2545 return label;
2548 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2549 Create one if none exists so far for the current function.
2550 This is called when a label is used in a goto expression or
2551 has its address taken. */
2553 tree
2554 lookup_label (tree name)
2556 tree label;
2558 if (current_function_decl == 0)
2560 error ("label %qE referenced outside of any function", name);
2561 return 0;
2564 /* Use a label already defined or ref'd with this name, but not if
2565 it is inherited from a containing function and wasn't declared
2566 using __label__. */
2567 label = I_LABEL_DECL (name);
2568 if (label && (DECL_CONTEXT (label) == current_function_decl
2569 || C_DECLARED_LABEL_FLAG (label)))
2571 /* If the label has only been declared, update its apparent
2572 location to point here, for better diagnostics if it
2573 turns out not to have been defined. */
2574 if (!TREE_USED (label))
2575 DECL_SOURCE_LOCATION (label) = input_location;
2576 return label;
2579 /* No label binding for that identifier; make one. */
2580 label = make_label (name, input_location);
2582 /* Ordinary labels go in the current function scope. */
2583 bind (name, label, current_function_scope,
2584 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2585 return label;
2588 /* Make a label named NAME in the current function, shadowing silently
2589 any that may be inherited from containing functions or containing
2590 scopes. This is called for __label__ declarations. */
2592 tree
2593 declare_label (tree name)
2595 struct c_binding *b = I_LABEL_BINDING (name);
2596 tree label;
2598 /* Check to make sure that the label hasn't already been declared
2599 at this scope */
2600 if (b && B_IN_CURRENT_SCOPE (b))
2602 error ("duplicate label declaration %qE", name);
2603 locate_old_decl (b->decl);
2605 /* Just use the previous declaration. */
2606 return b->decl;
2609 label = make_label (name, input_location);
2610 C_DECLARED_LABEL_FLAG (label) = 1;
2612 /* Declared labels go in the current scope. */
2613 bind (name, label, current_scope,
2614 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2615 return label;
2618 /* Define a label, specifying the location in the source file.
2619 Return the LABEL_DECL node for the label, if the definition is valid.
2620 Otherwise return 0. */
2622 tree
2623 define_label (location_t location, tree name)
2625 /* Find any preexisting label with this name. It is an error
2626 if that label has already been defined in this function, or
2627 if there is a containing function with a declared label with
2628 the same name. */
2629 tree label = I_LABEL_DECL (name);
2630 struct c_label_list *nlist_se, *nlist_vm;
2632 if (label
2633 && ((DECL_CONTEXT (label) == current_function_decl
2634 && DECL_INITIAL (label) != 0)
2635 || (DECL_CONTEXT (label) != current_function_decl
2636 && C_DECLARED_LABEL_FLAG (label))))
2638 error ("%Hduplicate label %qD", &location, label);
2639 locate_old_decl (label);
2640 return 0;
2642 else if (label && DECL_CONTEXT (label) == current_function_decl)
2644 /* The label has been used or declared already in this function,
2645 but not defined. Update its location to point to this
2646 definition. */
2647 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2648 error ("%Jjump into statement expression", label);
2649 if (C_DECL_UNDEFINABLE_VM (label))
2650 error ("%Jjump into scope of identifier with variably modified type",
2651 label);
2652 DECL_SOURCE_LOCATION (label) = location;
2654 else
2656 /* No label binding for that identifier; make one. */
2657 label = make_label (name, location);
2659 /* Ordinary labels go in the current function scope. */
2660 bind (name, label, current_function_scope,
2661 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2664 if (!in_system_header && lookup_name (name))
2665 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2666 "for labels, identifier %qE conflicts", &location, name);
2668 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2669 nlist_se->next = label_context_stack_se->labels_def;
2670 nlist_se->label = label;
2671 label_context_stack_se->labels_def = nlist_se;
2673 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2674 nlist_vm->next = label_context_stack_vm->labels_def;
2675 nlist_vm->label = label;
2676 label_context_stack_vm->labels_def = nlist_vm;
2678 /* Mark label as having been defined. */
2679 DECL_INITIAL (label) = error_mark_node;
2680 return label;
2683 /* Given NAME, an IDENTIFIER_NODE,
2684 return the structure (or union or enum) definition for that name.
2685 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2686 CODE says which kind of type the caller wants;
2687 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2688 If the wrong kind of type is found, an error is reported. */
2690 static tree
2691 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2693 struct c_binding *b = I_TAG_BINDING (name);
2694 int thislevel = 0;
2696 if (!b || !b->decl)
2697 return 0;
2699 /* We only care about whether it's in this level if
2700 thislevel_only was set or it might be a type clash. */
2701 if (thislevel_only || TREE_CODE (b->decl) != code)
2703 /* For our purposes, a tag in the external scope is the same as
2704 a tag in the file scope. (Primarily relevant to Objective-C
2705 and its builtin structure tags, which get pushed before the
2706 file scope is created.) */
2707 if (B_IN_CURRENT_SCOPE (b)
2708 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2709 thislevel = 1;
2712 if (thislevel_only && !thislevel)
2713 return 0;
2715 if (TREE_CODE (b->decl) != code)
2717 /* Definition isn't the kind we were looking for. */
2718 pending_invalid_xref = name;
2719 pending_invalid_xref_location = input_location;
2721 /* If in the same binding level as a declaration as a tag
2722 of a different type, this must not be allowed to
2723 shadow that tag, so give the error immediately.
2724 (For example, "struct foo; union foo;" is invalid.) */
2725 if (thislevel)
2726 pending_xref_error ();
2728 return b->decl;
2731 /* Print an error message now
2732 for a recent invalid struct, union or enum cross reference.
2733 We don't print them immediately because they are not invalid
2734 when used in the `struct foo;' construct for shadowing. */
2736 void
2737 pending_xref_error (void)
2739 if (pending_invalid_xref != 0)
2740 error ("%H%qE defined as wrong kind of tag",
2741 &pending_invalid_xref_location, pending_invalid_xref);
2742 pending_invalid_xref = 0;
2746 /* Look up NAME in the current scope and its superiors
2747 in the namespace of variables, functions and typedefs.
2748 Return a ..._DECL node of some kind representing its definition,
2749 or return 0 if it is undefined. */
2751 tree
2752 lookup_name (tree name)
2754 struct c_binding *b = I_SYMBOL_BINDING (name);
2755 if (b && !b->invisible)
2756 return b->decl;
2757 return 0;
2760 /* Similar to `lookup_name' but look only at the indicated scope. */
2762 static tree
2763 lookup_name_in_scope (tree name, struct c_scope *scope)
2765 struct c_binding *b;
2767 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2768 if (B_IN_SCOPE (b, scope))
2769 return b->decl;
2770 return 0;
2773 /* Create the predefined scalar types of C,
2774 and some nodes representing standard constants (0, 1, (void *) 0).
2775 Initialize the global scope.
2776 Make definitions for built-in primitive functions. */
2778 void
2779 c_init_decl_processing (void)
2781 location_t save_loc = input_location;
2783 /* Initialize reserved words for parser. */
2784 c_parse_init ();
2786 current_function_decl = 0;
2788 gcc_obstack_init (&parser_obstack);
2790 /* Make the externals scope. */
2791 push_scope ();
2792 external_scope = current_scope;
2794 /* Declarations from c_common_nodes_and_builtins must not be associated
2795 with this input file, lest we get differences between using and not
2796 using preprocessed headers. */
2797 input_location = BUILTINS_LOCATION;
2799 build_common_tree_nodes (flag_signed_char, false);
2801 c_common_nodes_and_builtins ();
2803 /* In C, comparisons and TRUTH_* expressions have type int. */
2804 truthvalue_type_node = integer_type_node;
2805 truthvalue_true_node = integer_one_node;
2806 truthvalue_false_node = integer_zero_node;
2808 /* Even in C99, which has a real boolean type. */
2809 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2810 boolean_type_node));
2812 input_location = save_loc;
2814 pedantic_lvalues = true;
2816 make_fname_decl = c_make_fname_decl;
2817 start_fname_decls ();
2820 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2821 decl, NAME is the initialization string and TYPE_DEP indicates whether
2822 NAME depended on the type of the function. As we don't yet implement
2823 delayed emission of static data, we mark the decl as emitted
2824 so it is not placed in the output. Anything using it must therefore pull
2825 out the STRING_CST initializer directly. FIXME. */
2827 static tree
2828 c_make_fname_decl (tree id, int type_dep)
2830 const char *name = fname_as_string (type_dep);
2831 tree decl, type, init;
2832 size_t length = strlen (name);
2834 type = build_array_type (char_type_node,
2835 build_index_type (size_int (length)));
2836 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2838 decl = build_decl (VAR_DECL, id, type);
2840 TREE_STATIC (decl) = 1;
2841 TREE_READONLY (decl) = 1;
2842 DECL_ARTIFICIAL (decl) = 1;
2844 init = build_string (length + 1, name);
2845 free (CONST_CAST (char *, name));
2846 TREE_TYPE (init) = type;
2847 DECL_INITIAL (decl) = init;
2849 TREE_USED (decl) = 1;
2851 if (current_function_decl
2852 /* For invalid programs like this:
2854 void foo()
2855 const char* p = __FUNCTION__;
2857 the __FUNCTION__ is believed to appear in K&R style function
2858 parameter declarator. In that case we still don't have
2859 function_scope. */
2860 && (!errorcount || current_function_scope))
2862 DECL_CONTEXT (decl) = current_function_decl;
2863 bind (id, decl, current_function_scope,
2864 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
2867 finish_decl (decl, init, NULL_TREE, NULL_TREE);
2869 return decl;
2872 tree
2873 c_builtin_function (tree decl)
2875 tree type = TREE_TYPE (decl);
2876 tree id = DECL_NAME (decl);
2878 const char *name = IDENTIFIER_POINTER (id);
2879 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2881 /* Should never be called on a symbol with a preexisting meaning. */
2882 gcc_assert (!I_SYMBOL_BINDING (id));
2884 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
2885 UNKNOWN_LOCATION);
2887 /* Builtins in the implementation namespace are made visible without
2888 needing to be explicitly declared. See push_file_scope. */
2889 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2891 TREE_CHAIN (decl) = visible_builtins;
2892 visible_builtins = decl;
2895 return decl;
2898 tree
2899 c_builtin_function_ext_scope (tree decl)
2901 tree type = TREE_TYPE (decl);
2902 tree id = DECL_NAME (decl);
2904 const char *name = IDENTIFIER_POINTER (id);
2905 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2907 /* Should never be called on a symbol with a preexisting meaning. */
2908 gcc_assert (!I_SYMBOL_BINDING (id));
2910 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
2911 UNKNOWN_LOCATION);
2913 /* Builtins in the implementation namespace are made visible without
2914 needing to be explicitly declared. See push_file_scope. */
2915 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2917 TREE_CHAIN (decl) = visible_builtins;
2918 visible_builtins = decl;
2921 return decl;
2924 /* Called when a declaration is seen that contains no names to declare.
2925 If its type is a reference to a structure, union or enum inherited
2926 from a containing scope, shadow that tag name for the current scope
2927 with a forward reference.
2928 If its type defines a new named structure or union
2929 or defines an enum, it is valid but we need not do anything here.
2930 Otherwise, it is an error. */
2932 void
2933 shadow_tag (const struct c_declspecs *declspecs)
2935 shadow_tag_warned (declspecs, 0);
2938 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2939 but no pedwarn. */
2940 void
2941 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2943 bool found_tag = false;
2945 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2947 tree value = declspecs->type;
2948 enum tree_code code = TREE_CODE (value);
2950 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2951 /* Used to test also that TYPE_SIZE (value) != 0.
2952 That caused warning for `struct foo;' at top level in the file. */
2954 tree name = TYPE_NAME (value);
2955 tree t;
2957 found_tag = true;
2959 if (declspecs->restrict_p)
2961 error ("invalid use of %<restrict%>");
2962 warned = 1;
2965 if (name == 0)
2967 if (warned != 1 && code != ENUMERAL_TYPE)
2968 /* Empty unnamed enum OK */
2970 pedwarn (input_location, 0,
2971 "unnamed struct/union that defines no instances");
2972 warned = 1;
2975 else if (!declspecs->tag_defined_p
2976 && declspecs->storage_class != csc_none)
2978 if (warned != 1)
2979 pedwarn (input_location, 0,
2980 "empty declaration with storage class specifier "
2981 "does not redeclare tag");
2982 warned = 1;
2983 pending_xref_error ();
2985 else if (!declspecs->tag_defined_p
2986 && (declspecs->const_p
2987 || declspecs->volatile_p
2988 || declspecs->restrict_p))
2990 if (warned != 1)
2991 pedwarn (input_location, 0,
2992 "empty declaration with type qualifier "
2993 "does not redeclare tag");
2994 warned = 1;
2995 pending_xref_error ();
2997 else
2999 pending_invalid_xref = 0;
3000 t = lookup_tag (code, name, 1);
3002 if (t == 0)
3004 t = make_node (code);
3005 pushtag (name, t);
3009 else
3011 if (warned != 1 && !in_system_header)
3013 pedwarn (input_location, 0,
3014 "useless type name in empty declaration");
3015 warned = 1;
3019 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3021 pedwarn (input_location, 0, "useless type name in empty declaration");
3022 warned = 1;
3025 pending_invalid_xref = 0;
3027 if (declspecs->inline_p)
3029 error ("%<inline%> in empty declaration");
3030 warned = 1;
3033 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3035 error ("%<auto%> in file-scope empty declaration");
3036 warned = 1;
3039 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3041 error ("%<register%> in file-scope empty declaration");
3042 warned = 1;
3045 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3047 warning (0, "useless storage class specifier in empty declaration");
3048 warned = 2;
3051 if (!warned && !in_system_header && declspecs->thread_p)
3053 warning (0, "useless %<__thread%> in empty declaration");
3054 warned = 2;
3057 if (!warned && !in_system_header && (declspecs->const_p
3058 || declspecs->volatile_p
3059 || declspecs->restrict_p))
3061 warning (0, "useless type qualifier in empty declaration");
3062 warned = 2;
3065 if (warned != 1)
3067 if (!found_tag)
3068 pedwarn (input_location, 0, "empty declaration");
3073 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3074 bits. SPECS represents declaration specifiers that the grammar
3075 only permits to contain type qualifiers and attributes. */
3078 quals_from_declspecs (const struct c_declspecs *specs)
3080 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3081 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3082 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3083 gcc_assert (!specs->type
3084 && !specs->decl_attr
3085 && specs->typespec_word == cts_none
3086 && specs->storage_class == csc_none
3087 && !specs->typedef_p
3088 && !specs->explicit_signed_p
3089 && !specs->deprecated_p
3090 && !specs->long_p
3091 && !specs->long_long_p
3092 && !specs->short_p
3093 && !specs->signed_p
3094 && !specs->unsigned_p
3095 && !specs->complex_p
3096 && !specs->inline_p
3097 && !specs->thread_p);
3098 return quals;
3101 /* Construct an array declarator. EXPR is the expression inside [],
3102 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3103 applied to the pointer to which a parameter array is converted).
3104 STATIC_P is true if "static" is inside the [], false otherwise.
3105 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3106 length which is nevertheless a complete type, false otherwise. The
3107 field for the contained declarator is left to be filled in by
3108 set_array_declarator_inner. */
3110 struct c_declarator *
3111 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3112 bool vla_unspec_p)
3114 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3115 struct c_declarator);
3116 declarator->kind = cdk_array;
3117 declarator->declarator = 0;
3118 declarator->u.array.dimen = expr;
3119 if (quals)
3121 declarator->u.array.attrs = quals->attrs;
3122 declarator->u.array.quals = quals_from_declspecs (quals);
3124 else
3126 declarator->u.array.attrs = NULL_TREE;
3127 declarator->u.array.quals = 0;
3129 declarator->u.array.static_p = static_p;
3130 declarator->u.array.vla_unspec_p = vla_unspec_p;
3131 if (!flag_isoc99)
3133 if (static_p || quals != NULL)
3134 pedwarn (input_location, OPT_pedantic,
3135 "ISO C90 does not support %<static%> or type "
3136 "qualifiers in parameter array declarators");
3137 if (vla_unspec_p)
3138 pedwarn (input_location, OPT_pedantic,
3139 "ISO C90 does not support %<[*]%> array declarators");
3141 if (vla_unspec_p)
3143 if (!current_scope->parm_flag)
3145 /* C99 6.7.5.2p4 */
3146 error ("%<[*]%> not allowed in other than function prototype scope");
3147 declarator->u.array.vla_unspec_p = false;
3148 return NULL;
3150 current_scope->had_vla_unspec = true;
3152 return declarator;
3155 /* Set the contained declarator of an array declarator. DECL is the
3156 declarator, as constructed by build_array_declarator; INNER is what
3157 appears on the left of the []. */
3159 struct c_declarator *
3160 set_array_declarator_inner (struct c_declarator *decl,
3161 struct c_declarator *inner)
3163 decl->declarator = inner;
3164 return decl;
3167 /* INIT is a constructor that forms DECL's initializer. If the final
3168 element initializes a flexible array field, add the size of that
3169 initializer to DECL's size. */
3171 static void
3172 add_flexible_array_elts_to_size (tree decl, tree init)
3174 tree elt, type;
3176 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3177 return;
3179 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3180 type = TREE_TYPE (elt);
3181 if (TREE_CODE (type) == ARRAY_TYPE
3182 && TYPE_SIZE (type) == NULL_TREE
3183 && TYPE_DOMAIN (type) != NULL_TREE
3184 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3186 complete_array_type (&type, elt, false);
3187 DECL_SIZE (decl)
3188 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3189 DECL_SIZE_UNIT (decl)
3190 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3194 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3195 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3196 before the type name, and set *EXPR_CONST_OPERANDS, if
3197 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3198 appear in a constant expression. */
3200 tree
3201 groktypename (struct c_type_name *type_name, tree *expr,
3202 bool *expr_const_operands)
3204 tree type;
3205 tree attrs = type_name->specs->attrs;
3207 type_name->specs->attrs = NULL_TREE;
3209 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3210 false, NULL, &attrs, expr, expr_const_operands,
3211 DEPRECATED_NORMAL);
3213 /* Apply attributes. */
3214 decl_attributes (&type, attrs, 0);
3216 return type;
3219 /* Decode a declarator in an ordinary declaration or data definition.
3220 This is called as soon as the type information and variable name
3221 have been parsed, before parsing the initializer if any.
3222 Here we create the ..._DECL node, fill in its type,
3223 and put it on the list of decls for the current context.
3224 The ..._DECL node is returned as the value.
3226 Exception: for arrays where the length is not specified,
3227 the type is left null, to be filled in by `finish_decl'.
3229 Function definitions do not come here; they go to start_function
3230 instead. However, external and forward declarations of functions
3231 do go through here. Structure field declarations are done by
3232 grokfield and not through here. */
3234 tree
3235 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3236 bool initialized, tree attributes)
3238 tree decl;
3239 tree tem;
3240 tree expr = NULL_TREE;
3241 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3243 /* An object declared as __attribute__((deprecated)) suppresses
3244 warnings of uses of other deprecated items. */
3245 if (lookup_attribute ("deprecated", attributes))
3246 deprecated_state = DEPRECATED_SUPPRESS;
3248 decl = grokdeclarator (declarator, declspecs,
3249 NORMAL, initialized, NULL, &attributes, &expr, NULL,
3250 deprecated_state);
3251 if (!decl)
3252 return 0;
3254 if (expr)
3255 add_stmt (expr);
3257 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
3258 warning (OPT_Wmain, "%q+D is usually a function", decl);
3260 if (initialized)
3261 /* Is it valid for this decl to have an initializer at all?
3262 If not, set INITIALIZED to zero, which will indirectly
3263 tell 'finish_decl' to ignore the initializer once it is parsed. */
3264 switch (TREE_CODE (decl))
3266 case TYPE_DECL:
3267 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3268 initialized = 0;
3269 break;
3271 case FUNCTION_DECL:
3272 error ("function %qD is initialized like a variable", decl);
3273 initialized = 0;
3274 break;
3276 case PARM_DECL:
3277 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3278 error ("parameter %qD is initialized", decl);
3279 initialized = 0;
3280 break;
3282 default:
3283 /* Don't allow initializations for incomplete types except for
3284 arrays which might be completed by the initialization. */
3286 /* This can happen if the array size is an undefined macro.
3287 We already gave a warning, so we don't need another one. */
3288 if (TREE_TYPE (decl) == error_mark_node)
3289 initialized = 0;
3290 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3292 /* A complete type is ok if size is fixed. */
3294 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3295 || C_DECL_VARIABLE_SIZE (decl))
3297 error ("variable-sized object may not be initialized");
3298 initialized = 0;
3301 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3303 error ("variable %qD has initializer but incomplete type", decl);
3304 initialized = 0;
3306 else if (C_DECL_VARIABLE_SIZE (decl))
3308 /* Although C99 is unclear about whether incomplete arrays
3309 of VLAs themselves count as VLAs, it does not make
3310 sense to permit them to be initialized given that
3311 ordinary VLAs may not be initialized. */
3312 error ("variable-sized object may not be initialized");
3313 initialized = 0;
3317 if (initialized)
3319 if (current_scope == file_scope)
3320 TREE_STATIC (decl) = 1;
3322 /* Tell 'pushdecl' this is an initialized decl
3323 even though we don't yet have the initializer expression.
3324 Also tell 'finish_decl' it may store the real initializer. */
3325 DECL_INITIAL (decl) = error_mark_node;
3328 /* If this is a function declaration, write a record describing it to the
3329 prototypes file (if requested). */
3331 if (TREE_CODE (decl) == FUNCTION_DECL)
3332 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3334 /* ANSI specifies that a tentative definition which is not merged with
3335 a non-tentative definition behaves exactly like a definition with an
3336 initializer equal to zero. (Section 3.7.2)
3338 -fno-common gives strict ANSI behavior, though this tends to break
3339 a large body of code that grew up without this rule.
3341 Thread-local variables are never common, since there's no entrenched
3342 body of code to break, and it allows more efficient variable references
3343 in the presence of dynamic linking. */
3345 if (TREE_CODE (decl) == VAR_DECL
3346 && !initialized
3347 && TREE_PUBLIC (decl)
3348 && !DECL_THREAD_LOCAL_P (decl)
3349 && !flag_no_common)
3350 DECL_COMMON (decl) = 1;
3352 /* Set attributes here so if duplicate decl, will have proper attributes. */
3353 decl_attributes (&decl, attributes, 0);
3355 /* Handle gnu_inline attribute. */
3356 if (declspecs->inline_p
3357 && !flag_gnu89_inline
3358 && TREE_CODE (decl) == FUNCTION_DECL
3359 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
3360 || current_function_decl))
3362 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3364 else if (declspecs->storage_class != csc_static)
3365 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3368 if (TREE_CODE (decl) == FUNCTION_DECL
3369 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3371 struct c_declarator *ce = declarator;
3373 if (ce->kind == cdk_pointer)
3374 ce = declarator->declarator;
3375 if (ce->kind == cdk_function)
3377 tree args = ce->u.arg_info->parms;
3378 for (; args; args = TREE_CHAIN (args))
3380 tree type = TREE_TYPE (args);
3381 if (type && INTEGRAL_TYPE_P (type)
3382 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3383 DECL_ARG_TYPE (args) = integer_type_node;
3388 if (TREE_CODE (decl) == FUNCTION_DECL
3389 && DECL_DECLARED_INLINE_P (decl)
3390 && DECL_UNINLINABLE (decl)
3391 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3392 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3393 decl);
3395 /* C99 6.7.4p3: An inline definition of a function with external
3396 linkage shall not contain a definition of a modifiable object
3397 with static storage duration... */
3398 if (TREE_CODE (decl) == VAR_DECL
3399 && current_scope != file_scope
3400 && TREE_STATIC (decl)
3401 && !TREE_READONLY (decl)
3402 && DECL_DECLARED_INLINE_P (current_function_decl)
3403 && DECL_EXTERNAL (current_function_decl))
3404 record_inline_static (input_location, current_function_decl,
3405 decl, csi_modifiable);
3407 /* Add this decl to the current scope.
3408 TEM may equal DECL or it may be a previous decl of the same name. */
3409 tem = pushdecl (decl);
3411 if (initialized && DECL_EXTERNAL (tem))
3413 DECL_EXTERNAL (tem) = 0;
3414 TREE_STATIC (tem) = 1;
3417 return tem;
3420 /* Initialize EH if not initialized yet and exceptions are enabled. */
3422 void
3423 c_maybe_initialize_eh (void)
3425 if (!flag_exceptions || c_eh_initialized_p)
3426 return;
3428 c_eh_initialized_p = true;
3429 eh_personality_libfunc
3430 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3431 ? "__gcc_personality_sj0"
3432 : "__gcc_personality_v0");
3433 default_init_unwind_resume_libfunc ();
3434 using_eh_for_cleanups ();
3437 /* Finish processing of a declaration;
3438 install its initial value.
3439 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
3440 If the length of an array type is not known before,
3441 it must be determined now, from the initial value, or it is an error. */
3443 void
3444 finish_decl (tree decl, tree init, tree origtype, tree asmspec_tree)
3446 tree type;
3447 int was_incomplete = (DECL_SIZE (decl) == 0);
3448 const char *asmspec = 0;
3450 /* If a name was specified, get the string. */
3451 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3452 && DECL_FILE_SCOPE_P (decl))
3453 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3454 if (asmspec_tree)
3455 asmspec = TREE_STRING_POINTER (asmspec_tree);
3457 /* If `start_decl' didn't like having an initialization, ignore it now. */
3458 if (init != 0 && DECL_INITIAL (decl) == 0)
3459 init = 0;
3461 /* Don't crash if parm is initialized. */
3462 if (TREE_CODE (decl) == PARM_DECL)
3463 init = 0;
3465 if (init)
3466 store_init_value (decl, init, origtype);
3468 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3469 || TREE_CODE (decl) == FUNCTION_DECL
3470 || TREE_CODE (decl) == FIELD_DECL))
3471 objc_check_decl (decl);
3473 type = TREE_TYPE (decl);
3475 /* Deduce size of array from initialization, if not already known. */
3476 if (TREE_CODE (type) == ARRAY_TYPE
3477 && TYPE_DOMAIN (type) == 0
3478 && TREE_CODE (decl) != TYPE_DECL)
3480 bool do_default
3481 = (TREE_STATIC (decl)
3482 /* Even if pedantic, an external linkage array
3483 may have incomplete type at first. */
3484 ? pedantic && !TREE_PUBLIC (decl)
3485 : !DECL_EXTERNAL (decl));
3486 int failure
3487 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3488 do_default);
3490 /* Get the completed type made by complete_array_type. */
3491 type = TREE_TYPE (decl);
3493 switch (failure)
3495 case 1:
3496 error ("initializer fails to determine size of %q+D", decl);
3497 break;
3499 case 2:
3500 if (do_default)
3501 error ("array size missing in %q+D", decl);
3502 /* If a `static' var's size isn't known,
3503 make it extern as well as static, so it does not get
3504 allocated.
3505 If it is not `static', then do not mark extern;
3506 finish_incomplete_decl will give it a default size
3507 and it will get allocated. */
3508 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3509 DECL_EXTERNAL (decl) = 1;
3510 break;
3512 case 3:
3513 error ("zero or negative size array %q+D", decl);
3514 break;
3516 case 0:
3517 /* For global variables, update the copy of the type that
3518 exists in the binding. */
3519 if (TREE_PUBLIC (decl))
3521 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3522 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3523 b_ext = b_ext->shadowed;
3524 if (b_ext)
3526 if (b_ext->type)
3527 b_ext->type = composite_type (b_ext->type, type);
3528 else
3529 b_ext->type = type;
3532 break;
3534 default:
3535 gcc_unreachable ();
3538 if (DECL_INITIAL (decl))
3539 TREE_TYPE (DECL_INITIAL (decl)) = type;
3541 layout_decl (decl, 0);
3544 if (TREE_CODE (decl) == VAR_DECL)
3546 if (init && TREE_CODE (init) == CONSTRUCTOR)
3547 add_flexible_array_elts_to_size (decl, init);
3549 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3550 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3551 layout_decl (decl, 0);
3553 if (DECL_SIZE (decl) == 0
3554 /* Don't give an error if we already gave one earlier. */
3555 && TREE_TYPE (decl) != error_mark_node
3556 && (TREE_STATIC (decl)
3557 /* A static variable with an incomplete type
3558 is an error if it is initialized.
3559 Also if it is not file scope.
3560 Otherwise, let it through, but if it is not `extern'
3561 then it may cause an error message later. */
3562 ? (DECL_INITIAL (decl) != 0
3563 || !DECL_FILE_SCOPE_P (decl))
3564 /* An automatic variable with an incomplete type
3565 is an error. */
3566 : !DECL_EXTERNAL (decl)))
3568 error ("storage size of %q+D isn%'t known", decl);
3569 TREE_TYPE (decl) = error_mark_node;
3572 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3573 && DECL_SIZE (decl) != 0)
3575 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3576 constant_expression_warning (DECL_SIZE (decl));
3577 else
3579 error ("storage size of %q+D isn%'t constant", decl);
3580 TREE_TYPE (decl) = error_mark_node;
3584 if (TREE_USED (type))
3585 TREE_USED (decl) = 1;
3588 /* If this is a function and an assembler name is specified, reset DECL_RTL
3589 so we can give it its new name. Also, update built_in_decls if it
3590 was a normal built-in. */
3591 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3593 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3594 set_builtin_user_assembler_name (decl, asmspec);
3595 set_user_assembler_name (decl, asmspec);
3598 /* If #pragma weak was used, mark the decl weak now. */
3599 maybe_apply_pragma_weak (decl);
3601 /* Output the assembler code and/or RTL code for variables and functions,
3602 unless the type is an undefined structure or union.
3603 If not, it will get done when the type is completed. */
3605 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3607 /* Determine the ELF visibility. */
3608 if (TREE_PUBLIC (decl))
3609 c_determine_visibility (decl);
3611 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3612 if (c_dialect_objc ())
3613 objc_check_decl (decl);
3615 if (asmspec)
3617 /* If this is not a static variable, issue a warning.
3618 It doesn't make any sense to give an ASMSPEC for an
3619 ordinary, non-register local variable. Historically,
3620 GCC has accepted -- but ignored -- the ASMSPEC in
3621 this case. */
3622 if (!DECL_FILE_SCOPE_P (decl)
3623 && TREE_CODE (decl) == VAR_DECL
3624 && !C_DECL_REGISTER (decl)
3625 && !TREE_STATIC (decl))
3626 warning (0, "ignoring asm-specifier for non-static local "
3627 "variable %q+D", decl);
3628 else
3629 set_user_assembler_name (decl, asmspec);
3632 if (DECL_FILE_SCOPE_P (decl))
3634 if (DECL_INITIAL (decl) == NULL_TREE
3635 || DECL_INITIAL (decl) == error_mark_node)
3636 /* Don't output anything
3637 when a tentative file-scope definition is seen.
3638 But at end of compilation, do output code for them. */
3639 DECL_DEFER_OUTPUT (decl) = 1;
3640 rest_of_decl_compilation (decl, true, 0);
3642 else
3644 /* In conjunction with an ASMSPEC, the `register'
3645 keyword indicates that we should place the variable
3646 in a particular register. */
3647 if (asmspec && C_DECL_REGISTER (decl))
3649 DECL_HARD_REGISTER (decl) = 1;
3650 /* This cannot be done for a structure with volatile
3651 fields, on which DECL_REGISTER will have been
3652 reset. */
3653 if (!DECL_REGISTER (decl))
3654 error ("cannot put object with volatile field into register");
3657 if (TREE_CODE (decl) != FUNCTION_DECL)
3659 /* If we're building a variable sized type, and we might be
3660 reachable other than via the top of the current binding
3661 level, then create a new BIND_EXPR so that we deallocate
3662 the object at the right time. */
3663 /* Note that DECL_SIZE can be null due to errors. */
3664 if (DECL_SIZE (decl)
3665 && !TREE_CONSTANT (DECL_SIZE (decl))
3666 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3668 tree bind;
3669 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3670 TREE_SIDE_EFFECTS (bind) = 1;
3671 add_stmt (bind);
3672 BIND_EXPR_BODY (bind) = push_stmt_list ();
3674 add_stmt (build_stmt (DECL_EXPR, decl));
3679 if (!DECL_FILE_SCOPE_P (decl))
3681 /* Recompute the RTL of a local array now
3682 if it used to be an incomplete type. */
3683 if (was_incomplete
3684 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3686 /* If we used it already as memory, it must stay in memory. */
3687 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3688 /* If it's still incomplete now, no init will save it. */
3689 if (DECL_SIZE (decl) == 0)
3690 DECL_INITIAL (decl) = 0;
3695 if (TREE_CODE (decl) == TYPE_DECL)
3697 if (!DECL_FILE_SCOPE_P (decl)
3698 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3699 add_stmt (build_stmt (DECL_EXPR, decl));
3701 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3704 /* At the end of a declaration, throw away any variable type sizes
3705 of types defined inside that declaration. There is no use
3706 computing them in the following function definition. */
3707 if (current_scope == file_scope)
3708 get_pending_sizes ();
3710 /* Install a cleanup (aka destructor) if one was given. */
3711 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3713 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3714 if (attr)
3716 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3717 tree cleanup_decl = lookup_name (cleanup_id);
3718 tree cleanup;
3719 VEC(tree,gc) *vec;
3721 /* Build "cleanup(&decl)" for the destructor. */
3722 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
3723 vec = VEC_alloc (tree, gc, 1);
3724 VEC_quick_push (tree, vec, cleanup);
3725 cleanup = build_function_call_vec (cleanup_decl, vec, NULL);
3726 VEC_free (tree, gc, vec);
3728 /* Don't warn about decl unused; the cleanup uses it. */
3729 TREE_USED (decl) = 1;
3730 TREE_USED (cleanup_decl) = 1;
3732 /* Initialize EH, if we've been told to do so. */
3733 c_maybe_initialize_eh ();
3735 push_cleanup (decl, cleanup, false);
3740 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3742 tree
3743 grokparm (const struct c_parm *parm)
3745 tree attrs = parm->attrs;
3746 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3747 NULL, &attrs, NULL, NULL, DEPRECATED_NORMAL);
3749 decl_attributes (&decl, attrs, 0);
3751 return decl;
3754 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3755 and push that on the current scope. */
3757 void
3758 push_parm_decl (const struct c_parm *parm)
3760 tree attrs = parm->attrs;
3761 tree decl;
3763 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
3764 &attrs, NULL, NULL, DEPRECATED_NORMAL);
3765 decl_attributes (&decl, attrs, 0);
3767 decl = pushdecl (decl);
3769 finish_decl (decl, NULL_TREE, NULL_TREE, NULL_TREE);
3772 /* Mark all the parameter declarations to date as forward decls.
3773 Also diagnose use of this extension. */
3775 void
3776 mark_forward_parm_decls (void)
3778 struct c_binding *b;
3780 if (pedantic && !current_scope->warned_forward_parm_decls)
3782 pedwarn (input_location, OPT_pedantic,
3783 "ISO C forbids forward parameter declarations");
3784 current_scope->warned_forward_parm_decls = true;
3787 for (b = current_scope->bindings; b; b = b->prev)
3788 if (TREE_CODE (b->decl) == PARM_DECL)
3789 TREE_ASM_WRITTEN (b->decl) = 1;
3792 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3793 literal, which may be an incomplete array type completed by the
3794 initializer; INIT is a CONSTRUCTOR that initializes the compound
3795 literal. NON_CONST is true if the initializers contain something
3796 that cannot occur in a constant expression. */
3798 tree
3799 build_compound_literal (tree type, tree init, bool non_const)
3801 /* We do not use start_decl here because we have a type, not a declarator;
3802 and do not use finish_decl because the decl should be stored inside
3803 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3804 tree decl;
3805 tree complit;
3806 tree stmt;
3808 if (type == error_mark_node)
3809 return error_mark_node;
3811 decl = build_decl (VAR_DECL, NULL_TREE, type);
3812 DECL_EXTERNAL (decl) = 0;
3813 TREE_PUBLIC (decl) = 0;
3814 TREE_STATIC (decl) = (current_scope == file_scope);
3815 DECL_CONTEXT (decl) = current_function_decl;
3816 TREE_USED (decl) = 1;
3817 TREE_TYPE (decl) = type;
3818 TREE_READONLY (decl) = TYPE_READONLY (type);
3819 store_init_value (decl, init, NULL_TREE);
3821 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3823 int failure = complete_array_type (&TREE_TYPE (decl),
3824 DECL_INITIAL (decl), true);
3825 gcc_assert (!failure);
3827 type = TREE_TYPE (decl);
3828 TREE_TYPE (DECL_INITIAL (decl)) = type;
3831 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3832 return error_mark_node;
3834 stmt = build_stmt (DECL_EXPR, decl);
3835 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3836 TREE_SIDE_EFFECTS (complit) = 1;
3838 layout_decl (decl, 0);
3840 if (TREE_STATIC (decl))
3842 /* This decl needs a name for the assembler output. */
3843 set_compound_literal_name (decl);
3844 DECL_DEFER_OUTPUT (decl) = 1;
3845 DECL_COMDAT (decl) = 1;
3846 DECL_ARTIFICIAL (decl) = 1;
3847 DECL_IGNORED_P (decl) = 1;
3848 pushdecl (decl);
3849 rest_of_decl_compilation (decl, 1, 0);
3852 if (non_const)
3854 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
3855 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
3858 return complit;
3861 /* Determine whether TYPE is a structure with a flexible array member,
3862 or a union containing such a structure (possibly recursively). */
3864 static bool
3865 flexible_array_type_p (tree type)
3867 tree x;
3868 switch (TREE_CODE (type))
3870 case RECORD_TYPE:
3871 x = TYPE_FIELDS (type);
3872 if (x == NULL_TREE)
3873 return false;
3874 while (TREE_CHAIN (x) != NULL_TREE)
3875 x = TREE_CHAIN (x);
3876 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3877 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3878 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3879 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3880 return true;
3881 return false;
3882 case UNION_TYPE:
3883 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3885 if (flexible_array_type_p (TREE_TYPE (x)))
3886 return true;
3888 return false;
3889 default:
3890 return false;
3894 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3895 replacing with appropriate values if they are invalid. */
3896 static void
3897 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3899 tree type_mv;
3900 unsigned int max_width;
3901 unsigned HOST_WIDE_INT w;
3902 const char *name = orig_name ? orig_name: _("<anonymous>");
3904 /* Detect and ignore out of range field width and process valid
3905 field widths. */
3906 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3907 || TREE_CODE (*width) != INTEGER_CST)
3909 error ("bit-field %qs width not an integer constant", name);
3910 *width = integer_one_node;
3912 else
3914 constant_expression_warning (*width);
3915 if (tree_int_cst_sgn (*width) < 0)
3917 error ("negative width in bit-field %qs", name);
3918 *width = integer_one_node;
3920 else if (integer_zerop (*width) && orig_name)
3922 error ("zero width for bit-field %qs", name);
3923 *width = integer_one_node;
3927 /* Detect invalid bit-field type. */
3928 if (TREE_CODE (*type) != INTEGER_TYPE
3929 && TREE_CODE (*type) != BOOLEAN_TYPE
3930 && TREE_CODE (*type) != ENUMERAL_TYPE)
3932 error ("bit-field %qs has invalid type", name);
3933 *type = unsigned_type_node;
3936 type_mv = TYPE_MAIN_VARIANT (*type);
3937 if (!in_system_header
3938 && type_mv != integer_type_node
3939 && type_mv != unsigned_type_node
3940 && type_mv != boolean_type_node)
3941 pedwarn (input_location, OPT_pedantic,
3942 "type of bit-field %qs is a GCC extension", name);
3944 max_width = TYPE_PRECISION (*type);
3946 if (0 < compare_tree_int (*width, max_width))
3948 error ("width of %qs exceeds its type", name);
3949 w = max_width;
3950 *width = build_int_cst (NULL_TREE, w);
3952 else
3953 w = tree_low_cst (*width, 1);
3955 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3957 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3958 if (!lt
3959 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3960 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3961 warning (0, "%qs is narrower than values of its type", name);
3967 /* Print warning about variable length array if necessary. */
3969 static void
3970 warn_variable_length_array (const char *name, tree size)
3972 int const_size = TREE_CONSTANT (size);
3974 if (!flag_isoc99 && pedantic && warn_vla != 0)
3976 if (const_size)
3978 if (name)
3979 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array %qs whose size "
3980 "can%'t be evaluated",
3981 name);
3982 else
3983 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
3984 "can%'t be evaluated");
3986 else
3988 if (name)
3989 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array %qs",
3990 name);
3991 else
3992 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
3995 else if (warn_vla > 0)
3997 if (const_size)
3999 if (name)
4000 warning (OPT_Wvla,
4001 "the size of array %qs can"
4002 "%'t be evaluated", name);
4003 else
4004 warning (OPT_Wvla,
4005 "the size of array can %'t be evaluated");
4007 else
4009 if (name)
4010 warning (OPT_Wvla,
4011 "variable length array %qs is used",
4012 name);
4013 else
4014 warning (OPT_Wvla,
4015 "variable length array is used");
4020 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR to
4021 serve as the actual size-expression for a type or decl. This is
4022 like variable_size in stor-layout.c, but we make global_bindings_p
4023 return negative to avoid calls to that function from outside the
4024 front end resulting in errors at file scope, then call this version
4025 instead from front-end code. */
4027 static tree
4028 c_variable_size (tree size)
4030 tree save;
4032 if (TREE_CONSTANT (size))
4033 return size;
4035 size = save_expr (size);
4037 save = skip_simple_arithmetic (size);
4039 if (cfun && cfun->dont_save_pending_sizes_p)
4040 return size;
4042 if (!global_bindings_p ())
4043 put_pending_size (save);
4045 return size;
4048 /* Given declspecs and a declarator,
4049 determine the name and type of the object declared
4050 and construct a ..._DECL node for it.
4051 (In one case we can return a ..._TYPE node instead.
4052 For invalid input we sometimes return 0.)
4054 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4056 DECL_CONTEXT says which syntactic context this declaration is in:
4057 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4058 FUNCDEF for a function definition. Like NORMAL but a few different
4059 error messages in each case. Return value may be zero meaning
4060 this definition is too screwy to try to parse.
4061 PARM for a parameter declaration (either within a function prototype
4062 or before a function body). Make a PARM_DECL, or return void_type_node.
4063 TYPENAME if for a typename (in a cast or sizeof).
4064 Don't make a DECL node; just return the ..._TYPE node.
4065 FIELD for a struct or union field; make a FIELD_DECL.
4066 INITIALIZED is true if the decl has an initializer.
4067 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4068 representing the width of the bit-field.
4069 DECL_ATTRS points to the list of attributes that should be added to this
4070 decl. Any nested attributes that belong on the decl itself will be
4071 added to this list.
4072 If EXPR is not NULL, any expressions that need to be evaluated as
4073 part of evaluating variably modified types will be stored in *EXPR.
4074 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4075 set to indicate whether operands in *EXPR can be used in constant
4076 expressions.
4077 DEPRECATED_STATE is a deprecated_states value indicating whether
4078 deprecation warnings should be suppressed.
4080 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4081 It may also be so in the PARM case, for a prototype where the
4082 argument type is specified but not the name.
4084 This function is where the complicated C meanings of `static'
4085 and `extern' are interpreted. */
4087 static tree
4088 grokdeclarator (const struct c_declarator *declarator,
4089 struct c_declspecs *declspecs,
4090 enum decl_context decl_context, bool initialized, tree *width,
4091 tree *decl_attrs, tree *expr, bool *expr_const_operands,
4092 enum deprecated_states deprecated_state)
4094 tree type = declspecs->type;
4095 bool threadp = declspecs->thread_p;
4096 enum c_storage_class storage_class = declspecs->storage_class;
4097 int constp;
4098 int restrictp;
4099 int volatilep;
4100 int type_quals = TYPE_UNQUALIFIED;
4101 const char *name, *orig_name;
4102 bool funcdef_flag = false;
4103 bool funcdef_syntax = false;
4104 int size_varies = 0;
4105 tree decl_attr = declspecs->decl_attr;
4106 int array_ptr_quals = TYPE_UNQUALIFIED;
4107 tree array_ptr_attrs = NULL_TREE;
4108 int array_parm_static = 0;
4109 bool array_parm_vla_unspec_p = false;
4110 tree returned_attrs = NULL_TREE;
4111 bool bitfield = width != NULL;
4112 tree element_type;
4113 struct c_arg_info *arg_info = 0;
4114 tree expr_dummy;
4115 bool expr_const_operands_dummy;
4117 if (expr == NULL)
4118 expr = &expr_dummy;
4119 if (expr_const_operands == NULL)
4120 expr_const_operands = &expr_const_operands_dummy;
4122 *expr = declspecs->expr;
4123 *expr_const_operands = declspecs->expr_const_operands;
4125 if (decl_context == FUNCDEF)
4126 funcdef_flag = true, decl_context = NORMAL;
4128 /* Look inside a declarator for the name being declared
4129 and get it as a string, for an error message. */
4131 const struct c_declarator *decl = declarator;
4132 name = 0;
4134 while (decl)
4135 switch (decl->kind)
4137 case cdk_function:
4138 case cdk_array:
4139 case cdk_pointer:
4140 funcdef_syntax = (decl->kind == cdk_function);
4141 decl = decl->declarator;
4142 break;
4144 case cdk_attrs:
4145 decl = decl->declarator;
4146 break;
4148 case cdk_id:
4149 if (decl->u.id)
4150 name = IDENTIFIER_POINTER (decl->u.id);
4151 decl = 0;
4152 break;
4154 default:
4155 gcc_unreachable ();
4157 orig_name = name;
4158 if (name == 0)
4159 name = "type name";
4162 /* A function definition's declarator must have the form of
4163 a function declarator. */
4165 if (funcdef_flag && !funcdef_syntax)
4166 return 0;
4168 /* If this looks like a function definition, make it one,
4169 even if it occurs where parms are expected.
4170 Then store_parm_decls will reject it and not use it as a parm. */
4171 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4172 decl_context = PARM;
4174 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4175 warn_deprecated_use (declspecs->type);
4177 if ((decl_context == NORMAL || decl_context == FIELD)
4178 && current_scope == file_scope
4179 && variably_modified_type_p (type, NULL_TREE))
4181 error ("variably modified %qs at file scope", name);
4182 type = integer_type_node;
4185 size_varies = C_TYPE_VARIABLE_SIZE (type);
4187 /* Diagnose defaulting to "int". */
4189 if (declspecs->default_int_p && !in_system_header)
4191 /* Issue a warning if this is an ISO C 99 program or if
4192 -Wreturn-type and this is a function, or if -Wimplicit;
4193 prefer the former warning since it is more explicit. */
4194 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4195 && funcdef_flag)
4196 warn_about_return_type = 1;
4197 else
4198 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4199 "type defaults to %<int%> in declaration of %qs", name);
4202 /* Adjust the type if a bit-field is being declared,
4203 -funsigned-bitfields applied and the type is not explicitly
4204 "signed". */
4205 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4206 && TREE_CODE (type) == INTEGER_TYPE)
4207 type = unsigned_type_for (type);
4209 /* Figure out the type qualifiers for the declaration. There are
4210 two ways a declaration can become qualified. One is something
4211 like `const int i' where the `const' is explicit. Another is
4212 something like `typedef const int CI; CI i' where the type of the
4213 declaration contains the `const'. A third possibility is that
4214 there is a type qualifier on the element type of a typedefed
4215 array type, in which case we should extract that qualifier so
4216 that c_apply_type_quals_to_decl receives the full list of
4217 qualifiers to work with (C90 is not entirely clear about whether
4218 duplicate qualifiers should be diagnosed in this case, but it
4219 seems most appropriate to do so). */
4220 element_type = strip_array_types (type);
4221 constp = declspecs->const_p + TYPE_READONLY (element_type);
4222 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4223 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4224 if (pedantic && !flag_isoc99)
4226 if (constp > 1)
4227 pedwarn (input_location, OPT_pedantic, "duplicate %<const%>");
4228 if (restrictp > 1)
4229 pedwarn (input_location, OPT_pedantic, "duplicate %<restrict%>");
4230 if (volatilep > 1)
4231 pedwarn (input_location, OPT_pedantic, "duplicate %<volatile%>");
4233 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4234 type = TYPE_MAIN_VARIANT (type);
4235 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4236 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4237 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4239 /* Warn about storage classes that are invalid for certain
4240 kinds of declarations (parameters, typenames, etc.). */
4242 if (funcdef_flag
4243 && (threadp
4244 || storage_class == csc_auto
4245 || storage_class == csc_register
4246 || storage_class == csc_typedef))
4248 if (storage_class == csc_auto)
4249 pedwarn (input_location,
4250 (current_scope == file_scope) ? 0 : OPT_pedantic,
4251 "function definition declared %<auto%>");
4252 if (storage_class == csc_register)
4253 error ("function definition declared %<register%>");
4254 if (storage_class == csc_typedef)
4255 error ("function definition declared %<typedef%>");
4256 if (threadp)
4257 error ("function definition declared %<__thread%>");
4258 threadp = false;
4259 if (storage_class == csc_auto
4260 || storage_class == csc_register
4261 || storage_class == csc_typedef)
4262 storage_class = csc_none;
4264 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4266 if (decl_context == PARM && storage_class == csc_register)
4268 else
4270 switch (decl_context)
4272 case FIELD:
4273 error ("storage class specified for structure field %qs",
4274 name);
4275 break;
4276 case PARM:
4277 error ("storage class specified for parameter %qs", name);
4278 break;
4279 default:
4280 error ("storage class specified for typename");
4281 break;
4283 storage_class = csc_none;
4284 threadp = false;
4287 else if (storage_class == csc_extern
4288 && initialized
4289 && !funcdef_flag)
4291 /* 'extern' with initialization is invalid if not at file scope. */
4292 if (current_scope == file_scope)
4294 /* It is fine to have 'extern const' when compiling at C
4295 and C++ intersection. */
4296 if (!(warn_cxx_compat && constp))
4297 warning (0, "%qs initialized and declared %<extern%>", name);
4299 else
4300 error ("%qs has both %<extern%> and initializer", name);
4302 else if (current_scope == file_scope)
4304 if (storage_class == csc_auto)
4305 error ("file-scope declaration of %qs specifies %<auto%>", name);
4306 if (pedantic && storage_class == csc_register)
4307 pedwarn (input_location, OPT_pedantic,
4308 "file-scope declaration of %qs specifies %<register%>", name);
4310 else
4312 if (storage_class == csc_extern && funcdef_flag)
4313 error ("nested function %qs declared %<extern%>", name);
4314 else if (threadp && storage_class == csc_none)
4316 error ("function-scope %qs implicitly auto and declared "
4317 "%<__thread%>",
4318 name);
4319 threadp = false;
4323 /* Now figure out the structure of the declarator proper.
4324 Descend through it, creating more complex types, until we reach
4325 the declared identifier (or NULL_TREE, in an absolute declarator).
4326 At each stage we maintain an unqualified version of the type
4327 together with any qualifiers that should be applied to it with
4328 c_build_qualified_type; this way, array types including
4329 multidimensional array types are first built up in unqualified
4330 form and then the qualified form is created with
4331 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4333 while (declarator && declarator->kind != cdk_id)
4335 if (type == error_mark_node)
4337 declarator = declarator->declarator;
4338 continue;
4341 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4342 a cdk_pointer (for *...),
4343 a cdk_function (for ...(...)),
4344 a cdk_attrs (for nested attributes),
4345 or a cdk_id (for the name being declared
4346 or the place in an absolute declarator
4347 where the name was omitted).
4348 For the last case, we have just exited the loop.
4350 At this point, TYPE is the type of elements of an array,
4351 or for a function to return, or for a pointer to point to.
4352 After this sequence of ifs, TYPE is the type of the
4353 array or function or pointer, and DECLARATOR has had its
4354 outermost layer removed. */
4356 if (array_ptr_quals != TYPE_UNQUALIFIED
4357 || array_ptr_attrs != NULL_TREE
4358 || array_parm_static)
4360 /* Only the innermost declarator (making a parameter be of
4361 array type which is converted to pointer type)
4362 may have static or type qualifiers. */
4363 error ("static or type qualifiers in non-parameter array declarator");
4364 array_ptr_quals = TYPE_UNQUALIFIED;
4365 array_ptr_attrs = NULL_TREE;
4366 array_parm_static = 0;
4369 switch (declarator->kind)
4371 case cdk_attrs:
4373 /* A declarator with embedded attributes. */
4374 tree attrs = declarator->u.attrs;
4375 const struct c_declarator *inner_decl;
4376 int attr_flags = 0;
4377 declarator = declarator->declarator;
4378 inner_decl = declarator;
4379 while (inner_decl->kind == cdk_attrs)
4380 inner_decl = inner_decl->declarator;
4381 if (inner_decl->kind == cdk_id)
4382 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4383 else if (inner_decl->kind == cdk_function)
4384 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4385 else if (inner_decl->kind == cdk_array)
4386 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4387 returned_attrs = decl_attributes (&type,
4388 chainon (returned_attrs, attrs),
4389 attr_flags);
4390 break;
4392 case cdk_array:
4394 tree itype = NULL_TREE;
4395 tree size = declarator->u.array.dimen;
4396 /* The index is a signed object `sizetype' bits wide. */
4397 tree index_type = c_common_signed_type (sizetype);
4399 array_ptr_quals = declarator->u.array.quals;
4400 array_ptr_attrs = declarator->u.array.attrs;
4401 array_parm_static = declarator->u.array.static_p;
4402 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4404 declarator = declarator->declarator;
4406 /* Check for some types that there cannot be arrays of. */
4408 if (VOID_TYPE_P (type))
4410 error ("declaration of %qs as array of voids", name);
4411 type = error_mark_node;
4414 if (TREE_CODE (type) == FUNCTION_TYPE)
4416 error ("declaration of %qs as array of functions", name);
4417 type = error_mark_node;
4420 if (pedantic && !in_system_header && flexible_array_type_p (type))
4421 pedwarn (input_location, OPT_pedantic,
4422 "invalid use of structure with flexible array member");
4424 if (size == error_mark_node)
4425 type = error_mark_node;
4427 if (type == error_mark_node)
4428 continue;
4430 /* If size was specified, set ITYPE to a range-type for
4431 that size. Otherwise, ITYPE remains null. finish_decl
4432 may figure it out from an initial value. */
4434 if (size)
4436 bool size_maybe_const = true;
4437 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
4438 && !TREE_OVERFLOW (size));
4439 bool this_size_varies = false;
4441 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4442 lvalue. */
4443 STRIP_TYPE_NOPS (size);
4445 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4447 error ("size of array %qs has non-integer type", name);
4448 size = integer_one_node;
4451 size = c_fully_fold (size, false, &size_maybe_const);
4453 if (pedantic && size_maybe_const && integer_zerop (size))
4454 pedwarn (input_location, OPT_pedantic,
4455 "ISO C forbids zero-size array %qs", name);
4457 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
4459 constant_expression_warning (size);
4460 if (tree_int_cst_sgn (size) < 0)
4462 error ("size of array %qs is negative", name);
4463 size = integer_one_node;
4465 /* Handle a size folded to an integer constant but
4466 not an integer constant expression. */
4467 if (!size_int_const)
4469 /* If this is a file scope declaration of an
4470 ordinary identifier, this is invalid code;
4471 diagnosing it here and not subsequently
4472 treating the type as variable-length avoids
4473 more confusing diagnostics later. */
4474 if ((decl_context == NORMAL || decl_context == FIELD)
4475 && current_scope == file_scope)
4476 pedwarn (input_location, 0,
4477 "variably modified %qs at file scope", name);
4478 else
4479 this_size_varies = size_varies = 1;
4480 warn_variable_length_array (orig_name, size);
4483 else if ((decl_context == NORMAL || decl_context == FIELD)
4484 && current_scope == file_scope)
4486 error ("variably modified %qs at file scope", name);
4487 size = integer_one_node;
4489 else
4491 /* Make sure the array size remains visibly
4492 nonconstant even if it is (eg) a const variable
4493 with known value. */
4494 this_size_varies = size_varies = 1;
4495 warn_variable_length_array (orig_name, size);
4498 if (integer_zerop (size) && !this_size_varies)
4500 /* A zero-length array cannot be represented with
4501 an unsigned index type, which is what we'll
4502 get with build_index_type. Create an
4503 open-ended range instead. */
4504 itype = build_range_type (sizetype, size, NULL_TREE);
4506 else
4508 /* Arrange for the SAVE_EXPR on the inside of the
4509 MINUS_EXPR, which allows the -1 to get folded
4510 with the +1 that happens when building TYPE_SIZE. */
4511 if (size_varies)
4512 size = c_variable_size (size);
4513 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
4514 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4515 integer_zero_node, size);
4517 /* Compute the maximum valid index, that is, size
4518 - 1. Do the calculation in index_type, so that
4519 if it is a variable the computations will be
4520 done in the proper mode. */
4521 itype = fold_build2 (MINUS_EXPR, index_type,
4522 convert (index_type, size),
4523 convert (index_type,
4524 size_one_node));
4526 /* If that overflowed, the array is too big. ???
4527 While a size of INT_MAX+1 technically shouldn't
4528 cause an overflow (because we subtract 1), the
4529 overflow is recorded during the conversion to
4530 index_type, before the subtraction. Handling
4531 this case seems like an unnecessary
4532 complication. */
4533 if (TREE_CODE (itype) == INTEGER_CST
4534 && TREE_OVERFLOW (itype))
4536 error ("size of array %qs is too large", name);
4537 type = error_mark_node;
4538 continue;
4541 itype = build_index_type (itype);
4543 if (this_size_varies)
4545 if (*expr)
4546 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
4547 *expr, size);
4548 else
4549 *expr = size;
4550 *expr_const_operands &= size_maybe_const;
4553 else if (decl_context == FIELD)
4555 bool flexible_array_member = false;
4556 if (array_parm_vla_unspec_p)
4557 /* Field names can in fact have function prototype
4558 scope so [*] is disallowed here through making
4559 the field variably modified, not through being
4560 something other than a declaration with function
4561 prototype scope. */
4562 size_varies = 1;
4563 else
4565 const struct c_declarator *t = declarator;
4566 while (t->kind == cdk_attrs)
4567 t = t->declarator;
4568 flexible_array_member = (t->kind == cdk_id);
4570 if (flexible_array_member
4571 && pedantic && !flag_isoc99 && !in_system_header)
4572 pedwarn (input_location, OPT_pedantic,
4573 "ISO C90 does not support flexible array members");
4575 /* ISO C99 Flexible array members are effectively
4576 identical to GCC's zero-length array extension. */
4577 if (flexible_array_member || array_parm_vla_unspec_p)
4578 itype = build_range_type (sizetype, size_zero_node,
4579 NULL_TREE);
4581 else if (decl_context == PARM)
4583 if (array_parm_vla_unspec_p)
4585 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4586 size_varies = 1;
4589 else if (decl_context == TYPENAME)
4591 if (array_parm_vla_unspec_p)
4593 /* C99 6.7.5.2p4 */
4594 warning (0, "%<[*]%> not in a declaration");
4595 /* We use this to avoid messing up with incomplete
4596 array types of the same type, that would
4597 otherwise be modified below. */
4598 itype = build_range_type (sizetype, size_zero_node,
4599 NULL_TREE);
4600 size_varies = 1;
4604 /* Complain about arrays of incomplete types. */
4605 if (!COMPLETE_TYPE_P (type))
4607 error ("array type has incomplete element type");
4608 type = error_mark_node;
4610 else
4611 /* When itype is NULL, a shared incomplete array type is
4612 returned for all array of a given type. Elsewhere we
4613 make sure we don't complete that type before copying
4614 it, but here we want to make sure we don't ever
4615 modify the shared type, so we gcc_assert (itype)
4616 below. */
4617 type = build_array_type (type, itype);
4619 if (type != error_mark_node)
4621 if (size_varies)
4623 /* It is ok to modify type here even if itype is
4624 NULL: if size_varies, we're in a
4625 multi-dimensional array and the inner type has
4626 variable size, so the enclosing shared array type
4627 must too. */
4628 if (size && TREE_CODE (size) == INTEGER_CST)
4629 type
4630 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4631 C_TYPE_VARIABLE_SIZE (type) = 1;
4634 /* The GCC extension for zero-length arrays differs from
4635 ISO flexible array members in that sizeof yields
4636 zero. */
4637 if (size && integer_zerop (size))
4639 gcc_assert (itype);
4640 TYPE_SIZE (type) = bitsize_zero_node;
4641 TYPE_SIZE_UNIT (type) = size_zero_node;
4643 if (array_parm_vla_unspec_p)
4645 gcc_assert (itype);
4646 /* The type is complete. C99 6.7.5.2p4 */
4647 TYPE_SIZE (type) = bitsize_zero_node;
4648 TYPE_SIZE_UNIT (type) = size_zero_node;
4652 if (decl_context != PARM
4653 && (array_ptr_quals != TYPE_UNQUALIFIED
4654 || array_ptr_attrs != NULL_TREE
4655 || array_parm_static))
4657 error ("static or type qualifiers in non-parameter array declarator");
4658 array_ptr_quals = TYPE_UNQUALIFIED;
4659 array_ptr_attrs = NULL_TREE;
4660 array_parm_static = 0;
4662 break;
4664 case cdk_function:
4666 /* Say it's a definition only for the declarator closest
4667 to the identifier, apart possibly from some
4668 attributes. */
4669 bool really_funcdef = false;
4670 tree arg_types;
4671 if (funcdef_flag)
4673 const struct c_declarator *t = declarator->declarator;
4674 while (t->kind == cdk_attrs)
4675 t = t->declarator;
4676 really_funcdef = (t->kind == cdk_id);
4679 /* Declaring a function type. Make sure we have a valid
4680 type for the function to return. */
4681 if (type == error_mark_node)
4682 continue;
4684 size_varies = 0;
4686 /* Warn about some types functions can't return. */
4687 if (TREE_CODE (type) == FUNCTION_TYPE)
4689 error ("%qs declared as function returning a function", name);
4690 type = integer_type_node;
4692 if (TREE_CODE (type) == ARRAY_TYPE)
4694 error ("%qs declared as function returning an array", name);
4695 type = integer_type_node;
4698 /* Construct the function type and go to the next
4699 inner layer of declarator. */
4700 arg_info = declarator->u.arg_info;
4701 arg_types = grokparms (arg_info, really_funcdef);
4702 if (really_funcdef)
4703 put_pending_sizes (arg_info->pending_sizes);
4705 /* Type qualifiers before the return type of the function
4706 qualify the return type, not the function type. */
4707 if (type_quals)
4709 /* Type qualifiers on a function return type are
4710 normally permitted by the standard but have no
4711 effect, so give a warning at -Wreturn-type.
4712 Qualifiers on a void return type are banned on
4713 function definitions in ISO C; GCC used to used
4714 them for noreturn functions. */
4715 if (VOID_TYPE_P (type) && really_funcdef)
4716 pedwarn (input_location, 0,
4717 "function definition has qualified void return type");
4718 else
4719 warning (OPT_Wignored_qualifiers,
4720 "type qualifiers ignored on function return type");
4722 type = c_build_qualified_type (type, type_quals);
4724 type_quals = TYPE_UNQUALIFIED;
4726 type = build_function_type (type, arg_types);
4727 declarator = declarator->declarator;
4729 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4730 the formal parameter list of this FUNCTION_TYPE to point to
4731 the FUNCTION_TYPE node itself. */
4733 tree link;
4735 for (link = arg_info->tags;
4736 link;
4737 link = TREE_CHAIN (link))
4738 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4740 break;
4742 case cdk_pointer:
4744 /* Merge any constancy or volatility into the target type
4745 for the pointer. */
4747 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4748 && type_quals)
4749 pedwarn (input_location, OPT_pedantic,
4750 "ISO C forbids qualified function types");
4751 if (type_quals)
4752 type = c_build_qualified_type (type, type_quals);
4753 size_varies = 0;
4755 /* When the pointed-to type involves components of variable size,
4756 care must be taken to ensure that the size evaluation code is
4757 emitted early enough to dominate all the possible later uses
4758 and late enough for the variables on which it depends to have
4759 been assigned.
4761 This is expected to happen automatically when the pointed-to
4762 type has a name/declaration of it's own, but special attention
4763 is required if the type is anonymous.
4765 We handle the NORMAL and FIELD contexts here by attaching an
4766 artificial TYPE_DECL to such pointed-to type. This forces the
4767 sizes evaluation at a safe point and ensures it is not deferred
4768 until e.g. within a deeper conditional context.
4770 We expect nothing to be needed here for PARM or TYPENAME.
4771 Pushing a TYPE_DECL at this point for TYPENAME would actually
4772 be incorrect, as we might be in the middle of an expression
4773 with side effects on the pointed-to type size "arguments" prior
4774 to the pointer declaration point and the fake TYPE_DECL in the
4775 enclosing context would force the size evaluation prior to the
4776 side effects. */
4778 if (!TYPE_NAME (type)
4779 && (decl_context == NORMAL || decl_context == FIELD)
4780 && variably_modified_type_p (type, NULL_TREE))
4782 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4783 DECL_ARTIFICIAL (decl) = 1;
4784 pushdecl (decl);
4785 finish_decl (decl, NULL_TREE, NULL_TREE, NULL_TREE);
4786 TYPE_NAME (type) = decl;
4789 type = build_pointer_type (type);
4791 /* Process type qualifiers (such as const or volatile)
4792 that were given inside the `*'. */
4793 type_quals = declarator->u.pointer_quals;
4795 declarator = declarator->declarator;
4796 break;
4798 default:
4799 gcc_unreachable ();
4802 *decl_attrs = chainon (returned_attrs, *decl_attrs);
4804 /* Now TYPE has the actual type, apart from any qualifiers in
4805 TYPE_QUALS. */
4807 /* Check the type and width of a bit-field. */
4808 if (bitfield)
4809 check_bitfield_type_and_width (&type, width, orig_name);
4811 /* Did array size calculations overflow? */
4813 if (TREE_CODE (type) == ARRAY_TYPE
4814 && COMPLETE_TYPE_P (type)
4815 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4816 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4818 error ("size of array %qs is too large", name);
4819 /* If we proceed with the array type as it is, we'll eventually
4820 crash in tree_low_cst(). */
4821 type = error_mark_node;
4824 /* If this is declaring a typedef name, return a TYPE_DECL. */
4826 if (storage_class == csc_typedef)
4828 tree decl;
4829 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4830 && type_quals)
4831 pedwarn (input_location, OPT_pedantic,
4832 "ISO C forbids qualified function types");
4833 if (type_quals)
4834 type = c_build_qualified_type (type, type_quals);
4835 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4836 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4837 if (declspecs->explicit_signed_p)
4838 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4839 if (declspecs->inline_p)
4840 pedwarn (input_location, 0,"typedef %q+D declared %<inline%>", decl);
4841 return decl;
4844 /* If this is a type name (such as, in a cast or sizeof),
4845 compute the type and return it now. */
4847 if (decl_context == TYPENAME)
4849 /* Note that the grammar rejects storage classes in typenames
4850 and fields. */
4851 gcc_assert (storage_class == csc_none && !threadp
4852 && !declspecs->inline_p);
4853 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4854 && type_quals)
4855 pedwarn (input_location, OPT_pedantic,
4856 "ISO C forbids const or volatile function types");
4857 if (type_quals)
4858 type = c_build_qualified_type (type, type_quals);
4859 return type;
4862 if (pedantic && decl_context == FIELD
4863 && variably_modified_type_p (type, NULL_TREE))
4865 /* C99 6.7.2.1p8 */
4866 pedwarn (input_location, OPT_pedantic,
4867 "a member of a structure or union cannot have a variably modified type");
4870 /* Aside from typedefs and type names (handle above),
4871 `void' at top level (not within pointer)
4872 is allowed only in public variables.
4873 We don't complain about parms either, but that is because
4874 a better error message can be made later. */
4876 if (VOID_TYPE_P (type) && decl_context != PARM
4877 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4878 && (storage_class == csc_extern
4879 || (current_scope == file_scope
4880 && !(storage_class == csc_static
4881 || storage_class == csc_register)))))
4883 error ("variable or field %qs declared void", name);
4884 type = integer_type_node;
4887 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4888 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4891 tree decl;
4893 if (decl_context == PARM)
4895 tree promoted_type;
4897 /* A parameter declared as an array of T is really a pointer to T.
4898 One declared as a function is really a pointer to a function. */
4900 if (TREE_CODE (type) == ARRAY_TYPE)
4902 /* Transfer const-ness of array into that of type pointed to. */
4903 type = TREE_TYPE (type);
4904 if (type_quals)
4905 type = c_build_qualified_type (type, type_quals);
4906 type = build_pointer_type (type);
4907 type_quals = array_ptr_quals;
4908 if (type_quals)
4909 type = c_build_qualified_type (type, type_quals);
4911 /* We don't yet implement attributes in this context. */
4912 if (array_ptr_attrs != NULL_TREE)
4913 warning (OPT_Wattributes,
4914 "attributes in parameter array declarator ignored");
4916 size_varies = 0;
4918 else if (TREE_CODE (type) == FUNCTION_TYPE)
4920 if (type_quals)
4921 pedwarn (input_location, OPT_pedantic,
4922 "ISO C forbids qualified function types");
4923 if (type_quals)
4924 type = c_build_qualified_type (type, type_quals);
4925 type = build_pointer_type (type);
4926 type_quals = TYPE_UNQUALIFIED;
4928 else if (type_quals)
4929 type = c_build_qualified_type (type, type_quals);
4931 decl = build_decl (PARM_DECL, declarator->u.id, type);
4932 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4933 if (size_varies)
4934 C_DECL_VARIABLE_SIZE (decl) = 1;
4936 /* Compute the type actually passed in the parmlist,
4937 for the case where there is no prototype.
4938 (For example, shorts and chars are passed as ints.)
4939 When there is a prototype, this is overridden later. */
4941 if (type == error_mark_node)
4942 promoted_type = type;
4943 else
4944 promoted_type = c_type_promotes_to (type);
4946 DECL_ARG_TYPE (decl) = promoted_type;
4947 if (declspecs->inline_p)
4948 pedwarn (input_location, 0, "parameter %q+D declared %<inline%>", decl);
4950 else if (decl_context == FIELD)
4952 /* Note that the grammar rejects storage classes in typenames
4953 and fields. */
4954 gcc_assert (storage_class == csc_none && !threadp
4955 && !declspecs->inline_p);
4957 /* Structure field. It may not be a function. */
4959 if (TREE_CODE (type) == FUNCTION_TYPE)
4961 error ("field %qs declared as a function", name);
4962 type = build_pointer_type (type);
4964 else if (TREE_CODE (type) != ERROR_MARK
4965 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4967 error ("field %qs has incomplete type", name);
4968 type = error_mark_node;
4970 type = c_build_qualified_type (type, type_quals);
4971 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4972 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4973 DECL_NONADDRESSABLE_P (decl) = bitfield;
4974 if (bitfield && !declarator->u.id)
4975 TREE_NO_WARNING (decl) = 1;
4977 if (size_varies)
4978 C_DECL_VARIABLE_SIZE (decl) = 1;
4980 else if (TREE_CODE (type) == FUNCTION_TYPE)
4982 if (storage_class == csc_register || threadp)
4984 error ("invalid storage class for function %qs", name);
4986 else if (current_scope != file_scope)
4988 /* Function declaration not at file scope. Storage
4989 classes other than `extern' are not allowed, C99
4990 6.7.1p5, and `extern' makes no difference. However,
4991 GCC allows 'auto', perhaps with 'inline', to support
4992 nested functions. */
4993 if (storage_class == csc_auto)
4994 pedwarn (input_location, OPT_pedantic, "invalid storage class for function %qs", name);
4995 else if (storage_class == csc_static)
4997 error ("invalid storage class for function %qs", name);
4998 if (funcdef_flag)
4999 storage_class = declspecs->storage_class = csc_none;
5000 else
5001 return 0;
5005 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
5006 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5007 decl = build_decl_attribute_variant (decl, decl_attr);
5009 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
5010 pedwarn (input_location, OPT_pedantic,
5011 "ISO C forbids qualified function types");
5013 /* GNU C interprets a volatile-qualified function type to indicate
5014 that the function does not return. */
5015 if ((type_quals & TYPE_QUAL_VOLATILE)
5016 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5017 warning (0, "%<noreturn%> function returns non-void value");
5019 /* Every function declaration is an external reference
5020 (DECL_EXTERNAL) except for those which are not at file
5021 scope and are explicitly declared "auto". This is
5022 forbidden by standard C (C99 6.7.1p5) and is interpreted by
5023 GCC to signify a forward declaration of a nested function. */
5024 if (storage_class == csc_auto && current_scope != file_scope)
5025 DECL_EXTERNAL (decl) = 0;
5026 /* In C99, a function which is declared 'inline' with 'extern'
5027 is not an external reference (which is confusing). It
5028 means that the later definition of the function must be output
5029 in this file, C99 6.7.4p6. In GNU C89, a function declared
5030 'extern inline' is an external reference. */
5031 else if (declspecs->inline_p && storage_class != csc_static)
5032 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
5033 == flag_gnu89_inline);
5034 else
5035 DECL_EXTERNAL (decl) = !initialized;
5037 /* Record absence of global scope for `static' or `auto'. */
5038 TREE_PUBLIC (decl)
5039 = !(storage_class == csc_static || storage_class == csc_auto);
5041 /* For a function definition, record the argument information
5042 block where store_parm_decls will look for it. */
5043 if (funcdef_flag)
5044 current_function_arg_info = arg_info;
5046 if (declspecs->default_int_p)
5047 C_FUNCTION_IMPLICIT_INT (decl) = 1;
5049 /* Record presence of `inline', if it is reasonable. */
5050 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
5052 if (declspecs->inline_p)
5053 pedwarn (input_location, 0, "cannot inline function %<main%>");
5055 else if (declspecs->inline_p)
5056 /* Record that the function is declared `inline'. */
5057 DECL_DECLARED_INLINE_P (decl) = 1;
5059 else
5061 /* It's a variable. */
5062 /* An uninitialized decl with `extern' is a reference. */
5063 int extern_ref = !initialized && storage_class == csc_extern;
5065 type = c_build_qualified_type (type, type_quals);
5067 /* C99 6.2.2p7: It is invalid (compile-time undefined
5068 behavior) to create an 'extern' declaration for a
5069 variable if there is a global declaration that is
5070 'static' and the global declaration is not visible.
5071 (If the static declaration _is_ currently visible,
5072 the 'extern' declaration is taken to refer to that decl.) */
5073 if (extern_ref && current_scope != file_scope)
5075 tree global_decl = identifier_global_value (declarator->u.id);
5076 tree visible_decl = lookup_name (declarator->u.id);
5078 if (global_decl
5079 && global_decl != visible_decl
5080 && TREE_CODE (global_decl) == VAR_DECL
5081 && !TREE_PUBLIC (global_decl))
5082 error ("variable previously declared %<static%> redeclared "
5083 "%<extern%>");
5086 decl = build_decl (VAR_DECL, declarator->u.id, type);
5087 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
5088 if (size_varies)
5089 C_DECL_VARIABLE_SIZE (decl) = 1;
5091 if (declspecs->inline_p)
5092 pedwarn (input_location, 0, "variable %q+D declared %<inline%>", decl);
5094 /* At file scope, an initialized extern declaration may follow
5095 a static declaration. In that case, DECL_EXTERNAL will be
5096 reset later in start_decl. */
5097 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
5099 /* At file scope, the presence of a `static' or `register' storage
5100 class specifier, or the absence of all storage class specifiers
5101 makes this declaration a definition (perhaps tentative). Also,
5102 the absence of `static' makes it public. */
5103 if (current_scope == file_scope)
5105 TREE_PUBLIC (decl) = storage_class != csc_static;
5106 TREE_STATIC (decl) = !extern_ref;
5108 /* Not at file scope, only `static' makes a static definition. */
5109 else
5111 TREE_STATIC (decl) = (storage_class == csc_static);
5112 TREE_PUBLIC (decl) = extern_ref;
5115 if (threadp)
5116 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
5119 if ((storage_class == csc_extern
5120 || (storage_class == csc_none
5121 && TREE_CODE (type) == FUNCTION_TYPE
5122 && !funcdef_flag))
5123 && variably_modified_type_p (type, NULL_TREE))
5125 /* C99 6.7.5.2p2 */
5126 if (TREE_CODE (type) == FUNCTION_TYPE)
5127 error ("non-nested function with variably modified type");
5128 else
5129 error ("object with variably modified type must have no linkage");
5132 /* Record `register' declaration for warnings on &
5133 and in case doing stupid register allocation. */
5135 if (storage_class == csc_register)
5137 C_DECL_REGISTER (decl) = 1;
5138 DECL_REGISTER (decl) = 1;
5141 /* Record constancy and volatility. */
5142 c_apply_type_quals_to_decl (type_quals, decl);
5144 /* If a type has volatile components, it should be stored in memory.
5145 Otherwise, the fact that those components are volatile
5146 will be ignored, and would even crash the compiler.
5147 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
5148 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
5149 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
5150 || TREE_CODE (decl) == RESULT_DECL))
5152 /* It is not an error for a structure with volatile fields to
5153 be declared register, but reset DECL_REGISTER since it
5154 cannot actually go in a register. */
5155 int was_reg = C_DECL_REGISTER (decl);
5156 C_DECL_REGISTER (decl) = 0;
5157 DECL_REGISTER (decl) = 0;
5158 c_mark_addressable (decl);
5159 C_DECL_REGISTER (decl) = was_reg;
5162 /* This is the earliest point at which we might know the assembler
5163 name of a variable. Thus, if it's known before this, die horribly. */
5164 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
5166 return decl;
5170 /* Decode the parameter-list info for a function type or function definition.
5171 The argument is the value returned by `get_parm_info' (or made in c-parse.c
5172 if there is an identifier list instead of a parameter decl list).
5173 These two functions are separate because when a function returns
5174 or receives functions then each is called multiple times but the order
5175 of calls is different. The last call to `grokparms' is always the one
5176 that contains the formal parameter names of a function definition.
5178 Return a list of arg types to use in the FUNCTION_TYPE for this function.
5180 FUNCDEF_FLAG is true for a function definition, false for
5181 a mere declaration. A nonempty identifier-list gets an error message
5182 when FUNCDEF_FLAG is false. */
5184 static tree
5185 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
5187 tree arg_types = arg_info->types;
5189 if (funcdef_flag && arg_info->had_vla_unspec)
5191 /* A function definition isn't function prototype scope C99 6.2.1p4. */
5192 /* C99 6.7.5.2p4 */
5193 error ("%<[*]%> not allowed in other than function prototype scope");
5196 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5197 warning (OPT_Wstrict_prototypes,
5198 "function declaration isn%'t a prototype");
5200 if (arg_types == error_mark_node)
5201 return 0; /* don't set TYPE_ARG_TYPES in this case */
5203 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5205 if (!funcdef_flag)
5206 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
5208 arg_info->parms = arg_info->types;
5209 arg_info->types = 0;
5210 return 0;
5212 else
5214 tree parm, type, typelt;
5215 unsigned int parmno;
5217 /* If there is a parameter of incomplete type in a definition,
5218 this is an error. In a declaration this is valid, and a
5219 struct or union type may be completed later, before any calls
5220 or definition of the function. In the case where the tag was
5221 first declared within the parameter list, a warning has
5222 already been given. If a parameter has void type, then
5223 however the function cannot be defined or called, so
5224 warn. */
5226 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5227 parm;
5228 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5230 type = TREE_VALUE (typelt);
5231 if (type == error_mark_node)
5232 continue;
5234 if (!COMPLETE_TYPE_P (type))
5236 if (funcdef_flag)
5238 if (DECL_NAME (parm))
5239 error ("parameter %u (%q+D) has incomplete type",
5240 parmno, parm);
5241 else
5242 error ("%Jparameter %u has incomplete type",
5243 parm, parmno);
5245 TREE_VALUE (typelt) = error_mark_node;
5246 TREE_TYPE (parm) = error_mark_node;
5248 else if (VOID_TYPE_P (type))
5250 if (DECL_NAME (parm))
5251 warning (0, "parameter %u (%q+D) has void type",
5252 parmno, parm);
5253 else
5254 warning (0, "%Jparameter %u has void type",
5255 parm, parmno);
5259 if (DECL_NAME (parm) && TREE_USED (parm))
5260 warn_if_shadowing (parm);
5262 return arg_types;
5266 /* Take apart the current scope and return a c_arg_info structure with
5267 info on a parameter list just parsed.
5269 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5271 ELLIPSIS being true means the argument list ended in '...' so don't
5272 append a sentinel (void_list_node) to the end of the type-list. */
5274 struct c_arg_info *
5275 get_parm_info (bool ellipsis)
5277 struct c_binding *b = current_scope->bindings;
5278 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5279 struct c_arg_info);
5280 tree parms = 0;
5281 tree tags = 0;
5282 tree types = 0;
5283 tree others = 0;
5285 static bool explained_incomplete_types = false;
5286 bool gave_void_only_once_err = false;
5288 arg_info->parms = 0;
5289 arg_info->tags = 0;
5290 arg_info->types = 0;
5291 arg_info->others = 0;
5292 arg_info->pending_sizes = 0;
5293 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5295 /* The bindings in this scope must not get put into a block.
5296 We will take care of deleting the binding nodes. */
5297 current_scope->bindings = 0;
5299 /* This function is only called if there was *something* on the
5300 parameter list. */
5301 gcc_assert (b);
5303 /* A parameter list consisting solely of 'void' indicates that the
5304 function takes no arguments. But if the 'void' is qualified
5305 (by 'const' or 'volatile'), or has a storage class specifier
5306 ('register'), then the behavior is undefined; issue an error.
5307 Typedefs for 'void' are OK (see DR#157). */
5308 if (b->prev == 0 /* one binding */
5309 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5310 && !DECL_NAME (b->decl) /* anonymous */
5311 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5313 if (TREE_THIS_VOLATILE (b->decl)
5314 || TREE_READONLY (b->decl)
5315 || C_DECL_REGISTER (b->decl))
5316 error ("%<void%> as only parameter may not be qualified");
5318 /* There cannot be an ellipsis. */
5319 if (ellipsis)
5320 error ("%<void%> must be the only parameter");
5322 arg_info->types = void_list_node;
5323 return arg_info;
5326 if (!ellipsis)
5327 types = void_list_node;
5329 /* Break up the bindings list into parms, tags, types, and others;
5330 apply sanity checks; purge the name-to-decl bindings. */
5331 while (b)
5333 tree decl = b->decl;
5334 tree type = TREE_TYPE (decl);
5335 const char *keyword;
5337 switch (TREE_CODE (decl))
5339 case PARM_DECL:
5340 if (b->id)
5342 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5343 I_SYMBOL_BINDING (b->id) = b->shadowed;
5346 /* Check for forward decls that never got their actual decl. */
5347 if (TREE_ASM_WRITTEN (decl))
5348 error ("parameter %q+D has just a forward declaration", decl);
5349 /* Check for (..., void, ...) and issue an error. */
5350 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5352 if (!gave_void_only_once_err)
5354 error ("%<void%> must be the only parameter");
5355 gave_void_only_once_err = true;
5358 else
5360 /* Valid parameter, add it to the list. */
5361 TREE_CHAIN (decl) = parms;
5362 parms = decl;
5364 /* Since there is a prototype, args are passed in their
5365 declared types. The back end may override this later. */
5366 DECL_ARG_TYPE (decl) = type;
5367 types = tree_cons (0, type, types);
5369 break;
5371 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5372 case UNION_TYPE: keyword = "union"; goto tag;
5373 case RECORD_TYPE: keyword = "struct"; goto tag;
5374 tag:
5375 /* Types may not have tag-names, in which case the type
5376 appears in the bindings list with b->id NULL. */
5377 if (b->id)
5379 gcc_assert (I_TAG_BINDING (b->id) == b);
5380 I_TAG_BINDING (b->id) = b->shadowed;
5383 /* Warn about any struct, union or enum tags defined in a
5384 parameter list. The scope of such types is limited to
5385 the parameter list, which is rarely if ever desirable
5386 (it's impossible to call such a function with type-
5387 correct arguments). An anonymous union parm type is
5388 meaningful as a GNU extension, so don't warn for that. */
5389 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5391 if (b->id)
5392 /* The %s will be one of 'struct', 'union', or 'enum'. */
5393 warning (0, "%<%s %E%> declared inside parameter list",
5394 keyword, b->id);
5395 else
5396 /* The %s will be one of 'struct', 'union', or 'enum'. */
5397 warning (0, "anonymous %s declared inside parameter list",
5398 keyword);
5400 if (!explained_incomplete_types)
5402 warning (0, "its scope is only this definition or declaration,"
5403 " which is probably not what you want");
5404 explained_incomplete_types = true;
5408 tags = tree_cons (b->id, decl, tags);
5409 break;
5411 case CONST_DECL:
5412 case TYPE_DECL:
5413 case FUNCTION_DECL:
5414 /* CONST_DECLs appear here when we have an embedded enum,
5415 and TYPE_DECLs appear here when we have an embedded struct
5416 or union. No warnings for this - we already warned about the
5417 type itself. FUNCTION_DECLs appear when there is an implicit
5418 function declaration in the parameter list. */
5420 TREE_CHAIN (decl) = others;
5421 others = decl;
5422 /* fall through */
5424 case ERROR_MARK:
5425 /* error_mark_node appears here when we have an undeclared
5426 variable. Just throw it away. */
5427 if (b->id)
5429 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5430 I_SYMBOL_BINDING (b->id) = b->shadowed;
5432 break;
5434 /* Other things that might be encountered. */
5435 case LABEL_DECL:
5436 case VAR_DECL:
5437 default:
5438 gcc_unreachable ();
5441 b = free_binding_and_advance (b);
5444 arg_info->parms = parms;
5445 arg_info->tags = tags;
5446 arg_info->types = types;
5447 arg_info->others = others;
5448 arg_info->pending_sizes = get_pending_sizes ();
5449 return arg_info;
5452 /* Get the struct, enum or union (CODE says which) with tag NAME.
5453 Define the tag as a forward-reference if it is not defined.
5454 Return a c_typespec structure for the type specifier. */
5456 struct c_typespec
5457 parser_xref_tag (enum tree_code code, tree name)
5459 struct c_typespec ret;
5460 tree ref;
5462 ret.expr = NULL_TREE;
5463 ret.expr_const_operands = true;
5465 /* If a cross reference is requested, look up the type
5466 already defined for this tag and return it. */
5468 ref = lookup_tag (code, name, 0);
5469 /* If this is the right type of tag, return what we found.
5470 (This reference will be shadowed by shadow_tag later if appropriate.)
5471 If this is the wrong type of tag, do not return it. If it was the
5472 wrong type in the same scope, we will have had an error
5473 message already; if in a different scope and declaring
5474 a name, pending_xref_error will give an error message; but if in a
5475 different scope and not declaring a name, this tag should
5476 shadow the previous declaration of a different type of tag, and
5477 this would not work properly if we return the reference found.
5478 (For example, with "struct foo" in an outer scope, "union foo;"
5479 must shadow that tag with a new one of union type.) */
5480 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5481 if (ref && TREE_CODE (ref) == code)
5483 ret.spec = ref;
5484 return ret;
5487 /* If no such tag is yet defined, create a forward-reference node
5488 and record it as the "definition".
5489 When a real declaration of this type is found,
5490 the forward-reference will be altered into a real type. */
5492 ref = make_node (code);
5493 if (code == ENUMERAL_TYPE)
5495 /* Give the type a default layout like unsigned int
5496 to avoid crashing if it does not get defined. */
5497 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
5498 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5499 TYPE_USER_ALIGN (ref) = 0;
5500 TYPE_UNSIGNED (ref) = 1;
5501 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5502 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5503 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5506 pushtag (name, ref);
5508 ret.spec = ref;
5509 return ret;
5512 /* Get the struct, enum or union (CODE says which) with tag NAME.
5513 Define the tag as a forward-reference if it is not defined.
5514 Return a tree for the type. */
5516 tree
5517 xref_tag (enum tree_code code, tree name)
5519 return parser_xref_tag (code, name).spec;
5522 /* Make sure that the tag NAME is defined *in the current scope*
5523 at least as a forward reference.
5524 CODE says which kind of tag NAME ought to be. */
5526 tree
5527 start_struct (enum tree_code code, tree name)
5529 /* If there is already a tag defined at this scope
5530 (as a forward reference), just return it. */
5532 tree ref = 0;
5534 if (name != 0)
5535 ref = lookup_tag (code, name, 1);
5536 if (ref && TREE_CODE (ref) == code)
5538 if (TYPE_SIZE (ref))
5540 if (code == UNION_TYPE)
5541 error ("redefinition of %<union %E%>", name);
5542 else
5543 error ("redefinition of %<struct %E%>", name);
5544 /* Don't create structures using a name already in use. */
5545 ref = NULL_TREE;
5547 else if (C_TYPE_BEING_DEFINED (ref))
5549 if (code == UNION_TYPE)
5550 error ("nested redefinition of %<union %E%>", name);
5551 else
5552 error ("nested redefinition of %<struct %E%>", name);
5553 /* Don't create structures that contain themselves. */
5554 ref = NULL_TREE;
5558 /* Otherwise create a forward-reference just so the tag is in scope. */
5560 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5562 ref = make_node (code);
5563 pushtag (name, ref);
5566 C_TYPE_BEING_DEFINED (ref) = 1;
5567 TYPE_PACKED (ref) = flag_pack_struct;
5568 return ref;
5571 /* Process the specs, declarator and width (NULL if omitted)
5572 of a structure component, returning a FIELD_DECL node.
5573 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5574 DECL_ATTRS is as for grokdeclarator.
5576 LOC is the location of the structure component.
5578 This is done during the parsing of the struct declaration.
5579 The FIELD_DECL nodes are chained together and the lot of them
5580 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5582 tree
5583 grokfield (location_t loc,
5584 struct c_declarator *declarator, struct c_declspecs *declspecs,
5585 tree width, tree *decl_attrs)
5587 tree value;
5589 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5590 && width == NULL_TREE)
5592 /* This is an unnamed decl.
5594 If we have something of the form "union { list } ;" then this
5595 is the anonymous union extension. Similarly for struct.
5597 If this is something of the form "struct foo;", then
5598 If MS extensions are enabled, this is handled as an
5599 anonymous struct.
5600 Otherwise this is a forward declaration of a structure tag.
5602 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5603 If MS extensions are enabled and foo names a structure, then
5604 again this is an anonymous struct.
5605 Otherwise this is an error.
5607 Oh what a horrid tangled web we weave. I wonder if MS consciously
5608 took this from Plan 9 or if it was an accident of implementation
5609 that took root before someone noticed the bug... */
5611 tree type = declspecs->type;
5612 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5613 || TREE_CODE (type) == UNION_TYPE);
5614 bool ok = false;
5616 if (type_ok
5617 && (flag_ms_extensions || !declspecs->typedef_p))
5619 if (flag_ms_extensions)
5620 ok = true;
5621 else if (flag_iso)
5622 ok = false;
5623 else if (TYPE_NAME (type) == NULL)
5624 ok = true;
5625 else
5626 ok = false;
5628 if (!ok)
5630 pedwarn (loc, 0, "declaration does not declare anything");
5631 return NULL_TREE;
5633 pedwarn (loc, OPT_pedantic, "ISO C doesn%'t support unnamed structs/unions");
5636 value = grokdeclarator (declarator, declspecs, FIELD, false,
5637 width ? &width : NULL, decl_attrs, NULL, NULL,
5638 DEPRECATED_NORMAL);
5640 finish_decl (value, NULL_TREE, NULL_TREE, NULL_TREE);
5641 DECL_INITIAL (value) = width;
5643 return value;
5646 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5647 the list such that this does not present a problem later. */
5649 static void
5650 detect_field_duplicates (tree fieldlist)
5652 tree x, y;
5653 int timeout = 10;
5655 /* First, see if there are more than "a few" fields.
5656 This is trivially true if there are zero or one fields. */
5657 if (!fieldlist)
5658 return;
5659 x = TREE_CHAIN (fieldlist);
5660 if (!x)
5661 return;
5662 do {
5663 timeout--;
5664 x = TREE_CHAIN (x);
5665 } while (timeout > 0 && x);
5667 /* If there were "few" fields, avoid the overhead of allocating
5668 a hash table. Instead just do the nested traversal thing. */
5669 if (timeout > 0)
5671 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5672 if (DECL_NAME (x))
5674 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5675 if (DECL_NAME (y) == DECL_NAME (x))
5677 error ("duplicate member %q+D", x);
5678 DECL_NAME (x) = NULL_TREE;
5682 else
5684 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5685 void **slot;
5687 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5688 if ((y = DECL_NAME (x)) != 0)
5690 slot = htab_find_slot (htab, y, INSERT);
5691 if (*slot)
5693 error ("duplicate member %q+D", x);
5694 DECL_NAME (x) = NULL_TREE;
5696 *slot = y;
5699 htab_delete (htab);
5703 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5704 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5705 ATTRIBUTES are attributes to be applied to the structure. */
5707 tree
5708 finish_struct (tree t, tree fieldlist, tree attributes)
5710 tree x;
5711 bool toplevel = file_scope == current_scope;
5712 int saw_named_field;
5714 /* If this type was previously laid out as a forward reference,
5715 make sure we lay it out again. */
5717 TYPE_SIZE (t) = 0;
5719 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5721 if (pedantic)
5723 for (x = fieldlist; x; x = TREE_CHAIN (x))
5724 if (DECL_NAME (x) != 0)
5725 break;
5727 if (x == 0)
5729 if (TREE_CODE (t) == UNION_TYPE)
5731 if (fieldlist)
5732 pedwarn (input_location, OPT_pedantic, "union has no named members");
5733 else
5734 pedwarn (input_location, OPT_pedantic, "union has no members");
5736 else
5738 if (fieldlist)
5739 pedwarn (input_location, OPT_pedantic, "struct has no named members");
5740 else
5741 pedwarn (input_location, OPT_pedantic, "struct has no members");
5746 /* Install struct as DECL_CONTEXT of each field decl.
5747 Also process specified field sizes, found in the DECL_INITIAL,
5748 storing 0 there after the type has been changed to precision equal
5749 to its width, rather than the precision of the specified standard
5750 type. (Correct layout requires the original type to have been preserved
5751 until now.) */
5753 saw_named_field = 0;
5754 for (x = fieldlist; x; x = TREE_CHAIN (x))
5756 if (TREE_TYPE (x) == error_mark_node)
5757 continue;
5759 DECL_CONTEXT (x) = t;
5761 /* If any field is const, the structure type is pseudo-const. */
5762 if (TREE_READONLY (x))
5763 C_TYPE_FIELDS_READONLY (t) = 1;
5764 else
5766 /* A field that is pseudo-const makes the structure likewise. */
5767 tree t1 = TREE_TYPE (x);
5768 while (TREE_CODE (t1) == ARRAY_TYPE)
5769 t1 = TREE_TYPE (t1);
5770 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5771 && C_TYPE_FIELDS_READONLY (t1))
5772 C_TYPE_FIELDS_READONLY (t) = 1;
5775 /* Any field that is volatile means variables of this type must be
5776 treated in some ways as volatile. */
5777 if (TREE_THIS_VOLATILE (x))
5778 C_TYPE_FIELDS_VOLATILE (t) = 1;
5780 /* Any field of nominal variable size implies structure is too. */
5781 if (C_DECL_VARIABLE_SIZE (x))
5782 C_TYPE_VARIABLE_SIZE (t) = 1;
5784 if (DECL_INITIAL (x))
5786 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5787 DECL_SIZE (x) = bitsize_int (width);
5788 DECL_BIT_FIELD (x) = 1;
5789 SET_DECL_C_BIT_FIELD (x);
5792 if (TYPE_PACKED (t)
5793 && (DECL_BIT_FIELD (x)
5794 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
5795 DECL_PACKED (x) = 1;
5797 /* Detect flexible array member in an invalid context. */
5798 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5799 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5800 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5801 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5803 if (TREE_CODE (t) == UNION_TYPE)
5805 error ("%Jflexible array member in union", x);
5806 TREE_TYPE (x) = error_mark_node;
5808 else if (TREE_CHAIN (x) != NULL_TREE)
5810 error ("%Jflexible array member not at end of struct", x);
5811 TREE_TYPE (x) = error_mark_node;
5813 else if (!saw_named_field)
5815 error ("%Jflexible array member in otherwise empty struct", x);
5816 TREE_TYPE (x) = error_mark_node;
5820 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5821 && flexible_array_type_p (TREE_TYPE (x)))
5822 pedwarn (input_location, OPT_pedantic,
5823 "%Jinvalid use of structure with flexible array member", x);
5825 if (DECL_NAME (x))
5826 saw_named_field = 1;
5829 detect_field_duplicates (fieldlist);
5831 /* Now we have the nearly final fieldlist. Record it,
5832 then lay out the structure or union (including the fields). */
5834 TYPE_FIELDS (t) = fieldlist;
5836 layout_type (t);
5838 /* Give bit-fields their proper types. */
5840 tree *fieldlistp = &fieldlist;
5841 while (*fieldlistp)
5842 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5843 && TREE_TYPE (*fieldlistp) != error_mark_node)
5845 unsigned HOST_WIDE_INT width
5846 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5847 tree type = TREE_TYPE (*fieldlistp);
5848 if (width != TYPE_PRECISION (type))
5850 TREE_TYPE (*fieldlistp)
5851 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5852 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5854 DECL_INITIAL (*fieldlistp) = 0;
5856 else
5857 fieldlistp = &TREE_CHAIN (*fieldlistp);
5860 /* Now we have the truly final field list.
5861 Store it in this type and in the variants. */
5863 TYPE_FIELDS (t) = fieldlist;
5865 /* If there are lots of fields, sort so we can look through them fast.
5866 We arbitrarily consider 16 or more elts to be "a lot". */
5869 int len = 0;
5871 for (x = fieldlist; x; x = TREE_CHAIN (x))
5873 if (len > 15 || DECL_NAME (x) == NULL)
5874 break;
5875 len += 1;
5878 if (len > 15)
5880 tree *field_array;
5881 struct lang_type *space;
5882 struct sorted_fields_type *space2;
5884 len += list_length (x);
5886 /* Use the same allocation policy here that make_node uses, to
5887 ensure that this lives as long as the rest of the struct decl.
5888 All decls in an inline function need to be saved. */
5890 space = GGC_CNEW (struct lang_type);
5891 space2 = GGC_NEWVAR (struct sorted_fields_type,
5892 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5894 len = 0;
5895 space->s = space2;
5896 field_array = &space2->elts[0];
5897 for (x = fieldlist; x; x = TREE_CHAIN (x))
5899 field_array[len++] = x;
5901 /* If there is anonymous struct or union, break out of the loop. */
5902 if (DECL_NAME (x) == NULL)
5903 break;
5905 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5906 if (x == NULL)
5908 TYPE_LANG_SPECIFIC (t) = space;
5909 TYPE_LANG_SPECIFIC (t)->s->len = len;
5910 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5911 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5916 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5918 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5919 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5920 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5921 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5922 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5925 /* If this was supposed to be a transparent union, but we can't
5926 make it one, warn and turn off the flag. */
5927 if (TREE_CODE (t) == UNION_TYPE
5928 && TYPE_TRANSPARENT_UNION (t)
5929 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5931 TYPE_TRANSPARENT_UNION (t) = 0;
5932 warning (0, "union cannot be made transparent");
5935 /* If this structure or union completes the type of any previous
5936 variable declaration, lay it out and output its rtl. */
5937 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5939 x = TREE_CHAIN (x))
5941 tree decl = TREE_VALUE (x);
5942 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5943 layout_array_type (TREE_TYPE (decl));
5944 if (TREE_CODE (decl) != TYPE_DECL)
5946 layout_decl (decl, 0);
5947 if (c_dialect_objc ())
5948 objc_check_decl (decl);
5949 rest_of_decl_compilation (decl, toplevel, 0);
5950 if (!toplevel)
5951 expand_decl (decl);
5954 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5956 /* Finish debugging output for this type. */
5957 rest_of_type_compilation (t, toplevel);
5959 /* If we're inside a function proper, i.e. not file-scope and not still
5960 parsing parameters, then arrange for the size of a variable sized type
5961 to be bound now. */
5962 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5963 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5965 return t;
5968 /* Lay out the type T, and its element type, and so on. */
5970 static void
5971 layout_array_type (tree t)
5973 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5974 layout_array_type (TREE_TYPE (t));
5975 layout_type (t);
5978 /* Begin compiling the definition of an enumeration type.
5979 NAME is its name (or null if anonymous).
5980 Returns the type object, as yet incomplete.
5981 Also records info about it so that build_enumerator
5982 may be used to declare the individual values as they are read. */
5984 tree
5985 start_enum (struct c_enum_contents *the_enum, tree name)
5987 tree enumtype = 0;
5989 /* If this is the real definition for a previous forward reference,
5990 fill in the contents in the same object that used to be the
5991 forward reference. */
5993 if (name != 0)
5994 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5996 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5998 enumtype = make_node (ENUMERAL_TYPE);
5999 pushtag (name, enumtype);
6002 if (C_TYPE_BEING_DEFINED (enumtype))
6003 error ("nested redefinition of %<enum %E%>", name);
6005 C_TYPE_BEING_DEFINED (enumtype) = 1;
6007 if (TYPE_VALUES (enumtype) != 0)
6009 /* This enum is a named one that has been declared already. */
6010 error ("redeclaration of %<enum %E%>", name);
6012 /* Completely replace its old definition.
6013 The old enumerators remain defined, however. */
6014 TYPE_VALUES (enumtype) = 0;
6017 the_enum->enum_next_value = integer_zero_node;
6018 the_enum->enum_overflow = 0;
6020 if (flag_short_enums)
6021 TYPE_PACKED (enumtype) = 1;
6023 return enumtype;
6026 /* After processing and defining all the values of an enumeration type,
6027 install their decls in the enumeration type and finish it off.
6028 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
6029 and ATTRIBUTES are the specified attributes.
6030 Returns ENUMTYPE. */
6032 tree
6033 finish_enum (tree enumtype, tree values, tree attributes)
6035 tree pair, tem;
6036 tree minnode = 0, maxnode = 0;
6037 int precision, unsign;
6038 bool toplevel = (file_scope == current_scope);
6039 struct lang_type *lt;
6041 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6043 /* Calculate the maximum value of any enumerator in this type. */
6045 if (values == error_mark_node)
6046 minnode = maxnode = integer_zero_node;
6047 else
6049 minnode = maxnode = TREE_VALUE (values);
6050 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
6052 tree value = TREE_VALUE (pair);
6053 if (tree_int_cst_lt (maxnode, value))
6054 maxnode = value;
6055 if (tree_int_cst_lt (value, minnode))
6056 minnode = value;
6060 /* Construct the final type of this enumeration. It is the same
6061 as one of the integral types - the narrowest one that fits, except
6062 that normally we only go as narrow as int - and signed iff any of
6063 the values are negative. */
6064 unsign = (tree_int_cst_sgn (minnode) >= 0);
6065 precision = MAX (tree_int_cst_min_precision (minnode, unsign),
6066 tree_int_cst_min_precision (maxnode, unsign));
6068 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
6070 tem = c_common_type_for_size (precision, unsign);
6071 if (tem == NULL)
6073 warning (0, "enumeration values exceed range of largest integer");
6074 tem = long_long_integer_type_node;
6077 else
6078 tem = unsign ? unsigned_type_node : integer_type_node;
6080 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
6081 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
6082 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
6083 TYPE_SIZE (enumtype) = 0;
6085 /* If the precision of the type was specific with an attribute and it
6086 was too small, give an error. Otherwise, use it. */
6087 if (TYPE_PRECISION (enumtype))
6089 if (precision > TYPE_PRECISION (enumtype))
6090 error ("specified mode too small for enumeral values");
6092 else
6093 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
6095 layout_type (enumtype);
6097 if (values != error_mark_node)
6099 /* Change the type of the enumerators to be the enum type. We
6100 need to do this irrespective of the size of the enum, for
6101 proper type checking. Replace the DECL_INITIALs of the
6102 enumerators, and the value slots of the list, with copies
6103 that have the enum type; they cannot be modified in place
6104 because they may be shared (e.g. integer_zero_node) Finally,
6105 change the purpose slots to point to the names of the decls. */
6106 for (pair = values; pair; pair = TREE_CHAIN (pair))
6108 tree enu = TREE_PURPOSE (pair);
6109 tree ini = DECL_INITIAL (enu);
6111 TREE_TYPE (enu) = enumtype;
6113 /* The ISO C Standard mandates enumerators to have type int,
6114 even though the underlying type of an enum type is
6115 unspecified. However, GCC allows enumerators of any
6116 integer type as an extensions. build_enumerator()
6117 converts any enumerators that fit in an int to type int,
6118 to avoid promotions to unsigned types when comparing
6119 integers with enumerators that fit in the int range.
6120 When -pedantic is given, build_enumerator() would have
6121 already warned about those that don't fit. Here we
6122 convert the rest to the enumerator type. */
6123 if (TREE_TYPE (ini) != integer_type_node)
6124 ini = convert (enumtype, ini);
6126 DECL_INITIAL (enu) = ini;
6127 TREE_PURPOSE (pair) = DECL_NAME (enu);
6128 TREE_VALUE (pair) = ini;
6131 TYPE_VALUES (enumtype) = values;
6134 /* Record the min/max values so that we can warn about bit-field
6135 enumerations that are too small for the values. */
6136 lt = GGC_CNEW (struct lang_type);
6137 lt->enum_min = minnode;
6138 lt->enum_max = maxnode;
6139 TYPE_LANG_SPECIFIC (enumtype) = lt;
6141 /* Fix up all variant types of this enum type. */
6142 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
6144 if (tem == enumtype)
6145 continue;
6146 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
6147 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
6148 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
6149 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
6150 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
6151 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
6152 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
6153 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
6154 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
6155 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
6156 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
6159 /* Finish debugging output for this type. */
6160 rest_of_type_compilation (enumtype, toplevel);
6162 return enumtype;
6165 /* Build and install a CONST_DECL for one value of the
6166 current enumeration type (one that was begun with start_enum).
6167 Return a tree-list containing the CONST_DECL and its value.
6168 Assignment of sequential values by default is handled here. */
6170 tree
6171 build_enumerator (struct c_enum_contents *the_enum, tree name, tree value,
6172 location_t value_loc)
6174 tree decl, type;
6176 /* Validate and default VALUE. */
6178 if (value != 0)
6180 /* Don't issue more errors for error_mark_node (i.e. an
6181 undeclared identifier) - just ignore the value expression. */
6182 if (value == error_mark_node)
6183 value = 0;
6184 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
6186 error ("enumerator value for %qE is not an integer constant", name);
6187 value = 0;
6189 else
6191 if (TREE_CODE (value) != INTEGER_CST)
6193 value = c_fully_fold (value, false, NULL);
6194 if (TREE_CODE (value) == INTEGER_CST)
6195 pedwarn (value_loc, OPT_pedantic,
6196 "enumerator value for %qE is not an integer "
6197 "constant expression", name);
6199 if (TREE_CODE (value) != INTEGER_CST)
6201 error ("enumerator value for %qE is not an integer constant",
6202 name);
6203 value = 0;
6205 else
6207 value = default_conversion (value);
6208 constant_expression_warning (value);
6213 /* Default based on previous value. */
6214 /* It should no longer be possible to have NON_LVALUE_EXPR
6215 in the default. */
6216 if (value == 0)
6218 value = the_enum->enum_next_value;
6219 if (the_enum->enum_overflow)
6220 error ("overflow in enumeration values");
6222 /* Even though the underlying type of an enum is unspecified, the
6223 type of enumeration constants is explicitly defined as int
6224 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
6225 an extension. */
6226 else if (!int_fits_type_p (value, integer_type_node))
6227 pedwarn (value_loc, OPT_pedantic,
6228 "ISO C restricts enumerator values to range of %<int%>");
6230 /* The ISO C Standard mandates enumerators to have type int, even
6231 though the underlying type of an enum type is unspecified.
6232 However, GCC allows enumerators of any integer type as an
6233 extensions. Here we convert any enumerators that fit in an int
6234 to type int, to avoid promotions to unsigned types when comparing
6235 integers with enumerators that fit in the int range. When
6236 -pedantic is given, we would have already warned about those that
6237 don't fit. We have to do this here rather than in finish_enum
6238 because this value may be used to define more enumerators. */
6239 if (int_fits_type_p (value, integer_type_node))
6240 value = convert (integer_type_node, value);
6242 /* Set basis for default for next value. */
6243 the_enum->enum_next_value
6244 = build_binary_op
6245 (EXPR_HAS_LOCATION (value) ? EXPR_LOCATION (value) : input_location,
6246 PLUS_EXPR, value, integer_one_node, 0);
6247 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
6249 /* Now create a declaration for the enum value name. */
6251 type = TREE_TYPE (value);
6252 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6253 TYPE_PRECISION (integer_type_node)),
6254 (TYPE_PRECISION (type)
6255 >= TYPE_PRECISION (integer_type_node)
6256 && TYPE_UNSIGNED (type)));
6258 decl = build_decl (CONST_DECL, name, type);
6259 DECL_INITIAL (decl) = convert (type, value);
6260 pushdecl (decl);
6262 return tree_cons (decl, value, NULL_TREE);
6266 /* Create the FUNCTION_DECL for a function definition.
6267 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6268 the declaration; they describe the function's name and the type it returns,
6269 but twisted together in a fashion that parallels the syntax of C.
6271 This function creates a binding context for the function body
6272 as well as setting up the FUNCTION_DECL in current_function_decl.
6274 Returns 1 on success. If the DECLARATOR is not suitable for a function
6275 (it defines a datum instead), we return 0, which tells
6276 yyparse to report a parse error. */
6279 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6280 tree attributes)
6282 tree decl1, old_decl;
6283 tree restype, resdecl;
6284 struct c_label_context_se *nstack_se;
6285 struct c_label_context_vm *nstack_vm;
6287 current_function_returns_value = 0; /* Assume, until we see it does. */
6288 current_function_returns_null = 0;
6289 current_function_returns_abnormally = 0;
6290 warn_about_return_type = 0;
6291 c_switch_stack = NULL;
6293 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6294 nstack_se->labels_def = NULL;
6295 nstack_se->labels_used = NULL;
6296 nstack_se->next = label_context_stack_se;
6297 label_context_stack_se = nstack_se;
6299 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6300 nstack_vm->labels_def = NULL;
6301 nstack_vm->labels_used = NULL;
6302 nstack_vm->scope = 0;
6303 nstack_vm->next = label_context_stack_vm;
6304 label_context_stack_vm = nstack_vm;
6306 /* Indicate no valid break/continue context by setting these variables
6307 to some non-null, non-label value. We'll notice and emit the proper
6308 error message in c_finish_bc_stmt. */
6309 c_break_label = c_cont_label = size_zero_node;
6311 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
6312 &attributes, NULL, NULL, DEPRECATED_NORMAL);
6314 /* If the declarator is not suitable for a function definition,
6315 cause a syntax error. */
6316 if (decl1 == 0)
6318 label_context_stack_se = label_context_stack_se->next;
6319 label_context_stack_vm = label_context_stack_vm->next;
6320 return 0;
6323 decl_attributes (&decl1, attributes, 0);
6325 if (DECL_DECLARED_INLINE_P (decl1)
6326 && DECL_UNINLINABLE (decl1)
6327 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6328 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6329 decl1);
6331 /* Handle gnu_inline attribute. */
6332 if (declspecs->inline_p
6333 && !flag_gnu89_inline
6334 && TREE_CODE (decl1) == FUNCTION_DECL
6335 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
6336 || current_function_decl))
6338 if (declspecs->storage_class != csc_static)
6339 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6342 announce_function (decl1);
6344 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6346 error ("return type is an incomplete type");
6347 /* Make it return void instead. */
6348 TREE_TYPE (decl1)
6349 = build_function_type (void_type_node,
6350 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6353 if (warn_about_return_type)
6354 pedwarn_c99 (input_location, flag_isoc99 ? 0
6355 : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
6356 "return type defaults to %<int%>");
6358 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6359 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6360 DECL_INITIAL (decl1) = error_mark_node;
6362 /* If this definition isn't a prototype and we had a prototype declaration
6363 before, copy the arg type info from that prototype. */
6364 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6365 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6366 old_decl = 0;
6367 current_function_prototype_locus = UNKNOWN_LOCATION;
6368 current_function_prototype_built_in = false;
6369 current_function_prototype_arg_types = NULL_TREE;
6370 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6372 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6373 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6374 TREE_TYPE (TREE_TYPE (old_decl))))
6376 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6377 TREE_TYPE (decl1));
6378 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6379 current_function_prototype_built_in
6380 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6381 current_function_prototype_arg_types
6382 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6384 if (TREE_PUBLIC (decl1))
6386 /* If there is an external prototype declaration of this
6387 function, record its location but do not copy information
6388 to this decl. This may be an invisible declaration
6389 (built-in or in a scope which has finished) or simply
6390 have more refined argument types than any declaration
6391 found above. */
6392 struct c_binding *b;
6393 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6394 if (B_IN_SCOPE (b, external_scope))
6395 break;
6396 if (b)
6398 tree ext_decl, ext_type;
6399 ext_decl = b->decl;
6400 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6401 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6402 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6403 TREE_TYPE (ext_type)))
6405 current_function_prototype_locus
6406 = DECL_SOURCE_LOCATION (ext_decl);
6407 current_function_prototype_built_in
6408 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6409 current_function_prototype_arg_types
6410 = TYPE_ARG_TYPES (ext_type);
6416 /* Optionally warn of old-fashioned def with no previous prototype. */
6417 if (warn_strict_prototypes
6418 && old_decl != error_mark_node
6419 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6420 && C_DECL_ISNT_PROTOTYPE (old_decl))
6421 warning (OPT_Wstrict_prototypes,
6422 "function declaration isn%'t a prototype");
6423 /* Optionally warn of any global def with no previous prototype. */
6424 else if (warn_missing_prototypes
6425 && old_decl != error_mark_node
6426 && TREE_PUBLIC (decl1)
6427 && !MAIN_NAME_P (DECL_NAME (decl1))
6428 && C_DECL_ISNT_PROTOTYPE (old_decl))
6429 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6430 /* Optionally warn of any def with no previous prototype
6431 if the function has already been used. */
6432 else if (warn_missing_prototypes
6433 && old_decl != 0
6434 && old_decl != error_mark_node
6435 && TREE_USED (old_decl)
6436 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6437 warning (OPT_Wmissing_prototypes,
6438 "%q+D was used with no prototype before its definition", decl1);
6439 /* Optionally warn of any global def with no previous declaration. */
6440 else if (warn_missing_declarations
6441 && TREE_PUBLIC (decl1)
6442 && old_decl == 0
6443 && !MAIN_NAME_P (DECL_NAME (decl1)))
6444 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6445 decl1);
6446 /* Optionally warn of any def with no previous declaration
6447 if the function has already been used. */
6448 else if (warn_missing_declarations
6449 && old_decl != 0
6450 && old_decl != error_mark_node
6451 && TREE_USED (old_decl)
6452 && C_DECL_IMPLICIT (old_decl))
6453 warning (OPT_Wmissing_declarations,
6454 "%q+D was used with no declaration before its definition", decl1);
6456 /* This function exists in static storage.
6457 (This does not mean `static' in the C sense!) */
6458 TREE_STATIC (decl1) = 1;
6460 /* A nested function is not global. */
6461 if (current_function_decl != 0)
6462 TREE_PUBLIC (decl1) = 0;
6464 /* This is the earliest point at which we might know the assembler
6465 name of the function. Thus, if it's set before this, die horribly. */
6466 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6468 /* If #pragma weak was used, mark the decl weak now. */
6469 if (current_scope == file_scope)
6470 maybe_apply_pragma_weak (decl1);
6472 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6473 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
6475 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6476 != integer_type_node)
6477 pedwarn (input_location, OPT_Wmain, "return type of %q+D is not %<int%>", decl1);
6479 check_main_parameter_types (decl1);
6481 if (!TREE_PUBLIC (decl1))
6482 pedwarn (input_location, OPT_Wmain, "%q+D is normally a non-static function", decl1);
6485 /* Record the decl so that the function name is defined.
6486 If we already have a decl for this name, and it is a FUNCTION_DECL,
6487 use the old decl. */
6489 current_function_decl = pushdecl (decl1);
6491 push_scope ();
6492 declare_parm_level ();
6494 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6495 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6496 DECL_ARTIFICIAL (resdecl) = 1;
6497 DECL_IGNORED_P (resdecl) = 1;
6498 DECL_RESULT (current_function_decl) = resdecl;
6500 start_fname_decls ();
6502 return 1;
6505 /* Subroutine of store_parm_decls which handles new-style function
6506 definitions (prototype format). The parms already have decls, so we
6507 need only record them as in effect and complain if any redundant
6508 old-style parm decls were written. */
6509 static void
6510 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6512 tree decl;
6514 if (current_scope->bindings)
6516 error ("%Jold-style parameter declarations in prototyped "
6517 "function definition", fndecl);
6519 /* Get rid of the old-style declarations. */
6520 pop_scope ();
6521 push_scope ();
6523 /* Don't issue this warning for nested functions, and don't issue this
6524 warning if we got here because ARG_INFO_TYPES was error_mark_node
6525 (this happens when a function definition has just an ellipsis in
6526 its parameter list). */
6527 else if (!in_system_header && !current_function_scope
6528 && arg_info->types != error_mark_node)
6529 warning (OPT_Wtraditional,
6530 "%Jtraditional C rejects ISO C style function definitions",
6531 fndecl);
6533 /* Now make all the parameter declarations visible in the function body.
6534 We can bypass most of the grunt work of pushdecl. */
6535 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6537 DECL_CONTEXT (decl) = current_function_decl;
6538 if (DECL_NAME (decl))
6540 bind (DECL_NAME (decl), decl, current_scope,
6541 /*invisible=*/false, /*nested=*/false,
6542 UNKNOWN_LOCATION);
6543 if (!TREE_USED (decl))
6544 warn_if_shadowing (decl);
6546 else
6547 error ("%Jparameter name omitted", decl);
6550 /* Record the parameter list in the function declaration. */
6551 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6553 /* Now make all the ancillary declarations visible, likewise. */
6554 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6556 DECL_CONTEXT (decl) = current_function_decl;
6557 if (DECL_NAME (decl))
6558 bind (DECL_NAME (decl), decl, current_scope,
6559 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6562 /* And all the tag declarations. */
6563 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6564 if (TREE_PURPOSE (decl))
6565 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6566 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
6569 /* Subroutine of store_parm_decls which handles old-style function
6570 definitions (separate parameter list and declarations). */
6572 static void
6573 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6575 struct c_binding *b;
6576 tree parm, decl, last;
6577 tree parmids = arg_info->parms;
6578 struct pointer_set_t *seen_args = pointer_set_create ();
6580 if (!in_system_header)
6581 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6582 fndecl);
6584 /* Match each formal parameter name with its declaration. Save each
6585 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6586 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6588 if (TREE_VALUE (parm) == 0)
6590 error ("%Jparameter name missing from parameter list", fndecl);
6591 TREE_PURPOSE (parm) = 0;
6592 continue;
6595 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6596 if (b && B_IN_CURRENT_SCOPE (b))
6598 decl = b->decl;
6599 /* If we got something other than a PARM_DECL it is an error. */
6600 if (TREE_CODE (decl) != PARM_DECL)
6601 error ("%q+D declared as a non-parameter", decl);
6602 /* If the declaration is already marked, we have a duplicate
6603 name. Complain and ignore the duplicate. */
6604 else if (pointer_set_contains (seen_args, decl))
6606 error ("multiple parameters named %q+D", decl);
6607 TREE_PURPOSE (parm) = 0;
6608 continue;
6610 /* If the declaration says "void", complain and turn it into
6611 an int. */
6612 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6614 error ("parameter %q+D declared with void type", decl);
6615 TREE_TYPE (decl) = integer_type_node;
6616 DECL_ARG_TYPE (decl) = integer_type_node;
6617 layout_decl (decl, 0);
6619 warn_if_shadowing (decl);
6621 /* If no declaration found, default to int. */
6622 else
6624 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6625 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6626 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6627 pushdecl (decl);
6628 warn_if_shadowing (decl);
6630 if (flag_isoc99)
6631 pedwarn (input_location, 0, "type of %q+D defaults to %<int%>", decl);
6632 else
6633 warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
6636 TREE_PURPOSE (parm) = decl;
6637 pointer_set_insert (seen_args, decl);
6640 /* Now examine the parms chain for incomplete declarations
6641 and declarations with no corresponding names. */
6643 for (b = current_scope->bindings; b; b = b->prev)
6645 parm = b->decl;
6646 if (TREE_CODE (parm) != PARM_DECL)
6647 continue;
6649 if (TREE_TYPE (parm) != error_mark_node
6650 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6652 error ("parameter %q+D has incomplete type", parm);
6653 TREE_TYPE (parm) = error_mark_node;
6656 if (!pointer_set_contains (seen_args, parm))
6658 error ("declaration for parameter %q+D but no such parameter", parm);
6660 /* Pretend the parameter was not missing.
6661 This gets us to a standard state and minimizes
6662 further error messages. */
6663 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6667 /* Chain the declarations together in the order of the list of
6668 names. Store that chain in the function decl, replacing the
6669 list of names. Update the current scope to match. */
6670 DECL_ARGUMENTS (fndecl) = 0;
6672 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6673 if (TREE_PURPOSE (parm))
6674 break;
6675 if (parm && TREE_PURPOSE (parm))
6677 last = TREE_PURPOSE (parm);
6678 DECL_ARGUMENTS (fndecl) = last;
6680 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6681 if (TREE_PURPOSE (parm))
6683 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6684 last = TREE_PURPOSE (parm);
6686 TREE_CHAIN (last) = 0;
6689 pointer_set_destroy (seen_args);
6691 /* If there was a previous prototype,
6692 set the DECL_ARG_TYPE of each argument according to
6693 the type previously specified, and report any mismatches. */
6695 if (current_function_prototype_arg_types)
6697 tree type;
6698 for (parm = DECL_ARGUMENTS (fndecl),
6699 type = current_function_prototype_arg_types;
6700 parm || (type && TREE_VALUE (type) != error_mark_node
6701 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
6702 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6704 if (parm == 0 || type == 0
6705 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6707 if (current_function_prototype_built_in)
6708 warning (0, "number of arguments doesn%'t match "
6709 "built-in prototype");
6710 else
6712 error ("number of arguments doesn%'t match prototype");
6713 error ("%Hprototype declaration",
6714 &current_function_prototype_locus);
6716 break;
6718 /* Type for passing arg must be consistent with that
6719 declared for the arg. ISO C says we take the unqualified
6720 type for parameters declared with qualified type. */
6721 if (TREE_TYPE (parm) != error_mark_node
6722 && TREE_TYPE (type) != error_mark_node
6723 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6724 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6726 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6727 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6729 /* Adjust argument to match prototype. E.g. a previous
6730 `int foo(float);' prototype causes
6731 `int foo(x) float x; {...}' to be treated like
6732 `int foo(float x) {...}'. This is particularly
6733 useful for argument types like uid_t. */
6734 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6736 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6737 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6738 && TYPE_PRECISION (TREE_TYPE (parm))
6739 < TYPE_PRECISION (integer_type_node))
6740 DECL_ARG_TYPE (parm) = integer_type_node;
6742 /* ??? Is it possible to get here with a
6743 built-in prototype or will it always have
6744 been diagnosed as conflicting with an
6745 old-style definition and discarded? */
6746 if (current_function_prototype_built_in)
6747 warning (OPT_pedantic, "promoted argument %qD "
6748 "doesn%'t match built-in prototype", parm);
6749 else
6751 pedwarn (input_location, OPT_pedantic, "promoted argument %qD "
6752 "doesn%'t match prototype", parm);
6753 pedwarn (current_function_prototype_locus, OPT_pedantic,
6754 "prototype declaration");
6757 else
6759 if (current_function_prototype_built_in)
6760 warning (0, "argument %qD doesn%'t match "
6761 "built-in prototype", parm);
6762 else
6764 error ("argument %qD doesn%'t match prototype", parm);
6765 error ("%Hprototype declaration",
6766 &current_function_prototype_locus);
6771 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6774 /* Otherwise, create a prototype that would match. */
6776 else
6778 tree actual = 0, last = 0, type;
6780 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6782 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6783 if (last)
6784 TREE_CHAIN (last) = type;
6785 else
6786 actual = type;
6787 last = type;
6789 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6790 if (last)
6791 TREE_CHAIN (last) = type;
6792 else
6793 actual = type;
6795 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6796 of the type of this function, but we need to avoid having this
6797 affect the types of other similarly-typed functions, so we must
6798 first force the generation of an identical (but separate) type
6799 node for the relevant function type. The new node we create
6800 will be a variant of the main variant of the original function
6801 type. */
6803 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6805 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6809 /* Store parameter declarations passed in ARG_INFO into the current
6810 function declaration. */
6812 void
6813 store_parm_decls_from (struct c_arg_info *arg_info)
6815 current_function_arg_info = arg_info;
6816 store_parm_decls ();
6819 /* Store the parameter declarations into the current function declaration.
6820 This is called after parsing the parameter declarations, before
6821 digesting the body of the function.
6823 For an old-style definition, construct a prototype out of the old-style
6824 parameter declarations and inject it into the function's type. */
6826 void
6827 store_parm_decls (void)
6829 tree fndecl = current_function_decl;
6830 bool proto;
6832 /* The argument information block for FNDECL. */
6833 struct c_arg_info *arg_info = current_function_arg_info;
6834 current_function_arg_info = 0;
6836 /* True if this definition is written with a prototype. Note:
6837 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6838 list in a function definition as equivalent to (void) -- an
6839 empty argument list specifies the function has no parameters,
6840 but only (void) sets up a prototype for future calls. */
6841 proto = arg_info->types != 0;
6843 if (proto)
6844 store_parm_decls_newstyle (fndecl, arg_info);
6845 else
6846 store_parm_decls_oldstyle (fndecl, arg_info);
6848 /* The next call to push_scope will be a function body. */
6850 next_is_function_body = true;
6852 /* Write a record describing this function definition to the prototypes
6853 file (if requested). */
6855 gen_aux_info_record (fndecl, 1, 0, proto);
6857 /* Initialize the RTL code for the function. */
6858 allocate_struct_function (fndecl, false);
6860 /* Begin the statement tree for this function. */
6861 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6863 /* ??? Insert the contents of the pending sizes list into the function
6864 to be evaluated. The only reason left to have this is
6865 void foo(int n, int array[n++])
6866 because we throw away the array type in favor of a pointer type, and
6867 thus won't naturally see the SAVE_EXPR containing the increment. All
6868 other pending sizes would be handled by gimplify_parameters. */
6870 tree t;
6871 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6872 add_stmt (TREE_VALUE (t));
6875 /* Even though we're inside a function body, we still don't want to
6876 call expand_expr to calculate the size of a variable-sized array.
6877 We haven't necessarily assigned RTL to all variables yet, so it's
6878 not safe to try to expand expressions involving them. */
6879 cfun->dont_save_pending_sizes_p = 1;
6882 /* Emit diagnostics that require gimple input for detection. Operate on
6883 FNDECL and all its nested functions. */
6885 static void
6886 c_gimple_diagnostics_recursively (tree fndecl)
6888 struct cgraph_node *cgn;
6889 gimple_seq body = gimple_body (fndecl);
6891 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6892 c_warn_unused_result (body);
6894 /* Notice when OpenMP structured block constraints are violated. */
6895 if (flag_openmp)
6896 diagnose_omp_structured_block_errors (fndecl);
6898 /* Finalize all nested functions now. */
6899 cgn = cgraph_node (fndecl);
6900 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6901 c_gimple_diagnostics_recursively (cgn->decl);
6904 /* Finish up a function declaration and compile that function
6905 all the way to assembler language output. The free the storage
6906 for the function definition.
6908 This is called after parsing the body of the function definition. */
6910 void
6911 finish_function (void)
6913 tree fndecl = current_function_decl;
6915 label_context_stack_se = label_context_stack_se->next;
6916 label_context_stack_vm = label_context_stack_vm->next;
6918 if (TREE_CODE (fndecl) == FUNCTION_DECL
6919 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6921 tree args = DECL_ARGUMENTS (fndecl);
6922 for (; args; args = TREE_CHAIN (args))
6924 tree type = TREE_TYPE (args);
6925 if (INTEGRAL_TYPE_P (type)
6926 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6927 DECL_ARG_TYPE (args) = integer_type_node;
6931 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6932 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6934 /* Must mark the RESULT_DECL as being in this function. */
6936 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6937 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6939 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
6940 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6941 == integer_type_node && flag_isoc99)
6943 tree stmt = c_finish_return (integer_zero_node, NULL_TREE);
6944 /* Hack. We don't want the middle-end to warn that this return
6945 is unreachable, so we mark its location as special. Using
6946 UNKNOWN_LOCATION has the problem that it gets clobbered in
6947 annotate_one_with_locus. A cleaner solution might be to
6948 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6950 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6953 /* Tie off the statement tree for this function. */
6954 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6956 finish_fname_decls ();
6958 /* Complain if there's just no return statement. */
6959 if (warn_return_type
6960 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6961 && !current_function_returns_value && !current_function_returns_null
6962 /* Don't complain if we are no-return. */
6963 && !current_function_returns_abnormally
6964 /* Don't warn for main(). */
6965 && !MAIN_NAME_P (DECL_NAME (fndecl))
6966 /* Or if they didn't actually specify a return type. */
6967 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6968 /* Normally, with -Wreturn-type, flow will complain, but we might
6969 optimize out static functions. */
6970 && !TREE_PUBLIC (fndecl))
6972 warning (OPT_Wreturn_type,
6973 "no return statement in function returning non-void");
6974 TREE_NO_WARNING (fndecl) = 1;
6977 /* Store the end of the function, so that we get good line number
6978 info for the epilogue. */
6979 cfun->function_end_locus = input_location;
6981 /* Finalize the ELF visibility for the function. */
6982 c_determine_visibility (fndecl);
6984 /* For GNU C extern inline functions disregard inline limits. */
6985 if (DECL_EXTERNAL (fndecl)
6986 && DECL_DECLARED_INLINE_P (fndecl))
6987 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
6989 /* Genericize before inlining. Delay genericizing nested functions
6990 until their parent function is genericized. Since finalizing
6991 requires GENERIC, delay that as well. */
6993 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6994 && !undef_nested_function)
6996 if (!decl_function_context (fndecl))
6998 c_genericize (fndecl);
6999 c_gimple_diagnostics_recursively (fndecl);
7001 /* ??? Objc emits functions after finalizing the compilation unit.
7002 This should be cleaned up later and this conditional removed. */
7003 if (cgraph_global_info_ready)
7005 cgraph_add_new_function (fndecl, false);
7006 return;
7009 cgraph_finalize_function (fndecl, false);
7011 else
7013 /* Register this function with cgraph just far enough to get it
7014 added to our parent's nested function list. Handy, since the
7015 C front end doesn't have such a list. */
7016 (void) cgraph_node (fndecl);
7020 if (!decl_function_context (fndecl))
7021 undef_nested_function = false;
7023 /* We're leaving the context of this function, so zap cfun.
7024 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
7025 tree_rest_of_compilation. */
7026 set_cfun (NULL);
7027 current_function_decl = NULL;
7030 /* Check the declarations given in a for-loop for satisfying the C99
7031 constraints. If exactly one such decl is found, return it. */
7033 tree
7034 check_for_loop_decls (void)
7036 struct c_binding *b;
7037 tree one_decl = NULL_TREE;
7038 int n_decls = 0;
7040 if (!flag_isoc99)
7042 static bool hint = true;
7043 /* If we get here, declarations have been used in a for loop without
7044 the C99 for loop scope. This doesn't make much sense, so don't
7045 allow it. */
7046 error ("%<for%> loop initial declarations are only allowed in C99 mode");
7047 if (hint)
7049 inform (input_location,
7050 "use option -std=c99 or -std=gnu99 to compile your code");
7051 hint = false;
7053 return NULL_TREE;
7055 /* C99 subclause 6.8.5 paragraph 3:
7057 [#3] The declaration part of a for statement shall only
7058 declare identifiers for objects having storage class auto or
7059 register.
7061 It isn't clear whether, in this sentence, "identifiers" binds to
7062 "shall only declare" or to "objects" - that is, whether all identifiers
7063 declared must be identifiers for objects, or whether the restriction
7064 only applies to those that are. (A question on this in comp.std.c
7065 in November 2000 received no answer.) We implement the strictest
7066 interpretation, to avoid creating an extension which later causes
7067 problems. */
7069 for (b = current_scope->bindings; b; b = b->prev)
7071 tree id = b->id;
7072 tree decl = b->decl;
7074 if (!id)
7075 continue;
7077 switch (TREE_CODE (decl))
7079 case VAR_DECL:
7080 if (TREE_STATIC (decl))
7081 error ("declaration of static variable %q+D in %<for%> loop "
7082 "initial declaration", decl);
7083 else if (DECL_EXTERNAL (decl))
7084 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
7085 "initial declaration", decl);
7086 break;
7088 case RECORD_TYPE:
7089 error ("%<struct %E%> declared in %<for%> loop initial declaration",
7090 id);
7091 break;
7092 case UNION_TYPE:
7093 error ("%<union %E%> declared in %<for%> loop initial declaration",
7094 id);
7095 break;
7096 case ENUMERAL_TYPE:
7097 error ("%<enum %E%> declared in %<for%> loop initial declaration",
7098 id);
7099 break;
7100 default:
7101 error ("declaration of non-variable %q+D in %<for%> loop "
7102 "initial declaration", decl);
7105 n_decls++;
7106 one_decl = decl;
7109 return n_decls == 1 ? one_decl : NULL_TREE;
7112 /* Save and reinitialize the variables
7113 used during compilation of a C function. */
7115 void
7116 c_push_function_context (void)
7118 struct language_function *p;
7119 p = GGC_NEW (struct language_function);
7120 cfun->language = p;
7122 p->base.x_stmt_tree = c_stmt_tree;
7123 p->x_break_label = c_break_label;
7124 p->x_cont_label = c_cont_label;
7125 p->x_switch_stack = c_switch_stack;
7126 p->arg_info = current_function_arg_info;
7127 p->returns_value = current_function_returns_value;
7128 p->returns_null = current_function_returns_null;
7129 p->returns_abnormally = current_function_returns_abnormally;
7130 p->warn_about_return_type = warn_about_return_type;
7132 push_function_context ();
7135 /* Restore the variables used during compilation of a C function. */
7137 void
7138 c_pop_function_context (void)
7140 struct language_function *p;
7142 pop_function_context ();
7143 p = cfun->language;
7144 cfun->language = NULL;
7146 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
7147 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
7149 /* Stop pointing to the local nodes about to be freed. */
7150 /* But DECL_INITIAL must remain nonzero so we know this
7151 was an actual function definition. */
7152 DECL_INITIAL (current_function_decl) = error_mark_node;
7153 DECL_ARGUMENTS (current_function_decl) = 0;
7156 c_stmt_tree = p->base.x_stmt_tree;
7157 c_break_label = p->x_break_label;
7158 c_cont_label = p->x_cont_label;
7159 c_switch_stack = p->x_switch_stack;
7160 current_function_arg_info = p->arg_info;
7161 current_function_returns_value = p->returns_value;
7162 current_function_returns_null = p->returns_null;
7163 current_function_returns_abnormally = p->returns_abnormally;
7164 warn_about_return_type = p->warn_about_return_type;
7167 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
7169 void
7170 c_dup_lang_specific_decl (tree decl)
7172 struct lang_decl *ld;
7174 if (!DECL_LANG_SPECIFIC (decl))
7175 return;
7177 ld = GGC_NEW (struct lang_decl);
7178 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
7179 DECL_LANG_SPECIFIC (decl) = ld;
7182 /* The functions below are required for functionality of doing
7183 function at once processing in the C front end. Currently these
7184 functions are not called from anywhere in the C front end, but as
7185 these changes continue, that will change. */
7187 /* Returns the stmt_tree (if any) to which statements are currently
7188 being added. If there is no active statement-tree, NULL is
7189 returned. */
7191 stmt_tree
7192 current_stmt_tree (void)
7194 return &c_stmt_tree;
7197 /* Return the global value of T as a symbol. */
7199 tree
7200 identifier_global_value (tree t)
7202 struct c_binding *b;
7204 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7205 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7206 return b->decl;
7208 return 0;
7211 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
7212 otherwise the name is found in ridpointers from RID_INDEX. */
7214 void
7215 record_builtin_type (enum rid rid_index, const char *name, tree type)
7217 tree id, decl;
7218 if (name == 0)
7219 id = ridpointers[(int) rid_index];
7220 else
7221 id = get_identifier (name);
7222 decl = build_decl (TYPE_DECL, id, type);
7223 pushdecl (decl);
7224 if (debug_hooks->type_decl)
7225 debug_hooks->type_decl (decl, false);
7228 /* Build the void_list_node (void_type_node having been created). */
7229 tree
7230 build_void_list_node (void)
7232 tree t = build_tree_list (NULL_TREE, void_type_node);
7233 return t;
7236 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7238 struct c_parm *
7239 build_c_parm (struct c_declspecs *specs, tree attrs,
7240 struct c_declarator *declarator)
7242 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7243 ret->specs = specs;
7244 ret->attrs = attrs;
7245 ret->declarator = declarator;
7246 return ret;
7249 /* Return a declarator with nested attributes. TARGET is the inner
7250 declarator to which these attributes apply. ATTRS are the
7251 attributes. */
7253 struct c_declarator *
7254 build_attrs_declarator (tree attrs, struct c_declarator *target)
7256 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7257 ret->kind = cdk_attrs;
7258 ret->declarator = target;
7259 ret->u.attrs = attrs;
7260 return ret;
7263 /* Return a declarator for a function with arguments specified by ARGS
7264 and return type specified by TARGET. */
7266 struct c_declarator *
7267 build_function_declarator (struct c_arg_info *args,
7268 struct c_declarator *target)
7270 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7271 ret->kind = cdk_function;
7272 ret->declarator = target;
7273 ret->u.arg_info = args;
7274 return ret;
7277 /* Return a declarator for the identifier IDENT (which may be
7278 NULL_TREE for an abstract declarator). */
7280 struct c_declarator *
7281 build_id_declarator (tree ident)
7283 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7284 ret->kind = cdk_id;
7285 ret->declarator = 0;
7286 ret->u.id = ident;
7287 /* Default value - may get reset to a more precise location. */
7288 ret->id_loc = input_location;
7289 return ret;
7292 /* Return something to represent absolute declarators containing a *.
7293 TARGET is the absolute declarator that the * contains.
7294 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7295 to apply to the pointer type. */
7297 struct c_declarator *
7298 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7299 struct c_declarator *target)
7301 tree attrs;
7302 int quals = 0;
7303 struct c_declarator *itarget = target;
7304 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7305 if (type_quals_attrs)
7307 attrs = type_quals_attrs->attrs;
7308 quals = quals_from_declspecs (type_quals_attrs);
7309 if (attrs != NULL_TREE)
7310 itarget = build_attrs_declarator (attrs, target);
7312 ret->kind = cdk_pointer;
7313 ret->declarator = itarget;
7314 ret->u.pointer_quals = quals;
7315 return ret;
7318 /* Return a pointer to a structure for an empty list of declaration
7319 specifiers. */
7321 struct c_declspecs *
7322 build_null_declspecs (void)
7324 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7325 ret->type = 0;
7326 ret->expr = 0;
7327 ret->decl_attr = 0;
7328 ret->attrs = 0;
7329 ret->typespec_word = cts_none;
7330 ret->storage_class = csc_none;
7331 ret->expr_const_operands = true;
7332 ret->declspecs_seen_p = false;
7333 ret->type_seen_p = false;
7334 ret->non_sc_seen_p = false;
7335 ret->typedef_p = false;
7336 ret->tag_defined_p = false;
7337 ret->explicit_signed_p = false;
7338 ret->deprecated_p = false;
7339 ret->default_int_p = false;
7340 ret->long_p = false;
7341 ret->long_long_p = false;
7342 ret->short_p = false;
7343 ret->signed_p = false;
7344 ret->unsigned_p = false;
7345 ret->complex_p = false;
7346 ret->inline_p = false;
7347 ret->thread_p = false;
7348 ret->const_p = false;
7349 ret->volatile_p = false;
7350 ret->restrict_p = false;
7351 ret->saturating_p = false;
7352 return ret;
7355 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7356 returning SPECS. */
7358 struct c_declspecs *
7359 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7361 enum rid i;
7362 bool dupe = false;
7363 specs->non_sc_seen_p = true;
7364 specs->declspecs_seen_p = true;
7365 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7366 && C_IS_RESERVED_WORD (qual));
7367 i = C_RID_CODE (qual);
7368 switch (i)
7370 case RID_CONST:
7371 dupe = specs->const_p;
7372 specs->const_p = true;
7373 break;
7374 case RID_VOLATILE:
7375 dupe = specs->volatile_p;
7376 specs->volatile_p = true;
7377 break;
7378 case RID_RESTRICT:
7379 dupe = specs->restrict_p;
7380 specs->restrict_p = true;
7381 break;
7382 default:
7383 gcc_unreachable ();
7385 if (dupe && !flag_isoc99)
7386 pedwarn (input_location, OPT_pedantic, "duplicate %qE", qual);
7387 return specs;
7390 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7391 returning SPECS. */
7393 struct c_declspecs *
7394 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7396 tree type = spec.spec;
7397 specs->non_sc_seen_p = true;
7398 specs->declspecs_seen_p = true;
7399 specs->type_seen_p = true;
7400 if (TREE_DEPRECATED (type))
7401 specs->deprecated_p = true;
7403 /* Handle type specifier keywords. */
7404 if (TREE_CODE (type) == IDENTIFIER_NODE
7405 && C_IS_RESERVED_WORD (type)
7406 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
7408 enum rid i = C_RID_CODE (type);
7409 if (specs->type)
7411 error ("two or more data types in declaration specifiers");
7412 return specs;
7414 if ((int) i <= (int) RID_LAST_MODIFIER)
7416 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
7417 bool dupe = false;
7418 switch (i)
7420 case RID_LONG:
7421 if (specs->long_long_p)
7423 error ("%<long long long%> is too long for GCC");
7424 break;
7426 if (specs->long_p)
7428 if (specs->typespec_word == cts_double)
7430 error ("both %<long long%> and %<double%> in "
7431 "declaration specifiers");
7432 break;
7434 pedwarn_c90 (input_location, OPT_Wlong_long,
7435 "ISO C90 does not support %<long long%>");
7436 specs->long_long_p = 1;
7437 break;
7439 if (specs->short_p)
7440 error ("both %<long%> and %<short%> in "
7441 "declaration specifiers");
7442 else if (specs->typespec_word == cts_void)
7443 error ("both %<long%> and %<void%> in "
7444 "declaration specifiers");
7445 else if (specs->typespec_word == cts_bool)
7446 error ("both %<long%> and %<_Bool%> in "
7447 "declaration specifiers");
7448 else if (specs->typespec_word == cts_char)
7449 error ("both %<long%> and %<char%> in "
7450 "declaration specifiers");
7451 else if (specs->typespec_word == cts_float)
7452 error ("both %<long%> and %<float%> in "
7453 "declaration specifiers");
7454 else if (specs->typespec_word == cts_dfloat32)
7455 error ("both %<long%> and %<_Decimal32%> in "
7456 "declaration specifiers");
7457 else if (specs->typespec_word == cts_dfloat64)
7458 error ("both %<long%> and %<_Decimal64%> in "
7459 "declaration specifiers");
7460 else if (specs->typespec_word == cts_dfloat128)
7461 error ("both %<long%> and %<_Decimal128%> in "
7462 "declaration specifiers");
7463 else
7464 specs->long_p = true;
7465 break;
7466 case RID_SHORT:
7467 dupe = specs->short_p;
7468 if (specs->long_p)
7469 error ("both %<long%> and %<short%> in "
7470 "declaration specifiers");
7471 else if (specs->typespec_word == cts_void)
7472 error ("both %<short%> and %<void%> in "
7473 "declaration specifiers");
7474 else if (specs->typespec_word == cts_bool)
7475 error ("both %<short%> and %<_Bool%> in "
7476 "declaration specifiers");
7477 else if (specs->typespec_word == cts_char)
7478 error ("both %<short%> and %<char%> in "
7479 "declaration specifiers");
7480 else if (specs->typespec_word == cts_float)
7481 error ("both %<short%> and %<float%> in "
7482 "declaration specifiers");
7483 else if (specs->typespec_word == cts_double)
7484 error ("both %<short%> and %<double%> in "
7485 "declaration specifiers");
7486 else if (specs->typespec_word == cts_dfloat32)
7487 error ("both %<short%> and %<_Decimal32%> in "
7488 "declaration specifiers");
7489 else if (specs->typespec_word == cts_dfloat64)
7490 error ("both %<short%> and %<_Decimal64%> in "
7491 "declaration specifiers");
7492 else if (specs->typespec_word == cts_dfloat128)
7493 error ("both %<short%> and %<_Decimal128%> in "
7494 "declaration specifiers");
7495 else
7496 specs->short_p = true;
7497 break;
7498 case RID_SIGNED:
7499 dupe = specs->signed_p;
7500 if (specs->unsigned_p)
7501 error ("both %<signed%> and %<unsigned%> in "
7502 "declaration specifiers");
7503 else if (specs->typespec_word == cts_void)
7504 error ("both %<signed%> and %<void%> in "
7505 "declaration specifiers");
7506 else if (specs->typespec_word == cts_bool)
7507 error ("both %<signed%> and %<_Bool%> in "
7508 "declaration specifiers");
7509 else if (specs->typespec_word == cts_float)
7510 error ("both %<signed%> and %<float%> in "
7511 "declaration specifiers");
7512 else if (specs->typespec_word == cts_double)
7513 error ("both %<signed%> and %<double%> in "
7514 "declaration specifiers");
7515 else if (specs->typespec_word == cts_dfloat32)
7516 error ("both %<signed%> and %<_Decimal32%> in "
7517 "declaration specifiers");
7518 else if (specs->typespec_word == cts_dfloat64)
7519 error ("both %<signed%> and %<_Decimal64%> in "
7520 "declaration specifiers");
7521 else if (specs->typespec_word == cts_dfloat128)
7522 error ("both %<signed%> and %<_Decimal128%> in "
7523 "declaration specifiers");
7524 else
7525 specs->signed_p = true;
7526 break;
7527 case RID_UNSIGNED:
7528 dupe = specs->unsigned_p;
7529 if (specs->signed_p)
7530 error ("both %<signed%> and %<unsigned%> in "
7531 "declaration specifiers");
7532 else if (specs->typespec_word == cts_void)
7533 error ("both %<unsigned%> and %<void%> in "
7534 "declaration specifiers");
7535 else if (specs->typespec_word == cts_bool)
7536 error ("both %<unsigned%> and %<_Bool%> in "
7537 "declaration specifiers");
7538 else if (specs->typespec_word == cts_float)
7539 error ("both %<unsigned%> and %<float%> in "
7540 "declaration specifiers");
7541 else if (specs->typespec_word == cts_double)
7542 error ("both %<unsigned%> and %<double%> in "
7543 "declaration specifiers");
7544 else if (specs->typespec_word == cts_dfloat32)
7545 error ("both %<unsigned%> and %<_Decimal32%> in "
7546 "declaration specifiers");
7547 else if (specs->typespec_word == cts_dfloat64)
7548 error ("both %<unsigned%> and %<_Decimal64%> in "
7549 "declaration specifiers");
7550 else if (specs->typespec_word == cts_dfloat128)
7551 error ("both %<unsigned%> and %<_Decimal128%> in "
7552 "declaration specifiers");
7553 else
7554 specs->unsigned_p = true;
7555 break;
7556 case RID_COMPLEX:
7557 dupe = specs->complex_p;
7558 if (!flag_isoc99 && !in_system_header)
7559 pedwarn (input_location, OPT_pedantic, "ISO C90 does not support complex types");
7560 if (specs->typespec_word == cts_void)
7561 error ("both %<complex%> and %<void%> in "
7562 "declaration specifiers");
7563 else if (specs->typespec_word == cts_bool)
7564 error ("both %<complex%> and %<_Bool%> in "
7565 "declaration specifiers");
7566 else if (specs->typespec_word == cts_dfloat32)
7567 error ("both %<complex%> and %<_Decimal32%> in "
7568 "declaration specifiers");
7569 else if (specs->typespec_word == cts_dfloat64)
7570 error ("both %<complex%> and %<_Decimal64%> in "
7571 "declaration specifiers");
7572 else if (specs->typespec_word == cts_dfloat128)
7573 error ("both %<complex%> and %<_Decimal128%> in "
7574 "declaration specifiers");
7575 else if (specs->typespec_word == cts_fract)
7576 error ("both %<complex%> and %<_Fract%> in "
7577 "declaration specifiers");
7578 else if (specs->typespec_word == cts_accum)
7579 error ("both %<complex%> and %<_Accum%> in "
7580 "declaration specifiers");
7581 else if (specs->saturating_p)
7582 error ("both %<complex%> and %<_Sat%> in "
7583 "declaration specifiers");
7584 else
7585 specs->complex_p = true;
7586 break;
7587 case RID_SAT:
7588 dupe = specs->saturating_p;
7589 pedwarn (input_location, OPT_pedantic, "ISO C does not support saturating types");
7590 if (specs->typespec_word == cts_void)
7591 error ("both %<_Sat%> and %<void%> in "
7592 "declaration specifiers");
7593 else if (specs->typespec_word == cts_bool)
7594 error ("both %<_Sat%> and %<_Bool%> in "
7595 "declaration specifiers");
7596 else if (specs->typespec_word == cts_char)
7597 error ("both %<_Sat%> and %<char%> in "
7598 "declaration specifiers");
7599 else if (specs->typespec_word == cts_int)
7600 error ("both %<_Sat%> and %<int%> in "
7601 "declaration specifiers");
7602 else if (specs->typespec_word == cts_float)
7603 error ("both %<_Sat%> and %<float%> in "
7604 "declaration specifiers");
7605 else if (specs->typespec_word == cts_double)
7606 error ("both %<_Sat%> and %<double%> in "
7607 "declaration specifiers");
7608 else if (specs->typespec_word == cts_dfloat32)
7609 error ("both %<_Sat%> and %<_Decimal32%> in "
7610 "declaration specifiers");
7611 else if (specs->typespec_word == cts_dfloat64)
7612 error ("both %<_Sat%> and %<_Decimal64%> in "
7613 "declaration specifiers");
7614 else if (specs->typespec_word == cts_dfloat128)
7615 error ("both %<_Sat%> and %<_Decimal128%> in "
7616 "declaration specifiers");
7617 else if (specs->complex_p)
7618 error ("both %<_Sat%> and %<complex%> in "
7619 "declaration specifiers");
7620 else
7621 specs->saturating_p = true;
7622 break;
7623 default:
7624 gcc_unreachable ();
7627 if (dupe)
7628 error ("duplicate %qE", type);
7630 return specs;
7632 else
7634 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
7635 "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
7636 if (specs->typespec_word != cts_none)
7638 error ("two or more data types in declaration specifiers");
7639 return specs;
7641 switch (i)
7643 case RID_VOID:
7644 if (specs->long_p)
7645 error ("both %<long%> and %<void%> in "
7646 "declaration specifiers");
7647 else if (specs->short_p)
7648 error ("both %<short%> and %<void%> in "
7649 "declaration specifiers");
7650 else if (specs->signed_p)
7651 error ("both %<signed%> and %<void%> in "
7652 "declaration specifiers");
7653 else if (specs->unsigned_p)
7654 error ("both %<unsigned%> and %<void%> in "
7655 "declaration specifiers");
7656 else if (specs->complex_p)
7657 error ("both %<complex%> and %<void%> in "
7658 "declaration specifiers");
7659 else if (specs->saturating_p)
7660 error ("both %<_Sat%> and %<void%> in "
7661 "declaration specifiers");
7662 else
7663 specs->typespec_word = cts_void;
7664 return specs;
7665 case RID_BOOL:
7666 if (specs->long_p)
7667 error ("both %<long%> and %<_Bool%> in "
7668 "declaration specifiers");
7669 else if (specs->short_p)
7670 error ("both %<short%> and %<_Bool%> in "
7671 "declaration specifiers");
7672 else if (specs->signed_p)
7673 error ("both %<signed%> and %<_Bool%> in "
7674 "declaration specifiers");
7675 else if (specs->unsigned_p)
7676 error ("both %<unsigned%> and %<_Bool%> in "
7677 "declaration specifiers");
7678 else if (specs->complex_p)
7679 error ("both %<complex%> and %<_Bool%> in "
7680 "declaration specifiers");
7681 else if (specs->saturating_p)
7682 error ("both %<_Sat%> and %<_Bool%> in "
7683 "declaration specifiers");
7684 else
7685 specs->typespec_word = cts_bool;
7686 return specs;
7687 case RID_CHAR:
7688 if (specs->long_p)
7689 error ("both %<long%> and %<char%> in "
7690 "declaration specifiers");
7691 else if (specs->short_p)
7692 error ("both %<short%> and %<char%> in "
7693 "declaration specifiers");
7694 else if (specs->saturating_p)
7695 error ("both %<_Sat%> and %<char%> in "
7696 "declaration specifiers");
7697 else
7698 specs->typespec_word = cts_char;
7699 return specs;
7700 case RID_INT:
7701 if (specs->saturating_p)
7702 error ("both %<_Sat%> and %<int%> in "
7703 "declaration specifiers");
7704 else
7705 specs->typespec_word = cts_int;
7706 return specs;
7707 case RID_FLOAT:
7708 if (specs->long_p)
7709 error ("both %<long%> and %<float%> in "
7710 "declaration specifiers");
7711 else if (specs->short_p)
7712 error ("both %<short%> and %<float%> in "
7713 "declaration specifiers");
7714 else if (specs->signed_p)
7715 error ("both %<signed%> and %<float%> in "
7716 "declaration specifiers");
7717 else if (specs->unsigned_p)
7718 error ("both %<unsigned%> and %<float%> in "
7719 "declaration specifiers");
7720 else if (specs->saturating_p)
7721 error ("both %<_Sat%> and %<float%> in "
7722 "declaration specifiers");
7723 else
7724 specs->typespec_word = cts_float;
7725 return specs;
7726 case RID_DOUBLE:
7727 if (specs->long_long_p)
7728 error ("both %<long long%> and %<double%> in "
7729 "declaration specifiers");
7730 else if (specs->short_p)
7731 error ("both %<short%> and %<double%> in "
7732 "declaration specifiers");
7733 else if (specs->signed_p)
7734 error ("both %<signed%> and %<double%> in "
7735 "declaration specifiers");
7736 else if (specs->unsigned_p)
7737 error ("both %<unsigned%> and %<double%> in "
7738 "declaration specifiers");
7739 else if (specs->saturating_p)
7740 error ("both %<_Sat%> and %<double%> in "
7741 "declaration specifiers");
7742 else
7743 specs->typespec_word = cts_double;
7744 return specs;
7745 case RID_DFLOAT32:
7746 case RID_DFLOAT64:
7747 case RID_DFLOAT128:
7749 const char *str;
7750 if (i == RID_DFLOAT32)
7751 str = "_Decimal32";
7752 else if (i == RID_DFLOAT64)
7753 str = "_Decimal64";
7754 else
7755 str = "_Decimal128";
7756 if (specs->long_long_p)
7757 error ("both %<long long%> and %<%s%> in "
7758 "declaration specifiers", str);
7759 if (specs->long_p)
7760 error ("both %<long%> and %<%s%> in "
7761 "declaration specifiers", str);
7762 else if (specs->short_p)
7763 error ("both %<short%> and %<%s%> in "
7764 "declaration specifiers", str);
7765 else if (specs->signed_p)
7766 error ("both %<signed%> and %<%s%> in "
7767 "declaration specifiers", str);
7768 else if (specs->unsigned_p)
7769 error ("both %<unsigned%> and %<%s%> in "
7770 "declaration specifiers", str);
7771 else if (specs->complex_p)
7772 error ("both %<complex%> and %<%s%> in "
7773 "declaration specifiers", str);
7774 else if (specs->saturating_p)
7775 error ("both %<_Sat%> and %<%s%> in "
7776 "declaration specifiers", str);
7777 else if (i == RID_DFLOAT32)
7778 specs->typespec_word = cts_dfloat32;
7779 else if (i == RID_DFLOAT64)
7780 specs->typespec_word = cts_dfloat64;
7781 else
7782 specs->typespec_word = cts_dfloat128;
7784 if (!targetm.decimal_float_supported_p ())
7785 error ("decimal floating point not supported for this target");
7786 pedwarn (input_location, OPT_pedantic,
7787 "ISO C does not support decimal floating point");
7788 return specs;
7789 case RID_FRACT:
7790 case RID_ACCUM:
7792 const char *str;
7793 if (i == RID_FRACT)
7794 str = "_Fract";
7795 else
7796 str = "_Accum";
7797 if (specs->complex_p)
7798 error ("both %<complex%> and %<%s%> in "
7799 "declaration specifiers", str);
7800 else if (i == RID_FRACT)
7801 specs->typespec_word = cts_fract;
7802 else
7803 specs->typespec_word = cts_accum;
7805 if (!targetm.fixed_point_supported_p ())
7806 error ("fixed-point types not supported for this target");
7807 pedwarn (input_location, OPT_pedantic,
7808 "ISO C does not support fixed-point types");
7809 return specs;
7810 default:
7811 /* ObjC reserved word "id", handled below. */
7812 break;
7817 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7818 form of ObjC type, cases such as "int" and "long" being handled
7819 above), a TYPE (struct, union, enum and typeof specifiers) or an
7820 ERROR_MARK. In none of these cases may there have previously
7821 been any type specifiers. */
7822 if (specs->type || specs->typespec_word != cts_none
7823 || specs->long_p || specs->short_p || specs->signed_p
7824 || specs->unsigned_p || specs->complex_p)
7825 error ("two or more data types in declaration specifiers");
7826 else if (TREE_CODE (type) == TYPE_DECL)
7828 if (TREE_TYPE (type) == error_mark_node)
7829 ; /* Allow the type to default to int to avoid cascading errors. */
7830 else
7832 specs->type = TREE_TYPE (type);
7833 specs->decl_attr = DECL_ATTRIBUTES (type);
7834 specs->typedef_p = true;
7835 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7838 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7840 tree t = lookup_name (type);
7841 if (!t || TREE_CODE (t) != TYPE_DECL)
7842 error ("%qE fails to be a typedef or built in type", type);
7843 else if (TREE_TYPE (t) == error_mark_node)
7845 else
7846 specs->type = TREE_TYPE (t);
7848 else if (TREE_CODE (type) != ERROR_MARK)
7850 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7851 specs->tag_defined_p = true;
7852 if (spec.kind == ctsk_typeof)
7854 specs->typedef_p = true;
7855 if (spec.expr)
7857 if (specs->expr)
7858 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
7859 specs->expr, spec.expr);
7860 else
7861 specs->expr = spec.expr;
7862 specs->expr_const_operands &= spec.expr_const_operands;
7865 specs->type = type;
7868 return specs;
7871 /* Add the storage class specifier or function specifier SCSPEC to the
7872 declaration specifiers SPECS, returning SPECS. */
7874 struct c_declspecs *
7875 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7877 enum rid i;
7878 enum c_storage_class n = csc_none;
7879 bool dupe = false;
7880 specs->declspecs_seen_p = true;
7881 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7882 && C_IS_RESERVED_WORD (scspec));
7883 i = C_RID_CODE (scspec);
7884 if (specs->non_sc_seen_p)
7885 warning (OPT_Wold_style_declaration,
7886 "%qE is not at beginning of declaration", scspec);
7887 switch (i)
7889 case RID_INLINE:
7890 /* C99 permits duplicate inline. Although of doubtful utility,
7891 it seems simplest to permit it in gnu89 mode as well, as
7892 there is also little utility in maintaining this as a
7893 difference between gnu89 and C99 inline. */
7894 dupe = false;
7895 specs->inline_p = true;
7896 break;
7897 case RID_THREAD:
7898 dupe = specs->thread_p;
7899 if (specs->storage_class == csc_auto)
7900 error ("%<__thread%> used with %<auto%>");
7901 else if (specs->storage_class == csc_register)
7902 error ("%<__thread%> used with %<register%>");
7903 else if (specs->storage_class == csc_typedef)
7904 error ("%<__thread%> used with %<typedef%>");
7905 else
7906 specs->thread_p = true;
7907 break;
7908 case RID_AUTO:
7909 n = csc_auto;
7910 break;
7911 case RID_EXTERN:
7912 n = csc_extern;
7913 /* Diagnose "__thread extern". */
7914 if (specs->thread_p)
7915 error ("%<__thread%> before %<extern%>");
7916 break;
7917 case RID_REGISTER:
7918 n = csc_register;
7919 break;
7920 case RID_STATIC:
7921 n = csc_static;
7922 /* Diagnose "__thread static". */
7923 if (specs->thread_p)
7924 error ("%<__thread%> before %<static%>");
7925 break;
7926 case RID_TYPEDEF:
7927 n = csc_typedef;
7928 break;
7929 default:
7930 gcc_unreachable ();
7932 if (n != csc_none && n == specs->storage_class)
7933 dupe = true;
7934 if (dupe)
7935 error ("duplicate %qE", scspec);
7936 if (n != csc_none)
7938 if (specs->storage_class != csc_none && n != specs->storage_class)
7940 error ("multiple storage classes in declaration specifiers");
7942 else
7944 specs->storage_class = n;
7945 if (n != csc_extern && n != csc_static && specs->thread_p)
7947 error ("%<__thread%> used with %qE", scspec);
7948 specs->thread_p = false;
7952 return specs;
7955 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7956 returning SPECS. */
7958 struct c_declspecs *
7959 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7961 specs->attrs = chainon (attrs, specs->attrs);
7962 specs->declspecs_seen_p = true;
7963 return specs;
7966 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7967 specifiers with any other type specifier to determine the resulting
7968 type. This is where ISO C checks on complex types are made, since
7969 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7970 double". */
7972 struct c_declspecs *
7973 finish_declspecs (struct c_declspecs *specs)
7975 /* If a type was specified as a whole, we have no modifiers and are
7976 done. */
7977 if (specs->type != NULL_TREE)
7979 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7980 && !specs->signed_p && !specs->unsigned_p
7981 && !specs->complex_p);
7982 return specs;
7985 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7986 has been specified, treat it as "int" unless "_Complex" is
7987 present and there are no other specifiers. If we just have
7988 "_Complex", it is equivalent to "_Complex double", but e.g.
7989 "_Complex short" is equivalent to "_Complex short int". */
7990 if (specs->typespec_word == cts_none)
7992 if (specs->saturating_p)
7994 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
7995 if (!targetm.fixed_point_supported_p ())
7996 error ("fixed-point types not supported for this target");
7997 specs->typespec_word = cts_fract;
7999 else if (specs->long_p || specs->short_p
8000 || specs->signed_p || specs->unsigned_p)
8002 specs->typespec_word = cts_int;
8004 else if (specs->complex_p)
8006 specs->typespec_word = cts_double;
8007 pedwarn (input_location, OPT_pedantic,
8008 "ISO C does not support plain %<complex%> meaning "
8009 "%<double complex%>");
8011 else
8013 specs->typespec_word = cts_int;
8014 specs->default_int_p = true;
8015 /* We don't diagnose this here because grokdeclarator will
8016 give more specific diagnostics according to whether it is
8017 a function definition. */
8021 /* If "signed" was specified, record this to distinguish "int" and
8022 "signed int" in the case of a bit-field with
8023 -funsigned-bitfields. */
8024 specs->explicit_signed_p = specs->signed_p;
8026 /* Now compute the actual type. */
8027 switch (specs->typespec_word)
8029 case cts_void:
8030 gcc_assert (!specs->long_p && !specs->short_p
8031 && !specs->signed_p && !specs->unsigned_p
8032 && !specs->complex_p);
8033 specs->type = void_type_node;
8034 break;
8035 case cts_bool:
8036 gcc_assert (!specs->long_p && !specs->short_p
8037 && !specs->signed_p && !specs->unsigned_p
8038 && !specs->complex_p);
8039 specs->type = boolean_type_node;
8040 break;
8041 case cts_char:
8042 gcc_assert (!specs->long_p && !specs->short_p);
8043 gcc_assert (!(specs->signed_p && specs->unsigned_p));
8044 if (specs->signed_p)
8045 specs->type = signed_char_type_node;
8046 else if (specs->unsigned_p)
8047 specs->type = unsigned_char_type_node;
8048 else
8049 specs->type = char_type_node;
8050 if (specs->complex_p)
8052 pedwarn (input_location, OPT_pedantic,
8053 "ISO C does not support complex integer types");
8054 specs->type = build_complex_type (specs->type);
8056 break;
8057 case cts_int:
8058 gcc_assert (!(specs->long_p && specs->short_p));
8059 gcc_assert (!(specs->signed_p && specs->unsigned_p));
8060 if (specs->long_long_p)
8061 specs->type = (specs->unsigned_p
8062 ? long_long_unsigned_type_node
8063 : long_long_integer_type_node);
8064 else if (specs->long_p)
8065 specs->type = (specs->unsigned_p
8066 ? long_unsigned_type_node
8067 : long_integer_type_node);
8068 else if (specs->short_p)
8069 specs->type = (specs->unsigned_p
8070 ? short_unsigned_type_node
8071 : short_integer_type_node);
8072 else
8073 specs->type = (specs->unsigned_p
8074 ? unsigned_type_node
8075 : integer_type_node);
8076 if (specs->complex_p)
8078 pedwarn (input_location, OPT_pedantic,
8079 "ISO C does not support complex integer types");
8080 specs->type = build_complex_type (specs->type);
8082 break;
8083 case cts_float:
8084 gcc_assert (!specs->long_p && !specs->short_p
8085 && !specs->signed_p && !specs->unsigned_p);
8086 specs->type = (specs->complex_p
8087 ? complex_float_type_node
8088 : float_type_node);
8089 break;
8090 case cts_double:
8091 gcc_assert (!specs->long_long_p && !specs->short_p
8092 && !specs->signed_p && !specs->unsigned_p);
8093 if (specs->long_p)
8095 specs->type = (specs->complex_p
8096 ? complex_long_double_type_node
8097 : long_double_type_node);
8099 else
8101 specs->type = (specs->complex_p
8102 ? complex_double_type_node
8103 : double_type_node);
8105 break;
8106 case cts_dfloat32:
8107 case cts_dfloat64:
8108 case cts_dfloat128:
8109 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
8110 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
8111 if (specs->typespec_word == cts_dfloat32)
8112 specs->type = dfloat32_type_node;
8113 else if (specs->typespec_word == cts_dfloat64)
8114 specs->type = dfloat64_type_node;
8115 else
8116 specs->type = dfloat128_type_node;
8117 break;
8118 case cts_fract:
8119 gcc_assert (!specs->complex_p);
8120 if (!targetm.fixed_point_supported_p ())
8121 specs->type = integer_type_node;
8122 else if (specs->saturating_p)
8124 if (specs->long_long_p)
8125 specs->type = specs->unsigned_p
8126 ? sat_unsigned_long_long_fract_type_node
8127 : sat_long_long_fract_type_node;
8128 else if (specs->long_p)
8129 specs->type = specs->unsigned_p
8130 ? sat_unsigned_long_fract_type_node
8131 : sat_long_fract_type_node;
8132 else if (specs->short_p)
8133 specs->type = specs->unsigned_p
8134 ? sat_unsigned_short_fract_type_node
8135 : sat_short_fract_type_node;
8136 else
8137 specs->type = specs->unsigned_p
8138 ? sat_unsigned_fract_type_node
8139 : sat_fract_type_node;
8141 else
8143 if (specs->long_long_p)
8144 specs->type = specs->unsigned_p
8145 ? unsigned_long_long_fract_type_node
8146 : long_long_fract_type_node;
8147 else if (specs->long_p)
8148 specs->type = specs->unsigned_p
8149 ? unsigned_long_fract_type_node
8150 : long_fract_type_node;
8151 else if (specs->short_p)
8152 specs->type = specs->unsigned_p
8153 ? unsigned_short_fract_type_node
8154 : short_fract_type_node;
8155 else
8156 specs->type = specs->unsigned_p
8157 ? unsigned_fract_type_node
8158 : fract_type_node;
8160 break;
8161 case cts_accum:
8162 gcc_assert (!specs->complex_p);
8163 if (!targetm.fixed_point_supported_p ())
8164 specs->type = integer_type_node;
8165 else if (specs->saturating_p)
8167 if (specs->long_long_p)
8168 specs->type = specs->unsigned_p
8169 ? sat_unsigned_long_long_accum_type_node
8170 : sat_long_long_accum_type_node;
8171 else if (specs->long_p)
8172 specs->type = specs->unsigned_p
8173 ? sat_unsigned_long_accum_type_node
8174 : sat_long_accum_type_node;
8175 else if (specs->short_p)
8176 specs->type = specs->unsigned_p
8177 ? sat_unsigned_short_accum_type_node
8178 : sat_short_accum_type_node;
8179 else
8180 specs->type = specs->unsigned_p
8181 ? sat_unsigned_accum_type_node
8182 : sat_accum_type_node;
8184 else
8186 if (specs->long_long_p)
8187 specs->type = specs->unsigned_p
8188 ? unsigned_long_long_accum_type_node
8189 : long_long_accum_type_node;
8190 else if (specs->long_p)
8191 specs->type = specs->unsigned_p
8192 ? unsigned_long_accum_type_node
8193 : long_accum_type_node;
8194 else if (specs->short_p)
8195 specs->type = specs->unsigned_p
8196 ? unsigned_short_accum_type_node
8197 : short_accum_type_node;
8198 else
8199 specs->type = specs->unsigned_p
8200 ? unsigned_accum_type_node
8201 : accum_type_node;
8203 break;
8204 default:
8205 gcc_unreachable ();
8208 return specs;
8211 /* A subroutine of c_write_global_declarations. Perform final processing
8212 on one file scope's declarations (or the external scope's declarations),
8213 GLOBALS. */
8215 static void
8216 c_write_global_declarations_1 (tree globals)
8218 tree decl;
8219 bool reconsider;
8221 /* Process the decls in the order they were written. */
8222 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8224 /* Check for used but undefined static functions using the C
8225 standard's definition of "used", and set TREE_NO_WARNING so
8226 that check_global_declarations doesn't repeat the check. */
8227 if (TREE_CODE (decl) == FUNCTION_DECL
8228 && DECL_INITIAL (decl) == 0
8229 && DECL_EXTERNAL (decl)
8230 && !TREE_PUBLIC (decl)
8231 && C_DECL_USED (decl))
8233 pedwarn (input_location, 0, "%q+F used but never defined", decl);
8234 TREE_NO_WARNING (decl) = 1;
8237 wrapup_global_declaration_1 (decl);
8242 reconsider = false;
8243 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8244 reconsider |= wrapup_global_declaration_2 (decl);
8246 while (reconsider);
8248 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8249 check_global_declaration_1 (decl);
8252 /* A subroutine of c_write_global_declarations Emit debug information for each
8253 of the declarations in GLOBALS. */
8255 static void
8256 c_write_global_declarations_2 (tree globals)
8258 tree decl;
8260 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
8261 debug_hooks->global_decl (decl);
8264 /* Preserve the external declarations scope across a garbage collect. */
8265 static GTY(()) tree ext_block;
8267 void
8268 c_write_global_declarations (void)
8270 tree t;
8272 /* We don't want to do this if generating a PCH. */
8273 if (pch_file)
8274 return;
8276 /* Don't waste time on further processing if -fsyntax-only or we've
8277 encountered errors. */
8278 if (flag_syntax_only || errorcount || sorrycount)
8279 return;
8281 /* Close the external scope. */
8282 ext_block = pop_scope ();
8283 external_scope = 0;
8284 gcc_assert (!current_scope);
8286 if (ext_block)
8288 tree tmp = BLOCK_VARS (ext_block);
8289 int flags;
8290 FILE * stream = dump_begin (TDI_tu, &flags);
8291 if (stream && tmp)
8293 dump_node (tmp, flags & ~TDF_SLIM, stream);
8294 dump_end (TDI_tu, stream);
8298 /* Process all file scopes in this compilation, and the external_scope,
8299 through wrapup_global_declarations and check_global_declarations. */
8300 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8301 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
8302 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8304 /* We're done parsing; proceed to optimize and emit assembly.
8305 FIXME: shouldn't be the front end's responsibility to call this. */
8306 cgraph_optimize ();
8308 /* After cgraph has had a chance to emit everything that's going to
8309 be emitted, output debug information for globals. */
8310 if (errorcount == 0 && sorrycount == 0)
8312 timevar_push (TV_SYMOUT);
8313 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8314 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8315 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8316 timevar_pop (TV_SYMOUT);
8319 ext_block = NULL;
8322 #include "gt-c-decl.h"