2013-10-02 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / c / c-decl.c
blobf26334829d8c972635e7eb8a74618833b64a155f
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 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
3633 UNKNOWN_LOCATION);
3635 /* Builtins in the implementation namespace are made visible without
3636 needing to be explicitly declared. See push_file_scope. */
3637 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3639 DECL_CHAIN (decl) = visible_builtins;
3640 visible_builtins = decl;
3643 return decl;
3646 /* Called when a declaration is seen that contains no names to declare.
3647 If its type is a reference to a structure, union or enum inherited
3648 from a containing scope, shadow that tag name for the current scope
3649 with a forward reference.
3650 If its type defines a new named structure or union
3651 or defines an enum, it is valid but we need not do anything here.
3652 Otherwise, it is an error. */
3654 void
3655 shadow_tag (const struct c_declspecs *declspecs)
3657 shadow_tag_warned (declspecs, 0);
3660 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
3661 but no pedwarn. */
3662 void
3663 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
3665 bool found_tag = false;
3667 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
3669 tree value = declspecs->type;
3670 enum tree_code code = TREE_CODE (value);
3672 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3673 /* Used to test also that TYPE_SIZE (value) != 0.
3674 That caused warning for `struct foo;' at top level in the file. */
3676 tree name = TYPE_NAME (value);
3677 tree t;
3679 found_tag = true;
3681 if (declspecs->restrict_p)
3683 error ("invalid use of %<restrict%>");
3684 warned = 1;
3687 if (name == 0)
3689 if (warned != 1 && code != ENUMERAL_TYPE)
3690 /* Empty unnamed enum OK */
3692 pedwarn (input_location, 0,
3693 "unnamed struct/union that defines no instances");
3694 warned = 1;
3697 else if (declspecs->typespec_kind != ctsk_tagdef
3698 && declspecs->typespec_kind != ctsk_tagfirstref
3699 && declspecs->storage_class != csc_none)
3701 if (warned != 1)
3702 pedwarn (input_location, 0,
3703 "empty declaration with storage class specifier "
3704 "does not redeclare tag");
3705 warned = 1;
3706 pending_xref_error ();
3708 else if (declspecs->typespec_kind != ctsk_tagdef
3709 && declspecs->typespec_kind != ctsk_tagfirstref
3710 && (declspecs->const_p
3711 || declspecs->volatile_p
3712 || declspecs->restrict_p
3713 || declspecs->address_space))
3715 if (warned != 1)
3716 pedwarn (input_location, 0,
3717 "empty declaration with type qualifier "
3718 "does not redeclare tag");
3719 warned = 1;
3720 pending_xref_error ();
3722 else if (declspecs->typespec_kind != ctsk_tagdef
3723 && declspecs->typespec_kind != ctsk_tagfirstref
3724 && declspecs->alignas_p)
3726 if (warned != 1)
3727 pedwarn (input_location, 0,
3728 "empty declaration with %<_Alignas%> "
3729 "does not redeclare tag");
3730 warned = 1;
3731 pending_xref_error ();
3733 else
3735 pending_invalid_xref = 0;
3736 t = lookup_tag (code, name, 1, NULL);
3738 if (t == 0)
3740 t = make_node (code);
3741 pushtag (input_location, name, t);
3745 else
3747 if (warned != 1 && !in_system_header)
3749 pedwarn (input_location, 0,
3750 "useless type name in empty declaration");
3751 warned = 1;
3755 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3757 pedwarn (input_location, 0, "useless type name in empty declaration");
3758 warned = 1;
3761 pending_invalid_xref = 0;
3763 if (declspecs->inline_p)
3765 error ("%<inline%> in empty declaration");
3766 warned = 1;
3769 if (declspecs->noreturn_p)
3771 error ("%<_Noreturn%> in empty declaration");
3772 warned = 1;
3775 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3777 error ("%<auto%> in file-scope empty declaration");
3778 warned = 1;
3781 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3783 error ("%<register%> in file-scope empty declaration");
3784 warned = 1;
3787 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3789 warning (0, "useless storage class specifier in empty declaration");
3790 warned = 2;
3793 if (!warned && !in_system_header && declspecs->thread_p)
3795 warning (0, "useless %<__thread%> in empty declaration");
3796 warned = 2;
3799 if (!warned && !in_system_header && (declspecs->const_p
3800 || declspecs->volatile_p
3801 || declspecs->restrict_p
3802 || declspecs->address_space))
3804 warning (0, "useless type qualifier in empty declaration");
3805 warned = 2;
3808 if (!warned && !in_system_header && declspecs->alignas_p)
3810 warning (0, "useless %<_Alignas%> in empty declaration");
3811 warned = 2;
3814 if (warned != 1)
3816 if (!found_tag)
3817 pedwarn (input_location, 0, "empty declaration");
3822 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3823 bits. SPECS represents declaration specifiers that the grammar
3824 only permits to contain type qualifiers and attributes. */
3827 quals_from_declspecs (const struct c_declspecs *specs)
3829 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3830 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3831 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
3832 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
3833 gcc_assert (!specs->type
3834 && !specs->decl_attr
3835 && specs->typespec_word == cts_none
3836 && specs->storage_class == csc_none
3837 && !specs->typedef_p
3838 && !specs->explicit_signed_p
3839 && !specs->deprecated_p
3840 && !specs->long_p
3841 && !specs->long_long_p
3842 && !specs->short_p
3843 && !specs->signed_p
3844 && !specs->unsigned_p
3845 && !specs->complex_p
3846 && !specs->inline_p
3847 && !specs->noreturn_p
3848 && !specs->thread_p);
3849 return quals;
3852 /* Construct an array declarator. LOC is the location of the
3853 beginning of the array (usually the opening brace). EXPR is the
3854 expression inside [], or NULL_TREE. QUALS are the type qualifiers
3855 inside the [] (to be applied to the pointer to which a parameter
3856 array is converted). STATIC_P is true if "static" is inside the
3857 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
3858 VLA of unspecified length which is nevertheless a complete type,
3859 false otherwise. The field for the contained declarator is left to
3860 be filled in by set_array_declarator_inner. */
3862 struct c_declarator *
3863 build_array_declarator (location_t loc,
3864 tree expr, struct c_declspecs *quals, bool static_p,
3865 bool vla_unspec_p)
3867 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3868 struct c_declarator);
3869 declarator->id_loc = loc;
3870 declarator->kind = cdk_array;
3871 declarator->declarator = 0;
3872 declarator->u.array.dimen = expr;
3873 if (quals)
3875 declarator->u.array.attrs = quals->attrs;
3876 declarator->u.array.quals = quals_from_declspecs (quals);
3878 else
3880 declarator->u.array.attrs = NULL_TREE;
3881 declarator->u.array.quals = 0;
3883 declarator->u.array.static_p = static_p;
3884 declarator->u.array.vla_unspec_p = vla_unspec_p;
3885 if (!flag_isoc99)
3887 if (static_p || quals != NULL)
3888 pedwarn (loc, OPT_Wpedantic,
3889 "ISO C90 does not support %<static%> or type "
3890 "qualifiers in parameter array declarators");
3891 if (vla_unspec_p)
3892 pedwarn (loc, OPT_Wpedantic,
3893 "ISO C90 does not support %<[*]%> array declarators");
3895 if (vla_unspec_p)
3897 if (!current_scope->parm_flag)
3899 /* C99 6.7.5.2p4 */
3900 error_at (loc, "%<[*]%> not allowed in other than "
3901 "function prototype scope");
3902 declarator->u.array.vla_unspec_p = false;
3903 return NULL;
3905 current_scope->had_vla_unspec = true;
3907 return declarator;
3910 /* Set the contained declarator of an array declarator. DECL is the
3911 declarator, as constructed by build_array_declarator; INNER is what
3912 appears on the left of the []. */
3914 struct c_declarator *
3915 set_array_declarator_inner (struct c_declarator *decl,
3916 struct c_declarator *inner)
3918 decl->declarator = inner;
3919 return decl;
3922 /* INIT is a constructor that forms DECL's initializer. If the final
3923 element initializes a flexible array field, add the size of that
3924 initializer to DECL's size. */
3926 static void
3927 add_flexible_array_elts_to_size (tree decl, tree init)
3929 tree elt, type;
3931 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
3932 return;
3934 elt = CONSTRUCTOR_ELTS (init)->last ().value;
3935 type = TREE_TYPE (elt);
3936 if (TREE_CODE (type) == ARRAY_TYPE
3937 && TYPE_SIZE (type) == NULL_TREE
3938 && TYPE_DOMAIN (type) != NULL_TREE
3939 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3941 complete_array_type (&type, elt, false);
3942 DECL_SIZE (decl)
3943 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3944 DECL_SIZE_UNIT (decl)
3945 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3949 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3950 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3951 before the type name, and set *EXPR_CONST_OPERANDS, if
3952 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3953 appear in a constant expression. */
3955 tree
3956 groktypename (struct c_type_name *type_name, tree *expr,
3957 bool *expr_const_operands)
3959 tree type;
3960 tree attrs = type_name->specs->attrs;
3962 type_name->specs->attrs = NULL_TREE;
3964 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3965 false, NULL, &attrs, expr, expr_const_operands,
3966 DEPRECATED_NORMAL);
3968 /* Apply attributes. */
3969 decl_attributes (&type, attrs, 0);
3971 return type;
3974 /* Decode a declarator in an ordinary declaration or data definition.
3975 This is called as soon as the type information and variable name
3976 have been parsed, before parsing the initializer if any.
3977 Here we create the ..._DECL node, fill in its type,
3978 and put it on the list of decls for the current context.
3979 The ..._DECL node is returned as the value.
3981 Exception: for arrays where the length is not specified,
3982 the type is left null, to be filled in by `finish_decl'.
3984 Function definitions do not come here; they go to start_function
3985 instead. However, external and forward declarations of functions
3986 do go through here. Structure field declarations are done by
3987 grokfield and not through here. */
3989 tree
3990 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3991 bool initialized, tree attributes)
3993 tree decl;
3994 tree tem;
3995 tree expr = NULL_TREE;
3996 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3998 /* An object declared as __attribute__((deprecated)) suppresses
3999 warnings of uses of other deprecated items. */
4000 if (lookup_attribute ("deprecated", attributes))
4001 deprecated_state = DEPRECATED_SUPPRESS;
4003 decl = grokdeclarator (declarator, declspecs,
4004 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4005 deprecated_state);
4006 if (!decl)
4007 return 0;
4009 if (expr)
4010 add_stmt (fold_convert (void_type_node, expr));
4012 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4013 warning (OPT_Wmain, "%q+D is usually a function", decl);
4015 if (initialized)
4016 /* Is it valid for this decl to have an initializer at all?
4017 If not, set INITIALIZED to zero, which will indirectly
4018 tell 'finish_decl' to ignore the initializer once it is parsed. */
4019 switch (TREE_CODE (decl))
4021 case TYPE_DECL:
4022 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4023 initialized = 0;
4024 break;
4026 case FUNCTION_DECL:
4027 error ("function %qD is initialized like a variable", decl);
4028 initialized = 0;
4029 break;
4031 case PARM_DECL:
4032 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4033 error ("parameter %qD is initialized", decl);
4034 initialized = 0;
4035 break;
4037 default:
4038 /* Don't allow initializations for incomplete types except for
4039 arrays which might be completed by the initialization. */
4041 /* This can happen if the array size is an undefined macro.
4042 We already gave a warning, so we don't need another one. */
4043 if (TREE_TYPE (decl) == error_mark_node)
4044 initialized = 0;
4045 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4047 /* A complete type is ok if size is fixed. */
4049 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4050 || C_DECL_VARIABLE_SIZE (decl))
4052 error ("variable-sized object may not be initialized");
4053 initialized = 0;
4056 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4058 error ("variable %qD has initializer but incomplete type", decl);
4059 initialized = 0;
4061 else if (C_DECL_VARIABLE_SIZE (decl))
4063 /* Although C99 is unclear about whether incomplete arrays
4064 of VLAs themselves count as VLAs, it does not make
4065 sense to permit them to be initialized given that
4066 ordinary VLAs may not be initialized. */
4067 error ("variable-sized object may not be initialized");
4068 initialized = 0;
4072 if (initialized)
4074 if (current_scope == file_scope)
4075 TREE_STATIC (decl) = 1;
4077 /* Tell 'pushdecl' this is an initialized decl
4078 even though we don't yet have the initializer expression.
4079 Also tell 'finish_decl' it may store the real initializer. */
4080 DECL_INITIAL (decl) = error_mark_node;
4083 /* If this is a function declaration, write a record describing it to the
4084 prototypes file (if requested). */
4086 if (TREE_CODE (decl) == FUNCTION_DECL)
4087 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4089 /* ANSI specifies that a tentative definition which is not merged with
4090 a non-tentative definition behaves exactly like a definition with an
4091 initializer equal to zero. (Section 3.7.2)
4093 -fno-common gives strict ANSI behavior, though this tends to break
4094 a large body of code that grew up without this rule.
4096 Thread-local variables are never common, since there's no entrenched
4097 body of code to break, and it allows more efficient variable references
4098 in the presence of dynamic linking. */
4100 if (TREE_CODE (decl) == VAR_DECL
4101 && !initialized
4102 && TREE_PUBLIC (decl)
4103 && !DECL_THREAD_LOCAL_P (decl)
4104 && !flag_no_common)
4105 DECL_COMMON (decl) = 1;
4107 /* Set attributes here so if duplicate decl, will have proper attributes. */
4108 decl_attributes (&decl, attributes, 0);
4110 /* Handle gnu_inline attribute. */
4111 if (declspecs->inline_p
4112 && !flag_gnu89_inline
4113 && TREE_CODE (decl) == FUNCTION_DECL
4114 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4115 || current_function_decl))
4117 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4119 else if (declspecs->storage_class != csc_static)
4120 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4123 if (TREE_CODE (decl) == FUNCTION_DECL
4124 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4126 struct c_declarator *ce = declarator;
4128 if (ce->kind == cdk_pointer)
4129 ce = declarator->declarator;
4130 if (ce->kind == cdk_function)
4132 tree args = ce->u.arg_info->parms;
4133 for (; args; args = DECL_CHAIN (args))
4135 tree type = TREE_TYPE (args);
4136 if (type && INTEGRAL_TYPE_P (type)
4137 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4138 DECL_ARG_TYPE (args) = integer_type_node;
4143 if (TREE_CODE (decl) == FUNCTION_DECL
4144 && DECL_DECLARED_INLINE_P (decl)
4145 && DECL_UNINLINABLE (decl)
4146 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4147 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4148 decl);
4150 /* C99 6.7.4p3: An inline definition of a function with external
4151 linkage shall not contain a definition of a modifiable object
4152 with static storage duration... */
4153 if (TREE_CODE (decl) == VAR_DECL
4154 && current_scope != file_scope
4155 && TREE_STATIC (decl)
4156 && !TREE_READONLY (decl)
4157 && DECL_DECLARED_INLINE_P (current_function_decl)
4158 && DECL_EXTERNAL (current_function_decl))
4159 record_inline_static (input_location, current_function_decl,
4160 decl, csi_modifiable);
4162 if (c_dialect_objc ()
4163 && (TREE_CODE (decl) == VAR_DECL
4164 || TREE_CODE (decl) == FUNCTION_DECL))
4165 objc_check_global_decl (decl);
4167 /* Add this decl to the current scope.
4168 TEM may equal DECL or it may be a previous decl of the same name. */
4169 tem = pushdecl (decl);
4171 if (initialized && DECL_EXTERNAL (tem))
4173 DECL_EXTERNAL (tem) = 0;
4174 TREE_STATIC (tem) = 1;
4177 return tem;
4180 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4181 DECL or the non-array element type if DECL is an uninitialized array.
4182 If that type has a const member, diagnose this. */
4184 static void
4185 diagnose_uninitialized_cst_member (tree decl, tree type)
4187 tree field;
4188 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4190 tree field_type;
4191 if (TREE_CODE (field) != FIELD_DECL)
4192 continue;
4193 field_type = strip_array_types (TREE_TYPE (field));
4195 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4197 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4198 "uninitialized const member in %qT is invalid in C++",
4199 strip_array_types (TREE_TYPE (decl)));
4200 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4203 if (TREE_CODE (field_type) == RECORD_TYPE
4204 || TREE_CODE (field_type) == UNION_TYPE)
4205 diagnose_uninitialized_cst_member (decl, field_type);
4209 /* Finish processing of a declaration;
4210 install its initial value.
4211 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4212 If the length of an array type is not known before,
4213 it must be determined now, from the initial value, or it is an error.
4215 INIT_LOC is the location of the initial value. */
4217 void
4218 finish_decl (tree decl, location_t init_loc, tree init,
4219 tree origtype, tree asmspec_tree)
4221 tree type;
4222 bool was_incomplete = (DECL_SIZE (decl) == 0);
4223 const char *asmspec = 0;
4225 /* If a name was specified, get the string. */
4226 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4227 && DECL_FILE_SCOPE_P (decl))
4228 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4229 if (asmspec_tree)
4230 asmspec = TREE_STRING_POINTER (asmspec_tree);
4232 if (TREE_CODE (decl) == VAR_DECL
4233 && TREE_STATIC (decl)
4234 && global_bindings_p ())
4235 /* So decl is a global variable. Record the types it uses
4236 so that we can decide later to emit debug info for them. */
4237 record_types_used_by_current_var_decl (decl);
4239 /* If `start_decl' didn't like having an initialization, ignore it now. */
4240 if (init != 0 && DECL_INITIAL (decl) == 0)
4241 init = 0;
4243 /* Don't crash if parm is initialized. */
4244 if (TREE_CODE (decl) == PARM_DECL)
4245 init = 0;
4247 if (init)
4248 store_init_value (init_loc, decl, init, origtype);
4250 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
4251 || TREE_CODE (decl) == FUNCTION_DECL
4252 || TREE_CODE (decl) == FIELD_DECL))
4253 objc_check_decl (decl);
4255 type = TREE_TYPE (decl);
4257 /* Deduce size of array from initialization, if not already known. */
4258 if (TREE_CODE (type) == ARRAY_TYPE
4259 && TYPE_DOMAIN (type) == 0
4260 && TREE_CODE (decl) != TYPE_DECL)
4262 bool do_default
4263 = (TREE_STATIC (decl)
4264 /* Even if pedantic, an external linkage array
4265 may have incomplete type at first. */
4266 ? pedantic && !TREE_PUBLIC (decl)
4267 : !DECL_EXTERNAL (decl));
4268 int failure
4269 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4270 do_default);
4272 /* Get the completed type made by complete_array_type. */
4273 type = TREE_TYPE (decl);
4275 switch (failure)
4277 case 1:
4278 error ("initializer fails to determine size of %q+D", decl);
4279 break;
4281 case 2:
4282 if (do_default)
4283 error ("array size missing in %q+D", decl);
4284 /* If a `static' var's size isn't known,
4285 make it extern as well as static, so it does not get
4286 allocated.
4287 If it is not `static', then do not mark extern;
4288 finish_incomplete_decl will give it a default size
4289 and it will get allocated. */
4290 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4291 DECL_EXTERNAL (decl) = 1;
4292 break;
4294 case 3:
4295 error ("zero or negative size array %q+D", decl);
4296 break;
4298 case 0:
4299 /* For global variables, update the copy of the type that
4300 exists in the binding. */
4301 if (TREE_PUBLIC (decl))
4303 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4304 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4305 b_ext = b_ext->shadowed;
4306 if (b_ext)
4308 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4309 b_ext->u.type = composite_type (b_ext->u.type, type);
4310 else
4311 b_ext->u.type = type;
4314 break;
4316 default:
4317 gcc_unreachable ();
4320 if (DECL_INITIAL (decl))
4321 TREE_TYPE (DECL_INITIAL (decl)) = type;
4323 relayout_decl (decl);
4326 if (TREE_CODE (decl) == VAR_DECL)
4328 if (init && TREE_CODE (init) == CONSTRUCTOR)
4329 add_flexible_array_elts_to_size (decl, init);
4331 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4332 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4333 layout_decl (decl, 0);
4335 if (DECL_SIZE (decl) == 0
4336 /* Don't give an error if we already gave one earlier. */
4337 && TREE_TYPE (decl) != error_mark_node
4338 && (TREE_STATIC (decl)
4339 /* A static variable with an incomplete type
4340 is an error if it is initialized.
4341 Also if it is not file scope.
4342 Otherwise, let it through, but if it is not `extern'
4343 then it may cause an error message later. */
4344 ? (DECL_INITIAL (decl) != 0
4345 || !DECL_FILE_SCOPE_P (decl))
4346 /* An automatic variable with an incomplete type
4347 is an error. */
4348 : !DECL_EXTERNAL (decl)))
4350 error ("storage size of %q+D isn%'t known", decl);
4351 TREE_TYPE (decl) = error_mark_node;
4354 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4355 && DECL_SIZE (decl) != 0)
4357 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4358 constant_expression_warning (DECL_SIZE (decl));
4359 else
4361 error ("storage size of %q+D isn%'t constant", decl);
4362 TREE_TYPE (decl) = error_mark_node;
4366 if (TREE_USED (type))
4368 TREE_USED (decl) = 1;
4369 DECL_READ_P (decl) = 1;
4373 /* If this is a function and an assembler name is specified, reset DECL_RTL
4374 so we can give it its new name. Also, update builtin_decl if it
4375 was a normal built-in. */
4376 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4378 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4379 set_builtin_user_assembler_name (decl, asmspec);
4380 set_user_assembler_name (decl, asmspec);
4383 /* If #pragma weak was used, mark the decl weak now. */
4384 maybe_apply_pragma_weak (decl);
4386 /* Output the assembler code and/or RTL code for variables and functions,
4387 unless the type is an undefined structure or union.
4388 If not, it will get done when the type is completed. */
4390 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
4392 /* Determine the ELF visibility. */
4393 if (TREE_PUBLIC (decl))
4394 c_determine_visibility (decl);
4396 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4397 if (c_dialect_objc ())
4398 objc_check_decl (decl);
4400 if (asmspec)
4402 /* If this is not a static variable, issue a warning.
4403 It doesn't make any sense to give an ASMSPEC for an
4404 ordinary, non-register local variable. Historically,
4405 GCC has accepted -- but ignored -- the ASMSPEC in
4406 this case. */
4407 if (!DECL_FILE_SCOPE_P (decl)
4408 && TREE_CODE (decl) == VAR_DECL
4409 && !C_DECL_REGISTER (decl)
4410 && !TREE_STATIC (decl))
4411 warning (0, "ignoring asm-specifier for non-static local "
4412 "variable %q+D", decl);
4413 else
4414 set_user_assembler_name (decl, asmspec);
4417 if (DECL_FILE_SCOPE_P (decl))
4419 if (DECL_INITIAL (decl) == NULL_TREE
4420 || DECL_INITIAL (decl) == error_mark_node)
4421 /* Don't output anything
4422 when a tentative file-scope definition is seen.
4423 But at end of compilation, do output code for them. */
4424 DECL_DEFER_OUTPUT (decl) = 1;
4425 if (asmspec && C_DECL_REGISTER (decl))
4426 DECL_HARD_REGISTER (decl) = 1;
4427 rest_of_decl_compilation (decl, true, 0);
4429 else
4431 /* In conjunction with an ASMSPEC, the `register'
4432 keyword indicates that we should place the variable
4433 in a particular register. */
4434 if (asmspec && C_DECL_REGISTER (decl))
4436 DECL_HARD_REGISTER (decl) = 1;
4437 /* This cannot be done for a structure with volatile
4438 fields, on which DECL_REGISTER will have been
4439 reset. */
4440 if (!DECL_REGISTER (decl))
4441 error ("cannot put object with volatile field into register");
4444 if (TREE_CODE (decl) != FUNCTION_DECL)
4446 /* If we're building a variable sized type, and we might be
4447 reachable other than via the top of the current binding
4448 level, then create a new BIND_EXPR so that we deallocate
4449 the object at the right time. */
4450 /* Note that DECL_SIZE can be null due to errors. */
4451 if (DECL_SIZE (decl)
4452 && !TREE_CONSTANT (DECL_SIZE (decl))
4453 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4455 tree bind;
4456 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4457 TREE_SIDE_EFFECTS (bind) = 1;
4458 add_stmt (bind);
4459 BIND_EXPR_BODY (bind) = push_stmt_list ();
4461 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4462 DECL_EXPR, decl));
4467 if (!DECL_FILE_SCOPE_P (decl))
4469 /* Recompute the RTL of a local array now
4470 if it used to be an incomplete type. */
4471 if (was_incomplete
4472 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4474 /* If we used it already as memory, it must stay in memory. */
4475 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4476 /* If it's still incomplete now, no init will save it. */
4477 if (DECL_SIZE (decl) == 0)
4478 DECL_INITIAL (decl) = 0;
4483 if (TREE_CODE (decl) == TYPE_DECL)
4485 if (!DECL_FILE_SCOPE_P (decl)
4486 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4487 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4489 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4492 /* Install a cleanup (aka destructor) if one was given. */
4493 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4495 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4496 if (attr)
4498 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4499 tree cleanup_decl = lookup_name (cleanup_id);
4500 tree cleanup;
4501 vec<tree, va_gc> *v;
4503 /* Build "cleanup(&decl)" for the destructor. */
4504 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
4505 vec_alloc (v, 1);
4506 v->quick_push (cleanup);
4507 cleanup = build_function_call_vec (DECL_SOURCE_LOCATION (decl),
4508 cleanup_decl, v, NULL);
4509 vec_free (v);
4511 /* Don't warn about decl unused; the cleanup uses it. */
4512 TREE_USED (decl) = 1;
4513 TREE_USED (cleanup_decl) = 1;
4514 DECL_READ_P (decl) = 1;
4516 push_cleanup (decl, cleanup, false);
4520 if (warn_cxx_compat
4521 && TREE_CODE (decl) == VAR_DECL
4522 && !DECL_EXTERNAL (decl)
4523 && DECL_INITIAL (decl) == NULL_TREE)
4525 type = strip_array_types (type);
4526 if (TREE_READONLY (decl))
4527 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4528 "uninitialized const %qD is invalid in C++", decl);
4529 else if ((TREE_CODE (type) == RECORD_TYPE
4530 || TREE_CODE (type) == UNION_TYPE)
4531 && C_TYPE_FIELDS_READONLY (type))
4532 diagnose_uninitialized_cst_member (decl, type);
4535 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
4538 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
4539 EXPR is NULL or a pointer to an expression that needs to be
4540 evaluated for the side effects of array size expressions in the
4541 parameters. */
4543 tree
4544 grokparm (const struct c_parm *parm, tree *expr)
4546 tree attrs = parm->attrs;
4547 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4548 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
4550 decl_attributes (&decl, attrs, 0);
4552 return decl;
4555 /* Given a parsed parameter declaration, decode it into a PARM_DECL
4556 and push that on the current scope. EXPR is a pointer to an
4557 expression that needs to be evaluated for the side effects of array
4558 size expressions in the parameters. */
4560 void
4561 push_parm_decl (const struct c_parm *parm, tree *expr)
4563 tree attrs = parm->attrs;
4564 tree decl;
4566 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
4567 &attrs, expr, NULL, DEPRECATED_NORMAL);
4568 decl_attributes (&decl, attrs, 0);
4570 decl = pushdecl (decl);
4572 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
4575 /* Mark all the parameter declarations to date as forward decls.
4576 Also diagnose use of this extension. */
4578 void
4579 mark_forward_parm_decls (void)
4581 struct c_binding *b;
4583 if (pedantic && !current_scope->warned_forward_parm_decls)
4585 pedwarn (input_location, OPT_Wpedantic,
4586 "ISO C forbids forward parameter declarations");
4587 current_scope->warned_forward_parm_decls = true;
4590 for (b = current_scope->bindings; b; b = b->prev)
4591 if (TREE_CODE (b->decl) == PARM_DECL)
4592 TREE_ASM_WRITTEN (b->decl) = 1;
4595 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
4596 literal, which may be an incomplete array type completed by the
4597 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
4598 literal. NON_CONST is true if the initializers contain something
4599 that cannot occur in a constant expression. */
4601 tree
4602 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
4604 /* We do not use start_decl here because we have a type, not a declarator;
4605 and do not use finish_decl because the decl should be stored inside
4606 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
4607 tree decl;
4608 tree complit;
4609 tree stmt;
4611 if (type == error_mark_node
4612 || init == error_mark_node)
4613 return error_mark_node;
4615 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
4616 DECL_EXTERNAL (decl) = 0;
4617 TREE_PUBLIC (decl) = 0;
4618 TREE_STATIC (decl) = (current_scope == file_scope);
4619 DECL_CONTEXT (decl) = current_function_decl;
4620 TREE_USED (decl) = 1;
4621 DECL_READ_P (decl) = 1;
4622 TREE_TYPE (decl) = type;
4623 TREE_READONLY (decl) = (TYPE_READONLY (type)
4624 || (TREE_CODE (type) == ARRAY_TYPE
4625 && TYPE_READONLY (TREE_TYPE (type))));
4626 store_init_value (loc, decl, init, NULL_TREE);
4628 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
4630 int failure = complete_array_type (&TREE_TYPE (decl),
4631 DECL_INITIAL (decl), true);
4632 gcc_assert (!failure);
4634 type = TREE_TYPE (decl);
4635 TREE_TYPE (DECL_INITIAL (decl)) = type;
4638 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4640 c_incomplete_type_error (NULL_TREE, type);
4641 return error_mark_node;
4644 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
4645 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
4646 TREE_SIDE_EFFECTS (complit) = 1;
4648 layout_decl (decl, 0);
4650 if (TREE_STATIC (decl))
4652 /* This decl needs a name for the assembler output. */
4653 set_compound_literal_name (decl);
4654 DECL_DEFER_OUTPUT (decl) = 1;
4655 DECL_COMDAT (decl) = 1;
4656 DECL_ARTIFICIAL (decl) = 1;
4657 DECL_IGNORED_P (decl) = 1;
4658 pushdecl (decl);
4659 rest_of_decl_compilation (decl, 1, 0);
4662 if (non_const)
4664 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
4665 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
4668 return complit;
4671 /* Check the type of a compound literal. Here we just check that it
4672 is valid for C++. */
4674 void
4675 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
4677 if (warn_cxx_compat
4678 && (type_name->specs->typespec_kind == ctsk_tagdef
4679 || type_name->specs->typespec_kind == ctsk_tagfirstref))
4680 warning_at (loc, OPT_Wc___compat,
4681 "defining a type in a compound literal is invalid in C++");
4684 /* Determine whether TYPE is a structure with a flexible array member,
4685 or a union containing such a structure (possibly recursively). */
4687 static bool
4688 flexible_array_type_p (tree type)
4690 tree x;
4691 switch (TREE_CODE (type))
4693 case RECORD_TYPE:
4694 x = TYPE_FIELDS (type);
4695 if (x == NULL_TREE)
4696 return false;
4697 while (DECL_CHAIN (x) != NULL_TREE)
4698 x = DECL_CHAIN (x);
4699 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4700 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4701 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
4702 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
4703 return true;
4704 return false;
4705 case UNION_TYPE:
4706 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
4708 if (flexible_array_type_p (TREE_TYPE (x)))
4709 return true;
4711 return false;
4712 default:
4713 return false;
4717 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
4718 replacing with appropriate values if they are invalid. */
4719 static void
4720 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
4722 tree type_mv;
4723 unsigned int max_width;
4724 unsigned HOST_WIDE_INT w;
4725 const char *name = (orig_name
4726 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
4727 : _("<anonymous>"));
4729 /* Detect and ignore out of range field width and process valid
4730 field widths. */
4731 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
4733 error ("bit-field %qs width not an integer constant", name);
4734 *width = integer_one_node;
4736 else
4738 if (TREE_CODE (*width) != INTEGER_CST)
4740 *width = c_fully_fold (*width, false, NULL);
4741 if (TREE_CODE (*width) == INTEGER_CST)
4742 pedwarn (input_location, OPT_Wpedantic,
4743 "bit-field %qs width not an integer constant expression",
4744 name);
4746 if (TREE_CODE (*width) != INTEGER_CST)
4748 error ("bit-field %qs width not an integer constant", name);
4749 *width = integer_one_node;
4751 constant_expression_warning (*width);
4752 if (tree_int_cst_sgn (*width) < 0)
4754 error ("negative width in bit-field %qs", name);
4755 *width = integer_one_node;
4757 else if (integer_zerop (*width) && orig_name)
4759 error ("zero width for bit-field %qs", name);
4760 *width = integer_one_node;
4764 /* Detect invalid bit-field type. */
4765 if (TREE_CODE (*type) != INTEGER_TYPE
4766 && TREE_CODE (*type) != BOOLEAN_TYPE
4767 && TREE_CODE (*type) != ENUMERAL_TYPE)
4769 error ("bit-field %qs has invalid type", name);
4770 *type = unsigned_type_node;
4773 type_mv = TYPE_MAIN_VARIANT (*type);
4774 if (!in_system_header
4775 && type_mv != integer_type_node
4776 && type_mv != unsigned_type_node
4777 && type_mv != boolean_type_node)
4778 pedwarn (input_location, OPT_Wpedantic,
4779 "type of bit-field %qs is a GCC extension", name);
4781 max_width = TYPE_PRECISION (*type);
4783 if (0 < compare_tree_int (*width, max_width))
4785 error ("width of %qs exceeds its type", name);
4786 w = max_width;
4787 *width = build_int_cst (integer_type_node, w);
4789 else
4790 w = tree_low_cst (*width, 1);
4792 if (TREE_CODE (*type) == ENUMERAL_TYPE)
4794 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
4795 if (!lt
4796 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
4797 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
4798 warning (0, "%qs is narrower than values of its type", name);
4804 /* Print warning about variable length array if necessary. */
4806 static void
4807 warn_variable_length_array (tree name, tree size)
4809 int const_size = TREE_CONSTANT (size);
4811 if (!flag_isoc99 && pedantic && warn_vla != 0)
4813 if (const_size)
4815 if (name)
4816 pedwarn (input_location, OPT_Wvla,
4817 "ISO C90 forbids array %qE whose size "
4818 "can%'t be evaluated",
4819 name);
4820 else
4821 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
4822 "can%'t be evaluated");
4824 else
4826 if (name)
4827 pedwarn (input_location, OPT_Wvla,
4828 "ISO C90 forbids variable length array %qE",
4829 name);
4830 else
4831 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
4834 else if (warn_vla > 0)
4836 if (const_size)
4838 if (name)
4839 warning (OPT_Wvla,
4840 "the size of array %qE can"
4841 "%'t be evaluated", name);
4842 else
4843 warning (OPT_Wvla,
4844 "the size of array can %'t be evaluated");
4846 else
4848 if (name)
4849 warning (OPT_Wvla,
4850 "variable length array %qE is used",
4851 name);
4852 else
4853 warning (OPT_Wvla,
4854 "variable length array is used");
4859 /* Given declspecs and a declarator,
4860 determine the name and type of the object declared
4861 and construct a ..._DECL node for it.
4862 (In one case we can return a ..._TYPE node instead.
4863 For invalid input we sometimes return 0.)
4865 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4867 DECL_CONTEXT says which syntactic context this declaration is in:
4868 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4869 FUNCDEF for a function definition. Like NORMAL but a few different
4870 error messages in each case. Return value may be zero meaning
4871 this definition is too screwy to try to parse.
4872 PARM for a parameter declaration (either within a function prototype
4873 or before a function body). Make a PARM_DECL, or return void_type_node.
4874 TYPENAME if for a typename (in a cast or sizeof).
4875 Don't make a DECL node; just return the ..._TYPE node.
4876 FIELD for a struct or union field; make a FIELD_DECL.
4877 INITIALIZED is true if the decl has an initializer.
4878 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4879 representing the width of the bit-field.
4880 DECL_ATTRS points to the list of attributes that should be added to this
4881 decl. Any nested attributes that belong on the decl itself will be
4882 added to this list.
4883 If EXPR is not NULL, any expressions that need to be evaluated as
4884 part of evaluating variably modified types will be stored in *EXPR.
4885 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4886 set to indicate whether operands in *EXPR can be used in constant
4887 expressions.
4888 DEPRECATED_STATE is a deprecated_states value indicating whether
4889 deprecation warnings should be suppressed.
4891 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4892 It may also be so in the PARM case, for a prototype where the
4893 argument type is specified but not the name.
4895 This function is where the complicated C meanings of `static'
4896 and `extern' are interpreted. */
4898 static tree
4899 grokdeclarator (const struct c_declarator *declarator,
4900 struct c_declspecs *declspecs,
4901 enum decl_context decl_context, bool initialized, tree *width,
4902 tree *decl_attrs, tree *expr, bool *expr_const_operands,
4903 enum deprecated_states deprecated_state)
4905 tree type = declspecs->type;
4906 bool threadp = declspecs->thread_p;
4907 enum c_storage_class storage_class = declspecs->storage_class;
4908 int constp;
4909 int restrictp;
4910 int volatilep;
4911 int type_quals = TYPE_UNQUALIFIED;
4912 tree name = NULL_TREE;
4913 bool funcdef_flag = false;
4914 bool funcdef_syntax = false;
4915 bool size_varies = false;
4916 tree decl_attr = declspecs->decl_attr;
4917 int array_ptr_quals = TYPE_UNQUALIFIED;
4918 tree array_ptr_attrs = NULL_TREE;
4919 int array_parm_static = 0;
4920 bool array_parm_vla_unspec_p = false;
4921 tree returned_attrs = NULL_TREE;
4922 bool bitfield = width != NULL;
4923 tree element_type;
4924 struct c_arg_info *arg_info = 0;
4925 addr_space_t as1, as2, address_space;
4926 location_t loc = UNKNOWN_LOCATION;
4927 const char *errmsg;
4928 tree expr_dummy;
4929 bool expr_const_operands_dummy;
4930 enum c_declarator_kind first_non_attr_kind;
4931 unsigned int alignas_align = 0;
4933 if (TREE_CODE (type) == ERROR_MARK)
4934 return error_mark_node;
4935 if (expr == NULL)
4936 expr = &expr_dummy;
4937 if (expr_const_operands == NULL)
4938 expr_const_operands = &expr_const_operands_dummy;
4940 *expr = declspecs->expr;
4941 *expr_const_operands = declspecs->expr_const_operands;
4943 if (decl_context == FUNCDEF)
4944 funcdef_flag = true, decl_context = NORMAL;
4946 /* Look inside a declarator for the name being declared
4947 and get it as an IDENTIFIER_NODE, for an error message. */
4949 const struct c_declarator *decl = declarator;
4951 first_non_attr_kind = cdk_attrs;
4952 while (decl)
4953 switch (decl->kind)
4955 case cdk_array:
4956 loc = decl->id_loc;
4957 /* FALL THRU. */
4959 case cdk_function:
4960 case cdk_pointer:
4961 funcdef_syntax = (decl->kind == cdk_function);
4962 decl = decl->declarator;
4963 if (first_non_attr_kind == cdk_attrs)
4964 first_non_attr_kind = decl->kind;
4965 break;
4967 case cdk_attrs:
4968 decl = decl->declarator;
4969 break;
4971 case cdk_id:
4972 loc = decl->id_loc;
4973 if (decl->u.id)
4974 name = decl->u.id;
4975 if (first_non_attr_kind == cdk_attrs)
4976 first_non_attr_kind = decl->kind;
4977 decl = 0;
4978 break;
4980 default:
4981 gcc_unreachable ();
4983 if (name == 0)
4985 gcc_assert (decl_context == PARM
4986 || decl_context == TYPENAME
4987 || (decl_context == FIELD
4988 && declarator->kind == cdk_id));
4989 gcc_assert (!initialized);
4993 /* A function definition's declarator must have the form of
4994 a function declarator. */
4996 if (funcdef_flag && !funcdef_syntax)
4997 return 0;
4999 /* If this looks like a function definition, make it one,
5000 even if it occurs where parms are expected.
5001 Then store_parm_decls will reject it and not use it as a parm. */
5002 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5003 decl_context = PARM;
5005 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5006 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5008 if ((decl_context == NORMAL || decl_context == FIELD)
5009 && current_scope == file_scope
5010 && variably_modified_type_p (type, NULL_TREE))
5012 if (name)
5013 error_at (loc, "variably modified %qE at file scope", name);
5014 else
5015 error_at (loc, "variably modified field at file scope");
5016 type = integer_type_node;
5019 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5021 /* Diagnose defaulting to "int". */
5023 if (declspecs->default_int_p && !in_system_header)
5025 /* Issue a warning if this is an ISO C 99 program or if
5026 -Wreturn-type and this is a function, or if -Wimplicit;
5027 prefer the former warning since it is more explicit. */
5028 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5029 && funcdef_flag)
5030 warn_about_return_type = 1;
5031 else
5033 if (name)
5034 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wimplicit_int,
5035 "type defaults to %<int%> in declaration of %qE",
5036 name);
5037 else
5038 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
5039 "type defaults to %<int%> in type name");
5043 /* Adjust the type if a bit-field is being declared,
5044 -funsigned-bitfields applied and the type is not explicitly
5045 "signed". */
5046 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5047 && TREE_CODE (type) == INTEGER_TYPE)
5048 type = unsigned_type_for (type);
5050 /* Figure out the type qualifiers for the declaration. There are
5051 two ways a declaration can become qualified. One is something
5052 like `const int i' where the `const' is explicit. Another is
5053 something like `typedef const int CI; CI i' where the type of the
5054 declaration contains the `const'. A third possibility is that
5055 there is a type qualifier on the element type of a typedefed
5056 array type, in which case we should extract that qualifier so
5057 that c_apply_type_quals_to_decl receives the full list of
5058 qualifiers to work with (C90 is not entirely clear about whether
5059 duplicate qualifiers should be diagnosed in this case, but it
5060 seems most appropriate to do so). */
5061 element_type = strip_array_types (type);
5062 constp = declspecs->const_p + TYPE_READONLY (element_type);
5063 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5064 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5065 as1 = declspecs->address_space;
5066 as2 = TYPE_ADDR_SPACE (element_type);
5067 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5069 if (pedantic && !flag_isoc99)
5071 if (constp > 1)
5072 pedwarn (loc, OPT_Wpedantic, "duplicate %<const%>");
5073 if (restrictp > 1)
5074 pedwarn (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5075 if (volatilep > 1)
5076 pedwarn (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5079 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5080 error_at (loc, "conflicting named address spaces (%s vs %s)",
5081 c_addr_space_name (as1), c_addr_space_name (as2));
5083 if ((TREE_CODE (type) == ARRAY_TYPE
5084 || first_non_attr_kind == cdk_array)
5085 && TYPE_QUALS (element_type))
5086 type = TYPE_MAIN_VARIANT (type);
5087 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5088 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5089 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5090 | ENCODE_QUAL_ADDR_SPACE (address_space));
5092 /* Warn about storage classes that are invalid for certain
5093 kinds of declarations (parameters, typenames, etc.). */
5095 if (funcdef_flag
5096 && (threadp
5097 || storage_class == csc_auto
5098 || storage_class == csc_register
5099 || storage_class == csc_typedef))
5101 if (storage_class == csc_auto)
5102 pedwarn (loc,
5103 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5104 "function definition declared %<auto%>");
5105 if (storage_class == csc_register)
5106 error_at (loc, "function definition declared %<register%>");
5107 if (storage_class == csc_typedef)
5108 error_at (loc, "function definition declared %<typedef%>");
5109 if (threadp)
5110 error_at (loc, "function definition declared %<__thread%>");
5111 threadp = false;
5112 if (storage_class == csc_auto
5113 || storage_class == csc_register
5114 || storage_class == csc_typedef)
5115 storage_class = csc_none;
5117 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5119 if (decl_context == PARM && storage_class == csc_register)
5121 else
5123 switch (decl_context)
5125 case FIELD:
5126 if (name)
5127 error_at (loc, "storage class specified for structure "
5128 "field %qE", name);
5129 else
5130 error_at (loc, "storage class specified for structure field");
5131 break;
5132 case PARM:
5133 if (name)
5134 error_at (loc, "storage class specified for parameter %qE",
5135 name);
5136 else
5137 error_at (loc, "storage class specified for unnamed parameter");
5138 break;
5139 default:
5140 error_at (loc, "storage class specified for typename");
5141 break;
5143 storage_class = csc_none;
5144 threadp = false;
5147 else if (storage_class == csc_extern
5148 && initialized
5149 && !funcdef_flag)
5151 /* 'extern' with initialization is invalid if not at file scope. */
5152 if (current_scope == file_scope)
5154 /* It is fine to have 'extern const' when compiling at C
5155 and C++ intersection. */
5156 if (!(warn_cxx_compat && constp))
5157 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5158 name);
5160 else
5161 error_at (loc, "%qE has both %<extern%> and initializer", name);
5163 else if (current_scope == file_scope)
5165 if (storage_class == csc_auto)
5166 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5167 name);
5168 if (pedantic && storage_class == csc_register)
5169 pedwarn (input_location, OPT_Wpedantic,
5170 "file-scope declaration of %qE specifies %<register%>", name);
5172 else
5174 if (storage_class == csc_extern && funcdef_flag)
5175 error_at (loc, "nested function %qE declared %<extern%>", name);
5176 else if (threadp && storage_class == csc_none)
5178 error_at (loc, "function-scope %qE implicitly auto and declared "
5179 "%<__thread%>",
5180 name);
5181 threadp = false;
5185 /* Now figure out the structure of the declarator proper.
5186 Descend through it, creating more complex types, until we reach
5187 the declared identifier (or NULL_TREE, in an absolute declarator).
5188 At each stage we maintain an unqualified version of the type
5189 together with any qualifiers that should be applied to it with
5190 c_build_qualified_type; this way, array types including
5191 multidimensional array types are first built up in unqualified
5192 form and then the qualified form is created with
5193 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5195 while (declarator && declarator->kind != cdk_id)
5197 if (type == error_mark_node)
5199 declarator = declarator->declarator;
5200 continue;
5203 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5204 a cdk_pointer (for *...),
5205 a cdk_function (for ...(...)),
5206 a cdk_attrs (for nested attributes),
5207 or a cdk_id (for the name being declared
5208 or the place in an absolute declarator
5209 where the name was omitted).
5210 For the last case, we have just exited the loop.
5212 At this point, TYPE is the type of elements of an array,
5213 or for a function to return, or for a pointer to point to.
5214 After this sequence of ifs, TYPE is the type of the
5215 array or function or pointer, and DECLARATOR has had its
5216 outermost layer removed. */
5218 if (array_ptr_quals != TYPE_UNQUALIFIED
5219 || array_ptr_attrs != NULL_TREE
5220 || array_parm_static)
5222 /* Only the innermost declarator (making a parameter be of
5223 array type which is converted to pointer type)
5224 may have static or type qualifiers. */
5225 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5226 array_ptr_quals = TYPE_UNQUALIFIED;
5227 array_ptr_attrs = NULL_TREE;
5228 array_parm_static = 0;
5231 switch (declarator->kind)
5233 case cdk_attrs:
5235 /* A declarator with embedded attributes. */
5236 tree attrs = declarator->u.attrs;
5237 const struct c_declarator *inner_decl;
5238 int attr_flags = 0;
5239 declarator = declarator->declarator;
5240 inner_decl = declarator;
5241 while (inner_decl->kind == cdk_attrs)
5242 inner_decl = inner_decl->declarator;
5243 if (inner_decl->kind == cdk_id)
5244 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5245 else if (inner_decl->kind == cdk_function)
5246 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5247 else if (inner_decl->kind == cdk_array)
5248 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5249 returned_attrs = decl_attributes (&type,
5250 chainon (returned_attrs, attrs),
5251 attr_flags);
5252 break;
5254 case cdk_array:
5256 tree itype = NULL_TREE;
5257 tree size = declarator->u.array.dimen;
5258 /* The index is a signed object `sizetype' bits wide. */
5259 tree index_type = c_common_signed_type (sizetype);
5261 array_ptr_quals = declarator->u.array.quals;
5262 array_ptr_attrs = declarator->u.array.attrs;
5263 array_parm_static = declarator->u.array.static_p;
5264 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5266 declarator = declarator->declarator;
5268 /* Check for some types that there cannot be arrays of. */
5270 if (VOID_TYPE_P (type))
5272 if (name)
5273 error_at (loc, "declaration of %qE as array of voids", name);
5274 else
5275 error_at (loc, "declaration of type name as array of voids");
5276 type = error_mark_node;
5279 if (TREE_CODE (type) == FUNCTION_TYPE)
5281 if (name)
5282 error_at (loc, "declaration of %qE as array of functions",
5283 name);
5284 else
5285 error_at (loc, "declaration of type name as array of "
5286 "functions");
5287 type = error_mark_node;
5290 if (pedantic && !in_system_header && flexible_array_type_p (type))
5291 pedwarn (loc, OPT_Wpedantic,
5292 "invalid use of structure with flexible array member");
5294 if (size == error_mark_node)
5295 type = error_mark_node;
5297 if (type == error_mark_node)
5298 continue;
5300 /* If size was specified, set ITYPE to a range-type for
5301 that size. Otherwise, ITYPE remains null. finish_decl
5302 may figure it out from an initial value. */
5304 if (size)
5306 bool size_maybe_const = true;
5307 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5308 && !TREE_OVERFLOW (size));
5309 bool this_size_varies = false;
5311 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5312 lvalue. */
5313 STRIP_TYPE_NOPS (size);
5315 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5317 if (name)
5318 error_at (loc, "size of array %qE has non-integer type",
5319 name);
5320 else
5321 error_at (loc,
5322 "size of unnamed array has non-integer type");
5323 size = integer_one_node;
5326 size = c_fully_fold (size, false, &size_maybe_const);
5328 if (pedantic && size_maybe_const && integer_zerop (size))
5330 if (name)
5331 pedwarn (loc, OPT_Wpedantic,
5332 "ISO C forbids zero-size array %qE", name);
5333 else
5334 pedwarn (loc, OPT_Wpedantic,
5335 "ISO C forbids zero-size array");
5338 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5340 constant_expression_warning (size);
5341 if (tree_int_cst_sgn (size) < 0)
5343 if (name)
5344 error_at (loc, "size of array %qE is negative", name);
5345 else
5346 error_at (loc, "size of unnamed array is negative");
5347 size = integer_one_node;
5349 /* Handle a size folded to an integer constant but
5350 not an integer constant expression. */
5351 if (!size_int_const)
5353 /* If this is a file scope declaration of an
5354 ordinary identifier, this is invalid code;
5355 diagnosing it here and not subsequently
5356 treating the type as variable-length avoids
5357 more confusing diagnostics later. */
5358 if ((decl_context == NORMAL || decl_context == FIELD)
5359 && current_scope == file_scope)
5360 pedwarn (input_location, 0,
5361 "variably modified %qE at file scope",
5362 name);
5363 else
5364 this_size_varies = size_varies = true;
5365 warn_variable_length_array (name, size);
5368 else if ((decl_context == NORMAL || decl_context == FIELD)
5369 && current_scope == file_scope)
5371 error_at (loc, "variably modified %qE at file scope", name);
5372 size = integer_one_node;
5374 else
5376 /* Make sure the array size remains visibly
5377 nonconstant even if it is (eg) a const variable
5378 with known value. */
5379 this_size_varies = size_varies = true;
5380 warn_variable_length_array (name, size);
5383 if (integer_zerop (size) && !this_size_varies)
5385 /* A zero-length array cannot be represented with
5386 an unsigned index type, which is what we'll
5387 get with build_index_type. Create an
5388 open-ended range instead. */
5389 itype = build_range_type (sizetype, size, NULL_TREE);
5391 else
5393 /* Arrange for the SAVE_EXPR on the inside of the
5394 MINUS_EXPR, which allows the -1 to get folded
5395 with the +1 that happens when building TYPE_SIZE. */
5396 if (size_varies)
5397 size = save_expr (size);
5398 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5399 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5400 integer_zero_node, size);
5402 /* Compute the maximum valid index, that is, size
5403 - 1. Do the calculation in index_type, so that
5404 if it is a variable the computations will be
5405 done in the proper mode. */
5406 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5407 convert (index_type, size),
5408 convert (index_type,
5409 size_one_node));
5411 /* The above overflows when size does not fit
5412 in index_type.
5413 ??? While a size of INT_MAX+1 technically shouldn't
5414 cause an overflow (because we subtract 1), handling
5415 this case seems like an unnecessary complication. */
5416 if (TREE_CODE (size) == INTEGER_CST
5417 && !int_fits_type_p (size, index_type))
5419 if (name)
5420 error_at (loc, "size of array %qE is too large",
5421 name);
5422 else
5423 error_at (loc, "size of unnamed array is too large");
5424 type = error_mark_node;
5425 continue;
5428 itype = build_index_type (itype);
5430 if (this_size_varies)
5432 if (*expr)
5433 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5434 *expr, size);
5435 else
5436 *expr = size;
5437 *expr_const_operands &= size_maybe_const;
5440 else if (decl_context == FIELD)
5442 bool flexible_array_member = false;
5443 if (array_parm_vla_unspec_p)
5444 /* Field names can in fact have function prototype
5445 scope so [*] is disallowed here through making
5446 the field variably modified, not through being
5447 something other than a declaration with function
5448 prototype scope. */
5449 size_varies = true;
5450 else
5452 const struct c_declarator *t = declarator;
5453 while (t->kind == cdk_attrs)
5454 t = t->declarator;
5455 flexible_array_member = (t->kind == cdk_id);
5457 if (flexible_array_member
5458 && pedantic && !flag_isoc99 && !in_system_header)
5459 pedwarn (loc, OPT_Wpedantic,
5460 "ISO C90 does not support flexible array members");
5462 /* ISO C99 Flexible array members are effectively
5463 identical to GCC's zero-length array extension. */
5464 if (flexible_array_member || array_parm_vla_unspec_p)
5465 itype = build_range_type (sizetype, size_zero_node,
5466 NULL_TREE);
5468 else if (decl_context == PARM)
5470 if (array_parm_vla_unspec_p)
5472 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
5473 size_varies = true;
5476 else if (decl_context == TYPENAME)
5478 if (array_parm_vla_unspec_p)
5480 /* C99 6.7.5.2p4 */
5481 warning (0, "%<[*]%> not in a declaration");
5482 /* We use this to avoid messing up with incomplete
5483 array types of the same type, that would
5484 otherwise be modified below. */
5485 itype = build_range_type (sizetype, size_zero_node,
5486 NULL_TREE);
5487 size_varies = true;
5491 /* Complain about arrays of incomplete types. */
5492 if (!COMPLETE_TYPE_P (type))
5494 error_at (loc, "array type has incomplete element type");
5495 type = error_mark_node;
5497 else
5498 /* When itype is NULL, a shared incomplete array type is
5499 returned for all array of a given type. Elsewhere we
5500 make sure we don't complete that type before copying
5501 it, but here we want to make sure we don't ever
5502 modify the shared type, so we gcc_assert (itype)
5503 below. */
5505 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
5506 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
5507 type = build_qualified_type (type,
5508 ENCODE_QUAL_ADDR_SPACE (as));
5510 type = build_array_type (type, itype);
5513 if (type != error_mark_node)
5515 if (size_varies)
5517 /* It is ok to modify type here even if itype is
5518 NULL: if size_varies, we're in a
5519 multi-dimensional array and the inner type has
5520 variable size, so the enclosing shared array type
5521 must too. */
5522 if (size && TREE_CODE (size) == INTEGER_CST)
5523 type
5524 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5525 C_TYPE_VARIABLE_SIZE (type) = 1;
5528 /* The GCC extension for zero-length arrays differs from
5529 ISO flexible array members in that sizeof yields
5530 zero. */
5531 if (size && integer_zerop (size))
5533 gcc_assert (itype);
5534 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5535 TYPE_SIZE (type) = bitsize_zero_node;
5536 TYPE_SIZE_UNIT (type) = size_zero_node;
5537 SET_TYPE_STRUCTURAL_EQUALITY (type);
5539 if (array_parm_vla_unspec_p)
5541 gcc_assert (itype);
5542 /* The type is complete. C99 6.7.5.2p4 */
5543 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5544 TYPE_SIZE (type) = bitsize_zero_node;
5545 TYPE_SIZE_UNIT (type) = size_zero_node;
5546 SET_TYPE_STRUCTURAL_EQUALITY (type);
5550 if (decl_context != PARM
5551 && (array_ptr_quals != TYPE_UNQUALIFIED
5552 || array_ptr_attrs != NULL_TREE
5553 || array_parm_static))
5555 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5556 array_ptr_quals = TYPE_UNQUALIFIED;
5557 array_ptr_attrs = NULL_TREE;
5558 array_parm_static = 0;
5560 break;
5562 case cdk_function:
5564 /* Say it's a definition only for the declarator closest
5565 to the identifier, apart possibly from some
5566 attributes. */
5567 bool really_funcdef = false;
5568 tree arg_types;
5569 if (funcdef_flag)
5571 const struct c_declarator *t = declarator->declarator;
5572 while (t->kind == cdk_attrs)
5573 t = t->declarator;
5574 really_funcdef = (t->kind == cdk_id);
5577 /* Declaring a function type. Make sure we have a valid
5578 type for the function to return. */
5579 if (type == error_mark_node)
5580 continue;
5582 size_varies = false;
5584 /* Warn about some types functions can't return. */
5585 if (TREE_CODE (type) == FUNCTION_TYPE)
5587 if (name)
5588 error_at (loc, "%qE declared as function returning a "
5589 "function", name);
5590 else
5591 error_at (loc, "type name declared as function "
5592 "returning a function");
5593 type = integer_type_node;
5595 if (TREE_CODE (type) == ARRAY_TYPE)
5597 if (name)
5598 error_at (loc, "%qE declared as function returning an array",
5599 name);
5600 else
5601 error_at (loc, "type name declared as function returning "
5602 "an array");
5603 type = integer_type_node;
5605 errmsg = targetm.invalid_return_type (type);
5606 if (errmsg)
5608 error (errmsg);
5609 type = integer_type_node;
5612 /* Construct the function type and go to the next
5613 inner layer of declarator. */
5614 arg_info = declarator->u.arg_info;
5615 arg_types = grokparms (arg_info, really_funcdef);
5617 /* Type qualifiers before the return type of the function
5618 qualify the return type, not the function type. */
5619 if (type_quals)
5621 /* Type qualifiers on a function return type are
5622 normally permitted by the standard but have no
5623 effect, so give a warning at -Wreturn-type.
5624 Qualifiers on a void return type are banned on
5625 function definitions in ISO C; GCC used to used
5626 them for noreturn functions. */
5627 if (VOID_TYPE_P (type) && really_funcdef)
5628 pedwarn (loc, 0,
5629 "function definition has qualified void return type");
5630 else
5631 warning_at (loc, OPT_Wignored_qualifiers,
5632 "type qualifiers ignored on function return type");
5634 type = c_build_qualified_type (type, type_quals);
5636 type_quals = TYPE_UNQUALIFIED;
5638 type = build_function_type (type, arg_types);
5639 declarator = declarator->declarator;
5641 /* Set the TYPE_CONTEXTs for each tagged type which is local to
5642 the formal parameter list of this FUNCTION_TYPE to point to
5643 the FUNCTION_TYPE node itself. */
5645 c_arg_tag *tag;
5646 unsigned ix;
5648 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
5649 TYPE_CONTEXT (tag->type) = type;
5651 break;
5653 case cdk_pointer:
5655 /* Merge any constancy or volatility into the target type
5656 for the pointer. */
5658 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5659 && type_quals)
5660 pedwarn (loc, OPT_Wpedantic,
5661 "ISO C forbids qualified function types");
5662 if (type_quals)
5663 type = c_build_qualified_type (type, type_quals);
5664 size_varies = false;
5666 /* When the pointed-to type involves components of variable size,
5667 care must be taken to ensure that the size evaluation code is
5668 emitted early enough to dominate all the possible later uses
5669 and late enough for the variables on which it depends to have
5670 been assigned.
5672 This is expected to happen automatically when the pointed-to
5673 type has a name/declaration of it's own, but special attention
5674 is required if the type is anonymous.
5676 We handle the NORMAL and FIELD contexts here by attaching an
5677 artificial TYPE_DECL to such pointed-to type. This forces the
5678 sizes evaluation at a safe point and ensures it is not deferred
5679 until e.g. within a deeper conditional context.
5681 We expect nothing to be needed here for PARM or TYPENAME.
5682 Pushing a TYPE_DECL at this point for TYPENAME would actually
5683 be incorrect, as we might be in the middle of an expression
5684 with side effects on the pointed-to type size "arguments" prior
5685 to the pointer declaration point and the fake TYPE_DECL in the
5686 enclosing context would force the size evaluation prior to the
5687 side effects. */
5689 if (!TYPE_NAME (type)
5690 && (decl_context == NORMAL || decl_context == FIELD)
5691 && variably_modified_type_p (type, NULL_TREE))
5693 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
5694 DECL_ARTIFICIAL (decl) = 1;
5695 pushdecl (decl);
5696 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
5697 TYPE_NAME (type) = decl;
5700 type = c_build_pointer_type (type);
5702 /* Process type qualifiers (such as const or volatile)
5703 that were given inside the `*'. */
5704 type_quals = declarator->u.pointer_quals;
5706 declarator = declarator->declarator;
5707 break;
5709 default:
5710 gcc_unreachable ();
5713 *decl_attrs = chainon (returned_attrs, *decl_attrs);
5715 /* Now TYPE has the actual type, apart from any qualifiers in
5716 TYPE_QUALS. */
5718 /* Warn about address space used for things other than static memory or
5719 pointers. */
5720 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
5721 if (!ADDR_SPACE_GENERIC_P (address_space))
5723 if (decl_context == NORMAL)
5725 switch (storage_class)
5727 case csc_auto:
5728 error ("%qs combined with %<auto%> qualifier for %qE",
5729 c_addr_space_name (address_space), name);
5730 break;
5731 case csc_register:
5732 error ("%qs combined with %<register%> qualifier for %qE",
5733 c_addr_space_name (address_space), name);
5734 break;
5735 case csc_none:
5736 if (current_function_scope)
5738 error ("%qs specified for auto variable %qE",
5739 c_addr_space_name (address_space), name);
5740 break;
5742 break;
5743 case csc_static:
5744 case csc_extern:
5745 case csc_typedef:
5746 break;
5747 default:
5748 gcc_unreachable ();
5751 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
5753 if (name)
5754 error ("%qs specified for parameter %qE",
5755 c_addr_space_name (address_space), name);
5756 else
5757 error ("%qs specified for unnamed parameter",
5758 c_addr_space_name (address_space));
5760 else if (decl_context == FIELD)
5762 if (name)
5763 error ("%qs specified for structure field %qE",
5764 c_addr_space_name (address_space), name);
5765 else
5766 error ("%qs specified for structure field",
5767 c_addr_space_name (address_space));
5771 /* Check the type and width of a bit-field. */
5772 if (bitfield)
5773 check_bitfield_type_and_width (&type, width, name);
5775 /* Reject invalid uses of _Alignas. */
5776 if (declspecs->alignas_p)
5778 if (storage_class == csc_typedef)
5779 error_at (loc, "alignment specified for typedef %qE", name);
5780 else if (storage_class == csc_register)
5781 error_at (loc, "alignment specified for %<register%> object %qE",
5782 name);
5783 else if (decl_context == PARM)
5785 if (name)
5786 error_at (loc, "alignment specified for parameter %qE", name);
5787 else
5788 error_at (loc, "alignment specified for unnamed parameter");
5790 else if (bitfield)
5792 if (name)
5793 error_at (loc, "alignment specified for bit-field %qE", name);
5794 else
5795 error_at (loc, "alignment specified for unnamed bit-field");
5797 else if (TREE_CODE (type) == FUNCTION_TYPE)
5798 error_at (loc, "alignment specified for function %qE", name);
5799 else if (declspecs->align_log != -1)
5801 alignas_align = 1U << declspecs->align_log;
5802 if (alignas_align < TYPE_ALIGN_UNIT (type))
5804 if (name)
5805 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
5806 "alignment of %qE", name);
5807 else
5808 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
5809 "alignment of unnamed field");
5810 alignas_align = 0;
5815 /* Did array size calculations overflow or does the array cover more
5816 than half of the address-space? */
5817 if (TREE_CODE (type) == ARRAY_TYPE
5818 && COMPLETE_TYPE_P (type)
5819 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
5820 && ! valid_constant_size_p (TYPE_SIZE_UNIT (type)))
5822 if (name)
5823 error_at (loc, "size of array %qE is too large", name);
5824 else
5825 error_at (loc, "size of unnamed array is too large");
5826 /* If we proceed with the array type as it is, we'll eventually
5827 crash in tree_low_cst(). */
5828 type = error_mark_node;
5831 /* If this is declaring a typedef name, return a TYPE_DECL. */
5833 if (storage_class == csc_typedef)
5835 tree decl;
5836 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5837 && type_quals)
5838 pedwarn (loc, OPT_Wpedantic,
5839 "ISO C forbids qualified function types");
5840 if (type_quals)
5841 type = c_build_qualified_type (type, type_quals);
5842 decl = build_decl (declarator->id_loc,
5843 TYPE_DECL, declarator->u.id, type);
5844 if (declspecs->explicit_signed_p)
5845 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
5846 if (declspecs->inline_p)
5847 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
5848 if (declspecs->noreturn_p)
5849 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
5851 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
5853 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
5855 if (b != NULL
5856 && b->decl != NULL_TREE
5857 && (B_IN_CURRENT_SCOPE (b)
5858 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
5859 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
5861 warning_at (declarator->id_loc, OPT_Wc___compat,
5862 ("using %qD as both a typedef and a tag is "
5863 "invalid in C++"),
5864 decl);
5865 if (b->locus != UNKNOWN_LOCATION)
5866 inform (b->locus, "originally defined here");
5870 return decl;
5873 /* If this is a type name (such as, in a cast or sizeof),
5874 compute the type and return it now. */
5876 if (decl_context == TYPENAME)
5878 /* Note that the grammar rejects storage classes in typenames
5879 and fields. */
5880 gcc_assert (storage_class == csc_none && !threadp
5881 && !declspecs->inline_p && !declspecs->noreturn_p);
5882 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5883 && type_quals)
5884 pedwarn (loc, OPT_Wpedantic,
5885 "ISO C forbids const or volatile function types");
5886 if (type_quals)
5887 type = c_build_qualified_type (type, type_quals);
5888 return type;
5891 if (pedantic && decl_context == FIELD
5892 && variably_modified_type_p (type, NULL_TREE))
5894 /* C99 6.7.2.1p8 */
5895 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
5896 "have a variably modified type");
5899 /* Aside from typedefs and type names (handle above),
5900 `void' at top level (not within pointer)
5901 is allowed only in public variables.
5902 We don't complain about parms either, but that is because
5903 a better error message can be made later. */
5905 if (VOID_TYPE_P (type) && decl_context != PARM
5906 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
5907 && (storage_class == csc_extern
5908 || (current_scope == file_scope
5909 && !(storage_class == csc_static
5910 || storage_class == csc_register)))))
5912 error_at (loc, "variable or field %qE declared void", name);
5913 type = integer_type_node;
5916 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
5917 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
5920 tree decl;
5922 if (decl_context == PARM)
5924 tree promoted_type;
5926 /* A parameter declared as an array of T is really a pointer to T.
5927 One declared as a function is really a pointer to a function. */
5929 if (TREE_CODE (type) == ARRAY_TYPE)
5931 /* Transfer const-ness of array into that of type pointed to. */
5932 type = TREE_TYPE (type);
5933 if (type_quals)
5934 type = c_build_qualified_type (type, type_quals);
5935 type = c_build_pointer_type (type);
5936 type_quals = array_ptr_quals;
5937 if (type_quals)
5938 type = c_build_qualified_type (type, type_quals);
5940 /* We don't yet implement attributes in this context. */
5941 if (array_ptr_attrs != NULL_TREE)
5942 warning_at (loc, OPT_Wattributes,
5943 "attributes in parameter array declarator ignored");
5945 size_varies = false;
5947 else if (TREE_CODE (type) == FUNCTION_TYPE)
5949 if (type_quals)
5950 pedwarn (loc, OPT_Wpedantic,
5951 "ISO C forbids qualified function types");
5952 if (type_quals)
5953 type = c_build_qualified_type (type, type_quals);
5954 type = c_build_pointer_type (type);
5955 type_quals = TYPE_UNQUALIFIED;
5957 else if (type_quals)
5958 type = c_build_qualified_type (type, type_quals);
5960 decl = build_decl (declarator->id_loc,
5961 PARM_DECL, declarator->u.id, type);
5962 if (size_varies)
5963 C_DECL_VARIABLE_SIZE (decl) = 1;
5965 /* Compute the type actually passed in the parmlist,
5966 for the case where there is no prototype.
5967 (For example, shorts and chars are passed as ints.)
5968 When there is a prototype, this is overridden later. */
5970 if (type == error_mark_node)
5971 promoted_type = type;
5972 else
5973 promoted_type = c_type_promotes_to (type);
5975 DECL_ARG_TYPE (decl) = promoted_type;
5976 if (declspecs->inline_p)
5977 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
5978 if (declspecs->noreturn_p)
5979 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
5981 else if (decl_context == FIELD)
5983 /* Note that the grammar rejects storage classes in typenames
5984 and fields. */
5985 gcc_assert (storage_class == csc_none && !threadp
5986 && !declspecs->inline_p && !declspecs->noreturn_p);
5988 /* Structure field. It may not be a function. */
5990 if (TREE_CODE (type) == FUNCTION_TYPE)
5992 error_at (loc, "field %qE declared as a function", name);
5993 type = build_pointer_type (type);
5995 else if (TREE_CODE (type) != ERROR_MARK
5996 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
5998 if (name)
5999 error_at (loc, "field %qE has incomplete type", name);
6000 else
6001 error_at (loc, "unnamed field has incomplete type");
6002 type = error_mark_node;
6004 type = c_build_qualified_type (type, type_quals);
6005 decl = build_decl (declarator->id_loc,
6006 FIELD_DECL, declarator->u.id, type);
6007 DECL_NONADDRESSABLE_P (decl) = bitfield;
6008 if (bitfield && !declarator->u.id)
6009 TREE_NO_WARNING (decl) = 1;
6011 if (size_varies)
6012 C_DECL_VARIABLE_SIZE (decl) = 1;
6014 else if (TREE_CODE (type) == FUNCTION_TYPE)
6016 if (storage_class == csc_register || threadp)
6018 error_at (loc, "invalid storage class for function %qE", name);
6020 else if (current_scope != file_scope)
6022 /* Function declaration not at file scope. Storage
6023 classes other than `extern' are not allowed, C99
6024 6.7.1p5, and `extern' makes no difference. However,
6025 GCC allows 'auto', perhaps with 'inline', to support
6026 nested functions. */
6027 if (storage_class == csc_auto)
6028 pedwarn (loc, OPT_Wpedantic,
6029 "invalid storage class for function %qE", name);
6030 else if (storage_class == csc_static)
6032 error_at (loc, "invalid storage class for function %qE", name);
6033 if (funcdef_flag)
6034 storage_class = declspecs->storage_class = csc_none;
6035 else
6036 return 0;
6040 decl = build_decl (declarator->id_loc,
6041 FUNCTION_DECL, declarator->u.id, type);
6042 decl = build_decl_attribute_variant (decl, decl_attr);
6044 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6045 pedwarn (loc, OPT_Wpedantic,
6046 "ISO C forbids qualified function types");
6048 /* Every function declaration is an external reference
6049 (DECL_EXTERNAL) except for those which are not at file
6050 scope and are explicitly declared "auto". This is
6051 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6052 GCC to signify a forward declaration of a nested function. */
6053 if (storage_class == csc_auto && current_scope != file_scope)
6054 DECL_EXTERNAL (decl) = 0;
6055 /* In C99, a function which is declared 'inline' with 'extern'
6056 is not an external reference (which is confusing). It
6057 means that the later definition of the function must be output
6058 in this file, C99 6.7.4p6. In GNU C89, a function declared
6059 'extern inline' is an external reference. */
6060 else if (declspecs->inline_p && storage_class != csc_static)
6061 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6062 == flag_gnu89_inline);
6063 else
6064 DECL_EXTERNAL (decl) = !initialized;
6066 /* Record absence of global scope for `static' or `auto'. */
6067 TREE_PUBLIC (decl)
6068 = !(storage_class == csc_static || storage_class == csc_auto);
6070 /* For a function definition, record the argument information
6071 block where store_parm_decls will look for it. */
6072 if (funcdef_flag)
6073 current_function_arg_info = arg_info;
6075 if (declspecs->default_int_p)
6076 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6078 /* Record presence of `inline' and `_Noreturn', if it is
6079 reasonable. */
6080 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6082 if (declspecs->inline_p)
6083 pedwarn (loc, 0, "cannot inline function %<main%>");
6084 if (declspecs->noreturn_p)
6085 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6087 else
6089 if (declspecs->inline_p)
6090 /* Record that the function is declared `inline'. */
6091 DECL_DECLARED_INLINE_P (decl) = 1;
6092 if (declspecs->noreturn_p)
6094 if (!flag_isoc11)
6096 if (flag_isoc99)
6097 pedwarn (loc, OPT_Wpedantic,
6098 "ISO C99 does not support %<_Noreturn%>");
6099 else
6100 pedwarn (loc, OPT_Wpedantic,
6101 "ISO C90 does not support %<_Noreturn%>");
6103 TREE_THIS_VOLATILE (decl) = 1;
6107 else
6109 /* It's a variable. */
6110 /* An uninitialized decl with `extern' is a reference. */
6111 int extern_ref = !initialized && storage_class == csc_extern;
6113 type = c_build_qualified_type (type, type_quals);
6115 /* C99 6.2.2p7: It is invalid (compile-time undefined
6116 behavior) to create an 'extern' declaration for a
6117 variable if there is a global declaration that is
6118 'static' and the global declaration is not visible.
6119 (If the static declaration _is_ currently visible,
6120 the 'extern' declaration is taken to refer to that decl.) */
6121 if (extern_ref && current_scope != file_scope)
6123 tree global_decl = identifier_global_value (declarator->u.id);
6124 tree visible_decl = lookup_name (declarator->u.id);
6126 if (global_decl
6127 && global_decl != visible_decl
6128 && TREE_CODE (global_decl) == VAR_DECL
6129 && !TREE_PUBLIC (global_decl))
6130 error_at (loc, "variable previously declared %<static%> "
6131 "redeclared %<extern%>");
6134 decl = build_decl (declarator->id_loc,
6135 VAR_DECL, declarator->u.id, type);
6136 if (size_varies)
6137 C_DECL_VARIABLE_SIZE (decl) = 1;
6139 if (declspecs->inline_p)
6140 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6141 if (declspecs->noreturn_p)
6142 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6144 /* At file scope, an initialized extern declaration may follow
6145 a static declaration. In that case, DECL_EXTERNAL will be
6146 reset later in start_decl. */
6147 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6149 /* At file scope, the presence of a `static' or `register' storage
6150 class specifier, or the absence of all storage class specifiers
6151 makes this declaration a definition (perhaps tentative). Also,
6152 the absence of `static' makes it public. */
6153 if (current_scope == file_scope)
6155 TREE_PUBLIC (decl) = storage_class != csc_static;
6156 TREE_STATIC (decl) = !extern_ref;
6158 /* Not at file scope, only `static' makes a static definition. */
6159 else
6161 TREE_STATIC (decl) = (storage_class == csc_static);
6162 TREE_PUBLIC (decl) = extern_ref;
6165 if (threadp)
6166 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
6169 if ((storage_class == csc_extern
6170 || (storage_class == csc_none
6171 && TREE_CODE (type) == FUNCTION_TYPE
6172 && !funcdef_flag))
6173 && variably_modified_type_p (type, NULL_TREE))
6175 /* C99 6.7.5.2p2 */
6176 if (TREE_CODE (type) == FUNCTION_TYPE)
6177 error_at (loc, "non-nested function with variably modified type");
6178 else
6179 error_at (loc, "object with variably modified type must have "
6180 "no linkage");
6183 /* Record `register' declaration for warnings on &
6184 and in case doing stupid register allocation. */
6186 if (storage_class == csc_register)
6188 C_DECL_REGISTER (decl) = 1;
6189 DECL_REGISTER (decl) = 1;
6192 /* Record constancy and volatility. */
6193 c_apply_type_quals_to_decl (type_quals, decl);
6195 /* Apply _Alignas specifiers. */
6196 if (alignas_align)
6198 DECL_ALIGN (decl) = alignas_align * BITS_PER_UNIT;
6199 DECL_USER_ALIGN (decl) = 1;
6202 /* If a type has volatile components, it should be stored in memory.
6203 Otherwise, the fact that those components are volatile
6204 will be ignored, and would even crash the compiler.
6205 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6206 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6207 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
6208 || TREE_CODE (decl) == RESULT_DECL))
6210 /* It is not an error for a structure with volatile fields to
6211 be declared register, but reset DECL_REGISTER since it
6212 cannot actually go in a register. */
6213 int was_reg = C_DECL_REGISTER (decl);
6214 C_DECL_REGISTER (decl) = 0;
6215 DECL_REGISTER (decl) = 0;
6216 c_mark_addressable (decl);
6217 C_DECL_REGISTER (decl) = was_reg;
6220 /* This is the earliest point at which we might know the assembler
6221 name of a variable. Thus, if it's known before this, die horribly. */
6222 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6224 if (warn_cxx_compat
6225 && TREE_CODE (decl) == VAR_DECL
6226 && TREE_PUBLIC (decl)
6227 && TREE_STATIC (decl)
6228 && (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6229 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6230 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6231 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6232 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6233 ("non-local variable %qD with anonymous type is "
6234 "questionable in C++"),
6235 decl);
6237 return decl;
6241 /* Decode the parameter-list info for a function type or function definition.
6242 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6243 if there is an identifier list instead of a parameter decl list).
6244 These two functions are separate because when a function returns
6245 or receives functions then each is called multiple times but the order
6246 of calls is different. The last call to `grokparms' is always the one
6247 that contains the formal parameter names of a function definition.
6249 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6251 FUNCDEF_FLAG is true for a function definition, false for
6252 a mere declaration. A nonempty identifier-list gets an error message
6253 when FUNCDEF_FLAG is false. */
6255 static tree
6256 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6258 tree arg_types = arg_info->types;
6260 if (funcdef_flag && arg_info->had_vla_unspec)
6262 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6263 /* C99 6.7.5.2p4 */
6264 error ("%<[*]%> not allowed in other than function prototype scope");
6267 if (arg_types == 0 && !funcdef_flag && !in_system_header)
6268 warning (OPT_Wstrict_prototypes,
6269 "function declaration isn%'t a prototype");
6271 if (arg_types == error_mark_node)
6272 return 0; /* don't set TYPE_ARG_TYPES in this case */
6274 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6276 if (!funcdef_flag)
6278 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6279 arg_info->parms = NULL_TREE;
6281 else
6282 arg_info->parms = arg_info->types;
6284 arg_info->types = 0;
6285 return 0;
6287 else
6289 tree parm, type, typelt;
6290 unsigned int parmno;
6291 const char *errmsg;
6293 /* If there is a parameter of incomplete type in a definition,
6294 this is an error. In a declaration this is valid, and a
6295 struct or union type may be completed later, before any calls
6296 or definition of the function. In the case where the tag was
6297 first declared within the parameter list, a warning has
6298 already been given. If a parameter has void type, then
6299 however the function cannot be defined or called, so
6300 warn. */
6302 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
6303 parm;
6304 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
6306 type = TREE_VALUE (typelt);
6307 if (type == error_mark_node)
6308 continue;
6310 if (!COMPLETE_TYPE_P (type))
6312 if (funcdef_flag)
6314 if (DECL_NAME (parm))
6315 error_at (input_location,
6316 "parameter %u (%q+D) has incomplete type",
6317 parmno, parm);
6318 else
6319 error_at (DECL_SOURCE_LOCATION (parm),
6320 "parameter %u has incomplete type",
6321 parmno);
6323 TREE_VALUE (typelt) = error_mark_node;
6324 TREE_TYPE (parm) = error_mark_node;
6325 arg_types = NULL_TREE;
6327 else if (VOID_TYPE_P (type))
6329 if (DECL_NAME (parm))
6330 warning_at (input_location, 0,
6331 "parameter %u (%q+D) has void type",
6332 parmno, parm);
6333 else
6334 warning_at (DECL_SOURCE_LOCATION (parm), 0,
6335 "parameter %u has void type",
6336 parmno);
6340 errmsg = targetm.invalid_parameter_type (type);
6341 if (errmsg)
6343 error (errmsg);
6344 TREE_VALUE (typelt) = error_mark_node;
6345 TREE_TYPE (parm) = error_mark_node;
6346 arg_types = NULL_TREE;
6349 if (DECL_NAME (parm) && TREE_USED (parm))
6350 warn_if_shadowing (parm);
6352 return arg_types;
6356 /* Allocate and initialize a c_arg_info structure from the parser's
6357 obstack. */
6359 struct c_arg_info *
6360 build_arg_info (void)
6362 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
6363 ret->parms = NULL_TREE;
6364 ret->tags = NULL;
6365 ret->types = NULL_TREE;
6366 ret->others = NULL_TREE;
6367 ret->pending_sizes = NULL;
6368 ret->had_vla_unspec = 0;
6369 return ret;
6372 /* Take apart the current scope and return a c_arg_info structure with
6373 info on a parameter list just parsed.
6375 This structure is later fed to 'grokparms' and 'store_parm_decls'.
6377 ELLIPSIS being true means the argument list ended in '...' so don't
6378 append a sentinel (void_list_node) to the end of the type-list.
6380 EXPR is NULL or an expression that needs to be evaluated for the
6381 side effects of array size expressions in the parameters. */
6383 struct c_arg_info *
6384 get_parm_info (bool ellipsis, tree expr)
6386 struct c_binding *b = current_scope->bindings;
6387 struct c_arg_info *arg_info = build_arg_info ();
6389 tree parms = 0;
6390 vec<c_arg_tag, va_gc> *tags = NULL;
6391 tree types = 0;
6392 tree others = 0;
6394 static bool explained_incomplete_types = false;
6395 bool gave_void_only_once_err = false;
6397 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
6399 /* The bindings in this scope must not get put into a block.
6400 We will take care of deleting the binding nodes. */
6401 current_scope->bindings = 0;
6403 /* This function is only called if there was *something* on the
6404 parameter list. */
6405 gcc_assert (b);
6407 /* A parameter list consisting solely of 'void' indicates that the
6408 function takes no arguments. But if the 'void' is qualified
6409 (by 'const' or 'volatile'), or has a storage class specifier
6410 ('register'), then the behavior is undefined; issue an error.
6411 Typedefs for 'void' are OK (see DR#157). */
6412 if (b->prev == 0 /* one binding */
6413 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
6414 && !DECL_NAME (b->decl) /* anonymous */
6415 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
6417 if (TREE_THIS_VOLATILE (b->decl)
6418 || TREE_READONLY (b->decl)
6419 || C_DECL_REGISTER (b->decl))
6420 error ("%<void%> as only parameter may not be qualified");
6422 /* There cannot be an ellipsis. */
6423 if (ellipsis)
6424 error ("%<void%> must be the only parameter");
6426 arg_info->types = void_list_node;
6427 return arg_info;
6430 if (!ellipsis)
6431 types = void_list_node;
6433 /* Break up the bindings list into parms, tags, types, and others;
6434 apply sanity checks; purge the name-to-decl bindings. */
6435 while (b)
6437 tree decl = b->decl;
6438 tree type = TREE_TYPE (decl);
6439 c_arg_tag tag;
6440 const char *keyword;
6442 switch (TREE_CODE (decl))
6444 case PARM_DECL:
6445 if (b->id)
6447 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6448 I_SYMBOL_BINDING (b->id) = b->shadowed;
6451 /* Check for forward decls that never got their actual decl. */
6452 if (TREE_ASM_WRITTEN (decl))
6453 error ("parameter %q+D has just a forward declaration", decl);
6454 /* Check for (..., void, ...) and issue an error. */
6455 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
6457 if (!gave_void_only_once_err)
6459 error ("%<void%> must be the only parameter");
6460 gave_void_only_once_err = true;
6463 else
6465 /* Valid parameter, add it to the list. */
6466 DECL_CHAIN (decl) = parms;
6467 parms = decl;
6469 /* Since there is a prototype, args are passed in their
6470 declared types. The back end may override this later. */
6471 DECL_ARG_TYPE (decl) = type;
6472 types = tree_cons (0, type, types);
6474 break;
6476 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
6477 case UNION_TYPE: keyword = "union"; goto tag;
6478 case RECORD_TYPE: keyword = "struct"; goto tag;
6479 tag:
6480 /* Types may not have tag-names, in which case the type
6481 appears in the bindings list with b->id NULL. */
6482 if (b->id)
6484 gcc_assert (I_TAG_BINDING (b->id) == b);
6485 I_TAG_BINDING (b->id) = b->shadowed;
6488 /* Warn about any struct, union or enum tags defined in a
6489 parameter list. The scope of such types is limited to
6490 the parameter list, which is rarely if ever desirable
6491 (it's impossible to call such a function with type-
6492 correct arguments). An anonymous union parm type is
6493 meaningful as a GNU extension, so don't warn for that. */
6494 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
6496 if (b->id)
6497 /* The %s will be one of 'struct', 'union', or 'enum'. */
6498 warning (0, "%<%s %E%> declared inside parameter list",
6499 keyword, b->id);
6500 else
6501 /* The %s will be one of 'struct', 'union', or 'enum'. */
6502 warning (0, "anonymous %s declared inside parameter list",
6503 keyword);
6505 if (!explained_incomplete_types)
6507 warning (0, "its scope is only this definition or declaration,"
6508 " which is probably not what you want");
6509 explained_incomplete_types = true;
6513 tag.id = b->id;
6514 tag.type = decl;
6515 vec_safe_push (tags, tag);
6516 break;
6518 case CONST_DECL:
6519 case TYPE_DECL:
6520 case FUNCTION_DECL:
6521 /* CONST_DECLs appear here when we have an embedded enum,
6522 and TYPE_DECLs appear here when we have an embedded struct
6523 or union. No warnings for this - we already warned about the
6524 type itself. FUNCTION_DECLs appear when there is an implicit
6525 function declaration in the parameter list. */
6527 /* When we reinsert this decl in the function body, we need
6528 to reconstruct whether it was marked as nested. */
6529 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6530 ? b->nested
6531 : !b->nested);
6532 DECL_CHAIN (decl) = others;
6533 others = decl;
6534 /* fall through */
6536 case ERROR_MARK:
6537 /* error_mark_node appears here when we have an undeclared
6538 variable. Just throw it away. */
6539 if (b->id)
6541 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6542 I_SYMBOL_BINDING (b->id) = b->shadowed;
6544 break;
6546 /* Other things that might be encountered. */
6547 case LABEL_DECL:
6548 case VAR_DECL:
6549 default:
6550 gcc_unreachable ();
6553 b = free_binding_and_advance (b);
6556 arg_info->parms = parms;
6557 arg_info->tags = tags;
6558 arg_info->types = types;
6559 arg_info->others = others;
6560 arg_info->pending_sizes = expr;
6561 return arg_info;
6564 /* Get the struct, enum or union (CODE says which) with tag NAME.
6565 Define the tag as a forward-reference with location LOC if it is
6566 not defined. Return a c_typespec structure for the type
6567 specifier. */
6569 struct c_typespec
6570 parser_xref_tag (location_t loc, enum tree_code code, tree name)
6572 struct c_typespec ret;
6573 tree ref;
6574 location_t refloc;
6576 ret.expr = NULL_TREE;
6577 ret.expr_const_operands = true;
6579 /* If a cross reference is requested, look up the type
6580 already defined for this tag and return it. */
6582 ref = lookup_tag (code, name, 0, &refloc);
6583 /* If this is the right type of tag, return what we found.
6584 (This reference will be shadowed by shadow_tag later if appropriate.)
6585 If this is the wrong type of tag, do not return it. If it was the
6586 wrong type in the same scope, we will have had an error
6587 message already; if in a different scope and declaring
6588 a name, pending_xref_error will give an error message; but if in a
6589 different scope and not declaring a name, this tag should
6590 shadow the previous declaration of a different type of tag, and
6591 this would not work properly if we return the reference found.
6592 (For example, with "struct foo" in an outer scope, "union foo;"
6593 must shadow that tag with a new one of union type.) */
6594 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
6595 if (ref && TREE_CODE (ref) == code)
6597 if (C_TYPE_DEFINED_IN_STRUCT (ref)
6598 && loc != UNKNOWN_LOCATION
6599 && warn_cxx_compat)
6601 switch (code)
6603 case ENUMERAL_TYPE:
6604 warning_at (loc, OPT_Wc___compat,
6605 ("enum type defined in struct or union "
6606 "is not visible in C++"));
6607 inform (refloc, "enum type defined here");
6608 break;
6609 case RECORD_TYPE:
6610 warning_at (loc, OPT_Wc___compat,
6611 ("struct defined in struct or union "
6612 "is not visible in C++"));
6613 inform (refloc, "struct defined here");
6614 break;
6615 case UNION_TYPE:
6616 warning_at (loc, OPT_Wc___compat,
6617 ("union defined in struct or union "
6618 "is not visible in C++"));
6619 inform (refloc, "union defined here");
6620 break;
6621 default:
6622 gcc_unreachable();
6626 ret.spec = ref;
6627 return ret;
6630 /* If no such tag is yet defined, create a forward-reference node
6631 and record it as the "definition".
6632 When a real declaration of this type is found,
6633 the forward-reference will be altered into a real type. */
6635 ref = make_node (code);
6636 if (code == ENUMERAL_TYPE)
6638 /* Give the type a default layout like unsigned int
6639 to avoid crashing if it does not get defined. */
6640 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
6641 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
6642 TYPE_USER_ALIGN (ref) = 0;
6643 TYPE_UNSIGNED (ref) = 1;
6644 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
6645 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
6646 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
6649 pushtag (loc, name, ref);
6651 ret.spec = ref;
6652 return ret;
6655 /* Get the struct, enum or union (CODE says which) with tag NAME.
6656 Define the tag as a forward-reference if it is not defined.
6657 Return a tree for the type. */
6659 tree
6660 xref_tag (enum tree_code code, tree name)
6662 return parser_xref_tag (input_location, code, name).spec;
6665 /* Make sure that the tag NAME is defined *in the current scope*
6666 at least as a forward reference.
6667 LOC is the location of the struct's definition.
6668 CODE says which kind of tag NAME ought to be.
6670 This stores the current value of the file static STRUCT_PARSE_INFO
6671 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
6672 new c_struct_parse_info structure. The old value of
6673 STRUCT_PARSE_INFO is restored in finish_struct. */
6675 tree
6676 start_struct (location_t loc, enum tree_code code, tree name,
6677 struct c_struct_parse_info **enclosing_struct_parse_info)
6679 /* If there is already a tag defined at this scope
6680 (as a forward reference), just return it. */
6682 tree ref = NULL_TREE;
6683 location_t refloc = UNKNOWN_LOCATION;
6685 if (name != NULL_TREE)
6686 ref = lookup_tag (code, name, 1, &refloc);
6687 if (ref && TREE_CODE (ref) == code)
6689 if (TYPE_SIZE (ref))
6691 if (code == UNION_TYPE)
6692 error_at (loc, "redefinition of %<union %E%>", name);
6693 else
6694 error_at (loc, "redefinition of %<struct %E%>", name);
6695 if (refloc != UNKNOWN_LOCATION)
6696 inform (refloc, "originally defined here");
6697 /* Don't create structures using a name already in use. */
6698 ref = NULL_TREE;
6700 else if (C_TYPE_BEING_DEFINED (ref))
6702 if (code == UNION_TYPE)
6703 error_at (loc, "nested redefinition of %<union %E%>", name);
6704 else
6705 error_at (loc, "nested redefinition of %<struct %E%>", name);
6706 /* Don't bother to report "originally defined here" for a
6707 nested redefinition; the original definition should be
6708 obvious. */
6709 /* Don't create structures that contain themselves. */
6710 ref = NULL_TREE;
6714 /* Otherwise create a forward-reference just so the tag is in scope. */
6716 if (ref == NULL_TREE || TREE_CODE (ref) != code)
6718 ref = make_node (code);
6719 pushtag (loc, name, ref);
6722 C_TYPE_BEING_DEFINED (ref) = 1;
6723 TYPE_PACKED (ref) = flag_pack_struct;
6725 *enclosing_struct_parse_info = struct_parse_info;
6726 struct_parse_info = XNEW (struct c_struct_parse_info);
6727 struct_parse_info->struct_types.create (0);
6728 struct_parse_info->fields.create (0);
6729 struct_parse_info->typedefs_seen.create (0);
6731 /* FIXME: This will issue a warning for a use of a type defined
6732 within a statement expr used within sizeof, et. al. This is not
6733 terribly serious as C++ doesn't permit statement exprs within
6734 sizeof anyhow. */
6735 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
6736 warning_at (loc, OPT_Wc___compat,
6737 "defining type in %qs expression is invalid in C++",
6738 (in_sizeof
6739 ? "sizeof"
6740 : (in_typeof ? "typeof" : "alignof")));
6742 return ref;
6745 /* Process the specs, declarator and width (NULL if omitted)
6746 of a structure component, returning a FIELD_DECL node.
6747 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
6748 DECL_ATTRS is as for grokdeclarator.
6750 LOC is the location of the structure component.
6752 This is done during the parsing of the struct declaration.
6753 The FIELD_DECL nodes are chained together and the lot of them
6754 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
6756 tree
6757 grokfield (location_t loc,
6758 struct c_declarator *declarator, struct c_declspecs *declspecs,
6759 tree width, tree *decl_attrs)
6761 tree value;
6763 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
6764 && width == NULL_TREE)
6766 /* This is an unnamed decl.
6768 If we have something of the form "union { list } ;" then this
6769 is the anonymous union extension. Similarly for struct.
6771 If this is something of the form "struct foo;", then
6772 If MS or Plan 9 extensions are enabled, this is handled as
6773 an anonymous struct.
6774 Otherwise this is a forward declaration of a structure tag.
6776 If this is something of the form "foo;" and foo is a TYPE_DECL, then
6777 If foo names a structure or union without a tag, then this
6778 is an anonymous struct (this is permitted by C11).
6779 If MS or Plan 9 extensions are enabled and foo names a
6780 structure, then again this is an anonymous struct.
6781 Otherwise this is an error.
6783 Oh what a horrid tangled web we weave. I wonder if MS consciously
6784 took this from Plan 9 or if it was an accident of implementation
6785 that took root before someone noticed the bug... */
6787 tree type = declspecs->type;
6788 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
6789 || TREE_CODE (type) == UNION_TYPE);
6790 bool ok = false;
6792 if (type_ok
6793 && (flag_ms_extensions
6794 || flag_plan9_extensions
6795 || !declspecs->typedef_p))
6797 if (flag_ms_extensions || flag_plan9_extensions)
6798 ok = true;
6799 else if (TYPE_NAME (type) == NULL)
6800 ok = true;
6801 else
6802 ok = false;
6804 if (!ok)
6806 pedwarn (loc, 0, "declaration does not declare anything");
6807 return NULL_TREE;
6809 if (!flag_isoc11)
6811 if (flag_isoc99)
6812 pedwarn (loc, OPT_Wpedantic,
6813 "ISO C99 doesn%'t support unnamed structs/unions");
6814 else
6815 pedwarn (loc, OPT_Wpedantic,
6816 "ISO C90 doesn%'t support unnamed structs/unions");
6820 value = grokdeclarator (declarator, declspecs, FIELD, false,
6821 width ? &width : NULL, decl_attrs, NULL, NULL,
6822 DEPRECATED_NORMAL);
6824 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6825 DECL_INITIAL (value) = width;
6827 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
6829 /* If we currently have a binding for this field, set the
6830 in_struct field in the binding, so that we warn about lookups
6831 which find it. */
6832 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
6833 if (b != NULL)
6835 /* If the in_struct field is not yet set, push it on a list
6836 to be cleared when this struct is finished. */
6837 if (!b->in_struct)
6839 struct_parse_info->fields.safe_push (b);
6840 b->in_struct = 1;
6845 return value;
6848 /* Subroutine of detect_field_duplicates: return whether X and Y,
6849 which are both fields in the same struct, have duplicate field
6850 names. */
6852 static bool
6853 is_duplicate_field (tree x, tree y)
6855 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
6856 return true;
6858 /* When using -fplan9-extensions, an anonymous field whose name is a
6859 typedef can duplicate a field name. */
6860 if (flag_plan9_extensions
6861 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
6863 tree xt, xn, yt, yn;
6865 xt = TREE_TYPE (x);
6866 if (DECL_NAME (x) != NULL_TREE)
6867 xn = DECL_NAME (x);
6868 else if ((TREE_CODE (xt) == RECORD_TYPE || TREE_CODE (xt) == UNION_TYPE)
6869 && TYPE_NAME (xt) != NULL_TREE
6870 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
6871 xn = DECL_NAME (TYPE_NAME (xt));
6872 else
6873 xn = NULL_TREE;
6875 yt = TREE_TYPE (y);
6876 if (DECL_NAME (y) != NULL_TREE)
6877 yn = DECL_NAME (y);
6878 else if ((TREE_CODE (yt) == RECORD_TYPE || TREE_CODE (yt) == UNION_TYPE)
6879 && TYPE_NAME (yt) != NULL_TREE
6880 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
6881 yn = DECL_NAME (TYPE_NAME (yt));
6882 else
6883 yn = NULL_TREE;
6885 if (xn != NULL_TREE && xn == yn)
6886 return true;
6889 return false;
6892 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
6893 to HTAB, giving errors for any duplicates. */
6895 static void
6896 detect_field_duplicates_hash (tree fieldlist,
6897 hash_table <pointer_hash <tree_node> > htab)
6899 tree x, y;
6900 tree_node **slot;
6902 for (x = fieldlist; x ; x = DECL_CHAIN (x))
6903 if ((y = DECL_NAME (x)) != 0)
6905 slot = htab.find_slot (y, INSERT);
6906 if (*slot)
6908 error ("duplicate member %q+D", x);
6909 DECL_NAME (x) = NULL_TREE;
6911 *slot = y;
6913 else if (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6914 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6916 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
6918 /* When using -fplan9-extensions, an anonymous field whose
6919 name is a typedef can duplicate a field name. */
6920 if (flag_plan9_extensions
6921 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
6922 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
6924 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
6925 slot = htab.find_slot (xn, INSERT);
6926 if (*slot)
6927 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
6928 *slot = xn;
6933 /* Generate an error for any duplicate field names in FIELDLIST. Munge
6934 the list such that this does not present a problem later. */
6936 static void
6937 detect_field_duplicates (tree fieldlist)
6939 tree x, y;
6940 int timeout = 10;
6942 /* If the struct is the list of instance variables of an Objective-C
6943 class, then we need to check all the instance variables of
6944 superclasses when checking for duplicates (since you can't have
6945 an instance variable in a subclass with the same name as an
6946 instance variable in a superclass). We pass on this job to the
6947 Objective-C compiler. objc_detect_field_duplicates() will return
6948 false if we are not checking the list of instance variables and
6949 the C frontend should proceed with the standard field duplicate
6950 checks. If we are checking the list of instance variables, the
6951 ObjC frontend will do the check, emit the errors if needed, and
6952 then return true. */
6953 if (c_dialect_objc ())
6954 if (objc_detect_field_duplicates (false))
6955 return;
6957 /* First, see if there are more than "a few" fields.
6958 This is trivially true if there are zero or one fields. */
6959 if (!fieldlist || !DECL_CHAIN (fieldlist))
6960 return;
6961 x = fieldlist;
6962 do {
6963 timeout--;
6964 if (DECL_NAME (x) == NULL_TREE
6965 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6966 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
6967 timeout = 0;
6968 x = DECL_CHAIN (x);
6969 } while (timeout > 0 && x);
6971 /* If there were "few" fields and no anonymous structures or unions,
6972 avoid the overhead of allocating a hash table. Instead just do
6973 the nested traversal thing. */
6974 if (timeout > 0)
6976 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
6977 /* When using -fplan9-extensions, we can have duplicates
6978 between typedef names and fields. */
6979 if (DECL_NAME (x)
6980 || (flag_plan9_extensions
6981 && DECL_NAME (x) == NULL_TREE
6982 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6983 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6984 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
6985 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
6987 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
6988 if (is_duplicate_field (y, x))
6990 error ("duplicate member %q+D", x);
6991 DECL_NAME (x) = NULL_TREE;
6995 else
6997 hash_table <pointer_hash <tree_node> > htab;
6998 htab.create (37);
7000 detect_field_duplicates_hash (fieldlist, htab);
7001 htab.dispose ();
7005 /* Finish up struct info used by -Wc++-compat. */
7007 static void
7008 warn_cxx_compat_finish_struct (tree fieldlist)
7010 unsigned int ix;
7011 tree x;
7012 struct c_binding *b;
7014 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7015 the current struct. We do this now at the end of the struct
7016 because the flag is used to issue visibility warnings, and we
7017 only want to issue those warnings if the type is referenced
7018 outside of the struct declaration. */
7019 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7020 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7022 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7023 typedefs used when declaring fields in this struct. If the name
7024 of any of the fields is also a typedef name then the struct would
7025 not parse in C++, because the C++ lookup rules say that the
7026 typedef name would be looked up in the context of the struct, and
7027 would thus be the field rather than the typedef. */
7028 if (!struct_parse_info->typedefs_seen.is_empty ()
7029 && fieldlist != NULL_TREE)
7031 /* Use a pointer_set using the name of the typedef. We can use
7032 a pointer_set because identifiers are interned. */
7033 struct pointer_set_t *tset = pointer_set_create ();
7035 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7036 pointer_set_insert (tset, DECL_NAME (x));
7038 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7040 if (DECL_NAME (x) != NULL_TREE
7041 && pointer_set_contains (tset, DECL_NAME (x)))
7043 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7044 ("using %qD as both field and typedef name is "
7045 "invalid in C++"),
7047 /* FIXME: It would be nice to report the location where
7048 the typedef name is used. */
7052 pointer_set_destroy (tset);
7055 /* For each field which has a binding and which was not defined in
7056 an enclosing struct, clear the in_struct field. */
7057 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7058 b->in_struct = 0;
7061 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7062 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7063 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7064 ATTRIBUTES are attributes to be applied to the structure.
7066 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7067 the struct was started. */
7069 tree
7070 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7071 struct c_struct_parse_info *enclosing_struct_parse_info)
7073 tree x;
7074 bool toplevel = file_scope == current_scope;
7075 int saw_named_field;
7077 /* If this type was previously laid out as a forward reference,
7078 make sure we lay it out again. */
7080 TYPE_SIZE (t) = 0;
7082 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7084 if (pedantic)
7086 for (x = fieldlist; x; x = DECL_CHAIN (x))
7088 if (DECL_NAME (x) != 0)
7089 break;
7090 if (flag_isoc11
7091 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7092 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7093 break;
7096 if (x == 0)
7098 if (TREE_CODE (t) == UNION_TYPE)
7100 if (fieldlist)
7101 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7102 else
7103 pedwarn (loc, OPT_Wpedantic, "union has no members");
7105 else
7107 if (fieldlist)
7108 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7109 else
7110 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7115 /* Install struct as DECL_CONTEXT of each field decl.
7116 Also process specified field sizes, found in the DECL_INITIAL,
7117 storing 0 there after the type has been changed to precision equal
7118 to its width, rather than the precision of the specified standard
7119 type. (Correct layout requires the original type to have been preserved
7120 until now.) */
7122 saw_named_field = 0;
7123 for (x = fieldlist; x; x = DECL_CHAIN (x))
7125 if (TREE_TYPE (x) == error_mark_node)
7126 continue;
7128 DECL_CONTEXT (x) = t;
7130 /* If any field is const, the structure type is pseudo-const. */
7131 if (TREE_READONLY (x))
7132 C_TYPE_FIELDS_READONLY (t) = 1;
7133 else
7135 /* A field that is pseudo-const makes the structure likewise. */
7136 tree t1 = strip_array_types (TREE_TYPE (x));
7137 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
7138 && C_TYPE_FIELDS_READONLY (t1))
7139 C_TYPE_FIELDS_READONLY (t) = 1;
7142 /* Any field that is volatile means variables of this type must be
7143 treated in some ways as volatile. */
7144 if (TREE_THIS_VOLATILE (x))
7145 C_TYPE_FIELDS_VOLATILE (t) = 1;
7147 /* Any field of nominal variable size implies structure is too. */
7148 if (C_DECL_VARIABLE_SIZE (x))
7149 C_TYPE_VARIABLE_SIZE (t) = 1;
7151 if (DECL_INITIAL (x))
7153 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
7154 DECL_SIZE (x) = bitsize_int (width);
7155 DECL_BIT_FIELD (x) = 1;
7156 SET_DECL_C_BIT_FIELD (x);
7159 if (TYPE_PACKED (t)
7160 && (DECL_BIT_FIELD (x)
7161 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7162 DECL_PACKED (x) = 1;
7164 /* Detect flexible array member in an invalid context. */
7165 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7166 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7167 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7168 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7170 if (TREE_CODE (t) == UNION_TYPE)
7172 error_at (DECL_SOURCE_LOCATION (x),
7173 "flexible array member in union");
7174 TREE_TYPE (x) = error_mark_node;
7176 else if (DECL_CHAIN (x) != NULL_TREE)
7178 error_at (DECL_SOURCE_LOCATION (x),
7179 "flexible array member not at end of struct");
7180 TREE_TYPE (x) = error_mark_node;
7182 else if (!saw_named_field)
7184 error_at (DECL_SOURCE_LOCATION (x),
7185 "flexible array member in otherwise empty struct");
7186 TREE_TYPE (x) = error_mark_node;
7190 if (pedantic && TREE_CODE (t) == RECORD_TYPE
7191 && flexible_array_type_p (TREE_TYPE (x)))
7192 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
7193 "invalid use of structure with flexible array member");
7195 if (DECL_NAME (x)
7196 || TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7197 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7198 saw_named_field = 1;
7201 detect_field_duplicates (fieldlist);
7203 /* Now we have the nearly final fieldlist. Record it,
7204 then lay out the structure or union (including the fields). */
7206 TYPE_FIELDS (t) = fieldlist;
7208 layout_type (t);
7210 if (TYPE_SIZE_UNIT (t)
7211 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7212 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7213 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7214 error ("type %qT is too large", t);
7216 /* Give bit-fields their proper types. */
7218 tree *fieldlistp = &fieldlist;
7219 while (*fieldlistp)
7220 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
7221 && TREE_TYPE (*fieldlistp) != error_mark_node)
7223 unsigned HOST_WIDE_INT width
7224 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
7225 tree type = TREE_TYPE (*fieldlistp);
7226 if (width != TYPE_PRECISION (type))
7228 TREE_TYPE (*fieldlistp)
7229 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
7230 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
7232 DECL_INITIAL (*fieldlistp) = 0;
7234 else
7235 fieldlistp = &DECL_CHAIN (*fieldlistp);
7238 /* Now we have the truly final field list.
7239 Store it in this type and in the variants. */
7241 TYPE_FIELDS (t) = fieldlist;
7243 /* If there are lots of fields, sort so we can look through them fast.
7244 We arbitrarily consider 16 or more elts to be "a lot". */
7247 int len = 0;
7249 for (x = fieldlist; x; x = DECL_CHAIN (x))
7251 if (len > 15 || DECL_NAME (x) == NULL)
7252 break;
7253 len += 1;
7256 if (len > 15)
7258 tree *field_array;
7259 struct lang_type *space;
7260 struct sorted_fields_type *space2;
7262 len += list_length (x);
7264 /* Use the same allocation policy here that make_node uses, to
7265 ensure that this lives as long as the rest of the struct decl.
7266 All decls in an inline function need to be saved. */
7268 space = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
7269 space2 = ggc_alloc_sorted_fields_type
7270 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
7272 len = 0;
7273 space->s = space2;
7274 field_array = &space2->elts[0];
7275 for (x = fieldlist; x; x = DECL_CHAIN (x))
7277 field_array[len++] = x;
7279 /* If there is anonymous struct or union, break out of the loop. */
7280 if (DECL_NAME (x) == NULL)
7281 break;
7283 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
7284 if (x == NULL)
7286 TYPE_LANG_SPECIFIC (t) = space;
7287 TYPE_LANG_SPECIFIC (t)->s->len = len;
7288 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
7289 qsort (field_array, len, sizeof (tree), field_decl_cmp);
7294 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7296 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7297 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
7298 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
7299 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
7300 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
7303 /* If this was supposed to be a transparent union, but we can't
7304 make it one, warn and turn off the flag. */
7305 if (TREE_CODE (t) == UNION_TYPE
7306 && TYPE_TRANSPARENT_AGGR (t)
7307 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
7309 TYPE_TRANSPARENT_AGGR (t) = 0;
7310 warning_at (loc, 0, "union cannot be made transparent");
7313 /* If this structure or union completes the type of any previous
7314 variable declaration, lay it out and output its rtl. */
7315 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
7317 x = TREE_CHAIN (x))
7319 tree decl = TREE_VALUE (x);
7320 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7321 layout_array_type (TREE_TYPE (decl));
7322 if (TREE_CODE (decl) != TYPE_DECL)
7324 layout_decl (decl, 0);
7325 if (c_dialect_objc ())
7326 objc_check_decl (decl);
7327 rest_of_decl_compilation (decl, toplevel, 0);
7330 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
7332 /* Update type location to the one of the definition, instead of e.g.
7333 a forward declaration. */
7334 if (TYPE_STUB_DECL (t))
7335 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
7337 /* Finish debugging output for this type. */
7338 rest_of_type_compilation (t, toplevel);
7340 /* If we're inside a function proper, i.e. not file-scope and not still
7341 parsing parameters, then arrange for the size of a variable sized type
7342 to be bound now. */
7343 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
7344 add_stmt (build_stmt (loc,
7345 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
7347 if (warn_cxx_compat)
7348 warn_cxx_compat_finish_struct (fieldlist);
7350 struct_parse_info->struct_types.release ();
7351 struct_parse_info->fields.release ();
7352 struct_parse_info->typedefs_seen.release ();
7353 XDELETE (struct_parse_info);
7355 struct_parse_info = enclosing_struct_parse_info;
7357 /* If this struct is defined inside a struct, add it to
7358 struct_types. */
7359 if (warn_cxx_compat
7360 && struct_parse_info != NULL
7361 && !in_sizeof && !in_typeof && !in_alignof)
7362 struct_parse_info->struct_types.safe_push (t);
7364 return t;
7367 /* Lay out the type T, and its element type, and so on. */
7369 static void
7370 layout_array_type (tree t)
7372 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7373 layout_array_type (TREE_TYPE (t));
7374 layout_type (t);
7377 /* Begin compiling the definition of an enumeration type.
7378 NAME is its name (or null if anonymous).
7379 LOC is the enum's location.
7380 Returns the type object, as yet incomplete.
7381 Also records info about it so that build_enumerator
7382 may be used to declare the individual values as they are read. */
7384 tree
7385 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
7387 tree enumtype = NULL_TREE;
7388 location_t enumloc = UNKNOWN_LOCATION;
7390 /* If this is the real definition for a previous forward reference,
7391 fill in the contents in the same object that used to be the
7392 forward reference. */
7394 if (name != NULL_TREE)
7395 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
7397 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
7399 enumtype = make_node (ENUMERAL_TYPE);
7400 pushtag (loc, name, enumtype);
7403 if (C_TYPE_BEING_DEFINED (enumtype))
7404 error_at (loc, "nested redefinition of %<enum %E%>", name);
7406 C_TYPE_BEING_DEFINED (enumtype) = 1;
7408 if (TYPE_VALUES (enumtype) != 0)
7410 /* This enum is a named one that has been declared already. */
7411 error_at (loc, "redeclaration of %<enum %E%>", name);
7412 if (enumloc != UNKNOWN_LOCATION)
7413 inform (enumloc, "originally defined here");
7415 /* Completely replace its old definition.
7416 The old enumerators remain defined, however. */
7417 TYPE_VALUES (enumtype) = 0;
7420 the_enum->enum_next_value = integer_zero_node;
7421 the_enum->enum_overflow = 0;
7423 if (flag_short_enums)
7424 TYPE_PACKED (enumtype) = 1;
7426 /* FIXME: This will issue a warning for a use of a type defined
7427 within sizeof in a statement expr. This is not terribly serious
7428 as C++ doesn't permit statement exprs within sizeof anyhow. */
7429 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7430 warning_at (loc, OPT_Wc___compat,
7431 "defining type in %qs expression is invalid in C++",
7432 (in_sizeof
7433 ? "sizeof"
7434 : (in_typeof ? "typeof" : "alignof")));
7436 return enumtype;
7439 /* After processing and defining all the values of an enumeration type,
7440 install their decls in the enumeration type and finish it off.
7441 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
7442 and ATTRIBUTES are the specified attributes.
7443 Returns ENUMTYPE. */
7445 tree
7446 finish_enum (tree enumtype, tree values, tree attributes)
7448 tree pair, tem;
7449 tree minnode = 0, maxnode = 0;
7450 int precision, unsign;
7451 bool toplevel = (file_scope == current_scope);
7452 struct lang_type *lt;
7454 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7456 /* Calculate the maximum value of any enumerator in this type. */
7458 if (values == error_mark_node)
7459 minnode = maxnode = integer_zero_node;
7460 else
7462 minnode = maxnode = TREE_VALUE (values);
7463 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
7465 tree value = TREE_VALUE (pair);
7466 if (tree_int_cst_lt (maxnode, value))
7467 maxnode = value;
7468 if (tree_int_cst_lt (value, minnode))
7469 minnode = value;
7473 /* Construct the final type of this enumeration. It is the same
7474 as one of the integral types - the narrowest one that fits, except
7475 that normally we only go as narrow as int - and signed iff any of
7476 the values are negative. */
7477 unsign = (tree_int_cst_sgn (minnode) >= 0);
7478 precision = MAX (tree_int_cst_min_precision (minnode, unsign),
7479 tree_int_cst_min_precision (maxnode, unsign));
7481 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
7483 tem = c_common_type_for_size (precision, unsign);
7484 if (tem == NULL)
7486 warning (0, "enumeration values exceed range of largest integer");
7487 tem = long_long_integer_type_node;
7490 else
7491 tem = unsign ? unsigned_type_node : integer_type_node;
7493 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
7494 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
7495 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
7496 TYPE_SIZE (enumtype) = 0;
7498 /* If the precision of the type was specific with an attribute and it
7499 was too small, give an error. Otherwise, use it. */
7500 if (TYPE_PRECISION (enumtype))
7502 if (precision > TYPE_PRECISION (enumtype))
7503 error ("specified mode too small for enumeral values");
7505 else
7506 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
7508 layout_type (enumtype);
7510 if (values != error_mark_node)
7512 /* Change the type of the enumerators to be the enum type. We
7513 need to do this irrespective of the size of the enum, for
7514 proper type checking. Replace the DECL_INITIALs of the
7515 enumerators, and the value slots of the list, with copies
7516 that have the enum type; they cannot be modified in place
7517 because they may be shared (e.g. integer_zero_node) Finally,
7518 change the purpose slots to point to the names of the decls. */
7519 for (pair = values; pair; pair = TREE_CHAIN (pair))
7521 tree enu = TREE_PURPOSE (pair);
7522 tree ini = DECL_INITIAL (enu);
7524 TREE_TYPE (enu) = enumtype;
7526 /* The ISO C Standard mandates enumerators to have type int,
7527 even though the underlying type of an enum type is
7528 unspecified. However, GCC allows enumerators of any
7529 integer type as an extensions. build_enumerator()
7530 converts any enumerators that fit in an int to type int,
7531 to avoid promotions to unsigned types when comparing
7532 integers with enumerators that fit in the int range.
7533 When -pedantic is given, build_enumerator() would have
7534 already warned about those that don't fit. Here we
7535 convert the rest to the enumerator type. */
7536 if (TREE_TYPE (ini) != integer_type_node)
7537 ini = convert (enumtype, ini);
7539 DECL_INITIAL (enu) = ini;
7540 TREE_PURPOSE (pair) = DECL_NAME (enu);
7541 TREE_VALUE (pair) = ini;
7544 TYPE_VALUES (enumtype) = values;
7547 /* Record the min/max values so that we can warn about bit-field
7548 enumerations that are too small for the values. */
7549 lt = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
7550 lt->enum_min = minnode;
7551 lt->enum_max = maxnode;
7552 TYPE_LANG_SPECIFIC (enumtype) = lt;
7554 /* Fix up all variant types of this enum type. */
7555 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
7557 if (tem == enumtype)
7558 continue;
7559 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
7560 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
7561 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
7562 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
7563 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
7564 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
7565 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
7566 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
7567 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
7568 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
7569 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
7572 /* Finish debugging output for this type. */
7573 rest_of_type_compilation (enumtype, toplevel);
7575 /* If this enum is defined inside a struct, add it to
7576 struct_types. */
7577 if (warn_cxx_compat
7578 && struct_parse_info != NULL
7579 && !in_sizeof && !in_typeof && !in_alignof)
7580 struct_parse_info->struct_types.safe_push (enumtype);
7582 return enumtype;
7585 /* Build and install a CONST_DECL for one value of the
7586 current enumeration type (one that was begun with start_enum).
7587 DECL_LOC is the location of the enumerator.
7588 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
7589 Return a tree-list containing the CONST_DECL and its value.
7590 Assignment of sequential values by default is handled here. */
7592 tree
7593 build_enumerator (location_t decl_loc, location_t loc,
7594 struct c_enum_contents *the_enum, tree name, tree value)
7596 tree decl, type;
7598 /* Validate and default VALUE. */
7600 if (value != 0)
7602 /* Don't issue more errors for error_mark_node (i.e. an
7603 undeclared identifier) - just ignore the value expression. */
7604 if (value == error_mark_node)
7605 value = 0;
7606 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
7608 error_at (loc, "enumerator value for %qE is not an integer constant",
7609 name);
7610 value = 0;
7612 else
7614 if (TREE_CODE (value) != INTEGER_CST)
7616 value = c_fully_fold (value, false, NULL);
7617 if (TREE_CODE (value) == INTEGER_CST)
7618 pedwarn (loc, OPT_Wpedantic,
7619 "enumerator value for %qE is not an integer "
7620 "constant expression", name);
7622 if (TREE_CODE (value) != INTEGER_CST)
7624 error ("enumerator value for %qE is not an integer constant",
7625 name);
7626 value = 0;
7628 else
7630 value = default_conversion (value);
7631 constant_expression_warning (value);
7636 /* Default based on previous value. */
7637 /* It should no longer be possible to have NON_LVALUE_EXPR
7638 in the default. */
7639 if (value == 0)
7641 value = the_enum->enum_next_value;
7642 if (the_enum->enum_overflow)
7643 error_at (loc, "overflow in enumeration values");
7645 /* Even though the underlying type of an enum is unspecified, the
7646 type of enumeration constants is explicitly defined as int
7647 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
7648 an extension. */
7649 else if (!int_fits_type_p (value, integer_type_node))
7650 pedwarn (loc, OPT_Wpedantic,
7651 "ISO C restricts enumerator values to range of %<int%>");
7653 /* The ISO C Standard mandates enumerators to have type int, even
7654 though the underlying type of an enum type is unspecified.
7655 However, GCC allows enumerators of any integer type as an
7656 extensions. Here we convert any enumerators that fit in an int
7657 to type int, to avoid promotions to unsigned types when comparing
7658 integers with enumerators that fit in the int range. When
7659 -pedantic is given, we would have already warned about those that
7660 don't fit. We have to do this here rather than in finish_enum
7661 because this value may be used to define more enumerators. */
7662 if (int_fits_type_p (value, integer_type_node))
7663 value = convert (integer_type_node, value);
7665 /* Set basis for default for next value. */
7666 the_enum->enum_next_value
7667 = build_binary_op (EXPR_LOC_OR_HERE (value),
7668 PLUS_EXPR, value, integer_one_node, 0);
7669 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
7671 /* Now create a declaration for the enum value name. */
7673 type = TREE_TYPE (value);
7674 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
7675 TYPE_PRECISION (integer_type_node)),
7676 (TYPE_PRECISION (type)
7677 >= TYPE_PRECISION (integer_type_node)
7678 && TYPE_UNSIGNED (type)));
7680 decl = build_decl (decl_loc, CONST_DECL, name, type);
7681 DECL_INITIAL (decl) = convert (type, value);
7682 pushdecl (decl);
7684 return tree_cons (decl, value, NULL_TREE);
7688 /* Create the FUNCTION_DECL for a function definition.
7689 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
7690 the declaration; they describe the function's name and the type it returns,
7691 but twisted together in a fashion that parallels the syntax of C.
7693 This function creates a binding context for the function body
7694 as well as setting up the FUNCTION_DECL in current_function_decl.
7696 Returns 1 on success. If the DECLARATOR is not suitable for a function
7697 (it defines a datum instead), we return 0, which tells
7698 yyparse to report a parse error. */
7701 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
7702 tree attributes)
7704 tree decl1, old_decl;
7705 tree restype, resdecl;
7706 location_t loc;
7708 current_function_returns_value = 0; /* Assume, until we see it does. */
7709 current_function_returns_null = 0;
7710 current_function_returns_abnormally = 0;
7711 warn_about_return_type = 0;
7712 c_switch_stack = NULL;
7714 /* Indicate no valid break/continue context by setting these variables
7715 to some non-null, non-label value. We'll notice and emit the proper
7716 error message in c_finish_bc_stmt. */
7717 c_break_label = c_cont_label = size_zero_node;
7719 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
7720 &attributes, NULL, NULL, DEPRECATED_NORMAL);
7722 /* If the declarator is not suitable for a function definition,
7723 cause a syntax error. */
7724 if (decl1 == 0
7725 || TREE_CODE (decl1) != FUNCTION_DECL)
7726 return 0;
7728 loc = DECL_SOURCE_LOCATION (decl1);
7730 decl_attributes (&decl1, attributes, 0);
7732 if (DECL_DECLARED_INLINE_P (decl1)
7733 && DECL_UNINLINABLE (decl1)
7734 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
7735 warning_at (loc, OPT_Wattributes,
7736 "inline function %qD given attribute noinline",
7737 decl1);
7739 /* Handle gnu_inline attribute. */
7740 if (declspecs->inline_p
7741 && !flag_gnu89_inline
7742 && TREE_CODE (decl1) == FUNCTION_DECL
7743 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
7744 || current_function_decl))
7746 if (declspecs->storage_class != csc_static)
7747 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
7750 announce_function (decl1);
7752 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
7754 error_at (loc, "return type is an incomplete type");
7755 /* Make it return void instead. */
7756 TREE_TYPE (decl1)
7757 = build_function_type (void_type_node,
7758 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
7761 if (warn_about_return_type)
7762 pedwarn_c99 (loc, flag_isoc99 ? 0
7763 : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
7764 "return type defaults to %<int%>");
7766 /* Make the init_value nonzero so pushdecl knows this is not tentative.
7767 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
7768 DECL_INITIAL (decl1) = error_mark_node;
7770 /* A nested function is not global. */
7771 if (current_function_decl != 0)
7772 TREE_PUBLIC (decl1) = 0;
7774 /* If this definition isn't a prototype and we had a prototype declaration
7775 before, copy the arg type info from that prototype. */
7776 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
7777 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
7778 old_decl = 0;
7779 current_function_prototype_locus = UNKNOWN_LOCATION;
7780 current_function_prototype_built_in = false;
7781 current_function_prototype_arg_types = NULL_TREE;
7782 if (!prototype_p (TREE_TYPE (decl1)))
7784 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
7785 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7786 TREE_TYPE (TREE_TYPE (old_decl))))
7788 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
7789 TREE_TYPE (decl1));
7790 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
7791 current_function_prototype_built_in
7792 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
7793 current_function_prototype_arg_types
7794 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
7796 if (TREE_PUBLIC (decl1))
7798 /* If there is an external prototype declaration of this
7799 function, record its location but do not copy information
7800 to this decl. This may be an invisible declaration
7801 (built-in or in a scope which has finished) or simply
7802 have more refined argument types than any declaration
7803 found above. */
7804 struct c_binding *b;
7805 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
7806 if (B_IN_SCOPE (b, external_scope))
7807 break;
7808 if (b)
7810 tree ext_decl, ext_type;
7811 ext_decl = b->decl;
7812 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
7813 if (TREE_CODE (ext_type) == FUNCTION_TYPE
7814 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7815 TREE_TYPE (ext_type)))
7817 current_function_prototype_locus
7818 = DECL_SOURCE_LOCATION (ext_decl);
7819 current_function_prototype_built_in
7820 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
7821 current_function_prototype_arg_types
7822 = TYPE_ARG_TYPES (ext_type);
7828 /* Optionally warn of old-fashioned def with no previous prototype. */
7829 if (warn_strict_prototypes
7830 && old_decl != error_mark_node
7831 && !prototype_p (TREE_TYPE (decl1))
7832 && C_DECL_ISNT_PROTOTYPE (old_decl))
7833 warning_at (loc, OPT_Wstrict_prototypes,
7834 "function declaration isn%'t a prototype");
7835 /* Optionally warn of any global def with no previous prototype. */
7836 else if (warn_missing_prototypes
7837 && old_decl != error_mark_node
7838 && TREE_PUBLIC (decl1)
7839 && !MAIN_NAME_P (DECL_NAME (decl1))
7840 && C_DECL_ISNT_PROTOTYPE (old_decl))
7841 warning_at (loc, OPT_Wmissing_prototypes,
7842 "no previous prototype for %qD", decl1);
7843 /* Optionally warn of any def with no previous prototype
7844 if the function has already been used. */
7845 else if (warn_missing_prototypes
7846 && old_decl != 0
7847 && old_decl != error_mark_node
7848 && TREE_USED (old_decl)
7849 && !prototype_p (TREE_TYPE (old_decl)))
7850 warning_at (loc, OPT_Wmissing_prototypes,
7851 "%qD was used with no prototype before its definition", decl1);
7852 /* Optionally warn of any global def with no previous declaration. */
7853 else if (warn_missing_declarations
7854 && TREE_PUBLIC (decl1)
7855 && old_decl == 0
7856 && !MAIN_NAME_P (DECL_NAME (decl1)))
7857 warning_at (loc, OPT_Wmissing_declarations,
7858 "no previous declaration for %qD",
7859 decl1);
7860 /* Optionally warn of any def with no previous declaration
7861 if the function has already been used. */
7862 else if (warn_missing_declarations
7863 && old_decl != 0
7864 && old_decl != error_mark_node
7865 && TREE_USED (old_decl)
7866 && C_DECL_IMPLICIT (old_decl))
7867 warning_at (loc, OPT_Wmissing_declarations,
7868 "%qD was used with no declaration before its definition", decl1);
7870 /* This function exists in static storage.
7871 (This does not mean `static' in the C sense!) */
7872 TREE_STATIC (decl1) = 1;
7874 /* This is the earliest point at which we might know the assembler
7875 name of the function. Thus, if it's set before this, die horribly. */
7876 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
7878 /* If #pragma weak was used, mark the decl weak now. */
7879 if (current_scope == file_scope)
7880 maybe_apply_pragma_weak (decl1);
7882 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
7883 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
7885 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
7886 != integer_type_node)
7887 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
7889 check_main_parameter_types (decl1);
7891 if (!TREE_PUBLIC (decl1))
7892 pedwarn (loc, OPT_Wmain,
7893 "%qD is normally a non-static function", decl1);
7896 /* Record the decl so that the function name is defined.
7897 If we already have a decl for this name, and it is a FUNCTION_DECL,
7898 use the old decl. */
7900 current_function_decl = pushdecl (decl1);
7902 push_scope ();
7903 declare_parm_level ();
7905 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
7906 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
7907 DECL_ARTIFICIAL (resdecl) = 1;
7908 DECL_IGNORED_P (resdecl) = 1;
7909 DECL_RESULT (current_function_decl) = resdecl;
7911 start_fname_decls ();
7913 return 1;
7916 /* Subroutine of store_parm_decls which handles new-style function
7917 definitions (prototype format). The parms already have decls, so we
7918 need only record them as in effect and complain if any redundant
7919 old-style parm decls were written. */
7920 static void
7921 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
7923 tree decl;
7924 c_arg_tag *tag;
7925 unsigned ix;
7927 if (current_scope->bindings)
7929 error_at (DECL_SOURCE_LOCATION (fndecl),
7930 "old-style parameter declarations in prototyped "
7931 "function definition");
7933 /* Get rid of the old-style declarations. */
7934 pop_scope ();
7935 push_scope ();
7937 /* Don't issue this warning for nested functions, and don't issue this
7938 warning if we got here because ARG_INFO_TYPES was error_mark_node
7939 (this happens when a function definition has just an ellipsis in
7940 its parameter list). */
7941 else if (!in_system_header && !current_function_scope
7942 && arg_info->types != error_mark_node)
7943 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
7944 "traditional C rejects ISO C style function definitions");
7946 /* Now make all the parameter declarations visible in the function body.
7947 We can bypass most of the grunt work of pushdecl. */
7948 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
7950 DECL_CONTEXT (decl) = current_function_decl;
7951 if (DECL_NAME (decl))
7953 bind (DECL_NAME (decl), decl, current_scope,
7954 /*invisible=*/false, /*nested=*/false,
7955 UNKNOWN_LOCATION);
7956 if (!TREE_USED (decl))
7957 warn_if_shadowing (decl);
7959 else
7960 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
7963 /* Record the parameter list in the function declaration. */
7964 DECL_ARGUMENTS (fndecl) = arg_info->parms;
7966 /* Now make all the ancillary declarations visible, likewise. */
7967 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
7969 DECL_CONTEXT (decl) = current_function_decl;
7970 if (DECL_NAME (decl))
7971 bind (DECL_NAME (decl), decl, current_scope,
7972 /*invisible=*/false,
7973 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
7974 UNKNOWN_LOCATION);
7977 /* And all the tag declarations. */
7978 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7979 if (tag->id)
7980 bind (tag->id, tag->type, current_scope,
7981 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
7984 /* Subroutine of store_parm_decls which handles old-style function
7985 definitions (separate parameter list and declarations). */
7987 static void
7988 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
7990 struct c_binding *b;
7991 tree parm, decl, last;
7992 tree parmids = arg_info->parms;
7993 struct pointer_set_t *seen_args = pointer_set_create ();
7995 if (!in_system_header)
7996 warning_at (DECL_SOURCE_LOCATION (fndecl),
7997 OPT_Wold_style_definition, "old-style function definition");
7999 /* Match each formal parameter name with its declaration. Save each
8000 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8001 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8003 if (TREE_VALUE (parm) == 0)
8005 error_at (DECL_SOURCE_LOCATION (fndecl),
8006 "parameter name missing from parameter list");
8007 TREE_PURPOSE (parm) = 0;
8008 continue;
8011 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8012 if (b && B_IN_CURRENT_SCOPE (b))
8014 decl = b->decl;
8015 /* Skip erroneous parameters. */
8016 if (decl == error_mark_node)
8017 continue;
8018 /* If we got something other than a PARM_DECL it is an error. */
8019 if (TREE_CODE (decl) != PARM_DECL)
8020 error_at (DECL_SOURCE_LOCATION (decl),
8021 "%qD declared as a non-parameter", decl);
8022 /* If the declaration is already marked, we have a duplicate
8023 name. Complain and ignore the duplicate. */
8024 else if (pointer_set_contains (seen_args, decl))
8026 error_at (DECL_SOURCE_LOCATION (decl),
8027 "multiple parameters named %qD", decl);
8028 TREE_PURPOSE (parm) = 0;
8029 continue;
8031 /* If the declaration says "void", complain and turn it into
8032 an int. */
8033 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8035 error_at (DECL_SOURCE_LOCATION (decl),
8036 "parameter %qD declared with void type", decl);
8037 TREE_TYPE (decl) = integer_type_node;
8038 DECL_ARG_TYPE (decl) = integer_type_node;
8039 layout_decl (decl, 0);
8041 warn_if_shadowing (decl);
8043 /* If no declaration found, default to int. */
8044 else
8046 /* FIXME diagnostics: This should be the location of the argument,
8047 not the FNDECL. E.g., for an old-style declaration
8049 int f10(v) { blah; }
8051 We should use the location of the V, not the F10.
8052 Unfortunately, the V is an IDENTIFIER_NODE which has no
8053 location. In the future we need locations for c_arg_info
8054 entries.
8056 See gcc.dg/Wshadow-3.c for an example of this problem. */
8057 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8058 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8059 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8060 pushdecl (decl);
8061 warn_if_shadowing (decl);
8063 if (flag_isoc99)
8064 pedwarn (DECL_SOURCE_LOCATION (decl),
8065 0, "type of %qD defaults to %<int%>", decl);
8066 else
8067 warning_at (DECL_SOURCE_LOCATION (decl),
8068 OPT_Wmissing_parameter_type,
8069 "type of %qD defaults to %<int%>", decl);
8072 TREE_PURPOSE (parm) = decl;
8073 pointer_set_insert (seen_args, decl);
8076 /* Now examine the parms chain for incomplete declarations
8077 and declarations with no corresponding names. */
8079 for (b = current_scope->bindings; b; b = b->prev)
8081 parm = b->decl;
8082 if (TREE_CODE (parm) != PARM_DECL)
8083 continue;
8085 if (TREE_TYPE (parm) != error_mark_node
8086 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
8088 error_at (DECL_SOURCE_LOCATION (parm),
8089 "parameter %qD has incomplete type", parm);
8090 TREE_TYPE (parm) = error_mark_node;
8093 if (!pointer_set_contains (seen_args, parm))
8095 error_at (DECL_SOURCE_LOCATION (parm),
8096 "declaration for parameter %qD but no such parameter",
8097 parm);
8099 /* Pretend the parameter was not missing.
8100 This gets us to a standard state and minimizes
8101 further error messages. */
8102 parmids = chainon (parmids, tree_cons (parm, 0, 0));
8106 /* Chain the declarations together in the order of the list of
8107 names. Store that chain in the function decl, replacing the
8108 list of names. Update the current scope to match. */
8109 DECL_ARGUMENTS (fndecl) = 0;
8111 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8112 if (TREE_PURPOSE (parm))
8113 break;
8114 if (parm && TREE_PURPOSE (parm))
8116 last = TREE_PURPOSE (parm);
8117 DECL_ARGUMENTS (fndecl) = last;
8119 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8120 if (TREE_PURPOSE (parm))
8122 DECL_CHAIN (last) = TREE_PURPOSE (parm);
8123 last = TREE_PURPOSE (parm);
8125 DECL_CHAIN (last) = 0;
8128 pointer_set_destroy (seen_args);
8130 /* If there was a previous prototype,
8131 set the DECL_ARG_TYPE of each argument according to
8132 the type previously specified, and report any mismatches. */
8134 if (current_function_prototype_arg_types)
8136 tree type;
8137 for (parm = DECL_ARGUMENTS (fndecl),
8138 type = current_function_prototype_arg_types;
8139 parm || (type && TREE_VALUE (type) != error_mark_node
8140 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
8141 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8143 if (parm == 0 || type == 0
8144 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
8146 if (current_function_prototype_built_in)
8147 warning_at (DECL_SOURCE_LOCATION (fndecl),
8148 0, "number of arguments doesn%'t match "
8149 "built-in prototype");
8150 else
8152 /* FIXME diagnostics: This should be the location of
8153 FNDECL, but there is bug when a prototype is
8154 declared inside function context, but defined
8155 outside of it (e.g., gcc.dg/pr15698-2.c). In
8156 which case FNDECL gets the location of the
8157 prototype, not the definition. */
8158 error_at (input_location,
8159 "number of arguments doesn%'t match prototype");
8161 error_at (current_function_prototype_locus,
8162 "prototype declaration");
8164 break;
8166 /* Type for passing arg must be consistent with that
8167 declared for the arg. ISO C says we take the unqualified
8168 type for parameters declared with qualified type. */
8169 if (TREE_TYPE (parm) != error_mark_node
8170 && TREE_TYPE (type) != error_mark_node
8171 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
8172 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
8174 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
8175 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
8177 /* Adjust argument to match prototype. E.g. a previous
8178 `int foo(float);' prototype causes
8179 `int foo(x) float x; {...}' to be treated like
8180 `int foo(float x) {...}'. This is particularly
8181 useful for argument types like uid_t. */
8182 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
8184 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
8185 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
8186 && TYPE_PRECISION (TREE_TYPE (parm))
8187 < TYPE_PRECISION (integer_type_node))
8188 DECL_ARG_TYPE (parm) = integer_type_node;
8190 /* ??? Is it possible to get here with a
8191 built-in prototype or will it always have
8192 been diagnosed as conflicting with an
8193 old-style definition and discarded? */
8194 if (current_function_prototype_built_in)
8195 warning_at (DECL_SOURCE_LOCATION (parm),
8196 OPT_Wpedantic, "promoted argument %qD "
8197 "doesn%'t match built-in prototype", parm);
8198 else
8200 pedwarn (DECL_SOURCE_LOCATION (parm),
8201 OPT_Wpedantic, "promoted argument %qD "
8202 "doesn%'t match prototype", parm);
8203 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
8204 "prototype declaration");
8207 else
8209 if (current_function_prototype_built_in)
8210 warning_at (DECL_SOURCE_LOCATION (parm),
8211 0, "argument %qD doesn%'t match "
8212 "built-in prototype", parm);
8213 else
8215 error_at (DECL_SOURCE_LOCATION (parm),
8216 "argument %qD doesn%'t match prototype", parm);
8217 error_at (current_function_prototype_locus,
8218 "prototype declaration");
8223 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
8226 /* Otherwise, create a prototype that would match. */
8228 else
8230 tree actual = 0, last = 0, type;
8232 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
8234 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
8235 if (last)
8236 TREE_CHAIN (last) = type;
8237 else
8238 actual = type;
8239 last = type;
8241 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
8242 if (last)
8243 TREE_CHAIN (last) = type;
8244 else
8245 actual = type;
8247 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
8248 of the type of this function, but we need to avoid having this
8249 affect the types of other similarly-typed functions, so we must
8250 first force the generation of an identical (but separate) type
8251 node for the relevant function type. The new node we create
8252 will be a variant of the main variant of the original function
8253 type. */
8255 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
8257 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
8261 /* Store parameter declarations passed in ARG_INFO into the current
8262 function declaration. */
8264 void
8265 store_parm_decls_from (struct c_arg_info *arg_info)
8267 current_function_arg_info = arg_info;
8268 store_parm_decls ();
8271 /* Store the parameter declarations into the current function declaration.
8272 This is called after parsing the parameter declarations, before
8273 digesting the body of the function.
8275 For an old-style definition, construct a prototype out of the old-style
8276 parameter declarations and inject it into the function's type. */
8278 void
8279 store_parm_decls (void)
8281 tree fndecl = current_function_decl;
8282 bool proto;
8284 /* The argument information block for FNDECL. */
8285 struct c_arg_info *arg_info = current_function_arg_info;
8286 current_function_arg_info = 0;
8288 /* True if this definition is written with a prototype. Note:
8289 despite C99 6.7.5.3p14, we can *not* treat an empty argument
8290 list in a function definition as equivalent to (void) -- an
8291 empty argument list specifies the function has no parameters,
8292 but only (void) sets up a prototype for future calls. */
8293 proto = arg_info->types != 0;
8295 if (proto)
8296 store_parm_decls_newstyle (fndecl, arg_info);
8297 else
8298 store_parm_decls_oldstyle (fndecl, arg_info);
8300 /* The next call to push_scope will be a function body. */
8302 next_is_function_body = true;
8304 /* Write a record describing this function definition to the prototypes
8305 file (if requested). */
8307 gen_aux_info_record (fndecl, 1, 0, proto);
8309 /* Initialize the RTL code for the function. */
8310 allocate_struct_function (fndecl, false);
8312 if (warn_unused_local_typedefs)
8313 cfun->language = ggc_alloc_cleared_language_function ();
8315 /* Begin the statement tree for this function. */
8316 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
8318 /* ??? Insert the contents of the pending sizes list into the function
8319 to be evaluated. The only reason left to have this is
8320 void foo(int n, int array[n++])
8321 because we throw away the array type in favor of a pointer type, and
8322 thus won't naturally see the SAVE_EXPR containing the increment. All
8323 other pending sizes would be handled by gimplify_parameters. */
8324 if (arg_info->pending_sizes)
8325 add_stmt (arg_info->pending_sizes);
8329 /* Finish up a function declaration and compile that function
8330 all the way to assembler language output. Then free the storage
8331 for the function definition.
8333 This is called after parsing the body of the function definition. */
8335 void
8336 finish_function (void)
8338 tree fndecl = current_function_decl;
8340 if (c_dialect_objc ())
8341 objc_finish_function ();
8343 if (TREE_CODE (fndecl) == FUNCTION_DECL
8344 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
8346 tree args = DECL_ARGUMENTS (fndecl);
8347 for (; args; args = DECL_CHAIN (args))
8349 tree type = TREE_TYPE (args);
8350 if (INTEGRAL_TYPE_P (type)
8351 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8352 DECL_ARG_TYPE (args) = integer_type_node;
8356 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
8357 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8359 /* Must mark the RESULT_DECL as being in this function. */
8361 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
8362 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8364 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
8365 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
8366 == integer_type_node && flag_isoc99)
8368 /* Hack. We don't want the middle-end to warn that this return
8369 is unreachable, so we mark its location as special. Using
8370 UNKNOWN_LOCATION has the problem that it gets clobbered in
8371 annotate_one_with_locus. A cleaner solution might be to
8372 ensure ! should_carry_locus_p (stmt), but that needs a flag.
8374 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
8377 /* Tie off the statement tree for this function. */
8378 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
8380 finish_fname_decls ();
8382 /* Complain if there's just no return statement. */
8383 if (warn_return_type
8384 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
8385 && !current_function_returns_value && !current_function_returns_null
8386 /* Don't complain if we are no-return. */
8387 && !current_function_returns_abnormally
8388 /* Don't complain if we are declared noreturn. */
8389 && !TREE_THIS_VOLATILE (fndecl)
8390 /* Don't warn for main(). */
8391 && !MAIN_NAME_P (DECL_NAME (fndecl))
8392 /* Or if they didn't actually specify a return type. */
8393 && !C_FUNCTION_IMPLICIT_INT (fndecl)
8394 /* Normally, with -Wreturn-type, flow will complain, but we might
8395 optimize out static functions. */
8396 && !TREE_PUBLIC (fndecl))
8398 warning (OPT_Wreturn_type,
8399 "no return statement in function returning non-void");
8400 TREE_NO_WARNING (fndecl) = 1;
8403 /* Complain about parameters that are only set, but never otherwise used. */
8404 if (warn_unused_but_set_parameter)
8406 tree decl;
8408 for (decl = DECL_ARGUMENTS (fndecl);
8409 decl;
8410 decl = DECL_CHAIN (decl))
8411 if (TREE_USED (decl)
8412 && TREE_CODE (decl) == PARM_DECL
8413 && !DECL_READ_P (decl)
8414 && DECL_NAME (decl)
8415 && !DECL_ARTIFICIAL (decl)
8416 && !TREE_NO_WARNING (decl))
8417 warning_at (DECL_SOURCE_LOCATION (decl),
8418 OPT_Wunused_but_set_parameter,
8419 "parameter %qD set but not used", decl);
8422 /* Complain about locally defined typedefs that are not used in this
8423 function. */
8424 maybe_warn_unused_local_typedefs ();
8426 /* Store the end of the function, so that we get good line number
8427 info for the epilogue. */
8428 cfun->function_end_locus = input_location;
8430 /* Finalize the ELF visibility for the function. */
8431 c_determine_visibility (fndecl);
8433 /* For GNU C extern inline functions disregard inline limits. */
8434 if (DECL_EXTERNAL (fndecl)
8435 && DECL_DECLARED_INLINE_P (fndecl))
8436 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
8438 /* Genericize before inlining. Delay genericizing nested functions
8439 until their parent function is genericized. Since finalizing
8440 requires GENERIC, delay that as well. */
8442 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
8443 && !undef_nested_function)
8445 if (!decl_function_context (fndecl))
8447 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
8448 c_genericize (fndecl);
8450 /* ??? Objc emits functions after finalizing the compilation unit.
8451 This should be cleaned up later and this conditional removed. */
8452 if (cgraph_global_info_ready)
8454 cgraph_add_new_function (fndecl, false);
8455 return;
8457 cgraph_finalize_function (fndecl, false);
8459 else
8461 /* Register this function with cgraph just far enough to get it
8462 added to our parent's nested function list. Handy, since the
8463 C front end doesn't have such a list. */
8464 (void) cgraph_get_create_node (fndecl);
8468 if (!decl_function_context (fndecl))
8469 undef_nested_function = false;
8471 if (cfun->language != NULL)
8473 ggc_free (cfun->language);
8474 cfun->language = NULL;
8477 /* We're leaving the context of this function, so zap cfun.
8478 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
8479 tree_rest_of_compilation. */
8480 set_cfun (NULL);
8481 current_function_decl = NULL;
8484 /* Check the declarations given in a for-loop for satisfying the C99
8485 constraints. If exactly one such decl is found, return it. LOC is
8486 the location of the opening parenthesis of the for loop. The last
8487 parameter allows you to control the "for loop initial declarations
8488 are only allowed in C99 mode". Normally, you should pass
8489 flag_isoc99 as that parameter. But in some cases (Objective-C
8490 foreach loop, for example) we want to run the checks in this
8491 function even if not in C99 mode, so we allow the caller to turn
8492 off the error about not being in C99 mode.
8495 tree
8496 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
8498 struct c_binding *b;
8499 tree one_decl = NULL_TREE;
8500 int n_decls = 0;
8502 if (!turn_off_iso_c99_error)
8504 static bool hint = true;
8505 /* If we get here, declarations have been used in a for loop without
8506 the C99 for loop scope. This doesn't make much sense, so don't
8507 allow it. */
8508 error_at (loc, "%<for%> loop initial declarations "
8509 "are only allowed in C99 or C11 mode");
8510 if (hint)
8512 inform (loc,
8513 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
8514 "to compile your code");
8515 hint = false;
8517 return NULL_TREE;
8519 /* C99 subclause 6.8.5 paragraph 3:
8521 [#3] The declaration part of a for statement shall only
8522 declare identifiers for objects having storage class auto or
8523 register.
8525 It isn't clear whether, in this sentence, "identifiers" binds to
8526 "shall only declare" or to "objects" - that is, whether all identifiers
8527 declared must be identifiers for objects, or whether the restriction
8528 only applies to those that are. (A question on this in comp.std.c
8529 in November 2000 received no answer.) We implement the strictest
8530 interpretation, to avoid creating an extension which later causes
8531 problems. */
8533 for (b = current_scope->bindings; b; b = b->prev)
8535 tree id = b->id;
8536 tree decl = b->decl;
8538 if (!id)
8539 continue;
8541 switch (TREE_CODE (decl))
8543 case VAR_DECL:
8545 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
8546 if (TREE_STATIC (decl))
8547 error_at (decl_loc,
8548 "declaration of static variable %qD in %<for%> loop "
8549 "initial declaration", decl);
8550 else if (DECL_EXTERNAL (decl))
8551 error_at (decl_loc,
8552 "declaration of %<extern%> variable %qD in %<for%> loop "
8553 "initial declaration", decl);
8555 break;
8557 case RECORD_TYPE:
8558 error_at (loc,
8559 "%<struct %E%> declared in %<for%> loop initial "
8560 "declaration", id);
8561 break;
8562 case UNION_TYPE:
8563 error_at (loc,
8564 "%<union %E%> declared in %<for%> loop initial declaration",
8565 id);
8566 break;
8567 case ENUMERAL_TYPE:
8568 error_at (loc, "%<enum %E%> declared in %<for%> loop "
8569 "initial declaration", id);
8570 break;
8571 default:
8572 error_at (loc, "declaration of non-variable "
8573 "%qD in %<for%> loop initial declaration", decl);
8576 n_decls++;
8577 one_decl = decl;
8580 return n_decls == 1 ? one_decl : NULL_TREE;
8583 /* Save and reinitialize the variables
8584 used during compilation of a C function. */
8586 void
8587 c_push_function_context (void)
8589 struct language_function *p = cfun->language;
8590 /* cfun->language might have been already allocated by the use of
8591 -Wunused-local-typedefs. In that case, just re-use it. */
8592 if (p == NULL)
8593 cfun->language = p = ggc_alloc_cleared_language_function ();
8595 p->base.x_stmt_tree = c_stmt_tree;
8596 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
8597 p->x_break_label = c_break_label;
8598 p->x_cont_label = c_cont_label;
8599 p->x_switch_stack = c_switch_stack;
8600 p->arg_info = current_function_arg_info;
8601 p->returns_value = current_function_returns_value;
8602 p->returns_null = current_function_returns_null;
8603 p->returns_abnormally = current_function_returns_abnormally;
8604 p->warn_about_return_type = warn_about_return_type;
8606 push_function_context ();
8609 /* Restore the variables used during compilation of a C function. */
8611 void
8612 c_pop_function_context (void)
8614 struct language_function *p;
8616 pop_function_context ();
8617 p = cfun->language;
8619 /* When -Wunused-local-typedefs is in effect, cfun->languages is
8620 used to store data throughout the life time of the current cfun,
8621 So don't deallocate it. */
8622 if (!warn_unused_local_typedefs)
8623 cfun->language = NULL;
8625 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
8626 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
8628 /* Stop pointing to the local nodes about to be freed. */
8629 /* But DECL_INITIAL must remain nonzero so we know this
8630 was an actual function definition. */
8631 DECL_INITIAL (current_function_decl) = error_mark_node;
8632 DECL_ARGUMENTS (current_function_decl) = 0;
8635 c_stmt_tree = p->base.x_stmt_tree;
8636 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
8637 c_break_label = p->x_break_label;
8638 c_cont_label = p->x_cont_label;
8639 c_switch_stack = p->x_switch_stack;
8640 current_function_arg_info = p->arg_info;
8641 current_function_returns_value = p->returns_value;
8642 current_function_returns_null = p->returns_null;
8643 current_function_returns_abnormally = p->returns_abnormally;
8644 warn_about_return_type = p->warn_about_return_type;
8647 /* The functions below are required for functionality of doing
8648 function at once processing in the C front end. Currently these
8649 functions are not called from anywhere in the C front end, but as
8650 these changes continue, that will change. */
8652 /* Returns the stmt_tree (if any) to which statements are currently
8653 being added. If there is no active statement-tree, NULL is
8654 returned. */
8656 stmt_tree
8657 current_stmt_tree (void)
8659 return &c_stmt_tree;
8662 /* Return the global value of T as a symbol. */
8664 tree
8665 identifier_global_value (tree t)
8667 struct c_binding *b;
8669 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
8670 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
8671 return b->decl;
8673 return 0;
8676 /* In C, the only C-linkage public declaration is at file scope. */
8678 tree
8679 c_linkage_bindings (tree name)
8681 return identifier_global_value (name);
8684 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
8685 otherwise the name is found in ridpointers from RID_INDEX. */
8687 void
8688 record_builtin_type (enum rid rid_index, const char *name, tree type)
8690 tree id, decl;
8691 if (name == 0)
8692 id = ridpointers[(int) rid_index];
8693 else
8694 id = get_identifier (name);
8695 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
8696 pushdecl (decl);
8697 if (debug_hooks->type_decl)
8698 debug_hooks->type_decl (decl, false);
8701 /* Build the void_list_node (void_type_node having been created). */
8702 tree
8703 build_void_list_node (void)
8705 tree t = build_tree_list (NULL_TREE, void_type_node);
8706 return t;
8709 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
8711 struct c_parm *
8712 build_c_parm (struct c_declspecs *specs, tree attrs,
8713 struct c_declarator *declarator)
8715 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
8716 ret->specs = specs;
8717 ret->attrs = attrs;
8718 ret->declarator = declarator;
8719 return ret;
8722 /* Return a declarator with nested attributes. TARGET is the inner
8723 declarator to which these attributes apply. ATTRS are the
8724 attributes. */
8726 struct c_declarator *
8727 build_attrs_declarator (tree attrs, struct c_declarator *target)
8729 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8730 ret->kind = cdk_attrs;
8731 ret->declarator = target;
8732 ret->u.attrs = attrs;
8733 return ret;
8736 /* Return a declarator for a function with arguments specified by ARGS
8737 and return type specified by TARGET. */
8739 struct c_declarator *
8740 build_function_declarator (struct c_arg_info *args,
8741 struct c_declarator *target)
8743 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8744 ret->kind = cdk_function;
8745 ret->declarator = target;
8746 ret->u.arg_info = args;
8747 return ret;
8750 /* Return a declarator for the identifier IDENT (which may be
8751 NULL_TREE for an abstract declarator). */
8753 struct c_declarator *
8754 build_id_declarator (tree ident)
8756 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8757 ret->kind = cdk_id;
8758 ret->declarator = 0;
8759 ret->u.id = ident;
8760 /* Default value - may get reset to a more precise location. */
8761 ret->id_loc = input_location;
8762 return ret;
8765 /* Return something to represent absolute declarators containing a *.
8766 TARGET is the absolute declarator that the * contains.
8767 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
8768 to apply to the pointer type. */
8770 struct c_declarator *
8771 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
8772 struct c_declarator *target)
8774 tree attrs;
8775 int quals = 0;
8776 struct c_declarator *itarget = target;
8777 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8778 if (type_quals_attrs)
8780 attrs = type_quals_attrs->attrs;
8781 quals = quals_from_declspecs (type_quals_attrs);
8782 if (attrs != NULL_TREE)
8783 itarget = build_attrs_declarator (attrs, target);
8785 ret->kind = cdk_pointer;
8786 ret->declarator = itarget;
8787 ret->u.pointer_quals = quals;
8788 return ret;
8791 /* Return a pointer to a structure for an empty list of declaration
8792 specifiers. */
8794 struct c_declspecs *
8795 build_null_declspecs (void)
8797 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
8798 memset (&ret->locations, 0, cdw_number_of_elements);
8799 ret->type = 0;
8800 ret->expr = 0;
8801 ret->decl_attr = 0;
8802 ret->attrs = 0;
8803 ret->align_log = -1;
8804 ret->typespec_word = cts_none;
8805 ret->storage_class = csc_none;
8806 ret->expr_const_operands = true;
8807 ret->declspecs_seen_p = false;
8808 ret->typespec_kind = ctsk_none;
8809 ret->non_sc_seen_p = false;
8810 ret->typedef_p = false;
8811 ret->explicit_signed_p = false;
8812 ret->deprecated_p = false;
8813 ret->default_int_p = false;
8814 ret->long_p = false;
8815 ret->long_long_p = false;
8816 ret->short_p = false;
8817 ret->signed_p = false;
8818 ret->unsigned_p = false;
8819 ret->complex_p = false;
8820 ret->inline_p = false;
8821 ret->noreturn_p = false;
8822 ret->thread_p = false;
8823 ret->const_p = false;
8824 ret->volatile_p = false;
8825 ret->restrict_p = false;
8826 ret->saturating_p = false;
8827 ret->alignas_p = false;
8828 ret->address_space = ADDR_SPACE_GENERIC;
8829 return ret;
8832 /* Add the address space ADDRSPACE to the declaration specifiers
8833 SPECS, returning SPECS. */
8835 struct c_declspecs *
8836 declspecs_add_addrspace (source_location location,
8837 struct c_declspecs *specs, addr_space_t as)
8839 specs->non_sc_seen_p = true;
8840 specs->declspecs_seen_p = true;
8842 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
8843 && specs->address_space != as)
8844 error ("incompatible address space qualifiers %qs and %qs",
8845 c_addr_space_name (as),
8846 c_addr_space_name (specs->address_space));
8847 else
8849 specs->address_space = as;
8850 specs->locations[cdw_address_space] = location;
8852 return specs;
8855 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
8856 returning SPECS. */
8858 struct c_declspecs *
8859 declspecs_add_qual (source_location loc,
8860 struct c_declspecs *specs, tree qual)
8862 enum rid i;
8863 bool dupe = false;
8864 specs->non_sc_seen_p = true;
8865 specs->declspecs_seen_p = true;
8866 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
8867 && C_IS_RESERVED_WORD (qual));
8868 i = C_RID_CODE (qual);
8869 switch (i)
8871 case RID_CONST:
8872 dupe = specs->const_p;
8873 specs->const_p = true;
8874 specs->locations[cdw_const] = loc;
8875 break;
8876 case RID_VOLATILE:
8877 dupe = specs->volatile_p;
8878 specs->volatile_p = true;
8879 specs->locations[cdw_volatile] = loc;
8880 break;
8881 case RID_RESTRICT:
8882 dupe = specs->restrict_p;
8883 specs->restrict_p = true;
8884 specs->locations[cdw_restrict] = loc;
8885 break;
8886 default:
8887 gcc_unreachable ();
8889 if (dupe && !flag_isoc99)
8890 pedwarn (loc, OPT_Wpedantic, "duplicate %qE", qual);
8891 return specs;
8894 /* Add the type specifier TYPE to the declaration specifiers SPECS,
8895 returning SPECS. */
8897 struct c_declspecs *
8898 declspecs_add_type (location_t loc, struct c_declspecs *specs,
8899 struct c_typespec spec)
8901 tree type = spec.spec;
8902 specs->non_sc_seen_p = true;
8903 specs->declspecs_seen_p = true;
8904 specs->typespec_kind = spec.kind;
8905 if (TREE_DEPRECATED (type))
8906 specs->deprecated_p = true;
8908 /* Handle type specifier keywords. */
8909 if (TREE_CODE (type) == IDENTIFIER_NODE
8910 && C_IS_RESERVED_WORD (type)
8911 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
8913 enum rid i = C_RID_CODE (type);
8914 if (specs->type)
8916 error_at (loc, "two or more data types in declaration specifiers");
8917 return specs;
8919 if ((int) i <= (int) RID_LAST_MODIFIER)
8921 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
8922 bool dupe = false;
8923 switch (i)
8925 case RID_LONG:
8926 if (specs->long_long_p)
8928 error_at (loc, "%<long long long%> is too long for GCC");
8929 break;
8931 if (specs->long_p)
8933 if (specs->typespec_word == cts_double)
8935 error_at (loc,
8936 ("both %<long long%> and %<double%> in "
8937 "declaration specifiers"));
8938 break;
8940 pedwarn_c90 (loc, OPT_Wlong_long,
8941 "ISO C90 does not support %<long long%>");
8942 specs->long_long_p = 1;
8943 specs->locations[cdw_long_long] = loc;
8944 break;
8946 if (specs->short_p)
8947 error_at (loc,
8948 ("both %<long%> and %<short%> in "
8949 "declaration specifiers"));
8950 else if (specs->typespec_word == cts_void)
8951 error_at (loc,
8952 ("both %<long%> and %<void%> in "
8953 "declaration specifiers"));
8954 else if (specs->typespec_word == cts_int128)
8955 error_at (loc,
8956 ("both %<long%> and %<__int128%> in "
8957 "declaration specifiers"));
8958 else if (specs->typespec_word == cts_bool)
8959 error_at (loc,
8960 ("both %<long%> and %<_Bool%> in "
8961 "declaration specifiers"));
8962 else if (specs->typespec_word == cts_char)
8963 error_at (loc,
8964 ("both %<long%> and %<char%> in "
8965 "declaration specifiers"));
8966 else if (specs->typespec_word == cts_float)
8967 error_at (loc,
8968 ("both %<long%> and %<float%> in "
8969 "declaration specifiers"));
8970 else if (specs->typespec_word == cts_dfloat32)
8971 error_at (loc,
8972 ("both %<long%> and %<_Decimal32%> in "
8973 "declaration specifiers"));
8974 else if (specs->typespec_word == cts_dfloat64)
8975 error_at (loc,
8976 ("both %<long%> and %<_Decimal64%> in "
8977 "declaration specifiers"));
8978 else if (specs->typespec_word == cts_dfloat128)
8979 error_at (loc,
8980 ("both %<long%> and %<_Decimal128%> in "
8981 "declaration specifiers"));
8982 else
8984 specs->long_p = true;
8985 specs->locations[cdw_long] = loc;
8987 break;
8988 case RID_SHORT:
8989 dupe = specs->short_p;
8990 if (specs->long_p)
8991 error_at (loc,
8992 ("both %<long%> and %<short%> in "
8993 "declaration specifiers"));
8994 else if (specs->typespec_word == cts_void)
8995 error_at (loc,
8996 ("both %<short%> and %<void%> in "
8997 "declaration specifiers"));
8998 else if (specs->typespec_word == cts_int128)
8999 error_at (loc,
9000 ("both %<short%> and %<__int128%> in "
9001 "declaration specifiers"));
9002 else if (specs->typespec_word == cts_bool)
9003 error_at (loc,
9004 ("both %<short%> and %<_Bool%> in "
9005 "declaration specifiers"));
9006 else if (specs->typespec_word == cts_char)
9007 error_at (loc,
9008 ("both %<short%> and %<char%> in "
9009 "declaration specifiers"));
9010 else if (specs->typespec_word == cts_float)
9011 error_at (loc,
9012 ("both %<short%> and %<float%> in "
9013 "declaration specifiers"));
9014 else if (specs->typespec_word == cts_double)
9015 error_at (loc,
9016 ("both %<short%> and %<double%> in "
9017 "declaration specifiers"));
9018 else if (specs->typespec_word == cts_dfloat32)
9019 error_at (loc,
9020 ("both %<short%> and %<_Decimal32%> in "
9021 "declaration specifiers"));
9022 else if (specs->typespec_word == cts_dfloat64)
9023 error_at (loc,
9024 ("both %<short%> and %<_Decimal64%> in "
9025 "declaration specifiers"));
9026 else if (specs->typespec_word == cts_dfloat128)
9027 error_at (loc,
9028 ("both %<short%> and %<_Decimal128%> in "
9029 "declaration specifiers"));
9030 else
9032 specs->short_p = true;
9033 specs->locations[cdw_short] = loc;
9035 break;
9036 case RID_SIGNED:
9037 dupe = specs->signed_p;
9038 if (specs->unsigned_p)
9039 error_at (loc,
9040 ("both %<signed%> and %<unsigned%> in "
9041 "declaration specifiers"));
9042 else if (specs->typespec_word == cts_void)
9043 error_at (loc,
9044 ("both %<signed%> and %<void%> in "
9045 "declaration specifiers"));
9046 else if (specs->typespec_word == cts_bool)
9047 error_at (loc,
9048 ("both %<signed%> and %<_Bool%> in "
9049 "declaration specifiers"));
9050 else if (specs->typespec_word == cts_float)
9051 error_at (loc,
9052 ("both %<signed%> and %<float%> in "
9053 "declaration specifiers"));
9054 else if (specs->typespec_word == cts_double)
9055 error_at (loc,
9056 ("both %<signed%> and %<double%> in "
9057 "declaration specifiers"));
9058 else if (specs->typespec_word == cts_dfloat32)
9059 error_at (loc,
9060 ("both %<signed%> and %<_Decimal32%> in "
9061 "declaration specifiers"));
9062 else if (specs->typespec_word == cts_dfloat64)
9063 error_at (loc,
9064 ("both %<signed%> and %<_Decimal64%> in "
9065 "declaration specifiers"));
9066 else if (specs->typespec_word == cts_dfloat128)
9067 error_at (loc,
9068 ("both %<signed%> and %<_Decimal128%> in "
9069 "declaration specifiers"));
9070 else
9072 specs->signed_p = true;
9073 specs->locations[cdw_signed] = loc;
9075 break;
9076 case RID_UNSIGNED:
9077 dupe = specs->unsigned_p;
9078 if (specs->signed_p)
9079 error_at (loc,
9080 ("both %<signed%> and %<unsigned%> in "
9081 "declaration specifiers"));
9082 else if (specs->typespec_word == cts_void)
9083 error_at (loc,
9084 ("both %<unsigned%> and %<void%> in "
9085 "declaration specifiers"));
9086 else if (specs->typespec_word == cts_bool)
9087 error_at (loc,
9088 ("both %<unsigned%> and %<_Bool%> in "
9089 "declaration specifiers"));
9090 else if (specs->typespec_word == cts_float)
9091 error_at (loc,
9092 ("both %<unsigned%> and %<float%> in "
9093 "declaration specifiers"));
9094 else if (specs->typespec_word == cts_double)
9095 error_at (loc,
9096 ("both %<unsigned%> and %<double%> in "
9097 "declaration specifiers"));
9098 else if (specs->typespec_word == cts_dfloat32)
9099 error_at (loc,
9100 ("both %<unsigned%> and %<_Decimal32%> in "
9101 "declaration specifiers"));
9102 else if (specs->typespec_word == cts_dfloat64)
9103 error_at (loc,
9104 ("both %<unsigned%> and %<_Decimal64%> in "
9105 "declaration specifiers"));
9106 else if (specs->typespec_word == cts_dfloat128)
9107 error_at (loc,
9108 ("both %<unsigned%> and %<_Decimal128%> in "
9109 "declaration specifiers"));
9110 else
9112 specs->unsigned_p = true;
9113 specs->locations[cdw_unsigned] = loc;
9115 break;
9116 case RID_COMPLEX:
9117 dupe = specs->complex_p;
9118 if (!flag_isoc99 && !in_system_header_at (loc))
9119 pedwarn (loc, OPT_Wpedantic,
9120 "ISO C90 does not support complex types");
9121 if (specs->typespec_word == cts_void)
9122 error_at (loc,
9123 ("both %<complex%> and %<void%> in "
9124 "declaration specifiers"));
9125 else if (specs->typespec_word == cts_bool)
9126 error_at (loc,
9127 ("both %<complex%> and %<_Bool%> in "
9128 "declaration specifiers"));
9129 else if (specs->typespec_word == cts_dfloat32)
9130 error_at (loc,
9131 ("both %<complex%> and %<_Decimal32%> in "
9132 "declaration specifiers"));
9133 else if (specs->typespec_word == cts_dfloat64)
9134 error_at (loc,
9135 ("both %<complex%> and %<_Decimal64%> in "
9136 "declaration specifiers"));
9137 else if (specs->typespec_word == cts_dfloat128)
9138 error_at (loc,
9139 ("both %<complex%> and %<_Decimal128%> in "
9140 "declaration specifiers"));
9141 else if (specs->typespec_word == cts_fract)
9142 error_at (loc,
9143 ("both %<complex%> and %<_Fract%> in "
9144 "declaration specifiers"));
9145 else if (specs->typespec_word == cts_accum)
9146 error_at (loc,
9147 ("both %<complex%> and %<_Accum%> in "
9148 "declaration specifiers"));
9149 else if (specs->saturating_p)
9150 error_at (loc,
9151 ("both %<complex%> and %<_Sat%> in "
9152 "declaration specifiers"));
9153 else
9155 specs->complex_p = true;
9156 specs->locations[cdw_complex] = loc;
9158 break;
9159 case RID_SAT:
9160 dupe = specs->saturating_p;
9161 pedwarn (loc, OPT_Wpedantic,
9162 "ISO C does not support saturating types");
9163 if (specs->typespec_word == cts_int128)
9165 error_at (loc,
9166 ("both %<_Sat%> and %<__int128%> in "
9167 "declaration specifiers"));
9169 else if (specs->typespec_word == cts_void)
9170 error_at (loc,
9171 ("both %<_Sat%> and %<void%> in "
9172 "declaration specifiers"));
9173 else if (specs->typespec_word == cts_bool)
9174 error_at (loc,
9175 ("both %<_Sat%> and %<_Bool%> in "
9176 "declaration specifiers"));
9177 else if (specs->typespec_word == cts_char)
9178 error_at (loc,
9179 ("both %<_Sat%> and %<char%> in "
9180 "declaration specifiers"));
9181 else if (specs->typespec_word == cts_int)
9182 error_at (loc,
9183 ("both %<_Sat%> and %<int%> in "
9184 "declaration specifiers"));
9185 else if (specs->typespec_word == cts_float)
9186 error_at (loc,
9187 ("both %<_Sat%> and %<float%> in "
9188 "declaration specifiers"));
9189 else if (specs->typespec_word == cts_double)
9190 error_at (loc,
9191 ("both %<_Sat%> and %<double%> in "
9192 "declaration specifiers"));
9193 else if (specs->typespec_word == cts_dfloat32)
9194 error_at (loc,
9195 ("both %<_Sat%> and %<_Decimal32%> in "
9196 "declaration specifiers"));
9197 else if (specs->typespec_word == cts_dfloat64)
9198 error_at (loc,
9199 ("both %<_Sat%> and %<_Decimal64%> in "
9200 "declaration specifiers"));
9201 else if (specs->typespec_word == cts_dfloat128)
9202 error_at (loc,
9203 ("both %<_Sat%> and %<_Decimal128%> in "
9204 "declaration specifiers"));
9205 else if (specs->complex_p)
9206 error_at (loc,
9207 ("both %<_Sat%> and %<complex%> in "
9208 "declaration specifiers"));
9209 else
9211 specs->saturating_p = true;
9212 specs->locations[cdw_saturating] = loc;
9214 break;
9215 default:
9216 gcc_unreachable ();
9219 if (dupe)
9220 error_at (loc, "duplicate %qE", type);
9222 return specs;
9224 else
9226 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
9227 "__int128", "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
9228 if (specs->typespec_word != cts_none)
9230 error_at (loc,
9231 "two or more data types in declaration specifiers");
9232 return specs;
9234 switch (i)
9236 case RID_INT128:
9237 if (int128_integer_type_node == NULL_TREE)
9239 error_at (loc, "%<__int128%> is not supported for this target");
9240 return specs;
9242 if (!in_system_header)
9243 pedwarn (loc, OPT_Wpedantic,
9244 "ISO C does not support %<__int128%> type");
9246 if (specs->long_p)
9247 error_at (loc,
9248 ("both %<__int128%> and %<long%> in "
9249 "declaration specifiers"));
9250 else if (specs->saturating_p)
9251 error_at (loc,
9252 ("both %<_Sat%> and %<__int128%> in "
9253 "declaration specifiers"));
9254 else if (specs->short_p)
9255 error_at (loc,
9256 ("both %<__int128%> and %<short%> in "
9257 "declaration specifiers"));
9258 else
9260 specs->typespec_word = cts_int128;
9261 specs->locations[cdw_typespec] = loc;
9263 return specs;
9264 case RID_VOID:
9265 if (specs->long_p)
9266 error_at (loc,
9267 ("both %<long%> and %<void%> in "
9268 "declaration specifiers"));
9269 else if (specs->short_p)
9270 error_at (loc,
9271 ("both %<short%> and %<void%> in "
9272 "declaration specifiers"));
9273 else if (specs->signed_p)
9274 error_at (loc,
9275 ("both %<signed%> and %<void%> in "
9276 "declaration specifiers"));
9277 else if (specs->unsigned_p)
9278 error_at (loc,
9279 ("both %<unsigned%> and %<void%> in "
9280 "declaration specifiers"));
9281 else if (specs->complex_p)
9282 error_at (loc,
9283 ("both %<complex%> and %<void%> in "
9284 "declaration specifiers"));
9285 else if (specs->saturating_p)
9286 error_at (loc,
9287 ("both %<_Sat%> and %<void%> in "
9288 "declaration specifiers"));
9289 else
9291 specs->typespec_word = cts_void;
9292 specs->locations[cdw_typespec] = loc;
9294 return specs;
9295 case RID_BOOL:
9296 if (specs->long_p)
9297 error_at (loc,
9298 ("both %<long%> and %<_Bool%> in "
9299 "declaration specifiers"));
9300 else if (specs->short_p)
9301 error_at (loc,
9302 ("both %<short%> and %<_Bool%> in "
9303 "declaration specifiers"));
9304 else if (specs->signed_p)
9305 error_at (loc,
9306 ("both %<signed%> and %<_Bool%> in "
9307 "declaration specifiers"));
9308 else if (specs->unsigned_p)
9309 error_at (loc,
9310 ("both %<unsigned%> and %<_Bool%> in "
9311 "declaration specifiers"));
9312 else if (specs->complex_p)
9313 error_at (loc,
9314 ("both %<complex%> and %<_Bool%> in "
9315 "declaration specifiers"));
9316 else if (specs->saturating_p)
9317 error_at (loc,
9318 ("both %<_Sat%> and %<_Bool%> in "
9319 "declaration specifiers"));
9320 else
9322 specs->typespec_word = cts_bool;
9323 specs->locations[cdw_typespec] = loc;
9325 return specs;
9326 case RID_CHAR:
9327 if (specs->long_p)
9328 error_at (loc,
9329 ("both %<long%> and %<char%> in "
9330 "declaration specifiers"));
9331 else if (specs->short_p)
9332 error_at (loc,
9333 ("both %<short%> and %<char%> in "
9334 "declaration specifiers"));
9335 else if (specs->saturating_p)
9336 error_at (loc,
9337 ("both %<_Sat%> and %<char%> in "
9338 "declaration specifiers"));
9339 else
9341 specs->typespec_word = cts_char;
9342 specs->locations[cdw_typespec] = loc;
9344 return specs;
9345 case RID_INT:
9346 if (specs->saturating_p)
9347 error_at (loc,
9348 ("both %<_Sat%> and %<int%> in "
9349 "declaration specifiers"));
9350 else
9352 specs->typespec_word = cts_int;
9353 specs->locations[cdw_typespec] = loc;
9355 return specs;
9356 case RID_FLOAT:
9357 if (specs->long_p)
9358 error_at (loc,
9359 ("both %<long%> and %<float%> in "
9360 "declaration specifiers"));
9361 else if (specs->short_p)
9362 error_at (loc,
9363 ("both %<short%> and %<float%> in "
9364 "declaration specifiers"));
9365 else if (specs->signed_p)
9366 error_at (loc,
9367 ("both %<signed%> and %<float%> in "
9368 "declaration specifiers"));
9369 else if (specs->unsigned_p)
9370 error_at (loc,
9371 ("both %<unsigned%> and %<float%> in "
9372 "declaration specifiers"));
9373 else if (specs->saturating_p)
9374 error_at (loc,
9375 ("both %<_Sat%> and %<float%> in "
9376 "declaration specifiers"));
9377 else
9379 specs->typespec_word = cts_float;
9380 specs->locations[cdw_typespec] = loc;
9382 return specs;
9383 case RID_DOUBLE:
9384 if (specs->long_long_p)
9385 error_at (loc,
9386 ("both %<long long%> and %<double%> in "
9387 "declaration specifiers"));
9388 else if (specs->short_p)
9389 error_at (loc,
9390 ("both %<short%> and %<double%> in "
9391 "declaration specifiers"));
9392 else if (specs->signed_p)
9393 error_at (loc,
9394 ("both %<signed%> and %<double%> in "
9395 "declaration specifiers"));
9396 else if (specs->unsigned_p)
9397 error_at (loc,
9398 ("both %<unsigned%> and %<double%> in "
9399 "declaration specifiers"));
9400 else if (specs->saturating_p)
9401 error_at (loc,
9402 ("both %<_Sat%> and %<double%> in "
9403 "declaration specifiers"));
9404 else
9406 specs->typespec_word = cts_double;
9407 specs->locations[cdw_typespec] = loc;
9409 return specs;
9410 case RID_DFLOAT32:
9411 case RID_DFLOAT64:
9412 case RID_DFLOAT128:
9414 const char *str;
9415 if (i == RID_DFLOAT32)
9416 str = "_Decimal32";
9417 else if (i == RID_DFLOAT64)
9418 str = "_Decimal64";
9419 else
9420 str = "_Decimal128";
9421 if (specs->long_long_p)
9422 error_at (loc,
9423 ("both %<long long%> and %<%s%> in "
9424 "declaration specifiers"),
9425 str);
9426 if (specs->long_p)
9427 error_at (loc,
9428 ("both %<long%> and %<%s%> in "
9429 "declaration specifiers"),
9430 str);
9431 else if (specs->short_p)
9432 error_at (loc,
9433 ("both %<short%> and %<%s%> in "
9434 "declaration specifiers"),
9435 str);
9436 else if (specs->signed_p)
9437 error_at (loc,
9438 ("both %<signed%> and %<%s%> in "
9439 "declaration specifiers"),
9440 str);
9441 else if (specs->unsigned_p)
9442 error_at (loc,
9443 ("both %<unsigned%> and %<%s%> in "
9444 "declaration specifiers"),
9445 str);
9446 else if (specs->complex_p)
9447 error_at (loc,
9448 ("both %<complex%> and %<%s%> in "
9449 "declaration specifiers"),
9450 str);
9451 else if (specs->saturating_p)
9452 error_at (loc,
9453 ("both %<_Sat%> and %<%s%> in "
9454 "declaration specifiers"),
9455 str);
9456 else if (i == RID_DFLOAT32)
9457 specs->typespec_word = cts_dfloat32;
9458 else if (i == RID_DFLOAT64)
9459 specs->typespec_word = cts_dfloat64;
9460 else
9461 specs->typespec_word = cts_dfloat128;
9462 specs->locations[cdw_typespec] = loc;
9464 if (!targetm.decimal_float_supported_p ())
9465 error_at (loc,
9466 ("decimal floating point not supported "
9467 "for this target"));
9468 pedwarn (loc, OPT_Wpedantic,
9469 "ISO C does not support decimal floating point");
9470 return specs;
9471 case RID_FRACT:
9472 case RID_ACCUM:
9474 const char *str;
9475 if (i == RID_FRACT)
9476 str = "_Fract";
9477 else
9478 str = "_Accum";
9479 if (specs->complex_p)
9480 error_at (loc,
9481 ("both %<complex%> and %<%s%> in "
9482 "declaration specifiers"),
9483 str);
9484 else if (i == RID_FRACT)
9485 specs->typespec_word = cts_fract;
9486 else
9487 specs->typespec_word = cts_accum;
9488 specs->locations[cdw_typespec] = loc;
9490 if (!targetm.fixed_point_supported_p ())
9491 error_at (loc,
9492 "fixed-point types not supported for this target");
9493 pedwarn (loc, OPT_Wpedantic,
9494 "ISO C does not support fixed-point types");
9495 return specs;
9496 default:
9497 /* ObjC reserved word "id", handled below. */
9498 break;
9503 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
9504 form of ObjC type, cases such as "int" and "long" being handled
9505 above), a TYPE (struct, union, enum and typeof specifiers) or an
9506 ERROR_MARK. In none of these cases may there have previously
9507 been any type specifiers. */
9508 if (specs->type || specs->typespec_word != cts_none
9509 || specs->long_p || specs->short_p || specs->signed_p
9510 || specs->unsigned_p || specs->complex_p)
9511 error_at (loc, "two or more data types in declaration specifiers");
9512 else if (TREE_CODE (type) == TYPE_DECL)
9514 if (TREE_TYPE (type) == error_mark_node)
9515 ; /* Allow the type to default to int to avoid cascading errors. */
9516 else
9518 specs->type = TREE_TYPE (type);
9519 specs->decl_attr = DECL_ATTRIBUTES (type);
9520 specs->typedef_p = true;
9521 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
9522 specs->locations[cdw_typedef] = loc;
9524 /* If this typedef name is defined in a struct, then a C++
9525 lookup would return a different value. */
9526 if (warn_cxx_compat
9527 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
9528 warning_at (loc, OPT_Wc___compat,
9529 "C++ lookup of %qD would return a field, not a type",
9530 type);
9532 /* If we are parsing a struct, record that a struct field
9533 used a typedef. */
9534 if (warn_cxx_compat && struct_parse_info != NULL)
9535 struct_parse_info->typedefs_seen.safe_push (type);
9538 else if (TREE_CODE (type) == IDENTIFIER_NODE)
9540 tree t = lookup_name (type);
9541 if (!t || TREE_CODE (t) != TYPE_DECL)
9542 error_at (loc, "%qE fails to be a typedef or built in type", type);
9543 else if (TREE_TYPE (t) == error_mark_node)
9545 else
9547 specs->type = TREE_TYPE (t);
9548 specs->locations[cdw_typespec] = loc;
9551 else
9553 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
9555 specs->typedef_p = true;
9556 specs->locations[cdw_typedef] = loc;
9557 if (spec.expr)
9559 if (specs->expr)
9560 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
9561 specs->expr, spec.expr);
9562 else
9563 specs->expr = spec.expr;
9564 specs->expr_const_operands &= spec.expr_const_operands;
9567 specs->type = type;
9570 return specs;
9573 /* Add the storage class specifier or function specifier SCSPEC to the
9574 declaration specifiers SPECS, returning SPECS. */
9576 struct c_declspecs *
9577 declspecs_add_scspec (source_location loc,
9578 struct c_declspecs *specs,
9579 tree scspec)
9581 enum rid i;
9582 enum c_storage_class n = csc_none;
9583 bool dupe = false;
9584 specs->declspecs_seen_p = true;
9585 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
9586 && C_IS_RESERVED_WORD (scspec));
9587 i = C_RID_CODE (scspec);
9588 if (specs->non_sc_seen_p)
9589 warning (OPT_Wold_style_declaration,
9590 "%qE is not at beginning of declaration", scspec);
9591 switch (i)
9593 case RID_INLINE:
9594 /* C99 permits duplicate inline. Although of doubtful utility,
9595 it seems simplest to permit it in gnu89 mode as well, as
9596 there is also little utility in maintaining this as a
9597 difference between gnu89 and C99 inline. */
9598 dupe = false;
9599 specs->inline_p = true;
9600 specs->locations[cdw_inline] = loc;
9601 break;
9602 case RID_NORETURN:
9603 /* Duplicate _Noreturn is permitted. */
9604 dupe = false;
9605 specs->noreturn_p = true;
9606 specs->locations[cdw_noreturn] = loc;
9607 break;
9608 case RID_THREAD:
9609 dupe = specs->thread_p;
9610 if (specs->storage_class == csc_auto)
9611 error ("%<__thread%> used with %<auto%>");
9612 else if (specs->storage_class == csc_register)
9613 error ("%<__thread%> used with %<register%>");
9614 else if (specs->storage_class == csc_typedef)
9615 error ("%<__thread%> used with %<typedef%>");
9616 else
9618 specs->thread_p = true;
9619 specs->locations[cdw_thread] = loc;
9621 break;
9622 case RID_AUTO:
9623 n = csc_auto;
9624 break;
9625 case RID_EXTERN:
9626 n = csc_extern;
9627 /* Diagnose "__thread extern". */
9628 if (specs->thread_p)
9629 error ("%<__thread%> before %<extern%>");
9630 break;
9631 case RID_REGISTER:
9632 n = csc_register;
9633 break;
9634 case RID_STATIC:
9635 n = csc_static;
9636 /* Diagnose "__thread static". */
9637 if (specs->thread_p)
9638 error ("%<__thread%> before %<static%>");
9639 break;
9640 case RID_TYPEDEF:
9641 n = csc_typedef;
9642 break;
9643 default:
9644 gcc_unreachable ();
9646 if (n != csc_none && n == specs->storage_class)
9647 dupe = true;
9648 if (dupe)
9649 error ("duplicate %qE", scspec);
9650 if (n != csc_none)
9652 if (specs->storage_class != csc_none && n != specs->storage_class)
9654 error ("multiple storage classes in declaration specifiers");
9656 else
9658 specs->storage_class = n;
9659 specs->locations[cdw_storage_class] = loc;
9660 if (n != csc_extern && n != csc_static && specs->thread_p)
9662 error ("%<__thread%> used with %qE", scspec);
9663 specs->thread_p = false;
9667 return specs;
9670 /* Add the attributes ATTRS to the declaration specifiers SPECS,
9671 returning SPECS. */
9673 struct c_declspecs *
9674 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
9676 specs->attrs = chainon (attrs, specs->attrs);
9677 specs->locations[cdw_attributes] = loc;
9678 specs->declspecs_seen_p = true;
9679 return specs;
9682 /* Add an _Alignas specifier (expression ALIGN, or type whose
9683 alignment is ALIGN) to the declaration specifiers SPECS, returning
9684 SPECS. */
9685 struct c_declspecs *
9686 declspecs_add_alignas (source_location loc,
9687 struct c_declspecs *specs, tree align)
9689 int align_log;
9690 specs->alignas_p = true;
9691 specs->locations[cdw_alignas] = loc;
9692 if (align == error_mark_node)
9693 return specs;
9694 align_log = check_user_alignment (align, true);
9695 if (align_log > specs->align_log)
9696 specs->align_log = align_log;
9697 return specs;
9700 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
9701 specifiers with any other type specifier to determine the resulting
9702 type. This is where ISO C checks on complex types are made, since
9703 "_Complex long" is a prefix of the valid ISO C type "_Complex long
9704 double". */
9706 struct c_declspecs *
9707 finish_declspecs (struct c_declspecs *specs)
9709 /* If a type was specified as a whole, we have no modifiers and are
9710 done. */
9711 if (specs->type != NULL_TREE)
9713 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9714 && !specs->signed_p && !specs->unsigned_p
9715 && !specs->complex_p);
9717 /* Set a dummy type. */
9718 if (TREE_CODE (specs->type) == ERROR_MARK)
9719 specs->type = integer_type_node;
9720 return specs;
9723 /* If none of "void", "_Bool", "char", "int", "float" or "double"
9724 has been specified, treat it as "int" unless "_Complex" is
9725 present and there are no other specifiers. If we just have
9726 "_Complex", it is equivalent to "_Complex double", but e.g.
9727 "_Complex short" is equivalent to "_Complex short int". */
9728 if (specs->typespec_word == cts_none)
9730 if (specs->saturating_p)
9732 error_at (specs->locations[cdw_saturating],
9733 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
9734 if (!targetm.fixed_point_supported_p ())
9735 error_at (specs->locations[cdw_saturating],
9736 "fixed-point types not supported for this target");
9737 specs->typespec_word = cts_fract;
9739 else if (specs->long_p || specs->short_p
9740 || specs->signed_p || specs->unsigned_p)
9742 specs->typespec_word = cts_int;
9744 else if (specs->complex_p)
9746 specs->typespec_word = cts_double;
9747 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
9748 "ISO C does not support plain %<complex%> meaning "
9749 "%<double complex%>");
9751 else
9753 specs->typespec_word = cts_int;
9754 specs->default_int_p = true;
9755 /* We don't diagnose this here because grokdeclarator will
9756 give more specific diagnostics according to whether it is
9757 a function definition. */
9761 /* If "signed" was specified, record this to distinguish "int" and
9762 "signed int" in the case of a bit-field with
9763 -funsigned-bitfields. */
9764 specs->explicit_signed_p = specs->signed_p;
9766 /* Now compute the actual type. */
9767 switch (specs->typespec_word)
9769 case cts_void:
9770 gcc_assert (!specs->long_p && !specs->short_p
9771 && !specs->signed_p && !specs->unsigned_p
9772 && !specs->complex_p);
9773 specs->type = void_type_node;
9774 break;
9775 case cts_bool:
9776 gcc_assert (!specs->long_p && !specs->short_p
9777 && !specs->signed_p && !specs->unsigned_p
9778 && !specs->complex_p);
9779 specs->type = boolean_type_node;
9780 break;
9781 case cts_char:
9782 gcc_assert (!specs->long_p && !specs->short_p);
9783 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9784 if (specs->signed_p)
9785 specs->type = signed_char_type_node;
9786 else if (specs->unsigned_p)
9787 specs->type = unsigned_char_type_node;
9788 else
9789 specs->type = char_type_node;
9790 if (specs->complex_p)
9792 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
9793 "ISO C does not support complex integer types");
9794 specs->type = build_complex_type (specs->type);
9796 break;
9797 case cts_int128:
9798 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
9799 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9800 specs->type = (specs->unsigned_p
9801 ? int128_unsigned_type_node
9802 : int128_integer_type_node);
9803 if (specs->complex_p)
9805 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
9806 "ISO C does not support complex integer types");
9807 specs->type = build_complex_type (specs->type);
9809 break;
9810 case cts_int:
9811 gcc_assert (!(specs->long_p && specs->short_p));
9812 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9813 if (specs->long_long_p)
9814 specs->type = (specs->unsigned_p
9815 ? long_long_unsigned_type_node
9816 : long_long_integer_type_node);
9817 else if (specs->long_p)
9818 specs->type = (specs->unsigned_p
9819 ? long_unsigned_type_node
9820 : long_integer_type_node);
9821 else if (specs->short_p)
9822 specs->type = (specs->unsigned_p
9823 ? short_unsigned_type_node
9824 : short_integer_type_node);
9825 else
9826 specs->type = (specs->unsigned_p
9827 ? unsigned_type_node
9828 : integer_type_node);
9829 if (specs->complex_p)
9831 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
9832 "ISO C does not support complex integer types");
9833 specs->type = build_complex_type (specs->type);
9835 break;
9836 case cts_float:
9837 gcc_assert (!specs->long_p && !specs->short_p
9838 && !specs->signed_p && !specs->unsigned_p);
9839 specs->type = (specs->complex_p
9840 ? complex_float_type_node
9841 : float_type_node);
9842 break;
9843 case cts_double:
9844 gcc_assert (!specs->long_long_p && !specs->short_p
9845 && !specs->signed_p && !specs->unsigned_p);
9846 if (specs->long_p)
9848 specs->type = (specs->complex_p
9849 ? complex_long_double_type_node
9850 : long_double_type_node);
9852 else
9854 specs->type = (specs->complex_p
9855 ? complex_double_type_node
9856 : double_type_node);
9858 break;
9859 case cts_dfloat32:
9860 case cts_dfloat64:
9861 case cts_dfloat128:
9862 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9863 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
9864 if (specs->typespec_word == cts_dfloat32)
9865 specs->type = dfloat32_type_node;
9866 else if (specs->typespec_word == cts_dfloat64)
9867 specs->type = dfloat64_type_node;
9868 else
9869 specs->type = dfloat128_type_node;
9870 break;
9871 case cts_fract:
9872 gcc_assert (!specs->complex_p);
9873 if (!targetm.fixed_point_supported_p ())
9874 specs->type = integer_type_node;
9875 else if (specs->saturating_p)
9877 if (specs->long_long_p)
9878 specs->type = specs->unsigned_p
9879 ? sat_unsigned_long_long_fract_type_node
9880 : sat_long_long_fract_type_node;
9881 else if (specs->long_p)
9882 specs->type = specs->unsigned_p
9883 ? sat_unsigned_long_fract_type_node
9884 : sat_long_fract_type_node;
9885 else if (specs->short_p)
9886 specs->type = specs->unsigned_p
9887 ? sat_unsigned_short_fract_type_node
9888 : sat_short_fract_type_node;
9889 else
9890 specs->type = specs->unsigned_p
9891 ? sat_unsigned_fract_type_node
9892 : sat_fract_type_node;
9894 else
9896 if (specs->long_long_p)
9897 specs->type = specs->unsigned_p
9898 ? unsigned_long_long_fract_type_node
9899 : long_long_fract_type_node;
9900 else if (specs->long_p)
9901 specs->type = specs->unsigned_p
9902 ? unsigned_long_fract_type_node
9903 : long_fract_type_node;
9904 else if (specs->short_p)
9905 specs->type = specs->unsigned_p
9906 ? unsigned_short_fract_type_node
9907 : short_fract_type_node;
9908 else
9909 specs->type = specs->unsigned_p
9910 ? unsigned_fract_type_node
9911 : fract_type_node;
9913 break;
9914 case cts_accum:
9915 gcc_assert (!specs->complex_p);
9916 if (!targetm.fixed_point_supported_p ())
9917 specs->type = integer_type_node;
9918 else if (specs->saturating_p)
9920 if (specs->long_long_p)
9921 specs->type = specs->unsigned_p
9922 ? sat_unsigned_long_long_accum_type_node
9923 : sat_long_long_accum_type_node;
9924 else if (specs->long_p)
9925 specs->type = specs->unsigned_p
9926 ? sat_unsigned_long_accum_type_node
9927 : sat_long_accum_type_node;
9928 else if (specs->short_p)
9929 specs->type = specs->unsigned_p
9930 ? sat_unsigned_short_accum_type_node
9931 : sat_short_accum_type_node;
9932 else
9933 specs->type = specs->unsigned_p
9934 ? sat_unsigned_accum_type_node
9935 : sat_accum_type_node;
9937 else
9939 if (specs->long_long_p)
9940 specs->type = specs->unsigned_p
9941 ? unsigned_long_long_accum_type_node
9942 : long_long_accum_type_node;
9943 else if (specs->long_p)
9944 specs->type = specs->unsigned_p
9945 ? unsigned_long_accum_type_node
9946 : long_accum_type_node;
9947 else if (specs->short_p)
9948 specs->type = specs->unsigned_p
9949 ? unsigned_short_accum_type_node
9950 : short_accum_type_node;
9951 else
9952 specs->type = specs->unsigned_p
9953 ? unsigned_accum_type_node
9954 : accum_type_node;
9956 break;
9957 default:
9958 gcc_unreachable ();
9961 return specs;
9964 /* A subroutine of c_write_global_declarations. Perform final processing
9965 on one file scope's declarations (or the external scope's declarations),
9966 GLOBALS. */
9968 static void
9969 c_write_global_declarations_1 (tree globals)
9971 tree decl;
9972 bool reconsider;
9974 /* Process the decls in the order they were written. */
9975 for (decl = globals; decl; decl = DECL_CHAIN (decl))
9977 /* Check for used but undefined static functions using the C
9978 standard's definition of "used", and set TREE_NO_WARNING so
9979 that check_global_declarations doesn't repeat the check. */
9980 if (TREE_CODE (decl) == FUNCTION_DECL
9981 && DECL_INITIAL (decl) == 0
9982 && DECL_EXTERNAL (decl)
9983 && !TREE_PUBLIC (decl)
9984 && C_DECL_USED (decl))
9986 pedwarn (input_location, 0, "%q+F used but never defined", decl);
9987 TREE_NO_WARNING (decl) = 1;
9990 wrapup_global_declaration_1 (decl);
9995 reconsider = false;
9996 for (decl = globals; decl; decl = DECL_CHAIN (decl))
9997 reconsider |= wrapup_global_declaration_2 (decl);
9999 while (reconsider);
10001 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10002 check_global_declaration_1 (decl);
10005 /* A subroutine of c_write_global_declarations Emit debug information for each
10006 of the declarations in GLOBALS. */
10008 static void
10009 c_write_global_declarations_2 (tree globals)
10011 tree decl;
10013 for (decl = globals; decl ; decl = DECL_CHAIN (decl))
10014 debug_hooks->global_decl (decl);
10017 /* Callback to collect a source_ref from a DECL. */
10019 static void
10020 collect_source_ref_cb (tree decl)
10022 if (!DECL_IS_BUILTIN (decl))
10023 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
10026 /* Preserve the external declarations scope across a garbage collect. */
10027 static GTY(()) tree ext_block;
10029 /* Collect all references relevant to SOURCE_FILE. */
10031 static void
10032 collect_all_refs (const char *source_file)
10034 tree t;
10035 unsigned i;
10037 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10038 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
10040 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
10043 /* Iterate over all global declarations and call CALLBACK. */
10045 static void
10046 for_each_global_decl (void (*callback) (tree decl))
10048 tree t;
10049 tree decls;
10050 tree decl;
10051 unsigned i;
10053 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10055 decls = DECL_INITIAL (t);
10056 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
10057 callback (decl);
10060 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
10061 callback (decl);
10064 void
10065 c_write_global_declarations (void)
10067 tree t;
10068 unsigned i;
10070 /* We don't want to do this if generating a PCH. */
10071 if (pch_file)
10072 return;
10074 timevar_start (TV_PHASE_DEFERRED);
10076 /* Do the Objective-C stuff. This is where all the Objective-C
10077 module stuff gets generated (symtab, class/protocol/selector
10078 lists etc). */
10079 if (c_dialect_objc ())
10080 objc_write_global_declarations ();
10082 /* Close the external scope. */
10083 ext_block = pop_scope ();
10084 external_scope = 0;
10085 gcc_assert (!current_scope);
10087 /* Handle -fdump-ada-spec[-slim]. */
10088 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
10090 /* Build a table of files to generate specs for */
10091 if (flag_dump_ada_spec_slim)
10092 collect_source_ref (main_input_filename);
10093 else
10094 for_each_global_decl (collect_source_ref_cb);
10096 dump_ada_specs (collect_all_refs, NULL);
10099 if (ext_block)
10101 tree tmp = BLOCK_VARS (ext_block);
10102 int flags;
10103 FILE * stream = dump_begin (TDI_tu, &flags);
10104 if (stream && tmp)
10106 dump_node (tmp, flags & ~TDF_SLIM, stream);
10107 dump_end (TDI_tu, stream);
10111 /* Process all file scopes in this compilation, and the external_scope,
10112 through wrapup_global_declarations and check_global_declarations. */
10113 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10114 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
10115 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
10117 timevar_stop (TV_PHASE_DEFERRED);
10118 timevar_start (TV_PHASE_OPT_GEN);
10120 /* We're done parsing; proceed to optimize and emit assembly.
10121 FIXME: shouldn't be the front end's responsibility to call this. */
10122 finalize_compilation_unit ();
10124 timevar_stop (TV_PHASE_OPT_GEN);
10125 timevar_start (TV_PHASE_DBGINFO);
10127 /* After cgraph has had a chance to emit everything that's going to
10128 be emitted, output debug information for globals. */
10129 if (!seen_error ())
10131 timevar_push (TV_SYMOUT);
10132 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10133 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
10134 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
10135 timevar_pop (TV_SYMOUT);
10138 ext_block = NULL;
10139 timevar_stop (TV_PHASE_DBGINFO);
10142 /* Register reserved keyword WORD as qualifier for address space AS. */
10144 void
10145 c_register_addr_space (const char *word, addr_space_t as)
10147 int rid = RID_FIRST_ADDR_SPACE + as;
10148 tree id;
10150 /* Address space qualifiers are only supported
10151 in C with GNU extensions enabled. */
10152 if (c_dialect_objc () || flag_no_asm)
10153 return;
10155 id = get_identifier (word);
10156 C_SET_RID_CODE (id, rid);
10157 C_IS_RESERVED_WORD (id) = 1;
10158 ridpointers [rid] = id;
10161 #include "gt-c-c-decl.h"