]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
[PR debug/67192] Fix C loops' back-jump location
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
5624e564 2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
400fbf9f 3
1322177d 4This file is part of GCC.
400fbf9f 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
400fbf9f 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
400fbf9f
JW
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
400fbf9f
JW
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
5088b058 24 and some optimization. */
400fbf9f
JW
25
26#include "config.h"
670ee920 27#include "system.h"
4977bab6 28#include "coretypes.h"
2adfab87
AM
29#include "target.h"
30#include "function.h"
31#include "bitmap.h"
400fbf9f 32#include "tree.h"
2adfab87
AM
33#include "c-family/c-common.h"
34#include "c-tree.h"
35#include "gimple-expr.h"
36#include "predict.h"
d8a2d370
DN
37#include "stor-layout.h"
38#include "trans-mem.h"
39#include "varasm.h"
40#include "stmt.h"
e57e265b 41#include "langhooks.h"
29cc57cf 42#include "c-lang.h"
400fbf9f 43#include "flags.h"
ab87f8c8 44#include "intl.h"
325c3691 45#include "tree-iterator.h"
45b0be94 46#include "gimplify.h"
acf0174b 47#include "tree-inline.h"
0645c1a2 48#include "omp-low.h"
61d3ce20 49#include "c-family/c-objc.h"
de5a5fa1 50#include "c-family/c-ubsan.h"
12893402 51#include "cilk.h"
41dbbb37 52#include "gomp-constants.h"
325c3691 53
2ac2f164
JM
54/* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
56enum impl_conv {
57 ic_argpass,
58 ic_assign,
59 ic_init,
60 ic_return
61};
62
bc4b653b
JM
63/* The level of nesting inside "__alignof__". */
64int in_alignof;
65
66/* The level of nesting inside "sizeof". */
67int in_sizeof;
68
69/* The level of nesting inside "typeof". */
70int in_typeof;
400fbf9f 71
1a4049e7
JJ
72/* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74tree c_last_sizeof_arg;
75
9bac5cbb
G
76/* Nonzero if we might need to print a "missing braces around
77 initializer" message within this initializer. */
78static int found_missing_braces;
103b7b17 79
bf730f15
RS
80static int require_constant_value;
81static int require_constant_elements;
82
58f9752a 83static bool null_pointer_constant_p (const_tree);
f55ade6e 84static tree qualify_type (tree, tree);
dc5027f4
JM
85static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
86 bool *);
744aa42f 87static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
88static int function_types_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 91static tree lookup_field (tree, tree);
81e5eca8
MP
92static int convert_arguments (location_t, vec<location_t>, tree,
93 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
94 tree);
db3927fb 95static tree pointer_diff (location_t, tree, tree);
68fca595 96static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 97 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
98static tree valid_compound_expr_initializer (tree, tree);
99static void push_string (const char *);
100static void push_member_name (tree);
f55ade6e
AJ
101static int spelling_length (void);
102static char *print_spelling (char *);
96b40f8d 103static void warning_init (location_t, int, const char *);
c2255bc4 104static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
105static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
106 bool, struct obstack *);
a1e3b3d9 107static void output_pending_init_elements (int, struct obstack *);
ea58ef42 108static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 109static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
110static void add_pending_init (location_t, tree, tree, tree, bool,
111 struct obstack *);
a1e3b3d9
LB
112static void set_nonincremental_init (struct obstack *);
113static void set_nonincremental_init_from_string (tree, struct obstack *);
114static tree find_init_member (tree, struct obstack *);
f37acdf9 115static void readonly_warning (tree, enum lvalue_use);
7bd11157 116static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 117static void record_maybe_used_decl (tree);
dc5027f4 118static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
119\f
120/* Return true if EXP is a null pointer constant, false otherwise. */
121
122static bool
58f9752a 123null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
124{
125 /* This should really operate on c_expr structures, but they aren't
126 yet available everywhere required. */
127 tree type = TREE_TYPE (expr);
128 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 129 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
130 && integer_zerop (expr)
131 && (INTEGRAL_TYPE_P (type)
132 || (TREE_CODE (type) == POINTER_TYPE
133 && VOID_TYPE_P (TREE_TYPE (type))
134 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
135}
928c19bb
JM
136
137/* EXPR may appear in an unevaluated part of an integer constant
138 expression, but not in an evaluated part. Wrap it in a
139 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
140 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
141
142static tree
143note_integer_operands (tree expr)
144{
145 tree ret;
146 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
147 {
148 ret = copy_node (expr);
149 TREE_OVERFLOW (ret) = 1;
150 }
151 else
152 {
153 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
154 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
155 }
156 return ret;
157}
158
4d84fe7c
JM
159/* Having checked whether EXPR may appear in an unevaluated part of an
160 integer constant expression and found that it may, remove any
161 C_MAYBE_CONST_EXPR noting this fact and return the resulting
162 expression. */
163
164static inline tree
165remove_c_maybe_const_expr (tree expr)
166{
167 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
168 return C_MAYBE_CONST_EXPR_EXPR (expr);
169 else
170 return expr;
171}
172
f13c9b2c
AP
173\f/* This is a cache to hold if two types are compatible or not. */
174
175struct tagged_tu_seen_cache {
176 const struct tagged_tu_seen_cache * next;
58f9752a
KG
177 const_tree t1;
178 const_tree t2;
f13c9b2c
AP
179 /* The return value of tagged_types_tu_compatible_p if we had seen
180 these two types already. */
181 int val;
182};
183
184static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
185static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
186
400fbf9f
JW
187/* Do `exp = require_complete_type (exp);' to make sure exp
188 does not have an incomplete type. (That includes void types.) */
189
190tree
2f6e4e97 191require_complete_type (tree value)
400fbf9f
JW
192{
193 tree type = TREE_TYPE (value);
194
7a0ca710 195 if (error_operand_p (value))
ea0f786b
CB
196 return error_mark_node;
197
400fbf9f 198 /* First, detect a valid value with a complete type. */
d0f062fb 199 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
200 return value;
201
7a228918 202 c_incomplete_type_error (value, type);
400fbf9f
JW
203 return error_mark_node;
204}
205
206/* Print an error message for invalid use of an incomplete type.
207 VALUE is the expression that was used (or 0 if that isn't known)
208 and TYPE is the type that was invalid. */
209
210void
ac7d7749 211c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 212{
400fbf9f
JW
213 /* Avoid duplicate error message. */
214 if (TREE_CODE (type) == ERROR_MARK)
215 return;
216
0ae9bd27 217 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
ac9f18db 218 error ("%qD has an incomplete type %qT", value, type);
400fbf9f
JW
219 else
220 {
221 retry:
222 /* We must print an error message. Be clever about what it says. */
223
224 switch (TREE_CODE (type))
225 {
226 case RECORD_TYPE:
400fbf9f 227 case UNION_TYPE:
400fbf9f 228 case ENUMERAL_TYPE:
400fbf9f
JW
229 break;
230
231 case VOID_TYPE:
232 error ("invalid use of void expression");
233 return;
234
235 case ARRAY_TYPE:
236 if (TYPE_DOMAIN (type))
237 {
fba78abb
RH
238 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
239 {
240 error ("invalid use of flexible array member");
241 return;
242 }
400fbf9f
JW
243 type = TREE_TYPE (type);
244 goto retry;
245 }
246 error ("invalid use of array with unspecified bounds");
247 return;
248
249 default:
366de0ce 250 gcc_unreachable ();
400fbf9f
JW
251 }
252
253 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
ac9f18db 254 error ("invalid use of undefined type %qT", type);
400fbf9f
JW
255 else
256 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
ac9f18db 257 error ("invalid use of incomplete typedef %qT", type);
400fbf9f
JW
258 }
259}
260
ab393bf1
NB
261/* Given a type, apply default promotions wrt unnamed function
262 arguments and return the new type. */
263
264tree
2f6e4e97 265c_type_promotes_to (tree type)
ab393bf1 266{
267bac10 267 tree ret = NULL_TREE;
ab393bf1 268
267bac10
JM
269 if (TYPE_MAIN_VARIANT (type) == float_type_node)
270 ret = double_type_node;
271 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
272 {
273 /* Preserve unsignedness if not really getting any wider. */
8df83eae 274 if (TYPE_UNSIGNED (type)
c22cacf3 275 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
276 ret = unsigned_type_node;
277 else
278 ret = integer_type_node;
ab393bf1
NB
279 }
280
267bac10
JM
281 if (ret != NULL_TREE)
282 return (TYPE_ATOMIC (type)
283 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
284 : ret);
285
ab393bf1
NB
286 return type;
287}
288
36c5e70a
BE
289/* Return true if between two named address spaces, whether there is a superset
290 named address space that encompasses both address spaces. If there is a
291 superset, return which address space is the superset. */
292
293static bool
294addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
295{
296 if (as1 == as2)
297 {
298 *common = as1;
299 return true;
300 }
301 else if (targetm.addr_space.subset_p (as1, as2))
302 {
303 *common = as2;
304 return true;
305 }
306 else if (targetm.addr_space.subset_p (as2, as1))
307 {
308 *common = as1;
309 return true;
310 }
311 else
312 return false;
313}
314
400fbf9f
JW
315/* Return a variant of TYPE which has all the type qualifiers of LIKE
316 as well as those of TYPE. */
317
318static tree
2f6e4e97 319qualify_type (tree type, tree like)
400fbf9f 320{
36c5e70a
BE
321 addr_space_t as_type = TYPE_ADDR_SPACE (type);
322 addr_space_t as_like = TYPE_ADDR_SPACE (like);
323 addr_space_t as_common;
324
325 /* If the two named address spaces are different, determine the common
326 superset address space. If there isn't one, raise an error. */
327 if (!addr_space_superset (as_type, as_like, &as_common))
328 {
329 as_common = as_type;
330 error ("%qT and %qT are in disjoint named address spaces",
331 type, like);
332 }
333
2f6e4e97 334 return c_build_qualified_type (type,
36c5e70a 335 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 336 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 337 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 338}
52ffd86e
MS
339
340/* Return true iff the given tree T is a variable length array. */
341
342bool
ac7d7749 343c_vla_type_p (const_tree t)
52ffd86e
MS
344{
345 if (TREE_CODE (t) == ARRAY_TYPE
346 && C_TYPE_VARIABLE_SIZE (t))
347 return true;
348 return false;
349}
400fbf9f 350\f
10bc1b1b 351/* Return the composite type of two compatible types.
5305f6d7 352
10bc1b1b
JM
353 We assume that comptypes has already been done and returned
354 nonzero; if that isn't so, this may crash. In particular, we
355 assume that qualifiers match. */
400fbf9f
JW
356
357tree
10bc1b1b 358composite_type (tree t1, tree t2)
400fbf9f 359{
b3694847
SS
360 enum tree_code code1;
361 enum tree_code code2;
4b027d16 362 tree attributes;
400fbf9f
JW
363
364 /* Save time if the two types are the same. */
365
366 if (t1 == t2) return t1;
367
368 /* If one type is nonsense, use the other. */
369 if (t1 == error_mark_node)
370 return t2;
371 if (t2 == error_mark_node)
372 return t1;
373
10bc1b1b
JM
374 code1 = TREE_CODE (t1);
375 code2 = TREE_CODE (t2);
376
d9525bec 377 /* Merge the attributes. */
5fd9b178 378 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 379
10bc1b1b
JM
380 /* If one is an enumerated type and the other is the compatible
381 integer type, the composite type might be either of the two
382 (DR#013 question 3). For consistency, use the enumerated type as
383 the composite type. */
400fbf9f 384
10bc1b1b
JM
385 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
386 return t1;
387 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
388 return t2;
75326e8c 389
366de0ce 390 gcc_assert (code1 == code2);
b6a10c9f 391
400fbf9f
JW
392 switch (code1)
393 {
400fbf9f 394 case POINTER_TYPE:
10bc1b1b 395 /* For two pointers, do this recursively on the target type. */
400fbf9f 396 {
3932261a
MM
397 tree pointed_to_1 = TREE_TYPE (t1);
398 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 399 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 400 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
401 t1 = build_type_attribute_variant (t1, attributes);
402 return qualify_type (t1, t2);
400fbf9f 403 }
400fbf9f
JW
404
405 case ARRAY_TYPE:
406 {
10bc1b1b 407 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
408 int quals;
409 tree unqual_elt;
ca8bdb78
JM
410 tree d1 = TYPE_DOMAIN (t1);
411 tree d2 = TYPE_DOMAIN (t2);
412 bool d1_variable, d2_variable;
413 bool d1_zero, d2_zero;
f6294de7 414 bool t1_complete, t2_complete;
46df2823 415
de46b2fe 416 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
417 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
418 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 419
f6294de7
JM
420 t1_complete = COMPLETE_TYPE_P (t1);
421 t2_complete = COMPLETE_TYPE_P (t2);
422
ca8bdb78
JM
423 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
424 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
425
426 d1_variable = (!d1_zero
427 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
428 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
429 d2_variable = (!d2_zero
430 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
431 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
432 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
433 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 434
400fbf9f 435 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
436 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
437 && (d2_variable || d2_zero || !d1_variable))
4b027d16 438 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
439 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
440 && (d1_variable || d1_zero || !d2_variable))
4b027d16 441 return build_type_attribute_variant (t2, attributes);
c22cacf3 442
de46b2fe
AP
443 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
444 return build_type_attribute_variant (t1, attributes);
445 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
446 return build_type_attribute_variant (t2, attributes);
c22cacf3 447
46df2823
JM
448 /* Merge the element types, and have a size if either arg has
449 one. We may have qualifiers on the element types. To set
450 up TYPE_MAIN_VARIANT correctly, we need to form the
451 composite of the unqualified types and add the qualifiers
452 back at the end. */
453 quals = TYPE_QUALS (strip_array_types (elt));
454 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
455 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
456 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
457 && (d2_variable
458 || d2_zero
459 || !d1_variable))
460 ? t1
461 : t2));
f6294de7
JM
462 /* Ensure a composite type involving a zero-length array type
463 is a zero-length type not an incomplete type. */
464 if (d1_zero && d2_zero
465 && (t1_complete || t2_complete)
466 && !COMPLETE_TYPE_P (t1))
467 {
468 TYPE_SIZE (t1) = bitsize_zero_node;
469 TYPE_SIZE_UNIT (t1) = size_zero_node;
470 }
46df2823 471 t1 = c_build_qualified_type (t1, quals);
de46b2fe 472 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
473 }
474
fcb99e7b
JJ
475 case ENUMERAL_TYPE:
476 case RECORD_TYPE:
477 case UNION_TYPE:
478 if (attributes != NULL)
479 {
480 /* Try harder not to create a new aggregate type. */
481 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
482 return t1;
483 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
484 return t2;
485 }
486 return build_type_attribute_variant (t1, attributes);
487
400fbf9f
JW
488 case FUNCTION_TYPE:
489 /* Function types: prefer the one that specified arg types.
490 If both do, merge the arg types. Also merge the return types. */
491 {
10bc1b1b 492 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
493 tree p1 = TYPE_ARG_TYPES (t1);
494 tree p2 = TYPE_ARG_TYPES (t2);
495 int len;
496 tree newargs, n;
497 int i;
498
499 /* Save space: see if the result is identical to one of the args. */
3f75a254 500 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 501 return build_type_attribute_variant (t1, attributes);
3f75a254 502 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 503 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
504
505 /* Simple way if one arg fails to specify argument types. */
506 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 507 {
fe7080d2
AP
508 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
509 t1 = build_type_attribute_variant (t1, attributes);
510 return qualify_type (t1, t2);
4b027d16 511 }
400fbf9f 512 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
513 {
514 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
515 t1 = build_type_attribute_variant (t1, attributes);
516 return qualify_type (t1, t2);
4b027d16 517 }
400fbf9f
JW
518
519 /* If both args specify argument types, we must merge the two
520 lists, argument by argument. */
2f4e8f2b 521
400fbf9f
JW
522 len = list_length (p1);
523 newargs = 0;
524
525 for (i = 0; i < len; i++)
8d9bfdc5 526 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
527
528 n = newargs;
529
530 for (; p1;
531 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
532 {
533 /* A null type means arg type is not specified.
534 Take whatever the other function type has. */
535 if (TREE_VALUE (p1) == 0)
536 {
537 TREE_VALUE (n) = TREE_VALUE (p2);
538 goto parm_done;
539 }
540 if (TREE_VALUE (p2) == 0)
541 {
542 TREE_VALUE (n) = TREE_VALUE (p1);
543 goto parm_done;
544 }
2f6e4e97 545
400fbf9f
JW
546 /* Given wait (union {union wait *u; int *i} *)
547 and wait (union wait *),
548 prefer union wait * as type of parm. */
549 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
550 && TREE_VALUE (p1) != TREE_VALUE (p2))
551 {
552 tree memb;
58cb41e6
JJ
553 tree mv2 = TREE_VALUE (p2);
554 if (mv2 && mv2 != error_mark_node
555 && TREE_CODE (mv2) != ARRAY_TYPE)
556 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 557 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 558 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
559 {
560 tree mv3 = TREE_TYPE (memb);
561 if (mv3 && mv3 != error_mark_node
562 && TREE_CODE (mv3) != ARRAY_TYPE)
563 mv3 = TYPE_MAIN_VARIANT (mv3);
564 if (comptypes (mv3, mv2))
565 {
566 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
567 TREE_VALUE (p2));
c1771a20 568 pedwarn (input_location, OPT_Wpedantic,
fcf73884 569 "function types not truly compatible in ISO C");
58cb41e6
JJ
570 goto parm_done;
571 }
572 }
400fbf9f
JW
573 }
574 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
575 && TREE_VALUE (p2) != TREE_VALUE (p1))
576 {
577 tree memb;
58cb41e6
JJ
578 tree mv1 = TREE_VALUE (p1);
579 if (mv1 && mv1 != error_mark_node
580 && TREE_CODE (mv1) != ARRAY_TYPE)
581 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 582 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 583 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
584 {
585 tree mv3 = TREE_TYPE (memb);
586 if (mv3 && mv3 != error_mark_node
587 && TREE_CODE (mv3) != ARRAY_TYPE)
588 mv3 = TYPE_MAIN_VARIANT (mv3);
589 if (comptypes (mv3, mv1))
590 {
591 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
592 TREE_VALUE (p1));
c1771a20 593 pedwarn (input_location, OPT_Wpedantic,
fcf73884 594 "function types not truly compatible in ISO C");
58cb41e6
JJ
595 goto parm_done;
596 }
597 }
400fbf9f 598 }
10bc1b1b 599 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
600 parm_done: ;
601 }
602
4b027d16 603 t1 = build_function_type (valtype, newargs);
fe7080d2 604 t1 = qualify_type (t1, t2);
0f41302f 605 /* ... falls through ... */
400fbf9f
JW
606 }
607
608 default:
4b027d16 609 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
610 }
611
612}
10bc1b1b
JM
613
614/* Return the type of a conditional expression between pointers to
615 possibly differently qualified versions of compatible types.
616
617 We assume that comp_target_types has already been done and returned
618 nonzero; if that isn't so, this may crash. */
619
620static tree
621common_pointer_type (tree t1, tree t2)
622{
623 tree attributes;
46df2823
JM
624 tree pointed_to_1, mv1;
625 tree pointed_to_2, mv2;
10bc1b1b 626 tree target;
eb1387a0 627 unsigned target_quals;
36c5e70a
BE
628 addr_space_t as1, as2, as_common;
629 int quals1, quals2;
10bc1b1b
JM
630
631 /* Save time if the two types are the same. */
632
633 if (t1 == t2) return t1;
634
635 /* If one type is nonsense, use the other. */
636 if (t1 == error_mark_node)
637 return t2;
638 if (t2 == error_mark_node)
639 return t1;
640
366de0ce 641 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 642 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
643
644 /* Merge the attributes. */
645 attributes = targetm.merge_type_attributes (t1, t2);
646
647 /* Find the composite type of the target types, and combine the
46df2823
JM
648 qualifiers of the two types' targets. Do not lose qualifiers on
649 array element types by taking the TYPE_MAIN_VARIANT. */
650 mv1 = pointed_to_1 = TREE_TYPE (t1);
651 mv2 = pointed_to_2 = TREE_TYPE (t2);
652 if (TREE_CODE (mv1) != ARRAY_TYPE)
653 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
654 if (TREE_CODE (mv2) != ARRAY_TYPE)
655 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
656 target = composite_type (mv1, mv2);
eb1387a0 657
768952be
MU
658 /* Strip array types to get correct qualifier for pointers to arrays */
659 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
660 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
661
eb1387a0
RG
662 /* For function types do not merge const qualifiers, but drop them
663 if used inconsistently. The middle-end uses these to mark const
664 and noreturn functions. */
665 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 666 target_quals = (quals1 & quals2);
eb1387a0 667 else
36c5e70a
BE
668 target_quals = (quals1 | quals2);
669
670 /* If the two named address spaces are different, determine the common
671 superset address space. This is guaranteed to exist due to the
672 assumption that comp_target_type returned non-zero. */
673 as1 = TYPE_ADDR_SPACE (pointed_to_1);
674 as2 = TYPE_ADDR_SPACE (pointed_to_2);
675 if (!addr_space_superset (as1, as2, &as_common))
676 gcc_unreachable ();
677
678 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
679
eb1387a0 680 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
681 return build_type_attribute_variant (t1, attributes);
682}
683
684/* Return the common type for two arithmetic types under the usual
685 arithmetic conversions. The default conversions have already been
686 applied, and enumerated types converted to their compatible integer
687 types. The resulting type is unqualified and has no attributes.
688
689 This is the type for the result of most arithmetic operations
690 if the operands have the given two types. */
691
ccf7f880
JJ
692static tree
693c_common_type (tree t1, tree t2)
10bc1b1b
JM
694{
695 enum tree_code code1;
696 enum tree_code code2;
697
698 /* If one type is nonsense, use the other. */
699 if (t1 == error_mark_node)
700 return t2;
701 if (t2 == error_mark_node)
702 return t1;
703
704 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
705 t1 = TYPE_MAIN_VARIANT (t1);
706
707 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
708 t2 = TYPE_MAIN_VARIANT (t2);
709
710 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
711 t1 = build_type_attribute_variant (t1, NULL_TREE);
712
713 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
714 t2 = build_type_attribute_variant (t2, NULL_TREE);
715
716 /* Save time if the two types are the same. */
717
718 if (t1 == t2) return t1;
719
720 code1 = TREE_CODE (t1);
721 code2 = TREE_CODE (t2);
722
366de0ce 723 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
724 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
725 || code1 == INTEGER_TYPE);
366de0ce 726 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
727 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
728 || code2 == INTEGER_TYPE);
10bc1b1b 729
5fc89bfd
JJ
730 /* When one operand is a decimal float type, the other operand cannot be
731 a generic float type or a complex type. We also disallow vector types
732 here. */
733 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
734 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
735 {
736 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
737 {
738 error ("can%'t mix operands of decimal float and vector types");
739 return error_mark_node;
740 }
741 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
742 {
743 error ("can%'t mix operands of decimal float and complex types");
744 return error_mark_node;
745 }
746 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
747 {
748 error ("can%'t mix operands of decimal float and other float types");
749 return error_mark_node;
750 }
751 }
752
10bc1b1b
JM
753 /* If one type is a vector type, return that type. (How the usual
754 arithmetic conversions apply to the vector types extension is not
755 precisely specified.) */
756 if (code1 == VECTOR_TYPE)
757 return t1;
758
759 if (code2 == VECTOR_TYPE)
760 return t2;
761
762 /* If one type is complex, form the common type of the non-complex
763 components, then make that complex. Use T1 or T2 if it is the
764 required type. */
765 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
766 {
767 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
768 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 769 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
770
771 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
772 return t1;
773 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
774 return t2;
775 else
776 return build_complex_type (subtype);
777 }
778
779 /* If only one is real, use it as the result. */
780
781 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
782 return t1;
783
784 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
785 return t2;
786
9a8ce21f
JG
787 /* If both are real and either are decimal floating point types, use
788 the decimal floating point type with the greater precision. */
789
790 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
791 {
792 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
793 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
794 return dfloat128_type_node;
795 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
796 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
797 return dfloat64_type_node;
798 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
799 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
800 return dfloat32_type_node;
801 }
802
ab22c1fa
CF
803 /* Deal with fixed-point types. */
804 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
805 {
806 unsigned int unsignedp = 0, satp = 0;
ef4bddc2 807 machine_mode m1, m2;
ab22c1fa
CF
808 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
809
810 m1 = TYPE_MODE (t1);
811 m2 = TYPE_MODE (t2);
812
813 /* If one input type is saturating, the result type is saturating. */
814 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
815 satp = 1;
816
817 /* If both fixed-point types are unsigned, the result type is unsigned.
818 When mixing fixed-point and integer types, follow the sign of the
819 fixed-point type.
820 Otherwise, the result type is signed. */
821 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
822 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
823 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
824 && TYPE_UNSIGNED (t1))
825 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
826 && TYPE_UNSIGNED (t2)))
827 unsignedp = 1;
828
829 /* The result type is signed. */
830 if (unsignedp == 0)
831 {
832 /* If the input type is unsigned, we need to convert to the
833 signed type. */
834 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
835 {
d75d71e0 836 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
837 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
838 mclass = MODE_FRACT;
839 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
840 mclass = MODE_ACCUM;
841 else
842 gcc_unreachable ();
843 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
844 }
845 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
846 {
d75d71e0 847 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
848 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
849 mclass = MODE_FRACT;
850 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
851 mclass = MODE_ACCUM;
852 else
853 gcc_unreachable ();
854 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
855 }
856 }
857
858 if (code1 == FIXED_POINT_TYPE)
859 {
860 fbit1 = GET_MODE_FBIT (m1);
861 ibit1 = GET_MODE_IBIT (m1);
862 }
863 else
864 {
865 fbit1 = 0;
866 /* Signed integers need to subtract one sign bit. */
867 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
868 }
869
870 if (code2 == FIXED_POINT_TYPE)
871 {
872 fbit2 = GET_MODE_FBIT (m2);
873 ibit2 = GET_MODE_IBIT (m2);
874 }
875 else
876 {
877 fbit2 = 0;
878 /* Signed integers need to subtract one sign bit. */
879 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
880 }
881
882 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
883 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
884 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
885 satp);
886 }
887
10bc1b1b
JM
888 /* Both real or both integers; use the one with greater precision. */
889
890 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
891 return t1;
892 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
893 return t2;
894
895 /* Same precision. Prefer long longs to longs to ints when the
896 same precision, following the C99 rules on integer type rank
897 (which are equivalent to the C90 rules for C90 types). */
898
899 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
900 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
901 return long_long_unsigned_type_node;
902
903 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
904 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
905 {
906 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
907 return long_long_unsigned_type_node;
908 else
c22cacf3 909 return long_long_integer_type_node;
10bc1b1b
JM
910 }
911
912 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
914 return long_unsigned_type_node;
915
916 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
918 {
919 /* But preserve unsignedness from the other type,
920 since long cannot hold all the values of an unsigned int. */
921 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
922 return long_unsigned_type_node;
923 else
924 return long_integer_type_node;
925 }
926
927 /* Likewise, prefer long double to double even if same size. */
928 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
929 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
930 return long_double_type_node;
931
2531a1d9
JR
932 /* Likewise, prefer double to float even if same size.
933 We got a couple of embedded targets with 32 bit doubles, and the
934 pdp11 might have 64 bit floats. */
935 if (TYPE_MAIN_VARIANT (t1) == double_type_node
936 || TYPE_MAIN_VARIANT (t2) == double_type_node)
937 return double_type_node;
938
10bc1b1b
JM
939 /* Otherwise prefer the unsigned one. */
940
941 if (TYPE_UNSIGNED (t1))
942 return t1;
943 else
944 return t2;
945}
400fbf9f 946\f
5922c215
JM
947/* Wrapper around c_common_type that is used by c-common.c and other
948 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
949 are allowed here and are converted to their compatible integer types.
950 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
951 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
952tree
953common_type (tree t1, tree t2)
954{
955 if (TREE_CODE (t1) == ENUMERAL_TYPE)
956 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
957 if (TREE_CODE (t2) == ENUMERAL_TYPE)
958 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
959
960 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
961 if (TREE_CODE (t1) == BOOLEAN_TYPE
962 && TREE_CODE (t2) == BOOLEAN_TYPE)
963 return boolean_type_node;
964
965 /* If either type is BOOLEAN_TYPE, then return the other. */
966 if (TREE_CODE (t1) == BOOLEAN_TYPE)
967 return t2;
968 if (TREE_CODE (t2) == BOOLEAN_TYPE)
969 return t1;
970
ccf7f880
JJ
971 return c_common_type (t1, t2);
972}
f13c9b2c 973
400fbf9f
JW
974/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
975 or various other operations. Return 2 if they are compatible
976 but a warning may be needed if you use them together. */
977
978int
132da1a5 979comptypes (tree type1, tree type2)
f13c9b2c
AP
980{
981 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
982 int val;
983
dc5027f4 984 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
985 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
986
987 return val;
988}
989
990/* Like comptypes, but if it returns non-zero because enum and int are
991 compatible, it sets *ENUM_AND_INT_P to true. */
992
993static int
994comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
995{
996 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
997 int val;
998
dc5027f4
JM
999 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1000 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1001
1002 return val;
1003}
1004
1005/* Like comptypes, but if it returns nonzero for different types, it
1006 sets *DIFFERENT_TYPES_P to true. */
1007
1008int
1009comptypes_check_different_types (tree type1, tree type2,
1010 bool *different_types_p)
1011{
1012 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1013 int val;
1014
1015 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1016 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1017
f13c9b2c 1018 return val;
c22cacf3
MS
1019}
1020\f
f13c9b2c
AP
1021/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1022 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1023 but a warning may be needed if you use them together. If
1024 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1025 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1026 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1027 NULL, and the types are compatible but different enough not to be
48b0b196 1028 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1029 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1030 false, but may or may not be set if the types are incompatible.
1031 This differs from comptypes, in that we don't free the seen
1032 types. */
f13c9b2c
AP
1033
1034static int
dc5027f4
JM
1035comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1036 bool *different_types_p)
400fbf9f 1037{
58f9752a
KG
1038 const_tree t1 = type1;
1039 const_tree t2 = type2;
4b027d16 1040 int attrval, val;
400fbf9f
JW
1041
1042 /* Suppress errors caused by previously reported errors. */
1043
8d47dfc5
RH
1044 if (t1 == t2 || !t1 || !t2
1045 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1046 return 1;
1047
bca63328
JM
1048 /* Enumerated types are compatible with integer types, but this is
1049 not transitive: two enumerated types in the same translation unit
1050 are compatible with each other only if they are the same type. */
400fbf9f 1051
bca63328 1052 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1053 {
1054 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1055 if (TREE_CODE (t2) != VOID_TYPE)
1056 {
1057 if (enum_and_int_p != NULL)
1058 *enum_and_int_p = true;
1059 if (different_types_p != NULL)
1060 *different_types_p = true;
1061 }
744aa42f 1062 }
bca63328 1063 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1064 {
1065 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1066 if (TREE_CODE (t1) != VOID_TYPE)
1067 {
1068 if (enum_and_int_p != NULL)
1069 *enum_and_int_p = true;
1070 if (different_types_p != NULL)
1071 *different_types_p = true;
1072 }
744aa42f 1073 }
400fbf9f
JW
1074
1075 if (t1 == t2)
1076 return 1;
1077
1078 /* Different classes of types can't be compatible. */
1079
3aeb3655
EC
1080 if (TREE_CODE (t1) != TREE_CODE (t2))
1081 return 0;
400fbf9f 1082
118a3a8b 1083 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1084
3932261a 1085 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1086 return 0;
1087
08632da2
RS
1088 /* Allow for two different type nodes which have essentially the same
1089 definition. Note that we already checked for equality of the type
38e01259 1090 qualifiers (just above). */
400fbf9f 1091
46df2823
JM
1092 if (TREE_CODE (t1) != ARRAY_TYPE
1093 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1094 return 1;
1095
4b027d16 1096 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1097 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1098 return 0;
1099
1100 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1101 val = 0;
1102
400fbf9f
JW
1103 switch (TREE_CODE (t1))
1104 {
1105 case POINTER_TYPE:
106f5de5
UW
1106 /* Do not remove mode or aliasing information. */
1107 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1108 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1109 break;
4b027d16 1110 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1111 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1112 enum_and_int_p, different_types_p));
4b027d16 1113 break;
400fbf9f
JW
1114
1115 case FUNCTION_TYPE:
dc5027f4
JM
1116 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1117 different_types_p);
4b027d16 1118 break;
400fbf9f
JW
1119
1120 case ARRAY_TYPE:
1121 {
400fbf9f
JW
1122 tree d1 = TYPE_DOMAIN (t1);
1123 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1124 bool d1_variable, d2_variable;
1125 bool d1_zero, d2_zero;
4b027d16 1126 val = 1;
400fbf9f
JW
1127
1128 /* Target types must match incl. qualifiers. */
1129 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1130 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1131 enum_and_int_p,
1132 different_types_p)))
400fbf9f
JW
1133 return 0;
1134
dc5027f4
JM
1135 if (different_types_p != NULL
1136 && (d1 == 0) != (d2 == 0))
1137 *different_types_p = true;
400fbf9f 1138 /* Sizes must match unless one is missing or variable. */
3f85558f 1139 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1140 break;
400fbf9f 1141
3f75a254
JM
1142 d1_zero = !TYPE_MAX_VALUE (d1);
1143 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1144
3f75a254 1145 d1_variable = (!d1_zero
3f85558f
RH
1146 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1147 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1148 d2_variable = (!d2_zero
3f85558f
RH
1149 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1150 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1151 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1152 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1153
dc5027f4
JM
1154 if (different_types_p != NULL
1155 && d1_variable != d2_variable)
1156 *different_types_p = true;
3f85558f
RH
1157 if (d1_variable || d2_variable)
1158 break;
1159 if (d1_zero && d2_zero)
1160 break;
1161 if (d1_zero || d2_zero
3f75a254
JM
1162 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1163 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1164 val = 0;
1165
c22cacf3 1166 break;
400fbf9f
JW
1167 }
1168
d1bd0ded 1169 case ENUMERAL_TYPE:
58393038 1170 case RECORD_TYPE:
d1bd0ded 1171 case UNION_TYPE:
766beae1 1172 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1173 {
fcb99e7b
JJ
1174 tree a1 = TYPE_ATTRIBUTES (t1);
1175 tree a2 = TYPE_ATTRIBUTES (t2);
1176
1177 if (! attribute_list_contained (a1, a2)
1178 && ! attribute_list_contained (a2, a1))
1179 break;
1180
f13c9b2c 1181 if (attrval != 2)
dc5027f4
JM
1182 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1183 different_types_p);
1184 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1185 different_types_p);
f13c9b2c 1186 }
4b027d16 1187 break;
e9a25f70 1188
62e1dfcf 1189 case VECTOR_TYPE:
744aa42f
ILT
1190 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1191 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1192 enum_and_int_p, different_types_p));
62e1dfcf
NC
1193 break;
1194
e9a25f70
JL
1195 default:
1196 break;
400fbf9f 1197 }
4b027d16 1198 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1199}
1200
36c5e70a
BE
1201/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1202 their qualifiers, except for named address spaces. If the pointers point to
1203 different named addresses, then we must determine if one address space is a
1204 subset of the other. */
400fbf9f
JW
1205
1206static int
744aa42f 1207comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1208{
392202b0 1209 int val;
768952be 1210 int val_ped;
36c5e70a
BE
1211 tree mvl = TREE_TYPE (ttl);
1212 tree mvr = TREE_TYPE (ttr);
1213 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1214 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1215 addr_space_t as_common;
744aa42f 1216 bool enum_and_int_p;
8b40563c 1217
36c5e70a
BE
1218 /* Fail if pointers point to incompatible address spaces. */
1219 if (!addr_space_superset (asl, asr, &as_common))
1220 return 0;
1221
768952be
MU
1222 /* For pedantic record result of comptypes on arrays before losing
1223 qualifiers on the element type below. */
1224 val_ped = 1;
1225
1226 if (TREE_CODE (mvl) == ARRAY_TYPE
1227 && TREE_CODE (mvr) == ARRAY_TYPE)
1228 val_ped = comptypes (mvl, mvr);
1229
1230 /* Qualifiers on element types of array types that are
1231 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1232
1233 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1234 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1235 : TYPE_MAIN_VARIANT (mvl));
1236
1237 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1238 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1239 : TYPE_MAIN_VARIANT (mvr));
1240
744aa42f
ILT
1241 enum_and_int_p = false;
1242 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1243
768952be
MU
1244 if (val == 1 && val_ped != 1)
1245 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1246 "are incompatible in ISO C");
1247
fcf73884 1248 if (val == 2)
c1771a20 1249 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1250
1251 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1252 warning_at (location, OPT_Wc___compat,
1253 "pointer target types incompatible in C++");
1254
400fbf9f
JW
1255 return val;
1256}
1257\f
1258/* Subroutines of `comptypes'. */
1259
f75fbaf7
ZW
1260/* Determine whether two trees derive from the same translation unit.
1261 If the CONTEXT chain ends in a null, that tree's context is still
1262 being parsed, so if two trees have context chains ending in null,
766beae1 1263 they're in the same translation unit. */
f75fbaf7 1264int
58f9752a 1265same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1266{
1267 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1268 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1269 {
6615c446
JO
1270 case tcc_declaration:
1271 t1 = DECL_CONTEXT (t1); break;
1272 case tcc_type:
1273 t1 = TYPE_CONTEXT (t1); break;
1274 case tcc_exceptional:
1275 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1276 default: gcc_unreachable ();
766beae1
ZW
1277 }
1278
1279 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1280 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1281 {
6615c446
JO
1282 case tcc_declaration:
1283 t2 = DECL_CONTEXT (t2); break;
1284 case tcc_type:
1285 t2 = TYPE_CONTEXT (t2); break;
1286 case tcc_exceptional:
1287 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1288 default: gcc_unreachable ();
766beae1
ZW
1289 }
1290
1291 return t1 == t2;
1292}
1293
f13c9b2c 1294/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1295
f13c9b2c 1296static struct tagged_tu_seen_cache *
58f9752a 1297alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1298{
cceb1885 1299 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1300 tu->next = tagged_tu_seen_base;
1301 tu->t1 = t1;
1302 tu->t2 = t2;
c22cacf3 1303
f13c9b2c 1304 tagged_tu_seen_base = tu;
c22cacf3 1305
f13c9b2c
AP
1306 /* The C standard says that two structures in different translation
1307 units are compatible with each other only if the types of their
1308 fields are compatible (among other things). We assume that they
1309 are compatible until proven otherwise when building the cache.
1310 An example where this can occur is:
1311 struct a
1312 {
1313 struct a *next;
1314 };
1315 If we are comparing this against a similar struct in another TU,
c83eecad 1316 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1317 loop. */
1318 tu->val = 1;
1319 return tu;
1320}
d1bd0ded 1321
f13c9b2c 1322/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1323
f13c9b2c
AP
1324static void
1325free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1326{
1327 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1328 while (tu != tu_til)
1329 {
741ac903
KG
1330 const struct tagged_tu_seen_cache *const tu1
1331 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1332 tu = tu1->next;
b1d5455a 1333 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1334 }
1335 tagged_tu_seen_base = tu_til;
1336}
d1bd0ded
GK
1337
1338/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1339 compatible. If the two types are not the same (which has been
1340 checked earlier), this can only happen when multiple translation
1341 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1342 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1343 comptypes_internal. */
d1bd0ded
GK
1344
1345static int
744aa42f 1346tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1347 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1348{
1349 tree s1, s2;
1350 bool needs_warning = false;
3aeb3655 1351
d1bd0ded
GK
1352 /* We have to verify that the tags of the types are the same. This
1353 is harder than it looks because this may be a typedef, so we have
1354 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1355 typedef...
1356 In the case of compiler-created builtin structs the TYPE_DECL
1357 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1358 while (TYPE_NAME (t1)
1359 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1360 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1361 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1362
dea984dc
ILT
1363 while (TYPE_NAME (t2)
1364 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1365 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1366 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1367
1368 /* C90 didn't have the requirement that the two tags be the same. */
1369 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1370 return 0;
3aeb3655 1371
d1bd0ded
GK
1372 /* C90 didn't say what happened if one or both of the types were
1373 incomplete; we choose to follow C99 rules here, which is that they
1374 are compatible. */
1375 if (TYPE_SIZE (t1) == NULL
1376 || TYPE_SIZE (t2) == NULL)
1377 return 1;
3aeb3655 1378
d1bd0ded 1379 {
f13c9b2c 1380 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1381 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1382 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1383 return tts_i->val;
d1bd0ded 1384 }
3aeb3655 1385
d1bd0ded
GK
1386 switch (TREE_CODE (t1))
1387 {
1388 case ENUMERAL_TYPE:
1389 {
f13c9b2c 1390 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1391 /* Speed up the case where the type values are in the same order. */
1392 tree tv1 = TYPE_VALUES (t1);
1393 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1394
c22cacf3 1395 if (tv1 == tv2)
f13c9b2c
AP
1396 {
1397 return 1;
1398 }
3aeb3655 1399
c22cacf3
MS
1400 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1401 {
1402 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1403 break;
1404 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1405 {
c22cacf3 1406 tu->val = 0;
f13c9b2c
AP
1407 return 0;
1408 }
c22cacf3 1409 }
3aeb3655 1410
c22cacf3 1411 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1412 {
1413 return 1;
1414 }
c22cacf3 1415 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1416 {
1417 tu->val = 0;
1418 return 0;
1419 }
3aeb3655 1420
d1bd0ded 1421 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1422 {
1423 tu->val = 0;
1424 return 0;
1425 }
3aeb3655 1426
d1bd0ded
GK
1427 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1428 {
1429 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1430 if (s2 == NULL
1431 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1432 {
1433 tu->val = 0;
1434 return 0;
1435 }
d1bd0ded
GK
1436 }
1437 return 1;
1438 }
1439
1440 case UNION_TYPE:
1441 {
f13c9b2c 1442 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1443 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1444 {
1445 tu->val = 0;
1446 return 0;
1447 }
c22cacf3 1448
f13c9b2c
AP
1449 /* Speed up the common case where the fields are in the same order. */
1450 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1451 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1452 {
1453 int result;
c22cacf3 1454
3ae4d3cc 1455 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1456 break;
744aa42f 1457 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1458 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1459
1460 if (result != 1 && !DECL_NAME (s1))
1461 break;
f13c9b2c
AP
1462 if (result == 0)
1463 {
1464 tu->val = 0;
1465 return 0;
1466 }
1467 if (result == 2)
1468 needs_warning = true;
1469
1470 if (TREE_CODE (s1) == FIELD_DECL
1471 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1472 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1473 {
1474 tu->val = 0;
1475 return 0;
1476 }
1477 }
1478 if (!s1 && !s2)
1479 {
1480 tu->val = needs_warning ? 2 : 1;
1481 return tu->val;
1482 }
d1bd0ded 1483
910ad8de 1484 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1485 {
1486 bool ok = false;
3aeb3655 1487
910ad8de 1488 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1489 if (DECL_NAME (s1) == DECL_NAME (s2))
1490 {
1491 int result;
1492
744aa42f 1493 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1494 enum_and_int_p,
1495 different_types_p);
3ae4d3cc
AO
1496
1497 if (result != 1 && !DECL_NAME (s1))
1498 continue;
1499 if (result == 0)
1500 {
1501 tu->val = 0;
1502 return 0;
1503 }
1504 if (result == 2)
1505 needs_warning = true;
1506
1507 if (TREE_CODE (s1) == FIELD_DECL
1508 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1509 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1510 break;
3ae4d3cc
AO
1511
1512 ok = true;
1513 break;
1514 }
3f75a254 1515 if (!ok)
f13c9b2c
AP
1516 {
1517 tu->val = 0;
1518 return 0;
1519 }
d1bd0ded 1520 }
f13c9b2c
AP
1521 tu->val = needs_warning ? 2 : 10;
1522 return tu->val;
d1bd0ded
GK
1523 }
1524
1525 case RECORD_TYPE:
1526 {
c22cacf3 1527 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1528
1529 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1530 s1 && s2;
910ad8de 1531 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1532 {
1533 int result;
1534 if (TREE_CODE (s1) != TREE_CODE (s2)
1535 || DECL_NAME (s1) != DECL_NAME (s2))
1536 break;
744aa42f 1537 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1538 enum_and_int_p, different_types_p);
d1bd0ded
GK
1539 if (result == 0)
1540 break;
1541 if (result == 2)
1542 needs_warning = true;
3aeb3655 1543
d1bd0ded
GK
1544 if (TREE_CODE (s1) == FIELD_DECL
1545 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1546 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1547 break;
1548 }
d1bd0ded 1549 if (s1 && s2)
f13c9b2c
AP
1550 tu->val = 0;
1551 else
1552 tu->val = needs_warning ? 2 : 1;
1553 return tu->val;
d1bd0ded
GK
1554 }
1555
1556 default:
366de0ce 1557 gcc_unreachable ();
d1bd0ded
GK
1558 }
1559}
1560
400fbf9f
JW
1561/* Return 1 if two function types F1 and F2 are compatible.
1562 If either type specifies no argument types,
1563 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1564 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1565 the other must specify that number of self-promoting arg types.
744aa42f 1566 Otherwise, the argument types must match.
dc5027f4 1567 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1568
1569static int
744aa42f 1570function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1571 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1572{
1573 tree args1, args2;
1574 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1575 int val = 1;
1576 int val1;
a6fdc086
GK
1577 tree ret1, ret2;
1578
1579 ret1 = TREE_TYPE (f1);
1580 ret2 = TREE_TYPE (f2);
1581
e508a019
JM
1582 /* 'volatile' qualifiers on a function's return type used to mean
1583 the function is noreturn. */
1584 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1585 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1586 if (TYPE_VOLATILE (ret1))
1587 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1588 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1589 if (TYPE_VOLATILE (ret2))
1590 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1591 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1592 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1593 if (val == 0)
400fbf9f
JW
1594 return 0;
1595
1596 args1 = TYPE_ARG_TYPES (f1);
1597 args2 = TYPE_ARG_TYPES (f2);
1598
dc5027f4
JM
1599 if (different_types_p != NULL
1600 && (args1 == 0) != (args2 == 0))
1601 *different_types_p = true;
1602
400fbf9f
JW
1603 /* An unspecified parmlist matches any specified parmlist
1604 whose argument types don't need default promotions. */
1605
1606 if (args1 == 0)
1607 {
1608 if (!self_promoting_args_p (args2))
1609 return 0;
1610 /* If one of these types comes from a non-prototype fn definition,
1611 compare that with the other type's arglist.
3176a0c2 1612 If they don't match, ask for a warning (but no error). */
400fbf9f 1613 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1614 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1615 enum_and_int_p, different_types_p))
400fbf9f
JW
1616 val = 2;
1617 return val;
1618 }
1619 if (args2 == 0)
1620 {
1621 if (!self_promoting_args_p (args1))
1622 return 0;
1623 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1624 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1625 enum_and_int_p, different_types_p))
400fbf9f
JW
1626 val = 2;
1627 return val;
1628 }
1629
1630 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1631 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1632 different_types_p);
400fbf9f
JW
1633 return val1 != 1 ? val1 : val;
1634}
1635
744aa42f
ILT
1636/* Check two lists of types for compatibility, returning 0 for
1637 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1638 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1639 comptypes_internal. */
400fbf9f
JW
1640
1641static int
744aa42f 1642type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1643 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1644{
1645 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1646 int val = 1;
9d5f3e49 1647 int newval = 0;
400fbf9f
JW
1648
1649 while (1)
1650 {
46df2823 1651 tree a1, mv1, a2, mv2;
400fbf9f
JW
1652 if (args1 == 0 && args2 == 0)
1653 return val;
1654 /* If one list is shorter than the other,
1655 they fail to match. */
1656 if (args1 == 0 || args2 == 0)
1657 return 0;
46df2823
JM
1658 mv1 = a1 = TREE_VALUE (args1);
1659 mv2 = a2 = TREE_VALUE (args2);
1660 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1661 mv1 = (TYPE_ATOMIC (mv1)
1662 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1663 TYPE_QUAL_ATOMIC)
1664 : TYPE_MAIN_VARIANT (mv1));
46df2823 1665 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1666 mv2 = (TYPE_ATOMIC (mv2)
1667 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1668 TYPE_QUAL_ATOMIC)
1669 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1670 /* A null pointer instead of a type
1671 means there is supposed to be an argument
1672 but nothing is specified about what type it has.
1673 So match anything that self-promotes. */
dc5027f4
JM
1674 if (different_types_p != NULL
1675 && (a1 == 0) != (a2 == 0))
1676 *different_types_p = true;
46df2823 1677 if (a1 == 0)
400fbf9f 1678 {
46df2823 1679 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1680 return 0;
1681 }
46df2823 1682 else if (a2 == 0)
400fbf9f 1683 {
46df2823 1684 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1685 return 0;
1686 }
8f5b6d29 1687 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1688 else if (TREE_CODE (a1) == ERROR_MARK
1689 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1690 ;
dc5027f4
JM
1691 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1692 different_types_p)))
400fbf9f 1693 {
dc5027f4
JM
1694 if (different_types_p != NULL)
1695 *different_types_p = true;
400fbf9f
JW
1696 /* Allow wait (union {union wait *u; int *i} *)
1697 and wait (union wait *) to be compatible. */
46df2823
JM
1698 if (TREE_CODE (a1) == UNION_TYPE
1699 && (TYPE_NAME (a1) == 0
ebf0bf7f 1700 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1701 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1702 && tree_int_cst_equal (TYPE_SIZE (a1),
1703 TYPE_SIZE (a2)))
400fbf9f
JW
1704 {
1705 tree memb;
46df2823 1706 for (memb = TYPE_FIELDS (a1);
910ad8de 1707 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1708 {
1709 tree mv3 = TREE_TYPE (memb);
1710 if (mv3 && mv3 != error_mark_node
1711 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1712 mv3 = (TYPE_ATOMIC (mv3)
1713 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1714 TYPE_QUAL_ATOMIC)
1715 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1716 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1717 different_types_p))
58cb41e6
JJ
1718 break;
1719 }
400fbf9f
JW
1720 if (memb == 0)
1721 return 0;
1722 }
46df2823
JM
1723 else if (TREE_CODE (a2) == UNION_TYPE
1724 && (TYPE_NAME (a2) == 0
ebf0bf7f 1725 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1726 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1727 && tree_int_cst_equal (TYPE_SIZE (a2),
1728 TYPE_SIZE (a1)))
400fbf9f
JW
1729 {
1730 tree memb;
46df2823 1731 for (memb = TYPE_FIELDS (a2);
910ad8de 1732 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1733 {
1734 tree mv3 = TREE_TYPE (memb);
1735 if (mv3 && mv3 != error_mark_node
1736 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1737 mv3 = (TYPE_ATOMIC (mv3)
1738 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1739 TYPE_QUAL_ATOMIC)
1740 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1741 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1742 different_types_p))
58cb41e6
JJ
1743 break;
1744 }
400fbf9f
JW
1745 if (memb == 0)
1746 return 0;
1747 }
1748 else
1749 return 0;
1750 }
1751
1752 /* comptypes said ok, but record if it said to warn. */
1753 if (newval > val)
1754 val = newval;
1755
1756 args1 = TREE_CHAIN (args1);
1757 args2 = TREE_CHAIN (args2);
1758 }
1759}
400fbf9f 1760\f
a0e24419
MP
1761/* Compute the size to increment a pointer by. When a function type or void
1762 type or incomplete type is passed, size_one_node is returned.
1763 This function does not emit any diagnostics; the caller is responsible
1764 for that. */
400fbf9f 1765
4e2fb7de 1766static tree
58f9752a 1767c_size_in_bytes (const_tree type)
400fbf9f
JW
1768{
1769 enum tree_code code = TREE_CODE (type);
1770
a0e24419
MP
1771 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1772 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1773 return size_one_node;
1774
400fbf9f 1775 /* Convert in case a char is more than one unit. */
db3927fb
AH
1776 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1777 size_int (TYPE_PRECISION (char_type_node)
1778 / BITS_PER_UNIT));
400fbf9f 1779}
400fbf9f 1780\f
400fbf9f
JW
1781/* Return either DECL or its known constant value (if it has one). */
1782
56cb9733 1783tree
2f6e4e97 1784decl_constant_value (tree decl)
400fbf9f 1785{
a7c1916a 1786 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1787 in a place where a variable is invalid. Note that DECL_INITIAL
1788 isn't valid for a PARM_DECL. */
a7c1916a 1789 current_function_decl != 0
4f976745 1790 && TREE_CODE (decl) != PARM_DECL
3f75a254 1791 && !TREE_THIS_VOLATILE (decl)
83bab8db 1792 && TREE_READONLY (decl)
400fbf9f
JW
1793 && DECL_INITIAL (decl) != 0
1794 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1795 /* This is invalid if initial value is not constant.
1796 If it has either a function call, a memory reference,
1797 or a variable, then re-evaluating it could give different results. */
1798 && TREE_CONSTANT (DECL_INITIAL (decl))
1799 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1800 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1801 return DECL_INITIAL (decl);
1802 return decl;
1803}
1804
f2a71bbc
JM
1805/* Convert the array expression EXP to a pointer. */
1806static tree
c2255bc4 1807array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1808{
f2a71bbc 1809 tree orig_exp = exp;
207bf485 1810 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1811 tree adr;
1812 tree restype = TREE_TYPE (type);
1813 tree ptrtype;
207bf485 1814
f2a71bbc 1815 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1816
f2a71bbc 1817 STRIP_TYPE_NOPS (exp);
207bf485 1818
487a92fe
JM
1819 if (TREE_NO_WARNING (orig_exp))
1820 TREE_NO_WARNING (exp) = 1;
207bf485 1821
f2a71bbc
JM
1822 ptrtype = build_pointer_type (restype);
1823
22d03525 1824 if (INDIRECT_REF_P (exp))
f2a71bbc
JM
1825 return convert (ptrtype, TREE_OPERAND (exp, 0));
1826
1f37c583
JM
1827 /* In C++ array compound literals are temporary objects unless they are
1828 const or appear in namespace scope, so they are destroyed too soon
1829 to use them for much of anything (c++/53220). */
1830 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1831 {
1832 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1833 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1834 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1835 "converting an array compound literal to a pointer "
1836 "is ill-formed in C++");
1837 }
1838
c2255bc4 1839 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1840 return convert (ptrtype, adr);
1841}
207bf485 1842
f2a71bbc
JM
1843/* Convert the function expression EXP to a pointer. */
1844static tree
c2255bc4 1845function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1846{
1847 tree orig_exp = exp;
207bf485 1848
f2a71bbc 1849 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1850
f2a71bbc 1851 STRIP_TYPE_NOPS (exp);
207bf485 1852
f2a71bbc
JM
1853 if (TREE_NO_WARNING (orig_exp))
1854 TREE_NO_WARNING (exp) = 1;
207bf485 1855
c2255bc4 1856 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1857}
207bf485 1858
ebfbbdc5
JJ
1859/* Mark EXP as read, not just set, for set but not used -Wunused
1860 warning purposes. */
1861
1862void
1863mark_exp_read (tree exp)
1864{
1865 switch (TREE_CODE (exp))
1866 {
1867 case VAR_DECL:
1868 case PARM_DECL:
1869 DECL_READ_P (exp) = 1;
1870 break;
1871 case ARRAY_REF:
1872 case COMPONENT_REF:
1873 case MODIFY_EXPR:
1874 case REALPART_EXPR:
1875 case IMAGPART_EXPR:
1876 CASE_CONVERT:
1877 case ADDR_EXPR:
1878 mark_exp_read (TREE_OPERAND (exp, 0));
1879 break;
1880 case COMPOUND_EXPR:
82c3c067 1881 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1882 mark_exp_read (TREE_OPERAND (exp, 1));
1883 break;
1884 default:
1885 break;
1886 }
1887}
1888
f2a71bbc
JM
1889/* Perform the default conversion of arrays and functions to pointers.
1890 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1891 return EXP.
1892
1893 LOC is the location of the expression. */
f2a71bbc
JM
1894
1895struct c_expr
c2255bc4 1896default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1897{
1898 tree orig_exp = exp.value;
1899 tree type = TREE_TYPE (exp.value);
1900 enum tree_code code = TREE_CODE (type);
1901
1902 switch (code)
1903 {
1904 case ARRAY_TYPE:
1905 {
1906 bool not_lvalue = false;
1907 bool lvalue_array_p;
1908
1909 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1910 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1911 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1912 {
1913 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1914 not_lvalue = true;
1915 exp.value = TREE_OPERAND (exp.value, 0);
1916 }
1917
1918 if (TREE_NO_WARNING (orig_exp))
1919 TREE_NO_WARNING (exp.value) = 1;
1920
1921 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1922 if (!flag_isoc99 && !lvalue_array_p)
1923 {
1924 /* Before C99, non-lvalue arrays do not decay to pointers.
1925 Normally, using such an array would be invalid; but it can
1926 be used correctly inside sizeof or as a statement expression.
1927 Thus, do not give an error here; an error will result later. */
1928 return exp;
1929 }
1930
c2255bc4 1931 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1932 }
1933 break;
1934 case FUNCTION_TYPE:
c2255bc4 1935 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1936 break;
1937 default:
f2a71bbc 1938 break;
207bf485 1939 }
f2a71bbc 1940
207bf485
JM
1941 return exp;
1942}
1943
ebfbbdc5
JJ
1944struct c_expr
1945default_function_array_read_conversion (location_t loc, struct c_expr exp)
1946{
1947 mark_exp_read (exp.value);
1948 return default_function_array_conversion (loc, exp);
1949}
522ddfa2 1950
267bac10
JM
1951/* Return whether EXPR should be treated as an atomic lvalue for the
1952 purposes of load and store handling. */
1953
1954static bool
1955really_atomic_lvalue (tree expr)
1956{
7a0ca710 1957 if (error_operand_p (expr))
267bac10
JM
1958 return false;
1959 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1960 return false;
1961 if (!lvalue_p (expr))
1962 return false;
1963
1964 /* Ignore _Atomic on register variables, since their addresses can't
1965 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1966 sequences wouldn't work. Ignore _Atomic on structures containing
1967 bit-fields, since accessing elements of atomic structures or
1968 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1969 it's undefined at translation time or execution time, and the
1970 normal atomic sequences again wouldn't work. */
1971 while (handled_component_p (expr))
1972 {
1973 if (TREE_CODE (expr) == COMPONENT_REF
1974 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1975 return false;
1976 expr = TREE_OPERAND (expr, 0);
1977 }
1978 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1979 return false;
1980 return true;
1981}
1982
1983/* Convert expression EXP (location LOC) from lvalue to rvalue,
1984 including converting functions and arrays to pointers if CONVERT_P.
1985 If READ_P, also mark the expression as having been read. */
1986
1987struct c_expr
1988convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1989 bool convert_p, bool read_p)
1990{
1991 if (read_p)
1992 mark_exp_read (exp.value);
1993 if (convert_p)
1994 exp = default_function_array_conversion (loc, exp);
1995 if (really_atomic_lvalue (exp.value))
1996 {
1997 vec<tree, va_gc> *params;
1998 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1999 tree expr_type = TREE_TYPE (exp.value);
2000 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2001 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2002
2003 gcc_assert (TYPE_ATOMIC (expr_type));
2004
2005 /* Expansion of a generic atomic load may require an addition
2006 element, so allocate enough to prevent a resize. */
2007 vec_alloc (params, 4);
2008
2009 /* Remove the qualifiers for the rest of the expressions and
2010 create the VAL temp variable to hold the RHS. */
2011 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
5c4abbb8 2012 tmp = create_tmp_var_raw (nonatomic_type);
267bac10
JM
2013 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2014 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2015 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2016
2017 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2018 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2019 params->quick_push (expr_addr);
2020 params->quick_push (tmp_addr);
2021 params->quick_push (seq_cst);
8edbfaa6 2022 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2023
cc28fc7f
MP
2024 /* EXPR is always read. */
2025 mark_exp_read (exp.value);
2026
267bac10 2027 /* Return tmp which contains the value loaded. */
5c4abbb8
MP
2028 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2029 NULL_TREE, NULL_TREE);
267bac10
JM
2030 }
2031 return exp;
2032}
2033
522ddfa2
JM
2034/* EXP is an expression of integer type. Apply the integer promotions
2035 to it and return the promoted value. */
400fbf9f
JW
2036
2037tree
522ddfa2 2038perform_integral_promotions (tree exp)
400fbf9f 2039{
b3694847
SS
2040 tree type = TREE_TYPE (exp);
2041 enum tree_code code = TREE_CODE (type);
400fbf9f 2042
522ddfa2 2043 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2044
400fbf9f
JW
2045 /* Normally convert enums to int,
2046 but convert wide enums to something wider. */
2047 if (code == ENUMERAL_TYPE)
2048 {
b0c48229
NB
2049 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2050 TYPE_PRECISION (integer_type_node)),
2051 ((TYPE_PRECISION (type)
2052 >= TYPE_PRECISION (integer_type_node))
8df83eae 2053 && TYPE_UNSIGNED (type)));
05bccae2 2054
400fbf9f
JW
2055 return convert (type, exp);
2056 }
2057
522ddfa2
JM
2058 /* ??? This should no longer be needed now bit-fields have their
2059 proper types. */
9753f113 2060 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2061 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2062 /* If it's thinner than an int, promote it like a
d72040f5 2063 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2064 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2065 TYPE_PRECISION (integer_type_node)))
f458d1d5 2066 return convert (integer_type_node, exp);
9753f113 2067
d72040f5 2068 if (c_promoting_integer_type_p (type))
400fbf9f 2069 {
f458d1d5 2070 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2071 if (TYPE_UNSIGNED (type)
f458d1d5 2072 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2073 return convert (unsigned_type_node, exp);
05bccae2 2074
400fbf9f
JW
2075 return convert (integer_type_node, exp);
2076 }
05bccae2 2077
522ddfa2
JM
2078 return exp;
2079}
2080
2081
2082/* Perform default promotions for C data used in expressions.
46bdb9cf 2083 Enumeral types or short or char are converted to int.
522ddfa2
JM
2084 In addition, manifest constants symbols are replaced by their values. */
2085
2086tree
2087default_conversion (tree exp)
2088{
2089 tree orig_exp;
2090 tree type = TREE_TYPE (exp);
2091 enum tree_code code = TREE_CODE (type);
40449a90 2092 tree promoted_type;
522ddfa2 2093
ebfbbdc5
JJ
2094 mark_exp_read (exp);
2095
46bdb9cf
JM
2096 /* Functions and arrays have been converted during parsing. */
2097 gcc_assert (code != FUNCTION_TYPE);
2098 if (code == ARRAY_TYPE)
2099 return exp;
522ddfa2
JM
2100
2101 /* Constants can be used directly unless they're not loadable. */
2102 if (TREE_CODE (exp) == CONST_DECL)
2103 exp = DECL_INITIAL (exp);
2104
522ddfa2
JM
2105 /* Strip no-op conversions. */
2106 orig_exp = exp;
2107 STRIP_TYPE_NOPS (exp);
2108
2109 if (TREE_NO_WARNING (orig_exp))
2110 TREE_NO_WARNING (exp) = 1;
2111
400fbf9f
JW
2112 if (code == VOID_TYPE)
2113 {
5436fa2e
MP
2114 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2115 "void value not ignored as it ought to be");
400fbf9f
JW
2116 return error_mark_node;
2117 }
808d6eaa
JM
2118
2119 exp = require_complete_type (exp);
2120 if (exp == error_mark_node)
2121 return error_mark_node;
2122
40449a90
SL
2123 promoted_type = targetm.promoted_type (type);
2124 if (promoted_type)
2125 return convert (promoted_type, exp);
2126
808d6eaa
JM
2127 if (INTEGRAL_TYPE_P (type))
2128 return perform_integral_promotions (exp);
2129
400fbf9f
JW
2130 return exp;
2131}
2132\f
0fb96aa4 2133/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2134
2135 If the component name is not found, returns NULL_TREE. Otherwise,
2136 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2137 stepping down the chain to the component, which is in the last
2138 TREE_VALUE of the list. Normally the list is of length one, but if
2139 the component is embedded within (nested) anonymous structures or
2140 unions, the list steps down the chain to the component. */
2f6e4e97 2141
2f2d13da 2142static tree
0fb96aa4 2143lookup_field (tree type, tree component)
2f2d13da
DE
2144{
2145 tree field;
2146
2147 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2148 to the field elements. Use a binary search on this array to quickly
2149 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2150 will always be set for structures which have many elements. */
2151
22a0b85f 2152 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2153 {
2154 int bot, top, half;
d07605f5 2155 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2156
2157 field = TYPE_FIELDS (type);
2158 bot = 0;
d07605f5 2159 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2160 while (top - bot > 1)
2161 {
2f2d13da
DE
2162 half = (top - bot + 1) >> 1;
2163 field = field_array[bot+half];
2164
2165 if (DECL_NAME (field) == NULL_TREE)
2166 {
2167 /* Step through all anon unions in linear fashion. */
2168 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2169 {
2f2d13da 2170 field = field_array[bot++];
a68b98cf
RK
2171 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2172 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2173 {
0fb96aa4 2174 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2175
2176 if (anon)
2177 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2178
2179 /* The Plan 9 compiler permits referring
2180 directly to an anonymous struct/union field
2181 using a typedef name. */
2182 if (flag_plan9_extensions
2183 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2184 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2185 == TYPE_DECL)
2186 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2187 == component))
2188 break;
2f6e4e97 2189 }
2f2d13da
DE
2190 }
2191
2192 /* Entire record is only anon unions. */
2193 if (bot > top)
2194 return NULL_TREE;
2195
2196 /* Restart the binary search, with new lower bound. */
2197 continue;
2198 }
2199
e8b87aac 2200 if (DECL_NAME (field) == component)
2f2d13da 2201 break;
e8b87aac 2202 if (DECL_NAME (field) < component)
2f2d13da
DE
2203 bot += half;
2204 else
2205 top = bot + half;
2206 }
2207
2208 if (DECL_NAME (field_array[bot]) == component)
2209 field = field_array[bot];
2210 else if (DECL_NAME (field) != component)
e9b2c823 2211 return NULL_TREE;
2f2d13da
DE
2212 }
2213 else
2214 {
910ad8de 2215 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2216 {
e9b2c823
NB
2217 if (DECL_NAME (field) == NULL_TREE
2218 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2219 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2220 {
0fb96aa4 2221 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2222
e9b2c823
NB
2223 if (anon)
2224 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2225
2226 /* The Plan 9 compiler permits referring directly to an
2227 anonymous struct/union field using a typedef
2228 name. */
2229 if (flag_plan9_extensions
2230 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2231 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2232 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2233 == component))
2234 break;
2f2d13da
DE
2235 }
2236
2237 if (DECL_NAME (field) == component)
2238 break;
2239 }
e9b2c823
NB
2240
2241 if (field == NULL_TREE)
2242 return NULL_TREE;
2f2d13da
DE
2243 }
2244
e9b2c823 2245 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2246}
2247
c2255bc4
AH
2248/* Make an expression to refer to the COMPONENT field of structure or
2249 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2250 location of the COMPONENT_REF. */
400fbf9f
JW
2251
2252tree
c2255bc4 2253build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2254{
b3694847
SS
2255 tree type = TREE_TYPE (datum);
2256 enum tree_code code = TREE_CODE (type);
2257 tree field = NULL;
2258 tree ref;
1e57bf47 2259 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2260
7a3ea201
RH
2261 if (!objc_is_public (datum, component))
2262 return error_mark_node;
2263
46a88c12 2264 /* Detect Objective-C property syntax object.property. */
668ea4b1 2265 if (c_dialect_objc ()
46a88c12 2266 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2267 return ref;
2268
400fbf9f
JW
2269 /* See if there is a field or component with name COMPONENT. */
2270
2271 if (code == RECORD_TYPE || code == UNION_TYPE)
2272 {
d0f062fb 2273 if (!COMPLETE_TYPE_P (type))
400fbf9f 2274 {
7a228918 2275 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2276 return error_mark_node;
2277 }
2278
0fb96aa4 2279 field = lookup_field (type, component);
400fbf9f
JW
2280
2281 if (!field)
2282 {
c2255bc4 2283 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2284 return error_mark_node;
2285 }
400fbf9f 2286
e9b2c823
NB
2287 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2288 This might be better solved in future the way the C++ front
2289 end does it - by giving the anonymous entities each a
2290 separate name and type, and then have build_component_ref
2291 recursively call itself. We can't do that here. */
46ea50cb 2292 do
19d76e60 2293 {
e9b2c823 2294 tree subdatum = TREE_VALUE (field);
efed193e
JM
2295 int quals;
2296 tree subtype;
1e57bf47 2297 bool use_datum_quals;
e9b2c823
NB
2298
2299 if (TREE_TYPE (subdatum) == error_mark_node)
2300 return error_mark_node;
2301
1e57bf47
JM
2302 /* If this is an rvalue, it does not have qualifiers in C
2303 standard terms and we must avoid propagating such
2304 qualifiers down to a non-lvalue array that is then
2305 converted to a pointer. */
2306 use_datum_quals = (datum_lvalue
2307 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2308
efed193e 2309 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2310 if (use_datum_quals)
2311 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2312 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2313
2314 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2315 NULL_TREE);
c2255bc4 2316 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2317 if (TREE_READONLY (subdatum)
2318 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2319 TREE_READONLY (ref) = 1;
1e57bf47
JM
2320 if (TREE_THIS_VOLATILE (subdatum)
2321 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2322 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2323
2324 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2325 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2326
19d76e60 2327 datum = ref;
46ea50cb
RS
2328
2329 field = TREE_CHAIN (field);
19d76e60 2330 }
46ea50cb 2331 while (field);
19d76e60 2332
400fbf9f
JW
2333 return ref;
2334 }
2335 else if (code != ERROR_MARK)
c2255bc4
AH
2336 error_at (loc,
2337 "request for member %qE in something not a structure or union",
2338 component);
400fbf9f
JW
2339
2340 return error_mark_node;
2341}
2342\f
2343/* Given an expression PTR for a pointer, return an expression
2344 for the value pointed to.
6a3799eb
AH
2345 ERRORSTRING is the name of the operator to appear in error messages.
2346
2347 LOC is the location to use for the generated tree. */
400fbf9f
JW
2348
2349tree
dd865ef6 2350build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2351{
b3694847
SS
2352 tree pointer = default_conversion (ptr);
2353 tree type = TREE_TYPE (pointer);
6a3799eb 2354 tree ref;
400fbf9f
JW
2355
2356 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2357 {
1043771b 2358 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2359 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2360 {
2361 /* If a warning is issued, mark it to avoid duplicates from
2362 the backend. This only needs to be done at
2363 warn_strict_aliasing > 2. */
2364 if (warn_strict_aliasing > 2)
2365 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2366 type, TREE_OPERAND (pointer, 0)))
2367 TREE_NO_WARNING (pointer) = 1;
2368 }
2369
870cc33b 2370 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2371 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2372 == TREE_TYPE (type)))
6a3799eb
AH
2373 {
2374 ref = TREE_OPERAND (pointer, 0);
2375 protected_set_expr_location (ref, loc);
2376 return ref;
2377 }
870cc33b
RS
2378 else
2379 {
2380 tree t = TREE_TYPE (type);
46df2823 2381
984dfd8c 2382 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2383
baae9b65 2384 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2385 {
d9b7be2e
MP
2386 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2387 {
2388 error_at (loc, "dereferencing pointer to incomplete type "
2389 "%qT", t);
2390 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2391 }
870cc33b
RS
2392 return error_mark_node;
2393 }
7d882b83 2394 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2395 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2396
2397 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2398 so that we get the proper error message if the result is used
2399 to assign to. Also, &* is supposed to be a no-op.
2400 And ANSI C seems to specify that the type of the result
2401 should be the const type. */
2402 /* A de-reference of a pointer to const is not a const. It is valid
2403 to change it via some other pointer. */
2404 TREE_READONLY (ref) = TYPE_READONLY (t);
2405 TREE_SIDE_EFFECTS (ref)
271bd540 2406 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2407 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2408 protected_set_expr_location (ref, loc);
870cc33b
RS
2409 return ref;
2410 }
2411 }
400fbf9f 2412 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2413 invalid_indirection_error (loc, type, errstring);
2414
400fbf9f
JW
2415 return error_mark_node;
2416}
2417
2418/* This handles expressions of the form "a[i]", which denotes
2419 an array reference.
2420
2421 This is logically equivalent in C to *(a+i), but we may do it differently.
2422 If A is a variable or a member, we generate a primitive ARRAY_REF.
2423 This avoids forcing the array out of registers, and can work on
2424 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2425 by functions).
2426
30cd1c5d
AS
2427 For vector types, allow vector[i] but not i[vector], and create
2428 *(((type*)&vectortype) + i) for the expression.
2429
6a3799eb 2430 LOC is the location to use for the returned expression. */
400fbf9f
JW
2431
2432tree
c2255bc4 2433build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2434{
6a3799eb 2435 tree ret;
a4ab7973 2436 bool swapped = false;
400fbf9f
JW
2437 if (TREE_TYPE (array) == error_mark_node
2438 || TREE_TYPE (index) == error_mark_node)
2439 return error_mark_node;
2440
b72271b9 2441 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2442 {
2443 size_t rank = 0;
2444 if (!find_rank (loc, index, index, true, &rank))
2445 return error_mark_node;
2446 if (rank > 1)
2447 {
2448 error_at (loc, "rank of the array's index is greater than 1");
2449 return error_mark_node;
2450 }
2451 }
a4ab7973 2452 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2453 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2454 /* Allow vector[index] but not index[vector]. */
31521951 2455 && !VECTOR_TYPE_P (TREE_TYPE (array)))
400fbf9f 2456 {
a4ab7973
JM
2457 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2458 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2459 {
f90e8e2e 2460 error_at (loc,
30cd1c5d
AS
2461 "subscripted value is neither array nor pointer nor vector");
2462
fdeefd49
RS
2463 return error_mark_node;
2464 }
fab27f52 2465 std::swap (array, index);
a4ab7973
JM
2466 swapped = true;
2467 }
2468
2469 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2470 {
a63068b6 2471 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2472 return error_mark_node;
2473 }
2474
2475 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2476 {
a63068b6 2477 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2478 return error_mark_node;
2479 }
2480
ff6b6641
GDR
2481 /* ??? Existing practice has been to warn only when the char
2482 index is syntactically the index, not for char[array]. */
2483 if (!swapped)
5bd012f8 2484 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2485
2486 /* Apply default promotions *after* noticing character types. */
2487 index = default_conversion (index);
f406ae1f
MP
2488 if (index == error_mark_node)
2489 return error_mark_node;
a4ab7973
JM
2490
2491 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2492
aa7da51a
JJ
2493 bool non_lvalue
2494 = convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2495
2496 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2497 {
e4d35515 2498 tree rval, type;
fdeefd49 2499
400fbf9f
JW
2500 /* An array that is indexed by a non-constant
2501 cannot be stored in a register; we must be able to do
2502 address arithmetic on its address.
2503 Likewise an array of elements of variable size. */
2504 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2505 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2506 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2507 {
dffd7eb6 2508 if (!c_mark_addressable (array))
400fbf9f
JW
2509 return error_mark_node;
2510 }
e6d52559
JW
2511 /* An array that is indexed by a constant value which is not within
2512 the array bounds cannot be stored in a register either; because we
2513 would get a crash in store_bit_field/extract_bit_field when trying
2514 to access a non-existent part of the register. */
2515 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2516 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2517 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2518 {
dffd7eb6 2519 if (!c_mark_addressable (array))
e6d52559
JW
2520 return error_mark_node;
2521 }
400fbf9f 2522
f3bede71 2523 if (pedantic || warn_c90_c99_compat)
400fbf9f
JW
2524 {
2525 tree foo = array;
2526 while (TREE_CODE (foo) == COMPONENT_REF)
2527 foo = TREE_OPERAND (foo, 0);
0ae9bd27 2528 if (VAR_P (foo) && C_DECL_REGISTER (foo))
c1771a20 2529 pedwarn (loc, OPT_Wpedantic,
fcf73884 2530 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2531 else if (!lvalue_p (foo))
2532 pedwarn_c90 (loc, OPT_Wpedantic,
2533 "ISO C90 forbids subscripting non-lvalue "
2534 "array");
400fbf9f
JW
2535 }
2536
46df2823 2537 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2538 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2539 /* Array ref is const/volatile if the array elements are
c22cacf3 2540 or if the array is. */
400fbf9f
JW
2541 TREE_READONLY (rval)
2542 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2543 | TREE_READONLY (array));
2544 TREE_SIDE_EFFECTS (rval)
2545 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2546 | TREE_SIDE_EFFECTS (array));
2547 TREE_THIS_VOLATILE (rval)
2548 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2549 /* This was added by rms on 16 Nov 91.
2f6e4e97 2550 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2551 in an inline function.
2552 Hope it doesn't break something else. */
2553 | TREE_THIS_VOLATILE (array));
928c19bb 2554 ret = require_complete_type (rval);
6a3799eb 2555 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2556 if (non_lvalue)
2557 ret = non_lvalue_loc (loc, ret);
6a3799eb 2558 return ret;
400fbf9f 2559 }
a4ab7973
JM
2560 else
2561 {
2562 tree ar = default_conversion (array);
400fbf9f 2563
a4ab7973
JM
2564 if (ar == error_mark_node)
2565 return ar;
400fbf9f 2566
a4ab7973
JM
2567 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2568 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2569
aa7da51a
JJ
2570 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2571 index, 0),
2572 RO_ARRAY_INDEXING);
2573 if (non_lvalue)
2574 ret = non_lvalue_loc (loc, ret);
2575 return ret;
a4ab7973 2576 }
400fbf9f
JW
2577}
2578\f
7e585d16 2579/* Build an external reference to identifier ID. FUN indicates
766beb40 2580 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2581 location of the identifier. This sets *TYPE to the type of the
2582 identifier, which is not the same as the type of the returned value
2583 for CONST_DECLs defined as enum constants. If the type of the
2584 identifier is not available, *TYPE is set to NULL. */
7e585d16 2585tree
c2255bc4 2586build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2587{
2588 tree ref;
2589 tree decl = lookup_name (id);
16b34ad6
ZL
2590
2591 /* In Objective-C, an instance variable (ivar) may be preferred to
2592 whatever lookup_name() found. */
2593 decl = objc_lookup_ivar (decl, id);
7e585d16 2594
6866c6e8 2595 *type = NULL;
339a28b9 2596 if (decl && decl != error_mark_node)
6866c6e8
ILT
2597 {
2598 ref = decl;
2599 *type = TREE_TYPE (ref);
2600 }
339a28b9
ZW
2601 else if (fun)
2602 /* Implicit function declaration. */
c2255bc4 2603 ref = implicitly_declare (loc, id);
339a28b9
ZW
2604 else if (decl == error_mark_node)
2605 /* Don't complain about something that's already been
2606 complained about. */
2607 return error_mark_node;
2608 else
2609 {
c2255bc4 2610 undeclared_variable (loc, id);
339a28b9
ZW
2611 return error_mark_node;
2612 }
7e585d16
ZW
2613
2614 if (TREE_TYPE (ref) == error_mark_node)
2615 return error_mark_node;
2616
339a28b9 2617 if (TREE_DEPRECATED (ref))
9b86d6bb 2618 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2619
ad960f56 2620 /* Recursive call does not count as usage. */
b8698a0f 2621 if (ref != current_function_decl)
ad960f56 2622 {
ad960f56
MLI
2623 TREE_USED (ref) = 1;
2624 }
7e585d16 2625
bc4b653b
JM
2626 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2627 {
2628 if (!in_sizeof && !in_typeof)
2629 C_DECL_USED (ref) = 1;
2630 else if (DECL_INITIAL (ref) == 0
2631 && DECL_EXTERNAL (ref)
2632 && !TREE_PUBLIC (ref))
2633 record_maybe_used_decl (ref);
2634 }
2635
7e585d16
ZW
2636 if (TREE_CODE (ref) == CONST_DECL)
2637 {
6193b8b7 2638 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2639
2640 if (warn_cxx_compat
2641 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2642 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2643 {
2644 warning_at (loc, OPT_Wc___compat,
2645 ("enum constant defined in struct or union "
2646 "is not visible in C++"));
2647 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2648 }
2649
7e585d16
ZW
2650 ref = DECL_INITIAL (ref);
2651 TREE_CONSTANT (ref) = 1;
2652 }
6a29edea 2653 else if (current_function_decl != 0
4b1e44be 2654 && !DECL_FILE_SCOPE_P (current_function_decl)
21b634ae
MP
2655 && (VAR_OR_FUNCTION_DECL_P (ref)
2656 || TREE_CODE (ref) == PARM_DECL))
6a29edea
EB
2657 {
2658 tree context = decl_function_context (ref);
2f6e4e97 2659
6a29edea
EB
2660 if (context != 0 && context != current_function_decl)
2661 DECL_NONLOCAL (ref) = 1;
2662 }
71113fcd
GK
2663 /* C99 6.7.4p3: An inline definition of a function with external
2664 linkage ... shall not contain a reference to an identifier with
2665 internal linkage. */
2666 else if (current_function_decl != 0
2667 && DECL_DECLARED_INLINE_P (current_function_decl)
2668 && DECL_EXTERNAL (current_function_decl)
2669 && VAR_OR_FUNCTION_DECL_P (ref)
0ae9bd27 2670 && (!VAR_P (ref) || TREE_STATIC (ref))
1033ffa8
JJ
2671 && ! TREE_PUBLIC (ref)
2672 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2673 record_inline_static (loc, current_function_decl, ref,
2674 csi_internal);
7e585d16
ZW
2675
2676 return ref;
2677}
2678
bc4b653b
JM
2679/* Record details of decls possibly used inside sizeof or typeof. */
2680struct maybe_used_decl
2681{
2682 /* The decl. */
2683 tree decl;
2684 /* The level seen at (in_sizeof + in_typeof). */
2685 int level;
2686 /* The next one at this level or above, or NULL. */
2687 struct maybe_used_decl *next;
2688};
2689
2690static struct maybe_used_decl *maybe_used_decls;
2691
2692/* Record that DECL, an undefined static function reference seen
2693 inside sizeof or typeof, might be used if the operand of sizeof is
2694 a VLA type or the operand of typeof is a variably modified
2695 type. */
2696
4e2fb7de 2697static void
bc4b653b
JM
2698record_maybe_used_decl (tree decl)
2699{
2700 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2701 t->decl = decl;
2702 t->level = in_sizeof + in_typeof;
2703 t->next = maybe_used_decls;
2704 maybe_used_decls = t;
2705}
2706
2707/* Pop the stack of decls possibly used inside sizeof or typeof. If
2708 USED is false, just discard them. If it is true, mark them used
2709 (if no longer inside sizeof or typeof) or move them to the next
2710 level up (if still inside sizeof or typeof). */
2711
2712void
2713pop_maybe_used (bool used)
2714{
2715 struct maybe_used_decl *p = maybe_used_decls;
2716 int cur_level = in_sizeof + in_typeof;
2717 while (p && p->level > cur_level)
2718 {
2719 if (used)
2720 {
2721 if (cur_level == 0)
2722 C_DECL_USED (p->decl) = 1;
2723 else
2724 p->level = cur_level;
2725 }
2726 p = p->next;
2727 }
2728 if (!used || cur_level == 0)
2729 maybe_used_decls = p;
2730}
2731
2732/* Return the result of sizeof applied to EXPR. */
2733
2734struct c_expr
c2255bc4 2735c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2736{
2737 struct c_expr ret;
ad97f4be
JM
2738 if (expr.value == error_mark_node)
2739 {
2740 ret.value = error_mark_node;
2741 ret.original_code = ERROR_MARK;
6866c6e8 2742 ret.original_type = NULL;
ad97f4be
JM
2743 pop_maybe_used (false);
2744 }
2745 else
2746 {
928c19bb 2747 bool expr_const_operands = true;
773ec47f
MP
2748
2749 if (TREE_CODE (expr.value) == PARM_DECL
2750 && C_ARRAY_PARAMETER (expr.value))
2751 {
2752 if (warning_at (loc, OPT_Wsizeof_array_argument,
2753 "%<sizeof%> on array function parameter %qE will "
2754 "return size of %qT", expr.value,
2755 expr.original_type))
2756 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2757 }
928c19bb
JM
2758 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2759 &expr_const_operands);
c2255bc4 2760 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2761 c_last_sizeof_arg = expr.value;
2762 ret.original_code = SIZEOF_EXPR;
6866c6e8 2763 ret.original_type = NULL;
928c19bb 2764 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2765 {
2766 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2767 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2768 folded_expr, ret.value);
2769 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2770 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2771 }
928c19bb 2772 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2773 }
bc4b653b
JM
2774 return ret;
2775}
2776
2777/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2778 name passed to sizeof (rather than the type itself). LOC is the
2779 location of the original expression. */
bc4b653b
JM
2780
2781struct c_expr
c2255bc4 2782c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2783{
2784 tree type;
2785 struct c_expr ret;
928c19bb
JM
2786 tree type_expr = NULL_TREE;
2787 bool type_expr_const = true;
2788 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2789 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2790 c_last_sizeof_arg = type;
2791 ret.original_code = SIZEOF_EXPR;
6866c6e8 2792 ret.original_type = NULL;
24070fcb
JM
2793 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2794 && c_vla_type_p (type))
928c19bb 2795 {
24070fcb
JM
2796 /* If the type is a [*] array, it is a VLA but is represented as
2797 having a size of zero. In such a case we must ensure that
2798 the result of sizeof does not get folded to a constant by
2799 c_fully_fold, because if the size is evaluated the result is
2800 not constant and so constraints on zero or negative size
2801 arrays must not be applied when this sizeof call is inside
2802 another array declarator. */
2803 if (!type_expr)
2804 type_expr = integer_zero_node;
928c19bb
JM
2805 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2806 type_expr, ret.value);
2807 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2808 }
16464cc1
VR
2809 pop_maybe_used (type != error_mark_node
2810 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2811 return ret;
2812}
2813
400fbf9f 2814/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2815 The function call is at LOC.
400fbf9f
JW
2816 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2817 TREE_VALUE of each node is a parameter-expression.
2818 FUNCTION's data type may be a function type or a pointer-to-function. */
2819
2820tree
c2255bc4 2821build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2822{
9771b263 2823 vec<tree, va_gc> *v;
bbbbb16a
ILT
2824 tree ret;
2825
9771b263 2826 vec_alloc (v, list_length (params));
bbbbb16a 2827 for (; params; params = TREE_CHAIN (params))
9771b263 2828 v->quick_push (TREE_VALUE (params));
8edbfaa6 2829 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2830 vec_free (v);
bbbbb16a
ILT
2831 return ret;
2832}
2833
ae52741c
MLI
2834/* Give a note about the location of the declaration of DECL. */
2835
c7b70a3c
MP
2836static void
2837inform_declaration (tree decl)
ae52741c 2838{
c7b70a3c 2839 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
ae52741c
MLI
2840 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2841}
2842
bbbbb16a
ILT
2843/* Build a function call to function FUNCTION with parameters PARAMS.
2844 ORIGTYPES, if not NULL, is a vector of types; each element is
2845 either NULL or the original type of the corresponding element in
2846 PARAMS. The original type may differ from TREE_TYPE of the
2847 parameter for enums. FUNCTION's data type may be a function type
2848 or pointer-to-function. This function changes the elements of
2849 PARAMS. */
2850
2851tree
81e5eca8
MP
2852build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2853 tree function, vec<tree, va_gc> *params,
9771b263 2854 vec<tree, va_gc> *origtypes)
400fbf9f 2855{
b3694847 2856 tree fntype, fundecl = 0;
4977bab6 2857 tree name = NULL_TREE, result;
c96f4f73 2858 tree tem;
94a0dd7b
SL
2859 int nargs;
2860 tree *argarray;
b8698a0f 2861
400fbf9f 2862
fc76e425 2863 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2864 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2865
2866 /* Convert anything with function type to a pointer-to-function. */
2867 if (TREE_CODE (function) == FUNCTION_DECL)
2868 {
2869 name = DECL_NAME (function);
0a35513e
AH
2870
2871 if (flag_tm)
2872 tm_malloc_replacement (function);
a5eadacc 2873 fundecl = function;
86951993
AM
2874 /* Atomic functions have type checking/casting already done. They are
2875 often rewritten and don't match the original parameter list. */
2876 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2877 origtypes = NULL;
36536d79 2878
b72271b9 2879 if (flag_cilkplus
36536d79
BI
2880 && is_cilkplus_reduce_builtin (function))
2881 origtypes = NULL;
400fbf9f 2882 }
f2a71bbc 2883 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2884 function = function_to_pointer_conversion (loc, function);
400fbf9f 2885
6e955430
ZL
2886 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2887 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2888 if (params && !params->is_empty ())
2889 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2890
928c19bb
JM
2891 function = c_fully_fold (function, false, NULL);
2892
400fbf9f
JW
2893 fntype = TREE_TYPE (function);
2894
2895 if (TREE_CODE (fntype) == ERROR_MARK)
2896 return error_mark_node;
2897
2898 if (!(TREE_CODE (fntype) == POINTER_TYPE
2899 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2900 {
ae52741c
MLI
2901 if (!flag_diagnostics_show_caret)
2902 error_at (loc,
2903 "called object %qE is not a function or function pointer",
2904 function);
2905 else if (DECL_P (function))
2906 {
2907 error_at (loc,
2908 "called object %qD is not a function or function pointer",
2909 function);
2910 inform_declaration (function);
2911 }
2912 else
2913 error_at (loc,
2914 "called object is not a function or function pointer");
400fbf9f
JW
2915 return error_mark_node;
2916 }
2917
5ce89b2e
JM
2918 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2919 current_function_returns_abnormally = 1;
2920
400fbf9f
JW
2921 /* fntype now gets the type of function pointed to. */
2922 fntype = TREE_TYPE (fntype);
2923
ab4194da
JM
2924 /* Convert the parameters to the types declared in the
2925 function prototype, or apply default promotions. */
2926
81e5eca8
MP
2927 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2928 origtypes, function, fundecl);
ab4194da
JM
2929 if (nargs < 0)
2930 return error_mark_node;
2931
c96f4f73 2932 /* Check that the function is called through a compatible prototype.
fa337f3a 2933 If it is not, warn. */
1043771b 2934 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2935 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2936 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2937 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2938 {
2939 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
2940
2941 /* This situation leads to run-time undefined behavior. We can't,
2942 therefore, simply error unless we can prove that all possible
2943 executions of the program must execute the code. */
fa337f3a 2944 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 2945
fa337f3a
RB
2946 if (VOID_TYPE_P (return_type)
2947 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2948 pedwarn (loc, 0,
2949 "function with qualified void return type called");
2950 }
c96f4f73 2951
9771b263 2952 argarray = vec_safe_address (params);
bbbbb16a 2953
83322951
RG
2954 /* Check that arguments to builtin functions match the expectations. */
2955 if (fundecl
2956 && DECL_BUILT_IN (fundecl)
2957 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2958 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2959 return error_mark_node;
400fbf9f 2960
83322951 2961 /* Check that the arguments to the function are valid. */
dde05067 2962 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2963
928c19bb
JM
2964 if (name != NULL_TREE
2965 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2966 {
928c19bb 2967 if (require_constant_value)
b8698a0f 2968 result =
db3927fb
AH
2969 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2970 function, nargs, argarray);
928c19bb 2971 else
db3927fb
AH
2972 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2973 function, nargs, argarray);
928c19bb
JM
2974 if (TREE_CODE (result) == NOP_EXPR
2975 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2976 STRIP_TYPE_NOPS (result);
bf730f15
RS
2977 }
2978 else
db3927fb
AH
2979 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2980 function, nargs, argarray);
b0b3afb2 2981
71653180 2982 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
2983 {
2984 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 2985 pedwarn (loc, 0,
3ce62965
JM
2986 "function with qualified void return type called");
2987 return result;
2988 }
1eb8759b 2989 return require_complete_type (result);
400fbf9f 2990}
8edbfaa6
JJ
2991
2992/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2993
2994tree
2995c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2996 tree function, vec<tree, va_gc> *params,
2997 vec<tree, va_gc> *origtypes)
2998{
2999 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3000 STRIP_TYPE_NOPS (function);
3001
3002 /* Convert anything with function type to a pointer-to-function. */
3003 if (TREE_CODE (function) == FUNCTION_DECL)
3004 {
3005 /* Implement type-directed function overloading for builtins.
3006 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3007 handle all the type checking. The result is a complete expression
3008 that implements this function call. */
3009 tree tem = resolve_overloaded_builtin (loc, function, params);
3010 if (tem)
3011 return tem;
3012 }
3013 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3014}
400fbf9f 3015\f
bbbbb16a
ILT
3016/* Convert the argument expressions in the vector VALUES
3017 to the types in the list TYPELIST.
400fbf9f
JW
3018
3019 If TYPELIST is exhausted, or when an element has NULL as its type,
3020 perform the default conversions.
3021
bbbbb16a
ILT
3022 ORIGTYPES is the original types of the expressions in VALUES. This
3023 holds the type of enum values which have been converted to integral
3024 types. It may be NULL.
400fbf9f 3025
03dafa61
JM
3026 FUNCTION is a tree for the called function. It is used only for
3027 error messages, where it is formatted with %qE.
400fbf9f
JW
3028
3029 This is also where warnings about wrong number of args are generated.
3030
81e5eca8
MP
3031 ARG_LOC are locations of function arguments (if any).
3032
94a0dd7b 3033 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3034 than the length of VALUES in some error situations), or -1 on
3035 failure. */
94a0dd7b
SL
3036
3037static int
81e5eca8
MP
3038convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3039 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3040 tree function, tree fundecl)
400fbf9f 3041{
bbbbb16a
ILT
3042 tree typetail, val;
3043 unsigned int parmnum;
06302a02 3044 bool error_args = false;
b5d32c25 3045 const bool type_generic = fundecl
81e5eca8 3046 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3047 bool type_generic_remove_excess_precision = false;
03dafa61 3048 tree selector;
03dafa61 3049
2ac2f164
JM
3050 /* Change pointer to function to the function itself for
3051 diagnostics. */
03dafa61
JM
3052 if (TREE_CODE (function) == ADDR_EXPR
3053 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3054 function = TREE_OPERAND (function, 0);
03dafa61
JM
3055
3056 /* Handle an ObjC selector specially for diagnostics. */
3057 selector = objc_message_selector ();
400fbf9f 3058
8ce94e44
JM
3059 /* For type-generic built-in functions, determine whether excess
3060 precision should be removed (classification) or not
3061 (comparison). */
3062 if (type_generic
3063 && DECL_BUILT_IN (fundecl)
3064 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3065 {
3066 switch (DECL_FUNCTION_CODE (fundecl))
3067 {
3068 case BUILT_IN_ISFINITE:
3069 case BUILT_IN_ISINF:
3070 case BUILT_IN_ISINF_SIGN:
3071 case BUILT_IN_ISNAN:
3072 case BUILT_IN_ISNORMAL:
3073 case BUILT_IN_FPCLASSIFY:
3074 type_generic_remove_excess_precision = true;
3075 break;
3076
3077 default:
3078 type_generic_remove_excess_precision = false;
3079 break;
3080 }
3081 }
b72271b9 3082 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3083 return vec_safe_length (values);
8ce94e44 3084
400fbf9f 3085 /* Scan the given expressions and types, producing individual
bbbbb16a 3086 converted arguments. */
400fbf9f 3087
bbbbb16a 3088 for (typetail = typelist, parmnum = 0;
9771b263 3089 values && values->iterate (parmnum, &val);
bbbbb16a 3090 ++parmnum)
400fbf9f 3091 {
b3694847 3092 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3093 tree valtype = TREE_TYPE (val);
03dafa61
JM
3094 tree rname = function;
3095 int argnum = parmnum + 1;
4d3e6fae 3096 const char *invalid_func_diag;
8ce94e44 3097 bool excess_precision = false;
928c19bb 3098 bool npc;
bbbbb16a 3099 tree parmval;
5c1bc275
MP
3100 /* Some __atomic_* builtins have additional hidden argument at
3101 position 0. */
3102 location_t ploc
3103 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3104 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3105 : input_location;
400fbf9f
JW
3106
3107 if (type == void_type_node)
3108 {
19dc6d01 3109 if (selector)
68fca595 3110 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3111 else
68fca595 3112 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3113 inform_declaration (fundecl);
d38f7dce 3114 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3115 }
3116
03dafa61
JM
3117 if (selector && argnum > 2)
3118 {
3119 rname = selector;
3120 argnum -= 2;
3121 }
3122
928c19bb 3123 npc = null_pointer_constant_p (val);
8ce94e44
JM
3124
3125 /* If there is excess precision and a prototype, convert once to
3126 the required type rather than converting via the semantic
3127 type. Likewise without a prototype a float value represented
3128 as long double should be converted once to double. But for
3129 type-generic classification functions excess precision must
3130 be removed here. */
3131 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3132 && (type || !type_generic || !type_generic_remove_excess_precision))
3133 {
3134 val = TREE_OPERAND (val, 0);
3135 excess_precision = true;
3136 }
928c19bb 3137 val = c_fully_fold (val, false, NULL);
ed248cf7 3138 STRIP_TYPE_NOPS (val);
400fbf9f 3139
400fbf9f
JW
3140 val = require_complete_type (val);
3141
3142 if (type != 0)
3143 {
3144 /* Formal parm type is specified by a function prototype. */
400fbf9f 3145
20913689 3146 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3147 {
5c1bc275
MP
3148 error_at (ploc, "type of formal parameter %d is incomplete",
3149 parmnum + 1);
400fbf9f
JW
3150 parmval = val;
3151 }
3152 else
3153 {
bbbbb16a
ILT
3154 tree origtype;
3155
d45cf215
RS
3156 /* Optionally warn about conversions that
3157 differ from the default conversions. */
05170031 3158 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3159 {
e3a64162 3160 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3161
aae43c5f 3162 if (INTEGRAL_TYPE_P (type)
8ce94e44 3163 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3164 warning_at (ploc, OPT_Wtraditional_conversion,
3165 "passing argument %d of %qE as integer rather "
3166 "than floating due to prototype",
3167 argnum, rname);
03829ad2 3168 if (INTEGRAL_TYPE_P (type)
8ce94e44 3169 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3170 warning_at (ploc, OPT_Wtraditional_conversion,
3171 "passing argument %d of %qE as integer rather "
3172 "than complex due to prototype",
3173 argnum, rname);
aae43c5f 3174 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3175 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3176 warning_at (ploc, OPT_Wtraditional_conversion,
3177 "passing argument %d of %qE as complex rather "
3178 "than floating due to prototype",
3179 argnum, rname);
400fbf9f 3180 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3181 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3182 warning_at (ploc, OPT_Wtraditional_conversion,
3183 "passing argument %d of %qE as floating rather "
3184 "than integer due to prototype",
3185 argnum, rname);
03829ad2 3186 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3187 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3188 warning_at (ploc, OPT_Wtraditional_conversion,
3189 "passing argument %d of %qE as complex rather "
3190 "than integer due to prototype",
3191 argnum, rname);
aae43c5f 3192 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3193 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3194 warning_at (ploc, OPT_Wtraditional_conversion,
3195 "passing argument %d of %qE as floating rather "
3196 "than complex due to prototype",
3197 argnum, rname);
aae43c5f
RK
3198 /* ??? At some point, messages should be written about
3199 conversions between complex types, but that's too messy
3200 to do now. */
d45cf215 3201 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3202 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3203 {
3204 /* Warn if any argument is passed as `float',
047de90b 3205 since without a prototype it would be `double'. */
9a8ce21f
JG
3206 if (formal_prec == TYPE_PRECISION (float_type_node)
3207 && type != dfloat32_type_node)
5c1bc275
MP
3208 warning_at (ploc, 0,
3209 "passing argument %d of %qE as %<float%> "
3210 "rather than %<double%> due to prototype",
3211 argnum, rname);
9a8ce21f
JG
3212
3213 /* Warn if mismatch between argument and prototype
3214 for decimal float types. Warn of conversions with
3215 binary float types and of precision narrowing due to
3216 prototype. */
8ce94e44 3217 else if (type != valtype
9a8ce21f
JG
3218 && (type == dfloat32_type_node
3219 || type == dfloat64_type_node
c22cacf3 3220 || type == dfloat128_type_node
8ce94e44
JM
3221 || valtype == dfloat32_type_node
3222 || valtype == dfloat64_type_node
3223 || valtype == dfloat128_type_node)
c22cacf3 3224 && (formal_prec
8ce94e44 3225 <= TYPE_PRECISION (valtype)
9a8ce21f 3226 || (type == dfloat128_type_node
8ce94e44 3227 && (valtype
c22cacf3 3228 != dfloat64_type_node
8ce94e44 3229 && (valtype
9a8ce21f
JG
3230 != dfloat32_type_node)))
3231 || (type == dfloat64_type_node
8ce94e44 3232 && (valtype
9a8ce21f 3233 != dfloat32_type_node))))
5c1bc275
MP
3234 warning_at (ploc, 0,
3235 "passing argument %d of %qE as %qT "
3236 "rather than %qT due to prototype",
3237 argnum, rname, type, valtype);
9a8ce21f 3238
d45cf215 3239 }
3ed56f8a
KG
3240 /* Detect integer changing in width or signedness.
3241 These warnings are only activated with
05170031
MLI
3242 -Wtraditional-conversion, not with -Wtraditional. */
3243 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3244 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3245 {
d45cf215
RS
3246 tree would_have_been = default_conversion (val);
3247 tree type1 = TREE_TYPE (would_have_been);
3248
754a4d82 3249 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3250 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3251 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3252 /* No warning if function asks for enum
3253 and the actual arg is that enum type. */
3254 ;
3255 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3256 warning_at (ploc, OPT_Wtraditional_conversion,
3257 "passing argument %d of %qE "
3258 "with different width due to prototype",
3259 argnum, rname);
8df83eae 3260 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3261 ;
800cd3b9
RS
3262 /* Don't complain if the formal parameter type
3263 is an enum, because we can't tell now whether
3264 the value was an enum--even the same enum. */
3265 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3266 ;
400fbf9f
JW
3267 else if (TREE_CODE (val) == INTEGER_CST
3268 && int_fits_type_p (val, type))
3269 /* Change in signedness doesn't matter
3270 if a constant value is unaffected. */
3271 ;
ce9895ae
RS
3272 /* If the value is extended from a narrower
3273 unsigned type, it doesn't matter whether we
3274 pass it as signed or unsigned; the value
3275 certainly is the same either way. */
8ce94e44
JM
3276 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3277 && TYPE_UNSIGNED (valtype))
ce9895ae 3278 ;
8df83eae 3279 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3280 warning_at (ploc, OPT_Wtraditional_conversion,
3281 "passing argument %d of %qE "
3282 "as unsigned due to prototype",
3283 argnum, rname);
3ed56f8a 3284 else
5c1bc275
MP
3285 warning_at (ploc, OPT_Wtraditional_conversion,
3286 "passing argument %d of %qE "
3287 "as signed due to prototype",
3288 argnum, rname);
400fbf9f
JW
3289 }
3290 }
3291
8ce94e44
JM
3292 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3293 sake of better warnings from convert_and_check. */
3294 if (excess_precision)
3295 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3296 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3297 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3298 val, origtype, ic_argpass,
3299 npc, fundecl, function,
2ac2f164 3300 parmnum + 1);
2f6e4e97 3301
61f71b34 3302 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3303 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3304 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3305 parmval = default_conversion (parmval);
400fbf9f 3306 }
400fbf9f 3307 }
8ce94e44
JM
3308 else if (TREE_CODE (valtype) == REAL_TYPE
3309 && (TYPE_PRECISION (valtype)
2531a1d9 3310 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3311 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3312 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3313 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3314 {
3315 if (type_generic)
bbbbb16a 3316 parmval = val;
b5d32c25 3317 else
0a0b3574
MM
3318 {
3319 /* Convert `float' to `double'. */
3320 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3321 warning_at (ploc, OPT_Wdouble_promotion,
3322 "implicit conversion from %qT to %qT when passing "
3323 "argument to function",
3324 valtype, double_type_node);
0a0b3574
MM
3325 parmval = convert (double_type_node, val);
3326 }
b5d32c25 3327 }
8ce94e44
JM
3328 else if (excess_precision && !type_generic)
3329 /* A "double" argument with excess precision being passed
3330 without a prototype or in variable arguments. */
bbbbb16a 3331 parmval = convert (valtype, val);
c22cacf3
MS
3332 else if ((invalid_func_diag =
3333 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3334 {
3335 error (invalid_func_diag);
94a0dd7b 3336 return -1;
4d3e6fae 3337 }
1807ffc1
MS
3338 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3339 {
3340 return -1;
3341 }
400fbf9f
JW
3342 else
3343 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3344 parmval = default_conversion (val);
3345
9771b263 3346 (*values)[parmnum] = parmval;
06302a02
JJ
3347 if (parmval == error_mark_node)
3348 error_args = true;
400fbf9f
JW
3349
3350 if (typetail)
3351 typetail = TREE_CHAIN (typetail);
3352 }
3353
9771b263 3354 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3355
400fbf9f 3356 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3357 {
68fca595 3358 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3359 inform_declaration (fundecl);
3360 return -1;
3789b316 3361 }
400fbf9f 3362
06302a02 3363 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3364}
3365\f
43f6dfd3
RS
3366/* This is the entry point used by the parser to build unary operators
3367 in the input. CODE, a tree_code, specifies the unary operator, and
3368 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3369 CONVERT_EXPR for code.
3370
3371 LOC is the location to use for the tree generated.
3372*/
43f6dfd3
RS
3373
3374struct c_expr
c2255bc4 3375parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3376{
3377 struct c_expr result;
3378
100d537d 3379 result.original_code = code;
6866c6e8
ILT
3380 result.original_type = NULL;
3381
1807ffc1
MS
3382 if (reject_gcc_builtin (arg.value))
3383 {
3384 result.value = error_mark_node;
3385 }
3386 else
3387 {
3388 result.value = build_unary_op (loc, code, arg.value, 0);
3389
59c0753d 3390 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3391 overflow_warning (loc, result.value);
1807ffc1 3392 }
59c0753d 3393
43f6dfd3
RS
3394 return result;
3395}
3396
3397/* This is the entry point used by the parser to build binary operators
3398 in the input. CODE, a tree_code, specifies the binary operator, and
3399 ARG1 and ARG2 are the operands. In addition to constructing the
3400 expression, we check for operands that were written with other binary
ba47d38d
AH
3401 operators in a way that is likely to confuse the user.
3402
3403 LOCATION is the location of the binary operator. */
edc7c4ec 3404
487a92fe 3405struct c_expr
ba47d38d
AH
3406parser_build_binary_op (location_t location, enum tree_code code,
3407 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3408{
487a92fe 3409 struct c_expr result;
400fbf9f 3410
487a92fe
JM
3411 enum tree_code code1 = arg1.original_code;
3412 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3413 tree type1 = (arg1.original_type
3414 ? arg1.original_type
3415 : TREE_TYPE (arg1.value));
3416 tree type2 = (arg2.original_type
3417 ? arg2.original_type
3418 : TREE_TYPE (arg2.value));
400fbf9f 3419
ba47d38d
AH
3420 result.value = build_binary_op (location, code,
3421 arg1.value, arg2.value, 1);
487a92fe 3422 result.original_code = code;
6866c6e8 3423 result.original_type = NULL;
58bf601b 3424
487a92fe
JM
3425 if (TREE_CODE (result.value) == ERROR_MARK)
3426 return result;
400fbf9f 3427
ba47d38d
AH
3428 if (location != UNKNOWN_LOCATION)
3429 protected_set_expr_location (result.value, location);
3430
400fbf9f 3431 /* Check for cases such as x+y<<z which users are likely
487a92fe 3432 to misinterpret. */
400fbf9f 3433 if (warn_parentheses)
393e8e8b
MP
3434 warn_about_parentheses (location, code, code1, arg1.value, code2,
3435 arg2.value);
001af587 3436
ca409efd 3437 if (warn_logical_op)
393e8e8b 3438 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3439 code1, arg1.value, code2, arg2.value);
63a08740 3440
05b28fd6
MP
3441 if (warn_tautological_compare)
3442 warn_tautological_cmp (location, code, arg1.value, arg2.value);
3443
742938c9 3444 if (warn_logical_not_paren
7ccb1a11 3445 && TREE_CODE_CLASS (code) == tcc_comparison
47c2554f 3446 && code1 == TRUTH_NOT_EXPR
01177669
JJ
3447 && code2 != TRUTH_NOT_EXPR
3448 /* Avoid warning for !!x == y. */
3449 && (TREE_CODE (arg1.value) != NE_EXPR
3450 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3451 {
3452 /* Avoid warning for !b == y where b has _Bool type. */
3453 tree t = integer_zero_node;
3454 if (TREE_CODE (arg1.value) == EQ_EXPR
3455 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3456 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3457 {
3458 t = TREE_OPERAND (arg1.value, 0);
3459 do
3460 {
3461 if (TREE_TYPE (t) != integer_type_node)
3462 break;
3463 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3464 t = C_MAYBE_CONST_EXPR_EXPR (t);
3465 else if (CONVERT_EXPR_P (t))
3466 t = TREE_OPERAND (t, 0);
3467 else
3468 break;
3469 }
3470 while (1);
3471 }
3472 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3473 warn_logical_not_parentheses (location, code, arg2.value);
3474 }
742938c9 3475
e994a705
RS
3476 /* Warn about comparisons against string literals, with the exception
3477 of testing for equality or inequality of a string literal with NULL. */
3478 if (code == EQ_EXPR || code == NE_EXPR)
3479 {
3480 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3481 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3482 warning_at (location, OPT_Waddress,
3483 "comparison with string literal results in unspecified behavior");
e994a705
RS
3484 }
3485 else if (TREE_CODE_CLASS (code) == tcc_comparison
3486 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3487 warning_at (location, OPT_Waddress,
3488 "comparison with string literal results in unspecified behavior");
e994a705 3489
b8698a0f
L
3490 if (TREE_OVERFLOW_P (result.value)
3491 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3492 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3493 overflow_warning (location, result.value);
400fbf9f 3494
6866c6e8
ILT
3495 /* Warn about comparisons of different enum types. */
3496 if (warn_enum_compare
3497 && TREE_CODE_CLASS (code) == tcc_comparison
3498 && TREE_CODE (type1) == ENUMERAL_TYPE
3499 && TREE_CODE (type2) == ENUMERAL_TYPE
3500 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3501 warning_at (location, OPT_Wenum_compare,
3502 "comparison between %qT and %qT",
3503 type1, type2);
3504
400fbf9f
JW
3505 return result;
3506}
3e4093b6 3507\f
3e4093b6
RS
3508/* Return a tree for the difference of pointers OP0 and OP1.
3509 The resulting tree has type int. */
293c9fdd 3510
3e4093b6 3511static tree
db3927fb 3512pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3513{
3e4093b6 3514 tree restype = ptrdiff_type_node;
36c5e70a 3515 tree result, inttype;
400fbf9f 3516
36c5e70a
BE
3517 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3518 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3519 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3520 tree orig_op1 = op1;
400fbf9f 3521
36c5e70a
BE
3522 /* If the operands point into different address spaces, we need to
3523 explicitly convert them to pointers into the common address space
3524 before we can subtract the numerical address values. */
3525 if (as0 != as1)
3526 {
3527 addr_space_t as_common;
3528 tree common_type;
3529
3530 /* Determine the common superset address space. This is guaranteed
3531 to exist because the caller verified that comp_target_types
3532 returned non-zero. */
3533 if (!addr_space_superset (as0, as1, &as_common))
3534 gcc_unreachable ();
3535
3536 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3537 op0 = convert (common_type, op0);
3538 op1 = convert (common_type, op1);
3539 }
3540
3541 /* Determine integer type to perform computations in. This will usually
3542 be the same as the result type (ptrdiff_t), but may need to be a wider
3543 type if pointers for the address space are wider than ptrdiff_t. */
3544 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3545 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3546 else
3547 inttype = restype;
3548
fcf73884 3549 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3550 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3551 "pointer of type %<void *%> used in subtraction");
3552 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3553 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3554 "pointer to a function used in subtraction");
400fbf9f 3555
3e4093b6
RS
3556 /* First do the subtraction as integers;
3557 then drop through to build the divide operator.
3558 Do not do default conversions on the minus operator
3559 in case restype is a short type. */
400fbf9f 3560
db3927fb 3561 op0 = build_binary_op (loc,
36c5e70a
BE
3562 MINUS_EXPR, convert (inttype, op0),
3563 convert (inttype, op1), 0);
3e4093b6
RS
3564 /* This generates an error if op1 is pointer to incomplete type. */
3565 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3566 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3567
3e4093b6 3568 op1 = c_size_in_bytes (target_type);
400fbf9f 3569
f04dda30
MP
3570 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3571 error_at (loc, "arithmetic on pointer to an empty aggregate");
3572
3e4093b6 3573 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3574 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3575 op0, convert (inttype, op1));
3576
3577 /* Convert to final result type if necessary. */
3578 return convert (restype, result);
3e4093b6
RS
3579}
3580\f
267bac10
JM
3581/* Expand atomic compound assignments into an approriate sequence as
3582 specified by the C11 standard section 6.5.16.2.
3583 given
3584 _Atomic T1 E1
3585 T2 E2
3586 E1 op= E2
3587
3588 This sequence is used for all types for which these operations are
3589 supported.
3590
3591 In addition, built-in versions of the 'fe' prefixed routines may
3592 need to be invoked for floating point (real, complex or vector) when
3593 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3594
3595 T1 newval;
3596 T1 old;
3597 T1 *addr
3598 T2 val
3599 fenv_t fenv
3600
3601 addr = &E1;
3602 val = (E2);
3603 __atomic_load (addr, &old, SEQ_CST);
3604 feholdexcept (&fenv);
3605loop:
3606 newval = old op val;
3607 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3608 SEQ_CST))
3609 goto done;
3610 feclearexcept (FE_ALL_EXCEPT);
3611 goto loop:
3612done:
3613 feupdateenv (&fenv);
3614
3615 Also note that the compiler is simply issuing the generic form of
3616 the atomic operations. This requires temp(s) and has their address
3617 taken. The atomic processing is smart enough to figure out when the
3618 size of an object can utilize a lock-free version, and convert the
3619 built-in call to the appropriate lock-free routine. The optimizers
3620 will then dispose of any temps that are no longer required, and
3621 lock-free implementations are utilized as long as there is target
3622 support for the required size.
3623
3624 If the operator is NOP_EXPR, then this is a simple assignment, and
3625 an __atomic_store is issued to perform the assignment rather than
3626 the above loop.
3627
3628*/
3629
3630/* Build an atomic assignment at LOC, expanding into the proper
3631 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3632 the result of the operation, unless RETURN_OLD_P in which case
3633 return the old value of LHS (this is only for postincrement and
3634 postdecrement). */
3635static tree
3636build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3637 tree rhs, bool return_old_p)
3638{
3639 tree fndecl, func_call;
3640 vec<tree, va_gc> *params;
3641 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3642 tree old, old_addr;
3643 tree compound_stmt;
3644 tree stmt, goto_stmt;
3645 tree loop_label, loop_decl, done_label, done_decl;
3646
3647 tree lhs_type = TREE_TYPE (lhs);
3648 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3649 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3650 tree rhs_type = TREE_TYPE (rhs);
3651
3652 gcc_assert (TYPE_ATOMIC (lhs_type));
3653
3654 if (return_old_p)
3655 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3656
3657 /* Allocate enough vector items for a compare_exchange. */
3658 vec_alloc (params, 6);
3659
3660 /* Create a compound statement to hold the sequence of statements
3661 with a loop. */
3662 compound_stmt = c_begin_compound_stmt (false);
3663
3664 /* Fold the RHS if it hasn't already been folded. */
3665 if (modifycode != NOP_EXPR)
3666 rhs = c_fully_fold (rhs, false, NULL);
3667
3668 /* Remove the qualifiers for the rest of the expressions and create
3669 the VAL temp variable to hold the RHS. */
3670 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3671 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5c4abbb8 3672 val = create_tmp_var_raw (nonatomic_rhs_type);
267bac10 3673 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3674 TREE_NO_WARNING (val) = 1;
5c4abbb8
MP
3675 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3676 NULL_TREE);
267bac10
JM
3677 SET_EXPR_LOCATION (rhs, loc);
3678 add_stmt (rhs);
3679
3680 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3681 an atomic_store. */
3682 if (modifycode == NOP_EXPR)
3683 {
3684 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3685 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3686 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3687 params->quick_push (lhs_addr);
3688 params->quick_push (rhs);
3689 params->quick_push (seq_cst);
8edbfaa6 3690 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3691 add_stmt (func_call);
3692
3693 /* Finish the compound statement. */
3694 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3695
3696 /* VAL is the value which was stored, return a COMPOUND_STMT of
3697 the statement and that value. */
3698 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3699 }
3700
3701 /* Create the variables and labels required for the op= form. */
5c4abbb8 3702 old = create_tmp_var_raw (nonatomic_lhs_type);
267bac10 3703 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3704 TREE_ADDRESSABLE (old) = 1;
3705 TREE_NO_WARNING (old) = 1;
267bac10 3706
5c4abbb8 3707 newval = create_tmp_var_raw (nonatomic_lhs_type);
267bac10
JM
3708 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3709 TREE_ADDRESSABLE (newval) = 1;
3710
3711 loop_decl = create_artificial_label (loc);
3712 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3713
3714 done_decl = create_artificial_label (loc);
3715 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3716
3717 /* __atomic_load (addr, &old, SEQ_CST). */
3718 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3719 params->quick_push (lhs_addr);
3720 params->quick_push (old_addr);
3721 params->quick_push (seq_cst);
8edbfaa6 3722 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5c4abbb8
MP
3723 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3724 NULL_TREE);
3725 add_stmt (old);
267bac10
JM
3726 params->truncate (0);
3727
3728 /* Create the expressions for floating-point environment
3729 manipulation, if required. */
3730 bool need_fenv = (flag_trapping_math
3731 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3732 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3733 if (need_fenv)
3734 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3735
3736 if (hold_call)
3737 add_stmt (hold_call);
3738
3739 /* loop: */
3740 add_stmt (loop_label);
3741
3742 /* newval = old + val; */
3743 rhs = build_binary_op (loc, modifycode, old, val, 1);
5c4abbb8 3744 rhs = c_fully_fold (rhs, false, NULL);
68fca595
MP
3745 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3746 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3747 NULL_TREE, 0);
3748 if (rhs != error_mark_node)
3749 {
5c4abbb8
MP
3750 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3751 NULL_TREE);
267bac10
JM
3752 SET_EXPR_LOCATION (rhs, loc);
3753 add_stmt (rhs);
3754 }
3755
3756 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3757 goto done; */
3758 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3759 params->quick_push (lhs_addr);
3760 params->quick_push (old_addr);
3761 params->quick_push (newval_addr);
3762 params->quick_push (integer_zero_node);
3763 params->quick_push (seq_cst);
3764 params->quick_push (seq_cst);
8edbfaa6 3765 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3766
3767 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3768 SET_EXPR_LOCATION (goto_stmt, loc);
3769
3770 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3771 SET_EXPR_LOCATION (stmt, loc);
3772 add_stmt (stmt);
5c4abbb8 3773
267bac10
JM
3774 if (clear_call)
3775 add_stmt (clear_call);
3776
3777 /* goto loop; */
3778 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3779 SET_EXPR_LOCATION (goto_stmt, loc);
3780 add_stmt (goto_stmt);
5c4abbb8 3781
267bac10
JM
3782 /* done: */
3783 add_stmt (done_label);
3784
3785 if (update_call)
3786 add_stmt (update_call);
3787
3788 /* Finish the compound statement. */
3789 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3790
3791 /* NEWVAL is the value that was successfully stored, return a
3792 COMPOUND_EXPR of the statement and the appropriate value. */
3793 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3794 return_old_p ? old : newval);
3795}
3796
3e4093b6
RS
3797/* Construct and perhaps optimize a tree representation
3798 for a unary operation. CODE, a tree_code, specifies the operation
3799 and XARG is the operand.
3800 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3801 the default promotions (such as from short to int).
3802 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3803 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3804 arrays to pointers in C99.
3805
3806 LOCATION is the location of the operator. */
400fbf9f 3807
3e4093b6 3808tree
c9f9eb5d
AH
3809build_unary_op (location_t location,
3810 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3811{
3812 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3813 tree arg = xarg;
3814 tree argtype = 0;
808d6eaa 3815 enum tree_code typecode;
3e4093b6 3816 tree val;
c9f9eb5d 3817 tree ret = error_mark_node;
8ce94e44 3818 tree eptype = NULL_TREE;
3e4093b6 3819 int noconvert = flag;
4de67c26 3820 const char *invalid_op_diag;
928c19bb
JM
3821 bool int_operands;
3822
3823 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3824 if (int_operands)
3825 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3826
808d6eaa
JM
3827 if (code != ADDR_EXPR)
3828 arg = require_complete_type (arg);
3829
3830 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3831 if (typecode == ERROR_MARK)
3832 return error_mark_node;
3833 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3834 typecode = INTEGER_TYPE;
6c36d76b 3835
4de67c26
JM
3836 if ((invalid_op_diag
3837 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3838 {
c9f9eb5d 3839 error_at (location, invalid_op_diag);
4de67c26
JM
3840 return error_mark_node;
3841 }
3842
8ce94e44
JM
3843 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3844 {
3845 eptype = TREE_TYPE (arg);
3846 arg = TREE_OPERAND (arg, 0);
3847 }
3848
3e4093b6
RS
3849 switch (code)
3850 {
3851 case CONVERT_EXPR:
3852 /* This is used for unary plus, because a CONVERT_EXPR
3853 is enough to prevent anybody from looking inside for
3854 associativity, but won't generate any code. */
3855 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3856 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3857 || typecode == VECTOR_TYPE))
400fbf9f 3858 {
c9f9eb5d 3859 error_at (location, "wrong type argument to unary plus");
3e4093b6 3860 return error_mark_node;
400fbf9f 3861 }
3e4093b6
RS
3862 else if (!noconvert)
3863 arg = default_conversion (arg);
db3927fb 3864 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3865 break;
3866
3e4093b6
RS
3867 case NEGATE_EXPR:
3868 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3869 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3870 || typecode == VECTOR_TYPE))
3871 {
c9f9eb5d 3872 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3873 return error_mark_node;
3874 }
3875 else if (!noconvert)
3876 arg = default_conversion (arg);
400fbf9f
JW
3877 break;
3878
3e4093b6 3879 case BIT_NOT_EXPR:
462643f0
AP
3880 /* ~ works on integer types and non float vectors. */
3881 if (typecode == INTEGER_TYPE
3882 || (typecode == VECTOR_TYPE
3883 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3884 {
3e4093b6
RS
3885 if (!noconvert)
3886 arg = default_conversion (arg);
03d5b1f5 3887 }
3e4093b6 3888 else if (typecode == COMPLEX_TYPE)
400fbf9f 3889 {
3e4093b6 3890 code = CONJ_EXPR;
c1771a20 3891 pedwarn (location, OPT_Wpedantic,
fcf73884 3892 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3893 if (!noconvert)
3894 arg = default_conversion (arg);
3895 }
3896 else
3897 {
c9f9eb5d 3898 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3899 return error_mark_node;
400fbf9f
JW
3900 }
3901 break;
3902
3e4093b6 3903 case ABS_EXPR:
11017cc7 3904 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3905 {
c9f9eb5d 3906 error_at (location, "wrong type argument to abs");
3e4093b6 3907 return error_mark_node;
400fbf9f 3908 }
3e4093b6
RS
3909 else if (!noconvert)
3910 arg = default_conversion (arg);
400fbf9f
JW
3911 break;
3912
3e4093b6
RS
3913 case CONJ_EXPR:
3914 /* Conjugating a real value is a no-op, but allow it anyway. */
3915 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3916 || typecode == COMPLEX_TYPE))
400fbf9f 3917 {
c9f9eb5d 3918 error_at (location, "wrong type argument to conjugation");
3e4093b6 3919 return error_mark_node;
400fbf9f 3920 }
3e4093b6
RS
3921 else if (!noconvert)
3922 arg = default_conversion (arg);
400fbf9f
JW
3923 break;
3924
3e4093b6 3925 case TRUTH_NOT_EXPR:
ab22c1fa 3926 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3927 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3928 && typecode != COMPLEX_TYPE)
400fbf9f 3929 {
c9f9eb5d
AH
3930 error_at (location,
3931 "wrong type argument to unary exclamation mark");
3e4093b6 3932 return error_mark_node;
400fbf9f 3933 }
a27d595d
JM
3934 if (int_operands)
3935 {
3936 arg = c_objc_common_truthvalue_conversion (location, xarg);
3937 arg = remove_c_maybe_const_expr (arg);
3938 }
3939 else
3940 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3941 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3942 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3943 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3944 location = EXPR_LOCATION (ret);
c9f9eb5d 3945 goto return_build_unary_op;
3e4093b6 3946
3e4093b6 3947 case REALPART_EXPR:
3e4093b6 3948 case IMAGPART_EXPR:
fb52b50a
NF
3949 ret = build_real_imag_expr (location, code, arg);
3950 if (ret == error_mark_node)
3951 return error_mark_node;
8ce94e44
JM
3952 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3953 eptype = TREE_TYPE (eptype);
c9f9eb5d 3954 goto return_build_unary_op;
3e4093b6
RS
3955
3956 case PREINCREMENT_EXPR:
3957 case POSTINCREMENT_EXPR:
3958 case PREDECREMENT_EXPR:
3959 case POSTDECREMENT_EXPR:
3e4093b6 3960
928c19bb
JM
3961 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3962 {
3963 tree inner = build_unary_op (location, code,
3964 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3965 if (inner == error_mark_node)
3966 return error_mark_node;
3967 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3968 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3969 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3970 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3971 goto return_build_unary_op;
3972 }
3973
925e8657
NP
3974 /* Complain about anything that is not a true lvalue. In
3975 Objective-C, skip this check for property_refs. */
f90e8e2e 3976 if (!objc_is_property_ref (arg)
7bd11157
TT
3977 && !lvalue_or_else (location,
3978 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3979 || code == POSTINCREMENT_EXPR)
3980 ? lv_increment
3981 : lv_decrement)))
928c19bb
JM
3982 return error_mark_node;
3983
09639a83
ILT
3984 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3985 {
3986 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3987 warning_at (location, OPT_Wc___compat,
3988 "increment of enumeration value is invalid in C++");
3989 else
3990 warning_at (location, OPT_Wc___compat,
3991 "decrement of enumeration value is invalid in C++");
3992 }
3993
928c19bb
JM
3994 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3995 arg = c_fully_fold (arg, false, NULL);
3996
267bac10
JM
3997 bool atomic_op;
3998 atomic_op = really_atomic_lvalue (arg);
3999
3e4093b6
RS
4000 /* Increment or decrement the real part of the value,
4001 and don't change the imaginary part. */
4002 if (typecode == COMPLEX_TYPE)
400fbf9f 4003 {
3e4093b6
RS
4004 tree real, imag;
4005
c1771a20 4006 pedwarn (location, OPT_Wpedantic,
509c9d60 4007 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 4008
267bac10
JM
4009 if (!atomic_op)
4010 {
4011 arg = stabilize_reference (arg);
4012 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4013 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4014 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4015 if (real == error_mark_node || imag == error_mark_node)
4016 return error_mark_node;
4017 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4018 real, imag);
4019 goto return_build_unary_op;
4020 }
400fbf9f 4021 }
3e4093b6
RS
4022
4023 /* Report invalid types. */
4024
ab22c1fa 4025 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4026 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4027 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4028 {
3e4093b6 4029 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4030 error_at (location, "wrong type argument to increment");
c22cacf3 4031 else
c9f9eb5d 4032 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4033
4034 return error_mark_node;
400fbf9f 4035 }
400fbf9f 4036
3e4093b6
RS
4037 {
4038 tree inc;
400fbf9f 4039
3e4093b6
RS
4040 argtype = TREE_TYPE (arg);
4041
4042 /* Compute the increment. */
4043
4044 if (typecode == POINTER_TYPE)
4045 {
a0e24419 4046 /* If pointer target is an incomplete type,
3e4093b6 4047 we just cannot know how to do the arithmetic. */
b70cef5d 4048 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4049 {
4050 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4051 error_at (location,
a0e24419
MP
4052 "increment of pointer to an incomplete type %qT",
4053 TREE_TYPE (argtype));
3e4093b6 4054 else
c9f9eb5d 4055 error_at (location,
a0e24419
MP
4056 "decrement of pointer to an incomplete type %qT",
4057 TREE_TYPE (argtype));
3e4093b6 4058 }
b70cef5d
JJ
4059 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4060 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4061 {
3e4093b6 4062 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4063 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4064 "wrong type argument to increment");
3e4093b6 4065 else
44d90fe1 4066 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4067 "wrong type argument to decrement");
3e4093b6
RS
4068 }
4069
b70cef5d 4070 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4071 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4072 }
b70cef5d 4073 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4074 {
4075 /* For signed fract types, we invert ++ to -- or
4076 -- to ++, and change inc from 1 to -1, because
4077 it is not possible to represent 1 in signed fract constants.
4078 For unsigned fract types, the result always overflows and
4079 we get an undefined (original) or the maximum value. */
4080 if (code == PREINCREMENT_EXPR)
4081 code = PREDECREMENT_EXPR;
4082 else if (code == PREDECREMENT_EXPR)
4083 code = PREINCREMENT_EXPR;
4084 else if (code == POSTINCREMENT_EXPR)
4085 code = POSTDECREMENT_EXPR;
4086 else /* code == POSTDECREMENT_EXPR */
4087 code = POSTINCREMENT_EXPR;
4088
4089 inc = integer_minus_one_node;
4090 inc = convert (argtype, inc);
4091 }
3e4093b6 4092 else
5be014d5 4093 {
241b71bb
TV
4094 inc = VECTOR_TYPE_P (argtype)
4095 ? build_one_cst (argtype)
4096 : integer_one_node;
5be014d5
AP
4097 inc = convert (argtype, inc);
4098 }
3e4093b6 4099
925e8657
NP
4100 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4101 need to ask Objective-C to build the increment or decrement
4102 expression for it. */
4103 if (objc_is_property_ref (arg))
f90e8e2e 4104 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4105 arg, inc);
4106
3e4093b6 4107 /* Report a read-only lvalue. */
f37acdf9 4108 if (TYPE_READONLY (argtype))
953ff289 4109 {
c02065fc 4110 readonly_error (location, arg,
953ff289
DN
4111 ((code == PREINCREMENT_EXPR
4112 || code == POSTINCREMENT_EXPR)
4113 ? lv_increment : lv_decrement));
4114 return error_mark_node;
4115 }
f37acdf9
JM
4116 else if (TREE_READONLY (arg))
4117 readonly_warning (arg,
4118 ((code == PREINCREMENT_EXPR
4119 || code == POSTINCREMENT_EXPR)
4120 ? lv_increment : lv_decrement));
3e4093b6 4121
267bac10
JM
4122 /* If the argument is atomic, use the special code sequences for
4123 atomic compound assignment. */
4124 if (atomic_op)
4125 {
4126 arg = stabilize_reference (arg);
4127 ret = build_atomic_assign (location, arg,
4128 ((code == PREINCREMENT_EXPR
4129 || code == POSTINCREMENT_EXPR)
4130 ? PLUS_EXPR
4131 : MINUS_EXPR),
4132 (FRACT_MODE_P (TYPE_MODE (argtype))
4133 ? inc
4134 : integer_one_node),
4135 (code == POSTINCREMENT_EXPR
4136 || code == POSTDECREMENT_EXPR));
4137 goto return_build_unary_op;
4138 }
4139
3e4093b6
RS
4140 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4141 val = boolean_increment (code, arg);
4142 else
53fb4de3 4143 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4144 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4145 if (TREE_CODE (val) != code)
6de9cd9a 4146 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4147 ret = val;
4148 goto return_build_unary_op;
3e4093b6
RS
4149 }
4150
4151 case ADDR_EXPR:
4152 /* Note that this operation never does default_conversion. */
4153
2b4b7036
JM
4154 /* The operand of unary '&' must be an lvalue (which excludes
4155 expressions of type void), or, in C99, the result of a [] or
4156 unary '*' operator. */
4157 if (VOID_TYPE_P (TREE_TYPE (arg))
4158 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
22d03525 4159 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
2b4b7036
JM
4160 pedwarn (location, 0, "taking address of expression of type %<void%>");
4161
3e4093b6 4162 /* Let &* cancel out to simplify resulting code. */
22d03525 4163 if (INDIRECT_REF_P (arg))
400fbf9f 4164 {
3e4093b6
RS
4165 /* Don't let this be an lvalue. */
4166 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4167 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4168 ret = TREE_OPERAND (arg, 0);
4169 goto return_build_unary_op;
400fbf9f 4170 }
1eb8759b 4171
3e4093b6
RS
4172 /* Anything not already handled and not a true memory reference
4173 or a non-lvalue array is an error. */
ee45a32d
EB
4174 if (typecode != FUNCTION_TYPE && !flag
4175 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4176 return error_mark_node;
b6a10c9f 4177
928c19bb
JM
4178 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4179 folding later. */
4180 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4181 {
4182 tree inner = build_unary_op (location, code,
4183 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4184 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4185 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4186 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4187 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4188 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4189 goto return_build_unary_op;
4190 }
4191
3e4093b6
RS
4192 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4193 argtype = TREE_TYPE (arg);
400fbf9f 4194
3e4093b6 4195 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4196 to which the address will point. This is only needed
f2c1da78 4197 for function types. */
6615c446 4198 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4199 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4200 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4201 {
4202 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4203 int quals = orig_quals;
4204
4205 if (TREE_READONLY (arg))
4206 quals |= TYPE_QUAL_CONST;
4207 if (TREE_THIS_VOLATILE (arg))
4208 quals |= TYPE_QUAL_VOLATILE;
4209
f2c1da78
JM
4210 argtype = c_build_qualified_type (argtype, quals);
4211 }
400fbf9f 4212
ee45a32d
EB
4213 switch (TREE_CODE (arg))
4214 {
4215 case COMPONENT_REF:
4216 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4217 {
4218 error ("cannot take address of bit-field %qD",
4219 TREE_OPERAND (arg, 1));
4220 return error_mark_node;
4221 }
4222
4223 /* ... fall through ... */
4224
4225 case ARRAY_REF:
4226 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4227 {
4228 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4229 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4230 {
4231 error ("cannot take address of scalar with reverse storage "
4232 "order");
4233 return error_mark_node;
4234 }
4235
4236 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4237 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4238 warning (OPT_Wscalar_storage_order, "address of array with "
4239 "reverse scalar storage order requested");
4240 }
4241
4242 default:
4243 break;
4244 }
4245
3e4093b6
RS
4246 if (!c_mark_addressable (arg))
4247 return error_mark_node;
400fbf9f 4248
abb54d14
JM
4249 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4250 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4251
5cc200fc 4252 argtype = build_pointer_type (argtype);
5e55f99d
RH
4253
4254 /* ??? Cope with user tricks that amount to offsetof. Delete this
4255 when we have proper support for integer constant expressions. */
4256 val = get_base_address (arg);
22d03525 4257 if (val && INDIRECT_REF_P (val)
3aa2ddb8
JJ
4258 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4259 {
cf9e9959 4260 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4261 goto return_build_unary_op;
3aa2ddb8 4262 }
5e55f99d 4263
5cc200fc 4264 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4265
c9f9eb5d
AH
4266 ret = val;
4267 goto return_build_unary_op;
400fbf9f 4268
3e4093b6 4269 default:
1344f9a3 4270 gcc_unreachable ();
3e4093b6 4271 }
400fbf9f 4272
3e4093b6
RS
4273 if (argtype == 0)
4274 argtype = TREE_TYPE (arg);
928c19bb
JM
4275 if (TREE_CODE (arg) == INTEGER_CST)
4276 ret = (require_constant_value
db3927fb
AH
4277 ? fold_build1_initializer_loc (location, code, argtype, arg)
4278 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4279 else
4280 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4281 return_build_unary_op:
4282 gcc_assert (ret != error_mark_node);
928c19bb
JM
4283 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4284 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4285 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4286 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4287 ret = note_integer_operands (ret);
8ce94e44
JM
4288 if (eptype)
4289 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4290 protected_set_expr_location (ret, location);
4291 return ret;
3e4093b6 4292}
400fbf9f 4293
3e4093b6
RS
4294/* Return nonzero if REF is an lvalue valid for this language.
4295 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4296 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4297
1e4ae551 4298bool
58f9752a 4299lvalue_p (const_tree ref)
3e4093b6 4300{
58f9752a 4301 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4302
3e4093b6
RS
4303 switch (code)
4304 {
4305 case REALPART_EXPR:
4306 case IMAGPART_EXPR:
4307 case COMPONENT_REF:
4308 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4309
928c19bb
JM
4310 case C_MAYBE_CONST_EXPR:
4311 return lvalue_p (TREE_OPERAND (ref, 1));
4312
3e4093b6
RS
4313 case COMPOUND_LITERAL_EXPR:
4314 case STRING_CST:
4315 return 1;
400fbf9f 4316
3e4093b6
RS
4317 case INDIRECT_REF:
4318 case ARRAY_REF:
36536d79 4319 case ARRAY_NOTATION_REF:
3e4093b6
RS
4320 case VAR_DECL:
4321 case PARM_DECL:
4322 case RESULT_DECL:
4323 case ERROR_MARK:
4324 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4325 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4326
3e4093b6 4327 case BIND_EXPR:
3e4093b6 4328 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4329
3e4093b6
RS
4330 default:
4331 return 0;
4332 }
4333}
400fbf9f 4334\f
f37acdf9
JM
4335/* Give a warning for storing in something that is read-only in GCC
4336 terms but not const in ISO C terms. */
4337
4338static void
4339readonly_warning (tree arg, enum lvalue_use use)
4340{
4341 switch (use)
4342 {
4343 case lv_assign:
4344 warning (0, "assignment of read-only location %qE", arg);
4345 break;
4346 case lv_increment:
4347 warning (0, "increment of read-only location %qE", arg);
4348 break;
4349 case lv_decrement:
4350 warning (0, "decrement of read-only location %qE", arg);
4351 break;
4352 default:
4353 gcc_unreachable ();
4354 }
4355 return;
4356}
4357
37dc0d8d
JM
4358
4359/* Return nonzero if REF is an lvalue valid for this language;
4360 otherwise, print an error message and return zero. USE says
7bd11157
TT
4361 how the lvalue is being used and so selects the error message.
4362 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4363
4364static int
7bd11157 4365lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4366{
4367 int win = lvalue_p (ref);
4368
4369 if (!win)
7bd11157 4370 lvalue_error (loc, use);
37dc0d8d
JM
4371
4372 return win;
4373}
3e4093b6
RS
4374\f
4375/* Mark EXP saying that we need to be able to take the
4376 address of it; it should not be allocated in a register.
4377 Returns true if successful. */
54c93c30 4378
3e4093b6
RS
4379bool
4380c_mark_addressable (tree exp)
400fbf9f 4381{
3e4093b6 4382 tree x = exp;
95602da1 4383
3e4093b6
RS
4384 while (1)
4385 switch (TREE_CODE (x))
4386 {
4387 case COMPONENT_REF:
3e4093b6
RS
4388 case ADDR_EXPR:
4389 case ARRAY_REF:
4390 case REALPART_EXPR:
4391 case IMAGPART_EXPR:
4392 x = TREE_OPERAND (x, 0);
4393 break;
95602da1 4394
3e4093b6
RS
4395 case COMPOUND_LITERAL_EXPR:
4396 case CONSTRUCTOR:
4397 TREE_ADDRESSABLE (x) = 1;
4398 return true;
95602da1 4399
3e4093b6
RS
4400 case VAR_DECL:
4401 case CONST_DECL:
4402 case PARM_DECL:
4403 case RESULT_DECL:
5baeaac0 4404 if (C_DECL_REGISTER (x)
3e4093b6
RS
4405 && DECL_NONLOCAL (x))
4406 {
62f9079a 4407 if (TREE_PUBLIC (x) || is_global_var (x))
3e4093b6 4408 {
0039fa55
AN
4409 error
4410 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4411 return false;
4412 }
509c9d60 4413 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4414 }
5baeaac0 4415 else if (C_DECL_REGISTER (x))
3e4093b6 4416 {
62f9079a 4417 if (TREE_PUBLIC (x) || is_global_var (x))
0039fa55
AN
4418 error ("address of global register variable %qD requested", x);
4419 else
4420 error ("address of register variable %qD requested", x);
4421 return false;
3e4093b6 4422 }
400fbf9f 4423
3e4093b6
RS
4424 /* drops in */
4425 case FUNCTION_DECL:
4426 TREE_ADDRESSABLE (x) = 1;
4427 /* drops out */
4428 default:
4429 return true;
4430 }
4431}
4432\f
2d2e923f
MLI
4433/* Convert EXPR to TYPE, warning about conversion problems with
4434 constants. SEMANTIC_TYPE is the type this conversion would use
4435 without excess precision. If SEMANTIC_TYPE is NULL, this function
4436 is equivalent to convert_and_check. This function is a wrapper that
4437 handles conversions that may be different than
4438 the usual ones because of excess precision. */
4439
4440static tree
68fca595
MP
4441ep_convert_and_check (location_t loc, tree type, tree expr,
4442 tree semantic_type)
2d2e923f
MLI
4443{
4444 if (TREE_TYPE (expr) == type)
4445 return expr;
4446
4447 if (!semantic_type)
68fca595 4448 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4449
4450 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4451 && TREE_TYPE (expr) != semantic_type)
4452 {
4453 /* For integers, we need to check the real conversion, not
4454 the conversion to the excess precision type. */
68fca595 4455 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4456 }
4457 /* Result type is the excess precision type, which should be
4458 large enough, so do not check. */
4459 return convert (type, expr);
4460}
4461
928c19bb
JM
4462/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4463 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4464 if folded to an integer constant then the unselected half may
4465 contain arbitrary operations not normally permitted in constant
c2255bc4 4466 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4467
4468tree
744aa42f 4469build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4470 tree op1, tree op1_original_type, tree op2,
4471 tree op2_original_type)
400fbf9f 4472{
3e4093b6
RS
4473 tree type1;
4474 tree type2;
4475 enum tree_code code1;
4476 enum tree_code code2;
4477 tree result_type = NULL;
2d2e923f 4478 tree semantic_result_type = NULL;
3e4093b6 4479 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4480 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4481 bool ifexp_int_operands;
928c19bb 4482 tree ret;
400fbf9f 4483
4d84fe7c
JM
4484 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4485 if (op1_int_operands)
4486 op1 = remove_c_maybe_const_expr (op1);
4487 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4488 if (op2_int_operands)
4489 op2 = remove_c_maybe_const_expr (op2);
4490 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4491 if (ifexp_int_operands)
4492 ifexp = remove_c_maybe_const_expr (ifexp);
4493
3e4093b6
RS
4494 /* Promote both alternatives. */
4495
4496 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4497 op1 = default_conversion (op1);
4498 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4499 op2 = default_conversion (op2);
4500
4501 if (TREE_CODE (ifexp) == ERROR_MARK
4502 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4503 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4504 return error_mark_node;
400fbf9f 4505
3e4093b6
RS
4506 type1 = TREE_TYPE (op1);
4507 code1 = TREE_CODE (type1);
4508 type2 = TREE_TYPE (op2);
4509 code2 = TREE_CODE (type2);
4510
1807ffc1
MS
4511 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4512 return error_mark_node;
4513
4514 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4515 return error_mark_node;
4516
b1adf557
JM
4517 /* C90 does not permit non-lvalue arrays in conditional expressions.
4518 In C99 they will be pointers by now. */
4519 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4520 {
744aa42f 4521 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4522 return error_mark_node;
4523 }
4524
8ce94e44
JM
4525 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4526 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4527 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4528 || code1 == COMPLEX_TYPE)
4529 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4530 || code2 == COMPLEX_TYPE))
4531 {
2d2e923f 4532 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4533 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4534 {
4535 op1 = TREE_OPERAND (op1, 0);
4536 type1 = TREE_TYPE (op1);
4537 gcc_assert (TREE_CODE (type1) == code1);
4538 }
4539 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4540 {
4541 op2 = TREE_OPERAND (op2, 0);
4542 type2 = TREE_TYPE (op2);
4543 gcc_assert (TREE_CODE (type2) == code2);
4544 }
4545 }
4546
d130ae11
ILT
4547 if (warn_cxx_compat)
4548 {
4549 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4550 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4551
4552 if (TREE_CODE (t1) == ENUMERAL_TYPE
4553 && TREE_CODE (t2) == ENUMERAL_TYPE
4554 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4555 warning_at (colon_loc, OPT_Wc___compat,
4556 ("different enum types in conditional is "
4557 "invalid in C++: %qT vs %qT"),
4558 t1, t2);
4559 }
4560
3e4093b6
RS
4561 /* Quickly detect the usual case where op1 and op2 have the same type
4562 after promotion. */
4563 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4564 {
3e4093b6
RS
4565 if (type1 == type2)
4566 result_type = type1;
4567 else
4568 result_type = TYPE_MAIN_VARIANT (type1);
4569 }
4570 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4571 || code1 == COMPLEX_TYPE)
4572 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4573 || code2 == COMPLEX_TYPE))
3e4093b6 4574 {
ccf7f880 4575 result_type = c_common_type (type1, type2);
0a0b3574
MM
4576 do_warn_double_promotion (result_type, type1, type2,
4577 "implicit conversion from %qT to %qT to "
4578 "match other result of conditional",
4579 colon_loc);
400fbf9f 4580
3e4093b6
RS
4581 /* If -Wsign-compare, warn here if type1 and type2 have
4582 different signedness. We'll promote the signed to unsigned
4583 and later code won't know it used to be different.
4584 Do this check on the original types, so that explicit casts
4585 will be considered, but default promotions won't. */
7d882b83 4586 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4587 {
8df83eae
RK
4588 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4589 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4590
3e4093b6
RS
4591 if (unsigned_op1 ^ unsigned_op2)
4592 {
6ac01510
ILT
4593 bool ovf;
4594
3e4093b6
RS
4595 /* Do not warn if the result type is signed, since the
4596 signed type will only be chosen if it can represent
4597 all the values of the unsigned type. */
3f75a254 4598 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4599 /* OK */;
3e4093b6 4600 else
928c19bb
JM
4601 {
4602 bool op1_maybe_const = true;
4603 bool op2_maybe_const = true;
4604
4605 /* Do not warn if the signed quantity is an
4606 unsuffixed integer literal (or some static
4607 constant expression involving such literals) and
4608 it is non-negative. This warning requires the
4609 operands to be folded for best results, so do
4610 that folding in this case even without
4611 warn_sign_compare to avoid warning options
4612 possibly affecting code generation. */
f5178456
RS
4613 c_inhibit_evaluation_warnings
4614 += (ifexp == truthvalue_false_node);
928c19bb
JM
4615 op1 = c_fully_fold (op1, require_constant_value,
4616 &op1_maybe_const);
f5178456
RS
4617 c_inhibit_evaluation_warnings
4618 -= (ifexp == truthvalue_false_node);
4619
4620 c_inhibit_evaluation_warnings
4621 += (ifexp == truthvalue_true_node);
928c19bb
JM
4622 op2 = c_fully_fold (op2, require_constant_value,
4623 &op2_maybe_const);
f5178456
RS
4624 c_inhibit_evaluation_warnings
4625 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4626
4627 if (warn_sign_compare)
4628 {
4629 if ((unsigned_op2
4630 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4631 || (unsigned_op1
4632 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4633 /* OK */;
4634 else
744aa42f
ILT
4635 warning_at (colon_loc, OPT_Wsign_compare,
4636 ("signed and unsigned type in "
4637 "conditional expression"));
928c19bb
JM
4638 }
4639 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4640 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4641 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4642 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4643 }
3e4093b6
RS
4644 }
4645 }
4646 }
4647 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4648 {
fcf73884 4649 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4650 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4651 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4652 result_type = void_type_node;
4653 }
4654 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4655 {
36c5e70a
BE
4656 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4657 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4658 addr_space_t as_common;
4659
744aa42f 4660 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4661 result_type = common_pointer_type (type1, type2);
6aa3c60d 4662 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4663 result_type = type2;
6aa3c60d 4664 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4665 result_type = type1;
4666 else if (!addr_space_superset (as1, as2, &as_common))
4667 {
4668 error_at (colon_loc, "pointers to disjoint address spaces "
4669 "used in conditional expression");
4670 return error_mark_node;
4671 }
267bac10
JM
4672 else if (VOID_TYPE_P (TREE_TYPE (type1))
4673 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4674 {
768952be
MU
4675 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4676 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4677 & ~TYPE_QUALS (TREE_TYPE (type1))))
4678 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4679 "pointer to array loses qualifier "
4680 "in conditional expression");
4681
fcf73884 4682 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4683 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4684 "ISO C forbids conditional expr between "
bda67431 4685 "%<void *%> and function pointer");
3e4093b6
RS
4686 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4687 TREE_TYPE (type2)));
34a80643 4688 }
267bac10
JM
4689 else if (VOID_TYPE_P (TREE_TYPE (type2))
4690 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4691 {
768952be
MU
4692 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4693 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4694 & ~TYPE_QUALS (TREE_TYPE (type2))))
4695 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4696 "pointer to array loses qualifier "
4697 "in conditional expression");
4698
fcf73884 4699 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4700 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4701 "ISO C forbids conditional expr between "
bda67431 4702 "%<void *%> and function pointer");
3e4093b6
RS
4703 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4704 TREE_TYPE (type1)));
1c2a9b35 4705 }
b581b85b
NP
4706 /* Objective-C pointer comparisons are a bit more lenient. */
4707 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4708 result_type = objc_common_type (type1, type2);
34a80643 4709 else
ab87f8c8 4710 {
36c5e70a
BE
4711 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4712
b581b85b
NP
4713 pedwarn (colon_loc, 0,
4714 "pointer type mismatch in conditional expression");
36c5e70a
BE
4715 result_type = build_pointer_type
4716 (build_qualified_type (void_type_node, qual));
ab87f8c8 4717 }
3e4093b6
RS
4718 }
4719 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4720 {
6aa3c60d 4721 if (!null_pointer_constant_p (orig_op2))
744aa42f 4722 pedwarn (colon_loc, 0,
509c9d60 4723 "pointer/integer type mismatch in conditional expression");
3e4093b6 4724 else
ab87f8c8 4725 {
3e4093b6 4726 op2 = null_pointer_node;
ab87f8c8 4727 }
3e4093b6
RS
4728 result_type = type1;
4729 }
4730 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4731 {
6aa3c60d 4732 if (!null_pointer_constant_p (orig_op1))
744aa42f 4733 pedwarn (colon_loc, 0,
509c9d60 4734 "pointer/integer type mismatch in conditional expression");
3e4093b6 4735 else
ab87f8c8 4736 {
3e4093b6 4737 op1 = null_pointer_node;
ab87f8c8 4738 }
3e4093b6
RS
4739 result_type = type2;
4740 }
1c2a9b35 4741
3e4093b6
RS
4742 if (!result_type)
4743 {
4744 if (flag_cond_mismatch)
4745 result_type = void_type_node;
4746 else
400fbf9f 4747 {
c2255bc4 4748 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4749 return error_mark_node;
400fbf9f 4750 }
3e4093b6 4751 }
400fbf9f 4752
3e4093b6
RS
4753 /* Merge const and volatile flags of the incoming types. */
4754 result_type
4755 = build_type_variant (result_type,
afbd0665
AS
4756 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4757 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4758
68fca595
MP
4759 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4760 semantic_result_type);
4761 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4762 semantic_result_type);
b6a10c9f 4763
928c19bb
JM
4764 if (ifexp_bcp && ifexp == truthvalue_true_node)
4765 {
4766 op2_int_operands = true;
4767 op1 = c_fully_fold (op1, require_constant_value, NULL);
4768 }
4769 if (ifexp_bcp && ifexp == truthvalue_false_node)
4770 {
4771 op1_int_operands = true;
4772 op2 = c_fully_fold (op2, require_constant_value, NULL);
4773 }
4d84fe7c 4774 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4775 && op1_int_operands
4776 && op2_int_operands);
4777 if (int_operands)
4778 {
4779 int_const = ((ifexp == truthvalue_true_node
4780 && TREE_CODE (orig_op1) == INTEGER_CST
4781 && !TREE_OVERFLOW (orig_op1))
4782 || (ifexp == truthvalue_false_node
4783 && TREE_CODE (orig_op2) == INTEGER_CST
4784 && !TREE_OVERFLOW (orig_op2)));
4785 }
9f47c7e5
IE
4786
4787 /* Need to convert condition operand into a vector mask. */
4788 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
4789 {
4790 tree vectype = TREE_TYPE (ifexp);
4791 tree elem_type = TREE_TYPE (vectype);
4792 tree zero = build_int_cst (elem_type, 0);
4793 tree zero_vec = build_vector_from_val (vectype, zero);
4794 tree cmp_type = build_same_sized_truth_vector_type (vectype);
4795 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
4796 }
4797
928c19bb 4798 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4799 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4800 else
4801 {
01c7ccbb
JM
4802 if (int_operands)
4803 {
f34f1c87
MP
4804 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4805 nested inside of the expression. */
4806 op1 = c_fully_fold (op1, false, NULL);
4807 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4808 }
928c19bb
JM
4809 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4810 if (int_operands)
4811 ret = note_integer_operands (ret);
4812 }
2d2e923f
MLI
4813 if (semantic_result_type)
4814 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4815
c2255bc4 4816 protected_set_expr_location (ret, colon_loc);
928c19bb 4817 return ret;
3e4093b6
RS
4818}
4819\f
487a92fe 4820/* Return a compound expression that performs two expressions and
c2255bc4
AH
4821 returns the value of the second of them.
4822
4823 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4824
3e4093b6 4825tree
c2255bc4 4826build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4827{
4d84fe7c 4828 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4829 tree eptype = NULL_TREE;
928c19bb
JM
4830 tree ret;
4831
b72271b9 4832 if (flag_cilkplus
939b37da
BI
4833 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4834 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4835 {
4836 error_at (loc,
4837 "spawned function call cannot be part of a comma expression");
4838 return error_mark_node;
4839 }
4d84fe7c
JM
4840 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4841 if (expr1_int_operands)
4842 expr1 = remove_c_maybe_const_expr (expr1);
4843 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4844 if (expr2_int_operands)
4845 expr2 = remove_c_maybe_const_expr (expr2);
4846
8ce94e44
JM
4847 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4848 expr1 = TREE_OPERAND (expr1, 0);
4849 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4850 {
4851 eptype = TREE_TYPE (expr2);
4852 expr2 = TREE_OPERAND (expr2, 0);
4853 }
4854
3f75a254 4855 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4856 {
4857 /* The left-hand operand of a comma expression is like an expression
c5409249 4858 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4859 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4860 if (warn_unused_value)
47aecf47 4861 {
e14a6540 4862 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4863 && CONVERT_EXPR_P (expr1))
47aecf47 4864 ; /* (void) a, b */
e14a6540
JM
4865 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4866 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4867 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4868 ; /* (void) a, (void) b, c */
4869 else
b8698a0f 4870 warning_at (loc, OPT_Wunused_value,
c2255bc4 4871 "left-hand operand of comma expression has no effect");
47aecf47 4872 }
3e4093b6 4873 }
789eadcd
MP
4874 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4875 && warn_unused_value)
4876 {
4877 tree r = expr1;
4878 location_t cloc = loc;
4879 while (TREE_CODE (r) == COMPOUND_EXPR)
4880 {
4881 if (EXPR_HAS_LOCATION (r))
4882 cloc = EXPR_LOCATION (r);
4883 r = TREE_OPERAND (r, 1);
4884 }
4885 if (!TREE_SIDE_EFFECTS (r)
4886 && !VOID_TYPE_P (TREE_TYPE (r))
4887 && !CONVERT_EXPR_P (r))
4888 warning_at (cloc, OPT_Wunused_value,
4889 "right-hand operand of comma expression has no effect");
4890 }
400fbf9f 4891
3e4093b6
RS
4892 /* With -Wunused, we should also warn if the left-hand operand does have
4893 side-effects, but computes a value which is not used. For example, in
4894 `foo() + bar(), baz()' the result of the `+' operator is not used,
4895 so we should issue a warning. */
4896 else if (warn_unused_value)
c2255bc4 4897 warn_if_unused_value (expr1, loc);
400fbf9f 4898
e63d6886
AP
4899 if (expr2 == error_mark_node)
4900 return error_mark_node;
4901
928c19bb
JM
4902 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4903
4904 if (flag_isoc99
4d84fe7c
JM
4905 && expr1_int_operands
4906 && expr2_int_operands)
928c19bb
JM
4907 ret = note_integer_operands (ret);
4908
8ce94e44
JM
4909 if (eptype)
4910 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4911
c2255bc4 4912 protected_set_expr_location (ret, loc);
928c19bb 4913 return ret;
3e4093b6 4914}
400fbf9f 4915
67165eb3
ILT
4916/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4917 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4918 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4919 of the cast. -Wcast-qual appeared on the command line. Named
4920 address space qualifiers are not handled here, because they result
4921 in different warnings. */
67165eb3
ILT
4922
4923static void
2ee3cb35 4924handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4925{
4926 tree in_type = type;
4927 tree in_otype = otype;
4928 int added = 0;
4929 int discarded = 0;
4930 bool is_const;
4931
4932 /* Check that the qualifiers on IN_TYPE are a superset of the
4933 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4934 nodes is uninteresting and we stop as soon as we hit a
4935 non-POINTER_TYPE node on either type. */
4936 do
4937 {
4938 in_otype = TREE_TYPE (in_otype);
4939 in_type = TREE_TYPE (in_type);
4940
4941 /* GNU C allows cv-qualified function types. 'const' means the
4942 function is very pure, 'volatile' means it can't return. We
4943 need to warn when such qualifiers are added, not when they're
4944 taken away. */
4945 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4946 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4947 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4948 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4949 else
36c5e70a
BE
4950 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4951 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4952 }
4953 while (TREE_CODE (in_type) == POINTER_TYPE
4954 && TREE_CODE (in_otype) == POINTER_TYPE);
4955
4956 if (added)
2ee3cb35
MLI
4957 warning_at (loc, OPT_Wcast_qual,
4958 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4959
4960 if (discarded)
4961 /* There are qualifiers present in IN_OTYPE that are not present
4962 in IN_TYPE. */
2ee3cb35 4963 warning_at (loc, OPT_Wcast_qual,
7485aeea 4964 "cast discards %qv qualifier from pointer target type",
2ee3cb35 4965 discarded);
67165eb3
ILT
4966
4967 if (added || discarded)
4968 return;
4969
4970 /* A cast from **T to const **T is unsafe, because it can cause a
4971 const value to be changed with no additional warning. We only
4972 issue this warning if T is the same on both sides, and we only
4973 issue the warning if there are the same number of pointers on
4974 both sides, as otherwise the cast is clearly unsafe anyhow. A
4975 cast is unsafe when a qualifier is added at one level and const
4976 is not present at all outer levels.
4977
4978 To issue this warning, we check at each level whether the cast
4979 adds new qualifiers not already seen. We don't need to special
4980 case function types, as they won't have the same
4981 TYPE_MAIN_VARIANT. */
4982
4983 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4984 return;
4985 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4986 return;
4987
4988 in_type = type;
4989 in_otype = otype;
4990 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4991 do
4992 {
4993 in_type = TREE_TYPE (in_type);
4994 in_otype = TREE_TYPE (in_otype);
4995 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4996 && !is_const)
4997 {
2ee3cb35
MLI
4998 warning_at (loc, OPT_Wcast_qual,
4999 "to be safe all intermediate pointers in cast from "
5000 "%qT to %qT must be %<const%> qualified",
5001 otype, type);
67165eb3
ILT
5002 break;
5003 }
5004 if (is_const)
5005 is_const = TYPE_READONLY (in_type);
5006 }
5007 while (TREE_CODE (in_type) == POINTER_TYPE);
5008}
5009
b8698a0f 5010/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 5011 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 5012
3e4093b6 5013tree
c2255bc4 5014build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 5015{
8ce94e44
JM
5016 tree value;
5017
5018 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5019 expr = TREE_OPERAND (expr, 0);
5020
5021 value = expr;
400fbf9f 5022
3e4093b6
RS
5023 if (type == error_mark_node || expr == error_mark_node)
5024 return error_mark_node;
400fbf9f 5025
3e4093b6
RS
5026 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5027 only in <protocol> qualifications. But when constructing cast expressions,
5028 the protocols do matter and must be kept around. */
700686fa
ZL
5029 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5030 return build1 (NOP_EXPR, type, expr);
5031
5032 type = TYPE_MAIN_VARIANT (type);
400fbf9f 5033
3e4093b6
RS
5034 if (TREE_CODE (type) == ARRAY_TYPE)
5035 {
c2255bc4 5036 error_at (loc, "cast specifies array type");
3e4093b6
RS
5037 return error_mark_node;
5038 }
400fbf9f 5039
3e4093b6
RS
5040 if (TREE_CODE (type) == FUNCTION_TYPE)
5041 {
c2255bc4 5042 error_at (loc, "cast specifies function type");
3e4093b6
RS
5043 return error_mark_node;
5044 }
400fbf9f 5045
808d6eaa
JM
5046 if (!VOID_TYPE_P (type))
5047 {
5048 value = require_complete_type (value);
5049 if (value == error_mark_node)
5050 return error_mark_node;
5051 }
5052
3e4093b6
RS
5053 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5054 {
fcf73884
MLI
5055 if (TREE_CODE (type) == RECORD_TYPE
5056 || TREE_CODE (type) == UNION_TYPE)
c1771a20 5057 pedwarn (loc, OPT_Wpedantic,
fcf73884 5058 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5059
5060 /* Convert to remove any qualifiers from VALUE's type. */
5061 value = convert (type, value);
3e4093b6
RS
5062 }
5063 else if (TREE_CODE (type) == UNION_TYPE)
5064 {
5065 tree field;
400fbf9f 5066
910ad8de 5067 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5068 if (TREE_TYPE (field) != error_mark_node
5069 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5070 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5071 break;
5072
5073 if (field)
400fbf9f 5074 {
3e4093b6 5075 tree t;
e616f54d 5076 bool maybe_const = true;
3e4093b6 5077
c1771a20 5078 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5079 t = c_fully_fold (value, false, &maybe_const);
5080 t = build_constructor_single (type, field, t);
5081 if (!maybe_const)
5082 t = c_wrap_maybe_const (t, true);
5083 t = digest_init (loc, type, t,
bbbbb16a 5084 NULL_TREE, false, true, 0);
3e4093b6
RS
5085 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5086 return t;
400fbf9f 5087 }
c2255bc4 5088 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5089 return error_mark_node;
5090 }
5091 else
5092 {
5093 tree otype, ovalue;
400fbf9f 5094
3e4093b6 5095 if (type == void_type_node)
c2255bc4
AH
5096 {
5097 tree t = build1 (CONVERT_EXPR, type, value);
5098 SET_EXPR_LOCATION (t, loc);
5099 return t;
5100 }
400fbf9f 5101
3e4093b6 5102 otype = TREE_TYPE (value);
400fbf9f 5103
3e4093b6 5104 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5105 if (warn_cast_qual
5106 && TREE_CODE (type) == POINTER_TYPE
5107 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5108 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5109
36c5e70a
BE
5110 /* Warn about conversions between pointers to disjoint
5111 address spaces. */
5112 if (TREE_CODE (type) == POINTER_TYPE
5113 && TREE_CODE (otype) == POINTER_TYPE
5114 && !null_pointer_constant_p (value))
5115 {
5116 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5117 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5118 addr_space_t as_common;
5119
5120 if (!addr_space_superset (as_to, as_from, &as_common))
5121 {
5122 if (ADDR_SPACE_GENERIC_P (as_from))
5123 warning_at (loc, 0, "cast to %s address space pointer "
5124 "from disjoint generic address space pointer",
5125 c_addr_space_name (as_to));
5126
5127 else if (ADDR_SPACE_GENERIC_P (as_to))
5128 warning_at (loc, 0, "cast to generic address space pointer "
5129 "from disjoint %s address space pointer",
5130 c_addr_space_name (as_from));
5131
5132 else
5133 warning_at (loc, 0, "cast to %s address space pointer "
5134 "from disjoint %s address space pointer",
5135 c_addr_space_name (as_to),
5136 c_addr_space_name (as_from));
5137 }
5138 }
5139
3e4093b6 5140 /* Warn about possible alignment problems. */
3176a0c2 5141 if (STRICT_ALIGNMENT
3e4093b6
RS
5142 && TREE_CODE (type) == POINTER_TYPE
5143 && TREE_CODE (otype) == POINTER_TYPE
5144 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5145 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5146 /* Don't warn about opaque types, where the actual alignment
5147 restriction is unknown. */
5148 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5149 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5150 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5151 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5152 warning_at (loc, OPT_Wcast_align,
5153 "cast increases required alignment of target type");
e9a25f70 5154
3176a0c2 5155 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5156 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5157 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5158 /* Unlike conversion of integers to pointers, where the
5159 warning is disabled for converting constants because
5160 of cases such as SIG_*, warn about converting constant
5161 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5162 sign extension, and a warning is appropriate. */
c2255bc4
AH
5163 warning_at (loc, OPT_Wpointer_to_int_cast,
5164 "cast from pointer to integer of different size");
400fbf9f 5165
3176a0c2 5166 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5167 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5168 warning_at (loc, OPT_Wbad_function_cast,
5169 "cast from function call of type %qT "
5170 "to non-matching type %qT", otype, type);
400fbf9f 5171
3176a0c2 5172 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5173 && TREE_CODE (otype) == INTEGER_TYPE
5174 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5175 /* Don't warn about converting any constant. */
5176 && !TREE_CONSTANT (value))
c2255bc4
AH
5177 warning_at (loc,
5178 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5179 "of different size");
400fbf9f 5180
79bedddc
SR
5181 if (warn_strict_aliasing <= 2)
5182 strict_aliasing_warning (otype, type, expr);
400fbf9f 5183
3897f229
JM
5184 /* If pedantic, warn for conversions between function and object
5185 pointer types, except for converting a null pointer constant
5186 to function pointer type. */
5187 if (pedantic
5188 && TREE_CODE (type) == POINTER_TYPE
5189 && TREE_CODE (otype) == POINTER_TYPE
5190 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5191 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5192 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5193 "conversion of function pointer to object pointer type");
3897f229
JM
5194
5195 if (pedantic
5196 && TREE_CODE (type) == POINTER_TYPE
5197 && TREE_CODE (otype) == POINTER_TYPE
5198 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5199 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5200 && !null_pointer_constant_p (value))
c1771a20 5201 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5202 "conversion of object pointer to function pointer type");
3897f229 5203
3e4093b6 5204 ovalue = value;
3e4093b6 5205 value = convert (type, value);
400fbf9f 5206
3e4093b6 5207 /* Ignore any integer overflow caused by the cast. */
928c19bb 5208 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5209 {
8bcd6380 5210 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5211 {
8bcd6380
RS
5212 if (!TREE_OVERFLOW (value))
5213 {
5214 /* Avoid clobbering a shared constant. */
5215 value = copy_node (value);
5216 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5217 }
6414bad6 5218 }
8bcd6380 5219 else if (TREE_OVERFLOW (value))
d8e1f97b 5220 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5221 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5222 }
5223 }
400fbf9f 5224
53cd18ec 5225 /* Don't let a cast be an lvalue. */
9482b620 5226 if (lvalue_p (value))
db3927fb 5227 value = non_lvalue_loc (loc, value);
e9a25f70 5228
928c19bb
JM
5229 /* Don't allow the results of casting to floating-point or complex
5230 types be confused with actual constants, or casts involving
5231 integer and pointer types other than direct integer-to-integer
5232 and integer-to-pointer be confused with integer constant
5233 expressions and null pointer constants. */
5234 if (TREE_CODE (value) == REAL_CST
5235 || TREE_CODE (value) == COMPLEX_CST
5236 || (TREE_CODE (value) == INTEGER_CST
5237 && !((TREE_CODE (expr) == INTEGER_CST
5238 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5239 || TREE_CODE (expr) == REAL_CST
5240 || TREE_CODE (expr) == COMPLEX_CST)))
5241 value = build1 (NOP_EXPR, type, value);
5242
21ba0cea 5243 protected_set_expr_location (value, loc);
3e4093b6 5244 return value;
400fbf9f
JW
5245}
5246
c2255bc4
AH
5247/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5248 location of the open paren of the cast, or the position of the cast
5249 expr. */
3e4093b6 5250tree
c2255bc4 5251c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5252{
f8893e47 5253 tree type;
928c19bb
JM
5254 tree type_expr = NULL_TREE;
5255 bool type_expr_const = true;
5256 tree ret;
3e4093b6 5257 int saved_wsp = warn_strict_prototypes;
c5c76735 5258
3e4093b6
RS
5259 /* This avoids warnings about unprototyped casts on
5260 integers. E.g. "#define SIG_DFL (void(*)())0". */
5261 if (TREE_CODE (expr) == INTEGER_CST)
5262 warn_strict_prototypes = 0;
928c19bb 5263 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5264 warn_strict_prototypes = saved_wsp;
c5c76735 5265
1807ffc1
MS
5266 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5267 && reject_gcc_builtin (expr))
5268 return error_mark_node;
5269
c2255bc4 5270 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5271 if (type_expr)
5272 {
9f33203d
JM
5273 bool inner_expr_const = true;
5274 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5275 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5276 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5277 && inner_expr_const);
c2255bc4 5278 SET_EXPR_LOCATION (ret, loc);
928c19bb 5279 }
24b97832 5280
21ba0cea
MP
5281 if (!EXPR_HAS_LOCATION (ret))
5282 protected_set_expr_location (ret, loc);
24b97832 5283
9e5b2115
PB
5284 /* C++ does not permits types to be defined in a cast, but it
5285 allows references to incomplete types. */
5286 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5287 warning_at (loc, OPT_Wc___compat,
5288 "defining a type in a cast is invalid in C++");
5289
928c19bb 5290 return ret;
400fbf9f 5291}
3e4093b6
RS
5292\f
5293/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5294 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5295 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5296 MODIFYCODE is the code for a binary operator that we use
5297 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5298 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5299 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5300 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5301
c2255bc4
AH
5302 LOCATION is the location of the MODIFYCODE operator.
5303 RHS_LOC is the location of the RHS. */
2f6e4e97 5304
3e4093b6 5305tree
32e8bb8e 5306build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5307 enum tree_code modifycode,
c2255bc4 5308 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5309{
3e4093b6
RS
5310 tree result;
5311 tree newrhs;
241f845a 5312 tree rhseval = NULL_TREE;
8ce94e44 5313 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5314 tree lhstype = TREE_TYPE (lhs);
5315 tree olhstype = lhstype;
928c19bb 5316 bool npc;
267bac10 5317 bool is_atomic_op;
e9a25f70 5318
3e4093b6
RS
5319 /* Types that aren't fully specified cannot be used in assignments. */
5320 lhs = require_complete_type (lhs);
e9a25f70 5321
3e4093b6
RS
5322 /* Avoid duplicate error messages from operands that had errors. */
5323 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5324 return error_mark_node;
400fbf9f 5325
4c2ecab0
JM
5326 /* Ensure an error for assigning a non-lvalue array to an array in
5327 C90. */
5328 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5329 {
5330 error_at (location, "assignment to expression with array type");
5331 return error_mark_node;
5332 }
5333
46a88c12 5334 /* For ObjC properties, defer this check. */
7bd11157 5335 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5336 return error_mark_node;
5337
267bac10
JM
5338 is_atomic_op = really_atomic_lvalue (lhs);
5339
8ce94e44
JM
5340 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5341 {
5342 rhs_semantic_type = TREE_TYPE (rhs);
5343 rhs = TREE_OPERAND (rhs, 0);
5344 }
5345
3e4093b6 5346 newrhs = rhs;
400fbf9f 5347
928c19bb
JM
5348 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5349 {
5350 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5351 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5352 rhs_origtype);
928c19bb
JM
5353 if (inner == error_mark_node)
5354 return error_mark_node;
5355 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5356 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5357 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5358 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5359 protected_set_expr_location (result, location);
5360 return result;
5361 }
5362
3e4093b6
RS
5363 /* If a binary op has been requested, combine the old LHS value with the RHS
5364 producing the value we should actually store into the LHS. */
5365
5366 if (modifycode != NOP_EXPR)
400fbf9f 5367 {
928c19bb 5368 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5369 lhs = stabilize_reference (lhs);
bbbbb16a 5370
267bac10
JM
5371 /* Construct the RHS for any non-atomic compound assignemnt. */
5372 if (!is_atomic_op)
5373 {
241f845a
JJ
5374 /* If in LHS op= RHS the RHS has side-effects, ensure they
5375 are preevaluated before the rest of the assignment expression's
5376 side-effects, because RHS could contain e.g. function calls
5377 that modify LHS. */
5378 if (TREE_SIDE_EFFECTS (rhs))
5379 {
5380 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5381 rhseval = newrhs;
5382 }
267bac10 5383 newrhs = build_binary_op (location,
241f845a 5384 modifycode, lhs, newrhs, 1);
267bac10
JM
5385
5386 /* The original type of the right hand side is no longer
5387 meaningful. */
5388 rhs_origtype = NULL_TREE;
5389 }
400fbf9f 5390 }
400fbf9f 5391
668ea4b1
IS
5392 if (c_dialect_objc ())
5393 {
46a88c12
NP
5394 /* Check if we are modifying an Objective-C property reference;
5395 if so, we need to generate setter calls. */
5396 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5397 if (result)
241f845a 5398 goto return_result;
46a88c12
NP
5399
5400 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5401 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5402 return error_mark_node;
5403 }
5404
9bf24266 5405 /* Give an error for storing in something that is 'const'. */
bbbd6700 5406
f37acdf9 5407 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5408 || ((TREE_CODE (lhstype) == RECORD_TYPE
5409 || TREE_CODE (lhstype) == UNION_TYPE)
5410 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5411 {
c02065fc 5412 readonly_error (location, lhs, lv_assign);
953ff289
DN
5413 return error_mark_node;
5414 }
f37acdf9
JM
5415 else if (TREE_READONLY (lhs))
5416 readonly_warning (lhs, lv_assign);
bbbd6700 5417
3e4093b6
RS
5418 /* If storing into a structure or union member,
5419 it has probably been given type `int'.
5420 Compute the type that would go with
5421 the actual amount of storage the member occupies. */
bbbd6700 5422
3e4093b6
RS
5423 if (TREE_CODE (lhs) == COMPONENT_REF
5424 && (TREE_CODE (lhstype) == INTEGER_TYPE
5425 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5426 || TREE_CODE (lhstype) == REAL_TYPE
5427 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5428 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5429
3e4093b6
RS
5430 /* If storing in a field that is in actuality a short or narrower than one,
5431 we must store in the field in its actual type. */
5432
5433 if (lhstype != TREE_TYPE (lhs))
5434 {
5435 lhs = copy_node (lhs);
5436 TREE_TYPE (lhs) = lhstype;
400fbf9f 5437 }
400fbf9f 5438
32e8bb8e
ILT
5439 /* Issue -Wc++-compat warnings about an assignment to an enum type
5440 when LHS does not have its original type. This happens for,
5441 e.g., an enum bitfield in a struct. */
5442 if (warn_cxx_compat
5443 && lhs_origtype != NULL_TREE
5444 && lhs_origtype != lhstype
5445 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5446 {
5447 tree checktype = (rhs_origtype != NULL_TREE
5448 ? rhs_origtype
5449 : TREE_TYPE (rhs));
5450 if (checktype != error_mark_node
267bac10
JM
5451 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5452 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5453 warning_at (location, OPT_Wc___compat,
5454 "enum conversion in assignment is invalid in C++");
5455 }
5456
267bac10
JM
5457 /* If the lhs is atomic, remove that qualifier. */
5458 if (is_atomic_op)
5459 {
5460 lhstype = build_qualified_type (lhstype,
5461 (TYPE_QUALS (lhstype)
5462 & ~TYPE_QUAL_ATOMIC));
5463 olhstype = build_qualified_type (olhstype,
5464 (TYPE_QUALS (lhstype)
5465 & ~TYPE_QUAL_ATOMIC));
5466 }
5467
8ce94e44
JM
5468 /* Convert new value to destination type. Fold it first, then
5469 restore any excess precision information, for the sake of
5470 conversion warnings. */
400fbf9f 5471
267bac10
JM
5472 if (!(is_atomic_op && modifycode != NOP_EXPR))
5473 {
5474 npc = null_pointer_constant_p (newrhs);
5475 newrhs = c_fully_fold (newrhs, false, NULL);
5476 if (rhs_semantic_type)
5477 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5478 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5479 rhs_origtype, ic_assign, npc,
5480 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5481 if (TREE_CODE (newrhs) == ERROR_MARK)
5482 return error_mark_node;
5483 }
400fbf9f 5484
6e955430
ZL
5485 /* Emit ObjC write barrier, if necessary. */
5486 if (c_dialect_objc () && flag_objc_gc)
5487 {
5488 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5489 if (result)
c9f9eb5d
AH
5490 {
5491 protected_set_expr_location (result, location);
241f845a 5492 goto return_result;
c9f9eb5d 5493 }
6e955430
ZL
5494 }
5495
ea4b7848 5496 /* Scan operands. */
400fbf9f 5497
267bac10
JM
5498 if (is_atomic_op)
5499 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5500 else
5501 {
5502 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5503 TREE_SIDE_EFFECTS (result) = 1;
5504 protected_set_expr_location (result, location);
5505 }
400fbf9f 5506
3e4093b6
RS
5507 /* If we got the LHS in a different type for storing in,
5508 convert the result back to the nominal type of LHS
5509 so that the value we return always has the same type
5510 as the LHS argument. */
e855c5ce 5511
3e4093b6 5512 if (olhstype == TREE_TYPE (result))
241f845a 5513 goto return_result;
c9f9eb5d 5514
68fca595
MP
5515 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5516 rhs_origtype, ic_assign, false, NULL_TREE,
5517 NULL_TREE, 0);
c9f9eb5d 5518 protected_set_expr_location (result, location);
241f845a
JJ
5519
5520return_result:
5521 if (rhseval)
5522 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5523 return result;
3e4093b6
RS
5524}
5525\f
478a1c5b
ILT
5526/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5527 This is used to implement -fplan9-extensions. */
5528
5529static bool
5530find_anonymous_field_with_type (tree struct_type, tree type)
5531{
5532 tree field;
5533 bool found;
5534
5535 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5536 || TREE_CODE (struct_type) == UNION_TYPE);
5537 found = false;
5538 for (field = TYPE_FIELDS (struct_type);
5539 field != NULL_TREE;
5540 field = TREE_CHAIN (field))
5541 {
267bac10
JM
5542 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5543 ? c_build_qualified_type (TREE_TYPE (field),
5544 TYPE_QUAL_ATOMIC)
5545 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5546 if (DECL_NAME (field) == NULL
267bac10 5547 && comptypes (type, fieldtype))
478a1c5b
ILT
5548 {
5549 if (found)
5550 return false;
5551 found = true;
5552 }
5553 else if (DECL_NAME (field) == NULL
5554 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5555 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5556 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5557 {
5558 if (found)
5559 return false;
5560 found = true;
5561 }
5562 }
5563 return found;
5564}
5565
5566/* RHS is an expression whose type is pointer to struct. If there is
5567 an anonymous field in RHS with type TYPE, then return a pointer to
5568 that field in RHS. This is used with -fplan9-extensions. This
5569 returns NULL if no conversion could be found. */
5570
5571static tree
5572convert_to_anonymous_field (location_t location, tree type, tree rhs)
5573{
5574 tree rhs_struct_type, lhs_main_type;
5575 tree field, found_field;
5576 bool found_sub_field;
5577 tree ret;
5578
5579 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5580 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5581 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5582 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5583
5584 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5585 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5586 ? c_build_qualified_type (TREE_TYPE (type),
5587 TYPE_QUAL_ATOMIC)
5588 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5589
5590 found_field = NULL_TREE;
5591 found_sub_field = false;
5592 for (field = TYPE_FIELDS (rhs_struct_type);
5593 field != NULL_TREE;
5594 field = TREE_CHAIN (field))
5595 {
5596 if (DECL_NAME (field) != NULL_TREE
5597 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5598 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5599 continue;
267bac10
JM
5600 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5601 ? c_build_qualified_type (TREE_TYPE (field),
5602 TYPE_QUAL_ATOMIC)
5603 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5604 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5605 {
5606 if (found_field != NULL_TREE)
5607 return NULL_TREE;
5608 found_field = field;
5609 }
5610 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5611 lhs_main_type))
5612 {
5613 if (found_field != NULL_TREE)
5614 return NULL_TREE;
5615 found_field = field;
5616 found_sub_field = true;
5617 }
5618 }
5619
5620 if (found_field == NULL_TREE)
5621 return NULL_TREE;
5622
5623 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5624 build_fold_indirect_ref (rhs), found_field,
5625 NULL_TREE);
5626 ret = build_fold_addr_expr_loc (location, ret);
5627
5628 if (found_sub_field)
5629 {
5630 ret = convert_to_anonymous_field (location, type, ret);
5631 gcc_assert (ret != NULL_TREE);
5632 }
5633
5634 return ret;
5635}
5636
63bc4e87
MP
5637/* Issue an error message for a bad initializer component.
5638 GMSGID identifies the message.
5639 The component name is taken from the spelling stack. */
5640
5641static void
ea58ef42 5642error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5643{
5644 char *ofwhat;
5645
5646 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5647 error_at (loc, gmsgid);
63bc4e87
MP
5648 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5649 if (*ofwhat)
d7ff7ae5 5650 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5651}
5652
5653/* Issue a pedantic warning for a bad initializer component. OPT is
5654 the option OPT_* (from options.h) controlling this warning or 0 if
5655 it is unconditionally given. GMSGID identifies the message. The
5656 component name is taken from the spelling stack. */
5657
5658static void
5659pedwarn_init (location_t location, int opt, const char *gmsgid)
5660{
5661 char *ofwhat;
d7ff7ae5 5662 bool warned;
63bc4e87
MP
5663
5664 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5665 warned = pedwarn (location, opt, gmsgid);
63bc4e87 5666 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5667 if (*ofwhat && warned)
5668 inform (location, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5669}
5670
5671/* Issue a warning for a bad initializer component.
5672
5673 OPT is the OPT_W* value corresponding to the warning option that
5674 controls this warning. GMSGID identifies the message. The
5675 component name is taken from the spelling stack. */
5676
5677static void
5678warning_init (location_t loc, int opt, const char *gmsgid)
5679{
5680 char *ofwhat;
d7ff7ae5 5681 bool warned;
63bc4e87
MP
5682
5683 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5684 warned = warning_at (loc, opt, gmsgid);
63bc4e87 5685 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5686 if (*ofwhat && warned)
5687 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5688}
5689\f
5690/* If TYPE is an array type and EXPR is a parenthesized string
5691 constant, warn if pedantic that EXPR is being used to initialize an
5692 object of type TYPE. */
5693
5694void
d033409e 5695maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
5696{
5697 if (pedantic
5698 && TREE_CODE (type) == ARRAY_TYPE
5699 && TREE_CODE (expr.value) == STRING_CST
5700 && expr.original_code != STRING_CST)
d033409e 5701 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
5702 "array initialized from parenthesized string constant");
5703}
5704
bbbbb16a
ILT
5705/* Convert value RHS to type TYPE as preparation for an assignment to
5706 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5707 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5708 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5709 constant before any folding.
3e4093b6
RS
5710 The real work of conversion is done by `convert'.
5711 The purpose of this function is to generate error messages
5712 for assignments that are not allowed in C.
2ac2f164
JM
5713 ERRTYPE says whether it is argument passing, assignment,
5714 initialization or return.
2f6e4e97 5715
aabef2de
MP
5716 In the following example, '~' denotes where EXPR_LOC and '^' where
5717 LOCATION point to:
5718
5719 f (var); [ic_argpass]
5720 ^ ~~~
5721 x = var; [ic_assign]
5722 ^ ~~~;
5723 int x = var; [ic_init]
5724 ^^^
5725 return x; [ic_return]
5726 ^
5727
2ac2f164 5728 FUNCTION is a tree for the function being called.
3e4093b6 5729 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5730
3e4093b6 5731static tree
81e5eca8 5732convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5733 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5734 bool null_pointer_constant, tree fundecl,
5735 tree function, int parmnum)
3e4093b6
RS
5736{
5737 enum tree_code codel = TREE_CODE (type);
8ce94e44 5738 tree orig_rhs = rhs;
3e4093b6
RS
5739 tree rhstype;
5740 enum tree_code coder;
2ac2f164 5741 tree rname = NULL_TREE;
58393038 5742 bool objc_ok = false;
2ac2f164 5743
1c7485af
MP
5744 /* Use the expansion point location to handle cases such as user's
5745 function returning a wrong-type macro defined in a system header. */
5746 location = expansion_point_location_if_in_system_header (location);
5747
6b4ef5c1 5748 if (errtype == ic_argpass)
2ac2f164
JM
5749 {
5750 tree selector;
5751 /* Change pointer to function to the function itself for
5752 diagnostics. */
5753 if (TREE_CODE (function) == ADDR_EXPR
5754 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5755 function = TREE_OPERAND (function, 0);
5756
5757 /* Handle an ObjC selector specially for diagnostics. */
5758 selector = objc_message_selector ();
5759 rname = function;
5760 if (selector && parmnum > 2)
5761 {
5762 rname = selector;
5763 parmnum -= 2;
5764 }
5765 }
5766
5767 /* This macro is used to emit diagnostics to ensure that all format
5768 strings are complete sentences, visible to gettext and checked at
5769 compile time. */
768952be 5770#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5771 do { \
5772 switch (errtype) \
5773 { \
5774 case ic_argpass: \
5c1bc275 5775 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 5776 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5777 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
5778 "expected %qT but argument is of type %qT", \
5779 type, rhstype); \
5780 break; \
1e053dfe
MLI
5781 case ic_assign: \
5782 pedwarn (LOCATION, OPT, AS); \
5783 break; \
5784 case ic_init: \
6a8f4e12 5785 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5786 break; \
5787 case ic_return: \
d033409e 5788 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5789 break; \
5790 default: \
5791 gcc_unreachable (); \
5792 } \
2ac2f164 5793 } while (0)
cb3ca04e 5794
49706e39
MLI
5795 /* This macro is used to emit diagnostics to ensure that all format
5796 strings are complete sentences, visible to gettext and checked at
768952be 5797 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 5798 extra parameter to enumerate qualifiers. */
768952be 5799#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
5800 do { \
5801 switch (errtype) \
5802 { \
5803 case ic_argpass: \
5c1bc275 5804 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 5805 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5806 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
5807 "expected %qT but argument is of type %qT", \
5808 type, rhstype); \
5809 break; \
5810 case ic_assign: \
5c1bc275 5811 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
5812 break; \
5813 case ic_init: \
5c1bc275 5814 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
5815 break; \
5816 case ic_return: \
5c1bc275 5817 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
5818 break; \
5819 default: \
5820 gcc_unreachable (); \
5821 } \
5822 } while (0)
5823
768952be
MU
5824 /* This macro is used to emit diagnostics to ensure that all format
5825 strings are complete sentences, visible to gettext and checked at
5826 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5827 warning_at instead of pedwarn. */
5828#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5829 do { \
5830 switch (errtype) \
5831 { \
5832 case ic_argpass: \
5833 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5834 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5835 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5836 "expected %qT but argument is of type %qT", \
5837 type, rhstype); \
5838 break; \
5839 case ic_assign: \
5840 warning_at (LOCATION, OPT, AS, QUALS); \
5841 break; \
5842 case ic_init: \
5843 warning_at (LOCATION, OPT, IN, QUALS); \
5844 break; \
5845 case ic_return: \
5846 warning_at (LOCATION, OPT, RE, QUALS); \
5847 break; \
5848 default: \
5849 gcc_unreachable (); \
5850 } \
5851 } while (0)
5852
8ce94e44
JM
5853 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5854 rhs = TREE_OPERAND (rhs, 0);
5855
3e4093b6
RS
5856 rhstype = TREE_TYPE (rhs);
5857 coder = TREE_CODE (rhstype);
5858
5859 if (coder == ERROR_MARK)
5860 return error_mark_node;
5861
58393038
ZL
5862 if (c_dialect_objc ())
5863 {
5864 int parmno;
5865
5866 switch (errtype)
5867 {
5868 case ic_return:
5869 parmno = 0;
5870 break;
5871
5872 case ic_assign:
5873 parmno = -1;
5874 break;
5875
5876 case ic_init:
5877 parmno = -2;
5878 break;
5879
5880 default:
5881 parmno = parmnum;
5882 break;
5883 }
5884
5885 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5886 }
5887
bbbbb16a
ILT
5888 if (warn_cxx_compat)
5889 {
5890 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5891 if (checktype != error_mark_node
5892 && TREE_CODE (type) == ENUMERAL_TYPE
5893 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5894 {
768952be
MU
5895 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5896 G_("enum conversion when passing argument "
5897 "%d of %qE is invalid in C++"),
5898 G_("enum conversion in assignment is "
5899 "invalid in C++"),
5900 G_("enum conversion in initialization is "
5901 "invalid in C++"),
5902 G_("enum conversion in return is "
5903 "invalid in C++"));
bbbbb16a
ILT
5904 }
5905 }
5906
3e4093b6 5907 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5908 return rhs;
3e4093b6
RS
5909
5910 if (coder == VOID_TYPE)
400fbf9f 5911 {
6dcc04b0
JM
5912 /* Except for passing an argument to an unprototyped function,
5913 this is a constraint violation. When passing an argument to
5914 an unprototyped function, it is compile-time undefined;
5915 making it a constraint in that case was rejected in
5916 DR#252. */
c2255bc4 5917 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5918 return error_mark_node;
400fbf9f 5919 }
808d6eaa
JM
5920 rhs = require_complete_type (rhs);
5921 if (rhs == error_mark_node)
5922 return error_mark_node;
1807ffc1
MS
5923
5924 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
5925 return error_mark_node;
5926
cd192ccc
MS
5927 /* A non-reference type can convert to a reference. This handles
5928 va_start, va_copy and possibly port built-ins. */
5929 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5930 {
3e4093b6 5931 if (!lvalue_p (rhs))
400fbf9f 5932 {
c2255bc4 5933 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5934 return error_mark_node;
400fbf9f 5935 }
3e4093b6
RS
5936 if (!c_mark_addressable (rhs))
5937 return error_mark_node;
5938 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5939 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5940
81e5eca8 5941 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
5942 build_pointer_type (TREE_TYPE (type)),
5943 rhs, origtype, errtype,
5944 null_pointer_constant, fundecl, function,
5945 parmnum);
cd192ccc
MS
5946 if (rhs == error_mark_node)
5947 return error_mark_node;
3e4093b6
RS
5948
5949 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5950 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5951 return rhs;
400fbf9f 5952 }
3e4093b6 5953 /* Some types can interconvert without explicit casts. */
3274deff 5954 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5955 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5956 return convert (type, rhs);
5957 /* Arithmetic types all interconvert, and enum is treated like int. */
5958 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5959 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5960 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5961 || codel == BOOLEAN_TYPE)
5962 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5963 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5964 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5965 || coder == BOOLEAN_TYPE))
928c19bb
JM
5966 {
5967 tree ret;
5968 bool save = in_late_binary_op;
e5341100
JJ
5969 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5970 || (coder == REAL_TYPE
5971 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5972 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
928c19bb 5973 in_late_binary_op = true;
81e5eca8
MP
5974 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5975 ? expr_loc : location, type, orig_rhs);
e5341100 5976 in_late_binary_op = save;
928c19bb
JM
5977 return ret;
5978 }
400fbf9f 5979
79077aea
JJ
5980 /* Aggregates in different TUs might need conversion. */
5981 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5982 && codel == coder
5983 && comptypes (type, rhstype))
81e5eca8
MP
5984 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5985 ? expr_loc : location, type, rhs);
79077aea 5986
ebf0bf7f 5987 /* Conversion to a transparent union or record from its member types.
3e4093b6 5988 This applies only to function arguments. */
ebf0bf7f
JJ
5989 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5990 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5991 && errtype == ic_argpass)
400fbf9f 5992 {
0257e383 5993 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5994
910ad8de 5995 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5996 {
0257e383 5997 tree memb_type = TREE_TYPE (memb);
400fbf9f 5998
3e4093b6 5999 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 6000 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 6001 break;
e58cd767 6002
3e4093b6
RS
6003 if (TREE_CODE (memb_type) != POINTER_TYPE)
6004 continue;
2f6e4e97 6005
3e4093b6
RS
6006 if (coder == POINTER_TYPE)
6007 {
6008 tree ttl = TREE_TYPE (memb_type);
6009 tree ttr = TREE_TYPE (rhstype);
400fbf9f 6010
3e4093b6
RS
6011 /* Any non-function converts to a [const][volatile] void *
6012 and vice versa; otherwise, targets must be the same.
6013 Meanwhile, the lhs target must have all the qualifiers of
6014 the rhs. */
267bac10
JM
6015 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6016 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6017 || comp_target_types (location, memb_type, rhstype))
3e4093b6 6018 {
267bac10
JM
6019 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6020 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 6021 /* If this type won't generate any warnings, use it. */
267bac10 6022 if (lquals == rquals
3e4093b6
RS
6023 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6024 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
6025 ? ((lquals | rquals) == rquals)
6026 : ((lquals | rquals) == lquals)))
3e4093b6 6027 break;
400fbf9f 6028
3e4093b6 6029 /* Keep looking for a better type, but remember this one. */
0257e383
RH
6030 if (!marginal_memb)
6031 marginal_memb = memb;
3e4093b6
RS
6032 }
6033 }
82bde854 6034
3e4093b6 6035 /* Can convert integer zero to any pointer type. */
928c19bb 6036 if (null_pointer_constant)
3e4093b6
RS
6037 {
6038 rhs = null_pointer_node;
6039 break;
6040 }
6041 }
400fbf9f 6042
0257e383 6043 if (memb || marginal_memb)
3e4093b6 6044 {
0257e383 6045 if (!memb)
3e4093b6
RS
6046 {
6047 /* We have only a marginally acceptable member type;
6048 it needs a warning. */
0257e383 6049 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 6050 tree ttr = TREE_TYPE (rhstype);
714a0864 6051
3e4093b6
RS
6052 /* Const and volatile mean something different for function
6053 types, so the usual warnings are not appropriate. */
6054 if (TREE_CODE (ttr) == FUNCTION_TYPE
6055 && TREE_CODE (ttl) == FUNCTION_TYPE)
6056 {
6057 /* Because const and volatile on functions are
6058 restrictions that say the function will not do
6059 certain things, it is okay to use a const or volatile
6060 function where an ordinary one is wanted, but not
6061 vice-versa. */
36c5e70a
BE
6062 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6063 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6064 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6065 OPT_Wdiscarded_qualifiers,
6066 G_("passing argument %d of %qE "
6067 "makes %q#v qualified function "
6068 "pointer from unqualified"),
6069 G_("assignment makes %q#v qualified "
6070 "function pointer from "
6071 "unqualified"),
6072 G_("initialization makes %q#v qualified "
6073 "function pointer from "
6074 "unqualified"),
6075 G_("return makes %q#v qualified function "
6076 "pointer from unqualified"),
6077 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6078 }
36c5e70a
BE
6079 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6080 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6081 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6082 OPT_Wdiscarded_qualifiers,
6083 G_("passing argument %d of %qE discards "
6084 "%qv qualifier from pointer target type"),
6085 G_("assignment discards %qv qualifier "
6086 "from pointer target type"),
6087 G_("initialization discards %qv qualifier "
6088 "from pointer target type"),
6089 G_("return discards %qv qualifier from "
6090 "pointer target type"),
6091 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6092
6093 memb = marginal_memb;
3e4093b6 6094 }
400fbf9f 6095
fcf73884 6096 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6097 pedwarn (location, OPT_Wpedantic,
fcf73884 6098 "ISO C prohibits argument conversion to union type");
0e7c47fa 6099
db3927fb 6100 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6101 return build_constructor_single (type, memb, rhs);
3e4093b6 6102 }
0e7c47fa
RK
6103 }
6104
3e4093b6
RS
6105 /* Conversions among pointers */
6106 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6107 && (coder == codel))
400fbf9f 6108 {
3e4093b6
RS
6109 tree ttl = TREE_TYPE (type);
6110 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6111 tree mvl = ttl;
6112 tree mvr = ttr;
3e4093b6 6113 bool is_opaque_pointer;
264fa2db 6114 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6115 addr_space_t asl;
6116 addr_space_t asr;
400fbf9f 6117
46df2823 6118 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6119 mvl = (TYPE_ATOMIC (mvl)
6120 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6121 TYPE_QUAL_ATOMIC)
6122 : TYPE_MAIN_VARIANT (mvl));
46df2823 6123 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6124 mvr = (TYPE_ATOMIC (mvr)
6125 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6126 TYPE_QUAL_ATOMIC)
6127 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6128 /* Opaque pointers are treated like void pointers. */
f83c7f63 6129 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6130
478a1c5b
ILT
6131 /* The Plan 9 compiler permits a pointer to a struct to be
6132 automatically converted into a pointer to an anonymous field
6133 within the struct. */
6134 if (flag_plan9_extensions
6135 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6136 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6137 && mvl != mvr)
6138 {
6139 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6140 if (new_rhs != NULL_TREE)
6141 {
6142 rhs = new_rhs;
6143 rhstype = TREE_TYPE (rhs);
6144 coder = TREE_CODE (rhstype);
6145 ttr = TREE_TYPE (rhstype);
6146 mvr = TYPE_MAIN_VARIANT (ttr);
6147 }
6148 }
6149
b7e20b53 6150 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6151 for the purpose of reducing the number of false positives, we
6152 tolerate the special case of
b7e20b53 6153
c22cacf3 6154 int *p = NULL;
b7e20b53 6155
c22cacf3 6156 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6157 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6158 warning_at (errtype == ic_argpass ? expr_loc : location,
6159 OPT_Wc___compat,
6160 "request for implicit conversion "
c2255bc4 6161 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6162
36c5e70a
BE
6163 /* See if the pointers point to incompatible address spaces. */
6164 asl = TYPE_ADDR_SPACE (ttl);
6165 asr = TYPE_ADDR_SPACE (ttr);
6166 if (!null_pointer_constant_p (rhs)
6167 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6168 {
6169 switch (errtype)
6170 {
6171 case ic_argpass:
8ffcdea8 6172 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6173 "non-enclosed address space", parmnum, rname);
6174 break;
6175 case ic_assign:
6176 error_at (location, "assignment from pointer to "
6177 "non-enclosed address space");
6178 break;
6179 case ic_init:
6180 error_at (location, "initialization from pointer to "
6181 "non-enclosed address space");
6182 break;
6183 case ic_return:
6184 error_at (location, "return from pointer to "
6185 "non-enclosed address space");
6186 break;
6187 default:
6188 gcc_unreachable ();
6189 }
6190 return error_mark_node;
6191 }
6192
7876a414
KG
6193 /* Check if the right-hand side has a format attribute but the
6194 left-hand side doesn't. */
90137d8f 6195 if (warn_suggest_attribute_format
104f8784 6196 && check_missing_format_attribute (type, rhstype))
c22cacf3 6197 {
104f8784
KG
6198 switch (errtype)
6199 {
6200 case ic_argpass:
8ffcdea8 6201 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6202 "argument %d of %qE might be "
6203 "a candidate for a format attribute",
6204 parmnum, rname);
104f8784
KG
6205 break;
6206 case ic_assign:
90137d8f 6207 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6208 "assignment left-hand side might be "
6209 "a candidate for a format attribute");
104f8784
KG
6210 break;
6211 case ic_init:
90137d8f 6212 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6213 "initialization left-hand side might be "
6214 "a candidate for a format attribute");
104f8784
KG
6215 break;
6216 case ic_return:
90137d8f 6217 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6218 "return type might be "
6219 "a candidate for a format attribute");
104f8784
KG
6220 break;
6221 default:
6222 gcc_unreachable ();
6223 }
7876a414 6224 }
c22cacf3 6225
3e4093b6
RS
6226 /* Any non-function converts to a [const][volatile] void *
6227 and vice versa; otherwise, targets must be the same.
6228 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6229 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6230 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6231 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6232 || is_opaque_pointer
f8a93a2e
JJ
6233 || ((c_common_unsigned_type (mvl)
6234 == c_common_unsigned_type (mvr))
267bac10
JM
6235 && (c_common_signed_type (mvl)
6236 == c_common_signed_type (mvr))
6237 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6238 {
768952be
MU
6239 /* Warn about loss of qualifers from pointers to arrays with
6240 qualifiers on the element type. */
6241 if (TREE_CODE (ttr) == ARRAY_TYPE)
6242 {
6243 ttr = strip_array_types (ttr);
6244 ttl = strip_array_types (ttl);
6245
6246 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6247 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6248 WARNING_FOR_QUALIFIERS (location, expr_loc,
6249 OPT_Wdiscarded_array_qualifiers,
6250 G_("passing argument %d of %qE discards "
6251 "%qv qualifier from pointer target type"),
6252 G_("assignment discards %qv qualifier "
6253 "from pointer target type"),
6254 G_("initialization discards %qv qualifier "
6255 "from pointer target type"),
6256 G_("return discards %qv qualifier from "
6257 "pointer target type"),
6258 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6259 }
6260 else if (pedantic
3e4093b6
RS
6261 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6262 ||
6263 (VOID_TYPE_P (ttr)
928c19bb 6264 && !null_pointer_constant
3e4093b6 6265 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6266 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6267 G_("ISO C forbids passing argument %d of "
6268 "%qE between function pointer "
6269 "and %<void *%>"),
6270 G_("ISO C forbids assignment between "
6271 "function pointer and %<void *%>"),
6272 G_("ISO C forbids initialization between "
6273 "function pointer and %<void *%>"),
6274 G_("ISO C forbids return between function "
6275 "pointer and %<void *%>"));
3e4093b6
RS
6276 /* Const and volatile mean something different for function types,
6277 so the usual warnings are not appropriate. */
6278 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6279 && TREE_CODE (ttl) != FUNCTION_TYPE)
6280 {
768952be
MU
6281 /* Don't warn about loss of qualifier for conversions from
6282 qualified void* to pointers to arrays with corresponding
6283 qualifier on the element type. */
6284 if (!pedantic)
6285 ttl = strip_array_types (ttl);
6286
267bac10
JM
6287 /* Assignments between atomic and non-atomic objects are OK. */
6288 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6289 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6290 {
768952be
MU
6291 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6292 OPT_Wdiscarded_qualifiers,
6293 G_("passing argument %d of %qE discards "
6294 "%qv qualifier from pointer target type"),
6295 G_("assignment discards %qv qualifier "
6296 "from pointer target type"),
6297 G_("initialization discards %qv qualifier "
6298 "from pointer target type"),
6299 G_("return discards %qv qualifier from "
6300 "pointer target type"),
6301 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6302 }
3e4093b6
RS
6303 /* If this is not a case of ignoring a mismatch in signedness,
6304 no warning. */
6305 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6306 || target_cmp)
3e4093b6
RS
6307 ;
6308 /* If there is a mismatch, do warn. */
f2fd3821 6309 else if (warn_pointer_sign)
768952be
MU
6310 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6311 G_("pointer targets in passing argument "
6312 "%d of %qE differ in signedness"),
6313 G_("pointer targets in assignment "
6314 "differ in signedness"),
6315 G_("pointer targets in initialization "
6316 "differ in signedness"),
6317 G_("pointer targets in return differ "
6318 "in signedness"));
3e4093b6
RS
6319 }
6320 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6321 && TREE_CODE (ttr) == FUNCTION_TYPE)
6322 {
6323 /* Because const and volatile on functions are restrictions
6324 that say the function will not do certain things,
6325 it is okay to use a const or volatile function
6326 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6327 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6328 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6329 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6330 OPT_Wdiscarded_qualifiers,
6331 G_("passing argument %d of %qE makes "
6332 "%q#v qualified function pointer "
6333 "from unqualified"),
6334 G_("assignment makes %q#v qualified function "
6335 "pointer from unqualified"),
6336 G_("initialization makes %q#v qualified "
6337 "function pointer from unqualified"),
6338 G_("return makes %q#v qualified function "
6339 "pointer from unqualified"),
6340 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6341 }
6342 }
6343 else
58393038
ZL
6344 /* Avoid warning about the volatile ObjC EH puts on decls. */
6345 if (!objc_ok)
768952be
MU
6346 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6347 OPT_Wincompatible_pointer_types,
6348 G_("passing argument %d of %qE from "
6349 "incompatible pointer type"),
6350 G_("assignment from incompatible pointer type"),
6351 G_("initialization from incompatible "
6352 "pointer type"),
6353 G_("return from incompatible pointer type"));
58393038 6354
3e4093b6
RS
6355 return convert (type, rhs);
6356 }
b494fd98
EB
6357 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6358 {
6dcc04b0
JM
6359 /* ??? This should not be an error when inlining calls to
6360 unprototyped functions. */
c2255bc4 6361 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6362 return error_mark_node;
6363 }
3e4093b6 6364 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6365 {
3e4093b6
RS
6366 /* An explicit constant 0 can convert to a pointer,
6367 or one that results from arithmetic, even including
6368 a cast to integer type. */
928c19bb 6369 if (!null_pointer_constant)
768952be
MU
6370 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6371 OPT_Wint_conversion,
6372 G_("passing argument %d of %qE makes "
6373 "pointer from integer without a cast"),
6374 G_("assignment makes pointer from integer "
6375 "without a cast"),
6376 G_("initialization makes pointer from "
6377 "integer without a cast"),
6378 G_("return makes pointer from integer "
6379 "without a cast"));
b3006337
EB
6380
6381 return convert (type, rhs);
400fbf9f 6382 }
3e4093b6 6383 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6384 {
768952be
MU
6385 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6386 OPT_Wint_conversion,
6387 G_("passing argument %d of %qE makes integer "
6388 "from pointer without a cast"),
6389 G_("assignment makes integer from pointer "
6390 "without a cast"),
6391 G_("initialization makes integer from pointer "
6392 "without a cast"),
6393 G_("return makes integer from pointer "
6394 "without a cast"));
3e4093b6
RS
6395 return convert (type, rhs);
6396 }
6397 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6398 {
6399 tree ret;
6400 bool save = in_late_binary_op;
6401 in_late_binary_op = true;
6402 ret = convert (type, rhs);
6403 in_late_binary_op = save;
6404 return ret;
6405 }
400fbf9f 6406
2ac2f164 6407 switch (errtype)
3e4093b6 6408 {
2ac2f164 6409 case ic_argpass:
8ffcdea8
MP
6410 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6411 rname);
a7da8b42 6412 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6413 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6414 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6415 break;
6416 case ic_assign:
c2255bc4
AH
6417 error_at (location, "incompatible types when assigning to type %qT from "
6418 "type %qT", type, rhstype);
2ac2f164
JM
6419 break;
6420 case ic_init:
c2255bc4 6421 error_at (location,
8ffcdea8 6422 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6423 type, rhstype);
2ac2f164
JM
6424 break;
6425 case ic_return:
c2255bc4 6426 error_at (location,
8ffcdea8 6427 "incompatible types when returning type %qT but %qT was "
c2255bc4 6428 "expected", rhstype, type);
2ac2f164
JM
6429 break;
6430 default:
6431 gcc_unreachable ();
400fbf9f 6432 }
53b01f59 6433
3e4093b6
RS
6434 return error_mark_node;
6435}
3e4093b6
RS
6436\f
6437/* If VALUE is a compound expr all of whose expressions are constant, then
6438 return its value. Otherwise, return error_mark_node.
15b732b2 6439
3e4093b6
RS
6440 This is for handling COMPOUND_EXPRs as initializer elements
6441 which is allowed with a warning when -pedantic is specified. */
15b732b2 6442
3e4093b6
RS
6443static tree
6444valid_compound_expr_initializer (tree value, tree endtype)
6445{
6446 if (TREE_CODE (value) == COMPOUND_EXPR)
6447 {
6448 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6449 == error_mark_node)
6450 return error_mark_node;
6451 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6452 endtype);
6453 }
116df786 6454 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6455 return error_mark_node;
6456 else
6457 return value;
15b732b2 6458}
400fbf9f 6459\f
3e4093b6
RS
6460/* Perform appropriate conversions on the initial value of a variable,
6461 store it in the declaration DECL,
6462 and print any error messages that are appropriate.
bbbbb16a 6463 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6464 If the init is invalid, store an ERROR_MARK.
6465
6466 INIT_LOC is the location of the initial value. */
400fbf9f 6467
3e4093b6 6468void
c2255bc4 6469store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6470{
3e4093b6 6471 tree value, type;
928c19bb 6472 bool npc = false;
400fbf9f 6473
3e4093b6 6474 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6475
3e4093b6
RS
6476 type = TREE_TYPE (decl);
6477 if (TREE_CODE (type) == ERROR_MARK)
6478 return;
400fbf9f 6479
3e4093b6 6480 /* Digest the specified initializer into an expression. */
400fbf9f 6481
928c19bb
JM
6482 if (init)
6483 npc = null_pointer_constant_p (init);
c2255bc4
AH
6484 value = digest_init (init_loc, type, init, origtype, npc,
6485 true, TREE_STATIC (decl));
400fbf9f 6486
3e4093b6 6487 /* Store the expression if valid; else report error. */
400fbf9f 6488
8400e75e 6489 if (!in_system_header_at (input_location)
3f75a254 6490 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6491 warning (OPT_Wtraditional, "traditional C rejects automatic "
6492 "aggregate initialization");
2f6e4e97 6493
dea63e49
JJ
6494 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6495 DECL_INITIAL (decl) = value;
400fbf9f 6496
3e4093b6
RS
6497 /* ANSI wants warnings about out-of-range constant initializers. */
6498 STRIP_TYPE_NOPS (value);
b8698a0f 6499 if (TREE_STATIC (decl))
c2658540 6500 constant_expression_warning (value);
400fbf9f 6501
3e4093b6
RS
6502 /* Check if we need to set array size from compound literal size. */
6503 if (TREE_CODE (type) == ARRAY_TYPE
6504 && TYPE_DOMAIN (type) == 0
6505 && value != error_mark_node)
400fbf9f 6506 {
3e4093b6
RS
6507 tree inside_init = init;
6508
ed248cf7 6509 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6510 inside_init = fold (inside_init);
6511
6512 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6513 {
8d9f82d5 6514 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6515
8d9f82d5 6516 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6517 {
6518 /* For int foo[] = (int [3]){1}; we need to set array size
6519 now since later on array initializer will be just the
6520 brace enclosed list of the compound literal. */
e30ecc5d 6521 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6522 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6523 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6524 layout_type (type);
8d9f82d5 6525 layout_decl (cldecl, 0);
e30ecc5d
JJ
6526 TREE_TYPE (decl)
6527 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6528 }
6529 }
400fbf9f 6530 }
3e4093b6
RS
6531}
6532\f
6533/* Methods for storing and printing names for error messages. */
400fbf9f 6534
3e4093b6
RS
6535/* Implement a spelling stack that allows components of a name to be pushed
6536 and popped. Each element on the stack is this structure. */
400fbf9f 6537
3e4093b6
RS
6538struct spelling
6539{
6540 int kind;
6541 union
400fbf9f 6542 {
a0f0ab9f 6543 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6544 const char *s;
6545 } u;
6546};
2f6e4e97 6547
3e4093b6
RS
6548#define SPELLING_STRING 1
6549#define SPELLING_MEMBER 2
6550#define SPELLING_BOUNDS 3
400fbf9f 6551
3e4093b6
RS
6552static struct spelling *spelling; /* Next stack element (unused). */
6553static struct spelling *spelling_base; /* Spelling stack base. */
6554static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6555
3e4093b6
RS
6556/* Macros to save and restore the spelling stack around push_... functions.
6557 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6558
3e4093b6
RS
6559#define SPELLING_DEPTH() (spelling - spelling_base)
6560#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6561
3e4093b6
RS
6562/* Push an element on the spelling stack with type KIND and assign VALUE
6563 to MEMBER. */
400fbf9f 6564
3e4093b6
RS
6565#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6566{ \
6567 int depth = SPELLING_DEPTH (); \
6568 \
6569 if (depth >= spelling_size) \
6570 { \
6571 spelling_size += 10; \
cca8ead2
BI
6572 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6573 spelling_size); \
3e4093b6
RS
6574 RESTORE_SPELLING_DEPTH (depth); \
6575 } \
6576 \
6577 spelling->kind = (KIND); \
6578 spelling->MEMBER = (VALUE); \
6579 spelling++; \
6580}
400fbf9f 6581
3e4093b6 6582/* Push STRING on the stack. Printed literally. */
400fbf9f 6583
3e4093b6
RS
6584static void
6585push_string (const char *string)
6586{
6587 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6588}
400fbf9f 6589
3e4093b6 6590/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6591
3e4093b6
RS
6592static void
6593push_member_name (tree decl)
6594{
6595 const char *const string
88388a52
JM
6596 = (DECL_NAME (decl)
6597 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6598 : _("<anonymous>"));
3e4093b6
RS
6599 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6600}
400fbf9f 6601
3e4093b6 6602/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6603
3e4093b6 6604static void
a0f0ab9f 6605push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6606{
6607 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6608}
bb58bec5 6609
3e4093b6 6610/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6611
3e4093b6
RS
6612static int
6613spelling_length (void)
6614{
6615 int size = 0;
6616 struct spelling *p;
400fbf9f 6617
3e4093b6
RS
6618 for (p = spelling_base; p < spelling; p++)
6619 {
6620 if (p->kind == SPELLING_BOUNDS)
6621 size += 25;
6622 else
6623 size += strlen (p->u.s) + 1;
6624 }
6625
6626 return size;
400fbf9f 6627}
400fbf9f 6628
3e4093b6 6629/* Print the spelling to BUFFER and return it. */
400fbf9f 6630
3e4093b6
RS
6631static char *
6632print_spelling (char *buffer)
400fbf9f 6633{
3e4093b6
RS
6634 char *d = buffer;
6635 struct spelling *p;
400fbf9f 6636
3e4093b6
RS
6637 for (p = spelling_base; p < spelling; p++)
6638 if (p->kind == SPELLING_BOUNDS)
6639 {
a0f0ab9f 6640 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6641 d += strlen (d);
6642 }
6643 else
6644 {
6645 const char *s;
6646 if (p->kind == SPELLING_MEMBER)
6647 *d++ = '.';
6648 for (s = p->u.s; (*d = *s++); d++)
6649 ;
6650 }
6651 *d++ = '\0';
6652 return buffer;
6653}
400fbf9f 6654
3e4093b6
RS
6655/* Digest the parser output INIT as an initializer for type TYPE.
6656 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6657
bbbbb16a
ILT
6658 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6659
928c19bb
JM
6660 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6661
916c5919
JM
6662 If INIT is a string constant, STRICT_STRING is true if it is
6663 unparenthesized or we should not warn here for it being parenthesized.
6664 For other types of INIT, STRICT_STRING is not used.
6665
c2255bc4
AH
6666 INIT_LOC is the location of the INIT.
6667
3e4093b6
RS
6668 REQUIRE_CONSTANT requests an error if non-constant initializers or
6669 elements are seen. */
7e842ef8 6670
3e4093b6 6671static tree
c2255bc4
AH
6672digest_init (location_t init_loc, tree type, tree init, tree origtype,
6673 bool null_pointer_constant, bool strict_string,
6674 int require_constant)
3e4093b6
RS
6675{
6676 enum tree_code code = TREE_CODE (type);
6677 tree inside_init = init;
8ce94e44 6678 tree semantic_type = NULL_TREE;
928c19bb 6679 bool maybe_const = true;
7e842ef8 6680
3e4093b6 6681 if (type == error_mark_node
f01da1a5 6682 || !init
7a0ca710 6683 || error_operand_p (init))
3e4093b6 6684 return error_mark_node;
7e842ef8 6685
ed248cf7 6686 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6687
8ce94e44
JM
6688 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6689 {
6690 semantic_type = TREE_TYPE (inside_init);
6691 inside_init = TREE_OPERAND (inside_init, 0);
6692 }
928c19bb
JM
6693 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6694 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6695
3e4093b6
RS
6696 /* Initialization of an array of chars from a string constant
6697 optionally enclosed in braces. */
7e842ef8 6698
197463ae
JM
6699 if (code == ARRAY_TYPE && inside_init
6700 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6701 {
267bac10
JM
6702 tree typ1
6703 = (TYPE_ATOMIC (TREE_TYPE (type))
6704 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6705 TYPE_QUAL_ATOMIC)
6706 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6707 /* Note that an array could be both an array of character type
6708 and an array of wchar_t if wchar_t is signed char or unsigned
6709 char. */
6710 bool char_array = (typ1 == char_type_node
6711 || typ1 == signed_char_type_node
6712 || typ1 == unsigned_char_type_node);
6713 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6714 bool char16_array = !!comptypes (typ1, char16_type_node);
6715 bool char32_array = !!comptypes (typ1, char32_type_node);
6716
6717 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6718 {
916c5919 6719 struct c_expr expr;
c466b2cd 6720 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6721 expr.value = inside_init;
6722 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6723 expr.original_type = NULL;
d033409e 6724 maybe_warn_string_init (init_loc, type, expr);
916c5919 6725
a45e580b 6726 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6727 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6728 "initialization of a flexible array member");
6729
3e4093b6 6730 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6731 TYPE_MAIN_VARIANT (type)))
3e4093b6 6732 return inside_init;
7e842ef8 6733
c466b2cd 6734 if (char_array)
3e4093b6 6735 {
c466b2cd
KVH
6736 if (typ2 != char_type_node)
6737 {
ea58ef42
MP
6738 error_init (init_loc, "char-array initialized from wide "
6739 "string");
c466b2cd
KVH
6740 return error_mark_node;
6741 }
3e4093b6 6742 }
c466b2cd 6743 else
3e4093b6 6744 {
c466b2cd
KVH
6745 if (typ2 == char_type_node)
6746 {
ea58ef42
MP
6747 error_init (init_loc, "wide character array initialized "
6748 "from non-wide string");
c466b2cd
KVH
6749 return error_mark_node;
6750 }
6751 else if (!comptypes(typ1, typ2))
6752 {
ea58ef42
MP
6753 error_init (init_loc, "wide character array initialized "
6754 "from incompatible wide string");
c466b2cd
KVH
6755 return error_mark_node;
6756 }
7e842ef8 6757 }
2f6e4e97 6758
3e4093b6
RS
6759 TREE_TYPE (inside_init) = type;
6760 if (TYPE_DOMAIN (type) != 0
6761 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6762 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6763 {
6764 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6765
c466b2cd 6766 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6767 because it's ok to ignore the terminating null char
6768 that is counted in the length of the constant. */
5eb4df45
ILT
6769 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6770 (len
6771 - (TYPE_PRECISION (typ1)
6772 / BITS_PER_UNIT))))
6773 pedwarn_init (init_loc, 0,
6774 ("initializer-string for array of chars "
6775 "is too long"));
6776 else if (warn_cxx_compat
6777 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6778 warning_at (init_loc, OPT_Wc___compat,
6779 ("initializer-string for array chars "
6780 "is too long for C++"));
6781 }
7e842ef8 6782
3e4093b6 6783 return inside_init;
7e842ef8 6784 }
197463ae
JM
6785 else if (INTEGRAL_TYPE_P (typ1))
6786 {
ea58ef42 6787 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
6788 "from string constant");
6789 return error_mark_node;
6790 }
7e842ef8
PE
6791 }
6792
3e4093b6
RS
6793 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6794 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6795 below and handle as a constructor. */
e89be13b 6796 if (code == VECTOR_TYPE
31521951 6797 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
00c8e9f6 6798 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6799 && TREE_CONSTANT (inside_init))
6800 {
6801 if (TREE_CODE (inside_init) == VECTOR_CST
6802 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6803 TYPE_MAIN_VARIANT (type)))
6804 return inside_init;
6805
6806 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6807 {
4038c495
GB
6808 unsigned HOST_WIDE_INT ix;
6809 tree value;
6810 bool constant_p = true;
e89be13b
JJ
6811
6812 /* Iterate through elements and check if all constructor
6813 elements are *_CSTs. */
4038c495
GB
6814 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6815 if (!CONSTANT_CLASS_P (value))
6816 {
6817 constant_p = false;
6818 break;
6819 }
e89be13b 6820
4038c495
GB
6821 if (constant_p)
6822 return build_vector_from_ctor (type,
6823 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6824 }
6825 }
6035d635 6826
ca085fd7
MLI
6827 if (warn_sequence_point)
6828 verify_sequence_points (inside_init);
6829
3e4093b6
RS
6830 /* Any type can be initialized
6831 from an expression of the same type, optionally with braces. */
400fbf9f 6832
3e4093b6
RS
6833 if (inside_init && TREE_TYPE (inside_init) != 0
6834 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6835 TYPE_MAIN_VARIANT (type))
3e4093b6 6836 || (code == ARRAY_TYPE
132da1a5 6837 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6838 || (code == VECTOR_TYPE
132da1a5 6839 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6840 || (code == POINTER_TYPE
3897f229 6841 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6842 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6843 TREE_TYPE (type)))))
3e4093b6
RS
6844 {
6845 if (code == POINTER_TYPE)
b494fd98 6846 {
b494fd98
EB
6847 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6848 {
f2a71bbc
JM
6849 if (TREE_CODE (inside_init) == STRING_CST
6850 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6851 inside_init = array_to_pointer_conversion
6852 (init_loc, inside_init);
f2a71bbc
JM
6853 else
6854 {
ea58ef42 6855 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
6856 return error_mark_node;
6857 }
b494fd98 6858 }
f2a71bbc 6859 }
b494fd98 6860
bae39a73
NS
6861 if (code == VECTOR_TYPE)
6862 /* Although the types are compatible, we may require a
6863 conversion. */
6864 inside_init = convert (type, inside_init);
3e4093b6 6865
ca58211b 6866 if (require_constant
3e4093b6 6867 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6868 {
3e4093b6
RS
6869 /* As an extension, allow initializing objects with static storage
6870 duration with compound literals (which are then treated just as
ca58211b
PB
6871 the brace enclosed list they contain). Also allow this for
6872 vectors, as we can only assign them with compound literals. */
7278465e
MP
6873 if (flag_isoc99 && code != VECTOR_TYPE)
6874 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6875 "is not constant");
3e4093b6
RS
6876 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6877 inside_init = DECL_INITIAL (decl);
400fbf9f 6878 }
3e4093b6
RS
6879
6880 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6881 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6882 {
ea58ef42
MP
6883 error_init (init_loc, "array initialized from non-constant array "
6884 "expression");
3e4093b6 6885 return error_mark_node;
400fbf9f 6886 }
400fbf9f 6887
c1771a20 6888 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6889 -pedantic-errors is specified. In the later case, we always want
6890 an error. In the former case, we simply want a warning. */
6891 if (require_constant && pedantic
6892 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6893 {
6894 inside_init
6895 = valid_compound_expr_initializer (inside_init,
6896 TREE_TYPE (inside_init));
6897 if (inside_init == error_mark_node)
ea58ef42 6898 error_init (init_loc, "initializer element is not constant");
2f6e4e97 6899 else
c1771a20 6900 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6901 "initializer element is not constant");
3e4093b6
RS
6902 if (flag_pedantic_errors)
6903 inside_init = error_mark_node;
6904 }
6905 else if (require_constant
116df786
RH
6906 && !initializer_constant_valid_p (inside_init,
6907 TREE_TYPE (inside_init)))
3e4093b6 6908 {
ea58ef42 6909 error_init (init_loc, "initializer element is not constant");
3e4093b6 6910 inside_init = error_mark_node;
8b40563c 6911 }
928c19bb 6912 else if (require_constant && !maybe_const)
3aa3c9fc 6913 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 6914 "initializer element is not a constant expression");
f735a153 6915
90137d8f 6916 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6917 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
6918 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6919 type, inside_init, origtype,
bbbbb16a 6920 ic_init, null_pointer_constant,
928c19bb 6921 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6922 return inside_init;
6923 }
f735a153 6924
3e4093b6 6925 /* Handle scalar types, including conversions. */
400fbf9f 6926
ab22c1fa
CF
6927 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6928 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6929 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6930 {
f2a71bbc
JM
6931 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6932 && (TREE_CODE (init) == STRING_CST
6933 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6934 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6935 if (semantic_type)
6936 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6937 inside_init);
3e4093b6 6938 inside_init
68fca595
MP
6939 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6940 inside_init, origtype, ic_init,
6941 null_pointer_constant, NULL_TREE, NULL_TREE,
6942 0);
2f6e4e97 6943
3274deff
JW
6944 /* Check to see if we have already given an error message. */
6945 if (inside_init == error_mark_node)
6946 ;
3f75a254 6947 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6948 {
ea58ef42 6949 error_init (init_loc, "initializer element is not constant");
3e4093b6 6950 inside_init = error_mark_node;
400fbf9f 6951 }
3e4093b6 6952 else if (require_constant
116df786
RH
6953 && !initializer_constant_valid_p (inside_init,
6954 TREE_TYPE (inside_init)))
400fbf9f 6955 {
ea58ef42
MP
6956 error_init (init_loc, "initializer element is not computable at "
6957 "load time");
3e4093b6 6958 inside_init = error_mark_node;
400fbf9f 6959 }
928c19bb 6960 else if (require_constant && !maybe_const)
451b5e48 6961 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 6962 "initializer element is not a constant expression");
3e4093b6
RS
6963
6964 return inside_init;
400fbf9f 6965 }
d9fc6069 6966
3e4093b6 6967 /* Come here only for records and arrays. */
d9fc6069 6968
3e4093b6 6969 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6970 {
ea58ef42 6971 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 6972 return error_mark_node;
d9fc6069 6973 }
3e4093b6 6974
ea58ef42 6975 error_init (init_loc, "invalid initializer");
3e4093b6 6976 return error_mark_node;
d9fc6069 6977}
400fbf9f 6978\f
3e4093b6 6979/* Handle initializers that use braces. */
400fbf9f 6980
3e4093b6
RS
6981/* Type of object we are accumulating a constructor for.
6982 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6983static tree constructor_type;
400fbf9f 6984
3e4093b6
RS
6985/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6986 left to fill. */
6987static tree constructor_fields;
400fbf9f 6988
3e4093b6
RS
6989/* For an ARRAY_TYPE, this is the specified index
6990 at which to store the next element we get. */
6991static tree constructor_index;
400fbf9f 6992
3e4093b6
RS
6993/* For an ARRAY_TYPE, this is the maximum index. */
6994static tree constructor_max_index;
400fbf9f 6995
3e4093b6
RS
6996/* For a RECORD_TYPE, this is the first field not yet written out. */
6997static tree constructor_unfilled_fields;
400fbf9f 6998
3e4093b6
RS
6999/* For an ARRAY_TYPE, this is the index of the first element
7000 not yet written out. */
7001static tree constructor_unfilled_index;
895ea614 7002
3e4093b6
RS
7003/* In a RECORD_TYPE, the byte index of the next consecutive field.
7004 This is so we can generate gaps between fields, when appropriate. */
7005static tree constructor_bit_index;
10d5caec 7006
3e4093b6
RS
7007/* If we are saving up the elements rather than allocating them,
7008 this is the list of elements so far (in reverse order,
7009 most recent first). */
9771b263 7010static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 7011
3e4093b6
RS
7012/* 1 if constructor should be incrementally stored into a constructor chain,
7013 0 if all the elements should be kept in AVL tree. */
7014static int constructor_incremental;
ad47f1e5 7015
3e4093b6
RS
7016/* 1 if so far this constructor's elements are all compile-time constants. */
7017static int constructor_constant;
ad47f1e5 7018
3e4093b6
RS
7019/* 1 if so far this constructor's elements are all valid address constants. */
7020static int constructor_simple;
ad47f1e5 7021
928c19bb
JM
7022/* 1 if this constructor has an element that cannot be part of a
7023 constant expression. */
7024static int constructor_nonconst;
7025
3e4093b6
RS
7026/* 1 if this constructor is erroneous so far. */
7027static int constructor_erroneous;
d45cf215 7028
9bac5cbb
G
7029/* 1 if this constructor is the universal zero initializer { 0 }. */
7030static int constructor_zeroinit;
7031
3e4093b6
RS
7032/* Structure for managing pending initializer elements, organized as an
7033 AVL tree. */
d45cf215 7034
3e4093b6 7035struct init_node
d45cf215 7036{
3e4093b6
RS
7037 struct init_node *left, *right;
7038 struct init_node *parent;
7039 int balance;
7040 tree purpose;
7041 tree value;
bbbbb16a 7042 tree origtype;
d45cf215
RS
7043};
7044
3e4093b6
RS
7045/* Tree of pending elements at this constructor level.
7046 These are elements encountered out of order
7047 which belong at places we haven't reached yet in actually
7048 writing the output.
7049 Will never hold tree nodes across GC runs. */
7050static struct init_node *constructor_pending_elts;
d45cf215 7051
3e4093b6
RS
7052/* The SPELLING_DEPTH of this constructor. */
7053static int constructor_depth;
d45cf215 7054
3e4093b6
RS
7055/* DECL node for which an initializer is being read.
7056 0 means we are reading a constructor expression
7057 such as (struct foo) {...}. */
7058static tree constructor_decl;
d45cf215 7059
3e4093b6
RS
7060/* Nonzero if this is an initializer for a top-level decl. */
7061static int constructor_top_level;
d45cf215 7062
3e4093b6
RS
7063/* Nonzero if there were any member designators in this initializer. */
7064static int constructor_designated;
d45cf215 7065
3e4093b6
RS
7066/* Nesting depth of designator list. */
7067static int designator_depth;
d45cf215 7068
3e4093b6 7069/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7070static int designator_erroneous;
d45cf215 7071
3e4093b6
RS
7072\f
7073/* This stack has a level for each implicit or explicit level of
7074 structuring in the initializer, including the outermost one. It
7075 saves the values of most of the variables above. */
d45cf215 7076
3e4093b6
RS
7077struct constructor_range_stack;
7078
7079struct constructor_stack
d45cf215 7080{
3e4093b6
RS
7081 struct constructor_stack *next;
7082 tree type;
7083 tree fields;
7084 tree index;
7085 tree max_index;
7086 tree unfilled_index;
7087 tree unfilled_fields;
7088 tree bit_index;
9771b263 7089 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7090 struct init_node *pending_elts;
7091 int offset;
7092 int depth;
916c5919 7093 /* If value nonzero, this value should replace the entire
3e4093b6 7094 constructor at this level. */
916c5919 7095 struct c_expr replacement_value;
3e4093b6
RS
7096 struct constructor_range_stack *range_stack;
7097 char constant;
7098 char simple;
928c19bb 7099 char nonconst;
3e4093b6
RS
7100 char implicit;
7101 char erroneous;
7102 char outer;
7103 char incremental;
7104 char designated;
976d5a22 7105 int designator_depth;
3e4093b6 7106};
d45cf215 7107
802415d1 7108static struct constructor_stack *constructor_stack;
d45cf215 7109
3e4093b6
RS
7110/* This stack represents designators from some range designator up to
7111 the last designator in the list. */
d45cf215 7112
3e4093b6
RS
7113struct constructor_range_stack
7114{
7115 struct constructor_range_stack *next, *prev;
7116 struct constructor_stack *stack;
7117 tree range_start;
7118 tree index;
7119 tree range_end;
7120 tree fields;
7121};
d45cf215 7122
802415d1 7123static struct constructor_range_stack *constructor_range_stack;
d45cf215 7124
3e4093b6
RS
7125/* This stack records separate initializers that are nested.
7126 Nested initializers can't happen in ANSI C, but GNU C allows them
7127 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7128
3e4093b6 7129struct initializer_stack
d45cf215 7130{
3e4093b6
RS
7131 struct initializer_stack *next;
7132 tree decl;
3e4093b6
RS
7133 struct constructor_stack *constructor_stack;
7134 struct constructor_range_stack *constructor_range_stack;
9771b263 7135 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7136 struct spelling *spelling;
7137 struct spelling *spelling_base;
7138 int spelling_size;
7139 char top_level;
7140 char require_constant_value;
7141 char require_constant_elements;
7142};
d45cf215 7143
802415d1 7144static struct initializer_stack *initializer_stack;
3e4093b6
RS
7145\f
7146/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7147
7148void
a396f8ae 7149start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 7150{
3e4093b6 7151 const char *locus;
cceb1885 7152 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7153
3e4093b6 7154 p->decl = constructor_decl;
3e4093b6
RS
7155 p->require_constant_value = require_constant_value;
7156 p->require_constant_elements = require_constant_elements;
7157 p->constructor_stack = constructor_stack;
7158 p->constructor_range_stack = constructor_range_stack;
7159 p->elements = constructor_elements;
7160 p->spelling = spelling;
7161 p->spelling_base = spelling_base;
7162 p->spelling_size = spelling_size;
7163 p->top_level = constructor_top_level;
7164 p->next = initializer_stack;
7165 initializer_stack = p;
400fbf9f 7166
3e4093b6 7167 constructor_decl = decl;
3e4093b6
RS
7168 constructor_designated = 0;
7169 constructor_top_level = top_level;
400fbf9f 7170
6f17bbcf 7171 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7172 {
7173 require_constant_value = TREE_STATIC (decl);
7174 require_constant_elements
7175 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7176 /* For a scalar, you can always use any value to initialize,
7177 even within braces. */
296a8c2f 7178 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
88388a52 7179 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7180 }
7181 else
7182 {
7183 require_constant_value = 0;
7184 require_constant_elements = 0;
88388a52 7185 locus = _("(anonymous)");
3e4093b6 7186 }
b71c7f8a 7187
3e4093b6
RS
7188 constructor_stack = 0;
7189 constructor_range_stack = 0;
b71c7f8a 7190
9bac5cbb 7191 found_missing_braces = 0;
3e4093b6
RS
7192
7193 spelling_base = 0;
7194 spelling_size = 0;
7195 RESTORE_SPELLING_DEPTH (0);
7196
7197 if (locus)
7198 push_string (locus);
7199}
7200
7201void
7202finish_init (void)
b71c7f8a 7203{
3e4093b6 7204 struct initializer_stack *p = initializer_stack;
b71c7f8a 7205
3e4093b6
RS
7206 /* Free the whole constructor stack of this initializer. */
7207 while (constructor_stack)
7208 {
7209 struct constructor_stack *q = constructor_stack;
7210 constructor_stack = q->next;
7211 free (q);
7212 }
7213
366de0ce 7214 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7215
7216 /* Pop back to the data of the outer initializer (if any). */
36579663 7217 free (spelling_base);
3aeb3655 7218
3e4093b6 7219 constructor_decl = p->decl;
3e4093b6
RS
7220 require_constant_value = p->require_constant_value;
7221 require_constant_elements = p->require_constant_elements;
7222 constructor_stack = p->constructor_stack;
7223 constructor_range_stack = p->constructor_range_stack;
7224 constructor_elements = p->elements;
7225 spelling = p->spelling;
7226 spelling_base = p->spelling_base;
7227 spelling_size = p->spelling_size;
7228 constructor_top_level = p->top_level;
7229 initializer_stack = p->next;
7230 free (p);
b71c7f8a 7231}
400fbf9f 7232\f
3e4093b6
RS
7233/* Call here when we see the initializer is surrounded by braces.
7234 This is instead of a call to push_init_level;
7235 it is matched by a call to pop_init_level.
400fbf9f 7236
3e4093b6
RS
7237 TYPE is the type to initialize, for a constructor expression.
7238 For an initializer for a decl, TYPE is zero. */
400fbf9f 7239
3e4093b6
RS
7240void
7241really_start_incremental_init (tree type)
400fbf9f 7242{
5d038c4c 7243 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7244
3e4093b6
RS
7245 if (type == 0)
7246 type = TREE_TYPE (constructor_decl);
400fbf9f 7247
31521951 7248 if (VECTOR_TYPE_P (type)
b6fc2cdb 7249 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7250 error ("opaque vector types cannot be initialized");
400fbf9f 7251
3e4093b6
RS
7252 p->type = constructor_type;
7253 p->fields = constructor_fields;
7254 p->index = constructor_index;
7255 p->max_index = constructor_max_index;
7256 p->unfilled_index = constructor_unfilled_index;
7257 p->unfilled_fields = constructor_unfilled_fields;
7258 p->bit_index = constructor_bit_index;
7259 p->elements = constructor_elements;
7260 p->constant = constructor_constant;
7261 p->simple = constructor_simple;
928c19bb 7262 p->nonconst = constructor_nonconst;
3e4093b6
RS
7263 p->erroneous = constructor_erroneous;
7264 p->pending_elts = constructor_pending_elts;
7265 p->depth = constructor_depth;
916c5919
JM
7266 p->replacement_value.value = 0;
7267 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7268 p->replacement_value.original_type = NULL;
3e4093b6
RS
7269 p->implicit = 0;
7270 p->range_stack = 0;
7271 p->outer = 0;
7272 p->incremental = constructor_incremental;
7273 p->designated = constructor_designated;
976d5a22 7274 p->designator_depth = designator_depth;
3e4093b6
RS
7275 p->next = 0;
7276 constructor_stack = p;
b13aca19 7277
3e4093b6
RS
7278 constructor_constant = 1;
7279 constructor_simple = 1;
928c19bb 7280 constructor_nonconst = 0;
3e4093b6 7281 constructor_depth = SPELLING_DEPTH ();
9771b263 7282 constructor_elements = NULL;
3e4093b6
RS
7283 constructor_pending_elts = 0;
7284 constructor_type = type;
7285 constructor_incremental = 1;
7286 constructor_designated = 0;
9bac5cbb 7287 constructor_zeroinit = 1;
3e4093b6 7288 designator_depth = 0;
b06df647 7289 designator_erroneous = 0;
400fbf9f 7290
3e4093b6
RS
7291 if (TREE_CODE (constructor_type) == RECORD_TYPE
7292 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7293 {
3e4093b6
RS
7294 constructor_fields = TYPE_FIELDS (constructor_type);
7295 /* Skip any nameless bit fields at the beginning. */
7296 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7297 && DECL_NAME (constructor_fields) == 0)
910ad8de 7298 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7299
3e4093b6
RS
7300 constructor_unfilled_fields = constructor_fields;
7301 constructor_bit_index = bitsize_zero_node;
400fbf9f 7302 }
3e4093b6
RS
7303 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7304 {
7305 if (TYPE_DOMAIN (constructor_type))
7306 {
7307 constructor_max_index
7308 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7309
3e4093b6
RS
7310 /* Detect non-empty initializations of zero-length arrays. */
7311 if (constructor_max_index == NULL_TREE
7312 && TYPE_SIZE (constructor_type))
9a9d280e 7313 constructor_max_index = integer_minus_one_node;
400fbf9f 7314
3e4093b6
RS
7315 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7316 to initialize VLAs will cause a proper error; avoid tree
7317 checking errors as well by setting a safe value. */
7318 if (constructor_max_index
7319 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7320 constructor_max_index = integer_minus_one_node;
59c83dbf 7321
3e4093b6
RS
7322 constructor_index
7323 = convert (bitsizetype,
7324 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7325 }
3e4093b6 7326 else
493179da
JM
7327 {
7328 constructor_index = bitsize_zero_node;
7329 constructor_max_index = NULL_TREE;
7330 }
59c83dbf 7331
3e4093b6
RS
7332 constructor_unfilled_index = constructor_index;
7333 }
31521951 7334 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7335 {
7336 /* Vectors are like simple fixed-size arrays. */
7337 constructor_max_index =
c62c040f 7338 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7339 constructor_index = bitsize_zero_node;
3e4093b6
RS
7340 constructor_unfilled_index = constructor_index;
7341 }
7342 else
7343 {
7344 /* Handle the case of int x = {5}; */
7345 constructor_fields = constructor_type;
7346 constructor_unfilled_fields = constructor_type;
7347 }
7348}
7349\f
7350/* Push down into a subobject, for initialization.
7351 If this is for an explicit set of braces, IMPLICIT is 0.
7352 If it is because the next element belongs at a lower level,
7353 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7354
3e4093b6 7355void
ea58ef42
MP
7356push_init_level (location_t loc, int implicit,
7357 struct obstack *braced_init_obstack)
3e4093b6
RS
7358{
7359 struct constructor_stack *p;
7360 tree value = NULL_TREE;
400fbf9f 7361
3e4093b6 7362 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7363 pop them now. If implicit == 1, this will have been done in
7364 process_init_element; do not repeat it here because in the case
7365 of excess initializers for an empty aggregate this leads to an
7366 infinite cycle of popping a level and immediately recreating
7367 it. */
7368 if (implicit != 1)
3e4093b6 7369 {
472d98b4
JM
7370 while (constructor_stack->implicit)
7371 {
7372 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7373 || TREE_CODE (constructor_type) == UNION_TYPE)
7374 && constructor_fields == 0)
34cf811f 7375 process_init_element (input_location,
ea58ef42 7376 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7377 true, braced_init_obstack);
472d98b4
JM
7378 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7379 && constructor_max_index
7380 && tree_int_cst_lt (constructor_max_index,
7381 constructor_index))
34cf811f 7382 process_init_element (input_location,
ea58ef42 7383 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7384 true, braced_init_obstack);
472d98b4
JM
7385 else
7386 break;
7387 }
3e4093b6 7388 }
400fbf9f 7389
3e4093b6
RS
7390 /* Unless this is an explicit brace, we need to preserve previous
7391 content if any. */
7392 if (implicit)
7393 {
7394 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7395 || TREE_CODE (constructor_type) == UNION_TYPE)
7396 && constructor_fields)
a1e3b3d9 7397 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7398 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7399 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7400 }
7401
5d038c4c 7402 p = XNEW (struct constructor_stack);
3e4093b6
RS
7403 p->type = constructor_type;
7404 p->fields = constructor_fields;
7405 p->index = constructor_index;
7406 p->max_index = constructor_max_index;
7407 p->unfilled_index = constructor_unfilled_index;
7408 p->unfilled_fields = constructor_unfilled_fields;
7409 p->bit_index = constructor_bit_index;
7410 p->elements = constructor_elements;
7411 p->constant = constructor_constant;
7412 p->simple = constructor_simple;
928c19bb 7413 p->nonconst = constructor_nonconst;
3e4093b6
RS
7414 p->erroneous = constructor_erroneous;
7415 p->pending_elts = constructor_pending_elts;
7416 p->depth = constructor_depth;
916c5919
JM
7417 p->replacement_value.value = 0;
7418 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7419 p->replacement_value.original_type = NULL;
3e4093b6
RS
7420 p->implicit = implicit;
7421 p->outer = 0;
7422 p->incremental = constructor_incremental;
7423 p->designated = constructor_designated;
976d5a22 7424 p->designator_depth = designator_depth;
3e4093b6
RS
7425 p->next = constructor_stack;
7426 p->range_stack = 0;
7427 constructor_stack = p;
400fbf9f 7428
3e4093b6
RS
7429 constructor_constant = 1;
7430 constructor_simple = 1;
928c19bb 7431 constructor_nonconst = 0;
3e4093b6 7432 constructor_depth = SPELLING_DEPTH ();
9771b263 7433 constructor_elements = NULL;
3e4093b6
RS
7434 constructor_incremental = 1;
7435 constructor_designated = 0;
7436 constructor_pending_elts = 0;
7437 if (!implicit)
400fbf9f 7438 {
3e4093b6
RS
7439 p->range_stack = constructor_range_stack;
7440 constructor_range_stack = 0;
7441 designator_depth = 0;
b06df647 7442 designator_erroneous = 0;
3e4093b6 7443 }
400fbf9f 7444
3e4093b6
RS
7445 /* Don't die if an entire brace-pair level is superfluous
7446 in the containing level. */
7447 if (constructor_type == 0)
7448 ;
7449 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7450 || TREE_CODE (constructor_type) == UNION_TYPE)
7451 {
7452 /* Don't die if there are extra init elts at the end. */
7453 if (constructor_fields == 0)
7454 constructor_type = 0;
7455 else
400fbf9f 7456 {
3e4093b6
RS
7457 constructor_type = TREE_TYPE (constructor_fields);
7458 push_member_name (constructor_fields);
7459 constructor_depth++;
400fbf9f 7460 }
6a358dcb
MP
7461 /* If upper initializer is designated, then mark this as
7462 designated too to prevent bogus warnings. */
7463 constructor_designated = p->designated;
3e4093b6
RS
7464 }
7465 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7466 {
7467 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7468 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7469 constructor_depth++;
400fbf9f
JW
7470 }
7471
3e4093b6 7472 if (constructor_type == 0)
400fbf9f 7473 {
ea58ef42 7474 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7475 constructor_fields = 0;
7476 constructor_unfilled_fields = 0;
7477 return;
400fbf9f
JW
7478 }
7479
3e4093b6
RS
7480 if (value && TREE_CODE (value) == CONSTRUCTOR)
7481 {
7482 constructor_constant = TREE_CONSTANT (value);
7483 constructor_simple = TREE_STATIC (value);
928c19bb 7484 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7485 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7486 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7487 && (TREE_CODE (constructor_type) == RECORD_TYPE
7488 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7489 set_nonincremental_init (braced_init_obstack);
3e4093b6 7490 }
400fbf9f 7491
9bac5cbb
G
7492 if (implicit == 1)
7493 found_missing_braces = 1;
400fbf9f 7494
3e4093b6
RS
7495 if (TREE_CODE (constructor_type) == RECORD_TYPE
7496 || TREE_CODE (constructor_type) == UNION_TYPE)
7497 {
7498 constructor_fields = TYPE_FIELDS (constructor_type);
7499 /* Skip any nameless bit fields at the beginning. */
7500 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7501 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7502 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7503
3e4093b6
RS
7504 constructor_unfilled_fields = constructor_fields;
7505 constructor_bit_index = bitsize_zero_node;
7506 }
31521951 7507 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7508 {
7509 /* Vectors are like simple fixed-size arrays. */
7510 constructor_max_index =
c62c040f
RG
7511 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7512 constructor_index = bitsize_int (0);
3e4093b6
RS
7513 constructor_unfilled_index = constructor_index;
7514 }
7515 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7516 {
7517 if (TYPE_DOMAIN (constructor_type))
7518 {
7519 constructor_max_index
7520 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7521
3e4093b6
RS
7522 /* Detect non-empty initializations of zero-length arrays. */
7523 if (constructor_max_index == NULL_TREE
7524 && TYPE_SIZE (constructor_type))
9a9d280e 7525 constructor_max_index = integer_minus_one_node;
de520661 7526
3e4093b6
RS
7527 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7528 to initialize VLAs will cause a proper error; avoid tree
7529 checking errors as well by setting a safe value. */
7530 if (constructor_max_index
7531 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7532 constructor_max_index = integer_minus_one_node;
b62acd60 7533
3e4093b6
RS
7534 constructor_index
7535 = convert (bitsizetype,
7536 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7537 }
7538 else
7539 constructor_index = bitsize_zero_node;
de520661 7540
3e4093b6
RS
7541 constructor_unfilled_index = constructor_index;
7542 if (value && TREE_CODE (value) == STRING_CST)
7543 {
7544 /* We need to split the char/wchar array into individual
7545 characters, so that we don't have to special case it
7546 everywhere. */
a1e3b3d9 7547 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7548 }
7549 }
7550 else
7551 {
b4519d39 7552 if (constructor_type != error_mark_node)
96b40f8d 7553 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7554 constructor_fields = constructor_type;
7555 constructor_unfilled_fields = constructor_type;
7556 }
7557}
8b6a5902 7558
3e4093b6 7559/* At the end of an implicit or explicit brace level,
916c5919
JM
7560 finish up that level of constructor. If a single expression
7561 with redundant braces initialized that level, return the
7562 c_expr structure for that expression. Otherwise, the original_code
7563 element is set to ERROR_MARK.
7564 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7565 from inner levels (process_init_element ignores that),
916c5919 7566 but return error_mark_node as the value from the outermost level
3e4093b6 7567 (that's what we want to put in DECL_INITIAL).
916c5919 7568 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7569
916c5919 7570struct c_expr
ea58ef42
MP
7571pop_init_level (location_t loc, int implicit,
7572 struct obstack *braced_init_obstack)
3e4093b6
RS
7573{
7574 struct constructor_stack *p;
916c5919
JM
7575 struct c_expr ret;
7576 ret.value = 0;
7577 ret.original_code = ERROR_MARK;
6866c6e8 7578 ret.original_type = NULL;
de520661 7579
3e4093b6
RS
7580 if (implicit == 0)
7581 {
7582 /* When we come to an explicit close brace,
7583 pop any inner levels that didn't have explicit braces. */
7584 while (constructor_stack->implicit)
34cf811f 7585 process_init_element (input_location,
ea58ef42 7586 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7587 true, braced_init_obstack);
366de0ce 7588 gcc_assert (!constructor_range_stack);
3e4093b6 7589 }
e5e809f4 7590
0066ef9c
RH
7591 /* Now output all pending elements. */
7592 constructor_incremental = 1;
a1e3b3d9 7593 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7594
3e4093b6 7595 p = constructor_stack;
e5e809f4 7596
3e4093b6
RS
7597 /* Error for initializing a flexible array member, or a zero-length
7598 array member in an inappropriate context. */
7599 if (constructor_type && constructor_fields
7600 && TREE_CODE (constructor_type) == ARRAY_TYPE
7601 && TYPE_DOMAIN (constructor_type)
3f75a254 7602 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7603 {
7604 /* Silently discard empty initializations. The parser will
7605 already have pedwarned for empty brackets. */
7606 if (integer_zerop (constructor_unfilled_index))
7607 constructor_type = NULL_TREE;
366de0ce 7608 else
3e4093b6 7609 {
366de0ce 7610 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7611
3e4093b6 7612 if (constructor_depth > 2)
ea58ef42 7613 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7614 else
d033409e 7615 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7616 "initialization of a flexible array member");
de520661 7617
3e4093b6
RS
7618 /* We have already issued an error message for the existence
7619 of a flexible array member not at the end of the structure.
535a42b1 7620 Discard the initializer so that we do not die later. */
910ad8de 7621 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7622 constructor_type = NULL_TREE;
7623 }
3e4093b6 7624 }
de520661 7625
7b33f0c8
MP
7626 switch (vec_safe_length (constructor_elements))
7627 {
7628 case 0:
7629 /* Initialization with { } counts as zeroinit. */
7630 constructor_zeroinit = 1;
7631 break;
7632 case 1:
7633 /* This might be zeroinit as well. */
7634 if (integer_zerop ((*constructor_elements)[0].value))
7635 constructor_zeroinit = 1;
7636 break;
7637 default:
7638 /* If the constructor has more than one element, it can't be { 0 }. */
7639 constructor_zeroinit = 0;
7640 break;
7641 }
9bac5cbb
G
7642
7643 /* Warn when some structs are initialized with direct aggregation. */
7644 if (!implicit && found_missing_braces && warn_missing_braces
7645 && !constructor_zeroinit)
7b33f0c8
MP
7646 warning_init (loc, OPT_Wmissing_braces,
7647 "missing braces around initializer");
9bac5cbb 7648
3e4093b6 7649 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7650 if (warn_missing_field_initializers
3e4093b6
RS
7651 && constructor_type
7652 && TREE_CODE (constructor_type) == RECORD_TYPE
7653 && constructor_unfilled_fields)
7654 {
7655 /* Do not warn for flexible array members or zero-length arrays. */
7656 while (constructor_unfilled_fields
3f75a254 7657 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7658 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7659 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7660
49819fef
AM
7661 if (constructor_unfilled_fields
7662 /* Do not warn if this level of the initializer uses member
7663 designators; it is likely to be deliberate. */
7664 && !constructor_designated
84937de2 7665 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 7666 && !constructor_zeroinit)
3e4093b6 7667 {
32397f22
MLI
7668 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7669 "missing initializer for field %qD of %qT",
7670 constructor_unfilled_fields,
7671 constructor_type))
7672 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7673 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7674 }
7675 }
de520661 7676
3e4093b6 7677 /* Pad out the end of the structure. */
916c5919 7678 if (p->replacement_value.value)
3e4093b6
RS
7679 /* If this closes a superfluous brace pair,
7680 just pass out the element between them. */
916c5919 7681 ret = p->replacement_value;
3e4093b6
RS
7682 else if (constructor_type == 0)
7683 ;
7684 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7685 && TREE_CODE (constructor_type) != UNION_TYPE
7686 && TREE_CODE (constructor_type) != ARRAY_TYPE
31521951 7687 && !VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7688 {
7689 /* A nonincremental scalar initializer--just return
7690 the element, after verifying there is just one. */
9771b263 7691 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7692 {
7693 if (!constructor_erroneous)
ea58ef42 7694 error_init (loc, "empty scalar initializer");
916c5919 7695 ret.value = error_mark_node;
3e4093b6 7696 }
9771b263 7697 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 7698 {
ea58ef42 7699 error_init (loc, "extra elements in scalar initializer");
9771b263 7700 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7701 }
7702 else
9771b263 7703 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7704 }
7705 else
7706 {
7707 if (constructor_erroneous)
916c5919 7708 ret.value = error_mark_node;
3e4093b6
RS
7709 else
7710 {
916c5919 7711 ret.value = build_constructor (constructor_type,
4038c495 7712 constructor_elements);
3e4093b6 7713 if (constructor_constant)
51eed280 7714 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7715 if (constructor_constant && constructor_simple)
916c5919 7716 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7717 if (constructor_nonconst)
7718 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7719 }
7720 }
de520661 7721
928c19bb
JM
7722 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7723 {
7724 if (constructor_nonconst)
7725 ret.original_code = C_MAYBE_CONST_EXPR;
7726 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7727 ret.original_code = ERROR_MARK;
7728 }
7729
3e4093b6
RS
7730 constructor_type = p->type;
7731 constructor_fields = p->fields;
7732 constructor_index = p->index;
7733 constructor_max_index = p->max_index;
7734 constructor_unfilled_index = p->unfilled_index;
7735 constructor_unfilled_fields = p->unfilled_fields;
7736 constructor_bit_index = p->bit_index;
7737 constructor_elements = p->elements;
7738 constructor_constant = p->constant;
7739 constructor_simple = p->simple;
928c19bb 7740 constructor_nonconst = p->nonconst;
3e4093b6
RS
7741 constructor_erroneous = p->erroneous;
7742 constructor_incremental = p->incremental;
7743 constructor_designated = p->designated;
976d5a22 7744 designator_depth = p->designator_depth;
3e4093b6
RS
7745 constructor_pending_elts = p->pending_elts;
7746 constructor_depth = p->depth;
7747 if (!p->implicit)
7748 constructor_range_stack = p->range_stack;
7749 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7750
3e4093b6
RS
7751 constructor_stack = p->next;
7752 free (p);
b621a4dd 7753
5d5e98dc
VR
7754 if (ret.value == 0 && constructor_stack == 0)
7755 ret.value = error_mark_node;
916c5919 7756 return ret;
3e4093b6 7757}
8b6a5902 7758
3e4093b6
RS
7759/* Common handling for both array range and field name designators.
7760 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7761
3e4093b6 7762static int
ea58ef42
MP
7763set_designator (location_t loc, int array,
7764 struct obstack *braced_init_obstack)
de520661 7765{
3e4093b6
RS
7766 tree subtype;
7767 enum tree_code subcode;
de520661 7768
3e4093b6
RS
7769 /* Don't die if an entire brace-pair level is superfluous
7770 in the containing level. */
7771 if (constructor_type == 0)
7772 return 1;
de520661 7773
366de0ce
NS
7774 /* If there were errors in this designator list already, bail out
7775 silently. */
b06df647 7776 if (designator_erroneous)
3e4093b6 7777 return 1;
e28cae4f 7778
3e4093b6
RS
7779 if (!designator_depth)
7780 {
366de0ce 7781 gcc_assert (!constructor_range_stack);
de520661 7782
3e4093b6
RS
7783 /* Designator list starts at the level of closest explicit
7784 braces. */
7785 while (constructor_stack->implicit)
34cf811f 7786 process_init_element (input_location,
ea58ef42 7787 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7788 true, braced_init_obstack);
3e4093b6
RS
7789 constructor_designated = 1;
7790 return 0;
7791 }
de520661 7792
366de0ce 7793 switch (TREE_CODE (constructor_type))
3c3fa147 7794 {
366de0ce
NS
7795 case RECORD_TYPE:
7796 case UNION_TYPE:
3e4093b6
RS
7797 subtype = TREE_TYPE (constructor_fields);
7798 if (subtype != error_mark_node)
7799 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7800 break;
7801 case ARRAY_TYPE:
3e4093b6 7802 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7803 break;
7804 default:
7805 gcc_unreachable ();
de520661 7806 }
400fbf9f 7807
3e4093b6
RS
7808 subcode = TREE_CODE (subtype);
7809 if (array && subcode != ARRAY_TYPE)
7810 {
ea58ef42 7811 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
7812 return 1;
7813 }
7814 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7815 {
ea58ef42 7816 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
7817 return 1;
7818 }
d45cf215 7819
3e4093b6 7820 constructor_designated = 1;
ea58ef42 7821 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 7822 return 0;
de520661 7823}
400fbf9f 7824
3e4093b6
RS
7825/* If there are range designators in designator list, push a new designator
7826 to constructor_range_stack. RANGE_END is end of such stack range or
7827 NULL_TREE if there is no range designator at this level. */
400fbf9f 7828
3e4093b6 7829static void
a1e3b3d9 7830push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7831{
7832 struct constructor_range_stack *p;
400fbf9f 7833
a1e3b3d9
LB
7834 p = (struct constructor_range_stack *)
7835 obstack_alloc (braced_init_obstack,
7836 sizeof (struct constructor_range_stack));
3e4093b6
RS
7837 p->prev = constructor_range_stack;
7838 p->next = 0;
7839 p->fields = constructor_fields;
7840 p->range_start = constructor_index;
7841 p->index = constructor_index;
7842 p->stack = constructor_stack;
7843 p->range_end = range_end;
8b6a5902 7844 if (constructor_range_stack)
3e4093b6
RS
7845 constructor_range_stack->next = p;
7846 constructor_range_stack = p;
de520661 7847}
400fbf9f 7848
3e4093b6
RS
7849/* Within an array initializer, specify the next index to be initialized.
7850 FIRST is that index. If LAST is nonzero, then initialize a range
7851 of indices, running from FIRST through LAST. */
5a7ec9d9 7852
de520661 7853void
ea58ef42 7854set_init_index (location_t loc, tree first, tree last,
d033409e 7855 struct obstack *braced_init_obstack)
de520661 7856{
ea58ef42 7857 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 7858 return;
de520661 7859
b06df647 7860 designator_erroneous = 1;
de520661 7861
3ea8cd06
JM
7862 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7863 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7864 {
ea58ef42 7865 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
7866 return;
7867 }
7868
2b6da65c
JM
7869 if (TREE_CODE (first) != INTEGER_CST)
7870 {
7871 first = c_fully_fold (first, false, NULL);
7872 if (TREE_CODE (first) == INTEGER_CST)
d033409e 7873 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7874 "array index in initializer is not "
7875 "an integer constant expression");
7876 }
7877
7878 if (last && TREE_CODE (last) != INTEGER_CST)
7879 {
7880 last = c_fully_fold (last, false, NULL);
7881 if (TREE_CODE (last) == INTEGER_CST)
d033409e 7882 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7883 "array index in initializer is not "
7884 "an integer constant expression");
7885 }
7886
3e4093b6 7887 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 7888 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7889 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 7890 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7891 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 7892 error_init (loc, "array index in non-array initializer");
622adc7e 7893 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 7894 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
7895 else if (constructor_max_index
7896 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 7897 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 7898 else
de520661 7899 {
928c19bb
JM
7900 constant_expression_warning (first);
7901 if (last)
7902 constant_expression_warning (last);
3e4093b6 7903 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7904 if (tree_int_cst_lt (constructor_index, first))
7905 {
7906 constructor_index = copy_node (constructor_index);
7907 TREE_OVERFLOW (constructor_index) = 1;
7908 }
665f2503 7909
3e4093b6 7910 if (last)
2bede729 7911 {
3e4093b6
RS
7912 if (tree_int_cst_equal (first, last))
7913 last = 0;
7914 else if (tree_int_cst_lt (last, first))
7915 {
ea58ef42 7916 error_init (loc, "empty index range in initializer");
3e4093b6
RS
7917 last = 0;
7918 }
7919 else
7920 {
7921 last = convert (bitsizetype, last);
7922 if (constructor_max_index != 0
7923 && tree_int_cst_lt (constructor_max_index, last))
7924 {
ea58ef42
MP
7925 error_init (loc, "array index range in initializer exceeds "
7926 "array bounds");
3e4093b6
RS
7927 last = 0;
7928 }
7929 }
2bede729 7930 }
fed3cef0 7931
3e4093b6 7932 designator_depth++;
b06df647 7933 designator_erroneous = 0;
3e4093b6 7934 if (constructor_range_stack || last)
a1e3b3d9 7935 push_range_stack (last, braced_init_obstack);
de520661 7936 }
de520661 7937}
3e4093b6
RS
7938
7939/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7940
de520661 7941void
ea58ef42
MP
7942set_init_label (location_t loc, tree fieldname,
7943 struct obstack *braced_init_obstack)
de520661 7944{
0fb96aa4 7945 tree field;
94ba5069 7946
ea58ef42 7947 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
7948 return;
7949
b06df647 7950 designator_erroneous = 1;
3e4093b6
RS
7951
7952 if (TREE_CODE (constructor_type) != RECORD_TYPE
7953 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7954 {
ea58ef42 7955 error_init (loc, "field name not in record or union initializer");
3e4093b6 7956 return;
94ba5069
RS
7957 }
7958
0fb96aa4 7959 field = lookup_field (constructor_type, fieldname);
8b6a5902 7960
0fb96aa4 7961 if (field == 0)
9babc352 7962 error_at (loc, "unknown field %qE specified in initializer", fieldname);
3e4093b6 7963 else
0fb96aa4
JM
7964 do
7965 {
7966 constructor_fields = TREE_VALUE (field);
7967 designator_depth++;
7968 designator_erroneous = 0;
7969 if (constructor_range_stack)
7970 push_range_stack (NULL_TREE, braced_init_obstack);
7971 field = TREE_CHAIN (field);
7972 if (field)
7973 {
ea58ef42 7974 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
7975 return;
7976 }
7977 }
7978 while (field != NULL_TREE);
3e4093b6
RS
7979}
7980\f
7981/* Add a new initializer to the tree of pending initializers. PURPOSE
7982 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7983 VALUE is the value of that index or field. If ORIGTYPE is not
7984 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7985
7986 IMPLICIT is true if value comes from pop_init_level (1),
7987 the new initializer has been merged with the existing one
7988 and thus no warnings should be emitted about overriding an
7989 existing initializer. */
de520661 7990
3e4093b6 7991static void
96b40f8d
MP
7992add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7993 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
7994{
7995 struct init_node *p, **q, *r;
7996
7997 q = &constructor_pending_elts;
7998 p = 0;
7999
8000 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8001 {
3e4093b6 8002 while (*q != 0)
91fa3c30 8003 {
3e4093b6
RS
8004 p = *q;
8005 if (tree_int_cst_lt (purpose, p->purpose))
8006 q = &p->left;
8007 else if (tree_int_cst_lt (p->purpose, purpose))
8008 q = &p->right;
8009 else
8010 {
b295aee2
JJ
8011 if (!implicit)
8012 {
8013 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8014 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8015 "initialized field with side-effects "
8016 "overwritten");
b295aee2 8017 else if (warn_override_init)
96b40f8d
MP
8018 warning_init (loc, OPT_Woverride_init,
8019 "initialized field overwritten");
b295aee2 8020 }
3e4093b6 8021 p->value = value;
bbbbb16a 8022 p->origtype = origtype;
3e4093b6
RS
8023 return;
8024 }
91fa3c30 8025 }
de520661 8026 }
3e4093b6 8027 else
de520661 8028 {
3e4093b6 8029 tree bitpos;
400fbf9f 8030
3e4093b6
RS
8031 bitpos = bit_position (purpose);
8032 while (*q != NULL)
8033 {
8034 p = *q;
8035 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8036 q = &p->left;
8037 else if (p->purpose != purpose)
8038 q = &p->right;
8039 else
8040 {
b295aee2
JJ
8041 if (!implicit)
8042 {
8043 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8044 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8045 "initialized field with side-effects "
8046 "overwritten");
b295aee2 8047 else if (warn_override_init)
96b40f8d
MP
8048 warning_init (loc, OPT_Woverride_init,
8049 "initialized field overwritten");
b295aee2 8050 }
3e4093b6 8051 p->value = value;
bbbbb16a 8052 p->origtype = origtype;
3e4093b6
RS
8053 return;
8054 }
8055 }
91fa3c30 8056 }
b71c7f8a 8057
a1e3b3d9
LB
8058 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8059 sizeof (struct init_node));
3e4093b6
RS
8060 r->purpose = purpose;
8061 r->value = value;
bbbbb16a 8062 r->origtype = origtype;
8b6a5902 8063
3e4093b6
RS
8064 *q = r;
8065 r->parent = p;
8066 r->left = 0;
8067 r->right = 0;
8068 r->balance = 0;
b71c7f8a 8069
3e4093b6 8070 while (p)
de520661 8071 {
3e4093b6 8072 struct init_node *s;
665f2503 8073
3e4093b6 8074 if (r == p->left)
2bede729 8075 {
3e4093b6
RS
8076 if (p->balance == 0)
8077 p->balance = -1;
8078 else if (p->balance < 0)
8079 {
8080 if (r->balance < 0)
8081 {
8082 /* L rotation. */
8083 p->left = r->right;
8084 if (p->left)
8085 p->left->parent = p;
8086 r->right = p;
e7b6a0ee 8087
3e4093b6
RS
8088 p->balance = 0;
8089 r->balance = 0;
39bc99c2 8090
3e4093b6
RS
8091 s = p->parent;
8092 p->parent = r;
8093 r->parent = s;
8094 if (s)
8095 {
8096 if (s->left == p)
8097 s->left = r;
8098 else
8099 s->right = r;
8100 }
8101 else
8102 constructor_pending_elts = r;
8103 }
8104 else
8105 {
8106 /* LR rotation. */
8107 struct init_node *t = r->right;
e7b6a0ee 8108
3e4093b6
RS
8109 r->right = t->left;
8110 if (r->right)
8111 r->right->parent = r;
8112 t->left = r;
8113
8114 p->left = t->right;
8115 if (p->left)
8116 p->left->parent = p;
8117 t->right = p;
8118
8119 p->balance = t->balance < 0;
8120 r->balance = -(t->balance > 0);
8121 t->balance = 0;
8122
8123 s = p->parent;
8124 p->parent = t;
8125 r->parent = t;
8126 t->parent = s;
8127 if (s)
8128 {
8129 if (s->left == p)
8130 s->left = t;
8131 else
8132 s->right = t;
8133 }
8134 else
8135 constructor_pending_elts = t;
8136 }
8137 break;
8138 }
8139 else
8140 {
8141 /* p->balance == +1; growth of left side balances the node. */
8142 p->balance = 0;
8143 break;
8144 }
2bede729 8145 }
3e4093b6
RS
8146 else /* r == p->right */
8147 {
8148 if (p->balance == 0)
8149 /* Growth propagation from right side. */
8150 p->balance++;
8151 else if (p->balance > 0)
8152 {
8153 if (r->balance > 0)
8154 {
8155 /* R rotation. */
8156 p->right = r->left;
8157 if (p->right)
8158 p->right->parent = p;
8159 r->left = p;
8160
8161 p->balance = 0;
8162 r->balance = 0;
8163
8164 s = p->parent;
8165 p->parent = r;
8166 r->parent = s;
8167 if (s)
8168 {
8169 if (s->left == p)
8170 s->left = r;
8171 else
8172 s->right = r;
8173 }
8174 else
8175 constructor_pending_elts = r;
8176 }
8177 else /* r->balance == -1 */
8178 {
8179 /* RL rotation */
8180 struct init_node *t = r->left;
8181
8182 r->left = t->right;
8183 if (r->left)
8184 r->left->parent = r;
8185 t->right = r;
8186
8187 p->right = t->left;
8188 if (p->right)
8189 p->right->parent = p;
8190 t->left = p;
8191
8192 r->balance = (t->balance < 0);
8193 p->balance = -(t->balance > 0);
8194 t->balance = 0;
8195
8196 s = p->parent;
8197 p->parent = t;
8198 r->parent = t;
8199 t->parent = s;
8200 if (s)
8201 {
8202 if (s->left == p)
8203 s->left = t;
8204 else
8205 s->right = t;
8206 }
8207 else
8208 constructor_pending_elts = t;
8209 }
8210 break;
8211 }
8212 else
8213 {
8214 /* p->balance == -1; growth of right side balances the node. */
8215 p->balance = 0;
8216 break;
8217 }
8218 }
8219
8220 r = p;
8221 p = p->parent;
8222 }
8223}
8224
8225/* Build AVL tree from a sorted chain. */
8226
8227static void
a1e3b3d9 8228set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8229{
4038c495
GB
8230 unsigned HOST_WIDE_INT ix;
8231 tree index, value;
3e4093b6
RS
8232
8233 if (TREE_CODE (constructor_type) != RECORD_TYPE
8234 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8235 return;
8236
4038c495 8237 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8238 add_pending_init (input_location, index, value, NULL_TREE, true,
8239 braced_init_obstack);
9771b263 8240 constructor_elements = NULL;
3e4093b6
RS
8241 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8242 {
8243 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8244 /* Skip any nameless bit fields at the beginning. */
8245 while (constructor_unfilled_fields != 0
8246 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8247 && DECL_NAME (constructor_unfilled_fields) == 0)
8248 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8249
de520661 8250 }
3e4093b6 8251 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8252 {
3e4093b6
RS
8253 if (TYPE_DOMAIN (constructor_type))
8254 constructor_unfilled_index
8255 = convert (bitsizetype,
8256 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8257 else
8258 constructor_unfilled_index = bitsize_zero_node;
de520661 8259 }
3e4093b6 8260 constructor_incremental = 0;
de520661 8261}
400fbf9f 8262
3e4093b6 8263/* Build AVL tree from a string constant. */
de520661 8264
3e4093b6 8265static void
a1e3b3d9
LB
8266set_nonincremental_init_from_string (tree str,
8267 struct obstack * braced_init_obstack)
de520661 8268{
3e4093b6
RS
8269 tree value, purpose, type;
8270 HOST_WIDE_INT val[2];
8271 const char *p, *end;
8272 int byte, wchar_bytes, charwidth, bitpos;
de520661 8273
366de0ce 8274 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8275
c466b2cd 8276 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8277 charwidth = TYPE_PRECISION (char_type_node);
8278 type = TREE_TYPE (constructor_type);
8279 p = TREE_STRING_POINTER (str);
8280 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8281
3e4093b6 8282 for (purpose = bitsize_zero_node;
8824edff
JJ
8283 p < end
8284 && !(constructor_max_index
8285 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8286 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8287 {
3e4093b6 8288 if (wchar_bytes == 1)
ffc5c6a9 8289 {
807e902e
KZ
8290 val[0] = (unsigned char) *p++;
8291 val[1] = 0;
ffc5c6a9
RH
8292 }
8293 else
3e4093b6 8294 {
3e4093b6 8295 val[1] = 0;
807e902e 8296 val[0] = 0;
3e4093b6
RS
8297 for (byte = 0; byte < wchar_bytes; byte++)
8298 {
8299 if (BYTES_BIG_ENDIAN)
8300 bitpos = (wchar_bytes - byte - 1) * charwidth;
8301 else
8302 bitpos = byte * charwidth;
807e902e 8303 val[bitpos % HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8304 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8305 << (bitpos % HOST_BITS_PER_WIDE_INT);
8306 }
8307 }
584ef5fe 8308
8df83eae 8309 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8310 {
8311 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8312 if (bitpos < HOST_BITS_PER_WIDE_INT)
8313 {
807e902e 8314 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
3e4093b6 8315 {
aa256c4a 8316 val[0] |= HOST_WIDE_INT_M1U << bitpos;
807e902e 8317 val[1] = -1;
3e4093b6
RS
8318 }
8319 }
8320 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8321 {
807e902e
KZ
8322 if (val[0] < 0)
8323 val[1] = -1;
3e4093b6 8324 }
807e902e 8325 else if (val[1] & (((HOST_WIDE_INT) 1)
3e4093b6 8326 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
aa256c4a 8327 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
3e4093b6 8328 }
ffc5c6a9 8329
807e902e
KZ
8330 value = wide_int_to_tree (type,
8331 wide_int::from_array (val, 2,
8332 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8333 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8334 braced_init_obstack);
9dfcc8db
BH
8335 }
8336
3e4093b6
RS
8337 constructor_incremental = 0;
8338}
de520661 8339
3e4093b6
RS
8340/* Return value of FIELD in pending initializer or zero if the field was
8341 not initialized yet. */
8342
8343static tree
a1e3b3d9 8344find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8345{
8346 struct init_node *p;
8347
8348 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8349 {
3e4093b6
RS
8350 if (constructor_incremental
8351 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8352 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8353
8354 p = constructor_pending_elts;
8355 while (p)
19d76e60 8356 {
3e4093b6
RS
8357 if (tree_int_cst_lt (field, p->purpose))
8358 p = p->left;
8359 else if (tree_int_cst_lt (p->purpose, field))
8360 p = p->right;
8361 else
8362 return p->value;
19d76e60 8363 }
19d76e60 8364 }
3e4093b6 8365 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8366 {
3e4093b6 8367 tree bitpos = bit_position (field);
de520661 8368
3e4093b6
RS
8369 if (constructor_incremental
8370 && (!constructor_unfilled_fields
8371 || tree_int_cst_lt (bitpos,
8372 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8373 set_nonincremental_init (braced_init_obstack);
de520661 8374
3e4093b6
RS
8375 p = constructor_pending_elts;
8376 while (p)
8377 {
8378 if (field == p->purpose)
8379 return p->value;
8380 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8381 p = p->left;
8382 else
8383 p = p->right;
8384 }
8385 }
8386 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8387 {
9771b263
DN
8388 if (!vec_safe_is_empty (constructor_elements)
8389 && (constructor_elements->last ().index == field))
8390 return constructor_elements->last ().value;
de520661 8391 }
3e4093b6 8392 return 0;
de520661
RS
8393}
8394
3e4093b6
RS
8395/* "Output" the next constructor element.
8396 At top level, really output it to assembler code now.
8397 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8398 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8399 TYPE is the data type that the containing data type wants here.
8400 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8401 If VALUE is a string constant, STRICT_STRING is true if it is
8402 unparenthesized or we should not warn here for it being parenthesized.
8403 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8404
3e4093b6
RS
8405 PENDING if non-nil means output pending elements that belong
8406 right after this element. (PENDING is normally 1;
b295aee2
JJ
8407 it is 0 while outputting pending elements, to avoid recursion.)
8408
8409 IMPLICIT is true if value comes from pop_init_level (1),
8410 the new initializer has been merged with the existing one
8411 and thus no warnings should be emitted about overriding an
8412 existing initializer. */
8b6a5902 8413
3e4093b6 8414static void
34cf811f
MP
8415output_init_element (location_t loc, tree value, tree origtype,
8416 bool strict_string, tree type, tree field, int pending,
8417 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8418{
8ce94e44 8419 tree semantic_type = NULL_TREE;
928c19bb
JM
8420 bool maybe_const = true;
8421 bool npc;
4038c495 8422
0a880880 8423 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8424 {
3e4093b6
RS
8425 constructor_erroneous = 1;
8426 return;
8b6a5902 8427 }
46bdb9cf
JM
8428 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8429 && (TREE_CODE (value) == STRING_CST
8430 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8431 && !(TREE_CODE (value) == STRING_CST
8432 && TREE_CODE (type) == ARRAY_TYPE
8433 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8434 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8435 TYPE_MAIN_VARIANT (type)))
c2255bc4 8436 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8437
3e4093b6 8438 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8439 && require_constant_value && pending)
8b6a5902 8440 {
3e4093b6
RS
8441 /* As an extension, allow initializing objects with static storage
8442 duration with compound literals (which are then treated just as
8443 the brace enclosed list they contain). */
4435bb92
MP
8444 if (flag_isoc99)
8445 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8446 "constant");
3e4093b6
RS
8447 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8448 value = DECL_INITIAL (decl);
8b6a5902
JJ
8449 }
8450
928c19bb 8451 npc = null_pointer_constant_p (value);
8ce94e44
JM
8452 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8453 {
8454 semantic_type = TREE_TYPE (value);
8455 value = TREE_OPERAND (value, 0);
8456 }
928c19bb
JM
8457 value = c_fully_fold (value, require_constant_value, &maybe_const);
8458
3e4093b6
RS
8459 if (value == error_mark_node)
8460 constructor_erroneous = 1;
8461 else if (!TREE_CONSTANT (value))
8462 constructor_constant = 0;
ee45a32d
EB
8463 else if (!initializer_constant_valid_p (value,
8464 TREE_TYPE (value),
8465 AGGREGATE_TYPE_P (constructor_type)
8466 && TYPE_REVERSE_STORAGE_ORDER
8467 (constructor_type))
3e4093b6
RS
8468 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8469 || TREE_CODE (constructor_type) == UNION_TYPE)
8470 && DECL_C_BIT_FIELD (field)
8471 && TREE_CODE (value) != INTEGER_CST))
8472 constructor_simple = 0;
928c19bb
JM
8473 if (!maybe_const)
8474 constructor_nonconst = 1;
3e4093b6 8475
116df786 8476 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8477 {
116df786
RH
8478 if (require_constant_value)
8479 {
ea58ef42 8480 error_init (loc, "initializer element is not constant");
116df786
RH
8481 value = error_mark_node;
8482 }
8483 else if (require_constant_elements)
8337d1db 8484 pedwarn (loc, OPT_Wpedantic,
509c9d60 8485 "initializer element is not computable at load time");
8b6a5902 8486 }
928c19bb
JM
8487 else if (!maybe_const
8488 && (require_constant_value || require_constant_elements))
8337d1db 8489 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8490 "initializer element is not a constant expression");
3e4093b6 8491
81f40b79
ILT
8492 /* Issue -Wc++-compat warnings about initializing a bitfield with
8493 enum type. */
8494 if (warn_cxx_compat
8495 && field != NULL_TREE
8496 && TREE_CODE (field) == FIELD_DECL
8497 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8498 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8499 != TYPE_MAIN_VARIANT (type))
8500 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8501 {
8502 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8503 if (checktype != error_mark_node
8504 && (TYPE_MAIN_VARIANT (checktype)
8505 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8506 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8507 "enum conversion in initialization is invalid in C++");
8508 }
8509
3e4093b6
RS
8510 /* If this field is empty (and not at the end of structure),
8511 don't do anything other than checking the initializer. */
8512 if (field
8513 && (TREE_TYPE (field) == error_mark_node
8514 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8515 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8516 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8517 || DECL_CHAIN (field)))))
3e4093b6
RS
8518 return;
8519
8ce94e44
JM
8520 if (semantic_type)
8521 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8522 value = digest_init (loc, type, value, origtype, npc, strict_string,
8523 require_constant_value);
3e4093b6 8524 if (value == error_mark_node)
8b6a5902 8525 {
3e4093b6
RS
8526 constructor_erroneous = 1;
8527 return;
8b6a5902 8528 }
928c19bb
JM
8529 if (require_constant_value || require_constant_elements)
8530 constant_expression_warning (value);
8b6a5902 8531
3e4093b6
RS
8532 /* If this element doesn't come next in sequence,
8533 put it on constructor_pending_elts. */
8534 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8535 && (!constructor_incremental
8536 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8537 {
3e4093b6
RS
8538 if (constructor_incremental
8539 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8540 set_nonincremental_init (braced_init_obstack);
3e4093b6 8541
96b40f8d 8542 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8543 braced_init_obstack);
3e4093b6 8544 return;
8b6a5902 8545 }
3e4093b6
RS
8546 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8547 && (!constructor_incremental
8548 || field != constructor_unfilled_fields))
8b6a5902 8549 {
3e4093b6
RS
8550 /* We do this for records but not for unions. In a union,
8551 no matter which field is specified, it can be initialized
8552 right away since it starts at the beginning of the union. */
8553 if (constructor_incremental)
8554 {
8555 if (!constructor_unfilled_fields)
a1e3b3d9 8556 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8557 else
8558 {
8559 tree bitpos, unfillpos;
8560
8561 bitpos = bit_position (field);
8562 unfillpos = bit_position (constructor_unfilled_fields);
8563
8564 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8565 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8566 }
8567 }
8568
96b40f8d 8569 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8570 braced_init_obstack);
3e4093b6 8571 return;
8b6a5902 8572 }
3e4093b6 8573 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8574 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8575 {
b295aee2
JJ
8576 if (!implicit)
8577 {
9771b263 8578 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
755e528f 8579 warning_init (loc, OPT_Woverride_init_side_effects,
b295aee2
JJ
8580 "initialized field with side-effects overwritten");
8581 else if (warn_override_init)
96b40f8d
MP
8582 warning_init (loc, OPT_Woverride_init,
8583 "initialized field overwritten");
b295aee2 8584 }
8b6a5902 8585
3e4093b6 8586 /* We can have just one union field set. */
9771b263 8587 constructor_elements = NULL;
3e4093b6 8588 }
8b6a5902 8589
3e4093b6
RS
8590 /* Otherwise, output this element either to
8591 constructor_elements or to the assembler file. */
8b6a5902 8592
f32682ca 8593 constructor_elt celt = {field, value};
9771b263 8594 vec_safe_push (constructor_elements, celt);
8b6a5902 8595
3e4093b6
RS
8596 /* Advance the variable that indicates sequential elements output. */
8597 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8598 constructor_unfilled_index
db3927fb
AH
8599 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8600 bitsize_one_node);
3e4093b6
RS
8601 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8602 {
8603 constructor_unfilled_fields
910ad8de 8604 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8605
3e4093b6
RS
8606 /* Skip any nameless bit fields. */
8607 while (constructor_unfilled_fields != 0
8608 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8609 && DECL_NAME (constructor_unfilled_fields) == 0)
8610 constructor_unfilled_fields =
910ad8de 8611 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8612 }
8613 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8614 constructor_unfilled_fields = 0;
de520661 8615
3e4093b6
RS
8616 /* Now output any pending elements which have become next. */
8617 if (pending)
a1e3b3d9 8618 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8619}
8b6a5902 8620
3e4093b6
RS
8621/* Output any pending elements which have become next.
8622 As we output elements, constructor_unfilled_{fields,index}
8623 advances, which may cause other elements to become next;
8624 if so, they too are output.
8b6a5902 8625
3e4093b6
RS
8626 If ALL is 0, we return when there are
8627 no more pending elements to output now.
665f2503 8628
3e4093b6
RS
8629 If ALL is 1, we output space as necessary so that
8630 we can output all the pending elements. */
3e4093b6 8631static void
a1e3b3d9 8632output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8633{
8634 struct init_node *elt = constructor_pending_elts;
8635 tree next;
de520661 8636
3e4093b6
RS
8637 retry:
8638
ba228239 8639 /* Look through the whole pending tree.
3e4093b6
RS
8640 If we find an element that should be output now,
8641 output it. Otherwise, set NEXT to the element
8642 that comes first among those still pending. */
8643
8644 next = 0;
8645 while (elt)
8646 {
8647 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8648 {
3e4093b6
RS
8649 if (tree_int_cst_equal (elt->purpose,
8650 constructor_unfilled_index))
34cf811f
MP
8651 output_init_element (input_location, elt->value, elt->origtype,
8652 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8653 constructor_unfilled_index, 0, false,
8654 braced_init_obstack);
3e4093b6
RS
8655 else if (tree_int_cst_lt (constructor_unfilled_index,
8656 elt->purpose))
8b6a5902 8657 {
3e4093b6
RS
8658 /* Advance to the next smaller node. */
8659 if (elt->left)
8660 elt = elt->left;
8661 else
8662 {
8663 /* We have reached the smallest node bigger than the
8664 current unfilled index. Fill the space first. */
8665 next = elt->purpose;
8666 break;
8667 }
8b6a5902 8668 }
ce662d4c
JJ
8669 else
8670 {
3e4093b6
RS
8671 /* Advance to the next bigger node. */
8672 if (elt->right)
8673 elt = elt->right;
8674 else
ce662d4c 8675 {
3e4093b6
RS
8676 /* We have reached the biggest node in a subtree. Find
8677 the parent of it, which is the next bigger node. */
8678 while (elt->parent && elt->parent->right == elt)
8679 elt = elt->parent;
8680 elt = elt->parent;
8681 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8682 elt->purpose))
8683 {
8684 next = elt->purpose;
8685 break;
8686 }
ce662d4c
JJ
8687 }
8688 }
8b6a5902 8689 }
3e4093b6
RS
8690 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8691 || TREE_CODE (constructor_type) == UNION_TYPE)
8692 {
8693 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8694
3e4093b6
RS
8695 /* If the current record is complete we are done. */
8696 if (constructor_unfilled_fields == 0)
8697 break;
de520661 8698
3e4093b6
RS
8699 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8700 elt_bitpos = bit_position (elt->purpose);
8701 /* We can't compare fields here because there might be empty
8702 fields in between. */
8703 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8704 {
8705 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8706 output_init_element (input_location, elt->value, elt->origtype,
8707 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8708 elt->purpose, 0, false,
8709 braced_init_obstack);
3e4093b6
RS
8710 }
8711 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8712 {
8713 /* Advance to the next smaller node. */
8714 if (elt->left)
8715 elt = elt->left;
8716 else
8717 {
8718 /* We have reached the smallest node bigger than the
8719 current unfilled field. Fill the space first. */
8720 next = elt->purpose;
8721 break;
8722 }
8723 }
8724 else
8725 {
8726 /* Advance to the next bigger node. */
8727 if (elt->right)
8728 elt = elt->right;
8729 else
8730 {
8731 /* We have reached the biggest node in a subtree. Find
8732 the parent of it, which is the next bigger node. */
8733 while (elt->parent && elt->parent->right == elt)
8734 elt = elt->parent;
8735 elt = elt->parent;
8736 if (elt
8737 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8738 bit_position (elt->purpose))))
8739 {
8740 next = elt->purpose;
8741 break;
8742 }
8743 }
8744 }
8745 }
8746 }
de520661 8747
3e4093b6
RS
8748 /* Ordinarily return, but not if we want to output all
8749 and there are elements left. */
3f75a254 8750 if (!(all && next != 0))
e5cfb88f
RK
8751 return;
8752
3e4093b6
RS
8753 /* If it's not incremental, just skip over the gap, so that after
8754 jumping to retry we will output the next successive element. */
8755 if (TREE_CODE (constructor_type) == RECORD_TYPE
8756 || TREE_CODE (constructor_type) == UNION_TYPE)
8757 constructor_unfilled_fields = next;
8758 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8759 constructor_unfilled_index = next;
de520661 8760
3e4093b6
RS
8761 /* ELT now points to the node in the pending tree with the next
8762 initializer to output. */
8763 goto retry;
de520661
RS
8764}
8765\f
3e4093b6
RS
8766/* Add one non-braced element to the current constructor level.
8767 This adjusts the current position within the constructor's type.
8768 This may also start or terminate implicit levels
8769 to handle a partly-braced initializer.
e5e809f4 8770
3e4093b6 8771 Once this has found the correct level for the new element,
b295aee2
JJ
8772 it calls output_init_element.
8773
8774 IMPLICIT is true if value comes from pop_init_level (1),
8775 the new initializer has been merged with the existing one
8776 and thus no warnings should be emitted about overriding an
8777 existing initializer. */
3e4093b6
RS
8778
8779void
34cf811f 8780process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 8781 struct obstack * braced_init_obstack)
e5e809f4 8782{
916c5919
JM
8783 tree orig_value = value.value;
8784 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8785 bool strict_string = value.original_code == STRING_CST;
340baef7 8786 bool was_designated = designator_depth != 0;
e5e809f4 8787
3e4093b6 8788 designator_depth = 0;
b06df647 8789 designator_erroneous = 0;
e5e809f4 8790
9bac5cbb
G
8791 if (!implicit && value.value && !integer_zerop (value.value))
8792 constructor_zeroinit = 0;
8793
3e4093b6
RS
8794 /* Handle superfluous braces around string cst as in
8795 char x[] = {"foo"}; */
8796 if (string_flag
8797 && constructor_type
340baef7 8798 && !was_designated
3e4093b6 8799 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8800 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8801 && integer_zerop (constructor_unfilled_index))
e5e809f4 8802 {
916c5919 8803 if (constructor_stack->replacement_value.value)
ea58ef42 8804 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
8805 constructor_stack->replacement_value = value;
8806 return;
e5e809f4 8807 }
8b6a5902 8808
916c5919 8809 if (constructor_stack->replacement_value.value != 0)
3e4093b6 8810 {
ea58ef42 8811 error_init (loc, "excess elements in struct initializer");
3e4093b6 8812 return;
e5e809f4
JL
8813 }
8814
3e4093b6
RS
8815 /* Ignore elements of a brace group if it is entirely superfluous
8816 and has already been diagnosed. */
8817 if (constructor_type == 0)
8818 return;
e5e809f4 8819
976d5a22
TT
8820 if (!implicit && warn_designated_init && !was_designated
8821 && TREE_CODE (constructor_type) == RECORD_TYPE
8822 && lookup_attribute ("designated_init",
8823 TYPE_ATTRIBUTES (constructor_type)))
8824 warning_init (loc,
8825 OPT_Wdesignated_init,
8826 "positional initialization of field "
8827 "in %<struct%> declared with %<designated_init%> attribute");
8828
3e4093b6
RS
8829 /* If we've exhausted any levels that didn't have braces,
8830 pop them now. */
8831 while (constructor_stack->implicit)
8832 {
8833 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8834 || TREE_CODE (constructor_type) == UNION_TYPE)
8835 && constructor_fields == 0)
ea58ef42
MP
8836 process_init_element (loc,
8837 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8838 true, braced_init_obstack);
53650abe 8839 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
31521951 8840 || VECTOR_TYPE_P (constructor_type))
8824edff
JJ
8841 && constructor_max_index
8842 && tree_int_cst_lt (constructor_max_index,
8843 constructor_index))
ea58ef42
MP
8844 process_init_element (loc,
8845 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8846 true, braced_init_obstack);
3e4093b6
RS
8847 else
8848 break;
8849 }
e5e809f4 8850
3e4093b6
RS
8851 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8852 if (constructor_range_stack)
e5e809f4 8853 {
3e4093b6
RS
8854 /* If value is a compound literal and we'll be just using its
8855 content, don't put it into a SAVE_EXPR. */
916c5919 8856 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
c3e38a03 8857 || !require_constant_value)
8ce94e44
JM
8858 {
8859 tree semantic_type = NULL_TREE;
8860 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8861 {
8862 semantic_type = TREE_TYPE (value.value);
8863 value.value = TREE_OPERAND (value.value, 0);
8864 }
8865 value.value = c_save_expr (value.value);
8866 if (semantic_type)
8867 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8868 value.value);
8869 }
3e4093b6 8870 }
e5e809f4 8871
3e4093b6
RS
8872 while (1)
8873 {
8874 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8875 {
3e4093b6
RS
8876 tree fieldtype;
8877 enum tree_code fieldcode;
e5e809f4 8878
3e4093b6
RS
8879 if (constructor_fields == 0)
8880 {
ea58ef42 8881 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
8882 break;
8883 }
e5e809f4 8884
3e4093b6
RS
8885 fieldtype = TREE_TYPE (constructor_fields);
8886 if (fieldtype != error_mark_node)
8887 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8888 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8889
3e4093b6
RS
8890 /* Error for non-static initialization of a flexible array member. */
8891 if (fieldcode == ARRAY_TYPE
8892 && !require_constant_value
8893 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8894 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 8895 {
ea58ef42
MP
8896 error_init (loc, "non-static initialization of a flexible "
8897 "array member");
3e4093b6
RS
8898 break;
8899 }
e5e809f4 8900
2cc901dc
MP
8901 /* Error for initialization of a flexible array member with
8902 a string constant if the structure is in an array. E.g.:
8903 struct S { int x; char y[]; };
8904 struct S s[] = { { 1, "foo" } };
8905 is invalid. */
8906 if (string_flag
8907 && fieldcode == ARRAY_TYPE
8908 && constructor_depth > 1
8909 && TYPE_SIZE (fieldtype) == NULL_TREE
8910 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8911 {
8912 bool in_array_p = false;
8913 for (struct constructor_stack *p = constructor_stack;
8914 p && p->type; p = p->next)
8915 if (TREE_CODE (p->type) == ARRAY_TYPE)
8916 {
8917 in_array_p = true;
8918 break;
8919 }
8920 if (in_array_p)
8921 {
8922 error_init (loc, "initialization of flexible array "
8923 "member in a nested context");
8924 break;
8925 }
8926 }
8927
3e4093b6 8928 /* Accept a string constant to initialize a subarray. */
916c5919 8929 if (value.value != 0
3e4093b6 8930 && fieldcode == ARRAY_TYPE
197463ae 8931 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8932 && string_flag)
916c5919 8933 value.value = orig_value;
3e4093b6
RS
8934 /* Otherwise, if we have come to a subaggregate,
8935 and we don't have an element of its type, push into it. */
0953878d 8936 else if (value.value != 0
916c5919
JM
8937 && value.value != error_mark_node
8938 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8939 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8940 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8941 {
ea58ef42 8942 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8943 continue;
8944 }
e5e809f4 8945
916c5919 8946 if (value.value)
3e4093b6
RS
8947 {
8948 push_member_name (constructor_fields);
34cf811f 8949 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8950 strict_string, fieldtype,
a1e3b3d9
LB
8951 constructor_fields, 1, implicit,
8952 braced_init_obstack);
3e4093b6 8953 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8954 }
8955 else
3e4093b6
RS
8956 /* Do the bookkeeping for an element that was
8957 directly output as a constructor. */
e5e809f4 8958 {
3e4093b6
RS
8959 /* For a record, keep track of end position of last field. */
8960 if (DECL_SIZE (constructor_fields))
c22cacf3 8961 constructor_bit_index
db3927fb
AH
8962 = size_binop_loc (input_location, PLUS_EXPR,
8963 bit_position (constructor_fields),
8964 DECL_SIZE (constructor_fields));
3e4093b6
RS
8965
8966 /* If the current field was the first one not yet written out,
8967 it isn't now, so update. */
8968 if (constructor_unfilled_fields == constructor_fields)
8969 {
910ad8de 8970 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8971 /* Skip any nameless bit fields. */
8972 while (constructor_unfilled_fields != 0
8973 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8974 && DECL_NAME (constructor_unfilled_fields) == 0)
8975 constructor_unfilled_fields =
910ad8de 8976 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8977 }
e5e809f4 8978 }
3e4093b6 8979
910ad8de 8980 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8981 /* Skip any nameless bit fields at the beginning. */
8982 while (constructor_fields != 0
8983 && DECL_C_BIT_FIELD (constructor_fields)
8984 && DECL_NAME (constructor_fields) == 0)
910ad8de 8985 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8986 }
3e4093b6 8987 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8988 {
3e4093b6
RS
8989 tree fieldtype;
8990 enum tree_code fieldcode;
e5e809f4 8991
3e4093b6
RS
8992 if (constructor_fields == 0)
8993 {
d033409e 8994 pedwarn_init (loc, 0,
509c9d60 8995 "excess elements in union initializer");
3e4093b6
RS
8996 break;
8997 }
e5e809f4 8998
3e4093b6
RS
8999 fieldtype = TREE_TYPE (constructor_fields);
9000 if (fieldtype != error_mark_node)
9001 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9002 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9003
3e4093b6
RS
9004 /* Warn that traditional C rejects initialization of unions.
9005 We skip the warning if the value is zero. This is done
9006 under the assumption that the zero initializer in user
9007 code appears conditioned on e.g. __STDC__ to avoid
9008 "missing initializer" warnings and relies on default
9009 initialization to zero in the traditional C case.
9010 We also skip the warning if the initializer is designated,
9011 again on the assumption that this must be conditional on
9012 __STDC__ anyway (and we've already complained about the
9013 member-designator already). */
8400e75e 9014 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
9015 && !(value.value && (integer_zerop (value.value)
9016 || real_zerop (value.value))))
3176a0c2
DD
9017 warning (OPT_Wtraditional, "traditional C rejects initialization "
9018 "of unions");
e5e809f4 9019
3e4093b6 9020 /* Accept a string constant to initialize a subarray. */
916c5919 9021 if (value.value != 0
3e4093b6 9022 && fieldcode == ARRAY_TYPE
197463ae 9023 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9024 && string_flag)
916c5919 9025 value.value = orig_value;
3e4093b6
RS
9026 /* Otherwise, if we have come to a subaggregate,
9027 and we don't have an element of its type, push into it. */
0953878d 9028 else if (value.value != 0
916c5919
JM
9029 && value.value != error_mark_node
9030 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9031 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9032 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9033 {
ea58ef42 9034 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9035 continue;
9036 }
e5e809f4 9037
916c5919 9038 if (value.value)
3e4093b6
RS
9039 {
9040 push_member_name (constructor_fields);
34cf811f 9041 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9042 strict_string, fieldtype,
a1e3b3d9
LB
9043 constructor_fields, 1, implicit,
9044 braced_init_obstack);
3e4093b6 9045 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9046 }
9047 else
3e4093b6
RS
9048 /* Do the bookkeeping for an element that was
9049 directly output as a constructor. */
e5e809f4 9050 {
3e4093b6 9051 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 9052 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9053 }
e5e809f4 9054
3e4093b6
RS
9055 constructor_fields = 0;
9056 }
9057 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9058 {
9059 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9060 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 9061
3e4093b6 9062 /* Accept a string constant to initialize a subarray. */
916c5919 9063 if (value.value != 0
3e4093b6 9064 && eltcode == ARRAY_TYPE
197463ae 9065 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 9066 && string_flag)
916c5919 9067 value.value = orig_value;
3e4093b6
RS
9068 /* Otherwise, if we have come to a subaggregate,
9069 and we don't have an element of its type, push into it. */
0953878d 9070 else if (value.value != 0
916c5919
JM
9071 && value.value != error_mark_node
9072 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9073 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9074 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9075 {
ea58ef42 9076 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9077 continue;
9078 }
8b6a5902 9079
3e4093b6
RS
9080 if (constructor_max_index != 0
9081 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9082 || integer_all_onesp (constructor_max_index)))
9083 {
d033409e 9084 pedwarn_init (loc, 0,
509c9d60 9085 "excess elements in array initializer");
3e4093b6
RS
9086 break;
9087 }
8b6a5902 9088
3e4093b6 9089 /* Now output the actual element. */
916c5919 9090 if (value.value)
3e4093b6 9091 {
ae7e9ddd 9092 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9093 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9094 strict_string, elttype,
a1e3b3d9
LB
9095 constructor_index, 1, implicit,
9096 braced_init_obstack);
3e4093b6
RS
9097 RESTORE_SPELLING_DEPTH (constructor_depth);
9098 }
2f6e4e97 9099
3e4093b6 9100 constructor_index
db3927fb
AH
9101 = size_binop_loc (input_location, PLUS_EXPR,
9102 constructor_index, bitsize_one_node);
8b6a5902 9103
916c5919 9104 if (!value.value)
3e4093b6
RS
9105 /* If we are doing the bookkeeping for an element that was
9106 directly output as a constructor, we must update
9107 constructor_unfilled_index. */
9108 constructor_unfilled_index = constructor_index;
9109 }
31521951 9110 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
9111 {
9112 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9113
c22cacf3
MS
9114 /* Do a basic check of initializer size. Note that vectors
9115 always have a fixed size derived from their type. */
3e4093b6
RS
9116 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9117 {
d033409e 9118 pedwarn_init (loc, 0,
509c9d60 9119 "excess elements in vector initializer");
3e4093b6
RS
9120 break;
9121 }
8b6a5902 9122
3e4093b6 9123 /* Now output the actual element. */
916c5919 9124 if (value.value)
53650abe
AP
9125 {
9126 if (TREE_CODE (value.value) == VECTOR_CST)
9127 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9128 output_init_element (loc, value.value, value.original_type,
53650abe 9129 strict_string, elttype,
a1e3b3d9
LB
9130 constructor_index, 1, implicit,
9131 braced_init_obstack);
53650abe 9132 }
8b6a5902 9133
3e4093b6 9134 constructor_index
db3927fb
AH
9135 = size_binop_loc (input_location,
9136 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9137
916c5919 9138 if (!value.value)
3e4093b6
RS
9139 /* If we are doing the bookkeeping for an element that was
9140 directly output as a constructor, we must update
9141 constructor_unfilled_index. */
9142 constructor_unfilled_index = constructor_index;
9143 }
8b6a5902 9144
3e4093b6
RS
9145 /* Handle the sole element allowed in a braced initializer
9146 for a scalar variable. */
b4519d39
SB
9147 else if (constructor_type != error_mark_node
9148 && constructor_fields == 0)
8b6a5902 9149 {
d033409e 9150 pedwarn_init (loc, 0,
509c9d60 9151 "excess elements in scalar initializer");
3e4093b6 9152 break;
8b6a5902
JJ
9153 }
9154 else
9155 {
916c5919 9156 if (value.value)
34cf811f 9157 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9158 strict_string, constructor_type,
a1e3b3d9
LB
9159 NULL_TREE, 1, implicit,
9160 braced_init_obstack);
3e4093b6 9161 constructor_fields = 0;
8b6a5902
JJ
9162 }
9163
3e4093b6
RS
9164 /* Handle range initializers either at this level or anywhere higher
9165 in the designator stack. */
9166 if (constructor_range_stack)
8b6a5902 9167 {
3e4093b6
RS
9168 struct constructor_range_stack *p, *range_stack;
9169 int finish = 0;
9170
9171 range_stack = constructor_range_stack;
9172 constructor_range_stack = 0;
9173 while (constructor_stack != range_stack->stack)
8b6a5902 9174 {
366de0ce 9175 gcc_assert (constructor_stack->implicit);
34cf811f 9176 process_init_element (loc,
ea58ef42
MP
9177 pop_init_level (loc, 1,
9178 braced_init_obstack),
a1e3b3d9 9179 true, braced_init_obstack);
8b6a5902 9180 }
3e4093b6
RS
9181 for (p = range_stack;
9182 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9183 p = p->prev)
8b6a5902 9184 {
366de0ce 9185 gcc_assert (constructor_stack->implicit);
34cf811f 9186 process_init_element (loc,
ea58ef42
MP
9187 pop_init_level (loc, 1,
9188 braced_init_obstack),
a1e3b3d9 9189 true, braced_init_obstack);
8b6a5902 9190 }
3e4093b6 9191
db3927fb
AH
9192 p->index = size_binop_loc (input_location,
9193 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9194 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9195 finish = 1;
9196
9197 while (1)
9198 {
9199 constructor_index = p->index;
9200 constructor_fields = p->fields;
9201 if (finish && p->range_end && p->index == p->range_start)
9202 {
9203 finish = 0;
9204 p->prev = 0;
9205 }
9206 p = p->next;
9207 if (!p)
9208 break;
ea58ef42 9209 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9210 p->stack = constructor_stack;
9211 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9212 p->index = p->range_start;
9213 }
9214
9215 if (!finish)
9216 constructor_range_stack = range_stack;
9217 continue;
8b6a5902
JJ
9218 }
9219
3e4093b6 9220 break;
8b6a5902
JJ
9221 }
9222
3e4093b6
RS
9223 constructor_range_stack = 0;
9224}
9225\f
9f0e2d86
ZW
9226/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9227 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9228 an ASM_EXPR node). */
3e4093b6 9229tree
9f0e2d86 9230build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9231{
6de9cd9a
DN
9232 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9233 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9234 return add_stmt (args);
8b6a5902
JJ
9235}
9236
9f0e2d86
ZW
9237/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9238 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9239 SIMPLE indicates whether there was anything at all after the
9240 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9241 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9242tree
c2255bc4 9243build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9244 tree clobbers, tree labels, bool simple)
e5e809f4 9245{
3e4093b6 9246 tree tail;
9f0e2d86 9247 tree args;
6de9cd9a
DN
9248 int i;
9249 const char *constraint;
74f0c611 9250 const char **oconstraints;
6de9cd9a 9251 bool allows_mem, allows_reg, is_inout;
74f0c611 9252 int ninputs, noutputs;
6de9cd9a
DN
9253
9254 ninputs = list_length (inputs);
9255 noutputs = list_length (outputs);
74f0c611
RH
9256 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9257
1c384bf1 9258 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9259
6de9cd9a
DN
9260 /* Remove output conversions that change the type but not the mode. */
9261 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9262 {
3e4093b6 9263 tree output = TREE_VALUE (tail);
74f0c611 9264
eadd3d0d
JJ
9265 output = c_fully_fold (output, false, NULL);
9266
74f0c611
RH
9267 /* ??? Really, this should not be here. Users should be using a
9268 proper lvalue, dammit. But there's a long history of using casts
9269 in the output operands. In cases like longlong.h, this becomes a
9270 primitive form of typechecking -- if the cast can be removed, then
9271 the output operand had a type of the proper width; otherwise we'll
9272 get an error. Gross, but ... */
3e4093b6 9273 STRIP_NOPS (output);
74f0c611 9274
7bd11157 9275 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9276 output = error_mark_node;
8b6a5902 9277
5544530a
PB
9278 if (output != error_mark_node
9279 && (TREE_READONLY (output)
9280 || TYPE_READONLY (TREE_TYPE (output))
9281 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9282 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9283 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9284 readonly_error (loc, output, lv_asm);
5544530a 9285
6de9cd9a 9286 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9287 oconstraints[i] = constraint;
9288
9289 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9290 &allows_mem, &allows_reg, &is_inout))
9291 {
9292 /* If the operand is going to end up in memory,
9293 mark it addressable. */
9294 if (!allows_reg && !c_mark_addressable (output))
9295 output = error_mark_node;
bae5cddf
JJ
9296 if (!(!allows_reg && allows_mem)
9297 && output != error_mark_node
9298 && VOID_TYPE_P (TREE_TYPE (output)))
9299 {
9300 error_at (loc, "invalid use of void expression");
9301 output = error_mark_node;
9302 }
74f0c611
RH
9303 }
9304 else
c22cacf3 9305 output = error_mark_node;
3e4093b6 9306
74f0c611 9307 TREE_VALUE (tail) = output;
8b6a5902 9308 }
3e4093b6 9309
74f0c611
RH
9310 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9311 {
9312 tree input;
9313
9314 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9315 input = TREE_VALUE (tail);
9316
74f0c611
RH
9317 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9318 oconstraints, &allows_mem, &allows_reg))
9319 {
9320 /* If the operand is going to end up in memory,
9321 mark it addressable. */
b4c33883
AP
9322 if (!allows_reg && allows_mem)
9323 {
eadd3d0d
JJ
9324 input = c_fully_fold (input, false, NULL);
9325
b4c33883
AP
9326 /* Strip the nops as we allow this case. FIXME, this really
9327 should be rejected or made deprecated. */
9328 STRIP_NOPS (input);
9329 if (!c_mark_addressable (input))
9330 input = error_mark_node;
bae5cddf 9331 }
eadd3d0d 9332 else
bae5cddf 9333 {
eadd3d0d
JJ
9334 struct c_expr expr;
9335 memset (&expr, 0, sizeof (expr));
9336 expr.value = input;
267bac10 9337 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9338 input = c_fully_fold (expr.value, false, NULL);
9339
9340 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9341 {
9342 error_at (loc, "invalid use of void expression");
9343 input = error_mark_node;
9344 }
bae5cddf 9345 }
74f0c611
RH
9346 }
9347 else
9348 input = error_mark_node;
9349
9350 TREE_VALUE (tail) = input;
9351 }
3e4093b6 9352
1c384bf1
RH
9353 /* ASMs with labels cannot have outputs. This should have been
9354 enforced by the parser. */
9355 gcc_assert (outputs == NULL || labels == NULL);
9356
9357 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9358
5544530a
PB
9359 /* asm statements without outputs, including simple ones, are treated
9360 as volatile. */
9361 ASM_INPUT_P (args) = simple;
9362 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9363
9f0e2d86 9364 return args;
e5e809f4 9365}
3e4093b6 9366\f
c2255bc4
AH
9367/* Generate a goto statement to LABEL. LOC is the location of the
9368 GOTO. */
506e2710
RH
9369
9370tree
c2255bc4 9371c_finish_goto_label (location_t loc, tree label)
506e2710 9372{
e1b7793c 9373 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9374 if (!decl)
9375 return NULL_TREE;
506e2710 9376 TREE_USED (decl) = 1;
c2255bc4
AH
9377 {
9378 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9379 SET_EXPR_LOCATION (t, loc);
9380 return add_stmt (t);
9381 }
506e2710
RH
9382}
9383
c2255bc4
AH
9384/* Generate a computed goto statement to EXPR. LOC is the location of
9385 the GOTO. */
506e2710
RH
9386
9387tree
c2255bc4 9388c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9389{
c2255bc4 9390 tree t;
c1771a20 9391 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9392 expr = c_fully_fold (expr, false, NULL);
506e2710 9393 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9394 t = build1 (GOTO_EXPR, void_type_node, expr);
9395 SET_EXPR_LOCATION (t, loc);
9396 return add_stmt (t);
506e2710
RH
9397}
9398
5088b058 9399/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9400 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9401 the location of the return statement, or the location of the expression,
9402 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9403 is the original type of RETVAL. */
de520661 9404
506e2710 9405tree
c2255bc4 9406c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9407{
0c9b182b
JJ
9408 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9409 bool no_warning = false;
928c19bb 9410 bool npc = false;
36536d79 9411 size_t rank = 0;
3e4093b6 9412
de8ddd5f
MP
9413 /* Use the expansion point to handle cases such as returning NULL
9414 in a function returning void. */
9415 source_location xloc = expansion_point_location_if_in_system_header (loc);
9416
3e4093b6 9417 if (TREE_THIS_VOLATILE (current_function_decl))
de8ddd5f 9418 warning_at (xloc, 0,
c2255bc4 9419 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9420
b72271b9 9421 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9422 {
9423 /* Array notations are allowed in a return statement if it is inside a
9424 built-in array notation reduction function. */
9425 if (!find_rank (loc, retval, retval, false, &rank))
9426 return error_mark_node;
9427 if (rank >= 1)
9428 {
9429 error_at (loc, "array notation expression cannot be used as a "
9430 "return value");
9431 return error_mark_node;
9432 }
9433 }
3af9c5e9 9434 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9435 {
9436 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9437 "allowed");
9438 return error_mark_node;
9439 }
928c19bb
JM
9440 if (retval)
9441 {
8ce94e44 9442 tree semantic_type = NULL_TREE;
928c19bb 9443 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9444 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9445 {
9446 semantic_type = TREE_TYPE (retval);
9447 retval = TREE_OPERAND (retval, 0);
9448 }
928c19bb 9449 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9450 if (semantic_type)
9451 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9452 }
9453
3e4093b6 9454 if (!retval)
de520661 9455 {
3e4093b6
RS
9456 current_function_returns_null = 1;
9457 if ((warn_return_type || flag_isoc99)
9458 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9459 {
35aff4fb
MP
9460 if (flag_isoc99)
9461 pedwarn (loc, 0, "%<return%> with no value, in "
9462 "function returning non-void");
9463 else
9464 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9465 "in function returning non-void");
0c9b182b
JJ
9466 no_warning = true;
9467 }
400fbf9f 9468 }
3e4093b6 9469 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9470 {
3e4093b6 9471 current_function_returns_null = 1;
2397c575 9472 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
de8ddd5f 9473 pedwarn (xloc, 0,
509c9d60 9474 "%<return%> with a value, in function returning void");
b8698a0f 9475 else
de8ddd5f 9476 pedwarn (xloc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9477 "%<return%> with expression, in function returning void");
de520661 9478 }
3e4093b6 9479 else
de520661 9480 {
68fca595
MP
9481 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9482 retval, origtype, ic_return,
c2255bc4 9483 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9484 tree res = DECL_RESULT (current_function_decl);
9485 tree inner;
9feb29df 9486 bool save;
3e4093b6
RS
9487
9488 current_function_returns_value = 1;
9489 if (t == error_mark_node)
506e2710 9490 return NULL_TREE;
3e4093b6 9491
9feb29df
JJ
9492 save = in_late_binary_op;
9493 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
9494 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9495 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9496 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9497 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9498 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9feb29df 9499 in_late_binary_op = true;
3e4093b6 9500 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9501 in_late_binary_op = save;
3e4093b6
RS
9502
9503 /* Strip any conversions, additions, and subtractions, and see if
9504 we are returning the address of a local variable. Warn if so. */
9505 while (1)
8b6a5902 9506 {
3e4093b6 9507 switch (TREE_CODE (inner))
8b6a5902 9508 {
849421a3
JJ
9509 CASE_CONVERT:
9510 case NON_LVALUE_EXPR:
3e4093b6 9511 case PLUS_EXPR:
849421a3 9512 case POINTER_PLUS_EXPR:
3e4093b6
RS
9513 inner = TREE_OPERAND (inner, 0);
9514 continue;
9515
9516 case MINUS_EXPR:
9517 /* If the second operand of the MINUS_EXPR has a pointer
9518 type (or is converted from it), this may be valid, so
9519 don't give a warning. */
9520 {
9521 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9522
3f75a254 9523 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9524 && (CONVERT_EXPR_P (op1)
9525 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9526 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9527
3e4093b6
RS
9528 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9529 break;
8b6a5902 9530
3e4093b6
RS
9531 inner = TREE_OPERAND (inner, 0);
9532 continue;
9533 }
400fbf9f 9534
3e4093b6
RS
9535 case ADDR_EXPR:
9536 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9537
6615c446 9538 while (REFERENCE_CLASS_P (inner)
22d03525 9539 && !INDIRECT_REF_P (inner))
3e4093b6 9540 inner = TREE_OPERAND (inner, 0);
8b6a5902 9541
a2f1f4c3 9542 if (DECL_P (inner)
3f75a254
JM
9543 && !DECL_EXTERNAL (inner)
9544 && !TREE_STATIC (inner)
3e4093b6 9545 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9546 {
9547 if (TREE_CODE (inner) == LABEL_DECL)
9548 warning_at (loc, OPT_Wreturn_local_addr,
9549 "function returns address of label");
9550 else
b4dfdc11
MG
9551 {
9552 warning_at (loc, OPT_Wreturn_local_addr,
9553 "function returns address of local variable");
9554 tree zero = build_zero_cst (TREE_TYPE (res));
9555 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9556 }
19fc9faa 9557 }
3e4093b6 9558 break;
8b6a5902 9559
3e4093b6
RS
9560 default:
9561 break;
9562 }
de520661 9563
3e4093b6
RS
9564 break;
9565 }
9566
53fb4de3 9567 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9568 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9569
9570 if (warn_sequence_point)
9571 verify_sequence_points (retval);
de520661 9572 }
8b6a5902 9573
c2255bc4 9574 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9575 TREE_NO_WARNING (ret_stmt) |= no_warning;
9576 return add_stmt (ret_stmt);
de520661 9577}
3e4093b6
RS
9578\f
9579struct c_switch {
604f5adf
ILT
9580 /* The SWITCH_EXPR being built. */
9581 tree switch_expr;
a6c0a76c 9582
89dbed81 9583 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9584 default conversion is applied. */
9585 tree orig_type;
9586
3e4093b6
RS
9587 /* A splay-tree mapping the low element of a case range to the high
9588 element, or NULL_TREE if there is no high element. Used to
9589 determine whether or not a new case label duplicates an old case
9590 label. We need a tree, rather than simply a hash table, because
9591 of the GNU case range extension. */
9592 splay_tree cases;
a6c0a76c 9593
e1b7793c
ILT
9594 /* The bindings at the point of the switch. This is used for
9595 warnings crossing decls when branching to a case label. */
9596 struct c_spot_bindings *bindings;
187230a7 9597
3e4093b6
RS
9598 /* The next node on the stack. */
9599 struct c_switch *next;
b155cfd9
MP
9600
9601 /* Remember whether the controlling expression had boolean type
9602 before integer promotions for the sake of -Wswitch-bool. */
9603 bool bool_cond_p;
9604
9605 /* Remember whether there was a case value that is outside the
9606 range of the ORIG_TYPE. */
9607 bool outside_range_p;
3e4093b6 9608};
400fbf9f 9609
3e4093b6
RS
9610/* A stack of the currently active switch statements. The innermost
9611 switch statement is on the top of the stack. There is no need to
9612 mark the stack for garbage collection because it is only active
9613 during the processing of the body of a function, and we never
9614 collect at that point. */
de520661 9615
506e2710 9616struct c_switch *c_switch_stack;
de520661 9617
3e4093b6 9618/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9619 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef 9620 SWITCH_COND_LOC is the location of the switch's condition.
b155cfd9 9621 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
de520661 9622
3e4093b6 9623tree
c2255bc4
AH
9624c_start_case (location_t switch_loc,
9625 location_t switch_cond_loc,
fedfecef 9626 tree exp, bool explicit_cast_p)
de520661 9627{
c58e8676 9628 tree orig_type = error_mark_node;
b155cfd9 9629 bool bool_cond_p = false;
3e4093b6 9630 struct c_switch *cs;
2f6e4e97 9631
3e4093b6 9632 if (exp != error_mark_node)
de520661 9633 {
3e4093b6
RS
9634 orig_type = TREE_TYPE (exp);
9635
c58e8676 9636 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9637 {
c58e8676
VR
9638 if (orig_type != error_mark_node)
9639 {
c2255bc4 9640 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9641 orig_type = error_mark_node;
9642 }
3e4093b6 9643 exp = integer_zero_node;
de520661 9644 }
3e4093b6 9645 else
de520661 9646 {
c58e8676 9647 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9648 tree e = exp;
9649
9650 /* Warn if the condition has boolean value. */
9651 while (TREE_CODE (e) == COMPOUND_EXPR)
9652 e = TREE_OPERAND (e, 1);
9653
9654 if ((TREE_CODE (type) == BOOLEAN_TYPE
9655 || truth_value_p (TREE_CODE (e)))
9656 /* Explicit cast to int suppresses this warning. */
9657 && !(TREE_CODE (type) == INTEGER_TYPE
9658 && explicit_cast_p))
b155cfd9 9659 bool_cond_p = true;
8b6a5902 9660
8400e75e 9661 if (!in_system_header_at (input_location)
3e4093b6
RS
9662 && (type == long_integer_type_node
9663 || type == long_unsigned_type_node))
c2255bc4
AH
9664 warning_at (switch_cond_loc,
9665 OPT_Wtraditional, "%<long%> switch expression not "
9666 "converted to %<int%> in ISO C");
8b6a5902 9667
928c19bb 9668 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9669 exp = default_conversion (exp);
ca085fd7
MLI
9670
9671 if (warn_sequence_point)
9672 verify_sequence_points (exp);
3e4093b6
RS
9673 }
9674 }
9675
604f5adf 9676 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9677 cs = XNEW (struct c_switch);
604f5adf 9678 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9679 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9680 cs->orig_type = orig_type;
3e4093b6 9681 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9682 cs->bindings = c_get_switch_bindings ();
b155cfd9
MP
9683 cs->bool_cond_p = bool_cond_p;
9684 cs->outside_range_p = false;
506e2710
RH
9685 cs->next = c_switch_stack;
9686 c_switch_stack = cs;
3e4093b6 9687
604f5adf 9688 return add_stmt (cs->switch_expr);
3e4093b6
RS
9689}
9690
c2255bc4 9691/* Process a case label at location LOC. */
3e4093b6
RS
9692
9693tree
c2255bc4 9694do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9695{
9696 tree label = NULL_TREE;
9697
17cede2e
JM
9698 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9699 {
9700 low_value = c_fully_fold (low_value, false, NULL);
9701 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 9702 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
9703 "case label is not an integer constant expression");
9704 }
9705
9706 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9707 {
9708 high_value = c_fully_fold (high_value, false, NULL);
9709 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9710 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9711 "case label is not an integer constant expression");
9712 }
9713
e1b7793c 9714 if (c_switch_stack == NULL)
187230a7
JM
9715 {
9716 if (low_value)
e1b7793c 9717 error_at (loc, "case label not within a switch statement");
187230a7 9718 else
e1b7793c
ILT
9719 error_at (loc, "%<default%> label not within a switch statement");
9720 return NULL_TREE;
187230a7 9721 }
de520661 9722
e1b7793c
ILT
9723 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9724 EXPR_LOCATION (c_switch_stack->switch_expr),
9725 loc))
9726 return NULL_TREE;
9727
9728 label = c_add_case_label (loc, c_switch_stack->cases,
9729 SWITCH_COND (c_switch_stack->switch_expr),
9730 c_switch_stack->orig_type,
b155cfd9
MP
9731 low_value, high_value,
9732 &c_switch_stack->outside_range_p);
e1b7793c
ILT
9733 if (label == error_mark_node)
9734 label = NULL_TREE;
3e4093b6
RS
9735 return label;
9736}
de520661 9737
083e891e
MP
9738/* Finish the switch statement. TYPE is the original type of the
9739 controlling expression of the switch, or NULL_TREE. */
de520661 9740
3e4093b6 9741void
083e891e 9742c_finish_case (tree body, tree type)
3e4093b6 9743{
506e2710 9744 struct c_switch *cs = c_switch_stack;
fbc315db 9745 location_t switch_location;
3e4093b6 9746
604f5adf 9747 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9748
6de9cd9a 9749 /* Emit warnings as needed. */
c2255bc4 9750 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 9751 c_do_switch_warnings (cs->cases, switch_location,
083e891e 9752 type ? type : TREE_TYPE (cs->switch_expr),
b155cfd9
MP
9753 SWITCH_COND (cs->switch_expr),
9754 cs->bool_cond_p, cs->outside_range_p);
6de9cd9a 9755
3e4093b6 9756 /* Pop the stack. */
506e2710 9757 c_switch_stack = cs->next;
3e4093b6 9758 splay_tree_delete (cs->cases);
e1b7793c 9759 c_release_switch_bindings (cs->bindings);
5d038c4c 9760 XDELETE (cs);
de520661 9761}
325c3691 9762\f
506e2710
RH
9763/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9764 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9765 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9766 statement, and was not surrounded with parenthesis. */
325c3691 9767
9e51cf9d 9768void
506e2710
RH
9769c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9770 tree else_block, bool nested_if)
325c3691 9771{
506e2710 9772 tree stmt;
325c3691 9773
25c22937
BI
9774 /* If the condition has array notations, then the rank of the then_block and
9775 else_block must be either 0 or be equal to the rank of the condition. If
9776 the condition does not have array notations then break them up as it is
9777 broken up in a normal expression. */
b72271b9 9778 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9779 {
9780 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9781 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9782 return;
9783 if (then_block
9784 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9785 return;
9786 if (else_block
9787 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9788 return;
9789 if (cond_rank != then_rank && then_rank != 0)
9790 {
9791 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9792 " and the then-block");
9793 return;
9794 }
9795 else if (cond_rank != else_rank && else_rank != 0)
9796 {
9797 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9798 " and the else-block");
9799 return;
9800 }
9801 }
506e2710
RH
9802 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9803 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9804 {
506e2710 9805 tree inner_if = then_block;
16865eaa 9806
61ada8ae 9807 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9808 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9809 it might not be exactly THEN_BLOCK, but should be the last
9810 non-container statement within. */
9811 while (1)
9812 switch (TREE_CODE (inner_if))
9813 {
9814 case COND_EXPR:
9815 goto found;
9816 case BIND_EXPR:
9817 inner_if = BIND_EXPR_BODY (inner_if);
9818 break;
9819 case STATEMENT_LIST:
9820 inner_if = expr_last (then_block);
9821 break;
9822 case TRY_FINALLY_EXPR:
9823 case TRY_CATCH_EXPR:
9824 inner_if = TREE_OPERAND (inner_if, 0);
9825 break;
9826 default:
366de0ce 9827 gcc_unreachable ();
506e2710
RH
9828 }
9829 found:
16865eaa 9830
506e2710 9831 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9832 warning_at (if_locus, OPT_Wparentheses,
9833 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9834 }
16865eaa 9835
2214de30 9836 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9837 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9838 add_stmt (stmt);
325c3691
RH
9839}
9840
506e2710
RH
9841/* Emit a general-purpose loop construct. START_LOCUS is the location of
9842 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9843 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9844 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9845 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9846
9847void
506e2710
RH
9848c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9849 tree blab, tree clab, bool cond_is_first)
325c3691 9850{
506e2710
RH
9851 tree entry = NULL, exit = NULL, t;
9852
e5e44252
AK
9853 /* In theory could forbid cilk spawn for loop increment expression,
9854 but it should work just fine. */
36536d79 9855
28af952a
RS
9856 /* If the condition is zero don't generate a loop construct. */
9857 if (cond && integer_zerop (cond))
9858 {
9859 if (cond_is_first)
9860 {
9861 t = build_and_jump (&blab);
9862 SET_EXPR_LOCATION (t, start_locus);
9863 add_stmt (t);
9864 }
9865 }
9866 else
506e2710
RH
9867 {
9868 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9869
506e2710 9870 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9871 out of the loop, or to the top of it. If there's no exit condition,
9872 then we just build a jump back to the top. */
506e2710 9873 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9874
28af952a 9875 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9876 {
9877 /* Canonicalize the loop condition to the end. This means
9878 generating a branch to the loop condition. Reuse the
9879 continue label, if possible. */
9880 if (cond_is_first)
9881 {
9882 if (incr || !clab)
9883 {
9884 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9885 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9886 }
9887 else
9888 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9889 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9890 add_stmt (t);
9891 }
9892
506e2710 9893 t = build_and_jump (&blab);
506e2710 9894 if (cond_is_first)
db3927fb
AH
9895 exit = fold_build3_loc (start_locus,
9896 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9897 else
db3927fb
AH
9898 exit = fold_build3_loc (input_location,
9899 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9900 }
9901
506e2710
RH
9902 add_stmt (top);
9903 }
c22cacf3 9904
506e2710
RH
9905 if (body)
9906 add_stmt (body);
9907 if (clab)
9908 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9909 if (incr)
9910 add_stmt (incr);
9911 if (entry)
9912 add_stmt (entry);
9913 if (exit)
9914 add_stmt (exit);
9915 if (blab)
9916 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9917}
325c3691
RH
9918
9919tree
c2255bc4 9920c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9921{
089efaa4 9922 bool skip;
506e2710 9923 tree label = *label_p;
325c3691 9924
089efaa4
ILT
9925 /* In switch statements break is sometimes stylistically used after
9926 a return statement. This can lead to spurious warnings about
9927 control reaching the end of a non-void function when it is
9928 inlined. Note that we are calling block_may_fallthru with
9929 language specific tree nodes; this works because
9930 block_may_fallthru returns true when given something it does not
9931 understand. */
9932 skip = !block_may_fallthru (cur_stmt_list);
9933
506e2710 9934 if (!label)
089efaa4
ILT
9935 {
9936 if (!skip)
c2255bc4 9937 *label_p = label = create_artificial_label (loc);
089efaa4 9938 }
953ff289
DN
9939 else if (TREE_CODE (label) == LABEL_DECL)
9940 ;
9941 else switch (TREE_INT_CST_LOW (label))
506e2710 9942 {
953ff289 9943 case 0:
506e2710 9944 if (is_break)
c2255bc4 9945 error_at (loc, "break statement not within loop or switch");
506e2710 9946 else
c2255bc4 9947 error_at (loc, "continue statement not within a loop");
506e2710 9948 return NULL_TREE;
953ff289
DN
9949
9950 case 1:
9951 gcc_assert (is_break);
c2255bc4 9952 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9953 return NULL_TREE;
9954
c02065fc
AH
9955 case 2:
9956 if (is_break)
9957 error ("break statement within %<#pragma simd%> loop body");
9958 else
9959 error ("continue statement within %<#pragma simd%> loop body");
9960 return NULL_TREE;
9961
953ff289
DN
9962 default:
9963 gcc_unreachable ();
506e2710 9964 }
325c3691 9965
089efaa4
ILT
9966 if (skip)
9967 return NULL_TREE;
9968
2e28e797
JH
9969 if (!is_break)
9970 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9971
53fb4de3 9972 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9973}
9974
506e2710 9975/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9976
9977static void
c2255bc4 9978emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9979{
e6b5a630
RH
9980 if (expr == error_mark_node)
9981 ;
9982 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9983 {
9984 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9985 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9986 }
789eadcd
MP
9987 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9988 {
9989 tree r = expr;
9990 location_t cloc = loc;
9991 while (TREE_CODE (r) == COMPOUND_EXPR)
9992 {
9993 if (EXPR_HAS_LOCATION (r))
9994 cloc = EXPR_LOCATION (r);
9995 r = TREE_OPERAND (r, 1);
9996 }
9997 if (!TREE_SIDE_EFFECTS (r)
9998 && !VOID_TYPE_P (TREE_TYPE (r))
9999 && !CONVERT_EXPR_P (r)
cc28fc7f 10000 && !TREE_NO_WARNING (r)
789eadcd
MP
10001 && !TREE_NO_WARNING (expr))
10002 warning_at (cloc, OPT_Wunused_value,
10003 "right-hand operand of comma expression has no effect");
10004 }
27f33b15 10005 else
c2255bc4 10006 warn_if_unused_value (expr, loc);
3a5b9284
RH
10007}
10008
506e2710 10009/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
10010 diagnostics, but do not call ADD_STMT. LOC is the location of the
10011 statement. */
3a5b9284 10012
506e2710 10013tree
c2255bc4 10014c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 10015{
056928b2
JJ
10016 tree exprv;
10017
3a5b9284 10018 if (!expr)
506e2710 10019 return NULL_TREE;
3a5b9284 10020
928c19bb
JM
10021 expr = c_fully_fold (expr, false, NULL);
10022
3a5b9284
RH
10023 if (warn_sequence_point)
10024 verify_sequence_points (expr);
10025
10026 if (TREE_TYPE (expr) != error_mark_node
10027 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10028 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 10029 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
10030
10031 /* If we're not processing a statement expression, warn about unused values.
10032 Warnings for statement expressions will be emitted later, once we figure
10033 out which is the result. */
10034 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 10035 && warn_unused_value)
c2255bc4 10036 emit_side_effect_warnings (loc, expr);
3a5b9284 10037
056928b2
JJ
10038 exprv = expr;
10039 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10040 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
10041 while (CONVERT_EXPR_P (exprv))
10042 exprv = TREE_OPERAND (exprv, 0);
10043 if (DECL_P (exprv)
10044 || handled_component_p (exprv)
10045 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 10046 mark_exp_read (exprv);
fa8351f8 10047
3a5b9284
RH
10048 /* If the expression is not of a type to which we cannot assign a line
10049 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 10050 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
10051 {
10052 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10053 SET_EXPR_LOCATION (expr, loc);
10054 }
506e2710
RH
10055
10056 return expr;
10057}
10058
c2255bc4
AH
10059/* Emit an expression as a statement. LOC is the location of the
10060 expression. */
506e2710
RH
10061
10062tree
c2255bc4 10063c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
10064{
10065 if (expr)
c2255bc4 10066 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
10067 else
10068 return NULL;
3a5b9284
RH
10069}
10070
10071/* Do the opposite and emit a statement as an expression. To begin,
10072 create a new binding level and return it. */
325c3691
RH
10073
10074tree
10075c_begin_stmt_expr (void)
10076{
10077 tree ret;
10078
10079 /* We must force a BLOCK for this level so that, if it is not expanded
10080 later, there is a way to turn off the entire subtree of blocks that
10081 are contained in it. */
10082 keep_next_level ();
10083 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10084
10085 c_bindings_start_stmt_expr (c_switch_stack == NULL
10086 ? NULL
10087 : c_switch_stack->bindings);
325c3691
RH
10088
10089 /* Mark the current statement list as belonging to a statement list. */
10090 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10091
10092 return ret;
10093}
10094
c2255bc4
AH
10095/* LOC is the location of the compound statement to which this body
10096 belongs. */
10097
325c3691 10098tree
c2255bc4 10099c_finish_stmt_expr (location_t loc, tree body)
325c3691 10100{
3a5b9284 10101 tree last, type, tmp, val;
325c3691
RH
10102 tree *last_p;
10103
c2255bc4 10104 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10105
10106 c_bindings_end_stmt_expr (c_switch_stack == NULL
10107 ? NULL
10108 : c_switch_stack->bindings);
325c3691 10109
3a5b9284
RH
10110 /* Locate the last statement in BODY. See c_end_compound_stmt
10111 about always returning a BIND_EXPR. */
10112 last_p = &BIND_EXPR_BODY (body);
10113 last = BIND_EXPR_BODY (body);
10114
10115 continue_searching:
325c3691
RH
10116 if (TREE_CODE (last) == STATEMENT_LIST)
10117 {
3a5b9284
RH
10118 tree_stmt_iterator i;
10119
10120 /* This can happen with degenerate cases like ({ }). No value. */
10121 if (!TREE_SIDE_EFFECTS (last))
10122 return body;
10123
10124 /* If we're supposed to generate side effects warnings, process
10125 all of the statements except the last. */
27f33b15 10126 if (warn_unused_value)
325c3691 10127 {
3a5b9284 10128 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10129 {
10130 location_t tloc;
10131 tree t = tsi_stmt (i);
10132
10133 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10134 emit_side_effect_warnings (tloc, t);
10135 }
325c3691
RH
10136 }
10137 else
3a5b9284
RH
10138 i = tsi_last (last);
10139 last_p = tsi_stmt_ptr (i);
10140 last = *last_p;
325c3691
RH
10141 }
10142
3a5b9284
RH
10143 /* If the end of the list is exception related, then the list was split
10144 by a call to push_cleanup. Continue searching. */
10145 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10146 || TREE_CODE (last) == TRY_CATCH_EXPR)
10147 {
10148 last_p = &TREE_OPERAND (last, 0);
10149 last = *last_p;
10150 goto continue_searching;
10151 }
10152
26d8af35
JM
10153 if (last == error_mark_node)
10154 return last;
10155
3a5b9284
RH
10156 /* In the case that the BIND_EXPR is not necessary, return the
10157 expression out from inside it. */
26d8af35
JM
10158 if (last == BIND_EXPR_BODY (body)
10159 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10160 {
928c19bb
JM
10161 /* Even if this looks constant, do not allow it in a constant
10162 expression. */
e5a94231 10163 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10164 /* Do not warn if the return value of a statement expression is
10165 unused. */
928c19bb 10166 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10167 return last;
10168 }
325c3691
RH
10169
10170 /* Extract the type of said expression. */
10171 type = TREE_TYPE (last);
325c3691 10172
3a5b9284
RH
10173 /* If we're not returning a value at all, then the BIND_EXPR that
10174 we already have is a fine expression to return. */
10175 if (!type || VOID_TYPE_P (type))
10176 return body;
10177
10178 /* Now that we've located the expression containing the value, it seems
10179 silly to make voidify_wrapper_expr repeat the process. Create a
10180 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10181 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10182
10183 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10184 tree_expr_nonnegative_p giving up immediately. */
10185 val = last;
10186 if (TREE_CODE (val) == NOP_EXPR
10187 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10188 val = TREE_OPERAND (val, 0);
10189
53fb4de3 10190 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10191 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10192
c2255bc4
AH
10193 {
10194 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10195 SET_EXPR_LOCATION (t, loc);
10196 return t;
10197 }
325c3691
RH
10198}
10199\f
10200/* Begin and end compound statements. This is as simple as pushing
10201 and popping new statement lists from the tree. */
10202
10203tree
10204c_begin_compound_stmt (bool do_scope)
10205{
10206 tree stmt = push_stmt_list ();
10207 if (do_scope)
4dfa0342 10208 push_scope ();
325c3691
RH
10209 return stmt;
10210}
10211
c2255bc4
AH
10212/* End a compound statement. STMT is the statement. LOC is the
10213 location of the compound statement-- this is usually the location
10214 of the opening brace. */
10215
325c3691 10216tree
c2255bc4 10217c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10218{
10219 tree block = NULL;
10220
10221 if (do_scope)
10222 {
10223 if (c_dialect_objc ())
10224 objc_clear_super_receiver ();
10225 block = pop_scope ();
10226 }
10227
10228 stmt = pop_stmt_list (stmt);
c2255bc4 10229 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10230
10231 /* If this compound statement is nested immediately inside a statement
10232 expression, then force a BIND_EXPR to be created. Otherwise we'll
10233 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10234 STATEMENT_LISTs merge, and thus we can lose track of what statement
10235 was really last. */
38e01f9e 10236 if (building_stmt_list_p ()
325c3691
RH
10237 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10238 && TREE_CODE (stmt) != BIND_EXPR)
10239 {
53fb4de3 10240 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10241 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10242 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10243 }
10244
10245 return stmt;
10246}
5a508662
RH
10247
10248/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10249 when the current scope is exited. EH_ONLY is true when this is not
10250 meant to apply to normal control flow transfer. */
10251
10252void
c2255bc4 10253push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10254{
3a5b9284
RH
10255 enum tree_code code;
10256 tree stmt, list;
10257 bool stmt_expr;
10258
10259 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10260 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10261 add_stmt (stmt);
3a5b9284
RH
10262 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10263 list = push_stmt_list ();
10264 TREE_OPERAND (stmt, 0) = list;
10265 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10266}
325c3691 10267\f
9f47c7e5
IE
10268/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10269 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10270
10271static tree
10272build_vec_cmp (tree_code code, tree type,
10273 tree arg0, tree arg1)
10274{
10275 tree zero_vec = build_zero_cst (type);
10276 tree minus_one_vec = build_minus_one_cst (type);
10277 tree cmp_type = build_same_sized_truth_vector_type (type);
10278 tree cmp = build2 (code, cmp_type, arg0, arg1);
10279 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10280}
10281
3e4093b6
RS
10282/* Build a binary-operation expression without default conversions.
10283 CODE is the kind of expression to build.
ba47d38d 10284 LOCATION is the operator's location.
3e4093b6
RS
10285 This function differs from `build' in several ways:
10286 the data type of the result is computed and recorded in it,
10287 warnings are generated if arg data types are invalid,
10288 special handling for addition and subtraction of pointers is known,
10289 and some optimization is done (operations on narrow ints
10290 are done in the narrower type when that gives the same result).
10291 Constant folding is also done before the result is returned.
de520661 10292
3e4093b6
RS
10293 Note that the operands will never have enumeral types, or function
10294 or array types, because either they will have the default conversions
10295 performed or they have both just been converted to some other type in which
10296 the arithmetic is to be done. */
10297
10298tree
ba47d38d
AH
10299build_binary_op (location_t location, enum tree_code code,
10300 tree orig_op0, tree orig_op1, int convert_p)
de520661 10301{
8ce94e44
JM
10302 tree type0, type1, orig_type0, orig_type1;
10303 tree eptype;
3e4093b6
RS
10304 enum tree_code code0, code1;
10305 tree op0, op1;
c9f9eb5d 10306 tree ret = error_mark_node;
4de67c26 10307 const char *invalid_op_diag;
4d84fe7c 10308 bool op0_int_operands, op1_int_operands;
928c19bb 10309 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10310
3e4093b6
RS
10311 /* Expression code to give to the expression when it is built.
10312 Normally this is CODE, which is what the caller asked for,
10313 but in some special cases we change it. */
10314 enum tree_code resultcode = code;
8b6a5902 10315
3e4093b6
RS
10316 /* Data type in which the computation is to be performed.
10317 In the simplest cases this is the common type of the arguments. */
10318 tree result_type = NULL;
10319
8ce94e44
JM
10320 /* When the computation is in excess precision, the type of the
10321 final EXCESS_PRECISION_EXPR. */
2d2e923f 10322 tree semantic_result_type = NULL;
8ce94e44 10323
3e4093b6
RS
10324 /* Nonzero means operands have already been type-converted
10325 in whatever way is necessary.
10326 Zero means they need to be converted to RESULT_TYPE. */
10327 int converted = 0;
10328
10329 /* Nonzero means create the expression with this type, rather than
10330 RESULT_TYPE. */
10331 tree build_type = 0;
10332
10333 /* Nonzero means after finally constructing the expression
10334 convert it to this type. */
10335 tree final_type = 0;
10336
10337 /* Nonzero if this is an operation like MIN or MAX which can
10338 safely be computed in short if both args are promoted shorts.
10339 Also implies COMMON.
10340 -1 indicates a bitwise operation; this makes a difference
10341 in the exact conditions for when it is safe to do the operation
10342 in a narrower mode. */
10343 int shorten = 0;
10344
10345 /* Nonzero if this is a comparison operation;
10346 if both args are promoted shorts, compare the original shorts.
10347 Also implies COMMON. */
10348 int short_compare = 0;
10349
10350 /* Nonzero if this is a right-shift operation, which can be computed on the
10351 original short and then promoted if the operand is a promoted short. */
10352 int short_shift = 0;
10353
10354 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10355 int common = 0;
10356
58393038
ZL
10357 /* True means types are compatible as far as ObjC is concerned. */
10358 bool objc_ok;
10359
8ce94e44
JM
10360 /* True means this is an arithmetic operation that may need excess
10361 precision. */
10362 bool may_need_excess_precision;
10363
180f8dbb
JM
10364 /* True means this is a boolean operation that converts both its
10365 operands to truth-values. */
10366 bool boolean_op = false;
10367
de5a5fa1
MP
10368 /* Remember whether we're doing / or %. */
10369 bool doing_div_or_mod = false;
10370
10371 /* Remember whether we're doing << or >>. */
10372 bool doing_shift = false;
10373
10374 /* Tree holding instrumentation expression. */
10375 tree instrument_expr = NULL;
10376
ba47d38d
AH
10377 if (location == UNKNOWN_LOCATION)
10378 location = input_location;
10379
4d84fe7c
JM
10380 op0 = orig_op0;
10381 op1 = orig_op1;
10382
10383 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10384 if (op0_int_operands)
10385 op0 = remove_c_maybe_const_expr (op0);
10386 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10387 if (op1_int_operands)
10388 op1 = remove_c_maybe_const_expr (op1);
10389 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10390 if (int_operands)
10391 {
10392 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10393 && TREE_CODE (orig_op1) == INTEGER_CST);
10394 int_const = (int_const_or_overflow
10395 && !TREE_OVERFLOW (orig_op0)
10396 && !TREE_OVERFLOW (orig_op1));
10397 }
10398 else
10399 int_const = int_const_or_overflow = false;
10400
0e3a99ae 10401 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e 10402 if (convert_p
31521951 10403 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
790e9490 10404 {
4d84fe7c
JM
10405 op0 = default_conversion (op0);
10406 op1 = default_conversion (op1);
790e9490
RS
10407 }
10408
36536d79
BI
10409 /* When Cilk Plus is enabled and there are array notations inside op0, then
10410 we check to see if there are builtin array notation functions. If
10411 so, then we take on the type of the array notation inside it. */
b72271b9 10412 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10413 orig_type0 = type0 = find_correct_array_notation_type (op0);
10414 else
10415 orig_type0 = type0 = TREE_TYPE (op0);
10416
b72271b9 10417 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10418 orig_type1 = type1 = find_correct_array_notation_type (op1);
10419 else
10420 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10421
3e4093b6
RS
10422 /* The expression codes of the data types of the arguments tell us
10423 whether the arguments are integers, floating, pointers, etc. */
10424 code0 = TREE_CODE (type0);
10425 code1 = TREE_CODE (type1);
10426
10427 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10428 STRIP_TYPE_NOPS (op0);
10429 STRIP_TYPE_NOPS (op1);
10430
10431 /* If an error was already reported for one of the arguments,
10432 avoid reporting another error. */
10433
10434 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10435 return error_mark_node;
10436
1807ffc1
MS
10437 if (code0 == POINTER_TYPE
10438 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10439 return error_mark_node;
10440
10441 if (code1 == POINTER_TYPE
10442 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10443 return error_mark_node;
10444
4de67c26
JM
10445 if ((invalid_op_diag
10446 = targetm.invalid_binary_op (code, type0, type1)))
10447 {
ba47d38d 10448 error_at (location, invalid_op_diag);
4de67c26
JM
10449 return error_mark_node;
10450 }
10451
8ce94e44
JM
10452 switch (code)
10453 {
10454 case PLUS_EXPR:
10455 case MINUS_EXPR:
10456 case MULT_EXPR:
10457 case TRUNC_DIV_EXPR:
10458 case CEIL_DIV_EXPR:
10459 case FLOOR_DIV_EXPR:
10460 case ROUND_DIV_EXPR:
10461 case EXACT_DIV_EXPR:
10462 may_need_excess_precision = true;
10463 break;
10464 default:
10465 may_need_excess_precision = false;
10466 break;
10467 }
10468 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10469 {
10470 op0 = TREE_OPERAND (op0, 0);
10471 type0 = TREE_TYPE (op0);
10472 }
10473 else if (may_need_excess_precision
10474 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10475 {
10476 type0 = eptype;
10477 op0 = convert (eptype, op0);
10478 }
10479 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10480 {
10481 op1 = TREE_OPERAND (op1, 0);
10482 type1 = TREE_TYPE (op1);
10483 }
10484 else if (may_need_excess_precision
10485 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10486 {
10487 type1 = eptype;
10488 op1 = convert (eptype, op1);
10489 }
10490
58393038
ZL
10491 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10492
0e3a99ae
AS
10493 /* In case when one of the operands of the binary operation is
10494 a vector and another is a scalar -- convert scalar to vector. */
10495 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10496 {
a212e43f
MG
10497 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10498 true);
f90e8e2e 10499
0e3a99ae
AS
10500 switch (convert_flag)
10501 {
10502 case stv_error:
10503 return error_mark_node;
10504 case stv_firstarg:
10505 {
10506 bool maybe_const = true;
10507 tree sc;
10508 sc = c_fully_fold (op0, false, &maybe_const);
10509 sc = save_expr (sc);
10510 sc = convert (TREE_TYPE (type1), sc);
10511 op0 = build_vector_from_val (type1, sc);
10512 if (!maybe_const)
10513 op0 = c_wrap_maybe_const (op0, true);
10514 orig_type0 = type0 = TREE_TYPE (op0);
10515 code0 = TREE_CODE (type0);
10516 converted = 1;
10517 break;
10518 }
10519 case stv_secondarg:
10520 {
10521 bool maybe_const = true;
10522 tree sc;
10523 sc = c_fully_fold (op1, false, &maybe_const);
10524 sc = save_expr (sc);
10525 sc = convert (TREE_TYPE (type0), sc);
10526 op1 = build_vector_from_val (type0, sc);
10527 if (!maybe_const)
54b9f838 10528 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10529 orig_type1 = type1 = TREE_TYPE (op1);
10530 code1 = TREE_CODE (type1);
10531 converted = 1;
10532 break;
10533 }
10534 default:
10535 break;
10536 }
10537 }
10538
3e4093b6 10539 switch (code)
de520661 10540 {
3e4093b6
RS
10541 case PLUS_EXPR:
10542 /* Handle the pointer + int case. */
10543 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10544 {
db3927fb 10545 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10546 goto return_build_binary_op;
10547 }
3e4093b6 10548 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10549 {
db3927fb 10550 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10551 goto return_build_binary_op;
10552 }
fe67cf58 10553 else
3e4093b6
RS
10554 common = 1;
10555 break;
400fbf9f 10556
3e4093b6
RS
10557 case MINUS_EXPR:
10558 /* Subtraction of two similar pointers.
10559 We must subtract them as integers, then divide by object size. */
10560 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10561 && comp_target_types (location, type0, type1))
c9f9eb5d 10562 {
db3927fb 10563 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10564 goto return_build_binary_op;
10565 }
3e4093b6
RS
10566 /* Handle pointer minus int. Just like pointer plus int. */
10567 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10568 {
db3927fb 10569 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10570 goto return_build_binary_op;
10571 }
3e4093b6
RS
10572 else
10573 common = 1;
10574 break;
8b6a5902 10575
3e4093b6
RS
10576 case MULT_EXPR:
10577 common = 1;
10578 break;
10579
10580 case TRUNC_DIV_EXPR:
10581 case CEIL_DIV_EXPR:
10582 case FLOOR_DIV_EXPR:
10583 case ROUND_DIV_EXPR:
10584 case EXACT_DIV_EXPR:
de5a5fa1 10585 doing_div_or_mod = true;
c9f9eb5d 10586 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10587
10588 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10589 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10590 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10591 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10592 || code1 == FIXED_POINT_TYPE
3e4093b6 10593 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10594 {
5bed876a
AH
10595 enum tree_code tcode0 = code0, tcode1 = code1;
10596
3a021db2 10597 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10598 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10599 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10600 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10601
ab22c1fa
CF
10602 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10603 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10604 resultcode = RDIV_EXPR;
10605 else
10606 /* Although it would be tempting to shorten always here, that
10607 loses on some targets, since the modulo instruction is
10608 undefined if the quotient can't be represented in the
10609 computation mode. We shorten only if unsigned or if
10610 dividing by something we know != -1. */
8df83eae 10611 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10612 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10613 && !integer_all_onesp (op1)));
3e4093b6
RS
10614 common = 1;
10615 }
10616 break;
de520661 10617
3e4093b6 10618 case BIT_AND_EXPR:
3e4093b6
RS
10619 case BIT_IOR_EXPR:
10620 case BIT_XOR_EXPR:
10621 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10622 shorten = -1;
9ef0c8d9
AP
10623 /* Allow vector types which are not floating point types. */
10624 else if (code0 == VECTOR_TYPE
10625 && code1 == VECTOR_TYPE
10626 && !VECTOR_FLOAT_TYPE_P (type0)
10627 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10628 common = 1;
10629 break;
10630
10631 case TRUNC_MOD_EXPR:
10632 case FLOOR_MOD_EXPR:
de5a5fa1 10633 doing_div_or_mod = true;
c9f9eb5d 10634 warn_for_div_by_zero (location, op1);
de520661 10635
5cfd5d9b
AP
10636 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10637 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10638 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10639 common = 1;
10640 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10641 {
10642 /* Although it would be tempting to shorten always here, that loses
10643 on some targets, since the modulo instruction is undefined if the
10644 quotient can't be represented in the computation mode. We shorten
10645 only if unsigned or if dividing by something we know != -1. */
8df83eae 10646 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10647 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10648 && !integer_all_onesp (op1)));
3e4093b6
RS
10649 common = 1;
10650 }
10651 break;
de520661 10652
3e4093b6
RS
10653 case TRUTH_ANDIF_EXPR:
10654 case TRUTH_ORIF_EXPR:
10655 case TRUTH_AND_EXPR:
10656 case TRUTH_OR_EXPR:
10657 case TRUTH_XOR_EXPR:
10658 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10659 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10660 || code0 == FIXED_POINT_TYPE)
3e4093b6 10661 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10662 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10663 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10664 {
10665 /* Result of these operations is always an int,
10666 but that does not mean the operands should be
10667 converted to ints! */
10668 result_type = integer_type_node;
a27d595d
JM
10669 if (op0_int_operands)
10670 {
10671 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10672 op0 = remove_c_maybe_const_expr (op0);
10673 }
10674 else
10675 op0 = c_objc_common_truthvalue_conversion (location, op0);
10676 if (op1_int_operands)
10677 {
10678 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10679 op1 = remove_c_maybe_const_expr (op1);
10680 }
10681 else
10682 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10683 converted = 1;
180f8dbb 10684 boolean_op = true;
3e4093b6 10685 }
928c19bb
JM
10686 if (code == TRUTH_ANDIF_EXPR)
10687 {
10688 int_const_or_overflow = (int_operands
10689 && TREE_CODE (orig_op0) == INTEGER_CST
10690 && (op0 == truthvalue_false_node
10691 || TREE_CODE (orig_op1) == INTEGER_CST));
10692 int_const = (int_const_or_overflow
10693 && !TREE_OVERFLOW (orig_op0)
10694 && (op0 == truthvalue_false_node
10695 || !TREE_OVERFLOW (orig_op1)));
10696 }
10697 else if (code == TRUTH_ORIF_EXPR)
10698 {
10699 int_const_or_overflow = (int_operands
10700 && TREE_CODE (orig_op0) == INTEGER_CST
10701 && (op0 == truthvalue_true_node
10702 || TREE_CODE (orig_op1) == INTEGER_CST));
10703 int_const = (int_const_or_overflow
10704 && !TREE_OVERFLOW (orig_op0)
10705 && (op0 == truthvalue_true_node
10706 || !TREE_OVERFLOW (orig_op1)));
10707 }
3e4093b6 10708 break;
eba80994 10709
3e4093b6
RS
10710 /* Shift operations: result has same type as first operand;
10711 always convert second operand to int.
10712 Also set SHORT_SHIFT if shifting rightward. */
de520661 10713
3e4093b6 10714 case RSHIFT_EXPR:
f87bd04b
AS
10715 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10716 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10717 {
10718 result_type = type0;
10719 converted = 1;
10720 }
10721 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
451b5e48
MP
10722 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10723 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10724 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
10725 {
10726 result_type = type0;
10727 converted = 1;
10728 }
10729 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
451b5e48 10730 && code1 == INTEGER_TYPE)
3e4093b6 10731 {
de5a5fa1 10732 doing_shift = true;
928c19bb 10733 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10734 {
3e4093b6 10735 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10736 {
10737 int_const = false;
7d882b83 10738 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10739 warning_at (location, OPT_Wshift_count_negative,
10740 "right shift count is negative");
928c19bb 10741 }
3e4093b6 10742 else
bbb818c6 10743 {
3f75a254 10744 if (!integer_zerop (op1))
3e4093b6
RS
10745 short_shift = 1;
10746
10747 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10748 {
10749 int_const = false;
7d882b83 10750 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10751 warning_at (location, OPT_Wshift_count_overflow,
10752 "right shift count >= width of type");
928c19bb 10753 }
bbb818c6 10754 }
b62acd60 10755 }
de520661 10756
3e4093b6
RS
10757 /* Use the type of the value to be shifted. */
10758 result_type = type0;
3e4093b6
RS
10759 /* Avoid converting op1 to result_type later. */
10760 converted = 1;
400fbf9f 10761 }
3e4093b6 10762 break;
253b6b82 10763
3e4093b6 10764 case LSHIFT_EXPR:
f87bd04b
AS
10765 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10766 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10767 {
10768 result_type = type0;
10769 converted = 1;
10770 }
10771 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
451b5e48
MP
10772 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10773 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10774 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
10775 {
10776 result_type = type0;
10777 converted = 1;
10778 }
10779 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
451b5e48 10780 && code1 == INTEGER_TYPE)
3e4093b6 10781 {
de5a5fa1 10782 doing_shift = true;
0173bd2a
MP
10783 if (TREE_CODE (op0) == INTEGER_CST
10784 && tree_int_cst_sgn (op0) < 0)
10785 {
10786 /* Don't reject a left shift of a negative value in a context
10787 where a constant expression is needed in C90. */
10788 if (flag_isoc99)
10789 int_const = false;
10790 if (c_inhibit_evaluation_warnings == 0)
10791 warning_at (location, OPT_Wshift_negative_value,
10792 "left shift of negative value");
10793 }
928c19bb 10794 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10795 {
3e4093b6 10796 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10797 {
10798 int_const = false;
7d882b83 10799 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10800 warning_at (location, OPT_Wshift_count_negative,
10801 "left shift count is negative");
928c19bb 10802 }
3e4093b6 10803 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10804 {
10805 int_const = false;
7d882b83 10806 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10807 warning_at (location, OPT_Wshift_count_overflow,
10808 "left shift count >= width of type");
928c19bb 10809 }
451b5e48
MP
10810 else if (TREE_CODE (op0) == INTEGER_CST
10811 && maybe_warn_shift_overflow (location, op0, op1)
10812 && flag_isoc99)
10813 int_const = false;
94ba5069 10814 }
de520661 10815
3e4093b6
RS
10816 /* Use the type of the value to be shifted. */
10817 result_type = type0;
3e4093b6
RS
10818 /* Avoid converting op1 to result_type later. */
10819 converted = 1;
400fbf9f 10820 }
3e4093b6 10821 break;
de520661 10822
3e4093b6
RS
10823 case EQ_EXPR:
10824 case NE_EXPR:
d246ab4f
AS
10825 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10826 {
10827 tree intt;
0af94e6f 10828 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10829 {
10830 error_at (location, "comparing vectors with different "
10831 "element types");
10832 return error_mark_node;
10833 }
10834
10835 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10836 {
10837 error_at (location, "comparing vectors with different "
10838 "number of elements");
10839 return error_mark_node;
10840 }
10841
10842 /* Always construct signed integer vector type. */
10843 intt = c_common_type_for_size (GET_MODE_BITSIZE
10844 (TYPE_MODE (TREE_TYPE (type0))), 0);
10845 result_type = build_opaque_vector_type (intt,
10846 TYPE_VECTOR_SUBPARTS (type0));
10847 converted = 1;
9f47c7e5
IE
10848 ret = build_vec_cmp (resultcode, result_type, op0, op1);
10849 goto return_build_binary_op;
d246ab4f 10850 }
ae311566 10851 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10852 warning_at (location,
10853 OPT_Wfloat_equal,
10854 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10855 /* Result of comparison is always int,
10856 but don't convert the args to int! */
10857 build_type = integer_type_node;
10858 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10859 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10860 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10861 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10862 short_compare = 1;
637f1455
SZ
10863 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10864 {
dbb68221
MW
10865 if (warn_nonnull
10866 && TREE_CODE (op0) == PARM_DECL && nonnull_arg_p (op0))
10867 warning_at (location, OPT_Wnonnull,
10868 "nonnull argument %qD compared to NULL", op0);
10869
637f1455
SZ
10870 if (TREE_CODE (op0) == ADDR_EXPR
10871 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10872 {
10873 if (code == EQ_EXPR)
10874 warning_at (location,
10875 OPT_Waddress,
10876 "the comparison will always evaluate as %<false%> "
10877 "for the address of %qD will never be NULL",
10878 TREE_OPERAND (op0, 0));
10879 else
10880 warning_at (location,
10881 OPT_Waddress,
10882 "the comparison will always evaluate as %<true%> "
10883 "for the address of %qD will never be NULL",
10884 TREE_OPERAND (op0, 0));
10885 }
10886 result_type = type0;
10887 }
10888 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10889 {
dbb68221
MW
10890 if (warn_nonnull
10891 && TREE_CODE (op1) == PARM_DECL && nonnull_arg_p (op1))
10892 warning_at (location, OPT_Wnonnull,
10893 "nonnull argument %qD compared to NULL", op1);
10894
637f1455
SZ
10895 if (TREE_CODE (op1) == ADDR_EXPR
10896 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10897 {
10898 if (code == EQ_EXPR)
10899 warning_at (location,
f90e8e2e 10900 OPT_Waddress,
637f1455
SZ
10901 "the comparison will always evaluate as %<false%> "
10902 "for the address of %qD will never be NULL",
10903 TREE_OPERAND (op1, 0));
10904 else
10905 warning_at (location,
10906 OPT_Waddress,
10907 "the comparison will always evaluate as %<true%> "
10908 "for the address of %qD will never be NULL",
10909 TREE_OPERAND (op1, 0));
10910 }
10911 result_type = type1;
10912 }
3e4093b6
RS
10913 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10914 {
10915 tree tt0 = TREE_TYPE (type0);
10916 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10917 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10918 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10919 addr_space_t as_common = ADDR_SPACE_GENERIC;
10920
3e4093b6
RS
10921 /* Anything compares with void *. void * compares with anything.
10922 Otherwise, the targets must be compatible
10923 and both must be object or both incomplete. */
744aa42f 10924 if (comp_target_types (location, type0, type1))
10bc1b1b 10925 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10926 else if (!addr_space_superset (as0, as1, &as_common))
10927 {
10928 error_at (location, "comparison of pointers to "
10929 "disjoint address spaces");
10930 return error_mark_node;
10931 }
267bac10 10932 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10933 {
36c5e70a 10934 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10935 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10936 "comparison of %<void *%> with function pointer");
ee2990e7 10937 }
267bac10 10938 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10939 {
36c5e70a 10940 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10941 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10942 "comparison of %<void *%> with function pointer");
e6834654 10943 }
3e4093b6 10944 else
58393038
ZL
10945 /* Avoid warning about the volatile ObjC EH puts on decls. */
10946 if (!objc_ok)
ba47d38d 10947 pedwarn (location, 0,
509c9d60 10948 "comparison of distinct pointer types lacks a cast");
e6834654 10949
3e4093b6 10950 if (result_type == NULL_TREE)
36c5e70a
BE
10951 {
10952 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10953 result_type = build_pointer_type
10954 (build_qualified_type (void_type_node, qual));
10955 }
e6834654 10956 }
3e4093b6 10957 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10958 {
3e4093b6 10959 result_type = type0;
ba47d38d 10960 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10961 }
3e4093b6 10962 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10963 {
3e4093b6 10964 result_type = type1;
ba47d38d 10965 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10966 }
04159acf
MP
10967 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10968 || truth_value_p (TREE_CODE (orig_op0)))
10969 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10970 || truth_value_p (TREE_CODE (orig_op1))))
10971 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10972 break;
8b6a5902 10973
3e4093b6
RS
10974 case LE_EXPR:
10975 case GE_EXPR:
10976 case LT_EXPR:
10977 case GT_EXPR:
d246ab4f
AS
10978 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10979 {
10980 tree intt;
0af94e6f 10981 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10982 {
10983 error_at (location, "comparing vectors with different "
10984 "element types");
10985 return error_mark_node;
10986 }
10987
10988 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10989 {
10990 error_at (location, "comparing vectors with different "
10991 "number of elements");
10992 return error_mark_node;
10993 }
10994
10995 /* Always construct signed integer vector type. */
10996 intt = c_common_type_for_size (GET_MODE_BITSIZE
10997 (TYPE_MODE (TREE_TYPE (type0))), 0);
10998 result_type = build_opaque_vector_type (intt,
10999 TYPE_VECTOR_SUBPARTS (type0));
11000 converted = 1;
9f47c7e5
IE
11001 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11002 goto return_build_binary_op;
d246ab4f 11003 }
3e4093b6 11004 build_type = integer_type_node;
ab22c1fa
CF
11005 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11006 || code0 == FIXED_POINT_TYPE)
11007 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11008 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11009 short_compare = 1;
11010 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11011 {
36c5e70a
BE
11012 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11013 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11014 addr_space_t as_common;
11015
744aa42f 11016 if (comp_target_types (location, type0, type1))
3e4093b6 11017 {
10bc1b1b 11018 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
11019 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11020 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 11021 pedwarn (location, 0,
509c9d60 11022 "comparison of complete and incomplete pointers");
fcf73884 11023 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 11024 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11025 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
11026 else if (null_pointer_constant_p (orig_op0)
11027 || null_pointer_constant_p (orig_op1))
11028 warning_at (location, OPT_Wextra,
11029 "ordered comparison of pointer with null pointer");
11030
3e4093b6 11031 }
36c5e70a
BE
11032 else if (!addr_space_superset (as0, as1, &as_common))
11033 {
11034 error_at (location, "comparison of pointers to "
11035 "disjoint address spaces");
11036 return error_mark_node;
11037 }
3e4093b6
RS
11038 else
11039 {
36c5e70a
BE
11040 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11041 result_type = build_pointer_type
11042 (build_qualified_type (void_type_node, qual));
ba47d38d 11043 pedwarn (location, 0,
509c9d60 11044 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
11045 }
11046 }
6aa3c60d 11047 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
11048 {
11049 result_type = type0;
fcf73884 11050 if (pedantic)
c1771a20 11051 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
11052 "ordered comparison of pointer with integer zero");
11053 else if (extra_warnings)
ba47d38d 11054 warning_at (location, OPT_Wextra,
d42ba3b8 11055 "ordered comparison of pointer with integer zero");
3e4093b6 11056 }
6aa3c60d 11057 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
11058 {
11059 result_type = type1;
d42ba3b8 11060 if (pedantic)
c1771a20 11061 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
11062 "ordered comparison of pointer with integer zero");
11063 else if (extra_warnings)
11064 warning_at (location, OPT_Wextra,
11065 "ordered comparison of pointer with integer zero");
3e4093b6
RS
11066 }
11067 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11068 {
11069 result_type = type0;
ba47d38d 11070 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
11071 }
11072 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11073 {
11074 result_type = type1;
ba47d38d 11075 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 11076 }
04159acf
MP
11077 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11078 || truth_value_p (TREE_CODE (orig_op0)))
11079 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11080 || truth_value_p (TREE_CODE (orig_op1))))
11081 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11082 break;
64094f6a 11083
3e4093b6 11084 default:
37b2f290 11085 gcc_unreachable ();
c9fe6f9f 11086 }
8f17b5c5 11087
e57e265b
PB
11088 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11089 return error_mark_node;
11090
5bed876a
AH
11091 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11092 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 11093 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 11094 {
ba47d38d 11095 binary_op_error (location, code, type0, type1);
5bed876a
AH
11096 return error_mark_node;
11097 }
11098
3e4093b6 11099 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 11100 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
11101 &&
11102 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 11103 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 11104 {
2ca862e9
JM
11105 bool first_complex = (code0 == COMPLEX_TYPE);
11106 bool second_complex = (code1 == COMPLEX_TYPE);
11107 int none_complex = (!first_complex && !second_complex);
39b726dd 11108
3e4093b6 11109 if (shorten || common || short_compare)
3bf6bfcc
JJ
11110 {
11111 result_type = c_common_type (type0, type1);
0a0b3574
MM
11112 do_warn_double_promotion (result_type, type0, type1,
11113 "implicit conversion from %qT to %qT "
11114 "to match other operand of binary "
11115 "expression",
11116 location);
3bf6bfcc
JJ
11117 if (result_type == error_mark_node)
11118 return error_mark_node;
11119 }
400fbf9f 11120
2ca862e9
JM
11121 if (first_complex != second_complex
11122 && (code == PLUS_EXPR
11123 || code == MINUS_EXPR
11124 || code == MULT_EXPR
11125 || (code == TRUNC_DIV_EXPR && first_complex))
11126 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11127 && flag_signed_zeros)
11128 {
11129 /* An operation on mixed real/complex operands must be
11130 handled specially, but the language-independent code can
11131 more easily optimize the plain complex arithmetic if
11132 -fno-signed-zeros. */
11133 tree real_type = TREE_TYPE (result_type);
11134 tree real, imag;
11135 if (type0 != orig_type0 || type1 != orig_type1)
11136 {
11137 gcc_assert (may_need_excess_precision && common);
2d2e923f 11138 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11139 }
11140 if (first_complex)
11141 {
11142 if (TREE_TYPE (op0) != result_type)
68fca595 11143 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11144 if (TREE_TYPE (op1) != real_type)
68fca595 11145 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11146 }
11147 else
11148 {
11149 if (TREE_TYPE (op0) != real_type)
68fca595 11150 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11151 if (TREE_TYPE (op1) != result_type)
68fca595 11152 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11153 }
11154 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11155 return error_mark_node;
11156 if (first_complex)
11157 {
11158 op0 = c_save_expr (op0);
11159 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11160 op0, 1);
11161 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11162 op0, 1);
11163 switch (code)
11164 {
11165 case MULT_EXPR:
11166 case TRUNC_DIV_EXPR:
c4603e7c 11167 op1 = c_save_expr (op1);
2ca862e9
JM
11168 imag = build2 (resultcode, real_type, imag, op1);
11169 /* Fall through. */
11170 case PLUS_EXPR:
11171 case MINUS_EXPR:
11172 real = build2 (resultcode, real_type, real, op1);
11173 break;
11174 default:
11175 gcc_unreachable();
11176 }
11177 }
11178 else
11179 {
11180 op1 = c_save_expr (op1);
11181 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11182 op1, 1);
11183 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11184 op1, 1);
11185 switch (code)
11186 {
11187 case MULT_EXPR:
c4603e7c 11188 op0 = c_save_expr (op0);
2ca862e9
JM
11189 imag = build2 (resultcode, real_type, op0, imag);
11190 /* Fall through. */
11191 case PLUS_EXPR:
11192 real = build2 (resultcode, real_type, op0, real);
11193 break;
11194 case MINUS_EXPR:
11195 real = build2 (resultcode, real_type, op0, real);
11196 imag = build1 (NEGATE_EXPR, real_type, imag);
11197 break;
11198 default:
11199 gcc_unreachable();
11200 }
11201 }
11202 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11203 goto return_build_binary_op;
11204 }
11205
3e4093b6
RS
11206 /* For certain operations (which identify themselves by shorten != 0)
11207 if both args were extended from the same smaller type,
11208 do the arithmetic in that type and then extend.
400fbf9f 11209
3e4093b6
RS
11210 shorten !=0 and !=1 indicates a bitwise operation.
11211 For them, this optimization is safe only if
11212 both args are zero-extended or both are sign-extended.
11213 Otherwise, we might change the result.
11214 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11215 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11216
3e4093b6
RS
11217 if (shorten && none_complex)
11218 {
3e4093b6 11219 final_type = result_type;
b8698a0f 11220 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11221 shorten == -1);
3e4093b6 11222 }
88a3dbc1 11223
3e4093b6 11224 /* Shifts can be shortened if shifting right. */
2f6e4e97 11225
3e4093b6
RS
11226 if (short_shift)
11227 {
11228 int unsigned_arg;
11229 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11230
3e4093b6 11231 final_type = result_type;
abe80e6d 11232
3e4093b6 11233 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11234 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11235
3e4093b6 11236 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11237 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11238 /* We can shorten only if the shift count is less than the
11239 number of bits in the smaller type size. */
11240 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11241 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11242 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11243 {
11244 /* Do an unsigned shift if the operand was zero-extended. */
11245 result_type
11246 = c_common_signed_or_unsigned_type (unsigned_arg,
11247 TREE_TYPE (arg0));
11248 /* Convert value-to-be-shifted to that type. */
11249 if (TREE_TYPE (op0) != result_type)
11250 op0 = convert (result_type, op0);
11251 converted = 1;
abe80e6d 11252 }
88a3dbc1
RK
11253 }
11254
3e4093b6
RS
11255 /* Comparison operations are shortened too but differently.
11256 They identify themselves by setting short_compare = 1. */
56cb9733 11257
3e4093b6
RS
11258 if (short_compare)
11259 {
11260 /* Don't write &op0, etc., because that would prevent op0
11261 from being kept in a register.
11262 Instead, make copies of the our local variables and
11263 pass the copies by reference, then copy them back afterward. */
11264 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11265 enum tree_code xresultcode = resultcode;
11266 tree val
393e8e8b
MP
11267 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11268 &xresultcode);
8f17b5c5 11269
3e4093b6 11270 if (val != 0)
c9f9eb5d
AH
11271 {
11272 ret = val;
11273 goto return_build_binary_op;
11274 }
8f17b5c5 11275
3e4093b6
RS
11276 op0 = xop0, op1 = xop1;
11277 converted = 1;
11278 resultcode = xresultcode;
8f17b5c5 11279
7d882b83 11280 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11281 {
11282 bool op0_maybe_const = true;
11283 bool op1_maybe_const = true;
11284 tree orig_op0_folded, orig_op1_folded;
11285
11286 if (in_late_binary_op)
11287 {
11288 orig_op0_folded = orig_op0;
11289 orig_op1_folded = orig_op1;
11290 }
11291 else
11292 {
11293 /* Fold for the sake of possible warnings, as in
11294 build_conditional_expr. This requires the
11295 "original" values to be folded, not just op0 and
11296 op1. */
f5178456 11297 c_inhibit_evaluation_warnings++;
928c19bb
JM
11298 op0 = c_fully_fold (op0, require_constant_value,
11299 &op0_maybe_const);
11300 op1 = c_fully_fold (op1, require_constant_value,
11301 &op1_maybe_const);
f5178456 11302 c_inhibit_evaluation_warnings--;
928c19bb
JM
11303 orig_op0_folded = c_fully_fold (orig_op0,
11304 require_constant_value,
11305 NULL);
11306 orig_op1_folded = c_fully_fold (orig_op1,
11307 require_constant_value,
11308 NULL);
11309 }
11310
11311 if (warn_sign_compare)
11312 warn_for_sign_compare (location, orig_op0_folded,
11313 orig_op1_folded, op0, op1,
11314 result_type, resultcode);
5c2f94b4 11315 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11316 {
11317 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11318 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11319 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11320 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11321 }
3e4093b6 11322 }
2ad1815d 11323 }
64094f6a 11324 }
64094f6a 11325
3e4093b6
RS
11326 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11327 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11328 Then the expression will be built.
11329 It will be given type FINAL_TYPE if that is nonzero;
11330 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11331
3e4093b6
RS
11332 if (!result_type)
11333 {
ba47d38d 11334 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11335 return error_mark_node;
11336 }
400fbf9f 11337
3e4093b6 11338 if (build_type == NULL_TREE)
8ce94e44
JM
11339 {
11340 build_type = result_type;
180f8dbb
JM
11341 if ((type0 != orig_type0 || type1 != orig_type1)
11342 && !boolean_op)
8ce94e44
JM
11343 {
11344 gcc_assert (may_need_excess_precision && common);
2d2e923f 11345 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11346 }
11347 }
400fbf9f 11348
2d2e923f
MLI
11349 if (!converted)
11350 {
68fca595
MP
11351 op0 = ep_convert_and_check (location, result_type, op0,
11352 semantic_result_type);
11353 op1 = ep_convert_and_check (location, result_type, op1,
11354 semantic_result_type);
2d2e923f
MLI
11355
11356 /* This can happen if one operand has a vector type, and the other
11357 has a different type. */
11358 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11359 return error_mark_node;
11360 }
11361
f8ed5150
MP
11362 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11363 | SANITIZE_FLOAT_DIVIDE))
802ac282 11364 && do_ubsan_in_current_function ()
b2aaf235
MP
11365 && (doing_div_or_mod || doing_shift)
11366 && !require_constant_value)
de5a5fa1
MP
11367 {
11368 /* OP0 and/or OP1 might have side-effects. */
11369 op0 = c_save_expr (op0);
11370 op1 = c_save_expr (op1);
11371 op0 = c_fully_fold (op0, false, NULL);
11372 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11373 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11374 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11375 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11376 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11377 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11378 }
11379
c9f9eb5d 11380 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11381 if (int_const_or_overflow)
11382 ret = (require_constant_value
db3927fb
AH
11383 ? fold_build2_initializer_loc (location, resultcode, build_type,
11384 op0, op1)
11385 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11386 else
11387 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11388 if (final_type != 0)
11389 ret = convert (final_type, ret);
11390
11391 return_build_binary_op:
11392 gcc_assert (ret != error_mark_node);
928c19bb
JM
11393 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11394 ret = (int_operands
11395 ? note_integer_operands (ret)
11396 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11397 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11398 && !in_late_binary_op)
11399 ret = note_integer_operands (ret);
2d2e923f
MLI
11400 if (semantic_result_type)
11401 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11402 protected_set_expr_location (ret, location);
de5a5fa1 11403
a24d975c 11404 if (instrument_expr != NULL)
de5a5fa1
MP
11405 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11406 instrument_expr, ret);
11407
c9f9eb5d 11408 return ret;
400fbf9f 11409}
85498824
JM
11410
11411
11412/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11413 purpose. LOCATION is the source location for the expression. */
85498824
JM
11414
11415tree
ba47d38d 11416c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11417{
928c19bb
JM
11418 bool int_const, int_operands;
11419
85498824
JM
11420 switch (TREE_CODE (TREE_TYPE (expr)))
11421 {
11422 case ARRAY_TYPE:
ba47d38d 11423 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11424 return error_mark_node;
11425
11426 case RECORD_TYPE:
ba47d38d 11427 error_at (location, "used struct type value where scalar is required");
85498824
JM
11428 return error_mark_node;
11429
11430 case UNION_TYPE:
ba47d38d 11431 error_at (location, "used union type value where scalar is required");
85498824
JM
11432 return error_mark_node;
11433
04af8788
NP
11434 case VOID_TYPE:
11435 error_at (location, "void value not ignored as it ought to be");
11436 return error_mark_node;
11437
1807ffc1
MS
11438 case POINTER_TYPE:
11439 if (reject_gcc_builtin (expr))
11440 return error_mark_node;
11441 break;
11442
46bdb9cf
JM
11443 case FUNCTION_TYPE:
11444 gcc_unreachable ();
11445
d246ab4f
AS
11446 case VECTOR_TYPE:
11447 error_at (location, "used vector type where scalar is required");
11448 return error_mark_node;
11449
85498824
JM
11450 default:
11451 break;
11452 }
11453
928c19bb
JM
11454 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11455 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11456 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11457 {
11458 expr = remove_c_maybe_const_expr (expr);
11459 expr = build2 (NE_EXPR, integer_type_node, expr,
11460 convert (TREE_TYPE (expr), integer_zero_node));
11461 expr = note_integer_operands (expr);
11462 }
11463 else
11464 /* ??? Should we also give an error for vectors rather than leaving
11465 those to give errors later? */
11466 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11467
11468 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11469 {
11470 if (TREE_OVERFLOW (expr))
11471 return expr;
11472 else
11473 return note_integer_operands (expr);
11474 }
11475 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11476 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11477 return expr;
85498824 11478}
73f397d4
JM
11479\f
11480
11481/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11482 required. */
11483
11484tree
51eed280 11485c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11486{
11487 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11488 {
11489 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11490 /* Executing a compound literal inside a function reinitializes
11491 it. */
11492 if (!TREE_STATIC (decl))
11493 *se = true;
11494 return decl;
11495 }
11496 else
11497 return expr;
11498}
953ff289 11499\f
88bae6f4
TS
11500/* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11501 statement. LOC is the location of the construct. */
41dbbb37
TS
11502
11503tree
88bae6f4
TS
11504c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11505 tree clauses)
41dbbb37 11506{
88bae6f4 11507 body = c_end_compound_stmt (loc, body, true);
41dbbb37 11508
88bae6f4 11509 tree stmt = make_node (code);
41dbbb37 11510 TREE_TYPE (stmt) = void_type_node;
88bae6f4
TS
11511 OMP_BODY (stmt) = body;
11512 OMP_CLAUSES (stmt) = clauses;
41dbbb37
TS
11513 SET_EXPR_LOCATION (stmt, loc);
11514
11515 return add_stmt (stmt);
11516}
11517
11518/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11519 statement. LOC is the location of the OACC_DATA. */
11520
11521tree
11522c_finish_oacc_data (location_t loc, tree clauses, tree block)
11523{
11524 tree stmt;
11525
11526 block = c_end_compound_stmt (loc, block, true);
11527
11528 stmt = make_node (OACC_DATA);
11529 TREE_TYPE (stmt) = void_type_node;
11530 OACC_DATA_CLAUSES (stmt) = clauses;
11531 OACC_DATA_BODY (stmt) = block;
11532 SET_EXPR_LOCATION (stmt, loc);
11533
11534 return add_stmt (stmt);
11535}
11536
c0220ea4 11537/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11538
11539tree
11540c_begin_omp_parallel (void)
11541{
11542 tree block;
11543
11544 keep_next_level ();
11545 block = c_begin_compound_stmt (true);
11546
11547 return block;
11548}
11549
c2255bc4
AH
11550/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11551 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11552
953ff289 11553tree
c2255bc4 11554c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11555{
11556 tree stmt;
11557
c2255bc4 11558 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11559
11560 stmt = make_node (OMP_PARALLEL);
11561 TREE_TYPE (stmt) = void_type_node;
11562 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11563 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11564 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11565
11566 return add_stmt (stmt);
11567}
11568
a68ab351
JJ
11569/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11570
11571tree
11572c_begin_omp_task (void)
11573{
11574 tree block;
11575
11576 keep_next_level ();
11577 block = c_begin_compound_stmt (true);
11578
11579 return block;
11580}
11581
c2255bc4
AH
11582/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11583 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11584
11585tree
c2255bc4 11586c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11587{
11588 tree stmt;
11589
c2255bc4 11590 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11591
11592 stmt = make_node (OMP_TASK);
11593 TREE_TYPE (stmt) = void_type_node;
11594 OMP_TASK_CLAUSES (stmt) = clauses;
11595 OMP_TASK_BODY (stmt) = block;
c2255bc4 11596 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11597
11598 return add_stmt (stmt);
11599}
11600
acf0174b
JJ
11601/* Generate GOMP_cancel call for #pragma omp cancel. */
11602
11603void
11604c_finish_omp_cancel (location_t loc, tree clauses)
11605{
11606 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11607 int mask = 0;
11608 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11609 mask = 1;
11610 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11611 mask = 2;
11612 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11613 mask = 4;
11614 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11615 mask = 8;
11616 else
11617 {
11618 error_at (loc, "%<#pragma omp cancel must specify one of "
11619 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11620 "clauses");
11621 return;
11622 }
11623 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11624 if (ifc != NULL_TREE)
11625 {
11626 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11627 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11628 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11629 build_zero_cst (type));
11630 }
11631 else
11632 ifc = boolean_true_node;
11633 tree stmt = build_call_expr_loc (loc, fn, 2,
11634 build_int_cst (integer_type_node, mask),
11635 ifc);
11636 add_stmt (stmt);
11637}
11638
11639/* Generate GOMP_cancellation_point call for
11640 #pragma omp cancellation point. */
11641
11642void
11643c_finish_omp_cancellation_point (location_t loc, tree clauses)
11644{
11645 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11646 int mask = 0;
11647 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11648 mask = 1;
11649 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11650 mask = 2;
11651 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11652 mask = 4;
11653 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11654 mask = 8;
11655 else
11656 {
11657 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11658 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11659 "clauses");
11660 return;
11661 }
11662 tree stmt = build_call_expr_loc (loc, fn, 1,
11663 build_int_cst (integer_type_node, mask));
11664 add_stmt (stmt);
11665}
11666
11667/* Helper function for handle_omp_array_sections. Called recursively
11668 to handle multiple array-section-subscripts. C is the clause,
11669 T current expression (initially OMP_CLAUSE_DECL), which is either
11670 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11671 expression if specified, TREE_VALUE length expression if specified,
11672 TREE_CHAIN is what it has been specified after, or some decl.
11673 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11674 set to true if any of the array-section-subscript could have length
11675 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11676 first array-section-subscript which is known not to have length
11677 of one. Given say:
11678 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11679 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11680 all are or may have length of 1, array-section-subscript [:2] is the
d9a6bd32 11681 first one known not to have length 1. For array-section-subscript
acf0174b
JJ
11682 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11683 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11684 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11685 case though, as some lengths could be zero. */
11686
11687static tree
11688handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
d9a6bd32
JJ
11689 bool &maybe_zero_len, unsigned int &first_non_one,
11690 bool is_omp)
acf0174b
JJ
11691{
11692 tree ret, low_bound, length, type;
11693 if (TREE_CODE (t) != TREE_LIST)
11694 {
7a0ca710 11695 if (error_operand_p (t))
acf0174b 11696 return error_mark_node;
d9a6bd32
JJ
11697 ret = t;
11698 if (TREE_CODE (t) == COMPONENT_REF
11699 && is_omp
11700 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
11701 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
11702 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
11703 {
11704 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
11705 {
11706 error_at (OMP_CLAUSE_LOCATION (c),
11707 "bit-field %qE in %qs clause",
11708 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11709 return error_mark_node;
11710 }
11711 while (TREE_CODE (t) == COMPONENT_REF)
11712 {
11713 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
11714 {
11715 error_at (OMP_CLAUSE_LOCATION (c),
11716 "%qE is a member of a union", t);
11717 return error_mark_node;
11718 }
11719 t = TREE_OPERAND (t, 0);
11720 }
11721 }
0ae9bd27 11722 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
11723 {
11724 if (DECL_P (t))
11725 error_at (OMP_CLAUSE_LOCATION (c),
11726 "%qD is not a variable in %qs clause", t,
11727 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11728 else
11729 error_at (OMP_CLAUSE_LOCATION (c),
11730 "%qE is not a variable in %qs clause", t,
11731 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11732 return error_mark_node;
11733 }
11734 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
0ae9bd27 11735 && VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
11736 {
11737 error_at (OMP_CLAUSE_LOCATION (c),
11738 "%qD is threadprivate variable in %qs clause", t,
11739 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11740 return error_mark_node;
11741 }
d9a6bd32 11742 return ret;
acf0174b
JJ
11743 }
11744
11745 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
d9a6bd32 11746 maybe_zero_len, first_non_one, is_omp);
acf0174b
JJ
11747 if (ret == error_mark_node || ret == NULL_TREE)
11748 return ret;
11749
11750 type = TREE_TYPE (ret);
11751 low_bound = TREE_PURPOSE (t);
11752 length = TREE_VALUE (t);
11753
11754 if (low_bound == error_mark_node || length == error_mark_node)
11755 return error_mark_node;
11756
11757 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11758 {
11759 error_at (OMP_CLAUSE_LOCATION (c),
11760 "low bound %qE of array section does not have integral type",
11761 low_bound);
11762 return error_mark_node;
11763 }
11764 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11765 {
11766 error_at (OMP_CLAUSE_LOCATION (c),
11767 "length %qE of array section does not have integral type",
11768 length);
11769 return error_mark_node;
11770 }
11771 if (low_bound
11772 && TREE_CODE (low_bound) == INTEGER_CST
11773 && TYPE_PRECISION (TREE_TYPE (low_bound))
11774 > TYPE_PRECISION (sizetype))
11775 low_bound = fold_convert (sizetype, low_bound);
11776 if (length
11777 && TREE_CODE (length) == INTEGER_CST
11778 && TYPE_PRECISION (TREE_TYPE (length))
11779 > TYPE_PRECISION (sizetype))
11780 length = fold_convert (sizetype, length);
11781 if (low_bound == NULL_TREE)
11782 low_bound = integer_zero_node;
11783
11784 if (length != NULL_TREE)
11785 {
11786 if (!integer_nonzerop (length))
d9a6bd32
JJ
11787 {
11788 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11789 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
11790 {
11791 if (integer_zerop (length))
11792 {
11793 error_at (OMP_CLAUSE_LOCATION (c),
11794 "zero length array section in %qs clause",
11795 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11796 return error_mark_node;
11797 }
11798 }
11799 else
11800 maybe_zero_len = true;
11801 }
acf0174b
JJ
11802 if (first_non_one == types.length ()
11803 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11804 first_non_one++;
11805 }
11806 if (TREE_CODE (type) == ARRAY_TYPE)
11807 {
11808 if (length == NULL_TREE
11809 && (TYPE_DOMAIN (type) == NULL_TREE
11810 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11811 {
11812 error_at (OMP_CLAUSE_LOCATION (c),
11813 "for unknown bound array type length expression must "
11814 "be specified");
11815 return error_mark_node;
11816 }
11817 if (TREE_CODE (low_bound) == INTEGER_CST
11818 && tree_int_cst_sgn (low_bound) == -1)
11819 {
11820 error_at (OMP_CLAUSE_LOCATION (c),
11821 "negative low bound in array section in %qs clause",
11822 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11823 return error_mark_node;
11824 }
11825 if (length != NULL_TREE
11826 && TREE_CODE (length) == INTEGER_CST
11827 && tree_int_cst_sgn (length) == -1)
11828 {
11829 error_at (OMP_CLAUSE_LOCATION (c),
11830 "negative length in array section in %qs clause",
11831 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11832 return error_mark_node;
11833 }
11834 if (TYPE_DOMAIN (type)
11835 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11836 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11837 == INTEGER_CST)
11838 {
11839 tree size = size_binop (PLUS_EXPR,
11840 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11841 size_one_node);
11842 if (TREE_CODE (low_bound) == INTEGER_CST)
11843 {
11844 if (tree_int_cst_lt (size, low_bound))
11845 {
11846 error_at (OMP_CLAUSE_LOCATION (c),
11847 "low bound %qE above array section size "
11848 "in %qs clause", low_bound,
11849 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11850 return error_mark_node;
11851 }
11852 if (tree_int_cst_equal (size, low_bound))
d9a6bd32
JJ
11853 {
11854 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11855 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
11856 {
11857 error_at (OMP_CLAUSE_LOCATION (c),
11858 "zero length array section in %qs clause",
11859 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11860 return error_mark_node;
11861 }
11862 maybe_zero_len = true;
11863 }
acf0174b
JJ
11864 else if (length == NULL_TREE
11865 && first_non_one == types.length ()
11866 && tree_int_cst_equal
11867 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11868 low_bound))
11869 first_non_one++;
11870 }
11871 else if (length == NULL_TREE)
11872 {
d9a6bd32
JJ
11873 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11874 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
11875 maybe_zero_len = true;
acf0174b
JJ
11876 if (first_non_one == types.length ())
11877 first_non_one++;
11878 }
11879 if (length && TREE_CODE (length) == INTEGER_CST)
11880 {
11881 if (tree_int_cst_lt (size, length))
11882 {
11883 error_at (OMP_CLAUSE_LOCATION (c),
11884 "length %qE above array section size "
11885 "in %qs clause", length,
11886 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11887 return error_mark_node;
11888 }
11889 if (TREE_CODE (low_bound) == INTEGER_CST)
11890 {
11891 tree lbpluslen
11892 = size_binop (PLUS_EXPR,
11893 fold_convert (sizetype, low_bound),
11894 fold_convert (sizetype, length));
11895 if (TREE_CODE (lbpluslen) == INTEGER_CST
11896 && tree_int_cst_lt (size, lbpluslen))
11897 {
11898 error_at (OMP_CLAUSE_LOCATION (c),
11899 "high bound %qE above array section size "
11900 "in %qs clause", lbpluslen,
11901 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11902 return error_mark_node;
11903 }
11904 }
11905 }
11906 }
11907 else if (length == NULL_TREE)
11908 {
d9a6bd32
JJ
11909 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11910 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
11911 maybe_zero_len = true;
acf0174b
JJ
11912 if (first_non_one == types.length ())
11913 first_non_one++;
11914 }
11915
11916 /* For [lb:] we will need to evaluate lb more than once. */
11917 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11918 {
11919 tree lb = c_save_expr (low_bound);
11920 if (lb != low_bound)
11921 {
11922 TREE_PURPOSE (t) = lb;
11923 low_bound = lb;
11924 }
11925 }
11926 }
11927 else if (TREE_CODE (type) == POINTER_TYPE)
11928 {
11929 if (length == NULL_TREE)
11930 {
11931 error_at (OMP_CLAUSE_LOCATION (c),
11932 "for pointer type length expression must be specified");
11933 return error_mark_node;
11934 }
d9a6bd32
JJ
11935 if (length != NULL_TREE
11936 && TREE_CODE (length) == INTEGER_CST
11937 && tree_int_cst_sgn (length) == -1)
11938 {
11939 error_at (OMP_CLAUSE_LOCATION (c),
11940 "negative length in array section in %qs clause",
11941 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11942 return error_mark_node;
11943 }
acf0174b
JJ
11944 /* If there is a pointer type anywhere but in the very first
11945 array-section-subscript, the array section can't be contiguous. */
11946 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11947 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11948 {
11949 error_at (OMP_CLAUSE_LOCATION (c),
11950 "array section is not contiguous in %qs clause",
11951 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11952 return error_mark_node;
11953 }
11954 }
11955 else
11956 {
11957 error_at (OMP_CLAUSE_LOCATION (c),
11958 "%qE does not have pointer or array type", ret);
11959 return error_mark_node;
11960 }
11961 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11962 types.safe_push (TREE_TYPE (ret));
11963 /* We will need to evaluate lb more than once. */
11964 tree lb = c_save_expr (low_bound);
11965 if (lb != low_bound)
11966 {
11967 TREE_PURPOSE (t) = lb;
11968 low_bound = lb;
11969 }
11970 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11971 return ret;
11972}
11973
11974/* Handle array sections for clause C. */
11975
11976static bool
d9a6bd32 11977handle_omp_array_sections (tree c, bool is_omp)
acf0174b
JJ
11978{
11979 bool maybe_zero_len = false;
11980 unsigned int first_non_one = 0;
d9a6bd32 11981 auto_vec<tree, 10> types;
acf0174b 11982 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
d9a6bd32
JJ
11983 maybe_zero_len, first_non_one,
11984 is_omp);
acf0174b 11985 if (first == error_mark_node)
d9a6bd32 11986 return true;
acf0174b 11987 if (first == NULL_TREE)
d9a6bd32 11988 return false;
acf0174b
JJ
11989 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11990 {
11991 tree t = OMP_CLAUSE_DECL (c);
11992 tree tem = NULL_TREE;
acf0174b
JJ
11993 /* Need to evaluate side effects in the length expressions
11994 if any. */
11995 while (TREE_CODE (t) == TREE_LIST)
11996 {
11997 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11998 {
11999 if (tem == NULL_TREE)
12000 tem = TREE_VALUE (t);
12001 else
12002 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12003 TREE_VALUE (t), tem);
12004 }
12005 t = TREE_CHAIN (t);
12006 }
12007 if (tem)
12008 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12009 first = c_fully_fold (first, false, NULL);
12010 OMP_CLAUSE_DECL (c) = first;
12011 }
12012 else
12013 {
12014 unsigned int num = types.length (), i;
12015 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12016 tree condition = NULL_TREE;
12017
12018 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12019 maybe_zero_len = true;
12020
12021 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12022 t = TREE_CHAIN (t))
12023 {
12024 tree low_bound = TREE_PURPOSE (t);
12025 tree length = TREE_VALUE (t);
12026
12027 i--;
12028 if (low_bound
12029 && TREE_CODE (low_bound) == INTEGER_CST
12030 && TYPE_PRECISION (TREE_TYPE (low_bound))
12031 > TYPE_PRECISION (sizetype))
12032 low_bound = fold_convert (sizetype, low_bound);
12033 if (length
12034 && TREE_CODE (length) == INTEGER_CST
12035 && TYPE_PRECISION (TREE_TYPE (length))
12036 > TYPE_PRECISION (sizetype))
12037 length = fold_convert (sizetype, length);
12038 if (low_bound == NULL_TREE)
12039 low_bound = integer_zero_node;
12040 if (!maybe_zero_len && i > first_non_one)
12041 {
12042 if (integer_nonzerop (low_bound))
12043 goto do_warn_noncontiguous;
12044 if (length != NULL_TREE
12045 && TREE_CODE (length) == INTEGER_CST
12046 && TYPE_DOMAIN (types[i])
12047 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12048 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12049 == INTEGER_CST)
12050 {
12051 tree size;
12052 size = size_binop (PLUS_EXPR,
12053 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12054 size_one_node);
12055 if (!tree_int_cst_equal (length, size))
12056 {
12057 do_warn_noncontiguous:
12058 error_at (OMP_CLAUSE_LOCATION (c),
12059 "array section is not contiguous in %qs "
12060 "clause",
12061 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
acf0174b
JJ
12062 return true;
12063 }
12064 }
12065 if (length != NULL_TREE
12066 && TREE_SIDE_EFFECTS (length))
12067 {
12068 if (side_effects == NULL_TREE)
12069 side_effects = length;
12070 else
12071 side_effects = build2 (COMPOUND_EXPR,
12072 TREE_TYPE (side_effects),
12073 length, side_effects);
12074 }
12075 }
12076 else
12077 {
12078 tree l;
12079
d9a6bd32
JJ
12080 if (i > first_non_one
12081 && ((length && integer_nonzerop (length))
12082 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
acf0174b
JJ
12083 continue;
12084 if (length)
12085 l = fold_convert (sizetype, length);
12086 else
12087 {
12088 l = size_binop (PLUS_EXPR,
12089 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12090 size_one_node);
12091 l = size_binop (MINUS_EXPR, l,
12092 fold_convert (sizetype, low_bound));
12093 }
12094 if (i > first_non_one)
12095 {
12096 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12097 size_zero_node);
12098 if (condition == NULL_TREE)
12099 condition = l;
12100 else
12101 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12102 l, condition);
12103 }
12104 else if (size == NULL_TREE)
12105 {
12106 size = size_in_bytes (TREE_TYPE (types[i]));
d9a6bd32
JJ
12107 tree eltype = TREE_TYPE (types[num - 1]);
12108 while (TREE_CODE (eltype) == ARRAY_TYPE)
12109 eltype = TREE_TYPE (eltype);
12110 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12111 {
12112 if (integer_zerop (size)
12113 || integer_zerop (size_in_bytes (eltype)))
12114 {
12115 error_at (OMP_CLAUSE_LOCATION (c),
12116 "zero length array section in %qs clause",
12117 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12118 return error_mark_node;
12119 }
12120 size = size_binop (EXACT_DIV_EXPR, size,
12121 size_in_bytes (eltype));
12122 }
acf0174b
JJ
12123 size = size_binop (MULT_EXPR, size, l);
12124 if (condition)
12125 size = fold_build3 (COND_EXPR, sizetype, condition,
12126 size, size_zero_node);
12127 }
12128 else
12129 size = size_binop (MULT_EXPR, size, l);
12130 }
12131 }
acf0174b
JJ
12132 if (side_effects)
12133 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
d9a6bd32
JJ
12134 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12135 {
12136 size = size_binop (MINUS_EXPR, size, size_one_node);
12137 size = c_fully_fold (size, false, NULL);
12138 tree index_type = build_index_type (size);
12139 tree eltype = TREE_TYPE (first);
12140 while (TREE_CODE (eltype) == ARRAY_TYPE)
12141 eltype = TREE_TYPE (eltype);
12142 tree type = build_array_type (eltype, index_type);
12143 tree ptype = build_pointer_type (eltype);
12144 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12145 t = build_fold_addr_expr (t);
e01d41e5
JJ
12146 tree t2 = build_fold_addr_expr (first);
12147 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12148 ptrdiff_type_node, t2);
12149 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12150 ptrdiff_type_node, t2,
12151 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12152 ptrdiff_type_node, t));
12153 t2 = c_fully_fold (t2, false, NULL);
12154 if (tree_fits_shwi_p (t2))
12155 t = build2 (MEM_REF, type, t,
12156 build_int_cst (ptype, tree_to_shwi (t2)));
12157 else
12158 {
12159 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12160 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12161 TREE_TYPE (t), t, t2);
12162 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12163 }
d9a6bd32
JJ
12164 OMP_CLAUSE_DECL (c) = t;
12165 return false;
12166 }
acf0174b
JJ
12167 first = c_fully_fold (first, false, NULL);
12168 OMP_CLAUSE_DECL (c) = first;
12169 if (size)
12170 size = c_fully_fold (size, false, NULL);
12171 OMP_CLAUSE_SIZE (c) = size;
d9a6bd32
JJ
12172 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12173 || (TREE_CODE (t) == COMPONENT_REF
12174 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
acf0174b 12175 return false;
41dbbb37 12176 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
d9a6bd32
JJ
12177 if (is_omp)
12178 switch (OMP_CLAUSE_MAP_KIND (c))
12179 {
12180 case GOMP_MAP_ALLOC:
12181 case GOMP_MAP_TO:
12182 case GOMP_MAP_FROM:
12183 case GOMP_MAP_TOFROM:
12184 case GOMP_MAP_ALWAYS_TO:
12185 case GOMP_MAP_ALWAYS_FROM:
12186 case GOMP_MAP_ALWAYS_TOFROM:
12187 case GOMP_MAP_RELEASE:
12188 case GOMP_MAP_DELETE:
12189 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12190 break;
12191 default:
12192 break;
12193 }
acf0174b 12194 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
e01d41e5
JJ
12195 if (!is_omp)
12196 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12197 else if (TREE_CODE (t) == COMPONENT_REF)
12198 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12199 else
12200 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12201 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12202 && !c_mark_addressable (t))
acf0174b
JJ
12203 return false;
12204 OMP_CLAUSE_DECL (c2) = t;
12205 t = build_fold_addr_expr (first);
12206 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12207 tree ptr = OMP_CLAUSE_DECL (c2);
12208 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12209 ptr = build_fold_addr_expr (ptr);
12210 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12211 ptrdiff_type_node, t,
12212 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12213 ptrdiff_type_node, ptr));
12214 t = c_fully_fold (t, false, NULL);
12215 OMP_CLAUSE_SIZE (c2) = t;
12216 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12217 OMP_CLAUSE_CHAIN (c) = c2;
12218 }
12219 return false;
12220}
12221
12222/* Helper function of finish_omp_clauses. Clone STMT as if we were making
12223 an inline call. But, remap
12224 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12225 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12226
12227static tree
12228c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12229 tree decl, tree placeholder)
12230{
12231 copy_body_data id;
b787e7a2 12232 hash_map<tree, tree> decl_map;
acf0174b 12233
b787e7a2
TS
12234 decl_map.put (omp_decl1, placeholder);
12235 decl_map.put (omp_decl2, decl);
acf0174b
JJ
12236 memset (&id, 0, sizeof (id));
12237 id.src_fn = DECL_CONTEXT (omp_decl1);
12238 id.dst_fn = current_function_decl;
12239 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 12240 id.decl_map = &decl_map;
acf0174b
JJ
12241
12242 id.copy_decl = copy_decl_no_change;
12243 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12244 id.transform_new_cfg = true;
12245 id.transform_return_to_modify = false;
12246 id.transform_lang_insert_block = NULL;
12247 id.eh_lp_nr = 0;
12248 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
12249 return stmt;
12250}
12251
12252/* Helper function of c_finish_omp_clauses, called via walk_tree.
12253 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12254
12255static tree
12256c_find_omp_placeholder_r (tree *tp, int *, void *data)
12257{
12258 if (*tp == (tree) data)
12259 return *tp;
12260 return NULL_TREE;
12261}
12262
41dbbb37 12263/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
12264 Remove any elements from the list that are invalid. */
12265
12266tree
d9a6bd32 12267c_finish_omp_clauses (tree clauses, bool is_omp, bool declare_simd)
953ff289
DN
12268{
12269 bitmap_head generic_head, firstprivate_head, lastprivate_head;
e01d41e5 12270 bitmap_head aligned_head, map_head, map_field_head;
d9a6bd32
JJ
12271 tree c, t, type, *pc;
12272 tree simdlen = NULL_TREE, safelen = NULL_TREE;
acf0174b
JJ
12273 bool branch_seen = false;
12274 bool copyprivate_seen = false;
e01d41e5 12275 bool linear_variable_step_check = false;
acf0174b 12276 tree *nowait_clause = NULL;
e01d41e5
JJ
12277 bool ordered_seen = false;
12278 tree schedule_clause = NULL_TREE;
953ff289
DN
12279
12280 bitmap_obstack_initialize (NULL);
12281 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12282 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12283 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 12284 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
d9a6bd32
JJ
12285 bitmap_initialize (&map_head, &bitmap_default_obstack);
12286 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
953ff289
DN
12287
12288 for (pc = &clauses, c = clauses; c ; c = *pc)
12289 {
12290 bool remove = false;
12291 bool need_complete = false;
12292 bool need_implicitly_determined = false;
12293
aaf46ef9 12294 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
12295 {
12296 case OMP_CLAUSE_SHARED:
953ff289
DN
12297 need_implicitly_determined = true;
12298 goto check_dup_generic;
12299
12300 case OMP_CLAUSE_PRIVATE:
953ff289
DN
12301 need_complete = true;
12302 need_implicitly_determined = true;
12303 goto check_dup_generic;
12304
12305 case OMP_CLAUSE_REDUCTION:
953ff289
DN
12306 need_implicitly_determined = true;
12307 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12308 if (TREE_CODE (t) == TREE_LIST)
12309 {
12310 if (handle_omp_array_sections (c, is_omp))
12311 {
12312 remove = true;
12313 break;
12314 }
12315
12316 t = OMP_CLAUSE_DECL (c);
12317 }
12318 t = require_complete_type (t);
12319 if (t == error_mark_node)
12320 {
12321 remove = true;
12322 break;
12323 }
12324 type = TREE_TYPE (t);
12325 if (TREE_CODE (t) == MEM_REF)
12326 type = TREE_TYPE (type);
12327 if (TREE_CODE (type) == ARRAY_TYPE)
12328 {
12329 tree oatype = type;
12330 gcc_assert (TREE_CODE (t) != MEM_REF);
12331 while (TREE_CODE (type) == ARRAY_TYPE)
12332 type = TREE_TYPE (type);
12333 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12334 {
12335 error_at (OMP_CLAUSE_LOCATION (c),
12336 "%qD in %<reduction%> clause is a zero size array",
12337 t);
12338 remove = true;
12339 break;
12340 }
12341 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12342 TYPE_SIZE_UNIT (type));
12343 if (integer_zerop (size))
12344 {
12345 error_at (OMP_CLAUSE_LOCATION (c),
12346 "%qD in %<reduction%> clause is a zero size array",
12347 t);
12348 remove = true;
12349 break;
12350 }
12351 size = size_binop (MINUS_EXPR, size, size_one_node);
12352 tree index_type = build_index_type (size);
12353 tree atype = build_array_type (type, index_type);
12354 tree ptype = build_pointer_type (type);
12355 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12356 t = build_fold_addr_expr (t);
12357 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12358 OMP_CLAUSE_DECL (c) = t;
12359 }
acf0174b 12360 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
d9a6bd32
JJ
12361 && (FLOAT_TYPE_P (type)
12362 || TREE_CODE (type) == COMPLEX_TYPE))
953ff289
DN
12363 {
12364 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12365 const char *r_name = NULL;
12366
12367 switch (r_code)
12368 {
12369 case PLUS_EXPR:
12370 case MULT_EXPR:
12371 case MINUS_EXPR:
652fea39 12372 break;
20906c66 12373 case MIN_EXPR:
d9a6bd32 12374 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39
JJ
12375 r_name = "min";
12376 break;
20906c66 12377 case MAX_EXPR:
d9a6bd32 12378 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39 12379 r_name = "max";
953ff289
DN
12380 break;
12381 case BIT_AND_EXPR:
12382 r_name = "&";
12383 break;
12384 case BIT_XOR_EXPR:
12385 r_name = "^";
12386 break;
12387 case BIT_IOR_EXPR:
12388 r_name = "|";
12389 break;
12390 case TRUTH_ANDIF_EXPR:
d9a6bd32 12391 if (FLOAT_TYPE_P (type))
652fea39 12392 r_name = "&&";
953ff289
DN
12393 break;
12394 case TRUTH_ORIF_EXPR:
d9a6bd32 12395 if (FLOAT_TYPE_P (type))
652fea39 12396 r_name = "||";
953ff289
DN
12397 break;
12398 default:
12399 gcc_unreachable ();
12400 }
12401 if (r_name)
12402 {
c2255bc4
AH
12403 error_at (OMP_CLAUSE_LOCATION (c),
12404 "%qE has invalid type for %<reduction(%s)%>",
12405 t, r_name);
953ff289 12406 remove = true;
ee1d5a02 12407 break;
953ff289
DN
12408 }
12409 }
acf0174b
JJ
12410 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12411 {
12412 error_at (OMP_CLAUSE_LOCATION (c),
d9a6bd32 12413 "user defined reduction not found for %qE", t);
acf0174b 12414 remove = true;
ee1d5a02 12415 break;
acf0174b
JJ
12416 }
12417 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12418 {
12419 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
d9a6bd32 12420 type = TYPE_MAIN_VARIANT (type);
acf0174b
JJ
12421 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12422 VAR_DECL, NULL_TREE, type);
d9a6bd32 12423 tree decl_placeholder = NULL_TREE;
acf0174b
JJ
12424 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12425 DECL_ARTIFICIAL (placeholder) = 1;
12426 DECL_IGNORED_P (placeholder) = 1;
d9a6bd32
JJ
12427 if (TREE_CODE (t) == MEM_REF)
12428 {
12429 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12430 VAR_DECL, NULL_TREE, type);
12431 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12432 DECL_ARTIFICIAL (decl_placeholder) = 1;
12433 DECL_IGNORED_P (decl_placeholder) = 1;
12434 }
acf0174b
JJ
12435 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12436 c_mark_addressable (placeholder);
12437 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
d9a6bd32
JJ
12438 c_mark_addressable (decl_placeholder ? decl_placeholder
12439 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12440 OMP_CLAUSE_REDUCTION_MERGE (c)
12441 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12442 TREE_VEC_ELT (list, 0),
12443 TREE_VEC_ELT (list, 1),
d9a6bd32
JJ
12444 decl_placeholder ? decl_placeholder
12445 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b
JJ
12446 OMP_CLAUSE_REDUCTION_MERGE (c)
12447 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12448 void_type_node, NULL_TREE,
d9a6bd32 12449 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
acf0174b
JJ
12450 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12451 if (TREE_VEC_LENGTH (list) == 6)
12452 {
12453 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
d9a6bd32
JJ
12454 c_mark_addressable (decl_placeholder ? decl_placeholder
12455 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12456 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12457 c_mark_addressable (placeholder);
12458 tree init = TREE_VEC_ELT (list, 5);
12459 if (init == error_mark_node)
12460 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12461 OMP_CLAUSE_REDUCTION_INIT (c)
12462 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12463 TREE_VEC_ELT (list, 3),
d9a6bd32
JJ
12464 decl_placeholder ? decl_placeholder
12465 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b 12466 if (TREE_VEC_ELT (list, 5) == error_mark_node)
d9a6bd32
JJ
12467 {
12468 tree v = decl_placeholder ? decl_placeholder : t;
12469 OMP_CLAUSE_REDUCTION_INIT (c)
12470 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12471 OMP_CLAUSE_REDUCTION_INIT (c));
12472 }
acf0174b
JJ
12473 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12474 c_find_omp_placeholder_r,
12475 placeholder, NULL))
12476 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12477 }
12478 else
12479 {
12480 tree init;
d9a6bd32
JJ
12481 tree v = decl_placeholder ? decl_placeholder : t;
12482 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12483 init = build_constructor (TREE_TYPE (v), NULL);
acf0174b 12484 else
d9a6bd32 12485 init = fold_convert (TREE_TYPE (v), integer_zero_node);
acf0174b 12486 OMP_CLAUSE_REDUCTION_INIT (c)
d9a6bd32 12487 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
acf0174b
JJ
12488 }
12489 OMP_CLAUSE_REDUCTION_INIT (c)
12490 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12491 void_type_node, NULL_TREE,
12492 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12493 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12494 }
d9a6bd32
JJ
12495 if (TREE_CODE (t) == MEM_REF)
12496 {
12497 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12498 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12499 != INTEGER_CST)
12500 {
12501 sorry ("variable length element type in array "
12502 "%<reduction%> clause");
12503 remove = true;
12504 break;
12505 }
12506 t = TREE_OPERAND (t, 0);
e01d41e5
JJ
12507 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12508 t = TREE_OPERAND (t, 0);
d9a6bd32
JJ
12509 if (TREE_CODE (t) == ADDR_EXPR)
12510 t = TREE_OPERAND (t, 0);
12511 }
12512 goto check_dup_generic_t;
953ff289
DN
12513
12514 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
12515 copyprivate_seen = true;
12516 if (nowait_clause)
12517 {
12518 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12519 "%<nowait%> clause must not be used together "
12520 "with %<copyprivate%>");
12521 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12522 nowait_clause = NULL;
12523 }
953ff289
DN
12524 goto check_dup_generic;
12525
12526 case OMP_CLAUSE_COPYIN:
953ff289 12527 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12528 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
953ff289 12529 {
c2255bc4
AH
12530 error_at (OMP_CLAUSE_LOCATION (c),
12531 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12532 remove = true;
ee1d5a02 12533 break;
953ff289
DN
12534 }
12535 goto check_dup_generic;
12536
acf0174b 12537 case OMP_CLAUSE_LINEAR:
d9a6bd32
JJ
12538 if (!declare_simd)
12539 need_implicitly_determined = true;
acf0174b 12540 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12541 if (!declare_simd
12542 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
12543 {
12544 error_at (OMP_CLAUSE_LOCATION (c),
12545 "modifier should not be specified in %<linear%> "
12546 "clause on %<simd%> or %<for%> constructs");
12547 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
12548 }
acf0174b
JJ
12549 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12550 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12551 {
12552 error_at (OMP_CLAUSE_LOCATION (c),
12553 "linear clause applied to non-integral non-pointer "
12554 "variable with type %qT", TREE_TYPE (t));
12555 remove = true;
12556 break;
12557 }
e01d41e5
JJ
12558 if (declare_simd)
12559 {
12560 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12561 if (TREE_CODE (s) == PARM_DECL)
12562 {
12563 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
12564 /* map_head bitmap is used as uniform_head if
12565 declare_simd. */
12566 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
12567 linear_variable_step_check = true;
12568 goto check_dup_generic;
12569 }
12570 if (TREE_CODE (s) != INTEGER_CST)
12571 {
12572 error_at (OMP_CLAUSE_LOCATION (c),
12573 "%<linear%> clause step %qE is neither constant "
12574 "nor a parameter", s);
12575 remove = true;
12576 break;
12577 }
12578 }
acf0174b
JJ
12579 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12580 {
12581 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12582 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12583 OMP_CLAUSE_DECL (c), s);
12584 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
12585 sizetype, fold_convert (sizetype, s),
12586 fold_convert
12587 (sizetype, OMP_CLAUSE_DECL (c)));
acf0174b
JJ
12588 if (s == error_mark_node)
12589 s = size_one_node;
12590 OMP_CLAUSE_LINEAR_STEP (c) = s;
12591 }
da6f124d
JJ
12592 else
12593 OMP_CLAUSE_LINEAR_STEP (c)
12594 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
12595 goto check_dup_generic;
12596
953ff289
DN
12597 check_dup_generic:
12598 t = OMP_CLAUSE_DECL (c);
d9a6bd32 12599 check_dup_generic_t:
0ae9bd27 12600 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12601 {
c2255bc4 12602 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
12603 "%qE is not a variable in clause %qs", t,
12604 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12605 remove = true;
12606 }
12607 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12608 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12609 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12610 {
c2255bc4
AH
12611 error_at (OMP_CLAUSE_LOCATION (c),
12612 "%qE appears more than once in data clauses", t);
953ff289
DN
12613 remove = true;
12614 }
e01d41e5
JJ
12615 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12616 && bitmap_bit_p (&map_head, DECL_UID (t)))
12617 {
12618 error ("%qD appears both in data and map clauses", t);
12619 remove = true;
12620 }
953ff289
DN
12621 else
12622 bitmap_set_bit (&generic_head, DECL_UID (t));
12623 break;
12624
12625 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
12626 t = OMP_CLAUSE_DECL (c);
12627 need_complete = true;
12628 need_implicitly_determined = true;
0ae9bd27 12629 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12630 {
c2255bc4
AH
12631 error_at (OMP_CLAUSE_LOCATION (c),
12632 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
12633 remove = true;
12634 }
12635 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12636 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12637 {
c2255bc4
AH
12638 error_at (OMP_CLAUSE_LOCATION (c),
12639 "%qE appears more than once in data clauses", t);
953ff289
DN
12640 remove = true;
12641 }
e01d41e5
JJ
12642 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
12643 {
12644 error ("%qD appears both in data and map clauses", t);
12645 remove = true;
12646 }
953ff289
DN
12647 else
12648 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12649 break;
12650
12651 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
12652 t = OMP_CLAUSE_DECL (c);
12653 need_complete = true;
12654 need_implicitly_determined = true;
0ae9bd27 12655 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12656 {
c2255bc4
AH
12657 error_at (OMP_CLAUSE_LOCATION (c),
12658 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
12659 remove = true;
12660 }
12661 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12662 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12663 {
c2255bc4
AH
12664 error_at (OMP_CLAUSE_LOCATION (c),
12665 "%qE appears more than once in data clauses", t);
953ff289
DN
12666 remove = true;
12667 }
12668 else
12669 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12670 break;
12671
acf0174b
JJ
12672 case OMP_CLAUSE_ALIGNED:
12673 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12674 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12675 {
12676 error_at (OMP_CLAUSE_LOCATION (c),
12677 "%qE is not a variable in %<aligned%> clause", t);
12678 remove = true;
12679 }
5a9785fb
JJ
12680 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12681 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12682 {
12683 error_at (OMP_CLAUSE_LOCATION (c),
12684 "%qE in %<aligned%> clause is neither a pointer nor "
12685 "an array", t);
12686 remove = true;
12687 }
acf0174b
JJ
12688 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12689 {
12690 error_at (OMP_CLAUSE_LOCATION (c),
12691 "%qE appears more than once in %<aligned%> clauses",
12692 t);
12693 remove = true;
12694 }
12695 else
12696 bitmap_set_bit (&aligned_head, DECL_UID (t));
12697 break;
12698
12699 case OMP_CLAUSE_DEPEND:
12700 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12701 if (t == NULL_TREE)
12702 {
12703 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
12704 == OMP_CLAUSE_DEPEND_SOURCE);
12705 break;
12706 }
12707 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
12708 {
12709 gcc_assert (TREE_CODE (t) == TREE_LIST);
12710 for (; t; t = TREE_CHAIN (t))
12711 {
12712 tree decl = TREE_VALUE (t);
12713 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
12714 {
12715 tree offset = TREE_PURPOSE (t);
12716 bool neg = wi::neg_p ((wide_int) offset);
12717 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
12718 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
12719 neg ? MINUS_EXPR : PLUS_EXPR,
12720 decl, offset);
12721 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
12722 sizetype,
12723 fold_convert (sizetype, t2),
12724 fold_convert (sizetype, decl));
d9a6bd32
JJ
12725 if (t2 == error_mark_node)
12726 {
12727 remove = true;
12728 break;
12729 }
12730 TREE_PURPOSE (t) = t2;
12731 }
12732 }
12733 break;
12734 }
acf0174b
JJ
12735 if (TREE_CODE (t) == TREE_LIST)
12736 {
d9a6bd32 12737 if (handle_omp_array_sections (c, is_omp))
acf0174b
JJ
12738 remove = true;
12739 break;
12740 }
12741 if (t == error_mark_node)
12742 remove = true;
0ae9bd27 12743 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12744 {
12745 error_at (OMP_CLAUSE_LOCATION (c),
12746 "%qE is not a variable in %<depend%> clause", t);
12747 remove = true;
12748 }
12749 else if (!c_mark_addressable (t))
12750 remove = true;
12751 break;
12752
12753 case OMP_CLAUSE_MAP:
12754 case OMP_CLAUSE_TO:
12755 case OMP_CLAUSE_FROM:
41dbbb37 12756 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
12757 t = OMP_CLAUSE_DECL (c);
12758 if (TREE_CODE (t) == TREE_LIST)
12759 {
d9a6bd32 12760 if (handle_omp_array_sections (c, is_omp))
acf0174b
JJ
12761 remove = true;
12762 else
12763 {
12764 t = OMP_CLAUSE_DECL (c);
b17a8b07 12765 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12766 {
12767 error_at (OMP_CLAUSE_LOCATION (c),
12768 "array section does not have mappable type "
12769 "in %qs clause",
12770 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12771 remove = true;
12772 }
d9a6bd32
JJ
12773 while (TREE_CODE (t) == ARRAY_REF)
12774 t = TREE_OPERAND (t, 0);
12775 if (TREE_CODE (t) == COMPONENT_REF
12776 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12777 {
12778 while (TREE_CODE (t) == COMPONENT_REF)
12779 t = TREE_OPERAND (t, 0);
12780 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
12781 break;
12782 if (bitmap_bit_p (&map_head, DECL_UID (t)))
12783 {
12784 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12785 error ("%qD appears more than once in motion"
12786 " clauses", t);
12787 else
12788 error ("%qD appears more than once in map"
12789 " clauses", t);
12790 remove = true;
12791 }
12792 else
12793 {
12794 bitmap_set_bit (&map_head, DECL_UID (t));
12795 bitmap_set_bit (&map_field_head, DECL_UID (t));
12796 }
12797 }
acf0174b
JJ
12798 }
12799 break;
12800 }
12801 if (t == error_mark_node)
d9a6bd32
JJ
12802 {
12803 remove = true;
12804 break;
12805 }
12806 if (TREE_CODE (t) == COMPONENT_REF
12807 && is_omp
12808 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
12809 {
12810 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12811 {
12812 error_at (OMP_CLAUSE_LOCATION (c),
12813 "bit-field %qE in %qs clause",
12814 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12815 remove = true;
12816 }
12817 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12818 {
12819 error_at (OMP_CLAUSE_LOCATION (c),
12820 "%qE does not have a mappable type in %qs clause",
12821 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12822 remove = true;
12823 }
12824 while (TREE_CODE (t) == COMPONENT_REF)
12825 {
12826 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
12827 == UNION_TYPE)
12828 {
12829 error_at (OMP_CLAUSE_LOCATION (c),
12830 "%qE is a member of a union", t);
12831 remove = true;
12832 break;
12833 }
12834 t = TREE_OPERAND (t, 0);
12835 }
12836 if (remove)
12837 break;
12838 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
12839 {
e01d41e5 12840 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
d9a6bd32
JJ
12841 break;
12842 }
12843 }
12844 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12845 {
12846 error_at (OMP_CLAUSE_LOCATION (c),
12847 "%qE is not a variable in %qs clause", t,
12848 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12849 remove = true;
12850 }
0ae9bd27 12851 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
12852 {
12853 error_at (OMP_CLAUSE_LOCATION (c),
12854 "%qD is threadprivate variable in %qs clause", t,
12855 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12856 remove = true;
12857 }
12858 else if (!c_mark_addressable (t))
12859 remove = true;
b17a8b07 12860 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37 12861 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
d9a6bd32
JJ
12862 || (OMP_CLAUSE_MAP_KIND (c)
12863 == GOMP_MAP_FIRSTPRIVATE_POINTER)
41dbbb37
TS
12864 || (OMP_CLAUSE_MAP_KIND (c)
12865 == GOMP_MAP_FORCE_DEVICEPTR)))
d9a6bd32 12866 && t == OMP_CLAUSE_DECL (c)
b17a8b07 12867 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12868 {
12869 error_at (OMP_CLAUSE_LOCATION (c),
12870 "%qD does not have a mappable type in %qs clause", t,
12871 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12872 remove = true;
12873 }
d9a6bd32
JJ
12874 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12875 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
12876 {
12877 if (bitmap_bit_p (&generic_head, DECL_UID (t))
12878 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12879 {
12880 error ("%qD appears more than once in data clauses", t);
12881 remove = true;
12882 }
e01d41e5 12883 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
d9a6bd32 12884 {
e01d41e5
JJ
12885 error ("%qD appears both in data and map clauses", t);
12886 remove = true;
d9a6bd32 12887 }
e01d41e5
JJ
12888 else
12889 bitmap_set_bit (&generic_head, DECL_UID (t));
d9a6bd32
JJ
12890 }
12891 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
acf0174b
JJ
12892 {
12893 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12894 error ("%qD appears more than once in motion clauses", t);
12895 else
12896 error ("%qD appears more than once in map clauses", t);
12897 remove = true;
12898 }
e01d41e5
JJ
12899 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12900 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12901 {
12902 error ("%qD appears both in data and map clauses", t);
12903 remove = true;
12904 }
acf0174b 12905 else
d9a6bd32
JJ
12906 {
12907 bitmap_set_bit (&map_head, DECL_UID (t));
12908 if (t != OMP_CLAUSE_DECL (c)
12909 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
12910 bitmap_set_bit (&map_field_head, DECL_UID (t));
12911 }
12912 break;
12913
12914 case OMP_CLAUSE_TO_DECLARE:
d9a6bd32
JJ
12915 case OMP_CLAUSE_LINK:
12916 t = OMP_CLAUSE_DECL (c);
e01d41e5
JJ
12917 if (TREE_CODE (t) == FUNCTION_DECL
12918 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
12919 ;
12920 else if (!VAR_P (t))
d9a6bd32 12921 {
e01d41e5
JJ
12922 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
12923 error_at (OMP_CLAUSE_LOCATION (c),
12924 "%qE is neither a variable nor a function name in "
12925 "clause %qs", t,
12926 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12927 else
12928 error_at (OMP_CLAUSE_LOCATION (c),
12929 "%qE is not a variable in clause %qs", t,
12930 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
d9a6bd32
JJ
12931 remove = true;
12932 }
12933 else if (DECL_THREAD_LOCAL_P (t))
12934 {
12935 error_at (OMP_CLAUSE_LOCATION (c),
12936 "%qD is threadprivate variable in %qs clause", t,
12937 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12938 remove = true;
12939 }
12940 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12941 {
12942 error_at (OMP_CLAUSE_LOCATION (c),
12943 "%qD does not have a mappable type in %qs clause", t,
12944 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12945 remove = true;
12946 }
e01d41e5
JJ
12947 if (remove)
12948 break;
12949 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12950 {
12951 error_at (OMP_CLAUSE_LOCATION (c),
12952 "%qE appears more than once on the same "
12953 "%<declare target%> directive", t);
12954 remove = true;
12955 }
12956 else
12957 bitmap_set_bit (&generic_head, DECL_UID (t));
acf0174b
JJ
12958 break;
12959
12960 case OMP_CLAUSE_UNIFORM:
12961 t = OMP_CLAUSE_DECL (c);
12962 if (TREE_CODE (t) != PARM_DECL)
12963 {
12964 if (DECL_P (t))
12965 error_at (OMP_CLAUSE_LOCATION (c),
12966 "%qD is not an argument in %<uniform%> clause", t);
12967 else
12968 error_at (OMP_CLAUSE_LOCATION (c),
12969 "%qE is not an argument in %<uniform%> clause", t);
12970 remove = true;
ee1d5a02 12971 break;
acf0174b 12972 }
e01d41e5
JJ
12973 /* map_head bitmap is used as uniform_head if declare_simd. */
12974 bitmap_set_bit (&map_head, DECL_UID (t));
ee1d5a02 12975 goto check_dup_generic;
acf0174b 12976
d9a6bd32
JJ
12977 case OMP_CLAUSE_IS_DEVICE_PTR:
12978 case OMP_CLAUSE_USE_DEVICE_PTR:
12979 t = OMP_CLAUSE_DECL (c);
12980 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
12981 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12982 {
12983 error_at (OMP_CLAUSE_LOCATION (c),
12984 "%qs variable is neither a pointer nor an array",
12985 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12986 remove = true;
12987 }
12988 goto check_dup_generic;
12989
acf0174b
JJ
12990 case OMP_CLAUSE_NOWAIT:
12991 if (copyprivate_seen)
12992 {
12993 error_at (OMP_CLAUSE_LOCATION (c),
12994 "%<nowait%> clause must not be used together "
12995 "with %<copyprivate%>");
12996 remove = true;
12997 break;
12998 }
12999 nowait_clause = pc;
13000 pc = &OMP_CLAUSE_CHAIN (c);
13001 continue;
13002
953ff289
DN
13003 case OMP_CLAUSE_IF:
13004 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
13005 case OMP_CLAUSE_NUM_TEAMS:
13006 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 13007 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
13008 case OMP_CLAUSE_UNTIED:
13009 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
13010 case OMP_CLAUSE_FINAL:
13011 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
13012 case OMP_CLAUSE_DEVICE:
13013 case OMP_CLAUSE_DIST_SCHEDULE:
13014 case OMP_CLAUSE_PARALLEL:
13015 case OMP_CLAUSE_FOR:
13016 case OMP_CLAUSE_SECTIONS:
13017 case OMP_CLAUSE_TASKGROUP:
13018 case OMP_CLAUSE_PROC_BIND:
d9a6bd32
JJ
13019 case OMP_CLAUSE_PRIORITY:
13020 case OMP_CLAUSE_GRAINSIZE:
13021 case OMP_CLAUSE_NUM_TASKS:
13022 case OMP_CLAUSE_NOGROUP:
13023 case OMP_CLAUSE_THREADS:
13024 case OMP_CLAUSE_SIMD:
13025 case OMP_CLAUSE_HINT:
13026 case OMP_CLAUSE_DEFAULTMAP:
9a771876 13027 case OMP_CLAUSE__CILK_FOR_COUNT_:
41dbbb37
TS
13028 case OMP_CLAUSE_NUM_GANGS:
13029 case OMP_CLAUSE_NUM_WORKERS:
13030 case OMP_CLAUSE_VECTOR_LENGTH:
13031 case OMP_CLAUSE_ASYNC:
13032 case OMP_CLAUSE_WAIT:
13033 case OMP_CLAUSE_AUTO:
7a5e4956 13034 case OMP_CLAUSE_INDEPENDENT:
41dbbb37
TS
13035 case OMP_CLAUSE_SEQ:
13036 case OMP_CLAUSE_GANG:
13037 case OMP_CLAUSE_WORKER:
13038 case OMP_CLAUSE_VECTOR:
7a5e4956 13039 case OMP_CLAUSE_TILE:
acf0174b
JJ
13040 pc = &OMP_CLAUSE_CHAIN (c);
13041 continue;
13042
e01d41e5
JJ
13043 case OMP_CLAUSE_SCHEDULE:
13044 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13045 {
13046 const char *p = NULL;
13047 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13048 {
13049 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13050 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13051 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13052 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13053 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13054 default: gcc_unreachable ();
13055 }
13056 if (p)
13057 {
13058 error_at (OMP_CLAUSE_LOCATION (c),
13059 "%<nonmonotonic%> modifier specified for %qs "
13060 "schedule kind", p);
13061 OMP_CLAUSE_SCHEDULE_KIND (c)
13062 = (enum omp_clause_schedule_kind)
13063 (OMP_CLAUSE_SCHEDULE_KIND (c)
13064 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13065 }
13066 }
13067 schedule_clause = c;
13068 pc = &OMP_CLAUSE_CHAIN (c);
13069 continue;
13070
13071 case OMP_CLAUSE_ORDERED:
13072 ordered_seen = true;
13073 pc = &OMP_CLAUSE_CHAIN (c);
13074 continue;
13075
d9a6bd32
JJ
13076 case OMP_CLAUSE_SAFELEN:
13077 safelen = c;
13078 pc = &OMP_CLAUSE_CHAIN (c);
13079 continue;
13080 case OMP_CLAUSE_SIMDLEN:
13081 simdlen = c;
13082 pc = &OMP_CLAUSE_CHAIN (c);
13083 continue;
13084
acf0174b
JJ
13085 case OMP_CLAUSE_INBRANCH:
13086 case OMP_CLAUSE_NOTINBRANCH:
13087 if (branch_seen)
13088 {
13089 error_at (OMP_CLAUSE_LOCATION (c),
13090 "%<inbranch%> clause is incompatible with "
13091 "%<notinbranch%>");
13092 remove = true;
13093 break;
13094 }
13095 branch_seen = true;
953ff289
DN
13096 pc = &OMP_CLAUSE_CHAIN (c);
13097 continue;
13098
13099 default:
13100 gcc_unreachable ();
13101 }
13102
13103 if (!remove)
13104 {
13105 t = OMP_CLAUSE_DECL (c);
13106
13107 if (need_complete)
13108 {
13109 t = require_complete_type (t);
13110 if (t == error_mark_node)
13111 remove = true;
13112 }
13113
13114 if (need_implicitly_determined)
13115 {
13116 const char *share_name = NULL;
13117
0ae9bd27 13118 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
953ff289
DN
13119 share_name = "threadprivate";
13120 else switch (c_omp_predetermined_sharing (t))
13121 {
13122 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13123 break;
13124 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
13125 /* const vars may be specified in firstprivate clause. */
13126 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13127 && TREE_READONLY (t))
13128 break;
953ff289
DN
13129 share_name = "shared";
13130 break;
13131 case OMP_CLAUSE_DEFAULT_PRIVATE:
13132 share_name = "private";
13133 break;
13134 default:
13135 gcc_unreachable ();
13136 }
13137 if (share_name)
13138 {
c2255bc4
AH
13139 error_at (OMP_CLAUSE_LOCATION (c),
13140 "%qE is predetermined %qs for %qs",
acf0174b
JJ
13141 t, share_name,
13142 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13143 remove = true;
13144 }
13145 }
13146 }
13147
13148 if (remove)
13149 *pc = OMP_CLAUSE_CHAIN (c);
13150 else
13151 pc = &OMP_CLAUSE_CHAIN (c);
13152 }
13153
d9a6bd32
JJ
13154 if (simdlen
13155 && safelen
13156 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13157 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13158 {
13159 error_at (OMP_CLAUSE_LOCATION (simdlen),
13160 "%<simdlen%> clause value is bigger than "
13161 "%<safelen%> clause value");
13162 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13163 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13164 }
13165
e01d41e5
JJ
13166 if (ordered_seen
13167 && schedule_clause
13168 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13169 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13170 {
13171 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13172 "%<nonmonotonic%> schedule modifier specified together "
13173 "with %<ordered%> clause");
13174 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13175 = (enum omp_clause_schedule_kind)
13176 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13177 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13178 }
13179
13180 if (linear_variable_step_check)
13181 for (pc = &clauses, c = clauses; c ; c = *pc)
13182 {
13183 bool remove = false;
13184 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13185 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13186 && !bitmap_bit_p (&map_head,
13187 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13188 {
13189 error_at (OMP_CLAUSE_LOCATION (c),
13190 "%<linear%> clause step is a parameter %qD not "
13191 "specified in %<uniform%> clause",
13192 OMP_CLAUSE_LINEAR_STEP (c));
13193 remove = true;
13194 }
13195
13196 if (remove)
13197 *pc = OMP_CLAUSE_CHAIN (c);
13198 else
13199 pc = &OMP_CLAUSE_CHAIN (c);
13200 }
13201
953ff289
DN
13202 bitmap_obstack_release (NULL);
13203 return clauses;
13204}
9ae165a0 13205
0a35513e
AH
13206/* Create a transaction node. */
13207
13208tree
13209c_finish_transaction (location_t loc, tree block, int flags)
13210{
13211 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13212 if (flags & TM_STMT_ATTR_OUTER)
13213 TRANSACTION_EXPR_OUTER (stmt) = 1;
13214 if (flags & TM_STMT_ATTR_RELAXED)
13215 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13216 return add_stmt (stmt);
13217}
13218
9ae165a0
DG
13219/* Make a variant type in the proper way for C/C++, propagating qualifiers
13220 down to the element type of an array. */
13221
13222tree
13223c_build_qualified_type (tree type, int type_quals)
13224{
13225 if (type == error_mark_node)
13226 return type;
13227
13228 if (TREE_CODE (type) == ARRAY_TYPE)
13229 {
13230 tree t;
13231 tree element_type = c_build_qualified_type (TREE_TYPE (type),
13232 type_quals);
13233
13234 /* See if we already have an identically qualified type. */
13235 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13236 {
13237 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13238 && TYPE_NAME (t) == TYPE_NAME (type)
13239 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13240 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13241 TYPE_ATTRIBUTES (type)))
13242 break;
13243 }
13244 if (!t)
13245 {
13246 tree domain = TYPE_DOMAIN (type);
13247
13248 t = build_variant_type_copy (type);
13249 TREE_TYPE (t) = element_type;
13250
13251 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13252 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13253 SET_TYPE_STRUCTURAL_EQUALITY (t);
13254 else if (TYPE_CANONICAL (element_type) != element_type
13255 || (domain && TYPE_CANONICAL (domain) != domain))
13256 {
b8698a0f 13257 tree unqualified_canon
9ae165a0 13258 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 13259 domain? TYPE_CANONICAL (domain)
9ae165a0 13260 : NULL_TREE);
ee45a32d
EB
13261 if (TYPE_REVERSE_STORAGE_ORDER (type))
13262 {
13263 unqualified_canon
13264 = build_distinct_type_copy (unqualified_canon);
13265 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13266 }
b8698a0f 13267 TYPE_CANONICAL (t)
9ae165a0
DG
13268 = c_build_qualified_type (unqualified_canon, type_quals);
13269 }
13270 else
13271 TYPE_CANONICAL (t) = t;
13272 }
13273 return t;
13274 }
13275
13276 /* A restrict-qualified pointer type must be a pointer to object or
13277 incomplete type. Note that the use of POINTER_TYPE_P also allows
13278 REFERENCE_TYPEs, which is appropriate for C++. */
13279 if ((type_quals & TYPE_QUAL_RESTRICT)
13280 && (!POINTER_TYPE_P (type)
13281 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13282 {
13283 error ("invalid use of %<restrict%>");
13284 type_quals &= ~TYPE_QUAL_RESTRICT;
13285 }
13286
e922069e
JW
13287 tree var_type = build_qualified_type (type, type_quals);
13288 /* A variant type does not inherit the list of incomplete vars from the
13289 type main variant. */
13290 if (TREE_CODE (var_type) == RECORD_TYPE
13291 || TREE_CODE (var_type) == UNION_TYPE)
13292 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13293 return var_type;
9ae165a0 13294}
72b5577d
ILT
13295
13296/* Build a VA_ARG_EXPR for the C parser. */
13297
13298tree
c2255bc4 13299c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d 13300{
ec3fba51
MP
13301 if (error_operand_p (type))
13302 return error_mark_node;
13303 else if (!COMPLETE_TYPE_P (type))
4e81b788 13304 {
ec3fba51
MP
13305 error_at (loc, "second argument to %<va_arg%> is of incomplete "
13306 "type %qT", type);
4e81b788
MP
13307 return error_mark_node;
13308 }
ec3fba51
MP
13309 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
13310 warning_at (loc, OPT_Wc___compat,
13311 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 13312 return build_va_arg (loc, expr, type);
72b5577d 13313}
acf0174b
JJ
13314
13315/* Return truthvalue of whether T1 is the same tree structure as T2.
13316 Return 1 if they are the same. Return 0 if they are different. */
13317
13318bool
13319c_tree_equal (tree t1, tree t2)
13320{
13321 enum tree_code code1, code2;
13322
13323 if (t1 == t2)
13324 return true;
13325 if (!t1 || !t2)
13326 return false;
13327
13328 for (code1 = TREE_CODE (t1);
13329 CONVERT_EXPR_CODE_P (code1)
13330 || code1 == NON_LVALUE_EXPR;
13331 code1 = TREE_CODE (t1))
13332 t1 = TREE_OPERAND (t1, 0);
13333 for (code2 = TREE_CODE (t2);
13334 CONVERT_EXPR_CODE_P (code2)
13335 || code2 == NON_LVALUE_EXPR;
13336 code2 = TREE_CODE (t2))
13337 t2 = TREE_OPERAND (t2, 0);
13338
13339 /* They might have become equal now. */
13340 if (t1 == t2)
13341 return true;
13342
13343 if (code1 != code2)
13344 return false;
13345
13346 switch (code1)
13347 {
13348 case INTEGER_CST:
807e902e 13349 return wi::eq_p (t1, t2);
acf0174b
JJ
13350
13351 case REAL_CST:
624d31fe 13352 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
acf0174b
JJ
13353
13354 case STRING_CST:
13355 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13356 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13357 TREE_STRING_LENGTH (t1));
13358
13359 case FIXED_CST:
13360 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13361 TREE_FIXED_CST (t2));
13362
13363 case COMPLEX_CST:
13364 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13365 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13366
13367 case VECTOR_CST:
13368 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13369
13370 case CONSTRUCTOR:
13371 /* We need to do this when determining whether or not two
13372 non-type pointer to member function template arguments
13373 are the same. */
13374 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13375 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13376 return false;
13377 {
13378 tree field, value;
13379 unsigned int i;
13380 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13381 {
13382 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13383 if (!c_tree_equal (field, elt2->index)
13384 || !c_tree_equal (value, elt2->value))
13385 return false;
13386 }
13387 }
13388 return true;
13389
13390 case TREE_LIST:
13391 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13392 return false;
13393 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
13394 return false;
13395 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
13396
13397 case SAVE_EXPR:
13398 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13399
13400 case CALL_EXPR:
13401 {
13402 tree arg1, arg2;
13403 call_expr_arg_iterator iter1, iter2;
13404 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
13405 return false;
13406 for (arg1 = first_call_expr_arg (t1, &iter1),
13407 arg2 = first_call_expr_arg (t2, &iter2);
13408 arg1 && arg2;
13409 arg1 = next_call_expr_arg (&iter1),
13410 arg2 = next_call_expr_arg (&iter2))
13411 if (!c_tree_equal (arg1, arg2))
13412 return false;
13413 if (arg1 || arg2)
13414 return false;
13415 return true;
13416 }
13417
13418 case TARGET_EXPR:
13419 {
13420 tree o1 = TREE_OPERAND (t1, 0);
13421 tree o2 = TREE_OPERAND (t2, 0);
13422
13423 /* Special case: if either target is an unallocated VAR_DECL,
13424 it means that it's going to be unified with whatever the
13425 TARGET_EXPR is really supposed to initialize, so treat it
13426 as being equivalent to anything. */
0ae9bd27 13427 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
acf0174b
JJ
13428 && !DECL_RTL_SET_P (o1))
13429 /*Nop*/;
0ae9bd27 13430 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
acf0174b
JJ
13431 && !DECL_RTL_SET_P (o2))
13432 /*Nop*/;
13433 else if (!c_tree_equal (o1, o2))
13434 return false;
13435
13436 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
13437 }
13438
13439 case COMPONENT_REF:
13440 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
13441 return false;
13442 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13443
13444 case PARM_DECL:
13445 case VAR_DECL:
13446 case CONST_DECL:
13447 case FIELD_DECL:
13448 case FUNCTION_DECL:
13449 case IDENTIFIER_NODE:
13450 case SSA_NAME:
13451 return false;
13452
13453 case TREE_VEC:
13454 {
13455 unsigned ix;
13456 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
13457 return false;
13458 for (ix = TREE_VEC_LENGTH (t1); ix--;)
13459 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
13460 TREE_VEC_ELT (t2, ix)))
13461 return false;
13462 return true;
13463 }
13464
13465 default:
13466 break;
13467 }
13468
13469 switch (TREE_CODE_CLASS (code1))
13470 {
13471 case tcc_unary:
13472 case tcc_binary:
13473 case tcc_comparison:
13474 case tcc_expression:
13475 case tcc_vl_exp:
13476 case tcc_reference:
13477 case tcc_statement:
13478 {
13479 int i, n = TREE_OPERAND_LENGTH (t1);
13480
13481 switch (code1)
13482 {
13483 case PREINCREMENT_EXPR:
13484 case PREDECREMENT_EXPR:
13485 case POSTINCREMENT_EXPR:
13486 case POSTDECREMENT_EXPR:
13487 n = 1;
13488 break;
13489 case ARRAY_REF:
13490 n = 2;
13491 break;
13492 default:
13493 break;
13494 }
13495
13496 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
13497 && n != TREE_OPERAND_LENGTH (t2))
13498 return false;
13499
13500 for (i = 0; i < n; ++i)
13501 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
13502 return false;
13503
13504 return true;
13505 }
13506
13507 case tcc_type:
13508 return comptypes (t1, t2);
13509 default:
13510 gcc_unreachable ();
13511 }
13512 /* We can get here with --disable-checking. */
13513 return false;
13514}
12893402
BI
13515
13516/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
13517 spawn-helper and BODY is the newly created body for FNDECL. */
13518
13519void
13520cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
13521{
13522 tree list = alloc_stmt_list ();
13523 tree frame = make_cilk_frame (fndecl);
13524 tree dtor = create_cilk_function_exit (frame, false, true);
13525 add_local_decl (cfun, frame);
13526
13527 DECL_SAVED_TREE (fndecl) = list;
13528 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
13529 frame);
13530 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
13531 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
13532
13533 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
13534 append_to_statement_list (detach_expr, &body_list);
13535
13536 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
13537 body = fold_build_cleanup_point_expr (void_type_node, body);
13538
13539 append_to_statement_list (body, &body_list);
13540 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
13541 body_list, dtor), &list);
13542}
1807ffc1
MS
13543
13544/* Returns true when the function declaration FNDECL is implicit,
13545 introduced as a result of a call to an otherwise undeclared
13546 function, and false otherwise. */
13547
13548bool
13549c_decl_implicit (const_tree fndecl)
13550{
13551 return C_DECL_IMPLICIT (fndecl);
13552}