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