c-common.c (c_common_nodes_and_builtins): Use cxx11 in lieu of cxx0x.
[official-gcc.git] / gcc / c / c-decl.c
blob8170a80ac6d08d1d44a9df343c6109365bb38206
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* Process declarations and symbol lookup for C front end.
21 Also constructs types; the standard scalar types at initialization,
22 and structure, union, array and enum types when they are declared. */
24 /* ??? not all decl nodes are given the most useful possible
25 line numbers. For example, the CONST_DECLs for enum values. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "input.h"
31 #include "tm.h"
32 #include "intl.h"
33 #include "tree.h"
34 #include "tree-inline.h"
35 #include "flags.h"
36 #include "function.h"
37 #include "c-tree.h"
38 #include "toplev.h"
39 #include "tm_p.h"
40 #include "cpplib.h"
41 #include "target.h"
42 #include "debug.h"
43 #include "opts.h"
44 #include "timevar.h"
45 #include "c-family/c-common.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h"
48 #include "c-lang.h"
49 #include "langhooks.h"
50 #include "tree-iterator.h"
51 #include "diagnostic-core.h"
52 #include "dumpfile.h"
53 #include "cgraph.h"
54 #include "hash-table.h"
55 #include "langhooks-def.h"
56 #include "pointer-set.h"
57 #include "plugin.h"
58 #include "c-family/c-ada-spec.h"
60 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
61 enum decl_context
62 { NORMAL, /* Ordinary declaration */
63 FUNCDEF, /* Function definition */
64 PARM, /* Declaration of parm before function body */
65 FIELD, /* Declaration inside struct or union */
66 TYPENAME}; /* Typename (inside cast or sizeof) */
68 /* States indicating how grokdeclarator() should handle declspecs marked
69 with __attribute__((deprecated)). An object declared as
70 __attribute__((deprecated)) suppresses warnings of uses of other
71 deprecated items. */
73 enum deprecated_states {
74 DEPRECATED_NORMAL,
75 DEPRECATED_SUPPRESS
79 /* Nonzero if we have seen an invalid cross reference
80 to a struct, union, or enum, but not yet printed the message. */
81 tree pending_invalid_xref;
83 /* File and line to appear in the eventual error message. */
84 location_t pending_invalid_xref_location;
86 /* The file and line that the prototype came from if this is an
87 old-style definition; used for diagnostics in
88 store_parm_decls_oldstyle. */
90 static location_t current_function_prototype_locus;
92 /* Whether this prototype was built-in. */
94 static bool current_function_prototype_built_in;
96 /* The argument type information of this prototype. */
98 static tree current_function_prototype_arg_types;
100 /* The argument information structure for the function currently being
101 defined. */
103 static struct c_arg_info *current_function_arg_info;
105 /* The obstack on which parser and related data structures, which are
106 not live beyond their top-level declaration or definition, are
107 allocated. */
108 struct obstack parser_obstack;
110 /* The current statement tree. */
112 static GTY(()) struct stmt_tree_s c_stmt_tree;
114 /* State saving variables. */
115 tree c_break_label;
116 tree c_cont_label;
118 /* A list of decls to be made automatically visible in each file scope. */
119 static GTY(()) tree visible_builtins;
121 /* Set to 0 at beginning of a function definition, set to 1 if
122 a return statement that specifies a return value is seen. */
124 int current_function_returns_value;
126 /* Set to 0 at beginning of a function definition, set to 1 if
127 a return statement with no argument is seen. */
129 int current_function_returns_null;
131 /* Set to 0 at beginning of a function definition, set to 1 if
132 a call to a noreturn function is seen. */
134 int current_function_returns_abnormally;
136 /* Set to nonzero by `grokdeclarator' for a function
137 whose return type is defaulted, if warnings for this are desired. */
139 static int warn_about_return_type;
141 /* Nonzero when the current toplevel function contains a declaration
142 of a nested function which is never defined. */
144 static bool undef_nested_function;
146 /* Mode used to build pointers (VOIDmode means ptr_mode). */
148 enum machine_mode c_default_pointer_mode = VOIDmode;
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;
288 /* The additional information we keep track of for a label binding.
289 These fields are updated as scopes are popped. */
291 struct GTY(()) c_label_vars {
292 /* The shadowed c_label_vars, when one label shadows another (which
293 can only happen using a __label__ declaration). */
294 struct c_label_vars *shadowed;
295 /* The bindings when the label was defined. */
296 struct c_spot_bindings label_bindings;
297 /* A list of decls that we care about: decls about which we should
298 warn if a goto branches to this label from later in the function.
299 Decls are added to this list as scopes are popped. We only add
300 the decls that matter. */
301 vec<tree, va_gc> *decls_in_scope;
302 /* A list of goto statements to this label. This is only used for
303 goto statements seen before the label was defined, so that we can
304 issue appropriate warnings for them. */
305 vec<c_goto_bindings_p, va_gc> *gotos;
308 /* Each c_scope structure describes the complete contents of one
309 scope. Four scopes are distinguished specially: the innermost or
310 current scope, the innermost function scope, the file scope (always
311 the second to outermost) and the outermost or external scope.
313 Most declarations are recorded in the current scope.
315 All normal label declarations are recorded in the innermost
316 function scope, as are bindings of undeclared identifiers to
317 error_mark_node. (GCC permits nested functions as an extension,
318 hence the 'innermost' qualifier.) Explicitly declared labels
319 (using the __label__ extension) appear in the current scope.
321 Being in the file scope (current_scope == file_scope) causes
322 special behavior in several places below. Also, under some
323 conditions the Objective-C front end records declarations in the
324 file scope even though that isn't the current scope.
326 All declarations with external linkage are recorded in the external
327 scope, even if they aren't visible there; this models the fact that
328 such declarations are visible to the entire program, and (with a
329 bit of cleverness, see pushdecl) allows diagnosis of some violations
330 of C99 6.2.2p7 and 6.2.7p2:
332 If, within the same translation unit, the same identifier appears
333 with both internal and external linkage, the behavior is
334 undefined.
336 All declarations that refer to the same object or function shall
337 have compatible type; otherwise, the behavior is undefined.
339 Initially only the built-in declarations, which describe compiler
340 intrinsic functions plus a subset of the standard library, are in
341 this scope.
343 The order of the blocks list matters, and it is frequently appended
344 to. To avoid having to walk all the way to the end of the list on
345 each insertion, or reverse the list later, we maintain a pointer to
346 the last list entry. (FIXME: It should be feasible to use a reversed
347 list here.)
349 The bindings list is strictly in reverse order of declarations;
350 pop_scope relies on this. */
353 struct GTY((chain_next ("%h.outer"))) c_scope {
354 /* The scope containing this one. */
355 struct c_scope *outer;
357 /* The next outermost function scope. */
358 struct c_scope *outer_function;
360 /* All bindings in this scope. */
361 struct c_binding *bindings;
363 /* For each scope (except the global one), a chain of BLOCK nodes
364 for all the scopes that were entered and exited one level down. */
365 tree blocks;
366 tree blocks_last;
368 /* The depth of this scope. Used to keep the ->shadowed chain of
369 bindings sorted innermost to outermost. */
370 unsigned int depth : 28;
372 /* True if we are currently filling this scope with parameter
373 declarations. */
374 BOOL_BITFIELD parm_flag : 1;
376 /* True if we saw [*] in this scope. Used to give an error messages
377 if these appears in a function definition. */
378 BOOL_BITFIELD had_vla_unspec : 1;
380 /* True if we already complained about forward parameter decls
381 in this scope. This prevents double warnings on
382 foo (int a; int b; ...) */
383 BOOL_BITFIELD warned_forward_parm_decls : 1;
385 /* True if this is the outermost block scope of a function body.
386 This scope contains the parameters, the local variables declared
387 in the outermost block, and all the labels (except those in
388 nested functions, or declared at block scope with __label__). */
389 BOOL_BITFIELD function_body : 1;
391 /* True means make a BLOCK for this scope no matter what. */
392 BOOL_BITFIELD keep : 1;
394 /* True means that an unsuffixed float constant is _Decimal64. */
395 BOOL_BITFIELD float_const_decimal64 : 1;
397 /* True if this scope has any label bindings. This is used to speed
398 up searching for labels when popping scopes, particularly since
399 labels are normally only found at function scope. */
400 BOOL_BITFIELD has_label_bindings : 1;
402 /* True if we should issue a warning if a goto statement crosses any
403 of the bindings. We still need to check the list of bindings to
404 find the specific ones we need to warn about. This is true if
405 decl_jump_unsafe would return true for any of the bindings. This
406 is used to avoid looping over all the bindings unnecessarily. */
407 BOOL_BITFIELD has_jump_unsafe_decl : 1;
410 /* The scope currently in effect. */
412 static GTY(()) struct c_scope *current_scope;
414 /* The innermost function scope. Ordinary (not explicitly declared)
415 labels, bindings to error_mark_node, and the lazily-created
416 bindings of __func__ and its friends get this scope. */
418 static GTY(()) struct c_scope *current_function_scope;
420 /* The C file scope. This is reset for each input translation unit. */
422 static GTY(()) struct c_scope *file_scope;
424 /* The outermost scope. This is used for all declarations with
425 external linkage, and only these, hence the name. */
427 static GTY(()) struct c_scope *external_scope;
429 /* A chain of c_scope structures awaiting reuse. */
431 static GTY((deletable)) struct c_scope *scope_freelist;
433 /* A chain of c_binding structures awaiting reuse. */
435 static GTY((deletable)) struct c_binding *binding_freelist;
437 /* Append VAR to LIST in scope SCOPE. */
438 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
439 struct c_scope *s_ = (scope); \
440 tree d_ = (decl); \
441 if (s_->list##_last) \
442 BLOCK_CHAIN (s_->list##_last) = d_; \
443 else \
444 s_->list = d_; \
445 s_->list##_last = d_; \
446 } while (0)
448 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
449 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
450 struct c_scope *t_ = (tscope); \
451 struct c_scope *f_ = (fscope); \
452 if (t_->to##_last) \
453 BLOCK_CHAIN (t_->to##_last) = f_->from; \
454 else \
455 t_->to = f_->from; \
456 t_->to##_last = f_->from##_last; \
457 } while (0)
459 /* A c_inline_static structure stores details of a static identifier
460 referenced in a definition of a function that may be an inline
461 definition if no subsequent declaration of that function uses
462 "extern" or does not use "inline". */
464 struct GTY((chain_next ("%h.next"))) c_inline_static {
465 /* The location for a diagnostic. */
466 location_t location;
468 /* The function that may be an inline definition. */
469 tree function;
471 /* The object or function referenced. */
472 tree static_decl;
474 /* What sort of reference this is. */
475 enum c_inline_static_type type;
477 /* The next such structure or NULL. */
478 struct c_inline_static *next;
481 /* List of static identifiers used or referenced in functions that may
482 be inline definitions. */
483 static GTY(()) struct c_inline_static *c_inline_statics;
485 /* True means unconditionally make a BLOCK for the next scope pushed. */
487 static bool keep_next_level_flag;
489 /* True means the next call to push_scope will be the outermost scope
490 of a function body, so do not push a new scope, merely cease
491 expecting parameter decls. */
493 static bool next_is_function_body;
495 /* A vector of pointers to c_binding structures. */
497 typedef struct c_binding *c_binding_ptr;
499 /* Information that we keep for a struct or union while it is being
500 parsed. */
502 struct c_struct_parse_info
504 /* If warn_cxx_compat, a list of types defined within this
505 struct. */
506 vec<tree> struct_types;
507 /* If warn_cxx_compat, a list of field names which have bindings,
508 and which are defined in this struct, but which are not defined
509 in any enclosing struct. This is used to clear the in_struct
510 field of the c_bindings structure. */
511 vec<c_binding_ptr> fields;
512 /* If warn_cxx_compat, a list of typedef names used when defining
513 fields in this struct. */
514 vec<tree> typedefs_seen;
517 /* Information for the struct or union currently being parsed, or
518 NULL if not parsing a struct or union. */
519 static struct c_struct_parse_info *struct_parse_info;
521 /* Forward declarations. */
522 static tree lookup_name_in_scope (tree, struct c_scope *);
523 static tree c_make_fname_decl (location_t, tree, int);
524 static tree grokdeclarator (const struct c_declarator *,
525 struct c_declspecs *,
526 enum decl_context, bool, tree *, tree *, tree *,
527 bool *, enum deprecated_states);
528 static tree grokparms (struct c_arg_info *, bool);
529 static void layout_array_type (tree);
531 /* T is a statement. Add it to the statement-tree. This is the
532 C/ObjC version--C++ has a slightly different version of this
533 function. */
535 tree
536 add_stmt (tree t)
538 enum tree_code code = TREE_CODE (t);
540 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
542 if (!EXPR_HAS_LOCATION (t))
543 SET_EXPR_LOCATION (t, input_location);
546 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
547 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
549 /* Add T to the statement-tree. Non-side-effect statements need to be
550 recorded during statement expressions. */
551 if (!building_stmt_list_p ())
552 push_stmt_list ();
553 append_to_statement_list_force (t, &cur_stmt_list);
555 return t;
558 /* Build a pointer type using the default pointer mode. */
560 static tree
561 c_build_pointer_type (tree to_type)
563 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
564 : TYPE_ADDR_SPACE (to_type);
565 enum machine_mode pointer_mode;
567 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
568 pointer_mode = targetm.addr_space.pointer_mode (as);
569 else
570 pointer_mode = c_default_pointer_mode;
571 return build_pointer_type_for_mode (to_type, pointer_mode, false);
575 /* Return true if we will want to say something if a goto statement
576 crosses DECL. */
578 static bool
579 decl_jump_unsafe (tree decl)
581 if (error_operand_p (decl))
582 return false;
584 /* Always warn about crossing variably modified types. */
585 if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == TYPE_DECL)
586 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
587 return true;
589 /* Otherwise, only warn if -Wgoto-misses-init and this is an
590 initialized automatic decl. */
591 if (warn_jump_misses_init
592 && TREE_CODE (decl) == VAR_DECL
593 && !TREE_STATIC (decl)
594 && DECL_INITIAL (decl) != NULL_TREE)
595 return true;
597 return false;
601 void
602 c_print_identifier (FILE *file, tree node, int indent)
604 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
605 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
606 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
607 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
609 tree rid = ridpointers[C_RID_CODE (node)];
610 indent_to (file, indent + 4);
611 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
612 (void *) rid, IDENTIFIER_POINTER (rid));
616 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
617 which may be any of several kinds of DECL or TYPE or error_mark_node,
618 in the scope SCOPE. */
619 static void
620 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
621 bool nested, location_t locus)
623 struct c_binding *b, **here;
625 if (binding_freelist)
627 b = binding_freelist;
628 binding_freelist = b->prev;
630 else
631 b = ggc_alloc_c_binding ();
633 b->shadowed = 0;
634 b->decl = decl;
635 b->id = name;
636 b->depth = scope->depth;
637 b->invisible = invisible;
638 b->nested = nested;
639 b->inner_comp = 0;
640 b->in_struct = 0;
641 b->locus = locus;
643 b->u.type = NULL;
645 b->prev = scope->bindings;
646 scope->bindings = b;
648 if (decl_jump_unsafe (decl))
649 scope->has_jump_unsafe_decl = 1;
651 if (!name)
652 return;
654 switch (TREE_CODE (decl))
656 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
657 case ENUMERAL_TYPE:
658 case UNION_TYPE:
659 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
660 case VAR_DECL:
661 case FUNCTION_DECL:
662 case TYPE_DECL:
663 case CONST_DECL:
664 case PARM_DECL:
665 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
667 default:
668 gcc_unreachable ();
671 /* Locate the appropriate place in the chain of shadowed decls
672 to insert this binding. Normally, scope == current_scope and
673 this does nothing. */
674 while (*here && (*here)->depth > scope->depth)
675 here = &(*here)->shadowed;
677 b->shadowed = *here;
678 *here = b;
681 /* Clear the binding structure B, stick it on the binding_freelist,
682 and return the former value of b->prev. This is used by pop_scope
683 and get_parm_info to iterate destructively over all the bindings
684 from a given scope. */
685 static struct c_binding *
686 free_binding_and_advance (struct c_binding *b)
688 struct c_binding *prev = b->prev;
690 memset (b, 0, sizeof (struct c_binding));
691 b->prev = binding_freelist;
692 binding_freelist = b;
694 return prev;
697 /* Bind a label. Like bind, but skip fields which aren't used for
698 labels, and add the LABEL_VARS value. */
699 static void
700 bind_label (tree name, tree label, struct c_scope *scope,
701 struct c_label_vars *label_vars)
703 struct c_binding *b;
705 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
706 UNKNOWN_LOCATION);
708 scope->has_label_bindings = true;
710 b = scope->bindings;
711 gcc_assert (b->decl == label);
712 label_vars->shadowed = b->u.label;
713 b->u.label = label_vars;
716 /* Hook called at end of compilation to assume 1 elt
717 for a file-scope tentative array defn that wasn't complete before. */
719 void
720 c_finish_incomplete_decl (tree decl)
722 if (TREE_CODE (decl) == VAR_DECL)
724 tree type = TREE_TYPE (decl);
725 if (type != error_mark_node
726 && TREE_CODE (type) == ARRAY_TYPE
727 && !DECL_EXTERNAL (decl)
728 && TYPE_DOMAIN (type) == 0)
730 warning_at (DECL_SOURCE_LOCATION (decl),
731 0, "array %q+D assumed to have one element", decl);
733 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
735 relayout_decl (decl);
740 /* Record that inline function FUNC contains a reference (location
741 LOC) to static DECL (file-scope or function-local according to
742 TYPE). */
744 void
745 record_inline_static (location_t loc, tree func, tree decl,
746 enum c_inline_static_type type)
748 struct c_inline_static *csi = ggc_alloc_c_inline_static ();
749 csi->location = loc;
750 csi->function = func;
751 csi->static_decl = decl;
752 csi->type = type;
753 csi->next = c_inline_statics;
754 c_inline_statics = csi;
757 /* Check for references to static declarations in inline functions at
758 the end of the translation unit and diagnose them if the functions
759 are still inline definitions. */
761 static void
762 check_inline_statics (void)
764 struct c_inline_static *csi;
765 for (csi = c_inline_statics; csi; csi = csi->next)
767 if (DECL_EXTERNAL (csi->function))
768 switch (csi->type)
770 case csi_internal:
771 pedwarn (csi->location, 0,
772 "%qD is static but used in inline function %qD "
773 "which is not static", csi->static_decl, csi->function);
774 break;
775 case csi_modifiable:
776 pedwarn (csi->location, 0,
777 "%q+D is static but declared in inline function %qD "
778 "which is not static", csi->static_decl, csi->function);
779 break;
780 default:
781 gcc_unreachable ();
784 c_inline_statics = NULL;
787 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
788 for the current state, otherwise set it to uninitialized. */
790 static void
791 set_spot_bindings (struct c_spot_bindings *p, bool defining)
793 if (defining)
795 p->scope = current_scope;
796 p->bindings_in_scope = current_scope->bindings;
798 else
800 p->scope = NULL;
801 p->bindings_in_scope = NULL;
803 p->stmt_exprs = 0;
804 p->left_stmt_expr = false;
807 /* Update spot bindings P as we pop out of SCOPE. Return true if we
808 should push decls for a label. */
810 static bool
811 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
813 if (p->scope != scope)
815 /* This label or goto is defined in some other scope, or it is a
816 label which is not yet defined. There is nothing to
817 update. */
818 return false;
821 /* Adjust the spot bindings to refer to the bindings already defined
822 in the enclosing scope. */
823 p->scope = scope->outer;
824 p->bindings_in_scope = p->scope->bindings;
826 return true;
829 /* The Objective-C front-end often needs to determine the current scope. */
831 void *
832 objc_get_current_scope (void)
834 return current_scope;
837 /* The following function is used only by Objective-C. It needs to live here
838 because it accesses the innards of c_scope. */
840 void
841 objc_mark_locals_volatile (void *enclosing_blk)
843 struct c_scope *scope;
844 struct c_binding *b;
846 for (scope = current_scope;
847 scope && scope != enclosing_blk;
848 scope = scope->outer)
850 for (b = scope->bindings; b; b = b->prev)
851 objc_volatilize_decl (b->decl);
853 /* Do not climb up past the current function. */
854 if (scope->function_body)
855 break;
859 /* Return true if we are in the global binding level. */
861 bool
862 global_bindings_p (void)
864 return current_scope == file_scope;
867 void
868 keep_next_level (void)
870 keep_next_level_flag = true;
873 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
875 void
876 set_float_const_decimal64 (void)
878 current_scope->float_const_decimal64 = true;
881 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
883 void
884 clear_float_const_decimal64 (void)
886 current_scope->float_const_decimal64 = false;
889 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
891 bool
892 float_const_decimal64_p (void)
894 return current_scope->float_const_decimal64;
897 /* Identify this scope as currently being filled with parameters. */
899 void
900 declare_parm_level (void)
902 current_scope->parm_flag = true;
905 void
906 push_scope (void)
908 if (next_is_function_body)
910 /* This is the transition from the parameters to the top level
911 of the function body. These are the same scope
912 (C99 6.2.1p4,6) so we do not push another scope structure.
913 next_is_function_body is set only by store_parm_decls, which
914 in turn is called when and only when we are about to
915 encounter the opening curly brace for the function body.
917 The outermost block of a function always gets a BLOCK node,
918 because the debugging output routines expect that each
919 function has at least one BLOCK. */
920 current_scope->parm_flag = false;
921 current_scope->function_body = true;
922 current_scope->keep = true;
923 current_scope->outer_function = current_function_scope;
924 current_function_scope = current_scope;
926 keep_next_level_flag = false;
927 next_is_function_body = false;
929 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
930 if (current_scope->outer)
931 current_scope->float_const_decimal64
932 = current_scope->outer->float_const_decimal64;
933 else
934 current_scope->float_const_decimal64 = false;
936 else
938 struct c_scope *scope;
939 if (scope_freelist)
941 scope = scope_freelist;
942 scope_freelist = scope->outer;
944 else
945 scope = ggc_alloc_cleared_c_scope ();
947 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
948 if (current_scope)
949 scope->float_const_decimal64 = current_scope->float_const_decimal64;
950 else
951 scope->float_const_decimal64 = false;
953 scope->keep = keep_next_level_flag;
954 scope->outer = current_scope;
955 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
957 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
958 possible. */
959 if (current_scope && scope->depth == 0)
961 scope->depth--;
962 sorry ("GCC supports only %u nested scopes", scope->depth);
965 current_scope = scope;
966 keep_next_level_flag = false;
970 /* This is called when we are leaving SCOPE. For each label defined
971 in SCOPE, add any appropriate decls to its decls_in_scope fields.
972 These are the decls whose initialization will be skipped by a goto
973 later in the function. */
975 static void
976 update_label_decls (struct c_scope *scope)
978 struct c_scope *s;
980 s = scope;
981 while (s != NULL)
983 if (s->has_label_bindings)
985 struct c_binding *b;
987 for (b = s->bindings; b != NULL; b = b->prev)
989 struct c_label_vars *label_vars;
990 struct c_binding *b1;
991 bool hjud;
992 unsigned int ix;
993 struct c_goto_bindings *g;
995 if (TREE_CODE (b->decl) != LABEL_DECL)
996 continue;
997 label_vars = b->u.label;
999 b1 = label_vars->label_bindings.bindings_in_scope;
1000 if (label_vars->label_bindings.scope == NULL)
1001 hjud = false;
1002 else
1003 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1004 if (update_spot_bindings (scope, &label_vars->label_bindings))
1006 /* This label is defined in this scope. */
1007 if (hjud)
1009 for (; b1 != NULL; b1 = b1->prev)
1011 /* A goto from later in the function to this
1012 label will never see the initialization
1013 of B1, if any. Save it to issue a
1014 warning if needed. */
1015 if (decl_jump_unsafe (b1->decl))
1016 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1021 /* Update the bindings of any goto statements associated
1022 with this label. */
1023 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1024 update_spot_bindings (scope, &g->goto_bindings);
1028 /* Don't search beyond the current function. */
1029 if (s == current_function_scope)
1030 break;
1032 s = s->outer;
1036 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1038 static void
1039 set_type_context (tree type, tree context)
1041 for (type = TYPE_MAIN_VARIANT (type); type;
1042 type = TYPE_NEXT_VARIANT (type))
1043 TYPE_CONTEXT (type) = context;
1046 /* Exit a scope. Restore the state of the identifier-decl mappings
1047 that were in effect when this scope was entered. Return a BLOCK
1048 node containing all the DECLs in this scope that are of interest
1049 to debug info generation. */
1051 tree
1052 pop_scope (void)
1054 struct c_scope *scope = current_scope;
1055 tree block, context, p;
1056 struct c_binding *b;
1058 bool functionbody = scope->function_body;
1059 bool keep = functionbody || scope->keep || scope->bindings;
1061 update_label_decls (scope);
1063 /* If appropriate, create a BLOCK to record the decls for the life
1064 of this function. */
1065 block = 0;
1066 if (keep)
1068 block = make_node (BLOCK);
1069 BLOCK_SUBBLOCKS (block) = scope->blocks;
1070 TREE_USED (block) = 1;
1072 /* In each subblock, record that this is its superior. */
1073 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1074 BLOCK_SUPERCONTEXT (p) = block;
1076 BLOCK_VARS (block) = 0;
1079 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1080 scope must be set so that they point to the appropriate
1081 construct, i.e. either to the current FUNCTION_DECL node, or
1082 else to the BLOCK node we just constructed.
1084 Note that for tagged types whose scope is just the formal
1085 parameter list for some function type specification, we can't
1086 properly set their TYPE_CONTEXTs here, because we don't have a
1087 pointer to the appropriate FUNCTION_TYPE node readily available
1088 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1089 type nodes get set in `grokdeclarator' as soon as we have created
1090 the FUNCTION_TYPE node which will represent the "scope" for these
1091 "parameter list local" tagged types. */
1092 if (scope->function_body)
1093 context = current_function_decl;
1094 else if (scope == file_scope)
1096 tree file_decl = build_translation_unit_decl (NULL_TREE);
1097 context = file_decl;
1099 else
1100 context = block;
1102 /* Clear all bindings in this scope. */
1103 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1105 p = b->decl;
1106 switch (TREE_CODE (p))
1108 case LABEL_DECL:
1109 /* Warnings for unused labels, errors for undefined labels. */
1110 if (TREE_USED (p) && !DECL_INITIAL (p))
1112 error ("label %q+D used but not defined", p);
1113 DECL_INITIAL (p) = error_mark_node;
1115 else
1116 warn_for_unused_label (p);
1118 /* Labels go in BLOCK_VARS. */
1119 DECL_CHAIN (p) = BLOCK_VARS (block);
1120 BLOCK_VARS (block) = p;
1121 gcc_assert (I_LABEL_BINDING (b->id) == b);
1122 I_LABEL_BINDING (b->id) = b->shadowed;
1124 /* Also pop back to the shadowed label_vars. */
1125 release_tree_vector (b->u.label->decls_in_scope);
1126 b->u.label = b->u.label->shadowed;
1127 break;
1129 case ENUMERAL_TYPE:
1130 case UNION_TYPE:
1131 case RECORD_TYPE:
1132 set_type_context (p, context);
1134 /* Types may not have tag-names, in which case the type
1135 appears in the bindings list with b->id NULL. */
1136 if (b->id)
1138 gcc_assert (I_TAG_BINDING (b->id) == b);
1139 I_TAG_BINDING (b->id) = b->shadowed;
1141 break;
1143 case FUNCTION_DECL:
1144 /* Propagate TREE_ADDRESSABLE from nested functions to their
1145 containing functions. */
1146 if (!TREE_ASM_WRITTEN (p)
1147 && DECL_INITIAL (p) != 0
1148 && TREE_ADDRESSABLE (p)
1149 && DECL_ABSTRACT_ORIGIN (p) != 0
1150 && DECL_ABSTRACT_ORIGIN (p) != p)
1151 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1152 if (!DECL_EXTERNAL (p)
1153 && !DECL_INITIAL (p)
1154 && scope != file_scope
1155 && scope != external_scope)
1157 error ("nested function %q+D declared but never defined", p);
1158 undef_nested_function = true;
1160 else if (DECL_DECLARED_INLINE_P (p)
1161 && TREE_PUBLIC (p)
1162 && !DECL_INITIAL (p))
1164 /* C99 6.7.4p6: "a function with external linkage... declared
1165 with an inline function specifier ... shall also be defined
1166 in the same translation unit." */
1167 if (!flag_gnu89_inline)
1168 pedwarn (input_location, 0,
1169 "inline function %q+D declared but never defined", p);
1170 DECL_EXTERNAL (p) = 1;
1173 goto common_symbol;
1175 case VAR_DECL:
1176 /* Warnings for unused variables. */
1177 if ((!TREE_USED (p) || !DECL_READ_P (p))
1178 && !TREE_NO_WARNING (p)
1179 && !DECL_IN_SYSTEM_HEADER (p)
1180 && DECL_NAME (p)
1181 && !DECL_ARTIFICIAL (p)
1182 && scope != file_scope
1183 && scope != external_scope)
1185 if (!TREE_USED (p))
1186 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1187 else if (DECL_CONTEXT (p) == current_function_decl)
1188 warning_at (DECL_SOURCE_LOCATION (p),
1189 OPT_Wunused_but_set_variable,
1190 "variable %qD set but not used", p);
1193 if (b->inner_comp)
1195 error ("type of array %q+D completed incompatibly with"
1196 " implicit initialization", p);
1199 /* Fall through. */
1200 case TYPE_DECL:
1201 case CONST_DECL:
1202 common_symbol:
1203 /* All of these go in BLOCK_VARS, but only if this is the
1204 binding in the home scope. */
1205 if (!b->nested)
1207 DECL_CHAIN (p) = BLOCK_VARS (block);
1208 BLOCK_VARS (block) = p;
1210 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1212 /* For block local externs add a special
1213 DECL_EXTERNAL decl for debug info generation. */
1214 tree extp = copy_node (p);
1216 DECL_EXTERNAL (extp) = 1;
1217 TREE_STATIC (extp) = 0;
1218 TREE_PUBLIC (extp) = 1;
1219 DECL_INITIAL (extp) = NULL_TREE;
1220 DECL_LANG_SPECIFIC (extp) = NULL;
1221 DECL_CONTEXT (extp) = current_function_decl;
1222 if (TREE_CODE (p) == FUNCTION_DECL)
1224 DECL_RESULT (extp) = NULL_TREE;
1225 DECL_SAVED_TREE (extp) = NULL_TREE;
1226 DECL_STRUCT_FUNCTION (extp) = NULL;
1228 if (b->locus != UNKNOWN_LOCATION)
1229 DECL_SOURCE_LOCATION (extp) = b->locus;
1230 DECL_CHAIN (extp) = BLOCK_VARS (block);
1231 BLOCK_VARS (block) = extp;
1233 /* If this is the file scope set DECL_CONTEXT of each decl to
1234 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1235 work. */
1236 if (scope == file_scope)
1238 DECL_CONTEXT (p) = context;
1239 if (TREE_CODE (p) == TYPE_DECL
1240 && TREE_TYPE (p) != error_mark_node)
1241 set_type_context (TREE_TYPE (p), context);
1244 /* Fall through. */
1245 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1246 already been put there by store_parm_decls. Unused-
1247 parameter warnings are handled by function.c.
1248 error_mark_node obviously does not go in BLOCK_VARS and
1249 does not get unused-variable warnings. */
1250 case PARM_DECL:
1251 case ERROR_MARK:
1252 /* It is possible for a decl not to have a name. We get
1253 here with b->id NULL in this case. */
1254 if (b->id)
1256 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1257 I_SYMBOL_BINDING (b->id) = b->shadowed;
1258 if (b->shadowed && b->shadowed->u.type)
1259 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1261 break;
1263 default:
1264 gcc_unreachable ();
1269 /* Dispose of the block that we just made inside some higher level. */
1270 if ((scope->function_body || scope == file_scope) && context)
1272 DECL_INITIAL (context) = block;
1273 BLOCK_SUPERCONTEXT (block) = context;
1275 else if (scope->outer)
1277 if (block)
1278 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1279 /* If we did not make a block for the scope just exited, any
1280 blocks made for inner scopes must be carried forward so they
1281 will later become subblocks of something else. */
1282 else if (scope->blocks)
1283 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1286 /* Pop the current scope, and free the structure for reuse. */
1287 current_scope = scope->outer;
1288 if (scope->function_body)
1289 current_function_scope = scope->outer_function;
1291 memset (scope, 0, sizeof (struct c_scope));
1292 scope->outer = scope_freelist;
1293 scope_freelist = scope;
1295 return block;
1298 void
1299 push_file_scope (void)
1301 tree decl;
1303 if (file_scope)
1304 return;
1306 push_scope ();
1307 file_scope = current_scope;
1309 start_fname_decls ();
1311 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1312 bind (DECL_NAME (decl), decl, file_scope,
1313 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1316 void
1317 pop_file_scope (void)
1319 /* In case there were missing closebraces, get us back to the global
1320 binding level. */
1321 while (current_scope != file_scope)
1322 pop_scope ();
1324 /* __FUNCTION__ is defined at file scope (""). This
1325 call may not be necessary as my tests indicate it
1326 still works without it. */
1327 finish_fname_decls ();
1329 check_inline_statics ();
1331 /* This is the point to write out a PCH if we're doing that.
1332 In that case we do not want to do anything else. */
1333 if (pch_file)
1335 c_common_write_pch ();
1336 return;
1339 /* Pop off the file scope and close this translation unit. */
1340 pop_scope ();
1341 file_scope = 0;
1343 maybe_apply_pending_pragma_weaks ();
1346 /* Adjust the bindings for the start of a statement expression. */
1348 void
1349 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1351 struct c_scope *scope;
1353 for (scope = current_scope; scope != NULL; scope = scope->outer)
1355 struct c_binding *b;
1357 if (!scope->has_label_bindings)
1358 continue;
1360 for (b = scope->bindings; b != NULL; b = b->prev)
1362 struct c_label_vars *label_vars;
1363 unsigned int ix;
1364 struct c_goto_bindings *g;
1366 if (TREE_CODE (b->decl) != LABEL_DECL)
1367 continue;
1368 label_vars = b->u.label;
1369 ++label_vars->label_bindings.stmt_exprs;
1370 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1371 ++g->goto_bindings.stmt_exprs;
1375 if (switch_bindings != NULL)
1376 ++switch_bindings->stmt_exprs;
1379 /* Adjust the bindings for the end of a statement expression. */
1381 void
1382 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1384 struct c_scope *scope;
1386 for (scope = current_scope; scope != NULL; scope = scope->outer)
1388 struct c_binding *b;
1390 if (!scope->has_label_bindings)
1391 continue;
1393 for (b = scope->bindings; b != NULL; b = b->prev)
1395 struct c_label_vars *label_vars;
1396 unsigned int ix;
1397 struct c_goto_bindings *g;
1399 if (TREE_CODE (b->decl) != LABEL_DECL)
1400 continue;
1401 label_vars = b->u.label;
1402 --label_vars->label_bindings.stmt_exprs;
1403 if (label_vars->label_bindings.stmt_exprs < 0)
1405 label_vars->label_bindings.left_stmt_expr = true;
1406 label_vars->label_bindings.stmt_exprs = 0;
1408 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1410 --g->goto_bindings.stmt_exprs;
1411 if (g->goto_bindings.stmt_exprs < 0)
1413 g->goto_bindings.left_stmt_expr = true;
1414 g->goto_bindings.stmt_exprs = 0;
1420 if (switch_bindings != NULL)
1422 --switch_bindings->stmt_exprs;
1423 gcc_assert (switch_bindings->stmt_exprs >= 0);
1427 /* Push a definition or a declaration of struct, union or enum tag "name".
1428 "type" should be the type node.
1429 We assume that the tag "name" is not already defined, and has a location
1430 of LOC.
1432 Note that the definition may really be just a forward reference.
1433 In that case, the TYPE_SIZE will be zero. */
1435 static void
1436 pushtag (location_t loc, tree name, tree type)
1438 /* Record the identifier as the type's name if it has none. */
1439 if (name && !TYPE_NAME (type))
1440 TYPE_NAME (type) = name;
1441 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1443 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1444 tagged type we just added to the current scope. This fake
1445 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1446 to output a representation of a tagged type, and it also gives
1447 us a convenient place to record the "scope start" address for the
1448 tagged type. */
1450 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1451 TYPE_DECL, NULL_TREE, type));
1453 /* An approximation for now, so we can tell this is a function-scope tag.
1454 This will be updated in pop_scope. */
1455 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1457 if (warn_cxx_compat && name != NULL_TREE)
1459 struct c_binding *b = I_SYMBOL_BINDING (name);
1461 if (b != NULL
1462 && b->decl != NULL_TREE
1463 && TREE_CODE (b->decl) == TYPE_DECL
1464 && (B_IN_CURRENT_SCOPE (b)
1465 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1466 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1467 != TYPE_MAIN_VARIANT (type)))
1469 warning_at (loc, OPT_Wc___compat,
1470 ("using %qD as both a typedef and a tag is "
1471 "invalid in C++"),
1472 b->decl);
1473 if (b->locus != UNKNOWN_LOCATION)
1474 inform (b->locus, "originally defined here");
1479 /* Subroutine of compare_decls. Allow harmless mismatches in return
1480 and argument types provided that the type modes match. This function
1481 return a unified type given a suitable match, and 0 otherwise. */
1483 static tree
1484 match_builtin_function_types (tree newtype, tree oldtype)
1486 tree newrettype, oldrettype;
1487 tree newargs, oldargs;
1488 tree trytype, tryargs;
1490 /* Accept the return type of the new declaration if same modes. */
1491 oldrettype = TREE_TYPE (oldtype);
1492 newrettype = TREE_TYPE (newtype);
1494 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1495 return 0;
1497 oldargs = TYPE_ARG_TYPES (oldtype);
1498 newargs = TYPE_ARG_TYPES (newtype);
1499 tryargs = newargs;
1501 while (oldargs || newargs)
1503 if (!oldargs
1504 || !newargs
1505 || !TREE_VALUE (oldargs)
1506 || !TREE_VALUE (newargs)
1507 || TYPE_MODE (TREE_VALUE (oldargs))
1508 != TYPE_MODE (TREE_VALUE (newargs)))
1509 return 0;
1511 oldargs = TREE_CHAIN (oldargs);
1512 newargs = TREE_CHAIN (newargs);
1515 trytype = build_function_type (newrettype, tryargs);
1516 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1519 /* Subroutine of diagnose_mismatched_decls. Check for function type
1520 mismatch involving an empty arglist vs a nonempty one and give clearer
1521 diagnostics. */
1522 static void
1523 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1524 tree newtype, tree oldtype)
1526 tree t;
1528 if (TREE_CODE (olddecl) != FUNCTION_DECL
1529 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1530 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == 0)
1531 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == 0)))
1532 return;
1534 t = TYPE_ARG_TYPES (oldtype);
1535 if (t == 0)
1536 t = TYPE_ARG_TYPES (newtype);
1537 for (; t; t = TREE_CHAIN (t))
1539 tree type = TREE_VALUE (t);
1541 if (TREE_CHAIN (t) == 0
1542 && TYPE_MAIN_VARIANT (type) != void_type_node)
1544 inform (input_location, "a parameter list with an ellipsis can%'t match "
1545 "an empty parameter name list declaration");
1546 break;
1549 if (c_type_promotes_to (type) != type)
1551 inform (input_location, "an argument type that has a default promotion can%'t match "
1552 "an empty parameter name list declaration");
1553 break;
1558 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1559 old-style function definition, NEWDECL is a prototype declaration.
1560 Diagnose inconsistencies in the argument list. Returns TRUE if
1561 the prototype is compatible, FALSE if not. */
1562 static bool
1563 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1565 tree newargs, oldargs;
1566 int i;
1568 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1570 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1571 newargs = TYPE_ARG_TYPES (newtype);
1572 i = 1;
1574 for (;;)
1576 tree oldargtype = TREE_VALUE (oldargs);
1577 tree newargtype = TREE_VALUE (newargs);
1579 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1580 return false;
1582 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1583 newargtype = TYPE_MAIN_VARIANT (newargtype);
1585 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1586 break;
1588 /* Reaching the end of just one list means the two decls don't
1589 agree on the number of arguments. */
1590 if (END_OF_ARGLIST (oldargtype))
1592 error ("prototype for %q+D declares more arguments "
1593 "than previous old-style definition", newdecl);
1594 return false;
1596 else if (END_OF_ARGLIST (newargtype))
1598 error ("prototype for %q+D declares fewer arguments "
1599 "than previous old-style definition", newdecl);
1600 return false;
1603 /* Type for passing arg must be consistent with that declared
1604 for the arg. */
1605 else if (!comptypes (oldargtype, newargtype))
1607 error ("prototype for %q+D declares argument %d"
1608 " with incompatible type",
1609 newdecl, i);
1610 return false;
1613 oldargs = TREE_CHAIN (oldargs);
1614 newargs = TREE_CHAIN (newargs);
1615 i++;
1618 /* If we get here, no errors were found, but do issue a warning
1619 for this poor-style construct. */
1620 warning (0, "prototype for %q+D follows non-prototype definition",
1621 newdecl);
1622 return true;
1623 #undef END_OF_ARGLIST
1626 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1627 first in a pair of mismatched declarations, using the diagnostic
1628 function DIAG. */
1629 static void
1630 locate_old_decl (tree decl)
1632 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1634 else if (DECL_INITIAL (decl))
1635 inform (input_location, "previous definition of %q+D was here", decl);
1636 else if (C_DECL_IMPLICIT (decl))
1637 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1638 else
1639 inform (input_location, "previous declaration of %q+D was here", decl);
1642 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1643 Returns true if the caller should proceed to merge the two, false
1644 if OLDDECL should simply be discarded. As a side effect, issues
1645 all necessary diagnostics for invalid or poor-style combinations.
1646 If it returns true, writes the types of NEWDECL and OLDDECL to
1647 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1648 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1650 static bool
1651 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1652 tree *newtypep, tree *oldtypep)
1654 tree newtype, oldtype;
1655 bool pedwarned = false;
1656 bool warned = false;
1657 bool retval = true;
1659 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1660 && DECL_EXTERNAL (DECL))
1662 /* If we have error_mark_node for either decl or type, just discard
1663 the previous decl - we're in an error cascade already. */
1664 if (olddecl == error_mark_node || newdecl == error_mark_node)
1665 return false;
1666 *oldtypep = oldtype = TREE_TYPE (olddecl);
1667 *newtypep = newtype = TREE_TYPE (newdecl);
1668 if (oldtype == error_mark_node || newtype == error_mark_node)
1669 return false;
1671 /* Two different categories of symbol altogether. This is an error
1672 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1673 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1675 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1676 && DECL_BUILT_IN (olddecl)
1677 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1679 error ("%q+D redeclared as different kind of symbol", newdecl);
1680 locate_old_decl (olddecl);
1682 else if (TREE_PUBLIC (newdecl))
1683 warning (0, "built-in function %q+D declared as non-function",
1684 newdecl);
1685 else
1686 warning (OPT_Wshadow, "declaration of %q+D shadows "
1687 "a built-in function", newdecl);
1688 return false;
1691 /* Enumerators have no linkage, so may only be declared once in a
1692 given scope. */
1693 if (TREE_CODE (olddecl) == CONST_DECL)
1695 error ("redeclaration of enumerator %q+D", newdecl);
1696 locate_old_decl (olddecl);
1697 return false;
1700 if (!comptypes (oldtype, newtype))
1702 if (TREE_CODE (olddecl) == FUNCTION_DECL
1703 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1705 /* Accept harmless mismatch in function types.
1706 This is for the ffs and fprintf builtins. */
1707 tree trytype = match_builtin_function_types (newtype, oldtype);
1709 if (trytype && comptypes (newtype, trytype))
1710 *oldtypep = oldtype = trytype;
1711 else
1713 /* If types don't match for a built-in, throw away the
1714 built-in. No point in calling locate_old_decl here, it
1715 won't print anything. */
1716 warning (0, "conflicting types for built-in function %q+D",
1717 newdecl);
1718 return false;
1721 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1722 && DECL_IS_BUILTIN (olddecl))
1724 /* A conflicting function declaration for a predeclared
1725 function that isn't actually built in. Objective C uses
1726 these. The new declaration silently overrides everything
1727 but the volatility (i.e. noreturn) indication. See also
1728 below. FIXME: Make Objective C use normal builtins. */
1729 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1730 return false;
1732 /* Permit void foo (...) to match int foo (...) if the latter is
1733 the definition and implicit int was used. See
1734 c-torture/compile/920625-2.c. */
1735 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1736 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1737 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1738 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1740 pedwarned = pedwarn (input_location, 0,
1741 "conflicting types for %q+D", newdecl);
1742 /* Make sure we keep void as the return type. */
1743 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1744 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1746 /* Permit void foo (...) to match an earlier call to foo (...) with
1747 no declared type (thus, implicitly int). */
1748 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1749 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1750 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1751 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1753 pedwarned = pedwarn (input_location, 0,
1754 "conflicting types for %q+D", newdecl);
1755 /* Make sure we keep void as the return type. */
1756 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1758 else
1760 int new_quals = TYPE_QUALS (newtype);
1761 int old_quals = TYPE_QUALS (oldtype);
1763 if (new_quals != old_quals)
1765 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1766 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1767 if (new_addr != old_addr)
1769 if (ADDR_SPACE_GENERIC_P (new_addr))
1770 error ("conflicting named address spaces (generic vs %s) "
1771 "for %q+D",
1772 c_addr_space_name (old_addr), newdecl);
1773 else if (ADDR_SPACE_GENERIC_P (old_addr))
1774 error ("conflicting named address spaces (%s vs generic) "
1775 "for %q+D",
1776 c_addr_space_name (new_addr), newdecl);
1777 else
1778 error ("conflicting named address spaces (%s vs %s) "
1779 "for %q+D",
1780 c_addr_space_name (new_addr),
1781 c_addr_space_name (old_addr),
1782 newdecl);
1785 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1786 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1787 error ("conflicting type qualifiers for %q+D", newdecl);
1789 else
1790 error ("conflicting types for %q+D", newdecl);
1791 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1792 locate_old_decl (olddecl);
1793 return false;
1797 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1798 but silently ignore the redeclaration if either is in a system
1799 header. (Conflicting redeclarations were handled above.) This
1800 is allowed for C11 if the types are the same, not just
1801 compatible. */
1802 if (TREE_CODE (newdecl) == TYPE_DECL)
1804 bool types_different = false;
1805 int comptypes_result;
1807 comptypes_result
1808 = comptypes_check_different_types (oldtype, newtype, &types_different);
1810 if (comptypes_result != 1 || types_different)
1812 error ("redefinition of typedef %q+D with different type", newdecl);
1813 locate_old_decl (olddecl);
1814 return false;
1817 if (DECL_IN_SYSTEM_HEADER (newdecl)
1818 || DECL_IN_SYSTEM_HEADER (olddecl)
1819 || TREE_NO_WARNING (newdecl)
1820 || TREE_NO_WARNING (olddecl))
1821 return true; /* Allow OLDDECL to continue in use. */
1823 if (variably_modified_type_p (newtype, NULL))
1825 error ("redefinition of typedef %q+D with variably modified type",
1826 newdecl);
1827 locate_old_decl (olddecl);
1829 else if (pedantic && !flag_isoc11)
1831 pedwarn (input_location, OPT_Wpedantic,
1832 "redefinition of typedef %q+D", newdecl);
1833 locate_old_decl (olddecl);
1836 return true;
1839 /* Function declarations can either be 'static' or 'extern' (no
1840 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1841 can never conflict with each other on account of linkage
1842 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1843 gnu89 mode permits two definitions if one is 'extern inline' and
1844 one is not. The non- extern-inline definition supersedes the
1845 extern-inline definition. */
1847 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1849 /* If you declare a built-in function name as static, or
1850 define the built-in with an old-style definition (so we
1851 can't validate the argument list) the built-in definition is
1852 overridden, but optionally warn this was a bad choice of name. */
1853 if (DECL_BUILT_IN (olddecl)
1854 && !C_DECL_DECLARED_BUILTIN (olddecl)
1855 && (!TREE_PUBLIC (newdecl)
1856 || (DECL_INITIAL (newdecl)
1857 && !prototype_p (TREE_TYPE (newdecl)))))
1859 warning (OPT_Wshadow, "declaration of %q+D shadows "
1860 "a built-in function", newdecl);
1861 /* Discard the old built-in function. */
1862 return false;
1865 if (DECL_INITIAL (newdecl))
1867 if (DECL_INITIAL (olddecl))
1869 /* If both decls are in the same TU and the new declaration
1870 isn't overriding an extern inline reject the new decl.
1871 In c99, no overriding is allowed in the same translation
1872 unit. */
1873 if ((!DECL_EXTERN_INLINE (olddecl)
1874 || DECL_EXTERN_INLINE (newdecl)
1875 || (!flag_gnu89_inline
1876 && (!DECL_DECLARED_INLINE_P (olddecl)
1877 || !lookup_attribute ("gnu_inline",
1878 DECL_ATTRIBUTES (olddecl)))
1879 && (!DECL_DECLARED_INLINE_P (newdecl)
1880 || !lookup_attribute ("gnu_inline",
1881 DECL_ATTRIBUTES (newdecl))))
1883 && same_translation_unit_p (newdecl, olddecl))
1885 error ("redefinition of %q+D", newdecl);
1886 locate_old_decl (olddecl);
1887 return false;
1891 /* If we have a prototype after an old-style function definition,
1892 the argument types must be checked specially. */
1893 else if (DECL_INITIAL (olddecl)
1894 && !prototype_p (oldtype) && prototype_p (newtype)
1895 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1896 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1898 locate_old_decl (olddecl);
1899 return false;
1901 /* A non-static declaration (even an "extern") followed by a
1902 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1903 The same is true for a static forward declaration at block
1904 scope followed by a non-static declaration/definition at file
1905 scope. Static followed by non-static at the same scope is
1906 not undefined behavior, and is the most convenient way to get
1907 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1908 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1909 we do diagnose it if -Wtraditional. */
1910 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1912 /* Two exceptions to the rule. If olddecl is an extern
1913 inline, or a predeclared function that isn't actually
1914 built in, newdecl silently overrides olddecl. The latter
1915 occur only in Objective C; see also above. (FIXME: Make
1916 Objective C use normal builtins.) */
1917 if (!DECL_IS_BUILTIN (olddecl)
1918 && !DECL_EXTERN_INLINE (olddecl))
1920 error ("static declaration of %q+D follows "
1921 "non-static declaration", newdecl);
1922 locate_old_decl (olddecl);
1924 return false;
1926 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1928 if (DECL_CONTEXT (olddecl))
1930 error ("non-static declaration of %q+D follows "
1931 "static declaration", newdecl);
1932 locate_old_decl (olddecl);
1933 return false;
1935 else if (warn_traditional)
1937 warned |= warning (OPT_Wtraditional,
1938 "non-static declaration of %q+D "
1939 "follows static declaration", newdecl);
1943 /* Make sure gnu_inline attribute is either not present, or
1944 present on all inline decls. */
1945 if (DECL_DECLARED_INLINE_P (olddecl)
1946 && DECL_DECLARED_INLINE_P (newdecl))
1948 bool newa = lookup_attribute ("gnu_inline",
1949 DECL_ATTRIBUTES (newdecl)) != NULL;
1950 bool olda = lookup_attribute ("gnu_inline",
1951 DECL_ATTRIBUTES (olddecl)) != NULL;
1952 if (newa != olda)
1954 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
1955 newa ? newdecl : olddecl);
1956 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
1957 "but not here");
1961 else if (TREE_CODE (newdecl) == VAR_DECL)
1963 /* Only variables can be thread-local, and all declarations must
1964 agree on this property. */
1965 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1967 /* Nothing to check. Since OLDDECL is marked threadprivate
1968 and NEWDECL does not have a thread-local attribute, we
1969 will merge the threadprivate attribute into NEWDECL. */
1972 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1974 if (DECL_THREAD_LOCAL_P (newdecl))
1975 error ("thread-local declaration of %q+D follows "
1976 "non-thread-local declaration", newdecl);
1977 else
1978 error ("non-thread-local declaration of %q+D follows "
1979 "thread-local declaration", newdecl);
1981 locate_old_decl (olddecl);
1982 return false;
1985 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1986 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1988 error ("redefinition of %q+D", newdecl);
1989 locate_old_decl (olddecl);
1990 return false;
1993 /* Objects declared at file scope: if the first declaration had
1994 external linkage (even if it was an external reference) the
1995 second must have external linkage as well, or the behavior is
1996 undefined. If the first declaration had internal linkage, then
1997 the second must too, or else be an external reference (in which
1998 case the composite declaration still has internal linkage).
1999 As for function declarations, we warn about the static-then-
2000 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2001 if (DECL_FILE_SCOPE_P (newdecl)
2002 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2004 if (DECL_EXTERNAL (newdecl))
2006 if (!DECL_FILE_SCOPE_P (olddecl))
2008 error ("extern declaration of %q+D follows "
2009 "declaration with no linkage", newdecl);
2010 locate_old_decl (olddecl);
2011 return false;
2013 else if (warn_traditional)
2015 warned |= warning (OPT_Wtraditional,
2016 "non-static declaration of %q+D "
2017 "follows static declaration", newdecl);
2020 else
2022 if (TREE_PUBLIC (newdecl))
2023 error ("non-static declaration of %q+D follows "
2024 "static declaration", newdecl);
2025 else
2026 error ("static declaration of %q+D follows "
2027 "non-static declaration", newdecl);
2029 locate_old_decl (olddecl);
2030 return false;
2033 /* Two objects with the same name declared at the same block
2034 scope must both be external references (6.7p3). */
2035 else if (!DECL_FILE_SCOPE_P (newdecl))
2037 if (DECL_EXTERNAL (newdecl))
2039 /* Extern with initializer at block scope, which will
2040 already have received an error. */
2042 else if (DECL_EXTERNAL (olddecl))
2044 error ("declaration of %q+D with no linkage follows "
2045 "extern declaration", newdecl);
2046 locate_old_decl (olddecl);
2048 else
2050 error ("redeclaration of %q+D with no linkage", newdecl);
2051 locate_old_decl (olddecl);
2054 return false;
2057 /* C++ does not permit a decl to appear multiple times at file
2058 scope. */
2059 if (warn_cxx_compat
2060 && DECL_FILE_SCOPE_P (newdecl)
2061 && !DECL_EXTERNAL (newdecl)
2062 && !DECL_EXTERNAL (olddecl))
2063 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2064 OPT_Wc___compat,
2065 ("duplicate declaration of %qD is "
2066 "invalid in C++"),
2067 newdecl);
2070 /* warnings */
2071 /* All decls must agree on a visibility. */
2072 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2073 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2074 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2076 warned |= warning (0, "redeclaration of %q+D with different visibility "
2077 "(old visibility preserved)", newdecl);
2080 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2082 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2083 if (DECL_DECLARED_INLINE_P (newdecl)
2084 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2086 warned |= warning (OPT_Wattributes,
2087 "inline declaration of %qD follows "
2088 "declaration with attribute noinline", newdecl);
2090 else if (DECL_DECLARED_INLINE_P (olddecl)
2091 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2093 warned |= warning (OPT_Wattributes,
2094 "declaration of %q+D with attribute "
2095 "noinline follows inline declaration ", newdecl);
2098 else /* PARM_DECL, VAR_DECL */
2100 /* Redeclaration of a parameter is a constraint violation (this is
2101 not explicitly stated, but follows from C99 6.7p3 [no more than
2102 one declaration of the same identifier with no linkage in the
2103 same scope, except type tags] and 6.2.2p6 [parameters have no
2104 linkage]). We must check for a forward parameter declaration,
2105 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2106 an extension, the mandatory diagnostic for which is handled by
2107 mark_forward_parm_decls. */
2109 if (TREE_CODE (newdecl) == PARM_DECL
2110 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2112 error ("redefinition of parameter %q+D", newdecl);
2113 locate_old_decl (olddecl);
2114 return false;
2118 /* Optional warning for completely redundant decls. */
2119 if (!warned && !pedwarned
2120 && warn_redundant_decls
2121 /* Don't warn about a function declaration followed by a
2122 definition. */
2123 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2124 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2125 /* Don't warn about redundant redeclarations of builtins. */
2126 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2127 && !DECL_BUILT_IN (newdecl)
2128 && DECL_BUILT_IN (olddecl)
2129 && !C_DECL_DECLARED_BUILTIN (olddecl))
2130 /* Don't warn about an extern followed by a definition. */
2131 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2132 /* Don't warn about forward parameter decls. */
2133 && !(TREE_CODE (newdecl) == PARM_DECL
2134 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2135 /* Don't warn about a variable definition following a declaration. */
2136 && !(TREE_CODE (newdecl) == VAR_DECL
2137 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2139 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2140 newdecl);
2143 /* Report location of previous decl/defn. */
2144 if (warned || pedwarned)
2145 locate_old_decl (olddecl);
2147 #undef DECL_EXTERN_INLINE
2149 return retval;
2152 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2153 consistent with OLDDECL, but carries new information. Merge the
2154 new information into OLDDECL. This function issues no
2155 diagnostics. */
2157 static void
2158 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2160 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2161 && DECL_INITIAL (newdecl) != 0);
2162 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2163 && prototype_p (TREE_TYPE (newdecl)));
2164 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2165 && prototype_p (TREE_TYPE (olddecl)));
2167 /* For real parm decl following a forward decl, rechain the old decl
2168 in its new location and clear TREE_ASM_WRITTEN (it's not a
2169 forward decl anymore). */
2170 if (TREE_CODE (newdecl) == PARM_DECL
2171 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2173 struct c_binding *b, **here;
2175 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2176 if ((*here)->decl == olddecl)
2177 goto found;
2178 gcc_unreachable ();
2180 found:
2181 b = *here;
2182 *here = b->prev;
2183 b->prev = current_scope->bindings;
2184 current_scope->bindings = b;
2186 TREE_ASM_WRITTEN (olddecl) = 0;
2189 DECL_ATTRIBUTES (newdecl)
2190 = targetm.merge_decl_attributes (olddecl, newdecl);
2192 /* Merge the data types specified in the two decls. */
2193 TREE_TYPE (newdecl)
2194 = TREE_TYPE (olddecl)
2195 = composite_type (newtype, oldtype);
2197 /* Lay the type out, unless already done. */
2198 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2200 if (TREE_TYPE (newdecl) != error_mark_node)
2201 layout_type (TREE_TYPE (newdecl));
2202 if (TREE_CODE (newdecl) != FUNCTION_DECL
2203 && TREE_CODE (newdecl) != TYPE_DECL
2204 && TREE_CODE (newdecl) != CONST_DECL)
2205 layout_decl (newdecl, 0);
2207 else
2209 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2210 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2211 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2212 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2213 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2215 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2216 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2220 /* Keep the old rtl since we can safely use it. */
2221 if (HAS_RTL_P (olddecl))
2222 COPY_DECL_RTL (olddecl, newdecl);
2224 /* Merge the type qualifiers. */
2225 if (TREE_READONLY (newdecl))
2226 TREE_READONLY (olddecl) = 1;
2228 if (TREE_THIS_VOLATILE (newdecl))
2229 TREE_THIS_VOLATILE (olddecl) = 1;
2231 /* Merge deprecatedness. */
2232 if (TREE_DEPRECATED (newdecl))
2233 TREE_DEPRECATED (olddecl) = 1;
2235 /* If a decl is in a system header and the other isn't, keep the one on the
2236 system header. Otherwise, keep source location of definition rather than
2237 declaration and of prototype rather than non-prototype unless that
2238 prototype is built-in. */
2239 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2240 && DECL_IN_SYSTEM_HEADER (olddecl)
2241 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2242 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2243 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2244 && DECL_IN_SYSTEM_HEADER (newdecl)
2245 && !DECL_IN_SYSTEM_HEADER (olddecl))
2246 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2247 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2248 || (old_is_prototype && !new_is_prototype
2249 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2250 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2252 /* Merge the initialization information. */
2253 if (DECL_INITIAL (newdecl) == 0)
2254 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2256 /* Merge the threadprivate attribute. */
2257 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
2259 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
2260 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2263 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2265 /* Merge the section attribute.
2266 We want to issue an error if the sections conflict but that
2267 must be done later in decl_attributes since we are called
2268 before attributes are assigned. */
2269 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
2270 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
2272 /* Copy the assembler name.
2273 Currently, it can only be defined in the prototype. */
2274 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2276 /* Use visibility of whichever declaration had it specified */
2277 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2279 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2280 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2283 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2285 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2286 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2287 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2288 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2289 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2290 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2291 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2292 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2293 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2294 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2295 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2298 /* Merge the storage class information. */
2299 merge_weak (newdecl, olddecl);
2301 /* For functions, static overrides non-static. */
2302 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2304 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2305 /* This is since we don't automatically
2306 copy the attributes of NEWDECL into OLDDECL. */
2307 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2308 /* If this clears `static', clear it in the identifier too. */
2309 if (!TREE_PUBLIC (olddecl))
2310 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2314 /* In c99, 'extern' declaration before (or after) 'inline' means this
2315 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2316 is present. */
2317 if (TREE_CODE (newdecl) == FUNCTION_DECL
2318 && !flag_gnu89_inline
2319 && (DECL_DECLARED_INLINE_P (newdecl)
2320 || DECL_DECLARED_INLINE_P (olddecl))
2321 && (!DECL_DECLARED_INLINE_P (newdecl)
2322 || !DECL_DECLARED_INLINE_P (olddecl)
2323 || !DECL_EXTERNAL (olddecl))
2324 && DECL_EXTERNAL (newdecl)
2325 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2326 && !current_function_decl)
2327 DECL_EXTERNAL (newdecl) = 0;
2329 if (DECL_EXTERNAL (newdecl))
2331 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2332 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2334 /* An extern decl does not override previous storage class. */
2335 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2336 if (!DECL_EXTERNAL (newdecl))
2338 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2339 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2342 else
2344 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2345 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2348 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2350 /* If we're redefining a function previously defined as extern
2351 inline, make sure we emit debug info for the inline before we
2352 throw it away, in case it was inlined into a function that
2353 hasn't been written out yet. */
2354 if (new_is_definition && DECL_INITIAL (olddecl))
2355 /* The new defn must not be inline. */
2356 DECL_UNINLINABLE (newdecl) = 1;
2357 else
2359 /* If either decl says `inline', this fn is inline, unless
2360 its definition was passed already. */
2361 if (DECL_DECLARED_INLINE_P (newdecl)
2362 || DECL_DECLARED_INLINE_P (olddecl))
2363 DECL_DECLARED_INLINE_P (newdecl) = 1;
2365 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2366 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2368 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2369 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2370 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2371 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2374 if (DECL_BUILT_IN (olddecl))
2376 /* If redeclaring a builtin function, it stays built in.
2377 But it gets tagged as having been declared. */
2378 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2379 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2380 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2381 if (new_is_prototype)
2383 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2384 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2386 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2387 switch (fncode)
2389 /* If a compatible prototype of these builtin functions
2390 is seen, assume the runtime implements it with the
2391 expected semantics. */
2392 case BUILT_IN_STPCPY:
2393 if (builtin_decl_explicit_p (fncode))
2394 set_builtin_decl_implicit_p (fncode, true);
2395 break;
2396 default:
2397 break;
2401 else
2402 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2403 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2406 /* Preserve function specific target and optimization options */
2407 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2408 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2409 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2410 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2412 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2413 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2414 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2415 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2417 /* Also preserve various other info from the definition. */
2418 if (!new_is_definition)
2420 tree t;
2421 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2422 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2423 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2424 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2425 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2426 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2427 DECL_CONTEXT (t) = newdecl;
2429 /* See if we've got a function to instantiate from. */
2430 if (DECL_SAVED_TREE (olddecl))
2431 DECL_ABSTRACT_ORIGIN (newdecl)
2432 = DECL_ABSTRACT_ORIGIN (olddecl);
2436 /* Merge the USED information. */
2437 if (TREE_USED (olddecl))
2438 TREE_USED (newdecl) = 1;
2439 else if (TREE_USED (newdecl))
2440 TREE_USED (olddecl) = 1;
2441 if (TREE_CODE (olddecl) == VAR_DECL || TREE_CODE (olddecl) == PARM_DECL)
2442 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2443 if (DECL_PRESERVE_P (olddecl))
2444 DECL_PRESERVE_P (newdecl) = 1;
2445 else if (DECL_PRESERVE_P (newdecl))
2446 DECL_PRESERVE_P (olddecl) = 1;
2448 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2449 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2450 DECL_ARGUMENTS (if appropriate). */
2452 unsigned olddecl_uid = DECL_UID (olddecl);
2453 tree olddecl_context = DECL_CONTEXT (olddecl);
2454 tree olddecl_arguments = NULL;
2455 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2456 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2458 memcpy ((char *) olddecl + sizeof (struct tree_common),
2459 (char *) newdecl + sizeof (struct tree_common),
2460 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2461 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2462 switch (TREE_CODE (olddecl))
2464 case FUNCTION_DECL:
2465 case FIELD_DECL:
2466 case VAR_DECL:
2467 case PARM_DECL:
2468 case LABEL_DECL:
2469 case RESULT_DECL:
2470 case CONST_DECL:
2471 case TYPE_DECL:
2472 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2473 (char *) newdecl + sizeof (struct tree_decl_common),
2474 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2475 break;
2477 default:
2479 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2480 (char *) newdecl + sizeof (struct tree_decl_common),
2481 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2483 DECL_UID (olddecl) = olddecl_uid;
2484 DECL_CONTEXT (olddecl) = olddecl_context;
2485 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2486 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2489 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2490 so that encode_section_info has a chance to look at the new decl
2491 flags and attributes. */
2492 if (DECL_RTL_SET_P (olddecl)
2493 && (TREE_CODE (olddecl) == FUNCTION_DECL
2494 || (TREE_CODE (olddecl) == VAR_DECL
2495 && TREE_STATIC (olddecl))))
2496 make_decl_rtl (olddecl);
2499 /* Handle when a new declaration NEWDECL has the same name as an old
2500 one OLDDECL in the same binding contour. Prints an error message
2501 if appropriate.
2503 If safely possible, alter OLDDECL to look like NEWDECL, and return
2504 true. Otherwise, return false. */
2506 static bool
2507 duplicate_decls (tree newdecl, tree olddecl)
2509 tree newtype = NULL, oldtype = NULL;
2511 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2513 /* Avoid `unused variable' and other warnings for OLDDECL. */
2514 TREE_NO_WARNING (olddecl) = 1;
2515 return false;
2518 merge_decls (newdecl, olddecl, newtype, oldtype);
2519 return true;
2523 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2524 static void
2525 warn_if_shadowing (tree new_decl)
2527 struct c_binding *b;
2529 /* Shadow warnings wanted? */
2530 if (!warn_shadow
2531 /* No shadow warnings for internally generated vars. */
2532 || DECL_IS_BUILTIN (new_decl)
2533 /* No shadow warnings for vars made for inlining. */
2534 || DECL_FROM_INLINE (new_decl))
2535 return;
2537 /* Is anything being shadowed? Invisible decls do not count. */
2538 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2539 if (b->decl && b->decl != new_decl && !b->invisible
2540 && (b->decl == error_mark_node
2541 || diagnostic_report_warnings_p (global_dc,
2542 DECL_SOURCE_LOCATION (b->decl))))
2544 tree old_decl = b->decl;
2546 if (old_decl == error_mark_node)
2548 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2549 "non-variable", new_decl);
2550 break;
2552 else if (TREE_CODE (old_decl) == PARM_DECL)
2553 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2554 new_decl);
2555 else if (DECL_FILE_SCOPE_P (old_decl))
2557 /* Do not warn if a variable shadows a function, unless
2558 the variable is a function or a pointer-to-function. */
2559 if (TREE_CODE (old_decl) == FUNCTION_DECL
2560 && TREE_CODE (new_decl) != FUNCTION_DECL
2561 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2562 continue;
2564 warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2565 "declaration of %qD shadows a global declaration",
2566 new_decl);
2568 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2569 && DECL_BUILT_IN (old_decl))
2571 warning (OPT_Wshadow, "declaration of %q+D shadows "
2572 "a built-in function", new_decl);
2573 break;
2575 else
2576 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2577 new_decl);
2579 warning_at (DECL_SOURCE_LOCATION (old_decl), OPT_Wshadow,
2580 "shadowed declaration is here");
2582 break;
2586 /* Record a decl-node X as belonging to the current lexical scope.
2587 Check for errors (such as an incompatible declaration for the same
2588 name already seen in the same scope).
2590 Returns either X or an old decl for the same name.
2591 If an old decl is returned, it may have been smashed
2592 to agree with what X says. */
2594 tree
2595 pushdecl (tree x)
2597 tree name = DECL_NAME (x);
2598 struct c_scope *scope = current_scope;
2599 struct c_binding *b;
2600 bool nested = false;
2601 location_t locus = DECL_SOURCE_LOCATION (x);
2603 /* Must set DECL_CONTEXT for everything not at file scope or
2604 DECL_FILE_SCOPE_P won't work. Local externs don't count
2605 unless they have initializers (which generate code). */
2606 if (current_function_decl
2607 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2608 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2609 DECL_CONTEXT (x) = current_function_decl;
2611 /* Anonymous decls are just inserted in the scope. */
2612 if (!name)
2614 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2615 locus);
2616 return x;
2619 /* First, see if there is another declaration with the same name in
2620 the current scope. If there is, duplicate_decls may do all the
2621 work for us. If duplicate_decls returns false, that indicates
2622 two incompatible decls in the same scope; we are to silently
2623 replace the old one (duplicate_decls has issued all appropriate
2624 diagnostics). In particular, we should not consider possible
2625 duplicates in the external scope, or shadowing. */
2626 b = I_SYMBOL_BINDING (name);
2627 if (b && B_IN_SCOPE (b, scope))
2629 struct c_binding *b_ext, *b_use;
2630 tree type = TREE_TYPE (x);
2631 tree visdecl = b->decl;
2632 tree vistype = TREE_TYPE (visdecl);
2633 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2634 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2635 b->inner_comp = false;
2636 b_use = b;
2637 b_ext = b;
2638 /* If this is an external linkage declaration, we should check
2639 for compatibility with the type in the external scope before
2640 setting the type at this scope based on the visible
2641 information only. */
2642 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2644 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2645 b_ext = b_ext->shadowed;
2646 if (b_ext)
2648 b_use = b_ext;
2649 if (b_use->u.type)
2650 TREE_TYPE (b_use->decl) = b_use->u.type;
2653 if (duplicate_decls (x, b_use->decl))
2655 if (b_use != b)
2657 /* Save the updated type in the external scope and
2658 restore the proper type for this scope. */
2659 tree thistype;
2660 if (comptypes (vistype, type))
2661 thistype = composite_type (vistype, type);
2662 else
2663 thistype = TREE_TYPE (b_use->decl);
2664 b_use->u.type = TREE_TYPE (b_use->decl);
2665 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2666 && DECL_BUILT_IN (b_use->decl))
2667 thistype
2668 = build_type_attribute_variant (thistype,
2669 TYPE_ATTRIBUTES
2670 (b_use->u.type));
2671 TREE_TYPE (b_use->decl) = thistype;
2673 return b_use->decl;
2675 else
2676 goto skip_external_and_shadow_checks;
2679 /* All declarations with external linkage, and all external
2680 references, go in the external scope, no matter what scope is
2681 current. However, the binding in that scope is ignored for
2682 purposes of normal name lookup. A separate binding structure is
2683 created in the requested scope; this governs the normal
2684 visibility of the symbol.
2686 The binding in the externals scope is used exclusively for
2687 detecting duplicate declarations of the same object, no matter
2688 what scope they are in; this is what we do here. (C99 6.2.7p2:
2689 All declarations that refer to the same object or function shall
2690 have compatible type; otherwise, the behavior is undefined.) */
2691 if (DECL_EXTERNAL (x) || scope == file_scope)
2693 tree type = TREE_TYPE (x);
2694 tree vistype = 0;
2695 tree visdecl = 0;
2696 bool type_saved = false;
2697 if (b && !B_IN_EXTERNAL_SCOPE (b)
2698 && (TREE_CODE (b->decl) == FUNCTION_DECL
2699 || TREE_CODE (b->decl) == VAR_DECL)
2700 && DECL_FILE_SCOPE_P (b->decl))
2702 visdecl = b->decl;
2703 vistype = TREE_TYPE (visdecl);
2705 if (scope != file_scope
2706 && !DECL_IN_SYSTEM_HEADER (x))
2707 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2709 while (b && !B_IN_EXTERNAL_SCOPE (b))
2711 /* If this decl might be modified, save its type. This is
2712 done here rather than when the decl is first bound
2713 because the type may change after first binding, through
2714 being completed or through attributes being added. If we
2715 encounter multiple such decls, only the first should have
2716 its type saved; the others will already have had their
2717 proper types saved and the types will not have changed as
2718 their scopes will not have been re-entered. */
2719 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2721 b->u.type = TREE_TYPE (b->decl);
2722 type_saved = true;
2724 if (B_IN_FILE_SCOPE (b)
2725 && TREE_CODE (b->decl) == VAR_DECL
2726 && TREE_STATIC (b->decl)
2727 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2728 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2729 && TREE_CODE (type) == ARRAY_TYPE
2730 && TYPE_DOMAIN (type)
2731 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2732 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2734 /* Array type completed in inner scope, which should be
2735 diagnosed if the completion does not have size 1 and
2736 it does not get completed in the file scope. */
2737 b->inner_comp = true;
2739 b = b->shadowed;
2742 /* If a matching external declaration has been found, set its
2743 type to the composite of all the types of that declaration.
2744 After the consistency checks, it will be reset to the
2745 composite of the visible types only. */
2746 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2747 && b->u.type)
2748 TREE_TYPE (b->decl) = b->u.type;
2750 /* The point of the same_translation_unit_p check here is,
2751 we want to detect a duplicate decl for a construct like
2752 foo() { extern bar(); } ... static bar(); but not if
2753 they are in different translation units. In any case,
2754 the static does not go in the externals scope. */
2755 if (b
2756 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2757 && duplicate_decls (x, b->decl))
2759 tree thistype;
2760 if (vistype)
2762 if (comptypes (vistype, type))
2763 thistype = composite_type (vistype, type);
2764 else
2765 thistype = TREE_TYPE (b->decl);
2767 else
2768 thistype = type;
2769 b->u.type = TREE_TYPE (b->decl);
2770 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2771 thistype
2772 = build_type_attribute_variant (thistype,
2773 TYPE_ATTRIBUTES (b->u.type));
2774 TREE_TYPE (b->decl) = thistype;
2775 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2776 locus);
2777 return b->decl;
2779 else if (TREE_PUBLIC (x))
2781 if (visdecl && !b && duplicate_decls (x, visdecl))
2783 /* An external declaration at block scope referring to a
2784 visible entity with internal linkage. The composite
2785 type will already be correct for this scope, so we
2786 just need to fall through to make the declaration in
2787 this scope. */
2788 nested = true;
2789 x = visdecl;
2791 else
2793 bind (name, x, external_scope, /*invisible=*/true,
2794 /*nested=*/false, locus);
2795 nested = true;
2800 if (TREE_CODE (x) != PARM_DECL)
2801 warn_if_shadowing (x);
2803 skip_external_and_shadow_checks:
2804 if (TREE_CODE (x) == TYPE_DECL)
2806 /* So this is a typedef, set its underlying type. */
2807 set_underlying_type (x);
2809 /* If X is a typedef defined in the current function, record it
2810 for the purpose of implementing the -Wunused-local-typedefs
2811 warning. */
2812 record_locally_defined_typedef (x);
2815 bind (name, x, scope, /*invisible=*/false, nested, locus);
2817 /* If x's type is incomplete because it's based on a
2818 structure or union which has not yet been fully declared,
2819 attach it to that structure or union type, so we can go
2820 back and complete the variable declaration later, if the
2821 structure or union gets fully declared.
2823 If the input is erroneous, we can have error_mark in the type
2824 slot (e.g. "f(void a, ...)") - that doesn't count as an
2825 incomplete type. */
2826 if (TREE_TYPE (x) != error_mark_node
2827 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2829 tree element = TREE_TYPE (x);
2831 while (TREE_CODE (element) == ARRAY_TYPE)
2832 element = TREE_TYPE (element);
2833 element = TYPE_MAIN_VARIANT (element);
2835 if ((TREE_CODE (element) == RECORD_TYPE
2836 || TREE_CODE (element) == UNION_TYPE)
2837 && (TREE_CODE (x) != TYPE_DECL
2838 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2839 && !COMPLETE_TYPE_P (element))
2840 C_TYPE_INCOMPLETE_VARS (element)
2841 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2843 return x;
2846 /* Record X as belonging to file scope.
2847 This is used only internally by the Objective-C front end,
2848 and is limited to its needs. duplicate_decls is not called;
2849 if there is any preexisting decl for this identifier, it is an ICE. */
2851 tree
2852 pushdecl_top_level (tree x)
2854 tree name;
2855 bool nested = false;
2856 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2858 name = DECL_NAME (x);
2860 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2862 if (TREE_PUBLIC (x))
2864 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2865 UNKNOWN_LOCATION);
2866 nested = true;
2868 if (file_scope)
2869 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2871 return x;
2874 static void
2875 implicit_decl_warning (tree id, tree olddecl)
2877 if (warn_implicit_function_declaration)
2879 bool warned;
2881 if (flag_isoc99)
2882 warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2883 "implicit declaration of function %qE", id);
2884 else
2885 warned = warning (OPT_Wimplicit_function_declaration,
2886 G_("implicit declaration of function %qE"), id);
2887 if (olddecl && warned)
2888 locate_old_decl (olddecl);
2892 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
2893 function of type int (). */
2895 tree
2896 implicitly_declare (location_t loc, tree functionid)
2898 struct c_binding *b;
2899 tree decl = 0;
2900 tree asmspec_tree;
2902 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2904 if (B_IN_SCOPE (b, external_scope))
2906 decl = b->decl;
2907 break;
2911 if (decl)
2913 if (decl == error_mark_node)
2914 return decl;
2916 /* FIXME: Objective-C has weird not-really-builtin functions
2917 which are supposed to be visible automatically. They wind up
2918 in the external scope because they're pushed before the file
2919 scope gets created. Catch this here and rebind them into the
2920 file scope. */
2921 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2923 bind (functionid, decl, file_scope,
2924 /*invisible=*/false, /*nested=*/true,
2925 DECL_SOURCE_LOCATION (decl));
2926 return decl;
2928 else
2930 tree newtype = default_function_type;
2931 if (b->u.type)
2932 TREE_TYPE (decl) = b->u.type;
2933 /* Implicit declaration of a function already declared
2934 (somehow) in a different scope, or as a built-in.
2935 If this is the first time this has happened, warn;
2936 then recycle the old declaration but with the new type. */
2937 if (!C_DECL_IMPLICIT (decl))
2939 implicit_decl_warning (functionid, decl);
2940 C_DECL_IMPLICIT (decl) = 1;
2942 if (DECL_BUILT_IN (decl))
2944 newtype = build_type_attribute_variant (newtype,
2945 TYPE_ATTRIBUTES
2946 (TREE_TYPE (decl)));
2947 if (!comptypes (newtype, TREE_TYPE (decl)))
2949 warning_at (loc, 0, "incompatible implicit declaration of "
2950 "built-in function %qD", decl);
2951 newtype = TREE_TYPE (decl);
2954 else
2956 if (!comptypes (newtype, TREE_TYPE (decl)))
2958 error_at (loc, "incompatible implicit declaration of function %qD", decl);
2959 locate_old_decl (decl);
2962 b->u.type = TREE_TYPE (decl);
2963 TREE_TYPE (decl) = newtype;
2964 bind (functionid, decl, current_scope,
2965 /*invisible=*/false, /*nested=*/true,
2966 DECL_SOURCE_LOCATION (decl));
2967 return decl;
2971 /* Not seen before. */
2972 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
2973 DECL_EXTERNAL (decl) = 1;
2974 TREE_PUBLIC (decl) = 1;
2975 C_DECL_IMPLICIT (decl) = 1;
2976 implicit_decl_warning (functionid, 0);
2977 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2978 if (asmspec_tree)
2979 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2981 /* C89 says implicit declarations are in the innermost block.
2982 So we record the decl in the standard fashion. */
2983 decl = pushdecl (decl);
2985 /* No need to call objc_check_decl here - it's a function type. */
2986 rest_of_decl_compilation (decl, 0, 0);
2988 /* Write a record describing this implicit function declaration
2989 to the prototypes file (if requested). */
2990 gen_aux_info_record (decl, 0, 1, 0);
2992 /* Possibly apply some default attributes to this implicit declaration. */
2993 decl_attributes (&decl, NULL_TREE, 0);
2995 return decl;
2998 /* Issue an error message for a reference to an undeclared variable
2999 ID, including a reference to a builtin outside of function-call
3000 context. Establish a binding of the identifier to error_mark_node
3001 in an appropriate scope, which will suppress further errors for the
3002 same identifier. The error message should be given location LOC. */
3003 void
3004 undeclared_variable (location_t loc, tree id)
3006 static bool already = false;
3007 struct c_scope *scope;
3009 if (current_function_decl == 0)
3011 error_at (loc, "%qE undeclared here (not in a function)", id);
3012 scope = current_scope;
3014 else
3016 if (!objc_diagnose_private_ivar (id))
3017 error_at (loc, "%qE undeclared (first use in this function)", id);
3018 if (!already)
3020 inform (loc, "each undeclared identifier is reported only"
3021 " once for each function it appears in");
3022 already = true;
3025 /* If we are parsing old-style parameter decls, current_function_decl
3026 will be nonnull but current_function_scope will be null. */
3027 scope = current_function_scope ? current_function_scope : current_scope;
3029 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3030 UNKNOWN_LOCATION);
3033 /* Subroutine of lookup_label, declare_label, define_label: construct a
3034 LABEL_DECL with all the proper frills. Also create a struct
3035 c_label_vars initialized for the current scope. */
3037 static tree
3038 make_label (location_t location, tree name, bool defining,
3039 struct c_label_vars **p_label_vars)
3041 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3042 struct c_label_vars *label_vars;
3044 DECL_CONTEXT (label) = current_function_decl;
3045 DECL_MODE (label) = VOIDmode;
3047 label_vars = ggc_alloc_c_label_vars ();
3048 label_vars->shadowed = NULL;
3049 set_spot_bindings (&label_vars->label_bindings, defining);
3050 label_vars->decls_in_scope = make_tree_vector ();
3051 label_vars->gotos = NULL;
3052 *p_label_vars = label_vars;
3054 return label;
3057 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3058 Create one if none exists so far for the current function.
3059 This is called when a label is used in a goto expression or
3060 has its address taken. */
3062 tree
3063 lookup_label (tree name)
3065 tree label;
3066 struct c_label_vars *label_vars;
3068 if (current_function_scope == 0)
3070 error ("label %qE referenced outside of any function", name);
3071 return 0;
3074 /* Use a label already defined or ref'd with this name, but not if
3075 it is inherited from a containing function and wasn't declared
3076 using __label__. */
3077 label = I_LABEL_DECL (name);
3078 if (label && (DECL_CONTEXT (label) == current_function_decl
3079 || C_DECLARED_LABEL_FLAG (label)))
3081 /* If the label has only been declared, update its apparent
3082 location to point here, for better diagnostics if it
3083 turns out not to have been defined. */
3084 if (DECL_INITIAL (label) == NULL_TREE)
3085 DECL_SOURCE_LOCATION (label) = input_location;
3086 return label;
3089 /* No label binding for that identifier; make one. */
3090 label = make_label (input_location, name, false, &label_vars);
3092 /* Ordinary labels go in the current function scope. */
3093 bind_label (name, label, current_function_scope, label_vars);
3095 return label;
3098 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3099 to LABEL. */
3101 static void
3102 warn_about_goto (location_t goto_loc, tree label, tree decl)
3104 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3105 error_at (goto_loc,
3106 "jump into scope of identifier with variably modified type");
3107 else
3108 warning_at (goto_loc, OPT_Wjump_misses_init,
3109 "jump skips variable initialization");
3110 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3111 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3114 /* Look up a label because of a goto statement. This is like
3115 lookup_label, but also issues any appropriate warnings. */
3117 tree
3118 lookup_label_for_goto (location_t loc, tree name)
3120 tree label;
3121 struct c_label_vars *label_vars;
3122 unsigned int ix;
3123 tree decl;
3125 label = lookup_label (name);
3126 if (label == NULL_TREE)
3127 return NULL_TREE;
3129 /* If we are jumping to a different function, we can't issue any
3130 useful warnings. */
3131 if (DECL_CONTEXT (label) != current_function_decl)
3133 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3134 return label;
3137 label_vars = I_LABEL_BINDING (name)->u.label;
3139 /* If the label has not yet been defined, then push this goto on a
3140 list for possible later warnings. */
3141 if (label_vars->label_bindings.scope == NULL)
3143 struct c_goto_bindings *g;
3145 g = ggc_alloc_c_goto_bindings ();
3146 g->loc = loc;
3147 set_spot_bindings (&g->goto_bindings, true);
3148 vec_safe_push (label_vars->gotos, g);
3149 return label;
3152 /* If there are any decls in label_vars->decls_in_scope, then this
3153 goto has missed the declaration of the decl. This happens for a
3154 case like
3155 int i = 1;
3156 lab:
3158 goto lab;
3159 Issue a warning or error. */
3160 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3161 warn_about_goto (loc, label, decl);
3163 if (label_vars->label_bindings.left_stmt_expr)
3165 error_at (loc, "jump into statement expression");
3166 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3169 return label;
3172 /* Make a label named NAME in the current function, shadowing silently
3173 any that may be inherited from containing functions or containing
3174 scopes. This is called for __label__ declarations. */
3176 tree
3177 declare_label (tree name)
3179 struct c_binding *b = I_LABEL_BINDING (name);
3180 tree label;
3181 struct c_label_vars *label_vars;
3183 /* Check to make sure that the label hasn't already been declared
3184 at this scope */
3185 if (b && B_IN_CURRENT_SCOPE (b))
3187 error ("duplicate label declaration %qE", name);
3188 locate_old_decl (b->decl);
3190 /* Just use the previous declaration. */
3191 return b->decl;
3194 label = make_label (input_location, name, false, &label_vars);
3195 C_DECLARED_LABEL_FLAG (label) = 1;
3197 /* Declared labels go in the current scope. */
3198 bind_label (name, label, current_scope, label_vars);
3200 return label;
3203 /* When we define a label, issue any appropriate warnings if there are
3204 any gotos earlier in the function which jump to this label. */
3206 static void
3207 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3209 unsigned int ix;
3210 struct c_goto_bindings *g;
3212 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3214 struct c_binding *b;
3215 struct c_scope *scope;
3217 /* We have a goto to this label. The goto is going forward. In
3218 g->scope, the goto is going to skip any binding which was
3219 defined after g->bindings_in_scope. */
3220 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3222 for (b = g->goto_bindings.scope->bindings;
3223 b != g->goto_bindings.bindings_in_scope;
3224 b = b->prev)
3226 if (decl_jump_unsafe (b->decl))
3227 warn_about_goto (g->loc, label, b->decl);
3231 /* We also need to warn about decls defined in any scopes
3232 between the scope of the label and the scope of the goto. */
3233 for (scope = label_vars->label_bindings.scope;
3234 scope != g->goto_bindings.scope;
3235 scope = scope->outer)
3237 gcc_assert (scope != NULL);
3238 if (scope->has_jump_unsafe_decl)
3240 if (scope == label_vars->label_bindings.scope)
3241 b = label_vars->label_bindings.bindings_in_scope;
3242 else
3243 b = scope->bindings;
3244 for (; b != NULL; b = b->prev)
3246 if (decl_jump_unsafe (b->decl))
3247 warn_about_goto (g->loc, label, b->decl);
3252 if (g->goto_bindings.stmt_exprs > 0)
3254 error_at (g->loc, "jump into statement expression");
3255 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3256 label);
3260 /* Now that the label is defined, we will issue warnings about
3261 subsequent gotos to this label when we see them. */
3262 vec_safe_truncate (label_vars->gotos, 0);
3263 label_vars->gotos = NULL;
3266 /* Define a label, specifying the location in the source file.
3267 Return the LABEL_DECL node for the label, if the definition is valid.
3268 Otherwise return 0. */
3270 tree
3271 define_label (location_t location, tree name)
3273 /* Find any preexisting label with this name. It is an error
3274 if that label has already been defined in this function, or
3275 if there is a containing function with a declared label with
3276 the same name. */
3277 tree label = I_LABEL_DECL (name);
3279 if (label
3280 && ((DECL_CONTEXT (label) == current_function_decl
3281 && DECL_INITIAL (label) != 0)
3282 || (DECL_CONTEXT (label) != current_function_decl
3283 && C_DECLARED_LABEL_FLAG (label))))
3285 error_at (location, "duplicate label %qD", label);
3286 locate_old_decl (label);
3287 return 0;
3289 else if (label && DECL_CONTEXT (label) == current_function_decl)
3291 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3293 /* The label has been used or declared already in this function,
3294 but not defined. Update its location to point to this
3295 definition. */
3296 DECL_SOURCE_LOCATION (label) = location;
3297 set_spot_bindings (&label_vars->label_bindings, true);
3299 /* Issue warnings as required about any goto statements from
3300 earlier in the function. */
3301 check_earlier_gotos (label, label_vars);
3303 else
3305 struct c_label_vars *label_vars;
3307 /* No label binding for that identifier; make one. */
3308 label = make_label (location, name, true, &label_vars);
3310 /* Ordinary labels go in the current function scope. */
3311 bind_label (name, label, current_function_scope, label_vars);
3314 if (!in_system_header && lookup_name (name))
3315 warning_at (location, OPT_Wtraditional,
3316 "traditional C lacks a separate namespace "
3317 "for labels, identifier %qE conflicts", name);
3319 /* Mark label as having been defined. */
3320 DECL_INITIAL (label) = error_mark_node;
3321 return label;
3324 /* Get the bindings for a new switch statement. This is used to issue
3325 warnings as appropriate for jumps from the switch to case or
3326 default labels. */
3328 struct c_spot_bindings *
3329 c_get_switch_bindings (void)
3331 struct c_spot_bindings *switch_bindings;
3333 switch_bindings = XNEW (struct c_spot_bindings);
3334 set_spot_bindings (switch_bindings, true);
3335 return switch_bindings;
3338 void
3339 c_release_switch_bindings (struct c_spot_bindings *bindings)
3341 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3342 XDELETE (bindings);
3345 /* This is called at the point of a case or default label to issue
3346 warnings about decls as needed. It returns true if it found an
3347 error, not just a warning. */
3349 bool
3350 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3351 location_t switch_loc, location_t case_loc)
3353 bool saw_error;
3354 struct c_scope *scope;
3356 saw_error = false;
3357 for (scope = current_scope;
3358 scope != switch_bindings->scope;
3359 scope = scope->outer)
3361 struct c_binding *b;
3363 gcc_assert (scope != NULL);
3365 if (!scope->has_jump_unsafe_decl)
3366 continue;
3368 for (b = scope->bindings; b != NULL; b = b->prev)
3370 if (decl_jump_unsafe (b->decl))
3372 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3374 saw_error = true;
3375 error_at (case_loc,
3376 ("switch jumps into scope of identifier with "
3377 "variably modified type"));
3379 else
3380 warning_at (case_loc, OPT_Wjump_misses_init,
3381 "switch jumps over variable initialization");
3382 inform (switch_loc, "switch starts here");
3383 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3384 b->decl);
3389 if (switch_bindings->stmt_exprs > 0)
3391 saw_error = true;
3392 error_at (case_loc, "switch jumps into statement expression");
3393 inform (switch_loc, "switch starts here");
3396 return saw_error;
3399 /* Given NAME, an IDENTIFIER_NODE,
3400 return the structure (or union or enum) definition for that name.
3401 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3402 CODE says which kind of type the caller wants;
3403 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3404 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3405 location where the tag was defined.
3406 If the wrong kind of type is found, an error is reported. */
3408 static tree
3409 lookup_tag (enum tree_code code, tree name, int thislevel_only,
3410 location_t *ploc)
3412 struct c_binding *b = I_TAG_BINDING (name);
3413 int thislevel = 0;
3415 if (!b || !b->decl)
3416 return 0;
3418 /* We only care about whether it's in this level if
3419 thislevel_only was set or it might be a type clash. */
3420 if (thislevel_only || TREE_CODE (b->decl) != code)
3422 /* For our purposes, a tag in the external scope is the same as
3423 a tag in the file scope. (Primarily relevant to Objective-C
3424 and its builtin structure tags, which get pushed before the
3425 file scope is created.) */
3426 if (B_IN_CURRENT_SCOPE (b)
3427 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3428 thislevel = 1;
3431 if (thislevel_only && !thislevel)
3432 return 0;
3434 if (TREE_CODE (b->decl) != code)
3436 /* Definition isn't the kind we were looking for. */
3437 pending_invalid_xref = name;
3438 pending_invalid_xref_location = input_location;
3440 /* If in the same binding level as a declaration as a tag
3441 of a different type, this must not be allowed to
3442 shadow that tag, so give the error immediately.
3443 (For example, "struct foo; union foo;" is invalid.) */
3444 if (thislevel)
3445 pending_xref_error ();
3448 if (ploc != NULL)
3449 *ploc = b->locus;
3451 return b->decl;
3454 /* Print an error message now
3455 for a recent invalid struct, union or enum cross reference.
3456 We don't print them immediately because they are not invalid
3457 when used in the `struct foo;' construct for shadowing. */
3459 void
3460 pending_xref_error (void)
3462 if (pending_invalid_xref != 0)
3463 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3464 pending_invalid_xref);
3465 pending_invalid_xref = 0;
3469 /* Look up NAME in the current scope and its superiors
3470 in the namespace of variables, functions and typedefs.
3471 Return a ..._DECL node of some kind representing its definition,
3472 or return 0 if it is undefined. */
3474 tree
3475 lookup_name (tree name)
3477 struct c_binding *b = I_SYMBOL_BINDING (name);
3478 if (b && !b->invisible)
3480 maybe_record_typedef_use (b->decl);
3481 return b->decl;
3483 return 0;
3486 /* Similar to `lookup_name' but look only at the indicated scope. */
3488 static tree
3489 lookup_name_in_scope (tree name, struct c_scope *scope)
3491 struct c_binding *b;
3493 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3494 if (B_IN_SCOPE (b, scope))
3495 return b->decl;
3496 return 0;
3499 /* Create the predefined scalar types of C,
3500 and some nodes representing standard constants (0, 1, (void *) 0).
3501 Initialize the global scope.
3502 Make definitions for built-in primitive functions. */
3504 void
3505 c_init_decl_processing (void)
3507 location_t save_loc = input_location;
3509 /* Initialize reserved words for parser. */
3510 c_parse_init ();
3512 current_function_decl = 0;
3514 gcc_obstack_init (&parser_obstack);
3516 /* Make the externals scope. */
3517 push_scope ();
3518 external_scope = current_scope;
3520 /* Declarations from c_common_nodes_and_builtins must not be associated
3521 with this input file, lest we get differences between using and not
3522 using preprocessed headers. */
3523 input_location = BUILTINS_LOCATION;
3525 c_common_nodes_and_builtins ();
3527 /* In C, comparisons and TRUTH_* expressions have type int. */
3528 truthvalue_type_node = integer_type_node;
3529 truthvalue_true_node = integer_one_node;
3530 truthvalue_false_node = integer_zero_node;
3532 /* Even in C99, which has a real boolean type. */
3533 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3534 boolean_type_node));
3536 input_location = save_loc;
3538 pedantic_lvalues = true;
3540 make_fname_decl = c_make_fname_decl;
3541 start_fname_decls ();
3544 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3545 give the decl, NAME is the initialization string and TYPE_DEP
3546 indicates whether NAME depended on the type of the function. As we
3547 don't yet implement delayed emission of static data, we mark the
3548 decl as emitted so it is not placed in the output. Anything using
3549 it must therefore pull out the STRING_CST initializer directly.
3550 FIXME. */
3552 static tree
3553 c_make_fname_decl (location_t loc, tree id, int type_dep)
3555 const char *name = fname_as_string (type_dep);
3556 tree decl, type, init;
3557 size_t length = strlen (name);
3559 type = build_array_type (char_type_node,
3560 build_index_type (size_int (length)));
3561 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3563 decl = build_decl (loc, VAR_DECL, id, type);
3565 TREE_STATIC (decl) = 1;
3566 TREE_READONLY (decl) = 1;
3567 DECL_ARTIFICIAL (decl) = 1;
3569 init = build_string (length + 1, name);
3570 free (CONST_CAST (char *, name));
3571 TREE_TYPE (init) = type;
3572 DECL_INITIAL (decl) = init;
3574 TREE_USED (decl) = 1;
3576 if (current_function_decl
3577 /* For invalid programs like this:
3579 void foo()
3580 const char* p = __FUNCTION__;
3582 the __FUNCTION__ is believed to appear in K&R style function
3583 parameter declarator. In that case we still don't have
3584 function_scope. */
3585 && (!seen_error () || current_function_scope))
3587 DECL_CONTEXT (decl) = current_function_decl;
3588 bind (id, decl, current_function_scope,
3589 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
3592 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
3594 return decl;
3597 tree
3598 c_builtin_function (tree decl)
3600 tree type = TREE_TYPE (decl);
3601 tree id = DECL_NAME (decl);
3603 const char *name = IDENTIFIER_POINTER (id);
3604 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
3606 /* Should never be called on a symbol with a preexisting meaning. */
3607 gcc_assert (!I_SYMBOL_BINDING (id));
3609 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
3610 UNKNOWN_LOCATION);
3612 /* Builtins in the implementation namespace are made visible without
3613 needing to be explicitly declared. See push_file_scope. */
3614 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3616 DECL_CHAIN (decl) = visible_builtins;
3617 visible_builtins = decl;
3620 return decl;
3623 tree
3624 c_builtin_function_ext_scope (tree decl)
3626 tree type = TREE_TYPE (decl);
3627 tree id = DECL_NAME (decl);
3629 const char *name = IDENTIFIER_POINTER (id);
3630 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
3632 /* Should never be called on a symbol with a preexisting meaning. */
3633 gcc_assert (!I_SYMBOL_BINDING (id));
3635 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
3636 UNKNOWN_LOCATION);
3638 /* Builtins in the implementation namespace are made visible without
3639 needing to be explicitly declared. See push_file_scope. */
3640 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3642 DECL_CHAIN (decl) = visible_builtins;
3643 visible_builtins = decl;
3646 return decl;
3649 /* Called when a declaration is seen that contains no names to declare.
3650 If its type is a reference to a structure, union or enum inherited
3651 from a containing scope, shadow that tag name for the current scope
3652 with a forward reference.
3653 If its type defines a new named structure or union
3654 or defines an enum, it is valid but we need not do anything here.
3655 Otherwise, it is an error. */
3657 void
3658 shadow_tag (const struct c_declspecs *declspecs)
3660 shadow_tag_warned (declspecs, 0);
3663 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
3664 but no pedwarn. */
3665 void
3666 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
3668 bool found_tag = false;
3670 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
3672 tree value = declspecs->type;
3673 enum tree_code code = TREE_CODE (value);
3675 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3676 /* Used to test also that TYPE_SIZE (value) != 0.
3677 That caused warning for `struct foo;' at top level in the file. */
3679 tree name = TYPE_NAME (value);
3680 tree t;
3682 found_tag = true;
3684 if (declspecs->restrict_p)
3686 error ("invalid use of %<restrict%>");
3687 warned = 1;
3690 if (name == 0)
3692 if (warned != 1 && code != ENUMERAL_TYPE)
3693 /* Empty unnamed enum OK */
3695 pedwarn (input_location, 0,
3696 "unnamed struct/union that defines no instances");
3697 warned = 1;
3700 else if (declspecs->typespec_kind != ctsk_tagdef
3701 && declspecs->typespec_kind != ctsk_tagfirstref
3702 && declspecs->storage_class != csc_none)
3704 if (warned != 1)
3705 pedwarn (input_location, 0,
3706 "empty declaration with storage class specifier "
3707 "does not redeclare tag");
3708 warned = 1;
3709 pending_xref_error ();
3711 else if (declspecs->typespec_kind != ctsk_tagdef
3712 && declspecs->typespec_kind != ctsk_tagfirstref
3713 && (declspecs->const_p
3714 || declspecs->volatile_p
3715 || declspecs->restrict_p
3716 || declspecs->address_space))
3718 if (warned != 1)
3719 pedwarn (input_location, 0,
3720 "empty declaration with type qualifier "
3721 "does not redeclare tag");
3722 warned = 1;
3723 pending_xref_error ();
3725 else if (declspecs->typespec_kind != ctsk_tagdef
3726 && declspecs->typespec_kind != ctsk_tagfirstref
3727 && declspecs->alignas_p)
3729 if (warned != 1)
3730 pedwarn (input_location, 0,
3731 "empty declaration with %<_Alignas%> "
3732 "does not redeclare tag");
3733 warned = 1;
3734 pending_xref_error ();
3736 else
3738 pending_invalid_xref = 0;
3739 t = lookup_tag (code, name, 1, NULL);
3741 if (t == 0)
3743 t = make_node (code);
3744 pushtag (input_location, name, t);
3748 else
3750 if (warned != 1 && !in_system_header)
3752 pedwarn (input_location, 0,
3753 "useless type name in empty declaration");
3754 warned = 1;
3758 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3760 pedwarn (input_location, 0, "useless type name in empty declaration");
3761 warned = 1;
3764 pending_invalid_xref = 0;
3766 if (declspecs->inline_p)
3768 error ("%<inline%> in empty declaration");
3769 warned = 1;
3772 if (declspecs->noreturn_p)
3774 error ("%<_Noreturn%> in empty declaration");
3775 warned = 1;
3778 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3780 error ("%<auto%> in file-scope empty declaration");
3781 warned = 1;
3784 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3786 error ("%<register%> in file-scope empty declaration");
3787 warned = 1;
3790 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3792 warning (0, "useless storage class specifier in empty declaration");
3793 warned = 2;
3796 if (!warned && !in_system_header && declspecs->thread_p)
3798 warning (0, "useless %<__thread%> in empty declaration");
3799 warned = 2;
3802 if (!warned && !in_system_header && (declspecs->const_p
3803 || declspecs->volatile_p
3804 || declspecs->restrict_p
3805 || declspecs->address_space))
3807 warning (0, "useless type qualifier in empty declaration");
3808 warned = 2;
3811 if (!warned && !in_system_header && declspecs->alignas_p)
3813 warning (0, "useless %<_Alignas%> in empty declaration");
3814 warned = 2;
3817 if (warned != 1)
3819 if (!found_tag)
3820 pedwarn (input_location, 0, "empty declaration");
3825 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3826 bits. SPECS represents declaration specifiers that the grammar
3827 only permits to contain type qualifiers and attributes. */
3830 quals_from_declspecs (const struct c_declspecs *specs)
3832 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3833 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3834 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
3835 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
3836 gcc_assert (!specs->type
3837 && !specs->decl_attr
3838 && specs->typespec_word == cts_none
3839 && specs->storage_class == csc_none
3840 && !specs->typedef_p
3841 && !specs->explicit_signed_p
3842 && !specs->deprecated_p
3843 && !specs->long_p
3844 && !specs->long_long_p
3845 && !specs->short_p
3846 && !specs->signed_p
3847 && !specs->unsigned_p
3848 && !specs->complex_p
3849 && !specs->inline_p
3850 && !specs->noreturn_p
3851 && !specs->thread_p);
3852 return quals;
3855 /* Construct an array declarator. LOC is the location of the
3856 beginning of the array (usually the opening brace). EXPR is the
3857 expression inside [], or NULL_TREE. QUALS are the type qualifiers
3858 inside the [] (to be applied to the pointer to which a parameter
3859 array is converted). STATIC_P is true if "static" is inside the
3860 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
3861 VLA of unspecified length which is nevertheless a complete type,
3862 false otherwise. The field for the contained declarator is left to
3863 be filled in by set_array_declarator_inner. */
3865 struct c_declarator *
3866 build_array_declarator (location_t loc,
3867 tree expr, struct c_declspecs *quals, bool static_p,
3868 bool vla_unspec_p)
3870 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3871 struct c_declarator);
3872 declarator->id_loc = loc;
3873 declarator->kind = cdk_array;
3874 declarator->declarator = 0;
3875 declarator->u.array.dimen = expr;
3876 if (quals)
3878 declarator->u.array.attrs = quals->attrs;
3879 declarator->u.array.quals = quals_from_declspecs (quals);
3881 else
3883 declarator->u.array.attrs = NULL_TREE;
3884 declarator->u.array.quals = 0;
3886 declarator->u.array.static_p = static_p;
3887 declarator->u.array.vla_unspec_p = vla_unspec_p;
3888 if (!flag_isoc99)
3890 if (static_p || quals != NULL)
3891 pedwarn (loc, OPT_Wpedantic,
3892 "ISO C90 does not support %<static%> or type "
3893 "qualifiers in parameter array declarators");
3894 if (vla_unspec_p)
3895 pedwarn (loc, OPT_Wpedantic,
3896 "ISO C90 does not support %<[*]%> array declarators");
3898 if (vla_unspec_p)
3900 if (!current_scope->parm_flag)
3902 /* C99 6.7.5.2p4 */
3903 error_at (loc, "%<[*]%> not allowed in other than "
3904 "function prototype scope");
3905 declarator->u.array.vla_unspec_p = false;
3906 return NULL;
3908 current_scope->had_vla_unspec = true;
3910 return declarator;
3913 /* Set the contained declarator of an array declarator. DECL is the
3914 declarator, as constructed by build_array_declarator; INNER is what
3915 appears on the left of the []. */
3917 struct c_declarator *
3918 set_array_declarator_inner (struct c_declarator *decl,
3919 struct c_declarator *inner)
3921 decl->declarator = inner;
3922 return decl;
3925 /* INIT is a constructor that forms DECL's initializer. If the final
3926 element initializes a flexible array field, add the size of that
3927 initializer to DECL's size. */
3929 static void
3930 add_flexible_array_elts_to_size (tree decl, tree init)
3932 tree elt, type;
3934 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
3935 return;
3937 elt = CONSTRUCTOR_ELTS (init)->last ().value;
3938 type = TREE_TYPE (elt);
3939 if (TREE_CODE (type) == ARRAY_TYPE
3940 && TYPE_SIZE (type) == NULL_TREE
3941 && TYPE_DOMAIN (type) != NULL_TREE
3942 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3944 complete_array_type (&type, elt, false);
3945 DECL_SIZE (decl)
3946 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3947 DECL_SIZE_UNIT (decl)
3948 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3952 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3953 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3954 before the type name, and set *EXPR_CONST_OPERANDS, if
3955 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3956 appear in a constant expression. */
3958 tree
3959 groktypename (struct c_type_name *type_name, tree *expr,
3960 bool *expr_const_operands)
3962 tree type;
3963 tree attrs = type_name->specs->attrs;
3965 type_name->specs->attrs = NULL_TREE;
3967 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3968 false, NULL, &attrs, expr, expr_const_operands,
3969 DEPRECATED_NORMAL);
3971 /* Apply attributes. */
3972 decl_attributes (&type, attrs, 0);
3974 return type;
3977 /* Decode a declarator in an ordinary declaration or data definition.
3978 This is called as soon as the type information and variable name
3979 have been parsed, before parsing the initializer if any.
3980 Here we create the ..._DECL node, fill in its type,
3981 and put it on the list of decls for the current context.
3982 The ..._DECL node is returned as the value.
3984 Exception: for arrays where the length is not specified,
3985 the type is left null, to be filled in by `finish_decl'.
3987 Function definitions do not come here; they go to start_function
3988 instead. However, external and forward declarations of functions
3989 do go through here. Structure field declarations are done by
3990 grokfield and not through here. */
3992 tree
3993 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3994 bool initialized, tree attributes)
3996 tree decl;
3997 tree tem;
3998 tree expr = NULL_TREE;
3999 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4001 /* An object declared as __attribute__((deprecated)) suppresses
4002 warnings of uses of other deprecated items. */
4003 if (lookup_attribute ("deprecated", attributes))
4004 deprecated_state = DEPRECATED_SUPPRESS;
4006 decl = grokdeclarator (declarator, declspecs,
4007 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4008 deprecated_state);
4009 if (!decl)
4010 return 0;
4012 if (expr)
4013 add_stmt (fold_convert (void_type_node, expr));
4015 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4016 warning (OPT_Wmain, "%q+D is usually a function", decl);
4018 if (initialized)
4019 /* Is it valid for this decl to have an initializer at all?
4020 If not, set INITIALIZED to zero, which will indirectly
4021 tell 'finish_decl' to ignore the initializer once it is parsed. */
4022 switch (TREE_CODE (decl))
4024 case TYPE_DECL:
4025 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4026 initialized = 0;
4027 break;
4029 case FUNCTION_DECL:
4030 error ("function %qD is initialized like a variable", decl);
4031 initialized = 0;
4032 break;
4034 case PARM_DECL:
4035 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4036 error ("parameter %qD is initialized", decl);
4037 initialized = 0;
4038 break;
4040 default:
4041 /* Don't allow initializations for incomplete types except for
4042 arrays which might be completed by the initialization. */
4044 /* This can happen if the array size is an undefined macro.
4045 We already gave a warning, so we don't need another one. */
4046 if (TREE_TYPE (decl) == error_mark_node)
4047 initialized = 0;
4048 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4050 /* A complete type is ok if size is fixed. */
4052 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4053 || C_DECL_VARIABLE_SIZE (decl))
4055 error ("variable-sized object may not be initialized");
4056 initialized = 0;
4059 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4061 error ("variable %qD has initializer but incomplete type", decl);
4062 initialized = 0;
4064 else if (C_DECL_VARIABLE_SIZE (decl))
4066 /* Although C99 is unclear about whether incomplete arrays
4067 of VLAs themselves count as VLAs, it does not make
4068 sense to permit them to be initialized given that
4069 ordinary VLAs may not be initialized. */
4070 error ("variable-sized object may not be initialized");
4071 initialized = 0;
4075 if (initialized)
4077 if (current_scope == file_scope)
4078 TREE_STATIC (decl) = 1;
4080 /* Tell 'pushdecl' this is an initialized decl
4081 even though we don't yet have the initializer expression.
4082 Also tell 'finish_decl' it may store the real initializer. */
4083 DECL_INITIAL (decl) = error_mark_node;
4086 /* If this is a function declaration, write a record describing it to the
4087 prototypes file (if requested). */
4089 if (TREE_CODE (decl) == FUNCTION_DECL)
4090 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4092 /* ANSI specifies that a tentative definition which is not merged with
4093 a non-tentative definition behaves exactly like a definition with an
4094 initializer equal to zero. (Section 3.7.2)
4096 -fno-common gives strict ANSI behavior, though this tends to break
4097 a large body of code that grew up without this rule.
4099 Thread-local variables are never common, since there's no entrenched
4100 body of code to break, and it allows more efficient variable references
4101 in the presence of dynamic linking. */
4103 if (TREE_CODE (decl) == VAR_DECL
4104 && !initialized
4105 && TREE_PUBLIC (decl)
4106 && !DECL_THREAD_LOCAL_P (decl)
4107 && !flag_no_common)
4108 DECL_COMMON (decl) = 1;
4110 /* Set attributes here so if duplicate decl, will have proper attributes. */
4111 decl_attributes (&decl, attributes, 0);
4113 /* Handle gnu_inline attribute. */
4114 if (declspecs->inline_p
4115 && !flag_gnu89_inline
4116 && TREE_CODE (decl) == FUNCTION_DECL
4117 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4118 || current_function_decl))
4120 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4122 else if (declspecs->storage_class != csc_static)
4123 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4126 if (TREE_CODE (decl) == FUNCTION_DECL
4127 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4129 struct c_declarator *ce = declarator;
4131 if (ce->kind == cdk_pointer)
4132 ce = declarator->declarator;
4133 if (ce->kind == cdk_function)
4135 tree args = ce->u.arg_info->parms;
4136 for (; args; args = DECL_CHAIN (args))
4138 tree type = TREE_TYPE (args);
4139 if (type && INTEGRAL_TYPE_P (type)
4140 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4141 DECL_ARG_TYPE (args) = integer_type_node;
4146 if (TREE_CODE (decl) == FUNCTION_DECL
4147 && DECL_DECLARED_INLINE_P (decl)
4148 && DECL_UNINLINABLE (decl)
4149 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4150 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4151 decl);
4153 /* C99 6.7.4p3: An inline definition of a function with external
4154 linkage shall not contain a definition of a modifiable object
4155 with static storage duration... */
4156 if (TREE_CODE (decl) == VAR_DECL
4157 && current_scope != file_scope
4158 && TREE_STATIC (decl)
4159 && !TREE_READONLY (decl)
4160 && DECL_DECLARED_INLINE_P (current_function_decl)
4161 && DECL_EXTERNAL (current_function_decl))
4162 record_inline_static (input_location, current_function_decl,
4163 decl, csi_modifiable);
4165 if (c_dialect_objc ()
4166 && (TREE_CODE (decl) == VAR_DECL
4167 || TREE_CODE (decl) == FUNCTION_DECL))
4168 objc_check_global_decl (decl);
4170 /* Add this decl to the current scope.
4171 TEM may equal DECL or it may be a previous decl of the same name. */
4172 tem = pushdecl (decl);
4174 if (initialized && DECL_EXTERNAL (tem))
4176 DECL_EXTERNAL (tem) = 0;
4177 TREE_STATIC (tem) = 1;
4180 return tem;
4183 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4184 DECL or the non-array element type if DECL is an uninitialized array.
4185 If that type has a const member, diagnose this. */
4187 static void
4188 diagnose_uninitialized_cst_member (tree decl, tree type)
4190 tree field;
4191 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4193 tree field_type;
4194 if (TREE_CODE (field) != FIELD_DECL)
4195 continue;
4196 field_type = strip_array_types (TREE_TYPE (field));
4198 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4200 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4201 "uninitialized const member in %qT is invalid in C++",
4202 strip_array_types (TREE_TYPE (decl)));
4203 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4206 if (TREE_CODE (field_type) == RECORD_TYPE
4207 || TREE_CODE (field_type) == UNION_TYPE)
4208 diagnose_uninitialized_cst_member (decl, field_type);
4212 /* Finish processing of a declaration;
4213 install its initial value.
4214 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4215 If the length of an array type is not known before,
4216 it must be determined now, from the initial value, or it is an error.
4218 INIT_LOC is the location of the initial value. */
4220 void
4221 finish_decl (tree decl, location_t init_loc, tree init,
4222 tree origtype, tree asmspec_tree)
4224 tree type;
4225 bool was_incomplete = (DECL_SIZE (decl) == 0);
4226 const char *asmspec = 0;
4228 /* If a name was specified, get the string. */
4229 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4230 && DECL_FILE_SCOPE_P (decl))
4231 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4232 if (asmspec_tree)
4233 asmspec = TREE_STRING_POINTER (asmspec_tree);
4235 if (TREE_CODE (decl) == VAR_DECL
4236 && TREE_STATIC (decl)
4237 && global_bindings_p ())
4238 /* So decl is a global variable. Record the types it uses
4239 so that we can decide later to emit debug info for them. */
4240 record_types_used_by_current_var_decl (decl);
4242 /* If `start_decl' didn't like having an initialization, ignore it now. */
4243 if (init != 0 && DECL_INITIAL (decl) == 0)
4244 init = 0;
4246 /* Don't crash if parm is initialized. */
4247 if (TREE_CODE (decl) == PARM_DECL)
4248 init = 0;
4250 if (init)
4251 store_init_value (init_loc, decl, init, origtype);
4253 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
4254 || TREE_CODE (decl) == FUNCTION_DECL
4255 || TREE_CODE (decl) == FIELD_DECL))
4256 objc_check_decl (decl);
4258 type = TREE_TYPE (decl);
4260 /* Deduce size of array from initialization, if not already known. */
4261 if (TREE_CODE (type) == ARRAY_TYPE
4262 && TYPE_DOMAIN (type) == 0
4263 && TREE_CODE (decl) != TYPE_DECL)
4265 bool do_default
4266 = (TREE_STATIC (decl)
4267 /* Even if pedantic, an external linkage array
4268 may have incomplete type at first. */
4269 ? pedantic && !TREE_PUBLIC (decl)
4270 : !DECL_EXTERNAL (decl));
4271 int failure
4272 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4273 do_default);
4275 /* Get the completed type made by complete_array_type. */
4276 type = TREE_TYPE (decl);
4278 switch (failure)
4280 case 1:
4281 error ("initializer fails to determine size of %q+D", decl);
4282 break;
4284 case 2:
4285 if (do_default)
4286 error ("array size missing in %q+D", decl);
4287 /* If a `static' var's size isn't known,
4288 make it extern as well as static, so it does not get
4289 allocated.
4290 If it is not `static', then do not mark extern;
4291 finish_incomplete_decl will give it a default size
4292 and it will get allocated. */
4293 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4294 DECL_EXTERNAL (decl) = 1;
4295 break;
4297 case 3:
4298 error ("zero or negative size array %q+D", decl);
4299 break;
4301 case 0:
4302 /* For global variables, update the copy of the type that
4303 exists in the binding. */
4304 if (TREE_PUBLIC (decl))
4306 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4307 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4308 b_ext = b_ext->shadowed;
4309 if (b_ext)
4311 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4312 b_ext->u.type = composite_type (b_ext->u.type, type);
4313 else
4314 b_ext->u.type = type;
4317 break;
4319 default:
4320 gcc_unreachable ();
4323 if (DECL_INITIAL (decl))
4324 TREE_TYPE (DECL_INITIAL (decl)) = type;
4326 relayout_decl (decl);
4329 if (TREE_CODE (decl) == VAR_DECL)
4331 if (init && TREE_CODE (init) == CONSTRUCTOR)
4332 add_flexible_array_elts_to_size (decl, init);
4334 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4335 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4336 layout_decl (decl, 0);
4338 if (DECL_SIZE (decl) == 0
4339 /* Don't give an error if we already gave one earlier. */
4340 && TREE_TYPE (decl) != error_mark_node
4341 && (TREE_STATIC (decl)
4342 /* A static variable with an incomplete type
4343 is an error if it is initialized.
4344 Also if it is not file scope.
4345 Otherwise, let it through, but if it is not `extern'
4346 then it may cause an error message later. */
4347 ? (DECL_INITIAL (decl) != 0
4348 || !DECL_FILE_SCOPE_P (decl))
4349 /* An automatic variable with an incomplete type
4350 is an error. */
4351 : !DECL_EXTERNAL (decl)))
4353 error ("storage size of %q+D isn%'t known", decl);
4354 TREE_TYPE (decl) = error_mark_node;
4357 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4358 && DECL_SIZE (decl) != 0)
4360 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4361 constant_expression_warning (DECL_SIZE (decl));
4362 else
4364 error ("storage size of %q+D isn%'t constant", decl);
4365 TREE_TYPE (decl) = error_mark_node;
4369 if (TREE_USED (type))
4371 TREE_USED (decl) = 1;
4372 DECL_READ_P (decl) = 1;
4376 /* If this is a function and an assembler name is specified, reset DECL_RTL
4377 so we can give it its new name. Also, update builtin_decl if it
4378 was a normal built-in. */
4379 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4381 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4382 set_builtin_user_assembler_name (decl, asmspec);
4383 set_user_assembler_name (decl, asmspec);
4386 /* If #pragma weak was used, mark the decl weak now. */
4387 maybe_apply_pragma_weak (decl);
4389 /* Output the assembler code and/or RTL code for variables and functions,
4390 unless the type is an undefined structure or union.
4391 If not, it will get done when the type is completed. */
4393 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
4395 /* Determine the ELF visibility. */
4396 if (TREE_PUBLIC (decl))
4397 c_determine_visibility (decl);
4399 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4400 if (c_dialect_objc ())
4401 objc_check_decl (decl);
4403 if (asmspec)
4405 /* If this is not a static variable, issue a warning.
4406 It doesn't make any sense to give an ASMSPEC for an
4407 ordinary, non-register local variable. Historically,
4408 GCC has accepted -- but ignored -- the ASMSPEC in
4409 this case. */
4410 if (!DECL_FILE_SCOPE_P (decl)
4411 && TREE_CODE (decl) == VAR_DECL
4412 && !C_DECL_REGISTER (decl)
4413 && !TREE_STATIC (decl))
4414 warning (0, "ignoring asm-specifier for non-static local "
4415 "variable %q+D", decl);
4416 else
4417 set_user_assembler_name (decl, asmspec);
4420 if (DECL_FILE_SCOPE_P (decl))
4422 if (DECL_INITIAL (decl) == NULL_TREE
4423 || DECL_INITIAL (decl) == error_mark_node)
4424 /* Don't output anything
4425 when a tentative file-scope definition is seen.
4426 But at end of compilation, do output code for them. */
4427 DECL_DEFER_OUTPUT (decl) = 1;
4428 if (asmspec && C_DECL_REGISTER (decl))
4429 DECL_HARD_REGISTER (decl) = 1;
4430 rest_of_decl_compilation (decl, true, 0);
4432 else
4434 /* In conjunction with an ASMSPEC, the `register'
4435 keyword indicates that we should place the variable
4436 in a particular register. */
4437 if (asmspec && C_DECL_REGISTER (decl))
4439 DECL_HARD_REGISTER (decl) = 1;
4440 /* This cannot be done for a structure with volatile
4441 fields, on which DECL_REGISTER will have been
4442 reset. */
4443 if (!DECL_REGISTER (decl))
4444 error ("cannot put object with volatile field into register");
4447 if (TREE_CODE (decl) != FUNCTION_DECL)
4449 /* If we're building a variable sized type, and we might be
4450 reachable other than via the top of the current binding
4451 level, then create a new BIND_EXPR so that we deallocate
4452 the object at the right time. */
4453 /* Note that DECL_SIZE can be null due to errors. */
4454 if (DECL_SIZE (decl)
4455 && !TREE_CONSTANT (DECL_SIZE (decl))
4456 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4458 tree bind;
4459 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4460 TREE_SIDE_EFFECTS (bind) = 1;
4461 add_stmt (bind);
4462 BIND_EXPR_BODY (bind) = push_stmt_list ();
4464 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4465 DECL_EXPR, decl));
4470 if (!DECL_FILE_SCOPE_P (decl))
4472 /* Recompute the RTL of a local array now
4473 if it used to be an incomplete type. */
4474 if (was_incomplete
4475 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4477 /* If we used it already as memory, it must stay in memory. */
4478 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4479 /* If it's still incomplete now, no init will save it. */
4480 if (DECL_SIZE (decl) == 0)
4481 DECL_INITIAL (decl) = 0;
4486 if (TREE_CODE (decl) == TYPE_DECL)
4488 if (!DECL_FILE_SCOPE_P (decl)
4489 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4490 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4492 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4495 /* Install a cleanup (aka destructor) if one was given. */
4496 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4498 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4499 if (attr)
4501 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4502 tree cleanup_decl = lookup_name (cleanup_id);
4503 tree cleanup;
4504 vec<tree, va_gc> *v;
4506 /* Build "cleanup(&decl)" for the destructor. */
4507 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
4508 vec_alloc (v, 1);
4509 v->quick_push (cleanup);
4510 cleanup = build_function_call_vec (DECL_SOURCE_LOCATION (decl),
4511 cleanup_decl, v, NULL);
4512 vec_free (v);
4514 /* Don't warn about decl unused; the cleanup uses it. */
4515 TREE_USED (decl) = 1;
4516 TREE_USED (cleanup_decl) = 1;
4517 DECL_READ_P (decl) = 1;
4519 push_cleanup (decl, cleanup, false);
4523 if (warn_cxx_compat
4524 && TREE_CODE (decl) == VAR_DECL
4525 && !DECL_EXTERNAL (decl)
4526 && DECL_INITIAL (decl) == NULL_TREE)
4528 type = strip_array_types (type);
4529 if (TREE_READONLY (decl))
4530 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4531 "uninitialized const %qD is invalid in C++", decl);
4532 else if ((TREE_CODE (type) == RECORD_TYPE
4533 || TREE_CODE (type) == UNION_TYPE)
4534 && C_TYPE_FIELDS_READONLY (type))
4535 diagnose_uninitialized_cst_member (decl, type);
4538 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
4541 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
4542 EXPR is NULL or a pointer to an expression that needs to be
4543 evaluated for the side effects of array size expressions in the
4544 parameters. */
4546 tree
4547 grokparm (const struct c_parm *parm, tree *expr)
4549 tree attrs = parm->attrs;
4550 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4551 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
4553 decl_attributes (&decl, attrs, 0);
4555 return decl;
4558 /* Given a parsed parameter declaration, decode it into a PARM_DECL
4559 and push that on the current scope. EXPR is a pointer to an
4560 expression that needs to be evaluated for the side effects of array
4561 size expressions in the parameters. */
4563 void
4564 push_parm_decl (const struct c_parm *parm, tree *expr)
4566 tree attrs = parm->attrs;
4567 tree decl;
4569 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
4570 &attrs, expr, NULL, DEPRECATED_NORMAL);
4571 decl_attributes (&decl, attrs, 0);
4573 decl = pushdecl (decl);
4575 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
4578 /* Mark all the parameter declarations to date as forward decls.
4579 Also diagnose use of this extension. */
4581 void
4582 mark_forward_parm_decls (void)
4584 struct c_binding *b;
4586 if (pedantic && !current_scope->warned_forward_parm_decls)
4588 pedwarn (input_location, OPT_Wpedantic,
4589 "ISO C forbids forward parameter declarations");
4590 current_scope->warned_forward_parm_decls = true;
4593 for (b = current_scope->bindings; b; b = b->prev)
4594 if (TREE_CODE (b->decl) == PARM_DECL)
4595 TREE_ASM_WRITTEN (b->decl) = 1;
4598 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
4599 literal, which may be an incomplete array type completed by the
4600 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
4601 literal. NON_CONST is true if the initializers contain something
4602 that cannot occur in a constant expression. */
4604 tree
4605 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
4607 /* We do not use start_decl here because we have a type, not a declarator;
4608 and do not use finish_decl because the decl should be stored inside
4609 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
4610 tree decl;
4611 tree complit;
4612 tree stmt;
4614 if (type == error_mark_node
4615 || init == error_mark_node)
4616 return error_mark_node;
4618 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
4619 DECL_EXTERNAL (decl) = 0;
4620 TREE_PUBLIC (decl) = 0;
4621 TREE_STATIC (decl) = (current_scope == file_scope);
4622 DECL_CONTEXT (decl) = current_function_decl;
4623 TREE_USED (decl) = 1;
4624 DECL_READ_P (decl) = 1;
4625 TREE_TYPE (decl) = type;
4626 TREE_READONLY (decl) = (TYPE_READONLY (type)
4627 || (TREE_CODE (type) == ARRAY_TYPE
4628 && TYPE_READONLY (TREE_TYPE (type))));
4629 store_init_value (loc, decl, init, NULL_TREE);
4631 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
4633 int failure = complete_array_type (&TREE_TYPE (decl),
4634 DECL_INITIAL (decl), true);
4635 gcc_assert (!failure);
4637 type = TREE_TYPE (decl);
4638 TREE_TYPE (DECL_INITIAL (decl)) = type;
4641 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4643 c_incomplete_type_error (NULL_TREE, type);
4644 return error_mark_node;
4647 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
4648 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
4649 TREE_SIDE_EFFECTS (complit) = 1;
4651 layout_decl (decl, 0);
4653 if (TREE_STATIC (decl))
4655 /* This decl needs a name for the assembler output. */
4656 set_compound_literal_name (decl);
4657 DECL_DEFER_OUTPUT (decl) = 1;
4658 DECL_COMDAT (decl) = 1;
4659 DECL_ARTIFICIAL (decl) = 1;
4660 DECL_IGNORED_P (decl) = 1;
4661 pushdecl (decl);
4662 rest_of_decl_compilation (decl, 1, 0);
4665 if (non_const)
4667 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
4668 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
4671 return complit;
4674 /* Check the type of a compound literal. Here we just check that it
4675 is valid for C++. */
4677 void
4678 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
4680 if (warn_cxx_compat
4681 && (type_name->specs->typespec_kind == ctsk_tagdef
4682 || type_name->specs->typespec_kind == ctsk_tagfirstref))
4683 warning_at (loc, OPT_Wc___compat,
4684 "defining a type in a compound literal is invalid in C++");
4687 /* Determine whether TYPE is a structure with a flexible array member,
4688 or a union containing such a structure (possibly recursively). */
4690 static bool
4691 flexible_array_type_p (tree type)
4693 tree x;
4694 switch (TREE_CODE (type))
4696 case RECORD_TYPE:
4697 x = TYPE_FIELDS (type);
4698 if (x == NULL_TREE)
4699 return false;
4700 while (DECL_CHAIN (x) != NULL_TREE)
4701 x = DECL_CHAIN (x);
4702 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4703 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4704 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
4705 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
4706 return true;
4707 return false;
4708 case UNION_TYPE:
4709 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
4711 if (flexible_array_type_p (TREE_TYPE (x)))
4712 return true;
4714 return false;
4715 default:
4716 return false;
4720 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
4721 replacing with appropriate values if they are invalid. */
4722 static void
4723 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
4725 tree type_mv;
4726 unsigned int max_width;
4727 unsigned HOST_WIDE_INT w;
4728 const char *name = (orig_name
4729 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
4730 : _("<anonymous>"));
4732 /* Detect and ignore out of range field width and process valid
4733 field widths. */
4734 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
4736 error ("bit-field %qs width not an integer constant", name);
4737 *width = integer_one_node;
4739 else
4741 if (TREE_CODE (*width) != INTEGER_CST)
4743 *width = c_fully_fold (*width, false, NULL);
4744 if (TREE_CODE (*width) == INTEGER_CST)
4745 pedwarn (input_location, OPT_Wpedantic,
4746 "bit-field %qs width not an integer constant expression",
4747 name);
4749 if (TREE_CODE (*width) != INTEGER_CST)
4751 error ("bit-field %qs width not an integer constant", name);
4752 *width = integer_one_node;
4754 constant_expression_warning (*width);
4755 if (tree_int_cst_sgn (*width) < 0)
4757 error ("negative width in bit-field %qs", name);
4758 *width = integer_one_node;
4760 else if (integer_zerop (*width) && orig_name)
4762 error ("zero width for bit-field %qs", name);
4763 *width = integer_one_node;
4767 /* Detect invalid bit-field type. */
4768 if (TREE_CODE (*type) != INTEGER_TYPE
4769 && TREE_CODE (*type) != BOOLEAN_TYPE
4770 && TREE_CODE (*type) != ENUMERAL_TYPE)
4772 error ("bit-field %qs has invalid type", name);
4773 *type = unsigned_type_node;
4776 type_mv = TYPE_MAIN_VARIANT (*type);
4777 if (!in_system_header
4778 && type_mv != integer_type_node
4779 && type_mv != unsigned_type_node
4780 && type_mv != boolean_type_node)
4781 pedwarn (input_location, OPT_Wpedantic,
4782 "type of bit-field %qs is a GCC extension", name);
4784 max_width = TYPE_PRECISION (*type);
4786 if (0 < compare_tree_int (*width, max_width))
4788 error ("width of %qs exceeds its type", name);
4789 w = max_width;
4790 *width = build_int_cst (integer_type_node, w);
4792 else
4793 w = tree_low_cst (*width, 1);
4795 if (TREE_CODE (*type) == ENUMERAL_TYPE)
4797 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
4798 if (!lt
4799 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
4800 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
4801 warning (0, "%qs is narrower than values of its type", name);
4807 /* Print warning about variable length array if necessary. */
4809 static void
4810 warn_variable_length_array (tree name, tree size)
4812 int const_size = TREE_CONSTANT (size);
4814 if (!flag_isoc99 && pedantic && warn_vla != 0)
4816 if (const_size)
4818 if (name)
4819 pedwarn (input_location, OPT_Wvla,
4820 "ISO C90 forbids array %qE whose size "
4821 "can%'t be evaluated",
4822 name);
4823 else
4824 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
4825 "can%'t be evaluated");
4827 else
4829 if (name)
4830 pedwarn (input_location, OPT_Wvla,
4831 "ISO C90 forbids variable length array %qE",
4832 name);
4833 else
4834 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
4837 else if (warn_vla > 0)
4839 if (const_size)
4841 if (name)
4842 warning (OPT_Wvla,
4843 "the size of array %qE can"
4844 "%'t be evaluated", name);
4845 else
4846 warning (OPT_Wvla,
4847 "the size of array can %'t be evaluated");
4849 else
4851 if (name)
4852 warning (OPT_Wvla,
4853 "variable length array %qE is used",
4854 name);
4855 else
4856 warning (OPT_Wvla,
4857 "variable length array is used");
4862 /* Given declspecs and a declarator,
4863 determine the name and type of the object declared
4864 and construct a ..._DECL node for it.
4865 (In one case we can return a ..._TYPE node instead.
4866 For invalid input we sometimes return 0.)
4868 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4870 DECL_CONTEXT says which syntactic context this declaration is in:
4871 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4872 FUNCDEF for a function definition. Like NORMAL but a few different
4873 error messages in each case. Return value may be zero meaning
4874 this definition is too screwy to try to parse.
4875 PARM for a parameter declaration (either within a function prototype
4876 or before a function body). Make a PARM_DECL, or return void_type_node.
4877 TYPENAME if for a typename (in a cast or sizeof).
4878 Don't make a DECL node; just return the ..._TYPE node.
4879 FIELD for a struct or union field; make a FIELD_DECL.
4880 INITIALIZED is true if the decl has an initializer.
4881 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4882 representing the width of the bit-field.
4883 DECL_ATTRS points to the list of attributes that should be added to this
4884 decl. Any nested attributes that belong on the decl itself will be
4885 added to this list.
4886 If EXPR is not NULL, any expressions that need to be evaluated as
4887 part of evaluating variably modified types will be stored in *EXPR.
4888 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4889 set to indicate whether operands in *EXPR can be used in constant
4890 expressions.
4891 DEPRECATED_STATE is a deprecated_states value indicating whether
4892 deprecation warnings should be suppressed.
4894 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4895 It may also be so in the PARM case, for a prototype where the
4896 argument type is specified but not the name.
4898 This function is where the complicated C meanings of `static'
4899 and `extern' are interpreted. */
4901 static tree
4902 grokdeclarator (const struct c_declarator *declarator,
4903 struct c_declspecs *declspecs,
4904 enum decl_context decl_context, bool initialized, tree *width,
4905 tree *decl_attrs, tree *expr, bool *expr_const_operands,
4906 enum deprecated_states deprecated_state)
4908 tree type = declspecs->type;
4909 bool threadp = declspecs->thread_p;
4910 enum c_storage_class storage_class = declspecs->storage_class;
4911 int constp;
4912 int restrictp;
4913 int volatilep;
4914 int type_quals = TYPE_UNQUALIFIED;
4915 tree name = NULL_TREE;
4916 bool funcdef_flag = false;
4917 bool funcdef_syntax = false;
4918 bool size_varies = false;
4919 tree decl_attr = declspecs->decl_attr;
4920 int array_ptr_quals = TYPE_UNQUALIFIED;
4921 tree array_ptr_attrs = NULL_TREE;
4922 int array_parm_static = 0;
4923 bool array_parm_vla_unspec_p = false;
4924 tree returned_attrs = NULL_TREE;
4925 bool bitfield = width != NULL;
4926 tree element_type;
4927 struct c_arg_info *arg_info = 0;
4928 addr_space_t as1, as2, address_space;
4929 location_t loc = UNKNOWN_LOCATION;
4930 const char *errmsg;
4931 tree expr_dummy;
4932 bool expr_const_operands_dummy;
4933 enum c_declarator_kind first_non_attr_kind;
4934 unsigned int alignas_align = 0;
4936 if (TREE_CODE (type) == ERROR_MARK)
4937 return error_mark_node;
4938 if (expr == NULL)
4939 expr = &expr_dummy;
4940 if (expr_const_operands == NULL)
4941 expr_const_operands = &expr_const_operands_dummy;
4943 *expr = declspecs->expr;
4944 *expr_const_operands = declspecs->expr_const_operands;
4946 if (decl_context == FUNCDEF)
4947 funcdef_flag = true, decl_context = NORMAL;
4949 /* Look inside a declarator for the name being declared
4950 and get it as an IDENTIFIER_NODE, for an error message. */
4952 const struct c_declarator *decl = declarator;
4954 first_non_attr_kind = cdk_attrs;
4955 while (decl)
4956 switch (decl->kind)
4958 case cdk_array:
4959 loc = decl->id_loc;
4960 /* FALL THRU. */
4962 case cdk_function:
4963 case cdk_pointer:
4964 funcdef_syntax = (decl->kind == cdk_function);
4965 decl = decl->declarator;
4966 if (first_non_attr_kind == cdk_attrs)
4967 first_non_attr_kind = decl->kind;
4968 break;
4970 case cdk_attrs:
4971 decl = decl->declarator;
4972 break;
4974 case cdk_id:
4975 loc = decl->id_loc;
4976 if (decl->u.id)
4977 name = decl->u.id;
4978 if (first_non_attr_kind == cdk_attrs)
4979 first_non_attr_kind = decl->kind;
4980 decl = 0;
4981 break;
4983 default:
4984 gcc_unreachable ();
4986 if (name == 0)
4988 gcc_assert (decl_context == PARM
4989 || decl_context == TYPENAME
4990 || (decl_context == FIELD
4991 && declarator->kind == cdk_id));
4992 gcc_assert (!initialized);
4996 /* A function definition's declarator must have the form of
4997 a function declarator. */
4999 if (funcdef_flag && !funcdef_syntax)
5000 return 0;
5002 /* If this looks like a function definition, make it one,
5003 even if it occurs where parms are expected.
5004 Then store_parm_decls will reject it and not use it as a parm. */
5005 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5006 decl_context = PARM;
5008 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5009 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5011 if ((decl_context == NORMAL || decl_context == FIELD)
5012 && current_scope == file_scope
5013 && variably_modified_type_p (type, NULL_TREE))
5015 if (name)
5016 error_at (loc, "variably modified %qE at file scope", name);
5017 else
5018 error_at (loc, "variably modified field at file scope");
5019 type = integer_type_node;
5022 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5024 /* Diagnose defaulting to "int". */
5026 if (declspecs->default_int_p && !in_system_header)
5028 /* Issue a warning if this is an ISO C 99 program or if
5029 -Wreturn-type and this is a function, or if -Wimplicit;
5030 prefer the former warning since it is more explicit. */
5031 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5032 && funcdef_flag)
5033 warn_about_return_type = 1;
5034 else
5036 if (name)
5037 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wimplicit_int,
5038 "type defaults to %<int%> in declaration of %qE",
5039 name);
5040 else
5041 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
5042 "type defaults to %<int%> in type name");
5046 /* Adjust the type if a bit-field is being declared,
5047 -funsigned-bitfields applied and the type is not explicitly
5048 "signed". */
5049 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5050 && TREE_CODE (type) == INTEGER_TYPE)
5051 type = unsigned_type_for (type);
5053 /* Figure out the type qualifiers for the declaration. There are
5054 two ways a declaration can become qualified. One is something
5055 like `const int i' where the `const' is explicit. Another is
5056 something like `typedef const int CI; CI i' where the type of the
5057 declaration contains the `const'. A third possibility is that
5058 there is a type qualifier on the element type of a typedefed
5059 array type, in which case we should extract that qualifier so
5060 that c_apply_type_quals_to_decl receives the full list of
5061 qualifiers to work with (C90 is not entirely clear about whether
5062 duplicate qualifiers should be diagnosed in this case, but it
5063 seems most appropriate to do so). */
5064 element_type = strip_array_types (type);
5065 constp = declspecs->const_p + TYPE_READONLY (element_type);
5066 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5067 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5068 as1 = declspecs->address_space;
5069 as2 = TYPE_ADDR_SPACE (element_type);
5070 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5072 if (pedantic && !flag_isoc99)
5074 if (constp > 1)
5075 pedwarn (loc, OPT_Wpedantic, "duplicate %<const%>");
5076 if (restrictp > 1)
5077 pedwarn (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5078 if (volatilep > 1)
5079 pedwarn (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5082 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5083 error_at (loc, "conflicting named address spaces (%s vs %s)",
5084 c_addr_space_name (as1), c_addr_space_name (as2));
5086 if ((TREE_CODE (type) == ARRAY_TYPE
5087 || first_non_attr_kind == cdk_array)
5088 && TYPE_QUALS (element_type))
5089 type = TYPE_MAIN_VARIANT (type);
5090 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5091 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5092 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5093 | ENCODE_QUAL_ADDR_SPACE (address_space));
5095 /* Warn about storage classes that are invalid for certain
5096 kinds of declarations (parameters, typenames, etc.). */
5098 if (funcdef_flag
5099 && (threadp
5100 || storage_class == csc_auto
5101 || storage_class == csc_register
5102 || storage_class == csc_typedef))
5104 if (storage_class == csc_auto)
5105 pedwarn (loc,
5106 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5107 "function definition declared %<auto%>");
5108 if (storage_class == csc_register)
5109 error_at (loc, "function definition declared %<register%>");
5110 if (storage_class == csc_typedef)
5111 error_at (loc, "function definition declared %<typedef%>");
5112 if (threadp)
5113 error_at (loc, "function definition declared %<__thread%>");
5114 threadp = false;
5115 if (storage_class == csc_auto
5116 || storage_class == csc_register
5117 || storage_class == csc_typedef)
5118 storage_class = csc_none;
5120 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5122 if (decl_context == PARM && storage_class == csc_register)
5124 else
5126 switch (decl_context)
5128 case FIELD:
5129 if (name)
5130 error_at (loc, "storage class specified for structure "
5131 "field %qE", name);
5132 else
5133 error_at (loc, "storage class specified for structure field");
5134 break;
5135 case PARM:
5136 if (name)
5137 error_at (loc, "storage class specified for parameter %qE",
5138 name);
5139 else
5140 error_at (loc, "storage class specified for unnamed parameter");
5141 break;
5142 default:
5143 error_at (loc, "storage class specified for typename");
5144 break;
5146 storage_class = csc_none;
5147 threadp = false;
5150 else if (storage_class == csc_extern
5151 && initialized
5152 && !funcdef_flag)
5154 /* 'extern' with initialization is invalid if not at file scope. */
5155 if (current_scope == file_scope)
5157 /* It is fine to have 'extern const' when compiling at C
5158 and C++ intersection. */
5159 if (!(warn_cxx_compat && constp))
5160 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5161 name);
5163 else
5164 error_at (loc, "%qE has both %<extern%> and initializer", name);
5166 else if (current_scope == file_scope)
5168 if (storage_class == csc_auto)
5169 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5170 name);
5171 if (pedantic && storage_class == csc_register)
5172 pedwarn (input_location, OPT_Wpedantic,
5173 "file-scope declaration of %qE specifies %<register%>", name);
5175 else
5177 if (storage_class == csc_extern && funcdef_flag)
5178 error_at (loc, "nested function %qE declared %<extern%>", name);
5179 else if (threadp && storage_class == csc_none)
5181 error_at (loc, "function-scope %qE implicitly auto and declared "
5182 "%<__thread%>",
5183 name);
5184 threadp = false;
5188 /* Now figure out the structure of the declarator proper.
5189 Descend through it, creating more complex types, until we reach
5190 the declared identifier (or NULL_TREE, in an absolute declarator).
5191 At each stage we maintain an unqualified version of the type
5192 together with any qualifiers that should be applied to it with
5193 c_build_qualified_type; this way, array types including
5194 multidimensional array types are first built up in unqualified
5195 form and then the qualified form is created with
5196 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5198 while (declarator && declarator->kind != cdk_id)
5200 if (type == error_mark_node)
5202 declarator = declarator->declarator;
5203 continue;
5206 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5207 a cdk_pointer (for *...),
5208 a cdk_function (for ...(...)),
5209 a cdk_attrs (for nested attributes),
5210 or a cdk_id (for the name being declared
5211 or the place in an absolute declarator
5212 where the name was omitted).
5213 For the last case, we have just exited the loop.
5215 At this point, TYPE is the type of elements of an array,
5216 or for a function to return, or for a pointer to point to.
5217 After this sequence of ifs, TYPE is the type of the
5218 array or function or pointer, and DECLARATOR has had its
5219 outermost layer removed. */
5221 if (array_ptr_quals != TYPE_UNQUALIFIED
5222 || array_ptr_attrs != NULL_TREE
5223 || array_parm_static)
5225 /* Only the innermost declarator (making a parameter be of
5226 array type which is converted to pointer type)
5227 may have static or type qualifiers. */
5228 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5229 array_ptr_quals = TYPE_UNQUALIFIED;
5230 array_ptr_attrs = NULL_TREE;
5231 array_parm_static = 0;
5234 switch (declarator->kind)
5236 case cdk_attrs:
5238 /* A declarator with embedded attributes. */
5239 tree attrs = declarator->u.attrs;
5240 const struct c_declarator *inner_decl;
5241 int attr_flags = 0;
5242 declarator = declarator->declarator;
5243 inner_decl = declarator;
5244 while (inner_decl->kind == cdk_attrs)
5245 inner_decl = inner_decl->declarator;
5246 if (inner_decl->kind == cdk_id)
5247 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5248 else if (inner_decl->kind == cdk_function)
5249 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5250 else if (inner_decl->kind == cdk_array)
5251 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5252 returned_attrs = decl_attributes (&type,
5253 chainon (returned_attrs, attrs),
5254 attr_flags);
5255 break;
5257 case cdk_array:
5259 tree itype = NULL_TREE;
5260 tree size = declarator->u.array.dimen;
5261 /* The index is a signed object `sizetype' bits wide. */
5262 tree index_type = c_common_signed_type (sizetype);
5264 array_ptr_quals = declarator->u.array.quals;
5265 array_ptr_attrs = declarator->u.array.attrs;
5266 array_parm_static = declarator->u.array.static_p;
5267 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5269 declarator = declarator->declarator;
5271 /* Check for some types that there cannot be arrays of. */
5273 if (VOID_TYPE_P (type))
5275 if (name)
5276 error_at (loc, "declaration of %qE as array of voids", name);
5277 else
5278 error_at (loc, "declaration of type name as array of voids");
5279 type = error_mark_node;
5282 if (TREE_CODE (type) == FUNCTION_TYPE)
5284 if (name)
5285 error_at (loc, "declaration of %qE as array of functions",
5286 name);
5287 else
5288 error_at (loc, "declaration of type name as array of "
5289 "functions");
5290 type = error_mark_node;
5293 if (pedantic && !in_system_header && flexible_array_type_p (type))
5294 pedwarn (loc, OPT_Wpedantic,
5295 "invalid use of structure with flexible array member");
5297 if (size == error_mark_node)
5298 type = error_mark_node;
5300 if (type == error_mark_node)
5301 continue;
5303 /* If size was specified, set ITYPE to a range-type for
5304 that size. Otherwise, ITYPE remains null. finish_decl
5305 may figure it out from an initial value. */
5307 if (size)
5309 bool size_maybe_const = true;
5310 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5311 && !TREE_OVERFLOW (size));
5312 bool this_size_varies = false;
5314 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5315 lvalue. */
5316 STRIP_TYPE_NOPS (size);
5318 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5320 if (name)
5321 error_at (loc, "size of array %qE has non-integer type",
5322 name);
5323 else
5324 error_at (loc,
5325 "size of unnamed array has non-integer type");
5326 size = integer_one_node;
5329 size = c_fully_fold (size, false, &size_maybe_const);
5331 if (pedantic && size_maybe_const && integer_zerop (size))
5333 if (name)
5334 pedwarn (loc, OPT_Wpedantic,
5335 "ISO C forbids zero-size array %qE", name);
5336 else
5337 pedwarn (loc, OPT_Wpedantic,
5338 "ISO C forbids zero-size array");
5341 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5343 constant_expression_warning (size);
5344 if (tree_int_cst_sgn (size) < 0)
5346 if (name)
5347 error_at (loc, "size of array %qE is negative", name);
5348 else
5349 error_at (loc, "size of unnamed array is negative");
5350 size = integer_one_node;
5352 /* Handle a size folded to an integer constant but
5353 not an integer constant expression. */
5354 if (!size_int_const)
5356 /* If this is a file scope declaration of an
5357 ordinary identifier, this is invalid code;
5358 diagnosing it here and not subsequently
5359 treating the type as variable-length avoids
5360 more confusing diagnostics later. */
5361 if ((decl_context == NORMAL || decl_context == FIELD)
5362 && current_scope == file_scope)
5363 pedwarn (input_location, 0,
5364 "variably modified %qE at file scope",
5365 name);
5366 else
5367 this_size_varies = size_varies = true;
5368 warn_variable_length_array (name, size);
5371 else if ((decl_context == NORMAL || decl_context == FIELD)
5372 && current_scope == file_scope)
5374 error_at (loc, "variably modified %qE at file scope", name);
5375 size = integer_one_node;
5377 else
5379 /* Make sure the array size remains visibly
5380 nonconstant even if it is (eg) a const variable
5381 with known value. */
5382 this_size_varies = size_varies = true;
5383 warn_variable_length_array (name, size);
5386 if (integer_zerop (size) && !this_size_varies)
5388 /* A zero-length array cannot be represented with
5389 an unsigned index type, which is what we'll
5390 get with build_index_type. Create an
5391 open-ended range instead. */
5392 itype = build_range_type (sizetype, size, NULL_TREE);
5394 else
5396 /* Arrange for the SAVE_EXPR on the inside of the
5397 MINUS_EXPR, which allows the -1 to get folded
5398 with the +1 that happens when building TYPE_SIZE. */
5399 if (size_varies)
5400 size = save_expr (size);
5401 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5402 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5403 integer_zero_node, size);
5405 /* Compute the maximum valid index, that is, size
5406 - 1. Do the calculation in index_type, so that
5407 if it is a variable the computations will be
5408 done in the proper mode. */
5409 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5410 convert (index_type, size),
5411 convert (index_type,
5412 size_one_node));
5414 /* The above overflows when size does not fit
5415 in index_type.
5416 ??? While a size of INT_MAX+1 technically shouldn't
5417 cause an overflow (because we subtract 1), handling
5418 this case seems like an unnecessary complication. */
5419 if (TREE_CODE (size) == INTEGER_CST
5420 && !int_fits_type_p (size, index_type))
5422 if (name)
5423 error_at (loc, "size of array %qE is too large",
5424 name);
5425 else
5426 error_at (loc, "size of unnamed array is too large");
5427 type = error_mark_node;
5428 continue;
5431 itype = build_index_type (itype);
5433 if (this_size_varies)
5435 if (*expr)
5436 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5437 *expr, size);
5438 else
5439 *expr = size;
5440 *expr_const_operands &= size_maybe_const;
5443 else if (decl_context == FIELD)
5445 bool flexible_array_member = false;
5446 if (array_parm_vla_unspec_p)
5447 /* Field names can in fact have function prototype
5448 scope so [*] is disallowed here through making
5449 the field variably modified, not through being
5450 something other than a declaration with function
5451 prototype scope. */
5452 size_varies = true;
5453 else
5455 const struct c_declarator *t = declarator;
5456 while (t->kind == cdk_attrs)
5457 t = t->declarator;
5458 flexible_array_member = (t->kind == cdk_id);
5460 if (flexible_array_member
5461 && pedantic && !flag_isoc99 && !in_system_header)
5462 pedwarn (loc, OPT_Wpedantic,
5463 "ISO C90 does not support flexible array members");
5465 /* ISO C99 Flexible array members are effectively
5466 identical to GCC's zero-length array extension. */
5467 if (flexible_array_member || array_parm_vla_unspec_p)
5468 itype = build_range_type (sizetype, size_zero_node,
5469 NULL_TREE);
5471 else if (decl_context == PARM)
5473 if (array_parm_vla_unspec_p)
5475 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
5476 size_varies = true;
5479 else if (decl_context == TYPENAME)
5481 if (array_parm_vla_unspec_p)
5483 /* C99 6.7.5.2p4 */
5484 warning (0, "%<[*]%> not in a declaration");
5485 /* We use this to avoid messing up with incomplete
5486 array types of the same type, that would
5487 otherwise be modified below. */
5488 itype = build_range_type (sizetype, size_zero_node,
5489 NULL_TREE);
5490 size_varies = true;
5494 /* Complain about arrays of incomplete types. */
5495 if (!COMPLETE_TYPE_P (type))
5497 error_at (loc, "array type has incomplete element type");
5498 type = error_mark_node;
5500 else
5501 /* When itype is NULL, a shared incomplete array type is
5502 returned for all array of a given type. Elsewhere we
5503 make sure we don't complete that type before copying
5504 it, but here we want to make sure we don't ever
5505 modify the shared type, so we gcc_assert (itype)
5506 below. */
5508 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
5509 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
5510 type = build_qualified_type (type,
5511 ENCODE_QUAL_ADDR_SPACE (as));
5513 type = build_array_type (type, itype);
5516 if (type != error_mark_node)
5518 if (size_varies)
5520 /* It is ok to modify type here even if itype is
5521 NULL: if size_varies, we're in a
5522 multi-dimensional array and the inner type has
5523 variable size, so the enclosing shared array type
5524 must too. */
5525 if (size && TREE_CODE (size) == INTEGER_CST)
5526 type
5527 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5528 C_TYPE_VARIABLE_SIZE (type) = 1;
5531 /* The GCC extension for zero-length arrays differs from
5532 ISO flexible array members in that sizeof yields
5533 zero. */
5534 if (size && integer_zerop (size))
5536 gcc_assert (itype);
5537 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5538 TYPE_SIZE (type) = bitsize_zero_node;
5539 TYPE_SIZE_UNIT (type) = size_zero_node;
5540 SET_TYPE_STRUCTURAL_EQUALITY (type);
5542 if (array_parm_vla_unspec_p)
5544 gcc_assert (itype);
5545 /* The type is complete. C99 6.7.5.2p4 */
5546 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5547 TYPE_SIZE (type) = bitsize_zero_node;
5548 TYPE_SIZE_UNIT (type) = size_zero_node;
5549 SET_TYPE_STRUCTURAL_EQUALITY (type);
5553 if (decl_context != PARM
5554 && (array_ptr_quals != TYPE_UNQUALIFIED
5555 || array_ptr_attrs != NULL_TREE
5556 || array_parm_static))
5558 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5559 array_ptr_quals = TYPE_UNQUALIFIED;
5560 array_ptr_attrs = NULL_TREE;
5561 array_parm_static = 0;
5563 break;
5565 case cdk_function:
5567 /* Say it's a definition only for the declarator closest
5568 to the identifier, apart possibly from some
5569 attributes. */
5570 bool really_funcdef = false;
5571 tree arg_types;
5572 if (funcdef_flag)
5574 const struct c_declarator *t = declarator->declarator;
5575 while (t->kind == cdk_attrs)
5576 t = t->declarator;
5577 really_funcdef = (t->kind == cdk_id);
5580 /* Declaring a function type. Make sure we have a valid
5581 type for the function to return. */
5582 if (type == error_mark_node)
5583 continue;
5585 size_varies = false;
5587 /* Warn about some types functions can't return. */
5588 if (TREE_CODE (type) == FUNCTION_TYPE)
5590 if (name)
5591 error_at (loc, "%qE declared as function returning a "
5592 "function", name);
5593 else
5594 error_at (loc, "type name declared as function "
5595 "returning a function");
5596 type = integer_type_node;
5598 if (TREE_CODE (type) == ARRAY_TYPE)
5600 if (name)
5601 error_at (loc, "%qE declared as function returning an array",
5602 name);
5603 else
5604 error_at (loc, "type name declared as function returning "
5605 "an array");
5606 type = integer_type_node;
5608 errmsg = targetm.invalid_return_type (type);
5609 if (errmsg)
5611 error (errmsg);
5612 type = integer_type_node;
5615 /* Construct the function type and go to the next
5616 inner layer of declarator. */
5617 arg_info = declarator->u.arg_info;
5618 arg_types = grokparms (arg_info, really_funcdef);
5620 /* Type qualifiers before the return type of the function
5621 qualify the return type, not the function type. */
5622 if (type_quals)
5624 /* Type qualifiers on a function return type are
5625 normally permitted by the standard but have no
5626 effect, so give a warning at -Wreturn-type.
5627 Qualifiers on a void return type are banned on
5628 function definitions in ISO C; GCC used to used
5629 them for noreturn functions. */
5630 if (VOID_TYPE_P (type) && really_funcdef)
5631 pedwarn (loc, 0,
5632 "function definition has qualified void return type");
5633 else
5634 warning_at (loc, OPT_Wignored_qualifiers,
5635 "type qualifiers ignored on function return type");
5637 type = c_build_qualified_type (type, type_quals);
5639 type_quals = TYPE_UNQUALIFIED;
5641 type = build_function_type (type, arg_types);
5642 declarator = declarator->declarator;
5644 /* Set the TYPE_CONTEXTs for each tagged type which is local to
5645 the formal parameter list of this FUNCTION_TYPE to point to
5646 the FUNCTION_TYPE node itself. */
5648 c_arg_tag *tag;
5649 unsigned ix;
5651 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
5652 TYPE_CONTEXT (tag->type) = type;
5654 break;
5656 case cdk_pointer:
5658 /* Merge any constancy or volatility into the target type
5659 for the pointer. */
5661 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5662 && type_quals)
5663 pedwarn (loc, OPT_Wpedantic,
5664 "ISO C forbids qualified function types");
5665 if (type_quals)
5666 type = c_build_qualified_type (type, type_quals);
5667 size_varies = false;
5669 /* When the pointed-to type involves components of variable size,
5670 care must be taken to ensure that the size evaluation code is
5671 emitted early enough to dominate all the possible later uses
5672 and late enough for the variables on which it depends to have
5673 been assigned.
5675 This is expected to happen automatically when the pointed-to
5676 type has a name/declaration of it's own, but special attention
5677 is required if the type is anonymous.
5679 We handle the NORMAL and FIELD contexts here by attaching an
5680 artificial TYPE_DECL to such pointed-to type. This forces the
5681 sizes evaluation at a safe point and ensures it is not deferred
5682 until e.g. within a deeper conditional context.
5684 We expect nothing to be needed here for PARM or TYPENAME.
5685 Pushing a TYPE_DECL at this point for TYPENAME would actually
5686 be incorrect, as we might be in the middle of an expression
5687 with side effects on the pointed-to type size "arguments" prior
5688 to the pointer declaration point and the fake TYPE_DECL in the
5689 enclosing context would force the size evaluation prior to the
5690 side effects. */
5692 if (!TYPE_NAME (type)
5693 && (decl_context == NORMAL || decl_context == FIELD)
5694 && variably_modified_type_p (type, NULL_TREE))
5696 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
5697 DECL_ARTIFICIAL (decl) = 1;
5698 pushdecl (decl);
5699 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
5700 TYPE_NAME (type) = decl;
5703 type = c_build_pointer_type (type);
5705 /* Process type qualifiers (such as const or volatile)
5706 that were given inside the `*'. */
5707 type_quals = declarator->u.pointer_quals;
5709 declarator = declarator->declarator;
5710 break;
5712 default:
5713 gcc_unreachable ();
5716 *decl_attrs = chainon (returned_attrs, *decl_attrs);
5718 /* Now TYPE has the actual type, apart from any qualifiers in
5719 TYPE_QUALS. */
5721 /* Warn about address space used for things other than static memory or
5722 pointers. */
5723 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
5724 if (!ADDR_SPACE_GENERIC_P (address_space))
5726 if (decl_context == NORMAL)
5728 switch (storage_class)
5730 case csc_auto:
5731 error ("%qs combined with %<auto%> qualifier for %qE",
5732 c_addr_space_name (address_space), name);
5733 break;
5734 case csc_register:
5735 error ("%qs combined with %<register%> qualifier for %qE",
5736 c_addr_space_name (address_space), name);
5737 break;
5738 case csc_none:
5739 if (current_function_scope)
5741 error ("%qs specified for auto variable %qE",
5742 c_addr_space_name (address_space), name);
5743 break;
5745 break;
5746 case csc_static:
5747 case csc_extern:
5748 case csc_typedef:
5749 break;
5750 default:
5751 gcc_unreachable ();
5754 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
5756 if (name)
5757 error ("%qs specified for parameter %qE",
5758 c_addr_space_name (address_space), name);
5759 else
5760 error ("%qs specified for unnamed parameter",
5761 c_addr_space_name (address_space));
5763 else if (decl_context == FIELD)
5765 if (name)
5766 error ("%qs specified for structure field %qE",
5767 c_addr_space_name (address_space), name);
5768 else
5769 error ("%qs specified for structure field",
5770 c_addr_space_name (address_space));
5774 /* Check the type and width of a bit-field. */
5775 if (bitfield)
5776 check_bitfield_type_and_width (&type, width, name);
5778 /* Reject invalid uses of _Alignas. */
5779 if (declspecs->alignas_p)
5781 if (storage_class == csc_typedef)
5782 error_at (loc, "alignment specified for typedef %qE", name);
5783 else if (storage_class == csc_register)
5784 error_at (loc, "alignment specified for %<register%> object %qE",
5785 name);
5786 else if (decl_context == PARM)
5788 if (name)
5789 error_at (loc, "alignment specified for parameter %qE", name);
5790 else
5791 error_at (loc, "alignment specified for unnamed parameter");
5793 else if (bitfield)
5795 if (name)
5796 error_at (loc, "alignment specified for bit-field %qE", name);
5797 else
5798 error_at (loc, "alignment specified for unnamed bit-field");
5800 else if (TREE_CODE (type) == FUNCTION_TYPE)
5801 error_at (loc, "alignment specified for function %qE", name);
5802 else if (declspecs->align_log != -1)
5804 alignas_align = 1U << declspecs->align_log;
5805 if (alignas_align < TYPE_ALIGN_UNIT (type))
5807 if (name)
5808 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
5809 "alignment of %qE", name);
5810 else
5811 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
5812 "alignment of unnamed field");
5813 alignas_align = 0;
5818 /* Did array size calculations overflow or does the array cover more
5819 than half of the address-space? */
5820 if (TREE_CODE (type) == ARRAY_TYPE
5821 && COMPLETE_TYPE_P (type)
5822 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
5823 && ! valid_constant_size_p (TYPE_SIZE_UNIT (type)))
5825 if (name)
5826 error_at (loc, "size of array %qE is too large", name);
5827 else
5828 error_at (loc, "size of unnamed array is too large");
5829 /* If we proceed with the array type as it is, we'll eventually
5830 crash in tree_low_cst(). */
5831 type = error_mark_node;
5834 /* If this is declaring a typedef name, return a TYPE_DECL. */
5836 if (storage_class == csc_typedef)
5838 tree decl;
5839 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5840 && type_quals)
5841 pedwarn (loc, OPT_Wpedantic,
5842 "ISO C forbids qualified function types");
5843 if (type_quals)
5844 type = c_build_qualified_type (type, type_quals);
5845 decl = build_decl (declarator->id_loc,
5846 TYPE_DECL, declarator->u.id, type);
5847 if (declspecs->explicit_signed_p)
5848 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
5849 if (declspecs->inline_p)
5850 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
5851 if (declspecs->noreturn_p)
5852 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
5854 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
5856 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
5858 if (b != NULL
5859 && b->decl != NULL_TREE
5860 && (B_IN_CURRENT_SCOPE (b)
5861 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
5862 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
5864 warning_at (declarator->id_loc, OPT_Wc___compat,
5865 ("using %qD as both a typedef and a tag is "
5866 "invalid in C++"),
5867 decl);
5868 if (b->locus != UNKNOWN_LOCATION)
5869 inform (b->locus, "originally defined here");
5873 return decl;
5876 /* If this is a type name (such as, in a cast or sizeof),
5877 compute the type and return it now. */
5879 if (decl_context == TYPENAME)
5881 /* Note that the grammar rejects storage classes in typenames
5882 and fields. */
5883 gcc_assert (storage_class == csc_none && !threadp
5884 && !declspecs->inline_p && !declspecs->noreturn_p);
5885 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5886 && type_quals)
5887 pedwarn (loc, OPT_Wpedantic,
5888 "ISO C forbids const or volatile function types");
5889 if (type_quals)
5890 type = c_build_qualified_type (type, type_quals);
5891 return type;
5894 if (pedantic && decl_context == FIELD
5895 && variably_modified_type_p (type, NULL_TREE))
5897 /* C99 6.7.2.1p8 */
5898 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
5899 "have a variably modified type");
5902 /* Aside from typedefs and type names (handle above),
5903 `void' at top level (not within pointer)
5904 is allowed only in public variables.
5905 We don't complain about parms either, but that is because
5906 a better error message can be made later. */
5908 if (VOID_TYPE_P (type) && decl_context != PARM
5909 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
5910 && (storage_class == csc_extern
5911 || (current_scope == file_scope
5912 && !(storage_class == csc_static
5913 || storage_class == csc_register)))))
5915 error_at (loc, "variable or field %qE declared void", name);
5916 type = integer_type_node;
5919 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
5920 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
5923 tree decl;
5925 if (decl_context == PARM)
5927 tree promoted_type;
5929 /* A parameter declared as an array of T is really a pointer to T.
5930 One declared as a function is really a pointer to a function. */
5932 if (TREE_CODE (type) == ARRAY_TYPE)
5934 /* Transfer const-ness of array into that of type pointed to. */
5935 type = TREE_TYPE (type);
5936 if (type_quals)
5937 type = c_build_qualified_type (type, type_quals);
5938 type = c_build_pointer_type (type);
5939 type_quals = array_ptr_quals;
5940 if (type_quals)
5941 type = c_build_qualified_type (type, type_quals);
5943 /* We don't yet implement attributes in this context. */
5944 if (array_ptr_attrs != NULL_TREE)
5945 warning_at (loc, OPT_Wattributes,
5946 "attributes in parameter array declarator ignored");
5948 size_varies = false;
5950 else if (TREE_CODE (type) == FUNCTION_TYPE)
5952 if (type_quals)
5953 pedwarn (loc, OPT_Wpedantic,
5954 "ISO C forbids qualified function types");
5955 if (type_quals)
5956 type = c_build_qualified_type (type, type_quals);
5957 type = c_build_pointer_type (type);
5958 type_quals = TYPE_UNQUALIFIED;
5960 else if (type_quals)
5961 type = c_build_qualified_type (type, type_quals);
5963 decl = build_decl (declarator->id_loc,
5964 PARM_DECL, declarator->u.id, type);
5965 if (size_varies)
5966 C_DECL_VARIABLE_SIZE (decl) = 1;
5968 /* Compute the type actually passed in the parmlist,
5969 for the case where there is no prototype.
5970 (For example, shorts and chars are passed as ints.)
5971 When there is a prototype, this is overridden later. */
5973 if (type == error_mark_node)
5974 promoted_type = type;
5975 else
5976 promoted_type = c_type_promotes_to (type);
5978 DECL_ARG_TYPE (decl) = promoted_type;
5979 if (declspecs->inline_p)
5980 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
5981 if (declspecs->noreturn_p)
5982 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
5984 else if (decl_context == FIELD)
5986 /* Note that the grammar rejects storage classes in typenames
5987 and fields. */
5988 gcc_assert (storage_class == csc_none && !threadp
5989 && !declspecs->inline_p && !declspecs->noreturn_p);
5991 /* Structure field. It may not be a function. */
5993 if (TREE_CODE (type) == FUNCTION_TYPE)
5995 error_at (loc, "field %qE declared as a function", name);
5996 type = build_pointer_type (type);
5998 else if (TREE_CODE (type) != ERROR_MARK
5999 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6001 if (name)
6002 error_at (loc, "field %qE has incomplete type", name);
6003 else
6004 error_at (loc, "unnamed field has incomplete type");
6005 type = error_mark_node;
6007 type = c_build_qualified_type (type, type_quals);
6008 decl = build_decl (declarator->id_loc,
6009 FIELD_DECL, declarator->u.id, type);
6010 DECL_NONADDRESSABLE_P (decl) = bitfield;
6011 if (bitfield && !declarator->u.id)
6012 TREE_NO_WARNING (decl) = 1;
6014 if (size_varies)
6015 C_DECL_VARIABLE_SIZE (decl) = 1;
6017 else if (TREE_CODE (type) == FUNCTION_TYPE)
6019 if (storage_class == csc_register || threadp)
6021 error_at (loc, "invalid storage class for function %qE", name);
6023 else if (current_scope != file_scope)
6025 /* Function declaration not at file scope. Storage
6026 classes other than `extern' are not allowed, C99
6027 6.7.1p5, and `extern' makes no difference. However,
6028 GCC allows 'auto', perhaps with 'inline', to support
6029 nested functions. */
6030 if (storage_class == csc_auto)
6031 pedwarn (loc, OPT_Wpedantic,
6032 "invalid storage class for function %qE", name);
6033 else if (storage_class == csc_static)
6035 error_at (loc, "invalid storage class for function %qE", name);
6036 if (funcdef_flag)
6037 storage_class = declspecs->storage_class = csc_none;
6038 else
6039 return 0;
6043 decl = build_decl (declarator->id_loc,
6044 FUNCTION_DECL, declarator->u.id, type);
6045 decl = build_decl_attribute_variant (decl, decl_attr);
6047 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6048 pedwarn (loc, OPT_Wpedantic,
6049 "ISO C forbids qualified function types");
6051 /* Every function declaration is an external reference
6052 (DECL_EXTERNAL) except for those which are not at file
6053 scope and are explicitly declared "auto". This is
6054 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6055 GCC to signify a forward declaration of a nested function. */
6056 if (storage_class == csc_auto && current_scope != file_scope)
6057 DECL_EXTERNAL (decl) = 0;
6058 /* In C99, a function which is declared 'inline' with 'extern'
6059 is not an external reference (which is confusing). It
6060 means that the later definition of the function must be output
6061 in this file, C99 6.7.4p6. In GNU C89, a function declared
6062 'extern inline' is an external reference. */
6063 else if (declspecs->inline_p && storage_class != csc_static)
6064 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6065 == flag_gnu89_inline);
6066 else
6067 DECL_EXTERNAL (decl) = !initialized;
6069 /* Record absence of global scope for `static' or `auto'. */
6070 TREE_PUBLIC (decl)
6071 = !(storage_class == csc_static || storage_class == csc_auto);
6073 /* For a function definition, record the argument information
6074 block where store_parm_decls will look for it. */
6075 if (funcdef_flag)
6076 current_function_arg_info = arg_info;
6078 if (declspecs->default_int_p)
6079 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6081 /* Record presence of `inline' and `_Noreturn', if it is
6082 reasonable. */
6083 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6085 if (declspecs->inline_p)
6086 pedwarn (loc, 0, "cannot inline function %<main%>");
6087 if (declspecs->noreturn_p)
6088 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6090 else
6092 if (declspecs->inline_p)
6093 /* Record that the function is declared `inline'. */
6094 DECL_DECLARED_INLINE_P (decl) = 1;
6095 if (declspecs->noreturn_p)
6097 if (!flag_isoc11)
6099 if (flag_isoc99)
6100 pedwarn (loc, OPT_Wpedantic,
6101 "ISO C99 does not support %<_Noreturn%>");
6102 else
6103 pedwarn (loc, OPT_Wpedantic,
6104 "ISO C90 does not support %<_Noreturn%>");
6106 TREE_THIS_VOLATILE (decl) = 1;
6110 else
6112 /* It's a variable. */
6113 /* An uninitialized decl with `extern' is a reference. */
6114 int extern_ref = !initialized && storage_class == csc_extern;
6116 type = c_build_qualified_type (type, type_quals);
6118 /* C99 6.2.2p7: It is invalid (compile-time undefined
6119 behavior) to create an 'extern' declaration for a
6120 variable if there is a global declaration that is
6121 'static' and the global declaration is not visible.
6122 (If the static declaration _is_ currently visible,
6123 the 'extern' declaration is taken to refer to that decl.) */
6124 if (extern_ref && current_scope != file_scope)
6126 tree global_decl = identifier_global_value (declarator->u.id);
6127 tree visible_decl = lookup_name (declarator->u.id);
6129 if (global_decl
6130 && global_decl != visible_decl
6131 && TREE_CODE (global_decl) == VAR_DECL
6132 && !TREE_PUBLIC (global_decl))
6133 error_at (loc, "variable previously declared %<static%> "
6134 "redeclared %<extern%>");
6137 decl = build_decl (declarator->id_loc,
6138 VAR_DECL, declarator->u.id, type);
6139 if (size_varies)
6140 C_DECL_VARIABLE_SIZE (decl) = 1;
6142 if (declspecs->inline_p)
6143 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6144 if (declspecs->noreturn_p)
6145 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6147 /* At file scope, an initialized extern declaration may follow
6148 a static declaration. In that case, DECL_EXTERNAL will be
6149 reset later in start_decl. */
6150 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6152 /* At file scope, the presence of a `static' or `register' storage
6153 class specifier, or the absence of all storage class specifiers
6154 makes this declaration a definition (perhaps tentative). Also,
6155 the absence of `static' makes it public. */
6156 if (current_scope == file_scope)
6158 TREE_PUBLIC (decl) = storage_class != csc_static;
6159 TREE_STATIC (decl) = !extern_ref;
6161 /* Not at file scope, only `static' makes a static definition. */
6162 else
6164 TREE_STATIC (decl) = (storage_class == csc_static);
6165 TREE_PUBLIC (decl) = extern_ref;
6168 if (threadp)
6169 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
6172 if ((storage_class == csc_extern
6173 || (storage_class == csc_none
6174 && TREE_CODE (type) == FUNCTION_TYPE
6175 && !funcdef_flag))
6176 && variably_modified_type_p (type, NULL_TREE))
6178 /* C99 6.7.5.2p2 */
6179 if (TREE_CODE (type) == FUNCTION_TYPE)
6180 error_at (loc, "non-nested function with variably modified type");
6181 else
6182 error_at (loc, "object with variably modified type must have "
6183 "no linkage");
6186 /* Record `register' declaration for warnings on &
6187 and in case doing stupid register allocation. */
6189 if (storage_class == csc_register)
6191 C_DECL_REGISTER (decl) = 1;
6192 DECL_REGISTER (decl) = 1;
6195 /* Record constancy and volatility. */
6196 c_apply_type_quals_to_decl (type_quals, decl);
6198 /* Apply _Alignas specifiers. */
6199 if (alignas_align)
6201 DECL_ALIGN (decl) = alignas_align * BITS_PER_UNIT;
6202 DECL_USER_ALIGN (decl) = 1;
6205 /* If a type has volatile components, it should be stored in memory.
6206 Otherwise, the fact that those components are volatile
6207 will be ignored, and would even crash the compiler.
6208 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6209 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6210 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
6211 || TREE_CODE (decl) == RESULT_DECL))
6213 /* It is not an error for a structure with volatile fields to
6214 be declared register, but reset DECL_REGISTER since it
6215 cannot actually go in a register. */
6216 int was_reg = C_DECL_REGISTER (decl);
6217 C_DECL_REGISTER (decl) = 0;
6218 DECL_REGISTER (decl) = 0;
6219 c_mark_addressable (decl);
6220 C_DECL_REGISTER (decl) = was_reg;
6223 /* This is the earliest point at which we might know the assembler
6224 name of a variable. Thus, if it's known before this, die horribly. */
6225 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6227 if (warn_cxx_compat
6228 && TREE_CODE (decl) == VAR_DECL
6229 && TREE_PUBLIC (decl)
6230 && TREE_STATIC (decl)
6231 && (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6232 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6233 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6234 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6235 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6236 ("non-local variable %qD with anonymous type is "
6237 "questionable in C++"),
6238 decl);
6240 return decl;
6244 /* Decode the parameter-list info for a function type or function definition.
6245 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6246 if there is an identifier list instead of a parameter decl list).
6247 These two functions are separate because when a function returns
6248 or receives functions then each is called multiple times but the order
6249 of calls is different. The last call to `grokparms' is always the one
6250 that contains the formal parameter names of a function definition.
6252 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6254 FUNCDEF_FLAG is true for a function definition, false for
6255 a mere declaration. A nonempty identifier-list gets an error message
6256 when FUNCDEF_FLAG is false. */
6258 static tree
6259 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6261 tree arg_types = arg_info->types;
6263 if (funcdef_flag && arg_info->had_vla_unspec)
6265 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6266 /* C99 6.7.5.2p4 */
6267 error ("%<[*]%> not allowed in other than function prototype scope");
6270 if (arg_types == 0 && !funcdef_flag && !in_system_header)
6271 warning (OPT_Wstrict_prototypes,
6272 "function declaration isn%'t a prototype");
6274 if (arg_types == error_mark_node)
6275 return 0; /* don't set TYPE_ARG_TYPES in this case */
6277 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6279 if (!funcdef_flag)
6281 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6282 arg_info->parms = NULL_TREE;
6284 else
6285 arg_info->parms = arg_info->types;
6287 arg_info->types = 0;
6288 return 0;
6290 else
6292 tree parm, type, typelt;
6293 unsigned int parmno;
6294 const char *errmsg;
6296 /* If there is a parameter of incomplete type in a definition,
6297 this is an error. In a declaration this is valid, and a
6298 struct or union type may be completed later, before any calls
6299 or definition of the function. In the case where the tag was
6300 first declared within the parameter list, a warning has
6301 already been given. If a parameter has void type, then
6302 however the function cannot be defined or called, so
6303 warn. */
6305 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
6306 parm;
6307 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
6309 type = TREE_VALUE (typelt);
6310 if (type == error_mark_node)
6311 continue;
6313 if (!COMPLETE_TYPE_P (type))
6315 if (funcdef_flag)
6317 if (DECL_NAME (parm))
6318 error_at (input_location,
6319 "parameter %u (%q+D) has incomplete type",
6320 parmno, parm);
6321 else
6322 error_at (DECL_SOURCE_LOCATION (parm),
6323 "parameter %u has incomplete type",
6324 parmno);
6326 TREE_VALUE (typelt) = error_mark_node;
6327 TREE_TYPE (parm) = error_mark_node;
6328 arg_types = NULL_TREE;
6330 else if (VOID_TYPE_P (type))
6332 if (DECL_NAME (parm))
6333 warning_at (input_location, 0,
6334 "parameter %u (%q+D) has void type",
6335 parmno, parm);
6336 else
6337 warning_at (DECL_SOURCE_LOCATION (parm), 0,
6338 "parameter %u has void type",
6339 parmno);
6343 errmsg = targetm.invalid_parameter_type (type);
6344 if (errmsg)
6346 error (errmsg);
6347 TREE_VALUE (typelt) = error_mark_node;
6348 TREE_TYPE (parm) = error_mark_node;
6349 arg_types = NULL_TREE;
6352 if (DECL_NAME (parm) && TREE_USED (parm))
6353 warn_if_shadowing (parm);
6355 return arg_types;
6359 /* Allocate and initialize a c_arg_info structure from the parser's
6360 obstack. */
6362 struct c_arg_info *
6363 build_arg_info (void)
6365 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
6366 ret->parms = NULL_TREE;
6367 ret->tags = NULL;
6368 ret->types = NULL_TREE;
6369 ret->others = NULL_TREE;
6370 ret->pending_sizes = NULL;
6371 ret->had_vla_unspec = 0;
6372 return ret;
6375 /* Take apart the current scope and return a c_arg_info structure with
6376 info on a parameter list just parsed.
6378 This structure is later fed to 'grokparms' and 'store_parm_decls'.
6380 ELLIPSIS being true means the argument list ended in '...' so don't
6381 append a sentinel (void_list_node) to the end of the type-list.
6383 EXPR is NULL or an expression that needs to be evaluated for the
6384 side effects of array size expressions in the parameters. */
6386 struct c_arg_info *
6387 get_parm_info (bool ellipsis, tree expr)
6389 struct c_binding *b = current_scope->bindings;
6390 struct c_arg_info *arg_info = build_arg_info ();
6392 tree parms = 0;
6393 vec<c_arg_tag, va_gc> *tags = NULL;
6394 tree types = 0;
6395 tree others = 0;
6397 static bool explained_incomplete_types = false;
6398 bool gave_void_only_once_err = false;
6400 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
6402 /* The bindings in this scope must not get put into a block.
6403 We will take care of deleting the binding nodes. */
6404 current_scope->bindings = 0;
6406 /* This function is only called if there was *something* on the
6407 parameter list. */
6408 gcc_assert (b);
6410 /* A parameter list consisting solely of 'void' indicates that the
6411 function takes no arguments. But if the 'void' is qualified
6412 (by 'const' or 'volatile'), or has a storage class specifier
6413 ('register'), then the behavior is undefined; issue an error.
6414 Typedefs for 'void' are OK (see DR#157). */
6415 if (b->prev == 0 /* one binding */
6416 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
6417 && !DECL_NAME (b->decl) /* anonymous */
6418 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
6420 if (TREE_THIS_VOLATILE (b->decl)
6421 || TREE_READONLY (b->decl)
6422 || C_DECL_REGISTER (b->decl))
6423 error ("%<void%> as only parameter may not be qualified");
6425 /* There cannot be an ellipsis. */
6426 if (ellipsis)
6427 error ("%<void%> must be the only parameter");
6429 arg_info->types = void_list_node;
6430 return arg_info;
6433 if (!ellipsis)
6434 types = void_list_node;
6436 /* Break up the bindings list into parms, tags, types, and others;
6437 apply sanity checks; purge the name-to-decl bindings. */
6438 while (b)
6440 tree decl = b->decl;
6441 tree type = TREE_TYPE (decl);
6442 c_arg_tag tag;
6443 const char *keyword;
6445 switch (TREE_CODE (decl))
6447 case PARM_DECL:
6448 if (b->id)
6450 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6451 I_SYMBOL_BINDING (b->id) = b->shadowed;
6454 /* Check for forward decls that never got their actual decl. */
6455 if (TREE_ASM_WRITTEN (decl))
6456 error ("parameter %q+D has just a forward declaration", decl);
6457 /* Check for (..., void, ...) and issue an error. */
6458 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
6460 if (!gave_void_only_once_err)
6462 error ("%<void%> must be the only parameter");
6463 gave_void_only_once_err = true;
6466 else
6468 /* Valid parameter, add it to the list. */
6469 DECL_CHAIN (decl) = parms;
6470 parms = decl;
6472 /* Since there is a prototype, args are passed in their
6473 declared types. The back end may override this later. */
6474 DECL_ARG_TYPE (decl) = type;
6475 types = tree_cons (0, type, types);
6477 break;
6479 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
6480 case UNION_TYPE: keyword = "union"; goto tag;
6481 case RECORD_TYPE: keyword = "struct"; goto tag;
6482 tag:
6483 /* Types may not have tag-names, in which case the type
6484 appears in the bindings list with b->id NULL. */
6485 if (b->id)
6487 gcc_assert (I_TAG_BINDING (b->id) == b);
6488 I_TAG_BINDING (b->id) = b->shadowed;
6491 /* Warn about any struct, union or enum tags defined in a
6492 parameter list. The scope of such types is limited to
6493 the parameter list, which is rarely if ever desirable
6494 (it's impossible to call such a function with type-
6495 correct arguments). An anonymous union parm type is
6496 meaningful as a GNU extension, so don't warn for that. */
6497 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
6499 if (b->id)
6500 /* The %s will be one of 'struct', 'union', or 'enum'. */
6501 warning (0, "%<%s %E%> declared inside parameter list",
6502 keyword, b->id);
6503 else
6504 /* The %s will be one of 'struct', 'union', or 'enum'. */
6505 warning (0, "anonymous %s declared inside parameter list",
6506 keyword);
6508 if (!explained_incomplete_types)
6510 warning (0, "its scope is only this definition or declaration,"
6511 " which is probably not what you want");
6512 explained_incomplete_types = true;
6516 tag.id = b->id;
6517 tag.type = decl;
6518 vec_safe_push (tags, tag);
6519 break;
6521 case CONST_DECL:
6522 case TYPE_DECL:
6523 case FUNCTION_DECL:
6524 /* CONST_DECLs appear here when we have an embedded enum,
6525 and TYPE_DECLs appear here when we have an embedded struct
6526 or union. No warnings for this - we already warned about the
6527 type itself. FUNCTION_DECLs appear when there is an implicit
6528 function declaration in the parameter list. */
6530 /* When we reinsert this decl in the function body, we need
6531 to reconstruct whether it was marked as nested. */
6532 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6533 ? b->nested
6534 : !b->nested);
6535 DECL_CHAIN (decl) = others;
6536 others = decl;
6537 /* fall through */
6539 case ERROR_MARK:
6540 /* error_mark_node appears here when we have an undeclared
6541 variable. Just throw it away. */
6542 if (b->id)
6544 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6545 I_SYMBOL_BINDING (b->id) = b->shadowed;
6547 break;
6549 /* Other things that might be encountered. */
6550 case LABEL_DECL:
6551 case VAR_DECL:
6552 default:
6553 gcc_unreachable ();
6556 b = free_binding_and_advance (b);
6559 arg_info->parms = parms;
6560 arg_info->tags = tags;
6561 arg_info->types = types;
6562 arg_info->others = others;
6563 arg_info->pending_sizes = expr;
6564 return arg_info;
6567 /* Get the struct, enum or union (CODE says which) with tag NAME.
6568 Define the tag as a forward-reference with location LOC if it is
6569 not defined. Return a c_typespec structure for the type
6570 specifier. */
6572 struct c_typespec
6573 parser_xref_tag (location_t loc, enum tree_code code, tree name)
6575 struct c_typespec ret;
6576 tree ref;
6577 location_t refloc;
6579 ret.expr = NULL_TREE;
6580 ret.expr_const_operands = true;
6582 /* If a cross reference is requested, look up the type
6583 already defined for this tag and return it. */
6585 ref = lookup_tag (code, name, 0, &refloc);
6586 /* If this is the right type of tag, return what we found.
6587 (This reference will be shadowed by shadow_tag later if appropriate.)
6588 If this is the wrong type of tag, do not return it. If it was the
6589 wrong type in the same scope, we will have had an error
6590 message already; if in a different scope and declaring
6591 a name, pending_xref_error will give an error message; but if in a
6592 different scope and not declaring a name, this tag should
6593 shadow the previous declaration of a different type of tag, and
6594 this would not work properly if we return the reference found.
6595 (For example, with "struct foo" in an outer scope, "union foo;"
6596 must shadow that tag with a new one of union type.) */
6597 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
6598 if (ref && TREE_CODE (ref) == code)
6600 if (C_TYPE_DEFINED_IN_STRUCT (ref)
6601 && loc != UNKNOWN_LOCATION
6602 && warn_cxx_compat)
6604 switch (code)
6606 case ENUMERAL_TYPE:
6607 warning_at (loc, OPT_Wc___compat,
6608 ("enum type defined in struct or union "
6609 "is not visible in C++"));
6610 inform (refloc, "enum type defined here");
6611 break;
6612 case RECORD_TYPE:
6613 warning_at (loc, OPT_Wc___compat,
6614 ("struct defined in struct or union "
6615 "is not visible in C++"));
6616 inform (refloc, "struct defined here");
6617 break;
6618 case UNION_TYPE:
6619 warning_at (loc, OPT_Wc___compat,
6620 ("union defined in struct or union "
6621 "is not visible in C++"));
6622 inform (refloc, "union defined here");
6623 break;
6624 default:
6625 gcc_unreachable();
6629 ret.spec = ref;
6630 return ret;
6633 /* If no such tag is yet defined, create a forward-reference node
6634 and record it as the "definition".
6635 When a real declaration of this type is found,
6636 the forward-reference will be altered into a real type. */
6638 ref = make_node (code);
6639 if (code == ENUMERAL_TYPE)
6641 /* Give the type a default layout like unsigned int
6642 to avoid crashing if it does not get defined. */
6643 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
6644 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
6645 TYPE_USER_ALIGN (ref) = 0;
6646 TYPE_UNSIGNED (ref) = 1;
6647 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
6648 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
6649 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
6652 pushtag (loc, name, ref);
6654 ret.spec = ref;
6655 return ret;
6658 /* Get the struct, enum or union (CODE says which) with tag NAME.
6659 Define the tag as a forward-reference if it is not defined.
6660 Return a tree for the type. */
6662 tree
6663 xref_tag (enum tree_code code, tree name)
6665 return parser_xref_tag (input_location, code, name).spec;
6668 /* Make sure that the tag NAME is defined *in the current scope*
6669 at least as a forward reference.
6670 LOC is the location of the struct's definition.
6671 CODE says which kind of tag NAME ought to be.
6673 This stores the current value of the file static STRUCT_PARSE_INFO
6674 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
6675 new c_struct_parse_info structure. The old value of
6676 STRUCT_PARSE_INFO is restored in finish_struct. */
6678 tree
6679 start_struct (location_t loc, enum tree_code code, tree name,
6680 struct c_struct_parse_info **enclosing_struct_parse_info)
6682 /* If there is already a tag defined at this scope
6683 (as a forward reference), just return it. */
6685 tree ref = NULL_TREE;
6686 location_t refloc = UNKNOWN_LOCATION;
6688 if (name != NULL_TREE)
6689 ref = lookup_tag (code, name, 1, &refloc);
6690 if (ref && TREE_CODE (ref) == code)
6692 if (TYPE_SIZE (ref))
6694 if (code == UNION_TYPE)
6695 error_at (loc, "redefinition of %<union %E%>", name);
6696 else
6697 error_at (loc, "redefinition of %<struct %E%>", name);
6698 if (refloc != UNKNOWN_LOCATION)
6699 inform (refloc, "originally defined here");
6700 /* Don't create structures using a name already in use. */
6701 ref = NULL_TREE;
6703 else if (C_TYPE_BEING_DEFINED (ref))
6705 if (code == UNION_TYPE)
6706 error_at (loc, "nested redefinition of %<union %E%>", name);
6707 else
6708 error_at (loc, "nested redefinition of %<struct %E%>", name);
6709 /* Don't bother to report "originally defined here" for a
6710 nested redefinition; the original definition should be
6711 obvious. */
6712 /* Don't create structures that contain themselves. */
6713 ref = NULL_TREE;
6717 /* Otherwise create a forward-reference just so the tag is in scope. */
6719 if (ref == NULL_TREE || TREE_CODE (ref) != code)
6721 ref = make_node (code);
6722 pushtag (loc, name, ref);
6725 C_TYPE_BEING_DEFINED (ref) = 1;
6726 TYPE_PACKED (ref) = flag_pack_struct;
6728 *enclosing_struct_parse_info = struct_parse_info;
6729 struct_parse_info = XNEW (struct c_struct_parse_info);
6730 struct_parse_info->struct_types.create (0);
6731 struct_parse_info->fields.create (0);
6732 struct_parse_info->typedefs_seen.create (0);
6734 /* FIXME: This will issue a warning for a use of a type defined
6735 within a statement expr used within sizeof, et. al. This is not
6736 terribly serious as C++ doesn't permit statement exprs within
6737 sizeof anyhow. */
6738 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
6739 warning_at (loc, OPT_Wc___compat,
6740 "defining type in %qs expression is invalid in C++",
6741 (in_sizeof
6742 ? "sizeof"
6743 : (in_typeof ? "typeof" : "alignof")));
6745 return ref;
6748 /* Process the specs, declarator and width (NULL if omitted)
6749 of a structure component, returning a FIELD_DECL node.
6750 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
6751 DECL_ATTRS is as for grokdeclarator.
6753 LOC is the location of the structure component.
6755 This is done during the parsing of the struct declaration.
6756 The FIELD_DECL nodes are chained together and the lot of them
6757 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
6759 tree
6760 grokfield (location_t loc,
6761 struct c_declarator *declarator, struct c_declspecs *declspecs,
6762 tree width, tree *decl_attrs)
6764 tree value;
6766 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
6767 && width == NULL_TREE)
6769 /* This is an unnamed decl.
6771 If we have something of the form "union { list } ;" then this
6772 is the anonymous union extension. Similarly for struct.
6774 If this is something of the form "struct foo;", then
6775 If MS or Plan 9 extensions are enabled, this is handled as
6776 an anonymous struct.
6777 Otherwise this is a forward declaration of a structure tag.
6779 If this is something of the form "foo;" and foo is a TYPE_DECL, then
6780 If foo names a structure or union without a tag, then this
6781 is an anonymous struct (this is permitted by C11).
6782 If MS or Plan 9 extensions are enabled and foo names a
6783 structure, then again this is an anonymous struct.
6784 Otherwise this is an error.
6786 Oh what a horrid tangled web we weave. I wonder if MS consciously
6787 took this from Plan 9 or if it was an accident of implementation
6788 that took root before someone noticed the bug... */
6790 tree type = declspecs->type;
6791 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
6792 || TREE_CODE (type) == UNION_TYPE);
6793 bool ok = false;
6795 if (type_ok
6796 && (flag_ms_extensions
6797 || flag_plan9_extensions
6798 || !declspecs->typedef_p))
6800 if (flag_ms_extensions || flag_plan9_extensions)
6801 ok = true;
6802 else if (TYPE_NAME (type) == NULL)
6803 ok = true;
6804 else
6805 ok = false;
6807 if (!ok)
6809 pedwarn (loc, 0, "declaration does not declare anything");
6810 return NULL_TREE;
6812 if (!flag_isoc11)
6814 if (flag_isoc99)
6815 pedwarn (loc, OPT_Wpedantic,
6816 "ISO C99 doesn%'t support unnamed structs/unions");
6817 else
6818 pedwarn (loc, OPT_Wpedantic,
6819 "ISO C90 doesn%'t support unnamed structs/unions");
6823 value = grokdeclarator (declarator, declspecs, FIELD, false,
6824 width ? &width : NULL, decl_attrs, NULL, NULL,
6825 DEPRECATED_NORMAL);
6827 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6828 DECL_INITIAL (value) = width;
6830 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
6832 /* If we currently have a binding for this field, set the
6833 in_struct field in the binding, so that we warn about lookups
6834 which find it. */
6835 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
6836 if (b != NULL)
6838 /* If the in_struct field is not yet set, push it on a list
6839 to be cleared when this struct is finished. */
6840 if (!b->in_struct)
6842 struct_parse_info->fields.safe_push (b);
6843 b->in_struct = 1;
6848 return value;
6851 /* Subroutine of detect_field_duplicates: return whether X and Y,
6852 which are both fields in the same struct, have duplicate field
6853 names. */
6855 static bool
6856 is_duplicate_field (tree x, tree y)
6858 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
6859 return true;
6861 /* When using -fplan9-extensions, an anonymous field whose name is a
6862 typedef can duplicate a field name. */
6863 if (flag_plan9_extensions
6864 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
6866 tree xt, xn, yt, yn;
6868 xt = TREE_TYPE (x);
6869 if (DECL_NAME (x) != NULL_TREE)
6870 xn = DECL_NAME (x);
6871 else if ((TREE_CODE (xt) == RECORD_TYPE || TREE_CODE (xt) == UNION_TYPE)
6872 && TYPE_NAME (xt) != NULL_TREE
6873 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
6874 xn = DECL_NAME (TYPE_NAME (xt));
6875 else
6876 xn = NULL_TREE;
6878 yt = TREE_TYPE (y);
6879 if (DECL_NAME (y) != NULL_TREE)
6880 yn = DECL_NAME (y);
6881 else if ((TREE_CODE (yt) == RECORD_TYPE || TREE_CODE (yt) == UNION_TYPE)
6882 && TYPE_NAME (yt) != NULL_TREE
6883 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
6884 yn = DECL_NAME (TYPE_NAME (yt));
6885 else
6886 yn = NULL_TREE;
6888 if (xn != NULL_TREE && xn == yn)
6889 return true;
6892 return false;
6895 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
6896 to HTAB, giving errors for any duplicates. */
6898 static void
6899 detect_field_duplicates_hash (tree fieldlist,
6900 hash_table <pointer_hash <tree_node> > htab)
6902 tree x, y;
6903 tree_node **slot;
6905 for (x = fieldlist; x ; x = DECL_CHAIN (x))
6906 if ((y = DECL_NAME (x)) != 0)
6908 slot = htab.find_slot (y, INSERT);
6909 if (*slot)
6911 error ("duplicate member %q+D", x);
6912 DECL_NAME (x) = NULL_TREE;
6914 *slot = y;
6916 else if (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6917 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6919 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
6921 /* When using -fplan9-extensions, an anonymous field whose
6922 name is a typedef can duplicate a field name. */
6923 if (flag_plan9_extensions
6924 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
6925 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
6927 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
6928 slot = htab.find_slot (xn, INSERT);
6929 if (*slot)
6930 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
6931 *slot = xn;
6936 /* Generate an error for any duplicate field names in FIELDLIST. Munge
6937 the list such that this does not present a problem later. */
6939 static void
6940 detect_field_duplicates (tree fieldlist)
6942 tree x, y;
6943 int timeout = 10;
6945 /* If the struct is the list of instance variables of an Objective-C
6946 class, then we need to check all the instance variables of
6947 superclasses when checking for duplicates (since you can't have
6948 an instance variable in a subclass with the same name as an
6949 instance variable in a superclass). We pass on this job to the
6950 Objective-C compiler. objc_detect_field_duplicates() will return
6951 false if we are not checking the list of instance variables and
6952 the C frontend should proceed with the standard field duplicate
6953 checks. If we are checking the list of instance variables, the
6954 ObjC frontend will do the check, emit the errors if needed, and
6955 then return true. */
6956 if (c_dialect_objc ())
6957 if (objc_detect_field_duplicates (false))
6958 return;
6960 /* First, see if there are more than "a few" fields.
6961 This is trivially true if there are zero or one fields. */
6962 if (!fieldlist || !DECL_CHAIN (fieldlist))
6963 return;
6964 x = fieldlist;
6965 do {
6966 timeout--;
6967 if (DECL_NAME (x) == NULL_TREE
6968 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6969 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
6970 timeout = 0;
6971 x = DECL_CHAIN (x);
6972 } while (timeout > 0 && x);
6974 /* If there were "few" fields and no anonymous structures or unions,
6975 avoid the overhead of allocating a hash table. Instead just do
6976 the nested traversal thing. */
6977 if (timeout > 0)
6979 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
6980 /* When using -fplan9-extensions, we can have duplicates
6981 between typedef names and fields. */
6982 if (DECL_NAME (x)
6983 || (flag_plan9_extensions
6984 && DECL_NAME (x) == NULL_TREE
6985 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6986 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6987 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
6988 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
6990 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
6991 if (is_duplicate_field (y, x))
6993 error ("duplicate member %q+D", x);
6994 DECL_NAME (x) = NULL_TREE;
6998 else
7000 hash_table <pointer_hash <tree_node> > htab;
7001 htab.create (37);
7003 detect_field_duplicates_hash (fieldlist, htab);
7004 htab.dispose ();
7008 /* Finish up struct info used by -Wc++-compat. */
7010 static void
7011 warn_cxx_compat_finish_struct (tree fieldlist)
7013 unsigned int ix;
7014 tree x;
7015 struct c_binding *b;
7017 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7018 the current struct. We do this now at the end of the struct
7019 because the flag is used to issue visibility warnings, and we
7020 only want to issue those warnings if the type is referenced
7021 outside of the struct declaration. */
7022 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7023 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7025 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7026 typedefs used when declaring fields in this struct. If the name
7027 of any of the fields is also a typedef name then the struct would
7028 not parse in C++, because the C++ lookup rules say that the
7029 typedef name would be looked up in the context of the struct, and
7030 would thus be the field rather than the typedef. */
7031 if (!struct_parse_info->typedefs_seen.is_empty ()
7032 && fieldlist != NULL_TREE)
7034 /* Use a pointer_set using the name of the typedef. We can use
7035 a pointer_set because identifiers are interned. */
7036 struct pointer_set_t *tset = pointer_set_create ();
7038 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7039 pointer_set_insert (tset, DECL_NAME (x));
7041 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7043 if (DECL_NAME (x) != NULL_TREE
7044 && pointer_set_contains (tset, DECL_NAME (x)))
7046 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7047 ("using %qD as both field and typedef name is "
7048 "invalid in C++"),
7050 /* FIXME: It would be nice to report the location where
7051 the typedef name is used. */
7055 pointer_set_destroy (tset);
7058 /* For each field which has a binding and which was not defined in
7059 an enclosing struct, clear the in_struct field. */
7060 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7061 b->in_struct = 0;
7064 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7065 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7066 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7067 ATTRIBUTES are attributes to be applied to the structure.
7069 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7070 the struct was started. */
7072 tree
7073 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7074 struct c_struct_parse_info *enclosing_struct_parse_info)
7076 tree x;
7077 bool toplevel = file_scope == current_scope;
7078 int saw_named_field;
7080 /* If this type was previously laid out as a forward reference,
7081 make sure we lay it out again. */
7083 TYPE_SIZE (t) = 0;
7085 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7087 if (pedantic)
7089 for (x = fieldlist; x; x = DECL_CHAIN (x))
7091 if (DECL_NAME (x) != 0)
7092 break;
7093 if (flag_isoc11
7094 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7095 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7096 break;
7099 if (x == 0)
7101 if (TREE_CODE (t) == UNION_TYPE)
7103 if (fieldlist)
7104 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7105 else
7106 pedwarn (loc, OPT_Wpedantic, "union has no members");
7108 else
7110 if (fieldlist)
7111 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7112 else
7113 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7118 /* Install struct as DECL_CONTEXT of each field decl.
7119 Also process specified field sizes, found in the DECL_INITIAL,
7120 storing 0 there after the type has been changed to precision equal
7121 to its width, rather than the precision of the specified standard
7122 type. (Correct layout requires the original type to have been preserved
7123 until now.) */
7125 saw_named_field = 0;
7126 for (x = fieldlist; x; x = DECL_CHAIN (x))
7128 if (TREE_TYPE (x) == error_mark_node)
7129 continue;
7131 DECL_CONTEXT (x) = t;
7133 /* If any field is const, the structure type is pseudo-const. */
7134 if (TREE_READONLY (x))
7135 C_TYPE_FIELDS_READONLY (t) = 1;
7136 else
7138 /* A field that is pseudo-const makes the structure likewise. */
7139 tree t1 = strip_array_types (TREE_TYPE (x));
7140 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
7141 && C_TYPE_FIELDS_READONLY (t1))
7142 C_TYPE_FIELDS_READONLY (t) = 1;
7145 /* Any field that is volatile means variables of this type must be
7146 treated in some ways as volatile. */
7147 if (TREE_THIS_VOLATILE (x))
7148 C_TYPE_FIELDS_VOLATILE (t) = 1;
7150 /* Any field of nominal variable size implies structure is too. */
7151 if (C_DECL_VARIABLE_SIZE (x))
7152 C_TYPE_VARIABLE_SIZE (t) = 1;
7154 if (DECL_INITIAL (x))
7156 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
7157 DECL_SIZE (x) = bitsize_int (width);
7158 DECL_BIT_FIELD (x) = 1;
7159 SET_DECL_C_BIT_FIELD (x);
7162 if (TYPE_PACKED (t)
7163 && (DECL_BIT_FIELD (x)
7164 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7165 DECL_PACKED (x) = 1;
7167 /* Detect flexible array member in an invalid context. */
7168 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7169 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7170 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7171 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7173 if (TREE_CODE (t) == UNION_TYPE)
7175 error_at (DECL_SOURCE_LOCATION (x),
7176 "flexible array member in union");
7177 TREE_TYPE (x) = error_mark_node;
7179 else if (DECL_CHAIN (x) != NULL_TREE)
7181 error_at (DECL_SOURCE_LOCATION (x),
7182 "flexible array member not at end of struct");
7183 TREE_TYPE (x) = error_mark_node;
7185 else if (!saw_named_field)
7187 error_at (DECL_SOURCE_LOCATION (x),
7188 "flexible array member in otherwise empty struct");
7189 TREE_TYPE (x) = error_mark_node;
7193 if (pedantic && TREE_CODE (t) == RECORD_TYPE
7194 && flexible_array_type_p (TREE_TYPE (x)))
7195 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
7196 "invalid use of structure with flexible array member");
7198 if (DECL_NAME (x)
7199 || TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7200 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7201 saw_named_field = 1;
7204 detect_field_duplicates (fieldlist);
7206 /* Now we have the nearly final fieldlist. Record it,
7207 then lay out the structure or union (including the fields). */
7209 TYPE_FIELDS (t) = fieldlist;
7211 layout_type (t);
7213 /* Give bit-fields their proper types. */
7215 tree *fieldlistp = &fieldlist;
7216 while (*fieldlistp)
7217 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
7218 && TREE_TYPE (*fieldlistp) != error_mark_node)
7220 unsigned HOST_WIDE_INT width
7221 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
7222 tree type = TREE_TYPE (*fieldlistp);
7223 if (width != TYPE_PRECISION (type))
7225 TREE_TYPE (*fieldlistp)
7226 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
7227 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
7229 DECL_INITIAL (*fieldlistp) = 0;
7231 else
7232 fieldlistp = &DECL_CHAIN (*fieldlistp);
7235 /* Now we have the truly final field list.
7236 Store it in this type and in the variants. */
7238 TYPE_FIELDS (t) = fieldlist;
7240 /* If there are lots of fields, sort so we can look through them fast.
7241 We arbitrarily consider 16 or more elts to be "a lot". */
7244 int len = 0;
7246 for (x = fieldlist; x; x = DECL_CHAIN (x))
7248 if (len > 15 || DECL_NAME (x) == NULL)
7249 break;
7250 len += 1;
7253 if (len > 15)
7255 tree *field_array;
7256 struct lang_type *space;
7257 struct sorted_fields_type *space2;
7259 len += list_length (x);
7261 /* Use the same allocation policy here that make_node uses, to
7262 ensure that this lives as long as the rest of the struct decl.
7263 All decls in an inline function need to be saved. */
7265 space = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
7266 space2 = ggc_alloc_sorted_fields_type
7267 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
7269 len = 0;
7270 space->s = space2;
7271 field_array = &space2->elts[0];
7272 for (x = fieldlist; x; x = DECL_CHAIN (x))
7274 field_array[len++] = x;
7276 /* If there is anonymous struct or union, break out of the loop. */
7277 if (DECL_NAME (x) == NULL)
7278 break;
7280 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
7281 if (x == NULL)
7283 TYPE_LANG_SPECIFIC (t) = space;
7284 TYPE_LANG_SPECIFIC (t)->s->len = len;
7285 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
7286 qsort (field_array, len, sizeof (tree), field_decl_cmp);
7291 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7293 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7294 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
7295 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
7296 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
7297 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
7300 /* If this was supposed to be a transparent union, but we can't
7301 make it one, warn and turn off the flag. */
7302 if (TREE_CODE (t) == UNION_TYPE
7303 && TYPE_TRANSPARENT_AGGR (t)
7304 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
7306 TYPE_TRANSPARENT_AGGR (t) = 0;
7307 warning_at (loc, 0, "union cannot be made transparent");
7310 /* If this structure or union completes the type of any previous
7311 variable declaration, lay it out and output its rtl. */
7312 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
7314 x = TREE_CHAIN (x))
7316 tree decl = TREE_VALUE (x);
7317 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7318 layout_array_type (TREE_TYPE (decl));
7319 if (TREE_CODE (decl) != TYPE_DECL)
7321 layout_decl (decl, 0);
7322 if (c_dialect_objc ())
7323 objc_check_decl (decl);
7324 rest_of_decl_compilation (decl, toplevel, 0);
7327 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
7329 /* Update type location to the one of the definition, instead of e.g.
7330 a forward declaration. */
7331 if (TYPE_STUB_DECL (t))
7332 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
7334 /* Finish debugging output for this type. */
7335 rest_of_type_compilation (t, toplevel);
7337 /* If we're inside a function proper, i.e. not file-scope and not still
7338 parsing parameters, then arrange for the size of a variable sized type
7339 to be bound now. */
7340 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
7341 add_stmt (build_stmt (loc,
7342 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
7344 if (warn_cxx_compat)
7345 warn_cxx_compat_finish_struct (fieldlist);
7347 struct_parse_info->struct_types.release ();
7348 struct_parse_info->fields.release ();
7349 struct_parse_info->typedefs_seen.release ();
7350 XDELETE (struct_parse_info);
7352 struct_parse_info = enclosing_struct_parse_info;
7354 /* If this struct is defined inside a struct, add it to
7355 struct_types. */
7356 if (warn_cxx_compat
7357 && struct_parse_info != NULL
7358 && !in_sizeof && !in_typeof && !in_alignof)
7359 struct_parse_info->struct_types.safe_push (t);
7361 return t;
7364 /* Lay out the type T, and its element type, and so on. */
7366 static void
7367 layout_array_type (tree t)
7369 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7370 layout_array_type (TREE_TYPE (t));
7371 layout_type (t);
7374 /* Begin compiling the definition of an enumeration type.
7375 NAME is its name (or null if anonymous).
7376 LOC is the enum's location.
7377 Returns the type object, as yet incomplete.
7378 Also records info about it so that build_enumerator
7379 may be used to declare the individual values as they are read. */
7381 tree
7382 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
7384 tree enumtype = NULL_TREE;
7385 location_t enumloc = UNKNOWN_LOCATION;
7387 /* If this is the real definition for a previous forward reference,
7388 fill in the contents in the same object that used to be the
7389 forward reference. */
7391 if (name != NULL_TREE)
7392 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
7394 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
7396 enumtype = make_node (ENUMERAL_TYPE);
7397 pushtag (loc, name, enumtype);
7400 if (C_TYPE_BEING_DEFINED (enumtype))
7401 error_at (loc, "nested redefinition of %<enum %E%>", name);
7403 C_TYPE_BEING_DEFINED (enumtype) = 1;
7405 if (TYPE_VALUES (enumtype) != 0)
7407 /* This enum is a named one that has been declared already. */
7408 error_at (loc, "redeclaration of %<enum %E%>", name);
7409 if (enumloc != UNKNOWN_LOCATION)
7410 inform (enumloc, "originally defined here");
7412 /* Completely replace its old definition.
7413 The old enumerators remain defined, however. */
7414 TYPE_VALUES (enumtype) = 0;
7417 the_enum->enum_next_value = integer_zero_node;
7418 the_enum->enum_overflow = 0;
7420 if (flag_short_enums)
7421 TYPE_PACKED (enumtype) = 1;
7423 /* FIXME: This will issue a warning for a use of a type defined
7424 within sizeof in a statement expr. This is not terribly serious
7425 as C++ doesn't permit statement exprs within sizeof anyhow. */
7426 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7427 warning_at (loc, OPT_Wc___compat,
7428 "defining type in %qs expression is invalid in C++",
7429 (in_sizeof
7430 ? "sizeof"
7431 : (in_typeof ? "typeof" : "alignof")));
7433 return enumtype;
7436 /* After processing and defining all the values of an enumeration type,
7437 install their decls in the enumeration type and finish it off.
7438 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
7439 and ATTRIBUTES are the specified attributes.
7440 Returns ENUMTYPE. */
7442 tree
7443 finish_enum (tree enumtype, tree values, tree attributes)
7445 tree pair, tem;
7446 tree minnode = 0, maxnode = 0;
7447 int precision, unsign;
7448 bool toplevel = (file_scope == current_scope);
7449 struct lang_type *lt;
7451 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7453 /* Calculate the maximum value of any enumerator in this type. */
7455 if (values == error_mark_node)
7456 minnode = maxnode = integer_zero_node;
7457 else
7459 minnode = maxnode = TREE_VALUE (values);
7460 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
7462 tree value = TREE_VALUE (pair);
7463 if (tree_int_cst_lt (maxnode, value))
7464 maxnode = value;
7465 if (tree_int_cst_lt (value, minnode))
7466 minnode = value;
7470 /* Construct the final type of this enumeration. It is the same
7471 as one of the integral types - the narrowest one that fits, except
7472 that normally we only go as narrow as int - and signed iff any of
7473 the values are negative. */
7474 unsign = (tree_int_cst_sgn (minnode) >= 0);
7475 precision = MAX (tree_int_cst_min_precision (minnode, unsign),
7476 tree_int_cst_min_precision (maxnode, unsign));
7478 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
7480 tem = c_common_type_for_size (precision, unsign);
7481 if (tem == NULL)
7483 warning (0, "enumeration values exceed range of largest integer");
7484 tem = long_long_integer_type_node;
7487 else
7488 tem = unsign ? unsigned_type_node : integer_type_node;
7490 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
7491 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
7492 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
7493 TYPE_SIZE (enumtype) = 0;
7495 /* If the precision of the type was specific with an attribute and it
7496 was too small, give an error. Otherwise, use it. */
7497 if (TYPE_PRECISION (enumtype))
7499 if (precision > TYPE_PRECISION (enumtype))
7500 error ("specified mode too small for enumeral values");
7502 else
7503 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
7505 layout_type (enumtype);
7507 if (values != error_mark_node)
7509 /* Change the type of the enumerators to be the enum type. We
7510 need to do this irrespective of the size of the enum, for
7511 proper type checking. Replace the DECL_INITIALs of the
7512 enumerators, and the value slots of the list, with copies
7513 that have the enum type; they cannot be modified in place
7514 because they may be shared (e.g. integer_zero_node) Finally,
7515 change the purpose slots to point to the names of the decls. */
7516 for (pair = values; pair; pair = TREE_CHAIN (pair))
7518 tree enu = TREE_PURPOSE (pair);
7519 tree ini = DECL_INITIAL (enu);
7521 TREE_TYPE (enu) = enumtype;
7523 /* The ISO C Standard mandates enumerators to have type int,
7524 even though the underlying type of an enum type is
7525 unspecified. However, GCC allows enumerators of any
7526 integer type as an extensions. build_enumerator()
7527 converts any enumerators that fit in an int to type int,
7528 to avoid promotions to unsigned types when comparing
7529 integers with enumerators that fit in the int range.
7530 When -pedantic is given, build_enumerator() would have
7531 already warned about those that don't fit. Here we
7532 convert the rest to the enumerator type. */
7533 if (TREE_TYPE (ini) != integer_type_node)
7534 ini = convert (enumtype, ini);
7536 DECL_INITIAL (enu) = ini;
7537 TREE_PURPOSE (pair) = DECL_NAME (enu);
7538 TREE_VALUE (pair) = ini;
7541 TYPE_VALUES (enumtype) = values;
7544 /* Record the min/max values so that we can warn about bit-field
7545 enumerations that are too small for the values. */
7546 lt = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
7547 lt->enum_min = minnode;
7548 lt->enum_max = maxnode;
7549 TYPE_LANG_SPECIFIC (enumtype) = lt;
7551 /* Fix up all variant types of this enum type. */
7552 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
7554 if (tem == enumtype)
7555 continue;
7556 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
7557 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
7558 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
7559 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
7560 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
7561 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
7562 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
7563 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
7564 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
7565 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
7566 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
7569 /* Finish debugging output for this type. */
7570 rest_of_type_compilation (enumtype, toplevel);
7572 /* If this enum is defined inside a struct, add it to
7573 struct_types. */
7574 if (warn_cxx_compat
7575 && struct_parse_info != NULL
7576 && !in_sizeof && !in_typeof && !in_alignof)
7577 struct_parse_info->struct_types.safe_push (enumtype);
7579 return enumtype;
7582 /* Build and install a CONST_DECL for one value of the
7583 current enumeration type (one that was begun with start_enum).
7584 DECL_LOC is the location of the enumerator.
7585 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
7586 Return a tree-list containing the CONST_DECL and its value.
7587 Assignment of sequential values by default is handled here. */
7589 tree
7590 build_enumerator (location_t decl_loc, location_t loc,
7591 struct c_enum_contents *the_enum, tree name, tree value)
7593 tree decl, type;
7595 /* Validate and default VALUE. */
7597 if (value != 0)
7599 /* Don't issue more errors for error_mark_node (i.e. an
7600 undeclared identifier) - just ignore the value expression. */
7601 if (value == error_mark_node)
7602 value = 0;
7603 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
7605 error_at (loc, "enumerator value for %qE is not an integer constant",
7606 name);
7607 value = 0;
7609 else
7611 if (TREE_CODE (value) != INTEGER_CST)
7613 value = c_fully_fold (value, false, NULL);
7614 if (TREE_CODE (value) == INTEGER_CST)
7615 pedwarn (loc, OPT_Wpedantic,
7616 "enumerator value for %qE is not an integer "
7617 "constant expression", name);
7619 if (TREE_CODE (value) != INTEGER_CST)
7621 error ("enumerator value for %qE is not an integer constant",
7622 name);
7623 value = 0;
7625 else
7627 value = default_conversion (value);
7628 constant_expression_warning (value);
7633 /* Default based on previous value. */
7634 /* It should no longer be possible to have NON_LVALUE_EXPR
7635 in the default. */
7636 if (value == 0)
7638 value = the_enum->enum_next_value;
7639 if (the_enum->enum_overflow)
7640 error_at (loc, "overflow in enumeration values");
7642 /* Even though the underlying type of an enum is unspecified, the
7643 type of enumeration constants is explicitly defined as int
7644 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
7645 an extension. */
7646 else if (!int_fits_type_p (value, integer_type_node))
7647 pedwarn (loc, OPT_Wpedantic,
7648 "ISO C restricts enumerator values to range of %<int%>");
7650 /* The ISO C Standard mandates enumerators to have type int, even
7651 though the underlying type of an enum type is unspecified.
7652 However, GCC allows enumerators of any integer type as an
7653 extensions. Here we convert any enumerators that fit in an int
7654 to type int, to avoid promotions to unsigned types when comparing
7655 integers with enumerators that fit in the int range. When
7656 -pedantic is given, we would have already warned about those that
7657 don't fit. We have to do this here rather than in finish_enum
7658 because this value may be used to define more enumerators. */
7659 if (int_fits_type_p (value, integer_type_node))
7660 value = convert (integer_type_node, value);
7662 /* Set basis for default for next value. */
7663 the_enum->enum_next_value
7664 = build_binary_op (EXPR_LOC_OR_HERE (value),
7665 PLUS_EXPR, value, integer_one_node, 0);
7666 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
7668 /* Now create a declaration for the enum value name. */
7670 type = TREE_TYPE (value);
7671 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
7672 TYPE_PRECISION (integer_type_node)),
7673 (TYPE_PRECISION (type)
7674 >= TYPE_PRECISION (integer_type_node)
7675 && TYPE_UNSIGNED (type)));
7677 decl = build_decl (decl_loc, CONST_DECL, name, type);
7678 DECL_INITIAL (decl) = convert (type, value);
7679 pushdecl (decl);
7681 return tree_cons (decl, value, NULL_TREE);
7685 /* Create the FUNCTION_DECL for a function definition.
7686 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
7687 the declaration; they describe the function's name and the type it returns,
7688 but twisted together in a fashion that parallels the syntax of C.
7690 This function creates a binding context for the function body
7691 as well as setting up the FUNCTION_DECL in current_function_decl.
7693 Returns 1 on success. If the DECLARATOR is not suitable for a function
7694 (it defines a datum instead), we return 0, which tells
7695 yyparse to report a parse error. */
7698 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
7699 tree attributes)
7701 tree decl1, old_decl;
7702 tree restype, resdecl;
7703 location_t loc;
7705 current_function_returns_value = 0; /* Assume, until we see it does. */
7706 current_function_returns_null = 0;
7707 current_function_returns_abnormally = 0;
7708 warn_about_return_type = 0;
7709 c_switch_stack = NULL;
7711 /* Indicate no valid break/continue context by setting these variables
7712 to some non-null, non-label value. We'll notice and emit the proper
7713 error message in c_finish_bc_stmt. */
7714 c_break_label = c_cont_label = size_zero_node;
7716 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
7717 &attributes, NULL, NULL, DEPRECATED_NORMAL);
7719 /* If the declarator is not suitable for a function definition,
7720 cause a syntax error. */
7721 if (decl1 == 0
7722 || TREE_CODE (decl1) != FUNCTION_DECL)
7723 return 0;
7725 loc = DECL_SOURCE_LOCATION (decl1);
7727 decl_attributes (&decl1, attributes, 0);
7729 if (DECL_DECLARED_INLINE_P (decl1)
7730 && DECL_UNINLINABLE (decl1)
7731 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
7732 warning_at (loc, OPT_Wattributes,
7733 "inline function %qD given attribute noinline",
7734 decl1);
7736 /* Handle gnu_inline attribute. */
7737 if (declspecs->inline_p
7738 && !flag_gnu89_inline
7739 && TREE_CODE (decl1) == FUNCTION_DECL
7740 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
7741 || current_function_decl))
7743 if (declspecs->storage_class != csc_static)
7744 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
7747 announce_function (decl1);
7749 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
7751 error_at (loc, "return type is an incomplete type");
7752 /* Make it return void instead. */
7753 TREE_TYPE (decl1)
7754 = build_function_type (void_type_node,
7755 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
7758 if (warn_about_return_type)
7759 pedwarn_c99 (loc, flag_isoc99 ? 0
7760 : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
7761 "return type defaults to %<int%>");
7763 /* Make the init_value nonzero so pushdecl knows this is not tentative.
7764 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
7765 DECL_INITIAL (decl1) = error_mark_node;
7767 /* A nested function is not global. */
7768 if (current_function_decl != 0)
7769 TREE_PUBLIC (decl1) = 0;
7771 /* If this definition isn't a prototype and we had a prototype declaration
7772 before, copy the arg type info from that prototype. */
7773 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
7774 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
7775 old_decl = 0;
7776 current_function_prototype_locus = UNKNOWN_LOCATION;
7777 current_function_prototype_built_in = false;
7778 current_function_prototype_arg_types = NULL_TREE;
7779 if (!prototype_p (TREE_TYPE (decl1)))
7781 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
7782 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7783 TREE_TYPE (TREE_TYPE (old_decl))))
7785 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
7786 TREE_TYPE (decl1));
7787 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
7788 current_function_prototype_built_in
7789 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
7790 current_function_prototype_arg_types
7791 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
7793 if (TREE_PUBLIC (decl1))
7795 /* If there is an external prototype declaration of this
7796 function, record its location but do not copy information
7797 to this decl. This may be an invisible declaration
7798 (built-in or in a scope which has finished) or simply
7799 have more refined argument types than any declaration
7800 found above. */
7801 struct c_binding *b;
7802 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
7803 if (B_IN_SCOPE (b, external_scope))
7804 break;
7805 if (b)
7807 tree ext_decl, ext_type;
7808 ext_decl = b->decl;
7809 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
7810 if (TREE_CODE (ext_type) == FUNCTION_TYPE
7811 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7812 TREE_TYPE (ext_type)))
7814 current_function_prototype_locus
7815 = DECL_SOURCE_LOCATION (ext_decl);
7816 current_function_prototype_built_in
7817 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
7818 current_function_prototype_arg_types
7819 = TYPE_ARG_TYPES (ext_type);
7825 /* Optionally warn of old-fashioned def with no previous prototype. */
7826 if (warn_strict_prototypes
7827 && old_decl != error_mark_node
7828 && !prototype_p (TREE_TYPE (decl1))
7829 && C_DECL_ISNT_PROTOTYPE (old_decl))
7830 warning_at (loc, OPT_Wstrict_prototypes,
7831 "function declaration isn%'t a prototype");
7832 /* Optionally warn of any global def with no previous prototype. */
7833 else if (warn_missing_prototypes
7834 && old_decl != error_mark_node
7835 && TREE_PUBLIC (decl1)
7836 && !MAIN_NAME_P (DECL_NAME (decl1))
7837 && C_DECL_ISNT_PROTOTYPE (old_decl))
7838 warning_at (loc, OPT_Wmissing_prototypes,
7839 "no previous prototype for %qD", decl1);
7840 /* Optionally warn of any def with no previous prototype
7841 if the function has already been used. */
7842 else if (warn_missing_prototypes
7843 && old_decl != 0
7844 && old_decl != error_mark_node
7845 && TREE_USED (old_decl)
7846 && !prototype_p (TREE_TYPE (old_decl)))
7847 warning_at (loc, OPT_Wmissing_prototypes,
7848 "%qD was used with no prototype before its definition", decl1);
7849 /* Optionally warn of any global def with no previous declaration. */
7850 else if (warn_missing_declarations
7851 && TREE_PUBLIC (decl1)
7852 && old_decl == 0
7853 && !MAIN_NAME_P (DECL_NAME (decl1)))
7854 warning_at (loc, OPT_Wmissing_declarations,
7855 "no previous declaration for %qD",
7856 decl1);
7857 /* Optionally warn of any def with no previous declaration
7858 if the function has already been used. */
7859 else if (warn_missing_declarations
7860 && old_decl != 0
7861 && old_decl != error_mark_node
7862 && TREE_USED (old_decl)
7863 && C_DECL_IMPLICIT (old_decl))
7864 warning_at (loc, OPT_Wmissing_declarations,
7865 "%qD was used with no declaration before its definition", decl1);
7867 /* This function exists in static storage.
7868 (This does not mean `static' in the C sense!) */
7869 TREE_STATIC (decl1) = 1;
7871 /* This is the earliest point at which we might know the assembler
7872 name of the function. Thus, if it's set before this, die horribly. */
7873 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
7875 /* If #pragma weak was used, mark the decl weak now. */
7876 if (current_scope == file_scope)
7877 maybe_apply_pragma_weak (decl1);
7879 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
7880 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
7882 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
7883 != integer_type_node)
7884 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
7886 check_main_parameter_types (decl1);
7888 if (!TREE_PUBLIC (decl1))
7889 pedwarn (loc, OPT_Wmain,
7890 "%qD is normally a non-static function", decl1);
7893 /* Record the decl so that the function name is defined.
7894 If we already have a decl for this name, and it is a FUNCTION_DECL,
7895 use the old decl. */
7897 current_function_decl = pushdecl (decl1);
7899 push_scope ();
7900 declare_parm_level ();
7902 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
7903 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
7904 DECL_ARTIFICIAL (resdecl) = 1;
7905 DECL_IGNORED_P (resdecl) = 1;
7906 DECL_RESULT (current_function_decl) = resdecl;
7908 start_fname_decls ();
7910 return 1;
7913 /* Subroutine of store_parm_decls which handles new-style function
7914 definitions (prototype format). The parms already have decls, so we
7915 need only record them as in effect and complain if any redundant
7916 old-style parm decls were written. */
7917 static void
7918 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
7920 tree decl;
7921 c_arg_tag *tag;
7922 unsigned ix;
7924 if (current_scope->bindings)
7926 error_at (DECL_SOURCE_LOCATION (fndecl),
7927 "old-style parameter declarations in prototyped "
7928 "function definition");
7930 /* Get rid of the old-style declarations. */
7931 pop_scope ();
7932 push_scope ();
7934 /* Don't issue this warning for nested functions, and don't issue this
7935 warning if we got here because ARG_INFO_TYPES was error_mark_node
7936 (this happens when a function definition has just an ellipsis in
7937 its parameter list). */
7938 else if (!in_system_header && !current_function_scope
7939 && arg_info->types != error_mark_node)
7940 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
7941 "traditional C rejects ISO C style function definitions");
7943 /* Now make all the parameter declarations visible in the function body.
7944 We can bypass most of the grunt work of pushdecl. */
7945 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
7947 DECL_CONTEXT (decl) = current_function_decl;
7948 if (DECL_NAME (decl))
7950 bind (DECL_NAME (decl), decl, current_scope,
7951 /*invisible=*/false, /*nested=*/false,
7952 UNKNOWN_LOCATION);
7953 if (!TREE_USED (decl))
7954 warn_if_shadowing (decl);
7956 else
7957 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
7960 /* Record the parameter list in the function declaration. */
7961 DECL_ARGUMENTS (fndecl) = arg_info->parms;
7963 /* Now make all the ancillary declarations visible, likewise. */
7964 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
7966 DECL_CONTEXT (decl) = current_function_decl;
7967 if (DECL_NAME (decl))
7968 bind (DECL_NAME (decl), decl, current_scope,
7969 /*invisible=*/false,
7970 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
7971 UNKNOWN_LOCATION);
7974 /* And all the tag declarations. */
7975 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7976 if (tag->id)
7977 bind (tag->id, tag->type, current_scope,
7978 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
7981 /* Subroutine of store_parm_decls which handles old-style function
7982 definitions (separate parameter list and declarations). */
7984 static void
7985 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
7987 struct c_binding *b;
7988 tree parm, decl, last;
7989 tree parmids = arg_info->parms;
7990 struct pointer_set_t *seen_args = pointer_set_create ();
7992 if (!in_system_header)
7993 warning_at (DECL_SOURCE_LOCATION (fndecl),
7994 OPT_Wold_style_definition, "old-style function definition");
7996 /* Match each formal parameter name with its declaration. Save each
7997 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
7998 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8000 if (TREE_VALUE (parm) == 0)
8002 error_at (DECL_SOURCE_LOCATION (fndecl),
8003 "parameter name missing from parameter list");
8004 TREE_PURPOSE (parm) = 0;
8005 continue;
8008 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8009 if (b && B_IN_CURRENT_SCOPE (b))
8011 decl = b->decl;
8012 /* Skip erroneous parameters. */
8013 if (decl == error_mark_node)
8014 continue;
8015 /* If we got something other than a PARM_DECL it is an error. */
8016 if (TREE_CODE (decl) != PARM_DECL)
8017 error_at (DECL_SOURCE_LOCATION (decl),
8018 "%qD declared as a non-parameter", decl);
8019 /* If the declaration is already marked, we have a duplicate
8020 name. Complain and ignore the duplicate. */
8021 else if (pointer_set_contains (seen_args, decl))
8023 error_at (DECL_SOURCE_LOCATION (decl),
8024 "multiple parameters named %qD", decl);
8025 TREE_PURPOSE (parm) = 0;
8026 continue;
8028 /* If the declaration says "void", complain and turn it into
8029 an int. */
8030 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8032 error_at (DECL_SOURCE_LOCATION (decl),
8033 "parameter %qD declared with void type", decl);
8034 TREE_TYPE (decl) = integer_type_node;
8035 DECL_ARG_TYPE (decl) = integer_type_node;
8036 layout_decl (decl, 0);
8038 warn_if_shadowing (decl);
8040 /* If no declaration found, default to int. */
8041 else
8043 /* FIXME diagnostics: This should be the location of the argument,
8044 not the FNDECL. E.g., for an old-style declaration
8046 int f10(v) { blah; }
8048 We should use the location of the V, not the F10.
8049 Unfortunately, the V is an IDENTIFIER_NODE which has no
8050 location. In the future we need locations for c_arg_info
8051 entries.
8053 See gcc.dg/Wshadow-3.c for an example of this problem. */
8054 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8055 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8056 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8057 pushdecl (decl);
8058 warn_if_shadowing (decl);
8060 if (flag_isoc99)
8061 pedwarn (DECL_SOURCE_LOCATION (decl),
8062 0, "type of %qD defaults to %<int%>", decl);
8063 else
8064 warning_at (DECL_SOURCE_LOCATION (decl),
8065 OPT_Wmissing_parameter_type,
8066 "type of %qD defaults to %<int%>", decl);
8069 TREE_PURPOSE (parm) = decl;
8070 pointer_set_insert (seen_args, decl);
8073 /* Now examine the parms chain for incomplete declarations
8074 and declarations with no corresponding names. */
8076 for (b = current_scope->bindings; b; b = b->prev)
8078 parm = b->decl;
8079 if (TREE_CODE (parm) != PARM_DECL)
8080 continue;
8082 if (TREE_TYPE (parm) != error_mark_node
8083 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
8085 error_at (DECL_SOURCE_LOCATION (parm),
8086 "parameter %qD has incomplete type", parm);
8087 TREE_TYPE (parm) = error_mark_node;
8090 if (!pointer_set_contains (seen_args, parm))
8092 error_at (DECL_SOURCE_LOCATION (parm),
8093 "declaration for parameter %qD but no such parameter",
8094 parm);
8096 /* Pretend the parameter was not missing.
8097 This gets us to a standard state and minimizes
8098 further error messages. */
8099 parmids = chainon (parmids, tree_cons (parm, 0, 0));
8103 /* Chain the declarations together in the order of the list of
8104 names. Store that chain in the function decl, replacing the
8105 list of names. Update the current scope to match. */
8106 DECL_ARGUMENTS (fndecl) = 0;
8108 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8109 if (TREE_PURPOSE (parm))
8110 break;
8111 if (parm && TREE_PURPOSE (parm))
8113 last = TREE_PURPOSE (parm);
8114 DECL_ARGUMENTS (fndecl) = last;
8116 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8117 if (TREE_PURPOSE (parm))
8119 DECL_CHAIN (last) = TREE_PURPOSE (parm);
8120 last = TREE_PURPOSE (parm);
8122 DECL_CHAIN (last) = 0;
8125 pointer_set_destroy (seen_args);
8127 /* If there was a previous prototype,
8128 set the DECL_ARG_TYPE of each argument according to
8129 the type previously specified, and report any mismatches. */
8131 if (current_function_prototype_arg_types)
8133 tree type;
8134 for (parm = DECL_ARGUMENTS (fndecl),
8135 type = current_function_prototype_arg_types;
8136 parm || (type && TREE_VALUE (type) != error_mark_node
8137 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
8138 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8140 if (parm == 0 || type == 0
8141 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
8143 if (current_function_prototype_built_in)
8144 warning_at (DECL_SOURCE_LOCATION (fndecl),
8145 0, "number of arguments doesn%'t match "
8146 "built-in prototype");
8147 else
8149 /* FIXME diagnostics: This should be the location of
8150 FNDECL, but there is bug when a prototype is
8151 declared inside function context, but defined
8152 outside of it (e.g., gcc.dg/pr15698-2.c). In
8153 which case FNDECL gets the location of the
8154 prototype, not the definition. */
8155 error_at (input_location,
8156 "number of arguments doesn%'t match prototype");
8158 error_at (current_function_prototype_locus,
8159 "prototype declaration");
8161 break;
8163 /* Type for passing arg must be consistent with that
8164 declared for the arg. ISO C says we take the unqualified
8165 type for parameters declared with qualified type. */
8166 if (TREE_TYPE (parm) != error_mark_node
8167 && TREE_TYPE (type) != error_mark_node
8168 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
8169 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
8171 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
8172 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
8174 /* Adjust argument to match prototype. E.g. a previous
8175 `int foo(float);' prototype causes
8176 `int foo(x) float x; {...}' to be treated like
8177 `int foo(float x) {...}'. This is particularly
8178 useful for argument types like uid_t. */
8179 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
8181 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
8182 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
8183 && TYPE_PRECISION (TREE_TYPE (parm))
8184 < TYPE_PRECISION (integer_type_node))
8185 DECL_ARG_TYPE (parm) = integer_type_node;
8187 /* ??? Is it possible to get here with a
8188 built-in prototype or will it always have
8189 been diagnosed as conflicting with an
8190 old-style definition and discarded? */
8191 if (current_function_prototype_built_in)
8192 warning_at (DECL_SOURCE_LOCATION (parm),
8193 OPT_Wpedantic, "promoted argument %qD "
8194 "doesn%'t match built-in prototype", parm);
8195 else
8197 pedwarn (DECL_SOURCE_LOCATION (parm),
8198 OPT_Wpedantic, "promoted argument %qD "
8199 "doesn%'t match prototype", parm);
8200 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
8201 "prototype declaration");
8204 else
8206 if (current_function_prototype_built_in)
8207 warning_at (DECL_SOURCE_LOCATION (parm),
8208 0, "argument %qD doesn%'t match "
8209 "built-in prototype", parm);
8210 else
8212 error_at (DECL_SOURCE_LOCATION (parm),
8213 "argument %qD doesn%'t match prototype", parm);
8214 error_at (current_function_prototype_locus,
8215 "prototype declaration");
8220 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
8223 /* Otherwise, create a prototype that would match. */
8225 else
8227 tree actual = 0, last = 0, type;
8229 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
8231 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
8232 if (last)
8233 TREE_CHAIN (last) = type;
8234 else
8235 actual = type;
8236 last = type;
8238 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
8239 if (last)
8240 TREE_CHAIN (last) = type;
8241 else
8242 actual = type;
8244 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
8245 of the type of this function, but we need to avoid having this
8246 affect the types of other similarly-typed functions, so we must
8247 first force the generation of an identical (but separate) type
8248 node for the relevant function type. The new node we create
8249 will be a variant of the main variant of the original function
8250 type. */
8252 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
8254 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
8258 /* Store parameter declarations passed in ARG_INFO into the current
8259 function declaration. */
8261 void
8262 store_parm_decls_from (struct c_arg_info *arg_info)
8264 current_function_arg_info = arg_info;
8265 store_parm_decls ();
8268 /* Store the parameter declarations into the current function declaration.
8269 This is called after parsing the parameter declarations, before
8270 digesting the body of the function.
8272 For an old-style definition, construct a prototype out of the old-style
8273 parameter declarations and inject it into the function's type. */
8275 void
8276 store_parm_decls (void)
8278 tree fndecl = current_function_decl;
8279 bool proto;
8281 /* The argument information block for FNDECL. */
8282 struct c_arg_info *arg_info = current_function_arg_info;
8283 current_function_arg_info = 0;
8285 /* True if this definition is written with a prototype. Note:
8286 despite C99 6.7.5.3p14, we can *not* treat an empty argument
8287 list in a function definition as equivalent to (void) -- an
8288 empty argument list specifies the function has no parameters,
8289 but only (void) sets up a prototype for future calls. */
8290 proto = arg_info->types != 0;
8292 if (proto)
8293 store_parm_decls_newstyle (fndecl, arg_info);
8294 else
8295 store_parm_decls_oldstyle (fndecl, arg_info);
8297 /* The next call to push_scope will be a function body. */
8299 next_is_function_body = true;
8301 /* Write a record describing this function definition to the prototypes
8302 file (if requested). */
8304 gen_aux_info_record (fndecl, 1, 0, proto);
8306 /* Initialize the RTL code for the function. */
8307 allocate_struct_function (fndecl, false);
8309 if (warn_unused_local_typedefs)
8310 cfun->language = ggc_alloc_cleared_language_function ();
8312 /* Begin the statement tree for this function. */
8313 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
8315 /* ??? Insert the contents of the pending sizes list into the function
8316 to be evaluated. The only reason left to have this is
8317 void foo(int n, int array[n++])
8318 because we throw away the array type in favor of a pointer type, and
8319 thus won't naturally see the SAVE_EXPR containing the increment. All
8320 other pending sizes would be handled by gimplify_parameters. */
8321 if (arg_info->pending_sizes)
8322 add_stmt (arg_info->pending_sizes);
8326 /* Finish up a function declaration and compile that function
8327 all the way to assembler language output. Then free the storage
8328 for the function definition.
8330 This is called after parsing the body of the function definition. */
8332 void
8333 finish_function (void)
8335 tree fndecl = current_function_decl;
8337 if (c_dialect_objc ())
8338 objc_finish_function ();
8340 if (TREE_CODE (fndecl) == FUNCTION_DECL
8341 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
8343 tree args = DECL_ARGUMENTS (fndecl);
8344 for (; args; args = DECL_CHAIN (args))
8346 tree type = TREE_TYPE (args);
8347 if (INTEGRAL_TYPE_P (type)
8348 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8349 DECL_ARG_TYPE (args) = integer_type_node;
8353 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
8354 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8356 /* Must mark the RESULT_DECL as being in this function. */
8358 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
8359 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8361 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
8362 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
8363 == integer_type_node && flag_isoc99)
8365 /* Hack. We don't want the middle-end to warn that this return
8366 is unreachable, so we mark its location as special. Using
8367 UNKNOWN_LOCATION has the problem that it gets clobbered in
8368 annotate_one_with_locus. A cleaner solution might be to
8369 ensure ! should_carry_locus_p (stmt), but that needs a flag.
8371 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
8374 /* Tie off the statement tree for this function. */
8375 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
8377 finish_fname_decls ();
8379 /* Complain if there's just no return statement. */
8380 if (warn_return_type
8381 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
8382 && !current_function_returns_value && !current_function_returns_null
8383 /* Don't complain if we are no-return. */
8384 && !current_function_returns_abnormally
8385 /* Don't complain if we are declared noreturn. */
8386 && !TREE_THIS_VOLATILE (fndecl)
8387 /* Don't warn for main(). */
8388 && !MAIN_NAME_P (DECL_NAME (fndecl))
8389 /* Or if they didn't actually specify a return type. */
8390 && !C_FUNCTION_IMPLICIT_INT (fndecl)
8391 /* Normally, with -Wreturn-type, flow will complain, but we might
8392 optimize out static functions. */
8393 && !TREE_PUBLIC (fndecl))
8395 warning (OPT_Wreturn_type,
8396 "no return statement in function returning non-void");
8397 TREE_NO_WARNING (fndecl) = 1;
8400 /* Complain about parameters that are only set, but never otherwise used. */
8401 if (warn_unused_but_set_parameter)
8403 tree decl;
8405 for (decl = DECL_ARGUMENTS (fndecl);
8406 decl;
8407 decl = DECL_CHAIN (decl))
8408 if (TREE_USED (decl)
8409 && TREE_CODE (decl) == PARM_DECL
8410 && !DECL_READ_P (decl)
8411 && DECL_NAME (decl)
8412 && !DECL_ARTIFICIAL (decl)
8413 && !TREE_NO_WARNING (decl))
8414 warning_at (DECL_SOURCE_LOCATION (decl),
8415 OPT_Wunused_but_set_parameter,
8416 "parameter %qD set but not used", decl);
8419 /* Complain about locally defined typedefs that are not used in this
8420 function. */
8421 maybe_warn_unused_local_typedefs ();
8423 /* Store the end of the function, so that we get good line number
8424 info for the epilogue. */
8425 cfun->function_end_locus = input_location;
8427 /* Finalize the ELF visibility for the function. */
8428 c_determine_visibility (fndecl);
8430 /* For GNU C extern inline functions disregard inline limits. */
8431 if (DECL_EXTERNAL (fndecl)
8432 && DECL_DECLARED_INLINE_P (fndecl))
8433 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
8435 /* Genericize before inlining. Delay genericizing nested functions
8436 until their parent function is genericized. Since finalizing
8437 requires GENERIC, delay that as well. */
8439 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
8440 && !undef_nested_function)
8442 if (!decl_function_context (fndecl))
8444 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
8445 c_genericize (fndecl);
8447 /* ??? Objc emits functions after finalizing the compilation unit.
8448 This should be cleaned up later and this conditional removed. */
8449 if (cgraph_global_info_ready)
8451 cgraph_add_new_function (fndecl, false);
8452 return;
8454 cgraph_finalize_function (fndecl, false);
8456 else
8458 /* Register this function with cgraph just far enough to get it
8459 added to our parent's nested function list. Handy, since the
8460 C front end doesn't have such a list. */
8461 (void) cgraph_get_create_node (fndecl);
8465 if (!decl_function_context (fndecl))
8466 undef_nested_function = false;
8468 if (cfun->language != NULL)
8470 ggc_free (cfun->language);
8471 cfun->language = NULL;
8474 /* We're leaving the context of this function, so zap cfun.
8475 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
8476 tree_rest_of_compilation. */
8477 set_cfun (NULL);
8478 current_function_decl = NULL;
8481 /* Check the declarations given in a for-loop for satisfying the C99
8482 constraints. If exactly one such decl is found, return it. LOC is
8483 the location of the opening parenthesis of the for loop. The last
8484 parameter allows you to control the "for loop initial declarations
8485 are only allowed in C99 mode". Normally, you should pass
8486 flag_isoc99 as that parameter. But in some cases (Objective-C
8487 foreach loop, for example) we want to run the checks in this
8488 function even if not in C99 mode, so we allow the caller to turn
8489 off the error about not being in C99 mode.
8492 tree
8493 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
8495 struct c_binding *b;
8496 tree one_decl = NULL_TREE;
8497 int n_decls = 0;
8499 if (!turn_off_iso_c99_error)
8501 static bool hint = true;
8502 /* If we get here, declarations have been used in a for loop without
8503 the C99 for loop scope. This doesn't make much sense, so don't
8504 allow it. */
8505 error_at (loc, "%<for%> loop initial declarations "
8506 "are only allowed in C99 or C11 mode");
8507 if (hint)
8509 inform (loc,
8510 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
8511 "to compile your code");
8512 hint = false;
8514 return NULL_TREE;
8516 /* C99 subclause 6.8.5 paragraph 3:
8518 [#3] The declaration part of a for statement shall only
8519 declare identifiers for objects having storage class auto or
8520 register.
8522 It isn't clear whether, in this sentence, "identifiers" binds to
8523 "shall only declare" or to "objects" - that is, whether all identifiers
8524 declared must be identifiers for objects, or whether the restriction
8525 only applies to those that are. (A question on this in comp.std.c
8526 in November 2000 received no answer.) We implement the strictest
8527 interpretation, to avoid creating an extension which later causes
8528 problems. */
8530 for (b = current_scope->bindings; b; b = b->prev)
8532 tree id = b->id;
8533 tree decl = b->decl;
8535 if (!id)
8536 continue;
8538 switch (TREE_CODE (decl))
8540 case VAR_DECL:
8542 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
8543 if (TREE_STATIC (decl))
8544 error_at (decl_loc,
8545 "declaration of static variable %qD in %<for%> loop "
8546 "initial declaration", decl);
8547 else if (DECL_EXTERNAL (decl))
8548 error_at (decl_loc,
8549 "declaration of %<extern%> variable %qD in %<for%> loop "
8550 "initial declaration", decl);
8552 break;
8554 case RECORD_TYPE:
8555 error_at (loc,
8556 "%<struct %E%> declared in %<for%> loop initial "
8557 "declaration", id);
8558 break;
8559 case UNION_TYPE:
8560 error_at (loc,
8561 "%<union %E%> declared in %<for%> loop initial declaration",
8562 id);
8563 break;
8564 case ENUMERAL_TYPE:
8565 error_at (loc, "%<enum %E%> declared in %<for%> loop "
8566 "initial declaration", id);
8567 break;
8568 default:
8569 error_at (loc, "declaration of non-variable "
8570 "%qD in %<for%> loop initial declaration", decl);
8573 n_decls++;
8574 one_decl = decl;
8577 return n_decls == 1 ? one_decl : NULL_TREE;
8580 /* Save and reinitialize the variables
8581 used during compilation of a C function. */
8583 void
8584 c_push_function_context (void)
8586 struct language_function *p = cfun->language;
8587 /* cfun->language might have been already allocated by the use of
8588 -Wunused-local-typedefs. In that case, just re-use it. */
8589 if (p == NULL)
8590 cfun->language = p = ggc_alloc_cleared_language_function ();
8592 p->base.x_stmt_tree = c_stmt_tree;
8593 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
8594 p->x_break_label = c_break_label;
8595 p->x_cont_label = c_cont_label;
8596 p->x_switch_stack = c_switch_stack;
8597 p->arg_info = current_function_arg_info;
8598 p->returns_value = current_function_returns_value;
8599 p->returns_null = current_function_returns_null;
8600 p->returns_abnormally = current_function_returns_abnormally;
8601 p->warn_about_return_type = warn_about_return_type;
8603 push_function_context ();
8606 /* Restore the variables used during compilation of a C function. */
8608 void
8609 c_pop_function_context (void)
8611 struct language_function *p;
8613 pop_function_context ();
8614 p = cfun->language;
8616 /* When -Wunused-local-typedefs is in effect, cfun->languages is
8617 used to store data throughout the life time of the current cfun,
8618 So don't deallocate it. */
8619 if (!warn_unused_local_typedefs)
8620 cfun->language = NULL;
8622 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
8623 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
8625 /* Stop pointing to the local nodes about to be freed. */
8626 /* But DECL_INITIAL must remain nonzero so we know this
8627 was an actual function definition. */
8628 DECL_INITIAL (current_function_decl) = error_mark_node;
8629 DECL_ARGUMENTS (current_function_decl) = 0;
8632 c_stmt_tree = p->base.x_stmt_tree;
8633 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
8634 c_break_label = p->x_break_label;
8635 c_cont_label = p->x_cont_label;
8636 c_switch_stack = p->x_switch_stack;
8637 current_function_arg_info = p->arg_info;
8638 current_function_returns_value = p->returns_value;
8639 current_function_returns_null = p->returns_null;
8640 current_function_returns_abnormally = p->returns_abnormally;
8641 warn_about_return_type = p->warn_about_return_type;
8644 /* The functions below are required for functionality of doing
8645 function at once processing in the C front end. Currently these
8646 functions are not called from anywhere in the C front end, but as
8647 these changes continue, that will change. */
8649 /* Returns the stmt_tree (if any) to which statements are currently
8650 being added. If there is no active statement-tree, NULL is
8651 returned. */
8653 stmt_tree
8654 current_stmt_tree (void)
8656 return &c_stmt_tree;
8659 /* Return the global value of T as a symbol. */
8661 tree
8662 identifier_global_value (tree t)
8664 struct c_binding *b;
8666 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
8667 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
8668 return b->decl;
8670 return 0;
8673 /* In C, the only C-linkage public declaration is at file scope. */
8675 tree
8676 c_linkage_bindings (tree name)
8678 return identifier_global_value (name);
8681 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
8682 otherwise the name is found in ridpointers from RID_INDEX. */
8684 void
8685 record_builtin_type (enum rid rid_index, const char *name, tree type)
8687 tree id, decl;
8688 if (name == 0)
8689 id = ridpointers[(int) rid_index];
8690 else
8691 id = get_identifier (name);
8692 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
8693 pushdecl (decl);
8694 if (debug_hooks->type_decl)
8695 debug_hooks->type_decl (decl, false);
8698 /* Build the void_list_node (void_type_node having been created). */
8699 tree
8700 build_void_list_node (void)
8702 tree t = build_tree_list (NULL_TREE, void_type_node);
8703 return t;
8706 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
8708 struct c_parm *
8709 build_c_parm (struct c_declspecs *specs, tree attrs,
8710 struct c_declarator *declarator)
8712 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
8713 ret->specs = specs;
8714 ret->attrs = attrs;
8715 ret->declarator = declarator;
8716 return ret;
8719 /* Return a declarator with nested attributes. TARGET is the inner
8720 declarator to which these attributes apply. ATTRS are the
8721 attributes. */
8723 struct c_declarator *
8724 build_attrs_declarator (tree attrs, struct c_declarator *target)
8726 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8727 ret->kind = cdk_attrs;
8728 ret->declarator = target;
8729 ret->u.attrs = attrs;
8730 return ret;
8733 /* Return a declarator for a function with arguments specified by ARGS
8734 and return type specified by TARGET. */
8736 struct c_declarator *
8737 build_function_declarator (struct c_arg_info *args,
8738 struct c_declarator *target)
8740 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8741 ret->kind = cdk_function;
8742 ret->declarator = target;
8743 ret->u.arg_info = args;
8744 return ret;
8747 /* Return a declarator for the identifier IDENT (which may be
8748 NULL_TREE for an abstract declarator). */
8750 struct c_declarator *
8751 build_id_declarator (tree ident)
8753 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8754 ret->kind = cdk_id;
8755 ret->declarator = 0;
8756 ret->u.id = ident;
8757 /* Default value - may get reset to a more precise location. */
8758 ret->id_loc = input_location;
8759 return ret;
8762 /* Return something to represent absolute declarators containing a *.
8763 TARGET is the absolute declarator that the * contains.
8764 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
8765 to apply to the pointer type. */
8767 struct c_declarator *
8768 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
8769 struct c_declarator *target)
8771 tree attrs;
8772 int quals = 0;
8773 struct c_declarator *itarget = target;
8774 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8775 if (type_quals_attrs)
8777 attrs = type_quals_attrs->attrs;
8778 quals = quals_from_declspecs (type_quals_attrs);
8779 if (attrs != NULL_TREE)
8780 itarget = build_attrs_declarator (attrs, target);
8782 ret->kind = cdk_pointer;
8783 ret->declarator = itarget;
8784 ret->u.pointer_quals = quals;
8785 return ret;
8788 /* Return a pointer to a structure for an empty list of declaration
8789 specifiers. */
8791 struct c_declspecs *
8792 build_null_declspecs (void)
8794 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
8795 memset (&ret->locations, 0, cdw_number_of_elements);
8796 ret->type = 0;
8797 ret->expr = 0;
8798 ret->decl_attr = 0;
8799 ret->attrs = 0;
8800 ret->align_log = -1;
8801 ret->typespec_word = cts_none;
8802 ret->storage_class = csc_none;
8803 ret->expr_const_operands = true;
8804 ret->declspecs_seen_p = false;
8805 ret->typespec_kind = ctsk_none;
8806 ret->non_sc_seen_p = false;
8807 ret->typedef_p = false;
8808 ret->explicit_signed_p = false;
8809 ret->deprecated_p = false;
8810 ret->default_int_p = false;
8811 ret->long_p = false;
8812 ret->long_long_p = false;
8813 ret->short_p = false;
8814 ret->signed_p = false;
8815 ret->unsigned_p = false;
8816 ret->complex_p = false;
8817 ret->inline_p = false;
8818 ret->noreturn_p = false;
8819 ret->thread_p = false;
8820 ret->const_p = false;
8821 ret->volatile_p = false;
8822 ret->restrict_p = false;
8823 ret->saturating_p = false;
8824 ret->alignas_p = false;
8825 ret->address_space = ADDR_SPACE_GENERIC;
8826 return ret;
8829 /* Add the address space ADDRSPACE to the declaration specifiers
8830 SPECS, returning SPECS. */
8832 struct c_declspecs *
8833 declspecs_add_addrspace (source_location location,
8834 struct c_declspecs *specs, addr_space_t as)
8836 specs->non_sc_seen_p = true;
8837 specs->declspecs_seen_p = true;
8839 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
8840 && specs->address_space != as)
8841 error ("incompatible address space qualifiers %qs and %qs",
8842 c_addr_space_name (as),
8843 c_addr_space_name (specs->address_space));
8844 else
8846 specs->address_space = as;
8847 specs->locations[cdw_address_space] = location;
8849 return specs;
8852 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
8853 returning SPECS. */
8855 struct c_declspecs *
8856 declspecs_add_qual (source_location loc,
8857 struct c_declspecs *specs, tree qual)
8859 enum rid i;
8860 bool dupe = false;
8861 specs->non_sc_seen_p = true;
8862 specs->declspecs_seen_p = true;
8863 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
8864 && C_IS_RESERVED_WORD (qual));
8865 i = C_RID_CODE (qual);
8866 switch (i)
8868 case RID_CONST:
8869 dupe = specs->const_p;
8870 specs->const_p = true;
8871 specs->locations[cdw_const] = loc;
8872 break;
8873 case RID_VOLATILE:
8874 dupe = specs->volatile_p;
8875 specs->volatile_p = true;
8876 specs->locations[cdw_volatile] = loc;
8877 break;
8878 case RID_RESTRICT:
8879 dupe = specs->restrict_p;
8880 specs->restrict_p = true;
8881 specs->locations[cdw_restrict] = loc;
8882 break;
8883 default:
8884 gcc_unreachable ();
8886 if (dupe && !flag_isoc99)
8887 pedwarn (loc, OPT_Wpedantic, "duplicate %qE", qual);
8888 return specs;
8891 /* Add the type specifier TYPE to the declaration specifiers SPECS,
8892 returning SPECS. */
8894 struct c_declspecs *
8895 declspecs_add_type (location_t loc, struct c_declspecs *specs,
8896 struct c_typespec spec)
8898 tree type = spec.spec;
8899 specs->non_sc_seen_p = true;
8900 specs->declspecs_seen_p = true;
8901 specs->typespec_kind = spec.kind;
8902 if (TREE_DEPRECATED (type))
8903 specs->deprecated_p = true;
8905 /* Handle type specifier keywords. */
8906 if (TREE_CODE (type) == IDENTIFIER_NODE
8907 && C_IS_RESERVED_WORD (type)
8908 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
8910 enum rid i = C_RID_CODE (type);
8911 if (specs->type)
8913 error_at (loc, "two or more data types in declaration specifiers");
8914 return specs;
8916 if ((int) i <= (int) RID_LAST_MODIFIER)
8918 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
8919 bool dupe = false;
8920 switch (i)
8922 case RID_LONG:
8923 if (specs->long_long_p)
8925 error_at (loc, "%<long long long%> is too long for GCC");
8926 break;
8928 if (specs->long_p)
8930 if (specs->typespec_word == cts_double)
8932 error_at (loc,
8933 ("both %<long long%> and %<double%> in "
8934 "declaration specifiers"));
8935 break;
8937 pedwarn_c90 (loc, OPT_Wlong_long,
8938 "ISO C90 does not support %<long long%>");
8939 specs->long_long_p = 1;
8940 specs->locations[cdw_long_long] = loc;
8941 break;
8943 if (specs->short_p)
8944 error_at (loc,
8945 ("both %<long%> and %<short%> in "
8946 "declaration specifiers"));
8947 else if (specs->typespec_word == cts_void)
8948 error_at (loc,
8949 ("both %<long%> and %<void%> in "
8950 "declaration specifiers"));
8951 else if (specs->typespec_word == cts_int128)
8952 error_at (loc,
8953 ("both %<long%> and %<__int128%> in "
8954 "declaration specifiers"));
8955 else if (specs->typespec_word == cts_bool)
8956 error_at (loc,
8957 ("both %<long%> and %<_Bool%> in "
8958 "declaration specifiers"));
8959 else if (specs->typespec_word == cts_char)
8960 error_at (loc,
8961 ("both %<long%> and %<char%> in "
8962 "declaration specifiers"));
8963 else if (specs->typespec_word == cts_float)
8964 error_at (loc,
8965 ("both %<long%> and %<float%> in "
8966 "declaration specifiers"));
8967 else if (specs->typespec_word == cts_dfloat32)
8968 error_at (loc,
8969 ("both %<long%> and %<_Decimal32%> in "
8970 "declaration specifiers"));
8971 else if (specs->typespec_word == cts_dfloat64)
8972 error_at (loc,
8973 ("both %<long%> and %<_Decimal64%> in "
8974 "declaration specifiers"));
8975 else if (specs->typespec_word == cts_dfloat128)
8976 error_at (loc,
8977 ("both %<long%> and %<_Decimal128%> in "
8978 "declaration specifiers"));
8979 else
8981 specs->long_p = true;
8982 specs->locations[cdw_long] = loc;
8984 break;
8985 case RID_SHORT:
8986 dupe = specs->short_p;
8987 if (specs->long_p)
8988 error_at (loc,
8989 ("both %<long%> and %<short%> in "
8990 "declaration specifiers"));
8991 else if (specs->typespec_word == cts_void)
8992 error_at (loc,
8993 ("both %<short%> and %<void%> in "
8994 "declaration specifiers"));
8995 else if (specs->typespec_word == cts_int128)
8996 error_at (loc,
8997 ("both %<short%> and %<__int128%> in "
8998 "declaration specifiers"));
8999 else if (specs->typespec_word == cts_bool)
9000 error_at (loc,
9001 ("both %<short%> and %<_Bool%> in "
9002 "declaration specifiers"));
9003 else if (specs->typespec_word == cts_char)
9004 error_at (loc,
9005 ("both %<short%> and %<char%> in "
9006 "declaration specifiers"));
9007 else if (specs->typespec_word == cts_float)
9008 error_at (loc,
9009 ("both %<short%> and %<float%> in "
9010 "declaration specifiers"));
9011 else if (specs->typespec_word == cts_double)
9012 error_at (loc,
9013 ("both %<short%> and %<double%> in "
9014 "declaration specifiers"));
9015 else if (specs->typespec_word == cts_dfloat32)
9016 error_at (loc,
9017 ("both %<short%> and %<_Decimal32%> in "
9018 "declaration specifiers"));
9019 else if (specs->typespec_word == cts_dfloat64)
9020 error_at (loc,
9021 ("both %<short%> and %<_Decimal64%> in "
9022 "declaration specifiers"));
9023 else if (specs->typespec_word == cts_dfloat128)
9024 error_at (loc,
9025 ("both %<short%> and %<_Decimal128%> in "
9026 "declaration specifiers"));
9027 else
9029 specs->short_p = true;
9030 specs->locations[cdw_short] = loc;
9032 break;
9033 case RID_SIGNED:
9034 dupe = specs->signed_p;
9035 if (specs->unsigned_p)
9036 error_at (loc,
9037 ("both %<signed%> and %<unsigned%> in "
9038 "declaration specifiers"));
9039 else if (specs->typespec_word == cts_void)
9040 error_at (loc,
9041 ("both %<signed%> and %<void%> in "
9042 "declaration specifiers"));
9043 else if (specs->typespec_word == cts_bool)
9044 error_at (loc,
9045 ("both %<signed%> and %<_Bool%> in "
9046 "declaration specifiers"));
9047 else if (specs->typespec_word == cts_float)
9048 error_at (loc,
9049 ("both %<signed%> and %<float%> in "
9050 "declaration specifiers"));
9051 else if (specs->typespec_word == cts_double)
9052 error_at (loc,
9053 ("both %<signed%> and %<double%> in "
9054 "declaration specifiers"));
9055 else if (specs->typespec_word == cts_dfloat32)
9056 error_at (loc,
9057 ("both %<signed%> and %<_Decimal32%> in "
9058 "declaration specifiers"));
9059 else if (specs->typespec_word == cts_dfloat64)
9060 error_at (loc,
9061 ("both %<signed%> and %<_Decimal64%> in "
9062 "declaration specifiers"));
9063 else if (specs->typespec_word == cts_dfloat128)
9064 error_at (loc,
9065 ("both %<signed%> and %<_Decimal128%> in "
9066 "declaration specifiers"));
9067 else
9069 specs->signed_p = true;
9070 specs->locations[cdw_signed] = loc;
9072 break;
9073 case RID_UNSIGNED:
9074 dupe = specs->unsigned_p;
9075 if (specs->signed_p)
9076 error_at (loc,
9077 ("both %<signed%> and %<unsigned%> in "
9078 "declaration specifiers"));
9079 else if (specs->typespec_word == cts_void)
9080 error_at (loc,
9081 ("both %<unsigned%> and %<void%> in "
9082 "declaration specifiers"));
9083 else if (specs->typespec_word == cts_bool)
9084 error_at (loc,
9085 ("both %<unsigned%> and %<_Bool%> in "
9086 "declaration specifiers"));
9087 else if (specs->typespec_word == cts_float)
9088 error_at (loc,
9089 ("both %<unsigned%> and %<float%> in "
9090 "declaration specifiers"));
9091 else if (specs->typespec_word == cts_double)
9092 error_at (loc,
9093 ("both %<unsigned%> and %<double%> in "
9094 "declaration specifiers"));
9095 else if (specs->typespec_word == cts_dfloat32)
9096 error_at (loc,
9097 ("both %<unsigned%> and %<_Decimal32%> in "
9098 "declaration specifiers"));
9099 else if (specs->typespec_word == cts_dfloat64)
9100 error_at (loc,
9101 ("both %<unsigned%> and %<_Decimal64%> in "
9102 "declaration specifiers"));
9103 else if (specs->typespec_word == cts_dfloat128)
9104 error_at (loc,
9105 ("both %<unsigned%> and %<_Decimal128%> in "
9106 "declaration specifiers"));
9107 else
9109 specs->unsigned_p = true;
9110 specs->locations[cdw_unsigned] = loc;
9112 break;
9113 case RID_COMPLEX:
9114 dupe = specs->complex_p;
9115 if (!flag_isoc99 && !in_system_header_at (loc))
9116 pedwarn (loc, OPT_Wpedantic,
9117 "ISO C90 does not support complex types");
9118 if (specs->typespec_word == cts_void)
9119 error_at (loc,
9120 ("both %<complex%> and %<void%> in "
9121 "declaration specifiers"));
9122 else if (specs->typespec_word == cts_bool)
9123 error_at (loc,
9124 ("both %<complex%> and %<_Bool%> in "
9125 "declaration specifiers"));
9126 else if (specs->typespec_word == cts_dfloat32)
9127 error_at (loc,
9128 ("both %<complex%> and %<_Decimal32%> in "
9129 "declaration specifiers"));
9130 else if (specs->typespec_word == cts_dfloat64)
9131 error_at (loc,
9132 ("both %<complex%> and %<_Decimal64%> in "
9133 "declaration specifiers"));
9134 else if (specs->typespec_word == cts_dfloat128)
9135 error_at (loc,
9136 ("both %<complex%> and %<_Decimal128%> in "
9137 "declaration specifiers"));
9138 else if (specs->typespec_word == cts_fract)
9139 error_at (loc,
9140 ("both %<complex%> and %<_Fract%> in "
9141 "declaration specifiers"));
9142 else if (specs->typespec_word == cts_accum)
9143 error_at (loc,
9144 ("both %<complex%> and %<_Accum%> in "
9145 "declaration specifiers"));
9146 else if (specs->saturating_p)
9147 error_at (loc,
9148 ("both %<complex%> and %<_Sat%> in "
9149 "declaration specifiers"));
9150 else
9152 specs->complex_p = true;
9153 specs->locations[cdw_complex] = loc;
9155 break;
9156 case RID_SAT:
9157 dupe = specs->saturating_p;
9158 pedwarn (loc, OPT_Wpedantic,
9159 "ISO C does not support saturating types");
9160 if (specs->typespec_word == cts_int128)
9162 error_at (loc,
9163 ("both %<_Sat%> and %<__int128%> in "
9164 "declaration specifiers"));
9166 else if (specs->typespec_word == cts_void)
9167 error_at (loc,
9168 ("both %<_Sat%> and %<void%> in "
9169 "declaration specifiers"));
9170 else if (specs->typespec_word == cts_bool)
9171 error_at (loc,
9172 ("both %<_Sat%> and %<_Bool%> in "
9173 "declaration specifiers"));
9174 else if (specs->typespec_word == cts_char)
9175 error_at (loc,
9176 ("both %<_Sat%> and %<char%> in "
9177 "declaration specifiers"));
9178 else if (specs->typespec_word == cts_int)
9179 error_at (loc,
9180 ("both %<_Sat%> and %<int%> in "
9181 "declaration specifiers"));
9182 else if (specs->typespec_word == cts_float)
9183 error_at (loc,
9184 ("both %<_Sat%> and %<float%> in "
9185 "declaration specifiers"));
9186 else if (specs->typespec_word == cts_double)
9187 error_at (loc,
9188 ("both %<_Sat%> and %<double%> in "
9189 "declaration specifiers"));
9190 else if (specs->typespec_word == cts_dfloat32)
9191 error_at (loc,
9192 ("both %<_Sat%> and %<_Decimal32%> in "
9193 "declaration specifiers"));
9194 else if (specs->typespec_word == cts_dfloat64)
9195 error_at (loc,
9196 ("both %<_Sat%> and %<_Decimal64%> in "
9197 "declaration specifiers"));
9198 else if (specs->typespec_word == cts_dfloat128)
9199 error_at (loc,
9200 ("both %<_Sat%> and %<_Decimal128%> in "
9201 "declaration specifiers"));
9202 else if (specs->complex_p)
9203 error_at (loc,
9204 ("both %<_Sat%> and %<complex%> in "
9205 "declaration specifiers"));
9206 else
9208 specs->saturating_p = true;
9209 specs->locations[cdw_saturating] = loc;
9211 break;
9212 default:
9213 gcc_unreachable ();
9216 if (dupe)
9217 error_at (loc, "duplicate %qE", type);
9219 return specs;
9221 else
9223 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
9224 "__int128", "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
9225 if (specs->typespec_word != cts_none)
9227 error_at (loc,
9228 "two or more data types in declaration specifiers");
9229 return specs;
9231 switch (i)
9233 case RID_INT128:
9234 if (int128_integer_type_node == NULL_TREE)
9236 error_at (loc, "%<__int128%> is not supported for this target");
9237 return specs;
9239 if (!in_system_header)
9240 pedwarn (loc, OPT_Wpedantic,
9241 "ISO C does not support %<__int128%> type");
9243 if (specs->long_p)
9244 error_at (loc,
9245 ("both %<__int128%> and %<long%> in "
9246 "declaration specifiers"));
9247 else if (specs->saturating_p)
9248 error_at (loc,
9249 ("both %<_Sat%> and %<__int128%> in "
9250 "declaration specifiers"));
9251 else if (specs->short_p)
9252 error_at (loc,
9253 ("both %<__int128%> and %<short%> in "
9254 "declaration specifiers"));
9255 else
9257 specs->typespec_word = cts_int128;
9258 specs->locations[cdw_typespec] = loc;
9260 return specs;
9261 case RID_VOID:
9262 if (specs->long_p)
9263 error_at (loc,
9264 ("both %<long%> and %<void%> in "
9265 "declaration specifiers"));
9266 else if (specs->short_p)
9267 error_at (loc,
9268 ("both %<short%> and %<void%> in "
9269 "declaration specifiers"));
9270 else if (specs->signed_p)
9271 error_at (loc,
9272 ("both %<signed%> and %<void%> in "
9273 "declaration specifiers"));
9274 else if (specs->unsigned_p)
9275 error_at (loc,
9276 ("both %<unsigned%> and %<void%> in "
9277 "declaration specifiers"));
9278 else if (specs->complex_p)
9279 error_at (loc,
9280 ("both %<complex%> and %<void%> in "
9281 "declaration specifiers"));
9282 else if (specs->saturating_p)
9283 error_at (loc,
9284 ("both %<_Sat%> and %<void%> in "
9285 "declaration specifiers"));
9286 else
9288 specs->typespec_word = cts_void;
9289 specs->locations[cdw_typespec] = loc;
9291 return specs;
9292 case RID_BOOL:
9293 if (specs->long_p)
9294 error_at (loc,
9295 ("both %<long%> and %<_Bool%> in "
9296 "declaration specifiers"));
9297 else if (specs->short_p)
9298 error_at (loc,
9299 ("both %<short%> and %<_Bool%> in "
9300 "declaration specifiers"));
9301 else if (specs->signed_p)
9302 error_at (loc,
9303 ("both %<signed%> and %<_Bool%> in "
9304 "declaration specifiers"));
9305 else if (specs->unsigned_p)
9306 error_at (loc,
9307 ("both %<unsigned%> and %<_Bool%> in "
9308 "declaration specifiers"));
9309 else if (specs->complex_p)
9310 error_at (loc,
9311 ("both %<complex%> and %<_Bool%> in "
9312 "declaration specifiers"));
9313 else if (specs->saturating_p)
9314 error_at (loc,
9315 ("both %<_Sat%> and %<_Bool%> in "
9316 "declaration specifiers"));
9317 else
9319 specs->typespec_word = cts_bool;
9320 specs->locations[cdw_typespec] = loc;
9322 return specs;
9323 case RID_CHAR:
9324 if (specs->long_p)
9325 error_at (loc,
9326 ("both %<long%> and %<char%> in "
9327 "declaration specifiers"));
9328 else if (specs->short_p)
9329 error_at (loc,
9330 ("both %<short%> and %<char%> in "
9331 "declaration specifiers"));
9332 else if (specs->saturating_p)
9333 error_at (loc,
9334 ("both %<_Sat%> and %<char%> in "
9335 "declaration specifiers"));
9336 else
9338 specs->typespec_word = cts_char;
9339 specs->locations[cdw_typespec] = loc;
9341 return specs;
9342 case RID_INT:
9343 if (specs->saturating_p)
9344 error_at (loc,
9345 ("both %<_Sat%> and %<int%> in "
9346 "declaration specifiers"));
9347 else
9349 specs->typespec_word = cts_int;
9350 specs->locations[cdw_typespec] = loc;
9352 return specs;
9353 case RID_FLOAT:
9354 if (specs->long_p)
9355 error_at (loc,
9356 ("both %<long%> and %<float%> in "
9357 "declaration specifiers"));
9358 else if (specs->short_p)
9359 error_at (loc,
9360 ("both %<short%> and %<float%> in "
9361 "declaration specifiers"));
9362 else if (specs->signed_p)
9363 error_at (loc,
9364 ("both %<signed%> and %<float%> in "
9365 "declaration specifiers"));
9366 else if (specs->unsigned_p)
9367 error_at (loc,
9368 ("both %<unsigned%> and %<float%> in "
9369 "declaration specifiers"));
9370 else if (specs->saturating_p)
9371 error_at (loc,
9372 ("both %<_Sat%> and %<float%> in "
9373 "declaration specifiers"));
9374 else
9376 specs->typespec_word = cts_float;
9377 specs->locations[cdw_typespec] = loc;
9379 return specs;
9380 case RID_DOUBLE:
9381 if (specs->long_long_p)
9382 error_at (loc,
9383 ("both %<long long%> and %<double%> in "
9384 "declaration specifiers"));
9385 else if (specs->short_p)
9386 error_at (loc,
9387 ("both %<short%> and %<double%> in "
9388 "declaration specifiers"));
9389 else if (specs->signed_p)
9390 error_at (loc,
9391 ("both %<signed%> and %<double%> in "
9392 "declaration specifiers"));
9393 else if (specs->unsigned_p)
9394 error_at (loc,
9395 ("both %<unsigned%> and %<double%> in "
9396 "declaration specifiers"));
9397 else if (specs->saturating_p)
9398 error_at (loc,
9399 ("both %<_Sat%> and %<double%> in "
9400 "declaration specifiers"));
9401 else
9403 specs->typespec_word = cts_double;
9404 specs->locations[cdw_typespec] = loc;
9406 return specs;
9407 case RID_DFLOAT32:
9408 case RID_DFLOAT64:
9409 case RID_DFLOAT128:
9411 const char *str;
9412 if (i == RID_DFLOAT32)
9413 str = "_Decimal32";
9414 else if (i == RID_DFLOAT64)
9415 str = "_Decimal64";
9416 else
9417 str = "_Decimal128";
9418 if (specs->long_long_p)
9419 error_at (loc,
9420 ("both %<long long%> and %<%s%> in "
9421 "declaration specifiers"),
9422 str);
9423 if (specs->long_p)
9424 error_at (loc,
9425 ("both %<long%> and %<%s%> in "
9426 "declaration specifiers"),
9427 str);
9428 else if (specs->short_p)
9429 error_at (loc,
9430 ("both %<short%> and %<%s%> in "
9431 "declaration specifiers"),
9432 str);
9433 else if (specs->signed_p)
9434 error_at (loc,
9435 ("both %<signed%> and %<%s%> in "
9436 "declaration specifiers"),
9437 str);
9438 else if (specs->unsigned_p)
9439 error_at (loc,
9440 ("both %<unsigned%> and %<%s%> in "
9441 "declaration specifiers"),
9442 str);
9443 else if (specs->complex_p)
9444 error_at (loc,
9445 ("both %<complex%> and %<%s%> in "
9446 "declaration specifiers"),
9447 str);
9448 else if (specs->saturating_p)
9449 error_at (loc,
9450 ("both %<_Sat%> and %<%s%> in "
9451 "declaration specifiers"),
9452 str);
9453 else if (i == RID_DFLOAT32)
9454 specs->typespec_word = cts_dfloat32;
9455 else if (i == RID_DFLOAT64)
9456 specs->typespec_word = cts_dfloat64;
9457 else
9458 specs->typespec_word = cts_dfloat128;
9459 specs->locations[cdw_typespec] = loc;
9461 if (!targetm.decimal_float_supported_p ())
9462 error_at (loc,
9463 ("decimal floating point not supported "
9464 "for this target"));
9465 pedwarn (loc, OPT_Wpedantic,
9466 "ISO C does not support decimal floating point");
9467 return specs;
9468 case RID_FRACT:
9469 case RID_ACCUM:
9471 const char *str;
9472 if (i == RID_FRACT)
9473 str = "_Fract";
9474 else
9475 str = "_Accum";
9476 if (specs->complex_p)
9477 error_at (loc,
9478 ("both %<complex%> and %<%s%> in "
9479 "declaration specifiers"),
9480 str);
9481 else if (i == RID_FRACT)
9482 specs->typespec_word = cts_fract;
9483 else
9484 specs->typespec_word = cts_accum;
9485 specs->locations[cdw_typespec] = loc;
9487 if (!targetm.fixed_point_supported_p ())
9488 error_at (loc,
9489 "fixed-point types not supported for this target");
9490 pedwarn (loc, OPT_Wpedantic,
9491 "ISO C does not support fixed-point types");
9492 return specs;
9493 default:
9494 /* ObjC reserved word "id", handled below. */
9495 break;
9500 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
9501 form of ObjC type, cases such as "int" and "long" being handled
9502 above), a TYPE (struct, union, enum and typeof specifiers) or an
9503 ERROR_MARK. In none of these cases may there have previously
9504 been any type specifiers. */
9505 if (specs->type || specs->typespec_word != cts_none
9506 || specs->long_p || specs->short_p || specs->signed_p
9507 || specs->unsigned_p || specs->complex_p)
9508 error_at (loc, "two or more data types in declaration specifiers");
9509 else if (TREE_CODE (type) == TYPE_DECL)
9511 if (TREE_TYPE (type) == error_mark_node)
9512 ; /* Allow the type to default to int to avoid cascading errors. */
9513 else
9515 specs->type = TREE_TYPE (type);
9516 specs->decl_attr = DECL_ATTRIBUTES (type);
9517 specs->typedef_p = true;
9518 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
9519 specs->locations[cdw_typedef] = loc;
9521 /* If this typedef name is defined in a struct, then a C++
9522 lookup would return a different value. */
9523 if (warn_cxx_compat
9524 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
9525 warning_at (loc, OPT_Wc___compat,
9526 "C++ lookup of %qD would return a field, not a type",
9527 type);
9529 /* If we are parsing a struct, record that a struct field
9530 used a typedef. */
9531 if (warn_cxx_compat && struct_parse_info != NULL)
9532 struct_parse_info->typedefs_seen.safe_push (type);
9535 else if (TREE_CODE (type) == IDENTIFIER_NODE)
9537 tree t = lookup_name (type);
9538 if (!t || TREE_CODE (t) != TYPE_DECL)
9539 error_at (loc, "%qE fails to be a typedef or built in type", type);
9540 else if (TREE_TYPE (t) == error_mark_node)
9542 else
9544 specs->type = TREE_TYPE (t);
9545 specs->locations[cdw_typespec] = loc;
9548 else
9550 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
9552 specs->typedef_p = true;
9553 specs->locations[cdw_typedef] = loc;
9554 if (spec.expr)
9556 if (specs->expr)
9557 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
9558 specs->expr, spec.expr);
9559 else
9560 specs->expr = spec.expr;
9561 specs->expr_const_operands &= spec.expr_const_operands;
9564 specs->type = type;
9567 return specs;
9570 /* Add the storage class specifier or function specifier SCSPEC to the
9571 declaration specifiers SPECS, returning SPECS. */
9573 struct c_declspecs *
9574 declspecs_add_scspec (source_location loc,
9575 struct c_declspecs *specs,
9576 tree scspec)
9578 enum rid i;
9579 enum c_storage_class n = csc_none;
9580 bool dupe = false;
9581 specs->declspecs_seen_p = true;
9582 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
9583 && C_IS_RESERVED_WORD (scspec));
9584 i = C_RID_CODE (scspec);
9585 if (specs->non_sc_seen_p)
9586 warning (OPT_Wold_style_declaration,
9587 "%qE is not at beginning of declaration", scspec);
9588 switch (i)
9590 case RID_INLINE:
9591 /* C99 permits duplicate inline. Although of doubtful utility,
9592 it seems simplest to permit it in gnu89 mode as well, as
9593 there is also little utility in maintaining this as a
9594 difference between gnu89 and C99 inline. */
9595 dupe = false;
9596 specs->inline_p = true;
9597 specs->locations[cdw_inline] = loc;
9598 break;
9599 case RID_NORETURN:
9600 /* Duplicate _Noreturn is permitted. */
9601 dupe = false;
9602 specs->noreturn_p = true;
9603 specs->locations[cdw_noreturn] = loc;
9604 break;
9605 case RID_THREAD:
9606 dupe = specs->thread_p;
9607 if (specs->storage_class == csc_auto)
9608 error ("%<__thread%> used with %<auto%>");
9609 else if (specs->storage_class == csc_register)
9610 error ("%<__thread%> used with %<register%>");
9611 else if (specs->storage_class == csc_typedef)
9612 error ("%<__thread%> used with %<typedef%>");
9613 else
9615 specs->thread_p = true;
9616 specs->locations[cdw_thread] = loc;
9618 break;
9619 case RID_AUTO:
9620 n = csc_auto;
9621 break;
9622 case RID_EXTERN:
9623 n = csc_extern;
9624 /* Diagnose "__thread extern". */
9625 if (specs->thread_p)
9626 error ("%<__thread%> before %<extern%>");
9627 break;
9628 case RID_REGISTER:
9629 n = csc_register;
9630 break;
9631 case RID_STATIC:
9632 n = csc_static;
9633 /* Diagnose "__thread static". */
9634 if (specs->thread_p)
9635 error ("%<__thread%> before %<static%>");
9636 break;
9637 case RID_TYPEDEF:
9638 n = csc_typedef;
9639 break;
9640 default:
9641 gcc_unreachable ();
9643 if (n != csc_none && n == specs->storage_class)
9644 dupe = true;
9645 if (dupe)
9646 error ("duplicate %qE", scspec);
9647 if (n != csc_none)
9649 if (specs->storage_class != csc_none && n != specs->storage_class)
9651 error ("multiple storage classes in declaration specifiers");
9653 else
9655 specs->storage_class = n;
9656 specs->locations[cdw_storage_class] = loc;
9657 if (n != csc_extern && n != csc_static && specs->thread_p)
9659 error ("%<__thread%> used with %qE", scspec);
9660 specs->thread_p = false;
9664 return specs;
9667 /* Add the attributes ATTRS to the declaration specifiers SPECS,
9668 returning SPECS. */
9670 struct c_declspecs *
9671 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
9673 specs->attrs = chainon (attrs, specs->attrs);
9674 specs->locations[cdw_attributes] = loc;
9675 specs->declspecs_seen_p = true;
9676 return specs;
9679 /* Add an _Alignas specifier (expression ALIGN, or type whose
9680 alignment is ALIGN) to the declaration specifiers SPECS, returning
9681 SPECS. */
9682 struct c_declspecs *
9683 declspecs_add_alignas (source_location loc,
9684 struct c_declspecs *specs, tree align)
9686 int align_log;
9687 specs->alignas_p = true;
9688 specs->locations[cdw_alignas] = loc;
9689 if (align == error_mark_node)
9690 return specs;
9691 align_log = check_user_alignment (align, true);
9692 if (align_log > specs->align_log)
9693 specs->align_log = align_log;
9694 return specs;
9697 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
9698 specifiers with any other type specifier to determine the resulting
9699 type. This is where ISO C checks on complex types are made, since
9700 "_Complex long" is a prefix of the valid ISO C type "_Complex long
9701 double". */
9703 struct c_declspecs *
9704 finish_declspecs (struct c_declspecs *specs)
9706 /* If a type was specified as a whole, we have no modifiers and are
9707 done. */
9708 if (specs->type != NULL_TREE)
9710 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9711 && !specs->signed_p && !specs->unsigned_p
9712 && !specs->complex_p);
9714 /* Set a dummy type. */
9715 if (TREE_CODE (specs->type) == ERROR_MARK)
9716 specs->type = integer_type_node;
9717 return specs;
9720 /* If none of "void", "_Bool", "char", "int", "float" or "double"
9721 has been specified, treat it as "int" unless "_Complex" is
9722 present and there are no other specifiers. If we just have
9723 "_Complex", it is equivalent to "_Complex double", but e.g.
9724 "_Complex short" is equivalent to "_Complex short int". */
9725 if (specs->typespec_word == cts_none)
9727 if (specs->saturating_p)
9729 error_at (specs->locations[cdw_saturating],
9730 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
9731 if (!targetm.fixed_point_supported_p ())
9732 error_at (specs->locations[cdw_saturating],
9733 "fixed-point types not supported for this target");
9734 specs->typespec_word = cts_fract;
9736 else if (specs->long_p || specs->short_p
9737 || specs->signed_p || specs->unsigned_p)
9739 specs->typespec_word = cts_int;
9741 else if (specs->complex_p)
9743 specs->typespec_word = cts_double;
9744 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
9745 "ISO C does not support plain %<complex%> meaning "
9746 "%<double complex%>");
9748 else
9750 specs->typespec_word = cts_int;
9751 specs->default_int_p = true;
9752 /* We don't diagnose this here because grokdeclarator will
9753 give more specific diagnostics according to whether it is
9754 a function definition. */
9758 /* If "signed" was specified, record this to distinguish "int" and
9759 "signed int" in the case of a bit-field with
9760 -funsigned-bitfields. */
9761 specs->explicit_signed_p = specs->signed_p;
9763 /* Now compute the actual type. */
9764 switch (specs->typespec_word)
9766 case cts_void:
9767 gcc_assert (!specs->long_p && !specs->short_p
9768 && !specs->signed_p && !specs->unsigned_p
9769 && !specs->complex_p);
9770 specs->type = void_type_node;
9771 break;
9772 case cts_bool:
9773 gcc_assert (!specs->long_p && !specs->short_p
9774 && !specs->signed_p && !specs->unsigned_p
9775 && !specs->complex_p);
9776 specs->type = boolean_type_node;
9777 break;
9778 case cts_char:
9779 gcc_assert (!specs->long_p && !specs->short_p);
9780 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9781 if (specs->signed_p)
9782 specs->type = signed_char_type_node;
9783 else if (specs->unsigned_p)
9784 specs->type = unsigned_char_type_node;
9785 else
9786 specs->type = char_type_node;
9787 if (specs->complex_p)
9789 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
9790 "ISO C does not support complex integer types");
9791 specs->type = build_complex_type (specs->type);
9793 break;
9794 case cts_int128:
9795 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
9796 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9797 specs->type = (specs->unsigned_p
9798 ? int128_unsigned_type_node
9799 : int128_integer_type_node);
9800 if (specs->complex_p)
9802 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
9803 "ISO C does not support complex integer types");
9804 specs->type = build_complex_type (specs->type);
9806 break;
9807 case cts_int:
9808 gcc_assert (!(specs->long_p && specs->short_p));
9809 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9810 if (specs->long_long_p)
9811 specs->type = (specs->unsigned_p
9812 ? long_long_unsigned_type_node
9813 : long_long_integer_type_node);
9814 else if (specs->long_p)
9815 specs->type = (specs->unsigned_p
9816 ? long_unsigned_type_node
9817 : long_integer_type_node);
9818 else if (specs->short_p)
9819 specs->type = (specs->unsigned_p
9820 ? short_unsigned_type_node
9821 : short_integer_type_node);
9822 else
9823 specs->type = (specs->unsigned_p
9824 ? unsigned_type_node
9825 : integer_type_node);
9826 if (specs->complex_p)
9828 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
9829 "ISO C does not support complex integer types");
9830 specs->type = build_complex_type (specs->type);
9832 break;
9833 case cts_float:
9834 gcc_assert (!specs->long_p && !specs->short_p
9835 && !specs->signed_p && !specs->unsigned_p);
9836 specs->type = (specs->complex_p
9837 ? complex_float_type_node
9838 : float_type_node);
9839 break;
9840 case cts_double:
9841 gcc_assert (!specs->long_long_p && !specs->short_p
9842 && !specs->signed_p && !specs->unsigned_p);
9843 if (specs->long_p)
9845 specs->type = (specs->complex_p
9846 ? complex_long_double_type_node
9847 : long_double_type_node);
9849 else
9851 specs->type = (specs->complex_p
9852 ? complex_double_type_node
9853 : double_type_node);
9855 break;
9856 case cts_dfloat32:
9857 case cts_dfloat64:
9858 case cts_dfloat128:
9859 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9860 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
9861 if (specs->typespec_word == cts_dfloat32)
9862 specs->type = dfloat32_type_node;
9863 else if (specs->typespec_word == cts_dfloat64)
9864 specs->type = dfloat64_type_node;
9865 else
9866 specs->type = dfloat128_type_node;
9867 break;
9868 case cts_fract:
9869 gcc_assert (!specs->complex_p);
9870 if (!targetm.fixed_point_supported_p ())
9871 specs->type = integer_type_node;
9872 else if (specs->saturating_p)
9874 if (specs->long_long_p)
9875 specs->type = specs->unsigned_p
9876 ? sat_unsigned_long_long_fract_type_node
9877 : sat_long_long_fract_type_node;
9878 else if (specs->long_p)
9879 specs->type = specs->unsigned_p
9880 ? sat_unsigned_long_fract_type_node
9881 : sat_long_fract_type_node;
9882 else if (specs->short_p)
9883 specs->type = specs->unsigned_p
9884 ? sat_unsigned_short_fract_type_node
9885 : sat_short_fract_type_node;
9886 else
9887 specs->type = specs->unsigned_p
9888 ? sat_unsigned_fract_type_node
9889 : sat_fract_type_node;
9891 else
9893 if (specs->long_long_p)
9894 specs->type = specs->unsigned_p
9895 ? unsigned_long_long_fract_type_node
9896 : long_long_fract_type_node;
9897 else if (specs->long_p)
9898 specs->type = specs->unsigned_p
9899 ? unsigned_long_fract_type_node
9900 : long_fract_type_node;
9901 else if (specs->short_p)
9902 specs->type = specs->unsigned_p
9903 ? unsigned_short_fract_type_node
9904 : short_fract_type_node;
9905 else
9906 specs->type = specs->unsigned_p
9907 ? unsigned_fract_type_node
9908 : fract_type_node;
9910 break;
9911 case cts_accum:
9912 gcc_assert (!specs->complex_p);
9913 if (!targetm.fixed_point_supported_p ())
9914 specs->type = integer_type_node;
9915 else if (specs->saturating_p)
9917 if (specs->long_long_p)
9918 specs->type = specs->unsigned_p
9919 ? sat_unsigned_long_long_accum_type_node
9920 : sat_long_long_accum_type_node;
9921 else if (specs->long_p)
9922 specs->type = specs->unsigned_p
9923 ? sat_unsigned_long_accum_type_node
9924 : sat_long_accum_type_node;
9925 else if (specs->short_p)
9926 specs->type = specs->unsigned_p
9927 ? sat_unsigned_short_accum_type_node
9928 : sat_short_accum_type_node;
9929 else
9930 specs->type = specs->unsigned_p
9931 ? sat_unsigned_accum_type_node
9932 : sat_accum_type_node;
9934 else
9936 if (specs->long_long_p)
9937 specs->type = specs->unsigned_p
9938 ? unsigned_long_long_accum_type_node
9939 : long_long_accum_type_node;
9940 else if (specs->long_p)
9941 specs->type = specs->unsigned_p
9942 ? unsigned_long_accum_type_node
9943 : long_accum_type_node;
9944 else if (specs->short_p)
9945 specs->type = specs->unsigned_p
9946 ? unsigned_short_accum_type_node
9947 : short_accum_type_node;
9948 else
9949 specs->type = specs->unsigned_p
9950 ? unsigned_accum_type_node
9951 : accum_type_node;
9953 break;
9954 default:
9955 gcc_unreachable ();
9958 return specs;
9961 /* A subroutine of c_write_global_declarations. Perform final processing
9962 on one file scope's declarations (or the external scope's declarations),
9963 GLOBALS. */
9965 static void
9966 c_write_global_declarations_1 (tree globals)
9968 tree decl;
9969 bool reconsider;
9971 /* Process the decls in the order they were written. */
9972 for (decl = globals; decl; decl = DECL_CHAIN (decl))
9974 /* Check for used but undefined static functions using the C
9975 standard's definition of "used", and set TREE_NO_WARNING so
9976 that check_global_declarations doesn't repeat the check. */
9977 if (TREE_CODE (decl) == FUNCTION_DECL
9978 && DECL_INITIAL (decl) == 0
9979 && DECL_EXTERNAL (decl)
9980 && !TREE_PUBLIC (decl)
9981 && C_DECL_USED (decl))
9983 pedwarn (input_location, 0, "%q+F used but never defined", decl);
9984 TREE_NO_WARNING (decl) = 1;
9987 wrapup_global_declaration_1 (decl);
9992 reconsider = false;
9993 for (decl = globals; decl; decl = DECL_CHAIN (decl))
9994 reconsider |= wrapup_global_declaration_2 (decl);
9996 while (reconsider);
9998 for (decl = globals; decl; decl = DECL_CHAIN (decl))
9999 check_global_declaration_1 (decl);
10002 /* A subroutine of c_write_global_declarations Emit debug information for each
10003 of the declarations in GLOBALS. */
10005 static void
10006 c_write_global_declarations_2 (tree globals)
10008 tree decl;
10010 for (decl = globals; decl ; decl = DECL_CHAIN (decl))
10011 debug_hooks->global_decl (decl);
10014 /* Callback to collect a source_ref from a DECL. */
10016 static void
10017 collect_source_ref_cb (tree decl)
10019 if (!DECL_IS_BUILTIN (decl))
10020 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
10023 /* Preserve the external declarations scope across a garbage collect. */
10024 static GTY(()) tree ext_block;
10026 /* Collect all references relevant to SOURCE_FILE. */
10028 static void
10029 collect_all_refs (const char *source_file)
10031 tree t;
10032 unsigned i;
10034 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10035 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
10037 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
10040 /* Iterate over all global declarations and call CALLBACK. */
10042 static void
10043 for_each_global_decl (void (*callback) (tree decl))
10045 tree t;
10046 tree decls;
10047 tree decl;
10048 unsigned i;
10050 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10052 decls = DECL_INITIAL (t);
10053 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
10054 callback (decl);
10057 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
10058 callback (decl);
10061 void
10062 c_write_global_declarations (void)
10064 tree t;
10065 unsigned i;
10067 /* We don't want to do this if generating a PCH. */
10068 if (pch_file)
10069 return;
10071 timevar_start (TV_PHASE_DEFERRED);
10073 /* Do the Objective-C stuff. This is where all the Objective-C
10074 module stuff gets generated (symtab, class/protocol/selector
10075 lists etc). */
10076 if (c_dialect_objc ())
10077 objc_write_global_declarations ();
10079 /* Close the external scope. */
10080 ext_block = pop_scope ();
10081 external_scope = 0;
10082 gcc_assert (!current_scope);
10084 /* Handle -fdump-ada-spec[-slim]. */
10085 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
10087 /* Build a table of files to generate specs for */
10088 if (flag_dump_ada_spec_slim)
10089 collect_source_ref (main_input_filename);
10090 else
10091 for_each_global_decl (collect_source_ref_cb);
10093 dump_ada_specs (collect_all_refs, NULL);
10096 if (ext_block)
10098 tree tmp = BLOCK_VARS (ext_block);
10099 int flags;
10100 FILE * stream = dump_begin (TDI_tu, &flags);
10101 if (stream && tmp)
10103 dump_node (tmp, flags & ~TDF_SLIM, stream);
10104 dump_end (TDI_tu, stream);
10108 /* Process all file scopes in this compilation, and the external_scope,
10109 through wrapup_global_declarations and check_global_declarations. */
10110 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10111 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
10112 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
10114 timevar_stop (TV_PHASE_DEFERRED);
10115 timevar_start (TV_PHASE_OPT_GEN);
10117 /* We're done parsing; proceed to optimize and emit assembly.
10118 FIXME: shouldn't be the front end's responsibility to call this. */
10119 finalize_compilation_unit ();
10121 timevar_stop (TV_PHASE_OPT_GEN);
10122 timevar_start (TV_PHASE_DBGINFO);
10124 /* After cgraph has had a chance to emit everything that's going to
10125 be emitted, output debug information for globals. */
10126 if (!seen_error ())
10128 timevar_push (TV_SYMOUT);
10129 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10130 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
10131 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
10132 timevar_pop (TV_SYMOUT);
10135 ext_block = NULL;
10136 timevar_stop (TV_PHASE_DBGINFO);
10139 /* Register reserved keyword WORD as qualifier for address space AS. */
10141 void
10142 c_register_addr_space (const char *word, addr_space_t as)
10144 int rid = RID_FIRST_ADDR_SPACE + as;
10145 tree id;
10147 /* Address space qualifiers are only supported
10148 in C with GNU extensions enabled. */
10149 if (c_dialect_objc () || flag_no_asm)
10150 return;
10152 id = get_identifier (word);
10153 C_SET_RID_CODE (id, rid);
10154 C_IS_RESERVED_WORD (id) = 1;
10155 ridpointers [rid] = id;
10158 #include "gt-c-c-decl.h"