]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c/c-decl.c
ec0d0ef09a7f400e24b52519f06cdb50d8983a2a
[thirdparty/gcc.git] / gcc / c / c-decl.c
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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. */
23
24 /* ??? not all decl nodes are given the most useful possible
25 line numbers. For example, the CONST_DECLs for enum values. */
26
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 "print-tree.h"
35 #include "stor-layout.h"
36 #include "varasm.h"
37 #include "attribs.h"
38 #include "stringpool.h"
39 #include "tree-inline.h"
40 #include "flags.h"
41 #include "function.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "tm_p.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "debug.h"
48 #include "opts.h"
49 #include "timevar.h"
50 #include "c-family/c-common.h"
51 #include "c-family/c-objc.h"
52 #include "c-family/c-pragma.h"
53 #include "c-family/c-ubsan.h"
54 #include "c-lang.h"
55 #include "langhooks.h"
56 #include "tree-iterator.h"
57 #include "diagnostic-core.h"
58 #include "dumpfile.h"
59 #include "cgraph.h"
60 #include "hash-table.h"
61 #include "langhooks-def.h"
62 #include "hash-set.h"
63 #include "plugin.h"
64 #include "c-family/c-ada-spec.h"
65 #include "cilk.h"
66 #include "builtins.h"
67
68 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
69 enum decl_context
70 { NORMAL, /* Ordinary declaration */
71 FUNCDEF, /* Function definition */
72 PARM, /* Declaration of parm before function body */
73 FIELD, /* Declaration inside struct or union */
74 TYPENAME}; /* Typename (inside cast or sizeof) */
75
76 /* States indicating how grokdeclarator() should handle declspecs marked
77 with __attribute__((deprecated)). An object declared as
78 __attribute__((deprecated)) suppresses warnings of uses of other
79 deprecated items. */
80
81 enum deprecated_states {
82 DEPRECATED_NORMAL,
83 DEPRECATED_SUPPRESS
84 };
85
86 \f
87 /* Nonzero if we have seen an invalid cross reference
88 to a struct, union, or enum, but not yet printed the message. */
89 tree pending_invalid_xref;
90
91 /* File and line to appear in the eventual error message. */
92 location_t pending_invalid_xref_location;
93
94 /* The file and line that the prototype came from if this is an
95 old-style definition; used for diagnostics in
96 store_parm_decls_oldstyle. */
97
98 static location_t current_function_prototype_locus;
99
100 /* Whether this prototype was built-in. */
101
102 static bool current_function_prototype_built_in;
103
104 /* The argument type information of this prototype. */
105
106 static tree current_function_prototype_arg_types;
107
108 /* The argument information structure for the function currently being
109 defined. */
110
111 static struct c_arg_info *current_function_arg_info;
112
113 /* The obstack on which parser and related data structures, which are
114 not live beyond their top-level declaration or definition, are
115 allocated. */
116 struct obstack parser_obstack;
117
118 /* The current statement tree. */
119
120 static GTY(()) struct stmt_tree_s c_stmt_tree;
121
122 /* State saving variables. */
123 tree c_break_label;
124 tree c_cont_label;
125
126 /* A list of decls to be made automatically visible in each file scope. */
127 static GTY(()) tree visible_builtins;
128
129 /* Set to 0 at beginning of a function definition, set to 1 if
130 a return statement that specifies a return value is seen. */
131
132 int current_function_returns_value;
133
134 /* Set to 0 at beginning of a function definition, set to 1 if
135 a return statement with no argument is seen. */
136
137 int current_function_returns_null;
138
139 /* Set to 0 at beginning of a function definition, set to 1 if
140 a call to a noreturn function is seen. */
141
142 int current_function_returns_abnormally;
143
144 /* Set to nonzero by `grokdeclarator' for a function
145 whose return type is defaulted, if warnings for this are desired. */
146
147 static int warn_about_return_type;
148
149 /* Nonzero when the current toplevel function contains a declaration
150 of a nested function which is never defined. */
151
152 static bool undef_nested_function;
153
154 /* Mode used to build pointers (VOIDmode means ptr_mode). */
155
156 enum machine_mode c_default_pointer_mode = VOIDmode;
157
158 /* If non-zero, implicit "omp declare target" attribute is added into the
159 attribute lists. */
160 int current_omp_declare_target_attribute;
161 \f
162 /* Each c_binding structure describes one binding of an identifier to
163 a decl. All the decls in a scope - irrespective of namespace - are
164 chained together by the ->prev field, which (as the name implies)
165 runs in reverse order. All the decls in a given namespace bound to
166 a given identifier are chained by the ->shadowed field, which runs
167 from inner to outer scopes.
168
169 The ->decl field usually points to a DECL node, but there are two
170 exceptions. In the namespace of type tags, the bound entity is a
171 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
172 identifier is encountered, it is bound to error_mark_node to
173 suppress further errors about that identifier in the current
174 function.
175
176 The ->u.type field stores the type of the declaration in this scope;
177 if NULL, the type is the type of the ->decl field. This is only of
178 relevance for objects with external or internal linkage which may
179 be redeclared in inner scopes, forming composite types that only
180 persist for the duration of those scopes. In the external scope,
181 this stores the composite of all the types declared for this
182 object, visible or not. The ->inner_comp field (used only at file
183 scope) stores whether an incomplete array type at file scope was
184 completed at an inner scope to an array size other than 1.
185
186 The ->u.label field is used for labels. It points to a structure
187 which stores additional information used for warnings.
188
189 The depth field is copied from the scope structure that holds this
190 decl. It is used to preserve the proper ordering of the ->shadowed
191 field (see bind()) and also for a handful of special-case checks.
192 Finally, the invisible bit is true for a decl which should be
193 ignored for purposes of normal name lookup, and the nested bit is
194 true for a decl that's been bound a second time in an inner scope;
195 in all such cases, the binding in the outer scope will have its
196 invisible bit true. */
197
198 struct GTY((chain_next ("%h.prev"))) c_binding {
199 union GTY(()) { /* first so GTY desc can use decl */
200 tree GTY((tag ("0"))) type; /* the type in this scope */
201 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
202 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
203 tree decl; /* the decl bound */
204 tree id; /* the identifier it's bound to */
205 struct c_binding *prev; /* the previous decl in this scope */
206 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
207 unsigned int depth : 28; /* depth of this scope */
208 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
209 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
210 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
211 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
212 location_t locus; /* location for nested bindings */
213 };
214 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
215 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
216 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
217 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
218
219 #define I_SYMBOL_BINDING(node) \
220 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
221 #define I_SYMBOL_DECL(node) \
222 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
223
224 #define I_TAG_BINDING(node) \
225 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
226 #define I_TAG_DECL(node) \
227 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
228
229 #define I_LABEL_BINDING(node) \
230 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
231 #define I_LABEL_DECL(node) \
232 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
233
234 /* Each C symbol points to three linked lists of c_binding structures.
235 These describe the values of the identifier in the three different
236 namespaces defined by the language. */
237
238 struct GTY(()) lang_identifier {
239 struct c_common_identifier common_id;
240 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
241 struct c_binding *tag_binding; /* struct/union/enum tags */
242 struct c_binding *label_binding; /* labels */
243 };
244
245 /* Validate c-lang.c's assumptions. */
246 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
247 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
248
249 /* The resulting tree type. */
250
251 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
252 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
253 {
254 union tree_node GTY ((tag ("0"),
255 desc ("tree_node_structure (&%h)")))
256 generic;
257 struct lang_identifier GTY ((tag ("1"))) identifier;
258 };
259
260 /* Track bindings and other things that matter for goto warnings. For
261 efficiency, we do not gather all the decls at the point of
262 definition. Instead, we point into the bindings structure. As
263 scopes are popped, we update these structures and gather the decls
264 that matter at that time. */
265
266 struct GTY(()) c_spot_bindings {
267 /* The currently open scope which holds bindings defined when the
268 label was defined or the goto statement was found. */
269 struct c_scope *scope;
270 /* The bindings in the scope field which were defined at the point
271 of the label or goto. This lets us look at older or newer
272 bindings in the scope, as appropriate. */
273 struct c_binding *bindings_in_scope;
274 /* The number of statement expressions that have started since this
275 label or goto statement was defined. This is zero if we are at
276 the same statement expression level. It is positive if we are in
277 a statement expression started since this spot. It is negative
278 if this spot was in a statement expression and we have left
279 it. */
280 int stmt_exprs;
281 /* Whether we started in a statement expression but are no longer in
282 it. This is set to true if stmt_exprs ever goes negative. */
283 bool left_stmt_expr;
284 };
285
286 /* This structure is used to keep track of bindings seen when a goto
287 statement is defined. This is only used if we see the goto
288 statement before we see the label. */
289
290 struct GTY(()) c_goto_bindings {
291 /* The location of the goto statement. */
292 location_t loc;
293 /* The bindings of the goto statement. */
294 struct c_spot_bindings goto_bindings;
295 };
296
297 typedef struct c_goto_bindings *c_goto_bindings_p;
298
299 /* The additional information we keep track of for a label binding.
300 These fields are updated as scopes are popped. */
301
302 struct GTY(()) c_label_vars {
303 /* The shadowed c_label_vars, when one label shadows another (which
304 can only happen using a __label__ declaration). */
305 struct c_label_vars *shadowed;
306 /* The bindings when the label was defined. */
307 struct c_spot_bindings label_bindings;
308 /* A list of decls that we care about: decls about which we should
309 warn if a goto branches to this label from later in the function.
310 Decls are added to this list as scopes are popped. We only add
311 the decls that matter. */
312 vec<tree, va_gc> *decls_in_scope;
313 /* A list of goto statements to this label. This is only used for
314 goto statements seen before the label was defined, so that we can
315 issue appropriate warnings for them. */
316 vec<c_goto_bindings_p, va_gc> *gotos;
317 };
318
319 /* Each c_scope structure describes the complete contents of one
320 scope. Four scopes are distinguished specially: the innermost or
321 current scope, the innermost function scope, the file scope (always
322 the second to outermost) and the outermost or external scope.
323
324 Most declarations are recorded in the current scope.
325
326 All normal label declarations are recorded in the innermost
327 function scope, as are bindings of undeclared identifiers to
328 error_mark_node. (GCC permits nested functions as an extension,
329 hence the 'innermost' qualifier.) Explicitly declared labels
330 (using the __label__ extension) appear in the current scope.
331
332 Being in the file scope (current_scope == file_scope) causes
333 special behavior in several places below. Also, under some
334 conditions the Objective-C front end records declarations in the
335 file scope even though that isn't the current scope.
336
337 All declarations with external linkage are recorded in the external
338 scope, even if they aren't visible there; this models the fact that
339 such declarations are visible to the entire program, and (with a
340 bit of cleverness, see pushdecl) allows diagnosis of some violations
341 of C99 6.2.2p7 and 6.2.7p2:
342
343 If, within the same translation unit, the same identifier appears
344 with both internal and external linkage, the behavior is
345 undefined.
346
347 All declarations that refer to the same object or function shall
348 have compatible type; otherwise, the behavior is undefined.
349
350 Initially only the built-in declarations, which describe compiler
351 intrinsic functions plus a subset of the standard library, are in
352 this scope.
353
354 The order of the blocks list matters, and it is frequently appended
355 to. To avoid having to walk all the way to the end of the list on
356 each insertion, or reverse the list later, we maintain a pointer to
357 the last list entry. (FIXME: It should be feasible to use a reversed
358 list here.)
359
360 The bindings list is strictly in reverse order of declarations;
361 pop_scope relies on this. */
362
363
364 struct GTY((chain_next ("%h.outer"))) c_scope {
365 /* The scope containing this one. */
366 struct c_scope *outer;
367
368 /* The next outermost function scope. */
369 struct c_scope *outer_function;
370
371 /* All bindings in this scope. */
372 struct c_binding *bindings;
373
374 /* For each scope (except the global one), a chain of BLOCK nodes
375 for all the scopes that were entered and exited one level down. */
376 tree blocks;
377 tree blocks_last;
378
379 /* The depth of this scope. Used to keep the ->shadowed chain of
380 bindings sorted innermost to outermost. */
381 unsigned int depth : 28;
382
383 /* True if we are currently filling this scope with parameter
384 declarations. */
385 BOOL_BITFIELD parm_flag : 1;
386
387 /* True if we saw [*] in this scope. Used to give an error messages
388 if these appears in a function definition. */
389 BOOL_BITFIELD had_vla_unspec : 1;
390
391 /* True if we already complained about forward parameter decls
392 in this scope. This prevents double warnings on
393 foo (int a; int b; ...) */
394 BOOL_BITFIELD warned_forward_parm_decls : 1;
395
396 /* True if this is the outermost block scope of a function body.
397 This scope contains the parameters, the local variables declared
398 in the outermost block, and all the labels (except those in
399 nested functions, or declared at block scope with __label__). */
400 BOOL_BITFIELD function_body : 1;
401
402 /* True means make a BLOCK for this scope no matter what. */
403 BOOL_BITFIELD keep : 1;
404
405 /* True means that an unsuffixed float constant is _Decimal64. */
406 BOOL_BITFIELD float_const_decimal64 : 1;
407
408 /* True if this scope has any label bindings. This is used to speed
409 up searching for labels when popping scopes, particularly since
410 labels are normally only found at function scope. */
411 BOOL_BITFIELD has_label_bindings : 1;
412
413 /* True if we should issue a warning if a goto statement crosses any
414 of the bindings. We still need to check the list of bindings to
415 find the specific ones we need to warn about. This is true if
416 decl_jump_unsafe would return true for any of the bindings. This
417 is used to avoid looping over all the bindings unnecessarily. */
418 BOOL_BITFIELD has_jump_unsafe_decl : 1;
419 };
420
421 /* The scope currently in effect. */
422
423 static GTY(()) struct c_scope *current_scope;
424
425 /* The innermost function scope. Ordinary (not explicitly declared)
426 labels, bindings to error_mark_node, and the lazily-created
427 bindings of __func__ and its friends get this scope. */
428
429 static GTY(()) struct c_scope *current_function_scope;
430
431 /* The C file scope. This is reset for each input translation unit. */
432
433 static GTY(()) struct c_scope *file_scope;
434
435 /* The outermost scope. This is used for all declarations with
436 external linkage, and only these, hence the name. */
437
438 static GTY(()) struct c_scope *external_scope;
439
440 /* A chain of c_scope structures awaiting reuse. */
441
442 static GTY((deletable)) struct c_scope *scope_freelist;
443
444 /* A chain of c_binding structures awaiting reuse. */
445
446 static GTY((deletable)) struct c_binding *binding_freelist;
447
448 /* Append VAR to LIST in scope SCOPE. */
449 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
450 struct c_scope *s_ = (scope); \
451 tree d_ = (decl); \
452 if (s_->list##_last) \
453 BLOCK_CHAIN (s_->list##_last) = d_; \
454 else \
455 s_->list = d_; \
456 s_->list##_last = d_; \
457 } while (0)
458
459 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
460 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
461 struct c_scope *t_ = (tscope); \
462 struct c_scope *f_ = (fscope); \
463 if (t_->to##_last) \
464 BLOCK_CHAIN (t_->to##_last) = f_->from; \
465 else \
466 t_->to = f_->from; \
467 t_->to##_last = f_->from##_last; \
468 } while (0)
469
470 /* A c_inline_static structure stores details of a static identifier
471 referenced in a definition of a function that may be an inline
472 definition if no subsequent declaration of that function uses
473 "extern" or does not use "inline". */
474
475 struct GTY((chain_next ("%h.next"))) c_inline_static {
476 /* The location for a diagnostic. */
477 location_t location;
478
479 /* The function that may be an inline definition. */
480 tree function;
481
482 /* The object or function referenced. */
483 tree static_decl;
484
485 /* What sort of reference this is. */
486 enum c_inline_static_type type;
487
488 /* The next such structure or NULL. */
489 struct c_inline_static *next;
490 };
491
492 /* List of static identifiers used or referenced in functions that may
493 be inline definitions. */
494 static GTY(()) struct c_inline_static *c_inline_statics;
495
496 /* True means unconditionally make a BLOCK for the next scope pushed. */
497
498 static bool keep_next_level_flag;
499
500 /* True means the next call to push_scope will be the outermost scope
501 of a function body, so do not push a new scope, merely cease
502 expecting parameter decls. */
503
504 static bool next_is_function_body;
505
506 /* A vector of pointers to c_binding structures. */
507
508 typedef struct c_binding *c_binding_ptr;
509
510 /* Information that we keep for a struct or union while it is being
511 parsed. */
512
513 struct c_struct_parse_info
514 {
515 /* If warn_cxx_compat, a list of types defined within this
516 struct. */
517 vec<tree> struct_types;
518 /* If warn_cxx_compat, a list of field names which have bindings,
519 and which are defined in this struct, but which are not defined
520 in any enclosing struct. This is used to clear the in_struct
521 field of the c_bindings structure. */
522 vec<c_binding_ptr> fields;
523 /* If warn_cxx_compat, a list of typedef names used when defining
524 fields in this struct. */
525 vec<tree> typedefs_seen;
526 };
527
528 /* Information for the struct or union currently being parsed, or
529 NULL if not parsing a struct or union. */
530 static struct c_struct_parse_info *struct_parse_info;
531
532 /* Forward declarations. */
533 static tree lookup_name_in_scope (tree, struct c_scope *);
534 static tree c_make_fname_decl (location_t, tree, int);
535 static tree grokdeclarator (const struct c_declarator *,
536 struct c_declspecs *,
537 enum decl_context, bool, tree *, tree *, tree *,
538 bool *, enum deprecated_states);
539 static tree grokparms (struct c_arg_info *, bool);
540 static void layout_array_type (tree);
541 static void warn_defaults_to (location_t, int, const char *, ...)
542 ATTRIBUTE_GCC_DIAG(3,4);
543 \f
544 /* T is a statement. Add it to the statement-tree. This is the
545 C/ObjC version--C++ has a slightly different version of this
546 function. */
547
548 tree
549 add_stmt (tree t)
550 {
551 enum tree_code code = TREE_CODE (t);
552
553 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
554 {
555 if (!EXPR_HAS_LOCATION (t))
556 SET_EXPR_LOCATION (t, input_location);
557 }
558
559 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
560 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
561
562 /* Add T to the statement-tree. Non-side-effect statements need to be
563 recorded during statement expressions. */
564 if (!building_stmt_list_p ())
565 push_stmt_list ();
566 append_to_statement_list_force (t, &cur_stmt_list);
567
568 return t;
569 }
570 \f
571 /* Build a pointer type using the default pointer mode. */
572
573 static tree
574 c_build_pointer_type (tree to_type)
575 {
576 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
577 : TYPE_ADDR_SPACE (to_type);
578 enum machine_mode pointer_mode;
579
580 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
581 pointer_mode = targetm.addr_space.pointer_mode (as);
582 else
583 pointer_mode = c_default_pointer_mode;
584 return build_pointer_type_for_mode (to_type, pointer_mode, false);
585 }
586
587 \f
588 /* Return true if we will want to say something if a goto statement
589 crosses DECL. */
590
591 static bool
592 decl_jump_unsafe (tree decl)
593 {
594 if (error_operand_p (decl))
595 return false;
596
597 /* Always warn about crossing variably modified types. */
598 if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == TYPE_DECL)
599 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
600 return true;
601
602 /* Otherwise, only warn if -Wgoto-misses-init and this is an
603 initialized automatic decl. */
604 if (warn_jump_misses_init
605 && TREE_CODE (decl) == VAR_DECL
606 && !TREE_STATIC (decl)
607 && DECL_INITIAL (decl) != NULL_TREE)
608 return true;
609
610 return false;
611 }
612 \f
613
614 void
615 c_print_identifier (FILE *file, tree node, int indent)
616 {
617 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
618 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
619 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
620 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
621 {
622 tree rid = ridpointers[C_RID_CODE (node)];
623 indent_to (file, indent + 4);
624 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
625 (void *) rid, IDENTIFIER_POINTER (rid));
626 }
627 }
628
629 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
630 which may be any of several kinds of DECL or TYPE or error_mark_node,
631 in the scope SCOPE. */
632 static void
633 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
634 bool nested, location_t locus)
635 {
636 struct c_binding *b, **here;
637
638 if (binding_freelist)
639 {
640 b = binding_freelist;
641 binding_freelist = b->prev;
642 }
643 else
644 b = ggc_alloc<c_binding> ();
645
646 b->shadowed = 0;
647 b->decl = decl;
648 b->id = name;
649 b->depth = scope->depth;
650 b->invisible = invisible;
651 b->nested = nested;
652 b->inner_comp = 0;
653 b->in_struct = 0;
654 b->locus = locus;
655
656 b->u.type = NULL;
657
658 b->prev = scope->bindings;
659 scope->bindings = b;
660
661 if (decl_jump_unsafe (decl))
662 scope->has_jump_unsafe_decl = 1;
663
664 if (!name)
665 return;
666
667 switch (TREE_CODE (decl))
668 {
669 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
670 case ENUMERAL_TYPE:
671 case UNION_TYPE:
672 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
673 case VAR_DECL:
674 case FUNCTION_DECL:
675 case TYPE_DECL:
676 case CONST_DECL:
677 case PARM_DECL:
678 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
679
680 default:
681 gcc_unreachable ();
682 }
683
684 /* Locate the appropriate place in the chain of shadowed decls
685 to insert this binding. Normally, scope == current_scope and
686 this does nothing. */
687 while (*here && (*here)->depth > scope->depth)
688 here = &(*here)->shadowed;
689
690 b->shadowed = *here;
691 *here = b;
692 }
693
694 /* Clear the binding structure B, stick it on the binding_freelist,
695 and return the former value of b->prev. This is used by pop_scope
696 and get_parm_info to iterate destructively over all the bindings
697 from a given scope. */
698 static struct c_binding *
699 free_binding_and_advance (struct c_binding *b)
700 {
701 struct c_binding *prev = b->prev;
702
703 memset (b, 0, sizeof (struct c_binding));
704 b->prev = binding_freelist;
705 binding_freelist = b;
706
707 return prev;
708 }
709
710 /* Bind a label. Like bind, but skip fields which aren't used for
711 labels, and add the LABEL_VARS value. */
712 static void
713 bind_label (tree name, tree label, struct c_scope *scope,
714 struct c_label_vars *label_vars)
715 {
716 struct c_binding *b;
717
718 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
719 UNKNOWN_LOCATION);
720
721 scope->has_label_bindings = true;
722
723 b = scope->bindings;
724 gcc_assert (b->decl == label);
725 label_vars->shadowed = b->u.label;
726 b->u.label = label_vars;
727 }
728 \f
729 /* Hook called at end of compilation to assume 1 elt
730 for a file-scope tentative array defn that wasn't complete before. */
731
732 void
733 c_finish_incomplete_decl (tree decl)
734 {
735 if (TREE_CODE (decl) == VAR_DECL)
736 {
737 tree type = TREE_TYPE (decl);
738 if (type != error_mark_node
739 && TREE_CODE (type) == ARRAY_TYPE
740 && !DECL_EXTERNAL (decl)
741 && TYPE_DOMAIN (type) == 0)
742 {
743 warning_at (DECL_SOURCE_LOCATION (decl),
744 0, "array %q+D assumed to have one element", decl);
745
746 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
747
748 relayout_decl (decl);
749 }
750 }
751 }
752 \f
753 /* Record that inline function FUNC contains a reference (location
754 LOC) to static DECL (file-scope or function-local according to
755 TYPE). */
756
757 void
758 record_inline_static (location_t loc, tree func, tree decl,
759 enum c_inline_static_type type)
760 {
761 c_inline_static *csi = ggc_alloc<c_inline_static> ();
762 csi->location = loc;
763 csi->function = func;
764 csi->static_decl = decl;
765 csi->type = type;
766 csi->next = c_inline_statics;
767 c_inline_statics = csi;
768 }
769
770 /* Check for references to static declarations in inline functions at
771 the end of the translation unit and diagnose them if the functions
772 are still inline definitions. */
773
774 static void
775 check_inline_statics (void)
776 {
777 struct c_inline_static *csi;
778 for (csi = c_inline_statics; csi; csi = csi->next)
779 {
780 if (DECL_EXTERNAL (csi->function))
781 switch (csi->type)
782 {
783 case csi_internal:
784 pedwarn (csi->location, 0,
785 "%qD is static but used in inline function %qD "
786 "which is not static", csi->static_decl, csi->function);
787 break;
788 case csi_modifiable:
789 pedwarn (csi->location, 0,
790 "%q+D is static but declared in inline function %qD "
791 "which is not static", csi->static_decl, csi->function);
792 break;
793 default:
794 gcc_unreachable ();
795 }
796 }
797 c_inline_statics = NULL;
798 }
799 \f
800 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
801 for the current state, otherwise set it to uninitialized. */
802
803 static void
804 set_spot_bindings (struct c_spot_bindings *p, bool defining)
805 {
806 if (defining)
807 {
808 p->scope = current_scope;
809 p->bindings_in_scope = current_scope->bindings;
810 }
811 else
812 {
813 p->scope = NULL;
814 p->bindings_in_scope = NULL;
815 }
816 p->stmt_exprs = 0;
817 p->left_stmt_expr = false;
818 }
819
820 /* Update spot bindings P as we pop out of SCOPE. Return true if we
821 should push decls for a label. */
822
823 static bool
824 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
825 {
826 if (p->scope != scope)
827 {
828 /* This label or goto is defined in some other scope, or it is a
829 label which is not yet defined. There is nothing to
830 update. */
831 return false;
832 }
833
834 /* Adjust the spot bindings to refer to the bindings already defined
835 in the enclosing scope. */
836 p->scope = scope->outer;
837 p->bindings_in_scope = p->scope->bindings;
838
839 return true;
840 }
841 \f
842 /* The Objective-C front-end often needs to determine the current scope. */
843
844 void *
845 objc_get_current_scope (void)
846 {
847 return current_scope;
848 }
849
850 /* The following function is used only by Objective-C. It needs to live here
851 because it accesses the innards of c_scope. */
852
853 void
854 objc_mark_locals_volatile (void *enclosing_blk)
855 {
856 struct c_scope *scope;
857 struct c_binding *b;
858
859 for (scope = current_scope;
860 scope && scope != enclosing_blk;
861 scope = scope->outer)
862 {
863 for (b = scope->bindings; b; b = b->prev)
864 objc_volatilize_decl (b->decl);
865
866 /* Do not climb up past the current function. */
867 if (scope->function_body)
868 break;
869 }
870 }
871
872 /* Return true if we are in the global binding level. */
873
874 bool
875 global_bindings_p (void)
876 {
877 return current_scope == file_scope;
878 }
879
880 void
881 keep_next_level (void)
882 {
883 keep_next_level_flag = true;
884 }
885
886 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
887
888 void
889 set_float_const_decimal64 (void)
890 {
891 current_scope->float_const_decimal64 = true;
892 }
893
894 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
895
896 void
897 clear_float_const_decimal64 (void)
898 {
899 current_scope->float_const_decimal64 = false;
900 }
901
902 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
903
904 bool
905 float_const_decimal64_p (void)
906 {
907 return current_scope->float_const_decimal64;
908 }
909
910 /* Identify this scope as currently being filled with parameters. */
911
912 void
913 declare_parm_level (void)
914 {
915 current_scope->parm_flag = true;
916 }
917
918 void
919 push_scope (void)
920 {
921 if (next_is_function_body)
922 {
923 /* This is the transition from the parameters to the top level
924 of the function body. These are the same scope
925 (C99 6.2.1p4,6) so we do not push another scope structure.
926 next_is_function_body is set only by store_parm_decls, which
927 in turn is called when and only when we are about to
928 encounter the opening curly brace for the function body.
929
930 The outermost block of a function always gets a BLOCK node,
931 because the debugging output routines expect that each
932 function has at least one BLOCK. */
933 current_scope->parm_flag = false;
934 current_scope->function_body = true;
935 current_scope->keep = true;
936 current_scope->outer_function = current_function_scope;
937 current_function_scope = current_scope;
938
939 keep_next_level_flag = false;
940 next_is_function_body = false;
941
942 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
943 if (current_scope->outer)
944 current_scope->float_const_decimal64
945 = current_scope->outer->float_const_decimal64;
946 else
947 current_scope->float_const_decimal64 = false;
948 }
949 else
950 {
951 struct c_scope *scope;
952 if (scope_freelist)
953 {
954 scope = scope_freelist;
955 scope_freelist = scope->outer;
956 }
957 else
958 scope = ggc_cleared_alloc<c_scope> ();
959
960 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
961 if (current_scope)
962 scope->float_const_decimal64 = current_scope->float_const_decimal64;
963 else
964 scope->float_const_decimal64 = false;
965
966 scope->keep = keep_next_level_flag;
967 scope->outer = current_scope;
968 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
969
970 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
971 possible. */
972 if (current_scope && scope->depth == 0)
973 {
974 scope->depth--;
975 sorry ("GCC supports only %u nested scopes", scope->depth);
976 }
977
978 current_scope = scope;
979 keep_next_level_flag = false;
980 }
981 }
982
983 /* This is called when we are leaving SCOPE. For each label defined
984 in SCOPE, add any appropriate decls to its decls_in_scope fields.
985 These are the decls whose initialization will be skipped by a goto
986 later in the function. */
987
988 static void
989 update_label_decls (struct c_scope *scope)
990 {
991 struct c_scope *s;
992
993 s = scope;
994 while (s != NULL)
995 {
996 if (s->has_label_bindings)
997 {
998 struct c_binding *b;
999
1000 for (b = s->bindings; b != NULL; b = b->prev)
1001 {
1002 struct c_label_vars *label_vars;
1003 struct c_binding *b1;
1004 bool hjud;
1005 unsigned int ix;
1006 struct c_goto_bindings *g;
1007
1008 if (TREE_CODE (b->decl) != LABEL_DECL)
1009 continue;
1010 label_vars = b->u.label;
1011
1012 b1 = label_vars->label_bindings.bindings_in_scope;
1013 if (label_vars->label_bindings.scope == NULL)
1014 hjud = false;
1015 else
1016 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1017 if (update_spot_bindings (scope, &label_vars->label_bindings))
1018 {
1019 /* This label is defined in this scope. */
1020 if (hjud)
1021 {
1022 for (; b1 != NULL; b1 = b1->prev)
1023 {
1024 /* A goto from later in the function to this
1025 label will never see the initialization
1026 of B1, if any. Save it to issue a
1027 warning if needed. */
1028 if (decl_jump_unsafe (b1->decl))
1029 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1030 }
1031 }
1032 }
1033
1034 /* Update the bindings of any goto statements associated
1035 with this label. */
1036 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1037 update_spot_bindings (scope, &g->goto_bindings);
1038 }
1039 }
1040
1041 /* Don't search beyond the current function. */
1042 if (s == current_function_scope)
1043 break;
1044
1045 s = s->outer;
1046 }
1047 }
1048
1049 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1050
1051 static void
1052 set_type_context (tree type, tree context)
1053 {
1054 for (type = TYPE_MAIN_VARIANT (type); type;
1055 type = TYPE_NEXT_VARIANT (type))
1056 TYPE_CONTEXT (type) = context;
1057 }
1058
1059 /* Exit a scope. Restore the state of the identifier-decl mappings
1060 that were in effect when this scope was entered. Return a BLOCK
1061 node containing all the DECLs in this scope that are of interest
1062 to debug info generation. */
1063
1064 tree
1065 pop_scope (void)
1066 {
1067 struct c_scope *scope = current_scope;
1068 tree block, context, p;
1069 struct c_binding *b;
1070
1071 bool functionbody = scope->function_body;
1072 bool keep = functionbody || scope->keep || scope->bindings;
1073
1074 update_label_decls (scope);
1075
1076 /* If appropriate, create a BLOCK to record the decls for the life
1077 of this function. */
1078 block = 0;
1079 if (keep)
1080 {
1081 block = make_node (BLOCK);
1082 BLOCK_SUBBLOCKS (block) = scope->blocks;
1083 TREE_USED (block) = 1;
1084
1085 /* In each subblock, record that this is its superior. */
1086 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1087 BLOCK_SUPERCONTEXT (p) = block;
1088
1089 BLOCK_VARS (block) = 0;
1090 }
1091
1092 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1093 scope must be set so that they point to the appropriate
1094 construct, i.e. either to the current FUNCTION_DECL node, or
1095 else to the BLOCK node we just constructed.
1096
1097 Note that for tagged types whose scope is just the formal
1098 parameter list for some function type specification, we can't
1099 properly set their TYPE_CONTEXTs here, because we don't have a
1100 pointer to the appropriate FUNCTION_TYPE node readily available
1101 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1102 type nodes get set in `grokdeclarator' as soon as we have created
1103 the FUNCTION_TYPE node which will represent the "scope" for these
1104 "parameter list local" tagged types. */
1105 if (scope->function_body)
1106 context = current_function_decl;
1107 else if (scope == file_scope)
1108 {
1109 tree file_decl = build_translation_unit_decl (NULL_TREE);
1110 context = file_decl;
1111 }
1112 else
1113 context = block;
1114
1115 /* Clear all bindings in this scope. */
1116 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1117 {
1118 p = b->decl;
1119 switch (TREE_CODE (p))
1120 {
1121 case LABEL_DECL:
1122 /* Warnings for unused labels, errors for undefined labels. */
1123 if (TREE_USED (p) && !DECL_INITIAL (p))
1124 {
1125 error ("label %q+D used but not defined", p);
1126 DECL_INITIAL (p) = error_mark_node;
1127 }
1128 else
1129 warn_for_unused_label (p);
1130
1131 /* Labels go in BLOCK_VARS. */
1132 DECL_CHAIN (p) = BLOCK_VARS (block);
1133 BLOCK_VARS (block) = p;
1134 gcc_assert (I_LABEL_BINDING (b->id) == b);
1135 I_LABEL_BINDING (b->id) = b->shadowed;
1136
1137 /* Also pop back to the shadowed label_vars. */
1138 release_tree_vector (b->u.label->decls_in_scope);
1139 b->u.label = b->u.label->shadowed;
1140 break;
1141
1142 case ENUMERAL_TYPE:
1143 case UNION_TYPE:
1144 case RECORD_TYPE:
1145 set_type_context (p, context);
1146
1147 /* Types may not have tag-names, in which case the type
1148 appears in the bindings list with b->id NULL. */
1149 if (b->id)
1150 {
1151 gcc_assert (I_TAG_BINDING (b->id) == b);
1152 I_TAG_BINDING (b->id) = b->shadowed;
1153 }
1154 break;
1155
1156 case FUNCTION_DECL:
1157 /* Propagate TREE_ADDRESSABLE from nested functions to their
1158 containing functions. */
1159 if (!TREE_ASM_WRITTEN (p)
1160 && DECL_INITIAL (p) != 0
1161 && TREE_ADDRESSABLE (p)
1162 && DECL_ABSTRACT_ORIGIN (p) != 0
1163 && DECL_ABSTRACT_ORIGIN (p) != p)
1164 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1165 if (!DECL_EXTERNAL (p)
1166 && !DECL_INITIAL (p)
1167 && scope != file_scope
1168 && scope != external_scope)
1169 {
1170 error ("nested function %q+D declared but never defined", p);
1171 undef_nested_function = true;
1172 }
1173 else if (DECL_DECLARED_INLINE_P (p)
1174 && TREE_PUBLIC (p)
1175 && !DECL_INITIAL (p))
1176 {
1177 /* C99 6.7.4p6: "a function with external linkage... declared
1178 with an inline function specifier ... shall also be defined
1179 in the same translation unit." */
1180 if (!flag_gnu89_inline)
1181 pedwarn (input_location, 0,
1182 "inline function %q+D declared but never defined", p);
1183 DECL_EXTERNAL (p) = 1;
1184 }
1185
1186 goto common_symbol;
1187
1188 case VAR_DECL:
1189 /* Warnings for unused variables. */
1190 if ((!TREE_USED (p) || !DECL_READ_P (p))
1191 && !TREE_NO_WARNING (p)
1192 && !DECL_IN_SYSTEM_HEADER (p)
1193 && DECL_NAME (p)
1194 && !DECL_ARTIFICIAL (p)
1195 && scope != file_scope
1196 && scope != external_scope)
1197 {
1198 if (!TREE_USED (p))
1199 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1200 else if (DECL_CONTEXT (p) == current_function_decl)
1201 warning_at (DECL_SOURCE_LOCATION (p),
1202 OPT_Wunused_but_set_variable,
1203 "variable %qD set but not used", p);
1204 }
1205
1206 if (b->inner_comp)
1207 {
1208 error ("type of array %q+D completed incompatibly with"
1209 " implicit initialization", p);
1210 }
1211
1212 /* Fall through. */
1213 case TYPE_DECL:
1214 case CONST_DECL:
1215 common_symbol:
1216 /* All of these go in BLOCK_VARS, but only if this is the
1217 binding in the home scope. */
1218 if (!b->nested)
1219 {
1220 DECL_CHAIN (p) = BLOCK_VARS (block);
1221 BLOCK_VARS (block) = p;
1222 }
1223 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1224 {
1225 /* For block local externs add a special
1226 DECL_EXTERNAL decl for debug info generation. */
1227 tree extp = copy_node (p);
1228
1229 DECL_EXTERNAL (extp) = 1;
1230 TREE_STATIC (extp) = 0;
1231 TREE_PUBLIC (extp) = 1;
1232 DECL_INITIAL (extp) = NULL_TREE;
1233 DECL_LANG_SPECIFIC (extp) = NULL;
1234 DECL_CONTEXT (extp) = current_function_decl;
1235 if (TREE_CODE (p) == FUNCTION_DECL)
1236 {
1237 DECL_RESULT (extp) = NULL_TREE;
1238 DECL_SAVED_TREE (extp) = NULL_TREE;
1239 DECL_STRUCT_FUNCTION (extp) = NULL;
1240 }
1241 if (b->locus != UNKNOWN_LOCATION)
1242 DECL_SOURCE_LOCATION (extp) = b->locus;
1243 DECL_CHAIN (extp) = BLOCK_VARS (block);
1244 BLOCK_VARS (block) = extp;
1245 }
1246 /* If this is the file scope set DECL_CONTEXT of each decl to
1247 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1248 work. */
1249 if (scope == file_scope)
1250 {
1251 DECL_CONTEXT (p) = context;
1252 if (TREE_CODE (p) == TYPE_DECL
1253 && TREE_TYPE (p) != error_mark_node)
1254 set_type_context (TREE_TYPE (p), context);
1255 }
1256
1257 /* Fall through. */
1258 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1259 already been put there by store_parm_decls. Unused-
1260 parameter warnings are handled by function.c.
1261 error_mark_node obviously does not go in BLOCK_VARS and
1262 does not get unused-variable warnings. */
1263 case PARM_DECL:
1264 case ERROR_MARK:
1265 /* It is possible for a decl not to have a name. We get
1266 here with b->id NULL in this case. */
1267 if (b->id)
1268 {
1269 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1270 I_SYMBOL_BINDING (b->id) = b->shadowed;
1271 if (b->shadowed && b->shadowed->u.type)
1272 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1273 }
1274 break;
1275
1276 default:
1277 gcc_unreachable ();
1278 }
1279 }
1280
1281
1282 /* Dispose of the block that we just made inside some higher level. */
1283 if ((scope->function_body || scope == file_scope) && context)
1284 {
1285 DECL_INITIAL (context) = block;
1286 BLOCK_SUPERCONTEXT (block) = context;
1287 }
1288 else if (scope->outer)
1289 {
1290 if (block)
1291 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1292 /* If we did not make a block for the scope just exited, any
1293 blocks made for inner scopes must be carried forward so they
1294 will later become subblocks of something else. */
1295 else if (scope->blocks)
1296 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1297 }
1298
1299 /* Pop the current scope, and free the structure for reuse. */
1300 current_scope = scope->outer;
1301 if (scope->function_body)
1302 current_function_scope = scope->outer_function;
1303
1304 memset (scope, 0, sizeof (struct c_scope));
1305 scope->outer = scope_freelist;
1306 scope_freelist = scope;
1307
1308 return block;
1309 }
1310
1311 void
1312 push_file_scope (void)
1313 {
1314 tree decl;
1315
1316 if (file_scope)
1317 return;
1318
1319 push_scope ();
1320 file_scope = current_scope;
1321
1322 start_fname_decls ();
1323
1324 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1325 bind (DECL_NAME (decl), decl, file_scope,
1326 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1327 }
1328
1329 void
1330 pop_file_scope (void)
1331 {
1332 /* In case there were missing closebraces, get us back to the global
1333 binding level. */
1334 while (current_scope != file_scope)
1335 pop_scope ();
1336
1337 /* __FUNCTION__ is defined at file scope (""). This
1338 call may not be necessary as my tests indicate it
1339 still works without it. */
1340 finish_fname_decls ();
1341
1342 check_inline_statics ();
1343
1344 /* This is the point to write out a PCH if we're doing that.
1345 In that case we do not want to do anything else. */
1346 if (pch_file)
1347 {
1348 c_common_write_pch ();
1349 return;
1350 }
1351
1352 /* Pop off the file scope and close this translation unit. */
1353 pop_scope ();
1354 file_scope = 0;
1355
1356 maybe_apply_pending_pragma_weaks ();
1357 }
1358 \f
1359 /* Adjust the bindings for the start of a statement expression. */
1360
1361 void
1362 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1363 {
1364 struct c_scope *scope;
1365
1366 for (scope = current_scope; scope != NULL; scope = scope->outer)
1367 {
1368 struct c_binding *b;
1369
1370 if (!scope->has_label_bindings)
1371 continue;
1372
1373 for (b = scope->bindings; b != NULL; b = b->prev)
1374 {
1375 struct c_label_vars *label_vars;
1376 unsigned int ix;
1377 struct c_goto_bindings *g;
1378
1379 if (TREE_CODE (b->decl) != LABEL_DECL)
1380 continue;
1381 label_vars = b->u.label;
1382 ++label_vars->label_bindings.stmt_exprs;
1383 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1384 ++g->goto_bindings.stmt_exprs;
1385 }
1386 }
1387
1388 if (switch_bindings != NULL)
1389 ++switch_bindings->stmt_exprs;
1390 }
1391
1392 /* Adjust the bindings for the end of a statement expression. */
1393
1394 void
1395 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1396 {
1397 struct c_scope *scope;
1398
1399 for (scope = current_scope; scope != NULL; scope = scope->outer)
1400 {
1401 struct c_binding *b;
1402
1403 if (!scope->has_label_bindings)
1404 continue;
1405
1406 for (b = scope->bindings; b != NULL; b = b->prev)
1407 {
1408 struct c_label_vars *label_vars;
1409 unsigned int ix;
1410 struct c_goto_bindings *g;
1411
1412 if (TREE_CODE (b->decl) != LABEL_DECL)
1413 continue;
1414 label_vars = b->u.label;
1415 --label_vars->label_bindings.stmt_exprs;
1416 if (label_vars->label_bindings.stmt_exprs < 0)
1417 {
1418 label_vars->label_bindings.left_stmt_expr = true;
1419 label_vars->label_bindings.stmt_exprs = 0;
1420 }
1421 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1422 {
1423 --g->goto_bindings.stmt_exprs;
1424 if (g->goto_bindings.stmt_exprs < 0)
1425 {
1426 g->goto_bindings.left_stmt_expr = true;
1427 g->goto_bindings.stmt_exprs = 0;
1428 }
1429 }
1430 }
1431 }
1432
1433 if (switch_bindings != NULL)
1434 {
1435 --switch_bindings->stmt_exprs;
1436 gcc_assert (switch_bindings->stmt_exprs >= 0);
1437 }
1438 }
1439 \f
1440 /* Push a definition or a declaration of struct, union or enum tag "name".
1441 "type" should be the type node.
1442 We assume that the tag "name" is not already defined, and has a location
1443 of LOC.
1444
1445 Note that the definition may really be just a forward reference.
1446 In that case, the TYPE_SIZE will be zero. */
1447
1448 static void
1449 pushtag (location_t loc, tree name, tree type)
1450 {
1451 /* Record the identifier as the type's name if it has none. */
1452 if (name && !TYPE_NAME (type))
1453 TYPE_NAME (type) = name;
1454 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1455
1456 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1457 tagged type we just added to the current scope. This fake
1458 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1459 to output a representation of a tagged type, and it also gives
1460 us a convenient place to record the "scope start" address for the
1461 tagged type. */
1462
1463 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1464 TYPE_DECL, NULL_TREE, type));
1465
1466 /* An approximation for now, so we can tell this is a function-scope tag.
1467 This will be updated in pop_scope. */
1468 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1469
1470 if (warn_cxx_compat && name != NULL_TREE)
1471 {
1472 struct c_binding *b = I_SYMBOL_BINDING (name);
1473
1474 if (b != NULL
1475 && b->decl != NULL_TREE
1476 && TREE_CODE (b->decl) == TYPE_DECL
1477 && (B_IN_CURRENT_SCOPE (b)
1478 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1479 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1480 != TYPE_MAIN_VARIANT (type)))
1481 {
1482 warning_at (loc, OPT_Wc___compat,
1483 ("using %qD as both a typedef and a tag is "
1484 "invalid in C++"),
1485 b->decl);
1486 if (b->locus != UNKNOWN_LOCATION)
1487 inform (b->locus, "originally defined here");
1488 }
1489 }
1490 }
1491 \f
1492 /* Subroutine of compare_decls. Allow harmless mismatches in return
1493 and argument types provided that the type modes match. This function
1494 return a unified type given a suitable match, and 0 otherwise. */
1495
1496 static tree
1497 match_builtin_function_types (tree newtype, tree oldtype)
1498 {
1499 tree newrettype, oldrettype;
1500 tree newargs, oldargs;
1501 tree trytype, tryargs;
1502
1503 /* Accept the return type of the new declaration if same modes. */
1504 oldrettype = TREE_TYPE (oldtype);
1505 newrettype = TREE_TYPE (newtype);
1506
1507 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1508 return 0;
1509
1510 oldargs = TYPE_ARG_TYPES (oldtype);
1511 newargs = TYPE_ARG_TYPES (newtype);
1512 tryargs = newargs;
1513
1514 while (oldargs || newargs)
1515 {
1516 if (!oldargs
1517 || !newargs
1518 || !TREE_VALUE (oldargs)
1519 || !TREE_VALUE (newargs)
1520 || TYPE_MODE (TREE_VALUE (oldargs))
1521 != TYPE_MODE (TREE_VALUE (newargs)))
1522 return 0;
1523
1524 oldargs = TREE_CHAIN (oldargs);
1525 newargs = TREE_CHAIN (newargs);
1526 }
1527
1528 trytype = build_function_type (newrettype, tryargs);
1529 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1530 }
1531
1532 /* Subroutine of diagnose_mismatched_decls. Check for function type
1533 mismatch involving an empty arglist vs a nonempty one and give clearer
1534 diagnostics. */
1535 static void
1536 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1537 tree newtype, tree oldtype)
1538 {
1539 tree t;
1540
1541 if (TREE_CODE (olddecl) != FUNCTION_DECL
1542 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1543 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == 0)
1544 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == 0)))
1545 return;
1546
1547 t = TYPE_ARG_TYPES (oldtype);
1548 if (t == 0)
1549 t = TYPE_ARG_TYPES (newtype);
1550 for (; t; t = TREE_CHAIN (t))
1551 {
1552 tree type = TREE_VALUE (t);
1553
1554 if (TREE_CHAIN (t) == 0
1555 && TYPE_MAIN_VARIANT (type) != void_type_node)
1556 {
1557 inform (input_location, "a parameter list with an ellipsis can%'t match "
1558 "an empty parameter name list declaration");
1559 break;
1560 }
1561
1562 if (c_type_promotes_to (type) != type)
1563 {
1564 inform (input_location, "an argument type that has a default promotion can%'t match "
1565 "an empty parameter name list declaration");
1566 break;
1567 }
1568 }
1569 }
1570
1571 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1572 old-style function definition, NEWDECL is a prototype declaration.
1573 Diagnose inconsistencies in the argument list. Returns TRUE if
1574 the prototype is compatible, FALSE if not. */
1575 static bool
1576 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1577 {
1578 tree newargs, oldargs;
1579 int i;
1580
1581 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1582
1583 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1584 newargs = TYPE_ARG_TYPES (newtype);
1585 i = 1;
1586
1587 for (;;)
1588 {
1589 tree oldargtype = TREE_VALUE (oldargs);
1590 tree newargtype = TREE_VALUE (newargs);
1591
1592 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1593 return false;
1594
1595 oldargtype = (TYPE_ATOMIC (oldargtype)
1596 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1597 TYPE_QUAL_ATOMIC)
1598 : TYPE_MAIN_VARIANT (oldargtype));
1599 newargtype = (TYPE_ATOMIC (newargtype)
1600 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1601 TYPE_QUAL_ATOMIC)
1602 : TYPE_MAIN_VARIANT (newargtype));
1603
1604 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1605 break;
1606
1607 /* Reaching the end of just one list means the two decls don't
1608 agree on the number of arguments. */
1609 if (END_OF_ARGLIST (oldargtype))
1610 {
1611 error ("prototype for %q+D declares more arguments "
1612 "than previous old-style definition", newdecl);
1613 return false;
1614 }
1615 else if (END_OF_ARGLIST (newargtype))
1616 {
1617 error ("prototype for %q+D declares fewer arguments "
1618 "than previous old-style definition", newdecl);
1619 return false;
1620 }
1621
1622 /* Type for passing arg must be consistent with that declared
1623 for the arg. */
1624 else if (!comptypes (oldargtype, newargtype))
1625 {
1626 error ("prototype for %q+D declares argument %d"
1627 " with incompatible type",
1628 newdecl, i);
1629 return false;
1630 }
1631
1632 oldargs = TREE_CHAIN (oldargs);
1633 newargs = TREE_CHAIN (newargs);
1634 i++;
1635 }
1636
1637 /* If we get here, no errors were found, but do issue a warning
1638 for this poor-style construct. */
1639 warning (0, "prototype for %q+D follows non-prototype definition",
1640 newdecl);
1641 return true;
1642 #undef END_OF_ARGLIST
1643 }
1644
1645 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1646 first in a pair of mismatched declarations, using the diagnostic
1647 function DIAG. */
1648 static void
1649 locate_old_decl (tree decl)
1650 {
1651 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)
1652 && !C_DECL_DECLARED_BUILTIN (decl))
1653 ;
1654 else if (DECL_INITIAL (decl))
1655 inform (input_location, "previous definition of %q+D was here", decl);
1656 else if (C_DECL_IMPLICIT (decl))
1657 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1658 else
1659 inform (input_location, "previous declaration of %q+D was here", decl);
1660 }
1661
1662 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1663 Returns true if the caller should proceed to merge the two, false
1664 if OLDDECL should simply be discarded. As a side effect, issues
1665 all necessary diagnostics for invalid or poor-style combinations.
1666 If it returns true, writes the types of NEWDECL and OLDDECL to
1667 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1668 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1669
1670 static bool
1671 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1672 tree *newtypep, tree *oldtypep)
1673 {
1674 tree newtype, oldtype;
1675 bool pedwarned = false;
1676 bool warned = false;
1677 bool retval = true;
1678
1679 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1680 && DECL_EXTERNAL (DECL))
1681
1682 /* If we have error_mark_node for either decl or type, just discard
1683 the previous decl - we're in an error cascade already. */
1684 if (olddecl == error_mark_node || newdecl == error_mark_node)
1685 return false;
1686 *oldtypep = oldtype = TREE_TYPE (olddecl);
1687 *newtypep = newtype = TREE_TYPE (newdecl);
1688 if (oldtype == error_mark_node || newtype == error_mark_node)
1689 return false;
1690
1691 /* Two different categories of symbol altogether. This is an error
1692 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1693 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1694 {
1695 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1696 && DECL_BUILT_IN (olddecl)
1697 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1698 {
1699 error ("%q+D redeclared as different kind of symbol", newdecl);
1700 locate_old_decl (olddecl);
1701 }
1702 else if (TREE_PUBLIC (newdecl))
1703 warning (0, "built-in function %q+D declared as non-function",
1704 newdecl);
1705 else
1706 warning (OPT_Wshadow, "declaration of %q+D shadows "
1707 "a built-in function", newdecl);
1708 return false;
1709 }
1710
1711 /* Enumerators have no linkage, so may only be declared once in a
1712 given scope. */
1713 if (TREE_CODE (olddecl) == CONST_DECL)
1714 {
1715 error ("redeclaration of enumerator %q+D", newdecl);
1716 locate_old_decl (olddecl);
1717 return false;
1718 }
1719
1720 if (!comptypes (oldtype, newtype))
1721 {
1722 if (TREE_CODE (olddecl) == FUNCTION_DECL
1723 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1724 {
1725 /* Accept harmless mismatch in function types.
1726 This is for the ffs and fprintf builtins. */
1727 tree trytype = match_builtin_function_types (newtype, oldtype);
1728
1729 if (trytype && comptypes (newtype, trytype))
1730 *oldtypep = oldtype = trytype;
1731 else
1732 {
1733 /* If types don't match for a built-in, throw away the
1734 built-in. No point in calling locate_old_decl here, it
1735 won't print anything. */
1736 warning (0, "conflicting types for built-in function %q+D",
1737 newdecl);
1738 return false;
1739 }
1740 }
1741 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1742 && DECL_IS_BUILTIN (olddecl))
1743 {
1744 /* A conflicting function declaration for a predeclared
1745 function that isn't actually built in. Objective C uses
1746 these. The new declaration silently overrides everything
1747 but the volatility (i.e. noreturn) indication. See also
1748 below. FIXME: Make Objective C use normal builtins. */
1749 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1750 return false;
1751 }
1752 /* Permit void foo (...) to match int foo (...) if the latter is
1753 the definition and implicit int was used. See
1754 c-torture/compile/920625-2.c. */
1755 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1756 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1757 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1758 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1759 {
1760 pedwarned = pedwarn (input_location, 0,
1761 "conflicting types for %q+D", newdecl);
1762 /* Make sure we keep void as the return type. */
1763 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1764 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1765 }
1766 /* Permit void foo (...) to match an earlier call to foo (...) with
1767 no declared type (thus, implicitly int). */
1768 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1769 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1770 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1771 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1772 {
1773 pedwarned = pedwarn (input_location, 0,
1774 "conflicting types for %q+D", newdecl);
1775 /* Make sure we keep void as the return type. */
1776 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1777 }
1778 else
1779 {
1780 int new_quals = TYPE_QUALS (newtype);
1781 int old_quals = TYPE_QUALS (oldtype);
1782
1783 if (new_quals != old_quals)
1784 {
1785 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1786 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1787 if (new_addr != old_addr)
1788 {
1789 if (ADDR_SPACE_GENERIC_P (new_addr))
1790 error ("conflicting named address spaces (generic vs %s) "
1791 "for %q+D",
1792 c_addr_space_name (old_addr), newdecl);
1793 else if (ADDR_SPACE_GENERIC_P (old_addr))
1794 error ("conflicting named address spaces (%s vs generic) "
1795 "for %q+D",
1796 c_addr_space_name (new_addr), newdecl);
1797 else
1798 error ("conflicting named address spaces (%s vs %s) "
1799 "for %q+D",
1800 c_addr_space_name (new_addr),
1801 c_addr_space_name (old_addr),
1802 newdecl);
1803 }
1804
1805 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1806 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1807 error ("conflicting type qualifiers for %q+D", newdecl);
1808 }
1809 else
1810 error ("conflicting types for %q+D", newdecl);
1811 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1812 locate_old_decl (olddecl);
1813 return false;
1814 }
1815 }
1816
1817 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1818 but silently ignore the redeclaration if either is in a system
1819 header. (Conflicting redeclarations were handled above.) This
1820 is allowed for C11 if the types are the same, not just
1821 compatible. */
1822 if (TREE_CODE (newdecl) == TYPE_DECL)
1823 {
1824 bool types_different = false;
1825 int comptypes_result;
1826
1827 comptypes_result
1828 = comptypes_check_different_types (oldtype, newtype, &types_different);
1829
1830 if (comptypes_result != 1 || types_different)
1831 {
1832 error ("redefinition of typedef %q+D with different type", newdecl);
1833 locate_old_decl (olddecl);
1834 return false;
1835 }
1836
1837 if (DECL_IN_SYSTEM_HEADER (newdecl)
1838 || DECL_IN_SYSTEM_HEADER (olddecl)
1839 || TREE_NO_WARNING (newdecl)
1840 || TREE_NO_WARNING (olddecl))
1841 return true; /* Allow OLDDECL to continue in use. */
1842
1843 if (variably_modified_type_p (newtype, NULL))
1844 {
1845 error ("redefinition of typedef %q+D with variably modified type",
1846 newdecl);
1847 locate_old_decl (olddecl);
1848 }
1849 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
1850 "redefinition of typedef %q+D", newdecl))
1851 locate_old_decl (olddecl);
1852
1853 return true;
1854 }
1855
1856 /* Function declarations can either be 'static' or 'extern' (no
1857 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1858 can never conflict with each other on account of linkage
1859 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1860 gnu89 mode permits two definitions if one is 'extern inline' and
1861 one is not. The non- extern-inline definition supersedes the
1862 extern-inline definition. */
1863
1864 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1865 {
1866 /* If you declare a built-in function name as static, or
1867 define the built-in with an old-style definition (so we
1868 can't validate the argument list) the built-in definition is
1869 overridden, but optionally warn this was a bad choice of name. */
1870 if (DECL_BUILT_IN (olddecl)
1871 && !C_DECL_DECLARED_BUILTIN (olddecl)
1872 && (!TREE_PUBLIC (newdecl)
1873 || (DECL_INITIAL (newdecl)
1874 && !prototype_p (TREE_TYPE (newdecl)))))
1875 {
1876 warning (OPT_Wshadow, "declaration of %q+D shadows "
1877 "a built-in function", newdecl);
1878 /* Discard the old built-in function. */
1879 return false;
1880 }
1881
1882 if (DECL_INITIAL (newdecl))
1883 {
1884 if (DECL_INITIAL (olddecl))
1885 {
1886 /* If both decls are in the same TU and the new declaration
1887 isn't overriding an extern inline reject the new decl.
1888 In c99, no overriding is allowed in the same translation
1889 unit. */
1890 if ((!DECL_EXTERN_INLINE (olddecl)
1891 || DECL_EXTERN_INLINE (newdecl)
1892 || (!flag_gnu89_inline
1893 && (!DECL_DECLARED_INLINE_P (olddecl)
1894 || !lookup_attribute ("gnu_inline",
1895 DECL_ATTRIBUTES (olddecl)))
1896 && (!DECL_DECLARED_INLINE_P (newdecl)
1897 || !lookup_attribute ("gnu_inline",
1898 DECL_ATTRIBUTES (newdecl))))
1899 )
1900 && same_translation_unit_p (newdecl, olddecl))
1901 {
1902 error ("redefinition of %q+D", newdecl);
1903 locate_old_decl (olddecl);
1904 return false;
1905 }
1906 }
1907 }
1908 /* If we have a prototype after an old-style function definition,
1909 the argument types must be checked specially. */
1910 else if (DECL_INITIAL (olddecl)
1911 && !prototype_p (oldtype) && prototype_p (newtype)
1912 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1913 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1914 {
1915 locate_old_decl (olddecl);
1916 return false;
1917 }
1918 /* A non-static declaration (even an "extern") followed by a
1919 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1920 The same is true for a static forward declaration at block
1921 scope followed by a non-static declaration/definition at file
1922 scope. Static followed by non-static at the same scope is
1923 not undefined behavior, and is the most convenient way to get
1924 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1925 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1926 we do diagnose it if -Wtraditional. */
1927 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1928 {
1929 /* Two exceptions to the rule. If olddecl is an extern
1930 inline, or a predeclared function that isn't actually
1931 built in, newdecl silently overrides olddecl. The latter
1932 occur only in Objective C; see also above. (FIXME: Make
1933 Objective C use normal builtins.) */
1934 if (!DECL_IS_BUILTIN (olddecl)
1935 && !DECL_EXTERN_INLINE (olddecl))
1936 {
1937 error ("static declaration of %q+D follows "
1938 "non-static declaration", newdecl);
1939 locate_old_decl (olddecl);
1940 }
1941 return false;
1942 }
1943 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1944 {
1945 if (DECL_CONTEXT (olddecl))
1946 {
1947 error ("non-static declaration of %q+D follows "
1948 "static declaration", newdecl);
1949 locate_old_decl (olddecl);
1950 return false;
1951 }
1952 else if (warn_traditional)
1953 {
1954 warned |= warning (OPT_Wtraditional,
1955 "non-static declaration of %q+D "
1956 "follows static declaration", newdecl);
1957 }
1958 }
1959
1960 /* Make sure gnu_inline attribute is either not present, or
1961 present on all inline decls. */
1962 if (DECL_DECLARED_INLINE_P (olddecl)
1963 && DECL_DECLARED_INLINE_P (newdecl))
1964 {
1965 bool newa = lookup_attribute ("gnu_inline",
1966 DECL_ATTRIBUTES (newdecl)) != NULL;
1967 bool olda = lookup_attribute ("gnu_inline",
1968 DECL_ATTRIBUTES (olddecl)) != NULL;
1969 if (newa != olda)
1970 {
1971 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
1972 newa ? newdecl : olddecl);
1973 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
1974 "but not here");
1975 }
1976 }
1977 }
1978 else if (TREE_CODE (newdecl) == VAR_DECL)
1979 {
1980 /* Only variables can be thread-local, and all declarations must
1981 agree on this property. */
1982 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1983 {
1984 /* Nothing to check. Since OLDDECL is marked threadprivate
1985 and NEWDECL does not have a thread-local attribute, we
1986 will merge the threadprivate attribute into NEWDECL. */
1987 ;
1988 }
1989 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1990 {
1991 if (DECL_THREAD_LOCAL_P (newdecl))
1992 error ("thread-local declaration of %q+D follows "
1993 "non-thread-local declaration", newdecl);
1994 else
1995 error ("non-thread-local declaration of %q+D follows "
1996 "thread-local declaration", newdecl);
1997
1998 locate_old_decl (olddecl);
1999 return false;
2000 }
2001
2002 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2003 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2004 {
2005 error ("redefinition of %q+D", newdecl);
2006 locate_old_decl (olddecl);
2007 return false;
2008 }
2009
2010 /* Objects declared at file scope: if the first declaration had
2011 external linkage (even if it was an external reference) the
2012 second must have external linkage as well, or the behavior is
2013 undefined. If the first declaration had internal linkage, then
2014 the second must too, or else be an external reference (in which
2015 case the composite declaration still has internal linkage).
2016 As for function declarations, we warn about the static-then-
2017 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2018 if (DECL_FILE_SCOPE_P (newdecl)
2019 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2020 {
2021 if (DECL_EXTERNAL (newdecl))
2022 {
2023 if (!DECL_FILE_SCOPE_P (olddecl))
2024 {
2025 error ("extern declaration of %q+D follows "
2026 "declaration with no linkage", newdecl);
2027 locate_old_decl (olddecl);
2028 return false;
2029 }
2030 else if (warn_traditional)
2031 {
2032 warned |= warning (OPT_Wtraditional,
2033 "non-static declaration of %q+D "
2034 "follows static declaration", newdecl);
2035 }
2036 }
2037 else
2038 {
2039 if (TREE_PUBLIC (newdecl))
2040 error ("non-static declaration of %q+D follows "
2041 "static declaration", newdecl);
2042 else
2043 error ("static declaration of %q+D follows "
2044 "non-static declaration", newdecl);
2045
2046 locate_old_decl (olddecl);
2047 return false;
2048 }
2049 }
2050 /* Two objects with the same name declared at the same block
2051 scope must both be external references (6.7p3). */
2052 else if (!DECL_FILE_SCOPE_P (newdecl))
2053 {
2054 if (DECL_EXTERNAL (newdecl))
2055 {
2056 /* Extern with initializer at block scope, which will
2057 already have received an error. */
2058 }
2059 else if (DECL_EXTERNAL (olddecl))
2060 {
2061 error ("declaration of %q+D with no linkage follows "
2062 "extern declaration", newdecl);
2063 locate_old_decl (olddecl);
2064 }
2065 else
2066 {
2067 error ("redeclaration of %q+D with no linkage", newdecl);
2068 locate_old_decl (olddecl);
2069 }
2070
2071 return false;
2072 }
2073
2074 /* C++ does not permit a decl to appear multiple times at file
2075 scope. */
2076 if (warn_cxx_compat
2077 && DECL_FILE_SCOPE_P (newdecl)
2078 && !DECL_EXTERNAL (newdecl)
2079 && !DECL_EXTERNAL (olddecl))
2080 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2081 OPT_Wc___compat,
2082 ("duplicate declaration of %qD is "
2083 "invalid in C++"),
2084 newdecl);
2085 }
2086
2087 /* warnings */
2088 /* All decls must agree on a visibility. */
2089 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2090 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2091 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2092 {
2093 warned |= warning (0, "redeclaration of %q+D with different visibility "
2094 "(old visibility preserved)", newdecl);
2095 }
2096
2097 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2098 {
2099 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2100 if (DECL_DECLARED_INLINE_P (newdecl)
2101 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2102 warned |= warning (OPT_Wattributes,
2103 "inline declaration of %qD follows "
2104 "declaration with attribute noinline", newdecl);
2105 else if (DECL_DECLARED_INLINE_P (olddecl)
2106 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2107 warned |= warning (OPT_Wattributes,
2108 "declaration of %q+D with attribute "
2109 "noinline follows inline declaration ", newdecl);
2110 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))
2111 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl)))
2112 warned |= warning (OPT_Wattributes,
2113 "declaration of %q+D with attribute "
2114 "%qs follows declaration with attribute %qs",
2115 newdecl, "noinline", "always_inline");
2116 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl))
2117 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2118 warned |= warning (OPT_Wattributes,
2119 "declaration of %q+D with attribute "
2120 "%qs follows declaration with attribute %qs",
2121 newdecl, "always_inline", "noinline");
2122 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl))
2123 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl)))
2124 warned |= warning (OPT_Wattributes,
2125 "declaration of %q+D with attribute %qs follows "
2126 "declaration with attribute %qs", newdecl, "cold",
2127 "hot");
2128 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl))
2129 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl)))
2130 warned |= warning (OPT_Wattributes,
2131 "declaration of %q+D with attribute %qs follows "
2132 "declaration with attribute %qs", newdecl, "hot",
2133 "cold");
2134 }
2135 else /* PARM_DECL, VAR_DECL */
2136 {
2137 /* Redeclaration of a parameter is a constraint violation (this is
2138 not explicitly stated, but follows from C99 6.7p3 [no more than
2139 one declaration of the same identifier with no linkage in the
2140 same scope, except type tags] and 6.2.2p6 [parameters have no
2141 linkage]). We must check for a forward parameter declaration,
2142 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2143 an extension, the mandatory diagnostic for which is handled by
2144 mark_forward_parm_decls. */
2145
2146 if (TREE_CODE (newdecl) == PARM_DECL
2147 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2148 {
2149 error ("redefinition of parameter %q+D", newdecl);
2150 locate_old_decl (olddecl);
2151 return false;
2152 }
2153 }
2154
2155 /* Optional warning for completely redundant decls. */
2156 if (!warned && !pedwarned
2157 && warn_redundant_decls
2158 /* Don't warn about a function declaration followed by a
2159 definition. */
2160 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2161 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2162 /* Don't warn about redundant redeclarations of builtins. */
2163 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2164 && !DECL_BUILT_IN (newdecl)
2165 && DECL_BUILT_IN (olddecl)
2166 && !C_DECL_DECLARED_BUILTIN (olddecl))
2167 /* Don't warn about an extern followed by a definition. */
2168 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2169 /* Don't warn about forward parameter decls. */
2170 && !(TREE_CODE (newdecl) == PARM_DECL
2171 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2172 /* Don't warn about a variable definition following a declaration. */
2173 && !(TREE_CODE (newdecl) == VAR_DECL
2174 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2175 {
2176 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2177 newdecl);
2178 }
2179
2180 /* Report location of previous decl/defn. */
2181 if (warned || pedwarned)
2182 locate_old_decl (olddecl);
2183
2184 #undef DECL_EXTERN_INLINE
2185
2186 return retval;
2187 }
2188
2189 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2190 consistent with OLDDECL, but carries new information. Merge the
2191 new information into OLDDECL. This function issues no
2192 diagnostics. */
2193
2194 static void
2195 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2196 {
2197 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2198 && DECL_INITIAL (newdecl) != 0);
2199 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2200 && prototype_p (TREE_TYPE (newdecl)));
2201 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2202 && prototype_p (TREE_TYPE (olddecl)));
2203
2204 /* For real parm decl following a forward decl, rechain the old decl
2205 in its new location and clear TREE_ASM_WRITTEN (it's not a
2206 forward decl anymore). */
2207 if (TREE_CODE (newdecl) == PARM_DECL
2208 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2209 {
2210 struct c_binding *b, **here;
2211
2212 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2213 if ((*here)->decl == olddecl)
2214 goto found;
2215 gcc_unreachable ();
2216
2217 found:
2218 b = *here;
2219 *here = b->prev;
2220 b->prev = current_scope->bindings;
2221 current_scope->bindings = b;
2222
2223 TREE_ASM_WRITTEN (olddecl) = 0;
2224 }
2225
2226 DECL_ATTRIBUTES (newdecl)
2227 = targetm.merge_decl_attributes (olddecl, newdecl);
2228
2229 /* Merge the data types specified in the two decls. */
2230 TREE_TYPE (newdecl)
2231 = TREE_TYPE (olddecl)
2232 = composite_type (newtype, oldtype);
2233
2234 /* Lay the type out, unless already done. */
2235 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2236 {
2237 if (TREE_TYPE (newdecl) != error_mark_node)
2238 layout_type (TREE_TYPE (newdecl));
2239 if (TREE_CODE (newdecl) != FUNCTION_DECL
2240 && TREE_CODE (newdecl) != TYPE_DECL
2241 && TREE_CODE (newdecl) != CONST_DECL)
2242 layout_decl (newdecl, 0);
2243 }
2244 else
2245 {
2246 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2247 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2248 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2249 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2250 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2251 {
2252 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2253 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2254 }
2255 }
2256
2257 /* Keep the old rtl since we can safely use it. */
2258 if (HAS_RTL_P (olddecl))
2259 COPY_DECL_RTL (olddecl, newdecl);
2260
2261 /* Merge the type qualifiers. */
2262 if (TREE_READONLY (newdecl))
2263 TREE_READONLY (olddecl) = 1;
2264
2265 if (TREE_THIS_VOLATILE (newdecl))
2266 TREE_THIS_VOLATILE (olddecl) = 1;
2267
2268 /* Merge deprecatedness. */
2269 if (TREE_DEPRECATED (newdecl))
2270 TREE_DEPRECATED (olddecl) = 1;
2271
2272 /* If a decl is in a system header and the other isn't, keep the one on the
2273 system header. Otherwise, keep source location of definition rather than
2274 declaration and of prototype rather than non-prototype unless that
2275 prototype is built-in. */
2276 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2277 && DECL_IN_SYSTEM_HEADER (olddecl)
2278 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2279 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2280 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2281 && DECL_IN_SYSTEM_HEADER (newdecl)
2282 && !DECL_IN_SYSTEM_HEADER (olddecl))
2283 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2284 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2285 || (old_is_prototype && !new_is_prototype
2286 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2287 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2288
2289 /* Merge the initialization information. */
2290 if (DECL_INITIAL (newdecl) == 0)
2291 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2292
2293 /* Merge the threadprivate attribute. */
2294 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
2295 {
2296 set_decl_tls_model (newdecl, DECL_TLS_MODEL (olddecl));
2297 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2298 }
2299
2300 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2301 {
2302 /* Merge the section attribute.
2303 We want to issue an error if the sections conflict but that
2304 must be done later in decl_attributes since we are called
2305 before attributes are assigned. */
2306 if ((DECL_EXTERNAL (olddecl) || TREE_PUBLIC (olddecl) || TREE_STATIC (olddecl))
2307 && DECL_SECTION_NAME (newdecl) == NULL
2308 && DECL_SECTION_NAME (olddecl))
2309 set_decl_section_name (newdecl, DECL_SECTION_NAME (olddecl));
2310
2311 /* Copy the assembler name.
2312 Currently, it can only be defined in the prototype. */
2313 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2314
2315 /* Use visibility of whichever declaration had it specified */
2316 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2317 {
2318 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2319 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2320 }
2321
2322 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2323 {
2324 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2325 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2326 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2327 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2328 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2329 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2330 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2331 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2332 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2333 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2334 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2335 }
2336
2337 /* Merge the storage class information. */
2338 merge_weak (newdecl, olddecl);
2339
2340 /* For functions, static overrides non-static. */
2341 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2342 {
2343 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2344 /* This is since we don't automatically
2345 copy the attributes of NEWDECL into OLDDECL. */
2346 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2347 /* If this clears `static', clear it in the identifier too. */
2348 if (!TREE_PUBLIC (olddecl))
2349 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2350 }
2351 }
2352
2353 /* In c99, 'extern' declaration before (or after) 'inline' means this
2354 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2355 is present. */
2356 if (TREE_CODE (newdecl) == FUNCTION_DECL
2357 && !flag_gnu89_inline
2358 && (DECL_DECLARED_INLINE_P (newdecl)
2359 || DECL_DECLARED_INLINE_P (olddecl))
2360 && (!DECL_DECLARED_INLINE_P (newdecl)
2361 || !DECL_DECLARED_INLINE_P (olddecl)
2362 || !DECL_EXTERNAL (olddecl))
2363 && DECL_EXTERNAL (newdecl)
2364 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2365 && !current_function_decl)
2366 DECL_EXTERNAL (newdecl) = 0;
2367
2368 /* An inline definition following a static declaration is not
2369 DECL_EXTERNAL. */
2370 if (new_is_definition
2371 && (DECL_DECLARED_INLINE_P (newdecl)
2372 || DECL_DECLARED_INLINE_P (olddecl))
2373 && !TREE_PUBLIC (olddecl))
2374 DECL_EXTERNAL (newdecl) = 0;
2375
2376 if (DECL_EXTERNAL (newdecl))
2377 {
2378 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2379 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2380
2381 /* An extern decl does not override previous storage class. */
2382 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2383 if (!DECL_EXTERNAL (newdecl))
2384 {
2385 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2386 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2387 }
2388 }
2389 else
2390 {
2391 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2392 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2393 }
2394
2395 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2396 {
2397 /* If we're redefining a function previously defined as extern
2398 inline, make sure we emit debug info for the inline before we
2399 throw it away, in case it was inlined into a function that
2400 hasn't been written out yet. */
2401 if (new_is_definition && DECL_INITIAL (olddecl))
2402 /* The new defn must not be inline. */
2403 DECL_UNINLINABLE (newdecl) = 1;
2404 else
2405 {
2406 /* If either decl says `inline', this fn is inline, unless
2407 its definition was passed already. */
2408 if (DECL_DECLARED_INLINE_P (newdecl)
2409 || DECL_DECLARED_INLINE_P (olddecl))
2410 DECL_DECLARED_INLINE_P (newdecl) = 1;
2411
2412 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2413 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2414
2415 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2416 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2417 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2418 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2419 }
2420
2421 if (DECL_BUILT_IN (olddecl))
2422 {
2423 /* If redeclaring a builtin function, it stays built in.
2424 But it gets tagged as having been declared. */
2425 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2426 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2427 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2428 if (new_is_prototype)
2429 {
2430 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2431 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2432 {
2433 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2434 switch (fncode)
2435 {
2436 /* If a compatible prototype of these builtin functions
2437 is seen, assume the runtime implements it with the
2438 expected semantics. */
2439 case BUILT_IN_STPCPY:
2440 if (builtin_decl_explicit_p (fncode))
2441 set_builtin_decl_implicit_p (fncode, true);
2442 break;
2443 default:
2444 break;
2445 }
2446 }
2447 }
2448 else
2449 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2450 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2451 }
2452
2453 /* Preserve function specific target and optimization options */
2454 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2455 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2456 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2457 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2458
2459 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2460 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2461 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2462 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2463
2464 /* Also preserve various other info from the definition. */
2465 if (!new_is_definition)
2466 {
2467 tree t;
2468 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2469 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2470 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2471 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2472 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2473 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2474 DECL_CONTEXT (t) = newdecl;
2475
2476 /* See if we've got a function to instantiate from. */
2477 if (DECL_SAVED_TREE (olddecl))
2478 DECL_ABSTRACT_ORIGIN (newdecl)
2479 = DECL_ABSTRACT_ORIGIN (olddecl);
2480 }
2481 }
2482
2483 /* Merge the USED information. */
2484 if (TREE_USED (olddecl))
2485 TREE_USED (newdecl) = 1;
2486 else if (TREE_USED (newdecl))
2487 TREE_USED (olddecl) = 1;
2488 if (TREE_CODE (olddecl) == VAR_DECL || TREE_CODE (olddecl) == PARM_DECL)
2489 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2490 if (DECL_PRESERVE_P (olddecl))
2491 DECL_PRESERVE_P (newdecl) = 1;
2492 else if (DECL_PRESERVE_P (newdecl))
2493 DECL_PRESERVE_P (olddecl) = 1;
2494
2495 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2496 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2497 DECL_ARGUMENTS (if appropriate). */
2498 {
2499 unsigned olddecl_uid = DECL_UID (olddecl);
2500 tree olddecl_context = DECL_CONTEXT (olddecl);
2501 tree olddecl_arguments = NULL;
2502 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2503 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2504
2505 memcpy ((char *) olddecl + sizeof (struct tree_common),
2506 (char *) newdecl + sizeof (struct tree_common),
2507 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2508 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2509 switch (TREE_CODE (olddecl))
2510 {
2511 case FUNCTION_DECL:
2512 case VAR_DECL:
2513 {
2514 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2515
2516 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2517 (char *) newdecl + sizeof (struct tree_decl_common),
2518 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2519 olddecl->decl_with_vis.symtab_node = snode;
2520 break;
2521 }
2522
2523 case FIELD_DECL:
2524 case PARM_DECL:
2525 case LABEL_DECL:
2526 case RESULT_DECL:
2527 case CONST_DECL:
2528 case TYPE_DECL:
2529 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2530 (char *) newdecl + sizeof (struct tree_decl_common),
2531 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2532 break;
2533
2534 default:
2535
2536 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2537 (char *) newdecl + sizeof (struct tree_decl_common),
2538 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2539 }
2540 DECL_UID (olddecl) = olddecl_uid;
2541 DECL_CONTEXT (olddecl) = olddecl_context;
2542 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2543 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2544 }
2545
2546 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2547 so that encode_section_info has a chance to look at the new decl
2548 flags and attributes. */
2549 if (DECL_RTL_SET_P (olddecl)
2550 && (TREE_CODE (olddecl) == FUNCTION_DECL
2551 || (TREE_CODE (olddecl) == VAR_DECL
2552 && TREE_STATIC (olddecl))))
2553 make_decl_rtl (olddecl);
2554 }
2555
2556 /* Handle when a new declaration NEWDECL has the same name as an old
2557 one OLDDECL in the same binding contour. Prints an error message
2558 if appropriate.
2559
2560 If safely possible, alter OLDDECL to look like NEWDECL, and return
2561 true. Otherwise, return false. */
2562
2563 static bool
2564 duplicate_decls (tree newdecl, tree olddecl)
2565 {
2566 tree newtype = NULL, oldtype = NULL;
2567
2568 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2569 {
2570 /* Avoid `unused variable' and other warnings for OLDDECL. */
2571 TREE_NO_WARNING (olddecl) = 1;
2572 return false;
2573 }
2574
2575 merge_decls (newdecl, olddecl, newtype, oldtype);
2576
2577 /* The NEWDECL will no longer be needed.
2578
2579 Before releasing the node, be sure to remove function from symbol
2580 table that might have been inserted there to record comdat group.
2581 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2582 structure is shared in between NEWDECL and OLDECL. */
2583 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2584 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2585 if (TREE_CODE (newdecl) == FUNCTION_DECL
2586 || TREE_CODE (newdecl) == VAR_DECL)
2587 {
2588 struct symtab_node *snode = symtab_node::get (newdecl);
2589 if (snode)
2590 snode->remove ();
2591 }
2592 ggc_free (newdecl);
2593 return true;
2594 }
2595
2596 \f
2597 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2598 static void
2599 warn_if_shadowing (tree new_decl)
2600 {
2601 struct c_binding *b;
2602
2603 /* Shadow warnings wanted? */
2604 if (!warn_shadow
2605 /* No shadow warnings for internally generated vars. */
2606 || DECL_IS_BUILTIN (new_decl)
2607 /* No shadow warnings for vars made for inlining. */
2608 || DECL_FROM_INLINE (new_decl))
2609 return;
2610
2611 /* Is anything being shadowed? Invisible decls do not count. */
2612 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2613 if (b->decl && b->decl != new_decl && !b->invisible
2614 && (b->decl == error_mark_node
2615 || diagnostic_report_warnings_p (global_dc,
2616 DECL_SOURCE_LOCATION (b->decl))))
2617 {
2618 tree old_decl = b->decl;
2619 bool warned = false;
2620
2621 if (old_decl == error_mark_node)
2622 {
2623 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2624 "non-variable", new_decl);
2625 break;
2626 }
2627 else if (TREE_CODE (old_decl) == PARM_DECL)
2628 warned = warning (OPT_Wshadow,
2629 "declaration of %q+D shadows a parameter",
2630 new_decl);
2631 else if (DECL_FILE_SCOPE_P (old_decl))
2632 {
2633 /* Do not warn if a variable shadows a function, unless
2634 the variable is a function or a pointer-to-function. */
2635 if (TREE_CODE (old_decl) == FUNCTION_DECL
2636 && TREE_CODE (new_decl) != FUNCTION_DECL
2637 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2638 continue;
2639
2640 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2641 "declaration of %qD shadows a global "
2642 "declaration",
2643 new_decl);
2644 }
2645 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2646 && DECL_BUILT_IN (old_decl))
2647 {
2648 warning (OPT_Wshadow, "declaration of %q+D shadows "
2649 "a built-in function", new_decl);
2650 break;
2651 }
2652 else
2653 warned = warning (OPT_Wshadow, "declaration of %q+D shadows a "
2654 "previous local", new_decl);
2655
2656 if (warned)
2657 inform (DECL_SOURCE_LOCATION (old_decl),
2658 "shadowed declaration is here");
2659
2660 break;
2661 }
2662 }
2663
2664 /* Record a decl-node X as belonging to the current lexical scope.
2665 Check for errors (such as an incompatible declaration for the same
2666 name already seen in the same scope).
2667
2668 Returns either X or an old decl for the same name.
2669 If an old decl is returned, it may have been smashed
2670 to agree with what X says. */
2671
2672 tree
2673 pushdecl (tree x)
2674 {
2675 tree name = DECL_NAME (x);
2676 struct c_scope *scope = current_scope;
2677 struct c_binding *b;
2678 bool nested = false;
2679 location_t locus = DECL_SOURCE_LOCATION (x);
2680
2681 /* Must set DECL_CONTEXT for everything not at file scope or
2682 DECL_FILE_SCOPE_P won't work. Local externs don't count
2683 unless they have initializers (which generate code). */
2684 if (current_function_decl
2685 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2686 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2687 DECL_CONTEXT (x) = current_function_decl;
2688
2689 /* Anonymous decls are just inserted in the scope. */
2690 if (!name)
2691 {
2692 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2693 locus);
2694 return x;
2695 }
2696
2697 /* First, see if there is another declaration with the same name in
2698 the current scope. If there is, duplicate_decls may do all the
2699 work for us. If duplicate_decls returns false, that indicates
2700 two incompatible decls in the same scope; we are to silently
2701 replace the old one (duplicate_decls has issued all appropriate
2702 diagnostics). In particular, we should not consider possible
2703 duplicates in the external scope, or shadowing. */
2704 b = I_SYMBOL_BINDING (name);
2705 if (b && B_IN_SCOPE (b, scope))
2706 {
2707 struct c_binding *b_ext, *b_use;
2708 tree type = TREE_TYPE (x);
2709 tree visdecl = b->decl;
2710 tree vistype = TREE_TYPE (visdecl);
2711 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2712 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2713 b->inner_comp = false;
2714 b_use = b;
2715 b_ext = b;
2716 /* If this is an external linkage declaration, we should check
2717 for compatibility with the type in the external scope before
2718 setting the type at this scope based on the visible
2719 information only. */
2720 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2721 {
2722 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2723 b_ext = b_ext->shadowed;
2724 if (b_ext)
2725 {
2726 b_use = b_ext;
2727 if (b_use->u.type)
2728 TREE_TYPE (b_use->decl) = b_use->u.type;
2729 }
2730 }
2731 if (duplicate_decls (x, b_use->decl))
2732 {
2733 if (b_use != b)
2734 {
2735 /* Save the updated type in the external scope and
2736 restore the proper type for this scope. */
2737 tree thistype;
2738 if (comptypes (vistype, type))
2739 thistype = composite_type (vistype, type);
2740 else
2741 thistype = TREE_TYPE (b_use->decl);
2742 b_use->u.type = TREE_TYPE (b_use->decl);
2743 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2744 && DECL_BUILT_IN (b_use->decl))
2745 thistype
2746 = build_type_attribute_variant (thistype,
2747 TYPE_ATTRIBUTES
2748 (b_use->u.type));
2749 TREE_TYPE (b_use->decl) = thistype;
2750 }
2751 return b_use->decl;
2752 }
2753 else
2754 goto skip_external_and_shadow_checks;
2755 }
2756
2757 /* All declarations with external linkage, and all external
2758 references, go in the external scope, no matter what scope is
2759 current. However, the binding in that scope is ignored for
2760 purposes of normal name lookup. A separate binding structure is
2761 created in the requested scope; this governs the normal
2762 visibility of the symbol.
2763
2764 The binding in the externals scope is used exclusively for
2765 detecting duplicate declarations of the same object, no matter
2766 what scope they are in; this is what we do here. (C99 6.2.7p2:
2767 All declarations that refer to the same object or function shall
2768 have compatible type; otherwise, the behavior is undefined.) */
2769 if (DECL_EXTERNAL (x) || scope == file_scope)
2770 {
2771 tree type = TREE_TYPE (x);
2772 tree vistype = 0;
2773 tree visdecl = 0;
2774 bool type_saved = false;
2775 if (b && !B_IN_EXTERNAL_SCOPE (b)
2776 && (TREE_CODE (b->decl) == FUNCTION_DECL
2777 || TREE_CODE (b->decl) == VAR_DECL)
2778 && DECL_FILE_SCOPE_P (b->decl))
2779 {
2780 visdecl = b->decl;
2781 vistype = TREE_TYPE (visdecl);
2782 }
2783 if (scope != file_scope
2784 && !DECL_IN_SYSTEM_HEADER (x))
2785 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2786
2787 while (b && !B_IN_EXTERNAL_SCOPE (b))
2788 {
2789 /* If this decl might be modified, save its type. This is
2790 done here rather than when the decl is first bound
2791 because the type may change after first binding, through
2792 being completed or through attributes being added. If we
2793 encounter multiple such decls, only the first should have
2794 its type saved; the others will already have had their
2795 proper types saved and the types will not have changed as
2796 their scopes will not have been re-entered. */
2797 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2798 {
2799 b->u.type = TREE_TYPE (b->decl);
2800 type_saved = true;
2801 }
2802 if (B_IN_FILE_SCOPE (b)
2803 && TREE_CODE (b->decl) == VAR_DECL
2804 && TREE_STATIC (b->decl)
2805 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2806 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2807 && TREE_CODE (type) == ARRAY_TYPE
2808 && TYPE_DOMAIN (type)
2809 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2810 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2811 {
2812 /* Array type completed in inner scope, which should be
2813 diagnosed if the completion does not have size 1 and
2814 it does not get completed in the file scope. */
2815 b->inner_comp = true;
2816 }
2817 b = b->shadowed;
2818 }
2819
2820 /* If a matching external declaration has been found, set its
2821 type to the composite of all the types of that declaration.
2822 After the consistency checks, it will be reset to the
2823 composite of the visible types only. */
2824 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2825 && b->u.type)
2826 TREE_TYPE (b->decl) = b->u.type;
2827
2828 /* The point of the same_translation_unit_p check here is,
2829 we want to detect a duplicate decl for a construct like
2830 foo() { extern bar(); } ... static bar(); but not if
2831 they are in different translation units. In any case,
2832 the static does not go in the externals scope. */
2833 if (b
2834 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2835 && duplicate_decls (x, b->decl))
2836 {
2837 tree thistype;
2838 if (vistype)
2839 {
2840 if (comptypes (vistype, type))
2841 thistype = composite_type (vistype, type);
2842 else
2843 thistype = TREE_TYPE (b->decl);
2844 }
2845 else
2846 thistype = type;
2847 b->u.type = TREE_TYPE (b->decl);
2848 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2849 thistype
2850 = build_type_attribute_variant (thistype,
2851 TYPE_ATTRIBUTES (b->u.type));
2852 TREE_TYPE (b->decl) = thistype;
2853 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2854 locus);
2855 return b->decl;
2856 }
2857 else if (TREE_PUBLIC (x))
2858 {
2859 if (visdecl && !b && duplicate_decls (x, visdecl))
2860 {
2861 /* An external declaration at block scope referring to a
2862 visible entity with internal linkage. The composite
2863 type will already be correct for this scope, so we
2864 just need to fall through to make the declaration in
2865 this scope. */
2866 nested = true;
2867 x = visdecl;
2868 }
2869 else
2870 {
2871 bind (name, x, external_scope, /*invisible=*/true,
2872 /*nested=*/false, locus);
2873 nested = true;
2874 }
2875 }
2876 }
2877
2878 if (TREE_CODE (x) != PARM_DECL)
2879 warn_if_shadowing (x);
2880
2881 skip_external_and_shadow_checks:
2882 if (TREE_CODE (x) == TYPE_DECL)
2883 {
2884 /* So this is a typedef, set its underlying type. */
2885 set_underlying_type (x);
2886
2887 /* If X is a typedef defined in the current function, record it
2888 for the purpose of implementing the -Wunused-local-typedefs
2889 warning. */
2890 record_locally_defined_typedef (x);
2891 }
2892
2893 bind (name, x, scope, /*invisible=*/false, nested, locus);
2894
2895 /* If x's type is incomplete because it's based on a
2896 structure or union which has not yet been fully declared,
2897 attach it to that structure or union type, so we can go
2898 back and complete the variable declaration later, if the
2899 structure or union gets fully declared.
2900
2901 If the input is erroneous, we can have error_mark in the type
2902 slot (e.g. "f(void a, ...)") - that doesn't count as an
2903 incomplete type. */
2904 if (TREE_TYPE (x) != error_mark_node
2905 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2906 {
2907 tree element = TREE_TYPE (x);
2908
2909 while (TREE_CODE (element) == ARRAY_TYPE)
2910 element = TREE_TYPE (element);
2911 element = TYPE_MAIN_VARIANT (element);
2912
2913 if ((TREE_CODE (element) == RECORD_TYPE
2914 || TREE_CODE (element) == UNION_TYPE)
2915 && (TREE_CODE (x) != TYPE_DECL
2916 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2917 && !COMPLETE_TYPE_P (element))
2918 C_TYPE_INCOMPLETE_VARS (element)
2919 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2920 }
2921 return x;
2922 }
2923
2924 /* Record X as belonging to file scope.
2925 This is used only internally by the Objective-C front end,
2926 and is limited to its needs. duplicate_decls is not called;
2927 if there is any preexisting decl for this identifier, it is an ICE. */
2928
2929 tree
2930 pushdecl_top_level (tree x)
2931 {
2932 tree name;
2933 bool nested = false;
2934 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2935
2936 name = DECL_NAME (x);
2937
2938 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2939
2940 if (TREE_PUBLIC (x))
2941 {
2942 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2943 UNKNOWN_LOCATION);
2944 nested = true;
2945 }
2946 if (file_scope)
2947 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2948
2949 return x;
2950 }
2951 \f
2952 static void
2953 implicit_decl_warning (location_t loc, tree id, tree olddecl)
2954 {
2955 if (warn_implicit_function_declaration)
2956 {
2957 bool warned;
2958
2959 if (flag_isoc99)
2960 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
2961 "implicit declaration of function %qE", id);
2962 else
2963 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
2964 G_("implicit declaration of function %qE"), id);
2965 if (olddecl && warned)
2966 locate_old_decl (olddecl);
2967 }
2968 }
2969
2970 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
2971 function of type int (). */
2972
2973 tree
2974 implicitly_declare (location_t loc, tree functionid)
2975 {
2976 struct c_binding *b;
2977 tree decl = 0;
2978 tree asmspec_tree;
2979
2980 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2981 {
2982 if (B_IN_SCOPE (b, external_scope))
2983 {
2984 decl = b->decl;
2985 break;
2986 }
2987 }
2988
2989 if (decl)
2990 {
2991 if (decl == error_mark_node)
2992 return decl;
2993
2994 /* FIXME: Objective-C has weird not-really-builtin functions
2995 which are supposed to be visible automatically. They wind up
2996 in the external scope because they're pushed before the file
2997 scope gets created. Catch this here and rebind them into the
2998 file scope. */
2999 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
3000 {
3001 bind (functionid, decl, file_scope,
3002 /*invisible=*/false, /*nested=*/true,
3003 DECL_SOURCE_LOCATION (decl));
3004 return decl;
3005 }
3006 else
3007 {
3008 tree newtype = default_function_type;
3009 if (b->u.type)
3010 TREE_TYPE (decl) = b->u.type;
3011 /* Implicit declaration of a function already declared
3012 (somehow) in a different scope, or as a built-in.
3013 If this is the first time this has happened, warn;
3014 then recycle the old declaration but with the new type. */
3015 if (!C_DECL_IMPLICIT (decl))
3016 {
3017 implicit_decl_warning (loc, functionid, decl);
3018 C_DECL_IMPLICIT (decl) = 1;
3019 }
3020 if (DECL_BUILT_IN (decl))
3021 {
3022 newtype = build_type_attribute_variant (newtype,
3023 TYPE_ATTRIBUTES
3024 (TREE_TYPE (decl)));
3025 if (!comptypes (newtype, TREE_TYPE (decl)))
3026 {
3027 warning_at (loc, 0, "incompatible implicit declaration of "
3028 "built-in function %qD", decl);
3029 newtype = TREE_TYPE (decl);
3030 }
3031 }
3032 else
3033 {
3034 if (!comptypes (newtype, TREE_TYPE (decl)))
3035 {
3036 error_at (loc, "incompatible implicit declaration of function %qD", decl);
3037 locate_old_decl (decl);
3038 }
3039 }
3040 b->u.type = TREE_TYPE (decl);
3041 TREE_TYPE (decl) = newtype;
3042 bind (functionid, decl, current_scope,
3043 /*invisible=*/false, /*nested=*/true,
3044 DECL_SOURCE_LOCATION (decl));
3045 return decl;
3046 }
3047 }
3048
3049 /* Not seen before. */
3050 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3051 DECL_EXTERNAL (decl) = 1;
3052 TREE_PUBLIC (decl) = 1;
3053 C_DECL_IMPLICIT (decl) = 1;
3054 implicit_decl_warning (loc, functionid, 0);
3055 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3056 if (asmspec_tree)
3057 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3058
3059 /* C89 says implicit declarations are in the innermost block.
3060 So we record the decl in the standard fashion. */
3061 decl = pushdecl (decl);
3062
3063 /* No need to call objc_check_decl here - it's a function type. */
3064 rest_of_decl_compilation (decl, 0, 0);
3065
3066 /* Write a record describing this implicit function declaration
3067 to the prototypes file (if requested). */
3068 gen_aux_info_record (decl, 0, 1, 0);
3069
3070 /* Possibly apply some default attributes to this implicit declaration. */
3071 decl_attributes (&decl, NULL_TREE, 0);
3072
3073 return decl;
3074 }
3075
3076 /* Issue an error message for a reference to an undeclared variable
3077 ID, including a reference to a builtin outside of function-call
3078 context. Establish a binding of the identifier to error_mark_node
3079 in an appropriate scope, which will suppress further errors for the
3080 same identifier. The error message should be given location LOC. */
3081 void
3082 undeclared_variable (location_t loc, tree id)
3083 {
3084 static bool already = false;
3085 struct c_scope *scope;
3086
3087 if (current_function_decl == 0)
3088 {
3089 error_at (loc, "%qE undeclared here (not in a function)", id);
3090 scope = current_scope;
3091 }
3092 else
3093 {
3094 if (!objc_diagnose_private_ivar (id))
3095 error_at (loc, "%qE undeclared (first use in this function)", id);
3096 if (!already)
3097 {
3098 inform (loc, "each undeclared identifier is reported only"
3099 " once for each function it appears in");
3100 already = true;
3101 }
3102
3103 /* If we are parsing old-style parameter decls, current_function_decl
3104 will be nonnull but current_function_scope will be null. */
3105 scope = current_function_scope ? current_function_scope : current_scope;
3106 }
3107 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3108 UNKNOWN_LOCATION);
3109 }
3110 \f
3111 /* Subroutine of lookup_label, declare_label, define_label: construct a
3112 LABEL_DECL with all the proper frills. Also create a struct
3113 c_label_vars initialized for the current scope. */
3114
3115 static tree
3116 make_label (location_t location, tree name, bool defining,
3117 struct c_label_vars **p_label_vars)
3118 {
3119 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3120 DECL_CONTEXT (label) = current_function_decl;
3121 DECL_MODE (label) = VOIDmode;
3122
3123 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3124 label_vars->shadowed = NULL;
3125 set_spot_bindings (&label_vars->label_bindings, defining);
3126 label_vars->decls_in_scope = make_tree_vector ();
3127 label_vars->gotos = NULL;
3128 *p_label_vars = label_vars;
3129
3130 return label;
3131 }
3132
3133 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3134 Create one if none exists so far for the current function.
3135 This is called when a label is used in a goto expression or
3136 has its address taken. */
3137
3138 tree
3139 lookup_label (tree name)
3140 {
3141 tree label;
3142 struct c_label_vars *label_vars;
3143
3144 if (current_function_scope == 0)
3145 {
3146 error ("label %qE referenced outside of any function", name);
3147 return 0;
3148 }
3149
3150 /* Use a label already defined or ref'd with this name, but not if
3151 it is inherited from a containing function and wasn't declared
3152 using __label__. */
3153 label = I_LABEL_DECL (name);
3154 if (label && (DECL_CONTEXT (label) == current_function_decl
3155 || C_DECLARED_LABEL_FLAG (label)))
3156 {
3157 /* If the label has only been declared, update its apparent
3158 location to point here, for better diagnostics if it
3159 turns out not to have been defined. */
3160 if (DECL_INITIAL (label) == NULL_TREE)
3161 DECL_SOURCE_LOCATION (label) = input_location;
3162 return label;
3163 }
3164
3165 /* No label binding for that identifier; make one. */
3166 label = make_label (input_location, name, false, &label_vars);
3167
3168 /* Ordinary labels go in the current function scope. */
3169 bind_label (name, label, current_function_scope, label_vars);
3170
3171 return label;
3172 }
3173
3174 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3175 to LABEL. */
3176
3177 static void
3178 warn_about_goto (location_t goto_loc, tree label, tree decl)
3179 {
3180 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3181 error_at (goto_loc,
3182 "jump into scope of identifier with variably modified type");
3183 else
3184 warning_at (goto_loc, OPT_Wjump_misses_init,
3185 "jump skips variable initialization");
3186 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3187 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3188 }
3189
3190 /* Look up a label because of a goto statement. This is like
3191 lookup_label, but also issues any appropriate warnings. */
3192
3193 tree
3194 lookup_label_for_goto (location_t loc, tree name)
3195 {
3196 tree label;
3197 struct c_label_vars *label_vars;
3198 unsigned int ix;
3199 tree decl;
3200
3201 label = lookup_label (name);
3202 if (label == NULL_TREE)
3203 return NULL_TREE;
3204
3205 /* If we are jumping to a different function, we can't issue any
3206 useful warnings. */
3207 if (DECL_CONTEXT (label) != current_function_decl)
3208 {
3209 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3210 return label;
3211 }
3212
3213 label_vars = I_LABEL_BINDING (name)->u.label;
3214
3215 /* If the label has not yet been defined, then push this goto on a
3216 list for possible later warnings. */
3217 if (label_vars->label_bindings.scope == NULL)
3218 {
3219 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3220
3221 g->loc = loc;
3222 set_spot_bindings (&g->goto_bindings, true);
3223 vec_safe_push (label_vars->gotos, g);
3224 return label;
3225 }
3226
3227 /* If there are any decls in label_vars->decls_in_scope, then this
3228 goto has missed the declaration of the decl. This happens for a
3229 case like
3230 int i = 1;
3231 lab:
3232 ...
3233 goto lab;
3234 Issue a warning or error. */
3235 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3236 warn_about_goto (loc, label, decl);
3237
3238 if (label_vars->label_bindings.left_stmt_expr)
3239 {
3240 error_at (loc, "jump into statement expression");
3241 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3242 }
3243
3244 return label;
3245 }
3246
3247 /* Make a label named NAME in the current function, shadowing silently
3248 any that may be inherited from containing functions or containing
3249 scopes. This is called for __label__ declarations. */
3250
3251 tree
3252 declare_label (tree name)
3253 {
3254 struct c_binding *b = I_LABEL_BINDING (name);
3255 tree label;
3256 struct c_label_vars *label_vars;
3257
3258 /* Check to make sure that the label hasn't already been declared
3259 at this scope */
3260 if (b && B_IN_CURRENT_SCOPE (b))
3261 {
3262 error ("duplicate label declaration %qE", name);
3263 locate_old_decl (b->decl);
3264
3265 /* Just use the previous declaration. */
3266 return b->decl;
3267 }
3268
3269 label = make_label (input_location, name, false, &label_vars);
3270 C_DECLARED_LABEL_FLAG (label) = 1;
3271
3272 /* Declared labels go in the current scope. */
3273 bind_label (name, label, current_scope, label_vars);
3274
3275 return label;
3276 }
3277
3278 /* When we define a label, issue any appropriate warnings if there are
3279 any gotos earlier in the function which jump to this label. */
3280
3281 static void
3282 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3283 {
3284 unsigned int ix;
3285 struct c_goto_bindings *g;
3286
3287 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3288 {
3289 struct c_binding *b;
3290 struct c_scope *scope;
3291
3292 /* We have a goto to this label. The goto is going forward. In
3293 g->scope, the goto is going to skip any binding which was
3294 defined after g->bindings_in_scope. */
3295 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3296 {
3297 for (b = g->goto_bindings.scope->bindings;
3298 b != g->goto_bindings.bindings_in_scope;
3299 b = b->prev)
3300 {
3301 if (decl_jump_unsafe (b->decl))
3302 warn_about_goto (g->loc, label, b->decl);
3303 }
3304 }
3305
3306 /* We also need to warn about decls defined in any scopes
3307 between the scope of the label and the scope of the goto. */
3308 for (scope = label_vars->label_bindings.scope;
3309 scope != g->goto_bindings.scope;
3310 scope = scope->outer)
3311 {
3312 gcc_assert (scope != NULL);
3313 if (scope->has_jump_unsafe_decl)
3314 {
3315 if (scope == label_vars->label_bindings.scope)
3316 b = label_vars->label_bindings.bindings_in_scope;
3317 else
3318 b = scope->bindings;
3319 for (; b != NULL; b = b->prev)
3320 {
3321 if (decl_jump_unsafe (b->decl))
3322 warn_about_goto (g->loc, label, b->decl);
3323 }
3324 }
3325 }
3326
3327 if (g->goto_bindings.stmt_exprs > 0)
3328 {
3329 error_at (g->loc, "jump into statement expression");
3330 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3331 label);
3332 }
3333 }
3334
3335 /* Now that the label is defined, we will issue warnings about
3336 subsequent gotos to this label when we see them. */
3337 vec_safe_truncate (label_vars->gotos, 0);
3338 label_vars->gotos = NULL;
3339 }
3340
3341 /* Define a label, specifying the location in the source file.
3342 Return the LABEL_DECL node for the label, if the definition is valid.
3343 Otherwise return 0. */
3344
3345 tree
3346 define_label (location_t location, tree name)
3347 {
3348 /* Find any preexisting label with this name. It is an error
3349 if that label has already been defined in this function, or
3350 if there is a containing function with a declared label with
3351 the same name. */
3352 tree label = I_LABEL_DECL (name);
3353
3354 if (label
3355 && ((DECL_CONTEXT (label) == current_function_decl
3356 && DECL_INITIAL (label) != 0)
3357 || (DECL_CONTEXT (label) != current_function_decl
3358 && C_DECLARED_LABEL_FLAG (label))))
3359 {
3360 error_at (location, "duplicate label %qD", label);
3361 locate_old_decl (label);
3362 return 0;
3363 }
3364 else if (label && DECL_CONTEXT (label) == current_function_decl)
3365 {
3366 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3367
3368 /* The label has been used or declared already in this function,
3369 but not defined. Update its location to point to this
3370 definition. */
3371 DECL_SOURCE_LOCATION (label) = location;
3372 set_spot_bindings (&label_vars->label_bindings, true);
3373
3374 /* Issue warnings as required about any goto statements from
3375 earlier in the function. */
3376 check_earlier_gotos (label, label_vars);
3377 }
3378 else
3379 {
3380 struct c_label_vars *label_vars;
3381
3382 /* No label binding for that identifier; make one. */
3383 label = make_label (location, name, true, &label_vars);
3384
3385 /* Ordinary labels go in the current function scope. */
3386 bind_label (name, label, current_function_scope, label_vars);
3387 }
3388
3389 if (!in_system_header_at (input_location) && lookup_name (name))
3390 warning_at (location, OPT_Wtraditional,
3391 "traditional C lacks a separate namespace "
3392 "for labels, identifier %qE conflicts", name);
3393
3394 /* Mark label as having been defined. */
3395 DECL_INITIAL (label) = error_mark_node;
3396 return label;
3397 }
3398 \f
3399 /* Get the bindings for a new switch statement. This is used to issue
3400 warnings as appropriate for jumps from the switch to case or
3401 default labels. */
3402
3403 struct c_spot_bindings *
3404 c_get_switch_bindings (void)
3405 {
3406 struct c_spot_bindings *switch_bindings;
3407
3408 switch_bindings = XNEW (struct c_spot_bindings);
3409 set_spot_bindings (switch_bindings, true);
3410 return switch_bindings;
3411 }
3412
3413 void
3414 c_release_switch_bindings (struct c_spot_bindings *bindings)
3415 {
3416 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3417 XDELETE (bindings);
3418 }
3419
3420 /* This is called at the point of a case or default label to issue
3421 warnings about decls as needed. It returns true if it found an
3422 error, not just a warning. */
3423
3424 bool
3425 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3426 location_t switch_loc, location_t case_loc)
3427 {
3428 bool saw_error;
3429 struct c_scope *scope;
3430
3431 saw_error = false;
3432 for (scope = current_scope;
3433 scope != switch_bindings->scope;
3434 scope = scope->outer)
3435 {
3436 struct c_binding *b;
3437
3438 gcc_assert (scope != NULL);
3439
3440 if (!scope->has_jump_unsafe_decl)
3441 continue;
3442
3443 for (b = scope->bindings; b != NULL; b = b->prev)
3444 {
3445 if (decl_jump_unsafe (b->decl))
3446 {
3447 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3448 {
3449 saw_error = true;
3450 error_at (case_loc,
3451 ("switch jumps into scope of identifier with "
3452 "variably modified type"));
3453 }
3454 else
3455 warning_at (case_loc, OPT_Wjump_misses_init,
3456 "switch jumps over variable initialization");
3457 inform (switch_loc, "switch starts here");
3458 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3459 b->decl);
3460 }
3461 }
3462 }
3463
3464 if (switch_bindings->stmt_exprs > 0)
3465 {
3466 saw_error = true;
3467 error_at (case_loc, "switch jumps into statement expression");
3468 inform (switch_loc, "switch starts here");
3469 }
3470
3471 return saw_error;
3472 }
3473 \f
3474 /* Given NAME, an IDENTIFIER_NODE,
3475 return the structure (or union or enum) definition for that name.
3476 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3477 CODE says which kind of type the caller wants;
3478 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3479 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3480 location where the tag was defined.
3481 If the wrong kind of type is found, an error is reported. */
3482
3483 static tree
3484 lookup_tag (enum tree_code code, tree name, int thislevel_only,
3485 location_t *ploc)
3486 {
3487 struct c_binding *b = I_TAG_BINDING (name);
3488 int thislevel = 0;
3489
3490 if (!b || !b->decl)
3491 return 0;
3492
3493 /* We only care about whether it's in this level if
3494 thislevel_only was set or it might be a type clash. */
3495 if (thislevel_only || TREE_CODE (b->decl) != code)
3496 {
3497 /* For our purposes, a tag in the external scope is the same as
3498 a tag in the file scope. (Primarily relevant to Objective-C
3499 and its builtin structure tags, which get pushed before the
3500 file scope is created.) */
3501 if (B_IN_CURRENT_SCOPE (b)
3502 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3503 thislevel = 1;
3504 }
3505
3506 if (thislevel_only && !thislevel)
3507 return 0;
3508
3509 if (TREE_CODE (b->decl) != code)
3510 {
3511 /* Definition isn't the kind we were looking for. */
3512 pending_invalid_xref = name;
3513 pending_invalid_xref_location = input_location;
3514
3515 /* If in the same binding level as a declaration as a tag
3516 of a different type, this must not be allowed to
3517 shadow that tag, so give the error immediately.
3518 (For example, "struct foo; union foo;" is invalid.) */
3519 if (thislevel)
3520 pending_xref_error ();
3521 }
3522
3523 if (ploc != NULL)
3524 *ploc = b->locus;
3525
3526 return b->decl;
3527 }
3528
3529 /* Print an error message now
3530 for a recent invalid struct, union or enum cross reference.
3531 We don't print them immediately because they are not invalid
3532 when used in the `struct foo;' construct for shadowing. */
3533
3534 void
3535 pending_xref_error (void)
3536 {
3537 if (pending_invalid_xref != 0)
3538 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3539 pending_invalid_xref);
3540 pending_invalid_xref = 0;
3541 }
3542
3543 \f
3544 /* Look up NAME in the current scope and its superiors
3545 in the namespace of variables, functions and typedefs.
3546 Return a ..._DECL node of some kind representing its definition,
3547 or return 0 if it is undefined. */
3548
3549 tree
3550 lookup_name (tree name)
3551 {
3552 struct c_binding *b = I_SYMBOL_BINDING (name);
3553 if (b && !b->invisible)
3554 {
3555 maybe_record_typedef_use (b->decl);
3556 return b->decl;
3557 }
3558 return 0;
3559 }
3560
3561 /* Similar to `lookup_name' but look only at the indicated scope. */
3562
3563 static tree
3564 lookup_name_in_scope (tree name, struct c_scope *scope)
3565 {
3566 struct c_binding *b;
3567
3568 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3569 if (B_IN_SCOPE (b, scope))
3570 return b->decl;
3571 return 0;
3572 }
3573 \f
3574 /* Create the predefined scalar types of C,
3575 and some nodes representing standard constants (0, 1, (void *) 0).
3576 Initialize the global scope.
3577 Make definitions for built-in primitive functions. */
3578
3579 void
3580 c_init_decl_processing (void)
3581 {
3582 location_t save_loc = input_location;
3583
3584 /* Initialize reserved words for parser. */
3585 c_parse_init ();
3586
3587 current_function_decl = 0;
3588
3589 gcc_obstack_init (&parser_obstack);
3590
3591 /* Make the externals scope. */
3592 push_scope ();
3593 external_scope = current_scope;
3594
3595 /* Declarations from c_common_nodes_and_builtins must not be associated
3596 with this input file, lest we get differences between using and not
3597 using preprocessed headers. */
3598 input_location = BUILTINS_LOCATION;
3599
3600 c_common_nodes_and_builtins ();
3601
3602 /* In C, comparisons and TRUTH_* expressions have type int. */
3603 truthvalue_type_node = integer_type_node;
3604 truthvalue_true_node = integer_one_node;
3605 truthvalue_false_node = integer_zero_node;
3606
3607 /* Even in C99, which has a real boolean type. */
3608 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3609 boolean_type_node));
3610
3611 input_location = save_loc;
3612
3613 pedantic_lvalues = true;
3614
3615 make_fname_decl = c_make_fname_decl;
3616 start_fname_decls ();
3617 }
3618
3619 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3620 give the decl, NAME is the initialization string and TYPE_DEP
3621 indicates whether NAME depended on the type of the function. As we
3622 don't yet implement delayed emission of static data, we mark the
3623 decl as emitted so it is not placed in the output. Anything using
3624 it must therefore pull out the STRING_CST initializer directly.
3625 FIXME. */
3626
3627 static tree
3628 c_make_fname_decl (location_t loc, tree id, int type_dep)
3629 {
3630 const char *name = fname_as_string (type_dep);
3631 tree decl, type, init;
3632 size_t length = strlen (name);
3633
3634 type = build_array_type (char_type_node,
3635 build_index_type (size_int (length)));
3636 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3637
3638 decl = build_decl (loc, VAR_DECL, id, type);
3639
3640 TREE_STATIC (decl) = 1;
3641 TREE_READONLY (decl) = 1;
3642 DECL_ARTIFICIAL (decl) = 1;
3643
3644 init = build_string (length + 1, name);
3645 free (CONST_CAST (char *, name));
3646 TREE_TYPE (init) = type;
3647 DECL_INITIAL (decl) = init;
3648
3649 TREE_USED (decl) = 1;
3650
3651 if (current_function_decl
3652 /* For invalid programs like this:
3653
3654 void foo()
3655 const char* p = __FUNCTION__;
3656
3657 the __FUNCTION__ is believed to appear in K&R style function
3658 parameter declarator. In that case we still don't have
3659 function_scope. */
3660 && (!seen_error () || current_function_scope))
3661 {
3662 DECL_CONTEXT (decl) = current_function_decl;
3663 bind (id, decl, current_function_scope,
3664 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
3665 }
3666
3667 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
3668
3669 return decl;
3670 }
3671
3672 tree
3673 c_builtin_function (tree decl)
3674 {
3675 tree type = TREE_TYPE (decl);
3676 tree id = DECL_NAME (decl);
3677
3678 const char *name = IDENTIFIER_POINTER (id);
3679 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
3680
3681 /* Should never be called on a symbol with a preexisting meaning. */
3682 gcc_assert (!I_SYMBOL_BINDING (id));
3683
3684 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
3685 UNKNOWN_LOCATION);
3686
3687 /* Builtins in the implementation namespace are made visible without
3688 needing to be explicitly declared. See push_file_scope. */
3689 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3690 {
3691 DECL_CHAIN (decl) = visible_builtins;
3692 visible_builtins = decl;
3693 }
3694
3695 return decl;
3696 }
3697
3698 tree
3699 c_builtin_function_ext_scope (tree decl)
3700 {
3701 tree type = TREE_TYPE (decl);
3702 tree id = DECL_NAME (decl);
3703
3704 const char *name = IDENTIFIER_POINTER (id);
3705 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
3706
3707 if (external_scope)
3708 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
3709 UNKNOWN_LOCATION);
3710
3711 /* Builtins in the implementation namespace are made visible without
3712 needing to be explicitly declared. See push_file_scope. */
3713 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3714 {
3715 DECL_CHAIN (decl) = visible_builtins;
3716 visible_builtins = decl;
3717 }
3718
3719 return decl;
3720 }
3721 \f
3722 /* Called when a declaration is seen that contains no names to declare.
3723 If its type is a reference to a structure, union or enum inherited
3724 from a containing scope, shadow that tag name for the current scope
3725 with a forward reference.
3726 If its type defines a new named structure or union
3727 or defines an enum, it is valid but we need not do anything here.
3728 Otherwise, it is an error. */
3729
3730 void
3731 shadow_tag (const struct c_declspecs *declspecs)
3732 {
3733 shadow_tag_warned (declspecs, 0);
3734 }
3735
3736 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
3737 but no pedwarn. */
3738 void
3739 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
3740 {
3741 bool found_tag = false;
3742
3743 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
3744 {
3745 tree value = declspecs->type;
3746 enum tree_code code = TREE_CODE (value);
3747
3748 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3749 /* Used to test also that TYPE_SIZE (value) != 0.
3750 That caused warning for `struct foo;' at top level in the file. */
3751 {
3752 tree name = TYPE_NAME (value);
3753 tree t;
3754
3755 found_tag = true;
3756
3757 if (declspecs->restrict_p)
3758 {
3759 error ("invalid use of %<restrict%>");
3760 warned = 1;
3761 }
3762
3763 if (name == 0)
3764 {
3765 if (warned != 1 && code != ENUMERAL_TYPE)
3766 /* Empty unnamed enum OK */
3767 {
3768 pedwarn (input_location, 0,
3769 "unnamed struct/union that defines no instances");
3770 warned = 1;
3771 }
3772 }
3773 else if (declspecs->typespec_kind != ctsk_tagdef
3774 && declspecs->typespec_kind != ctsk_tagfirstref
3775 && declspecs->storage_class != csc_none)
3776 {
3777 if (warned != 1)
3778 pedwarn (input_location, 0,
3779 "empty declaration with storage class specifier "
3780 "does not redeclare tag");
3781 warned = 1;
3782 pending_xref_error ();
3783 }
3784 else if (declspecs->typespec_kind != ctsk_tagdef
3785 && declspecs->typespec_kind != ctsk_tagfirstref
3786 && (declspecs->const_p
3787 || declspecs->volatile_p
3788 || declspecs->atomic_p
3789 || declspecs->restrict_p
3790 || declspecs->address_space))
3791 {
3792 if (warned != 1)
3793 pedwarn (input_location, 0,
3794 "empty declaration with type qualifier "
3795 "does not redeclare tag");
3796 warned = 1;
3797 pending_xref_error ();
3798 }
3799 else if (declspecs->typespec_kind != ctsk_tagdef
3800 && declspecs->typespec_kind != ctsk_tagfirstref
3801 && declspecs->alignas_p)
3802 {
3803 if (warned != 1)
3804 pedwarn (input_location, 0,
3805 "empty declaration with %<_Alignas%> "
3806 "does not redeclare tag");
3807 warned = 1;
3808 pending_xref_error ();
3809 }
3810 else
3811 {
3812 pending_invalid_xref = 0;
3813 t = lookup_tag (code, name, 1, NULL);
3814
3815 if (t == 0)
3816 {
3817 t = make_node (code);
3818 pushtag (input_location, name, t);
3819 }
3820 }
3821 }
3822 else
3823 {
3824 if (warned != 1 && !in_system_header_at (input_location))
3825 {
3826 pedwarn (input_location, 0,
3827 "useless type name in empty declaration");
3828 warned = 1;
3829 }
3830 }
3831 }
3832 else if (warned != 1 && !in_system_header_at (input_location)
3833 && declspecs->typedef_p)
3834 {
3835 pedwarn (input_location, 0, "useless type name in empty declaration");
3836 warned = 1;
3837 }
3838
3839 pending_invalid_xref = 0;
3840
3841 if (declspecs->inline_p)
3842 {
3843 error ("%<inline%> in empty declaration");
3844 warned = 1;
3845 }
3846
3847 if (declspecs->noreturn_p)
3848 {
3849 error ("%<_Noreturn%> in empty declaration");
3850 warned = 1;
3851 }
3852
3853 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3854 {
3855 error ("%<auto%> in file-scope empty declaration");
3856 warned = 1;
3857 }
3858
3859 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3860 {
3861 error ("%<register%> in file-scope empty declaration");
3862 warned = 1;
3863 }
3864
3865 if (!warned && !in_system_header_at (input_location)
3866 && declspecs->storage_class != csc_none)
3867 {
3868 warning (0, "useless storage class specifier in empty declaration");
3869 warned = 2;
3870 }
3871
3872 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
3873 {
3874 warning (0, "useless %qs in empty declaration",
3875 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
3876 warned = 2;
3877 }
3878
3879 if (!warned
3880 && !in_system_header_at (input_location)
3881 && (declspecs->const_p
3882 || declspecs->volatile_p
3883 || declspecs->atomic_p
3884 || declspecs->restrict_p
3885 || declspecs->address_space))
3886 {
3887 warning (0, "useless type qualifier in empty declaration");
3888 warned = 2;
3889 }
3890
3891 if (!warned && !in_system_header_at (input_location)
3892 && declspecs->alignas_p)
3893 {
3894 warning (0, "useless %<_Alignas%> in empty declaration");
3895 warned = 2;
3896 }
3897
3898 if (warned != 1)
3899 {
3900 if (!found_tag)
3901 pedwarn (input_location, 0, "empty declaration");
3902 }
3903 }
3904 \f
3905
3906 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3907 bits. SPECS represents declaration specifiers that the grammar
3908 only permits to contain type qualifiers and attributes. */
3909
3910 int
3911 quals_from_declspecs (const struct c_declspecs *specs)
3912 {
3913 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3914 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3915 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
3916 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
3917 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
3918 gcc_assert (!specs->type
3919 && !specs->decl_attr
3920 && specs->typespec_word == cts_none
3921 && specs->storage_class == csc_none
3922 && !specs->typedef_p
3923 && !specs->explicit_signed_p
3924 && !specs->deprecated_p
3925 && !specs->long_p
3926 && !specs->long_long_p
3927 && !specs->short_p
3928 && !specs->signed_p
3929 && !specs->unsigned_p
3930 && !specs->complex_p
3931 && !specs->inline_p
3932 && !specs->noreturn_p
3933 && !specs->thread_p);
3934 return quals;
3935 }
3936
3937 /* Construct an array declarator. LOC is the location of the
3938 beginning of the array (usually the opening brace). EXPR is the
3939 expression inside [], or NULL_TREE. QUALS are the type qualifiers
3940 inside the [] (to be applied to the pointer to which a parameter
3941 array is converted). STATIC_P is true if "static" is inside the
3942 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
3943 VLA of unspecified length which is nevertheless a complete type,
3944 false otherwise. The field for the contained declarator is left to
3945 be filled in by set_array_declarator_inner. */
3946
3947 struct c_declarator *
3948 build_array_declarator (location_t loc,
3949 tree expr, struct c_declspecs *quals, bool static_p,
3950 bool vla_unspec_p)
3951 {
3952 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3953 struct c_declarator);
3954 declarator->id_loc = loc;
3955 declarator->kind = cdk_array;
3956 declarator->declarator = 0;
3957 declarator->u.array.dimen = expr;
3958 if (quals)
3959 {
3960 declarator->u.array.attrs = quals->attrs;
3961 declarator->u.array.quals = quals_from_declspecs (quals);
3962 }
3963 else
3964 {
3965 declarator->u.array.attrs = NULL_TREE;
3966 declarator->u.array.quals = 0;
3967 }
3968 declarator->u.array.static_p = static_p;
3969 declarator->u.array.vla_unspec_p = vla_unspec_p;
3970 if (static_p || quals != NULL)
3971 pedwarn_c90 (loc, OPT_Wpedantic,
3972 "ISO C90 does not support %<static%> or type "
3973 "qualifiers in parameter array declarators");
3974 if (vla_unspec_p)
3975 pedwarn_c90 (loc, OPT_Wpedantic,
3976 "ISO C90 does not support %<[*]%> array declarators");
3977 if (vla_unspec_p)
3978 {
3979 if (!current_scope->parm_flag)
3980 {
3981 /* C99 6.7.5.2p4 */
3982 error_at (loc, "%<[*]%> not allowed in other than "
3983 "function prototype scope");
3984 declarator->u.array.vla_unspec_p = false;
3985 return NULL;
3986 }
3987 current_scope->had_vla_unspec = true;
3988 }
3989 return declarator;
3990 }
3991
3992 /* Set the contained declarator of an array declarator. DECL is the
3993 declarator, as constructed by build_array_declarator; INNER is what
3994 appears on the left of the []. */
3995
3996 struct c_declarator *
3997 set_array_declarator_inner (struct c_declarator *decl,
3998 struct c_declarator *inner)
3999 {
4000 decl->declarator = inner;
4001 return decl;
4002 }
4003
4004 /* INIT is a constructor that forms DECL's initializer. If the final
4005 element initializes a flexible array field, add the size of that
4006 initializer to DECL's size. */
4007
4008 static void
4009 add_flexible_array_elts_to_size (tree decl, tree init)
4010 {
4011 tree elt, type;
4012
4013 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4014 return;
4015
4016 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4017 type = TREE_TYPE (elt);
4018 if (TREE_CODE (type) == ARRAY_TYPE
4019 && TYPE_SIZE (type) == NULL_TREE
4020 && TYPE_DOMAIN (type) != NULL_TREE
4021 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4022 {
4023 complete_array_type (&type, elt, false);
4024 DECL_SIZE (decl)
4025 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4026 DECL_SIZE_UNIT (decl)
4027 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4028 }
4029 }
4030 \f
4031 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4032 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4033 before the type name, and set *EXPR_CONST_OPERANDS, if
4034 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4035 appear in a constant expression. */
4036
4037 tree
4038 groktypename (struct c_type_name *type_name, tree *expr,
4039 bool *expr_const_operands)
4040 {
4041 tree type;
4042 tree attrs = type_name->specs->attrs;
4043
4044 type_name->specs->attrs = NULL_TREE;
4045
4046 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4047 false, NULL, &attrs, expr, expr_const_operands,
4048 DEPRECATED_NORMAL);
4049
4050 /* Apply attributes. */
4051 decl_attributes (&type, attrs, 0);
4052
4053 return type;
4054 }
4055
4056 /* Wrapper for decl_attributes that adds some implicit attributes
4057 to VAR_DECLs or FUNCTION_DECLs. */
4058
4059 static tree
4060 c_decl_attributes (tree *node, tree attributes, int flags)
4061 {
4062 /* Add implicit "omp declare target" attribute if requested. */
4063 if (current_omp_declare_target_attribute
4064 && ((TREE_CODE (*node) == VAR_DECL && TREE_STATIC (*node))
4065 || TREE_CODE (*node) == FUNCTION_DECL))
4066 {
4067 if (TREE_CODE (*node) == VAR_DECL
4068 && ((DECL_CONTEXT (*node)
4069 && TREE_CODE (DECL_CONTEXT (*node)) == FUNCTION_DECL)
4070 || (current_function_decl && !DECL_EXTERNAL (*node))))
4071 error ("%q+D in block scope inside of declare target directive",
4072 *node);
4073 else if (TREE_CODE (*node) == VAR_DECL
4074 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4075 error ("%q+D in declare target directive does not have mappable type",
4076 *node);
4077 else
4078 attributes = tree_cons (get_identifier ("omp declare target"),
4079 NULL_TREE, attributes);
4080 }
4081 return decl_attributes (node, attributes, flags);
4082 }
4083
4084
4085 /* Decode a declarator in an ordinary declaration or data definition.
4086 This is called as soon as the type information and variable name
4087 have been parsed, before parsing the initializer if any.
4088 Here we create the ..._DECL node, fill in its type,
4089 and put it on the list of decls for the current context.
4090 The ..._DECL node is returned as the value.
4091
4092 Exception: for arrays where the length is not specified,
4093 the type is left null, to be filled in by `finish_decl'.
4094
4095 Function definitions do not come here; they go to start_function
4096 instead. However, external and forward declarations of functions
4097 do go through here. Structure field declarations are done by
4098 grokfield and not through here. */
4099
4100 tree
4101 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4102 bool initialized, tree attributes)
4103 {
4104 tree decl;
4105 tree tem;
4106 tree expr = NULL_TREE;
4107 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4108
4109 /* An object declared as __attribute__((deprecated)) suppresses
4110 warnings of uses of other deprecated items. */
4111 if (lookup_attribute ("deprecated", attributes))
4112 deprecated_state = DEPRECATED_SUPPRESS;
4113
4114 decl = grokdeclarator (declarator, declspecs,
4115 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4116 deprecated_state);
4117 if (!decl)
4118 return 0;
4119
4120 if (expr)
4121 add_stmt (fold_convert (void_type_node, expr));
4122
4123 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4124 warning (OPT_Wmain, "%q+D is usually a function", decl);
4125
4126 if (initialized)
4127 /* Is it valid for this decl to have an initializer at all?
4128 If not, set INITIALIZED to zero, which will indirectly
4129 tell 'finish_decl' to ignore the initializer once it is parsed. */
4130 switch (TREE_CODE (decl))
4131 {
4132 case TYPE_DECL:
4133 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4134 initialized = 0;
4135 break;
4136
4137 case FUNCTION_DECL:
4138 error ("function %qD is initialized like a variable", decl);
4139 initialized = 0;
4140 break;
4141
4142 case PARM_DECL:
4143 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4144 error ("parameter %qD is initialized", decl);
4145 initialized = 0;
4146 break;
4147
4148 default:
4149 /* Don't allow initializations for incomplete types except for
4150 arrays which might be completed by the initialization. */
4151
4152 /* This can happen if the array size is an undefined macro.
4153 We already gave a warning, so we don't need another one. */
4154 if (TREE_TYPE (decl) == error_mark_node)
4155 initialized = 0;
4156 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4157 {
4158 /* A complete type is ok if size is fixed. */
4159
4160 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4161 || C_DECL_VARIABLE_SIZE (decl))
4162 {
4163 error ("variable-sized object may not be initialized");
4164 initialized = 0;
4165 }
4166 }
4167 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4168 {
4169 error ("variable %qD has initializer but incomplete type", decl);
4170 initialized = 0;
4171 }
4172 else if (C_DECL_VARIABLE_SIZE (decl))
4173 {
4174 /* Although C99 is unclear about whether incomplete arrays
4175 of VLAs themselves count as VLAs, it does not make
4176 sense to permit them to be initialized given that
4177 ordinary VLAs may not be initialized. */
4178 error ("variable-sized object may not be initialized");
4179 initialized = 0;
4180 }
4181 }
4182
4183 if (initialized)
4184 {
4185 if (current_scope == file_scope)
4186 TREE_STATIC (decl) = 1;
4187
4188 /* Tell 'pushdecl' this is an initialized decl
4189 even though we don't yet have the initializer expression.
4190 Also tell 'finish_decl' it may store the real initializer. */
4191 DECL_INITIAL (decl) = error_mark_node;
4192 }
4193
4194 /* If this is a function declaration, write a record describing it to the
4195 prototypes file (if requested). */
4196
4197 if (TREE_CODE (decl) == FUNCTION_DECL)
4198 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4199
4200 /* ANSI specifies that a tentative definition which is not merged with
4201 a non-tentative definition behaves exactly like a definition with an
4202 initializer equal to zero. (Section 3.7.2)
4203
4204 -fno-common gives strict ANSI behavior, though this tends to break
4205 a large body of code that grew up without this rule.
4206
4207 Thread-local variables are never common, since there's no entrenched
4208 body of code to break, and it allows more efficient variable references
4209 in the presence of dynamic linking. */
4210
4211 if (TREE_CODE (decl) == VAR_DECL
4212 && !initialized
4213 && TREE_PUBLIC (decl)
4214 && !DECL_THREAD_LOCAL_P (decl)
4215 && !flag_no_common)
4216 DECL_COMMON (decl) = 1;
4217
4218 /* Set attributes here so if duplicate decl, will have proper attributes. */
4219 c_decl_attributes (&decl, attributes, 0);
4220
4221 /* Handle gnu_inline attribute. */
4222 if (declspecs->inline_p
4223 && !flag_gnu89_inline
4224 && TREE_CODE (decl) == FUNCTION_DECL
4225 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4226 || current_function_decl))
4227 {
4228 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4229 ;
4230 else if (declspecs->storage_class != csc_static)
4231 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4232 }
4233
4234 if (TREE_CODE (decl) == FUNCTION_DECL
4235 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4236 {
4237 struct c_declarator *ce = declarator;
4238
4239 if (ce->kind == cdk_pointer)
4240 ce = declarator->declarator;
4241 if (ce->kind == cdk_function)
4242 {
4243 tree args = ce->u.arg_info->parms;
4244 for (; args; args = DECL_CHAIN (args))
4245 {
4246 tree type = TREE_TYPE (args);
4247 if (type && INTEGRAL_TYPE_P (type)
4248 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4249 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4250 }
4251 }
4252 }
4253
4254 if (TREE_CODE (decl) == FUNCTION_DECL
4255 && DECL_DECLARED_INLINE_P (decl)
4256 && DECL_UNINLINABLE (decl)
4257 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4258 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4259 decl);
4260
4261 /* C99 6.7.4p3: An inline definition of a function with external
4262 linkage shall not contain a definition of a modifiable object
4263 with static storage duration... */
4264 if (TREE_CODE (decl) == VAR_DECL
4265 && current_scope != file_scope
4266 && TREE_STATIC (decl)
4267 && !TREE_READONLY (decl)
4268 && DECL_DECLARED_INLINE_P (current_function_decl)
4269 && DECL_EXTERNAL (current_function_decl))
4270 record_inline_static (input_location, current_function_decl,
4271 decl, csi_modifiable);
4272
4273 if (c_dialect_objc ()
4274 && (TREE_CODE (decl) == VAR_DECL
4275 || TREE_CODE (decl) == FUNCTION_DECL))
4276 objc_check_global_decl (decl);
4277
4278 /* Add this decl to the current scope.
4279 TEM may equal DECL or it may be a previous decl of the same name. */
4280 tem = pushdecl (decl);
4281
4282 if (initialized && DECL_EXTERNAL (tem))
4283 {
4284 DECL_EXTERNAL (tem) = 0;
4285 TREE_STATIC (tem) = 1;
4286 }
4287
4288 return tem;
4289 }
4290
4291 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4292 DECL or the non-array element type if DECL is an uninitialized array.
4293 If that type has a const member, diagnose this. */
4294
4295 static void
4296 diagnose_uninitialized_cst_member (tree decl, tree type)
4297 {
4298 tree field;
4299 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4300 {
4301 tree field_type;
4302 if (TREE_CODE (field) != FIELD_DECL)
4303 continue;
4304 field_type = strip_array_types (TREE_TYPE (field));
4305
4306 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4307 {
4308 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4309 "uninitialized const member in %qT is invalid in C++",
4310 strip_array_types (TREE_TYPE (decl)));
4311 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4312 }
4313
4314 if (TREE_CODE (field_type) == RECORD_TYPE
4315 || TREE_CODE (field_type) == UNION_TYPE)
4316 diagnose_uninitialized_cst_member (decl, field_type);
4317 }
4318 }
4319
4320 /* Finish processing of a declaration;
4321 install its initial value.
4322 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4323 If the length of an array type is not known before,
4324 it must be determined now, from the initial value, or it is an error.
4325
4326 INIT_LOC is the location of the initial value. */
4327
4328 void
4329 finish_decl (tree decl, location_t init_loc, tree init,
4330 tree origtype, tree asmspec_tree)
4331 {
4332 tree type;
4333 bool was_incomplete = (DECL_SIZE (decl) == 0);
4334 const char *asmspec = 0;
4335
4336 /* If a name was specified, get the string. */
4337 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4338 && DECL_FILE_SCOPE_P (decl))
4339 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4340 if (asmspec_tree)
4341 asmspec = TREE_STRING_POINTER (asmspec_tree);
4342
4343 if (TREE_CODE (decl) == VAR_DECL
4344 && TREE_STATIC (decl)
4345 && global_bindings_p ())
4346 /* So decl is a global variable. Record the types it uses
4347 so that we can decide later to emit debug info for them. */
4348 record_types_used_by_current_var_decl (decl);
4349
4350 /* If `start_decl' didn't like having an initialization, ignore it now. */
4351 if (init != 0 && DECL_INITIAL (decl) == 0)
4352 init = 0;
4353
4354 /* Don't crash if parm is initialized. */
4355 if (TREE_CODE (decl) == PARM_DECL)
4356 init = 0;
4357
4358 if (init)
4359 store_init_value (init_loc, decl, init, origtype);
4360
4361 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
4362 || TREE_CODE (decl) == FUNCTION_DECL
4363 || TREE_CODE (decl) == FIELD_DECL))
4364 objc_check_decl (decl);
4365
4366 type = TREE_TYPE (decl);
4367
4368 /* Deduce size of array from initialization, if not already known. */
4369 if (TREE_CODE (type) == ARRAY_TYPE
4370 && TYPE_DOMAIN (type) == 0
4371 && TREE_CODE (decl) != TYPE_DECL)
4372 {
4373 bool do_default
4374 = (TREE_STATIC (decl)
4375 /* Even if pedantic, an external linkage array
4376 may have incomplete type at first. */
4377 ? pedantic && !TREE_PUBLIC (decl)
4378 : !DECL_EXTERNAL (decl));
4379 int failure
4380 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4381 do_default);
4382
4383 /* Get the completed type made by complete_array_type. */
4384 type = TREE_TYPE (decl);
4385
4386 switch (failure)
4387 {
4388 case 1:
4389 error ("initializer fails to determine size of %q+D", decl);
4390 break;
4391
4392 case 2:
4393 if (do_default)
4394 error ("array size missing in %q+D", decl);
4395 /* If a `static' var's size isn't known,
4396 make it extern as well as static, so it does not get
4397 allocated.
4398 If it is not `static', then do not mark extern;
4399 finish_incomplete_decl will give it a default size
4400 and it will get allocated. */
4401 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4402 DECL_EXTERNAL (decl) = 1;
4403 break;
4404
4405 case 3:
4406 error ("zero or negative size array %q+D", decl);
4407 break;
4408
4409 case 0:
4410 /* For global variables, update the copy of the type that
4411 exists in the binding. */
4412 if (TREE_PUBLIC (decl))
4413 {
4414 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4415 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4416 b_ext = b_ext->shadowed;
4417 if (b_ext)
4418 {
4419 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4420 b_ext->u.type = composite_type (b_ext->u.type, type);
4421 else
4422 b_ext->u.type = type;
4423 }
4424 }
4425 break;
4426
4427 default:
4428 gcc_unreachable ();
4429 }
4430
4431 if (DECL_INITIAL (decl))
4432 TREE_TYPE (DECL_INITIAL (decl)) = type;
4433
4434 relayout_decl (decl);
4435 }
4436
4437 if (TREE_CODE (decl) == VAR_DECL)
4438 {
4439 if (init && TREE_CODE (init) == CONSTRUCTOR)
4440 add_flexible_array_elts_to_size (decl, init);
4441
4442 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4443 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4444 layout_decl (decl, 0);
4445
4446 if (DECL_SIZE (decl) == 0
4447 /* Don't give an error if we already gave one earlier. */
4448 && TREE_TYPE (decl) != error_mark_node
4449 && (TREE_STATIC (decl)
4450 /* A static variable with an incomplete type
4451 is an error if it is initialized.
4452 Also if it is not file scope.
4453 Otherwise, let it through, but if it is not `extern'
4454 then it may cause an error message later. */
4455 ? (DECL_INITIAL (decl) != 0
4456 || !DECL_FILE_SCOPE_P (decl))
4457 /* An automatic variable with an incomplete type
4458 is an error. */
4459 : !DECL_EXTERNAL (decl)))
4460 {
4461 error ("storage size of %q+D isn%'t known", decl);
4462 TREE_TYPE (decl) = error_mark_node;
4463 }
4464
4465 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4466 && DECL_SIZE (decl) != 0)
4467 {
4468 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4469 constant_expression_warning (DECL_SIZE (decl));
4470 else
4471 {
4472 error ("storage size of %q+D isn%'t constant", decl);
4473 TREE_TYPE (decl) = error_mark_node;
4474 }
4475 }
4476
4477 if (TREE_USED (type))
4478 {
4479 TREE_USED (decl) = 1;
4480 DECL_READ_P (decl) = 1;
4481 }
4482 }
4483
4484 /* If this is a function and an assembler name is specified, reset DECL_RTL
4485 so we can give it its new name. Also, update builtin_decl if it
4486 was a normal built-in. */
4487 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4488 {
4489 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4490 set_builtin_user_assembler_name (decl, asmspec);
4491 set_user_assembler_name (decl, asmspec);
4492 }
4493
4494 /* If #pragma weak was used, mark the decl weak now. */
4495 maybe_apply_pragma_weak (decl);
4496
4497 /* Output the assembler code and/or RTL code for variables and functions,
4498 unless the type is an undefined structure or union.
4499 If not, it will get done when the type is completed. */
4500
4501 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
4502 {
4503 /* Determine the ELF visibility. */
4504 if (TREE_PUBLIC (decl))
4505 c_determine_visibility (decl);
4506
4507 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4508 if (c_dialect_objc ())
4509 objc_check_decl (decl);
4510
4511 if (asmspec)
4512 {
4513 /* If this is not a static variable, issue a warning.
4514 It doesn't make any sense to give an ASMSPEC for an
4515 ordinary, non-register local variable. Historically,
4516 GCC has accepted -- but ignored -- the ASMSPEC in
4517 this case. */
4518 if (!DECL_FILE_SCOPE_P (decl)
4519 && TREE_CODE (decl) == VAR_DECL
4520 && !C_DECL_REGISTER (decl)
4521 && !TREE_STATIC (decl))
4522 warning (0, "ignoring asm-specifier for non-static local "
4523 "variable %q+D", decl);
4524 else
4525 set_user_assembler_name (decl, asmspec);
4526 }
4527
4528 if (DECL_FILE_SCOPE_P (decl))
4529 {
4530 if (DECL_INITIAL (decl) == NULL_TREE
4531 || DECL_INITIAL (decl) == error_mark_node)
4532 /* Don't output anything
4533 when a tentative file-scope definition is seen.
4534 But at end of compilation, do output code for them. */
4535 DECL_DEFER_OUTPUT (decl) = 1;
4536 if (asmspec && C_DECL_REGISTER (decl))
4537 DECL_HARD_REGISTER (decl) = 1;
4538 rest_of_decl_compilation (decl, true, 0);
4539 }
4540 else
4541 {
4542 /* In conjunction with an ASMSPEC, the `register'
4543 keyword indicates that we should place the variable
4544 in a particular register. */
4545 if (asmspec && C_DECL_REGISTER (decl))
4546 {
4547 DECL_HARD_REGISTER (decl) = 1;
4548 /* This cannot be done for a structure with volatile
4549 fields, on which DECL_REGISTER will have been
4550 reset. */
4551 if (!DECL_REGISTER (decl))
4552 error ("cannot put object with volatile field into register");
4553 }
4554
4555 if (TREE_CODE (decl) != FUNCTION_DECL)
4556 {
4557 /* If we're building a variable sized type, and we might be
4558 reachable other than via the top of the current binding
4559 level, then create a new BIND_EXPR so that we deallocate
4560 the object at the right time. */
4561 /* Note that DECL_SIZE can be null due to errors. */
4562 if (DECL_SIZE (decl)
4563 && !TREE_CONSTANT (DECL_SIZE (decl))
4564 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4565 {
4566 tree bind;
4567 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4568 TREE_SIDE_EFFECTS (bind) = 1;
4569 add_stmt (bind);
4570 BIND_EXPR_BODY (bind) = push_stmt_list ();
4571 }
4572 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4573 DECL_EXPR, decl));
4574 }
4575 }
4576
4577
4578 if (!DECL_FILE_SCOPE_P (decl))
4579 {
4580 /* Recompute the RTL of a local array now
4581 if it used to be an incomplete type. */
4582 if (was_incomplete
4583 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4584 {
4585 /* If we used it already as memory, it must stay in memory. */
4586 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4587 /* If it's still incomplete now, no init will save it. */
4588 if (DECL_SIZE (decl) == 0)
4589 DECL_INITIAL (decl) = 0;
4590 }
4591 }
4592 }
4593
4594 if (TREE_CODE (decl) == TYPE_DECL)
4595 {
4596 if (!DECL_FILE_SCOPE_P (decl)
4597 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4598 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4599
4600 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4601 }
4602
4603 /* Install a cleanup (aka destructor) if one was given. */
4604 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4605 {
4606 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4607 if (attr)
4608 {
4609 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4610 tree cleanup_decl = lookup_name (cleanup_id);
4611 tree cleanup;
4612 vec<tree, va_gc> *v;
4613
4614 /* Build "cleanup(&decl)" for the destructor. */
4615 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
4616 vec_alloc (v, 1);
4617 v->quick_push (cleanup);
4618 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
4619 vNULL, cleanup_decl, v, NULL);
4620 vec_free (v);
4621
4622 /* Don't warn about decl unused; the cleanup uses it. */
4623 TREE_USED (decl) = 1;
4624 TREE_USED (cleanup_decl) = 1;
4625 DECL_READ_P (decl) = 1;
4626
4627 push_cleanup (decl, cleanup, false);
4628 }
4629 }
4630
4631 if (warn_cxx_compat
4632 && TREE_CODE (decl) == VAR_DECL
4633 && !DECL_EXTERNAL (decl)
4634 && DECL_INITIAL (decl) == NULL_TREE)
4635 {
4636 type = strip_array_types (type);
4637 if (TREE_READONLY (decl))
4638 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4639 "uninitialized const %qD is invalid in C++", decl);
4640 else if ((TREE_CODE (type) == RECORD_TYPE
4641 || TREE_CODE (type) == UNION_TYPE)
4642 && C_TYPE_FIELDS_READONLY (type))
4643 diagnose_uninitialized_cst_member (decl, type);
4644 }
4645
4646 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
4647 }
4648
4649 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
4650 EXPR is NULL or a pointer to an expression that needs to be
4651 evaluated for the side effects of array size expressions in the
4652 parameters. */
4653
4654 tree
4655 grokparm (const struct c_parm *parm, tree *expr)
4656 {
4657 tree attrs = parm->attrs;
4658 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4659 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
4660
4661 decl_attributes (&decl, attrs, 0);
4662
4663 return decl;
4664 }
4665
4666 /* Given a parsed parameter declaration, decode it into a PARM_DECL
4667 and push that on the current scope. EXPR is a pointer to an
4668 expression that needs to be evaluated for the side effects of array
4669 size expressions in the parameters. */
4670
4671 void
4672 push_parm_decl (const struct c_parm *parm, tree *expr)
4673 {
4674 tree attrs = parm->attrs;
4675 tree decl;
4676
4677 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
4678 &attrs, expr, NULL, DEPRECATED_NORMAL);
4679 decl_attributes (&decl, attrs, 0);
4680
4681 decl = pushdecl (decl);
4682
4683 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
4684 }
4685
4686 /* Mark all the parameter declarations to date as forward decls.
4687 Also diagnose use of this extension. */
4688
4689 void
4690 mark_forward_parm_decls (void)
4691 {
4692 struct c_binding *b;
4693
4694 if (pedantic && !current_scope->warned_forward_parm_decls)
4695 {
4696 pedwarn (input_location, OPT_Wpedantic,
4697 "ISO C forbids forward parameter declarations");
4698 current_scope->warned_forward_parm_decls = true;
4699 }
4700
4701 for (b = current_scope->bindings; b; b = b->prev)
4702 if (TREE_CODE (b->decl) == PARM_DECL)
4703 TREE_ASM_WRITTEN (b->decl) = 1;
4704 }
4705 \f
4706 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
4707 literal, which may be an incomplete array type completed by the
4708 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
4709 literal. NON_CONST is true if the initializers contain something
4710 that cannot occur in a constant expression. */
4711
4712 tree
4713 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
4714 {
4715 /* We do not use start_decl here because we have a type, not a declarator;
4716 and do not use finish_decl because the decl should be stored inside
4717 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
4718 tree decl;
4719 tree complit;
4720 tree stmt;
4721
4722 if (type == error_mark_node
4723 || init == error_mark_node)
4724 return error_mark_node;
4725
4726 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
4727 DECL_EXTERNAL (decl) = 0;
4728 TREE_PUBLIC (decl) = 0;
4729 TREE_STATIC (decl) = (current_scope == file_scope);
4730 DECL_CONTEXT (decl) = current_function_decl;
4731 TREE_USED (decl) = 1;
4732 DECL_READ_P (decl) = 1;
4733 TREE_TYPE (decl) = type;
4734 TREE_READONLY (decl) = (TYPE_READONLY (type)
4735 || (TREE_CODE (type) == ARRAY_TYPE
4736 && TYPE_READONLY (TREE_TYPE (type))));
4737 store_init_value (loc, decl, init, NULL_TREE);
4738
4739 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
4740 {
4741 int failure = complete_array_type (&TREE_TYPE (decl),
4742 DECL_INITIAL (decl), true);
4743 /* If complete_array_type returns 3, it means that the
4744 initial value of the compound literal is empty. Allow it. */
4745 gcc_assert (failure == 0 || failure == 3);
4746
4747 type = TREE_TYPE (decl);
4748 TREE_TYPE (DECL_INITIAL (decl)) = type;
4749 }
4750
4751 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4752 {
4753 c_incomplete_type_error (NULL_TREE, type);
4754 return error_mark_node;
4755 }
4756
4757 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
4758 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
4759 TREE_SIDE_EFFECTS (complit) = 1;
4760
4761 layout_decl (decl, 0);
4762
4763 if (TREE_STATIC (decl))
4764 {
4765 /* This decl needs a name for the assembler output. */
4766 set_compound_literal_name (decl);
4767 DECL_DEFER_OUTPUT (decl) = 1;
4768 DECL_COMDAT (decl) = 1;
4769 DECL_ARTIFICIAL (decl) = 1;
4770 DECL_IGNORED_P (decl) = 1;
4771 pushdecl (decl);
4772 rest_of_decl_compilation (decl, 1, 0);
4773 }
4774
4775 if (non_const)
4776 {
4777 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
4778 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
4779 }
4780
4781 return complit;
4782 }
4783
4784 /* Check the type of a compound literal. Here we just check that it
4785 is valid for C++. */
4786
4787 void
4788 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
4789 {
4790 if (warn_cxx_compat
4791 && (type_name->specs->typespec_kind == ctsk_tagdef
4792 || type_name->specs->typespec_kind == ctsk_tagfirstref))
4793 warning_at (loc, OPT_Wc___compat,
4794 "defining a type in a compound literal is invalid in C++");
4795 }
4796 \f
4797 /* Determine whether TYPE is a structure with a flexible array member,
4798 or a union containing such a structure (possibly recursively). */
4799
4800 static bool
4801 flexible_array_type_p (tree type)
4802 {
4803 tree x;
4804 switch (TREE_CODE (type))
4805 {
4806 case RECORD_TYPE:
4807 x = TYPE_FIELDS (type);
4808 if (x == NULL_TREE)
4809 return false;
4810 while (DECL_CHAIN (x) != NULL_TREE)
4811 x = DECL_CHAIN (x);
4812 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4813 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4814 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
4815 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
4816 return true;
4817 return false;
4818 case UNION_TYPE:
4819 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
4820 {
4821 if (flexible_array_type_p (TREE_TYPE (x)))
4822 return true;
4823 }
4824 return false;
4825 default:
4826 return false;
4827 }
4828 }
4829 \f
4830 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
4831 replacing with appropriate values if they are invalid. */
4832 static void
4833 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
4834 {
4835 tree type_mv;
4836 unsigned int max_width;
4837 unsigned HOST_WIDE_INT w;
4838 const char *name = (orig_name
4839 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
4840 : _("<anonymous>"));
4841
4842 /* Detect and ignore out of range field width and process valid
4843 field widths. */
4844 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
4845 {
4846 error ("bit-field %qs width not an integer constant", name);
4847 *width = integer_one_node;
4848 }
4849 else
4850 {
4851 if (TREE_CODE (*width) != INTEGER_CST)
4852 {
4853 *width = c_fully_fold (*width, false, NULL);
4854 if (TREE_CODE (*width) == INTEGER_CST)
4855 pedwarn (input_location, OPT_Wpedantic,
4856 "bit-field %qs width not an integer constant expression",
4857 name);
4858 }
4859 if (TREE_CODE (*width) != INTEGER_CST)
4860 {
4861 error ("bit-field %qs width not an integer constant", name);
4862 *width = integer_one_node;
4863 }
4864 constant_expression_warning (*width);
4865 if (tree_int_cst_sgn (*width) < 0)
4866 {
4867 error ("negative width in bit-field %qs", name);
4868 *width = integer_one_node;
4869 }
4870 else if (integer_zerop (*width) && orig_name)
4871 {
4872 error ("zero width for bit-field %qs", name);
4873 *width = integer_one_node;
4874 }
4875 }
4876
4877 /* Detect invalid bit-field type. */
4878 if (TREE_CODE (*type) != INTEGER_TYPE
4879 && TREE_CODE (*type) != BOOLEAN_TYPE
4880 && TREE_CODE (*type) != ENUMERAL_TYPE)
4881 {
4882 error ("bit-field %qs has invalid type", name);
4883 *type = unsigned_type_node;
4884 }
4885
4886 type_mv = TYPE_MAIN_VARIANT (*type);
4887 if (!in_system_header_at (input_location)
4888 && type_mv != integer_type_node
4889 && type_mv != unsigned_type_node
4890 && type_mv != boolean_type_node)
4891 pedwarn_c90 (input_location, OPT_Wpedantic,
4892 "type of bit-field %qs is a GCC extension", name);
4893
4894 max_width = TYPE_PRECISION (*type);
4895
4896 if (0 < compare_tree_int (*width, max_width))
4897 {
4898 error ("width of %qs exceeds its type", name);
4899 w = max_width;
4900 *width = build_int_cst (integer_type_node, w);
4901 }
4902 else
4903 w = tree_to_uhwi (*width);
4904
4905 if (TREE_CODE (*type) == ENUMERAL_TYPE)
4906 {
4907 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
4908 if (!lt
4909 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
4910 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
4911 warning (0, "%qs is narrower than values of its type", name);
4912 }
4913 }
4914
4915 \f
4916
4917 /* Print warning about variable length array if necessary. */
4918
4919 static void
4920 warn_variable_length_array (tree name, tree size)
4921 {
4922 if (TREE_CONSTANT (size))
4923 {
4924 if (name)
4925 pedwarn_c90 (input_location, OPT_Wvla,
4926 "ISO C90 forbids array %qE whose size "
4927 "can%'t be evaluated", name);
4928 else
4929 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
4930 "whose size can%'t be evaluated");
4931 }
4932 else
4933 {
4934 if (name)
4935 pedwarn_c90 (input_location, OPT_Wvla,
4936 "ISO C90 forbids variable length array %qE", name);
4937 else
4938 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
4939 "length array");
4940 }
4941 }
4942
4943 /* Print warning about defaulting to int if necessary. */
4944
4945 static void
4946 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
4947 {
4948 diagnostic_info diagnostic;
4949 va_list ap;
4950
4951 va_start (ap, gmsgid);
4952 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
4953 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
4954 diagnostic.option_index = opt;
4955 report_diagnostic (&diagnostic);
4956 va_end (ap);
4957 }
4958
4959 /* Given declspecs and a declarator,
4960 determine the name and type of the object declared
4961 and construct a ..._DECL node for it.
4962 (In one case we can return a ..._TYPE node instead.
4963 For invalid input we sometimes return 0.)
4964
4965 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4966
4967 DECL_CONTEXT says which syntactic context this declaration is in:
4968 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4969 FUNCDEF for a function definition. Like NORMAL but a few different
4970 error messages in each case. Return value may be zero meaning
4971 this definition is too screwy to try to parse.
4972 PARM for a parameter declaration (either within a function prototype
4973 or before a function body). Make a PARM_DECL, or return void_type_node.
4974 TYPENAME if for a typename (in a cast or sizeof).
4975 Don't make a DECL node; just return the ..._TYPE node.
4976 FIELD for a struct or union field; make a FIELD_DECL.
4977 INITIALIZED is true if the decl has an initializer.
4978 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4979 representing the width of the bit-field.
4980 DECL_ATTRS points to the list of attributes that should be added to this
4981 decl. Any nested attributes that belong on the decl itself will be
4982 added to this list.
4983 If EXPR is not NULL, any expressions that need to be evaluated as
4984 part of evaluating variably modified types will be stored in *EXPR.
4985 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4986 set to indicate whether operands in *EXPR can be used in constant
4987 expressions.
4988 DEPRECATED_STATE is a deprecated_states value indicating whether
4989 deprecation warnings should be suppressed.
4990
4991 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4992 It may also be so in the PARM case, for a prototype where the
4993 argument type is specified but not the name.
4994
4995 This function is where the complicated C meanings of `static'
4996 and `extern' are interpreted. */
4997
4998 static tree
4999 grokdeclarator (const struct c_declarator *declarator,
5000 struct c_declspecs *declspecs,
5001 enum decl_context decl_context, bool initialized, tree *width,
5002 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5003 enum deprecated_states deprecated_state)
5004 {
5005 tree type = declspecs->type;
5006 bool threadp = declspecs->thread_p;
5007 enum c_storage_class storage_class = declspecs->storage_class;
5008 int constp;
5009 int restrictp;
5010 int volatilep;
5011 int atomicp;
5012 int type_quals = TYPE_UNQUALIFIED;
5013 tree name = NULL_TREE;
5014 bool funcdef_flag = false;
5015 bool funcdef_syntax = false;
5016 bool size_varies = false;
5017 tree decl_attr = declspecs->decl_attr;
5018 int array_ptr_quals = TYPE_UNQUALIFIED;
5019 tree array_ptr_attrs = NULL_TREE;
5020 int array_parm_static = 0;
5021 bool array_parm_vla_unspec_p = false;
5022 tree returned_attrs = NULL_TREE;
5023 bool bitfield = width != NULL;
5024 tree element_type;
5025 struct c_arg_info *arg_info = 0;
5026 addr_space_t as1, as2, address_space;
5027 location_t loc = UNKNOWN_LOCATION;
5028 const char *errmsg;
5029 tree expr_dummy;
5030 bool expr_const_operands_dummy;
5031 enum c_declarator_kind first_non_attr_kind;
5032 unsigned int alignas_align = 0;
5033
5034 if (TREE_CODE (type) == ERROR_MARK)
5035 return error_mark_node;
5036 if (expr == NULL)
5037 expr = &expr_dummy;
5038 if (expr_const_operands == NULL)
5039 expr_const_operands = &expr_const_operands_dummy;
5040
5041 *expr = declspecs->expr;
5042 *expr_const_operands = declspecs->expr_const_operands;
5043
5044 if (decl_context == FUNCDEF)
5045 funcdef_flag = true, decl_context = NORMAL;
5046
5047 /* Look inside a declarator for the name being declared
5048 and get it as an IDENTIFIER_NODE, for an error message. */
5049 {
5050 const struct c_declarator *decl = declarator;
5051
5052 first_non_attr_kind = cdk_attrs;
5053 while (decl)
5054 switch (decl->kind)
5055 {
5056 case cdk_array:
5057 loc = decl->id_loc;
5058 /* FALL THRU. */
5059
5060 case cdk_function:
5061 case cdk_pointer:
5062 funcdef_syntax = (decl->kind == cdk_function);
5063 decl = decl->declarator;
5064 if (first_non_attr_kind == cdk_attrs)
5065 first_non_attr_kind = decl->kind;
5066 break;
5067
5068 case cdk_attrs:
5069 decl = decl->declarator;
5070 break;
5071
5072 case cdk_id:
5073 loc = decl->id_loc;
5074 if (decl->u.id)
5075 name = decl->u.id;
5076 if (first_non_attr_kind == cdk_attrs)
5077 first_non_attr_kind = decl->kind;
5078 decl = 0;
5079 break;
5080
5081 default:
5082 gcc_unreachable ();
5083 }
5084 if (name == 0)
5085 {
5086 gcc_assert (decl_context == PARM
5087 || decl_context == TYPENAME
5088 || (decl_context == FIELD
5089 && declarator->kind == cdk_id));
5090 gcc_assert (!initialized);
5091 }
5092 }
5093
5094 /* A function definition's declarator must have the form of
5095 a function declarator. */
5096
5097 if (funcdef_flag && !funcdef_syntax)
5098 return 0;
5099
5100 /* If this looks like a function definition, make it one,
5101 even if it occurs where parms are expected.
5102 Then store_parm_decls will reject it and not use it as a parm. */
5103 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5104 decl_context = PARM;
5105
5106 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5107 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5108
5109 if ((decl_context == NORMAL || decl_context == FIELD)
5110 && current_scope == file_scope
5111 && variably_modified_type_p (type, NULL_TREE))
5112 {
5113 if (name)
5114 error_at (loc, "variably modified %qE at file scope", name);
5115 else
5116 error_at (loc, "variably modified field at file scope");
5117 type = integer_type_node;
5118 }
5119
5120 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5121
5122 /* Diagnose defaulting to "int". */
5123
5124 if (declspecs->default_int_p && !in_system_header_at (input_location))
5125 {
5126 /* Issue a warning if this is an ISO C 99 program or if
5127 -Wreturn-type and this is a function, or if -Wimplicit;
5128 prefer the former warning since it is more explicit. */
5129 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5130 && funcdef_flag)
5131 warn_about_return_type = 1;
5132 else
5133 {
5134 if (name)
5135 warn_defaults_to (loc, flag_isoc99 ? 0 : OPT_Wimplicit_int,
5136 "type defaults to %<int%> in declaration "
5137 "of %qE", name);
5138 else
5139 warn_defaults_to (loc, flag_isoc99 ? 0 : OPT_Wimplicit_int,
5140 "type defaults to %<int%> in type name");
5141 }
5142 }
5143
5144 /* Adjust the type if a bit-field is being declared,
5145 -funsigned-bitfields applied and the type is not explicitly
5146 "signed". */
5147 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5148 && TREE_CODE (type) == INTEGER_TYPE)
5149 type = unsigned_type_for (type);
5150
5151 /* Figure out the type qualifiers for the declaration. There are
5152 two ways a declaration can become qualified. One is something
5153 like `const int i' where the `const' is explicit. Another is
5154 something like `typedef const int CI; CI i' where the type of the
5155 declaration contains the `const'. A third possibility is that
5156 there is a type qualifier on the element type of a typedefed
5157 array type, in which case we should extract that qualifier so
5158 that c_apply_type_quals_to_decl receives the full list of
5159 qualifiers to work with (C90 is not entirely clear about whether
5160 duplicate qualifiers should be diagnosed in this case, but it
5161 seems most appropriate to do so). */
5162 element_type = strip_array_types (type);
5163 constp = declspecs->const_p + TYPE_READONLY (element_type);
5164 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5165 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5166 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5167 as1 = declspecs->address_space;
5168 as2 = TYPE_ADDR_SPACE (element_type);
5169 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5170
5171 if (constp > 1)
5172 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5173 if (restrictp > 1)
5174 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5175 if (volatilep > 1)
5176 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5177 if (atomicp > 1)
5178 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5179
5180 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5181 error_at (loc, "conflicting named address spaces (%s vs %s)",
5182 c_addr_space_name (as1), c_addr_space_name (as2));
5183
5184 if ((TREE_CODE (type) == ARRAY_TYPE
5185 || first_non_attr_kind == cdk_array)
5186 && TYPE_QUALS (element_type))
5187 type = TYPE_MAIN_VARIANT (type);
5188 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5189 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5190 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5191 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5192 | ENCODE_QUAL_ADDR_SPACE (address_space));
5193
5194 /* Applying the _Atomic qualifier to an array type (through the use
5195 of typedefs or typeof) must be detected here. If the qualifier
5196 is introduced later, any appearance of applying it to an array is
5197 actually applying it to an element of that array. */
5198 if (atomicp && TREE_CODE (type) == ARRAY_TYPE)
5199 error_at (loc, "%<_Atomic%>-qualified array type");
5200
5201 /* Warn about storage classes that are invalid for certain
5202 kinds of declarations (parameters, typenames, etc.). */
5203
5204 if (funcdef_flag
5205 && (threadp
5206 || storage_class == csc_auto
5207 || storage_class == csc_register
5208 || storage_class == csc_typedef))
5209 {
5210 if (storage_class == csc_auto)
5211 pedwarn (loc,
5212 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5213 "function definition declared %<auto%>");
5214 if (storage_class == csc_register)
5215 error_at (loc, "function definition declared %<register%>");
5216 if (storage_class == csc_typedef)
5217 error_at (loc, "function definition declared %<typedef%>");
5218 if (threadp)
5219 error_at (loc, "function definition declared %qs",
5220 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5221 threadp = false;
5222 if (storage_class == csc_auto
5223 || storage_class == csc_register
5224 || storage_class == csc_typedef)
5225 storage_class = csc_none;
5226 }
5227 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5228 {
5229 if (decl_context == PARM && storage_class == csc_register)
5230 ;
5231 else
5232 {
5233 switch (decl_context)
5234 {
5235 case FIELD:
5236 if (name)
5237 error_at (loc, "storage class specified for structure "
5238 "field %qE", name);
5239 else
5240 error_at (loc, "storage class specified for structure field");
5241 break;
5242 case PARM:
5243 if (name)
5244 error_at (loc, "storage class specified for parameter %qE",
5245 name);
5246 else
5247 error_at (loc, "storage class specified for unnamed parameter");
5248 break;
5249 default:
5250 error_at (loc, "storage class specified for typename");
5251 break;
5252 }
5253 storage_class = csc_none;
5254 threadp = false;
5255 }
5256 }
5257 else if (storage_class == csc_extern
5258 && initialized
5259 && !funcdef_flag)
5260 {
5261 /* 'extern' with initialization is invalid if not at file scope. */
5262 if (current_scope == file_scope)
5263 {
5264 /* It is fine to have 'extern const' when compiling at C
5265 and C++ intersection. */
5266 if (!(warn_cxx_compat && constp))
5267 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5268 name);
5269 }
5270 else
5271 error_at (loc, "%qE has both %<extern%> and initializer", name);
5272 }
5273 else if (current_scope == file_scope)
5274 {
5275 if (storage_class == csc_auto)
5276 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5277 name);
5278 if (pedantic && storage_class == csc_register)
5279 pedwarn (input_location, OPT_Wpedantic,
5280 "file-scope declaration of %qE specifies %<register%>", name);
5281 }
5282 else
5283 {
5284 if (storage_class == csc_extern && funcdef_flag)
5285 error_at (loc, "nested function %qE declared %<extern%>", name);
5286 else if (threadp && storage_class == csc_none)
5287 {
5288 error_at (loc, "function-scope %qE implicitly auto and declared "
5289 "%qs", name,
5290 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5291 threadp = false;
5292 }
5293 }
5294
5295 /* Now figure out the structure of the declarator proper.
5296 Descend through it, creating more complex types, until we reach
5297 the declared identifier (or NULL_TREE, in an absolute declarator).
5298 At each stage we maintain an unqualified version of the type
5299 together with any qualifiers that should be applied to it with
5300 c_build_qualified_type; this way, array types including
5301 multidimensional array types are first built up in unqualified
5302 form and then the qualified form is created with
5303 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5304
5305 while (declarator && declarator->kind != cdk_id)
5306 {
5307 if (type == error_mark_node)
5308 {
5309 declarator = declarator->declarator;
5310 continue;
5311 }
5312
5313 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5314 a cdk_pointer (for *...),
5315 a cdk_function (for ...(...)),
5316 a cdk_attrs (for nested attributes),
5317 or a cdk_id (for the name being declared
5318 or the place in an absolute declarator
5319 where the name was omitted).
5320 For the last case, we have just exited the loop.
5321
5322 At this point, TYPE is the type of elements of an array,
5323 or for a function to return, or for a pointer to point to.
5324 After this sequence of ifs, TYPE is the type of the
5325 array or function or pointer, and DECLARATOR has had its
5326 outermost layer removed. */
5327
5328 if (array_ptr_quals != TYPE_UNQUALIFIED
5329 || array_ptr_attrs != NULL_TREE
5330 || array_parm_static)
5331 {
5332 /* Only the innermost declarator (making a parameter be of
5333 array type which is converted to pointer type)
5334 may have static or type qualifiers. */
5335 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5336 array_ptr_quals = TYPE_UNQUALIFIED;
5337 array_ptr_attrs = NULL_TREE;
5338 array_parm_static = 0;
5339 }
5340
5341 switch (declarator->kind)
5342 {
5343 case cdk_attrs:
5344 {
5345 /* A declarator with embedded attributes. */
5346 tree attrs = declarator->u.attrs;
5347 const struct c_declarator *inner_decl;
5348 int attr_flags = 0;
5349 declarator = declarator->declarator;
5350 inner_decl = declarator;
5351 while (inner_decl->kind == cdk_attrs)
5352 inner_decl = inner_decl->declarator;
5353 if (inner_decl->kind == cdk_id)
5354 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5355 else if (inner_decl->kind == cdk_function)
5356 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5357 else if (inner_decl->kind == cdk_array)
5358 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5359 returned_attrs = decl_attributes (&type,
5360 chainon (returned_attrs, attrs),
5361 attr_flags);
5362 break;
5363 }
5364 case cdk_array:
5365 {
5366 tree itype = NULL_TREE;
5367 tree size = declarator->u.array.dimen;
5368 /* The index is a signed object `sizetype' bits wide. */
5369 tree index_type = c_common_signed_type (sizetype);
5370
5371 array_ptr_quals = declarator->u.array.quals;
5372 array_ptr_attrs = declarator->u.array.attrs;
5373 array_parm_static = declarator->u.array.static_p;
5374 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5375
5376 declarator = declarator->declarator;
5377
5378 /* Check for some types that there cannot be arrays of. */
5379
5380 if (VOID_TYPE_P (type))
5381 {
5382 if (name)
5383 error_at (loc, "declaration of %qE as array of voids", name);
5384 else
5385 error_at (loc, "declaration of type name as array of voids");
5386 type = error_mark_node;
5387 }
5388
5389 if (TREE_CODE (type) == FUNCTION_TYPE)
5390 {
5391 if (name)
5392 error_at (loc, "declaration of %qE as array of functions",
5393 name);
5394 else
5395 error_at (loc, "declaration of type name as array of "
5396 "functions");
5397 type = error_mark_node;
5398 }
5399
5400 if (pedantic && !in_system_header_at (input_location)
5401 && flexible_array_type_p (type))
5402 pedwarn (loc, OPT_Wpedantic,
5403 "invalid use of structure with flexible array member");
5404
5405 if (size == error_mark_node)
5406 type = error_mark_node;
5407
5408 if (type == error_mark_node)
5409 continue;
5410
5411 /* If size was specified, set ITYPE to a range-type for
5412 that size. Otherwise, ITYPE remains null. finish_decl
5413 may figure it out from an initial value. */
5414
5415 if (size)
5416 {
5417 bool size_maybe_const = true;
5418 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5419 && !TREE_OVERFLOW (size));
5420 bool this_size_varies = false;
5421
5422 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5423 lvalue. */
5424 STRIP_TYPE_NOPS (size);
5425
5426 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5427 {
5428 if (name)
5429 error_at (loc, "size of array %qE has non-integer type",
5430 name);
5431 else
5432 error_at (loc,
5433 "size of unnamed array has non-integer type");
5434 size = integer_one_node;
5435 }
5436
5437 size = c_fully_fold (size, false, &size_maybe_const);
5438
5439 if (pedantic && size_maybe_const && integer_zerop (size))
5440 {
5441 if (name)
5442 pedwarn (loc, OPT_Wpedantic,
5443 "ISO C forbids zero-size array %qE", name);
5444 else
5445 pedwarn (loc, OPT_Wpedantic,
5446 "ISO C forbids zero-size array");
5447 }
5448
5449 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5450 {
5451 constant_expression_warning (size);
5452 if (tree_int_cst_sgn (size) < 0)
5453 {
5454 if (name)
5455 error_at (loc, "size of array %qE is negative", name);
5456 else
5457 error_at (loc, "size of unnamed array is negative");
5458 size = integer_one_node;
5459 }
5460 /* Handle a size folded to an integer constant but
5461 not an integer constant expression. */
5462 if (!size_int_const)
5463 {
5464 /* If this is a file scope declaration of an
5465 ordinary identifier, this is invalid code;
5466 diagnosing it here and not subsequently
5467 treating the type as variable-length avoids
5468 more confusing diagnostics later. */
5469 if ((decl_context == NORMAL || decl_context == FIELD)
5470 && current_scope == file_scope)
5471 pedwarn (input_location, 0,
5472 "variably modified %qE at file scope",
5473 name);
5474 else
5475 this_size_varies = size_varies = true;
5476 warn_variable_length_array (name, size);
5477 }
5478 }
5479 else if ((decl_context == NORMAL || decl_context == FIELD)
5480 && current_scope == file_scope)
5481 {
5482 error_at (loc, "variably modified %qE at file scope", name);
5483 size = integer_one_node;
5484 }
5485 else
5486 {
5487 /* Make sure the array size remains visibly
5488 nonconstant even if it is (eg) a const variable
5489 with known value. */
5490 this_size_varies = size_varies = true;
5491 warn_variable_length_array (name, size);
5492 if (flag_sanitize & SANITIZE_VLA
5493 && decl_context == NORMAL
5494 && current_function_decl != NULL_TREE
5495 && !lookup_attribute ("no_sanitize_undefined",
5496 DECL_ATTRIBUTES
5497 (current_function_decl)))
5498 {
5499 /* Evaluate the array size only once. */
5500 size = c_save_expr (size);
5501 size = c_fully_fold (size, false, NULL);
5502 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
5503 ubsan_instrument_vla (loc, size),
5504 size);
5505 }
5506 }
5507
5508 if (integer_zerop (size) && !this_size_varies)
5509 {
5510 /* A zero-length array cannot be represented with
5511 an unsigned index type, which is what we'll
5512 get with build_index_type. Create an
5513 open-ended range instead. */
5514 itype = build_range_type (sizetype, size, NULL_TREE);
5515 }
5516 else
5517 {
5518 /* Arrange for the SAVE_EXPR on the inside of the
5519 MINUS_EXPR, which allows the -1 to get folded
5520 with the +1 that happens when building TYPE_SIZE. */
5521 if (size_varies)
5522 size = save_expr (size);
5523 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5524 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5525 integer_zero_node, size);
5526
5527 /* Compute the maximum valid index, that is, size
5528 - 1. Do the calculation in index_type, so that
5529 if it is a variable the computations will be
5530 done in the proper mode. */
5531 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5532 convert (index_type, size),
5533 convert (index_type,
5534 size_one_node));
5535
5536 /* The above overflows when size does not fit
5537 in index_type.
5538 ??? While a size of INT_MAX+1 technically shouldn't
5539 cause an overflow (because we subtract 1), handling
5540 this case seems like an unnecessary complication. */
5541 if (TREE_CODE (size) == INTEGER_CST
5542 && !int_fits_type_p (size, index_type))
5543 {
5544 if (name)
5545 error_at (loc, "size of array %qE is too large",
5546 name);
5547 else
5548 error_at (loc, "size of unnamed array is too large");
5549 type = error_mark_node;
5550 continue;
5551 }
5552
5553 itype = build_index_type (itype);
5554 }
5555 if (this_size_varies)
5556 {
5557 if (*expr)
5558 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5559 *expr, size);
5560 else
5561 *expr = size;
5562 *expr_const_operands &= size_maybe_const;
5563 }
5564 }
5565 else if (decl_context == FIELD)
5566 {
5567 bool flexible_array_member = false;
5568 if (array_parm_vla_unspec_p)
5569 /* Field names can in fact have function prototype
5570 scope so [*] is disallowed here through making
5571 the field variably modified, not through being
5572 something other than a declaration with function
5573 prototype scope. */
5574 size_varies = true;
5575 else
5576 {
5577 const struct c_declarator *t = declarator;
5578 while (t->kind == cdk_attrs)
5579 t = t->declarator;
5580 flexible_array_member = (t->kind == cdk_id);
5581 }
5582 if (flexible_array_member
5583 && !in_system_header_at (input_location))
5584 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
5585 "support flexible array members");
5586
5587 /* ISO C99 Flexible array members are effectively
5588 identical to GCC's zero-length array extension. */
5589 if (flexible_array_member || array_parm_vla_unspec_p)
5590 itype = build_range_type (sizetype, size_zero_node,
5591 NULL_TREE);
5592 }
5593 else if (decl_context == PARM)
5594 {
5595 if (array_parm_vla_unspec_p)
5596 {
5597 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
5598 size_varies = true;
5599 }
5600 }
5601 else if (decl_context == TYPENAME)
5602 {
5603 if (array_parm_vla_unspec_p)
5604 {
5605 /* C99 6.7.5.2p4 */
5606 warning (0, "%<[*]%> not in a declaration");
5607 /* We use this to avoid messing up with incomplete
5608 array types of the same type, that would
5609 otherwise be modified below. */
5610 itype = build_range_type (sizetype, size_zero_node,
5611 NULL_TREE);
5612 size_varies = true;
5613 }
5614 }
5615
5616 /* Complain about arrays of incomplete types. */
5617 if (!COMPLETE_TYPE_P (type))
5618 {
5619 error_at (loc, "array type has incomplete element type");
5620 type = error_mark_node;
5621 }
5622 else
5623 /* When itype is NULL, a shared incomplete array type is
5624 returned for all array of a given type. Elsewhere we
5625 make sure we don't complete that type before copying
5626 it, but here we want to make sure we don't ever
5627 modify the shared type, so we gcc_assert (itype)
5628 below. */
5629 {
5630 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
5631 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
5632 type = build_qualified_type (type,
5633 ENCODE_QUAL_ADDR_SPACE (as));
5634
5635 type = build_array_type (type, itype);
5636 }
5637
5638 if (type != error_mark_node)
5639 {
5640 if (size_varies)
5641 {
5642 /* It is ok to modify type here even if itype is
5643 NULL: if size_varies, we're in a
5644 multi-dimensional array and the inner type has
5645 variable size, so the enclosing shared array type
5646 must too. */
5647 if (size && TREE_CODE (size) == INTEGER_CST)
5648 type
5649 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5650 C_TYPE_VARIABLE_SIZE (type) = 1;
5651 }
5652
5653 /* The GCC extension for zero-length arrays differs from
5654 ISO flexible array members in that sizeof yields
5655 zero. */
5656 if (size && integer_zerop (size))
5657 {
5658 gcc_assert (itype);
5659 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5660 TYPE_SIZE (type) = bitsize_zero_node;
5661 TYPE_SIZE_UNIT (type) = size_zero_node;
5662 SET_TYPE_STRUCTURAL_EQUALITY (type);
5663 }
5664 if (array_parm_vla_unspec_p)
5665 {
5666 gcc_assert (itype);
5667 /* The type is complete. C99 6.7.5.2p4 */
5668 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5669 TYPE_SIZE (type) = bitsize_zero_node;
5670 TYPE_SIZE_UNIT (type) = size_zero_node;
5671 SET_TYPE_STRUCTURAL_EQUALITY (type);
5672 }
5673 }
5674
5675 if (decl_context != PARM
5676 && (array_ptr_quals != TYPE_UNQUALIFIED
5677 || array_ptr_attrs != NULL_TREE
5678 || array_parm_static))
5679 {
5680 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5681 array_ptr_quals = TYPE_UNQUALIFIED;
5682 array_ptr_attrs = NULL_TREE;
5683 array_parm_static = 0;
5684 }
5685 break;
5686 }
5687 case cdk_function:
5688 {
5689 /* Say it's a definition only for the declarator closest
5690 to the identifier, apart possibly from some
5691 attributes. */
5692 bool really_funcdef = false;
5693 tree arg_types;
5694 if (funcdef_flag)
5695 {
5696 const struct c_declarator *t = declarator->declarator;
5697 while (t->kind == cdk_attrs)
5698 t = t->declarator;
5699 really_funcdef = (t->kind == cdk_id);
5700 }
5701
5702 /* Declaring a function type. Make sure we have a valid
5703 type for the function to return. */
5704 if (type == error_mark_node)
5705 continue;
5706
5707 size_varies = false;
5708
5709 /* Warn about some types functions can't return. */
5710 if (TREE_CODE (type) == FUNCTION_TYPE)
5711 {
5712 if (name)
5713 error_at (loc, "%qE declared as function returning a "
5714 "function", name);
5715 else
5716 error_at (loc, "type name declared as function "
5717 "returning a function");
5718 type = integer_type_node;
5719 }
5720 if (TREE_CODE (type) == ARRAY_TYPE)
5721 {
5722 if (name)
5723 error_at (loc, "%qE declared as function returning an array",
5724 name);
5725 else
5726 error_at (loc, "type name declared as function returning "
5727 "an array");
5728 type = integer_type_node;
5729 }
5730 errmsg = targetm.invalid_return_type (type);
5731 if (errmsg)
5732 {
5733 error (errmsg);
5734 type = integer_type_node;
5735 }
5736
5737 /* Construct the function type and go to the next
5738 inner layer of declarator. */
5739 arg_info = declarator->u.arg_info;
5740 arg_types = grokparms (arg_info, really_funcdef);
5741
5742 /* Type qualifiers before the return type of the function
5743 qualify the return type, not the function type. */
5744 if (type_quals)
5745 {
5746 /* Type qualifiers on a function return type are
5747 normally permitted by the standard but have no
5748 effect, so give a warning at -Wreturn-type.
5749 Qualifiers on a void return type are banned on
5750 function definitions in ISO C; GCC used to used
5751 them for noreturn functions. */
5752 if (VOID_TYPE_P (type) && really_funcdef)
5753 pedwarn (loc, 0,
5754 "function definition has qualified void return type");
5755 else
5756 warning_at (loc, OPT_Wignored_qualifiers,
5757 "type qualifiers ignored on function return type");
5758
5759 type = c_build_qualified_type (type, type_quals);
5760 }
5761 type_quals = TYPE_UNQUALIFIED;
5762
5763 type = build_function_type (type, arg_types);
5764 declarator = declarator->declarator;
5765
5766 /* Set the TYPE_CONTEXTs for each tagged type which is local to
5767 the formal parameter list of this FUNCTION_TYPE to point to
5768 the FUNCTION_TYPE node itself. */
5769 {
5770 c_arg_tag *tag;
5771 unsigned ix;
5772
5773 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
5774 TYPE_CONTEXT (tag->type) = type;
5775 }
5776 break;
5777 }
5778 case cdk_pointer:
5779 {
5780 /* Merge any constancy or volatility into the target type
5781 for the pointer. */
5782 if ((type_quals & TYPE_QUAL_ATOMIC)
5783 && TREE_CODE (type) == FUNCTION_TYPE)
5784 {
5785 error_at (loc,
5786 "%<_Atomic%>-qualified function type");
5787 type_quals &= ~TYPE_QUAL_ATOMIC;
5788 }
5789 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5790 && type_quals)
5791 pedwarn (loc, OPT_Wpedantic,
5792 "ISO C forbids qualified function types");
5793 if (type_quals)
5794 type = c_build_qualified_type (type, type_quals);
5795 size_varies = false;
5796
5797 /* When the pointed-to type involves components of variable size,
5798 care must be taken to ensure that the size evaluation code is
5799 emitted early enough to dominate all the possible later uses
5800 and late enough for the variables on which it depends to have
5801 been assigned.
5802
5803 This is expected to happen automatically when the pointed-to
5804 type has a name/declaration of it's own, but special attention
5805 is required if the type is anonymous.
5806
5807 We handle the NORMAL and FIELD contexts here by attaching an
5808 artificial TYPE_DECL to such pointed-to type. This forces the
5809 sizes evaluation at a safe point and ensures it is not deferred
5810 until e.g. within a deeper conditional context.
5811
5812 We expect nothing to be needed here for PARM or TYPENAME.
5813 Pushing a TYPE_DECL at this point for TYPENAME would actually
5814 be incorrect, as we might be in the middle of an expression
5815 with side effects on the pointed-to type size "arguments" prior
5816 to the pointer declaration point and the fake TYPE_DECL in the
5817 enclosing context would force the size evaluation prior to the
5818 side effects. */
5819
5820 if (!TYPE_NAME (type)
5821 && (decl_context == NORMAL || decl_context == FIELD)
5822 && variably_modified_type_p (type, NULL_TREE))
5823 {
5824 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
5825 DECL_ARTIFICIAL (decl) = 1;
5826 pushdecl (decl);
5827 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
5828 TYPE_NAME (type) = decl;
5829 }
5830
5831 type = c_build_pointer_type (type);
5832
5833 /* Process type qualifiers (such as const or volatile)
5834 that were given inside the `*'. */
5835 type_quals = declarator->u.pointer_quals;
5836
5837 declarator = declarator->declarator;
5838 break;
5839 }
5840 default:
5841 gcc_unreachable ();
5842 }
5843 }
5844 *decl_attrs = chainon (returned_attrs, *decl_attrs);
5845
5846 /* Now TYPE has the actual type, apart from any qualifiers in
5847 TYPE_QUALS. */
5848
5849 /* Warn about address space used for things other than static memory or
5850 pointers. */
5851 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
5852 if (!ADDR_SPACE_GENERIC_P (address_space))
5853 {
5854 if (decl_context == NORMAL)
5855 {
5856 switch (storage_class)
5857 {
5858 case csc_auto:
5859 error ("%qs combined with %<auto%> qualifier for %qE",
5860 c_addr_space_name (address_space), name);
5861 break;
5862 case csc_register:
5863 error ("%qs combined with %<register%> qualifier for %qE",
5864 c_addr_space_name (address_space), name);
5865 break;
5866 case csc_none:
5867 if (current_function_scope)
5868 {
5869 error ("%qs specified for auto variable %qE",
5870 c_addr_space_name (address_space), name);
5871 break;
5872 }
5873 break;
5874 case csc_static:
5875 case csc_extern:
5876 case csc_typedef:
5877 break;
5878 default:
5879 gcc_unreachable ();
5880 }
5881 }
5882 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
5883 {
5884 if (name)
5885 error ("%qs specified for parameter %qE",
5886 c_addr_space_name (address_space), name);
5887 else
5888 error ("%qs specified for unnamed parameter",
5889 c_addr_space_name (address_space));
5890 }
5891 else if (decl_context == FIELD)
5892 {
5893 if (name)
5894 error ("%qs specified for structure field %qE",
5895 c_addr_space_name (address_space), name);
5896 else
5897 error ("%qs specified for structure field",
5898 c_addr_space_name (address_space));
5899 }
5900 }
5901
5902 /* Check the type and width of a bit-field. */
5903 if (bitfield)
5904 {
5905 check_bitfield_type_and_width (&type, width, name);
5906 /* C11 makes it implementation-defined (6.7.2.1#5) whether
5907 atomic types are permitted for bit-fields; we have no code to
5908 make bit-field accesses atomic, so disallow them. */
5909 if (type_quals & TYPE_QUAL_ATOMIC)
5910 {
5911 if (name)
5912 error ("bit-field %qE has atomic type", name);
5913 else
5914 error ("bit-field has atomic type");
5915 type_quals &= ~TYPE_QUAL_ATOMIC;
5916 }
5917 }
5918
5919 /* Reject invalid uses of _Alignas. */
5920 if (declspecs->alignas_p)
5921 {
5922 if (storage_class == csc_typedef)
5923 error_at (loc, "alignment specified for typedef %qE", name);
5924 else if (storage_class == csc_register)
5925 error_at (loc, "alignment specified for %<register%> object %qE",
5926 name);
5927 else if (decl_context == PARM)
5928 {
5929 if (name)
5930 error_at (loc, "alignment specified for parameter %qE", name);
5931 else
5932 error_at (loc, "alignment specified for unnamed parameter");
5933 }
5934 else if (bitfield)
5935 {
5936 if (name)
5937 error_at (loc, "alignment specified for bit-field %qE", name);
5938 else
5939 error_at (loc, "alignment specified for unnamed bit-field");
5940 }
5941 else if (TREE_CODE (type) == FUNCTION_TYPE)
5942 error_at (loc, "alignment specified for function %qE", name);
5943 else if (declspecs->align_log != -1)
5944 {
5945 alignas_align = 1U << declspecs->align_log;
5946 if (alignas_align < min_align_of_type (type))
5947 {
5948 if (name)
5949 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
5950 "alignment of %qE", name);
5951 else
5952 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
5953 "alignment of unnamed field");
5954 alignas_align = 0;
5955 }
5956 }
5957 }
5958
5959 /* Did array size calculations overflow or does the array cover more
5960 than half of the address-space? */
5961 if (TREE_CODE (type) == ARRAY_TYPE
5962 && COMPLETE_TYPE_P (type)
5963 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
5964 && ! valid_constant_size_p (TYPE_SIZE_UNIT (type)))
5965 {
5966 if (name)
5967 error_at (loc, "size of array %qE is too large", name);
5968 else
5969 error_at (loc, "size of unnamed array is too large");
5970 /* If we proceed with the array type as it is, we'll eventually
5971 crash in tree_to_[su]hwi(). */
5972 type = error_mark_node;
5973 }
5974
5975 /* If this is declaring a typedef name, return a TYPE_DECL. */
5976
5977 if (storage_class == csc_typedef)
5978 {
5979 tree decl;
5980 if ((type_quals & TYPE_QUAL_ATOMIC)
5981 && TREE_CODE (type) == FUNCTION_TYPE)
5982 {
5983 error_at (loc,
5984 "%<_Atomic%>-qualified function type");
5985 type_quals &= ~TYPE_QUAL_ATOMIC;
5986 }
5987 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5988 && type_quals)
5989 pedwarn (loc, OPT_Wpedantic,
5990 "ISO C forbids qualified function types");
5991 if (type_quals)
5992 type = c_build_qualified_type (type, type_quals);
5993 decl = build_decl (declarator->id_loc,
5994 TYPE_DECL, declarator->u.id, type);
5995 if (declspecs->explicit_signed_p)
5996 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
5997 if (declspecs->inline_p)
5998 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
5999 if (declspecs->noreturn_p)
6000 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6001
6002 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6003 {
6004 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6005
6006 if (b != NULL
6007 && b->decl != NULL_TREE
6008 && (B_IN_CURRENT_SCOPE (b)
6009 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6010 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6011 {
6012 warning_at (declarator->id_loc, OPT_Wc___compat,
6013 ("using %qD as both a typedef and a tag is "
6014 "invalid in C++"),
6015 decl);
6016 if (b->locus != UNKNOWN_LOCATION)
6017 inform (b->locus, "originally defined here");
6018 }
6019 }
6020
6021 return decl;
6022 }
6023
6024 /* If this is a type name (such as, in a cast or sizeof),
6025 compute the type and return it now. */
6026
6027 if (decl_context == TYPENAME)
6028 {
6029 /* Note that the grammar rejects storage classes in typenames
6030 and fields. */
6031 gcc_assert (storage_class == csc_none && !threadp
6032 && !declspecs->inline_p && !declspecs->noreturn_p);
6033 if ((type_quals & TYPE_QUAL_ATOMIC)
6034 && TREE_CODE (type) == FUNCTION_TYPE)
6035 {
6036 error_at (loc,
6037 "%<_Atomic%>-qualified function type");
6038 type_quals &= ~TYPE_QUAL_ATOMIC;
6039 }
6040 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6041 && type_quals)
6042 pedwarn (loc, OPT_Wpedantic,
6043 "ISO C forbids const or volatile function types");
6044 if (type_quals)
6045 type = c_build_qualified_type (type, type_quals);
6046 return type;
6047 }
6048
6049 if (pedantic && decl_context == FIELD
6050 && variably_modified_type_p (type, NULL_TREE))
6051 {
6052 /* C99 6.7.2.1p8 */
6053 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6054 "have a variably modified type");
6055 }
6056
6057 /* Aside from typedefs and type names (handle above),
6058 `void' at top level (not within pointer)
6059 is allowed only in public variables.
6060 We don't complain about parms either, but that is because
6061 a better error message can be made later. */
6062
6063 if (VOID_TYPE_P (type) && decl_context != PARM
6064 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6065 && (storage_class == csc_extern
6066 || (current_scope == file_scope
6067 && !(storage_class == csc_static
6068 || storage_class == csc_register)))))
6069 {
6070 error_at (loc, "variable or field %qE declared void", name);
6071 type = integer_type_node;
6072 }
6073
6074 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6075 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6076
6077 {
6078 tree decl;
6079
6080 if (decl_context == PARM)
6081 {
6082 tree promoted_type;
6083 bool array_parameter_p = false;
6084
6085 /* A parameter declared as an array of T is really a pointer to T.
6086 One declared as a function is really a pointer to a function. */
6087
6088 if (TREE_CODE (type) == ARRAY_TYPE)
6089 {
6090 /* Transfer const-ness of array into that of type pointed to. */
6091 type = TREE_TYPE (type);
6092 if (type_quals)
6093 type = c_build_qualified_type (type, type_quals);
6094 type = c_build_pointer_type (type);
6095 type_quals = array_ptr_quals;
6096 if (type_quals)
6097 type = c_build_qualified_type (type, type_quals);
6098
6099 /* We don't yet implement attributes in this context. */
6100 if (array_ptr_attrs != NULL_TREE)
6101 warning_at (loc, OPT_Wattributes,
6102 "attributes in parameter array declarator ignored");
6103
6104 size_varies = false;
6105 array_parameter_p = true;
6106 }
6107 else if (TREE_CODE (type) == FUNCTION_TYPE)
6108 {
6109 if (type_quals & TYPE_QUAL_ATOMIC)
6110 {
6111 error_at (loc,
6112 "%<_Atomic%>-qualified function type");
6113 type_quals &= ~TYPE_QUAL_ATOMIC;
6114 }
6115 else if (type_quals)
6116 pedwarn (loc, OPT_Wpedantic,
6117 "ISO C forbids qualified function types");
6118 if (type_quals)
6119 type = c_build_qualified_type (type, type_quals);
6120 type = c_build_pointer_type (type);
6121 type_quals = TYPE_UNQUALIFIED;
6122 }
6123 else if (type_quals)
6124 type = c_build_qualified_type (type, type_quals);
6125
6126 decl = build_decl (declarator->id_loc,
6127 PARM_DECL, declarator->u.id, type);
6128 if (size_varies)
6129 C_DECL_VARIABLE_SIZE (decl) = 1;
6130 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6131
6132 /* Compute the type actually passed in the parmlist,
6133 for the case where there is no prototype.
6134 (For example, shorts and chars are passed as ints.)
6135 When there is a prototype, this is overridden later. */
6136
6137 if (type == error_mark_node)
6138 promoted_type = type;
6139 else
6140 promoted_type = c_type_promotes_to (type);
6141
6142 DECL_ARG_TYPE (decl) = promoted_type;
6143 if (declspecs->inline_p)
6144 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6145 if (declspecs->noreturn_p)
6146 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6147 }
6148 else if (decl_context == FIELD)
6149 {
6150 /* Note that the grammar rejects storage classes in typenames
6151 and fields. */
6152 gcc_assert (storage_class == csc_none && !threadp
6153 && !declspecs->inline_p && !declspecs->noreturn_p);
6154
6155 /* Structure field. It may not be a function. */
6156
6157 if (TREE_CODE (type) == FUNCTION_TYPE)
6158 {
6159 error_at (loc, "field %qE declared as a function", name);
6160 type = build_pointer_type (type);
6161 }
6162 else if (TREE_CODE (type) != ERROR_MARK
6163 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6164 {
6165 if (name)
6166 error_at (loc, "field %qE has incomplete type", name);
6167 else
6168 error_at (loc, "unnamed field has incomplete type");
6169 type = error_mark_node;
6170 }
6171 type = c_build_qualified_type (type, type_quals);
6172 decl = build_decl (declarator->id_loc,
6173 FIELD_DECL, declarator->u.id, type);
6174 DECL_NONADDRESSABLE_P (decl) = bitfield;
6175 if (bitfield && !declarator->u.id)
6176 TREE_NO_WARNING (decl) = 1;
6177
6178 if (size_varies)
6179 C_DECL_VARIABLE_SIZE (decl) = 1;
6180 }
6181 else if (TREE_CODE (type) == FUNCTION_TYPE)
6182 {
6183 if (storage_class == csc_register || threadp)
6184 {
6185 error_at (loc, "invalid storage class for function %qE", name);
6186 }
6187 else if (current_scope != file_scope)
6188 {
6189 /* Function declaration not at file scope. Storage
6190 classes other than `extern' are not allowed, C99
6191 6.7.1p5, and `extern' makes no difference. However,
6192 GCC allows 'auto', perhaps with 'inline', to support
6193 nested functions. */
6194 if (storage_class == csc_auto)
6195 pedwarn (loc, OPT_Wpedantic,
6196 "invalid storage class for function %qE", name);
6197 else if (storage_class == csc_static)
6198 {
6199 error_at (loc, "invalid storage class for function %qE", name);
6200 if (funcdef_flag)
6201 storage_class = declspecs->storage_class = csc_none;
6202 else
6203 return 0;
6204 }
6205 }
6206
6207 decl = build_decl (declarator->id_loc,
6208 FUNCTION_DECL, declarator->u.id, type);
6209 decl = build_decl_attribute_variant (decl, decl_attr);
6210
6211 if (type_quals & TYPE_QUAL_ATOMIC)
6212 {
6213 error_at (loc,
6214 "%<_Atomic%>-qualified function type");
6215 type_quals &= ~TYPE_QUAL_ATOMIC;
6216 }
6217 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6218 pedwarn (loc, OPT_Wpedantic,
6219 "ISO C forbids qualified function types");
6220
6221 /* Every function declaration is an external reference
6222 (DECL_EXTERNAL) except for those which are not at file
6223 scope and are explicitly declared "auto". This is
6224 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6225 GCC to signify a forward declaration of a nested function. */
6226 if (storage_class == csc_auto && current_scope != file_scope)
6227 DECL_EXTERNAL (decl) = 0;
6228 /* In C99, a function which is declared 'inline' with 'extern'
6229 is not an external reference (which is confusing). It
6230 means that the later definition of the function must be output
6231 in this file, C99 6.7.4p6. In GNU C89, a function declared
6232 'extern inline' is an external reference. */
6233 else if (declspecs->inline_p && storage_class != csc_static)
6234 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6235 == flag_gnu89_inline);
6236 else
6237 DECL_EXTERNAL (decl) = !initialized;
6238
6239 /* Record absence of global scope for `static' or `auto'. */
6240 TREE_PUBLIC (decl)
6241 = !(storage_class == csc_static || storage_class == csc_auto);
6242
6243 /* For a function definition, record the argument information
6244 block where store_parm_decls will look for it. */
6245 if (funcdef_flag)
6246 current_function_arg_info = arg_info;
6247
6248 if (declspecs->default_int_p)
6249 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6250
6251 /* Record presence of `inline' and `_Noreturn', if it is
6252 reasonable. */
6253 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6254 {
6255 if (declspecs->inline_p)
6256 pedwarn (loc, 0, "cannot inline function %<main%>");
6257 if (declspecs->noreturn_p)
6258 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6259 }
6260 else
6261 {
6262 if (declspecs->inline_p)
6263 /* Record that the function is declared `inline'. */
6264 DECL_DECLARED_INLINE_P (decl) = 1;
6265 if (declspecs->noreturn_p)
6266 {
6267 if (flag_isoc99)
6268 pedwarn_c99 (loc, OPT_Wpedantic,
6269 "ISO C99 does not support %<_Noreturn%>");
6270 else
6271 pedwarn_c99 (loc, OPT_Wpedantic,
6272 "ISO C90 does not support %<_Noreturn%>");
6273 TREE_THIS_VOLATILE (decl) = 1;
6274 }
6275 }
6276 }
6277 else
6278 {
6279 /* It's a variable. */
6280 /* An uninitialized decl with `extern' is a reference. */
6281 int extern_ref = !initialized && storage_class == csc_extern;
6282
6283 type = c_build_qualified_type (type, type_quals);
6284
6285 /* C99 6.2.2p7: It is invalid (compile-time undefined
6286 behavior) to create an 'extern' declaration for a
6287 variable if there is a global declaration that is
6288 'static' and the global declaration is not visible.
6289 (If the static declaration _is_ currently visible,
6290 the 'extern' declaration is taken to refer to that decl.) */
6291 if (extern_ref && current_scope != file_scope)
6292 {
6293 tree global_decl = identifier_global_value (declarator->u.id);
6294 tree visible_decl = lookup_name (declarator->u.id);
6295
6296 if (global_decl
6297 && global_decl != visible_decl
6298 && TREE_CODE (global_decl) == VAR_DECL
6299 && !TREE_PUBLIC (global_decl))
6300 error_at (loc, "variable previously declared %<static%> "
6301 "redeclared %<extern%>");
6302 }
6303
6304 decl = build_decl (declarator->id_loc,
6305 VAR_DECL, declarator->u.id, type);
6306 if (size_varies)
6307 C_DECL_VARIABLE_SIZE (decl) = 1;
6308
6309 if (declspecs->inline_p)
6310 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6311 if (declspecs->noreturn_p)
6312 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6313
6314 /* At file scope, an initialized extern declaration may follow
6315 a static declaration. In that case, DECL_EXTERNAL will be
6316 reset later in start_decl. */
6317 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6318
6319 /* At file scope, the presence of a `static' or `register' storage
6320 class specifier, or the absence of all storage class specifiers
6321 makes this declaration a definition (perhaps tentative). Also,
6322 the absence of `static' makes it public. */
6323 if (current_scope == file_scope)
6324 {
6325 TREE_PUBLIC (decl) = storage_class != csc_static;
6326 TREE_STATIC (decl) = !extern_ref;
6327 }
6328 /* Not at file scope, only `static' makes a static definition. */
6329 else
6330 {
6331 TREE_STATIC (decl) = (storage_class == csc_static);
6332 TREE_PUBLIC (decl) = extern_ref;
6333 }
6334
6335 if (threadp)
6336 set_decl_tls_model (decl, decl_default_tls_model (decl));
6337 }
6338
6339 if ((storage_class == csc_extern
6340 || (storage_class == csc_none
6341 && TREE_CODE (type) == FUNCTION_TYPE
6342 && !funcdef_flag))
6343 && variably_modified_type_p (type, NULL_TREE))
6344 {
6345 /* C99 6.7.5.2p2 */
6346 if (TREE_CODE (type) == FUNCTION_TYPE)
6347 error_at (loc, "non-nested function with variably modified type");
6348 else
6349 error_at (loc, "object with variably modified type must have "
6350 "no linkage");
6351 }
6352
6353 /* Record `register' declaration for warnings on &
6354 and in case doing stupid register allocation. */
6355
6356 if (storage_class == csc_register)
6357 {
6358 C_DECL_REGISTER (decl) = 1;
6359 DECL_REGISTER (decl) = 1;
6360 }
6361
6362 /* Record constancy and volatility. */
6363 c_apply_type_quals_to_decl (type_quals, decl);
6364
6365 /* Apply _Alignas specifiers. */
6366 if (alignas_align)
6367 {
6368 DECL_ALIGN (decl) = alignas_align * BITS_PER_UNIT;
6369 DECL_USER_ALIGN (decl) = 1;
6370 }
6371
6372 /* If a type has volatile components, it should be stored in memory.
6373 Otherwise, the fact that those components are volatile
6374 will be ignored, and would even crash the compiler.
6375 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6376 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6377 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
6378 || TREE_CODE (decl) == RESULT_DECL))
6379 {
6380 /* It is not an error for a structure with volatile fields to
6381 be declared register, but reset DECL_REGISTER since it
6382 cannot actually go in a register. */
6383 int was_reg = C_DECL_REGISTER (decl);
6384 C_DECL_REGISTER (decl) = 0;
6385 DECL_REGISTER (decl) = 0;
6386 c_mark_addressable (decl);
6387 C_DECL_REGISTER (decl) = was_reg;
6388 }
6389
6390 /* This is the earliest point at which we might know the assembler
6391 name of a variable. Thus, if it's known before this, die horribly. */
6392 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6393
6394 if (warn_cxx_compat
6395 && TREE_CODE (decl) == VAR_DECL
6396 && TREE_PUBLIC (decl)
6397 && TREE_STATIC (decl)
6398 && (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6399 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6400 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6401 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6402 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6403 ("non-local variable %qD with anonymous type is "
6404 "questionable in C++"),
6405 decl);
6406
6407 return decl;
6408 }
6409 }
6410 \f
6411 /* Decode the parameter-list info for a function type or function definition.
6412 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6413 if there is an identifier list instead of a parameter decl list).
6414 These two functions are separate because when a function returns
6415 or receives functions then each is called multiple times but the order
6416 of calls is different. The last call to `grokparms' is always the one
6417 that contains the formal parameter names of a function definition.
6418
6419 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6420
6421 FUNCDEF_FLAG is true for a function definition, false for
6422 a mere declaration. A nonempty identifier-list gets an error message
6423 when FUNCDEF_FLAG is false. */
6424
6425 static tree
6426 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6427 {
6428 tree arg_types = arg_info->types;
6429
6430 if (funcdef_flag && arg_info->had_vla_unspec)
6431 {
6432 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6433 /* C99 6.7.5.2p4 */
6434 error ("%<[*]%> not allowed in other than function prototype scope");
6435 }
6436
6437 if (arg_types == 0 && !funcdef_flag
6438 && !in_system_header_at (input_location))
6439 warning (OPT_Wstrict_prototypes,
6440 "function declaration isn%'t a prototype");
6441
6442 if (arg_types == error_mark_node)
6443 return 0; /* don't set TYPE_ARG_TYPES in this case */
6444
6445 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6446 {
6447 if (!funcdef_flag)
6448 {
6449 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6450 arg_info->parms = NULL_TREE;
6451 }
6452 else
6453 arg_info->parms = arg_info->types;
6454
6455 arg_info->types = 0;
6456 return 0;
6457 }
6458 else
6459 {
6460 tree parm, type, typelt;
6461 unsigned int parmno;
6462 const char *errmsg;
6463
6464 /* If there is a parameter of incomplete type in a definition,
6465 this is an error. In a declaration this is valid, and a
6466 struct or union type may be completed later, before any calls
6467 or definition of the function. In the case where the tag was
6468 first declared within the parameter list, a warning has
6469 already been given. If a parameter has void type, then
6470 however the function cannot be defined or called, so
6471 warn. */
6472
6473 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
6474 parm;
6475 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
6476 {
6477 type = TREE_VALUE (typelt);
6478 if (type == error_mark_node)
6479 continue;
6480
6481 if (!COMPLETE_TYPE_P (type))
6482 {
6483 if (funcdef_flag)
6484 {
6485 if (DECL_NAME (parm))
6486 error_at (input_location,
6487 "parameter %u (%q+D) has incomplete type",
6488 parmno, parm);
6489 else
6490 error_at (DECL_SOURCE_LOCATION (parm),
6491 "parameter %u has incomplete type",
6492 parmno);
6493
6494 TREE_VALUE (typelt) = error_mark_node;
6495 TREE_TYPE (parm) = error_mark_node;
6496 arg_types = NULL_TREE;
6497 }
6498 else if (VOID_TYPE_P (type))
6499 {
6500 if (DECL_NAME (parm))
6501 warning_at (input_location, 0,
6502 "parameter %u (%q+D) has void type",
6503 parmno, parm);
6504 else
6505 warning_at (DECL_SOURCE_LOCATION (parm), 0,
6506 "parameter %u has void type",
6507 parmno);
6508 }
6509 }
6510
6511 errmsg = targetm.invalid_parameter_type (type);
6512 if (errmsg)
6513 {
6514 error (errmsg);
6515 TREE_VALUE (typelt) = error_mark_node;
6516 TREE_TYPE (parm) = error_mark_node;
6517 arg_types = NULL_TREE;
6518 }
6519
6520 if (DECL_NAME (parm) && TREE_USED (parm))
6521 warn_if_shadowing (parm);
6522 }
6523 return arg_types;
6524 }
6525 }
6526
6527 /* Allocate and initialize a c_arg_info structure from the parser's
6528 obstack. */
6529
6530 struct c_arg_info *
6531 build_arg_info (void)
6532 {
6533 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
6534 ret->parms = NULL_TREE;
6535 ret->tags = NULL;
6536 ret->types = NULL_TREE;
6537 ret->others = NULL_TREE;
6538 ret->pending_sizes = NULL;
6539 ret->had_vla_unspec = 0;
6540 return ret;
6541 }
6542
6543 /* Take apart the current scope and return a c_arg_info structure with
6544 info on a parameter list just parsed.
6545
6546 This structure is later fed to 'grokparms' and 'store_parm_decls'.
6547
6548 ELLIPSIS being true means the argument list ended in '...' so don't
6549 append a sentinel (void_list_node) to the end of the type-list.
6550
6551 EXPR is NULL or an expression that needs to be evaluated for the
6552 side effects of array size expressions in the parameters. */
6553
6554 struct c_arg_info *
6555 get_parm_info (bool ellipsis, tree expr)
6556 {
6557 struct c_binding *b = current_scope->bindings;
6558 struct c_arg_info *arg_info = build_arg_info ();
6559
6560 tree parms = 0;
6561 vec<c_arg_tag, va_gc> *tags = NULL;
6562 tree types = 0;
6563 tree others = 0;
6564
6565 static bool explained_incomplete_types = false;
6566 bool gave_void_only_once_err = false;
6567
6568 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
6569
6570 /* The bindings in this scope must not get put into a block.
6571 We will take care of deleting the binding nodes. */
6572 current_scope->bindings = 0;
6573
6574 /* This function is only called if there was *something* on the
6575 parameter list. */
6576 gcc_assert (b);
6577
6578 /* A parameter list consisting solely of 'void' indicates that the
6579 function takes no arguments. But if the 'void' is qualified
6580 (by 'const' or 'volatile'), or has a storage class specifier
6581 ('register'), then the behavior is undefined; issue an error.
6582 Typedefs for 'void' are OK (see DR#157). */
6583 if (b->prev == 0 /* one binding */
6584 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
6585 && !DECL_NAME (b->decl) /* anonymous */
6586 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
6587 {
6588 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
6589 || C_DECL_REGISTER (b->decl))
6590 error ("%<void%> as only parameter may not be qualified");
6591
6592 /* There cannot be an ellipsis. */
6593 if (ellipsis)
6594 error ("%<void%> must be the only parameter");
6595
6596 arg_info->types = void_list_node;
6597 return arg_info;
6598 }
6599
6600 if (!ellipsis)
6601 types = void_list_node;
6602
6603 /* Break up the bindings list into parms, tags, types, and others;
6604 apply sanity checks; purge the name-to-decl bindings. */
6605 while (b)
6606 {
6607 tree decl = b->decl;
6608 tree type = TREE_TYPE (decl);
6609 c_arg_tag tag;
6610 const char *keyword;
6611
6612 switch (TREE_CODE (decl))
6613 {
6614 case PARM_DECL:
6615 if (b->id)
6616 {
6617 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6618 I_SYMBOL_BINDING (b->id) = b->shadowed;
6619 }
6620
6621 /* Check for forward decls that never got their actual decl. */
6622 if (TREE_ASM_WRITTEN (decl))
6623 error ("parameter %q+D has just a forward declaration", decl);
6624 /* Check for (..., void, ...) and issue an error. */
6625 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
6626 {
6627 if (!gave_void_only_once_err)
6628 {
6629 error ("%<void%> must be the only parameter");
6630 gave_void_only_once_err = true;
6631 }
6632 }
6633 else
6634 {
6635 /* Valid parameter, add it to the list. */
6636 DECL_CHAIN (decl) = parms;
6637 parms = decl;
6638
6639 /* Since there is a prototype, args are passed in their
6640 declared types. The back end may override this later. */
6641 DECL_ARG_TYPE (decl) = type;
6642 types = tree_cons (0, type, types);
6643 }
6644 break;
6645
6646 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
6647 case UNION_TYPE: keyword = "union"; goto tag;
6648 case RECORD_TYPE: keyword = "struct"; goto tag;
6649 tag:
6650 /* Types may not have tag-names, in which case the type
6651 appears in the bindings list with b->id NULL. */
6652 if (b->id)
6653 {
6654 gcc_assert (I_TAG_BINDING (b->id) == b);
6655 I_TAG_BINDING (b->id) = b->shadowed;
6656 }
6657
6658 /* Warn about any struct, union or enum tags defined in a
6659 parameter list. The scope of such types is limited to
6660 the parameter list, which is rarely if ever desirable
6661 (it's impossible to call such a function with type-
6662 correct arguments). An anonymous union parm type is
6663 meaningful as a GNU extension, so don't warn for that. */
6664 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
6665 {
6666 if (b->id)
6667 /* The %s will be one of 'struct', 'union', or 'enum'. */
6668 warning (0, "%<%s %E%> declared inside parameter list",
6669 keyword, b->id);
6670 else
6671 /* The %s will be one of 'struct', 'union', or 'enum'. */
6672 warning (0, "anonymous %s declared inside parameter list",
6673 keyword);
6674
6675 if (!explained_incomplete_types)
6676 {
6677 warning (0, "its scope is only this definition or declaration,"
6678 " which is probably not what you want");
6679 explained_incomplete_types = true;
6680 }
6681 }
6682
6683 tag.id = b->id;
6684 tag.type = decl;
6685 vec_safe_push (tags, tag);
6686 break;
6687
6688 case CONST_DECL:
6689 case TYPE_DECL:
6690 case FUNCTION_DECL:
6691 /* CONST_DECLs appear here when we have an embedded enum,
6692 and TYPE_DECLs appear here when we have an embedded struct
6693 or union. No warnings for this - we already warned about the
6694 type itself. FUNCTION_DECLs appear when there is an implicit
6695 function declaration in the parameter list. */
6696
6697 /* When we reinsert this decl in the function body, we need
6698 to reconstruct whether it was marked as nested. */
6699 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6700 ? b->nested
6701 : !b->nested);
6702 DECL_CHAIN (decl) = others;
6703 others = decl;
6704 /* fall through */
6705
6706 case ERROR_MARK:
6707 /* error_mark_node appears here when we have an undeclared
6708 variable. Just throw it away. */
6709 if (b->id)
6710 {
6711 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6712 I_SYMBOL_BINDING (b->id) = b->shadowed;
6713 }
6714 break;
6715
6716 /* Other things that might be encountered. */
6717 case LABEL_DECL:
6718 case VAR_DECL:
6719 default:
6720 gcc_unreachable ();
6721 }
6722
6723 b = free_binding_and_advance (b);
6724 }
6725
6726 arg_info->parms = parms;
6727 arg_info->tags = tags;
6728 arg_info->types = types;
6729 arg_info->others = others;
6730 arg_info->pending_sizes = expr;
6731 return arg_info;
6732 }
6733 \f
6734 /* Get the struct, enum or union (CODE says which) with tag NAME.
6735 Define the tag as a forward-reference with location LOC if it is
6736 not defined. Return a c_typespec structure for the type
6737 specifier. */
6738
6739 struct c_typespec
6740 parser_xref_tag (location_t loc, enum tree_code code, tree name)
6741 {
6742 struct c_typespec ret;
6743 tree ref;
6744 location_t refloc;
6745
6746 ret.expr = NULL_TREE;
6747 ret.expr_const_operands = true;
6748
6749 /* If a cross reference is requested, look up the type
6750 already defined for this tag and return it. */
6751
6752 ref = lookup_tag (code, name, 0, &refloc);
6753 /* If this is the right type of tag, return what we found.
6754 (This reference will be shadowed by shadow_tag later if appropriate.)
6755 If this is the wrong type of tag, do not return it. If it was the
6756 wrong type in the same scope, we will have had an error
6757 message already; if in a different scope and declaring
6758 a name, pending_xref_error will give an error message; but if in a
6759 different scope and not declaring a name, this tag should
6760 shadow the previous declaration of a different type of tag, and
6761 this would not work properly if we return the reference found.
6762 (For example, with "struct foo" in an outer scope, "union foo;"
6763 must shadow that tag with a new one of union type.) */
6764 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
6765 if (ref && TREE_CODE (ref) == code)
6766 {
6767 if (C_TYPE_DEFINED_IN_STRUCT (ref)
6768 && loc != UNKNOWN_LOCATION
6769 && warn_cxx_compat)
6770 {
6771 switch (code)
6772 {
6773 case ENUMERAL_TYPE:
6774 warning_at (loc, OPT_Wc___compat,
6775 ("enum type defined in struct or union "
6776 "is not visible in C++"));
6777 inform (refloc, "enum type defined here");
6778 break;
6779 case RECORD_TYPE:
6780 warning_at (loc, OPT_Wc___compat,
6781 ("struct defined in struct or union "
6782 "is not visible in C++"));
6783 inform (refloc, "struct defined here");
6784 break;
6785 case UNION_TYPE:
6786 warning_at (loc, OPT_Wc___compat,
6787 ("union defined in struct or union "
6788 "is not visible in C++"));
6789 inform (refloc, "union defined here");
6790 break;
6791 default:
6792 gcc_unreachable();
6793 }
6794 }
6795
6796 ret.spec = ref;
6797 return ret;
6798 }
6799
6800 /* If no such tag is yet defined, create a forward-reference node
6801 and record it as the "definition".
6802 When a real declaration of this type is found,
6803 the forward-reference will be altered into a real type. */
6804
6805 ref = make_node (code);
6806 if (code == ENUMERAL_TYPE)
6807 {
6808 /* Give the type a default layout like unsigned int
6809 to avoid crashing if it does not get defined. */
6810 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
6811 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
6812 TYPE_USER_ALIGN (ref) = 0;
6813 TYPE_UNSIGNED (ref) = 1;
6814 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
6815 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
6816 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
6817 }
6818
6819 pushtag (loc, name, ref);
6820
6821 ret.spec = ref;
6822 return ret;
6823 }
6824
6825 /* Get the struct, enum or union (CODE says which) with tag NAME.
6826 Define the tag as a forward-reference if it is not defined.
6827 Return a tree for the type. */
6828
6829 tree
6830 xref_tag (enum tree_code code, tree name)
6831 {
6832 return parser_xref_tag (input_location, code, name).spec;
6833 }
6834 \f
6835 /* Make sure that the tag NAME is defined *in the current scope*
6836 at least as a forward reference.
6837 LOC is the location of the struct's definition.
6838 CODE says which kind of tag NAME ought to be.
6839
6840 This stores the current value of the file static STRUCT_PARSE_INFO
6841 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
6842 new c_struct_parse_info structure. The old value of
6843 STRUCT_PARSE_INFO is restored in finish_struct. */
6844
6845 tree
6846 start_struct (location_t loc, enum tree_code code, tree name,
6847 struct c_struct_parse_info **enclosing_struct_parse_info)
6848 {
6849 /* If there is already a tag defined at this scope
6850 (as a forward reference), just return it. */
6851
6852 tree ref = NULL_TREE;
6853 location_t refloc = UNKNOWN_LOCATION;
6854
6855 if (name != NULL_TREE)
6856 ref = lookup_tag (code, name, 1, &refloc);
6857 if (ref && TREE_CODE (ref) == code)
6858 {
6859 if (TYPE_SIZE (ref))
6860 {
6861 if (code == UNION_TYPE)
6862 error_at (loc, "redefinition of %<union %E%>", name);
6863 else
6864 error_at (loc, "redefinition of %<struct %E%>", name);
6865 if (refloc != UNKNOWN_LOCATION)
6866 inform (refloc, "originally defined here");
6867 /* Don't create structures using a name already in use. */
6868 ref = NULL_TREE;
6869 }
6870 else if (C_TYPE_BEING_DEFINED (ref))
6871 {
6872 if (code == UNION_TYPE)
6873 error_at (loc, "nested redefinition of %<union %E%>", name);
6874 else
6875 error_at (loc, "nested redefinition of %<struct %E%>", name);
6876 /* Don't bother to report "originally defined here" for a
6877 nested redefinition; the original definition should be
6878 obvious. */
6879 /* Don't create structures that contain themselves. */
6880 ref = NULL_TREE;
6881 }
6882 }
6883
6884 /* Otherwise create a forward-reference just so the tag is in scope. */
6885
6886 if (ref == NULL_TREE || TREE_CODE (ref) != code)
6887 {
6888 ref = make_node (code);
6889 pushtag (loc, name, ref);
6890 }
6891
6892 C_TYPE_BEING_DEFINED (ref) = 1;
6893 TYPE_PACKED (ref) = flag_pack_struct;
6894
6895 *enclosing_struct_parse_info = struct_parse_info;
6896 struct_parse_info = XNEW (struct c_struct_parse_info);
6897 struct_parse_info->struct_types.create (0);
6898 struct_parse_info->fields.create (0);
6899 struct_parse_info->typedefs_seen.create (0);
6900
6901 /* FIXME: This will issue a warning for a use of a type defined
6902 within a statement expr used within sizeof, et. al. This is not
6903 terribly serious as C++ doesn't permit statement exprs within
6904 sizeof anyhow. */
6905 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
6906 warning_at (loc, OPT_Wc___compat,
6907 "defining type in %qs expression is invalid in C++",
6908 (in_sizeof
6909 ? "sizeof"
6910 : (in_typeof ? "typeof" : "alignof")));
6911
6912 return ref;
6913 }
6914
6915 /* Process the specs, declarator and width (NULL if omitted)
6916 of a structure component, returning a FIELD_DECL node.
6917 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
6918 DECL_ATTRS is as for grokdeclarator.
6919
6920 LOC is the location of the structure component.
6921
6922 This is done during the parsing of the struct declaration.
6923 The FIELD_DECL nodes are chained together and the lot of them
6924 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
6925
6926 tree
6927 grokfield (location_t loc,
6928 struct c_declarator *declarator, struct c_declspecs *declspecs,
6929 tree width, tree *decl_attrs)
6930 {
6931 tree value;
6932
6933 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
6934 && width == NULL_TREE)
6935 {
6936 /* This is an unnamed decl.
6937
6938 If we have something of the form "union { list } ;" then this
6939 is the anonymous union extension. Similarly for struct.
6940
6941 If this is something of the form "struct foo;", then
6942 If MS or Plan 9 extensions are enabled, this is handled as
6943 an anonymous struct.
6944 Otherwise this is a forward declaration of a structure tag.
6945
6946 If this is something of the form "foo;" and foo is a TYPE_DECL, then
6947 If foo names a structure or union without a tag, then this
6948 is an anonymous struct (this is permitted by C11).
6949 If MS or Plan 9 extensions are enabled and foo names a
6950 structure, then again this is an anonymous struct.
6951 Otherwise this is an error.
6952
6953 Oh what a horrid tangled web we weave. I wonder if MS consciously
6954 took this from Plan 9 or if it was an accident of implementation
6955 that took root before someone noticed the bug... */
6956
6957 tree type = declspecs->type;
6958 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
6959 || TREE_CODE (type) == UNION_TYPE);
6960 bool ok = false;
6961
6962 if (type_ok
6963 && (flag_ms_extensions
6964 || flag_plan9_extensions
6965 || !declspecs->typedef_p))
6966 {
6967 if (flag_ms_extensions || flag_plan9_extensions)
6968 ok = true;
6969 else if (TYPE_NAME (type) == NULL)
6970 ok = true;
6971 else
6972 ok = false;
6973 }
6974 if (!ok)
6975 {
6976 pedwarn (loc, 0, "declaration does not declare anything");
6977 return NULL_TREE;
6978 }
6979 if (flag_isoc99)
6980 pedwarn_c99 (loc, OPT_Wpedantic,
6981 "ISO C99 doesn%'t support unnamed structs/unions");
6982 else
6983 pedwarn_c99 (loc, OPT_Wpedantic,
6984 "ISO C90 doesn%'t support unnamed structs/unions");
6985 }
6986
6987 value = grokdeclarator (declarator, declspecs, FIELD, false,
6988 width ? &width : NULL, decl_attrs, NULL, NULL,
6989 DEPRECATED_NORMAL);
6990
6991 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6992 DECL_INITIAL (value) = width;
6993
6994 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
6995 {
6996 /* If we currently have a binding for this field, set the
6997 in_struct field in the binding, so that we warn about lookups
6998 which find it. */
6999 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7000 if (b != NULL)
7001 {
7002 /* If the in_struct field is not yet set, push it on a list
7003 to be cleared when this struct is finished. */
7004 if (!b->in_struct)
7005 {
7006 struct_parse_info->fields.safe_push (b);
7007 b->in_struct = 1;
7008 }
7009 }
7010 }
7011
7012 return value;
7013 }
7014 \f
7015 /* Subroutine of detect_field_duplicates: return whether X and Y,
7016 which are both fields in the same struct, have duplicate field
7017 names. */
7018
7019 static bool
7020 is_duplicate_field (tree x, tree y)
7021 {
7022 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7023 return true;
7024
7025 /* When using -fplan9-extensions, an anonymous field whose name is a
7026 typedef can duplicate a field name. */
7027 if (flag_plan9_extensions
7028 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7029 {
7030 tree xt, xn, yt, yn;
7031
7032 xt = TREE_TYPE (x);
7033 if (DECL_NAME (x) != NULL_TREE)
7034 xn = DECL_NAME (x);
7035 else if ((TREE_CODE (xt) == RECORD_TYPE || TREE_CODE (xt) == UNION_TYPE)
7036 && TYPE_NAME (xt) != NULL_TREE
7037 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7038 xn = DECL_NAME (TYPE_NAME (xt));
7039 else
7040 xn = NULL_TREE;
7041
7042 yt = TREE_TYPE (y);
7043 if (DECL_NAME (y) != NULL_TREE)
7044 yn = DECL_NAME (y);
7045 else if ((TREE_CODE (yt) == RECORD_TYPE || TREE_CODE (yt) == UNION_TYPE)
7046 && TYPE_NAME (yt) != NULL_TREE
7047 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7048 yn = DECL_NAME (TYPE_NAME (yt));
7049 else
7050 yn = NULL_TREE;
7051
7052 if (xn != NULL_TREE && xn == yn)
7053 return true;
7054 }
7055
7056 return false;
7057 }
7058
7059 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7060 to HTAB, giving errors for any duplicates. */
7061
7062 static void
7063 detect_field_duplicates_hash (tree fieldlist,
7064 hash_table<pointer_hash <tree_node> > *htab)
7065 {
7066 tree x, y;
7067 tree_node **slot;
7068
7069 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7070 if ((y = DECL_NAME (x)) != 0)
7071 {
7072 slot = htab->find_slot (y, INSERT);
7073 if (*slot)
7074 {
7075 error ("duplicate member %q+D", x);
7076 DECL_NAME (x) = NULL_TREE;
7077 }
7078 *slot = y;
7079 }
7080 else if (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7081 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7082 {
7083 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7084
7085 /* When using -fplan9-extensions, an anonymous field whose
7086 name is a typedef can duplicate a field name. */
7087 if (flag_plan9_extensions
7088 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7089 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7090 {
7091 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7092 slot = htab->find_slot (xn, INSERT);
7093 if (*slot)
7094 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7095 *slot = xn;
7096 }
7097 }
7098 }
7099
7100 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7101 the list such that this does not present a problem later. */
7102
7103 static void
7104 detect_field_duplicates (tree fieldlist)
7105 {
7106 tree x, y;
7107 int timeout = 10;
7108
7109 /* If the struct is the list of instance variables of an Objective-C
7110 class, then we need to check all the instance variables of
7111 superclasses when checking for duplicates (since you can't have
7112 an instance variable in a subclass with the same name as an
7113 instance variable in a superclass). We pass on this job to the
7114 Objective-C compiler. objc_detect_field_duplicates() will return
7115 false if we are not checking the list of instance variables and
7116 the C frontend should proceed with the standard field duplicate
7117 checks. If we are checking the list of instance variables, the
7118 ObjC frontend will do the check, emit the errors if needed, and
7119 then return true. */
7120 if (c_dialect_objc ())
7121 if (objc_detect_field_duplicates (false))
7122 return;
7123
7124 /* First, see if there are more than "a few" fields.
7125 This is trivially true if there are zero or one fields. */
7126 if (!fieldlist || !DECL_CHAIN (fieldlist))
7127 return;
7128 x = fieldlist;
7129 do {
7130 timeout--;
7131 if (DECL_NAME (x) == NULL_TREE
7132 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7133 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7134 timeout = 0;
7135 x = DECL_CHAIN (x);
7136 } while (timeout > 0 && x);
7137
7138 /* If there were "few" fields and no anonymous structures or unions,
7139 avoid the overhead of allocating a hash table. Instead just do
7140 the nested traversal thing. */
7141 if (timeout > 0)
7142 {
7143 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7144 /* When using -fplan9-extensions, we can have duplicates
7145 between typedef names and fields. */
7146 if (DECL_NAME (x)
7147 || (flag_plan9_extensions
7148 && DECL_NAME (x) == NULL_TREE
7149 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7150 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7151 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7152 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7153 {
7154 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7155 if (is_duplicate_field (y, x))
7156 {
7157 error ("duplicate member %q+D", x);
7158 DECL_NAME (x) = NULL_TREE;
7159 }
7160 }
7161 }
7162 else
7163 {
7164 hash_table<pointer_hash <tree_node> > htab (37);
7165 detect_field_duplicates_hash (fieldlist, &htab);
7166 }
7167 }
7168
7169 /* Finish up struct info used by -Wc++-compat. */
7170
7171 static void
7172 warn_cxx_compat_finish_struct (tree fieldlist)
7173 {
7174 unsigned int ix;
7175 tree x;
7176 struct c_binding *b;
7177
7178 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7179 the current struct. We do this now at the end of the struct
7180 because the flag is used to issue visibility warnings, and we
7181 only want to issue those warnings if the type is referenced
7182 outside of the struct declaration. */
7183 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7184 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7185
7186 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7187 typedefs used when declaring fields in this struct. If the name
7188 of any of the fields is also a typedef name then the struct would
7189 not parse in C++, because the C++ lookup rules say that the
7190 typedef name would be looked up in the context of the struct, and
7191 would thus be the field rather than the typedef. */
7192 if (!struct_parse_info->typedefs_seen.is_empty ()
7193 && fieldlist != NULL_TREE)
7194 {
7195 /* Use a hash_set<tree> using the name of the typedef. We can use
7196 a hash_set<tree> because identifiers are interned. */
7197 hash_set<tree> tset;
7198
7199 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7200 tset.add (DECL_NAME (x));
7201
7202 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7203 {
7204 if (DECL_NAME (x) != NULL_TREE
7205 && tset.contains (DECL_NAME (x)))
7206 {
7207 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7208 ("using %qD as both field and typedef name is "
7209 "invalid in C++"),
7210 x);
7211 /* FIXME: It would be nice to report the location where
7212 the typedef name is used. */
7213 }
7214 }
7215 }
7216
7217 /* For each field which has a binding and which was not defined in
7218 an enclosing struct, clear the in_struct field. */
7219 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7220 b->in_struct = 0;
7221 }
7222
7223 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7224 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7225 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7226 ATTRIBUTES are attributes to be applied to the structure.
7227
7228 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7229 the struct was started. */
7230
7231 tree
7232 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7233 struct c_struct_parse_info *enclosing_struct_parse_info)
7234 {
7235 tree x;
7236 bool toplevel = file_scope == current_scope;
7237 int saw_named_field;
7238
7239 /* If this type was previously laid out as a forward reference,
7240 make sure we lay it out again. */
7241
7242 TYPE_SIZE (t) = 0;
7243
7244 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7245
7246 if (pedantic)
7247 {
7248 for (x = fieldlist; x; x = DECL_CHAIN (x))
7249 {
7250 if (DECL_NAME (x) != 0)
7251 break;
7252 if (flag_isoc11
7253 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7254 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7255 break;
7256 }
7257
7258 if (x == 0)
7259 {
7260 if (TREE_CODE (t) == UNION_TYPE)
7261 {
7262 if (fieldlist)
7263 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7264 else
7265 pedwarn (loc, OPT_Wpedantic, "union has no members");
7266 }
7267 else
7268 {
7269 if (fieldlist)
7270 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7271 else
7272 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7273 }
7274 }
7275 }
7276
7277 /* Install struct as DECL_CONTEXT of each field decl.
7278 Also process specified field sizes, found in the DECL_INITIAL,
7279 storing 0 there after the type has been changed to precision equal
7280 to its width, rather than the precision of the specified standard
7281 type. (Correct layout requires the original type to have been preserved
7282 until now.) */
7283
7284 saw_named_field = 0;
7285 for (x = fieldlist; x; x = DECL_CHAIN (x))
7286 {
7287 if (TREE_TYPE (x) == error_mark_node)
7288 continue;
7289
7290 DECL_CONTEXT (x) = t;
7291
7292 /* If any field is const, the structure type is pseudo-const. */
7293 if (TREE_READONLY (x))
7294 C_TYPE_FIELDS_READONLY (t) = 1;
7295 else
7296 {
7297 /* A field that is pseudo-const makes the structure likewise. */
7298 tree t1 = strip_array_types (TREE_TYPE (x));
7299 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
7300 && C_TYPE_FIELDS_READONLY (t1))
7301 C_TYPE_FIELDS_READONLY (t) = 1;
7302 }
7303
7304 /* Any field that is volatile means variables of this type must be
7305 treated in some ways as volatile. */
7306 if (TREE_THIS_VOLATILE (x))
7307 C_TYPE_FIELDS_VOLATILE (t) = 1;
7308
7309 /* Any field of nominal variable size implies structure is too. */
7310 if (C_DECL_VARIABLE_SIZE (x))
7311 C_TYPE_VARIABLE_SIZE (t) = 1;
7312
7313 if (DECL_INITIAL (x))
7314 {
7315 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
7316 DECL_SIZE (x) = bitsize_int (width);
7317 DECL_BIT_FIELD (x) = 1;
7318 SET_DECL_C_BIT_FIELD (x);
7319 }
7320
7321 if (TYPE_PACKED (t)
7322 && (DECL_BIT_FIELD (x)
7323 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7324 DECL_PACKED (x) = 1;
7325
7326 /* Detect flexible array member in an invalid context. */
7327 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7328 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7329 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7330 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7331 {
7332 if (TREE_CODE (t) == UNION_TYPE)
7333 {
7334 error_at (DECL_SOURCE_LOCATION (x),
7335 "flexible array member in union");
7336 TREE_TYPE (x) = error_mark_node;
7337 }
7338 else if (DECL_CHAIN (x) != NULL_TREE)
7339 {
7340 error_at (DECL_SOURCE_LOCATION (x),
7341 "flexible array member not at end of struct");
7342 TREE_TYPE (x) = error_mark_node;
7343 }
7344 else if (!saw_named_field)
7345 {
7346 error_at (DECL_SOURCE_LOCATION (x),
7347 "flexible array member in otherwise empty struct");
7348 TREE_TYPE (x) = error_mark_node;
7349 }
7350 }
7351
7352 if (pedantic && TREE_CODE (t) == RECORD_TYPE
7353 && flexible_array_type_p (TREE_TYPE (x)))
7354 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
7355 "invalid use of structure with flexible array member");
7356
7357 if (DECL_NAME (x)
7358 || TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7359 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7360 saw_named_field = 1;
7361 }
7362
7363 detect_field_duplicates (fieldlist);
7364
7365 /* Now we have the nearly final fieldlist. Record it,
7366 then lay out the structure or union (including the fields). */
7367
7368 TYPE_FIELDS (t) = fieldlist;
7369
7370 layout_type (t);
7371
7372 if (TYPE_SIZE_UNIT (t)
7373 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7374 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7375 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7376 error ("type %qT is too large", t);
7377
7378 /* Give bit-fields their proper types. */
7379 {
7380 tree *fieldlistp = &fieldlist;
7381 while (*fieldlistp)
7382 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
7383 && TREE_TYPE (*fieldlistp) != error_mark_node)
7384 {
7385 unsigned HOST_WIDE_INT width
7386 = tree_to_uhwi (DECL_INITIAL (*fieldlistp));
7387 tree type = TREE_TYPE (*fieldlistp);
7388 if (width != TYPE_PRECISION (type))
7389 {
7390 TREE_TYPE (*fieldlistp)
7391 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
7392 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
7393 }
7394 DECL_INITIAL (*fieldlistp) = 0;
7395 }
7396 else
7397 fieldlistp = &DECL_CHAIN (*fieldlistp);
7398 }
7399
7400 /* Now we have the truly final field list.
7401 Store it in this type and in the variants. */
7402
7403 TYPE_FIELDS (t) = fieldlist;
7404
7405 /* If there are lots of fields, sort so we can look through them fast.
7406 We arbitrarily consider 16 or more elts to be "a lot". */
7407
7408 {
7409 int len = 0;
7410
7411 for (x = fieldlist; x; x = DECL_CHAIN (x))
7412 {
7413 if (len > 15 || DECL_NAME (x) == NULL)
7414 break;
7415 len += 1;
7416 }
7417
7418 if (len > 15)
7419 {
7420 tree *field_array;
7421 struct lang_type *space;
7422 struct sorted_fields_type *space2;
7423
7424 len += list_length (x);
7425
7426 /* Use the same allocation policy here that make_node uses, to
7427 ensure that this lives as long as the rest of the struct decl.
7428 All decls in an inline function need to be saved. */
7429
7430 space = ggc_cleared_alloc<struct lang_type> ();
7431 space2 = (sorted_fields_type *) ggc_internal_alloc
7432 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
7433
7434 len = 0;
7435 space->s = space2;
7436 field_array = &space2->elts[0];
7437 for (x = fieldlist; x; x = DECL_CHAIN (x))
7438 {
7439 field_array[len++] = x;
7440
7441 /* If there is anonymous struct or union, break out of the loop. */
7442 if (DECL_NAME (x) == NULL)
7443 break;
7444 }
7445 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
7446 if (x == NULL)
7447 {
7448 TYPE_LANG_SPECIFIC (t) = space;
7449 TYPE_LANG_SPECIFIC (t)->s->len = len;
7450 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
7451 qsort (field_array, len, sizeof (tree), field_decl_cmp);
7452 }
7453 }
7454 }
7455
7456 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7457 {
7458 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7459 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
7460 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
7461 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
7462 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
7463 }
7464
7465 /* If this was supposed to be a transparent union, but we can't
7466 make it one, warn and turn off the flag. */
7467 if (TREE_CODE (t) == UNION_TYPE
7468 && TYPE_TRANSPARENT_AGGR (t)
7469 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
7470 {
7471 TYPE_TRANSPARENT_AGGR (t) = 0;
7472 warning_at (loc, 0, "union cannot be made transparent");
7473 }
7474
7475 /* If this structure or union completes the type of any previous
7476 variable declaration, lay it out and output its rtl. */
7477 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
7478 x;
7479 x = TREE_CHAIN (x))
7480 {
7481 tree decl = TREE_VALUE (x);
7482 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7483 layout_array_type (TREE_TYPE (decl));
7484 if (TREE_CODE (decl) != TYPE_DECL)
7485 {
7486 layout_decl (decl, 0);
7487 if (c_dialect_objc ())
7488 objc_check_decl (decl);
7489 rest_of_decl_compilation (decl, toplevel, 0);
7490 }
7491 }
7492 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
7493
7494 /* Update type location to the one of the definition, instead of e.g.
7495 a forward declaration. */
7496 if (TYPE_STUB_DECL (t))
7497 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
7498
7499 /* Finish debugging output for this type. */
7500 rest_of_type_compilation (t, toplevel);
7501
7502 /* If we're inside a function proper, i.e. not file-scope and not still
7503 parsing parameters, then arrange for the size of a variable sized type
7504 to be bound now. */
7505 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
7506 add_stmt (build_stmt (loc,
7507 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
7508
7509 if (warn_cxx_compat)
7510 warn_cxx_compat_finish_struct (fieldlist);
7511
7512 struct_parse_info->struct_types.release ();
7513 struct_parse_info->fields.release ();
7514 struct_parse_info->typedefs_seen.release ();
7515 XDELETE (struct_parse_info);
7516
7517 struct_parse_info = enclosing_struct_parse_info;
7518
7519 /* If this struct is defined inside a struct, add it to
7520 struct_types. */
7521 if (warn_cxx_compat
7522 && struct_parse_info != NULL
7523 && !in_sizeof && !in_typeof && !in_alignof)
7524 struct_parse_info->struct_types.safe_push (t);
7525
7526 return t;
7527 }
7528
7529 /* Lay out the type T, and its element type, and so on. */
7530
7531 static void
7532 layout_array_type (tree t)
7533 {
7534 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7535 layout_array_type (TREE_TYPE (t));
7536 layout_type (t);
7537 }
7538 \f
7539 /* Begin compiling the definition of an enumeration type.
7540 NAME is its name (or null if anonymous).
7541 LOC is the enum's location.
7542 Returns the type object, as yet incomplete.
7543 Also records info about it so that build_enumerator
7544 may be used to declare the individual values as they are read. */
7545
7546 tree
7547 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
7548 {
7549 tree enumtype = NULL_TREE;
7550 location_t enumloc = UNKNOWN_LOCATION;
7551
7552 /* If this is the real definition for a previous forward reference,
7553 fill in the contents in the same object that used to be the
7554 forward reference. */
7555
7556 if (name != NULL_TREE)
7557 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
7558
7559 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
7560 {
7561 enumtype = make_node (ENUMERAL_TYPE);
7562 pushtag (loc, name, enumtype);
7563 }
7564
7565 if (C_TYPE_BEING_DEFINED (enumtype))
7566 error_at (loc, "nested redefinition of %<enum %E%>", name);
7567
7568 C_TYPE_BEING_DEFINED (enumtype) = 1;
7569
7570 if (TYPE_VALUES (enumtype) != 0)
7571 {
7572 /* This enum is a named one that has been declared already. */
7573 error_at (loc, "redeclaration of %<enum %E%>", name);
7574 if (enumloc != UNKNOWN_LOCATION)
7575 inform (enumloc, "originally defined here");
7576
7577 /* Completely replace its old definition.
7578 The old enumerators remain defined, however. */
7579 TYPE_VALUES (enumtype) = 0;
7580 }
7581
7582 the_enum->enum_next_value = integer_zero_node;
7583 the_enum->enum_overflow = 0;
7584
7585 if (flag_short_enums)
7586 TYPE_PACKED (enumtype) = 1;
7587
7588 /* FIXME: This will issue a warning for a use of a type defined
7589 within sizeof in a statement expr. This is not terribly serious
7590 as C++ doesn't permit statement exprs within sizeof anyhow. */
7591 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7592 warning_at (loc, OPT_Wc___compat,
7593 "defining type in %qs expression is invalid in C++",
7594 (in_sizeof
7595 ? "sizeof"
7596 : (in_typeof ? "typeof" : "alignof")));
7597
7598 return enumtype;
7599 }
7600
7601 /* After processing and defining all the values of an enumeration type,
7602 install their decls in the enumeration type and finish it off.
7603 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
7604 and ATTRIBUTES are the specified attributes.
7605 Returns ENUMTYPE. */
7606
7607 tree
7608 finish_enum (tree enumtype, tree values, tree attributes)
7609 {
7610 tree pair, tem;
7611 tree minnode = 0, maxnode = 0;
7612 int precision;
7613 signop sign;
7614 bool toplevel = (file_scope == current_scope);
7615 struct lang_type *lt;
7616
7617 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7618
7619 /* Calculate the maximum value of any enumerator in this type. */
7620
7621 if (values == error_mark_node)
7622 minnode = maxnode = integer_zero_node;
7623 else
7624 {
7625 minnode = maxnode = TREE_VALUE (values);
7626 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
7627 {
7628 tree value = TREE_VALUE (pair);
7629 if (tree_int_cst_lt (maxnode, value))
7630 maxnode = value;
7631 if (tree_int_cst_lt (value, minnode))
7632 minnode = value;
7633 }
7634 }
7635
7636 /* Construct the final type of this enumeration. It is the same
7637 as one of the integral types - the narrowest one that fits, except
7638 that normally we only go as narrow as int - and signed iff any of
7639 the values are negative. */
7640 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
7641 precision = MAX (tree_int_cst_min_precision (minnode, sign),
7642 tree_int_cst_min_precision (maxnode, sign));
7643
7644 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
7645 {
7646 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
7647 if (tem == NULL)
7648 {
7649 warning (0, "enumeration values exceed range of largest integer");
7650 tem = long_long_integer_type_node;
7651 }
7652 }
7653 else
7654 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
7655
7656 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
7657 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
7658 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
7659 TYPE_SIZE (enumtype) = 0;
7660
7661 /* If the precision of the type was specific with an attribute and it
7662 was too small, give an error. Otherwise, use it. */
7663 if (TYPE_PRECISION (enumtype))
7664 {
7665 if (precision > TYPE_PRECISION (enumtype))
7666 error ("specified mode too small for enumeral values");
7667 }
7668 else
7669 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
7670
7671 layout_type (enumtype);
7672
7673 if (values != error_mark_node)
7674 {
7675 /* Change the type of the enumerators to be the enum type. We
7676 need to do this irrespective of the size of the enum, for
7677 proper type checking. Replace the DECL_INITIALs of the
7678 enumerators, and the value slots of the list, with copies
7679 that have the enum type; they cannot be modified in place
7680 because they may be shared (e.g. integer_zero_node) Finally,
7681 change the purpose slots to point to the names of the decls. */
7682 for (pair = values; pair; pair = TREE_CHAIN (pair))
7683 {
7684 tree enu = TREE_PURPOSE (pair);
7685 tree ini = DECL_INITIAL (enu);
7686
7687 TREE_TYPE (enu) = enumtype;
7688
7689 /* The ISO C Standard mandates enumerators to have type int,
7690 even though the underlying type of an enum type is
7691 unspecified. However, GCC allows enumerators of any
7692 integer type as an extensions. build_enumerator()
7693 converts any enumerators that fit in an int to type int,
7694 to avoid promotions to unsigned types when comparing
7695 integers with enumerators that fit in the int range.
7696 When -pedantic is given, build_enumerator() would have
7697 already warned about those that don't fit. Here we
7698 convert the rest to the enumerator type. */
7699 if (TREE_TYPE (ini) != integer_type_node)
7700 ini = convert (enumtype, ini);
7701
7702 DECL_INITIAL (enu) = ini;
7703 TREE_PURPOSE (pair) = DECL_NAME (enu);
7704 TREE_VALUE (pair) = ini;
7705 }
7706
7707 TYPE_VALUES (enumtype) = values;
7708 }
7709
7710 /* Record the min/max values so that we can warn about bit-field
7711 enumerations that are too small for the values. */
7712 lt = ggc_cleared_alloc<struct lang_type> ();
7713 lt->enum_min = minnode;
7714 lt->enum_max = maxnode;
7715 TYPE_LANG_SPECIFIC (enumtype) = lt;
7716
7717 /* Fix up all variant types of this enum type. */
7718 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
7719 {
7720 if (tem == enumtype)
7721 continue;
7722 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
7723 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
7724 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
7725 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
7726 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
7727 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
7728 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
7729 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
7730 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
7731 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
7732 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
7733 }
7734
7735 /* Finish debugging output for this type. */
7736 rest_of_type_compilation (enumtype, toplevel);
7737
7738 /* If this enum is defined inside a struct, add it to
7739 struct_types. */
7740 if (warn_cxx_compat
7741 && struct_parse_info != NULL
7742 && !in_sizeof && !in_typeof && !in_alignof)
7743 struct_parse_info->struct_types.safe_push (enumtype);
7744
7745 return enumtype;
7746 }
7747
7748 /* Build and install a CONST_DECL for one value of the
7749 current enumeration type (one that was begun with start_enum).
7750 DECL_LOC is the location of the enumerator.
7751 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
7752 Return a tree-list containing the CONST_DECL and its value.
7753 Assignment of sequential values by default is handled here. */
7754
7755 tree
7756 build_enumerator (location_t decl_loc, location_t loc,
7757 struct c_enum_contents *the_enum, tree name, tree value)
7758 {
7759 tree decl, type;
7760
7761 /* Validate and default VALUE. */
7762
7763 if (value != 0)
7764 {
7765 /* Don't issue more errors for error_mark_node (i.e. an
7766 undeclared identifier) - just ignore the value expression. */
7767 if (value == error_mark_node)
7768 value = 0;
7769 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
7770 {
7771 error_at (loc, "enumerator value for %qE is not an integer constant",
7772 name);
7773 value = 0;
7774 }
7775 else
7776 {
7777 if (TREE_CODE (value) != INTEGER_CST)
7778 {
7779 value = c_fully_fold (value, false, NULL);
7780 if (TREE_CODE (value) == INTEGER_CST)
7781 pedwarn (loc, OPT_Wpedantic,
7782 "enumerator value for %qE is not an integer "
7783 "constant expression", name);
7784 }
7785 if (TREE_CODE (value) != INTEGER_CST)
7786 {
7787 error ("enumerator value for %qE is not an integer constant",
7788 name);
7789 value = 0;
7790 }
7791 else
7792 {
7793 value = default_conversion (value);
7794 constant_expression_warning (value);
7795 }
7796 }
7797 }
7798
7799 /* Default based on previous value. */
7800 /* It should no longer be possible to have NON_LVALUE_EXPR
7801 in the default. */
7802 if (value == 0)
7803 {
7804 value = the_enum->enum_next_value;
7805 if (the_enum->enum_overflow)
7806 error_at (loc, "overflow in enumeration values");
7807 }
7808 /* Even though the underlying type of an enum is unspecified, the
7809 type of enumeration constants is explicitly defined as int
7810 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
7811 an extension. */
7812 else if (!int_fits_type_p (value, integer_type_node))
7813 pedwarn (loc, OPT_Wpedantic,
7814 "ISO C restricts enumerator values to range of %<int%>");
7815
7816 /* The ISO C Standard mandates enumerators to have type int, even
7817 though the underlying type of an enum type is unspecified.
7818 However, GCC allows enumerators of any integer type as an
7819 extensions. Here we convert any enumerators that fit in an int
7820 to type int, to avoid promotions to unsigned types when comparing
7821 integers with enumerators that fit in the int range. When
7822 -pedantic is given, we would have already warned about those that
7823 don't fit. We have to do this here rather than in finish_enum
7824 because this value may be used to define more enumerators. */
7825 if (int_fits_type_p (value, integer_type_node))
7826 value = convert (integer_type_node, value);
7827
7828 /* Set basis for default for next value. */
7829 the_enum->enum_next_value
7830 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
7831 PLUS_EXPR, value, integer_one_node, 0);
7832 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
7833
7834 /* Now create a declaration for the enum value name. */
7835
7836 type = TREE_TYPE (value);
7837 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
7838 TYPE_PRECISION (integer_type_node)),
7839 (TYPE_PRECISION (type)
7840 >= TYPE_PRECISION (integer_type_node)
7841 && TYPE_UNSIGNED (type)));
7842
7843 decl = build_decl (decl_loc, CONST_DECL, name, type);
7844 DECL_INITIAL (decl) = convert (type, value);
7845 pushdecl (decl);
7846
7847 return tree_cons (decl, value, NULL_TREE);
7848 }
7849
7850 \f
7851 /* Create the FUNCTION_DECL for a function definition.
7852 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
7853 the declaration; they describe the function's name and the type it returns,
7854 but twisted together in a fashion that parallels the syntax of C.
7855
7856 This function creates a binding context for the function body
7857 as well as setting up the FUNCTION_DECL in current_function_decl.
7858
7859 Returns 1 on success. If the DECLARATOR is not suitable for a function
7860 (it defines a datum instead), we return 0, which tells
7861 yyparse to report a parse error. */
7862
7863 int
7864 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
7865 tree attributes)
7866 {
7867 tree decl1, old_decl;
7868 tree restype, resdecl;
7869 location_t loc;
7870
7871 current_function_returns_value = 0; /* Assume, until we see it does. */
7872 current_function_returns_null = 0;
7873 current_function_returns_abnormally = 0;
7874 warn_about_return_type = 0;
7875 c_switch_stack = NULL;
7876
7877 /* Indicate no valid break/continue context by setting these variables
7878 to some non-null, non-label value. We'll notice and emit the proper
7879 error message in c_finish_bc_stmt. */
7880 c_break_label = c_cont_label = size_zero_node;
7881
7882 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
7883 &attributes, NULL, NULL, DEPRECATED_NORMAL);
7884
7885 /* If the declarator is not suitable for a function definition,
7886 cause a syntax error. */
7887 if (decl1 == 0
7888 || TREE_CODE (decl1) != FUNCTION_DECL)
7889 return 0;
7890
7891 loc = DECL_SOURCE_LOCATION (decl1);
7892
7893 c_decl_attributes (&decl1, attributes, 0);
7894
7895 if (DECL_DECLARED_INLINE_P (decl1)
7896 && DECL_UNINLINABLE (decl1)
7897 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
7898 warning_at (loc, OPT_Wattributes,
7899 "inline function %qD given attribute noinline",
7900 decl1);
7901
7902 /* Handle gnu_inline attribute. */
7903 if (declspecs->inline_p
7904 && !flag_gnu89_inline
7905 && TREE_CODE (decl1) == FUNCTION_DECL
7906 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
7907 || current_function_decl))
7908 {
7909 if (declspecs->storage_class != csc_static)
7910 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
7911 }
7912
7913 announce_function (decl1);
7914
7915 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
7916 {
7917 error_at (loc, "return type is an incomplete type");
7918 /* Make it return void instead. */
7919 TREE_TYPE (decl1)
7920 = build_function_type (void_type_node,
7921 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
7922 }
7923
7924 if (warn_about_return_type)
7925 warn_defaults_to (loc, flag_isoc99 ? 0
7926 : (warn_return_type ? OPT_Wreturn_type
7927 : OPT_Wimplicit_int),
7928 "return type defaults to %<int%>");
7929
7930 /* Make the init_value nonzero so pushdecl knows this is not tentative.
7931 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
7932 DECL_INITIAL (decl1) = error_mark_node;
7933
7934 /* A nested function is not global. */
7935 if (current_function_decl != 0)
7936 TREE_PUBLIC (decl1) = 0;
7937
7938 /* If this definition isn't a prototype and we had a prototype declaration
7939 before, copy the arg type info from that prototype. */
7940 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
7941 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
7942 old_decl = 0;
7943 current_function_prototype_locus = UNKNOWN_LOCATION;
7944 current_function_prototype_built_in = false;
7945 current_function_prototype_arg_types = NULL_TREE;
7946 if (!prototype_p (TREE_TYPE (decl1)))
7947 {
7948 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
7949 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7950 TREE_TYPE (TREE_TYPE (old_decl))))
7951 {
7952 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
7953 TREE_TYPE (decl1));
7954 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
7955 current_function_prototype_built_in
7956 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
7957 current_function_prototype_arg_types
7958 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
7959 }
7960 if (TREE_PUBLIC (decl1))
7961 {
7962 /* If there is an external prototype declaration of this
7963 function, record its location but do not copy information
7964 to this decl. This may be an invisible declaration
7965 (built-in or in a scope which has finished) or simply
7966 have more refined argument types than any declaration
7967 found above. */
7968 struct c_binding *b;
7969 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
7970 if (B_IN_SCOPE (b, external_scope))
7971 break;
7972 if (b)
7973 {
7974 tree ext_decl, ext_type;
7975 ext_decl = b->decl;
7976 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
7977 if (TREE_CODE (ext_type) == FUNCTION_TYPE
7978 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7979 TREE_TYPE (ext_type)))
7980 {
7981 current_function_prototype_locus
7982 = DECL_SOURCE_LOCATION (ext_decl);
7983 current_function_prototype_built_in
7984 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
7985 current_function_prototype_arg_types
7986 = TYPE_ARG_TYPES (ext_type);
7987 }
7988 }
7989 }
7990 }
7991
7992 /* Optionally warn of old-fashioned def with no previous prototype. */
7993 if (warn_strict_prototypes
7994 && old_decl != error_mark_node
7995 && !prototype_p (TREE_TYPE (decl1))
7996 && C_DECL_ISNT_PROTOTYPE (old_decl))
7997 warning_at (loc, OPT_Wstrict_prototypes,
7998 "function declaration isn%'t a prototype");
7999 /* Optionally warn of any global def with no previous prototype. */
8000 else if (warn_missing_prototypes
8001 && old_decl != error_mark_node
8002 && TREE_PUBLIC (decl1)
8003 && !MAIN_NAME_P (DECL_NAME (decl1))
8004 && C_DECL_ISNT_PROTOTYPE (old_decl)
8005 && !DECL_DECLARED_INLINE_P (decl1))
8006 warning_at (loc, OPT_Wmissing_prototypes,
8007 "no previous prototype for %qD", decl1);
8008 /* Optionally warn of any def with no previous prototype
8009 if the function has already been used. */
8010 else if (warn_missing_prototypes
8011 && old_decl != 0
8012 && old_decl != error_mark_node
8013 && TREE_USED (old_decl)
8014 && !prototype_p (TREE_TYPE (old_decl)))
8015 warning_at (loc, OPT_Wmissing_prototypes,
8016 "%qD was used with no prototype before its definition", decl1);
8017 /* Optionally warn of any global def with no previous declaration. */
8018 else if (warn_missing_declarations
8019 && TREE_PUBLIC (decl1)
8020 && old_decl == 0
8021 && !MAIN_NAME_P (DECL_NAME (decl1)))
8022 warning_at (loc, OPT_Wmissing_declarations,
8023 "no previous declaration for %qD",
8024 decl1);
8025 /* Optionally warn of any def with no previous declaration
8026 if the function has already been used. */
8027 else if (warn_missing_declarations
8028 && old_decl != 0
8029 && old_decl != error_mark_node
8030 && TREE_USED (old_decl)
8031 && C_DECL_IMPLICIT (old_decl))
8032 warning_at (loc, OPT_Wmissing_declarations,
8033 "%qD was used with no declaration before its definition", decl1);
8034
8035 /* This function exists in static storage.
8036 (This does not mean `static' in the C sense!) */
8037 TREE_STATIC (decl1) = 1;
8038
8039 /* This is the earliest point at which we might know the assembler
8040 name of the function. Thus, if it's set before this, die horribly. */
8041 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8042
8043 /* If #pragma weak was used, mark the decl weak now. */
8044 if (current_scope == file_scope)
8045 maybe_apply_pragma_weak (decl1);
8046
8047 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8048 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8049 {
8050 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8051 != integer_type_node)
8052 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8053 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8054 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8055 decl1);
8056
8057 check_main_parameter_types (decl1);
8058
8059 if (!TREE_PUBLIC (decl1))
8060 pedwarn (loc, OPT_Wmain,
8061 "%qD is normally a non-static function", decl1);
8062 }
8063
8064 /* Record the decl so that the function name is defined.
8065 If we already have a decl for this name, and it is a FUNCTION_DECL,
8066 use the old decl. */
8067
8068 current_function_decl = pushdecl (decl1);
8069
8070 push_scope ();
8071 declare_parm_level ();
8072
8073 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8074 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8075 DECL_ARTIFICIAL (resdecl) = 1;
8076 DECL_IGNORED_P (resdecl) = 1;
8077 DECL_RESULT (current_function_decl) = resdecl;
8078
8079 start_fname_decls ();
8080
8081 return 1;
8082 }
8083 \f
8084 /* Subroutine of store_parm_decls which handles new-style function
8085 definitions (prototype format). The parms already have decls, so we
8086 need only record them as in effect and complain if any redundant
8087 old-style parm decls were written. */
8088 static void
8089 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8090 {
8091 tree decl;
8092 c_arg_tag *tag;
8093 unsigned ix;
8094
8095 if (current_scope->bindings)
8096 {
8097 error_at (DECL_SOURCE_LOCATION (fndecl),
8098 "old-style parameter declarations in prototyped "
8099 "function definition");
8100
8101 /* Get rid of the old-style declarations. */
8102 pop_scope ();
8103 push_scope ();
8104 }
8105 /* Don't issue this warning for nested functions, and don't issue this
8106 warning if we got here because ARG_INFO_TYPES was error_mark_node
8107 (this happens when a function definition has just an ellipsis in
8108 its parameter list). */
8109 else if (!in_system_header_at (input_location)
8110 && !current_function_scope
8111 && arg_info->types != error_mark_node)
8112 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8113 "traditional C rejects ISO C style function definitions");
8114
8115 /* Now make all the parameter declarations visible in the function body.
8116 We can bypass most of the grunt work of pushdecl. */
8117 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8118 {
8119 DECL_CONTEXT (decl) = current_function_decl;
8120 if (DECL_NAME (decl))
8121 {
8122 bind (DECL_NAME (decl), decl, current_scope,
8123 /*invisible=*/false, /*nested=*/false,
8124 UNKNOWN_LOCATION);
8125 if (!TREE_USED (decl))
8126 warn_if_shadowing (decl);
8127 }
8128 else
8129 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8130 }
8131
8132 /* Record the parameter list in the function declaration. */
8133 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8134
8135 /* Now make all the ancillary declarations visible, likewise. */
8136 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8137 {
8138 DECL_CONTEXT (decl) = current_function_decl;
8139 if (DECL_NAME (decl))
8140 bind (DECL_NAME (decl), decl, current_scope,
8141 /*invisible=*/false,
8142 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
8143 UNKNOWN_LOCATION);
8144 }
8145
8146 /* And all the tag declarations. */
8147 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
8148 if (tag->id)
8149 bind (tag->id, tag->type, current_scope,
8150 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
8151 }
8152
8153 /* Subroutine of store_parm_decls which handles old-style function
8154 definitions (separate parameter list and declarations). */
8155
8156 static void
8157 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
8158 {
8159 struct c_binding *b;
8160 tree parm, decl, last;
8161 tree parmids = arg_info->parms;
8162 hash_set<tree> seen_args;
8163
8164 if (!in_system_header_at (input_location))
8165 warning_at (DECL_SOURCE_LOCATION (fndecl),
8166 OPT_Wold_style_definition, "old-style function definition");
8167
8168 /* Match each formal parameter name with its declaration. Save each
8169 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8170 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8171 {
8172 if (TREE_VALUE (parm) == 0)
8173 {
8174 error_at (DECL_SOURCE_LOCATION (fndecl),
8175 "parameter name missing from parameter list");
8176 TREE_PURPOSE (parm) = 0;
8177 continue;
8178 }
8179
8180 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8181 if (b && B_IN_CURRENT_SCOPE (b))
8182 {
8183 decl = b->decl;
8184 /* Skip erroneous parameters. */
8185 if (decl == error_mark_node)
8186 continue;
8187 /* If we got something other than a PARM_DECL it is an error. */
8188 if (TREE_CODE (decl) != PARM_DECL)
8189 error_at (DECL_SOURCE_LOCATION (decl),
8190 "%qD declared as a non-parameter", decl);
8191 /* If the declaration is already marked, we have a duplicate
8192 name. Complain and ignore the duplicate. */
8193 else if (seen_args.contains (decl))
8194 {
8195 error_at (DECL_SOURCE_LOCATION (decl),
8196 "multiple parameters named %qD", decl);
8197 TREE_PURPOSE (parm) = 0;
8198 continue;
8199 }
8200 /* If the declaration says "void", complain and turn it into
8201 an int. */
8202 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8203 {
8204 error_at (DECL_SOURCE_LOCATION (decl),
8205 "parameter %qD declared with void type", decl);
8206 TREE_TYPE (decl) = integer_type_node;
8207 DECL_ARG_TYPE (decl) = integer_type_node;
8208 layout_decl (decl, 0);
8209 }
8210 warn_if_shadowing (decl);
8211 }
8212 /* If no declaration found, default to int. */
8213 else
8214 {
8215 /* FIXME diagnostics: This should be the location of the argument,
8216 not the FNDECL. E.g., for an old-style declaration
8217
8218 int f10(v) { blah; }
8219
8220 We should use the location of the V, not the F10.
8221 Unfortunately, the V is an IDENTIFIER_NODE which has no
8222 location. In the future we need locations for c_arg_info
8223 entries.
8224
8225 See gcc.dg/Wshadow-3.c for an example of this problem. */
8226 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8227 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8228 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8229 pushdecl (decl);
8230 warn_if_shadowing (decl);
8231
8232 if (flag_isoc99)
8233 pedwarn (DECL_SOURCE_LOCATION (decl),
8234 0, "type of %qD defaults to %<int%>", decl);
8235 else
8236 warning_at (DECL_SOURCE_LOCATION (decl),
8237 OPT_Wmissing_parameter_type,
8238 "type of %qD defaults to %<int%>", decl);
8239 }
8240
8241 TREE_PURPOSE (parm) = decl;
8242 seen_args.add (decl);
8243 }
8244
8245 /* Now examine the parms chain for incomplete declarations
8246 and declarations with no corresponding names. */
8247
8248 for (b = current_scope->bindings; b; b = b->prev)
8249 {
8250 parm = b->decl;
8251 if (TREE_CODE (parm) != PARM_DECL)
8252 continue;
8253
8254 if (TREE_TYPE (parm) != error_mark_node
8255 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
8256 {
8257 error_at (DECL_SOURCE_LOCATION (parm),
8258 "parameter %qD has incomplete type", parm);
8259 TREE_TYPE (parm) = error_mark_node;
8260 }
8261
8262 if (!seen_args.contains (parm))
8263 {
8264 error_at (DECL_SOURCE_LOCATION (parm),
8265 "declaration for parameter %qD but no such parameter",
8266 parm);
8267
8268 /* Pretend the parameter was not missing.
8269 This gets us to a standard state and minimizes
8270 further error messages. */
8271 parmids = chainon (parmids, tree_cons (parm, 0, 0));
8272 }
8273 }
8274
8275 /* Chain the declarations together in the order of the list of
8276 names. Store that chain in the function decl, replacing the
8277 list of names. Update the current scope to match. */
8278 DECL_ARGUMENTS (fndecl) = 0;
8279
8280 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8281 if (TREE_PURPOSE (parm))
8282 break;
8283 if (parm && TREE_PURPOSE (parm))
8284 {
8285 last = TREE_PURPOSE (parm);
8286 DECL_ARGUMENTS (fndecl) = last;
8287
8288 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8289 if (TREE_PURPOSE (parm))
8290 {
8291 DECL_CHAIN (last) = TREE_PURPOSE (parm);
8292 last = TREE_PURPOSE (parm);
8293 }
8294 DECL_CHAIN (last) = 0;
8295 }
8296
8297 /* If there was a previous prototype,
8298 set the DECL_ARG_TYPE of each argument according to
8299 the type previously specified, and report any mismatches. */
8300
8301 if (current_function_prototype_arg_types)
8302 {
8303 tree type;
8304 for (parm = DECL_ARGUMENTS (fndecl),
8305 type = current_function_prototype_arg_types;
8306 parm || (type && TREE_VALUE (type) != error_mark_node
8307 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
8308 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8309 {
8310 if (parm == 0 || type == 0
8311 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
8312 {
8313 if (current_function_prototype_built_in)
8314 warning_at (DECL_SOURCE_LOCATION (fndecl),
8315 0, "number of arguments doesn%'t match "
8316 "built-in prototype");
8317 else
8318 {
8319 /* FIXME diagnostics: This should be the location of
8320 FNDECL, but there is bug when a prototype is
8321 declared inside function context, but defined
8322 outside of it (e.g., gcc.dg/pr15698-2.c). In
8323 which case FNDECL gets the location of the
8324 prototype, not the definition. */
8325 error_at (input_location,
8326 "number of arguments doesn%'t match prototype");
8327
8328 error_at (current_function_prototype_locus,
8329 "prototype declaration");
8330 }
8331 break;
8332 }
8333 /* Type for passing arg must be consistent with that
8334 declared for the arg. ISO C says we take the unqualified
8335 type for parameters declared with qualified type. */
8336 if (TREE_TYPE (parm) != error_mark_node
8337 && TREE_TYPE (type) != error_mark_node
8338 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8339 != TYPE_ATOMIC (TREE_VALUE (type)))
8340 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
8341 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
8342 {
8343 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8344 == TYPE_ATOMIC (TREE_VALUE (type)))
8345 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
8346 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
8347 {
8348 /* Adjust argument to match prototype. E.g. a previous
8349 `int foo(float);' prototype causes
8350 `int foo(x) float x; {...}' to be treated like
8351 `int foo(float x) {...}'. This is particularly
8352 useful for argument types like uid_t. */
8353 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
8354
8355 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
8356 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
8357 && TYPE_PRECISION (TREE_TYPE (parm))
8358 < TYPE_PRECISION (integer_type_node))
8359 DECL_ARG_TYPE (parm)
8360 = c_type_promotes_to (TREE_TYPE (parm));
8361
8362 /* ??? Is it possible to get here with a
8363 built-in prototype or will it always have
8364 been diagnosed as conflicting with an
8365 old-style definition and discarded? */
8366 if (current_function_prototype_built_in)
8367 warning_at (DECL_SOURCE_LOCATION (parm),
8368 OPT_Wpedantic, "promoted argument %qD "
8369 "doesn%'t match built-in prototype", parm);
8370 else
8371 {
8372 pedwarn (DECL_SOURCE_LOCATION (parm),
8373 OPT_Wpedantic, "promoted argument %qD "
8374 "doesn%'t match prototype", parm);
8375 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
8376 "prototype declaration");
8377 }
8378 }
8379 else
8380 {
8381 if (current_function_prototype_built_in)
8382 warning_at (DECL_SOURCE_LOCATION (parm),
8383 0, "argument %qD doesn%'t match "
8384 "built-in prototype", parm);
8385 else
8386 {
8387 error_at (DECL_SOURCE_LOCATION (parm),
8388 "argument %qD doesn%'t match prototype", parm);
8389 error_at (current_function_prototype_locus,
8390 "prototype declaration");
8391 }
8392 }
8393 }
8394 }
8395 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
8396 }
8397
8398 /* Otherwise, create a prototype that would match. */
8399
8400 else
8401 {
8402 tree actual = 0, last = 0, type;
8403
8404 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
8405 {
8406 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
8407 if (last)
8408 TREE_CHAIN (last) = type;
8409 else
8410 actual = type;
8411 last = type;
8412 }
8413 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
8414 if (last)
8415 TREE_CHAIN (last) = type;
8416 else
8417 actual = type;
8418
8419 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
8420 of the type of this function, but we need to avoid having this
8421 affect the types of other similarly-typed functions, so we must
8422 first force the generation of an identical (but separate) type
8423 node for the relevant function type. The new node we create
8424 will be a variant of the main variant of the original function
8425 type. */
8426
8427 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
8428
8429 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
8430 }
8431 }
8432
8433 /* Store parameter declarations passed in ARG_INFO into the current
8434 function declaration. */
8435
8436 void
8437 store_parm_decls_from (struct c_arg_info *arg_info)
8438 {
8439 current_function_arg_info = arg_info;
8440 store_parm_decls ();
8441 }
8442
8443 /* Store the parameter declarations into the current function declaration.
8444 This is called after parsing the parameter declarations, before
8445 digesting the body of the function.
8446
8447 For an old-style definition, construct a prototype out of the old-style
8448 parameter declarations and inject it into the function's type. */
8449
8450 void
8451 store_parm_decls (void)
8452 {
8453 tree fndecl = current_function_decl;
8454 bool proto;
8455
8456 /* The argument information block for FNDECL. */
8457 struct c_arg_info *arg_info = current_function_arg_info;
8458 current_function_arg_info = 0;
8459
8460 /* True if this definition is written with a prototype. Note:
8461 despite C99 6.7.5.3p14, we can *not* treat an empty argument
8462 list in a function definition as equivalent to (void) -- an
8463 empty argument list specifies the function has no parameters,
8464 but only (void) sets up a prototype for future calls. */
8465 proto = arg_info->types != 0;
8466
8467 if (proto)
8468 store_parm_decls_newstyle (fndecl, arg_info);
8469 else
8470 store_parm_decls_oldstyle (fndecl, arg_info);
8471
8472 /* The next call to push_scope will be a function body. */
8473
8474 next_is_function_body = true;
8475
8476 /* Write a record describing this function definition to the prototypes
8477 file (if requested). */
8478
8479 gen_aux_info_record (fndecl, 1, 0, proto);
8480
8481 /* Initialize the RTL code for the function. */
8482 allocate_struct_function (fndecl, false);
8483
8484 if (warn_unused_local_typedefs)
8485 cfun->language = ggc_cleared_alloc<language_function> ();
8486
8487 /* Begin the statement tree for this function. */
8488 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
8489
8490 /* ??? Insert the contents of the pending sizes list into the function
8491 to be evaluated. The only reason left to have this is
8492 void foo(int n, int array[n++])
8493 because we throw away the array type in favor of a pointer type, and
8494 thus won't naturally see the SAVE_EXPR containing the increment. All
8495 other pending sizes would be handled by gimplify_parameters. */
8496 if (arg_info->pending_sizes)
8497 add_stmt (arg_info->pending_sizes);
8498 }
8499
8500 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
8501 c_finish_omp_declare_simd for function prototypes. No diagnostics
8502 should be done. */
8503
8504 void
8505 temp_store_parm_decls (tree fndecl, tree parms)
8506 {
8507 push_scope ();
8508 for (tree p = parms; p; p = DECL_CHAIN (p))
8509 {
8510 DECL_CONTEXT (p) = fndecl;
8511 if (DECL_NAME (p))
8512 bind (DECL_NAME (p), p, current_scope,
8513 /*invisible=*/false, /*nested=*/false,
8514 UNKNOWN_LOCATION);
8515 }
8516 }
8517
8518 /* Undo what temp_store_parm_decls did. */
8519
8520 void
8521 temp_pop_parm_decls (void)
8522 {
8523 /* Clear all bindings in this temporary scope, so that
8524 pop_scope doesn't create a BLOCK. */
8525 struct c_binding *b = current_scope->bindings;
8526 current_scope->bindings = NULL;
8527 for (; b; b = free_binding_and_advance (b))
8528 {
8529 gcc_assert (TREE_CODE (b->decl) == PARM_DECL);
8530 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8531 I_SYMBOL_BINDING (b->id) = b->shadowed;
8532 if (b->shadowed && b->shadowed->u.type)
8533 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
8534 }
8535 pop_scope ();
8536 }
8537 \f
8538
8539 /* Finish up a function declaration and compile that function
8540 all the way to assembler language output. Then free the storage
8541 for the function definition.
8542
8543 This is called after parsing the body of the function definition. */
8544
8545 void
8546 finish_function (void)
8547 {
8548 tree fndecl = current_function_decl;
8549
8550 if (c_dialect_objc ())
8551 objc_finish_function ();
8552
8553 if (TREE_CODE (fndecl) == FUNCTION_DECL
8554 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
8555 {
8556 tree args = DECL_ARGUMENTS (fndecl);
8557 for (; args; args = DECL_CHAIN (args))
8558 {
8559 tree type = TREE_TYPE (args);
8560 if (INTEGRAL_TYPE_P (type)
8561 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8562 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
8563 }
8564 }
8565
8566 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
8567 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8568
8569 /* Must mark the RESULT_DECL as being in this function. */
8570
8571 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
8572 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8573
8574 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
8575 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
8576 == integer_type_node && flag_isoc99)
8577 {
8578 /* Hack. We don't want the middle-end to warn that this return
8579 is unreachable, so we mark its location as special. Using
8580 UNKNOWN_LOCATION has the problem that it gets clobbered in
8581 annotate_one_with_locus. A cleaner solution might be to
8582 ensure ! should_carry_locus_p (stmt), but that needs a flag.
8583 */
8584 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
8585 }
8586
8587 /* Tie off the statement tree for this function. */
8588 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
8589
8590 /* If the function has _Cilk_spawn in front of a function call inside it
8591 i.e. it is a spawning function, then add the appropriate Cilk plus
8592 functions inside. */
8593 if (fn_contains_cilk_spawn_p (cfun))
8594 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
8595
8596 finish_fname_decls ();
8597
8598 /* Complain if there's just no return statement. */
8599 if (warn_return_type
8600 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
8601 && !current_function_returns_value && !current_function_returns_null
8602 /* Don't complain if we are no-return. */
8603 && !current_function_returns_abnormally
8604 /* Don't complain if we are declared noreturn. */
8605 && !TREE_THIS_VOLATILE (fndecl)
8606 /* Don't warn for main(). */
8607 && !MAIN_NAME_P (DECL_NAME (fndecl))
8608 /* Or if they didn't actually specify a return type. */
8609 && !C_FUNCTION_IMPLICIT_INT (fndecl)
8610 /* Normally, with -Wreturn-type, flow will complain, but we might
8611 optimize out static functions. */
8612 && !TREE_PUBLIC (fndecl))
8613 {
8614 warning (OPT_Wreturn_type,
8615 "no return statement in function returning non-void");
8616 TREE_NO_WARNING (fndecl) = 1;
8617 }
8618
8619 /* Complain about parameters that are only set, but never otherwise used. */
8620 if (warn_unused_but_set_parameter)
8621 {
8622 tree decl;
8623
8624 for (decl = DECL_ARGUMENTS (fndecl);
8625 decl;
8626 decl = DECL_CHAIN (decl))
8627 if (TREE_USED (decl)
8628 && TREE_CODE (decl) == PARM_DECL
8629 && !DECL_READ_P (decl)
8630 && DECL_NAME (decl)
8631 && !DECL_ARTIFICIAL (decl)
8632 && !TREE_NO_WARNING (decl))
8633 warning_at (DECL_SOURCE_LOCATION (decl),
8634 OPT_Wunused_but_set_parameter,
8635 "parameter %qD set but not used", decl);
8636 }
8637
8638 /* Complain about locally defined typedefs that are not used in this
8639 function. */
8640 maybe_warn_unused_local_typedefs ();
8641
8642 /* Store the end of the function, so that we get good line number
8643 info for the epilogue. */
8644 cfun->function_end_locus = input_location;
8645
8646 /* Finalize the ELF visibility for the function. */
8647 c_determine_visibility (fndecl);
8648
8649 /* For GNU C extern inline functions disregard inline limits. */
8650 if (DECL_EXTERNAL (fndecl)
8651 && DECL_DECLARED_INLINE_P (fndecl))
8652 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
8653
8654 /* Genericize before inlining. Delay genericizing nested functions
8655 until their parent function is genericized. Since finalizing
8656 requires GENERIC, delay that as well. */
8657
8658 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
8659 && !undef_nested_function)
8660 {
8661 if (!decl_function_context (fndecl))
8662 {
8663 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
8664 c_genericize (fndecl);
8665
8666 /* ??? Objc emits functions after finalizing the compilation unit.
8667 This should be cleaned up later and this conditional removed. */
8668 if (cgraph_global_info_ready)
8669 {
8670 cgraph_node::add_new_function (fndecl, false);
8671 return;
8672 }
8673 cgraph_finalize_function (fndecl, false);
8674 }
8675 else
8676 {
8677 /* Register this function with cgraph just far enough to get it
8678 added to our parent's nested function list. Handy, since the
8679 C front end doesn't have such a list. */
8680 (void) cgraph_node::get_create (fndecl);
8681 }
8682 }
8683
8684 if (!decl_function_context (fndecl))
8685 undef_nested_function = false;
8686
8687 if (cfun->language != NULL)
8688 {
8689 ggc_free (cfun->language);
8690 cfun->language = NULL;
8691 }
8692
8693 /* We're leaving the context of this function, so zap cfun.
8694 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
8695 tree_rest_of_compilation. */
8696 set_cfun (NULL);
8697 current_function_decl = NULL;
8698 }
8699 \f
8700 /* Check the declarations given in a for-loop for satisfying the C99
8701 constraints. If exactly one such decl is found, return it. LOC is
8702 the location of the opening parenthesis of the for loop. The last
8703 parameter allows you to control the "for loop initial declarations
8704 are only allowed in C99 mode". Normally, you should pass
8705 flag_isoc99 as that parameter. But in some cases (Objective-C
8706 foreach loop, for example) we want to run the checks in this
8707 function even if not in C99 mode, so we allow the caller to turn
8708 off the error about not being in C99 mode.
8709 */
8710
8711 tree
8712 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
8713 {
8714 struct c_binding *b;
8715 tree one_decl = NULL_TREE;
8716 int n_decls = 0;
8717
8718 if (!turn_off_iso_c99_error)
8719 {
8720 static bool hint = true;
8721 /* If we get here, declarations have been used in a for loop without
8722 the C99 for loop scope. This doesn't make much sense, so don't
8723 allow it. */
8724 error_at (loc, "%<for%> loop initial declarations "
8725 "are only allowed in C99 or C11 mode");
8726 if (hint)
8727 {
8728 inform (loc,
8729 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
8730 "to compile your code");
8731 hint = false;
8732 }
8733 return NULL_TREE;
8734 }
8735 /* C99 subclause 6.8.5 paragraph 3:
8736
8737 [#3] The declaration part of a for statement shall only
8738 declare identifiers for objects having storage class auto or
8739 register.
8740
8741 It isn't clear whether, in this sentence, "identifiers" binds to
8742 "shall only declare" or to "objects" - that is, whether all identifiers
8743 declared must be identifiers for objects, or whether the restriction
8744 only applies to those that are. (A question on this in comp.std.c
8745 in November 2000 received no answer.) We implement the strictest
8746 interpretation, to avoid creating an extension which later causes
8747 problems. */
8748
8749 for (b = current_scope->bindings; b; b = b->prev)
8750 {
8751 tree id = b->id;
8752 tree decl = b->decl;
8753
8754 if (!id)
8755 continue;
8756
8757 switch (TREE_CODE (decl))
8758 {
8759 case VAR_DECL:
8760 {
8761 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
8762 if (TREE_STATIC (decl))
8763 error_at (decl_loc,
8764 "declaration of static variable %qD in %<for%> loop "
8765 "initial declaration", decl);
8766 else if (DECL_EXTERNAL (decl))
8767 error_at (decl_loc,
8768 "declaration of %<extern%> variable %qD in %<for%> loop "
8769 "initial declaration", decl);
8770 }
8771 break;
8772
8773 case RECORD_TYPE:
8774 error_at (loc,
8775 "%<struct %E%> declared in %<for%> loop initial "
8776 "declaration", id);
8777 break;
8778 case UNION_TYPE:
8779 error_at (loc,
8780 "%<union %E%> declared in %<for%> loop initial declaration",
8781 id);
8782 break;
8783 case ENUMERAL_TYPE:
8784 error_at (loc, "%<enum %E%> declared in %<for%> loop "
8785 "initial declaration", id);
8786 break;
8787 default:
8788 error_at (loc, "declaration of non-variable "
8789 "%qD in %<for%> loop initial declaration", decl);
8790 }
8791
8792 n_decls++;
8793 one_decl = decl;
8794 }
8795
8796 return n_decls == 1 ? one_decl : NULL_TREE;
8797 }
8798 \f
8799 /* Save and reinitialize the variables
8800 used during compilation of a C function. */
8801
8802 void
8803 c_push_function_context (void)
8804 {
8805 struct language_function *p = cfun->language;
8806 /* cfun->language might have been already allocated by the use of
8807 -Wunused-local-typedefs. In that case, just re-use it. */
8808 if (p == NULL)
8809 cfun->language = p = ggc_cleared_alloc<language_function> ();
8810
8811 p->base.x_stmt_tree = c_stmt_tree;
8812 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
8813 p->x_break_label = c_break_label;
8814 p->x_cont_label = c_cont_label;
8815 p->x_switch_stack = c_switch_stack;
8816 p->arg_info = current_function_arg_info;
8817 p->returns_value = current_function_returns_value;
8818 p->returns_null = current_function_returns_null;
8819 p->returns_abnormally = current_function_returns_abnormally;
8820 p->warn_about_return_type = warn_about_return_type;
8821
8822 push_function_context ();
8823 }
8824
8825 /* Restore the variables used during compilation of a C function. */
8826
8827 void
8828 c_pop_function_context (void)
8829 {
8830 struct language_function *p;
8831
8832 pop_function_context ();
8833 p = cfun->language;
8834
8835 /* When -Wunused-local-typedefs is in effect, cfun->languages is
8836 used to store data throughout the life time of the current cfun,
8837 So don't deallocate it. */
8838 if (!warn_unused_local_typedefs)
8839 cfun->language = NULL;
8840
8841 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
8842 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
8843 {
8844 /* Stop pointing to the local nodes about to be freed. */
8845 /* But DECL_INITIAL must remain nonzero so we know this
8846 was an actual function definition. */
8847 DECL_INITIAL (current_function_decl) = error_mark_node;
8848 DECL_ARGUMENTS (current_function_decl) = 0;
8849 }
8850
8851 c_stmt_tree = p->base.x_stmt_tree;
8852 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
8853 c_break_label = p->x_break_label;
8854 c_cont_label = p->x_cont_label;
8855 c_switch_stack = p->x_switch_stack;
8856 current_function_arg_info = p->arg_info;
8857 current_function_returns_value = p->returns_value;
8858 current_function_returns_null = p->returns_null;
8859 current_function_returns_abnormally = p->returns_abnormally;
8860 warn_about_return_type = p->warn_about_return_type;
8861 }
8862
8863 /* The functions below are required for functionality of doing
8864 function at once processing in the C front end. Currently these
8865 functions are not called from anywhere in the C front end, but as
8866 these changes continue, that will change. */
8867
8868 /* Returns the stmt_tree (if any) to which statements are currently
8869 being added. If there is no active statement-tree, NULL is
8870 returned. */
8871
8872 stmt_tree
8873 current_stmt_tree (void)
8874 {
8875 return &c_stmt_tree;
8876 }
8877
8878 /* Return the global value of T as a symbol. */
8879
8880 tree
8881 identifier_global_value (tree t)
8882 {
8883 struct c_binding *b;
8884
8885 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
8886 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
8887 return b->decl;
8888
8889 return 0;
8890 }
8891
8892 /* In C, the only C-linkage public declaration is at file scope. */
8893
8894 tree
8895 c_linkage_bindings (tree name)
8896 {
8897 return identifier_global_value (name);
8898 }
8899
8900 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
8901 otherwise the name is found in ridpointers from RID_INDEX. */
8902
8903 void
8904 record_builtin_type (enum rid rid_index, const char *name, tree type)
8905 {
8906 tree id, decl;
8907 if (name == 0)
8908 id = ridpointers[(int) rid_index];
8909 else
8910 id = get_identifier (name);
8911 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
8912 pushdecl (decl);
8913 if (debug_hooks->type_decl)
8914 debug_hooks->type_decl (decl, false);
8915 }
8916
8917 /* Build the void_list_node (void_type_node having been created). */
8918 tree
8919 build_void_list_node (void)
8920 {
8921 tree t = build_tree_list (NULL_TREE, void_type_node);
8922 return t;
8923 }
8924
8925 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
8926
8927 struct c_parm *
8928 build_c_parm (struct c_declspecs *specs, tree attrs,
8929 struct c_declarator *declarator)
8930 {
8931 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
8932 ret->specs = specs;
8933 ret->attrs = attrs;
8934 ret->declarator = declarator;
8935 return ret;
8936 }
8937
8938 /* Return a declarator with nested attributes. TARGET is the inner
8939 declarator to which these attributes apply. ATTRS are the
8940 attributes. */
8941
8942 struct c_declarator *
8943 build_attrs_declarator (tree attrs, struct c_declarator *target)
8944 {
8945 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8946 ret->kind = cdk_attrs;
8947 ret->declarator = target;
8948 ret->u.attrs = attrs;
8949 return ret;
8950 }
8951
8952 /* Return a declarator for a function with arguments specified by ARGS
8953 and return type specified by TARGET. */
8954
8955 struct c_declarator *
8956 build_function_declarator (struct c_arg_info *args,
8957 struct c_declarator *target)
8958 {
8959 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8960 ret->kind = cdk_function;
8961 ret->declarator = target;
8962 ret->u.arg_info = args;
8963 return ret;
8964 }
8965
8966 /* Return a declarator for the identifier IDENT (which may be
8967 NULL_TREE for an abstract declarator). */
8968
8969 struct c_declarator *
8970 build_id_declarator (tree ident)
8971 {
8972 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8973 ret->kind = cdk_id;
8974 ret->declarator = 0;
8975 ret->u.id = ident;
8976 /* Default value - may get reset to a more precise location. */
8977 ret->id_loc = input_location;
8978 return ret;
8979 }
8980
8981 /* Return something to represent absolute declarators containing a *.
8982 TARGET is the absolute declarator that the * contains.
8983 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
8984 to apply to the pointer type. */
8985
8986 struct c_declarator *
8987 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
8988 struct c_declarator *target)
8989 {
8990 tree attrs;
8991 int quals = 0;
8992 struct c_declarator *itarget = target;
8993 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8994 if (type_quals_attrs)
8995 {
8996 attrs = type_quals_attrs->attrs;
8997 quals = quals_from_declspecs (type_quals_attrs);
8998 if (attrs != NULL_TREE)
8999 itarget = build_attrs_declarator (attrs, target);
9000 }
9001 ret->kind = cdk_pointer;
9002 ret->declarator = itarget;
9003 ret->u.pointer_quals = quals;
9004 return ret;
9005 }
9006
9007 /* Return a pointer to a structure for an empty list of declaration
9008 specifiers. */
9009
9010 struct c_declspecs *
9011 build_null_declspecs (void)
9012 {
9013 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9014 memset (&ret->locations, 0, cdw_number_of_elements);
9015 ret->type = 0;
9016 ret->expr = 0;
9017 ret->decl_attr = 0;
9018 ret->attrs = 0;
9019 ret->align_log = -1;
9020 ret->typespec_word = cts_none;
9021 ret->storage_class = csc_none;
9022 ret->expr_const_operands = true;
9023 ret->declspecs_seen_p = false;
9024 ret->typespec_kind = ctsk_none;
9025 ret->non_sc_seen_p = false;
9026 ret->typedef_p = false;
9027 ret->explicit_signed_p = false;
9028 ret->deprecated_p = false;
9029 ret->default_int_p = false;
9030 ret->long_p = false;
9031 ret->long_long_p = false;
9032 ret->short_p = false;
9033 ret->signed_p = false;
9034 ret->unsigned_p = false;
9035 ret->complex_p = false;
9036 ret->inline_p = false;
9037 ret->noreturn_p = false;
9038 ret->thread_p = false;
9039 ret->thread_gnu_p = false;
9040 ret->const_p = false;
9041 ret->volatile_p = false;
9042 ret->atomic_p = false;
9043 ret->restrict_p = false;
9044 ret->saturating_p = false;
9045 ret->alignas_p = false;
9046 ret->address_space = ADDR_SPACE_GENERIC;
9047 return ret;
9048 }
9049
9050 /* Add the address space ADDRSPACE to the declaration specifiers
9051 SPECS, returning SPECS. */
9052
9053 struct c_declspecs *
9054 declspecs_add_addrspace (source_location location,
9055 struct c_declspecs *specs, addr_space_t as)
9056 {
9057 specs->non_sc_seen_p = true;
9058 specs->declspecs_seen_p = true;
9059
9060 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9061 && specs->address_space != as)
9062 error ("incompatible address space qualifiers %qs and %qs",
9063 c_addr_space_name (as),
9064 c_addr_space_name (specs->address_space));
9065 else
9066 {
9067 specs->address_space = as;
9068 specs->locations[cdw_address_space] = location;
9069 }
9070 return specs;
9071 }
9072
9073 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9074 returning SPECS. */
9075
9076 struct c_declspecs *
9077 declspecs_add_qual (source_location loc,
9078 struct c_declspecs *specs, tree qual)
9079 {
9080 enum rid i;
9081 bool dupe = false;
9082 specs->non_sc_seen_p = true;
9083 specs->declspecs_seen_p = true;
9084 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9085 && C_IS_RESERVED_WORD (qual));
9086 i = C_RID_CODE (qual);
9087 switch (i)
9088 {
9089 case RID_CONST:
9090 dupe = specs->const_p;
9091 specs->const_p = true;
9092 specs->locations[cdw_const] = loc;
9093 break;
9094 case RID_VOLATILE:
9095 dupe = specs->volatile_p;
9096 specs->volatile_p = true;
9097 specs->locations[cdw_volatile] = loc;
9098 break;
9099 case RID_RESTRICT:
9100 dupe = specs->restrict_p;
9101 specs->restrict_p = true;
9102 specs->locations[cdw_restrict] = loc;
9103 break;
9104 case RID_ATOMIC:
9105 dupe = specs->atomic_p;
9106 specs->atomic_p = true;
9107 break;
9108 default:
9109 gcc_unreachable ();
9110 }
9111 if (dupe)
9112 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %qE", qual);
9113 return specs;
9114 }
9115
9116 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9117 returning SPECS. */
9118
9119 struct c_declspecs *
9120 declspecs_add_type (location_t loc, struct c_declspecs *specs,
9121 struct c_typespec spec)
9122 {
9123 tree type = spec.spec;
9124 specs->non_sc_seen_p = true;
9125 specs->declspecs_seen_p = true;
9126 specs->typespec_kind = spec.kind;
9127 if (TREE_DEPRECATED (type))
9128 specs->deprecated_p = true;
9129
9130 /* Handle type specifier keywords. */
9131 if (TREE_CODE (type) == IDENTIFIER_NODE
9132 && C_IS_RESERVED_WORD (type)
9133 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
9134 {
9135 enum rid i = C_RID_CODE (type);
9136 if (specs->type)
9137 {
9138 error_at (loc, "two or more data types in declaration specifiers");
9139 return specs;
9140 }
9141 if ((int) i <= (int) RID_LAST_MODIFIER)
9142 {
9143 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9144 bool dupe = false;
9145 switch (i)
9146 {
9147 case RID_LONG:
9148 if (specs->long_long_p)
9149 {
9150 error_at (loc, "%<long long long%> is too long for GCC");
9151 break;
9152 }
9153 if (specs->long_p)
9154 {
9155 if (specs->typespec_word == cts_double)
9156 {
9157 error_at (loc,
9158 ("both %<long long%> and %<double%> in "
9159 "declaration specifiers"));
9160 break;
9161 }
9162 pedwarn_c90 (loc, OPT_Wlong_long,
9163 "ISO C90 does not support %<long long%>");
9164 specs->long_long_p = 1;
9165 specs->locations[cdw_long_long] = loc;
9166 break;
9167 }
9168 if (specs->short_p)
9169 error_at (loc,
9170 ("both %<long%> and %<short%> in "
9171 "declaration specifiers"));
9172 else if (specs->typespec_word == cts_auto_type)
9173 error_at (loc,
9174 ("both %<long%> and %<__auto_type%> in "
9175 "declaration specifiers"));
9176 else if (specs->typespec_word == cts_void)
9177 error_at (loc,
9178 ("both %<long%> and %<void%> in "
9179 "declaration specifiers"));
9180 else if (specs->typespec_word == cts_int128)
9181 error_at (loc,
9182 ("both %<long%> and %<__int128%> in "
9183 "declaration specifiers"));
9184 else if (specs->typespec_word == cts_bool)
9185 error_at (loc,
9186 ("both %<long%> and %<_Bool%> in "
9187 "declaration specifiers"));
9188 else if (specs->typespec_word == cts_char)
9189 error_at (loc,
9190 ("both %<long%> and %<char%> in "
9191 "declaration specifiers"));
9192 else if (specs->typespec_word == cts_float)
9193 error_at (loc,
9194 ("both %<long%> and %<float%> in "
9195 "declaration specifiers"));
9196 else if (specs->typespec_word == cts_dfloat32)
9197 error_at (loc,
9198 ("both %<long%> and %<_Decimal32%> in "
9199 "declaration specifiers"));
9200 else if (specs->typespec_word == cts_dfloat64)
9201 error_at (loc,
9202 ("both %<long%> and %<_Decimal64%> in "
9203 "declaration specifiers"));
9204 else if (specs->typespec_word == cts_dfloat128)
9205 error_at (loc,
9206 ("both %<long%> and %<_Decimal128%> in "
9207 "declaration specifiers"));
9208 else
9209 {
9210 specs->long_p = true;
9211 specs->locations[cdw_long] = loc;
9212 }
9213 break;
9214 case RID_SHORT:
9215 dupe = specs->short_p;
9216 if (specs->long_p)
9217 error_at (loc,
9218 ("both %<long%> and %<short%> in "
9219 "declaration specifiers"));
9220 else if (specs->typespec_word == cts_auto_type)
9221 error_at (loc,
9222 ("both %<short%> and %<__auto_type%> in "
9223 "declaration specifiers"));
9224 else if (specs->typespec_word == cts_void)
9225 error_at (loc,
9226 ("both %<short%> and %<void%> in "
9227 "declaration specifiers"));
9228 else if (specs->typespec_word == cts_int128)
9229 error_at (loc,
9230 ("both %<short%> and %<__int128%> in "
9231 "declaration specifiers"));
9232 else if (specs->typespec_word == cts_bool)
9233 error_at (loc,
9234 ("both %<short%> and %<_Bool%> in "
9235 "declaration specifiers"));
9236 else if (specs->typespec_word == cts_char)
9237 error_at (loc,
9238 ("both %<short%> and %<char%> in "
9239 "declaration specifiers"));
9240 else if (specs->typespec_word == cts_float)
9241 error_at (loc,
9242 ("both %<short%> and %<float%> in "
9243 "declaration specifiers"));
9244 else if (specs->typespec_word == cts_double)
9245 error_at (loc,
9246 ("both %<short%> and %<double%> in "
9247 "declaration specifiers"));
9248 else if (specs->typespec_word == cts_dfloat32)
9249 error_at (loc,
9250 ("both %<short%> and %<_Decimal32%> in "
9251 "declaration specifiers"));
9252 else if (specs->typespec_word == cts_dfloat64)
9253 error_at (loc,
9254 ("both %<short%> and %<_Decimal64%> in "
9255 "declaration specifiers"));
9256 else if (specs->typespec_word == cts_dfloat128)
9257 error_at (loc,
9258 ("both %<short%> and %<_Decimal128%> in "
9259 "declaration specifiers"));
9260 else
9261 {
9262 specs->short_p = true;
9263 specs->locations[cdw_short] = loc;
9264 }
9265 break;
9266 case RID_SIGNED:
9267 dupe = specs->signed_p;
9268 if (specs->unsigned_p)
9269 error_at (loc,
9270 ("both %<signed%> and %<unsigned%> in "
9271 "declaration specifiers"));
9272 else if (specs->typespec_word == cts_auto_type)
9273 error_at (loc,
9274 ("both %<signed%> and %<__auto_type%> in "
9275 "declaration specifiers"));
9276 else if (specs->typespec_word == cts_void)
9277 error_at (loc,
9278 ("both %<signed%> and %<void%> in "
9279 "declaration specifiers"));
9280 else if (specs->typespec_word == cts_bool)
9281 error_at (loc,
9282 ("both %<signed%> and %<_Bool%> in "
9283 "declaration specifiers"));
9284 else if (specs->typespec_word == cts_float)
9285 error_at (loc,
9286 ("both %<signed%> and %<float%> in "
9287 "declaration specifiers"));
9288 else if (specs->typespec_word == cts_double)
9289 error_at (loc,
9290 ("both %<signed%> and %<double%> in "
9291 "declaration specifiers"));
9292 else if (specs->typespec_word == cts_dfloat32)
9293 error_at (loc,
9294 ("both %<signed%> and %<_Decimal32%> in "
9295 "declaration specifiers"));
9296 else if (specs->typespec_word == cts_dfloat64)
9297 error_at (loc,
9298 ("both %<signed%> and %<_Decimal64%> in "
9299 "declaration specifiers"));
9300 else if (specs->typespec_word == cts_dfloat128)
9301 error_at (loc,
9302 ("both %<signed%> and %<_Decimal128%> in "
9303 "declaration specifiers"));
9304 else
9305 {
9306 specs->signed_p = true;
9307 specs->locations[cdw_signed] = loc;
9308 }
9309 break;
9310 case RID_UNSIGNED:
9311 dupe = specs->unsigned_p;
9312 if (specs->signed_p)
9313 error_at (loc,
9314 ("both %<signed%> and %<unsigned%> in "
9315 "declaration specifiers"));
9316 else if (specs->typespec_word == cts_auto_type)
9317 error_at (loc,
9318 ("both %<unsigned%> and %<__auto_type%> in "
9319 "declaration specifiers"));
9320 else if (specs->typespec_word == cts_void)
9321 error_at (loc,
9322 ("both %<unsigned%> and %<void%> in "
9323 "declaration specifiers"));
9324 else if (specs->typespec_word == cts_bool)
9325 error_at (loc,
9326 ("both %<unsigned%> and %<_Bool%> in "
9327 "declaration specifiers"));
9328 else if (specs->typespec_word == cts_float)
9329 error_at (loc,
9330 ("both %<unsigned%> and %<float%> in "
9331 "declaration specifiers"));
9332 else if (specs->typespec_word == cts_double)
9333 error_at (loc,
9334 ("both %<unsigned%> and %<double%> in "
9335 "declaration specifiers"));
9336 else if (specs->typespec_word == cts_dfloat32)
9337 error_at (loc,
9338 ("both %<unsigned%> and %<_Decimal32%> in "
9339 "declaration specifiers"));
9340 else if (specs->typespec_word == cts_dfloat64)
9341 error_at (loc,
9342 ("both %<unsigned%> and %<_Decimal64%> in "
9343 "declaration specifiers"));
9344 else if (specs->typespec_word == cts_dfloat128)
9345 error_at (loc,
9346 ("both %<unsigned%> and %<_Decimal128%> in "
9347 "declaration specifiers"));
9348 else
9349 {
9350 specs->unsigned_p = true;
9351 specs->locations[cdw_unsigned] = loc;
9352 }
9353 break;
9354 case RID_COMPLEX:
9355 dupe = specs->complex_p;
9356 if (!in_system_header_at (loc))
9357 pedwarn_c90 (loc, OPT_Wpedantic,
9358 "ISO C90 does not support complex types");
9359 if (specs->typespec_word == cts_auto_type)
9360 error_at (loc,
9361 ("both %<complex%> and %<__auto_type%> in "
9362 "declaration specifiers"));
9363 else if (specs->typespec_word == cts_void)
9364 error_at (loc,
9365 ("both %<complex%> and %<void%> in "
9366 "declaration specifiers"));
9367 else if (specs->typespec_word == cts_bool)
9368 error_at (loc,
9369 ("both %<complex%> and %<_Bool%> in "
9370 "declaration specifiers"));
9371 else if (specs->typespec_word == cts_dfloat32)
9372 error_at (loc,
9373 ("both %<complex%> and %<_Decimal32%> in "
9374 "declaration specifiers"));
9375 else if (specs->typespec_word == cts_dfloat64)
9376 error_at (loc,
9377 ("both %<complex%> and %<_Decimal64%> in "
9378 "declaration specifiers"));
9379 else if (specs->typespec_word == cts_dfloat128)
9380 error_at (loc,
9381 ("both %<complex%> and %<_Decimal128%> in "
9382 "declaration specifiers"));
9383 else if (specs->typespec_word == cts_fract)
9384 error_at (loc,
9385 ("both %<complex%> and %<_Fract%> in "
9386 "declaration specifiers"));
9387 else if (specs->typespec_word == cts_accum)
9388 error_at (loc,
9389 ("both %<complex%> and %<_Accum%> in "
9390 "declaration specifiers"));
9391 else if (specs->saturating_p)
9392 error_at (loc,
9393 ("both %<complex%> and %<_Sat%> in "
9394 "declaration specifiers"));
9395 else
9396 {
9397 specs->complex_p = true;
9398 specs->locations[cdw_complex] = loc;
9399 }
9400 break;
9401 case RID_SAT:
9402 dupe = specs->saturating_p;
9403 pedwarn (loc, OPT_Wpedantic,
9404 "ISO C does not support saturating types");
9405 if (specs->typespec_word == cts_int128)
9406 {
9407 error_at (loc,
9408 ("both %<_Sat%> and %<__int128%> in "
9409 "declaration specifiers"));
9410 }
9411 else if (specs->typespec_word == cts_auto_type)
9412 error_at (loc,
9413 ("both %<_Sat%> and %<__auto_type%> in "
9414 "declaration specifiers"));
9415 else if (specs->typespec_word == cts_void)
9416 error_at (loc,
9417 ("both %<_Sat%> and %<void%> in "
9418 "declaration specifiers"));
9419 else if (specs->typespec_word == cts_bool)
9420 error_at (loc,
9421 ("both %<_Sat%> and %<_Bool%> in "
9422 "declaration specifiers"));
9423 else if (specs->typespec_word == cts_char)
9424 error_at (loc,
9425 ("both %<_Sat%> and %<char%> in "
9426 "declaration specifiers"));
9427 else if (specs->typespec_word == cts_int)
9428 error_at (loc,
9429 ("both %<_Sat%> and %<int%> in "
9430 "declaration specifiers"));
9431 else if (specs->typespec_word == cts_float)
9432 error_at (loc,
9433 ("both %<_Sat%> and %<float%> in "
9434 "declaration specifiers"));
9435 else if (specs->typespec_word == cts_double)
9436 error_at (loc,
9437 ("both %<_Sat%> and %<double%> in "
9438 "declaration specifiers"));
9439 else if (specs->typespec_word == cts_dfloat32)
9440 error_at (loc,
9441 ("both %<_Sat%> and %<_Decimal32%> in "
9442 "declaration specifiers"));
9443 else if (specs->typespec_word == cts_dfloat64)
9444 error_at (loc,
9445 ("both %<_Sat%> and %<_Decimal64%> in "
9446 "declaration specifiers"));
9447 else if (specs->typespec_word == cts_dfloat128)
9448 error_at (loc,
9449 ("both %<_Sat%> and %<_Decimal128%> in "
9450 "declaration specifiers"));
9451 else if (specs->complex_p)
9452 error_at (loc,
9453 ("both %<_Sat%> and %<complex%> in "
9454 "declaration specifiers"));
9455 else
9456 {
9457 specs->saturating_p = true;
9458 specs->locations[cdw_saturating] = loc;
9459 }
9460 break;
9461 default:
9462 gcc_unreachable ();
9463 }
9464
9465 if (dupe)
9466 error_at (loc, "duplicate %qE", type);
9467
9468 return specs;
9469 }
9470 else
9471 {
9472 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
9473 "__int128", "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
9474 "__auto_type". */
9475 if (specs->typespec_word != cts_none)
9476 {
9477 error_at (loc,
9478 "two or more data types in declaration specifiers");
9479 return specs;
9480 }
9481 switch (i)
9482 {
9483 case RID_AUTO_TYPE:
9484 if (specs->long_p)
9485 error_at (loc,
9486 ("both %<long%> and %<__auto_type%> in "
9487 "declaration specifiers"));
9488 else if (specs->short_p)
9489 error_at (loc,
9490 ("both %<short%> and %<__auto_type%> in "
9491 "declaration specifiers"));
9492 else if (specs->signed_p)
9493 error_at (loc,
9494 ("both %<signed%> and %<__auto_type%> in "
9495 "declaration specifiers"));
9496 else if (specs->unsigned_p)
9497 error_at (loc,
9498 ("both %<unsigned%> and %<__auto_type%> in "
9499 "declaration specifiers"));
9500 else if (specs->complex_p)
9501 error_at (loc,
9502 ("both %<complex%> and %<__auto_type%> in "
9503 "declaration specifiers"));
9504 else if (specs->saturating_p)
9505 error_at (loc,
9506 ("both %<_Sat%> and %<__auto_type%> in "
9507 "declaration specifiers"));
9508 else
9509 {
9510 specs->typespec_word = cts_auto_type;
9511 specs->locations[cdw_typespec] = loc;
9512 }
9513 return specs;
9514 case RID_INT128:
9515 if (int128_integer_type_node == NULL_TREE)
9516 {
9517 error_at (loc, "%<__int128%> is not supported for this target");
9518 return specs;
9519 }
9520 if (!in_system_header_at (input_location))
9521 pedwarn (loc, OPT_Wpedantic,
9522 "ISO C does not support %<__int128%> type");
9523
9524 if (specs->long_p)
9525 error_at (loc,
9526 ("both %<__int128%> and %<long%> in "
9527 "declaration specifiers"));
9528 else if (specs->saturating_p)
9529 error_at (loc,
9530 ("both %<_Sat%> and %<__int128%> in "
9531 "declaration specifiers"));
9532 else if (specs->short_p)
9533 error_at (loc,
9534 ("both %<__int128%> and %<short%> in "
9535 "declaration specifiers"));
9536 else
9537 {
9538 specs->typespec_word = cts_int128;
9539 specs->locations[cdw_typespec] = loc;
9540 }
9541 return specs;
9542 case RID_VOID:
9543 if (specs->long_p)
9544 error_at (loc,
9545 ("both %<long%> and %<void%> in "
9546 "declaration specifiers"));
9547 else if (specs->short_p)
9548 error_at (loc,
9549 ("both %<short%> and %<void%> in "
9550 "declaration specifiers"));
9551 else if (specs->signed_p)
9552 error_at (loc,
9553 ("both %<signed%> and %<void%> in "
9554 "declaration specifiers"));
9555 else if (specs->unsigned_p)
9556 error_at (loc,
9557 ("both %<unsigned%> and %<void%> in "
9558 "declaration specifiers"));
9559 else if (specs->complex_p)
9560 error_at (loc,
9561 ("both %<complex%> and %<void%> in "
9562 "declaration specifiers"));
9563 else if (specs->saturating_p)
9564 error_at (loc,
9565 ("both %<_Sat%> and %<void%> in "
9566 "declaration specifiers"));
9567 else
9568 {
9569 specs->typespec_word = cts_void;
9570 specs->locations[cdw_typespec] = loc;
9571 }
9572 return specs;
9573 case RID_BOOL:
9574 if (!in_system_header_at (loc))
9575 pedwarn_c90 (loc, OPT_Wpedantic,
9576 "ISO C90 does not support boolean types");
9577 if (specs->long_p)
9578 error_at (loc,
9579 ("both %<long%> and %<_Bool%> in "
9580 "declaration specifiers"));
9581 else if (specs->short_p)
9582 error_at (loc,
9583 ("both %<short%> and %<_Bool%> in "
9584 "declaration specifiers"));
9585 else if (specs->signed_p)
9586 error_at (loc,
9587 ("both %<signed%> and %<_Bool%> in "
9588 "declaration specifiers"));
9589 else if (specs->unsigned_p)
9590 error_at (loc,
9591 ("both %<unsigned%> and %<_Bool%> in "
9592 "declaration specifiers"));
9593 else if (specs->complex_p)
9594 error_at (loc,
9595 ("both %<complex%> and %<_Bool%> in "
9596 "declaration specifiers"));
9597 else if (specs->saturating_p)
9598 error_at (loc,
9599 ("both %<_Sat%> and %<_Bool%> in "
9600 "declaration specifiers"));
9601 else
9602 {
9603 specs->typespec_word = cts_bool;
9604 specs->locations[cdw_typespec] = loc;
9605 }
9606 return specs;
9607 case RID_CHAR:
9608 if (specs->long_p)
9609 error_at (loc,
9610 ("both %<long%> and %<char%> in "
9611 "declaration specifiers"));
9612 else if (specs->short_p)
9613 error_at (loc,
9614 ("both %<short%> and %<char%> in "
9615 "declaration specifiers"));
9616 else if (specs->saturating_p)
9617 error_at (loc,
9618 ("both %<_Sat%> and %<char%> in "
9619 "declaration specifiers"));
9620 else
9621 {
9622 specs->typespec_word = cts_char;
9623 specs->locations[cdw_typespec] = loc;
9624 }
9625 return specs;
9626 case RID_INT:
9627 if (specs->saturating_p)
9628 error_at (loc,
9629 ("both %<_Sat%> and %<int%> in "
9630 "declaration specifiers"));
9631 else
9632 {
9633 specs->typespec_word = cts_int;
9634 specs->locations[cdw_typespec] = loc;
9635 }
9636 return specs;
9637 case RID_FLOAT:
9638 if (specs->long_p)
9639 error_at (loc,
9640 ("both %<long%> and %<float%> in "
9641 "declaration specifiers"));
9642 else if (specs->short_p)
9643 error_at (loc,
9644 ("both %<short%> and %<float%> in "
9645 "declaration specifiers"));
9646 else if (specs->signed_p)
9647 error_at (loc,
9648 ("both %<signed%> and %<float%> in "
9649 "declaration specifiers"));
9650 else if (specs->unsigned_p)
9651 error_at (loc,
9652 ("both %<unsigned%> and %<float%> in "
9653 "declaration specifiers"));
9654 else if (specs->saturating_p)
9655 error_at (loc,
9656 ("both %<_Sat%> and %<float%> in "
9657 "declaration specifiers"));
9658 else
9659 {
9660 specs->typespec_word = cts_float;
9661 specs->locations[cdw_typespec] = loc;
9662 }
9663 return specs;
9664 case RID_DOUBLE:
9665 if (specs->long_long_p)
9666 error_at (loc,
9667 ("both %<long long%> and %<double%> in "
9668 "declaration specifiers"));
9669 else if (specs->short_p)
9670 error_at (loc,
9671 ("both %<short%> and %<double%> in "
9672 "declaration specifiers"));
9673 else if (specs->signed_p)
9674 error_at (loc,
9675 ("both %<signed%> and %<double%> in "
9676 "declaration specifiers"));
9677 else if (specs->unsigned_p)
9678 error_at (loc,
9679 ("both %<unsigned%> and %<double%> in "
9680 "declaration specifiers"));
9681 else if (specs->saturating_p)
9682 error_at (loc,
9683 ("both %<_Sat%> and %<double%> in "
9684 "declaration specifiers"));
9685 else
9686 {
9687 specs->typespec_word = cts_double;
9688 specs->locations[cdw_typespec] = loc;
9689 }
9690 return specs;
9691 case RID_DFLOAT32:
9692 case RID_DFLOAT64:
9693 case RID_DFLOAT128:
9694 {
9695 const char *str;
9696 if (i == RID_DFLOAT32)
9697 str = "_Decimal32";
9698 else if (i == RID_DFLOAT64)
9699 str = "_Decimal64";
9700 else
9701 str = "_Decimal128";
9702 if (specs->long_long_p)
9703 error_at (loc,
9704 ("both %<long long%> and %<%s%> in "
9705 "declaration specifiers"),
9706 str);
9707 if (specs->long_p)
9708 error_at (loc,
9709 ("both %<long%> and %<%s%> in "
9710 "declaration specifiers"),
9711 str);
9712 else if (specs->short_p)
9713 error_at (loc,
9714 ("both %<short%> and %<%s%> in "
9715 "declaration specifiers"),
9716 str);
9717 else if (specs->signed_p)
9718 error_at (loc,
9719 ("both %<signed%> and %<%s%> in "
9720 "declaration specifiers"),
9721 str);
9722 else if (specs->unsigned_p)
9723 error_at (loc,
9724 ("both %<unsigned%> and %<%s%> in "
9725 "declaration specifiers"),
9726 str);
9727 else if (specs->complex_p)
9728 error_at (loc,
9729 ("both %<complex%> and %<%s%> in "
9730 "declaration specifiers"),
9731 str);
9732 else if (specs->saturating_p)
9733 error_at (loc,
9734 ("both %<_Sat%> and %<%s%> in "
9735 "declaration specifiers"),
9736 str);
9737 else if (i == RID_DFLOAT32)
9738 specs->typespec_word = cts_dfloat32;
9739 else if (i == RID_DFLOAT64)
9740 specs->typespec_word = cts_dfloat64;
9741 else
9742 specs->typespec_word = cts_dfloat128;
9743 specs->locations[cdw_typespec] = loc;
9744 }
9745 if (!targetm.decimal_float_supported_p ())
9746 error_at (loc,
9747 ("decimal floating point not supported "
9748 "for this target"));
9749 pedwarn (loc, OPT_Wpedantic,
9750 "ISO C does not support decimal floating point");
9751 return specs;
9752 case RID_FRACT:
9753 case RID_ACCUM:
9754 {
9755 const char *str;
9756 if (i == RID_FRACT)
9757 str = "_Fract";
9758 else
9759 str = "_Accum";
9760 if (specs->complex_p)
9761 error_at (loc,
9762 ("both %<complex%> and %<%s%> in "
9763 "declaration specifiers"),
9764 str);
9765 else if (i == RID_FRACT)
9766 specs->typespec_word = cts_fract;
9767 else
9768 specs->typespec_word = cts_accum;
9769 specs->locations[cdw_typespec] = loc;
9770 }
9771 if (!targetm.fixed_point_supported_p ())
9772 error_at (loc,
9773 "fixed-point types not supported for this target");
9774 pedwarn (loc, OPT_Wpedantic,
9775 "ISO C does not support fixed-point types");
9776 return specs;
9777 default:
9778 /* ObjC reserved word "id", handled below. */
9779 break;
9780 }
9781 }
9782 }
9783
9784 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
9785 form of ObjC type, cases such as "int" and "long" being handled
9786 above), a TYPE (struct, union, enum and typeof specifiers) or an
9787 ERROR_MARK. In none of these cases may there have previously
9788 been any type specifiers. */
9789 if (specs->type || specs->typespec_word != cts_none
9790 || specs->long_p || specs->short_p || specs->signed_p
9791 || specs->unsigned_p || specs->complex_p)
9792 error_at (loc, "two or more data types in declaration specifiers");
9793 else if (TREE_CODE (type) == TYPE_DECL)
9794 {
9795 if (TREE_TYPE (type) == error_mark_node)
9796 ; /* Allow the type to default to int to avoid cascading errors. */
9797 else
9798 {
9799 specs->type = TREE_TYPE (type);
9800 specs->decl_attr = DECL_ATTRIBUTES (type);
9801 specs->typedef_p = true;
9802 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
9803 specs->locations[cdw_typedef] = loc;
9804
9805 /* If this typedef name is defined in a struct, then a C++
9806 lookup would return a different value. */
9807 if (warn_cxx_compat
9808 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
9809 warning_at (loc, OPT_Wc___compat,
9810 "C++ lookup of %qD would return a field, not a type",
9811 type);
9812
9813 /* If we are parsing a struct, record that a struct field
9814 used a typedef. */
9815 if (warn_cxx_compat && struct_parse_info != NULL)
9816 struct_parse_info->typedefs_seen.safe_push (type);
9817 }
9818 }
9819 else if (TREE_CODE (type) == IDENTIFIER_NODE)
9820 {
9821 tree t = lookup_name (type);
9822 if (!t || TREE_CODE (t) != TYPE_DECL)
9823 error_at (loc, "%qE fails to be a typedef or built in type", type);
9824 else if (TREE_TYPE (t) == error_mark_node)
9825 ;
9826 else
9827 {
9828 specs->type = TREE_TYPE (t);
9829 specs->locations[cdw_typespec] = loc;
9830 }
9831 }
9832 else
9833 {
9834 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
9835 {
9836 specs->typedef_p = true;
9837 specs->locations[cdw_typedef] = loc;
9838 if (spec.expr)
9839 {
9840 if (specs->expr)
9841 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
9842 specs->expr, spec.expr);
9843 else
9844 specs->expr = spec.expr;
9845 specs->expr_const_operands &= spec.expr_const_operands;
9846 }
9847 }
9848 specs->type = type;
9849 }
9850
9851 return specs;
9852 }
9853
9854 /* Add the storage class specifier or function specifier SCSPEC to the
9855 declaration specifiers SPECS, returning SPECS. */
9856
9857 struct c_declspecs *
9858 declspecs_add_scspec (source_location loc,
9859 struct c_declspecs *specs,
9860 tree scspec)
9861 {
9862 enum rid i;
9863 enum c_storage_class n = csc_none;
9864 bool dupe = false;
9865 specs->declspecs_seen_p = true;
9866 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
9867 && C_IS_RESERVED_WORD (scspec));
9868 i = C_RID_CODE (scspec);
9869 if (specs->non_sc_seen_p)
9870 warning (OPT_Wold_style_declaration,
9871 "%qE is not at beginning of declaration", scspec);
9872 switch (i)
9873 {
9874 case RID_INLINE:
9875 /* C99 permits duplicate inline. Although of doubtful utility,
9876 it seems simplest to permit it in gnu89 mode as well, as
9877 there is also little utility in maintaining this as a
9878 difference between gnu89 and C99 inline. */
9879 dupe = false;
9880 specs->inline_p = true;
9881 specs->locations[cdw_inline] = loc;
9882 break;
9883 case RID_NORETURN:
9884 /* Duplicate _Noreturn is permitted. */
9885 dupe = false;
9886 specs->noreturn_p = true;
9887 specs->locations[cdw_noreturn] = loc;
9888 break;
9889 case RID_THREAD:
9890 dupe = specs->thread_p;
9891 if (specs->storage_class == csc_auto)
9892 error ("%qE used with %<auto%>", scspec);
9893 else if (specs->storage_class == csc_register)
9894 error ("%qE used with %<register%>", scspec);
9895 else if (specs->storage_class == csc_typedef)
9896 error ("%qE used with %<typedef%>", scspec);
9897 else
9898 {
9899 specs->thread_p = true;
9900 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
9901 "__thread") == 0);
9902 /* A diagnostic is not required for the use of this
9903 identifier in the implementation namespace; only diagnose
9904 it for the C11 spelling because of existing code using
9905 the other spelling. */
9906 if (!specs->thread_gnu_p)
9907 {
9908 if (flag_isoc99)
9909 pedwarn_c99 (loc, OPT_Wpedantic,
9910 "ISO C99 does not support %qE", scspec);
9911 else
9912 pedwarn_c99 (loc, OPT_Wpedantic,
9913 "ISO C90 does not support %qE", scspec);
9914 }
9915 specs->locations[cdw_thread] = loc;
9916 }
9917 break;
9918 case RID_AUTO:
9919 n = csc_auto;
9920 break;
9921 case RID_EXTERN:
9922 n = csc_extern;
9923 /* Diagnose "__thread extern". */
9924 if (specs->thread_p && specs->thread_gnu_p)
9925 error ("%<__thread%> before %<extern%>");
9926 break;
9927 case RID_REGISTER:
9928 n = csc_register;
9929 break;
9930 case RID_STATIC:
9931 n = csc_static;
9932 /* Diagnose "__thread static". */
9933 if (specs->thread_p && specs->thread_gnu_p)
9934 error ("%<__thread%> before %<static%>");
9935 break;
9936 case RID_TYPEDEF:
9937 n = csc_typedef;
9938 break;
9939 default:
9940 gcc_unreachable ();
9941 }
9942 if (n != csc_none && n == specs->storage_class)
9943 dupe = true;
9944 if (dupe)
9945 {
9946 if (i == RID_THREAD)
9947 error ("duplicate %<_Thread_local%> or %<__thread%>");
9948 else
9949 error ("duplicate %qE", scspec);
9950 }
9951 if (n != csc_none)
9952 {
9953 if (specs->storage_class != csc_none && n != specs->storage_class)
9954 {
9955 error ("multiple storage classes in declaration specifiers");
9956 }
9957 else
9958 {
9959 specs->storage_class = n;
9960 specs->locations[cdw_storage_class] = loc;
9961 if (n != csc_extern && n != csc_static && specs->thread_p)
9962 {
9963 error ("%qs used with %qE",
9964 specs->thread_gnu_p ? "__thread" : "_Thread_local",
9965 scspec);
9966 specs->thread_p = false;
9967 }
9968 }
9969 }
9970 return specs;
9971 }
9972
9973 /* Add the attributes ATTRS to the declaration specifiers SPECS,
9974 returning SPECS. */
9975
9976 struct c_declspecs *
9977 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
9978 {
9979 specs->attrs = chainon (attrs, specs->attrs);
9980 specs->locations[cdw_attributes] = loc;
9981 specs->declspecs_seen_p = true;
9982 return specs;
9983 }
9984
9985 /* Add an _Alignas specifier (expression ALIGN, or type whose
9986 alignment is ALIGN) to the declaration specifiers SPECS, returning
9987 SPECS. */
9988 struct c_declspecs *
9989 declspecs_add_alignas (source_location loc,
9990 struct c_declspecs *specs, tree align)
9991 {
9992 int align_log;
9993 specs->alignas_p = true;
9994 specs->locations[cdw_alignas] = loc;
9995 if (align == error_mark_node)
9996 return specs;
9997 align_log = check_user_alignment (align, true);
9998 if (align_log > specs->align_log)
9999 specs->align_log = align_log;
10000 return specs;
10001 }
10002
10003 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10004 specifiers with any other type specifier to determine the resulting
10005 type. This is where ISO C checks on complex types are made, since
10006 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10007 double". */
10008
10009 struct c_declspecs *
10010 finish_declspecs (struct c_declspecs *specs)
10011 {
10012 /* If a type was specified as a whole, we have no modifiers and are
10013 done. */
10014 if (specs->type != NULL_TREE)
10015 {
10016 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10017 && !specs->signed_p && !specs->unsigned_p
10018 && !specs->complex_p);
10019
10020 /* Set a dummy type. */
10021 if (TREE_CODE (specs->type) == ERROR_MARK)
10022 specs->type = integer_type_node;
10023 return specs;
10024 }
10025
10026 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10027 has been specified, treat it as "int" unless "_Complex" is
10028 present and there are no other specifiers. If we just have
10029 "_Complex", it is equivalent to "_Complex double", but e.g.
10030 "_Complex short" is equivalent to "_Complex short int". */
10031 if (specs->typespec_word == cts_none)
10032 {
10033 if (specs->saturating_p)
10034 {
10035 error_at (specs->locations[cdw_saturating],
10036 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10037 if (!targetm.fixed_point_supported_p ())
10038 error_at (specs->locations[cdw_saturating],
10039 "fixed-point types not supported for this target");
10040 specs->typespec_word = cts_fract;
10041 }
10042 else if (specs->long_p || specs->short_p
10043 || specs->signed_p || specs->unsigned_p)
10044 {
10045 specs->typespec_word = cts_int;
10046 }
10047 else if (specs->complex_p)
10048 {
10049 specs->typespec_word = cts_double;
10050 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10051 "ISO C does not support plain %<complex%> meaning "
10052 "%<double complex%>");
10053 }
10054 else
10055 {
10056 specs->typespec_word = cts_int;
10057 specs->default_int_p = true;
10058 /* We don't diagnose this here because grokdeclarator will
10059 give more specific diagnostics according to whether it is
10060 a function definition. */
10061 }
10062 }
10063
10064 /* If "signed" was specified, record this to distinguish "int" and
10065 "signed int" in the case of a bit-field with
10066 -funsigned-bitfields. */
10067 specs->explicit_signed_p = specs->signed_p;
10068
10069 /* Now compute the actual type. */
10070 switch (specs->typespec_word)
10071 {
10072 case cts_auto_type:
10073 gcc_assert (!specs->long_p && !specs->short_p
10074 && !specs->signed_p && !specs->unsigned_p
10075 && !specs->complex_p);
10076 /* Type to be filled in later. */
10077 break;
10078 case cts_void:
10079 gcc_assert (!specs->long_p && !specs->short_p
10080 && !specs->signed_p && !specs->unsigned_p
10081 && !specs->complex_p);
10082 specs->type = void_type_node;
10083 break;
10084 case cts_bool:
10085 gcc_assert (!specs->long_p && !specs->short_p
10086 && !specs->signed_p && !specs->unsigned_p
10087 && !specs->complex_p);
10088 specs->type = boolean_type_node;
10089 break;
10090 case cts_char:
10091 gcc_assert (!specs->long_p && !specs->short_p);
10092 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10093 if (specs->signed_p)
10094 specs->type = signed_char_type_node;
10095 else if (specs->unsigned_p)
10096 specs->type = unsigned_char_type_node;
10097 else
10098 specs->type = char_type_node;
10099 if (specs->complex_p)
10100 {
10101 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10102 "ISO C does not support complex integer types");
10103 specs->type = build_complex_type (specs->type);
10104 }
10105 break;
10106 case cts_int128:
10107 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
10108 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10109 specs->type = (specs->unsigned_p
10110 ? int128_unsigned_type_node
10111 : int128_integer_type_node);
10112 if (specs->complex_p)
10113 {
10114 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10115 "ISO C does not support complex integer types");
10116 specs->type = build_complex_type (specs->type);
10117 }
10118 break;
10119 case cts_int:
10120 gcc_assert (!(specs->long_p && specs->short_p));
10121 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10122 if (specs->long_long_p)
10123 specs->type = (specs->unsigned_p
10124 ? long_long_unsigned_type_node
10125 : long_long_integer_type_node);
10126 else if (specs->long_p)
10127 specs->type = (specs->unsigned_p
10128 ? long_unsigned_type_node
10129 : long_integer_type_node);
10130 else if (specs->short_p)
10131 specs->type = (specs->unsigned_p
10132 ? short_unsigned_type_node
10133 : short_integer_type_node);
10134 else
10135 specs->type = (specs->unsigned_p
10136 ? unsigned_type_node
10137 : integer_type_node);
10138 if (specs->complex_p)
10139 {
10140 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10141 "ISO C does not support complex integer types");
10142 specs->type = build_complex_type (specs->type);
10143 }
10144 break;
10145 case cts_float:
10146 gcc_assert (!specs->long_p && !specs->short_p
10147 && !specs->signed_p && !specs->unsigned_p);
10148 specs->type = (specs->complex_p
10149 ? complex_float_type_node
10150 : float_type_node);
10151 break;
10152 case cts_double:
10153 gcc_assert (!specs->long_long_p && !specs->short_p
10154 && !specs->signed_p && !specs->unsigned_p);
10155 if (specs->long_p)
10156 {
10157 specs->type = (specs->complex_p
10158 ? complex_long_double_type_node
10159 : long_double_type_node);
10160 }
10161 else
10162 {
10163 specs->type = (specs->complex_p
10164 ? complex_double_type_node
10165 : double_type_node);
10166 }
10167 break;
10168 case cts_dfloat32:
10169 case cts_dfloat64:
10170 case cts_dfloat128:
10171 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10172 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
10173 if (specs->typespec_word == cts_dfloat32)
10174 specs->type = dfloat32_type_node;
10175 else if (specs->typespec_word == cts_dfloat64)
10176 specs->type = dfloat64_type_node;
10177 else
10178 specs->type = dfloat128_type_node;
10179 break;
10180 case cts_fract:
10181 gcc_assert (!specs->complex_p);
10182 if (!targetm.fixed_point_supported_p ())
10183 specs->type = integer_type_node;
10184 else if (specs->saturating_p)
10185 {
10186 if (specs->long_long_p)
10187 specs->type = specs->unsigned_p
10188 ? sat_unsigned_long_long_fract_type_node
10189 : sat_long_long_fract_type_node;
10190 else if (specs->long_p)
10191 specs->type = specs->unsigned_p
10192 ? sat_unsigned_long_fract_type_node
10193 : sat_long_fract_type_node;
10194 else if (specs->short_p)
10195 specs->type = specs->unsigned_p
10196 ? sat_unsigned_short_fract_type_node
10197 : sat_short_fract_type_node;
10198 else
10199 specs->type = specs->unsigned_p
10200 ? sat_unsigned_fract_type_node
10201 : sat_fract_type_node;
10202 }
10203 else
10204 {
10205 if (specs->long_long_p)
10206 specs->type = specs->unsigned_p
10207 ? unsigned_long_long_fract_type_node
10208 : long_long_fract_type_node;
10209 else if (specs->long_p)
10210 specs->type = specs->unsigned_p
10211 ? unsigned_long_fract_type_node
10212 : long_fract_type_node;
10213 else if (specs->short_p)
10214 specs->type = specs->unsigned_p
10215 ? unsigned_short_fract_type_node
10216 : short_fract_type_node;
10217 else
10218 specs->type = specs->unsigned_p
10219 ? unsigned_fract_type_node
10220 : fract_type_node;
10221 }
10222 break;
10223 case cts_accum:
10224 gcc_assert (!specs->complex_p);
10225 if (!targetm.fixed_point_supported_p ())
10226 specs->type = integer_type_node;
10227 else if (specs->saturating_p)
10228 {
10229 if (specs->long_long_p)
10230 specs->type = specs->unsigned_p
10231 ? sat_unsigned_long_long_accum_type_node
10232 : sat_long_long_accum_type_node;
10233 else if (specs->long_p)
10234 specs->type = specs->unsigned_p
10235 ? sat_unsigned_long_accum_type_node
10236 : sat_long_accum_type_node;
10237 else if (specs->short_p)
10238 specs->type = specs->unsigned_p
10239 ? sat_unsigned_short_accum_type_node
10240 : sat_short_accum_type_node;
10241 else
10242 specs->type = specs->unsigned_p
10243 ? sat_unsigned_accum_type_node
10244 : sat_accum_type_node;
10245 }
10246 else
10247 {
10248 if (specs->long_long_p)
10249 specs->type = specs->unsigned_p
10250 ? unsigned_long_long_accum_type_node
10251 : long_long_accum_type_node;
10252 else if (specs->long_p)
10253 specs->type = specs->unsigned_p
10254 ? unsigned_long_accum_type_node
10255 : long_accum_type_node;
10256 else if (specs->short_p)
10257 specs->type = specs->unsigned_p
10258 ? unsigned_short_accum_type_node
10259 : short_accum_type_node;
10260 else
10261 specs->type = specs->unsigned_p
10262 ? unsigned_accum_type_node
10263 : accum_type_node;
10264 }
10265 break;
10266 default:
10267 gcc_unreachable ();
10268 }
10269
10270 return specs;
10271 }
10272
10273 /* A subroutine of c_write_global_declarations. Perform final processing
10274 on one file scope's declarations (or the external scope's declarations),
10275 GLOBALS. */
10276
10277 static void
10278 c_write_global_declarations_1 (tree globals)
10279 {
10280 tree decl;
10281 bool reconsider;
10282
10283 /* Process the decls in the order they were written. */
10284 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10285 {
10286 /* Check for used but undefined static functions using the C
10287 standard's definition of "used", and set TREE_NO_WARNING so
10288 that check_global_declarations doesn't repeat the check. */
10289 if (TREE_CODE (decl) == FUNCTION_DECL
10290 && DECL_INITIAL (decl) == 0
10291 && DECL_EXTERNAL (decl)
10292 && !TREE_PUBLIC (decl)
10293 && C_DECL_USED (decl))
10294 {
10295 pedwarn (input_location, 0, "%q+F used but never defined", decl);
10296 TREE_NO_WARNING (decl) = 1;
10297 }
10298
10299 wrapup_global_declaration_1 (decl);
10300 }
10301
10302 do
10303 {
10304 reconsider = false;
10305 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10306 reconsider |= wrapup_global_declaration_2 (decl);
10307 }
10308 while (reconsider);
10309
10310 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10311 check_global_declaration_1 (decl);
10312 }
10313
10314 /* A subroutine of c_write_global_declarations Emit debug information for each
10315 of the declarations in GLOBALS. */
10316
10317 static void
10318 c_write_global_declarations_2 (tree globals)
10319 {
10320 tree decl;
10321
10322 for (decl = globals; decl ; decl = DECL_CHAIN (decl))
10323 debug_hooks->global_decl (decl);
10324 }
10325
10326 /* Callback to collect a source_ref from a DECL. */
10327
10328 static void
10329 collect_source_ref_cb (tree decl)
10330 {
10331 if (!DECL_IS_BUILTIN (decl))
10332 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
10333 }
10334
10335 /* Preserve the external declarations scope across a garbage collect. */
10336 static GTY(()) tree ext_block;
10337
10338 /* Collect all references relevant to SOURCE_FILE. */
10339
10340 static void
10341 collect_all_refs (const char *source_file)
10342 {
10343 tree t;
10344 unsigned i;
10345
10346 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10347 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
10348
10349 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
10350 }
10351
10352 /* Iterate over all global declarations and call CALLBACK. */
10353
10354 static void
10355 for_each_global_decl (void (*callback) (tree decl))
10356 {
10357 tree t;
10358 tree decls;
10359 tree decl;
10360 unsigned i;
10361
10362 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10363 {
10364 decls = DECL_INITIAL (t);
10365 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
10366 callback (decl);
10367 }
10368
10369 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
10370 callback (decl);
10371 }
10372
10373 void
10374 c_write_global_declarations (void)
10375 {
10376 tree t;
10377 unsigned i;
10378
10379 /* We don't want to do this if generating a PCH. */
10380 if (pch_file)
10381 return;
10382
10383 timevar_start (TV_PHASE_DEFERRED);
10384
10385 /* Do the Objective-C stuff. This is where all the Objective-C
10386 module stuff gets generated (symtab, class/protocol/selector
10387 lists etc). */
10388 if (c_dialect_objc ())
10389 objc_write_global_declarations ();
10390
10391 /* Close the external scope. */
10392 ext_block = pop_scope ();
10393 external_scope = 0;
10394 gcc_assert (!current_scope);
10395
10396 /* Handle -fdump-ada-spec[-slim]. */
10397 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
10398 {
10399 /* Build a table of files to generate specs for */
10400 if (flag_dump_ada_spec_slim)
10401 collect_source_ref (main_input_filename);
10402 else
10403 for_each_global_decl (collect_source_ref_cb);
10404
10405 dump_ada_specs (collect_all_refs, NULL);
10406 }
10407
10408 if (ext_block)
10409 {
10410 tree tmp = BLOCK_VARS (ext_block);
10411 int flags;
10412 FILE * stream = dump_begin (TDI_tu, &flags);
10413 if (stream && tmp)
10414 {
10415 dump_node (tmp, flags & ~TDF_SLIM, stream);
10416 dump_end (TDI_tu, stream);
10417 }
10418 }
10419
10420 /* Process all file scopes in this compilation, and the external_scope,
10421 through wrapup_global_declarations and check_global_declarations. */
10422 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10423 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
10424 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
10425
10426 timevar_stop (TV_PHASE_DEFERRED);
10427 timevar_start (TV_PHASE_OPT_GEN);
10428
10429 /* We're done parsing; proceed to optimize and emit assembly.
10430 FIXME: shouldn't be the front end's responsibility to call this. */
10431 finalize_compilation_unit ();
10432
10433 timevar_stop (TV_PHASE_OPT_GEN);
10434 timevar_start (TV_PHASE_DBGINFO);
10435
10436 /* After cgraph has had a chance to emit everything that's going to
10437 be emitted, output debug information for globals. */
10438 if (!seen_error ())
10439 {
10440 timevar_push (TV_SYMOUT);
10441 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10442 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
10443 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
10444 timevar_pop (TV_SYMOUT);
10445 }
10446
10447 ext_block = NULL;
10448 timevar_stop (TV_PHASE_DBGINFO);
10449 }
10450
10451 /* Register reserved keyword WORD as qualifier for address space AS. */
10452
10453 void
10454 c_register_addr_space (const char *word, addr_space_t as)
10455 {
10456 int rid = RID_FIRST_ADDR_SPACE + as;
10457 tree id;
10458
10459 /* Address space qualifiers are only supported
10460 in C with GNU extensions enabled. */
10461 if (c_dialect_objc () || flag_no_asm)
10462 return;
10463
10464 id = get_identifier (word);
10465 C_SET_RID_CODE (id, rid);
10466 C_IS_RESERVED_WORD (id) = 1;
10467 ridpointers [rid] = id;
10468 }
10469
10470 /* Return identifier to look up for omp declare reduction. */
10471
10472 tree
10473 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
10474 {
10475 const char *p = NULL;
10476 switch (reduction_code)
10477 {
10478 case PLUS_EXPR: p = "+"; break;
10479 case MULT_EXPR: p = "*"; break;
10480 case MINUS_EXPR: p = "-"; break;
10481 case BIT_AND_EXPR: p = "&"; break;
10482 case BIT_XOR_EXPR: p = "^"; break;
10483 case BIT_IOR_EXPR: p = "|"; break;
10484 case TRUTH_ANDIF_EXPR: p = "&&"; break;
10485 case TRUTH_ORIF_EXPR: p = "||"; break;
10486 case MIN_EXPR: p = "min"; break;
10487 case MAX_EXPR: p = "max"; break;
10488 default:
10489 break;
10490 }
10491
10492 if (p == NULL)
10493 {
10494 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
10495 return error_mark_node;
10496 p = IDENTIFIER_POINTER (reduction_id);
10497 }
10498
10499 const char prefix[] = "omp declare reduction ";
10500 size_t lenp = sizeof (prefix);
10501 size_t len = strlen (p);
10502 char *name = XALLOCAVEC (char, lenp + len);
10503 memcpy (name, prefix, lenp - 1);
10504 memcpy (name + lenp - 1, p, len + 1);
10505 return get_identifier (name);
10506 }
10507
10508 /* Lookup REDUCTION_ID in the current scope, or create an artificial
10509 VAR_DECL, bind it into the current scope and return it. */
10510
10511 tree
10512 c_omp_reduction_decl (tree reduction_id)
10513 {
10514 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
10515 if (b != NULL && B_IN_CURRENT_SCOPE (b))
10516 return b->decl;
10517
10518 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
10519 reduction_id, integer_type_node);
10520 DECL_ARTIFICIAL (decl) = 1;
10521 DECL_EXTERNAL (decl) = 1;
10522 TREE_STATIC (decl) = 1;
10523 TREE_PUBLIC (decl) = 0;
10524 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
10525 return decl;
10526 }
10527
10528 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
10529
10530 tree
10531 c_omp_reduction_lookup (tree reduction_id, tree type)
10532 {
10533 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
10534 while (b)
10535 {
10536 tree t;
10537 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
10538 if (comptypes (TREE_PURPOSE (t), type))
10539 return TREE_VALUE (t);
10540 b = b->shadowed;
10541 }
10542 return error_mark_node;
10543 }
10544
10545 /* Helper function called via walk_tree, to diagnose invalid
10546 #pragma omp declare reduction combiners or initializers. */
10547
10548 tree
10549 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
10550 {
10551 tree *vars = (tree *) data;
10552 if (SSA_VAR_P (*tp)
10553 && !DECL_ARTIFICIAL (*tp)
10554 && *tp != vars[0]
10555 && *tp != vars[1])
10556 {
10557 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
10558 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
10559 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
10560 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
10561 *tp);
10562 else
10563 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
10564 "to variable %qD which is not %<omp_priv%> nor "
10565 "%<omp_orig%>",
10566 *tp);
10567 return *tp;
10568 }
10569 return NULL_TREE;
10570 }
10571
10572 #include "gt-c-c-decl.h"