2011-08-31 Tom de Vries <tom@codesourcery.com>
[official-gcc.git] / gcc / c-decl.c
blobd683d4e36550182f87a1408ba9d01629ef89d7d1
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, 2010, 2011
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 "flags.h"
38 #include "function.h"
39 #include "output.h"
40 #include "c-tree.h"
41 #include "toplev.h"
42 #include "tm_p.h"
43 #include "cpplib.h"
44 #include "target.h"
45 #include "debug.h"
46 #include "opts.h"
47 #include "timevar.h"
48 #include "c-family/c-common.h"
49 #include "c-family/c-objc.h"
50 #include "c-family/c-pragma.h"
51 #include "c-lang.h"
52 #include "langhooks.h"
53 #include "tree-mudflap.h"
54 #include "tree-iterator.h"
55 #include "diagnostic-core.h"
56 #include "tree-dump.h"
57 #include "cgraph.h"
58 #include "hashtab.h"
59 #include "langhooks-def.h"
60 #include "pointer-set.h"
61 #include "plugin.h"
62 #include "c-family/c-ada-spec.h"
64 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
65 enum decl_context
66 { NORMAL, /* Ordinary declaration */
67 FUNCDEF, /* Function definition */
68 PARM, /* Declaration of parm before function body */
69 FIELD, /* Declaration inside struct or union */
70 TYPENAME}; /* Typename (inside cast or sizeof) */
72 /* States indicating how grokdeclarator() should handle declspecs marked
73 with __attribute__((deprecated)). An object declared as
74 __attribute__((deprecated)) suppresses warnings of uses of other
75 deprecated items. */
77 enum deprecated_states {
78 DEPRECATED_NORMAL,
79 DEPRECATED_SUPPRESS
83 /* Nonzero if we have seen an invalid cross reference
84 to a struct, union, or enum, but not yet printed the message. */
85 tree pending_invalid_xref;
87 /* File and line to appear in the eventual error message. */
88 location_t pending_invalid_xref_location;
90 /* The file and line that the prototype came from if this is an
91 old-style definition; used for diagnostics in
92 store_parm_decls_oldstyle. */
94 static location_t current_function_prototype_locus;
96 /* Whether this prototype was built-in. */
98 static bool current_function_prototype_built_in;
100 /* The argument type information of this prototype. */
102 static tree current_function_prototype_arg_types;
104 /* The argument information structure for the function currently being
105 defined. */
107 static struct c_arg_info *current_function_arg_info;
109 /* The obstack on which parser and related data structures, which are
110 not live beyond their top-level declaration or definition, are
111 allocated. */
112 struct obstack parser_obstack;
114 /* The current statement tree. */
116 static GTY(()) struct stmt_tree_s c_stmt_tree;
118 /* State saving variables. */
119 tree c_break_label;
120 tree c_cont_label;
122 /* A list of decls to be made automatically visible in each file scope. */
123 static GTY(()) tree visible_builtins;
125 /* Set to 0 at beginning of a function definition, set to 1 if
126 a return statement that specifies a return value is seen. */
128 int current_function_returns_value;
130 /* Set to 0 at beginning of a function definition, set to 1 if
131 a return statement with no argument is seen. */
133 int current_function_returns_null;
135 /* Set to 0 at beginning of a function definition, set to 1 if
136 a call to a noreturn function is seen. */
138 int current_function_returns_abnormally;
140 /* Set to nonzero by `grokdeclarator' for a function
141 whose return type is defaulted, if warnings for this are desired. */
143 static int warn_about_return_type;
145 /* Nonzero when the current toplevel function contains a declaration
146 of a nested function which is never defined. */
148 static bool undef_nested_function;
151 /* Each c_binding structure describes one binding of an identifier to
152 a decl. All the decls in a scope - irrespective of namespace - are
153 chained together by the ->prev field, which (as the name implies)
154 runs in reverse order. All the decls in a given namespace bound to
155 a given identifier are chained by the ->shadowed field, which runs
156 from inner to outer scopes.
158 The ->decl field usually points to a DECL node, but there are two
159 exceptions. In the namespace of type tags, the bound entity is a
160 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
161 identifier is encountered, it is bound to error_mark_node to
162 suppress further errors about that identifier in the current
163 function.
165 The ->u.type field stores the type of the declaration in this scope;
166 if NULL, the type is the type of the ->decl field. This is only of
167 relevance for objects with external or internal linkage which may
168 be redeclared in inner scopes, forming composite types that only
169 persist for the duration of those scopes. In the external scope,
170 this stores the composite of all the types declared for this
171 object, visible or not. The ->inner_comp field (used only at file
172 scope) stores whether an incomplete array type at file scope was
173 completed at an inner scope to an array size other than 1.
175 The ->u.label field is used for labels. It points to a structure
176 which stores additional information used for warnings.
178 The depth field is copied from the scope structure that holds this
179 decl. It is used to preserve the proper ordering of the ->shadowed
180 field (see bind()) and also for a handful of special-case checks.
181 Finally, the invisible bit is true for a decl which should be
182 ignored for purposes of normal name lookup, and the nested bit is
183 true for a decl that's been bound a second time in an inner scope;
184 in all such cases, the binding in the outer scope will have its
185 invisible bit true. */
187 struct GTY((chain_next ("%h.prev"))) c_binding {
188 union GTY(()) { /* first so GTY desc can use decl */
189 tree GTY((tag ("0"))) type; /* the type in this scope */
190 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
191 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
192 tree decl; /* the decl bound */
193 tree id; /* the identifier it's bound to */
194 struct c_binding *prev; /* the previous decl in this scope */
195 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
196 unsigned int depth : 28; /* depth of this scope */
197 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
198 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
199 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
200 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
201 location_t locus; /* location for nested bindings */
203 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
204 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
205 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
206 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
208 #define I_SYMBOL_BINDING(node) \
209 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
210 #define I_SYMBOL_DECL(node) \
211 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
213 #define I_TAG_BINDING(node) \
214 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
215 #define I_TAG_DECL(node) \
216 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
218 #define I_LABEL_BINDING(node) \
219 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
220 #define I_LABEL_DECL(node) \
221 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
223 /* Each C symbol points to three linked lists of c_binding structures.
224 These describe the values of the identifier in the three different
225 namespaces defined by the language. */
227 struct GTY(()) lang_identifier {
228 struct c_common_identifier common_id;
229 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
230 struct c_binding *tag_binding; /* struct/union/enum tags */
231 struct c_binding *label_binding; /* labels */
234 /* Validate c-lang.c's assumptions. */
235 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
236 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
238 /* The resulting tree type. */
240 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
241 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
243 union tree_node GTY ((tag ("0"),
244 desc ("tree_node_structure (&%h)")))
245 generic;
246 struct lang_identifier GTY ((tag ("1"))) identifier;
249 /* Track bindings and other things that matter for goto warnings. For
250 efficiency, we do not gather all the decls at the point of
251 definition. Instead, we point into the bindings structure. As
252 scopes are popped, we update these structures and gather the decls
253 that matter at that time. */
255 struct GTY(()) c_spot_bindings {
256 /* The currently open scope which holds bindings defined when the
257 label was defined or the goto statement was found. */
258 struct c_scope *scope;
259 /* The bindings in the scope field which were defined at the point
260 of the label or goto. This lets us look at older or newer
261 bindings in the scope, as appropriate. */
262 struct c_binding *bindings_in_scope;
263 /* The number of statement expressions that have started since this
264 label or goto statement was defined. This is zero if we are at
265 the same statement expression level. It is positive if we are in
266 a statement expression started since this spot. It is negative
267 if this spot was in a statement expression and we have left
268 it. */
269 int stmt_exprs;
270 /* Whether we started in a statement expression but are no longer in
271 it. This is set to true if stmt_exprs ever goes negative. */
272 bool left_stmt_expr;
275 /* This structure is used to keep track of bindings seen when a goto
276 statement is defined. This is only used if we see the goto
277 statement before we see the label. */
279 struct GTY(()) c_goto_bindings {
280 /* The location of the goto statement. */
281 location_t loc;
282 /* The bindings of the goto statement. */
283 struct c_spot_bindings goto_bindings;
286 typedef struct c_goto_bindings *c_goto_bindings_p;
287 DEF_VEC_P(c_goto_bindings_p);
288 DEF_VEC_ALLOC_P(c_goto_bindings_p,gc);
290 /* The additional information we keep track of for a label binding.
291 These fields are updated as scopes are popped. */
293 struct GTY(()) c_label_vars {
294 /* The shadowed c_label_vars, when one label shadows another (which
295 can only happen using a __label__ declaration). */
296 struct c_label_vars *shadowed;
297 /* The bindings when the label was defined. */
298 struct c_spot_bindings label_bindings;
299 /* A list of decls that we care about: decls about which we should
300 warn if a goto branches to this label from later in the function.
301 Decls are added to this list as scopes are popped. We only add
302 the decls that matter. */
303 VEC(tree,gc) *decls_in_scope;
304 /* A list of goto statements to this label. This is only used for
305 goto statements seen before the label was defined, so that we can
306 issue appropriate warnings for them. */
307 VEC(c_goto_bindings_p,gc) *gotos;
310 /* Each c_scope structure describes the complete contents of one
311 scope. Four scopes are distinguished specially: the innermost or
312 current scope, the innermost function scope, the file scope (always
313 the second to outermost) and the outermost or external scope.
315 Most declarations are recorded in the current scope.
317 All normal label declarations are recorded in the innermost
318 function scope, as are bindings of undeclared identifiers to
319 error_mark_node. (GCC permits nested functions as an extension,
320 hence the 'innermost' qualifier.) Explicitly declared labels
321 (using the __label__ extension) appear in the current scope.
323 Being in the file scope (current_scope == file_scope) causes
324 special behavior in several places below. Also, under some
325 conditions the Objective-C front end records declarations in the
326 file scope even though that isn't the current scope.
328 All declarations with external linkage are recorded in the external
329 scope, even if they aren't visible there; this models the fact that
330 such declarations are visible to the entire program, and (with a
331 bit of cleverness, see pushdecl) allows diagnosis of some violations
332 of C99 6.2.2p7 and 6.2.7p2:
334 If, within the same translation unit, the same identifier appears
335 with both internal and external linkage, the behavior is
336 undefined.
338 All declarations that refer to the same object or function shall
339 have compatible type; otherwise, the behavior is undefined.
341 Initially only the built-in declarations, which describe compiler
342 intrinsic functions plus a subset of the standard library, are in
343 this scope.
345 The order of the blocks list matters, and it is frequently appended
346 to. To avoid having to walk all the way to the end of the list on
347 each insertion, or reverse the list later, we maintain a pointer to
348 the last list entry. (FIXME: It should be feasible to use a reversed
349 list here.)
351 The bindings list is strictly in reverse order of declarations;
352 pop_scope relies on this. */
355 struct GTY((chain_next ("%h.outer"))) c_scope {
356 /* The scope containing this one. */
357 struct c_scope *outer;
359 /* The next outermost function scope. */
360 struct c_scope *outer_function;
362 /* All bindings in this scope. */
363 struct c_binding *bindings;
365 /* For each scope (except the global one), a chain of BLOCK nodes
366 for all the scopes that were entered and exited one level down. */
367 tree blocks;
368 tree blocks_last;
370 /* The depth of this scope. Used to keep the ->shadowed chain of
371 bindings sorted innermost to outermost. */
372 unsigned int depth : 28;
374 /* True if we are currently filling this scope with parameter
375 declarations. */
376 BOOL_BITFIELD parm_flag : 1;
378 /* True if we saw [*] in this scope. Used to give an error messages
379 if these appears in a function definition. */
380 BOOL_BITFIELD had_vla_unspec : 1;
382 /* True if we already complained about forward parameter decls
383 in this scope. This prevents double warnings on
384 foo (int a; int b; ...) */
385 BOOL_BITFIELD warned_forward_parm_decls : 1;
387 /* True if this is the outermost block scope of a function body.
388 This scope contains the parameters, the local variables declared
389 in the outermost block, and all the labels (except those in
390 nested functions, or declared at block scope with __label__). */
391 BOOL_BITFIELD function_body : 1;
393 /* True means make a BLOCK for this scope no matter what. */
394 BOOL_BITFIELD keep : 1;
396 /* True means that an unsuffixed float constant is _Decimal64. */
397 BOOL_BITFIELD float_const_decimal64 : 1;
399 /* True if this scope has any label bindings. This is used to speed
400 up searching for labels when popping scopes, particularly since
401 labels are normally only found at function scope. */
402 BOOL_BITFIELD has_label_bindings : 1;
404 /* True if we should issue a warning if a goto statement crosses any
405 of the bindings. We still need to check the list of bindings to
406 find the specific ones we need to warn about. This is true if
407 decl_jump_unsafe would return true for any of the bindings. This
408 is used to avoid looping over all the bindings unnecessarily. */
409 BOOL_BITFIELD has_jump_unsafe_decl : 1;
412 /* The scope currently in effect. */
414 static GTY(()) struct c_scope *current_scope;
416 /* The innermost function scope. Ordinary (not explicitly declared)
417 labels, bindings to error_mark_node, and the lazily-created
418 bindings of __func__ and its friends get this scope. */
420 static GTY(()) struct c_scope *current_function_scope;
422 /* The C file scope. This is reset for each input translation unit. */
424 static GTY(()) struct c_scope *file_scope;
426 /* The outermost scope. This is used for all declarations with
427 external linkage, and only these, hence the name. */
429 static GTY(()) struct c_scope *external_scope;
431 /* A chain of c_scope structures awaiting reuse. */
433 static GTY((deletable)) struct c_scope *scope_freelist;
435 /* A chain of c_binding structures awaiting reuse. */
437 static GTY((deletable)) struct c_binding *binding_freelist;
439 /* Append VAR to LIST in scope SCOPE. */
440 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
441 struct c_scope *s_ = (scope); \
442 tree d_ = (decl); \
443 if (s_->list##_last) \
444 BLOCK_CHAIN (s_->list##_last) = d_; \
445 else \
446 s_->list = d_; \
447 s_->list##_last = d_; \
448 } while (0)
450 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
451 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
452 struct c_scope *t_ = (tscope); \
453 struct c_scope *f_ = (fscope); \
454 if (t_->to##_last) \
455 BLOCK_CHAIN (t_->to##_last) = f_->from; \
456 else \
457 t_->to = f_->from; \
458 t_->to##_last = f_->from##_last; \
459 } while (0)
461 /* A c_inline_static structure stores details of a static identifier
462 referenced in a definition of a function that may be an inline
463 definition if no subsequent declaration of that function uses
464 "extern" or does not use "inline". */
466 struct GTY((chain_next ("%h.next"))) c_inline_static {
467 /* The location for a diagnostic. */
468 location_t location;
470 /* The function that may be an inline definition. */
471 tree function;
473 /* The object or function referenced. */
474 tree static_decl;
476 /* What sort of reference this is. */
477 enum c_inline_static_type type;
479 /* The next such structure or NULL. */
480 struct c_inline_static *next;
483 /* List of static identifiers used or referenced in functions that may
484 be inline definitions. */
485 static GTY(()) struct c_inline_static *c_inline_statics;
487 /* True means unconditionally make a BLOCK for the next scope pushed. */
489 static bool keep_next_level_flag;
491 /* True means the next call to push_scope will be the outermost scope
492 of a function body, so do not push a new scope, merely cease
493 expecting parameter decls. */
495 static bool next_is_function_body;
497 /* A VEC of pointers to c_binding structures. */
499 typedef struct c_binding *c_binding_ptr;
500 DEF_VEC_P(c_binding_ptr);
501 DEF_VEC_ALLOC_P(c_binding_ptr,heap);
503 /* Information that we keep for a struct or union while it is being
504 parsed. */
506 struct c_struct_parse_info
508 /* If warn_cxx_compat, a list of types defined within this
509 struct. */
510 VEC(tree,heap) *struct_types;
511 /* If warn_cxx_compat, a list of field names which have bindings,
512 and which are defined in this struct, but which are not defined
513 in any enclosing struct. This is used to clear the in_struct
514 field of the c_bindings structure. */
515 VEC(c_binding_ptr,heap) *fields;
516 /* If warn_cxx_compat, a list of typedef names used when defining
517 fields in this struct. */
518 VEC(tree,heap) *typedefs_seen;
521 /* Information for the struct or union currently being parsed, or
522 NULL if not parsing a struct or union. */
523 static struct c_struct_parse_info *struct_parse_info;
525 /* Forward declarations. */
526 static tree lookup_name_in_scope (tree, struct c_scope *);
527 static tree c_make_fname_decl (location_t, tree, int);
528 static tree grokdeclarator (const struct c_declarator *,
529 struct c_declspecs *,
530 enum decl_context, bool, tree *, tree *, tree *,
531 bool *, enum deprecated_states);
532 static tree grokparms (struct c_arg_info *, bool);
533 static void layout_array_type (tree);
535 /* T is a statement. Add it to the statement-tree. This is the
536 C/ObjC version--C++ has a slightly different version of this
537 function. */
539 tree
540 add_stmt (tree t)
542 enum tree_code code = TREE_CODE (t);
544 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
546 if (!EXPR_HAS_LOCATION (t))
547 SET_EXPR_LOCATION (t, input_location);
550 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
551 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
553 /* Add T to the statement-tree. Non-side-effect statements need to be
554 recorded during statement expressions. */
555 if (!building_stmt_list_p ())
556 push_stmt_list ();
557 append_to_statement_list_force (t, &cur_stmt_list);
559 return t;
562 /* Return true if we will want to say something if a goto statement
563 crosses DECL. */
565 static bool
566 decl_jump_unsafe (tree decl)
568 if (error_operand_p (decl))
569 return false;
571 /* Always warn about crossing variably modified types. */
572 if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == TYPE_DECL)
573 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
574 return true;
576 /* Otherwise, only warn if -Wgoto-misses-init and this is an
577 initialized automatic decl. */
578 if (warn_jump_misses_init
579 && TREE_CODE (decl) == VAR_DECL
580 && !TREE_STATIC (decl)
581 && DECL_INITIAL (decl) != NULL_TREE)
582 return true;
584 return false;
588 void
589 c_print_identifier (FILE *file, tree node, int indent)
591 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
592 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
593 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
594 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
596 tree rid = ridpointers[C_RID_CODE (node)];
597 indent_to (file, indent + 4);
598 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
599 (void *) rid, IDENTIFIER_POINTER (rid));
603 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
604 which may be any of several kinds of DECL or TYPE or error_mark_node,
605 in the scope SCOPE. */
606 static void
607 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
608 bool nested, location_t locus)
610 struct c_binding *b, **here;
612 if (binding_freelist)
614 b = binding_freelist;
615 binding_freelist = b->prev;
617 else
618 b = ggc_alloc_c_binding ();
620 b->shadowed = 0;
621 b->decl = decl;
622 b->id = name;
623 b->depth = scope->depth;
624 b->invisible = invisible;
625 b->nested = nested;
626 b->inner_comp = 0;
627 b->in_struct = 0;
628 b->locus = locus;
630 b->u.type = NULL;
632 b->prev = scope->bindings;
633 scope->bindings = b;
635 if (decl_jump_unsafe (decl))
636 scope->has_jump_unsafe_decl = 1;
638 if (!name)
639 return;
641 switch (TREE_CODE (decl))
643 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
644 case ENUMERAL_TYPE:
645 case UNION_TYPE:
646 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
647 case VAR_DECL:
648 case FUNCTION_DECL:
649 case TYPE_DECL:
650 case CONST_DECL:
651 case PARM_DECL:
652 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
654 default:
655 gcc_unreachable ();
658 /* Locate the appropriate place in the chain of shadowed decls
659 to insert this binding. Normally, scope == current_scope and
660 this does nothing. */
661 while (*here && (*here)->depth > scope->depth)
662 here = &(*here)->shadowed;
664 b->shadowed = *here;
665 *here = b;
668 /* Clear the binding structure B, stick it on the binding_freelist,
669 and return the former value of b->prev. This is used by pop_scope
670 and get_parm_info to iterate destructively over all the bindings
671 from a given scope. */
672 static struct c_binding *
673 free_binding_and_advance (struct c_binding *b)
675 struct c_binding *prev = b->prev;
677 memset (b, 0, sizeof (struct c_binding));
678 b->prev = binding_freelist;
679 binding_freelist = b;
681 return prev;
684 /* Bind a label. Like bind, but skip fields which aren't used for
685 labels, and add the LABEL_VARS value. */
686 static void
687 bind_label (tree name, tree label, struct c_scope *scope,
688 struct c_label_vars *label_vars)
690 struct c_binding *b;
692 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
693 UNKNOWN_LOCATION);
695 scope->has_label_bindings = true;
697 b = scope->bindings;
698 gcc_assert (b->decl == label);
699 label_vars->shadowed = b->u.label;
700 b->u.label = label_vars;
703 /* Hook called at end of compilation to assume 1 elt
704 for a file-scope tentative array defn that wasn't complete before. */
706 void
707 c_finish_incomplete_decl (tree decl)
709 if (TREE_CODE (decl) == VAR_DECL)
711 tree type = TREE_TYPE (decl);
712 if (type != error_mark_node
713 && TREE_CODE (type) == ARRAY_TYPE
714 && !DECL_EXTERNAL (decl)
715 && TYPE_DOMAIN (type) == 0)
717 warning_at (DECL_SOURCE_LOCATION (decl),
718 0, "array %q+D assumed to have one element", decl);
720 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
722 layout_decl (decl, 0);
727 /* Record that inline function FUNC contains a reference (location
728 LOC) to static DECL (file-scope or function-local according to
729 TYPE). */
731 void
732 record_inline_static (location_t loc, tree func, tree decl,
733 enum c_inline_static_type type)
735 struct c_inline_static *csi = ggc_alloc_c_inline_static ();
736 csi->location = loc;
737 csi->function = func;
738 csi->static_decl = decl;
739 csi->type = type;
740 csi->next = c_inline_statics;
741 c_inline_statics = csi;
744 /* Check for references to static declarations in inline functions at
745 the end of the translation unit and diagnose them if the functions
746 are still inline definitions. */
748 static void
749 check_inline_statics (void)
751 struct c_inline_static *csi;
752 for (csi = c_inline_statics; csi; csi = csi->next)
754 if (DECL_EXTERNAL (csi->function))
755 switch (csi->type)
757 case csi_internal:
758 pedwarn (csi->location, 0,
759 "%qD is static but used in inline function %qD "
760 "which is not static", csi->static_decl, csi->function);
761 break;
762 case csi_modifiable:
763 pedwarn (csi->location, 0,
764 "%q+D is static but declared in inline function %qD "
765 "which is not static", csi->static_decl, csi->function);
766 break;
767 default:
768 gcc_unreachable ();
771 c_inline_statics = NULL;
774 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
775 for the current state, otherwise set it to uninitialized. */
777 static void
778 set_spot_bindings (struct c_spot_bindings *p, bool defining)
780 if (defining)
782 p->scope = current_scope;
783 p->bindings_in_scope = current_scope->bindings;
785 else
787 p->scope = NULL;
788 p->bindings_in_scope = NULL;
790 p->stmt_exprs = 0;
791 p->left_stmt_expr = false;
794 /* Update spot bindings P as we pop out of SCOPE. Return true if we
795 should push decls for a label. */
797 static bool
798 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
800 if (p->scope != scope)
802 /* This label or goto is defined in some other scope, or it is a
803 label which is not yet defined. There is nothing to
804 update. */
805 return false;
808 /* Adjust the spot bindings to refer to the bindings already defined
809 in the enclosing scope. */
810 p->scope = scope->outer;
811 p->bindings_in_scope = p->scope->bindings;
813 return true;
816 /* The Objective-C front-end often needs to determine the current scope. */
818 void *
819 objc_get_current_scope (void)
821 return current_scope;
824 /* The following function is used only by Objective-C. It needs to live here
825 because it accesses the innards of c_scope. */
827 void
828 objc_mark_locals_volatile (void *enclosing_blk)
830 struct c_scope *scope;
831 struct c_binding *b;
833 for (scope = current_scope;
834 scope && scope != enclosing_blk;
835 scope = scope->outer)
837 for (b = scope->bindings; b; b = b->prev)
838 objc_volatilize_decl (b->decl);
840 /* Do not climb up past the current function. */
841 if (scope->function_body)
842 break;
846 /* Return true if we are in the global binding level. */
848 bool
849 global_bindings_p (void)
851 return current_scope == file_scope;
854 void
855 keep_next_level (void)
857 keep_next_level_flag = true;
860 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
862 void
863 set_float_const_decimal64 (void)
865 current_scope->float_const_decimal64 = true;
868 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
870 void
871 clear_float_const_decimal64 (void)
873 current_scope->float_const_decimal64 = false;
876 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
878 bool
879 float_const_decimal64_p (void)
881 return current_scope->float_const_decimal64;
884 /* Identify this scope as currently being filled with parameters. */
886 void
887 declare_parm_level (void)
889 current_scope->parm_flag = true;
892 void
893 push_scope (void)
895 if (next_is_function_body)
897 /* This is the transition from the parameters to the top level
898 of the function body. These are the same scope
899 (C99 6.2.1p4,6) so we do not push another scope structure.
900 next_is_function_body is set only by store_parm_decls, which
901 in turn is called when and only when we are about to
902 encounter the opening curly brace for the function body.
904 The outermost block of a function always gets a BLOCK node,
905 because the debugging output routines expect that each
906 function has at least one BLOCK. */
907 current_scope->parm_flag = false;
908 current_scope->function_body = true;
909 current_scope->keep = true;
910 current_scope->outer_function = current_function_scope;
911 current_function_scope = current_scope;
913 keep_next_level_flag = false;
914 next_is_function_body = false;
916 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
917 if (current_scope->outer)
918 current_scope->float_const_decimal64
919 = current_scope->outer->float_const_decimal64;
920 else
921 current_scope->float_const_decimal64 = false;
923 else
925 struct c_scope *scope;
926 if (scope_freelist)
928 scope = scope_freelist;
929 scope_freelist = scope->outer;
931 else
932 scope = ggc_alloc_cleared_c_scope ();
934 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
935 if (current_scope)
936 scope->float_const_decimal64 = current_scope->float_const_decimal64;
937 else
938 scope->float_const_decimal64 = false;
940 scope->keep = keep_next_level_flag;
941 scope->outer = current_scope;
942 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
944 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
945 possible. */
946 if (current_scope && scope->depth == 0)
948 scope->depth--;
949 sorry ("GCC supports only %u nested scopes", scope->depth);
952 current_scope = scope;
953 keep_next_level_flag = false;
957 /* This is called when we are leaving SCOPE. For each label defined
958 in SCOPE, add any appropriate decls to its decls_in_scope fields.
959 These are the decls whose initialization will be skipped by a goto
960 later in the function. */
962 static void
963 update_label_decls (struct c_scope *scope)
965 struct c_scope *s;
967 s = scope;
968 while (s != NULL)
970 if (s->has_label_bindings)
972 struct c_binding *b;
974 for (b = s->bindings; b != NULL; b = b->prev)
976 struct c_label_vars *label_vars;
977 struct c_binding *b1;
978 bool hjud;
979 unsigned int ix;
980 struct c_goto_bindings *g;
982 if (TREE_CODE (b->decl) != LABEL_DECL)
983 continue;
984 label_vars = b->u.label;
986 b1 = label_vars->label_bindings.bindings_in_scope;
987 if (label_vars->label_bindings.scope == NULL)
988 hjud = false;
989 else
990 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
991 if (update_spot_bindings (scope, &label_vars->label_bindings))
993 /* This label is defined in this scope. */
994 if (hjud)
996 for (; b1 != NULL; b1 = b1->prev)
998 /* A goto from later in the function to this
999 label will never see the initialization
1000 of B1, if any. Save it to issue a
1001 warning if needed. */
1002 if (decl_jump_unsafe (b1->decl))
1003 VEC_safe_push (tree, gc,
1004 label_vars->decls_in_scope,
1005 b1->decl);
1010 /* Update the bindings of any goto statements associated
1011 with this label. */
1012 FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
1013 update_spot_bindings (scope, &g->goto_bindings);
1017 /* Don't search beyond the current function. */
1018 if (s == current_function_scope)
1019 break;
1021 s = s->outer;
1025 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1027 static void
1028 set_type_context (tree type, tree context)
1030 for (type = TYPE_MAIN_VARIANT (type); type;
1031 type = TYPE_NEXT_VARIANT (type))
1032 TYPE_CONTEXT (type) = context;
1035 /* Exit a scope. Restore the state of the identifier-decl mappings
1036 that were in effect when this scope was entered. Return a BLOCK
1037 node containing all the DECLs in this scope that are of interest
1038 to debug info generation. */
1040 tree
1041 pop_scope (void)
1043 struct c_scope *scope = current_scope;
1044 tree block, context, p;
1045 struct c_binding *b;
1047 bool functionbody = scope->function_body;
1048 bool keep = functionbody || scope->keep || scope->bindings;
1050 update_label_decls (scope);
1052 /* If appropriate, create a BLOCK to record the decls for the life
1053 of this function. */
1054 block = 0;
1055 if (keep)
1057 block = make_node (BLOCK);
1058 BLOCK_SUBBLOCKS (block) = scope->blocks;
1059 TREE_USED (block) = 1;
1061 /* In each subblock, record that this is its superior. */
1062 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1063 BLOCK_SUPERCONTEXT (p) = block;
1065 BLOCK_VARS (block) = 0;
1068 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1069 scope must be set so that they point to the appropriate
1070 construct, i.e. either to the current FUNCTION_DECL node, or
1071 else to the BLOCK node we just constructed.
1073 Note that for tagged types whose scope is just the formal
1074 parameter list for some function type specification, we can't
1075 properly set their TYPE_CONTEXTs here, because we don't have a
1076 pointer to the appropriate FUNCTION_TYPE node readily available
1077 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1078 type nodes get set in `grokdeclarator' as soon as we have created
1079 the FUNCTION_TYPE node which will represent the "scope" for these
1080 "parameter list local" tagged types. */
1081 if (scope->function_body)
1082 context = current_function_decl;
1083 else if (scope == file_scope)
1085 tree file_decl = build_translation_unit_decl (NULL_TREE);
1086 context = file_decl;
1088 else
1089 context = block;
1091 /* Clear all bindings in this scope. */
1092 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1094 p = b->decl;
1095 switch (TREE_CODE (p))
1097 case LABEL_DECL:
1098 /* Warnings for unused labels, errors for undefined labels. */
1099 if (TREE_USED (p) && !DECL_INITIAL (p))
1101 error ("label %q+D used but not defined", p);
1102 DECL_INITIAL (p) = error_mark_node;
1104 else
1105 warn_for_unused_label (p);
1107 /* Labels go in BLOCK_VARS. */
1108 DECL_CHAIN (p) = BLOCK_VARS (block);
1109 BLOCK_VARS (block) = p;
1110 gcc_assert (I_LABEL_BINDING (b->id) == b);
1111 I_LABEL_BINDING (b->id) = b->shadowed;
1113 /* Also pop back to the shadowed label_vars. */
1114 release_tree_vector (b->u.label->decls_in_scope);
1115 b->u.label = b->u.label->shadowed;
1116 break;
1118 case ENUMERAL_TYPE:
1119 case UNION_TYPE:
1120 case RECORD_TYPE:
1121 set_type_context (p, context);
1123 /* Types may not have tag-names, in which case the type
1124 appears in the bindings list with b->id NULL. */
1125 if (b->id)
1127 gcc_assert (I_TAG_BINDING (b->id) == b);
1128 I_TAG_BINDING (b->id) = b->shadowed;
1130 break;
1132 case FUNCTION_DECL:
1133 /* Propagate TREE_ADDRESSABLE from nested functions to their
1134 containing functions. */
1135 if (!TREE_ASM_WRITTEN (p)
1136 && DECL_INITIAL (p) != 0
1137 && TREE_ADDRESSABLE (p)
1138 && DECL_ABSTRACT_ORIGIN (p) != 0
1139 && DECL_ABSTRACT_ORIGIN (p) != p)
1140 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1141 if (!DECL_EXTERNAL (p)
1142 && !DECL_INITIAL (p)
1143 && scope != file_scope
1144 && scope != external_scope)
1146 error ("nested function %q+D declared but never defined", p);
1147 undef_nested_function = true;
1149 else if (DECL_DECLARED_INLINE_P (p)
1150 && TREE_PUBLIC (p)
1151 && !DECL_INITIAL (p))
1153 /* C99 6.7.4p6: "a function with external linkage... declared
1154 with an inline function specifier ... shall also be defined
1155 in the same translation unit." */
1156 if (!flag_gnu89_inline)
1157 pedwarn (input_location, 0,
1158 "inline function %q+D declared but never defined", p);
1159 DECL_EXTERNAL (p) = 1;
1162 goto common_symbol;
1164 case VAR_DECL:
1165 /* Warnings for unused variables. */
1166 if ((!TREE_USED (p) || !DECL_READ_P (p))
1167 && !TREE_NO_WARNING (p)
1168 && !DECL_IN_SYSTEM_HEADER (p)
1169 && DECL_NAME (p)
1170 && !DECL_ARTIFICIAL (p)
1171 && scope != file_scope
1172 && scope != external_scope)
1174 if (!TREE_USED (p))
1175 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1176 else if (DECL_CONTEXT (p) == current_function_decl)
1177 warning_at (DECL_SOURCE_LOCATION (p),
1178 OPT_Wunused_but_set_variable,
1179 "variable %qD set but not used", p);
1182 if (b->inner_comp)
1184 error ("type of array %q+D completed incompatibly with"
1185 " implicit initialization", p);
1188 /* Fall through. */
1189 case TYPE_DECL:
1190 case CONST_DECL:
1191 common_symbol:
1192 /* All of these go in BLOCK_VARS, but only if this is the
1193 binding in the home scope. */
1194 if (!b->nested)
1196 DECL_CHAIN (p) = BLOCK_VARS (block);
1197 BLOCK_VARS (block) = p;
1199 else if (VAR_OR_FUNCTION_DECL_P (p))
1201 /* For block local externs add a special
1202 DECL_EXTERNAL decl for debug info generation. */
1203 tree extp = copy_node (p);
1205 DECL_EXTERNAL (extp) = 1;
1206 TREE_STATIC (extp) = 0;
1207 TREE_PUBLIC (extp) = 1;
1208 DECL_INITIAL (extp) = NULL_TREE;
1209 DECL_LANG_SPECIFIC (extp) = NULL;
1210 DECL_CONTEXT (extp) = current_function_decl;
1211 if (TREE_CODE (p) == FUNCTION_DECL)
1213 DECL_RESULT (extp) = NULL_TREE;
1214 DECL_SAVED_TREE (extp) = NULL_TREE;
1215 DECL_STRUCT_FUNCTION (extp) = NULL;
1217 if (b->locus != UNKNOWN_LOCATION)
1218 DECL_SOURCE_LOCATION (extp) = b->locus;
1219 DECL_CHAIN (extp) = BLOCK_VARS (block);
1220 BLOCK_VARS (block) = extp;
1222 /* If this is the file scope set DECL_CONTEXT of each decl to
1223 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1224 work. */
1225 if (scope == file_scope)
1227 DECL_CONTEXT (p) = context;
1228 if (TREE_CODE (p) == TYPE_DECL
1229 && TREE_TYPE (p) != error_mark_node)
1230 set_type_context (TREE_TYPE (p), context);
1233 /* Fall through. */
1234 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1235 already been put there by store_parm_decls. Unused-
1236 parameter warnings are handled by function.c.
1237 error_mark_node obviously does not go in BLOCK_VARS and
1238 does not get unused-variable warnings. */
1239 case PARM_DECL:
1240 case ERROR_MARK:
1241 /* It is possible for a decl not to have a name. We get
1242 here with b->id NULL in this case. */
1243 if (b->id)
1245 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1246 I_SYMBOL_BINDING (b->id) = b->shadowed;
1247 if (b->shadowed && b->shadowed->u.type)
1248 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1250 break;
1252 default:
1253 gcc_unreachable ();
1258 /* Dispose of the block that we just made inside some higher level. */
1259 if ((scope->function_body || scope == file_scope) && context)
1261 DECL_INITIAL (context) = block;
1262 BLOCK_SUPERCONTEXT (block) = context;
1264 else if (scope->outer)
1266 if (block)
1267 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1268 /* If we did not make a block for the scope just exited, any
1269 blocks made for inner scopes must be carried forward so they
1270 will later become subblocks of something else. */
1271 else if (scope->blocks)
1272 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1275 /* Pop the current scope, and free the structure for reuse. */
1276 current_scope = scope->outer;
1277 if (scope->function_body)
1278 current_function_scope = scope->outer_function;
1280 memset (scope, 0, sizeof (struct c_scope));
1281 scope->outer = scope_freelist;
1282 scope_freelist = scope;
1284 return block;
1287 void
1288 push_file_scope (void)
1290 tree decl;
1292 if (file_scope)
1293 return;
1295 push_scope ();
1296 file_scope = current_scope;
1298 start_fname_decls ();
1300 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1301 bind (DECL_NAME (decl), decl, file_scope,
1302 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1305 void
1306 pop_file_scope (void)
1308 /* In case there were missing closebraces, get us back to the global
1309 binding level. */
1310 while (current_scope != file_scope)
1311 pop_scope ();
1313 /* __FUNCTION__ is defined at file scope (""). This
1314 call may not be necessary as my tests indicate it
1315 still works without it. */
1316 finish_fname_decls ();
1318 check_inline_statics ();
1320 /* This is the point to write out a PCH if we're doing that.
1321 In that case we do not want to do anything else. */
1322 if (pch_file)
1324 c_common_write_pch ();
1325 return;
1328 /* Pop off the file scope and close this translation unit. */
1329 pop_scope ();
1330 file_scope = 0;
1332 maybe_apply_pending_pragma_weaks ();
1335 /* Adjust the bindings for the start of a statement expression. */
1337 void
1338 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1340 struct c_scope *scope;
1342 for (scope = current_scope; scope != NULL; scope = scope->outer)
1344 struct c_binding *b;
1346 if (!scope->has_label_bindings)
1347 continue;
1349 for (b = scope->bindings; b != NULL; b = b->prev)
1351 struct c_label_vars *label_vars;
1352 unsigned int ix;
1353 struct c_goto_bindings *g;
1355 if (TREE_CODE (b->decl) != LABEL_DECL)
1356 continue;
1357 label_vars = b->u.label;
1358 ++label_vars->label_bindings.stmt_exprs;
1359 FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
1360 ++g->goto_bindings.stmt_exprs;
1364 if (switch_bindings != NULL)
1365 ++switch_bindings->stmt_exprs;
1368 /* Adjust the bindings for the end of a statement expression. */
1370 void
1371 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1373 struct c_scope *scope;
1375 for (scope = current_scope; scope != NULL; scope = scope->outer)
1377 struct c_binding *b;
1379 if (!scope->has_label_bindings)
1380 continue;
1382 for (b = scope->bindings; b != NULL; b = b->prev)
1384 struct c_label_vars *label_vars;
1385 unsigned int ix;
1386 struct c_goto_bindings *g;
1388 if (TREE_CODE (b->decl) != LABEL_DECL)
1389 continue;
1390 label_vars = b->u.label;
1391 --label_vars->label_bindings.stmt_exprs;
1392 if (label_vars->label_bindings.stmt_exprs < 0)
1394 label_vars->label_bindings.left_stmt_expr = true;
1395 label_vars->label_bindings.stmt_exprs = 0;
1397 FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
1399 --g->goto_bindings.stmt_exprs;
1400 if (g->goto_bindings.stmt_exprs < 0)
1402 g->goto_bindings.left_stmt_expr = true;
1403 g->goto_bindings.stmt_exprs = 0;
1409 if (switch_bindings != NULL)
1411 --switch_bindings->stmt_exprs;
1412 gcc_assert (switch_bindings->stmt_exprs >= 0);
1416 /* Push a definition or a declaration of struct, union or enum tag "name".
1417 "type" should be the type node.
1418 We assume that the tag "name" is not already defined, and has a location
1419 of LOC.
1421 Note that the definition may really be just a forward reference.
1422 In that case, the TYPE_SIZE will be zero. */
1424 static void
1425 pushtag (location_t loc, tree name, tree type)
1427 /* Record the identifier as the type's name if it has none. */
1428 if (name && !TYPE_NAME (type))
1429 TYPE_NAME (type) = name;
1430 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1432 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1433 tagged type we just added to the current scope. This fake
1434 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1435 to output a representation of a tagged type, and it also gives
1436 us a convenient place to record the "scope start" address for the
1437 tagged type. */
1439 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1440 TYPE_DECL, NULL_TREE, type));
1442 /* An approximation for now, so we can tell this is a function-scope tag.
1443 This will be updated in pop_scope. */
1444 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1446 if (warn_cxx_compat && name != NULL_TREE)
1448 struct c_binding *b = I_SYMBOL_BINDING (name);
1450 if (b != NULL
1451 && b->decl != NULL_TREE
1452 && TREE_CODE (b->decl) == TYPE_DECL
1453 && (B_IN_CURRENT_SCOPE (b)
1454 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1455 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1456 != TYPE_MAIN_VARIANT (type)))
1458 warning_at (loc, OPT_Wc___compat,
1459 ("using %qD as both a typedef and a tag is "
1460 "invalid in C++"),
1461 b->decl);
1462 if (b->locus != UNKNOWN_LOCATION)
1463 inform (b->locus, "originally defined here");
1468 /* Subroutine of compare_decls. Allow harmless mismatches in return
1469 and argument types provided that the type modes match. This function
1470 return a unified type given a suitable match, and 0 otherwise. */
1472 static tree
1473 match_builtin_function_types (tree newtype, tree oldtype)
1475 tree newrettype, oldrettype;
1476 tree newargs, oldargs;
1477 tree trytype, tryargs;
1479 /* Accept the return type of the new declaration if same modes. */
1480 oldrettype = TREE_TYPE (oldtype);
1481 newrettype = TREE_TYPE (newtype);
1483 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1484 return 0;
1486 oldargs = TYPE_ARG_TYPES (oldtype);
1487 newargs = TYPE_ARG_TYPES (newtype);
1488 tryargs = newargs;
1490 while (oldargs || newargs)
1492 if (!oldargs
1493 || !newargs
1494 || !TREE_VALUE (oldargs)
1495 || !TREE_VALUE (newargs)
1496 || TYPE_MODE (TREE_VALUE (oldargs))
1497 != TYPE_MODE (TREE_VALUE (newargs)))
1498 return 0;
1500 oldargs = TREE_CHAIN (oldargs);
1501 newargs = TREE_CHAIN (newargs);
1504 trytype = build_function_type (newrettype, tryargs);
1505 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1508 /* Subroutine of diagnose_mismatched_decls. Check for function type
1509 mismatch involving an empty arglist vs a nonempty one and give clearer
1510 diagnostics. */
1511 static void
1512 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1513 tree newtype, tree oldtype)
1515 tree t;
1517 if (TREE_CODE (olddecl) != FUNCTION_DECL
1518 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1519 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == 0)
1520 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == 0)))
1521 return;
1523 t = TYPE_ARG_TYPES (oldtype);
1524 if (t == 0)
1525 t = TYPE_ARG_TYPES (newtype);
1526 for (; t; t = TREE_CHAIN (t))
1528 tree type = TREE_VALUE (t);
1530 if (TREE_CHAIN (t) == 0
1531 && TYPE_MAIN_VARIANT (type) != void_type_node)
1533 inform (input_location, "a parameter list with an ellipsis can%'t match "
1534 "an empty parameter name list declaration");
1535 break;
1538 if (c_type_promotes_to (type) != type)
1540 inform (input_location, "an argument type that has a default promotion can%'t match "
1541 "an empty parameter name list declaration");
1542 break;
1547 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1548 old-style function definition, NEWDECL is a prototype declaration.
1549 Diagnose inconsistencies in the argument list. Returns TRUE if
1550 the prototype is compatible, FALSE if not. */
1551 static bool
1552 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1554 tree newargs, oldargs;
1555 int i;
1557 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1559 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1560 newargs = TYPE_ARG_TYPES (newtype);
1561 i = 1;
1563 for (;;)
1565 tree oldargtype = TREE_VALUE (oldargs);
1566 tree newargtype = TREE_VALUE (newargs);
1568 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1569 return false;
1571 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1572 newargtype = TYPE_MAIN_VARIANT (newargtype);
1574 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1575 break;
1577 /* Reaching the end of just one list means the two decls don't
1578 agree on the number of arguments. */
1579 if (END_OF_ARGLIST (oldargtype))
1581 error ("prototype for %q+D declares more arguments "
1582 "than previous old-style definition", newdecl);
1583 return false;
1585 else if (END_OF_ARGLIST (newargtype))
1587 error ("prototype for %q+D declares fewer arguments "
1588 "than previous old-style definition", newdecl);
1589 return false;
1592 /* Type for passing arg must be consistent with that declared
1593 for the arg. */
1594 else if (!comptypes (oldargtype, newargtype))
1596 error ("prototype for %q+D declares argument %d"
1597 " with incompatible type",
1598 newdecl, i);
1599 return false;
1602 oldargs = TREE_CHAIN (oldargs);
1603 newargs = TREE_CHAIN (newargs);
1604 i++;
1607 /* If we get here, no errors were found, but do issue a warning
1608 for this poor-style construct. */
1609 warning (0, "prototype for %q+D follows non-prototype definition",
1610 newdecl);
1611 return true;
1612 #undef END_OF_ARGLIST
1615 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1616 first in a pair of mismatched declarations, using the diagnostic
1617 function DIAG. */
1618 static void
1619 locate_old_decl (tree decl)
1621 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1623 else if (DECL_INITIAL (decl))
1624 inform (input_location, "previous definition of %q+D was here", decl);
1625 else if (C_DECL_IMPLICIT (decl))
1626 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1627 else
1628 inform (input_location, "previous declaration of %q+D was here", decl);
1631 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1632 Returns true if the caller should proceed to merge the two, false
1633 if OLDDECL should simply be discarded. As a side effect, issues
1634 all necessary diagnostics for invalid or poor-style combinations.
1635 If it returns true, writes the types of NEWDECL and OLDDECL to
1636 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1637 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1639 static bool
1640 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1641 tree *newtypep, tree *oldtypep)
1643 tree newtype, oldtype;
1644 bool pedwarned = false;
1645 bool warned = false;
1646 bool retval = true;
1648 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1649 && DECL_EXTERNAL (DECL))
1651 /* If we have error_mark_node for either decl or type, just discard
1652 the previous decl - we're in an error cascade already. */
1653 if (olddecl == error_mark_node || newdecl == error_mark_node)
1654 return false;
1655 *oldtypep = oldtype = TREE_TYPE (olddecl);
1656 *newtypep = newtype = TREE_TYPE (newdecl);
1657 if (oldtype == error_mark_node || newtype == error_mark_node)
1658 return false;
1660 /* Two different categories of symbol altogether. This is an error
1661 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1662 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1664 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1665 && DECL_BUILT_IN (olddecl)
1666 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1668 error ("%q+D redeclared as different kind of symbol", newdecl);
1669 locate_old_decl (olddecl);
1671 else if (TREE_PUBLIC (newdecl))
1672 warning (0, "built-in function %q+D declared as non-function",
1673 newdecl);
1674 else
1675 warning (OPT_Wshadow, "declaration of %q+D shadows "
1676 "a built-in function", newdecl);
1677 return false;
1680 /* Enumerators have no linkage, so may only be declared once in a
1681 given scope. */
1682 if (TREE_CODE (olddecl) == CONST_DECL)
1684 error ("redeclaration of enumerator %q+D", newdecl);
1685 locate_old_decl (olddecl);
1686 return false;
1689 if (!comptypes (oldtype, newtype))
1691 if (TREE_CODE (olddecl) == FUNCTION_DECL
1692 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1694 /* Accept harmless mismatch in function types.
1695 This is for the ffs and fprintf builtins. */
1696 tree trytype = match_builtin_function_types (newtype, oldtype);
1698 if (trytype && comptypes (newtype, trytype))
1699 *oldtypep = oldtype = trytype;
1700 else
1702 /* If types don't match for a built-in, throw away the
1703 built-in. No point in calling locate_old_decl here, it
1704 won't print anything. */
1705 warning (0, "conflicting types for built-in function %q+D",
1706 newdecl);
1707 return false;
1710 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1711 && DECL_IS_BUILTIN (olddecl))
1713 /* A conflicting function declaration for a predeclared
1714 function that isn't actually built in. Objective C uses
1715 these. The new declaration silently overrides everything
1716 but the volatility (i.e. noreturn) indication. See also
1717 below. FIXME: Make Objective C use normal builtins. */
1718 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1719 return false;
1721 /* Permit void foo (...) to match int foo (...) if the latter is
1722 the definition and implicit int was used. See
1723 c-torture/compile/920625-2.c. */
1724 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1725 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1726 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1727 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1729 pedwarned = pedwarn (input_location, 0,
1730 "conflicting types for %q+D", newdecl);
1731 /* Make sure we keep void as the return type. */
1732 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1733 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1735 /* Permit void foo (...) to match an earlier call to foo (...) with
1736 no declared type (thus, implicitly int). */
1737 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1738 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1739 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1740 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1742 pedwarned = pedwarn (input_location, 0,
1743 "conflicting types for %q+D", newdecl);
1744 /* Make sure we keep void as the return type. */
1745 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1747 else
1749 int new_quals = TYPE_QUALS (newtype);
1750 int old_quals = TYPE_QUALS (oldtype);
1752 if (new_quals != old_quals)
1754 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1755 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1756 if (new_addr != old_addr)
1758 if (ADDR_SPACE_GENERIC_P (new_addr))
1759 error ("conflicting named address spaces (generic vs %s) "
1760 "for %q+D",
1761 c_addr_space_name (old_addr), newdecl);
1762 else if (ADDR_SPACE_GENERIC_P (old_addr))
1763 error ("conflicting named address spaces (%s vs generic) "
1764 "for %q+D",
1765 c_addr_space_name (new_addr), newdecl);
1766 else
1767 error ("conflicting named address spaces (%s vs %s) "
1768 "for %q+D",
1769 c_addr_space_name (new_addr),
1770 c_addr_space_name (old_addr),
1771 newdecl);
1774 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1775 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1776 error ("conflicting type qualifiers for %q+D", newdecl);
1778 else
1779 error ("conflicting types for %q+D", newdecl);
1780 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1781 locate_old_decl (olddecl);
1782 return false;
1786 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1787 but silently ignore the redeclaration if either is in a system
1788 header. (Conflicting redeclarations were handled above.) This
1789 is allowed for C1X if the types are the same, not just
1790 compatible. */
1791 if (TREE_CODE (newdecl) == TYPE_DECL)
1793 bool types_different = false;
1794 int comptypes_result;
1796 comptypes_result
1797 = comptypes_check_different_types (oldtype, newtype, &types_different);
1799 if (comptypes_result != 1 || types_different)
1801 error ("redefinition of typedef %q+D with different type", newdecl);
1802 locate_old_decl (olddecl);
1803 return false;
1806 if (DECL_IN_SYSTEM_HEADER (newdecl)
1807 || DECL_IN_SYSTEM_HEADER (olddecl)
1808 || TREE_NO_WARNING (newdecl)
1809 || TREE_NO_WARNING (olddecl))
1810 return true; /* Allow OLDDECL to continue in use. */
1812 if (variably_modified_type_p (newtype, NULL))
1814 error ("redefinition of typedef %q+D with variably modified type",
1815 newdecl);
1816 locate_old_decl (olddecl);
1818 else if (pedantic && !flag_isoc1x)
1820 pedwarn (input_location, OPT_pedantic,
1821 "redefinition of typedef %q+D", newdecl);
1822 locate_old_decl (olddecl);
1825 return true;
1828 /* Function declarations can either be 'static' or 'extern' (no
1829 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1830 can never conflict with each other on account of linkage
1831 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1832 gnu89 mode permits two definitions if one is 'extern inline' and
1833 one is not. The non- extern-inline definition supersedes the
1834 extern-inline definition. */
1836 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1838 /* If you declare a built-in function name as static, or
1839 define the built-in with an old-style definition (so we
1840 can't validate the argument list) the built-in definition is
1841 overridden, but optionally warn this was a bad choice of name. */
1842 if (DECL_BUILT_IN (olddecl)
1843 && !C_DECL_DECLARED_BUILTIN (olddecl)
1844 && (!TREE_PUBLIC (newdecl)
1845 || (DECL_INITIAL (newdecl)
1846 && !prototype_p (TREE_TYPE (newdecl)))))
1848 warning (OPT_Wshadow, "declaration of %q+D shadows "
1849 "a built-in function", newdecl);
1850 /* Discard the old built-in function. */
1851 return false;
1854 if (DECL_INITIAL (newdecl))
1856 if (DECL_INITIAL (olddecl))
1858 /* If both decls are in the same TU and the new declaration
1859 isn't overriding an extern inline reject the new decl.
1860 In c99, no overriding is allowed in the same translation
1861 unit. */
1862 if ((!DECL_EXTERN_INLINE (olddecl)
1863 || DECL_EXTERN_INLINE (newdecl)
1864 || (!flag_gnu89_inline
1865 && (!DECL_DECLARED_INLINE_P (olddecl)
1866 || !lookup_attribute ("gnu_inline",
1867 DECL_ATTRIBUTES (olddecl)))
1868 && (!DECL_DECLARED_INLINE_P (newdecl)
1869 || !lookup_attribute ("gnu_inline",
1870 DECL_ATTRIBUTES (newdecl))))
1872 && same_translation_unit_p (newdecl, olddecl))
1874 error ("redefinition of %q+D", newdecl);
1875 locate_old_decl (olddecl);
1876 return false;
1880 /* If we have a prototype after an old-style function definition,
1881 the argument types must be checked specially. */
1882 else if (DECL_INITIAL (olddecl)
1883 && !prototype_p (oldtype) && prototype_p (newtype)
1884 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1885 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1887 locate_old_decl (olddecl);
1888 return false;
1890 /* A non-static declaration (even an "extern") followed by a
1891 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1892 The same is true for a static forward declaration at block
1893 scope followed by a non-static declaration/definition at file
1894 scope. Static followed by non-static at the same scope is
1895 not undefined behavior, and is the most convenient way to get
1896 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1897 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1898 we do diagnose it if -Wtraditional. */
1899 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1901 /* Two exceptions to the rule. If olddecl is an extern
1902 inline, or a predeclared function that isn't actually
1903 built in, newdecl silently overrides olddecl. The latter
1904 occur only in Objective C; see also above. (FIXME: Make
1905 Objective C use normal builtins.) */
1906 if (!DECL_IS_BUILTIN (olddecl)
1907 && !DECL_EXTERN_INLINE (olddecl))
1909 error ("static declaration of %q+D follows "
1910 "non-static declaration", newdecl);
1911 locate_old_decl (olddecl);
1913 return false;
1915 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1917 if (DECL_CONTEXT (olddecl))
1919 error ("non-static declaration of %q+D follows "
1920 "static declaration", newdecl);
1921 locate_old_decl (olddecl);
1922 return false;
1924 else if (warn_traditional)
1926 warned |= warning (OPT_Wtraditional,
1927 "non-static declaration of %q+D "
1928 "follows static declaration", newdecl);
1932 /* Make sure gnu_inline attribute is either not present, or
1933 present on all inline decls. */
1934 if (DECL_DECLARED_INLINE_P (olddecl)
1935 && DECL_DECLARED_INLINE_P (newdecl))
1937 bool newa = lookup_attribute ("gnu_inline",
1938 DECL_ATTRIBUTES (newdecl)) != NULL;
1939 bool olda = lookup_attribute ("gnu_inline",
1940 DECL_ATTRIBUTES (olddecl)) != NULL;
1941 if (newa != olda)
1943 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
1944 newa ? newdecl : olddecl);
1945 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
1946 "but not here");
1950 else if (TREE_CODE (newdecl) == VAR_DECL)
1952 /* Only variables can be thread-local, and all declarations must
1953 agree on this property. */
1954 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1956 /* Nothing to check. Since OLDDECL is marked threadprivate
1957 and NEWDECL does not have a thread-local attribute, we
1958 will merge the threadprivate attribute into NEWDECL. */
1961 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1963 if (DECL_THREAD_LOCAL_P (newdecl))
1964 error ("thread-local declaration of %q+D follows "
1965 "non-thread-local declaration", newdecl);
1966 else
1967 error ("non-thread-local declaration of %q+D follows "
1968 "thread-local declaration", newdecl);
1970 locate_old_decl (olddecl);
1971 return false;
1974 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1975 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1977 error ("redefinition of %q+D", newdecl);
1978 locate_old_decl (olddecl);
1979 return false;
1982 /* Objects declared at file scope: if the first declaration had
1983 external linkage (even if it was an external reference) the
1984 second must have external linkage as well, or the behavior is
1985 undefined. If the first declaration had internal linkage, then
1986 the second must too, or else be an external reference (in which
1987 case the composite declaration still has internal linkage).
1988 As for function declarations, we warn about the static-then-
1989 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1990 if (DECL_FILE_SCOPE_P (newdecl)
1991 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1993 if (DECL_EXTERNAL (newdecl))
1995 if (!DECL_FILE_SCOPE_P (olddecl))
1997 error ("extern declaration of %q+D follows "
1998 "declaration with no linkage", newdecl);
1999 locate_old_decl (olddecl);
2000 return false;
2002 else if (warn_traditional)
2004 warned |= warning (OPT_Wtraditional,
2005 "non-static declaration of %q+D "
2006 "follows static declaration", newdecl);
2009 else
2011 if (TREE_PUBLIC (newdecl))
2012 error ("non-static declaration of %q+D follows "
2013 "static declaration", newdecl);
2014 else
2015 error ("static declaration of %q+D follows "
2016 "non-static declaration", newdecl);
2018 locate_old_decl (olddecl);
2019 return false;
2022 /* Two objects with the same name declared at the same block
2023 scope must both be external references (6.7p3). */
2024 else if (!DECL_FILE_SCOPE_P (newdecl))
2026 if (DECL_EXTERNAL (newdecl))
2028 /* Extern with initializer at block scope, which will
2029 already have received an error. */
2031 else if (DECL_EXTERNAL (olddecl))
2033 error ("declaration of %q+D with no linkage follows "
2034 "extern declaration", newdecl);
2035 locate_old_decl (olddecl);
2037 else
2039 error ("redeclaration of %q+D with no linkage", newdecl);
2040 locate_old_decl (olddecl);
2043 return false;
2046 /* C++ does not permit a decl to appear multiple times at file
2047 scope. */
2048 if (warn_cxx_compat
2049 && DECL_FILE_SCOPE_P (newdecl)
2050 && !DECL_EXTERNAL (newdecl)
2051 && !DECL_EXTERNAL (olddecl))
2052 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2053 OPT_Wc___compat,
2054 ("duplicate declaration of %qD is "
2055 "invalid in C++"),
2056 newdecl);
2059 /* warnings */
2060 /* All decls must agree on a visibility. */
2061 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2062 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2063 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2065 warned |= warning (0, "redeclaration of %q+D with different visibility "
2066 "(old visibility preserved)", newdecl);
2069 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2071 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2072 if (DECL_DECLARED_INLINE_P (newdecl)
2073 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2075 warned |= warning (OPT_Wattributes,
2076 "inline declaration of %qD follows "
2077 "declaration with attribute noinline", newdecl);
2079 else if (DECL_DECLARED_INLINE_P (olddecl)
2080 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2082 warned |= warning (OPT_Wattributes,
2083 "declaration of %q+D with attribute "
2084 "noinline follows inline declaration ", newdecl);
2087 else /* PARM_DECL, VAR_DECL */
2089 /* Redeclaration of a parameter is a constraint violation (this is
2090 not explicitly stated, but follows from C99 6.7p3 [no more than
2091 one declaration of the same identifier with no linkage in the
2092 same scope, except type tags] and 6.2.2p6 [parameters have no
2093 linkage]). We must check for a forward parameter declaration,
2094 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2095 an extension, the mandatory diagnostic for which is handled by
2096 mark_forward_parm_decls. */
2098 if (TREE_CODE (newdecl) == PARM_DECL
2099 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2101 error ("redefinition of parameter %q+D", newdecl);
2102 locate_old_decl (olddecl);
2103 return false;
2107 /* Optional warning for completely redundant decls. */
2108 if (!warned && !pedwarned
2109 && warn_redundant_decls
2110 /* Don't warn about a function declaration followed by a
2111 definition. */
2112 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2113 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2114 /* Don't warn about redundant redeclarations of builtins. */
2115 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2116 && !DECL_BUILT_IN (newdecl)
2117 && DECL_BUILT_IN (olddecl)
2118 && !C_DECL_DECLARED_BUILTIN (olddecl))
2119 /* Don't warn about an extern followed by a definition. */
2120 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2121 /* Don't warn about forward parameter decls. */
2122 && !(TREE_CODE (newdecl) == PARM_DECL
2123 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2124 /* Don't warn about a variable definition following a declaration. */
2125 && !(TREE_CODE (newdecl) == VAR_DECL
2126 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2128 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2129 newdecl);
2132 /* Report location of previous decl/defn. */
2133 if (warned || pedwarned)
2134 locate_old_decl (olddecl);
2136 #undef DECL_EXTERN_INLINE
2138 return retval;
2141 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2142 consistent with OLDDECL, but carries new information. Merge the
2143 new information into OLDDECL. This function issues no
2144 diagnostics. */
2146 static void
2147 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2149 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2150 && DECL_INITIAL (newdecl) != 0);
2151 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2152 && prototype_p (TREE_TYPE (newdecl)));
2153 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2154 && prototype_p (TREE_TYPE (olddecl)));
2155 bool extern_changed = false;
2157 /* For real parm decl following a forward decl, rechain the old decl
2158 in its new location and clear TREE_ASM_WRITTEN (it's not a
2159 forward decl anymore). */
2160 if (TREE_CODE (newdecl) == PARM_DECL
2161 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2163 struct c_binding *b, **here;
2165 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2166 if ((*here)->decl == olddecl)
2167 goto found;
2168 gcc_unreachable ();
2170 found:
2171 b = *here;
2172 *here = b->prev;
2173 b->prev = current_scope->bindings;
2174 current_scope->bindings = b;
2176 TREE_ASM_WRITTEN (olddecl) = 0;
2179 DECL_ATTRIBUTES (newdecl)
2180 = targetm.merge_decl_attributes (olddecl, newdecl);
2182 /* Merge the data types specified in the two decls. */
2183 TREE_TYPE (newdecl)
2184 = TREE_TYPE (olddecl)
2185 = composite_type (newtype, oldtype);
2187 /* Lay the type out, unless already done. */
2188 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2190 if (TREE_TYPE (newdecl) != error_mark_node)
2191 layout_type (TREE_TYPE (newdecl));
2192 if (TREE_CODE (newdecl) != FUNCTION_DECL
2193 && TREE_CODE (newdecl) != TYPE_DECL
2194 && TREE_CODE (newdecl) != CONST_DECL)
2195 layout_decl (newdecl, 0);
2197 else
2199 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2200 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2201 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2202 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2203 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2205 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2206 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2210 /* Keep the old rtl since we can safely use it. */
2211 if (HAS_RTL_P (olddecl))
2212 COPY_DECL_RTL (olddecl, newdecl);
2214 /* Merge the type qualifiers. */
2215 if (TREE_READONLY (newdecl))
2216 TREE_READONLY (olddecl) = 1;
2218 if (TREE_THIS_VOLATILE (newdecl))
2219 TREE_THIS_VOLATILE (olddecl) = 1;
2221 /* Merge deprecatedness. */
2222 if (TREE_DEPRECATED (newdecl))
2223 TREE_DEPRECATED (olddecl) = 1;
2225 /* If a decl is in a system header and the other isn't, keep the one on the
2226 system header. Otherwise, keep source location of definition rather than
2227 declaration and of prototype rather than non-prototype unless that
2228 prototype is built-in. */
2229 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2230 && DECL_IN_SYSTEM_HEADER (olddecl)
2231 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2232 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2233 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2234 && DECL_IN_SYSTEM_HEADER (newdecl)
2235 && !DECL_IN_SYSTEM_HEADER (olddecl))
2236 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2237 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2238 || (old_is_prototype && !new_is_prototype
2239 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2240 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2242 /* Merge the initialization information. */
2243 if (DECL_INITIAL (newdecl) == 0)
2244 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2246 /* Merge the threadprivate attribute. */
2247 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
2249 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
2250 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2253 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2255 /* Merge the section attribute.
2256 We want to issue an error if the sections conflict but that
2257 must be done later in decl_attributes since we are called
2258 before attributes are assigned. */
2259 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
2260 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
2262 /* Copy the assembler name.
2263 Currently, it can only be defined in the prototype. */
2264 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2266 /* Use visibility of whichever declaration had it specified */
2267 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2269 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2270 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2273 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2275 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2276 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2277 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2278 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2279 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2280 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2281 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2282 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2283 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2284 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2285 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2288 /* Merge the storage class information. */
2289 merge_weak (newdecl, olddecl);
2291 /* For functions, static overrides non-static. */
2292 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2294 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2295 /* This is since we don't automatically
2296 copy the attributes of NEWDECL into OLDDECL. */
2297 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2298 /* If this clears `static', clear it in the identifier too. */
2299 if (!TREE_PUBLIC (olddecl))
2300 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2304 /* In c99, 'extern' declaration before (or after) 'inline' means this
2305 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2306 is present. */
2307 if (TREE_CODE (newdecl) == FUNCTION_DECL
2308 && !flag_gnu89_inline
2309 && (DECL_DECLARED_INLINE_P (newdecl)
2310 || DECL_DECLARED_INLINE_P (olddecl))
2311 && (!DECL_DECLARED_INLINE_P (newdecl)
2312 || !DECL_DECLARED_INLINE_P (olddecl)
2313 || !DECL_EXTERNAL (olddecl))
2314 && DECL_EXTERNAL (newdecl)
2315 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2316 && !current_function_decl)
2317 DECL_EXTERNAL (newdecl) = 0;
2319 if (DECL_EXTERNAL (newdecl))
2321 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2322 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2324 /* An extern decl does not override previous storage class. */
2325 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2326 if (!DECL_EXTERNAL (newdecl))
2328 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2329 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2332 else
2334 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2335 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2338 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2340 /* If we're redefining a function previously defined as extern
2341 inline, make sure we emit debug info for the inline before we
2342 throw it away, in case it was inlined into a function that
2343 hasn't been written out yet. */
2344 if (new_is_definition && DECL_INITIAL (olddecl))
2345 /* The new defn must not be inline. */
2346 DECL_UNINLINABLE (newdecl) = 1;
2347 else
2349 /* If either decl says `inline', this fn is inline, unless
2350 its definition was passed already. */
2351 if (DECL_DECLARED_INLINE_P (newdecl)
2352 || DECL_DECLARED_INLINE_P (olddecl))
2353 DECL_DECLARED_INLINE_P (newdecl) = 1;
2355 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2356 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2358 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2359 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2360 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2361 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2364 if (DECL_BUILT_IN (olddecl))
2366 /* If redeclaring a builtin function, it stays built in.
2367 But it gets tagged as having been declared. */
2368 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2369 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2370 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2371 if (new_is_prototype)
2372 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2373 else
2374 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2375 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2378 /* Preserve function specific target and optimization options */
2379 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2380 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2381 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2382 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2384 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2385 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2386 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2387 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2389 /* Also preserve various other info from the definition. */
2390 if (!new_is_definition)
2392 tree t;
2393 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2394 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2395 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2396 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2397 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2398 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2399 DECL_CONTEXT (t) = newdecl;
2401 /* See if we've got a function to instantiate from. */
2402 if (DECL_SAVED_TREE (olddecl))
2403 DECL_ABSTRACT_ORIGIN (newdecl)
2404 = DECL_ABSTRACT_ORIGIN (olddecl);
2408 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
2410 /* Merge the USED information. */
2411 if (TREE_USED (olddecl))
2412 TREE_USED (newdecl) = 1;
2413 else if (TREE_USED (newdecl))
2414 TREE_USED (olddecl) = 1;
2415 if (TREE_CODE (olddecl) == VAR_DECL || TREE_CODE (olddecl) == PARM_DECL)
2416 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2417 if (DECL_PRESERVE_P (olddecl))
2418 DECL_PRESERVE_P (newdecl) = 1;
2419 else if (DECL_PRESERVE_P (newdecl))
2420 DECL_PRESERVE_P (olddecl) = 1;
2422 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2423 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2424 DECL_ARGUMENTS (if appropriate). */
2426 unsigned olddecl_uid = DECL_UID (olddecl);
2427 tree olddecl_context = DECL_CONTEXT (olddecl);
2428 tree olddecl_arguments = NULL;
2429 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2430 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2432 memcpy ((char *) olddecl + sizeof (struct tree_common),
2433 (char *) newdecl + sizeof (struct tree_common),
2434 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2435 switch (TREE_CODE (olddecl))
2437 case FUNCTION_DECL:
2438 case FIELD_DECL:
2439 case VAR_DECL:
2440 case PARM_DECL:
2441 case LABEL_DECL:
2442 case RESULT_DECL:
2443 case CONST_DECL:
2444 case TYPE_DECL:
2445 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2446 (char *) newdecl + sizeof (struct tree_decl_common),
2447 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2448 break;
2450 default:
2452 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2453 (char *) newdecl + sizeof (struct tree_decl_common),
2454 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2456 DECL_UID (olddecl) = olddecl_uid;
2457 DECL_CONTEXT (olddecl) = olddecl_context;
2458 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2459 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2462 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2463 so that encode_section_info has a chance to look at the new decl
2464 flags and attributes. */
2465 if (DECL_RTL_SET_P (olddecl)
2466 && (TREE_CODE (olddecl) == FUNCTION_DECL
2467 || (TREE_CODE (olddecl) == VAR_DECL
2468 && TREE_STATIC (olddecl))))
2469 make_decl_rtl (olddecl);
2471 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
2472 and the definition is coming from the old version, cgraph needs
2473 to be called again. */
2474 if (extern_changed && !new_is_definition
2475 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
2476 cgraph_mark_if_needed (olddecl);
2479 /* Handle when a new declaration NEWDECL has the same name as an old
2480 one OLDDECL in the same binding contour. Prints an error message
2481 if appropriate.
2483 If safely possible, alter OLDDECL to look like NEWDECL, and return
2484 true. Otherwise, return false. */
2486 static bool
2487 duplicate_decls (tree newdecl, tree olddecl)
2489 tree newtype = NULL, oldtype = NULL;
2491 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2493 /* Avoid `unused variable' and other warnings for OLDDECL. */
2494 TREE_NO_WARNING (olddecl) = 1;
2495 return false;
2498 merge_decls (newdecl, olddecl, newtype, oldtype);
2499 return true;
2503 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2504 static void
2505 warn_if_shadowing (tree new_decl)
2507 struct c_binding *b;
2509 /* Shadow warnings wanted? */
2510 if (!warn_shadow
2511 /* No shadow warnings for internally generated vars. */
2512 || DECL_IS_BUILTIN (new_decl)
2513 /* No shadow warnings for vars made for inlining. */
2514 || DECL_FROM_INLINE (new_decl))
2515 return;
2517 /* Is anything being shadowed? Invisible decls do not count. */
2518 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2519 if (b->decl && b->decl != new_decl && !b->invisible)
2521 tree old_decl = b->decl;
2523 if (old_decl == error_mark_node)
2525 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2526 "non-variable", new_decl);
2527 break;
2529 else if (TREE_CODE (old_decl) == PARM_DECL)
2530 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2531 new_decl);
2532 else if (DECL_FILE_SCOPE_P (old_decl))
2533 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2534 "declaration", new_decl);
2535 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2536 && DECL_BUILT_IN (old_decl))
2538 warning (OPT_Wshadow, "declaration of %q+D shadows "
2539 "a built-in function", new_decl);
2540 break;
2542 else
2543 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2544 new_decl);
2546 warning_at (DECL_SOURCE_LOCATION (old_decl), OPT_Wshadow,
2547 "shadowed declaration is here");
2549 break;
2553 /* Record a decl-node X as belonging to the current lexical scope.
2554 Check for errors (such as an incompatible declaration for the same
2555 name already seen in the same scope).
2557 Returns either X or an old decl for the same name.
2558 If an old decl is returned, it may have been smashed
2559 to agree with what X says. */
2561 tree
2562 pushdecl (tree x)
2564 tree name = DECL_NAME (x);
2565 struct c_scope *scope = current_scope;
2566 struct c_binding *b;
2567 bool nested = false;
2568 location_t locus = DECL_SOURCE_LOCATION (x);
2570 /* Must set DECL_CONTEXT for everything not at file scope or
2571 DECL_FILE_SCOPE_P won't work. Local externs don't count
2572 unless they have initializers (which generate code). */
2573 if (current_function_decl
2574 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2575 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2576 DECL_CONTEXT (x) = current_function_decl;
2578 /* Anonymous decls are just inserted in the scope. */
2579 if (!name)
2581 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2582 locus);
2583 return x;
2586 /* First, see if there is another declaration with the same name in
2587 the current scope. If there is, duplicate_decls may do all the
2588 work for us. If duplicate_decls returns false, that indicates
2589 two incompatible decls in the same scope; we are to silently
2590 replace the old one (duplicate_decls has issued all appropriate
2591 diagnostics). In particular, we should not consider possible
2592 duplicates in the external scope, or shadowing. */
2593 b = I_SYMBOL_BINDING (name);
2594 if (b && B_IN_SCOPE (b, scope))
2596 struct c_binding *b_ext, *b_use;
2597 tree type = TREE_TYPE (x);
2598 tree visdecl = b->decl;
2599 tree vistype = TREE_TYPE (visdecl);
2600 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2601 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2602 b->inner_comp = false;
2603 b_use = b;
2604 b_ext = b;
2605 /* If this is an external linkage declaration, we should check
2606 for compatibility with the type in the external scope before
2607 setting the type at this scope based on the visible
2608 information only. */
2609 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2611 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2612 b_ext = b_ext->shadowed;
2613 if (b_ext)
2615 b_use = b_ext;
2616 if (b_use->u.type)
2617 TREE_TYPE (b_use->decl) = b_use->u.type;
2620 if (duplicate_decls (x, b_use->decl))
2622 if (b_use != b)
2624 /* Save the updated type in the external scope and
2625 restore the proper type for this scope. */
2626 tree thistype;
2627 if (comptypes (vistype, type))
2628 thistype = composite_type (vistype, type);
2629 else
2630 thistype = TREE_TYPE (b_use->decl);
2631 b_use->u.type = TREE_TYPE (b_use->decl);
2632 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2633 && DECL_BUILT_IN (b_use->decl))
2634 thistype
2635 = build_type_attribute_variant (thistype,
2636 TYPE_ATTRIBUTES
2637 (b_use->u.type));
2638 TREE_TYPE (b_use->decl) = thistype;
2640 return b_use->decl;
2642 else
2643 goto skip_external_and_shadow_checks;
2646 /* All declarations with external linkage, and all external
2647 references, go in the external scope, no matter what scope is
2648 current. However, the binding in that scope is ignored for
2649 purposes of normal name lookup. A separate binding structure is
2650 created in the requested scope; this governs the normal
2651 visibility of the symbol.
2653 The binding in the externals scope is used exclusively for
2654 detecting duplicate declarations of the same object, no matter
2655 what scope they are in; this is what we do here. (C99 6.2.7p2:
2656 All declarations that refer to the same object or function shall
2657 have compatible type; otherwise, the behavior is undefined.) */
2658 if (DECL_EXTERNAL (x) || scope == file_scope)
2660 tree type = TREE_TYPE (x);
2661 tree vistype = 0;
2662 tree visdecl = 0;
2663 bool type_saved = false;
2664 if (b && !B_IN_EXTERNAL_SCOPE (b)
2665 && (TREE_CODE (b->decl) == FUNCTION_DECL
2666 || TREE_CODE (b->decl) == VAR_DECL)
2667 && DECL_FILE_SCOPE_P (b->decl))
2669 visdecl = b->decl;
2670 vistype = TREE_TYPE (visdecl);
2672 if (scope != file_scope
2673 && !DECL_IN_SYSTEM_HEADER (x))
2674 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2676 while (b && !B_IN_EXTERNAL_SCOPE (b))
2678 /* If this decl might be modified, save its type. This is
2679 done here rather than when the decl is first bound
2680 because the type may change after first binding, through
2681 being completed or through attributes being added. If we
2682 encounter multiple such decls, only the first should have
2683 its type saved; the others will already have had their
2684 proper types saved and the types will not have changed as
2685 their scopes will not have been re-entered. */
2686 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2688 b->u.type = TREE_TYPE (b->decl);
2689 type_saved = true;
2691 if (B_IN_FILE_SCOPE (b)
2692 && TREE_CODE (b->decl) == VAR_DECL
2693 && TREE_STATIC (b->decl)
2694 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2695 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2696 && TREE_CODE (type) == ARRAY_TYPE
2697 && TYPE_DOMAIN (type)
2698 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2699 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2701 /* Array type completed in inner scope, which should be
2702 diagnosed if the completion does not have size 1 and
2703 it does not get completed in the file scope. */
2704 b->inner_comp = true;
2706 b = b->shadowed;
2709 /* If a matching external declaration has been found, set its
2710 type to the composite of all the types of that declaration.
2711 After the consistency checks, it will be reset to the
2712 composite of the visible types only. */
2713 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2714 && b->u.type)
2715 TREE_TYPE (b->decl) = b->u.type;
2717 /* The point of the same_translation_unit_p check here is,
2718 we want to detect a duplicate decl for a construct like
2719 foo() { extern bar(); } ... static bar(); but not if
2720 they are in different translation units. In any case,
2721 the static does not go in the externals scope. */
2722 if (b
2723 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2724 && duplicate_decls (x, b->decl))
2726 tree thistype;
2727 if (vistype)
2729 if (comptypes (vistype, type))
2730 thistype = composite_type (vistype, type);
2731 else
2732 thistype = TREE_TYPE (b->decl);
2734 else
2735 thistype = type;
2736 b->u.type = TREE_TYPE (b->decl);
2737 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2738 thistype
2739 = build_type_attribute_variant (thistype,
2740 TYPE_ATTRIBUTES (b->u.type));
2741 TREE_TYPE (b->decl) = thistype;
2742 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2743 locus);
2744 return b->decl;
2746 else if (TREE_PUBLIC (x))
2748 if (visdecl && !b && duplicate_decls (x, visdecl))
2750 /* An external declaration at block scope referring to a
2751 visible entity with internal linkage. The composite
2752 type will already be correct for this scope, so we
2753 just need to fall through to make the declaration in
2754 this scope. */
2755 nested = true;
2756 x = visdecl;
2758 else
2760 bind (name, x, external_scope, /*invisible=*/true,
2761 /*nested=*/false, locus);
2762 nested = true;
2767 if (TREE_CODE (x) != PARM_DECL)
2768 warn_if_shadowing (x);
2770 skip_external_and_shadow_checks:
2771 if (TREE_CODE (x) == TYPE_DECL)
2772 set_underlying_type (x);
2774 bind (name, x, scope, /*invisible=*/false, nested, locus);
2776 /* If x's type is incomplete because it's based on a
2777 structure or union which has not yet been fully declared,
2778 attach it to that structure or union type, so we can go
2779 back and complete the variable declaration later, if the
2780 structure or union gets fully declared.
2782 If the input is erroneous, we can have error_mark in the type
2783 slot (e.g. "f(void a, ...)") - that doesn't count as an
2784 incomplete type. */
2785 if (TREE_TYPE (x) != error_mark_node
2786 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2788 tree element = TREE_TYPE (x);
2790 while (TREE_CODE (element) == ARRAY_TYPE)
2791 element = TREE_TYPE (element);
2792 element = TYPE_MAIN_VARIANT (element);
2794 if ((TREE_CODE (element) == RECORD_TYPE
2795 || TREE_CODE (element) == UNION_TYPE)
2796 && (TREE_CODE (x) != TYPE_DECL
2797 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2798 && !COMPLETE_TYPE_P (element))
2799 C_TYPE_INCOMPLETE_VARS (element)
2800 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2802 return x;
2805 /* Record X as belonging to file scope.
2806 This is used only internally by the Objective-C front end,
2807 and is limited to its needs. duplicate_decls is not called;
2808 if there is any preexisting decl for this identifier, it is an ICE. */
2810 tree
2811 pushdecl_top_level (tree x)
2813 tree name;
2814 bool nested = false;
2815 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2817 name = DECL_NAME (x);
2819 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2821 if (TREE_PUBLIC (x))
2823 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2824 UNKNOWN_LOCATION);
2825 nested = true;
2827 if (file_scope)
2828 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2830 return x;
2833 static void
2834 implicit_decl_warning (tree id, tree olddecl)
2836 if (warn_implicit_function_declaration)
2838 bool warned;
2840 if (flag_isoc99)
2841 warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2842 "implicit declaration of function %qE", id);
2843 else
2844 warned = warning (OPT_Wimplicit_function_declaration,
2845 G_("implicit declaration of function %qE"), id);
2846 if (olddecl && warned)
2847 locate_old_decl (olddecl);
2851 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
2852 function of type int (). */
2854 tree
2855 implicitly_declare (location_t loc, tree functionid)
2857 struct c_binding *b;
2858 tree decl = 0;
2859 tree asmspec_tree;
2861 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2863 if (B_IN_SCOPE (b, external_scope))
2865 decl = b->decl;
2866 break;
2870 if (decl)
2872 if (decl == error_mark_node)
2873 return decl;
2875 /* FIXME: Objective-C has weird not-really-builtin functions
2876 which are supposed to be visible automatically. They wind up
2877 in the external scope because they're pushed before the file
2878 scope gets created. Catch this here and rebind them into the
2879 file scope. */
2880 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2882 bind (functionid, decl, file_scope,
2883 /*invisible=*/false, /*nested=*/true,
2884 DECL_SOURCE_LOCATION (decl));
2885 return decl;
2887 else
2889 tree newtype = default_function_type;
2890 if (b->u.type)
2891 TREE_TYPE (decl) = b->u.type;
2892 /* Implicit declaration of a function already declared
2893 (somehow) in a different scope, or as a built-in.
2894 If this is the first time this has happened, warn;
2895 then recycle the old declaration but with the new type. */
2896 if (!C_DECL_IMPLICIT (decl))
2898 implicit_decl_warning (functionid, decl);
2899 C_DECL_IMPLICIT (decl) = 1;
2901 if (DECL_BUILT_IN (decl))
2903 newtype = build_type_attribute_variant (newtype,
2904 TYPE_ATTRIBUTES
2905 (TREE_TYPE (decl)));
2906 if (!comptypes (newtype, TREE_TYPE (decl)))
2908 warning_at (loc, 0, "incompatible implicit declaration of "
2909 "built-in function %qD", decl);
2910 newtype = TREE_TYPE (decl);
2913 else
2915 if (!comptypes (newtype, TREE_TYPE (decl)))
2917 error_at (loc, "incompatible implicit declaration of function %qD", decl);
2918 locate_old_decl (decl);
2921 b->u.type = TREE_TYPE (decl);
2922 TREE_TYPE (decl) = newtype;
2923 bind (functionid, decl, current_scope,
2924 /*invisible=*/false, /*nested=*/true,
2925 DECL_SOURCE_LOCATION (decl));
2926 return decl;
2930 /* Not seen before. */
2931 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
2932 DECL_EXTERNAL (decl) = 1;
2933 TREE_PUBLIC (decl) = 1;
2934 C_DECL_IMPLICIT (decl) = 1;
2935 implicit_decl_warning (functionid, 0);
2936 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2937 if (asmspec_tree)
2938 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2940 /* C89 says implicit declarations are in the innermost block.
2941 So we record the decl in the standard fashion. */
2942 decl = pushdecl (decl);
2944 /* No need to call objc_check_decl here - it's a function type. */
2945 rest_of_decl_compilation (decl, 0, 0);
2947 /* Write a record describing this implicit function declaration
2948 to the prototypes file (if requested). */
2949 gen_aux_info_record (decl, 0, 1, 0);
2951 /* Possibly apply some default attributes to this implicit declaration. */
2952 decl_attributes (&decl, NULL_TREE, 0);
2954 return decl;
2957 /* Issue an error message for a reference to an undeclared variable
2958 ID, including a reference to a builtin outside of function-call
2959 context. Establish a binding of the identifier to error_mark_node
2960 in an appropriate scope, which will suppress further errors for the
2961 same identifier. The error message should be given location LOC. */
2962 void
2963 undeclared_variable (location_t loc, tree id)
2965 static bool already = false;
2966 struct c_scope *scope;
2968 if (current_function_decl == 0)
2970 error_at (loc, "%qE undeclared here (not in a function)", id);
2971 scope = current_scope;
2973 else
2975 if (!objc_diagnose_private_ivar (id))
2976 error_at (loc, "%qE undeclared (first use in this function)", id);
2977 if (!already)
2979 inform (loc, "each undeclared identifier is reported only"
2980 " once for each function it appears in");
2981 already = true;
2984 /* If we are parsing old-style parameter decls, current_function_decl
2985 will be nonnull but current_function_scope will be null. */
2986 scope = current_function_scope ? current_function_scope : current_scope;
2988 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
2989 UNKNOWN_LOCATION);
2992 /* Subroutine of lookup_label, declare_label, define_label: construct a
2993 LABEL_DECL with all the proper frills. Also create a struct
2994 c_label_vars initialized for the current scope. */
2996 static tree
2997 make_label (location_t location, tree name, bool defining,
2998 struct c_label_vars **p_label_vars)
3000 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3001 struct c_label_vars *label_vars;
3003 DECL_CONTEXT (label) = current_function_decl;
3004 DECL_MODE (label) = VOIDmode;
3006 label_vars = ggc_alloc_c_label_vars ();
3007 label_vars->shadowed = NULL;
3008 set_spot_bindings (&label_vars->label_bindings, defining);
3009 label_vars->decls_in_scope = make_tree_vector ();
3010 label_vars->gotos = VEC_alloc (c_goto_bindings_p, gc, 0);
3011 *p_label_vars = label_vars;
3013 return label;
3016 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3017 Create one if none exists so far for the current function.
3018 This is called when a label is used in a goto expression or
3019 has its address taken. */
3021 tree
3022 lookup_label (tree name)
3024 tree label;
3025 struct c_label_vars *label_vars;
3027 if (current_function_scope == 0)
3029 error ("label %qE referenced outside of any function", name);
3030 return 0;
3033 /* Use a label already defined or ref'd with this name, but not if
3034 it is inherited from a containing function and wasn't declared
3035 using __label__. */
3036 label = I_LABEL_DECL (name);
3037 if (label && (DECL_CONTEXT (label) == current_function_decl
3038 || C_DECLARED_LABEL_FLAG (label)))
3040 /* If the label has only been declared, update its apparent
3041 location to point here, for better diagnostics if it
3042 turns out not to have been defined. */
3043 if (DECL_INITIAL (label) == NULL_TREE)
3044 DECL_SOURCE_LOCATION (label) = input_location;
3045 return label;
3048 /* No label binding for that identifier; make one. */
3049 label = make_label (input_location, name, false, &label_vars);
3051 /* Ordinary labels go in the current function scope. */
3052 bind_label (name, label, current_function_scope, label_vars);
3054 return label;
3057 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3058 to LABEL. */
3060 static void
3061 warn_about_goto (location_t goto_loc, tree label, tree decl)
3063 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3064 error_at (goto_loc,
3065 "jump into scope of identifier with variably modified type");
3066 else
3067 warning_at (goto_loc, OPT_Wjump_misses_init,
3068 "jump skips variable initialization");
3069 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3070 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3073 /* Look up a label because of a goto statement. This is like
3074 lookup_label, but also issues any appropriate warnings. */
3076 tree
3077 lookup_label_for_goto (location_t loc, tree name)
3079 tree label;
3080 struct c_label_vars *label_vars;
3081 unsigned int ix;
3082 tree decl;
3084 label = lookup_label (name);
3085 if (label == NULL_TREE)
3086 return NULL_TREE;
3088 /* If we are jumping to a different function, we can't issue any
3089 useful warnings. */
3090 if (DECL_CONTEXT (label) != current_function_decl)
3092 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3093 return label;
3096 label_vars = I_LABEL_BINDING (name)->u.label;
3098 /* If the label has not yet been defined, then push this goto on a
3099 list for possible later warnings. */
3100 if (label_vars->label_bindings.scope == NULL)
3102 struct c_goto_bindings *g;
3104 g = ggc_alloc_c_goto_bindings ();
3105 g->loc = loc;
3106 set_spot_bindings (&g->goto_bindings, true);
3107 VEC_safe_push (c_goto_bindings_p, gc, label_vars->gotos, g);
3108 return label;
3111 /* If there are any decls in label_vars->decls_in_scope, then this
3112 goto has missed the declaration of the decl. This happens for a
3113 case like
3114 int i = 1;
3115 lab:
3117 goto lab;
3118 Issue a warning or error. */
3119 FOR_EACH_VEC_ELT (tree, label_vars->decls_in_scope, ix, decl)
3120 warn_about_goto (loc, label, decl);
3122 if (label_vars->label_bindings.left_stmt_expr)
3124 error_at (loc, "jump into statement expression");
3125 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3128 return label;
3131 /* Make a label named NAME in the current function, shadowing silently
3132 any that may be inherited from containing functions or containing
3133 scopes. This is called for __label__ declarations. */
3135 tree
3136 declare_label (tree name)
3138 struct c_binding *b = I_LABEL_BINDING (name);
3139 tree label;
3140 struct c_label_vars *label_vars;
3142 /* Check to make sure that the label hasn't already been declared
3143 at this scope */
3144 if (b && B_IN_CURRENT_SCOPE (b))
3146 error ("duplicate label declaration %qE", name);
3147 locate_old_decl (b->decl);
3149 /* Just use the previous declaration. */
3150 return b->decl;
3153 label = make_label (input_location, name, false, &label_vars);
3154 C_DECLARED_LABEL_FLAG (label) = 1;
3156 /* Declared labels go in the current scope. */
3157 bind_label (name, label, current_scope, label_vars);
3159 return label;
3162 /* When we define a label, issue any appropriate warnings if there are
3163 any gotos earlier in the function which jump to this label. */
3165 static void
3166 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3168 unsigned int ix;
3169 struct c_goto_bindings *g;
3171 FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
3173 struct c_binding *b;
3174 struct c_scope *scope;
3176 /* We have a goto to this label. The goto is going forward. In
3177 g->scope, the goto is going to skip any binding which was
3178 defined after g->bindings_in_scope. */
3179 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3181 for (b = g->goto_bindings.scope->bindings;
3182 b != g->goto_bindings.bindings_in_scope;
3183 b = b->prev)
3185 if (decl_jump_unsafe (b->decl))
3186 warn_about_goto (g->loc, label, b->decl);
3190 /* We also need to warn about decls defined in any scopes
3191 between the scope of the label and the scope of the goto. */
3192 for (scope = label_vars->label_bindings.scope;
3193 scope != g->goto_bindings.scope;
3194 scope = scope->outer)
3196 gcc_assert (scope != NULL);
3197 if (scope->has_jump_unsafe_decl)
3199 if (scope == label_vars->label_bindings.scope)
3200 b = label_vars->label_bindings.bindings_in_scope;
3201 else
3202 b = scope->bindings;
3203 for (; b != NULL; b = b->prev)
3205 if (decl_jump_unsafe (b->decl))
3206 warn_about_goto (g->loc, label, b->decl);
3211 if (g->goto_bindings.stmt_exprs > 0)
3213 error_at (g->loc, "jump into statement expression");
3214 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3215 label);
3219 /* Now that the label is defined, we will issue warnings about
3220 subsequent gotos to this label when we see them. */
3221 VEC_truncate (c_goto_bindings_p, label_vars->gotos, 0);
3222 label_vars->gotos = NULL;
3225 /* Define a label, specifying the location in the source file.
3226 Return the LABEL_DECL node for the label, if the definition is valid.
3227 Otherwise return 0. */
3229 tree
3230 define_label (location_t location, tree name)
3232 /* Find any preexisting label with this name. It is an error
3233 if that label has already been defined in this function, or
3234 if there is a containing function with a declared label with
3235 the same name. */
3236 tree label = I_LABEL_DECL (name);
3238 if (label
3239 && ((DECL_CONTEXT (label) == current_function_decl
3240 && DECL_INITIAL (label) != 0)
3241 || (DECL_CONTEXT (label) != current_function_decl
3242 && C_DECLARED_LABEL_FLAG (label))))
3244 error_at (location, "duplicate label %qD", label);
3245 locate_old_decl (label);
3246 return 0;
3248 else if (label && DECL_CONTEXT (label) == current_function_decl)
3250 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3252 /* The label has been used or declared already in this function,
3253 but not defined. Update its location to point to this
3254 definition. */
3255 DECL_SOURCE_LOCATION (label) = location;
3256 set_spot_bindings (&label_vars->label_bindings, true);
3258 /* Issue warnings as required about any goto statements from
3259 earlier in the function. */
3260 check_earlier_gotos (label, label_vars);
3262 else
3264 struct c_label_vars *label_vars;
3266 /* No label binding for that identifier; make one. */
3267 label = make_label (location, name, true, &label_vars);
3269 /* Ordinary labels go in the current function scope. */
3270 bind_label (name, label, current_function_scope, label_vars);
3273 if (!in_system_header && lookup_name (name))
3274 warning_at (location, OPT_Wtraditional,
3275 "traditional C lacks a separate namespace "
3276 "for labels, identifier %qE conflicts", name);
3278 /* Mark label as having been defined. */
3279 DECL_INITIAL (label) = error_mark_node;
3280 return label;
3283 /* Get the bindings for a new switch statement. This is used to issue
3284 warnings as appropriate for jumps from the switch to case or
3285 default labels. */
3287 struct c_spot_bindings *
3288 c_get_switch_bindings (void)
3290 struct c_spot_bindings *switch_bindings;
3292 switch_bindings = XNEW (struct c_spot_bindings);
3293 set_spot_bindings (switch_bindings, true);
3294 return switch_bindings;
3297 void
3298 c_release_switch_bindings (struct c_spot_bindings *bindings)
3300 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3301 XDELETE (bindings);
3304 /* This is called at the point of a case or default label to issue
3305 warnings about decls as needed. It returns true if it found an
3306 error, not just a warning. */
3308 bool
3309 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3310 location_t switch_loc, location_t case_loc)
3312 bool saw_error;
3313 struct c_scope *scope;
3315 saw_error = false;
3316 for (scope = current_scope;
3317 scope != switch_bindings->scope;
3318 scope = scope->outer)
3320 struct c_binding *b;
3322 gcc_assert (scope != NULL);
3324 if (!scope->has_jump_unsafe_decl)
3325 continue;
3327 for (b = scope->bindings; b != NULL; b = b->prev)
3329 if (decl_jump_unsafe (b->decl))
3331 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3333 saw_error = true;
3334 error_at (case_loc,
3335 ("switch jumps into scope of identifier with "
3336 "variably modified type"));
3338 else
3339 warning_at (case_loc, OPT_Wjump_misses_init,
3340 "switch jumps over variable initialization");
3341 inform (switch_loc, "switch starts here");
3342 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3343 b->decl);
3348 if (switch_bindings->stmt_exprs > 0)
3350 saw_error = true;
3351 error_at (case_loc, "switch jumps into statement expression");
3352 inform (switch_loc, "switch starts here");
3355 return saw_error;
3358 /* Given NAME, an IDENTIFIER_NODE,
3359 return the structure (or union or enum) definition for that name.
3360 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3361 CODE says which kind of type the caller wants;
3362 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3363 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3364 location where the tag was defined.
3365 If the wrong kind of type is found, an error is reported. */
3367 static tree
3368 lookup_tag (enum tree_code code, tree name, int thislevel_only,
3369 location_t *ploc)
3371 struct c_binding *b = I_TAG_BINDING (name);
3372 int thislevel = 0;
3374 if (!b || !b->decl)
3375 return 0;
3377 /* We only care about whether it's in this level if
3378 thislevel_only was set or it might be a type clash. */
3379 if (thislevel_only || TREE_CODE (b->decl) != code)
3381 /* For our purposes, a tag in the external scope is the same as
3382 a tag in the file scope. (Primarily relevant to Objective-C
3383 and its builtin structure tags, which get pushed before the
3384 file scope is created.) */
3385 if (B_IN_CURRENT_SCOPE (b)
3386 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3387 thislevel = 1;
3390 if (thislevel_only && !thislevel)
3391 return 0;
3393 if (TREE_CODE (b->decl) != code)
3395 /* Definition isn't the kind we were looking for. */
3396 pending_invalid_xref = name;
3397 pending_invalid_xref_location = input_location;
3399 /* If in the same binding level as a declaration as a tag
3400 of a different type, this must not be allowed to
3401 shadow that tag, so give the error immediately.
3402 (For example, "struct foo; union foo;" is invalid.) */
3403 if (thislevel)
3404 pending_xref_error ();
3407 if (ploc != NULL)
3408 *ploc = b->locus;
3410 return b->decl;
3413 /* Print an error message now
3414 for a recent invalid struct, union or enum cross reference.
3415 We don't print them immediately because they are not invalid
3416 when used in the `struct foo;' construct for shadowing. */
3418 void
3419 pending_xref_error (void)
3421 if (pending_invalid_xref != 0)
3422 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3423 pending_invalid_xref);
3424 pending_invalid_xref = 0;
3428 /* Look up NAME in the current scope and its superiors
3429 in the namespace of variables, functions and typedefs.
3430 Return a ..._DECL node of some kind representing its definition,
3431 or return 0 if it is undefined. */
3433 tree
3434 lookup_name (tree name)
3436 struct c_binding *b = I_SYMBOL_BINDING (name);
3437 if (b && !b->invisible)
3438 return b->decl;
3439 return 0;
3442 /* Similar to `lookup_name' but look only at the indicated scope. */
3444 static tree
3445 lookup_name_in_scope (tree name, struct c_scope *scope)
3447 struct c_binding *b;
3449 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3450 if (B_IN_SCOPE (b, scope))
3451 return b->decl;
3452 return 0;
3455 /* Create the predefined scalar types of C,
3456 and some nodes representing standard constants (0, 1, (void *) 0).
3457 Initialize the global scope.
3458 Make definitions for built-in primitive functions. */
3460 void
3461 c_init_decl_processing (void)
3463 location_t save_loc = input_location;
3465 /* Initialize reserved words for parser. */
3466 c_parse_init ();
3468 current_function_decl = 0;
3470 gcc_obstack_init (&parser_obstack);
3472 /* Make the externals scope. */
3473 push_scope ();
3474 external_scope = current_scope;
3476 /* Declarations from c_common_nodes_and_builtins must not be associated
3477 with this input file, lest we get differences between using and not
3478 using preprocessed headers. */
3479 input_location = BUILTINS_LOCATION;
3481 c_common_nodes_and_builtins ();
3483 /* In C, comparisons and TRUTH_* expressions have type int. */
3484 truthvalue_type_node = integer_type_node;
3485 truthvalue_true_node = integer_one_node;
3486 truthvalue_false_node = integer_zero_node;
3488 /* Even in C99, which has a real boolean type. */
3489 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3490 boolean_type_node));
3492 input_location = save_loc;
3494 pedantic_lvalues = true;
3496 make_fname_decl = c_make_fname_decl;
3497 start_fname_decls ();
3500 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3501 give the decl, NAME is the initialization string and TYPE_DEP
3502 indicates whether NAME depended on the type of the function. As we
3503 don't yet implement delayed emission of static data, we mark the
3504 decl as emitted so it is not placed in the output. Anything using
3505 it must therefore pull out the STRING_CST initializer directly.
3506 FIXME. */
3508 static tree
3509 c_make_fname_decl (location_t loc, tree id, int type_dep)
3511 const char *name = fname_as_string (type_dep);
3512 tree decl, type, init;
3513 size_t length = strlen (name);
3515 type = build_array_type (char_type_node,
3516 build_index_type (size_int (length)));
3517 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3519 decl = build_decl (loc, VAR_DECL, id, type);
3521 TREE_STATIC (decl) = 1;
3522 TREE_READONLY (decl) = 1;
3523 DECL_ARTIFICIAL (decl) = 1;
3525 init = build_string (length + 1, name);
3526 free (CONST_CAST (char *, name));
3527 TREE_TYPE (init) = type;
3528 DECL_INITIAL (decl) = init;
3530 TREE_USED (decl) = 1;
3532 if (current_function_decl
3533 /* For invalid programs like this:
3535 void foo()
3536 const char* p = __FUNCTION__;
3538 the __FUNCTION__ is believed to appear in K&R style function
3539 parameter declarator. In that case we still don't have
3540 function_scope. */
3541 && (!seen_error () || current_function_scope))
3543 DECL_CONTEXT (decl) = current_function_decl;
3544 bind (id, decl, current_function_scope,
3545 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
3548 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
3550 return decl;
3553 tree
3554 c_builtin_function (tree decl)
3556 tree type = TREE_TYPE (decl);
3557 tree id = DECL_NAME (decl);
3559 const char *name = IDENTIFIER_POINTER (id);
3560 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
3562 /* Should never be called on a symbol with a preexisting meaning. */
3563 gcc_assert (!I_SYMBOL_BINDING (id));
3565 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
3566 UNKNOWN_LOCATION);
3568 /* Builtins in the implementation namespace are made visible without
3569 needing to be explicitly declared. See push_file_scope. */
3570 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3572 DECL_CHAIN (decl) = visible_builtins;
3573 visible_builtins = decl;
3576 return decl;
3579 tree
3580 c_builtin_function_ext_scope (tree decl)
3582 tree type = TREE_TYPE (decl);
3583 tree id = DECL_NAME (decl);
3585 const char *name = IDENTIFIER_POINTER (id);
3586 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
3588 /* Should never be called on a symbol with a preexisting meaning. */
3589 gcc_assert (!I_SYMBOL_BINDING (id));
3591 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
3592 UNKNOWN_LOCATION);
3594 /* Builtins in the implementation namespace are made visible without
3595 needing to be explicitly declared. See push_file_scope. */
3596 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3598 DECL_CHAIN (decl) = visible_builtins;
3599 visible_builtins = decl;
3602 return decl;
3605 /* Called when a declaration is seen that contains no names to declare.
3606 If its type is a reference to a structure, union or enum inherited
3607 from a containing scope, shadow that tag name for the current scope
3608 with a forward reference.
3609 If its type defines a new named structure or union
3610 or defines an enum, it is valid but we need not do anything here.
3611 Otherwise, it is an error. */
3613 void
3614 shadow_tag (const struct c_declspecs *declspecs)
3616 shadow_tag_warned (declspecs, 0);
3619 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
3620 but no pedwarn. */
3621 void
3622 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
3624 bool found_tag = false;
3626 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
3628 tree value = declspecs->type;
3629 enum tree_code code = TREE_CODE (value);
3631 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3632 /* Used to test also that TYPE_SIZE (value) != 0.
3633 That caused warning for `struct foo;' at top level in the file. */
3635 tree name = TYPE_NAME (value);
3636 tree t;
3638 found_tag = true;
3640 if (declspecs->restrict_p)
3642 error ("invalid use of %<restrict%>");
3643 warned = 1;
3646 if (name == 0)
3648 if (warned != 1 && code != ENUMERAL_TYPE)
3649 /* Empty unnamed enum OK */
3651 pedwarn (input_location, 0,
3652 "unnamed struct/union that defines no instances");
3653 warned = 1;
3656 else if (declspecs->typespec_kind != ctsk_tagdef
3657 && declspecs->typespec_kind != ctsk_tagfirstref
3658 && declspecs->storage_class != csc_none)
3660 if (warned != 1)
3661 pedwarn (input_location, 0,
3662 "empty declaration with storage class specifier "
3663 "does not redeclare tag");
3664 warned = 1;
3665 pending_xref_error ();
3667 else if (declspecs->typespec_kind != ctsk_tagdef
3668 && declspecs->typespec_kind != ctsk_tagfirstref
3669 && (declspecs->const_p
3670 || declspecs->volatile_p
3671 || declspecs->restrict_p
3672 || declspecs->address_space))
3674 if (warned != 1)
3675 pedwarn (input_location, 0,
3676 "empty declaration with type qualifier "
3677 "does not redeclare tag");
3678 warned = 1;
3679 pending_xref_error ();
3681 else
3683 pending_invalid_xref = 0;
3684 t = lookup_tag (code, name, 1, NULL);
3686 if (t == 0)
3688 t = make_node (code);
3689 pushtag (input_location, name, t);
3693 else
3695 if (warned != 1 && !in_system_header)
3697 pedwarn (input_location, 0,
3698 "useless type name in empty declaration");
3699 warned = 1;
3703 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3705 pedwarn (input_location, 0, "useless type name in empty declaration");
3706 warned = 1;
3709 pending_invalid_xref = 0;
3711 if (declspecs->inline_p)
3713 error ("%<inline%> in empty declaration");
3714 warned = 1;
3717 if (declspecs->noreturn_p)
3719 error ("%<_Noreturn%> in empty declaration");
3720 warned = 1;
3723 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3725 error ("%<auto%> in file-scope empty declaration");
3726 warned = 1;
3729 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3731 error ("%<register%> in file-scope empty declaration");
3732 warned = 1;
3735 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3737 warning (0, "useless storage class specifier in empty declaration");
3738 warned = 2;
3741 if (!warned && !in_system_header && declspecs->thread_p)
3743 warning (0, "useless %<__thread%> in empty declaration");
3744 warned = 2;
3747 if (!warned && !in_system_header && (declspecs->const_p
3748 || declspecs->volatile_p
3749 || declspecs->restrict_p
3750 || declspecs->address_space))
3752 warning (0, "useless type qualifier in empty declaration");
3753 warned = 2;
3756 if (warned != 1)
3758 if (!found_tag)
3759 pedwarn (input_location, 0, "empty declaration");
3764 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3765 bits. SPECS represents declaration specifiers that the grammar
3766 only permits to contain type qualifiers and attributes. */
3769 quals_from_declspecs (const struct c_declspecs *specs)
3771 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3772 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3773 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
3774 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
3775 gcc_assert (!specs->type
3776 && !specs->decl_attr
3777 && specs->typespec_word == cts_none
3778 && specs->storage_class == csc_none
3779 && !specs->typedef_p
3780 && !specs->explicit_signed_p
3781 && !specs->deprecated_p
3782 && !specs->long_p
3783 && !specs->long_long_p
3784 && !specs->short_p
3785 && !specs->signed_p
3786 && !specs->unsigned_p
3787 && !specs->complex_p
3788 && !specs->inline_p
3789 && !specs->noreturn_p
3790 && !specs->thread_p);
3791 return quals;
3794 /* Construct an array declarator. LOC is the location of the
3795 beginning of the array (usually the opening brace). EXPR is the
3796 expression inside [], or NULL_TREE. QUALS are the type qualifiers
3797 inside the [] (to be applied to the pointer to which a parameter
3798 array is converted). STATIC_P is true if "static" is inside the
3799 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
3800 VLA of unspecified length which is nevertheless a complete type,
3801 false otherwise. The field for the contained declarator is left to
3802 be filled in by set_array_declarator_inner. */
3804 struct c_declarator *
3805 build_array_declarator (location_t loc,
3806 tree expr, struct c_declspecs *quals, bool static_p,
3807 bool vla_unspec_p)
3809 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3810 struct c_declarator);
3811 declarator->id_loc = loc;
3812 declarator->kind = cdk_array;
3813 declarator->declarator = 0;
3814 declarator->u.array.dimen = expr;
3815 if (quals)
3817 declarator->u.array.attrs = quals->attrs;
3818 declarator->u.array.quals = quals_from_declspecs (quals);
3820 else
3822 declarator->u.array.attrs = NULL_TREE;
3823 declarator->u.array.quals = 0;
3825 declarator->u.array.static_p = static_p;
3826 declarator->u.array.vla_unspec_p = vla_unspec_p;
3827 if (!flag_isoc99)
3829 if (static_p || quals != NULL)
3830 pedwarn (loc, OPT_pedantic,
3831 "ISO C90 does not support %<static%> or type "
3832 "qualifiers in parameter array declarators");
3833 if (vla_unspec_p)
3834 pedwarn (loc, OPT_pedantic,
3835 "ISO C90 does not support %<[*]%> array declarators");
3837 if (vla_unspec_p)
3839 if (!current_scope->parm_flag)
3841 /* C99 6.7.5.2p4 */
3842 error_at (loc, "%<[*]%> not allowed in other than "
3843 "function prototype scope");
3844 declarator->u.array.vla_unspec_p = false;
3845 return NULL;
3847 current_scope->had_vla_unspec = true;
3849 return declarator;
3852 /* Set the contained declarator of an array declarator. DECL is the
3853 declarator, as constructed by build_array_declarator; INNER is what
3854 appears on the left of the []. */
3856 struct c_declarator *
3857 set_array_declarator_inner (struct c_declarator *decl,
3858 struct c_declarator *inner)
3860 decl->declarator = inner;
3861 return decl;
3864 /* INIT is a constructor that forms DECL's initializer. If the final
3865 element initializes a flexible array field, add the size of that
3866 initializer to DECL's size. */
3868 static void
3869 add_flexible_array_elts_to_size (tree decl, tree init)
3871 tree elt, type;
3873 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3874 return;
3876 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3877 type = TREE_TYPE (elt);
3878 if (TREE_CODE (type) == ARRAY_TYPE
3879 && TYPE_SIZE (type) == NULL_TREE
3880 && TYPE_DOMAIN (type) != NULL_TREE
3881 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3883 complete_array_type (&type, elt, false);
3884 DECL_SIZE (decl)
3885 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3886 DECL_SIZE_UNIT (decl)
3887 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3891 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3892 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3893 before the type name, and set *EXPR_CONST_OPERANDS, if
3894 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3895 appear in a constant expression. */
3897 tree
3898 groktypename (struct c_type_name *type_name, tree *expr,
3899 bool *expr_const_operands)
3901 tree type;
3902 tree attrs = type_name->specs->attrs;
3904 type_name->specs->attrs = NULL_TREE;
3906 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3907 false, NULL, &attrs, expr, expr_const_operands,
3908 DEPRECATED_NORMAL);
3910 /* Apply attributes. */
3911 decl_attributes (&type, attrs, 0);
3913 return type;
3916 /* Decode a declarator in an ordinary declaration or data definition.
3917 This is called as soon as the type information and variable name
3918 have been parsed, before parsing the initializer if any.
3919 Here we create the ..._DECL node, fill in its type,
3920 and put it on the list of decls for the current context.
3921 The ..._DECL node is returned as the value.
3923 Exception: for arrays where the length is not specified,
3924 the type is left null, to be filled in by `finish_decl'.
3926 Function definitions do not come here; they go to start_function
3927 instead. However, external and forward declarations of functions
3928 do go through here. Structure field declarations are done by
3929 grokfield and not through here. */
3931 tree
3932 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3933 bool initialized, tree attributes)
3935 tree decl;
3936 tree tem;
3937 tree expr = NULL_TREE;
3938 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3940 /* An object declared as __attribute__((deprecated)) suppresses
3941 warnings of uses of other deprecated items. */
3942 if (lookup_attribute ("deprecated", attributes))
3943 deprecated_state = DEPRECATED_SUPPRESS;
3945 decl = grokdeclarator (declarator, declspecs,
3946 NORMAL, initialized, NULL, &attributes, &expr, NULL,
3947 deprecated_state);
3948 if (!decl)
3949 return 0;
3951 if (expr)
3952 add_stmt (fold_convert (void_type_node, expr));
3954 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
3955 warning (OPT_Wmain, "%q+D is usually a function", decl);
3957 if (initialized)
3958 /* Is it valid for this decl to have an initializer at all?
3959 If not, set INITIALIZED to zero, which will indirectly
3960 tell 'finish_decl' to ignore the initializer once it is parsed. */
3961 switch (TREE_CODE (decl))
3963 case TYPE_DECL:
3964 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3965 initialized = 0;
3966 break;
3968 case FUNCTION_DECL:
3969 error ("function %qD is initialized like a variable", decl);
3970 initialized = 0;
3971 break;
3973 case PARM_DECL:
3974 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3975 error ("parameter %qD is initialized", decl);
3976 initialized = 0;
3977 break;
3979 default:
3980 /* Don't allow initializations for incomplete types except for
3981 arrays which might be completed by the initialization. */
3983 /* This can happen if the array size is an undefined macro.
3984 We already gave a warning, so we don't need another one. */
3985 if (TREE_TYPE (decl) == error_mark_node)
3986 initialized = 0;
3987 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3989 /* A complete type is ok if size is fixed. */
3991 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3992 || C_DECL_VARIABLE_SIZE (decl))
3994 error ("variable-sized object may not be initialized");
3995 initialized = 0;
3998 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4000 error ("variable %qD has initializer but incomplete type", decl);
4001 initialized = 0;
4003 else if (C_DECL_VARIABLE_SIZE (decl))
4005 /* Although C99 is unclear about whether incomplete arrays
4006 of VLAs themselves count as VLAs, it does not make
4007 sense to permit them to be initialized given that
4008 ordinary VLAs may not be initialized. */
4009 error ("variable-sized object may not be initialized");
4010 initialized = 0;
4014 if (initialized)
4016 if (current_scope == file_scope)
4017 TREE_STATIC (decl) = 1;
4019 /* Tell 'pushdecl' this is an initialized decl
4020 even though we don't yet have the initializer expression.
4021 Also tell 'finish_decl' it may store the real initializer. */
4022 DECL_INITIAL (decl) = error_mark_node;
4025 /* If this is a function declaration, write a record describing it to the
4026 prototypes file (if requested). */
4028 if (TREE_CODE (decl) == FUNCTION_DECL)
4029 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4031 /* ANSI specifies that a tentative definition which is not merged with
4032 a non-tentative definition behaves exactly like a definition with an
4033 initializer equal to zero. (Section 3.7.2)
4035 -fno-common gives strict ANSI behavior, though this tends to break
4036 a large body of code that grew up without this rule.
4038 Thread-local variables are never common, since there's no entrenched
4039 body of code to break, and it allows more efficient variable references
4040 in the presence of dynamic linking. */
4042 if (TREE_CODE (decl) == VAR_DECL
4043 && !initialized
4044 && TREE_PUBLIC (decl)
4045 && !DECL_THREAD_LOCAL_P (decl)
4046 && !flag_no_common)
4047 DECL_COMMON (decl) = 1;
4049 /* Set attributes here so if duplicate decl, will have proper attributes. */
4050 decl_attributes (&decl, attributes, 0);
4052 /* Handle gnu_inline attribute. */
4053 if (declspecs->inline_p
4054 && !flag_gnu89_inline
4055 && TREE_CODE (decl) == FUNCTION_DECL
4056 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4057 || current_function_decl))
4059 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4061 else if (declspecs->storage_class != csc_static)
4062 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4065 if (TREE_CODE (decl) == FUNCTION_DECL
4066 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4068 struct c_declarator *ce = declarator;
4070 if (ce->kind == cdk_pointer)
4071 ce = declarator->declarator;
4072 if (ce->kind == cdk_function)
4074 tree args = ce->u.arg_info->parms;
4075 for (; args; args = DECL_CHAIN (args))
4077 tree type = TREE_TYPE (args);
4078 if (type && INTEGRAL_TYPE_P (type)
4079 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4080 DECL_ARG_TYPE (args) = integer_type_node;
4085 if (TREE_CODE (decl) == FUNCTION_DECL
4086 && DECL_DECLARED_INLINE_P (decl)
4087 && DECL_UNINLINABLE (decl)
4088 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4089 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4090 decl);
4092 /* C99 6.7.4p3: An inline definition of a function with external
4093 linkage shall not contain a definition of a modifiable object
4094 with static storage duration... */
4095 if (TREE_CODE (decl) == VAR_DECL
4096 && current_scope != file_scope
4097 && TREE_STATIC (decl)
4098 && !TREE_READONLY (decl)
4099 && DECL_DECLARED_INLINE_P (current_function_decl)
4100 && DECL_EXTERNAL (current_function_decl))
4101 record_inline_static (input_location, current_function_decl,
4102 decl, csi_modifiable);
4104 if (c_dialect_objc ()
4105 && (TREE_CODE (decl) == VAR_DECL
4106 || TREE_CODE (decl) == FUNCTION_DECL))
4107 objc_check_global_decl (decl);
4109 /* Add this decl to the current scope.
4110 TEM may equal DECL or it may be a previous decl of the same name. */
4111 tem = pushdecl (decl);
4113 if (initialized && DECL_EXTERNAL (tem))
4115 DECL_EXTERNAL (tem) = 0;
4116 TREE_STATIC (tem) = 1;
4119 return tem;
4122 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4123 DECL or the non-array element type if DECL is an uninitialized array.
4124 If that type has a const member, diagnose this. */
4126 static void
4127 diagnose_uninitialized_cst_member (tree decl, tree type)
4129 tree field;
4130 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4132 tree field_type;
4133 if (TREE_CODE (field) != FIELD_DECL)
4134 continue;
4135 field_type = strip_array_types (TREE_TYPE (field));
4137 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4139 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4140 "uninitialized const member in %qT is invalid in C++",
4141 strip_array_types (TREE_TYPE (decl)));
4142 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4145 if (TREE_CODE (field_type) == RECORD_TYPE
4146 || TREE_CODE (field_type) == UNION_TYPE)
4147 diagnose_uninitialized_cst_member (decl, field_type);
4151 /* Finish processing of a declaration;
4152 install its initial value.
4153 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4154 If the length of an array type is not known before,
4155 it must be determined now, from the initial value, or it is an error.
4157 INIT_LOC is the location of the initial value. */
4159 void
4160 finish_decl (tree decl, location_t init_loc, tree init,
4161 tree origtype, tree asmspec_tree)
4163 tree type;
4164 bool was_incomplete = (DECL_SIZE (decl) == 0);
4165 const char *asmspec = 0;
4167 /* If a name was specified, get the string. */
4168 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4169 && DECL_FILE_SCOPE_P (decl))
4170 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4171 if (asmspec_tree)
4172 asmspec = TREE_STRING_POINTER (asmspec_tree);
4174 if (TREE_CODE (decl) == VAR_DECL
4175 && TREE_STATIC (decl)
4176 && global_bindings_p ())
4177 /* So decl is a global variable. Record the types it uses
4178 so that we can decide later to emit debug info for them. */
4179 record_types_used_by_current_var_decl (decl);
4181 /* If `start_decl' didn't like having an initialization, ignore it now. */
4182 if (init != 0 && DECL_INITIAL (decl) == 0)
4183 init = 0;
4185 /* Don't crash if parm is initialized. */
4186 if (TREE_CODE (decl) == PARM_DECL)
4187 init = 0;
4189 if (init)
4190 store_init_value (init_loc, decl, init, origtype);
4192 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
4193 || TREE_CODE (decl) == FUNCTION_DECL
4194 || TREE_CODE (decl) == FIELD_DECL))
4195 objc_check_decl (decl);
4197 type = TREE_TYPE (decl);
4199 /* Deduce size of array from initialization, if not already known. */
4200 if (TREE_CODE (type) == ARRAY_TYPE
4201 && TYPE_DOMAIN (type) == 0
4202 && TREE_CODE (decl) != TYPE_DECL)
4204 bool do_default
4205 = (TREE_STATIC (decl)
4206 /* Even if pedantic, an external linkage array
4207 may have incomplete type at first. */
4208 ? pedantic && !TREE_PUBLIC (decl)
4209 : !DECL_EXTERNAL (decl));
4210 int failure
4211 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4212 do_default);
4214 /* Get the completed type made by complete_array_type. */
4215 type = TREE_TYPE (decl);
4217 switch (failure)
4219 case 1:
4220 error ("initializer fails to determine size of %q+D", decl);
4221 break;
4223 case 2:
4224 if (do_default)
4225 error ("array size missing in %q+D", decl);
4226 /* If a `static' var's size isn't known,
4227 make it extern as well as static, so it does not get
4228 allocated.
4229 If it is not `static', then do not mark extern;
4230 finish_incomplete_decl will give it a default size
4231 and it will get allocated. */
4232 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4233 DECL_EXTERNAL (decl) = 1;
4234 break;
4236 case 3:
4237 error ("zero or negative size array %q+D", decl);
4238 break;
4240 case 0:
4241 /* For global variables, update the copy of the type that
4242 exists in the binding. */
4243 if (TREE_PUBLIC (decl))
4245 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4246 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4247 b_ext = b_ext->shadowed;
4248 if (b_ext)
4250 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4251 b_ext->u.type = composite_type (b_ext->u.type, type);
4252 else
4253 b_ext->u.type = type;
4256 break;
4258 default:
4259 gcc_unreachable ();
4262 if (DECL_INITIAL (decl))
4263 TREE_TYPE (DECL_INITIAL (decl)) = type;
4265 layout_decl (decl, 0);
4268 if (TREE_CODE (decl) == VAR_DECL)
4270 if (init && TREE_CODE (init) == CONSTRUCTOR)
4271 add_flexible_array_elts_to_size (decl, init);
4273 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4274 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4275 layout_decl (decl, 0);
4277 if (DECL_SIZE (decl) == 0
4278 /* Don't give an error if we already gave one earlier. */
4279 && TREE_TYPE (decl) != error_mark_node
4280 && (TREE_STATIC (decl)
4281 /* A static variable with an incomplete type
4282 is an error if it is initialized.
4283 Also if it is not file scope.
4284 Otherwise, let it through, but if it is not `extern'
4285 then it may cause an error message later. */
4286 ? (DECL_INITIAL (decl) != 0
4287 || !DECL_FILE_SCOPE_P (decl))
4288 /* An automatic variable with an incomplete type
4289 is an error. */
4290 : !DECL_EXTERNAL (decl)))
4292 error ("storage size of %q+D isn%'t known", decl);
4293 TREE_TYPE (decl) = error_mark_node;
4296 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4297 && DECL_SIZE (decl) != 0)
4299 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4300 constant_expression_warning (DECL_SIZE (decl));
4301 else
4303 error ("storage size of %q+D isn%'t constant", decl);
4304 TREE_TYPE (decl) = error_mark_node;
4308 if (TREE_USED (type))
4310 TREE_USED (decl) = 1;
4311 DECL_READ_P (decl) = 1;
4315 /* If this is a function and an assembler name is specified, reset DECL_RTL
4316 so we can give it its new name. Also, update built_in_decls if it
4317 was a normal built-in. */
4318 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4320 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4321 set_builtin_user_assembler_name (decl, asmspec);
4322 set_user_assembler_name (decl, asmspec);
4325 /* If #pragma weak was used, mark the decl weak now. */
4326 maybe_apply_pragma_weak (decl);
4328 /* Output the assembler code and/or RTL code for variables and functions,
4329 unless the type is an undefined structure or union.
4330 If not, it will get done when the type is completed. */
4332 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
4334 /* Determine the ELF visibility. */
4335 if (TREE_PUBLIC (decl))
4336 c_determine_visibility (decl);
4338 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4339 if (c_dialect_objc ())
4340 objc_check_decl (decl);
4342 if (asmspec)
4344 /* If this is not a static variable, issue a warning.
4345 It doesn't make any sense to give an ASMSPEC for an
4346 ordinary, non-register local variable. Historically,
4347 GCC has accepted -- but ignored -- the ASMSPEC in
4348 this case. */
4349 if (!DECL_FILE_SCOPE_P (decl)
4350 && TREE_CODE (decl) == VAR_DECL
4351 && !C_DECL_REGISTER (decl)
4352 && !TREE_STATIC (decl))
4353 warning (0, "ignoring asm-specifier for non-static local "
4354 "variable %q+D", decl);
4355 else
4356 set_user_assembler_name (decl, asmspec);
4359 if (DECL_FILE_SCOPE_P (decl))
4361 if (DECL_INITIAL (decl) == NULL_TREE
4362 || DECL_INITIAL (decl) == error_mark_node)
4363 /* Don't output anything
4364 when a tentative file-scope definition is seen.
4365 But at end of compilation, do output code for them. */
4366 DECL_DEFER_OUTPUT (decl) = 1;
4367 if (asmspec && C_DECL_REGISTER (decl))
4368 DECL_HARD_REGISTER (decl) = 1;
4369 rest_of_decl_compilation (decl, true, 0);
4371 else
4373 /* In conjunction with an ASMSPEC, the `register'
4374 keyword indicates that we should place the variable
4375 in a particular register. */
4376 if (asmspec && C_DECL_REGISTER (decl))
4378 DECL_HARD_REGISTER (decl) = 1;
4379 /* This cannot be done for a structure with volatile
4380 fields, on which DECL_REGISTER will have been
4381 reset. */
4382 if (!DECL_REGISTER (decl))
4383 error ("cannot put object with volatile field into register");
4386 if (TREE_CODE (decl) != FUNCTION_DECL)
4388 /* If we're building a variable sized type, and we might be
4389 reachable other than via the top of the current binding
4390 level, then create a new BIND_EXPR so that we deallocate
4391 the object at the right time. */
4392 /* Note that DECL_SIZE can be null due to errors. */
4393 if (DECL_SIZE (decl)
4394 && !TREE_CONSTANT (DECL_SIZE (decl))
4395 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4397 tree bind;
4398 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4399 TREE_SIDE_EFFECTS (bind) = 1;
4400 add_stmt (bind);
4401 BIND_EXPR_BODY (bind) = push_stmt_list ();
4403 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4404 DECL_EXPR, decl));
4409 if (!DECL_FILE_SCOPE_P (decl))
4411 /* Recompute the RTL of a local array now
4412 if it used to be an incomplete type. */
4413 if (was_incomplete
4414 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4416 /* If we used it already as memory, it must stay in memory. */
4417 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4418 /* If it's still incomplete now, no init will save it. */
4419 if (DECL_SIZE (decl) == 0)
4420 DECL_INITIAL (decl) = 0;
4425 if (TREE_CODE (decl) == TYPE_DECL)
4427 if (!DECL_FILE_SCOPE_P (decl)
4428 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4429 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4431 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4434 /* Install a cleanup (aka destructor) if one was given. */
4435 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4437 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4438 if (attr)
4440 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4441 tree cleanup_decl = lookup_name (cleanup_id);
4442 tree cleanup;
4443 VEC(tree,gc) *vec;
4445 /* Build "cleanup(&decl)" for the destructor. */
4446 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
4447 vec = VEC_alloc (tree, gc, 1);
4448 VEC_quick_push (tree, vec, cleanup);
4449 cleanup = build_function_call_vec (DECL_SOURCE_LOCATION (decl),
4450 cleanup_decl, vec, NULL);
4451 VEC_free (tree, gc, vec);
4453 /* Don't warn about decl unused; the cleanup uses it. */
4454 TREE_USED (decl) = 1;
4455 TREE_USED (cleanup_decl) = 1;
4456 DECL_READ_P (decl) = 1;
4458 push_cleanup (decl, cleanup, false);
4462 if (warn_cxx_compat
4463 && TREE_CODE (decl) == VAR_DECL
4464 && !DECL_EXTERNAL (decl)
4465 && DECL_INITIAL (decl) == NULL_TREE)
4467 type = strip_array_types (type);
4468 if (TREE_READONLY (decl))
4469 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4470 "uninitialized const %qD is invalid in C++", decl);
4471 else if ((TREE_CODE (type) == RECORD_TYPE
4472 || TREE_CODE (type) == UNION_TYPE)
4473 && C_TYPE_FIELDS_READONLY (type))
4474 diagnose_uninitialized_cst_member (decl, type);
4477 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
4480 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
4481 EXPR is NULL or a pointer to an expression that needs to be
4482 evaluated for the side effects of array size expressions in the
4483 parameters. */
4485 tree
4486 grokparm (const struct c_parm *parm, tree *expr)
4488 tree attrs = parm->attrs;
4489 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4490 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
4492 decl_attributes (&decl, attrs, 0);
4494 return decl;
4497 /* Given a parsed parameter declaration, decode it into a PARM_DECL
4498 and push that on the current scope. EXPR is a pointer to an
4499 expression that needs to be evaluated for the side effects of array
4500 size expressions in the parameters. */
4502 void
4503 push_parm_decl (const struct c_parm *parm, tree *expr)
4505 tree attrs = parm->attrs;
4506 tree decl;
4508 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
4509 &attrs, expr, NULL, DEPRECATED_NORMAL);
4510 decl_attributes (&decl, attrs, 0);
4512 decl = pushdecl (decl);
4514 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
4517 /* Mark all the parameter declarations to date as forward decls.
4518 Also diagnose use of this extension. */
4520 void
4521 mark_forward_parm_decls (void)
4523 struct c_binding *b;
4525 if (pedantic && !current_scope->warned_forward_parm_decls)
4527 pedwarn (input_location, OPT_pedantic,
4528 "ISO C forbids forward parameter declarations");
4529 current_scope->warned_forward_parm_decls = true;
4532 for (b = current_scope->bindings; b; b = b->prev)
4533 if (TREE_CODE (b->decl) == PARM_DECL)
4534 TREE_ASM_WRITTEN (b->decl) = 1;
4537 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
4538 literal, which may be an incomplete array type completed by the
4539 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
4540 literal. NON_CONST is true if the initializers contain something
4541 that cannot occur in a constant expression. */
4543 tree
4544 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
4546 /* We do not use start_decl here because we have a type, not a declarator;
4547 and do not use finish_decl because the decl should be stored inside
4548 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
4549 tree decl;
4550 tree complit;
4551 tree stmt;
4553 if (type == error_mark_node
4554 || init == error_mark_node)
4555 return error_mark_node;
4557 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
4558 DECL_EXTERNAL (decl) = 0;
4559 TREE_PUBLIC (decl) = 0;
4560 TREE_STATIC (decl) = (current_scope == file_scope);
4561 DECL_CONTEXT (decl) = current_function_decl;
4562 TREE_USED (decl) = 1;
4563 DECL_READ_P (decl) = 1;
4564 TREE_TYPE (decl) = type;
4565 TREE_READONLY (decl) = TYPE_READONLY (type);
4566 store_init_value (loc, decl, init, NULL_TREE);
4568 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
4570 int failure = complete_array_type (&TREE_TYPE (decl),
4571 DECL_INITIAL (decl), true);
4572 gcc_assert (!failure);
4574 type = TREE_TYPE (decl);
4575 TREE_TYPE (DECL_INITIAL (decl)) = type;
4578 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4579 return error_mark_node;
4581 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
4582 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
4583 TREE_SIDE_EFFECTS (complit) = 1;
4585 layout_decl (decl, 0);
4587 if (TREE_STATIC (decl))
4589 /* This decl needs a name for the assembler output. */
4590 set_compound_literal_name (decl);
4591 DECL_DEFER_OUTPUT (decl) = 1;
4592 DECL_COMDAT (decl) = 1;
4593 DECL_ARTIFICIAL (decl) = 1;
4594 DECL_IGNORED_P (decl) = 1;
4595 pushdecl (decl);
4596 rest_of_decl_compilation (decl, 1, 0);
4599 if (non_const)
4601 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
4602 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
4605 return complit;
4608 /* Check the type of a compound literal. Here we just check that it
4609 is valid for C++. */
4611 void
4612 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
4614 if (warn_cxx_compat
4615 && (type_name->specs->typespec_kind == ctsk_tagdef
4616 || type_name->specs->typespec_kind == ctsk_tagfirstref))
4617 warning_at (loc, OPT_Wc___compat,
4618 "defining a type in a compound literal is invalid in C++");
4621 /* Determine whether TYPE is a structure with a flexible array member,
4622 or a union containing such a structure (possibly recursively). */
4624 static bool
4625 flexible_array_type_p (tree type)
4627 tree x;
4628 switch (TREE_CODE (type))
4630 case RECORD_TYPE:
4631 x = TYPE_FIELDS (type);
4632 if (x == NULL_TREE)
4633 return false;
4634 while (DECL_CHAIN (x) != NULL_TREE)
4635 x = DECL_CHAIN (x);
4636 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4637 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4638 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
4639 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
4640 return true;
4641 return false;
4642 case UNION_TYPE:
4643 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
4645 if (flexible_array_type_p (TREE_TYPE (x)))
4646 return true;
4648 return false;
4649 default:
4650 return false;
4654 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
4655 replacing with appropriate values if they are invalid. */
4656 static void
4657 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
4659 tree type_mv;
4660 unsigned int max_width;
4661 unsigned HOST_WIDE_INT w;
4662 const char *name = (orig_name
4663 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
4664 : _("<anonymous>"));
4666 /* Detect and ignore out of range field width and process valid
4667 field widths. */
4668 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
4670 error ("bit-field %qs width not an integer constant", name);
4671 *width = integer_one_node;
4673 else
4675 if (TREE_CODE (*width) != INTEGER_CST)
4677 *width = c_fully_fold (*width, false, NULL);
4678 if (TREE_CODE (*width) == INTEGER_CST)
4679 pedwarn (input_location, OPT_pedantic,
4680 "bit-field %qs width not an integer constant expression",
4681 name);
4683 if (TREE_CODE (*width) != INTEGER_CST)
4685 error ("bit-field %qs width not an integer constant", name);
4686 *width = integer_one_node;
4688 constant_expression_warning (*width);
4689 if (tree_int_cst_sgn (*width) < 0)
4691 error ("negative width in bit-field %qs", name);
4692 *width = integer_one_node;
4694 else if (integer_zerop (*width) && orig_name)
4696 error ("zero width for bit-field %qs", name);
4697 *width = integer_one_node;
4701 /* Detect invalid bit-field type. */
4702 if (TREE_CODE (*type) != INTEGER_TYPE
4703 && TREE_CODE (*type) != BOOLEAN_TYPE
4704 && TREE_CODE (*type) != ENUMERAL_TYPE)
4706 error ("bit-field %qs has invalid type", name);
4707 *type = unsigned_type_node;
4710 type_mv = TYPE_MAIN_VARIANT (*type);
4711 if (!in_system_header
4712 && type_mv != integer_type_node
4713 && type_mv != unsigned_type_node
4714 && type_mv != boolean_type_node)
4715 pedwarn (input_location, OPT_pedantic,
4716 "type of bit-field %qs is a GCC extension", name);
4718 max_width = TYPE_PRECISION (*type);
4720 if (0 < compare_tree_int (*width, max_width))
4722 error ("width of %qs exceeds its type", name);
4723 w = max_width;
4724 *width = build_int_cst (integer_type_node, w);
4726 else
4727 w = tree_low_cst (*width, 1);
4729 if (TREE_CODE (*type) == ENUMERAL_TYPE)
4731 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
4732 if (!lt
4733 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
4734 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
4735 warning (0, "%qs is narrower than values of its type", name);
4741 /* Print warning about variable length array if necessary. */
4743 static void
4744 warn_variable_length_array (tree name, tree size)
4746 int const_size = TREE_CONSTANT (size);
4748 if (!flag_isoc99 && pedantic && warn_vla != 0)
4750 if (const_size)
4752 if (name)
4753 pedwarn (input_location, OPT_Wvla,
4754 "ISO C90 forbids array %qE whose size "
4755 "can%'t be evaluated",
4756 name);
4757 else
4758 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
4759 "can%'t be evaluated");
4761 else
4763 if (name)
4764 pedwarn (input_location, OPT_Wvla,
4765 "ISO C90 forbids variable length array %qE",
4766 name);
4767 else
4768 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
4771 else if (warn_vla > 0)
4773 if (const_size)
4775 if (name)
4776 warning (OPT_Wvla,
4777 "the size of array %qE can"
4778 "%'t be evaluated", name);
4779 else
4780 warning (OPT_Wvla,
4781 "the size of array can %'t be evaluated");
4783 else
4785 if (name)
4786 warning (OPT_Wvla,
4787 "variable length array %qE is used",
4788 name);
4789 else
4790 warning (OPT_Wvla,
4791 "variable length array is used");
4796 /* Given declspecs and a declarator,
4797 determine the name and type of the object declared
4798 and construct a ..._DECL node for it.
4799 (In one case we can return a ..._TYPE node instead.
4800 For invalid input we sometimes return 0.)
4802 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4804 DECL_CONTEXT says which syntactic context this declaration is in:
4805 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4806 FUNCDEF for a function definition. Like NORMAL but a few different
4807 error messages in each case. Return value may be zero meaning
4808 this definition is too screwy to try to parse.
4809 PARM for a parameter declaration (either within a function prototype
4810 or before a function body). Make a PARM_DECL, or return void_type_node.
4811 TYPENAME if for a typename (in a cast or sizeof).
4812 Don't make a DECL node; just return the ..._TYPE node.
4813 FIELD for a struct or union field; make a FIELD_DECL.
4814 INITIALIZED is true if the decl has an initializer.
4815 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4816 representing the width of the bit-field.
4817 DECL_ATTRS points to the list of attributes that should be added to this
4818 decl. Any nested attributes that belong on the decl itself will be
4819 added to this list.
4820 If EXPR is not NULL, any expressions that need to be evaluated as
4821 part of evaluating variably modified types will be stored in *EXPR.
4822 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4823 set to indicate whether operands in *EXPR can be used in constant
4824 expressions.
4825 DEPRECATED_STATE is a deprecated_states value indicating whether
4826 deprecation warnings should be suppressed.
4828 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4829 It may also be so in the PARM case, for a prototype where the
4830 argument type is specified but not the name.
4832 This function is where the complicated C meanings of `static'
4833 and `extern' are interpreted. */
4835 static tree
4836 grokdeclarator (const struct c_declarator *declarator,
4837 struct c_declspecs *declspecs,
4838 enum decl_context decl_context, bool initialized, tree *width,
4839 tree *decl_attrs, tree *expr, bool *expr_const_operands,
4840 enum deprecated_states deprecated_state)
4842 tree type = declspecs->type;
4843 bool threadp = declspecs->thread_p;
4844 enum c_storage_class storage_class = declspecs->storage_class;
4845 int constp;
4846 int restrictp;
4847 int volatilep;
4848 int type_quals = TYPE_UNQUALIFIED;
4849 tree name = NULL_TREE;
4850 bool funcdef_flag = false;
4851 bool funcdef_syntax = false;
4852 bool size_varies = false;
4853 tree decl_attr = declspecs->decl_attr;
4854 int array_ptr_quals = TYPE_UNQUALIFIED;
4855 tree array_ptr_attrs = NULL_TREE;
4856 int array_parm_static = 0;
4857 bool array_parm_vla_unspec_p = false;
4858 tree returned_attrs = NULL_TREE;
4859 bool bitfield = width != NULL;
4860 tree element_type;
4861 struct c_arg_info *arg_info = 0;
4862 addr_space_t as1, as2, address_space;
4863 location_t loc = UNKNOWN_LOCATION;
4864 const char *errmsg;
4865 tree expr_dummy;
4866 bool expr_const_operands_dummy;
4867 enum c_declarator_kind first_non_attr_kind;
4869 if (TREE_CODE (type) == ERROR_MARK)
4870 return error_mark_node;
4871 if (expr == NULL)
4872 expr = &expr_dummy;
4873 if (expr_const_operands == NULL)
4874 expr_const_operands = &expr_const_operands_dummy;
4876 *expr = declspecs->expr;
4877 *expr_const_operands = declspecs->expr_const_operands;
4879 if (decl_context == FUNCDEF)
4880 funcdef_flag = true, decl_context = NORMAL;
4882 /* Look inside a declarator for the name being declared
4883 and get it as an IDENTIFIER_NODE, for an error message. */
4885 const struct c_declarator *decl = declarator;
4887 first_non_attr_kind = cdk_attrs;
4888 while (decl)
4889 switch (decl->kind)
4891 case cdk_array:
4892 loc = decl->id_loc;
4893 /* FALL THRU. */
4895 case cdk_function:
4896 case cdk_pointer:
4897 funcdef_syntax = (decl->kind == cdk_function);
4898 decl = decl->declarator;
4899 if (first_non_attr_kind == cdk_attrs)
4900 first_non_attr_kind = decl->kind;
4901 break;
4903 case cdk_attrs:
4904 decl = decl->declarator;
4905 break;
4907 case cdk_id:
4908 loc = decl->id_loc;
4909 if (decl->u.id)
4910 name = decl->u.id;
4911 if (first_non_attr_kind == cdk_attrs)
4912 first_non_attr_kind = decl->kind;
4913 decl = 0;
4914 break;
4916 default:
4917 gcc_unreachable ();
4919 if (name == 0)
4921 gcc_assert (decl_context == PARM
4922 || decl_context == TYPENAME
4923 || (decl_context == FIELD
4924 && declarator->kind == cdk_id));
4925 gcc_assert (!initialized);
4929 /* A function definition's declarator must have the form of
4930 a function declarator. */
4932 if (funcdef_flag && !funcdef_syntax)
4933 return 0;
4935 /* If this looks like a function definition, make it one,
4936 even if it occurs where parms are expected.
4937 Then store_parm_decls will reject it and not use it as a parm. */
4938 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4939 decl_context = PARM;
4941 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4942 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
4944 if ((decl_context == NORMAL || decl_context == FIELD)
4945 && current_scope == file_scope
4946 && variably_modified_type_p (type, NULL_TREE))
4948 if (name)
4949 error_at (loc, "variably modified %qE at file scope", name);
4950 else
4951 error_at (loc, "variably modified field at file scope");
4952 type = integer_type_node;
4955 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
4957 /* Diagnose defaulting to "int". */
4959 if (declspecs->default_int_p && !in_system_header)
4961 /* Issue a warning if this is an ISO C 99 program or if
4962 -Wreturn-type and this is a function, or if -Wimplicit;
4963 prefer the former warning since it is more explicit. */
4964 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4965 && funcdef_flag)
4966 warn_about_return_type = 1;
4967 else
4969 if (name)
4970 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4971 "type defaults to %<int%> in declaration of %qE",
4972 name);
4973 else
4974 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
4975 "type defaults to %<int%> in type name");
4979 /* Adjust the type if a bit-field is being declared,
4980 -funsigned-bitfields applied and the type is not explicitly
4981 "signed". */
4982 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4983 && TREE_CODE (type) == INTEGER_TYPE)
4984 type = unsigned_type_for (type);
4986 /* Figure out the type qualifiers for the declaration. There are
4987 two ways a declaration can become qualified. One is something
4988 like `const int i' where the `const' is explicit. Another is
4989 something like `typedef const int CI; CI i' where the type of the
4990 declaration contains the `const'. A third possibility is that
4991 there is a type qualifier on the element type of a typedefed
4992 array type, in which case we should extract that qualifier so
4993 that c_apply_type_quals_to_decl receives the full list of
4994 qualifiers to work with (C90 is not entirely clear about whether
4995 duplicate qualifiers should be diagnosed in this case, but it
4996 seems most appropriate to do so). */
4997 element_type = strip_array_types (type);
4998 constp = declspecs->const_p + TYPE_READONLY (element_type);
4999 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5000 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5001 as1 = declspecs->address_space;
5002 as2 = TYPE_ADDR_SPACE (element_type);
5003 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5005 if (pedantic && !flag_isoc99)
5007 if (constp > 1)
5008 pedwarn (loc, OPT_pedantic, "duplicate %<const%>");
5009 if (restrictp > 1)
5010 pedwarn (loc, OPT_pedantic, "duplicate %<restrict%>");
5011 if (volatilep > 1)
5012 pedwarn (loc, OPT_pedantic, "duplicate %<volatile%>");
5015 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5016 error_at (loc, "conflicting named address spaces (%s vs %s)",
5017 c_addr_space_name (as1), c_addr_space_name (as2));
5019 if ((TREE_CODE (type) == ARRAY_TYPE
5020 || first_non_attr_kind == cdk_array)
5021 && TYPE_QUALS (element_type))
5022 type = TYPE_MAIN_VARIANT (type);
5023 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5024 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5025 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5026 | ENCODE_QUAL_ADDR_SPACE (address_space));
5028 /* Warn about storage classes that are invalid for certain
5029 kinds of declarations (parameters, typenames, etc.). */
5031 if (funcdef_flag
5032 && (threadp
5033 || storage_class == csc_auto
5034 || storage_class == csc_register
5035 || storage_class == csc_typedef))
5037 if (storage_class == csc_auto)
5038 pedwarn (loc,
5039 (current_scope == file_scope) ? 0 : OPT_pedantic,
5040 "function definition declared %<auto%>");
5041 if (storage_class == csc_register)
5042 error_at (loc, "function definition declared %<register%>");
5043 if (storage_class == csc_typedef)
5044 error_at (loc, "function definition declared %<typedef%>");
5045 if (threadp)
5046 error_at (loc, "function definition declared %<__thread%>");
5047 threadp = false;
5048 if (storage_class == csc_auto
5049 || storage_class == csc_register
5050 || storage_class == csc_typedef)
5051 storage_class = csc_none;
5053 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5055 if (decl_context == PARM && storage_class == csc_register)
5057 else
5059 switch (decl_context)
5061 case FIELD:
5062 if (name)
5063 error_at (loc, "storage class specified for structure "
5064 "field %qE", name);
5065 else
5066 error_at (loc, "storage class specified for structure field");
5067 break;
5068 case PARM:
5069 if (name)
5070 error_at (loc, "storage class specified for parameter %qE",
5071 name);
5072 else
5073 error_at (loc, "storage class specified for unnamed parameter");
5074 break;
5075 default:
5076 error_at (loc, "storage class specified for typename");
5077 break;
5079 storage_class = csc_none;
5080 threadp = false;
5083 else if (storage_class == csc_extern
5084 && initialized
5085 && !funcdef_flag)
5087 /* 'extern' with initialization is invalid if not at file scope. */
5088 if (current_scope == file_scope)
5090 /* It is fine to have 'extern const' when compiling at C
5091 and C++ intersection. */
5092 if (!(warn_cxx_compat && constp))
5093 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5094 name);
5096 else
5097 error_at (loc, "%qE has both %<extern%> and initializer", name);
5099 else if (current_scope == file_scope)
5101 if (storage_class == csc_auto)
5102 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5103 name);
5104 if (pedantic && storage_class == csc_register)
5105 pedwarn (input_location, OPT_pedantic,
5106 "file-scope declaration of %qE specifies %<register%>", name);
5108 else
5110 if (storage_class == csc_extern && funcdef_flag)
5111 error_at (loc, "nested function %qE declared %<extern%>", name);
5112 else if (threadp && storage_class == csc_none)
5114 error_at (loc, "function-scope %qE implicitly auto and declared "
5115 "%<__thread%>",
5116 name);
5117 threadp = false;
5121 /* Now figure out the structure of the declarator proper.
5122 Descend through it, creating more complex types, until we reach
5123 the declared identifier (or NULL_TREE, in an absolute declarator).
5124 At each stage we maintain an unqualified version of the type
5125 together with any qualifiers that should be applied to it with
5126 c_build_qualified_type; this way, array types including
5127 multidimensional array types are first built up in unqualified
5128 form and then the qualified form is created with
5129 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5131 while (declarator && declarator->kind != cdk_id)
5133 if (type == error_mark_node)
5135 declarator = declarator->declarator;
5136 continue;
5139 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5140 a cdk_pointer (for *...),
5141 a cdk_function (for ...(...)),
5142 a cdk_attrs (for nested attributes),
5143 or a cdk_id (for the name being declared
5144 or the place in an absolute declarator
5145 where the name was omitted).
5146 For the last case, we have just exited the loop.
5148 At this point, TYPE is the type of elements of an array,
5149 or for a function to return, or for a pointer to point to.
5150 After this sequence of ifs, TYPE is the type of the
5151 array or function or pointer, and DECLARATOR has had its
5152 outermost layer removed. */
5154 if (array_ptr_quals != TYPE_UNQUALIFIED
5155 || array_ptr_attrs != NULL_TREE
5156 || array_parm_static)
5158 /* Only the innermost declarator (making a parameter be of
5159 array type which is converted to pointer type)
5160 may have static or type qualifiers. */
5161 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5162 array_ptr_quals = TYPE_UNQUALIFIED;
5163 array_ptr_attrs = NULL_TREE;
5164 array_parm_static = 0;
5167 switch (declarator->kind)
5169 case cdk_attrs:
5171 /* A declarator with embedded attributes. */
5172 tree attrs = declarator->u.attrs;
5173 const struct c_declarator *inner_decl;
5174 int attr_flags = 0;
5175 declarator = declarator->declarator;
5176 inner_decl = declarator;
5177 while (inner_decl->kind == cdk_attrs)
5178 inner_decl = inner_decl->declarator;
5179 if (inner_decl->kind == cdk_id)
5180 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5181 else if (inner_decl->kind == cdk_function)
5182 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5183 else if (inner_decl->kind == cdk_array)
5184 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5185 returned_attrs = decl_attributes (&type,
5186 chainon (returned_attrs, attrs),
5187 attr_flags);
5188 break;
5190 case cdk_array:
5192 tree itype = NULL_TREE;
5193 tree size = declarator->u.array.dimen;
5194 /* The index is a signed object `sizetype' bits wide. */
5195 tree index_type = c_common_signed_type (sizetype);
5197 array_ptr_quals = declarator->u.array.quals;
5198 array_ptr_attrs = declarator->u.array.attrs;
5199 array_parm_static = declarator->u.array.static_p;
5200 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5202 declarator = declarator->declarator;
5204 /* Check for some types that there cannot be arrays of. */
5206 if (VOID_TYPE_P (type))
5208 if (name)
5209 error_at (loc, "declaration of %qE as array of voids", name);
5210 else
5211 error_at (loc, "declaration of type name as array of voids");
5212 type = error_mark_node;
5215 if (TREE_CODE (type) == FUNCTION_TYPE)
5217 if (name)
5218 error_at (loc, "declaration of %qE as array of functions",
5219 name);
5220 else
5221 error_at (loc, "declaration of type name as array of "
5222 "functions");
5223 type = error_mark_node;
5226 if (pedantic && !in_system_header && flexible_array_type_p (type))
5227 pedwarn (loc, OPT_pedantic,
5228 "invalid use of structure with flexible array member");
5230 if (size == error_mark_node)
5231 type = error_mark_node;
5233 if (type == error_mark_node)
5234 continue;
5236 /* If size was specified, set ITYPE to a range-type for
5237 that size. Otherwise, ITYPE remains null. finish_decl
5238 may figure it out from an initial value. */
5240 if (size)
5242 bool size_maybe_const = true;
5243 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5244 && !TREE_OVERFLOW (size));
5245 bool this_size_varies = false;
5247 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5248 lvalue. */
5249 STRIP_TYPE_NOPS (size);
5251 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5253 if (name)
5254 error_at (loc, "size of array %qE has non-integer type",
5255 name);
5256 else
5257 error_at (loc,
5258 "size of unnamed array has non-integer type");
5259 size = integer_one_node;
5262 size = c_fully_fold (size, false, &size_maybe_const);
5264 if (pedantic && size_maybe_const && integer_zerop (size))
5266 if (name)
5267 pedwarn (loc, OPT_pedantic,
5268 "ISO C forbids zero-size array %qE", name);
5269 else
5270 pedwarn (loc, OPT_pedantic,
5271 "ISO C forbids zero-size array");
5274 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5276 constant_expression_warning (size);
5277 if (tree_int_cst_sgn (size) < 0)
5279 if (name)
5280 error_at (loc, "size of array %qE is negative", name);
5281 else
5282 error_at (loc, "size of unnamed array is negative");
5283 size = integer_one_node;
5285 /* Handle a size folded to an integer constant but
5286 not an integer constant expression. */
5287 if (!size_int_const)
5289 /* If this is a file scope declaration of an
5290 ordinary identifier, this is invalid code;
5291 diagnosing it here and not subsequently
5292 treating the type as variable-length avoids
5293 more confusing diagnostics later. */
5294 if ((decl_context == NORMAL || decl_context == FIELD)
5295 && current_scope == file_scope)
5296 pedwarn (input_location, 0,
5297 "variably modified %qE at file scope",
5298 name);
5299 else
5300 this_size_varies = size_varies = true;
5301 warn_variable_length_array (name, size);
5304 else if ((decl_context == NORMAL || decl_context == FIELD)
5305 && current_scope == file_scope)
5307 error_at (loc, "variably modified %qE at file scope", name);
5308 size = integer_one_node;
5310 else
5312 /* Make sure the array size remains visibly
5313 nonconstant even if it is (eg) a const variable
5314 with known value. */
5315 this_size_varies = size_varies = true;
5316 warn_variable_length_array (name, size);
5319 if (integer_zerop (size) && !this_size_varies)
5321 /* A zero-length array cannot be represented with
5322 an unsigned index type, which is what we'll
5323 get with build_index_type. Create an
5324 open-ended range instead. */
5325 itype = build_range_type (sizetype, size, NULL_TREE);
5327 else
5329 /* Arrange for the SAVE_EXPR on the inside of the
5330 MINUS_EXPR, which allows the -1 to get folded
5331 with the +1 that happens when building TYPE_SIZE. */
5332 if (size_varies)
5333 size = save_expr (size);
5334 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5335 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5336 integer_zero_node, size);
5338 /* Compute the maximum valid index, that is, size
5339 - 1. Do the calculation in index_type, so that
5340 if it is a variable the computations will be
5341 done in the proper mode. */
5342 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5343 convert (index_type, size),
5344 convert (index_type,
5345 size_one_node));
5347 /* The above overflows when size does not fit
5348 in index_type.
5349 ??? While a size of INT_MAX+1 technically shouldn't
5350 cause an overflow (because we subtract 1), handling
5351 this case seems like an unnecessary complication. */
5352 if (TREE_CODE (size) == INTEGER_CST
5353 && !int_fits_type_p (size, index_type))
5355 if (name)
5356 error_at (loc, "size of array %qE is too large",
5357 name);
5358 else
5359 error_at (loc, "size of unnamed array is too large");
5360 type = error_mark_node;
5361 continue;
5364 itype = build_index_type (itype);
5366 if (this_size_varies)
5368 if (*expr)
5369 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5370 *expr, size);
5371 else
5372 *expr = size;
5373 *expr_const_operands &= size_maybe_const;
5376 else if (decl_context == FIELD)
5378 bool flexible_array_member = false;
5379 if (array_parm_vla_unspec_p)
5380 /* Field names can in fact have function prototype
5381 scope so [*] is disallowed here through making
5382 the field variably modified, not through being
5383 something other than a declaration with function
5384 prototype scope. */
5385 size_varies = true;
5386 else
5388 const struct c_declarator *t = declarator;
5389 while (t->kind == cdk_attrs)
5390 t = t->declarator;
5391 flexible_array_member = (t->kind == cdk_id);
5393 if (flexible_array_member
5394 && pedantic && !flag_isoc99 && !in_system_header)
5395 pedwarn (loc, OPT_pedantic,
5396 "ISO C90 does not support flexible array members");
5398 /* ISO C99 Flexible array members are effectively
5399 identical to GCC's zero-length array extension. */
5400 if (flexible_array_member || array_parm_vla_unspec_p)
5401 itype = build_range_type (sizetype, size_zero_node,
5402 NULL_TREE);
5404 else if (decl_context == PARM)
5406 if (array_parm_vla_unspec_p)
5408 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
5409 size_varies = true;
5412 else if (decl_context == TYPENAME)
5414 if (array_parm_vla_unspec_p)
5416 /* C99 6.7.5.2p4 */
5417 warning (0, "%<[*]%> not in a declaration");
5418 /* We use this to avoid messing up with incomplete
5419 array types of the same type, that would
5420 otherwise be modified below. */
5421 itype = build_range_type (sizetype, size_zero_node,
5422 NULL_TREE);
5423 size_varies = true;
5427 /* Complain about arrays of incomplete types. */
5428 if (!COMPLETE_TYPE_P (type))
5430 error_at (loc, "array type has incomplete element type");
5431 type = error_mark_node;
5433 else
5434 /* When itype is NULL, a shared incomplete array type is
5435 returned for all array of a given type. Elsewhere we
5436 make sure we don't complete that type before copying
5437 it, but here we want to make sure we don't ever
5438 modify the shared type, so we gcc_assert (itype)
5439 below. */
5441 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
5442 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
5443 type = build_qualified_type (type,
5444 ENCODE_QUAL_ADDR_SPACE (as));
5446 type = build_array_type (type, itype);
5449 if (type != error_mark_node)
5451 if (size_varies)
5453 /* It is ok to modify type here even if itype is
5454 NULL: if size_varies, we're in a
5455 multi-dimensional array and the inner type has
5456 variable size, so the enclosing shared array type
5457 must too. */
5458 if (size && TREE_CODE (size) == INTEGER_CST)
5459 type
5460 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5461 C_TYPE_VARIABLE_SIZE (type) = 1;
5464 /* The GCC extension for zero-length arrays differs from
5465 ISO flexible array members in that sizeof yields
5466 zero. */
5467 if (size && integer_zerop (size))
5469 gcc_assert (itype);
5470 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5471 TYPE_SIZE (type) = bitsize_zero_node;
5472 TYPE_SIZE_UNIT (type) = size_zero_node;
5473 SET_TYPE_STRUCTURAL_EQUALITY (type);
5475 if (array_parm_vla_unspec_p)
5477 gcc_assert (itype);
5478 /* The type is complete. C99 6.7.5.2p4 */
5479 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5480 TYPE_SIZE (type) = bitsize_zero_node;
5481 TYPE_SIZE_UNIT (type) = size_zero_node;
5482 SET_TYPE_STRUCTURAL_EQUALITY (type);
5486 if (decl_context != PARM
5487 && (array_ptr_quals != TYPE_UNQUALIFIED
5488 || array_ptr_attrs != NULL_TREE
5489 || array_parm_static))
5491 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5492 array_ptr_quals = TYPE_UNQUALIFIED;
5493 array_ptr_attrs = NULL_TREE;
5494 array_parm_static = 0;
5496 break;
5498 case cdk_function:
5500 /* Say it's a definition only for the declarator closest
5501 to the identifier, apart possibly from some
5502 attributes. */
5503 bool really_funcdef = false;
5504 tree arg_types;
5505 if (funcdef_flag)
5507 const struct c_declarator *t = declarator->declarator;
5508 while (t->kind == cdk_attrs)
5509 t = t->declarator;
5510 really_funcdef = (t->kind == cdk_id);
5513 /* Declaring a function type. Make sure we have a valid
5514 type for the function to return. */
5515 if (type == error_mark_node)
5516 continue;
5518 size_varies = false;
5520 /* Warn about some types functions can't return. */
5521 if (TREE_CODE (type) == FUNCTION_TYPE)
5523 if (name)
5524 error_at (loc, "%qE declared as function returning a "
5525 "function", name);
5526 else
5527 error_at (loc, "type name declared as function "
5528 "returning a function");
5529 type = integer_type_node;
5531 if (TREE_CODE (type) == ARRAY_TYPE)
5533 if (name)
5534 error_at (loc, "%qE declared as function returning an array",
5535 name);
5536 else
5537 error_at (loc, "type name declared as function returning "
5538 "an array");
5539 type = integer_type_node;
5541 errmsg = targetm.invalid_return_type (type);
5542 if (errmsg)
5544 error (errmsg);
5545 type = integer_type_node;
5548 /* Construct the function type and go to the next
5549 inner layer of declarator. */
5550 arg_info = declarator->u.arg_info;
5551 arg_types = grokparms (arg_info, really_funcdef);
5553 /* Type qualifiers before the return type of the function
5554 qualify the return type, not the function type. */
5555 if (type_quals)
5557 /* Type qualifiers on a function return type are
5558 normally permitted by the standard but have no
5559 effect, so give a warning at -Wreturn-type.
5560 Qualifiers on a void return type are banned on
5561 function definitions in ISO C; GCC used to used
5562 them for noreturn functions. */
5563 if (VOID_TYPE_P (type) && really_funcdef)
5564 pedwarn (loc, 0,
5565 "function definition has qualified void return type");
5566 else
5567 warning_at (loc, OPT_Wignored_qualifiers,
5568 "type qualifiers ignored on function return type");
5570 type = c_build_qualified_type (type, type_quals);
5572 type_quals = TYPE_UNQUALIFIED;
5574 type = build_function_type (type, arg_types);
5575 declarator = declarator->declarator;
5577 /* Set the TYPE_CONTEXTs for each tagged type which is local to
5578 the formal parameter list of this FUNCTION_TYPE to point to
5579 the FUNCTION_TYPE node itself. */
5581 c_arg_tag *tag;
5582 unsigned ix;
5584 FOR_EACH_VEC_ELT_REVERSE (c_arg_tag, arg_info->tags, ix, tag)
5585 TYPE_CONTEXT (tag->type) = type;
5587 break;
5589 case cdk_pointer:
5591 /* Merge any constancy or volatility into the target type
5592 for the pointer. */
5594 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5595 && type_quals)
5596 pedwarn (loc, OPT_pedantic,
5597 "ISO C forbids qualified function types");
5598 if (type_quals)
5599 type = c_build_qualified_type (type, type_quals);
5600 size_varies = false;
5602 /* When the pointed-to type involves components of variable size,
5603 care must be taken to ensure that the size evaluation code is
5604 emitted early enough to dominate all the possible later uses
5605 and late enough for the variables on which it depends to have
5606 been assigned.
5608 This is expected to happen automatically when the pointed-to
5609 type has a name/declaration of it's own, but special attention
5610 is required if the type is anonymous.
5612 We handle the NORMAL and FIELD contexts here by attaching an
5613 artificial TYPE_DECL to such pointed-to type. This forces the
5614 sizes evaluation at a safe point and ensures it is not deferred
5615 until e.g. within a deeper conditional context.
5617 We expect nothing to be needed here for PARM or TYPENAME.
5618 Pushing a TYPE_DECL at this point for TYPENAME would actually
5619 be incorrect, as we might be in the middle of an expression
5620 with side effects on the pointed-to type size "arguments" prior
5621 to the pointer declaration point and the fake TYPE_DECL in the
5622 enclosing context would force the size evaluation prior to the
5623 side effects. */
5625 if (!TYPE_NAME (type)
5626 && (decl_context == NORMAL || decl_context == FIELD)
5627 && variably_modified_type_p (type, NULL_TREE))
5629 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
5630 DECL_ARTIFICIAL (decl) = 1;
5631 pushdecl (decl);
5632 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
5633 TYPE_NAME (type) = decl;
5636 type = build_pointer_type (type);
5638 /* Process type qualifiers (such as const or volatile)
5639 that were given inside the `*'. */
5640 type_quals = declarator->u.pointer_quals;
5642 declarator = declarator->declarator;
5643 break;
5645 default:
5646 gcc_unreachable ();
5649 *decl_attrs = chainon (returned_attrs, *decl_attrs);
5651 /* Now TYPE has the actual type, apart from any qualifiers in
5652 TYPE_QUALS. */
5654 /* Warn about address space used for things other than static memory or
5655 pointers. */
5656 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
5657 if (!ADDR_SPACE_GENERIC_P (address_space))
5659 if (decl_context == NORMAL)
5661 switch (storage_class)
5663 case csc_auto:
5664 error ("%qs combined with %<auto%> qualifier for %qE",
5665 c_addr_space_name (address_space), name);
5666 break;
5667 case csc_register:
5668 error ("%qs combined with %<register%> qualifier for %qE",
5669 c_addr_space_name (address_space), name);
5670 break;
5671 case csc_none:
5672 if (current_function_scope)
5674 error ("%qs specified for auto variable %qE",
5675 c_addr_space_name (address_space), name);
5676 break;
5678 break;
5679 case csc_static:
5680 case csc_extern:
5681 case csc_typedef:
5682 break;
5683 default:
5684 gcc_unreachable ();
5687 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
5689 if (name)
5690 error ("%qs specified for parameter %qE",
5691 c_addr_space_name (address_space), name);
5692 else
5693 error ("%qs specified for unnamed parameter",
5694 c_addr_space_name (address_space));
5696 else if (decl_context == FIELD)
5698 if (name)
5699 error ("%qs specified for structure field %qE",
5700 c_addr_space_name (address_space), name);
5701 else
5702 error ("%qs specified for structure field",
5703 c_addr_space_name (address_space));
5707 /* Check the type and width of a bit-field. */
5708 if (bitfield)
5709 check_bitfield_type_and_width (&type, width, name);
5711 /* Did array size calculations overflow? */
5713 if (TREE_CODE (type) == ARRAY_TYPE
5714 && COMPLETE_TYPE_P (type)
5715 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
5716 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
5718 if (name)
5719 error_at (loc, "size of array %qE is too large", name);
5720 else
5721 error_at (loc, "size of unnamed array is too large");
5722 /* If we proceed with the array type as it is, we'll eventually
5723 crash in tree_low_cst(). */
5724 type = error_mark_node;
5727 /* If this is declaring a typedef name, return a TYPE_DECL. */
5729 if (storage_class == csc_typedef)
5731 tree decl;
5732 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5733 && type_quals)
5734 pedwarn (loc, OPT_pedantic,
5735 "ISO C forbids qualified function types");
5736 if (type_quals)
5737 type = c_build_qualified_type (type, type_quals);
5738 decl = build_decl (declarator->id_loc,
5739 TYPE_DECL, declarator->u.id, type);
5740 if (declspecs->explicit_signed_p)
5741 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
5742 if (declspecs->inline_p)
5743 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
5744 if (declspecs->noreturn_p)
5745 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
5747 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
5749 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
5751 if (b != NULL
5752 && b->decl != NULL_TREE
5753 && (B_IN_CURRENT_SCOPE (b)
5754 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
5755 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
5757 warning_at (declarator->id_loc, OPT_Wc___compat,
5758 ("using %qD as both a typedef and a tag is "
5759 "invalid in C++"),
5760 decl);
5761 if (b->locus != UNKNOWN_LOCATION)
5762 inform (b->locus, "originally defined here");
5766 return decl;
5769 /* If this is a type name (such as, in a cast or sizeof),
5770 compute the type and return it now. */
5772 if (decl_context == TYPENAME)
5774 /* Note that the grammar rejects storage classes in typenames
5775 and fields. */
5776 gcc_assert (storage_class == csc_none && !threadp
5777 && !declspecs->inline_p && !declspecs->noreturn_p);
5778 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5779 && type_quals)
5780 pedwarn (loc, OPT_pedantic,
5781 "ISO C forbids const or volatile function types");
5782 if (type_quals)
5783 type = c_build_qualified_type (type, type_quals);
5784 return type;
5787 if (pedantic && decl_context == FIELD
5788 && variably_modified_type_p (type, NULL_TREE))
5790 /* C99 6.7.2.1p8 */
5791 pedwarn (loc, OPT_pedantic, "a member of a structure or union cannot "
5792 "have a variably modified type");
5795 /* Aside from typedefs and type names (handle above),
5796 `void' at top level (not within pointer)
5797 is allowed only in public variables.
5798 We don't complain about parms either, but that is because
5799 a better error message can be made later. */
5801 if (VOID_TYPE_P (type) && decl_context != PARM
5802 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
5803 && (storage_class == csc_extern
5804 || (current_scope == file_scope
5805 && !(storage_class == csc_static
5806 || storage_class == csc_register)))))
5808 error_at (loc, "variable or field %qE declared void", name);
5809 type = integer_type_node;
5812 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
5813 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
5816 tree decl;
5818 if (decl_context == PARM)
5820 tree promoted_type;
5822 /* A parameter declared as an array of T is really a pointer to T.
5823 One declared as a function is really a pointer to a function. */
5825 if (TREE_CODE (type) == ARRAY_TYPE)
5827 /* Transfer const-ness of array into that of type pointed to. */
5828 type = TREE_TYPE (type);
5829 if (type_quals)
5830 type = c_build_qualified_type (type, type_quals);
5831 type = build_pointer_type (type);
5832 type_quals = array_ptr_quals;
5833 if (type_quals)
5834 type = c_build_qualified_type (type, type_quals);
5836 /* We don't yet implement attributes in this context. */
5837 if (array_ptr_attrs != NULL_TREE)
5838 warning_at (loc, OPT_Wattributes,
5839 "attributes in parameter array declarator ignored");
5841 size_varies = false;
5843 else if (TREE_CODE (type) == FUNCTION_TYPE)
5845 if (type_quals)
5846 pedwarn (loc, OPT_pedantic,
5847 "ISO C forbids qualified function types");
5848 if (type_quals)
5849 type = c_build_qualified_type (type, type_quals);
5850 type = build_pointer_type (type);
5851 type_quals = TYPE_UNQUALIFIED;
5853 else if (type_quals)
5854 type = c_build_qualified_type (type, type_quals);
5856 decl = build_decl (declarator->id_loc,
5857 PARM_DECL, declarator->u.id, type);
5858 if (size_varies)
5859 C_DECL_VARIABLE_SIZE (decl) = 1;
5861 /* Compute the type actually passed in the parmlist,
5862 for the case where there is no prototype.
5863 (For example, shorts and chars are passed as ints.)
5864 When there is a prototype, this is overridden later. */
5866 if (type == error_mark_node)
5867 promoted_type = type;
5868 else
5869 promoted_type = c_type_promotes_to (type);
5871 DECL_ARG_TYPE (decl) = promoted_type;
5872 if (declspecs->inline_p)
5873 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
5874 if (declspecs->noreturn_p)
5875 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
5877 else if (decl_context == FIELD)
5879 /* Note that the grammar rejects storage classes in typenames
5880 and fields. */
5881 gcc_assert (storage_class == csc_none && !threadp
5882 && !declspecs->inline_p && !declspecs->noreturn_p);
5884 /* Structure field. It may not be a function. */
5886 if (TREE_CODE (type) == FUNCTION_TYPE)
5888 error_at (loc, "field %qE declared as a function", name);
5889 type = build_pointer_type (type);
5891 else if (TREE_CODE (type) != ERROR_MARK
5892 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
5894 if (name)
5895 error_at (loc, "field %qE has incomplete type", name);
5896 else
5897 error_at (loc, "unnamed field has incomplete type");
5898 type = error_mark_node;
5900 type = c_build_qualified_type (type, type_quals);
5901 decl = build_decl (declarator->id_loc,
5902 FIELD_DECL, declarator->u.id, type);
5903 DECL_NONADDRESSABLE_P (decl) = bitfield;
5904 if (bitfield && !declarator->u.id)
5905 TREE_NO_WARNING (decl) = 1;
5907 if (size_varies)
5908 C_DECL_VARIABLE_SIZE (decl) = 1;
5910 else if (TREE_CODE (type) == FUNCTION_TYPE)
5912 if (storage_class == csc_register || threadp)
5914 error_at (loc, "invalid storage class for function %qE", name);
5916 else if (current_scope != file_scope)
5918 /* Function declaration not at file scope. Storage
5919 classes other than `extern' are not allowed, C99
5920 6.7.1p5, and `extern' makes no difference. However,
5921 GCC allows 'auto', perhaps with 'inline', to support
5922 nested functions. */
5923 if (storage_class == csc_auto)
5924 pedwarn (loc, OPT_pedantic,
5925 "invalid storage class for function %qE", name);
5926 else if (storage_class == csc_static)
5928 error_at (loc, "invalid storage class for function %qE", name);
5929 if (funcdef_flag)
5930 storage_class = declspecs->storage_class = csc_none;
5931 else
5932 return 0;
5936 decl = build_decl (declarator->id_loc,
5937 FUNCTION_DECL, declarator->u.id, type);
5938 decl = build_decl_attribute_variant (decl, decl_attr);
5940 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
5941 pedwarn (loc, OPT_pedantic,
5942 "ISO C forbids qualified function types");
5944 /* Every function declaration is an external reference
5945 (DECL_EXTERNAL) except for those which are not at file
5946 scope and are explicitly declared "auto". This is
5947 forbidden by standard C (C99 6.7.1p5) and is interpreted by
5948 GCC to signify a forward declaration of a nested function. */
5949 if (storage_class == csc_auto && current_scope != file_scope)
5950 DECL_EXTERNAL (decl) = 0;
5951 /* In C99, a function which is declared 'inline' with 'extern'
5952 is not an external reference (which is confusing). It
5953 means that the later definition of the function must be output
5954 in this file, C99 6.7.4p6. In GNU C89, a function declared
5955 'extern inline' is an external reference. */
5956 else if (declspecs->inline_p && storage_class != csc_static)
5957 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
5958 == flag_gnu89_inline);
5959 else
5960 DECL_EXTERNAL (decl) = !initialized;
5962 /* Record absence of global scope for `static' or `auto'. */
5963 TREE_PUBLIC (decl)
5964 = !(storage_class == csc_static || storage_class == csc_auto);
5966 /* For a function definition, record the argument information
5967 block where store_parm_decls will look for it. */
5968 if (funcdef_flag)
5969 current_function_arg_info = arg_info;
5971 if (declspecs->default_int_p)
5972 C_FUNCTION_IMPLICIT_INT (decl) = 1;
5974 /* Record presence of `inline' and `_Noreturn', if it is
5975 reasonable. */
5976 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
5978 if (declspecs->inline_p)
5979 pedwarn (loc, 0, "cannot inline function %<main%>");
5980 if (declspecs->noreturn_p)
5981 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
5983 else
5985 if (declspecs->inline_p)
5986 /* Record that the function is declared `inline'. */
5987 DECL_DECLARED_INLINE_P (decl) = 1;
5988 if (declspecs->noreturn_p)
5990 if (!flag_isoc1x)
5992 if (flag_isoc99)
5993 pedwarn (loc, OPT_pedantic,
5994 "ISO C99 does not support %<_Noreturn%>");
5995 else
5996 pedwarn (loc, OPT_pedantic,
5997 "ISO C90 does not support %<_Noreturn%>");
5999 TREE_THIS_VOLATILE (decl) = 1;
6003 else
6005 /* It's a variable. */
6006 /* An uninitialized decl with `extern' is a reference. */
6007 int extern_ref = !initialized && storage_class == csc_extern;
6009 type = c_build_qualified_type (type, type_quals);
6011 /* C99 6.2.2p7: It is invalid (compile-time undefined
6012 behavior) to create an 'extern' declaration for a
6013 variable if there is a global declaration that is
6014 'static' and the global declaration is not visible.
6015 (If the static declaration _is_ currently visible,
6016 the 'extern' declaration is taken to refer to that decl.) */
6017 if (extern_ref && current_scope != file_scope)
6019 tree global_decl = identifier_global_value (declarator->u.id);
6020 tree visible_decl = lookup_name (declarator->u.id);
6022 if (global_decl
6023 && global_decl != visible_decl
6024 && TREE_CODE (global_decl) == VAR_DECL
6025 && !TREE_PUBLIC (global_decl))
6026 error_at (loc, "variable previously declared %<static%> "
6027 "redeclared %<extern%>");
6030 decl = build_decl (declarator->id_loc,
6031 VAR_DECL, declarator->u.id, type);
6032 if (size_varies)
6033 C_DECL_VARIABLE_SIZE (decl) = 1;
6035 if (declspecs->inline_p)
6036 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6037 if (declspecs->noreturn_p)
6038 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6040 /* At file scope, an initialized extern declaration may follow
6041 a static declaration. In that case, DECL_EXTERNAL will be
6042 reset later in start_decl. */
6043 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6045 /* At file scope, the presence of a `static' or `register' storage
6046 class specifier, or the absence of all storage class specifiers
6047 makes this declaration a definition (perhaps tentative). Also,
6048 the absence of `static' makes it public. */
6049 if (current_scope == file_scope)
6051 TREE_PUBLIC (decl) = storage_class != csc_static;
6052 TREE_STATIC (decl) = !extern_ref;
6054 /* Not at file scope, only `static' makes a static definition. */
6055 else
6057 TREE_STATIC (decl) = (storage_class == csc_static);
6058 TREE_PUBLIC (decl) = extern_ref;
6061 if (threadp)
6062 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
6065 if ((storage_class == csc_extern
6066 || (storage_class == csc_none
6067 && TREE_CODE (type) == FUNCTION_TYPE
6068 && !funcdef_flag))
6069 && variably_modified_type_p (type, NULL_TREE))
6071 /* C99 6.7.5.2p2 */
6072 if (TREE_CODE (type) == FUNCTION_TYPE)
6073 error_at (loc, "non-nested function with variably modified type");
6074 else
6075 error_at (loc, "object with variably modified type must have "
6076 "no linkage");
6079 /* Record `register' declaration for warnings on &
6080 and in case doing stupid register allocation. */
6082 if (storage_class == csc_register)
6084 C_DECL_REGISTER (decl) = 1;
6085 DECL_REGISTER (decl) = 1;
6088 /* Record constancy and volatility. */
6089 c_apply_type_quals_to_decl (type_quals, decl);
6091 /* If a type has volatile components, it should be stored in memory.
6092 Otherwise, the fact that those components are volatile
6093 will be ignored, and would even crash the compiler.
6094 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6095 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6096 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
6097 || TREE_CODE (decl) == RESULT_DECL))
6099 /* It is not an error for a structure with volatile fields to
6100 be declared register, but reset DECL_REGISTER since it
6101 cannot actually go in a register. */
6102 int was_reg = C_DECL_REGISTER (decl);
6103 C_DECL_REGISTER (decl) = 0;
6104 DECL_REGISTER (decl) = 0;
6105 c_mark_addressable (decl);
6106 C_DECL_REGISTER (decl) = was_reg;
6109 /* This is the earliest point at which we might know the assembler
6110 name of a variable. Thus, if it's known before this, die horribly. */
6111 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6113 if (warn_cxx_compat
6114 && TREE_CODE (decl) == VAR_DECL
6115 && TREE_PUBLIC (decl)
6116 && TREE_STATIC (decl)
6117 && (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6118 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6119 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6120 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6121 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6122 ("non-local variable %qD with anonymous type is "
6123 "questionable in C++"),
6124 decl);
6126 return decl;
6130 /* Decode the parameter-list info for a function type or function definition.
6131 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6132 if there is an identifier list instead of a parameter decl list).
6133 These two functions are separate because when a function returns
6134 or receives functions then each is called multiple times but the order
6135 of calls is different. The last call to `grokparms' is always the one
6136 that contains the formal parameter names of a function definition.
6138 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6140 FUNCDEF_FLAG is true for a function definition, false for
6141 a mere declaration. A nonempty identifier-list gets an error message
6142 when FUNCDEF_FLAG is false. */
6144 static tree
6145 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6147 tree arg_types = arg_info->types;
6149 if (funcdef_flag && arg_info->had_vla_unspec)
6151 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6152 /* C99 6.7.5.2p4 */
6153 error ("%<[*]%> not allowed in other than function prototype scope");
6156 if (arg_types == 0 && !funcdef_flag && !in_system_header)
6157 warning (OPT_Wstrict_prototypes,
6158 "function declaration isn%'t a prototype");
6160 if (arg_types == error_mark_node)
6161 return 0; /* don't set TYPE_ARG_TYPES in this case */
6163 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6165 if (!funcdef_flag)
6167 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6168 arg_info->parms = NULL_TREE;
6170 else
6171 arg_info->parms = arg_info->types;
6173 arg_info->types = 0;
6174 return 0;
6176 else
6178 tree parm, type, typelt;
6179 unsigned int parmno;
6180 const char *errmsg;
6182 /* If there is a parameter of incomplete type in a definition,
6183 this is an error. In a declaration this is valid, and a
6184 struct or union type may be completed later, before any calls
6185 or definition of the function. In the case where the tag was
6186 first declared within the parameter list, a warning has
6187 already been given. If a parameter has void type, then
6188 however the function cannot be defined or called, so
6189 warn. */
6191 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
6192 parm;
6193 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
6195 type = TREE_VALUE (typelt);
6196 if (type == error_mark_node)
6197 continue;
6199 if (!COMPLETE_TYPE_P (type))
6201 if (funcdef_flag)
6203 if (DECL_NAME (parm))
6204 error_at (input_location,
6205 "parameter %u (%q+D) has incomplete type",
6206 parmno, parm);
6207 else
6208 error_at (DECL_SOURCE_LOCATION (parm),
6209 "parameter %u has incomplete type",
6210 parmno);
6212 TREE_VALUE (typelt) = error_mark_node;
6213 TREE_TYPE (parm) = error_mark_node;
6214 arg_types = NULL_TREE;
6216 else if (VOID_TYPE_P (type))
6218 if (DECL_NAME (parm))
6219 warning_at (input_location, 0,
6220 "parameter %u (%q+D) has void type",
6221 parmno, parm);
6222 else
6223 warning_at (DECL_SOURCE_LOCATION (parm), 0,
6224 "parameter %u has void type",
6225 parmno);
6229 errmsg = targetm.invalid_parameter_type (type);
6230 if (errmsg)
6232 error (errmsg);
6233 TREE_VALUE (typelt) = error_mark_node;
6234 TREE_TYPE (parm) = error_mark_node;
6235 arg_types = NULL_TREE;
6238 if (DECL_NAME (parm) && TREE_USED (parm))
6239 warn_if_shadowing (parm);
6241 return arg_types;
6245 /* Allocate and initialize a c_arg_info structure from the parser's
6246 obstack. */
6248 struct c_arg_info *
6249 build_arg_info (void)
6251 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
6252 ret->parms = NULL_TREE;
6253 ret->tags = NULL;
6254 ret->types = NULL_TREE;
6255 ret->others = NULL_TREE;
6256 ret->pending_sizes = NULL;
6257 ret->had_vla_unspec = 0;
6258 return ret;
6261 /* Take apart the current scope and return a c_arg_info structure with
6262 info on a parameter list just parsed.
6264 This structure is later fed to 'grokparms' and 'store_parm_decls'.
6266 ELLIPSIS being true means the argument list ended in '...' so don't
6267 append a sentinel (void_list_node) to the end of the type-list.
6269 EXPR is NULL or an expression that needs to be evaluated for the
6270 side effects of array size expressions in the parameters. */
6272 struct c_arg_info *
6273 get_parm_info (bool ellipsis, tree expr)
6275 struct c_binding *b = current_scope->bindings;
6276 struct c_arg_info *arg_info = build_arg_info ();
6278 tree parms = 0;
6279 VEC(c_arg_tag,gc) *tags = NULL;
6280 tree types = 0;
6281 tree others = 0;
6283 static bool explained_incomplete_types = false;
6284 bool gave_void_only_once_err = false;
6286 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
6288 /* The bindings in this scope must not get put into a block.
6289 We will take care of deleting the binding nodes. */
6290 current_scope->bindings = 0;
6292 /* This function is only called if there was *something* on the
6293 parameter list. */
6294 gcc_assert (b);
6296 /* A parameter list consisting solely of 'void' indicates that the
6297 function takes no arguments. But if the 'void' is qualified
6298 (by 'const' or 'volatile'), or has a storage class specifier
6299 ('register'), then the behavior is undefined; issue an error.
6300 Typedefs for 'void' are OK (see DR#157). */
6301 if (b->prev == 0 /* one binding */
6302 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
6303 && !DECL_NAME (b->decl) /* anonymous */
6304 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
6306 if (TREE_THIS_VOLATILE (b->decl)
6307 || TREE_READONLY (b->decl)
6308 || C_DECL_REGISTER (b->decl))
6309 error ("%<void%> as only parameter may not be qualified");
6311 /* There cannot be an ellipsis. */
6312 if (ellipsis)
6313 error ("%<void%> must be the only parameter");
6315 arg_info->types = void_list_node;
6316 return arg_info;
6319 if (!ellipsis)
6320 types = void_list_node;
6322 /* Break up the bindings list into parms, tags, types, and others;
6323 apply sanity checks; purge the name-to-decl bindings. */
6324 while (b)
6326 tree decl = b->decl;
6327 tree type = TREE_TYPE (decl);
6328 c_arg_tag *tag;
6329 const char *keyword;
6331 switch (TREE_CODE (decl))
6333 case PARM_DECL:
6334 if (b->id)
6336 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6337 I_SYMBOL_BINDING (b->id) = b->shadowed;
6340 /* Check for forward decls that never got their actual decl. */
6341 if (TREE_ASM_WRITTEN (decl))
6342 error ("parameter %q+D has just a forward declaration", decl);
6343 /* Check for (..., void, ...) and issue an error. */
6344 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
6346 if (!gave_void_only_once_err)
6348 error ("%<void%> must be the only parameter");
6349 gave_void_only_once_err = true;
6352 else
6354 /* Valid parameter, add it to the list. */
6355 DECL_CHAIN (decl) = parms;
6356 parms = decl;
6358 /* Since there is a prototype, args are passed in their
6359 declared types. The back end may override this later. */
6360 DECL_ARG_TYPE (decl) = type;
6361 types = tree_cons (0, type, types);
6363 break;
6365 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
6366 case UNION_TYPE: keyword = "union"; goto tag;
6367 case RECORD_TYPE: keyword = "struct"; goto tag;
6368 tag:
6369 /* Types may not have tag-names, in which case the type
6370 appears in the bindings list with b->id NULL. */
6371 if (b->id)
6373 gcc_assert (I_TAG_BINDING (b->id) == b);
6374 I_TAG_BINDING (b->id) = b->shadowed;
6377 /* Warn about any struct, union or enum tags defined in a
6378 parameter list. The scope of such types is limited to
6379 the parameter list, which is rarely if ever desirable
6380 (it's impossible to call such a function with type-
6381 correct arguments). An anonymous union parm type is
6382 meaningful as a GNU extension, so don't warn for that. */
6383 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
6385 if (b->id)
6386 /* The %s will be one of 'struct', 'union', or 'enum'. */
6387 warning (0, "%<%s %E%> declared inside parameter list",
6388 keyword, b->id);
6389 else
6390 /* The %s will be one of 'struct', 'union', or 'enum'. */
6391 warning (0, "anonymous %s declared inside parameter list",
6392 keyword);
6394 if (!explained_incomplete_types)
6396 warning (0, "its scope is only this definition or declaration,"
6397 " which is probably not what you want");
6398 explained_incomplete_types = true;
6402 tag = VEC_safe_push (c_arg_tag, gc, tags, NULL);
6403 tag->id = b->id;
6404 tag->type = decl;
6405 break;
6407 case CONST_DECL:
6408 case TYPE_DECL:
6409 case FUNCTION_DECL:
6410 /* CONST_DECLs appear here when we have an embedded enum,
6411 and TYPE_DECLs appear here when we have an embedded struct
6412 or union. No warnings for this - we already warned about the
6413 type itself. FUNCTION_DECLs appear when there is an implicit
6414 function declaration in the parameter list. */
6416 /* When we reinsert this decl in the function body, we need
6417 to reconstruct whether it was marked as nested. */
6418 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6419 ? b->nested
6420 : !b->nested);
6421 DECL_CHAIN (decl) = others;
6422 others = decl;
6423 /* fall through */
6425 case ERROR_MARK:
6426 /* error_mark_node appears here when we have an undeclared
6427 variable. Just throw it away. */
6428 if (b->id)
6430 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6431 I_SYMBOL_BINDING (b->id) = b->shadowed;
6433 break;
6435 /* Other things that might be encountered. */
6436 case LABEL_DECL:
6437 case VAR_DECL:
6438 default:
6439 gcc_unreachable ();
6442 b = free_binding_and_advance (b);
6445 arg_info->parms = parms;
6446 arg_info->tags = tags;
6447 arg_info->types = types;
6448 arg_info->others = others;
6449 arg_info->pending_sizes = expr;
6450 return arg_info;
6453 /* Get the struct, enum or union (CODE says which) with tag NAME.
6454 Define the tag as a forward-reference with location LOC if it is
6455 not defined. Return a c_typespec structure for the type
6456 specifier. */
6458 struct c_typespec
6459 parser_xref_tag (location_t loc, enum tree_code code, tree name)
6461 struct c_typespec ret;
6462 tree ref;
6463 location_t refloc;
6465 ret.expr = NULL_TREE;
6466 ret.expr_const_operands = true;
6468 /* If a cross reference is requested, look up the type
6469 already defined for this tag and return it. */
6471 ref = lookup_tag (code, name, 0, &refloc);
6472 /* If this is the right type of tag, return what we found.
6473 (This reference will be shadowed by shadow_tag later if appropriate.)
6474 If this is the wrong type of tag, do not return it. If it was the
6475 wrong type in the same scope, we will have had an error
6476 message already; if in a different scope and declaring
6477 a name, pending_xref_error will give an error message; but if in a
6478 different scope and not declaring a name, this tag should
6479 shadow the previous declaration of a different type of tag, and
6480 this would not work properly if we return the reference found.
6481 (For example, with "struct foo" in an outer scope, "union foo;"
6482 must shadow that tag with a new one of union type.) */
6483 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
6484 if (ref && TREE_CODE (ref) == code)
6486 if (C_TYPE_DEFINED_IN_STRUCT (ref)
6487 && loc != UNKNOWN_LOCATION
6488 && warn_cxx_compat)
6490 switch (code)
6492 case ENUMERAL_TYPE:
6493 warning_at (loc, OPT_Wc___compat,
6494 ("enum type defined in struct or union "
6495 "is not visible in C++"));
6496 inform (refloc, "enum type defined here");
6497 break;
6498 case RECORD_TYPE:
6499 warning_at (loc, OPT_Wc___compat,
6500 ("struct defined in struct or union "
6501 "is not visible in C++"));
6502 inform (refloc, "struct defined here");
6503 break;
6504 case UNION_TYPE:
6505 warning_at (loc, OPT_Wc___compat,
6506 ("union defined in struct or union "
6507 "is not visible in C++"));
6508 inform (refloc, "union defined here");
6509 break;
6510 default:
6511 gcc_unreachable();
6515 ret.spec = ref;
6516 return ret;
6519 /* If no such tag is yet defined, create a forward-reference node
6520 and record it as the "definition".
6521 When a real declaration of this type is found,
6522 the forward-reference will be altered into a real type. */
6524 ref = make_node (code);
6525 if (code == ENUMERAL_TYPE)
6527 /* Give the type a default layout like unsigned int
6528 to avoid crashing if it does not get defined. */
6529 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
6530 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
6531 TYPE_USER_ALIGN (ref) = 0;
6532 TYPE_UNSIGNED (ref) = 1;
6533 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
6534 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
6535 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
6538 pushtag (loc, name, ref);
6540 ret.spec = ref;
6541 return ret;
6544 /* Get the struct, enum or union (CODE says which) with tag NAME.
6545 Define the tag as a forward-reference if it is not defined.
6546 Return a tree for the type. */
6548 tree
6549 xref_tag (enum tree_code code, tree name)
6551 return parser_xref_tag (input_location, code, name).spec;
6554 /* Make sure that the tag NAME is defined *in the current scope*
6555 at least as a forward reference.
6556 LOC is the location of the struct's definition.
6557 CODE says which kind of tag NAME ought to be.
6559 This stores the current value of the file static STRUCT_PARSE_INFO
6560 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
6561 new c_struct_parse_info structure. The old value of
6562 STRUCT_PARSE_INFO is restored in finish_struct. */
6564 tree
6565 start_struct (location_t loc, enum tree_code code, tree name,
6566 struct c_struct_parse_info **enclosing_struct_parse_info)
6568 /* If there is already a tag defined at this scope
6569 (as a forward reference), just return it. */
6571 tree ref = NULL_TREE;
6572 location_t refloc = UNKNOWN_LOCATION;
6574 if (name != NULL_TREE)
6575 ref = lookup_tag (code, name, 1, &refloc);
6576 if (ref && TREE_CODE (ref) == code)
6578 if (TYPE_SIZE (ref))
6580 if (code == UNION_TYPE)
6581 error_at (loc, "redefinition of %<union %E%>", name);
6582 else
6583 error_at (loc, "redefinition of %<struct %E%>", name);
6584 if (refloc != UNKNOWN_LOCATION)
6585 inform (refloc, "originally defined here");
6586 /* Don't create structures using a name already in use. */
6587 ref = NULL_TREE;
6589 else if (C_TYPE_BEING_DEFINED (ref))
6591 if (code == UNION_TYPE)
6592 error_at (loc, "nested redefinition of %<union %E%>", name);
6593 else
6594 error_at (loc, "nested redefinition of %<struct %E%>", name);
6595 /* Don't bother to report "originally defined here" for a
6596 nested redefinition; the original definition should be
6597 obvious. */
6598 /* Don't create structures that contain themselves. */
6599 ref = NULL_TREE;
6603 /* Otherwise create a forward-reference just so the tag is in scope. */
6605 if (ref == NULL_TREE || TREE_CODE (ref) != code)
6607 ref = make_node (code);
6608 pushtag (loc, name, ref);
6611 C_TYPE_BEING_DEFINED (ref) = 1;
6612 TYPE_PACKED (ref) = flag_pack_struct;
6614 *enclosing_struct_parse_info = struct_parse_info;
6615 struct_parse_info = XNEW (struct c_struct_parse_info);
6616 struct_parse_info->struct_types = VEC_alloc (tree, heap, 0);
6617 struct_parse_info->fields = VEC_alloc (c_binding_ptr, heap, 0);
6618 struct_parse_info->typedefs_seen = VEC_alloc (tree, heap, 0);
6620 /* FIXME: This will issue a warning for a use of a type defined
6621 within a statement expr used within sizeof, et. al. This is not
6622 terribly serious as C++ doesn't permit statement exprs within
6623 sizeof anyhow. */
6624 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
6625 warning_at (loc, OPT_Wc___compat,
6626 "defining type in %qs expression is invalid in C++",
6627 (in_sizeof
6628 ? "sizeof"
6629 : (in_typeof ? "typeof" : "alignof")));
6631 return ref;
6634 /* Process the specs, declarator and width (NULL if omitted)
6635 of a structure component, returning a FIELD_DECL node.
6636 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
6637 DECL_ATTRS is as for grokdeclarator.
6639 LOC is the location of the structure component.
6641 This is done during the parsing of the struct declaration.
6642 The FIELD_DECL nodes are chained together and the lot of them
6643 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
6645 tree
6646 grokfield (location_t loc,
6647 struct c_declarator *declarator, struct c_declspecs *declspecs,
6648 tree width, tree *decl_attrs)
6650 tree value;
6652 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
6653 && width == NULL_TREE)
6655 /* This is an unnamed decl.
6657 If we have something of the form "union { list } ;" then this
6658 is the anonymous union extension. Similarly for struct.
6660 If this is something of the form "struct foo;", then
6661 If MS or Plan 9 extensions are enabled, this is handled as
6662 an anonymous struct.
6663 Otherwise this is a forward declaration of a structure tag.
6665 If this is something of the form "foo;" and foo is a TYPE_DECL, then
6666 If foo names a structure or union without a tag, then this
6667 is an anonymous struct (this is permitted by C1X).
6668 If MS or Plan 9 extensions are enabled and foo names a
6669 structure, then again this is an anonymous struct.
6670 Otherwise this is an error.
6672 Oh what a horrid tangled web we weave. I wonder if MS consciously
6673 took this from Plan 9 or if it was an accident of implementation
6674 that took root before someone noticed the bug... */
6676 tree type = declspecs->type;
6677 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
6678 || TREE_CODE (type) == UNION_TYPE);
6679 bool ok = false;
6681 if (type_ok
6682 && (flag_ms_extensions
6683 || flag_plan9_extensions
6684 || !declspecs->typedef_p))
6686 if (flag_ms_extensions || flag_plan9_extensions)
6687 ok = true;
6688 else if (TYPE_NAME (type) == NULL)
6689 ok = true;
6690 else
6691 ok = false;
6693 if (!ok)
6695 pedwarn (loc, 0, "declaration does not declare anything");
6696 return NULL_TREE;
6698 if (!flag_isoc1x)
6700 if (flag_isoc99)
6701 pedwarn (loc, OPT_pedantic,
6702 "ISO C99 doesn%'t support unnamed structs/unions");
6703 else
6704 pedwarn (loc, OPT_pedantic,
6705 "ISO C90 doesn%'t support unnamed structs/unions");
6709 value = grokdeclarator (declarator, declspecs, FIELD, false,
6710 width ? &width : NULL, decl_attrs, NULL, NULL,
6711 DEPRECATED_NORMAL);
6713 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6714 DECL_INITIAL (value) = width;
6716 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
6718 /* If we currently have a binding for this field, set the
6719 in_struct field in the binding, so that we warn about lookups
6720 which find it. */
6721 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
6722 if (b != NULL)
6724 /* If the in_struct field is not yet set, push it on a list
6725 to be cleared when this struct is finished. */
6726 if (!b->in_struct)
6728 VEC_safe_push (c_binding_ptr, heap,
6729 struct_parse_info->fields, b);
6730 b->in_struct = 1;
6735 return value;
6738 /* Subroutine of detect_field_duplicates: return whether X and Y,
6739 which are both fields in the same struct, have duplicate field
6740 names. */
6742 static bool
6743 is_duplicate_field (tree x, tree y)
6745 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
6746 return true;
6748 /* When using -fplan9-extensions, an anonymous field whose name is a
6749 typedef can duplicate a field name. */
6750 if (flag_plan9_extensions
6751 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
6753 tree xt, xn, yt, yn;
6755 xt = TREE_TYPE (x);
6756 if (DECL_NAME (x) != NULL_TREE)
6757 xn = DECL_NAME (x);
6758 else if ((TREE_CODE (xt) == RECORD_TYPE || TREE_CODE (xt) == UNION_TYPE)
6759 && TYPE_NAME (xt) != NULL_TREE
6760 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
6761 xn = DECL_NAME (TYPE_NAME (xt));
6762 else
6763 xn = NULL_TREE;
6765 yt = TREE_TYPE (y);
6766 if (DECL_NAME (y) != NULL_TREE)
6767 yn = DECL_NAME (y);
6768 else if ((TREE_CODE (yt) == RECORD_TYPE || TREE_CODE (yt) == UNION_TYPE)
6769 && TYPE_NAME (yt) != NULL_TREE
6770 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
6771 yn = DECL_NAME (TYPE_NAME (yt));
6772 else
6773 yn = NULL_TREE;
6775 if (xn != NULL_TREE && xn == yn)
6776 return true;
6779 return false;
6782 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
6783 to HTAB, giving errors for any duplicates. */
6785 static void
6786 detect_field_duplicates_hash (tree fieldlist, htab_t htab)
6788 tree x, y;
6789 void **slot;
6791 for (x = fieldlist; x ; x = DECL_CHAIN (x))
6792 if ((y = DECL_NAME (x)) != 0)
6794 slot = htab_find_slot (htab, y, INSERT);
6795 if (*slot)
6797 error ("duplicate member %q+D", x);
6798 DECL_NAME (x) = NULL_TREE;
6800 *slot = y;
6802 else if (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6803 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6805 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
6807 /* When using -fplan9-extensions, an anonymous field whose
6808 name is a typedef can duplicate a field name. */
6809 if (flag_plan9_extensions
6810 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
6811 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
6813 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
6814 slot = htab_find_slot (htab, xn, INSERT);
6815 if (*slot)
6816 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
6817 *slot = xn;
6822 /* Generate an error for any duplicate field names in FIELDLIST. Munge
6823 the list such that this does not present a problem later. */
6825 static void
6826 detect_field_duplicates (tree fieldlist)
6828 tree x, y;
6829 int timeout = 10;
6831 /* If the struct is the list of instance variables of an Objective-C
6832 class, then we need to check all the instance variables of
6833 superclasses when checking for duplicates (since you can't have
6834 an instance variable in a subclass with the same name as an
6835 instance variable in a superclass). We pass on this job to the
6836 Objective-C compiler. objc_detect_field_duplicates() will return
6837 false if we are not checking the list of instance variables and
6838 the C frontend should proceed with the standard field duplicate
6839 checks. If we are checking the list of instance variables, the
6840 ObjC frontend will do the check, emit the errors if needed, and
6841 then return true. */
6842 if (c_dialect_objc ())
6843 if (objc_detect_field_duplicates (false))
6844 return;
6846 /* First, see if there are more than "a few" fields.
6847 This is trivially true if there are zero or one fields. */
6848 if (!fieldlist || !DECL_CHAIN (fieldlist))
6849 return;
6850 x = fieldlist;
6851 do {
6852 timeout--;
6853 if (DECL_NAME (x) == NULL_TREE
6854 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6855 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
6856 timeout = 0;
6857 x = DECL_CHAIN (x);
6858 } while (timeout > 0 && x);
6860 /* If there were "few" fields and no anonymous structures or unions,
6861 avoid the overhead of allocating a hash table. Instead just do
6862 the nested traversal thing. */
6863 if (timeout > 0)
6865 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
6866 /* When using -fplan9-extensions, we can have duplicates
6867 between typedef names and fields. */
6868 if (DECL_NAME (x)
6869 || (flag_plan9_extensions
6870 && DECL_NAME (x) == NULL_TREE
6871 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6872 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6873 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
6874 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
6876 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
6877 if (is_duplicate_field (y, x))
6879 error ("duplicate member %q+D", x);
6880 DECL_NAME (x) = NULL_TREE;
6884 else
6886 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
6888 detect_field_duplicates_hash (fieldlist, htab);
6889 htab_delete (htab);
6893 /* Finish up struct info used by -Wc++-compat. */
6895 static void
6896 warn_cxx_compat_finish_struct (tree fieldlist)
6898 unsigned int ix;
6899 tree x;
6900 struct c_binding *b;
6902 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
6903 the current struct. We do this now at the end of the struct
6904 because the flag is used to issue visibility warnings, and we
6905 only want to issue those warnings if the type is referenced
6906 outside of the struct declaration. */
6907 FOR_EACH_VEC_ELT (tree, struct_parse_info->struct_types, ix, x)
6908 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
6910 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
6911 typedefs used when declaring fields in this struct. If the name
6912 of any of the fields is also a typedef name then the struct would
6913 not parse in C++, because the C++ lookup rules say that the
6914 typedef name would be looked up in the context of the struct, and
6915 would thus be the field rather than the typedef. */
6916 if (!VEC_empty (tree, struct_parse_info->typedefs_seen)
6917 && fieldlist != NULL_TREE)
6919 /* Use a pointer_set using the name of the typedef. We can use
6920 a pointer_set because identifiers are interned. */
6921 struct pointer_set_t *tset = pointer_set_create ();
6923 FOR_EACH_VEC_ELT (tree, struct_parse_info->typedefs_seen, ix, x)
6924 pointer_set_insert (tset, DECL_NAME (x));
6926 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
6928 if (DECL_NAME (x) != NULL_TREE
6929 && pointer_set_contains (tset, DECL_NAME (x)))
6931 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
6932 ("using %qD as both field and typedef name is "
6933 "invalid in C++"),
6935 /* FIXME: It would be nice to report the location where
6936 the typedef name is used. */
6940 pointer_set_destroy (tset);
6943 /* For each field which has a binding and which was not defined in
6944 an enclosing struct, clear the in_struct field. */
6945 FOR_EACH_VEC_ELT (c_binding_ptr, struct_parse_info->fields, ix, b)
6946 b->in_struct = 0;
6949 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
6950 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
6951 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
6952 ATTRIBUTES are attributes to be applied to the structure.
6954 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
6955 the struct was started. */
6957 tree
6958 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
6959 struct c_struct_parse_info *enclosing_struct_parse_info)
6961 tree x;
6962 bool toplevel = file_scope == current_scope;
6963 int saw_named_field;
6965 /* If this type was previously laid out as a forward reference,
6966 make sure we lay it out again. */
6968 TYPE_SIZE (t) = 0;
6970 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6972 if (pedantic)
6974 for (x = fieldlist; x; x = DECL_CHAIN (x))
6976 if (DECL_NAME (x) != 0)
6977 break;
6978 if (flag_isoc1x
6979 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6980 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
6981 break;
6984 if (x == 0)
6986 if (TREE_CODE (t) == UNION_TYPE)
6988 if (fieldlist)
6989 pedwarn (loc, OPT_pedantic, "union has no named members");
6990 else
6991 pedwarn (loc, OPT_pedantic, "union has no members");
6993 else
6995 if (fieldlist)
6996 pedwarn (loc, OPT_pedantic, "struct has no named members");
6997 else
6998 pedwarn (loc, OPT_pedantic, "struct has no members");
7003 /* Install struct as DECL_CONTEXT of each field decl.
7004 Also process specified field sizes, found in the DECL_INITIAL,
7005 storing 0 there after the type has been changed to precision equal
7006 to its width, rather than the precision of the specified standard
7007 type. (Correct layout requires the original type to have been preserved
7008 until now.) */
7010 saw_named_field = 0;
7011 for (x = fieldlist; x; x = DECL_CHAIN (x))
7013 if (TREE_TYPE (x) == error_mark_node)
7014 continue;
7016 DECL_CONTEXT (x) = t;
7018 /* If any field is const, the structure type is pseudo-const. */
7019 if (TREE_READONLY (x))
7020 C_TYPE_FIELDS_READONLY (t) = 1;
7021 else
7023 /* A field that is pseudo-const makes the structure likewise. */
7024 tree t1 = strip_array_types (TREE_TYPE (x));
7025 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
7026 && C_TYPE_FIELDS_READONLY (t1))
7027 C_TYPE_FIELDS_READONLY (t) = 1;
7030 /* Any field that is volatile means variables of this type must be
7031 treated in some ways as volatile. */
7032 if (TREE_THIS_VOLATILE (x))
7033 C_TYPE_FIELDS_VOLATILE (t) = 1;
7035 /* Any field of nominal variable size implies structure is too. */
7036 if (C_DECL_VARIABLE_SIZE (x))
7037 C_TYPE_VARIABLE_SIZE (t) = 1;
7039 if (DECL_INITIAL (x))
7041 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
7042 DECL_SIZE (x) = bitsize_int (width);
7043 DECL_BIT_FIELD (x) = 1;
7044 SET_DECL_C_BIT_FIELD (x);
7047 if (TYPE_PACKED (t)
7048 && (DECL_BIT_FIELD (x)
7049 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7050 DECL_PACKED (x) = 1;
7052 /* Detect flexible array member in an invalid context. */
7053 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7054 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7055 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7056 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7058 if (TREE_CODE (t) == UNION_TYPE)
7060 error_at (DECL_SOURCE_LOCATION (x),
7061 "flexible array member in union");
7062 TREE_TYPE (x) = error_mark_node;
7064 else if (DECL_CHAIN (x) != NULL_TREE)
7066 error_at (DECL_SOURCE_LOCATION (x),
7067 "flexible array member not at end of struct");
7068 TREE_TYPE (x) = error_mark_node;
7070 else if (!saw_named_field)
7072 error_at (DECL_SOURCE_LOCATION (x),
7073 "flexible array member in otherwise empty struct");
7074 TREE_TYPE (x) = error_mark_node;
7078 if (pedantic && TREE_CODE (t) == RECORD_TYPE
7079 && flexible_array_type_p (TREE_TYPE (x)))
7080 pedwarn (DECL_SOURCE_LOCATION (x), OPT_pedantic,
7081 "invalid use of structure with flexible array member");
7083 if (DECL_NAME (x)
7084 || TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7085 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7086 saw_named_field = 1;
7089 detect_field_duplicates (fieldlist);
7091 /* Now we have the nearly final fieldlist. Record it,
7092 then lay out the structure or union (including the fields). */
7094 TYPE_FIELDS (t) = fieldlist;
7096 layout_type (t);
7098 /* Give bit-fields their proper types. */
7100 tree *fieldlistp = &fieldlist;
7101 while (*fieldlistp)
7102 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
7103 && TREE_TYPE (*fieldlistp) != error_mark_node)
7105 unsigned HOST_WIDE_INT width
7106 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
7107 tree type = TREE_TYPE (*fieldlistp);
7108 if (width != TYPE_PRECISION (type))
7110 TREE_TYPE (*fieldlistp)
7111 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
7112 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
7114 DECL_INITIAL (*fieldlistp) = 0;
7116 else
7117 fieldlistp = &DECL_CHAIN (*fieldlistp);
7120 /* Now we have the truly final field list.
7121 Store it in this type and in the variants. */
7123 TYPE_FIELDS (t) = fieldlist;
7125 /* If there are lots of fields, sort so we can look through them fast.
7126 We arbitrarily consider 16 or more elts to be "a lot". */
7129 int len = 0;
7131 for (x = fieldlist; x; x = DECL_CHAIN (x))
7133 if (len > 15 || DECL_NAME (x) == NULL)
7134 break;
7135 len += 1;
7138 if (len > 15)
7140 tree *field_array;
7141 struct lang_type *space;
7142 struct sorted_fields_type *space2;
7144 len += list_length (x);
7146 /* Use the same allocation policy here that make_node uses, to
7147 ensure that this lives as long as the rest of the struct decl.
7148 All decls in an inline function need to be saved. */
7150 space = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
7151 space2 = ggc_alloc_sorted_fields_type
7152 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
7154 len = 0;
7155 space->s = space2;
7156 field_array = &space2->elts[0];
7157 for (x = fieldlist; x; x = DECL_CHAIN (x))
7159 field_array[len++] = x;
7161 /* If there is anonymous struct or union, break out of the loop. */
7162 if (DECL_NAME (x) == NULL)
7163 break;
7165 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
7166 if (x == NULL)
7168 TYPE_LANG_SPECIFIC (t) = space;
7169 TYPE_LANG_SPECIFIC (t)->s->len = len;
7170 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
7171 qsort (field_array, len, sizeof (tree), field_decl_cmp);
7176 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7178 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7179 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
7180 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
7181 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
7182 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
7185 /* If this was supposed to be a transparent union, but we can't
7186 make it one, warn and turn off the flag. */
7187 if (TREE_CODE (t) == UNION_TYPE
7188 && TYPE_TRANSPARENT_AGGR (t)
7189 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
7191 TYPE_TRANSPARENT_AGGR (t) = 0;
7192 warning_at (loc, 0, "union cannot be made transparent");
7195 /* If this structure or union completes the type of any previous
7196 variable declaration, lay it out and output its rtl. */
7197 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
7199 x = TREE_CHAIN (x))
7201 tree decl = TREE_VALUE (x);
7202 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7203 layout_array_type (TREE_TYPE (decl));
7204 if (TREE_CODE (decl) != TYPE_DECL)
7206 layout_decl (decl, 0);
7207 if (c_dialect_objc ())
7208 objc_check_decl (decl);
7209 rest_of_decl_compilation (decl, toplevel, 0);
7210 if (!toplevel)
7211 expand_decl (decl);
7214 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
7216 /* Update type location to the one of the definition, instead of e.g.
7217 a forward declaration. */
7218 if (TYPE_STUB_DECL (t))
7219 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
7221 /* Finish debugging output for this type. */
7222 rest_of_type_compilation (t, toplevel);
7224 /* If we're inside a function proper, i.e. not file-scope and not still
7225 parsing parameters, then arrange for the size of a variable sized type
7226 to be bound now. */
7227 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
7228 add_stmt (build_stmt (loc,
7229 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
7231 if (warn_cxx_compat)
7232 warn_cxx_compat_finish_struct (fieldlist);
7234 VEC_free (tree, heap, struct_parse_info->struct_types);
7235 VEC_free (c_binding_ptr, heap, struct_parse_info->fields);
7236 VEC_free (tree, heap, struct_parse_info->typedefs_seen);
7237 XDELETE (struct_parse_info);
7239 struct_parse_info = enclosing_struct_parse_info;
7241 /* If this struct is defined inside a struct, add it to
7242 struct_types. */
7243 if (warn_cxx_compat
7244 && struct_parse_info != NULL
7245 && !in_sizeof && !in_typeof && !in_alignof)
7246 VEC_safe_push (tree, heap, struct_parse_info->struct_types, t);
7248 return t;
7251 /* Lay out the type T, and its element type, and so on. */
7253 static void
7254 layout_array_type (tree t)
7256 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7257 layout_array_type (TREE_TYPE (t));
7258 layout_type (t);
7261 /* Begin compiling the definition of an enumeration type.
7262 NAME is its name (or null if anonymous).
7263 LOC is the enum's location.
7264 Returns the type object, as yet incomplete.
7265 Also records info about it so that build_enumerator
7266 may be used to declare the individual values as they are read. */
7268 tree
7269 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
7271 tree enumtype = NULL_TREE;
7272 location_t enumloc = UNKNOWN_LOCATION;
7274 /* If this is the real definition for a previous forward reference,
7275 fill in the contents in the same object that used to be the
7276 forward reference. */
7278 if (name != NULL_TREE)
7279 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
7281 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
7283 enumtype = make_node (ENUMERAL_TYPE);
7284 pushtag (loc, name, enumtype);
7287 if (C_TYPE_BEING_DEFINED (enumtype))
7288 error_at (loc, "nested redefinition of %<enum %E%>", name);
7290 C_TYPE_BEING_DEFINED (enumtype) = 1;
7292 if (TYPE_VALUES (enumtype) != 0)
7294 /* This enum is a named one that has been declared already. */
7295 error_at (loc, "redeclaration of %<enum %E%>", name);
7296 if (enumloc != UNKNOWN_LOCATION)
7297 inform (enumloc, "originally defined here");
7299 /* Completely replace its old definition.
7300 The old enumerators remain defined, however. */
7301 TYPE_VALUES (enumtype) = 0;
7304 the_enum->enum_next_value = integer_zero_node;
7305 the_enum->enum_overflow = 0;
7307 if (flag_short_enums)
7308 TYPE_PACKED (enumtype) = 1;
7310 /* FIXME: This will issue a warning for a use of a type defined
7311 within sizeof in a statement expr. This is not terribly serious
7312 as C++ doesn't permit statement exprs within sizeof anyhow. */
7313 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7314 warning_at (loc, OPT_Wc___compat,
7315 "defining type in %qs expression is invalid in C++",
7316 (in_sizeof
7317 ? "sizeof"
7318 : (in_typeof ? "typeof" : "alignof")));
7320 return enumtype;
7323 /* After processing and defining all the values of an enumeration type,
7324 install their decls in the enumeration type and finish it off.
7325 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
7326 and ATTRIBUTES are the specified attributes.
7327 Returns ENUMTYPE. */
7329 tree
7330 finish_enum (tree enumtype, tree values, tree attributes)
7332 tree pair, tem;
7333 tree minnode = 0, maxnode = 0;
7334 int precision, unsign;
7335 bool toplevel = (file_scope == current_scope);
7336 struct lang_type *lt;
7338 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7340 /* Calculate the maximum value of any enumerator in this type. */
7342 if (values == error_mark_node)
7343 minnode = maxnode = integer_zero_node;
7344 else
7346 minnode = maxnode = TREE_VALUE (values);
7347 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
7349 tree value = TREE_VALUE (pair);
7350 if (tree_int_cst_lt (maxnode, value))
7351 maxnode = value;
7352 if (tree_int_cst_lt (value, minnode))
7353 minnode = value;
7357 /* Construct the final type of this enumeration. It is the same
7358 as one of the integral types - the narrowest one that fits, except
7359 that normally we only go as narrow as int - and signed iff any of
7360 the values are negative. */
7361 unsign = (tree_int_cst_sgn (minnode) >= 0);
7362 precision = MAX (tree_int_cst_min_precision (minnode, unsign),
7363 tree_int_cst_min_precision (maxnode, unsign));
7365 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
7367 tem = c_common_type_for_size (precision, unsign);
7368 if (tem == NULL)
7370 warning (0, "enumeration values exceed range of largest integer");
7371 tem = long_long_integer_type_node;
7374 else
7375 tem = unsign ? unsigned_type_node : integer_type_node;
7377 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
7378 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
7379 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
7380 TYPE_SIZE (enumtype) = 0;
7382 /* If the precision of the type was specific with an attribute and it
7383 was too small, give an error. Otherwise, use it. */
7384 if (TYPE_PRECISION (enumtype))
7386 if (precision > TYPE_PRECISION (enumtype))
7387 error ("specified mode too small for enumeral values");
7389 else
7390 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
7392 layout_type (enumtype);
7394 if (values != error_mark_node)
7396 /* Change the type of the enumerators to be the enum type. We
7397 need to do this irrespective of the size of the enum, for
7398 proper type checking. Replace the DECL_INITIALs of the
7399 enumerators, and the value slots of the list, with copies
7400 that have the enum type; they cannot be modified in place
7401 because they may be shared (e.g. integer_zero_node) Finally,
7402 change the purpose slots to point to the names of the decls. */
7403 for (pair = values; pair; pair = TREE_CHAIN (pair))
7405 tree enu = TREE_PURPOSE (pair);
7406 tree ini = DECL_INITIAL (enu);
7408 TREE_TYPE (enu) = enumtype;
7410 /* The ISO C Standard mandates enumerators to have type int,
7411 even though the underlying type of an enum type is
7412 unspecified. However, GCC allows enumerators of any
7413 integer type as an extensions. build_enumerator()
7414 converts any enumerators that fit in an int to type int,
7415 to avoid promotions to unsigned types when comparing
7416 integers with enumerators that fit in the int range.
7417 When -pedantic is given, build_enumerator() would have
7418 already warned about those that don't fit. Here we
7419 convert the rest to the enumerator type. */
7420 if (TREE_TYPE (ini) != integer_type_node)
7421 ini = convert (enumtype, ini);
7423 DECL_INITIAL (enu) = ini;
7424 TREE_PURPOSE (pair) = DECL_NAME (enu);
7425 TREE_VALUE (pair) = ini;
7428 TYPE_VALUES (enumtype) = values;
7431 /* Record the min/max values so that we can warn about bit-field
7432 enumerations that are too small for the values. */
7433 lt = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
7434 lt->enum_min = minnode;
7435 lt->enum_max = maxnode;
7436 TYPE_LANG_SPECIFIC (enumtype) = lt;
7438 /* Fix up all variant types of this enum type. */
7439 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
7441 if (tem == enumtype)
7442 continue;
7443 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
7444 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
7445 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
7446 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
7447 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
7448 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
7449 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
7450 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
7451 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
7452 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
7453 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
7456 /* Finish debugging output for this type. */
7457 rest_of_type_compilation (enumtype, toplevel);
7459 /* If this enum is defined inside a struct, add it to
7460 struct_types. */
7461 if (warn_cxx_compat
7462 && struct_parse_info != NULL
7463 && !in_sizeof && !in_typeof && !in_alignof)
7464 VEC_safe_push (tree, heap, struct_parse_info->struct_types, enumtype);
7466 return enumtype;
7469 /* Build and install a CONST_DECL for one value of the
7470 current enumeration type (one that was begun with start_enum).
7471 DECL_LOC is the location of the enumerator.
7472 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
7473 Return a tree-list containing the CONST_DECL and its value.
7474 Assignment of sequential values by default is handled here. */
7476 tree
7477 build_enumerator (location_t decl_loc, location_t loc,
7478 struct c_enum_contents *the_enum, tree name, tree value)
7480 tree decl, type;
7482 /* Validate and default VALUE. */
7484 if (value != 0)
7486 /* Don't issue more errors for error_mark_node (i.e. an
7487 undeclared identifier) - just ignore the value expression. */
7488 if (value == error_mark_node)
7489 value = 0;
7490 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
7492 error_at (loc, "enumerator value for %qE is not an integer constant",
7493 name);
7494 value = 0;
7496 else
7498 if (TREE_CODE (value) != INTEGER_CST)
7500 value = c_fully_fold (value, false, NULL);
7501 if (TREE_CODE (value) == INTEGER_CST)
7502 pedwarn (loc, OPT_pedantic,
7503 "enumerator value for %qE is not an integer "
7504 "constant expression", name);
7506 if (TREE_CODE (value) != INTEGER_CST)
7508 error ("enumerator value for %qE is not an integer constant",
7509 name);
7510 value = 0;
7512 else
7514 value = default_conversion (value);
7515 constant_expression_warning (value);
7520 /* Default based on previous value. */
7521 /* It should no longer be possible to have NON_LVALUE_EXPR
7522 in the default. */
7523 if (value == 0)
7525 value = the_enum->enum_next_value;
7526 if (the_enum->enum_overflow)
7527 error_at (loc, "overflow in enumeration values");
7529 /* Even though the underlying type of an enum is unspecified, the
7530 type of enumeration constants is explicitly defined as int
7531 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
7532 an extension. */
7533 else if (!int_fits_type_p (value, integer_type_node))
7534 pedwarn (loc, OPT_pedantic,
7535 "ISO C restricts enumerator values to range of %<int%>");
7537 /* The ISO C Standard mandates enumerators to have type int, even
7538 though the underlying type of an enum type is unspecified.
7539 However, GCC allows enumerators of any integer type as an
7540 extensions. Here we convert any enumerators that fit in an int
7541 to type int, to avoid promotions to unsigned types when comparing
7542 integers with enumerators that fit in the int range. When
7543 -pedantic is given, we would have already warned about those that
7544 don't fit. We have to do this here rather than in finish_enum
7545 because this value may be used to define more enumerators. */
7546 if (int_fits_type_p (value, integer_type_node))
7547 value = convert (integer_type_node, value);
7549 /* Set basis for default for next value. */
7550 the_enum->enum_next_value
7551 = build_binary_op (EXPR_LOC_OR_HERE (value),
7552 PLUS_EXPR, value, integer_one_node, 0);
7553 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
7555 /* Now create a declaration for the enum value name. */
7557 type = TREE_TYPE (value);
7558 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
7559 TYPE_PRECISION (integer_type_node)),
7560 (TYPE_PRECISION (type)
7561 >= TYPE_PRECISION (integer_type_node)
7562 && TYPE_UNSIGNED (type)));
7564 decl = build_decl (decl_loc, CONST_DECL, name, type);
7565 DECL_INITIAL (decl) = convert (type, value);
7566 pushdecl (decl);
7568 return tree_cons (decl, value, NULL_TREE);
7572 /* Create the FUNCTION_DECL for a function definition.
7573 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
7574 the declaration; they describe the function's name and the type it returns,
7575 but twisted together in a fashion that parallels the syntax of C.
7577 This function creates a binding context for the function body
7578 as well as setting up the FUNCTION_DECL in current_function_decl.
7580 Returns 1 on success. If the DECLARATOR is not suitable for a function
7581 (it defines a datum instead), we return 0, which tells
7582 yyparse to report a parse error. */
7585 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
7586 tree attributes)
7588 tree decl1, old_decl;
7589 tree restype, resdecl;
7590 location_t loc;
7592 current_function_returns_value = 0; /* Assume, until we see it does. */
7593 current_function_returns_null = 0;
7594 current_function_returns_abnormally = 0;
7595 warn_about_return_type = 0;
7596 c_switch_stack = NULL;
7598 /* Indicate no valid break/continue context by setting these variables
7599 to some non-null, non-label value. We'll notice and emit the proper
7600 error message in c_finish_bc_stmt. */
7601 c_break_label = c_cont_label = size_zero_node;
7603 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
7604 &attributes, NULL, NULL, DEPRECATED_NORMAL);
7606 /* If the declarator is not suitable for a function definition,
7607 cause a syntax error. */
7608 if (decl1 == 0)
7609 return 0;
7611 loc = DECL_SOURCE_LOCATION (decl1);
7613 decl_attributes (&decl1, attributes, 0);
7615 if (DECL_DECLARED_INLINE_P (decl1)
7616 && DECL_UNINLINABLE (decl1)
7617 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
7618 warning_at (loc, OPT_Wattributes,
7619 "inline function %qD given attribute noinline",
7620 decl1);
7622 /* Handle gnu_inline attribute. */
7623 if (declspecs->inline_p
7624 && !flag_gnu89_inline
7625 && TREE_CODE (decl1) == FUNCTION_DECL
7626 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
7627 || current_function_decl))
7629 if (declspecs->storage_class != csc_static)
7630 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
7633 announce_function (decl1);
7635 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
7637 error_at (loc, "return type is an incomplete type");
7638 /* Make it return void instead. */
7639 TREE_TYPE (decl1)
7640 = build_function_type (void_type_node,
7641 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
7644 if (warn_about_return_type)
7645 pedwarn_c99 (loc, flag_isoc99 ? 0
7646 : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
7647 "return type defaults to %<int%>");
7649 /* Make the init_value nonzero so pushdecl knows this is not tentative.
7650 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
7651 DECL_INITIAL (decl1) = error_mark_node;
7653 /* A nested function is not global. */
7654 if (current_function_decl != 0)
7655 TREE_PUBLIC (decl1) = 0;
7657 /* If this definition isn't a prototype and we had a prototype declaration
7658 before, copy the arg type info from that prototype. */
7659 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
7660 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
7661 old_decl = 0;
7662 current_function_prototype_locus = UNKNOWN_LOCATION;
7663 current_function_prototype_built_in = false;
7664 current_function_prototype_arg_types = NULL_TREE;
7665 if (!prototype_p (TREE_TYPE (decl1)))
7667 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
7668 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7669 TREE_TYPE (TREE_TYPE (old_decl))))
7671 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
7672 TREE_TYPE (decl1));
7673 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
7674 current_function_prototype_built_in
7675 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
7676 current_function_prototype_arg_types
7677 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
7679 if (TREE_PUBLIC (decl1))
7681 /* If there is an external prototype declaration of this
7682 function, record its location but do not copy information
7683 to this decl. This may be an invisible declaration
7684 (built-in or in a scope which has finished) or simply
7685 have more refined argument types than any declaration
7686 found above. */
7687 struct c_binding *b;
7688 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
7689 if (B_IN_SCOPE (b, external_scope))
7690 break;
7691 if (b)
7693 tree ext_decl, ext_type;
7694 ext_decl = b->decl;
7695 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
7696 if (TREE_CODE (ext_type) == FUNCTION_TYPE
7697 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7698 TREE_TYPE (ext_type)))
7700 current_function_prototype_locus
7701 = DECL_SOURCE_LOCATION (ext_decl);
7702 current_function_prototype_built_in
7703 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
7704 current_function_prototype_arg_types
7705 = TYPE_ARG_TYPES (ext_type);
7711 /* Optionally warn of old-fashioned def with no previous prototype. */
7712 if (warn_strict_prototypes
7713 && old_decl != error_mark_node
7714 && !prototype_p (TREE_TYPE (decl1))
7715 && C_DECL_ISNT_PROTOTYPE (old_decl))
7716 warning_at (loc, OPT_Wstrict_prototypes,
7717 "function declaration isn%'t a prototype");
7718 /* Optionally warn of any global def with no previous prototype. */
7719 else if (warn_missing_prototypes
7720 && old_decl != error_mark_node
7721 && TREE_PUBLIC (decl1)
7722 && !MAIN_NAME_P (DECL_NAME (decl1))
7723 && C_DECL_ISNT_PROTOTYPE (old_decl))
7724 warning_at (loc, OPT_Wmissing_prototypes,
7725 "no previous prototype for %qD", decl1);
7726 /* Optionally warn of any def with no previous prototype
7727 if the function has already been used. */
7728 else if (warn_missing_prototypes
7729 && old_decl != 0
7730 && old_decl != error_mark_node
7731 && TREE_USED (old_decl)
7732 && !prototype_p (TREE_TYPE (old_decl)))
7733 warning_at (loc, OPT_Wmissing_prototypes,
7734 "%qD was used with no prototype before its definition", decl1);
7735 /* Optionally warn of any global def with no previous declaration. */
7736 else if (warn_missing_declarations
7737 && TREE_PUBLIC (decl1)
7738 && old_decl == 0
7739 && !MAIN_NAME_P (DECL_NAME (decl1)))
7740 warning_at (loc, OPT_Wmissing_declarations,
7741 "no previous declaration for %qD",
7742 decl1);
7743 /* Optionally warn of any def with no previous declaration
7744 if the function has already been used. */
7745 else if (warn_missing_declarations
7746 && old_decl != 0
7747 && old_decl != error_mark_node
7748 && TREE_USED (old_decl)
7749 && C_DECL_IMPLICIT (old_decl))
7750 warning_at (loc, OPT_Wmissing_declarations,
7751 "%qD was used with no declaration before its definition", decl1);
7753 /* This function exists in static storage.
7754 (This does not mean `static' in the C sense!) */
7755 TREE_STATIC (decl1) = 1;
7757 /* This is the earliest point at which we might know the assembler
7758 name of the function. Thus, if it's set before this, die horribly. */
7759 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
7761 /* If #pragma weak was used, mark the decl weak now. */
7762 if (current_scope == file_scope)
7763 maybe_apply_pragma_weak (decl1);
7765 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
7766 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
7768 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
7769 != integer_type_node)
7770 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
7772 check_main_parameter_types (decl1);
7774 if (!TREE_PUBLIC (decl1))
7775 pedwarn (loc, OPT_Wmain,
7776 "%qD is normally a non-static function", decl1);
7779 /* Record the decl so that the function name is defined.
7780 If we already have a decl for this name, and it is a FUNCTION_DECL,
7781 use the old decl. */
7783 current_function_decl = pushdecl (decl1);
7785 push_scope ();
7786 declare_parm_level ();
7788 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
7789 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
7790 DECL_ARTIFICIAL (resdecl) = 1;
7791 DECL_IGNORED_P (resdecl) = 1;
7792 DECL_RESULT (current_function_decl) = resdecl;
7794 start_fname_decls ();
7796 return 1;
7799 /* Subroutine of store_parm_decls which handles new-style function
7800 definitions (prototype format). The parms already have decls, so we
7801 need only record them as in effect and complain if any redundant
7802 old-style parm decls were written. */
7803 static void
7804 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
7806 tree decl;
7807 c_arg_tag *tag;
7808 unsigned ix;
7810 if (current_scope->bindings)
7812 error_at (DECL_SOURCE_LOCATION (fndecl),
7813 "old-style parameter declarations in prototyped "
7814 "function definition");
7816 /* Get rid of the old-style declarations. */
7817 pop_scope ();
7818 push_scope ();
7820 /* Don't issue this warning for nested functions, and don't issue this
7821 warning if we got here because ARG_INFO_TYPES was error_mark_node
7822 (this happens when a function definition has just an ellipsis in
7823 its parameter list). */
7824 else if (!in_system_header && !current_function_scope
7825 && arg_info->types != error_mark_node)
7826 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
7827 "traditional C rejects ISO C style function definitions");
7829 /* Now make all the parameter declarations visible in the function body.
7830 We can bypass most of the grunt work of pushdecl. */
7831 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
7833 DECL_CONTEXT (decl) = current_function_decl;
7834 if (DECL_NAME (decl))
7836 bind (DECL_NAME (decl), decl, current_scope,
7837 /*invisible=*/false, /*nested=*/false,
7838 UNKNOWN_LOCATION);
7839 if (!TREE_USED (decl))
7840 warn_if_shadowing (decl);
7842 else
7843 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
7846 /* Record the parameter list in the function declaration. */
7847 DECL_ARGUMENTS (fndecl) = arg_info->parms;
7849 /* Now make all the ancillary declarations visible, likewise. */
7850 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
7852 DECL_CONTEXT (decl) = current_function_decl;
7853 if (DECL_NAME (decl))
7854 bind (DECL_NAME (decl), decl, current_scope,
7855 /*invisible=*/false,
7856 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
7857 UNKNOWN_LOCATION);
7860 /* And all the tag declarations. */
7861 FOR_EACH_VEC_ELT_REVERSE (c_arg_tag, arg_info->tags, ix, tag)
7862 if (tag->id)
7863 bind (tag->id, tag->type, current_scope,
7864 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
7867 /* Subroutine of store_parm_decls which handles old-style function
7868 definitions (separate parameter list and declarations). */
7870 static void
7871 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
7873 struct c_binding *b;
7874 tree parm, decl, last;
7875 tree parmids = arg_info->parms;
7876 struct pointer_set_t *seen_args = pointer_set_create ();
7878 if (!in_system_header)
7879 warning_at (DECL_SOURCE_LOCATION (fndecl),
7880 OPT_Wold_style_definition, "old-style function definition");
7882 /* Match each formal parameter name with its declaration. Save each
7883 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
7884 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
7886 if (TREE_VALUE (parm) == 0)
7888 error_at (DECL_SOURCE_LOCATION (fndecl),
7889 "parameter name missing from parameter list");
7890 TREE_PURPOSE (parm) = 0;
7891 continue;
7894 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
7895 if (b && B_IN_CURRENT_SCOPE (b))
7897 decl = b->decl;
7898 /* Skip erroneous parameters. */
7899 if (decl == error_mark_node)
7900 continue;
7901 /* If we got something other than a PARM_DECL it is an error. */
7902 if (TREE_CODE (decl) != PARM_DECL)
7903 error_at (DECL_SOURCE_LOCATION (decl),
7904 "%qD declared as a non-parameter", decl);
7905 /* If the declaration is already marked, we have a duplicate
7906 name. Complain and ignore the duplicate. */
7907 else if (pointer_set_contains (seen_args, decl))
7909 error_at (DECL_SOURCE_LOCATION (decl),
7910 "multiple parameters named %qD", decl);
7911 TREE_PURPOSE (parm) = 0;
7912 continue;
7914 /* If the declaration says "void", complain and turn it into
7915 an int. */
7916 else if (VOID_TYPE_P (TREE_TYPE (decl)))
7918 error_at (DECL_SOURCE_LOCATION (decl),
7919 "parameter %qD declared with void type", decl);
7920 TREE_TYPE (decl) = integer_type_node;
7921 DECL_ARG_TYPE (decl) = integer_type_node;
7922 layout_decl (decl, 0);
7924 warn_if_shadowing (decl);
7926 /* If no declaration found, default to int. */
7927 else
7929 /* FIXME diagnostics: This should be the location of the argument,
7930 not the FNDECL. E.g., for an old-style declaration
7932 int f10(v) { blah; }
7934 We should use the location of the V, not the F10.
7935 Unfortunately, the V is an IDENTIFIER_NODE which has no
7936 location. In the future we need locations for c_arg_info
7937 entries.
7939 See gcc.dg/Wshadow-3.c for an example of this problem. */
7940 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
7941 PARM_DECL, TREE_VALUE (parm), integer_type_node);
7942 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
7943 pushdecl (decl);
7944 warn_if_shadowing (decl);
7946 if (flag_isoc99)
7947 pedwarn (DECL_SOURCE_LOCATION (decl),
7948 0, "type of %qD defaults to %<int%>", decl);
7949 else
7950 warning_at (DECL_SOURCE_LOCATION (decl),
7951 OPT_Wmissing_parameter_type,
7952 "type of %qD defaults to %<int%>", decl);
7955 TREE_PURPOSE (parm) = decl;
7956 pointer_set_insert (seen_args, decl);
7959 /* Now examine the parms chain for incomplete declarations
7960 and declarations with no corresponding names. */
7962 for (b = current_scope->bindings; b; b = b->prev)
7964 parm = b->decl;
7965 if (TREE_CODE (parm) != PARM_DECL)
7966 continue;
7968 if (TREE_TYPE (parm) != error_mark_node
7969 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
7971 error_at (DECL_SOURCE_LOCATION (parm),
7972 "parameter %qD has incomplete type", parm);
7973 TREE_TYPE (parm) = error_mark_node;
7976 if (!pointer_set_contains (seen_args, parm))
7978 error_at (DECL_SOURCE_LOCATION (parm),
7979 "declaration for parameter %qD but no such parameter",
7980 parm);
7982 /* Pretend the parameter was not missing.
7983 This gets us to a standard state and minimizes
7984 further error messages. */
7985 parmids = chainon (parmids, tree_cons (parm, 0, 0));
7989 /* Chain the declarations together in the order of the list of
7990 names. Store that chain in the function decl, replacing the
7991 list of names. Update the current scope to match. */
7992 DECL_ARGUMENTS (fndecl) = 0;
7994 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
7995 if (TREE_PURPOSE (parm))
7996 break;
7997 if (parm && TREE_PURPOSE (parm))
7999 last = TREE_PURPOSE (parm);
8000 DECL_ARGUMENTS (fndecl) = last;
8002 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8003 if (TREE_PURPOSE (parm))
8005 DECL_CHAIN (last) = TREE_PURPOSE (parm);
8006 last = TREE_PURPOSE (parm);
8008 DECL_CHAIN (last) = 0;
8011 pointer_set_destroy (seen_args);
8013 /* If there was a previous prototype,
8014 set the DECL_ARG_TYPE of each argument according to
8015 the type previously specified, and report any mismatches. */
8017 if (current_function_prototype_arg_types)
8019 tree type;
8020 for (parm = DECL_ARGUMENTS (fndecl),
8021 type = current_function_prototype_arg_types;
8022 parm || (type && TREE_VALUE (type) != error_mark_node
8023 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
8024 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8026 if (parm == 0 || type == 0
8027 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
8029 if (current_function_prototype_built_in)
8030 warning_at (DECL_SOURCE_LOCATION (fndecl),
8031 0, "number of arguments doesn%'t match "
8032 "built-in prototype");
8033 else
8035 /* FIXME diagnostics: This should be the location of
8036 FNDECL, but there is bug when a prototype is
8037 declared inside function context, but defined
8038 outside of it (e.g., gcc.dg/pr15698-2.c). In
8039 which case FNDECL gets the location of the
8040 prototype, not the definition. */
8041 error_at (input_location,
8042 "number of arguments doesn%'t match prototype");
8044 error_at (current_function_prototype_locus,
8045 "prototype declaration");
8047 break;
8049 /* Type for passing arg must be consistent with that
8050 declared for the arg. ISO C says we take the unqualified
8051 type for parameters declared with qualified type. */
8052 if (TREE_TYPE (parm) != error_mark_node
8053 && TREE_TYPE (type) != error_mark_node
8054 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
8055 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
8057 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
8058 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
8060 /* Adjust argument to match prototype. E.g. a previous
8061 `int foo(float);' prototype causes
8062 `int foo(x) float x; {...}' to be treated like
8063 `int foo(float x) {...}'. This is particularly
8064 useful for argument types like uid_t. */
8065 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
8067 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
8068 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
8069 && TYPE_PRECISION (TREE_TYPE (parm))
8070 < TYPE_PRECISION (integer_type_node))
8071 DECL_ARG_TYPE (parm) = integer_type_node;
8073 /* ??? Is it possible to get here with a
8074 built-in prototype or will it always have
8075 been diagnosed as conflicting with an
8076 old-style definition and discarded? */
8077 if (current_function_prototype_built_in)
8078 warning_at (DECL_SOURCE_LOCATION (parm),
8079 OPT_pedantic, "promoted argument %qD "
8080 "doesn%'t match built-in prototype", parm);
8081 else
8083 pedwarn (DECL_SOURCE_LOCATION (parm),
8084 OPT_pedantic, "promoted argument %qD "
8085 "doesn%'t match prototype", parm);
8086 pedwarn (current_function_prototype_locus, OPT_pedantic,
8087 "prototype declaration");
8090 else
8092 if (current_function_prototype_built_in)
8093 warning_at (DECL_SOURCE_LOCATION (parm),
8094 0, "argument %qD doesn%'t match "
8095 "built-in prototype", parm);
8096 else
8098 error_at (DECL_SOURCE_LOCATION (parm),
8099 "argument %qD doesn%'t match prototype", parm);
8100 error_at (current_function_prototype_locus,
8101 "prototype declaration");
8106 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
8109 /* Otherwise, create a prototype that would match. */
8111 else
8113 tree actual = 0, last = 0, type;
8115 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
8117 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
8118 if (last)
8119 TREE_CHAIN (last) = type;
8120 else
8121 actual = type;
8122 last = type;
8124 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
8125 if (last)
8126 TREE_CHAIN (last) = type;
8127 else
8128 actual = type;
8130 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
8131 of the type of this function, but we need to avoid having this
8132 affect the types of other similarly-typed functions, so we must
8133 first force the generation of an identical (but separate) type
8134 node for the relevant function type. The new node we create
8135 will be a variant of the main variant of the original function
8136 type. */
8138 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
8140 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
8144 /* Store parameter declarations passed in ARG_INFO into the current
8145 function declaration. */
8147 void
8148 store_parm_decls_from (struct c_arg_info *arg_info)
8150 current_function_arg_info = arg_info;
8151 store_parm_decls ();
8154 /* Store the parameter declarations into the current function declaration.
8155 This is called after parsing the parameter declarations, before
8156 digesting the body of the function.
8158 For an old-style definition, construct a prototype out of the old-style
8159 parameter declarations and inject it into the function's type. */
8161 void
8162 store_parm_decls (void)
8164 tree fndecl = current_function_decl;
8165 bool proto;
8167 /* The argument information block for FNDECL. */
8168 struct c_arg_info *arg_info = current_function_arg_info;
8169 current_function_arg_info = 0;
8171 /* True if this definition is written with a prototype. Note:
8172 despite C99 6.7.5.3p14, we can *not* treat an empty argument
8173 list in a function definition as equivalent to (void) -- an
8174 empty argument list specifies the function has no parameters,
8175 but only (void) sets up a prototype for future calls. */
8176 proto = arg_info->types != 0;
8178 if (proto)
8179 store_parm_decls_newstyle (fndecl, arg_info);
8180 else
8181 store_parm_decls_oldstyle (fndecl, arg_info);
8183 /* The next call to push_scope will be a function body. */
8185 next_is_function_body = true;
8187 /* Write a record describing this function definition to the prototypes
8188 file (if requested). */
8190 gen_aux_info_record (fndecl, 1, 0, proto);
8192 /* Initialize the RTL code for the function. */
8193 allocate_struct_function (fndecl, false);
8195 /* Begin the statement tree for this function. */
8196 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
8198 /* ??? Insert the contents of the pending sizes list into the function
8199 to be evaluated. The only reason left to have this is
8200 void foo(int n, int array[n++])
8201 because we throw away the array type in favor of a pointer type, and
8202 thus won't naturally see the SAVE_EXPR containing the increment. All
8203 other pending sizes would be handled by gimplify_parameters. */
8204 if (arg_info->pending_sizes)
8205 add_stmt (arg_info->pending_sizes);
8209 /* Finish up a function declaration and compile that function
8210 all the way to assembler language output. Then free the storage
8211 for the function definition.
8213 This is called after parsing the body of the function definition. */
8215 void
8216 finish_function (void)
8218 tree fndecl = current_function_decl;
8220 if (c_dialect_objc ())
8221 objc_finish_function ();
8223 if (TREE_CODE (fndecl) == FUNCTION_DECL
8224 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
8226 tree args = DECL_ARGUMENTS (fndecl);
8227 for (; args; args = DECL_CHAIN (args))
8229 tree type = TREE_TYPE (args);
8230 if (INTEGRAL_TYPE_P (type)
8231 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8232 DECL_ARG_TYPE (args) = integer_type_node;
8236 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
8237 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8239 /* Must mark the RESULT_DECL as being in this function. */
8241 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
8242 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8244 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
8245 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
8246 == integer_type_node && flag_isoc99)
8248 /* Hack. We don't want the middle-end to warn that this return
8249 is unreachable, so we mark its location as special. Using
8250 UNKNOWN_LOCATION has the problem that it gets clobbered in
8251 annotate_one_with_locus. A cleaner solution might be to
8252 ensure ! should_carry_locus_p (stmt), but that needs a flag.
8254 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
8257 /* Tie off the statement tree for this function. */
8258 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
8260 finish_fname_decls ();
8262 /* Complain if there's just no return statement. */
8263 if (warn_return_type
8264 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
8265 && !current_function_returns_value && !current_function_returns_null
8266 /* Don't complain if we are no-return. */
8267 && !current_function_returns_abnormally
8268 /* Don't complain if we are declared noreturn. */
8269 && !TREE_THIS_VOLATILE (fndecl)
8270 /* Don't warn for main(). */
8271 && !MAIN_NAME_P (DECL_NAME (fndecl))
8272 /* Or if they didn't actually specify a return type. */
8273 && !C_FUNCTION_IMPLICIT_INT (fndecl)
8274 /* Normally, with -Wreturn-type, flow will complain, but we might
8275 optimize out static functions. */
8276 && !TREE_PUBLIC (fndecl))
8278 warning (OPT_Wreturn_type,
8279 "no return statement in function returning non-void");
8280 TREE_NO_WARNING (fndecl) = 1;
8283 /* Complain about parameters that are only set, but never otherwise used. */
8284 if (warn_unused_but_set_parameter)
8286 tree decl;
8288 for (decl = DECL_ARGUMENTS (fndecl);
8289 decl;
8290 decl = DECL_CHAIN (decl))
8291 if (TREE_USED (decl)
8292 && TREE_CODE (decl) == PARM_DECL
8293 && !DECL_READ_P (decl)
8294 && DECL_NAME (decl)
8295 && !DECL_ARTIFICIAL (decl)
8296 && !TREE_NO_WARNING (decl))
8297 warning_at (DECL_SOURCE_LOCATION (decl),
8298 OPT_Wunused_but_set_parameter,
8299 "parameter %qD set but not used", decl);
8302 /* Store the end of the function, so that we get good line number
8303 info for the epilogue. */
8304 cfun->function_end_locus = input_location;
8306 /* Finalize the ELF visibility for the function. */
8307 c_determine_visibility (fndecl);
8309 /* For GNU C extern inline functions disregard inline limits. */
8310 if (DECL_EXTERNAL (fndecl)
8311 && DECL_DECLARED_INLINE_P (fndecl))
8312 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
8314 /* Genericize before inlining. Delay genericizing nested functions
8315 until their parent function is genericized. Since finalizing
8316 requires GENERIC, delay that as well. */
8318 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
8319 && !undef_nested_function)
8321 if (!decl_function_context (fndecl))
8323 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
8324 c_genericize (fndecl);
8326 /* ??? Objc emits functions after finalizing the compilation unit.
8327 This should be cleaned up later and this conditional removed. */
8328 if (cgraph_global_info_ready)
8330 cgraph_add_new_function (fndecl, false);
8331 return;
8333 cgraph_finalize_function (fndecl, false);
8335 else
8337 /* Register this function with cgraph just far enough to get it
8338 added to our parent's nested function list. Handy, since the
8339 C front end doesn't have such a list. */
8340 (void) cgraph_get_create_node (fndecl);
8344 if (!decl_function_context (fndecl))
8345 undef_nested_function = false;
8347 /* We're leaving the context of this function, so zap cfun.
8348 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
8349 tree_rest_of_compilation. */
8350 set_cfun (NULL);
8351 current_function_decl = NULL;
8354 /* Check the declarations given in a for-loop for satisfying the C99
8355 constraints. If exactly one such decl is found, return it. LOC is
8356 the location of the opening parenthesis of the for loop. The last
8357 parameter allows you to control the "for loop initial declarations
8358 are only allowed in C99 mode". Normally, you should pass
8359 flag_isoc99 as that parameter. But in some cases (Objective-C
8360 foreach loop, for example) we want to run the checks in this
8361 function even if not in C99 mode, so we allow the caller to turn
8362 off the error about not being in C99 mode.
8365 tree
8366 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
8368 struct c_binding *b;
8369 tree one_decl = NULL_TREE;
8370 int n_decls = 0;
8372 if (!turn_off_iso_c99_error)
8374 static bool hint = true;
8375 /* If we get here, declarations have been used in a for loop without
8376 the C99 for loop scope. This doesn't make much sense, so don't
8377 allow it. */
8378 error_at (loc, "%<for%> loop initial declarations "
8379 "are only allowed in C99 mode");
8380 if (hint)
8382 inform (loc,
8383 "use option -std=c99 or -std=gnu99 to compile your code");
8384 hint = false;
8386 return NULL_TREE;
8388 /* C99 subclause 6.8.5 paragraph 3:
8390 [#3] The declaration part of a for statement shall only
8391 declare identifiers for objects having storage class auto or
8392 register.
8394 It isn't clear whether, in this sentence, "identifiers" binds to
8395 "shall only declare" or to "objects" - that is, whether all identifiers
8396 declared must be identifiers for objects, or whether the restriction
8397 only applies to those that are. (A question on this in comp.std.c
8398 in November 2000 received no answer.) We implement the strictest
8399 interpretation, to avoid creating an extension which later causes
8400 problems. */
8402 for (b = current_scope->bindings; b; b = b->prev)
8404 tree id = b->id;
8405 tree decl = b->decl;
8407 if (!id)
8408 continue;
8410 switch (TREE_CODE (decl))
8412 case VAR_DECL:
8414 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
8415 if (TREE_STATIC (decl))
8416 error_at (decl_loc,
8417 "declaration of static variable %qD in %<for%> loop "
8418 "initial declaration", decl);
8419 else if (DECL_EXTERNAL (decl))
8420 error_at (decl_loc,
8421 "declaration of %<extern%> variable %qD in %<for%> loop "
8422 "initial declaration", decl);
8424 break;
8426 case RECORD_TYPE:
8427 error_at (loc,
8428 "%<struct %E%> declared in %<for%> loop initial "
8429 "declaration", id);
8430 break;
8431 case UNION_TYPE:
8432 error_at (loc,
8433 "%<union %E%> declared in %<for%> loop initial declaration",
8434 id);
8435 break;
8436 case ENUMERAL_TYPE:
8437 error_at (loc, "%<enum %E%> declared in %<for%> loop "
8438 "initial declaration", id);
8439 break;
8440 default:
8441 error_at (loc, "declaration of non-variable "
8442 "%qD in %<for%> loop initial declaration", decl);
8445 n_decls++;
8446 one_decl = decl;
8449 return n_decls == 1 ? one_decl : NULL_TREE;
8452 /* Save and reinitialize the variables
8453 used during compilation of a C function. */
8455 void
8456 c_push_function_context (void)
8458 struct language_function *p;
8459 p = ggc_alloc_language_function ();
8460 cfun->language = p;
8462 p->base.x_stmt_tree = c_stmt_tree;
8463 c_stmt_tree.x_cur_stmt_list
8464 = VEC_copy (tree, gc, c_stmt_tree.x_cur_stmt_list);
8465 p->x_break_label = c_break_label;
8466 p->x_cont_label = c_cont_label;
8467 p->x_switch_stack = c_switch_stack;
8468 p->arg_info = current_function_arg_info;
8469 p->returns_value = current_function_returns_value;
8470 p->returns_null = current_function_returns_null;
8471 p->returns_abnormally = current_function_returns_abnormally;
8472 p->warn_about_return_type = warn_about_return_type;
8474 push_function_context ();
8477 /* Restore the variables used during compilation of a C function. */
8479 void
8480 c_pop_function_context (void)
8482 struct language_function *p;
8484 pop_function_context ();
8485 p = cfun->language;
8486 cfun->language = NULL;
8488 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
8489 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
8491 /* Stop pointing to the local nodes about to be freed. */
8492 /* But DECL_INITIAL must remain nonzero so we know this
8493 was an actual function definition. */
8494 DECL_INITIAL (current_function_decl) = error_mark_node;
8495 DECL_ARGUMENTS (current_function_decl) = 0;
8498 c_stmt_tree = p->base.x_stmt_tree;
8499 c_break_label = p->x_break_label;
8500 c_cont_label = p->x_cont_label;
8501 c_switch_stack = p->x_switch_stack;
8502 current_function_arg_info = p->arg_info;
8503 current_function_returns_value = p->returns_value;
8504 current_function_returns_null = p->returns_null;
8505 current_function_returns_abnormally = p->returns_abnormally;
8506 warn_about_return_type = p->warn_about_return_type;
8509 /* The functions below are required for functionality of doing
8510 function at once processing in the C front end. Currently these
8511 functions are not called from anywhere in the C front end, but as
8512 these changes continue, that will change. */
8514 /* Returns the stmt_tree (if any) to which statements are currently
8515 being added. If there is no active statement-tree, NULL is
8516 returned. */
8518 stmt_tree
8519 current_stmt_tree (void)
8521 return &c_stmt_tree;
8524 /* Return the global value of T as a symbol. */
8526 tree
8527 identifier_global_value (tree t)
8529 struct c_binding *b;
8531 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
8532 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
8533 return b->decl;
8535 return 0;
8538 /* In C, the only C-linkage public declaration is at file scope. */
8540 tree
8541 c_linkage_bindings (tree name)
8543 return identifier_global_value (name);
8546 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
8547 otherwise the name is found in ridpointers from RID_INDEX. */
8549 void
8550 record_builtin_type (enum rid rid_index, const char *name, tree type)
8552 tree id, decl;
8553 if (name == 0)
8554 id = ridpointers[(int) rid_index];
8555 else
8556 id = get_identifier (name);
8557 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
8558 pushdecl (decl);
8559 if (debug_hooks->type_decl)
8560 debug_hooks->type_decl (decl, false);
8563 /* Build the void_list_node (void_type_node having been created). */
8564 tree
8565 build_void_list_node (void)
8567 tree t = build_tree_list (NULL_TREE, void_type_node);
8568 return t;
8571 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
8573 struct c_parm *
8574 build_c_parm (struct c_declspecs *specs, tree attrs,
8575 struct c_declarator *declarator)
8577 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
8578 ret->specs = specs;
8579 ret->attrs = attrs;
8580 ret->declarator = declarator;
8581 return ret;
8584 /* Return a declarator with nested attributes. TARGET is the inner
8585 declarator to which these attributes apply. ATTRS are the
8586 attributes. */
8588 struct c_declarator *
8589 build_attrs_declarator (tree attrs, struct c_declarator *target)
8591 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8592 ret->kind = cdk_attrs;
8593 ret->declarator = target;
8594 ret->u.attrs = attrs;
8595 return ret;
8598 /* Return a declarator for a function with arguments specified by ARGS
8599 and return type specified by TARGET. */
8601 struct c_declarator *
8602 build_function_declarator (struct c_arg_info *args,
8603 struct c_declarator *target)
8605 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8606 ret->kind = cdk_function;
8607 ret->declarator = target;
8608 ret->u.arg_info = args;
8609 return ret;
8612 /* Return a declarator for the identifier IDENT (which may be
8613 NULL_TREE for an abstract declarator). */
8615 struct c_declarator *
8616 build_id_declarator (tree ident)
8618 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8619 ret->kind = cdk_id;
8620 ret->declarator = 0;
8621 ret->u.id = ident;
8622 /* Default value - may get reset to a more precise location. */
8623 ret->id_loc = input_location;
8624 return ret;
8627 /* Return something to represent absolute declarators containing a *.
8628 TARGET is the absolute declarator that the * contains.
8629 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
8630 to apply to the pointer type. */
8632 struct c_declarator *
8633 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
8634 struct c_declarator *target)
8636 tree attrs;
8637 int quals = 0;
8638 struct c_declarator *itarget = target;
8639 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8640 if (type_quals_attrs)
8642 attrs = type_quals_attrs->attrs;
8643 quals = quals_from_declspecs (type_quals_attrs);
8644 if (attrs != NULL_TREE)
8645 itarget = build_attrs_declarator (attrs, target);
8647 ret->kind = cdk_pointer;
8648 ret->declarator = itarget;
8649 ret->u.pointer_quals = quals;
8650 return ret;
8653 /* Return a pointer to a structure for an empty list of declaration
8654 specifiers. */
8656 struct c_declspecs *
8657 build_null_declspecs (void)
8659 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
8660 ret->type = 0;
8661 ret->expr = 0;
8662 ret->decl_attr = 0;
8663 ret->attrs = 0;
8664 ret->typespec_word = cts_none;
8665 ret->storage_class = csc_none;
8666 ret->expr_const_operands = true;
8667 ret->declspecs_seen_p = false;
8668 ret->typespec_kind = ctsk_none;
8669 ret->non_sc_seen_p = false;
8670 ret->typedef_p = false;
8671 ret->explicit_signed_p = false;
8672 ret->deprecated_p = false;
8673 ret->default_int_p = false;
8674 ret->long_p = false;
8675 ret->long_long_p = false;
8676 ret->short_p = false;
8677 ret->signed_p = false;
8678 ret->unsigned_p = false;
8679 ret->complex_p = false;
8680 ret->inline_p = false;
8681 ret->noreturn_p = false;
8682 ret->thread_p = false;
8683 ret->const_p = false;
8684 ret->volatile_p = false;
8685 ret->restrict_p = false;
8686 ret->saturating_p = false;
8687 ret->address_space = ADDR_SPACE_GENERIC;
8688 return ret;
8691 /* Add the address space ADDRSPACE to the declaration specifiers
8692 SPECS, returning SPECS. */
8694 struct c_declspecs *
8695 declspecs_add_addrspace (struct c_declspecs *specs, addr_space_t as)
8697 specs->non_sc_seen_p = true;
8698 specs->declspecs_seen_p = true;
8700 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
8701 && specs->address_space != as)
8702 error ("incompatible address space qualifiers %qs and %qs",
8703 c_addr_space_name (as),
8704 c_addr_space_name (specs->address_space));
8705 else
8706 specs->address_space = as;
8707 return specs;
8710 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
8711 returning SPECS. */
8713 struct c_declspecs *
8714 declspecs_add_qual (struct c_declspecs *specs, tree qual)
8716 enum rid i;
8717 bool dupe = false;
8718 specs->non_sc_seen_p = true;
8719 specs->declspecs_seen_p = true;
8720 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
8721 && C_IS_RESERVED_WORD (qual));
8722 i = C_RID_CODE (qual);
8723 switch (i)
8725 case RID_CONST:
8726 dupe = specs->const_p;
8727 specs->const_p = true;
8728 break;
8729 case RID_VOLATILE:
8730 dupe = specs->volatile_p;
8731 specs->volatile_p = true;
8732 break;
8733 case RID_RESTRICT:
8734 dupe = specs->restrict_p;
8735 specs->restrict_p = true;
8736 break;
8737 default:
8738 gcc_unreachable ();
8740 if (dupe && !flag_isoc99)
8741 pedwarn (input_location, OPT_pedantic, "duplicate %qE", qual);
8742 return specs;
8745 /* Add the type specifier TYPE to the declaration specifiers SPECS,
8746 returning SPECS. */
8748 struct c_declspecs *
8749 declspecs_add_type (location_t loc, struct c_declspecs *specs,
8750 struct c_typespec spec)
8752 tree type = spec.spec;
8753 specs->non_sc_seen_p = true;
8754 specs->declspecs_seen_p = true;
8755 specs->typespec_kind = spec.kind;
8756 if (TREE_DEPRECATED (type))
8757 specs->deprecated_p = true;
8759 /* Handle type specifier keywords. */
8760 if (TREE_CODE (type) == IDENTIFIER_NODE
8761 && C_IS_RESERVED_WORD (type)
8762 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
8764 enum rid i = C_RID_CODE (type);
8765 if (specs->type)
8767 error_at (loc, "two or more data types in declaration specifiers");
8768 return specs;
8770 if ((int) i <= (int) RID_LAST_MODIFIER)
8772 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
8773 bool dupe = false;
8774 switch (i)
8776 case RID_LONG:
8777 if (specs->long_long_p)
8779 error_at (loc, "%<long long long%> is too long for GCC");
8780 break;
8782 if (specs->long_p)
8784 if (specs->typespec_word == cts_double)
8786 error_at (loc,
8787 ("both %<long long%> and %<double%> in "
8788 "declaration specifiers"));
8789 break;
8791 pedwarn_c90 (loc, OPT_Wlong_long,
8792 "ISO C90 does not support %<long long%>");
8793 specs->long_long_p = 1;
8794 break;
8796 if (specs->short_p)
8797 error_at (loc,
8798 ("both %<long%> and %<short%> in "
8799 "declaration specifiers"));
8800 else if (specs->typespec_word == cts_void)
8801 error_at (loc,
8802 ("both %<long%> and %<void%> in "
8803 "declaration specifiers"));
8804 else if (specs->typespec_word == cts_int128)
8805 error_at (loc,
8806 ("both %<long%> and %<__int128%> in "
8807 "declaration specifiers"));
8808 else if (specs->typespec_word == cts_bool)
8809 error_at (loc,
8810 ("both %<long%> and %<_Bool%> in "
8811 "declaration specifiers"));
8812 else if (specs->typespec_word == cts_char)
8813 error_at (loc,
8814 ("both %<long%> and %<char%> in "
8815 "declaration specifiers"));
8816 else if (specs->typespec_word == cts_float)
8817 error_at (loc,
8818 ("both %<long%> and %<float%> in "
8819 "declaration specifiers"));
8820 else if (specs->typespec_word == cts_dfloat32)
8821 error_at (loc,
8822 ("both %<long%> and %<_Decimal32%> in "
8823 "declaration specifiers"));
8824 else if (specs->typespec_word == cts_dfloat64)
8825 error_at (loc,
8826 ("both %<long%> and %<_Decimal64%> in "
8827 "declaration specifiers"));
8828 else if (specs->typespec_word == cts_dfloat128)
8829 error_at (loc,
8830 ("both %<long%> and %<_Decimal128%> in "
8831 "declaration specifiers"));
8832 else
8833 specs->long_p = true;
8834 break;
8835 case RID_SHORT:
8836 dupe = specs->short_p;
8837 if (specs->long_p)
8838 error_at (loc,
8839 ("both %<long%> and %<short%> in "
8840 "declaration specifiers"));
8841 else if (specs->typespec_word == cts_void)
8842 error_at (loc,
8843 ("both %<short%> and %<void%> in "
8844 "declaration specifiers"));
8845 else if (specs->typespec_word == cts_int128)
8846 error_at (loc,
8847 ("both %<short%> and %<__int128%> in "
8848 "declaration specifiers"));
8849 else if (specs->typespec_word == cts_bool)
8850 error_at (loc,
8851 ("both %<short%> and %<_Bool%> in "
8852 "declaration specifiers"));
8853 else if (specs->typespec_word == cts_char)
8854 error_at (loc,
8855 ("both %<short%> and %<char%> in "
8856 "declaration specifiers"));
8857 else if (specs->typespec_word == cts_float)
8858 error_at (loc,
8859 ("both %<short%> and %<float%> in "
8860 "declaration specifiers"));
8861 else if (specs->typespec_word == cts_double)
8862 error_at (loc,
8863 ("both %<short%> and %<double%> in "
8864 "declaration specifiers"));
8865 else if (specs->typespec_word == cts_dfloat32)
8866 error_at (loc,
8867 ("both %<short%> and %<_Decimal32%> in "
8868 "declaration specifiers"));
8869 else if (specs->typespec_word == cts_dfloat64)
8870 error_at (loc,
8871 ("both %<short%> and %<_Decimal64%> in "
8872 "declaration specifiers"));
8873 else if (specs->typespec_word == cts_dfloat128)
8874 error_at (loc,
8875 ("both %<short%> and %<_Decimal128%> in "
8876 "declaration specifiers"));
8877 else
8878 specs->short_p = true;
8879 break;
8880 case RID_SIGNED:
8881 dupe = specs->signed_p;
8882 if (specs->unsigned_p)
8883 error_at (loc,
8884 ("both %<signed%> and %<unsigned%> in "
8885 "declaration specifiers"));
8886 else if (specs->typespec_word == cts_void)
8887 error_at (loc,
8888 ("both %<signed%> and %<void%> in "
8889 "declaration specifiers"));
8890 else if (specs->typespec_word == cts_bool)
8891 error_at (loc,
8892 ("both %<signed%> and %<_Bool%> in "
8893 "declaration specifiers"));
8894 else if (specs->typespec_word == cts_float)
8895 error_at (loc,
8896 ("both %<signed%> and %<float%> in "
8897 "declaration specifiers"));
8898 else if (specs->typespec_word == cts_double)
8899 error_at (loc,
8900 ("both %<signed%> and %<double%> in "
8901 "declaration specifiers"));
8902 else if (specs->typespec_word == cts_dfloat32)
8903 error_at (loc,
8904 ("both %<signed%> and %<_Decimal32%> in "
8905 "declaration specifiers"));
8906 else if (specs->typespec_word == cts_dfloat64)
8907 error_at (loc,
8908 ("both %<signed%> and %<_Decimal64%> in "
8909 "declaration specifiers"));
8910 else if (specs->typespec_word == cts_dfloat128)
8911 error_at (loc,
8912 ("both %<signed%> and %<_Decimal128%> in "
8913 "declaration specifiers"));
8914 else
8915 specs->signed_p = true;
8916 break;
8917 case RID_UNSIGNED:
8918 dupe = specs->unsigned_p;
8919 if (specs->signed_p)
8920 error_at (loc,
8921 ("both %<signed%> and %<unsigned%> in "
8922 "declaration specifiers"));
8923 else if (specs->typespec_word == cts_void)
8924 error_at (loc,
8925 ("both %<unsigned%> and %<void%> in "
8926 "declaration specifiers"));
8927 else if (specs->typespec_word == cts_bool)
8928 error_at (loc,
8929 ("both %<unsigned%> and %<_Bool%> in "
8930 "declaration specifiers"));
8931 else if (specs->typespec_word == cts_float)
8932 error_at (loc,
8933 ("both %<unsigned%> and %<float%> in "
8934 "declaration specifiers"));
8935 else if (specs->typespec_word == cts_double)
8936 error_at (loc,
8937 ("both %<unsigned%> and %<double%> in "
8938 "declaration specifiers"));
8939 else if (specs->typespec_word == cts_dfloat32)
8940 error_at (loc,
8941 ("both %<unsigned%> and %<_Decimal32%> in "
8942 "declaration specifiers"));
8943 else if (specs->typespec_word == cts_dfloat64)
8944 error_at (loc,
8945 ("both %<unsigned%> and %<_Decimal64%> in "
8946 "declaration specifiers"));
8947 else if (specs->typespec_word == cts_dfloat128)
8948 error_at (loc,
8949 ("both %<unsigned%> and %<_Decimal128%> in "
8950 "declaration specifiers"));
8951 else
8952 specs->unsigned_p = true;
8953 break;
8954 case RID_COMPLEX:
8955 dupe = specs->complex_p;
8956 if (!flag_isoc99 && !in_system_header)
8957 pedwarn (loc, OPT_pedantic,
8958 "ISO C90 does not support complex types");
8959 if (specs->typespec_word == cts_void)
8960 error_at (loc,
8961 ("both %<complex%> and %<void%> in "
8962 "declaration specifiers"));
8963 else if (specs->typespec_word == cts_bool)
8964 error_at (loc,
8965 ("both %<complex%> and %<_Bool%> in "
8966 "declaration specifiers"));
8967 else if (specs->typespec_word == cts_dfloat32)
8968 error_at (loc,
8969 ("both %<complex%> and %<_Decimal32%> in "
8970 "declaration specifiers"));
8971 else if (specs->typespec_word == cts_dfloat64)
8972 error_at (loc,
8973 ("both %<complex%> and %<_Decimal64%> in "
8974 "declaration specifiers"));
8975 else if (specs->typespec_word == cts_dfloat128)
8976 error_at (loc,
8977 ("both %<complex%> and %<_Decimal128%> in "
8978 "declaration specifiers"));
8979 else if (specs->typespec_word == cts_fract)
8980 error_at (loc,
8981 ("both %<complex%> and %<_Fract%> in "
8982 "declaration specifiers"));
8983 else if (specs->typespec_word == cts_accum)
8984 error_at (loc,
8985 ("both %<complex%> and %<_Accum%> in "
8986 "declaration specifiers"));
8987 else if (specs->saturating_p)
8988 error_at (loc,
8989 ("both %<complex%> and %<_Sat%> in "
8990 "declaration specifiers"));
8991 else
8992 specs->complex_p = true;
8993 break;
8994 case RID_SAT:
8995 dupe = specs->saturating_p;
8996 pedwarn (loc, OPT_pedantic,
8997 "ISO C does not support saturating types");
8998 if (specs->typespec_word == cts_int128)
9000 error_at (loc,
9001 ("both %<_Sat%> and %<__int128%> in "
9002 "declaration specifiers"));
9004 else if (specs->typespec_word == cts_void)
9005 error_at (loc,
9006 ("both %<_Sat%> and %<void%> in "
9007 "declaration specifiers"));
9008 else if (specs->typespec_word == cts_bool)
9009 error_at (loc,
9010 ("both %<_Sat%> and %<_Bool%> in "
9011 "declaration specifiers"));
9012 else if (specs->typespec_word == cts_char)
9013 error_at (loc,
9014 ("both %<_Sat%> and %<char%> in "
9015 "declaration specifiers"));
9016 else if (specs->typespec_word == cts_int)
9017 error_at (loc,
9018 ("both %<_Sat%> and %<int%> in "
9019 "declaration specifiers"));
9020 else if (specs->typespec_word == cts_float)
9021 error_at (loc,
9022 ("both %<_Sat%> and %<float%> in "
9023 "declaration specifiers"));
9024 else if (specs->typespec_word == cts_double)
9025 error_at (loc,
9026 ("both %<_Sat%> and %<double%> in "
9027 "declaration specifiers"));
9028 else if (specs->typespec_word == cts_dfloat32)
9029 error_at (loc,
9030 ("both %<_Sat%> and %<_Decimal32%> in "
9031 "declaration specifiers"));
9032 else if (specs->typespec_word == cts_dfloat64)
9033 error_at (loc,
9034 ("both %<_Sat%> and %<_Decimal64%> in "
9035 "declaration specifiers"));
9036 else if (specs->typespec_word == cts_dfloat128)
9037 error_at (loc,
9038 ("both %<_Sat%> and %<_Decimal128%> in "
9039 "declaration specifiers"));
9040 else if (specs->complex_p)
9041 error_at (loc,
9042 ("both %<_Sat%> and %<complex%> in "
9043 "declaration specifiers"));
9044 else
9045 specs->saturating_p = true;
9046 break;
9047 default:
9048 gcc_unreachable ();
9051 if (dupe)
9052 error_at (loc, "duplicate %qE", type);
9054 return specs;
9056 else
9058 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
9059 "__int128", "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
9060 if (specs->typespec_word != cts_none)
9062 error_at (loc,
9063 "two or more data types in declaration specifiers");
9064 return specs;
9066 switch (i)
9068 case RID_INT128:
9069 if (int128_integer_type_node == NULL_TREE)
9071 error_at (loc, "%<__int128%> is not supported for this target");
9072 return specs;
9074 if (!in_system_header)
9075 pedwarn (loc, OPT_pedantic,
9076 "ISO C does not support %<__int128%> type");
9078 if (specs->long_p)
9079 error_at (loc,
9080 ("both %<__int128%> and %<long%> in "
9081 "declaration specifiers"));
9082 else if (specs->saturating_p)
9083 error_at (loc,
9084 ("both %<_Sat%> and %<__int128%> in "
9085 "declaration specifiers"));
9086 else if (specs->short_p)
9087 error_at (loc,
9088 ("both %<__int128%> and %<short%> in "
9089 "declaration specifiers"));
9090 else
9091 specs->typespec_word = cts_int128;
9092 return specs;
9093 case RID_VOID:
9094 if (specs->long_p)
9095 error_at (loc,
9096 ("both %<long%> and %<void%> in "
9097 "declaration specifiers"));
9098 else if (specs->short_p)
9099 error_at (loc,
9100 ("both %<short%> and %<void%> in "
9101 "declaration specifiers"));
9102 else if (specs->signed_p)
9103 error_at (loc,
9104 ("both %<signed%> and %<void%> in "
9105 "declaration specifiers"));
9106 else if (specs->unsigned_p)
9107 error_at (loc,
9108 ("both %<unsigned%> and %<void%> in "
9109 "declaration specifiers"));
9110 else if (specs->complex_p)
9111 error_at (loc,
9112 ("both %<complex%> and %<void%> in "
9113 "declaration specifiers"));
9114 else if (specs->saturating_p)
9115 error_at (loc,
9116 ("both %<_Sat%> and %<void%> in "
9117 "declaration specifiers"));
9118 else
9119 specs->typespec_word = cts_void;
9120 return specs;
9121 case RID_BOOL:
9122 if (specs->long_p)
9123 error_at (loc,
9124 ("both %<long%> and %<_Bool%> in "
9125 "declaration specifiers"));
9126 else if (specs->short_p)
9127 error_at (loc,
9128 ("both %<short%> and %<_Bool%> in "
9129 "declaration specifiers"));
9130 else if (specs->signed_p)
9131 error_at (loc,
9132 ("both %<signed%> and %<_Bool%> in "
9133 "declaration specifiers"));
9134 else if (specs->unsigned_p)
9135 error_at (loc,
9136 ("both %<unsigned%> and %<_Bool%> in "
9137 "declaration specifiers"));
9138 else if (specs->complex_p)
9139 error_at (loc,
9140 ("both %<complex%> and %<_Bool%> in "
9141 "declaration specifiers"));
9142 else if (specs->saturating_p)
9143 error_at (loc,
9144 ("both %<_Sat%> and %<_Bool%> in "
9145 "declaration specifiers"));
9146 else
9147 specs->typespec_word = cts_bool;
9148 return specs;
9149 case RID_CHAR:
9150 if (specs->long_p)
9151 error_at (loc,
9152 ("both %<long%> and %<char%> in "
9153 "declaration specifiers"));
9154 else if (specs->short_p)
9155 error_at (loc,
9156 ("both %<short%> and %<char%> in "
9157 "declaration specifiers"));
9158 else if (specs->saturating_p)
9159 error_at (loc,
9160 ("both %<_Sat%> and %<char%> in "
9161 "declaration specifiers"));
9162 else
9163 specs->typespec_word = cts_char;
9164 return specs;
9165 case RID_INT:
9166 if (specs->saturating_p)
9167 error_at (loc,
9168 ("both %<_Sat%> and %<int%> in "
9169 "declaration specifiers"));
9170 else
9171 specs->typespec_word = cts_int;
9172 return specs;
9173 case RID_FLOAT:
9174 if (specs->long_p)
9175 error_at (loc,
9176 ("both %<long%> and %<float%> in "
9177 "declaration specifiers"));
9178 else if (specs->short_p)
9179 error_at (loc,
9180 ("both %<short%> and %<float%> in "
9181 "declaration specifiers"));
9182 else if (specs->signed_p)
9183 error_at (loc,
9184 ("both %<signed%> and %<float%> in "
9185 "declaration specifiers"));
9186 else if (specs->unsigned_p)
9187 error_at (loc,
9188 ("both %<unsigned%> and %<float%> in "
9189 "declaration specifiers"));
9190 else if (specs->saturating_p)
9191 error_at (loc,
9192 ("both %<_Sat%> and %<float%> in "
9193 "declaration specifiers"));
9194 else
9195 specs->typespec_word = cts_float;
9196 return specs;
9197 case RID_DOUBLE:
9198 if (specs->long_long_p)
9199 error_at (loc,
9200 ("both %<long long%> and %<double%> in "
9201 "declaration specifiers"));
9202 else if (specs->short_p)
9203 error_at (loc,
9204 ("both %<short%> and %<double%> in "
9205 "declaration specifiers"));
9206 else if (specs->signed_p)
9207 error_at (loc,
9208 ("both %<signed%> and %<double%> in "
9209 "declaration specifiers"));
9210 else if (specs->unsigned_p)
9211 error_at (loc,
9212 ("both %<unsigned%> and %<double%> in "
9213 "declaration specifiers"));
9214 else if (specs->saturating_p)
9215 error_at (loc,
9216 ("both %<_Sat%> and %<double%> in "
9217 "declaration specifiers"));
9218 else
9219 specs->typespec_word = cts_double;
9220 return specs;
9221 case RID_DFLOAT32:
9222 case RID_DFLOAT64:
9223 case RID_DFLOAT128:
9225 const char *str;
9226 if (i == RID_DFLOAT32)
9227 str = "_Decimal32";
9228 else if (i == RID_DFLOAT64)
9229 str = "_Decimal64";
9230 else
9231 str = "_Decimal128";
9232 if (specs->long_long_p)
9233 error_at (loc,
9234 ("both %<long long%> and %<%s%> in "
9235 "declaration specifiers"),
9236 str);
9237 if (specs->long_p)
9238 error_at (loc,
9239 ("both %<long%> and %<%s%> in "
9240 "declaration specifiers"),
9241 str);
9242 else if (specs->short_p)
9243 error_at (loc,
9244 ("both %<short%> and %<%s%> in "
9245 "declaration specifiers"),
9246 str);
9247 else if (specs->signed_p)
9248 error_at (loc,
9249 ("both %<signed%> and %<%s%> in "
9250 "declaration specifiers"),
9251 str);
9252 else if (specs->unsigned_p)
9253 error_at (loc,
9254 ("both %<unsigned%> and %<%s%> in "
9255 "declaration specifiers"),
9256 str);
9257 else if (specs->complex_p)
9258 error_at (loc,
9259 ("both %<complex%> and %<%s%> in "
9260 "declaration specifiers"),
9261 str);
9262 else if (specs->saturating_p)
9263 error_at (loc,
9264 ("both %<_Sat%> and %<%s%> in "
9265 "declaration specifiers"),
9266 str);
9267 else if (i == RID_DFLOAT32)
9268 specs->typespec_word = cts_dfloat32;
9269 else if (i == RID_DFLOAT64)
9270 specs->typespec_word = cts_dfloat64;
9271 else
9272 specs->typespec_word = cts_dfloat128;
9274 if (!targetm.decimal_float_supported_p ())
9275 error_at (loc,
9276 ("decimal floating point not supported "
9277 "for this target"));
9278 pedwarn (loc, OPT_pedantic,
9279 "ISO C does not support decimal floating point");
9280 return specs;
9281 case RID_FRACT:
9282 case RID_ACCUM:
9284 const char *str;
9285 if (i == RID_FRACT)
9286 str = "_Fract";
9287 else
9288 str = "_Accum";
9289 if (specs->complex_p)
9290 error_at (loc,
9291 ("both %<complex%> and %<%s%> in "
9292 "declaration specifiers"),
9293 str);
9294 else if (i == RID_FRACT)
9295 specs->typespec_word = cts_fract;
9296 else
9297 specs->typespec_word = cts_accum;
9299 if (!targetm.fixed_point_supported_p ())
9300 error_at (loc,
9301 "fixed-point types not supported for this target");
9302 pedwarn (loc, OPT_pedantic,
9303 "ISO C does not support fixed-point types");
9304 return specs;
9305 default:
9306 /* ObjC reserved word "id", handled below. */
9307 break;
9312 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
9313 form of ObjC type, cases such as "int" and "long" being handled
9314 above), a TYPE (struct, union, enum and typeof specifiers) or an
9315 ERROR_MARK. In none of these cases may there have previously
9316 been any type specifiers. */
9317 if (specs->type || specs->typespec_word != cts_none
9318 || specs->long_p || specs->short_p || specs->signed_p
9319 || specs->unsigned_p || specs->complex_p)
9320 error_at (loc, "two or more data types in declaration specifiers");
9321 else if (TREE_CODE (type) == TYPE_DECL)
9323 if (TREE_TYPE (type) == error_mark_node)
9324 ; /* Allow the type to default to int to avoid cascading errors. */
9325 else
9327 specs->type = TREE_TYPE (type);
9328 specs->decl_attr = DECL_ATTRIBUTES (type);
9329 specs->typedef_p = true;
9330 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
9332 /* If this typedef name is defined in a struct, then a C++
9333 lookup would return a different value. */
9334 if (warn_cxx_compat
9335 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
9336 warning_at (loc, OPT_Wc___compat,
9337 "C++ lookup of %qD would return a field, not a type",
9338 type);
9340 /* If we are parsing a struct, record that a struct field
9341 used a typedef. */
9342 if (warn_cxx_compat && struct_parse_info != NULL)
9343 VEC_safe_push (tree, heap, struct_parse_info->typedefs_seen, type);
9346 else if (TREE_CODE (type) == IDENTIFIER_NODE)
9348 tree t = lookup_name (type);
9349 if (!t || TREE_CODE (t) != TYPE_DECL)
9350 error_at (loc, "%qE fails to be a typedef or built in type", type);
9351 else if (TREE_TYPE (t) == error_mark_node)
9353 else
9354 specs->type = TREE_TYPE (t);
9356 else
9358 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
9360 specs->typedef_p = true;
9361 if (spec.expr)
9363 if (specs->expr)
9364 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
9365 specs->expr, spec.expr);
9366 else
9367 specs->expr = spec.expr;
9368 specs->expr_const_operands &= spec.expr_const_operands;
9371 specs->type = type;
9374 return specs;
9377 /* Add the storage class specifier or function specifier SCSPEC to the
9378 declaration specifiers SPECS, returning SPECS. */
9380 struct c_declspecs *
9381 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
9383 enum rid i;
9384 enum c_storage_class n = csc_none;
9385 bool dupe = false;
9386 specs->declspecs_seen_p = true;
9387 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
9388 && C_IS_RESERVED_WORD (scspec));
9389 i = C_RID_CODE (scspec);
9390 if (specs->non_sc_seen_p)
9391 warning (OPT_Wold_style_declaration,
9392 "%qE is not at beginning of declaration", scspec);
9393 switch (i)
9395 case RID_INLINE:
9396 /* C99 permits duplicate inline. Although of doubtful utility,
9397 it seems simplest to permit it in gnu89 mode as well, as
9398 there is also little utility in maintaining this as a
9399 difference between gnu89 and C99 inline. */
9400 dupe = false;
9401 specs->inline_p = true;
9402 break;
9403 case RID_NORETURN:
9404 /* Duplicate _Noreturn is permitted. */
9405 dupe = false;
9406 specs->noreturn_p = true;
9407 break;
9408 case RID_THREAD:
9409 dupe = specs->thread_p;
9410 if (specs->storage_class == csc_auto)
9411 error ("%<__thread%> used with %<auto%>");
9412 else if (specs->storage_class == csc_register)
9413 error ("%<__thread%> used with %<register%>");
9414 else if (specs->storage_class == csc_typedef)
9415 error ("%<__thread%> used with %<typedef%>");
9416 else
9417 specs->thread_p = true;
9418 break;
9419 case RID_AUTO:
9420 n = csc_auto;
9421 break;
9422 case RID_EXTERN:
9423 n = csc_extern;
9424 /* Diagnose "__thread extern". */
9425 if (specs->thread_p)
9426 error ("%<__thread%> before %<extern%>");
9427 break;
9428 case RID_REGISTER:
9429 n = csc_register;
9430 break;
9431 case RID_STATIC:
9432 n = csc_static;
9433 /* Diagnose "__thread static". */
9434 if (specs->thread_p)
9435 error ("%<__thread%> before %<static%>");
9436 break;
9437 case RID_TYPEDEF:
9438 n = csc_typedef;
9439 break;
9440 default:
9441 gcc_unreachable ();
9443 if (n != csc_none && n == specs->storage_class)
9444 dupe = true;
9445 if (dupe)
9446 error ("duplicate %qE", scspec);
9447 if (n != csc_none)
9449 if (specs->storage_class != csc_none && n != specs->storage_class)
9451 error ("multiple storage classes in declaration specifiers");
9453 else
9455 specs->storage_class = n;
9456 if (n != csc_extern && n != csc_static && specs->thread_p)
9458 error ("%<__thread%> used with %qE", scspec);
9459 specs->thread_p = false;
9463 return specs;
9466 /* Add the attributes ATTRS to the declaration specifiers SPECS,
9467 returning SPECS. */
9469 struct c_declspecs *
9470 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
9472 specs->attrs = chainon (attrs, specs->attrs);
9473 specs->declspecs_seen_p = true;
9474 return specs;
9477 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
9478 specifiers with any other type specifier to determine the resulting
9479 type. This is where ISO C checks on complex types are made, since
9480 "_Complex long" is a prefix of the valid ISO C type "_Complex long
9481 double". */
9483 struct c_declspecs *
9484 finish_declspecs (struct c_declspecs *specs)
9486 /* If a type was specified as a whole, we have no modifiers and are
9487 done. */
9488 if (specs->type != NULL_TREE)
9490 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9491 && !specs->signed_p && !specs->unsigned_p
9492 && !specs->complex_p);
9494 /* Set a dummy type. */
9495 if (TREE_CODE (specs->type) == ERROR_MARK)
9496 specs->type = integer_type_node;
9497 return specs;
9500 /* If none of "void", "_Bool", "char", "int", "float" or "double"
9501 has been specified, treat it as "int" unless "_Complex" is
9502 present and there are no other specifiers. If we just have
9503 "_Complex", it is equivalent to "_Complex double", but e.g.
9504 "_Complex short" is equivalent to "_Complex short int". */
9505 if (specs->typespec_word == cts_none)
9507 if (specs->saturating_p)
9509 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
9510 if (!targetm.fixed_point_supported_p ())
9511 error ("fixed-point types not supported for this target");
9512 specs->typespec_word = cts_fract;
9514 else if (specs->long_p || specs->short_p
9515 || specs->signed_p || specs->unsigned_p)
9517 specs->typespec_word = cts_int;
9519 else if (specs->complex_p)
9521 specs->typespec_word = cts_double;
9522 pedwarn (input_location, OPT_pedantic,
9523 "ISO C does not support plain %<complex%> meaning "
9524 "%<double complex%>");
9526 else
9528 specs->typespec_word = cts_int;
9529 specs->default_int_p = true;
9530 /* We don't diagnose this here because grokdeclarator will
9531 give more specific diagnostics according to whether it is
9532 a function definition. */
9536 /* If "signed" was specified, record this to distinguish "int" and
9537 "signed int" in the case of a bit-field with
9538 -funsigned-bitfields. */
9539 specs->explicit_signed_p = specs->signed_p;
9541 /* Now compute the actual type. */
9542 switch (specs->typespec_word)
9544 case cts_void:
9545 gcc_assert (!specs->long_p && !specs->short_p
9546 && !specs->signed_p && !specs->unsigned_p
9547 && !specs->complex_p);
9548 specs->type = void_type_node;
9549 break;
9550 case cts_bool:
9551 gcc_assert (!specs->long_p && !specs->short_p
9552 && !specs->signed_p && !specs->unsigned_p
9553 && !specs->complex_p);
9554 specs->type = boolean_type_node;
9555 break;
9556 case cts_char:
9557 gcc_assert (!specs->long_p && !specs->short_p);
9558 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9559 if (specs->signed_p)
9560 specs->type = signed_char_type_node;
9561 else if (specs->unsigned_p)
9562 specs->type = unsigned_char_type_node;
9563 else
9564 specs->type = char_type_node;
9565 if (specs->complex_p)
9567 pedwarn (input_location, OPT_pedantic,
9568 "ISO C does not support complex integer types");
9569 specs->type = build_complex_type (specs->type);
9571 break;
9572 case cts_int128:
9573 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
9574 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9575 specs->type = (specs->unsigned_p
9576 ? int128_unsigned_type_node
9577 : int128_integer_type_node);
9578 if (specs->complex_p)
9580 pedwarn (input_location, OPT_pedantic,
9581 "ISO C does not support complex integer types");
9582 specs->type = build_complex_type (specs->type);
9584 break;
9585 case cts_int:
9586 gcc_assert (!(specs->long_p && specs->short_p));
9587 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9588 if (specs->long_long_p)
9589 specs->type = (specs->unsigned_p
9590 ? long_long_unsigned_type_node
9591 : long_long_integer_type_node);
9592 else if (specs->long_p)
9593 specs->type = (specs->unsigned_p
9594 ? long_unsigned_type_node
9595 : long_integer_type_node);
9596 else if (specs->short_p)
9597 specs->type = (specs->unsigned_p
9598 ? short_unsigned_type_node
9599 : short_integer_type_node);
9600 else
9601 specs->type = (specs->unsigned_p
9602 ? unsigned_type_node
9603 : integer_type_node);
9604 if (specs->complex_p)
9606 pedwarn (input_location, OPT_pedantic,
9607 "ISO C does not support complex integer types");
9608 specs->type = build_complex_type (specs->type);
9610 break;
9611 case cts_float:
9612 gcc_assert (!specs->long_p && !specs->short_p
9613 && !specs->signed_p && !specs->unsigned_p);
9614 specs->type = (specs->complex_p
9615 ? complex_float_type_node
9616 : float_type_node);
9617 break;
9618 case cts_double:
9619 gcc_assert (!specs->long_long_p && !specs->short_p
9620 && !specs->signed_p && !specs->unsigned_p);
9621 if (specs->long_p)
9623 specs->type = (specs->complex_p
9624 ? complex_long_double_type_node
9625 : long_double_type_node);
9627 else
9629 specs->type = (specs->complex_p
9630 ? complex_double_type_node
9631 : double_type_node);
9633 break;
9634 case cts_dfloat32:
9635 case cts_dfloat64:
9636 case cts_dfloat128:
9637 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9638 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
9639 if (specs->typespec_word == cts_dfloat32)
9640 specs->type = dfloat32_type_node;
9641 else if (specs->typespec_word == cts_dfloat64)
9642 specs->type = dfloat64_type_node;
9643 else
9644 specs->type = dfloat128_type_node;
9645 break;
9646 case cts_fract:
9647 gcc_assert (!specs->complex_p);
9648 if (!targetm.fixed_point_supported_p ())
9649 specs->type = integer_type_node;
9650 else if (specs->saturating_p)
9652 if (specs->long_long_p)
9653 specs->type = specs->unsigned_p
9654 ? sat_unsigned_long_long_fract_type_node
9655 : sat_long_long_fract_type_node;
9656 else if (specs->long_p)
9657 specs->type = specs->unsigned_p
9658 ? sat_unsigned_long_fract_type_node
9659 : sat_long_fract_type_node;
9660 else if (specs->short_p)
9661 specs->type = specs->unsigned_p
9662 ? sat_unsigned_short_fract_type_node
9663 : sat_short_fract_type_node;
9664 else
9665 specs->type = specs->unsigned_p
9666 ? sat_unsigned_fract_type_node
9667 : sat_fract_type_node;
9669 else
9671 if (specs->long_long_p)
9672 specs->type = specs->unsigned_p
9673 ? unsigned_long_long_fract_type_node
9674 : long_long_fract_type_node;
9675 else if (specs->long_p)
9676 specs->type = specs->unsigned_p
9677 ? unsigned_long_fract_type_node
9678 : long_fract_type_node;
9679 else if (specs->short_p)
9680 specs->type = specs->unsigned_p
9681 ? unsigned_short_fract_type_node
9682 : short_fract_type_node;
9683 else
9684 specs->type = specs->unsigned_p
9685 ? unsigned_fract_type_node
9686 : fract_type_node;
9688 break;
9689 case cts_accum:
9690 gcc_assert (!specs->complex_p);
9691 if (!targetm.fixed_point_supported_p ())
9692 specs->type = integer_type_node;
9693 else if (specs->saturating_p)
9695 if (specs->long_long_p)
9696 specs->type = specs->unsigned_p
9697 ? sat_unsigned_long_long_accum_type_node
9698 : sat_long_long_accum_type_node;
9699 else if (specs->long_p)
9700 specs->type = specs->unsigned_p
9701 ? sat_unsigned_long_accum_type_node
9702 : sat_long_accum_type_node;
9703 else if (specs->short_p)
9704 specs->type = specs->unsigned_p
9705 ? sat_unsigned_short_accum_type_node
9706 : sat_short_accum_type_node;
9707 else
9708 specs->type = specs->unsigned_p
9709 ? sat_unsigned_accum_type_node
9710 : sat_accum_type_node;
9712 else
9714 if (specs->long_long_p)
9715 specs->type = specs->unsigned_p
9716 ? unsigned_long_long_accum_type_node
9717 : long_long_accum_type_node;
9718 else if (specs->long_p)
9719 specs->type = specs->unsigned_p
9720 ? unsigned_long_accum_type_node
9721 : long_accum_type_node;
9722 else if (specs->short_p)
9723 specs->type = specs->unsigned_p
9724 ? unsigned_short_accum_type_node
9725 : short_accum_type_node;
9726 else
9727 specs->type = specs->unsigned_p
9728 ? unsigned_accum_type_node
9729 : accum_type_node;
9731 break;
9732 default:
9733 gcc_unreachable ();
9736 return specs;
9739 /* A subroutine of c_write_global_declarations. Perform final processing
9740 on one file scope's declarations (or the external scope's declarations),
9741 GLOBALS. */
9743 static void
9744 c_write_global_declarations_1 (tree globals)
9746 tree decl;
9747 bool reconsider;
9749 /* Process the decls in the order they were written. */
9750 for (decl = globals; decl; decl = DECL_CHAIN (decl))
9752 /* Check for used but undefined static functions using the C
9753 standard's definition of "used", and set TREE_NO_WARNING so
9754 that check_global_declarations doesn't repeat the check. */
9755 if (TREE_CODE (decl) == FUNCTION_DECL
9756 && DECL_INITIAL (decl) == 0
9757 && DECL_EXTERNAL (decl)
9758 && !TREE_PUBLIC (decl)
9759 && C_DECL_USED (decl))
9761 pedwarn (input_location, 0, "%q+F used but never defined", decl);
9762 TREE_NO_WARNING (decl) = 1;
9765 wrapup_global_declaration_1 (decl);
9770 reconsider = false;
9771 for (decl = globals; decl; decl = DECL_CHAIN (decl))
9772 reconsider |= wrapup_global_declaration_2 (decl);
9774 while (reconsider);
9776 for (decl = globals; decl; decl = DECL_CHAIN (decl))
9777 check_global_declaration_1 (decl);
9780 /* A subroutine of c_write_global_declarations Emit debug information for each
9781 of the declarations in GLOBALS. */
9783 static void
9784 c_write_global_declarations_2 (tree globals)
9786 tree decl;
9788 for (decl = globals; decl ; decl = DECL_CHAIN (decl))
9789 debug_hooks->global_decl (decl);
9792 /* Callback to collect a source_ref from a DECL. */
9794 static void
9795 collect_source_ref_cb (tree decl)
9797 if (!DECL_IS_BUILTIN (decl))
9798 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
9801 /* Collect all references relevant to SOURCE_FILE. */
9803 static void
9804 collect_all_refs (const char *source_file)
9806 tree t;
9807 unsigned i;
9809 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
9810 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
9813 /* Iterate over all global declarations and call CALLBACK. */
9815 static void
9816 for_each_global_decl (void (*callback) (tree decl))
9818 tree t;
9819 tree decls;
9820 tree decl;
9821 unsigned i;
9823 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
9825 decls = DECL_INITIAL (t);
9826 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
9827 callback (decl);
9831 /* Preserve the external declarations scope across a garbage collect. */
9832 static GTY(()) tree ext_block;
9834 void
9835 c_write_global_declarations (void)
9837 tree t;
9838 unsigned i;
9840 /* We don't want to do this if generating a PCH. */
9841 if (pch_file)
9842 return;
9844 timevar_start (TV_PHASE_DEFERRED);
9846 /* Do the Objective-C stuff. This is where all the Objective-C
9847 module stuff gets generated (symtab, class/protocol/selector
9848 lists etc). */
9849 if (c_dialect_objc ())
9850 objc_write_global_declarations ();
9852 /* Close the external scope. */
9853 ext_block = pop_scope ();
9854 external_scope = 0;
9855 gcc_assert (!current_scope);
9857 /* Handle -fdump-ada-spec[-slim]. */
9858 if (dump_enabled_p (TDI_ada))
9860 /* Build a table of files to generate specs for */
9861 if (get_dump_file_info (TDI_ada)->flags & TDF_SLIM)
9862 collect_source_ref (main_input_filename);
9863 else
9864 for_each_global_decl (collect_source_ref_cb);
9866 dump_ada_specs (collect_all_refs, NULL);
9869 if (ext_block)
9871 tree tmp = BLOCK_VARS (ext_block);
9872 int flags;
9873 FILE * stream = dump_begin (TDI_tu, &flags);
9874 if (stream && tmp)
9876 dump_node (tmp, flags & ~TDF_SLIM, stream);
9877 dump_end (TDI_tu, stream);
9881 /* Process all file scopes in this compilation, and the external_scope,
9882 through wrapup_global_declarations and check_global_declarations. */
9883 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
9884 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
9885 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
9887 timevar_stop (TV_PHASE_DEFERRED);
9888 timevar_start (TV_PHASE_CGRAPH);
9890 /* We're done parsing; proceed to optimize and emit assembly.
9891 FIXME: shouldn't be the front end's responsibility to call this. */
9892 cgraph_finalize_compilation_unit ();
9894 timevar_stop (TV_PHASE_CGRAPH);
9895 timevar_start (TV_PHASE_DBGINFO);
9897 /* After cgraph has had a chance to emit everything that's going to
9898 be emitted, output debug information for globals. */
9899 if (!seen_error ())
9901 timevar_push (TV_SYMOUT);
9902 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
9903 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
9904 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
9905 timevar_pop (TV_SYMOUT);
9908 ext_block = NULL;
9909 timevar_stop (TV_PHASE_DBGINFO);
9912 /* Register reserved keyword WORD as qualifier for address space AS. */
9914 void
9915 c_register_addr_space (const char *word, addr_space_t as)
9917 int rid = RID_FIRST_ADDR_SPACE + as;
9918 tree id;
9920 /* Address space qualifiers are only supported
9921 in C with GNU extensions enabled. */
9922 if (c_dialect_objc () || flag_no_asm)
9923 return;
9925 id = get_identifier (word);
9926 C_SET_RID_CODE (id, rid);
9927 C_IS_RESERVED_WORD (id) = 1;
9928 ridpointers [rid] = id;
9931 #include "gt-c-decl.h"