]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
re PR c/77490 (bit-not (~) on boolean should be warned about)
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
818ab71a 2 Copyright (C) 1987-2016 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"
2adfab87
AM
32#include "c-tree.h"
33#include "gimple-expr.h"
34#include "predict.h"
d8a2d370
DN
35#include "stor-layout.h"
36#include "trans-mem.h"
37#include "varasm.h"
38#include "stmt.h"
e57e265b 39#include "langhooks.h"
29cc57cf 40#include "c-lang.h"
ab87f8c8 41#include "intl.h"
325c3691 42#include "tree-iterator.h"
45b0be94 43#include "gimplify.h"
acf0174b 44#include "tree-inline.h"
0645c1a2 45#include "omp-low.h"
61d3ce20 46#include "c-family/c-objc.h"
de5a5fa1 47#include "c-family/c-ubsan.h"
12893402 48#include "cilk.h"
41dbbb37 49#include "gomp-constants.h"
6a3f203c 50#include "spellcheck-tree.h"
745e411d 51#include "gcc-rich-location.h"
325c3691 52
2ac2f164
JM
53/* Possible cases of implicit bad conversions. Used to select
54 diagnostic messages in convert_for_assignment. */
55enum impl_conv {
56 ic_argpass,
57 ic_assign,
58 ic_init,
59 ic_return
60};
61
bc4b653b
JM
62/* The level of nesting inside "__alignof__". */
63int in_alignof;
64
65/* The level of nesting inside "sizeof". */
66int in_sizeof;
67
68/* The level of nesting inside "typeof". */
69int in_typeof;
400fbf9f 70
1a4049e7
JJ
71/* The argument of last parsed sizeof expression, only to be tested
72 if expr.original_code == SIZEOF_EXPR. */
73tree c_last_sizeof_arg;
74
9bac5cbb
G
75/* Nonzero if we might need to print a "missing braces around
76 initializer" message within this initializer. */
77static int found_missing_braces;
103b7b17 78
bf730f15
RS
79static int require_constant_value;
80static int require_constant_elements;
81
58f9752a 82static bool null_pointer_constant_p (const_tree);
f55ade6e 83static tree qualify_type (tree, tree);
dc5027f4
JM
84static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
85 bool *);
744aa42f 86static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
87static int function_types_compatible_p (const_tree, const_tree, bool *,
88 bool *);
89static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 90static tree lookup_field (tree, tree);
81e5eca8
MP
91static int convert_arguments (location_t, vec<location_t>, tree,
92 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
93 tree);
db3927fb 94static tree pointer_diff (location_t, tree, tree);
68fca595 95static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 96 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
97static tree valid_compound_expr_initializer (tree, tree);
98static void push_string (const char *);
99static void push_member_name (tree);
f55ade6e
AJ
100static int spelling_length (void);
101static char *print_spelling (char *);
96b40f8d 102static void warning_init (location_t, int, const char *);
c2255bc4 103static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
104static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
105 bool, struct obstack *);
a1e3b3d9 106static void output_pending_init_elements (int, struct obstack *);
ea58ef42 107static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 108static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
109static void add_pending_init (location_t, tree, tree, tree, bool,
110 struct obstack *);
a1e3b3d9
LB
111static void set_nonincremental_init (struct obstack *);
112static void set_nonincremental_init_from_string (tree, struct obstack *);
113static tree find_init_member (tree, struct obstack *);
f37acdf9 114static void readonly_warning (tree, enum lvalue_use);
7bd11157 115static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 116static void record_maybe_used_decl (tree);
dc5027f4 117static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
118\f
119/* Return true if EXP is a null pointer constant, false otherwise. */
120
121static bool
58f9752a 122null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
123{
124 /* This should really operate on c_expr structures, but they aren't
125 yet available everywhere required. */
126 tree type = TREE_TYPE (expr);
127 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 128 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
129 && integer_zerop (expr)
130 && (INTEGRAL_TYPE_P (type)
131 || (TREE_CODE (type) == POINTER_TYPE
132 && VOID_TYPE_P (TREE_TYPE (type))
133 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
134}
928c19bb
JM
135
136/* EXPR may appear in an unevaluated part of an integer constant
137 expression, but not in an evaluated part. Wrap it in a
138 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
139 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
140
141static tree
142note_integer_operands (tree expr)
143{
144 tree ret;
145 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
146 {
147 ret = copy_node (expr);
148 TREE_OVERFLOW (ret) = 1;
149 }
150 else
151 {
152 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
153 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
154 }
155 return ret;
156}
157
4d84fe7c
JM
158/* Having checked whether EXPR may appear in an unevaluated part of an
159 integer constant expression and found that it may, remove any
160 C_MAYBE_CONST_EXPR noting this fact and return the resulting
161 expression. */
162
163static inline tree
164remove_c_maybe_const_expr (tree expr)
165{
166 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
167 return C_MAYBE_CONST_EXPR_EXPR (expr);
168 else
169 return expr;
170}
171
f13c9b2c
AP
172\f/* This is a cache to hold if two types are compatible or not. */
173
174struct tagged_tu_seen_cache {
175 const struct tagged_tu_seen_cache * next;
58f9752a
KG
176 const_tree t1;
177 const_tree t2;
f13c9b2c
AP
178 /* The return value of tagged_types_tu_compatible_p if we had seen
179 these two types already. */
180 int val;
181};
182
183static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
184static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
185
4f2e1536
MP
186/* Do `exp = require_complete_type (loc, exp);' to make sure exp
187 does not have an incomplete type. (That includes void types.)
188 LOC is the location of the use. */
400fbf9f
JW
189
190tree
4f2e1536 191require_complete_type (location_t loc, 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
4f2e1536 202 c_incomplete_type_error (loc, 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)
4f2e1536
MP
208 and TYPE is the type that was invalid. LOC is the location for
209 the error. */
400fbf9f
JW
210
211void
4f2e1536 212c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
400fbf9f 213{
400fbf9f
JW
214 /* Avoid duplicate error message. */
215 if (TREE_CODE (type) == ERROR_MARK)
216 return;
217
0ae9bd27 218 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
4f2e1536 219 error_at (loc, "%qD has an incomplete type %qT", value, type);
400fbf9f
JW
220 else
221 {
222 retry:
223 /* We must print an error message. Be clever about what it says. */
224
225 switch (TREE_CODE (type))
226 {
227 case RECORD_TYPE:
400fbf9f 228 case UNION_TYPE:
400fbf9f 229 case ENUMERAL_TYPE:
400fbf9f
JW
230 break;
231
232 case VOID_TYPE:
4f2e1536 233 error_at (loc, "invalid use of void expression");
400fbf9f
JW
234 return;
235
236 case ARRAY_TYPE:
237 if (TYPE_DOMAIN (type))
238 {
fba78abb
RH
239 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
240 {
4f2e1536 241 error_at (loc, "invalid use of flexible array member");
fba78abb
RH
242 return;
243 }
400fbf9f
JW
244 type = TREE_TYPE (type);
245 goto retry;
246 }
4f2e1536 247 error_at (loc, "invalid use of array with unspecified bounds");
400fbf9f
JW
248 return;
249
250 default:
366de0ce 251 gcc_unreachable ();
400fbf9f
JW
252 }
253
254 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
4f2e1536 255 error_at (loc, "invalid use of undefined type %qT", type);
400fbf9f
JW
256 else
257 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
4f2e1536 258 error_at (loc, "invalid use of incomplete typedef %qT", type);
400fbf9f
JW
259 }
260}
261
ab393bf1
NB
262/* Given a type, apply default promotions wrt unnamed function
263 arguments and return the new type. */
264
265tree
2f6e4e97 266c_type_promotes_to (tree type)
ab393bf1 267{
267bac10 268 tree ret = NULL_TREE;
ab393bf1 269
267bac10
JM
270 if (TYPE_MAIN_VARIANT (type) == float_type_node)
271 ret = double_type_node;
272 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
273 {
274 /* Preserve unsignedness if not really getting any wider. */
8df83eae 275 if (TYPE_UNSIGNED (type)
c22cacf3 276 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
277 ret = unsigned_type_node;
278 else
279 ret = integer_type_node;
ab393bf1
NB
280 }
281
267bac10
JM
282 if (ret != NULL_TREE)
283 return (TYPE_ATOMIC (type)
284 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
285 : ret);
286
ab393bf1
NB
287 return type;
288}
289
36c5e70a
BE
290/* Return true if between two named address spaces, whether there is a superset
291 named address space that encompasses both address spaces. If there is a
292 superset, return which address space is the superset. */
293
294static bool
295addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
296{
297 if (as1 == as2)
298 {
299 *common = as1;
300 return true;
301 }
302 else if (targetm.addr_space.subset_p (as1, as2))
303 {
304 *common = as2;
305 return true;
306 }
307 else if (targetm.addr_space.subset_p (as2, as1))
308 {
309 *common = as1;
310 return true;
311 }
312 else
313 return false;
314}
315
400fbf9f
JW
316/* Return a variant of TYPE which has all the type qualifiers of LIKE
317 as well as those of TYPE. */
318
319static tree
2f6e4e97 320qualify_type (tree type, tree like)
400fbf9f 321{
36c5e70a
BE
322 addr_space_t as_type = TYPE_ADDR_SPACE (type);
323 addr_space_t as_like = TYPE_ADDR_SPACE (like);
324 addr_space_t as_common;
325
326 /* If the two named address spaces are different, determine the common
327 superset address space. If there isn't one, raise an error. */
328 if (!addr_space_superset (as_type, as_like, &as_common))
329 {
330 as_common = as_type;
331 error ("%qT and %qT are in disjoint named address spaces",
332 type, like);
333 }
334
2f6e4e97 335 return c_build_qualified_type (type,
36c5e70a 336 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 337 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 338 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 339}
52ffd86e
MS
340
341/* Return true iff the given tree T is a variable length array. */
342
343bool
ac7d7749 344c_vla_type_p (const_tree t)
52ffd86e
MS
345{
346 if (TREE_CODE (t) == ARRAY_TYPE
347 && C_TYPE_VARIABLE_SIZE (t))
348 return true;
349 return false;
350}
400fbf9f 351\f
10bc1b1b 352/* Return the composite type of two compatible types.
5305f6d7 353
10bc1b1b
JM
354 We assume that comptypes has already been done and returned
355 nonzero; if that isn't so, this may crash. In particular, we
356 assume that qualifiers match. */
400fbf9f
JW
357
358tree
10bc1b1b 359composite_type (tree t1, tree t2)
400fbf9f 360{
b3694847
SS
361 enum tree_code code1;
362 enum tree_code code2;
4b027d16 363 tree attributes;
400fbf9f
JW
364
365 /* Save time if the two types are the same. */
366
367 if (t1 == t2) return t1;
368
369 /* If one type is nonsense, use the other. */
370 if (t1 == error_mark_node)
371 return t2;
372 if (t2 == error_mark_node)
373 return t1;
374
10bc1b1b
JM
375 code1 = TREE_CODE (t1);
376 code2 = TREE_CODE (t2);
377
d9525bec 378 /* Merge the attributes. */
5fd9b178 379 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 380
10bc1b1b
JM
381 /* If one is an enumerated type and the other is the compatible
382 integer type, the composite type might be either of the two
383 (DR#013 question 3). For consistency, use the enumerated type as
384 the composite type. */
400fbf9f 385
10bc1b1b
JM
386 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
387 return t1;
388 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
389 return t2;
75326e8c 390
366de0ce 391 gcc_assert (code1 == code2);
b6a10c9f 392
400fbf9f
JW
393 switch (code1)
394 {
400fbf9f 395 case POINTER_TYPE:
10bc1b1b 396 /* For two pointers, do this recursively on the target type. */
400fbf9f 397 {
3932261a
MM
398 tree pointed_to_1 = TREE_TYPE (t1);
399 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 400 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 401 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
402 t1 = build_type_attribute_variant (t1, attributes);
403 return qualify_type (t1, t2);
400fbf9f 404 }
400fbf9f
JW
405
406 case ARRAY_TYPE:
407 {
10bc1b1b 408 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
409 int quals;
410 tree unqual_elt;
ca8bdb78
JM
411 tree d1 = TYPE_DOMAIN (t1);
412 tree d2 = TYPE_DOMAIN (t2);
413 bool d1_variable, d2_variable;
414 bool d1_zero, d2_zero;
f6294de7 415 bool t1_complete, t2_complete;
46df2823 416
de46b2fe 417 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
418 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
419 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 420
f6294de7
JM
421 t1_complete = COMPLETE_TYPE_P (t1);
422 t2_complete = COMPLETE_TYPE_P (t2);
423
ca8bdb78
JM
424 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
425 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
426
427 d1_variable = (!d1_zero
428 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
429 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
430 d2_variable = (!d2_zero
431 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
432 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
433 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
434 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 435
400fbf9f 436 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
437 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
438 && (d2_variable || d2_zero || !d1_variable))
4b027d16 439 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
440 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
441 && (d1_variable || d1_zero || !d2_variable))
4b027d16 442 return build_type_attribute_variant (t2, attributes);
c22cacf3 443
de46b2fe
AP
444 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
445 return build_type_attribute_variant (t1, attributes);
446 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
447 return build_type_attribute_variant (t2, attributes);
c22cacf3 448
46df2823
JM
449 /* Merge the element types, and have a size if either arg has
450 one. We may have qualifiers on the element types. To set
451 up TYPE_MAIN_VARIANT correctly, we need to form the
452 composite of the unqualified types and add the qualifiers
453 back at the end. */
454 quals = TYPE_QUALS (strip_array_types (elt));
455 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
456 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
457 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
458 && (d2_variable
459 || d2_zero
460 || !d1_variable))
461 ? t1
462 : t2));
f6294de7
JM
463 /* Ensure a composite type involving a zero-length array type
464 is a zero-length type not an incomplete type. */
465 if (d1_zero && d2_zero
466 && (t1_complete || t2_complete)
467 && !COMPLETE_TYPE_P (t1))
468 {
469 TYPE_SIZE (t1) = bitsize_zero_node;
470 TYPE_SIZE_UNIT (t1) = size_zero_node;
471 }
46df2823 472 t1 = c_build_qualified_type (t1, quals);
de46b2fe 473 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
474 }
475
fcb99e7b
JJ
476 case ENUMERAL_TYPE:
477 case RECORD_TYPE:
478 case UNION_TYPE:
479 if (attributes != NULL)
480 {
481 /* Try harder not to create a new aggregate type. */
482 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
483 return t1;
484 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
485 return t2;
486 }
487 return build_type_attribute_variant (t1, attributes);
488
400fbf9f
JW
489 case FUNCTION_TYPE:
490 /* Function types: prefer the one that specified arg types.
491 If both do, merge the arg types. Also merge the return types. */
492 {
10bc1b1b 493 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
494 tree p1 = TYPE_ARG_TYPES (t1);
495 tree p2 = TYPE_ARG_TYPES (t2);
496 int len;
497 tree newargs, n;
498 int i;
499
500 /* Save space: see if the result is identical to one of the args. */
3f75a254 501 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 502 return build_type_attribute_variant (t1, attributes);
3f75a254 503 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 504 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
505
506 /* Simple way if one arg fails to specify argument types. */
507 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 508 {
fe7080d2
AP
509 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
510 t1 = build_type_attribute_variant (t1, attributes);
511 return qualify_type (t1, t2);
4b027d16 512 }
400fbf9f 513 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
514 {
515 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
516 t1 = build_type_attribute_variant (t1, attributes);
517 return qualify_type (t1, t2);
4b027d16 518 }
400fbf9f
JW
519
520 /* If both args specify argument types, we must merge the two
521 lists, argument by argument. */
2f4e8f2b 522
fcc2b74f
JJ
523 for (len = 0, newargs = p1;
524 newargs && newargs != void_list_node;
525 len++, newargs = TREE_CHAIN (newargs))
526 ;
400fbf9f
JW
527
528 for (i = 0; i < len; i++)
8d9bfdc5 529 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
530
531 n = newargs;
532
fcc2b74f 533 for (; p1 && p1 != void_list_node;
400fbf9f
JW
534 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
535 {
536 /* A null type means arg type is not specified.
537 Take whatever the other function type has. */
538 if (TREE_VALUE (p1) == 0)
539 {
540 TREE_VALUE (n) = TREE_VALUE (p2);
541 goto parm_done;
542 }
543 if (TREE_VALUE (p2) == 0)
544 {
545 TREE_VALUE (n) = TREE_VALUE (p1);
546 goto parm_done;
547 }
2f6e4e97 548
400fbf9f
JW
549 /* Given wait (union {union wait *u; int *i} *)
550 and wait (union wait *),
551 prefer union wait * as type of parm. */
552 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
553 && TREE_VALUE (p1) != TREE_VALUE (p2))
554 {
555 tree memb;
58cb41e6
JJ
556 tree mv2 = TREE_VALUE (p2);
557 if (mv2 && mv2 != error_mark_node
558 && TREE_CODE (mv2) != ARRAY_TYPE)
559 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 560 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 561 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
562 {
563 tree mv3 = TREE_TYPE (memb);
564 if (mv3 && mv3 != error_mark_node
565 && TREE_CODE (mv3) != ARRAY_TYPE)
566 mv3 = TYPE_MAIN_VARIANT (mv3);
567 if (comptypes (mv3, mv2))
568 {
569 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
570 TREE_VALUE (p2));
c1771a20 571 pedwarn (input_location, OPT_Wpedantic,
fcf73884 572 "function types not truly compatible in ISO C");
58cb41e6
JJ
573 goto parm_done;
574 }
575 }
400fbf9f
JW
576 }
577 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
578 && TREE_VALUE (p2) != TREE_VALUE (p1))
579 {
580 tree memb;
58cb41e6
JJ
581 tree mv1 = TREE_VALUE (p1);
582 if (mv1 && mv1 != error_mark_node
583 && TREE_CODE (mv1) != ARRAY_TYPE)
584 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 585 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 586 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
587 {
588 tree mv3 = TREE_TYPE (memb);
589 if (mv3 && mv3 != error_mark_node
590 && TREE_CODE (mv3) != ARRAY_TYPE)
591 mv3 = TYPE_MAIN_VARIANT (mv3);
592 if (comptypes (mv3, mv1))
593 {
594 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
595 TREE_VALUE (p1));
c1771a20 596 pedwarn (input_location, OPT_Wpedantic,
fcf73884 597 "function types not truly compatible in ISO C");
58cb41e6
JJ
598 goto parm_done;
599 }
600 }
400fbf9f 601 }
10bc1b1b 602 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
603 parm_done: ;
604 }
605
4b027d16 606 t1 = build_function_type (valtype, newargs);
fe7080d2 607 t1 = qualify_type (t1, t2);
400fbf9f 608 }
295844f6 609 /* FALLTHRU */
400fbf9f
JW
610
611 default:
4b027d16 612 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
613 }
614
615}
10bc1b1b
JM
616
617/* Return the type of a conditional expression between pointers to
618 possibly differently qualified versions of compatible types.
619
620 We assume that comp_target_types has already been done and returned
621 nonzero; if that isn't so, this may crash. */
622
623static tree
624common_pointer_type (tree t1, tree t2)
625{
626 tree attributes;
46df2823
JM
627 tree pointed_to_1, mv1;
628 tree pointed_to_2, mv2;
10bc1b1b 629 tree target;
eb1387a0 630 unsigned target_quals;
36c5e70a
BE
631 addr_space_t as1, as2, as_common;
632 int quals1, quals2;
10bc1b1b
JM
633
634 /* Save time if the two types are the same. */
635
636 if (t1 == t2) return t1;
637
638 /* If one type is nonsense, use the other. */
639 if (t1 == error_mark_node)
640 return t2;
641 if (t2 == error_mark_node)
642 return t1;
643
366de0ce 644 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 645 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
646
647 /* Merge the attributes. */
648 attributes = targetm.merge_type_attributes (t1, t2);
649
650 /* Find the composite type of the target types, and combine the
46df2823
JM
651 qualifiers of the two types' targets. Do not lose qualifiers on
652 array element types by taking the TYPE_MAIN_VARIANT. */
653 mv1 = pointed_to_1 = TREE_TYPE (t1);
654 mv2 = pointed_to_2 = TREE_TYPE (t2);
655 if (TREE_CODE (mv1) != ARRAY_TYPE)
656 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
657 if (TREE_CODE (mv2) != ARRAY_TYPE)
658 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
659 target = composite_type (mv1, mv2);
eb1387a0 660
768952be
MU
661 /* Strip array types to get correct qualifier for pointers to arrays */
662 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
663 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
664
eb1387a0
RG
665 /* For function types do not merge const qualifiers, but drop them
666 if used inconsistently. The middle-end uses these to mark const
667 and noreturn functions. */
668 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 669 target_quals = (quals1 & quals2);
eb1387a0 670 else
36c5e70a
BE
671 target_quals = (quals1 | quals2);
672
673 /* If the two named address spaces are different, determine the common
674 superset address space. This is guaranteed to exist due to the
675 assumption that comp_target_type returned non-zero. */
676 as1 = TYPE_ADDR_SPACE (pointed_to_1);
677 as2 = TYPE_ADDR_SPACE (pointed_to_2);
678 if (!addr_space_superset (as1, as2, &as_common))
679 gcc_unreachable ();
680
681 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
682
eb1387a0 683 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
684 return build_type_attribute_variant (t1, attributes);
685}
686
687/* Return the common type for two arithmetic types under the usual
688 arithmetic conversions. The default conversions have already been
689 applied, and enumerated types converted to their compatible integer
690 types. The resulting type is unqualified and has no attributes.
691
692 This is the type for the result of most arithmetic operations
693 if the operands have the given two types. */
694
ccf7f880
JJ
695static tree
696c_common_type (tree t1, tree t2)
10bc1b1b
JM
697{
698 enum tree_code code1;
699 enum tree_code code2;
700
701 /* If one type is nonsense, use the other. */
702 if (t1 == error_mark_node)
703 return t2;
704 if (t2 == error_mark_node)
705 return t1;
706
707 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
708 t1 = TYPE_MAIN_VARIANT (t1);
709
710 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
711 t2 = TYPE_MAIN_VARIANT (t2);
712
713 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
714 t1 = build_type_attribute_variant (t1, NULL_TREE);
715
716 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
717 t2 = build_type_attribute_variant (t2, NULL_TREE);
718
719 /* Save time if the two types are the same. */
720
721 if (t1 == t2) return t1;
722
723 code1 = TREE_CODE (t1);
724 code2 = TREE_CODE (t2);
725
366de0ce 726 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
727 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
728 || code1 == INTEGER_TYPE);
366de0ce 729 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
730 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
731 || code2 == INTEGER_TYPE);
10bc1b1b 732
5fc89bfd
JJ
733 /* When one operand is a decimal float type, the other operand cannot be
734 a generic float type or a complex type. We also disallow vector types
735 here. */
736 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
737 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
738 {
739 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
740 {
741 error ("can%'t mix operands of decimal float and vector types");
742 return error_mark_node;
743 }
744 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
745 {
746 error ("can%'t mix operands of decimal float and complex types");
747 return error_mark_node;
748 }
749 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
750 {
751 error ("can%'t mix operands of decimal float and other float types");
752 return error_mark_node;
753 }
754 }
755
10bc1b1b
JM
756 /* If one type is a vector type, return that type. (How the usual
757 arithmetic conversions apply to the vector types extension is not
758 precisely specified.) */
759 if (code1 == VECTOR_TYPE)
760 return t1;
761
762 if (code2 == VECTOR_TYPE)
763 return t2;
764
765 /* If one type is complex, form the common type of the non-complex
766 components, then make that complex. Use T1 or T2 if it is the
767 required type. */
768 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
769 {
770 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
771 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 772 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
773
774 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
775 return t1;
776 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
777 return t2;
778 else
779 return build_complex_type (subtype);
780 }
781
782 /* If only one is real, use it as the result. */
783
784 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
785 return t1;
786
787 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
788 return t2;
789
9a8ce21f
JG
790 /* If both are real and either are decimal floating point types, use
791 the decimal floating point type with the greater precision. */
792
793 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
794 {
795 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
796 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
797 return dfloat128_type_node;
798 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
799 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
800 return dfloat64_type_node;
801 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
802 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
803 return dfloat32_type_node;
804 }
805
ab22c1fa
CF
806 /* Deal with fixed-point types. */
807 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
808 {
809 unsigned int unsignedp = 0, satp = 0;
ef4bddc2 810 machine_mode m1, m2;
ab22c1fa
CF
811 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
812
813 m1 = TYPE_MODE (t1);
814 m2 = TYPE_MODE (t2);
815
816 /* If one input type is saturating, the result type is saturating. */
817 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
818 satp = 1;
819
820 /* If both fixed-point types are unsigned, the result type is unsigned.
821 When mixing fixed-point and integer types, follow the sign of the
822 fixed-point type.
823 Otherwise, the result type is signed. */
824 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
825 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
826 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
827 && TYPE_UNSIGNED (t1))
828 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
829 && TYPE_UNSIGNED (t2)))
830 unsignedp = 1;
831
832 /* The result type is signed. */
833 if (unsignedp == 0)
834 {
835 /* If the input type is unsigned, we need to convert to the
836 signed type. */
837 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
838 {
d75d71e0 839 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
840 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
841 mclass = MODE_FRACT;
842 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
843 mclass = MODE_ACCUM;
844 else
845 gcc_unreachable ();
846 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
847 }
848 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
849 {
d75d71e0 850 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
851 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
852 mclass = MODE_FRACT;
853 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
854 mclass = MODE_ACCUM;
855 else
856 gcc_unreachable ();
857 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
858 }
859 }
860
861 if (code1 == FIXED_POINT_TYPE)
862 {
863 fbit1 = GET_MODE_FBIT (m1);
864 ibit1 = GET_MODE_IBIT (m1);
865 }
866 else
867 {
868 fbit1 = 0;
869 /* Signed integers need to subtract one sign bit. */
870 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
871 }
872
873 if (code2 == FIXED_POINT_TYPE)
874 {
875 fbit2 = GET_MODE_FBIT (m2);
876 ibit2 = GET_MODE_IBIT (m2);
877 }
878 else
879 {
880 fbit2 = 0;
881 /* Signed integers need to subtract one sign bit. */
882 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
883 }
884
885 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
886 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
887 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
888 satp);
889 }
890
10bc1b1b
JM
891 /* Both real or both integers; use the one with greater precision. */
892
893 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
894 return t1;
895 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
896 return t2;
897
898 /* Same precision. Prefer long longs to longs to ints when the
899 same precision, following the C99 rules on integer type rank
900 (which are equivalent to the C90 rules for C90 types). */
901
902 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
903 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
904 return long_long_unsigned_type_node;
905
906 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
907 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
908 {
909 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
910 return long_long_unsigned_type_node;
911 else
c22cacf3 912 return long_long_integer_type_node;
10bc1b1b
JM
913 }
914
915 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
916 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
917 return long_unsigned_type_node;
918
919 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
920 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
921 {
922 /* But preserve unsignedness from the other type,
923 since long cannot hold all the values of an unsigned int. */
924 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
925 return long_unsigned_type_node;
926 else
927 return long_integer_type_node;
928 }
929
c65699ef
JM
930 /* For floating types of the same TYPE_PRECISION (which we here
931 assume means either the same set of values, or sets of values
932 neither a subset of the other, with behavior being undefined in
933 the latter case), follow the rules from TS 18661-3: prefer
934 interchange types _FloatN, then standard types long double,
935 double, float, then extended types _FloatNx. For extended types,
936 check them starting with _Float128x as that seems most consistent
937 in spirit with preferring long double to double; for interchange
938 types, also check in that order for consistency although it's not
939 possible for more than one of them to have the same
940 precision. */
941 tree mv1 = TYPE_MAIN_VARIANT (t1);
942 tree mv2 = TYPE_MAIN_VARIANT (t2);
943
944 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
945 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
946 return FLOATN_TYPE_NODE (i);
947
10bc1b1b 948 /* Likewise, prefer long double to double even if same size. */
c65699ef 949 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
10bc1b1b
JM
950 return long_double_type_node;
951
2531a1d9
JR
952 /* Likewise, prefer double to float even if same size.
953 We got a couple of embedded targets with 32 bit doubles, and the
954 pdp11 might have 64 bit floats. */
c65699ef 955 if (mv1 == double_type_node || mv2 == double_type_node)
2531a1d9
JR
956 return double_type_node;
957
c65699ef
JM
958 if (mv1 == float_type_node || mv2 == float_type_node)
959 return float_type_node;
960
961 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
962 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
963 return FLOATNX_TYPE_NODE (i);
964
10bc1b1b
JM
965 /* Otherwise prefer the unsigned one. */
966
967 if (TYPE_UNSIGNED (t1))
968 return t1;
969 else
970 return t2;
971}
400fbf9f 972\f
5922c215
JM
973/* Wrapper around c_common_type that is used by c-common.c and other
974 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
975 are allowed here and are converted to their compatible integer types.
976 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
977 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
978tree
979common_type (tree t1, tree t2)
980{
981 if (TREE_CODE (t1) == ENUMERAL_TYPE)
982 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
983 if (TREE_CODE (t2) == ENUMERAL_TYPE)
984 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
985
986 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
987 if (TREE_CODE (t1) == BOOLEAN_TYPE
988 && TREE_CODE (t2) == BOOLEAN_TYPE)
989 return boolean_type_node;
990
991 /* If either type is BOOLEAN_TYPE, then return the other. */
992 if (TREE_CODE (t1) == BOOLEAN_TYPE)
993 return t2;
994 if (TREE_CODE (t2) == BOOLEAN_TYPE)
995 return t1;
996
ccf7f880
JJ
997 return c_common_type (t1, t2);
998}
f13c9b2c 999
400fbf9f
JW
1000/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1001 or various other operations. Return 2 if they are compatible
1002 but a warning may be needed if you use them together. */
1003
1004int
132da1a5 1005comptypes (tree type1, tree type2)
f13c9b2c
AP
1006{
1007 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1008 int val;
1009
dc5027f4 1010 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
1011 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1012
1013 return val;
1014}
1015
1016/* Like comptypes, but if it returns non-zero because enum and int are
1017 compatible, it sets *ENUM_AND_INT_P to true. */
1018
1019static int
1020comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1021{
1022 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1023 int val;
1024
dc5027f4
JM
1025 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1026 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1027
1028 return val;
1029}
1030
1031/* Like comptypes, but if it returns nonzero for different types, it
1032 sets *DIFFERENT_TYPES_P to true. */
1033
1034int
1035comptypes_check_different_types (tree type1, tree type2,
1036 bool *different_types_p)
1037{
1038 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1039 int val;
1040
1041 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1042 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1043
f13c9b2c 1044 return val;
c22cacf3
MS
1045}
1046\f
f13c9b2c
AP
1047/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1048 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1049 but a warning may be needed if you use them together. If
1050 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1051 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1052 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1053 NULL, and the types are compatible but different enough not to be
48b0b196 1054 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1055 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1056 false, but may or may not be set if the types are incompatible.
1057 This differs from comptypes, in that we don't free the seen
1058 types. */
f13c9b2c
AP
1059
1060static int
dc5027f4
JM
1061comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1062 bool *different_types_p)
400fbf9f 1063{
58f9752a
KG
1064 const_tree t1 = type1;
1065 const_tree t2 = type2;
4b027d16 1066 int attrval, val;
400fbf9f
JW
1067
1068 /* Suppress errors caused by previously reported errors. */
1069
8d47dfc5
RH
1070 if (t1 == t2 || !t1 || !t2
1071 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1072 return 1;
1073
bca63328
JM
1074 /* Enumerated types are compatible with integer types, but this is
1075 not transitive: two enumerated types in the same translation unit
1076 are compatible with each other only if they are the same type. */
400fbf9f 1077
bca63328 1078 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1079 {
1080 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1081 if (TREE_CODE (t2) != VOID_TYPE)
1082 {
1083 if (enum_and_int_p != NULL)
1084 *enum_and_int_p = true;
1085 if (different_types_p != NULL)
1086 *different_types_p = true;
1087 }
744aa42f 1088 }
bca63328 1089 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1090 {
1091 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1092 if (TREE_CODE (t1) != VOID_TYPE)
1093 {
1094 if (enum_and_int_p != NULL)
1095 *enum_and_int_p = true;
1096 if (different_types_p != NULL)
1097 *different_types_p = true;
1098 }
744aa42f 1099 }
400fbf9f
JW
1100
1101 if (t1 == t2)
1102 return 1;
1103
1104 /* Different classes of types can't be compatible. */
1105
3aeb3655
EC
1106 if (TREE_CODE (t1) != TREE_CODE (t2))
1107 return 0;
400fbf9f 1108
118a3a8b 1109 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1110
3932261a 1111 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1112 return 0;
1113
08632da2
RS
1114 /* Allow for two different type nodes which have essentially the same
1115 definition. Note that we already checked for equality of the type
38e01259 1116 qualifiers (just above). */
400fbf9f 1117
46df2823
JM
1118 if (TREE_CODE (t1) != ARRAY_TYPE
1119 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1120 return 1;
1121
4b027d16 1122 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1123 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1124 return 0;
1125
1126 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1127 val = 0;
1128
400fbf9f
JW
1129 switch (TREE_CODE (t1))
1130 {
5545a907
MP
1131 case INTEGER_TYPE:
1132 case FIXED_POINT_TYPE:
1133 case REAL_TYPE:
1134 /* With these nodes, we can't determine type equivalence by
1135 looking at what is stored in the nodes themselves, because
1136 two nodes might have different TYPE_MAIN_VARIANTs but still
1137 represent the same type. For example, wchar_t and int could
1138 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1139 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1140 and are distinct types. On the other hand, int and the
1141 following typedef
1142
1143 typedef int INT __attribute((may_alias));
1144
1145 have identical properties, different TYPE_MAIN_VARIANTs, but
1146 represent the same type. The canonical type system keeps
1147 track of equivalence in this case, so we fall back on it. */
1148 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1149
400fbf9f 1150 case POINTER_TYPE:
5545a907
MP
1151 /* Do not remove mode information. */
1152 if (TYPE_MODE (t1) != TYPE_MODE (t2))
106f5de5 1153 break;
4b027d16 1154 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1155 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1156 enum_and_int_p, different_types_p));
4b027d16 1157 break;
400fbf9f
JW
1158
1159 case FUNCTION_TYPE:
dc5027f4
JM
1160 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1161 different_types_p);
4b027d16 1162 break;
400fbf9f
JW
1163
1164 case ARRAY_TYPE:
1165 {
400fbf9f
JW
1166 tree d1 = TYPE_DOMAIN (t1);
1167 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1168 bool d1_variable, d2_variable;
1169 bool d1_zero, d2_zero;
4b027d16 1170 val = 1;
400fbf9f
JW
1171
1172 /* Target types must match incl. qualifiers. */
1173 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1174 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1175 enum_and_int_p,
1176 different_types_p)))
400fbf9f
JW
1177 return 0;
1178
dc5027f4
JM
1179 if (different_types_p != NULL
1180 && (d1 == 0) != (d2 == 0))
1181 *different_types_p = true;
400fbf9f 1182 /* Sizes must match unless one is missing or variable. */
3f85558f 1183 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1184 break;
400fbf9f 1185
3f75a254
JM
1186 d1_zero = !TYPE_MAX_VALUE (d1);
1187 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1188
3f75a254 1189 d1_variable = (!d1_zero
3f85558f
RH
1190 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1191 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1192 d2_variable = (!d2_zero
3f85558f
RH
1193 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1194 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1195 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1196 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1197
dc5027f4
JM
1198 if (different_types_p != NULL
1199 && d1_variable != d2_variable)
1200 *different_types_p = true;
3f85558f
RH
1201 if (d1_variable || d2_variable)
1202 break;
1203 if (d1_zero && d2_zero)
1204 break;
1205 if (d1_zero || d2_zero
3f75a254
JM
1206 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1207 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1208 val = 0;
1209
c22cacf3 1210 break;
400fbf9f
JW
1211 }
1212
d1bd0ded 1213 case ENUMERAL_TYPE:
58393038 1214 case RECORD_TYPE:
d1bd0ded 1215 case UNION_TYPE:
766beae1 1216 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1217 {
fcb99e7b
JJ
1218 tree a1 = TYPE_ATTRIBUTES (t1);
1219 tree a2 = TYPE_ATTRIBUTES (t2);
1220
1221 if (! attribute_list_contained (a1, a2)
1222 && ! attribute_list_contained (a2, a1))
1223 break;
1224
f13c9b2c 1225 if (attrval != 2)
dc5027f4
JM
1226 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1227 different_types_p);
1228 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1229 different_types_p);
f13c9b2c 1230 }
4b027d16 1231 break;
e9a25f70 1232
62e1dfcf 1233 case VECTOR_TYPE:
744aa42f
ILT
1234 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1235 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1236 enum_and_int_p, different_types_p));
62e1dfcf
NC
1237 break;
1238
e9a25f70
JL
1239 default:
1240 break;
400fbf9f 1241 }
4b027d16 1242 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1243}
1244
36c5e70a
BE
1245/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1246 their qualifiers, except for named address spaces. If the pointers point to
1247 different named addresses, then we must determine if one address space is a
1248 subset of the other. */
400fbf9f
JW
1249
1250static int
744aa42f 1251comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1252{
392202b0 1253 int val;
768952be 1254 int val_ped;
36c5e70a
BE
1255 tree mvl = TREE_TYPE (ttl);
1256 tree mvr = TREE_TYPE (ttr);
1257 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1258 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1259 addr_space_t as_common;
744aa42f 1260 bool enum_and_int_p;
8b40563c 1261
36c5e70a
BE
1262 /* Fail if pointers point to incompatible address spaces. */
1263 if (!addr_space_superset (asl, asr, &as_common))
1264 return 0;
1265
768952be
MU
1266 /* For pedantic record result of comptypes on arrays before losing
1267 qualifiers on the element type below. */
1268 val_ped = 1;
1269
1270 if (TREE_CODE (mvl) == ARRAY_TYPE
1271 && TREE_CODE (mvr) == ARRAY_TYPE)
1272 val_ped = comptypes (mvl, mvr);
1273
1274 /* Qualifiers on element types of array types that are
1275 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1276
1277 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1278 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1279 : TYPE_MAIN_VARIANT (mvl));
1280
1281 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1282 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1283 : TYPE_MAIN_VARIANT (mvr));
1284
744aa42f
ILT
1285 enum_and_int_p = false;
1286 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1287
768952be
MU
1288 if (val == 1 && val_ped != 1)
1289 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1290 "are incompatible in ISO C");
1291
fcf73884 1292 if (val == 2)
c1771a20 1293 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1294
1295 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1296 warning_at (location, OPT_Wc___compat,
1297 "pointer target types incompatible in C++");
1298
400fbf9f
JW
1299 return val;
1300}
1301\f
1302/* Subroutines of `comptypes'. */
1303
f75fbaf7
ZW
1304/* Determine whether two trees derive from the same translation unit.
1305 If the CONTEXT chain ends in a null, that tree's context is still
1306 being parsed, so if two trees have context chains ending in null,
766beae1 1307 they're in the same translation unit. */
f75fbaf7 1308int
58f9752a 1309same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1310{
1311 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1312 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1313 {
6615c446
JO
1314 case tcc_declaration:
1315 t1 = DECL_CONTEXT (t1); break;
1316 case tcc_type:
1317 t1 = TYPE_CONTEXT (t1); break;
1318 case tcc_exceptional:
1319 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1320 default: gcc_unreachable ();
766beae1
ZW
1321 }
1322
1323 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1324 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1325 {
6615c446
JO
1326 case tcc_declaration:
1327 t2 = DECL_CONTEXT (t2); break;
1328 case tcc_type:
1329 t2 = TYPE_CONTEXT (t2); break;
1330 case tcc_exceptional:
1331 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1332 default: gcc_unreachable ();
766beae1
ZW
1333 }
1334
1335 return t1 == t2;
1336}
1337
f13c9b2c 1338/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1339
f13c9b2c 1340static struct tagged_tu_seen_cache *
58f9752a 1341alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1342{
cceb1885 1343 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1344 tu->next = tagged_tu_seen_base;
1345 tu->t1 = t1;
1346 tu->t2 = t2;
c22cacf3 1347
f13c9b2c 1348 tagged_tu_seen_base = tu;
c22cacf3 1349
f13c9b2c
AP
1350 /* The C standard says that two structures in different translation
1351 units are compatible with each other only if the types of their
1352 fields are compatible (among other things). We assume that they
1353 are compatible until proven otherwise when building the cache.
1354 An example where this can occur is:
1355 struct a
1356 {
1357 struct a *next;
1358 };
1359 If we are comparing this against a similar struct in another TU,
c83eecad 1360 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1361 loop. */
1362 tu->val = 1;
1363 return tu;
1364}
d1bd0ded 1365
f13c9b2c 1366/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1367
f13c9b2c
AP
1368static void
1369free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1370{
1371 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1372 while (tu != tu_til)
1373 {
741ac903
KG
1374 const struct tagged_tu_seen_cache *const tu1
1375 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1376 tu = tu1->next;
b1d5455a 1377 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1378 }
1379 tagged_tu_seen_base = tu_til;
1380}
d1bd0ded
GK
1381
1382/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1383 compatible. If the two types are not the same (which has been
1384 checked earlier), this can only happen when multiple translation
1385 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1386 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1387 comptypes_internal. */
d1bd0ded
GK
1388
1389static int
744aa42f 1390tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1391 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1392{
1393 tree s1, s2;
1394 bool needs_warning = false;
3aeb3655 1395
d1bd0ded
GK
1396 /* We have to verify that the tags of the types are the same. This
1397 is harder than it looks because this may be a typedef, so we have
1398 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1399 typedef...
1400 In the case of compiler-created builtin structs the TYPE_DECL
1401 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1402 while (TYPE_NAME (t1)
1403 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1404 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1405 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1406
dea984dc
ILT
1407 while (TYPE_NAME (t2)
1408 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1409 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1410 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1411
1412 /* C90 didn't have the requirement that the two tags be the same. */
1413 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1414 return 0;
3aeb3655 1415
d1bd0ded
GK
1416 /* C90 didn't say what happened if one or both of the types were
1417 incomplete; we choose to follow C99 rules here, which is that they
1418 are compatible. */
1419 if (TYPE_SIZE (t1) == NULL
1420 || TYPE_SIZE (t2) == NULL)
1421 return 1;
3aeb3655 1422
d1bd0ded 1423 {
f13c9b2c 1424 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1425 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1426 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1427 return tts_i->val;
d1bd0ded 1428 }
3aeb3655 1429
d1bd0ded
GK
1430 switch (TREE_CODE (t1))
1431 {
1432 case ENUMERAL_TYPE:
1433 {
f13c9b2c 1434 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1435 /* Speed up the case where the type values are in the same order. */
1436 tree tv1 = TYPE_VALUES (t1);
1437 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1438
c22cacf3 1439 if (tv1 == tv2)
f13c9b2c
AP
1440 {
1441 return 1;
1442 }
3aeb3655 1443
c22cacf3
MS
1444 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1445 {
1446 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1447 break;
1448 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1449 {
c22cacf3 1450 tu->val = 0;
f13c9b2c
AP
1451 return 0;
1452 }
c22cacf3 1453 }
3aeb3655 1454
c22cacf3 1455 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1456 {
1457 return 1;
1458 }
c22cacf3 1459 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1460 {
1461 tu->val = 0;
1462 return 0;
1463 }
3aeb3655 1464
d1bd0ded 1465 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1466 {
1467 tu->val = 0;
1468 return 0;
1469 }
3aeb3655 1470
d1bd0ded
GK
1471 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1472 {
1473 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1474 if (s2 == NULL
1475 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1476 {
1477 tu->val = 0;
1478 return 0;
1479 }
d1bd0ded
GK
1480 }
1481 return 1;
1482 }
1483
1484 case UNION_TYPE:
1485 {
f13c9b2c 1486 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1487 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1488 {
1489 tu->val = 0;
1490 return 0;
1491 }
c22cacf3 1492
f13c9b2c
AP
1493 /* Speed up the common case where the fields are in the same order. */
1494 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1495 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1496 {
1497 int result;
c22cacf3 1498
3ae4d3cc 1499 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1500 break;
744aa42f 1501 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1502 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1503
1504 if (result != 1 && !DECL_NAME (s1))
1505 break;
f13c9b2c
AP
1506 if (result == 0)
1507 {
1508 tu->val = 0;
1509 return 0;
1510 }
1511 if (result == 2)
1512 needs_warning = true;
1513
1514 if (TREE_CODE (s1) == FIELD_DECL
1515 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1516 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1517 {
1518 tu->val = 0;
1519 return 0;
1520 }
1521 }
1522 if (!s1 && !s2)
1523 {
1524 tu->val = needs_warning ? 2 : 1;
1525 return tu->val;
1526 }
d1bd0ded 1527
910ad8de 1528 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1529 {
1530 bool ok = false;
3aeb3655 1531
910ad8de 1532 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1533 if (DECL_NAME (s1) == DECL_NAME (s2))
1534 {
1535 int result;
1536
744aa42f 1537 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1538 enum_and_int_p,
1539 different_types_p);
3ae4d3cc
AO
1540
1541 if (result != 1 && !DECL_NAME (s1))
1542 continue;
1543 if (result == 0)
1544 {
1545 tu->val = 0;
1546 return 0;
1547 }
1548 if (result == 2)
1549 needs_warning = true;
1550
1551 if (TREE_CODE (s1) == FIELD_DECL
1552 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1553 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1554 break;
3ae4d3cc
AO
1555
1556 ok = true;
1557 break;
1558 }
3f75a254 1559 if (!ok)
f13c9b2c
AP
1560 {
1561 tu->val = 0;
1562 return 0;
1563 }
d1bd0ded 1564 }
f13c9b2c
AP
1565 tu->val = needs_warning ? 2 : 10;
1566 return tu->val;
d1bd0ded
GK
1567 }
1568
1569 case RECORD_TYPE:
1570 {
c22cacf3 1571 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1572
1573 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1574 s1 && s2;
910ad8de 1575 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1576 {
1577 int result;
1578 if (TREE_CODE (s1) != TREE_CODE (s2)
1579 || DECL_NAME (s1) != DECL_NAME (s2))
1580 break;
744aa42f 1581 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1582 enum_and_int_p, different_types_p);
d1bd0ded
GK
1583 if (result == 0)
1584 break;
1585 if (result == 2)
1586 needs_warning = true;
3aeb3655 1587
d1bd0ded
GK
1588 if (TREE_CODE (s1) == FIELD_DECL
1589 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1590 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1591 break;
1592 }
d1bd0ded 1593 if (s1 && s2)
f13c9b2c
AP
1594 tu->val = 0;
1595 else
1596 tu->val = needs_warning ? 2 : 1;
1597 return tu->val;
d1bd0ded
GK
1598 }
1599
1600 default:
366de0ce 1601 gcc_unreachable ();
d1bd0ded
GK
1602 }
1603}
1604
400fbf9f
JW
1605/* Return 1 if two function types F1 and F2 are compatible.
1606 If either type specifies no argument types,
1607 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1608 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1609 the other must specify that number of self-promoting arg types.
744aa42f 1610 Otherwise, the argument types must match.
dc5027f4 1611 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1612
1613static int
744aa42f 1614function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1615 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1616{
1617 tree args1, args2;
1618 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1619 int val = 1;
1620 int val1;
a6fdc086
GK
1621 tree ret1, ret2;
1622
1623 ret1 = TREE_TYPE (f1);
1624 ret2 = TREE_TYPE (f2);
1625
e508a019
JM
1626 /* 'volatile' qualifiers on a function's return type used to mean
1627 the function is noreturn. */
1628 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1629 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1630 if (TYPE_VOLATILE (ret1))
1631 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1632 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1633 if (TYPE_VOLATILE (ret2))
1634 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1635 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1636 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1637 if (val == 0)
400fbf9f
JW
1638 return 0;
1639
1640 args1 = TYPE_ARG_TYPES (f1);
1641 args2 = TYPE_ARG_TYPES (f2);
1642
dc5027f4
JM
1643 if (different_types_p != NULL
1644 && (args1 == 0) != (args2 == 0))
1645 *different_types_p = true;
1646
400fbf9f
JW
1647 /* An unspecified parmlist matches any specified parmlist
1648 whose argument types don't need default promotions. */
1649
1650 if (args1 == 0)
1651 {
1652 if (!self_promoting_args_p (args2))
1653 return 0;
1654 /* If one of these types comes from a non-prototype fn definition,
1655 compare that with the other type's arglist.
3176a0c2 1656 If they don't match, ask for a warning (but no error). */
400fbf9f 1657 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1658 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1659 enum_and_int_p, different_types_p))
400fbf9f
JW
1660 val = 2;
1661 return val;
1662 }
1663 if (args2 == 0)
1664 {
1665 if (!self_promoting_args_p (args1))
1666 return 0;
1667 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1668 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1669 enum_and_int_p, different_types_p))
400fbf9f
JW
1670 val = 2;
1671 return val;
1672 }
1673
1674 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1675 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1676 different_types_p);
400fbf9f
JW
1677 return val1 != 1 ? val1 : val;
1678}
1679
744aa42f
ILT
1680/* Check two lists of types for compatibility, returning 0 for
1681 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1682 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1683 comptypes_internal. */
400fbf9f
JW
1684
1685static int
744aa42f 1686type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1687 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1688{
1689 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1690 int val = 1;
9d5f3e49 1691 int newval = 0;
400fbf9f
JW
1692
1693 while (1)
1694 {
46df2823 1695 tree a1, mv1, a2, mv2;
400fbf9f
JW
1696 if (args1 == 0 && args2 == 0)
1697 return val;
1698 /* If one list is shorter than the other,
1699 they fail to match. */
1700 if (args1 == 0 || args2 == 0)
1701 return 0;
46df2823
JM
1702 mv1 = a1 = TREE_VALUE (args1);
1703 mv2 = a2 = TREE_VALUE (args2);
1704 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1705 mv1 = (TYPE_ATOMIC (mv1)
1706 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1707 TYPE_QUAL_ATOMIC)
1708 : TYPE_MAIN_VARIANT (mv1));
46df2823 1709 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1710 mv2 = (TYPE_ATOMIC (mv2)
1711 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1712 TYPE_QUAL_ATOMIC)
1713 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1714 /* A null pointer instead of a type
1715 means there is supposed to be an argument
1716 but nothing is specified about what type it has.
1717 So match anything that self-promotes. */
dc5027f4
JM
1718 if (different_types_p != NULL
1719 && (a1 == 0) != (a2 == 0))
1720 *different_types_p = true;
46df2823 1721 if (a1 == 0)
400fbf9f 1722 {
46df2823 1723 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1724 return 0;
1725 }
46df2823 1726 else if (a2 == 0)
400fbf9f 1727 {
46df2823 1728 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1729 return 0;
1730 }
8f5b6d29 1731 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1732 else if (TREE_CODE (a1) == ERROR_MARK
1733 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1734 ;
dc5027f4
JM
1735 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1736 different_types_p)))
400fbf9f 1737 {
dc5027f4
JM
1738 if (different_types_p != NULL)
1739 *different_types_p = true;
400fbf9f
JW
1740 /* Allow wait (union {union wait *u; int *i} *)
1741 and wait (union wait *) to be compatible. */
46df2823
JM
1742 if (TREE_CODE (a1) == UNION_TYPE
1743 && (TYPE_NAME (a1) == 0
ebf0bf7f 1744 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1745 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1746 && tree_int_cst_equal (TYPE_SIZE (a1),
1747 TYPE_SIZE (a2)))
400fbf9f
JW
1748 {
1749 tree memb;
46df2823 1750 for (memb = TYPE_FIELDS (a1);
910ad8de 1751 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1752 {
1753 tree mv3 = TREE_TYPE (memb);
1754 if (mv3 && mv3 != error_mark_node
1755 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1756 mv3 = (TYPE_ATOMIC (mv3)
1757 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1758 TYPE_QUAL_ATOMIC)
1759 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1760 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1761 different_types_p))
58cb41e6
JJ
1762 break;
1763 }
400fbf9f
JW
1764 if (memb == 0)
1765 return 0;
1766 }
46df2823
JM
1767 else if (TREE_CODE (a2) == UNION_TYPE
1768 && (TYPE_NAME (a2) == 0
ebf0bf7f 1769 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1770 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1771 && tree_int_cst_equal (TYPE_SIZE (a2),
1772 TYPE_SIZE (a1)))
400fbf9f
JW
1773 {
1774 tree memb;
46df2823 1775 for (memb = TYPE_FIELDS (a2);
910ad8de 1776 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1777 {
1778 tree mv3 = TREE_TYPE (memb);
1779 if (mv3 && mv3 != error_mark_node
1780 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1781 mv3 = (TYPE_ATOMIC (mv3)
1782 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1783 TYPE_QUAL_ATOMIC)
1784 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1785 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1786 different_types_p))
58cb41e6
JJ
1787 break;
1788 }
400fbf9f
JW
1789 if (memb == 0)
1790 return 0;
1791 }
1792 else
1793 return 0;
1794 }
1795
1796 /* comptypes said ok, but record if it said to warn. */
1797 if (newval > val)
1798 val = newval;
1799
1800 args1 = TREE_CHAIN (args1);
1801 args2 = TREE_CHAIN (args2);
1802 }
1803}
400fbf9f 1804\f
a0e24419
MP
1805/* Compute the size to increment a pointer by. When a function type or void
1806 type or incomplete type is passed, size_one_node is returned.
1807 This function does not emit any diagnostics; the caller is responsible
1808 for that. */
400fbf9f 1809
4e2fb7de 1810static tree
58f9752a 1811c_size_in_bytes (const_tree type)
400fbf9f
JW
1812{
1813 enum tree_code code = TREE_CODE (type);
1814
a0e24419
MP
1815 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1816 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1817 return size_one_node;
1818
400fbf9f 1819 /* Convert in case a char is more than one unit. */
db3927fb
AH
1820 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1821 size_int (TYPE_PRECISION (char_type_node)
1822 / BITS_PER_UNIT));
400fbf9f 1823}
400fbf9f 1824\f
400fbf9f
JW
1825/* Return either DECL or its known constant value (if it has one). */
1826
56cb9733 1827tree
2f6e4e97 1828decl_constant_value (tree decl)
400fbf9f 1829{
a7c1916a 1830 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1831 in a place where a variable is invalid. Note that DECL_INITIAL
1832 isn't valid for a PARM_DECL. */
a7c1916a 1833 current_function_decl != 0
4f976745 1834 && TREE_CODE (decl) != PARM_DECL
3f75a254 1835 && !TREE_THIS_VOLATILE (decl)
83bab8db 1836 && TREE_READONLY (decl)
400fbf9f
JW
1837 && DECL_INITIAL (decl) != 0
1838 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1839 /* This is invalid if initial value is not constant.
1840 If it has either a function call, a memory reference,
1841 or a variable, then re-evaluating it could give different results. */
1842 && TREE_CONSTANT (DECL_INITIAL (decl))
1843 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1844 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1845 return DECL_INITIAL (decl);
1846 return decl;
1847}
1848
f2a71bbc
JM
1849/* Convert the array expression EXP to a pointer. */
1850static tree
c2255bc4 1851array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1852{
f2a71bbc 1853 tree orig_exp = exp;
207bf485 1854 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1855 tree adr;
1856 tree restype = TREE_TYPE (type);
1857 tree ptrtype;
207bf485 1858
f2a71bbc 1859 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1860
f2a71bbc 1861 STRIP_TYPE_NOPS (exp);
207bf485 1862
487a92fe
JM
1863 if (TREE_NO_WARNING (orig_exp))
1864 TREE_NO_WARNING (exp) = 1;
207bf485 1865
f2a71bbc
JM
1866 ptrtype = build_pointer_type (restype);
1867
22d03525 1868 if (INDIRECT_REF_P (exp))
f2a71bbc
JM
1869 return convert (ptrtype, TREE_OPERAND (exp, 0));
1870
1f37c583
JM
1871 /* In C++ array compound literals are temporary objects unless they are
1872 const or appear in namespace scope, so they are destroyed too soon
1873 to use them for much of anything (c++/53220). */
1874 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1875 {
1876 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1877 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1878 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1879 "converting an array compound literal to a pointer "
1880 "is ill-formed in C++");
1881 }
1882
e51fbec3 1883 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
f2a71bbc
JM
1884 return convert (ptrtype, adr);
1885}
207bf485 1886
f2a71bbc
JM
1887/* Convert the function expression EXP to a pointer. */
1888static tree
c2255bc4 1889function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1890{
1891 tree orig_exp = exp;
207bf485 1892
f2a71bbc 1893 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1894
f2a71bbc 1895 STRIP_TYPE_NOPS (exp);
207bf485 1896
f2a71bbc
JM
1897 if (TREE_NO_WARNING (orig_exp))
1898 TREE_NO_WARNING (exp) = 1;
207bf485 1899
e51fbec3 1900 return build_unary_op (loc, ADDR_EXPR, exp, false);
f2a71bbc 1901}
207bf485 1902
ebfbbdc5
JJ
1903/* Mark EXP as read, not just set, for set but not used -Wunused
1904 warning purposes. */
1905
1906void
1907mark_exp_read (tree exp)
1908{
1909 switch (TREE_CODE (exp))
1910 {
1911 case VAR_DECL:
1912 case PARM_DECL:
1913 DECL_READ_P (exp) = 1;
1914 break;
1915 case ARRAY_REF:
1916 case COMPONENT_REF:
1917 case MODIFY_EXPR:
1918 case REALPART_EXPR:
1919 case IMAGPART_EXPR:
1920 CASE_CONVERT:
1921 case ADDR_EXPR:
557e8c49 1922 case VIEW_CONVERT_EXPR:
ebfbbdc5
JJ
1923 mark_exp_read (TREE_OPERAND (exp, 0));
1924 break;
1925 case COMPOUND_EXPR:
82c3c067 1926 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1927 mark_exp_read (TREE_OPERAND (exp, 1));
1928 break;
1929 default:
1930 break;
1931 }
1932}
1933
f2a71bbc
JM
1934/* Perform the default conversion of arrays and functions to pointers.
1935 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1936 return EXP.
1937
1938 LOC is the location of the expression. */
f2a71bbc
JM
1939
1940struct c_expr
c2255bc4 1941default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1942{
1943 tree orig_exp = exp.value;
1944 tree type = TREE_TYPE (exp.value);
1945 enum tree_code code = TREE_CODE (type);
1946
1947 switch (code)
1948 {
1949 case ARRAY_TYPE:
1950 {
1951 bool not_lvalue = false;
1952 bool lvalue_array_p;
1953
1954 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1955 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1956 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1957 {
1958 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1959 not_lvalue = true;
1960 exp.value = TREE_OPERAND (exp.value, 0);
1961 }
1962
1963 if (TREE_NO_WARNING (orig_exp))
1964 TREE_NO_WARNING (exp.value) = 1;
1965
1966 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1967 if (!flag_isoc99 && !lvalue_array_p)
1968 {
1969 /* Before C99, non-lvalue arrays do not decay to pointers.
1970 Normally, using such an array would be invalid; but it can
1971 be used correctly inside sizeof or as a statement expression.
1972 Thus, do not give an error here; an error will result later. */
1973 return exp;
1974 }
1975
c2255bc4 1976 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1977 }
1978 break;
1979 case FUNCTION_TYPE:
c2255bc4 1980 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1981 break;
1982 default:
f2a71bbc 1983 break;
207bf485 1984 }
f2a71bbc 1985
207bf485
JM
1986 return exp;
1987}
1988
ebfbbdc5
JJ
1989struct c_expr
1990default_function_array_read_conversion (location_t loc, struct c_expr exp)
1991{
1992 mark_exp_read (exp.value);
1993 return default_function_array_conversion (loc, exp);
1994}
522ddfa2 1995
267bac10
JM
1996/* Return whether EXPR should be treated as an atomic lvalue for the
1997 purposes of load and store handling. */
1998
1999static bool
2000really_atomic_lvalue (tree expr)
2001{
7a0ca710 2002 if (error_operand_p (expr))
267bac10
JM
2003 return false;
2004 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2005 return false;
2006 if (!lvalue_p (expr))
2007 return false;
2008
2009 /* Ignore _Atomic on register variables, since their addresses can't
2010 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2011 sequences wouldn't work. Ignore _Atomic on structures containing
2012 bit-fields, since accessing elements of atomic structures or
2013 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2014 it's undefined at translation time or execution time, and the
2015 normal atomic sequences again wouldn't work. */
2016 while (handled_component_p (expr))
2017 {
2018 if (TREE_CODE (expr) == COMPONENT_REF
2019 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2020 return false;
2021 expr = TREE_OPERAND (expr, 0);
2022 }
2023 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2024 return false;
2025 return true;
2026}
2027
2028/* Convert expression EXP (location LOC) from lvalue to rvalue,
2029 including converting functions and arrays to pointers if CONVERT_P.
2030 If READ_P, also mark the expression as having been read. */
2031
2032struct c_expr
2033convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2034 bool convert_p, bool read_p)
2035{
2036 if (read_p)
2037 mark_exp_read (exp.value);
2038 if (convert_p)
2039 exp = default_function_array_conversion (loc, exp);
2040 if (really_atomic_lvalue (exp.value))
2041 {
2042 vec<tree, va_gc> *params;
2043 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2044 tree expr_type = TREE_TYPE (exp.value);
e51fbec3 2045 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
267bac10
JM
2046 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2047
2048 gcc_assert (TYPE_ATOMIC (expr_type));
2049
2050 /* Expansion of a generic atomic load may require an addition
2051 element, so allocate enough to prevent a resize. */
2052 vec_alloc (params, 4);
2053
2054 /* Remove the qualifiers for the rest of the expressions and
2055 create the VAL temp variable to hold the RHS. */
2056 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
5c4abbb8 2057 tmp = create_tmp_var_raw (nonatomic_type);
e51fbec3 2058 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
267bac10 2059 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2060 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2061
2062 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2063 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2064 params->quick_push (expr_addr);
2065 params->quick_push (tmp_addr);
2066 params->quick_push (seq_cst);
8edbfaa6 2067 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2068
cc28fc7f
MP
2069 /* EXPR is always read. */
2070 mark_exp_read (exp.value);
2071
267bac10 2072 /* Return tmp which contains the value loaded. */
5c4abbb8
MP
2073 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2074 NULL_TREE, NULL_TREE);
267bac10
JM
2075 }
2076 return exp;
2077}
2078
522ddfa2
JM
2079/* EXP is an expression of integer type. Apply the integer promotions
2080 to it and return the promoted value. */
400fbf9f
JW
2081
2082tree
522ddfa2 2083perform_integral_promotions (tree exp)
400fbf9f 2084{
b3694847
SS
2085 tree type = TREE_TYPE (exp);
2086 enum tree_code code = TREE_CODE (type);
400fbf9f 2087
522ddfa2 2088 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2089
400fbf9f
JW
2090 /* Normally convert enums to int,
2091 but convert wide enums to something wider. */
2092 if (code == ENUMERAL_TYPE)
2093 {
b0c48229
NB
2094 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2095 TYPE_PRECISION (integer_type_node)),
2096 ((TYPE_PRECISION (type)
2097 >= TYPE_PRECISION (integer_type_node))
8df83eae 2098 && TYPE_UNSIGNED (type)));
05bccae2 2099
400fbf9f
JW
2100 return convert (type, exp);
2101 }
2102
522ddfa2
JM
2103 /* ??? This should no longer be needed now bit-fields have their
2104 proper types. */
9753f113 2105 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2106 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2107 /* If it's thinner than an int, promote it like a
d72040f5 2108 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2109 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2110 TYPE_PRECISION (integer_type_node)))
f458d1d5 2111 return convert (integer_type_node, exp);
9753f113 2112
d72040f5 2113 if (c_promoting_integer_type_p (type))
400fbf9f 2114 {
f458d1d5 2115 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2116 if (TYPE_UNSIGNED (type)
f458d1d5 2117 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2118 return convert (unsigned_type_node, exp);
05bccae2 2119
400fbf9f
JW
2120 return convert (integer_type_node, exp);
2121 }
05bccae2 2122
522ddfa2
JM
2123 return exp;
2124}
2125
2126
2127/* Perform default promotions for C data used in expressions.
46bdb9cf 2128 Enumeral types or short or char are converted to int.
522ddfa2
JM
2129 In addition, manifest constants symbols are replaced by their values. */
2130
2131tree
2132default_conversion (tree exp)
2133{
2134 tree orig_exp;
2135 tree type = TREE_TYPE (exp);
2136 enum tree_code code = TREE_CODE (type);
40449a90 2137 tree promoted_type;
522ddfa2 2138
ebfbbdc5
JJ
2139 mark_exp_read (exp);
2140
46bdb9cf
JM
2141 /* Functions and arrays have been converted during parsing. */
2142 gcc_assert (code != FUNCTION_TYPE);
2143 if (code == ARRAY_TYPE)
2144 return exp;
522ddfa2
JM
2145
2146 /* Constants can be used directly unless they're not loadable. */
2147 if (TREE_CODE (exp) == CONST_DECL)
2148 exp = DECL_INITIAL (exp);
2149
522ddfa2
JM
2150 /* Strip no-op conversions. */
2151 orig_exp = exp;
2152 STRIP_TYPE_NOPS (exp);
2153
2154 if (TREE_NO_WARNING (orig_exp))
2155 TREE_NO_WARNING (exp) = 1;
2156
400fbf9f
JW
2157 if (code == VOID_TYPE)
2158 {
5436fa2e
MP
2159 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2160 "void value not ignored as it ought to be");
400fbf9f
JW
2161 return error_mark_node;
2162 }
808d6eaa 2163
4f2e1536 2164 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
808d6eaa
JM
2165 if (exp == error_mark_node)
2166 return error_mark_node;
2167
40449a90
SL
2168 promoted_type = targetm.promoted_type (type);
2169 if (promoted_type)
2170 return convert (promoted_type, exp);
2171
808d6eaa
JM
2172 if (INTEGRAL_TYPE_P (type))
2173 return perform_integral_promotions (exp);
2174
400fbf9f
JW
2175 return exp;
2176}
2177\f
0fb96aa4 2178/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2179
2180 If the component name is not found, returns NULL_TREE. Otherwise,
2181 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2182 stepping down the chain to the component, which is in the last
2183 TREE_VALUE of the list. Normally the list is of length one, but if
2184 the component is embedded within (nested) anonymous structures or
2185 unions, the list steps down the chain to the component. */
2f6e4e97 2186
2f2d13da 2187static tree
0fb96aa4 2188lookup_field (tree type, tree component)
2f2d13da
DE
2189{
2190 tree field;
2191
2192 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2193 to the field elements. Use a binary search on this array to quickly
2194 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2195 will always be set for structures which have many elements. */
2196
22a0b85f 2197 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2198 {
2199 int bot, top, half;
d07605f5 2200 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2201
2202 field = TYPE_FIELDS (type);
2203 bot = 0;
d07605f5 2204 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2205 while (top - bot > 1)
2206 {
2f2d13da
DE
2207 half = (top - bot + 1) >> 1;
2208 field = field_array[bot+half];
2209
2210 if (DECL_NAME (field) == NULL_TREE)
2211 {
2212 /* Step through all anon unions in linear fashion. */
2213 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2214 {
2f2d13da 2215 field = field_array[bot++];
3e636daf 2216 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
19d76e60 2217 {
0fb96aa4 2218 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2219
2220 if (anon)
2221 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2222
2223 /* The Plan 9 compiler permits referring
2224 directly to an anonymous struct/union field
2225 using a typedef name. */
2226 if (flag_plan9_extensions
2227 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2228 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2229 == TYPE_DECL)
2230 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2231 == component))
2232 break;
2f6e4e97 2233 }
2f2d13da
DE
2234 }
2235
2236 /* Entire record is only anon unions. */
2237 if (bot > top)
2238 return NULL_TREE;
2239
2240 /* Restart the binary search, with new lower bound. */
2241 continue;
2242 }
2243
e8b87aac 2244 if (DECL_NAME (field) == component)
2f2d13da 2245 break;
e8b87aac 2246 if (DECL_NAME (field) < component)
2f2d13da
DE
2247 bot += half;
2248 else
2249 top = bot + half;
2250 }
2251
2252 if (DECL_NAME (field_array[bot]) == component)
2253 field = field_array[bot];
2254 else if (DECL_NAME (field) != component)
e9b2c823 2255 return NULL_TREE;
2f2d13da
DE
2256 }
2257 else
2258 {
910ad8de 2259 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2260 {
e9b2c823 2261 if (DECL_NAME (field) == NULL_TREE
3e636daf 2262 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2f2d13da 2263 {
0fb96aa4 2264 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2265
e9b2c823
NB
2266 if (anon)
2267 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2268
2269 /* The Plan 9 compiler permits referring directly to an
2270 anonymous struct/union field using a typedef
2271 name. */
2272 if (flag_plan9_extensions
2273 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2274 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2275 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2276 == component))
2277 break;
2f2d13da
DE
2278 }
2279
2280 if (DECL_NAME (field) == component)
2281 break;
2282 }
e9b2c823
NB
2283
2284 if (field == NULL_TREE)
2285 return NULL_TREE;
2f2d13da
DE
2286 }
2287
e9b2c823 2288 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2289}
2290
277fe616
DM
2291/* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2292
2293static void
2294lookup_field_fuzzy_find_candidates (tree type, tree component,
2295 vec<tree> *candidates)
2296{
2297 tree field;
2298 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2299 {
2300 if (DECL_NAME (field) == NULL_TREE
3e636daf
MP
2301 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2302 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2303 candidates);
277fe616
DM
2304
2305 if (DECL_NAME (field))
2306 candidates->safe_push (DECL_NAME (field));
2307 }
2308}
2309
2310/* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2311 rather than returning a TREE_LIST for an exact match. */
2312
2313static tree
2314lookup_field_fuzzy (tree type, tree component)
2315{
2316 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2317
2318 /* First, gather a list of candidates. */
2319 auto_vec <tree> candidates;
2320
2321 lookup_field_fuzzy_find_candidates (type, component,
2322 &candidates);
2323
8ece8dfb 2324 return find_closest_identifier (component, &candidates);
277fe616
DM
2325}
2326
a87a86e1
DM
2327/* Support function for build_component_ref's error-handling.
2328
2329 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2330 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2331
2332static bool
2333should_suggest_deref_p (tree datum_type)
2334{
2335 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2336 allows "." for ptrs; we could be handling a failed attempt
2337 to access a property. */
2338 if (c_dialect_objc ())
2339 return false;
2340
2341 /* Only suggest it for pointers... */
2342 if (TREE_CODE (datum_type) != POINTER_TYPE)
2343 return false;
2344
2345 /* ...to structs/unions. */
2346 tree underlying_type = TREE_TYPE (datum_type);
2347 enum tree_code code = TREE_CODE (underlying_type);
2348 if (code == RECORD_TYPE || code == UNION_TYPE)
2349 return true;
2350 else
2351 return false;
2352}
2353
c2255bc4
AH
2354/* Make an expression to refer to the COMPONENT field of structure or
2355 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
6ffd47b7
DM
2356 location of the COMPONENT_REF. COMPONENT_LOC is the location
2357 of COMPONENT. */
400fbf9f
JW
2358
2359tree
6ffd47b7
DM
2360build_component_ref (location_t loc, tree datum, tree component,
2361 location_t component_loc)
400fbf9f 2362{
b3694847
SS
2363 tree type = TREE_TYPE (datum);
2364 enum tree_code code = TREE_CODE (type);
2365 tree field = NULL;
2366 tree ref;
1e57bf47 2367 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2368
7a3ea201
RH
2369 if (!objc_is_public (datum, component))
2370 return error_mark_node;
2371
46a88c12 2372 /* Detect Objective-C property syntax object.property. */
668ea4b1 2373 if (c_dialect_objc ()
46a88c12 2374 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2375 return ref;
2376
400fbf9f
JW
2377 /* See if there is a field or component with name COMPONENT. */
2378
2379 if (code == RECORD_TYPE || code == UNION_TYPE)
2380 {
d0f062fb 2381 if (!COMPLETE_TYPE_P (type))
400fbf9f 2382 {
4f2e1536 2383 c_incomplete_type_error (loc, NULL_TREE, type);
400fbf9f
JW
2384 return error_mark_node;
2385 }
2386
0fb96aa4 2387 field = lookup_field (type, component);
400fbf9f
JW
2388
2389 if (!field)
2390 {
277fe616
DM
2391 tree guessed_id = lookup_field_fuzzy (type, component);
2392 if (guessed_id)
6ffd47b7
DM
2393 {
2394 /* Attempt to provide a fixit replacement hint, if
2395 we have a valid range for the component. */
2396 location_t reported_loc
2397 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
264757fb 2398 gcc_rich_location rich_loc (reported_loc);
6ffd47b7 2399 if (component_loc != UNKNOWN_LOCATION)
264757fb 2400 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
6ffd47b7
DM
2401 error_at_rich_loc
2402 (&rich_loc,
2403 "%qT has no member named %qE; did you mean %qE?",
2404 type, component, guessed_id);
2405 }
277fe616
DM
2406 else
2407 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2408 return error_mark_node;
2409 }
400fbf9f 2410
fda5652f
MP
2411 /* Accessing elements of atomic structures or unions is undefined
2412 behavior (C11 6.5.2.3#5). */
2413 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2414 {
2415 if (code == RECORD_TYPE)
2416 warning_at (loc, 0, "accessing a member %qE of an atomic "
2417 "structure %qE", component, datum);
2418 else
2419 warning_at (loc, 0, "accessing a member %qE of an atomic "
2420 "union %qE", component, datum);
2421 }
2422
e9b2c823
NB
2423 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2424 This might be better solved in future the way the C++ front
2425 end does it - by giving the anonymous entities each a
2426 separate name and type, and then have build_component_ref
2427 recursively call itself. We can't do that here. */
46ea50cb 2428 do
19d76e60 2429 {
e9b2c823 2430 tree subdatum = TREE_VALUE (field);
efed193e
JM
2431 int quals;
2432 tree subtype;
1e57bf47 2433 bool use_datum_quals;
e9b2c823
NB
2434
2435 if (TREE_TYPE (subdatum) == error_mark_node)
2436 return error_mark_node;
2437
1e57bf47
JM
2438 /* If this is an rvalue, it does not have qualifiers in C
2439 standard terms and we must avoid propagating such
2440 qualifiers down to a non-lvalue array that is then
2441 converted to a pointer. */
2442 use_datum_quals = (datum_lvalue
2443 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2444
efed193e 2445 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2446 if (use_datum_quals)
2447 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2448 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2449
2450 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2451 NULL_TREE);
c2255bc4 2452 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2453 if (TREE_READONLY (subdatum)
2454 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2455 TREE_READONLY (ref) = 1;
1e57bf47
JM
2456 if (TREE_THIS_VOLATILE (subdatum)
2457 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2458 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2459
2460 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2461 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2462
19d76e60 2463 datum = ref;
46ea50cb
RS
2464
2465 field = TREE_CHAIN (field);
19d76e60 2466 }
46ea50cb 2467 while (field);
19d76e60 2468
400fbf9f
JW
2469 return ref;
2470 }
a87a86e1
DM
2471 else if (should_suggest_deref_p (type))
2472 {
2473 /* Special-case the error message for "ptr.field" for the case
2474 where the user has confused "." vs "->". */
2475 rich_location richloc (line_table, loc);
2476 /* "loc" should be the "." token. */
f9087798 2477 richloc.add_fixit_replace ("->");
a87a86e1
DM
2478 error_at_rich_loc (&richloc,
2479 "%qE is a pointer; did you mean to use %<->%>?",
2480 datum);
2481 return error_mark_node;
2482 }
400fbf9f 2483 else if (code != ERROR_MARK)
c2255bc4
AH
2484 error_at (loc,
2485 "request for member %qE in something not a structure or union",
2486 component);
400fbf9f
JW
2487
2488 return error_mark_node;
2489}
2490\f
2491/* Given an expression PTR for a pointer, return an expression
2492 for the value pointed to.
6a3799eb
AH
2493 ERRORSTRING is the name of the operator to appear in error messages.
2494
2495 LOC is the location to use for the generated tree. */
400fbf9f
JW
2496
2497tree
dd865ef6 2498build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2499{
b3694847
SS
2500 tree pointer = default_conversion (ptr);
2501 tree type = TREE_TYPE (pointer);
6a3799eb 2502 tree ref;
400fbf9f
JW
2503
2504 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2505 {
1043771b 2506 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2507 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2508 {
2509 /* If a warning is issued, mark it to avoid duplicates from
2510 the backend. This only needs to be done at
2511 warn_strict_aliasing > 2. */
2512 if (warn_strict_aliasing > 2)
2513 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2514 type, TREE_OPERAND (pointer, 0)))
2515 TREE_NO_WARNING (pointer) = 1;
2516 }
2517
870cc33b 2518 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2519 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2520 == TREE_TYPE (type)))
6a3799eb
AH
2521 {
2522 ref = TREE_OPERAND (pointer, 0);
2523 protected_set_expr_location (ref, loc);
2524 return ref;
2525 }
870cc33b
RS
2526 else
2527 {
2528 tree t = TREE_TYPE (type);
46df2823 2529
984dfd8c 2530 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2531
baae9b65 2532 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2533 {
d9b7be2e
MP
2534 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2535 {
2536 error_at (loc, "dereferencing pointer to incomplete type "
2537 "%qT", t);
2538 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2539 }
870cc33b
RS
2540 return error_mark_node;
2541 }
7d882b83 2542 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2543 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2544
2545 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2546 so that we get the proper error message if the result is used
2547 to assign to. Also, &* is supposed to be a no-op.
2548 And ANSI C seems to specify that the type of the result
2549 should be the const type. */
2550 /* A de-reference of a pointer to const is not a const. It is valid
2551 to change it via some other pointer. */
2552 TREE_READONLY (ref) = TYPE_READONLY (t);
2553 TREE_SIDE_EFFECTS (ref)
271bd540 2554 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2555 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2556 protected_set_expr_location (ref, loc);
870cc33b
RS
2557 return ref;
2558 }
2559 }
400fbf9f 2560 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2561 invalid_indirection_error (loc, type, errstring);
2562
400fbf9f
JW
2563 return error_mark_node;
2564}
2565
2566/* This handles expressions of the form "a[i]", which denotes
2567 an array reference.
2568
2569 This is logically equivalent in C to *(a+i), but we may do it differently.
2570 If A is a variable or a member, we generate a primitive ARRAY_REF.
2571 This avoids forcing the array out of registers, and can work on
2572 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2573 by functions).
2574
30cd1c5d
AS
2575 For vector types, allow vector[i] but not i[vector], and create
2576 *(((type*)&vectortype) + i) for the expression.
2577
6a3799eb 2578 LOC is the location to use for the returned expression. */
400fbf9f
JW
2579
2580tree
c2255bc4 2581build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2582{
6a3799eb 2583 tree ret;
a4ab7973 2584 bool swapped = false;
400fbf9f
JW
2585 if (TREE_TYPE (array) == error_mark_node
2586 || TREE_TYPE (index) == error_mark_node)
2587 return error_mark_node;
2588
b72271b9 2589 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2590 {
2591 size_t rank = 0;
2592 if (!find_rank (loc, index, index, true, &rank))
2593 return error_mark_node;
2594 if (rank > 1)
2595 {
2596 error_at (loc, "rank of the array's index is greater than 1");
2597 return error_mark_node;
2598 }
2599 }
a4ab7973 2600 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2601 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2602 /* Allow vector[index] but not index[vector]. */
31521951 2603 && !VECTOR_TYPE_P (TREE_TYPE (array)))
400fbf9f 2604 {
a4ab7973
JM
2605 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2606 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2607 {
f90e8e2e 2608 error_at (loc,
30cd1c5d
AS
2609 "subscripted value is neither array nor pointer nor vector");
2610
fdeefd49
RS
2611 return error_mark_node;
2612 }
fab27f52 2613 std::swap (array, index);
a4ab7973
JM
2614 swapped = true;
2615 }
2616
2617 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2618 {
a63068b6 2619 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2620 return error_mark_node;
2621 }
2622
2623 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2624 {
a63068b6 2625 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2626 return error_mark_node;
2627 }
2628
ff6b6641
GDR
2629 /* ??? Existing practice has been to warn only when the char
2630 index is syntactically the index, not for char[array]. */
2631 if (!swapped)
5bd012f8 2632 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2633
2634 /* Apply default promotions *after* noticing character types. */
2635 index = default_conversion (index);
f406ae1f
MP
2636 if (index == error_mark_node)
2637 return error_mark_node;
a4ab7973
JM
2638
2639 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2640
f17a223d
RB
2641 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2642 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
a4ab7973
JM
2643
2644 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2645 {
e4d35515 2646 tree rval, type;
fdeefd49 2647
400fbf9f
JW
2648 /* An array that is indexed by a non-constant
2649 cannot be stored in a register; we must be able to do
2650 address arithmetic on its address.
2651 Likewise an array of elements of variable size. */
2652 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2653 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2654 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2655 {
dffd7eb6 2656 if (!c_mark_addressable (array))
400fbf9f
JW
2657 return error_mark_node;
2658 }
e6d52559
JW
2659 /* An array that is indexed by a constant value which is not within
2660 the array bounds cannot be stored in a register either; because we
2661 would get a crash in store_bit_field/extract_bit_field when trying
2662 to access a non-existent part of the register. */
2663 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2664 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2665 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2666 {
dffd7eb6 2667 if (!c_mark_addressable (array))
e6d52559
JW
2668 return error_mark_node;
2669 }
400fbf9f 2670
f17a223d
RB
2671 if ((pedantic || warn_c90_c99_compat)
2672 && ! was_vector)
400fbf9f
JW
2673 {
2674 tree foo = array;
2675 while (TREE_CODE (foo) == COMPONENT_REF)
2676 foo = TREE_OPERAND (foo, 0);
0ae9bd27 2677 if (VAR_P (foo) && C_DECL_REGISTER (foo))
c1771a20 2678 pedwarn (loc, OPT_Wpedantic,
fcf73884 2679 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2680 else if (!lvalue_p (foo))
2681 pedwarn_c90 (loc, OPT_Wpedantic,
2682 "ISO C90 forbids subscripting non-lvalue "
2683 "array");
400fbf9f
JW
2684 }
2685
46df2823 2686 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2687 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2688 /* Array ref is const/volatile if the array elements are
c22cacf3 2689 or if the array is. */
400fbf9f
JW
2690 TREE_READONLY (rval)
2691 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2692 | TREE_READONLY (array));
2693 TREE_SIDE_EFFECTS (rval)
2694 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2695 | TREE_SIDE_EFFECTS (array));
2696 TREE_THIS_VOLATILE (rval)
2697 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2698 /* This was added by rms on 16 Nov 91.
2f6e4e97 2699 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2700 in an inline function.
2701 Hope it doesn't break something else. */
2702 | TREE_THIS_VOLATILE (array));
4f2e1536 2703 ret = require_complete_type (loc, rval);
6a3799eb 2704 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2705 if (non_lvalue)
2706 ret = non_lvalue_loc (loc, ret);
6a3799eb 2707 return ret;
400fbf9f 2708 }
a4ab7973
JM
2709 else
2710 {
2711 tree ar = default_conversion (array);
400fbf9f 2712
a4ab7973
JM
2713 if (ar == error_mark_node)
2714 return ar;
400fbf9f 2715
a4ab7973
JM
2716 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2717 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2718
aa7da51a
JJ
2719 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2720 index, 0),
2721 RO_ARRAY_INDEXING);
2722 if (non_lvalue)
2723 ret = non_lvalue_loc (loc, ret);
2724 return ret;
a4ab7973 2725 }
400fbf9f
JW
2726}
2727\f
7e585d16 2728/* Build an external reference to identifier ID. FUN indicates
766beb40 2729 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2730 location of the identifier. This sets *TYPE to the type of the
2731 identifier, which is not the same as the type of the returned value
2732 for CONST_DECLs defined as enum constants. If the type of the
2733 identifier is not available, *TYPE is set to NULL. */
7e585d16 2734tree
c2255bc4 2735build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2736{
2737 tree ref;
2738 tree decl = lookup_name (id);
16b34ad6
ZL
2739
2740 /* In Objective-C, an instance variable (ivar) may be preferred to
2741 whatever lookup_name() found. */
2742 decl = objc_lookup_ivar (decl, id);
7e585d16 2743
6866c6e8 2744 *type = NULL;
339a28b9 2745 if (decl && decl != error_mark_node)
6866c6e8
ILT
2746 {
2747 ref = decl;
2748 *type = TREE_TYPE (ref);
2749 }
339a28b9
ZW
2750 else if (fun)
2751 /* Implicit function declaration. */
c2255bc4 2752 ref = implicitly_declare (loc, id);
339a28b9
ZW
2753 else if (decl == error_mark_node)
2754 /* Don't complain about something that's already been
2755 complained about. */
2756 return error_mark_node;
2757 else
2758 {
c2255bc4 2759 undeclared_variable (loc, id);
339a28b9
ZW
2760 return error_mark_node;
2761 }
7e585d16
ZW
2762
2763 if (TREE_TYPE (ref) == error_mark_node)
2764 return error_mark_node;
2765
339a28b9 2766 if (TREE_DEPRECATED (ref))
9b86d6bb 2767 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2768
ad960f56 2769 /* Recursive call does not count as usage. */
b8698a0f 2770 if (ref != current_function_decl)
ad960f56 2771 {
ad960f56
MLI
2772 TREE_USED (ref) = 1;
2773 }
7e585d16 2774
bc4b653b
JM
2775 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2776 {
2777 if (!in_sizeof && !in_typeof)
2778 C_DECL_USED (ref) = 1;
2779 else if (DECL_INITIAL (ref) == 0
2780 && DECL_EXTERNAL (ref)
2781 && !TREE_PUBLIC (ref))
2782 record_maybe_used_decl (ref);
2783 }
2784
7e585d16
ZW
2785 if (TREE_CODE (ref) == CONST_DECL)
2786 {
6193b8b7 2787 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2788
2789 if (warn_cxx_compat
2790 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2791 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2792 {
2793 warning_at (loc, OPT_Wc___compat,
2794 ("enum constant defined in struct or union "
2795 "is not visible in C++"));
2796 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2797 }
2798
7e585d16
ZW
2799 ref = DECL_INITIAL (ref);
2800 TREE_CONSTANT (ref) = 1;
2801 }
6a29edea 2802 else if (current_function_decl != 0
4b1e44be 2803 && !DECL_FILE_SCOPE_P (current_function_decl)
21b634ae
MP
2804 && (VAR_OR_FUNCTION_DECL_P (ref)
2805 || TREE_CODE (ref) == PARM_DECL))
6a29edea
EB
2806 {
2807 tree context = decl_function_context (ref);
2f6e4e97 2808
6a29edea
EB
2809 if (context != 0 && context != current_function_decl)
2810 DECL_NONLOCAL (ref) = 1;
2811 }
71113fcd
GK
2812 /* C99 6.7.4p3: An inline definition of a function with external
2813 linkage ... shall not contain a reference to an identifier with
2814 internal linkage. */
2815 else if (current_function_decl != 0
2816 && DECL_DECLARED_INLINE_P (current_function_decl)
2817 && DECL_EXTERNAL (current_function_decl)
2818 && VAR_OR_FUNCTION_DECL_P (ref)
0ae9bd27 2819 && (!VAR_P (ref) || TREE_STATIC (ref))
1033ffa8
JJ
2820 && ! TREE_PUBLIC (ref)
2821 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2822 record_inline_static (loc, current_function_decl, ref,
2823 csi_internal);
7e585d16
ZW
2824
2825 return ref;
2826}
2827
bc4b653b
JM
2828/* Record details of decls possibly used inside sizeof or typeof. */
2829struct maybe_used_decl
2830{
2831 /* The decl. */
2832 tree decl;
2833 /* The level seen at (in_sizeof + in_typeof). */
2834 int level;
2835 /* The next one at this level or above, or NULL. */
2836 struct maybe_used_decl *next;
2837};
2838
2839static struct maybe_used_decl *maybe_used_decls;
2840
2841/* Record that DECL, an undefined static function reference seen
2842 inside sizeof or typeof, might be used if the operand of sizeof is
2843 a VLA type or the operand of typeof is a variably modified
2844 type. */
2845
4e2fb7de 2846static void
bc4b653b
JM
2847record_maybe_used_decl (tree decl)
2848{
2849 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2850 t->decl = decl;
2851 t->level = in_sizeof + in_typeof;
2852 t->next = maybe_used_decls;
2853 maybe_used_decls = t;
2854}
2855
2856/* Pop the stack of decls possibly used inside sizeof or typeof. If
2857 USED is false, just discard them. If it is true, mark them used
2858 (if no longer inside sizeof or typeof) or move them to the next
2859 level up (if still inside sizeof or typeof). */
2860
2861void
2862pop_maybe_used (bool used)
2863{
2864 struct maybe_used_decl *p = maybe_used_decls;
2865 int cur_level = in_sizeof + in_typeof;
2866 while (p && p->level > cur_level)
2867 {
2868 if (used)
2869 {
2870 if (cur_level == 0)
2871 C_DECL_USED (p->decl) = 1;
2872 else
2873 p->level = cur_level;
2874 }
2875 p = p->next;
2876 }
2877 if (!used || cur_level == 0)
2878 maybe_used_decls = p;
2879}
2880
2881/* Return the result of sizeof applied to EXPR. */
2882
2883struct c_expr
c2255bc4 2884c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2885{
2886 struct c_expr ret;
ad97f4be
JM
2887 if (expr.value == error_mark_node)
2888 {
2889 ret.value = error_mark_node;
2890 ret.original_code = ERROR_MARK;
6866c6e8 2891 ret.original_type = NULL;
ad97f4be
JM
2892 pop_maybe_used (false);
2893 }
2894 else
2895 {
928c19bb 2896 bool expr_const_operands = true;
773ec47f
MP
2897
2898 if (TREE_CODE (expr.value) == PARM_DECL
2899 && C_ARRAY_PARAMETER (expr.value))
2900 {
2901 if (warning_at (loc, OPT_Wsizeof_array_argument,
2902 "%<sizeof%> on array function parameter %qE will "
2903 "return size of %qT", expr.value,
2904 expr.original_type))
2905 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2906 }
928c19bb
JM
2907 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2908 &expr_const_operands);
c2255bc4 2909 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2910 c_last_sizeof_arg = expr.value;
2911 ret.original_code = SIZEOF_EXPR;
6866c6e8 2912 ret.original_type = NULL;
928c19bb 2913 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2914 {
2915 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2916 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2917 folded_expr, ret.value);
2918 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2919 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2920 }
928c19bb 2921 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2922 }
bc4b653b
JM
2923 return ret;
2924}
2925
2926/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2927 name passed to sizeof (rather than the type itself). LOC is the
2928 location of the original expression. */
bc4b653b
JM
2929
2930struct c_expr
c2255bc4 2931c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2932{
2933 tree type;
2934 struct c_expr ret;
928c19bb
JM
2935 tree type_expr = NULL_TREE;
2936 bool type_expr_const = true;
2937 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2938 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2939 c_last_sizeof_arg = type;
2940 ret.original_code = SIZEOF_EXPR;
6866c6e8 2941 ret.original_type = NULL;
24070fcb
JM
2942 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2943 && c_vla_type_p (type))
928c19bb 2944 {
24070fcb
JM
2945 /* If the type is a [*] array, it is a VLA but is represented as
2946 having a size of zero. In such a case we must ensure that
2947 the result of sizeof does not get folded to a constant by
2948 c_fully_fold, because if the size is evaluated the result is
2949 not constant and so constraints on zero or negative size
2950 arrays must not be applied when this sizeof call is inside
2951 another array declarator. */
2952 if (!type_expr)
2953 type_expr = integer_zero_node;
928c19bb
JM
2954 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2955 type_expr, ret.value);
2956 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2957 }
16464cc1
VR
2958 pop_maybe_used (type != error_mark_node
2959 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2960 return ret;
2961}
2962
400fbf9f 2963/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2964 The function call is at LOC.
400fbf9f
JW
2965 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2966 TREE_VALUE of each node is a parameter-expression.
2967 FUNCTION's data type may be a function type or a pointer-to-function. */
2968
2969tree
c2255bc4 2970build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2971{
9771b263 2972 vec<tree, va_gc> *v;
bbbbb16a
ILT
2973 tree ret;
2974
9771b263 2975 vec_alloc (v, list_length (params));
bbbbb16a 2976 for (; params; params = TREE_CHAIN (params))
9771b263 2977 v->quick_push (TREE_VALUE (params));
8edbfaa6 2978 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2979 vec_free (v);
bbbbb16a
ILT
2980 return ret;
2981}
2982
ae52741c
MLI
2983/* Give a note about the location of the declaration of DECL. */
2984
c7b70a3c
MP
2985static void
2986inform_declaration (tree decl)
ae52741c 2987{
c7b70a3c 2988 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
ae52741c
MLI
2989 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2990}
2991
bbbbb16a
ILT
2992/* Build a function call to function FUNCTION with parameters PARAMS.
2993 ORIGTYPES, if not NULL, is a vector of types; each element is
2994 either NULL or the original type of the corresponding element in
2995 PARAMS. The original type may differ from TREE_TYPE of the
2996 parameter for enums. FUNCTION's data type may be a function type
2997 or pointer-to-function. This function changes the elements of
2998 PARAMS. */
2999
3000tree
81e5eca8
MP
3001build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3002 tree function, vec<tree, va_gc> *params,
9771b263 3003 vec<tree, va_gc> *origtypes)
400fbf9f 3004{
b3694847 3005 tree fntype, fundecl = 0;
4977bab6 3006 tree name = NULL_TREE, result;
c96f4f73 3007 tree tem;
94a0dd7b
SL
3008 int nargs;
3009 tree *argarray;
b8698a0f 3010
400fbf9f 3011
fc76e425 3012 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 3013 STRIP_TYPE_NOPS (function);
400fbf9f
JW
3014
3015 /* Convert anything with function type to a pointer-to-function. */
3016 if (TREE_CODE (function) == FUNCTION_DECL)
3017 {
3018 name = DECL_NAME (function);
0a35513e
AH
3019
3020 if (flag_tm)
3021 tm_malloc_replacement (function);
a5eadacc 3022 fundecl = function;
86951993
AM
3023 /* Atomic functions have type checking/casting already done. They are
3024 often rewritten and don't match the original parameter list. */
3025 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3026 origtypes = NULL;
36536d79 3027
b72271b9 3028 if (flag_cilkplus
36536d79
BI
3029 && is_cilkplus_reduce_builtin (function))
3030 origtypes = NULL;
400fbf9f 3031 }
f2a71bbc 3032 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 3033 function = function_to_pointer_conversion (loc, function);
400fbf9f 3034
6e955430
ZL
3035 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3036 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
3037 if (params && !params->is_empty ())
3038 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 3039
928c19bb
JM
3040 function = c_fully_fold (function, false, NULL);
3041
400fbf9f
JW
3042 fntype = TREE_TYPE (function);
3043
3044 if (TREE_CODE (fntype) == ERROR_MARK)
3045 return error_mark_node;
3046
3047 if (!(TREE_CODE (fntype) == POINTER_TYPE
3048 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3049 {
ae52741c
MLI
3050 if (!flag_diagnostics_show_caret)
3051 error_at (loc,
3052 "called object %qE is not a function or function pointer",
3053 function);
3054 else if (DECL_P (function))
3055 {
3056 error_at (loc,
3057 "called object %qD is not a function or function pointer",
3058 function);
3059 inform_declaration (function);
3060 }
3061 else
3062 error_at (loc,
3063 "called object is not a function or function pointer");
400fbf9f
JW
3064 return error_mark_node;
3065 }
3066
5ce89b2e
JM
3067 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3068 current_function_returns_abnormally = 1;
3069
400fbf9f
JW
3070 /* fntype now gets the type of function pointed to. */
3071 fntype = TREE_TYPE (fntype);
3072
ab4194da
JM
3073 /* Convert the parameters to the types declared in the
3074 function prototype, or apply default promotions. */
3075
81e5eca8
MP
3076 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3077 origtypes, function, fundecl);
ab4194da
JM
3078 if (nargs < 0)
3079 return error_mark_node;
3080
c96f4f73 3081 /* Check that the function is called through a compatible prototype.
fa337f3a 3082 If it is not, warn. */
1043771b 3083 if (CONVERT_EXPR_P (function)
c96f4f73
EB
3084 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3085 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 3086 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
3087 {
3088 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
3089
3090 /* This situation leads to run-time undefined behavior. We can't,
3091 therefore, simply error unless we can prove that all possible
3092 executions of the program must execute the code. */
fa337f3a 3093 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 3094
fa337f3a
RB
3095 if (VOID_TYPE_P (return_type)
3096 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3097 pedwarn (loc, 0,
3098 "function with qualified void return type called");
3099 }
c96f4f73 3100
9771b263 3101 argarray = vec_safe_address (params);
bbbbb16a 3102
83322951
RG
3103 /* Check that arguments to builtin functions match the expectations. */
3104 if (fundecl
3105 && DECL_BUILT_IN (fundecl)
3106 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
79ce98bc
MP
3107 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3108 argarray))
83322951 3109 return error_mark_node;
400fbf9f 3110
83322951 3111 /* Check that the arguments to the function are valid. */
3342fd71 3112 check_function_arguments (loc, fntype, nargs, argarray);
400fbf9f 3113
928c19bb
JM
3114 if (name != NULL_TREE
3115 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 3116 {
928c19bb 3117 if (require_constant_value)
b8698a0f 3118 result =
db3927fb
AH
3119 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3120 function, nargs, argarray);
928c19bb 3121 else
db3927fb
AH
3122 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3123 function, nargs, argarray);
928c19bb
JM
3124 if (TREE_CODE (result) == NOP_EXPR
3125 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3126 STRIP_TYPE_NOPS (result);
bf730f15
RS
3127 }
3128 else
db3927fb
AH
3129 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3130 function, nargs, argarray);
b0b3afb2 3131
ab4c578f
MP
3132 /* In this improbable scenario, a nested function returns a VM type.
3133 Create a TARGET_EXPR so that the call always has a LHS, much as
3134 what the C++ FE does for functions returning non-PODs. */
3135 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3136 {
3137 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3138 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3139 NULL_TREE, NULL_TREE);
3140 }
3141
71653180 3142 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
3143 {
3144 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 3145 pedwarn (loc, 0,
3ce62965
JM
3146 "function with qualified void return type called");
3147 return result;
3148 }
4f2e1536 3149 return require_complete_type (loc, result);
400fbf9f 3150}
8edbfaa6
JJ
3151
3152/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3153
3154tree
3155c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3156 tree function, vec<tree, va_gc> *params,
3157 vec<tree, va_gc> *origtypes)
3158{
3159 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3160 STRIP_TYPE_NOPS (function);
3161
3162 /* Convert anything with function type to a pointer-to-function. */
3163 if (TREE_CODE (function) == FUNCTION_DECL)
3164 {
3165 /* Implement type-directed function overloading for builtins.
3166 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3167 handle all the type checking. The result is a complete expression
3168 that implements this function call. */
3169 tree tem = resolve_overloaded_builtin (loc, function, params);
3170 if (tem)
3171 return tem;
3172 }
3173 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3174}
400fbf9f 3175\f
bbbbb16a
ILT
3176/* Convert the argument expressions in the vector VALUES
3177 to the types in the list TYPELIST.
400fbf9f
JW
3178
3179 If TYPELIST is exhausted, or when an element has NULL as its type,
3180 perform the default conversions.
3181
bbbbb16a
ILT
3182 ORIGTYPES is the original types of the expressions in VALUES. This
3183 holds the type of enum values which have been converted to integral
3184 types. It may be NULL.
400fbf9f 3185
03dafa61
JM
3186 FUNCTION is a tree for the called function. It is used only for
3187 error messages, where it is formatted with %qE.
400fbf9f
JW
3188
3189 This is also where warnings about wrong number of args are generated.
3190
81e5eca8
MP
3191 ARG_LOC are locations of function arguments (if any).
3192
94a0dd7b 3193 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3194 than the length of VALUES in some error situations), or -1 on
3195 failure. */
94a0dd7b
SL
3196
3197static int
81e5eca8
MP
3198convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3199 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3200 tree function, tree fundecl)
400fbf9f 3201{
bbbbb16a
ILT
3202 tree typetail, val;
3203 unsigned int parmnum;
06302a02 3204 bool error_args = false;
b5d32c25 3205 const bool type_generic = fundecl
81e5eca8 3206 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3207 bool type_generic_remove_excess_precision = false;
44a845ca 3208 bool type_generic_overflow_p = false;
03dafa61 3209 tree selector;
03dafa61 3210
2ac2f164
JM
3211 /* Change pointer to function to the function itself for
3212 diagnostics. */
03dafa61
JM
3213 if (TREE_CODE (function) == ADDR_EXPR
3214 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3215 function = TREE_OPERAND (function, 0);
03dafa61
JM
3216
3217 /* Handle an ObjC selector specially for diagnostics. */
3218 selector = objc_message_selector ();
400fbf9f 3219
8ce94e44
JM
3220 /* For type-generic built-in functions, determine whether excess
3221 precision should be removed (classification) or not
3222 (comparison). */
3223 if (type_generic
3224 && DECL_BUILT_IN (fundecl)
3225 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3226 {
3227 switch (DECL_FUNCTION_CODE (fundecl))
3228 {
3229 case BUILT_IN_ISFINITE:
3230 case BUILT_IN_ISINF:
3231 case BUILT_IN_ISINF_SIGN:
3232 case BUILT_IN_ISNAN:
3233 case BUILT_IN_ISNORMAL:
3234 case BUILT_IN_FPCLASSIFY:
3235 type_generic_remove_excess_precision = true;
3236 break;
3237
44a845ca
MS
3238 case BUILT_IN_ADD_OVERFLOW_P:
3239 case BUILT_IN_SUB_OVERFLOW_P:
3240 case BUILT_IN_MUL_OVERFLOW_P:
3241 /* The last argument of these type-generic builtins
3242 should not be promoted. */
3243 type_generic_overflow_p = true;
3244 break;
3245
8ce94e44 3246 default:
8ce94e44
JM
3247 break;
3248 }
3249 }
b72271b9 3250 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3251 return vec_safe_length (values);
8ce94e44 3252
400fbf9f 3253 /* Scan the given expressions and types, producing individual
bbbbb16a 3254 converted arguments. */
400fbf9f 3255
bbbbb16a 3256 for (typetail = typelist, parmnum = 0;
9771b263 3257 values && values->iterate (parmnum, &val);
bbbbb16a 3258 ++parmnum)
400fbf9f 3259 {
b3694847 3260 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3261 tree valtype = TREE_TYPE (val);
03dafa61
JM
3262 tree rname = function;
3263 int argnum = parmnum + 1;
4d3e6fae 3264 const char *invalid_func_diag;
8ce94e44 3265 bool excess_precision = false;
928c19bb 3266 bool npc;
bbbbb16a 3267 tree parmval;
5c1bc275
MP
3268 /* Some __atomic_* builtins have additional hidden argument at
3269 position 0. */
3270 location_t ploc
3271 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3272 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3273 : input_location;
400fbf9f
JW
3274
3275 if (type == void_type_node)
3276 {
19dc6d01 3277 if (selector)
68fca595 3278 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3279 else
68fca595 3280 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3281 inform_declaration (fundecl);
d38f7dce 3282 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3283 }
3284
03dafa61
JM
3285 if (selector && argnum > 2)
3286 {
3287 rname = selector;
3288 argnum -= 2;
3289 }
3290
928c19bb 3291 npc = null_pointer_constant_p (val);
8ce94e44
JM
3292
3293 /* If there is excess precision and a prototype, convert once to
3294 the required type rather than converting via the semantic
3295 type. Likewise without a prototype a float value represented
3296 as long double should be converted once to double. But for
3297 type-generic classification functions excess precision must
3298 be removed here. */
3299 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3300 && (type || !type_generic || !type_generic_remove_excess_precision))
3301 {
3302 val = TREE_OPERAND (val, 0);
3303 excess_precision = true;
3304 }
928c19bb 3305 val = c_fully_fold (val, false, NULL);
ed248cf7 3306 STRIP_TYPE_NOPS (val);
400fbf9f 3307
4f2e1536 3308 val = require_complete_type (ploc, val);
400fbf9f 3309
c65699ef
JM
3310 /* Some floating-point arguments must be promoted to double when
3311 no type is specified by a prototype. This applies to
3312 arguments of type float, and to architecture-specific types
3313 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3314 bool promote_float_arg = false;
3315 if (type == NULL_TREE
3316 && TREE_CODE (valtype) == REAL_TYPE
3317 && (TYPE_PRECISION (valtype)
3318 <= TYPE_PRECISION (double_type_node))
3319 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3320 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3321 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3322 {
3323 /* Promote this argument, unless it has a _FloatN or
3324 _FloatNx type. */
3325 promote_float_arg = true;
3326 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3327 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3328 {
3329 promote_float_arg = false;
3330 break;
3331 }
3332 }
3333
400fbf9f
JW
3334 if (type != 0)
3335 {
3336 /* Formal parm type is specified by a function prototype. */
400fbf9f 3337
20913689 3338 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3339 {
5c1bc275
MP
3340 error_at (ploc, "type of formal parameter %d is incomplete",
3341 parmnum + 1);
400fbf9f
JW
3342 parmval = val;
3343 }
3344 else
3345 {
bbbbb16a
ILT
3346 tree origtype;
3347
d45cf215
RS
3348 /* Optionally warn about conversions that
3349 differ from the default conversions. */
05170031 3350 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3351 {
e3a64162 3352 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3353
aae43c5f 3354 if (INTEGRAL_TYPE_P (type)
8ce94e44 3355 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3356 warning_at (ploc, OPT_Wtraditional_conversion,
3357 "passing argument %d of %qE as integer rather "
3358 "than floating due to prototype",
3359 argnum, rname);
03829ad2 3360 if (INTEGRAL_TYPE_P (type)
8ce94e44 3361 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3362 warning_at (ploc, OPT_Wtraditional_conversion,
3363 "passing argument %d of %qE as integer rather "
3364 "than complex due to prototype",
3365 argnum, rname);
aae43c5f 3366 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3367 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3368 warning_at (ploc, OPT_Wtraditional_conversion,
3369 "passing argument %d of %qE as complex rather "
3370 "than floating due to prototype",
3371 argnum, rname);
400fbf9f 3372 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3373 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3374 warning_at (ploc, OPT_Wtraditional_conversion,
3375 "passing argument %d of %qE as floating rather "
3376 "than integer due to prototype",
3377 argnum, rname);
03829ad2 3378 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3379 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3380 warning_at (ploc, OPT_Wtraditional_conversion,
3381 "passing argument %d of %qE as complex rather "
3382 "than integer due to prototype",
3383 argnum, rname);
aae43c5f 3384 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3385 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3386 warning_at (ploc, OPT_Wtraditional_conversion,
3387 "passing argument %d of %qE as floating rather "
3388 "than complex due to prototype",
3389 argnum, rname);
aae43c5f
RK
3390 /* ??? At some point, messages should be written about
3391 conversions between complex types, but that's too messy
3392 to do now. */
d45cf215 3393 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3394 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3395 {
3396 /* Warn if any argument is passed as `float',
047de90b 3397 since without a prototype it would be `double'. */
9a8ce21f
JG
3398 if (formal_prec == TYPE_PRECISION (float_type_node)
3399 && type != dfloat32_type_node)
5c1bc275
MP
3400 warning_at (ploc, 0,
3401 "passing argument %d of %qE as %<float%> "
3402 "rather than %<double%> due to prototype",
3403 argnum, rname);
9a8ce21f
JG
3404
3405 /* Warn if mismatch between argument and prototype
3406 for decimal float types. Warn of conversions with
3407 binary float types and of precision narrowing due to
3408 prototype. */
8ce94e44 3409 else if (type != valtype
9a8ce21f
JG
3410 && (type == dfloat32_type_node
3411 || type == dfloat64_type_node
c22cacf3 3412 || type == dfloat128_type_node
8ce94e44
JM
3413 || valtype == dfloat32_type_node
3414 || valtype == dfloat64_type_node
3415 || valtype == dfloat128_type_node)
c22cacf3 3416 && (formal_prec
8ce94e44 3417 <= TYPE_PRECISION (valtype)
9a8ce21f 3418 || (type == dfloat128_type_node
8ce94e44 3419 && (valtype
c22cacf3 3420 != dfloat64_type_node
8ce94e44 3421 && (valtype
9a8ce21f
JG
3422 != dfloat32_type_node)))
3423 || (type == dfloat64_type_node
8ce94e44 3424 && (valtype
9a8ce21f 3425 != dfloat32_type_node))))
5c1bc275
MP
3426 warning_at (ploc, 0,
3427 "passing argument %d of %qE as %qT "
3428 "rather than %qT due to prototype",
3429 argnum, rname, type, valtype);
9a8ce21f 3430
d45cf215 3431 }
3ed56f8a
KG
3432 /* Detect integer changing in width or signedness.
3433 These warnings are only activated with
05170031
MLI
3434 -Wtraditional-conversion, not with -Wtraditional. */
3435 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3436 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3437 {
d45cf215
RS
3438 tree would_have_been = default_conversion (val);
3439 tree type1 = TREE_TYPE (would_have_been);
3440
754a4d82 3441 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3442 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3443 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3444 /* No warning if function asks for enum
3445 and the actual arg is that enum type. */
3446 ;
3447 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3448 warning_at (ploc, OPT_Wtraditional_conversion,
3449 "passing argument %d of %qE "
3450 "with different width due to prototype",
3451 argnum, rname);
8df83eae 3452 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3453 ;
800cd3b9
RS
3454 /* Don't complain if the formal parameter type
3455 is an enum, because we can't tell now whether
3456 the value was an enum--even the same enum. */
3457 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3458 ;
400fbf9f
JW
3459 else if (TREE_CODE (val) == INTEGER_CST
3460 && int_fits_type_p (val, type))
3461 /* Change in signedness doesn't matter
3462 if a constant value is unaffected. */
3463 ;
ce9895ae
RS
3464 /* If the value is extended from a narrower
3465 unsigned type, it doesn't matter whether we
3466 pass it as signed or unsigned; the value
3467 certainly is the same either way. */
8ce94e44
JM
3468 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3469 && TYPE_UNSIGNED (valtype))
ce9895ae 3470 ;
8df83eae 3471 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3472 warning_at (ploc, OPT_Wtraditional_conversion,
3473 "passing argument %d of %qE "
3474 "as unsigned due to prototype",
3475 argnum, rname);
3ed56f8a 3476 else
5c1bc275
MP
3477 warning_at (ploc, OPT_Wtraditional_conversion,
3478 "passing argument %d of %qE "
3479 "as signed due to prototype",
3480 argnum, rname);
400fbf9f
JW
3481 }
3482 }
3483
8ce94e44
JM
3484 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3485 sake of better warnings from convert_and_check. */
3486 if (excess_precision)
3487 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3488 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3489 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3490 val, origtype, ic_argpass,
3491 npc, fundecl, function,
2ac2f164 3492 parmnum + 1);
2f6e4e97 3493
61f71b34 3494 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3495 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3496 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3497 parmval = default_conversion (parmval);
400fbf9f 3498 }
400fbf9f 3499 }
c65699ef 3500 else if (promote_float_arg)
b5d32c25
KG
3501 {
3502 if (type_generic)
bbbbb16a 3503 parmval = val;
b5d32c25 3504 else
0a0b3574
MM
3505 {
3506 /* Convert `float' to `double'. */
3507 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3508 warning_at (ploc, OPT_Wdouble_promotion,
3509 "implicit conversion from %qT to %qT when passing "
3510 "argument to function",
3511 valtype, double_type_node);
0a0b3574
MM
3512 parmval = convert (double_type_node, val);
3513 }
b5d32c25 3514 }
44a845ca
MS
3515 else if ((excess_precision && !type_generic)
3516 || (type_generic_overflow_p && parmnum == 2))
8ce94e44 3517 /* A "double" argument with excess precision being passed
44a845ca
MS
3518 without a prototype or in variable arguments.
3519 The last argument of __builtin_*_overflow_p should not be
3520 promoted. */
bbbbb16a 3521 parmval = convert (valtype, val);
c22cacf3
MS
3522 else if ((invalid_func_diag =
3523 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3524 {
3525 error (invalid_func_diag);
94a0dd7b 3526 return -1;
4d3e6fae 3527 }
1807ffc1
MS
3528 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3529 {
3530 return -1;
3531 }
400fbf9f
JW
3532 else
3533 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3534 parmval = default_conversion (val);
3535
9771b263 3536 (*values)[parmnum] = parmval;
06302a02
JJ
3537 if (parmval == error_mark_node)
3538 error_args = true;
400fbf9f
JW
3539
3540 if (typetail)
3541 typetail = TREE_CHAIN (typetail);
3542 }
3543
9771b263 3544 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3545
400fbf9f 3546 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3547 {
68fca595 3548 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3549 inform_declaration (fundecl);
3550 return -1;
3789b316 3551 }
400fbf9f 3552
06302a02 3553 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3554}
3555\f
43f6dfd3
RS
3556/* This is the entry point used by the parser to build unary operators
3557 in the input. CODE, a tree_code, specifies the unary operator, and
3558 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3559 CONVERT_EXPR for code.
3560
3561 LOC is the location to use for the tree generated.
3562*/
43f6dfd3
RS
3563
3564struct c_expr
c2255bc4 3565parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3566{
3567 struct c_expr result;
3568
100d537d 3569 result.original_code = code;
6866c6e8
ILT
3570 result.original_type = NULL;
3571
1807ffc1
MS
3572 if (reject_gcc_builtin (arg.value))
3573 {
3574 result.value = error_mark_node;
3575 }
3576 else
3577 {
e51fbec3 3578 result.value = build_unary_op (loc, code, arg.value, false);
1807ffc1 3579
e9892350
JG
3580 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3581 overflow_warning (loc, result.value);
1807ffc1 3582 }
59c0753d 3583
ebedc9a3
DM
3584 /* We are typically called when parsing a prefix token at LOC acting on
3585 ARG. Reflect this by updating the source range of the result to
3586 start at LOC and end at the end of ARG. */
3587 set_c_expr_source_range (&result,
3588 loc, arg.get_finish ());
3589
43f6dfd3
RS
3590 return result;
3591}
3592
3593/* This is the entry point used by the parser to build binary operators
3594 in the input. CODE, a tree_code, specifies the binary operator, and
3595 ARG1 and ARG2 are the operands. In addition to constructing the
3596 expression, we check for operands that were written with other binary
ba47d38d
AH
3597 operators in a way that is likely to confuse the user.
3598
3599 LOCATION is the location of the binary operator. */
edc7c4ec 3600
487a92fe 3601struct c_expr
ba47d38d
AH
3602parser_build_binary_op (location_t location, enum tree_code code,
3603 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3604{
487a92fe 3605 struct c_expr result;
400fbf9f 3606
487a92fe
JM
3607 enum tree_code code1 = arg1.original_code;
3608 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3609 tree type1 = (arg1.original_type
3610 ? arg1.original_type
3611 : TREE_TYPE (arg1.value));
3612 tree type2 = (arg2.original_type
3613 ? arg2.original_type
3614 : TREE_TYPE (arg2.value));
400fbf9f 3615
ba47d38d
AH
3616 result.value = build_binary_op (location, code,
3617 arg1.value, arg2.value, 1);
487a92fe 3618 result.original_code = code;
6866c6e8 3619 result.original_type = NULL;
58bf601b 3620
487a92fe 3621 if (TREE_CODE (result.value) == ERROR_MARK)
8a40fef3
DM
3622 {
3623 set_c_expr_source_range (&result,
3624 arg1.get_start (),
3625 arg2.get_finish ());
3626 return result;
3627 }
400fbf9f 3628
ba47d38d
AH
3629 if (location != UNKNOWN_LOCATION)
3630 protected_set_expr_location (result.value, location);
3631
ebedc9a3
DM
3632 set_c_expr_source_range (&result,
3633 arg1.get_start (),
3634 arg2.get_finish ());
3635
400fbf9f 3636 /* Check for cases such as x+y<<z which users are likely
487a92fe 3637 to misinterpret. */
400fbf9f 3638 if (warn_parentheses)
393e8e8b
MP
3639 warn_about_parentheses (location, code, code1, arg1.value, code2,
3640 arg2.value);
001af587 3641
ca409efd 3642 if (warn_logical_op)
393e8e8b 3643 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3644 code1, arg1.value, code2, arg2.value);
63a08740 3645
05b28fd6 3646 if (warn_tautological_compare)
433068cc
MP
3647 {
3648 tree lhs = arg1.value;
3649 tree rhs = arg2.value;
3650 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3651 {
3652 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3653 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3654 lhs = NULL_TREE;
3655 else
3656 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3657 }
3658 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3659 {
3660 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3661 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3662 rhs = NULL_TREE;
3663 else
3664 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3665 }
3666 if (lhs != NULL_TREE && rhs != NULL_TREE)
3667 warn_tautological_cmp (location, code, lhs, rhs);
3668 }
05b28fd6 3669
742938c9 3670 if (warn_logical_not_paren
7ccb1a11 3671 && TREE_CODE_CLASS (code) == tcc_comparison
47c2554f 3672 && code1 == TRUTH_NOT_EXPR
01177669
JJ
3673 && code2 != TRUTH_NOT_EXPR
3674 /* Avoid warning for !!x == y. */
3675 && (TREE_CODE (arg1.value) != NE_EXPR
3676 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3677 {
3678 /* Avoid warning for !b == y where b has _Bool type. */
3679 tree t = integer_zero_node;
3680 if (TREE_CODE (arg1.value) == EQ_EXPR
3681 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3682 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3683 {
3684 t = TREE_OPERAND (arg1.value, 0);
3685 do
3686 {
3687 if (TREE_TYPE (t) != integer_type_node)
3688 break;
3689 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3690 t = C_MAYBE_CONST_EXPR_EXPR (t);
3691 else if (CONVERT_EXPR_P (t))
3692 t = TREE_OPERAND (t, 0);
3693 else
3694 break;
3695 }
3696 while (1);
3697 }
3698 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
ebef225f 3699 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
01177669 3700 }
742938c9 3701
e994a705
RS
3702 /* Warn about comparisons against string literals, with the exception
3703 of testing for equality or inequality of a string literal with NULL. */
3704 if (code == EQ_EXPR || code == NE_EXPR)
3705 {
66756373
JJ
3706 if ((code1 == STRING_CST
3707 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3708 || (code2 == STRING_CST
3709 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
c2255bc4
AH
3710 warning_at (location, OPT_Waddress,
3711 "comparison with string literal results in unspecified behavior");
e994a705
RS
3712 }
3713 else if (TREE_CODE_CLASS (code) == tcc_comparison
3714 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3715 warning_at (location, OPT_Waddress,
3716 "comparison with string literal results in unspecified behavior");
e994a705 3717
b8698a0f
L
3718 if (TREE_OVERFLOW_P (result.value)
3719 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3720 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3721 overflow_warning (location, result.value);
400fbf9f 3722
6866c6e8
ILT
3723 /* Warn about comparisons of different enum types. */
3724 if (warn_enum_compare
3725 && TREE_CODE_CLASS (code) == tcc_comparison
3726 && TREE_CODE (type1) == ENUMERAL_TYPE
3727 && TREE_CODE (type2) == ENUMERAL_TYPE
3728 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3729 warning_at (location, OPT_Wenum_compare,
3730 "comparison between %qT and %qT",
3731 type1, type2);
3732
400fbf9f
JW
3733 return result;
3734}
3e4093b6 3735\f
3e4093b6
RS
3736/* Return a tree for the difference of pointers OP0 and OP1.
3737 The resulting tree has type int. */
293c9fdd 3738
3e4093b6 3739static tree
db3927fb 3740pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3741{
3e4093b6 3742 tree restype = ptrdiff_type_node;
36c5e70a 3743 tree result, inttype;
400fbf9f 3744
36c5e70a
BE
3745 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3746 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3747 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3748 tree orig_op1 = op1;
400fbf9f 3749
36c5e70a
BE
3750 /* If the operands point into different address spaces, we need to
3751 explicitly convert them to pointers into the common address space
3752 before we can subtract the numerical address values. */
3753 if (as0 != as1)
3754 {
3755 addr_space_t as_common;
3756 tree common_type;
3757
3758 /* Determine the common superset address space. This is guaranteed
3759 to exist because the caller verified that comp_target_types
3760 returned non-zero. */
3761 if (!addr_space_superset (as0, as1, &as_common))
3762 gcc_unreachable ();
3763
3764 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3765 op0 = convert (common_type, op0);
3766 op1 = convert (common_type, op1);
3767 }
3768
3769 /* Determine integer type to perform computations in. This will usually
3770 be the same as the result type (ptrdiff_t), but may need to be a wider
3771 type if pointers for the address space are wider than ptrdiff_t. */
3772 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3773 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3774 else
3775 inttype = restype;
3776
fcf73884 3777 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3778 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3779 "pointer of type %<void *%> used in subtraction");
3780 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3781 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3782 "pointer to a function used in subtraction");
400fbf9f 3783
3e4093b6
RS
3784 /* First do the subtraction as integers;
3785 then drop through to build the divide operator.
3786 Do not do default conversions on the minus operator
3787 in case restype is a short type. */
400fbf9f 3788
db3927fb 3789 op0 = build_binary_op (loc,
36c5e70a
BE
3790 MINUS_EXPR, convert (inttype, op0),
3791 convert (inttype, op1), 0);
3e4093b6
RS
3792 /* This generates an error if op1 is pointer to incomplete type. */
3793 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3794 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3795
3e4093b6 3796 op1 = c_size_in_bytes (target_type);
400fbf9f 3797
f04dda30
MP
3798 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3799 error_at (loc, "arithmetic on pointer to an empty aggregate");
3800
3e4093b6 3801 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3802 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3803 op0, convert (inttype, op1));
3804
3805 /* Convert to final result type if necessary. */
3806 return convert (restype, result);
3e4093b6
RS
3807}
3808\f
2fe0a208
MP
3809/* Expand atomic compound assignments into an appropriate sequence as
3810 specified by the C11 standard section 6.5.16.2.
3811
267bac10
JM
3812 _Atomic T1 E1
3813 T2 E2
3814 E1 op= E2
3815
3816 This sequence is used for all types for which these operations are
3817 supported.
3818
3819 In addition, built-in versions of the 'fe' prefixed routines may
3820 need to be invoked for floating point (real, complex or vector) when
3821 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3822
3823 T1 newval;
3824 T1 old;
3825 T1 *addr
3826 T2 val
3827 fenv_t fenv
3828
3829 addr = &E1;
3830 val = (E2);
3831 __atomic_load (addr, &old, SEQ_CST);
3832 feholdexcept (&fenv);
3833loop:
3834 newval = old op val;
3835 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3836 SEQ_CST))
3837 goto done;
3838 feclearexcept (FE_ALL_EXCEPT);
3839 goto loop:
3840done:
3841 feupdateenv (&fenv);
3842
2fe0a208
MP
3843 The compiler will issue the __atomic_fetch_* built-in when possible,
3844 otherwise it will generate the generic form of the atomic operations.
3845 This requires temp(s) and has their address taken. The atomic processing
3846 is smart enough to figure out when the size of an object can utilize
3847 a lock-free version, and convert the built-in call to the appropriate
3848 lock-free routine. The optimizers will then dispose of any temps that
3849 are no longer required, and lock-free implementations are utilized as
3850 long as there is target support for the required size.
267bac10
JM
3851
3852 If the operator is NOP_EXPR, then this is a simple assignment, and
3853 an __atomic_store is issued to perform the assignment rather than
2fe0a208 3854 the above loop. */
267bac10
JM
3855
3856/* Build an atomic assignment at LOC, expanding into the proper
3857 sequence to store LHS MODIFYCODE= RHS. Return a value representing
2fe0a208 3858 the result of the operation, unless RETURN_OLD_P, in which case
267bac10
JM
3859 return the old value of LHS (this is only for postincrement and
3860 postdecrement). */
2fe0a208 3861
267bac10
JM
3862static tree
3863build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3864 tree rhs, bool return_old_p)
3865{
3866 tree fndecl, func_call;
3867 vec<tree, va_gc> *params;
3868 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3869 tree old, old_addr;
3870 tree compound_stmt;
3871 tree stmt, goto_stmt;
3872 tree loop_label, loop_decl, done_label, done_decl;
3873
3874 tree lhs_type = TREE_TYPE (lhs);
e51fbec3 3875 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
267bac10
JM
3876 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3877 tree rhs_type = TREE_TYPE (rhs);
3878
3879 gcc_assert (TYPE_ATOMIC (lhs_type));
3880
3881 if (return_old_p)
3882 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3883
3884 /* Allocate enough vector items for a compare_exchange. */
3885 vec_alloc (params, 6);
3886
3887 /* Create a compound statement to hold the sequence of statements
3888 with a loop. */
3889 compound_stmt = c_begin_compound_stmt (false);
3890
3891 /* Fold the RHS if it hasn't already been folded. */
3892 if (modifycode != NOP_EXPR)
3893 rhs = c_fully_fold (rhs, false, NULL);
3894
3895 /* Remove the qualifiers for the rest of the expressions and create
3896 the VAL temp variable to hold the RHS. */
3897 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3898 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5c4abbb8 3899 val = create_tmp_var_raw (nonatomic_rhs_type);
267bac10 3900 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3901 TREE_NO_WARNING (val) = 1;
5c4abbb8
MP
3902 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3903 NULL_TREE);
267bac10
JM
3904 SET_EXPR_LOCATION (rhs, loc);
3905 add_stmt (rhs);
3906
3907 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3908 an atomic_store. */
3909 if (modifycode == NOP_EXPR)
3910 {
3911 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
e51fbec3 3912 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
267bac10
JM
3913 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3914 params->quick_push (lhs_addr);
3915 params->quick_push (rhs);
3916 params->quick_push (seq_cst);
8edbfaa6 3917 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3918 add_stmt (func_call);
3919
3920 /* Finish the compound statement. */
3921 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3922
3923 /* VAL is the value which was stored, return a COMPOUND_STMT of
3924 the statement and that value. */
3925 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3926 }
3927
2fe0a208
MP
3928 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3929 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3930 isn't applicable for such builtins. ??? Do we want to handle enums? */
3931 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3932 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3933 {
3934 built_in_function fncode;
3935 switch (modifycode)
3936 {
3937 case PLUS_EXPR:
3938 case POINTER_PLUS_EXPR:
3939 fncode = (return_old_p
3940 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3941 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3942 break;
3943 case MINUS_EXPR:
3944 fncode = (return_old_p
3945 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3946 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3947 break;
3948 case BIT_AND_EXPR:
3949 fncode = (return_old_p
3950 ? BUILT_IN_ATOMIC_FETCH_AND_N
3951 : BUILT_IN_ATOMIC_AND_FETCH_N);
3952 break;
3953 case BIT_IOR_EXPR:
3954 fncode = (return_old_p
3955 ? BUILT_IN_ATOMIC_FETCH_OR_N
3956 : BUILT_IN_ATOMIC_OR_FETCH_N);
3957 break;
3958 case BIT_XOR_EXPR:
3959 fncode = (return_old_p
3960 ? BUILT_IN_ATOMIC_FETCH_XOR_N
3961 : BUILT_IN_ATOMIC_XOR_FETCH_N);
3962 break;
3963 default:
3964 goto cas_loop;
3965 }
3966
3967 /* We can only use "_1" through "_16" variants of the atomic fetch
3968 built-ins. */
3969 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
3970 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
3971 goto cas_loop;
3972
3973 /* If this is a pointer type, we need to multiply by the size of
3974 the pointer target type. */
3975 if (POINTER_TYPE_P (lhs_type))
3976 {
3977 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
3978 /* ??? This would introduce -Wdiscarded-qualifiers
3979 warning: __atomic_fetch_* expect volatile void *
3980 type as the first argument. (Assignments between
3981 atomic and non-atomic objects are OK.) */
3982 || TYPE_RESTRICT (lhs_type))
3983 goto cas_loop;
3984 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
3985 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
3986 convert (ptrdiff_type_node, rhs),
3987 convert (ptrdiff_type_node, sz));
3988 }
3989
3990 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
3991 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
3992 fndecl = builtin_decl_explicit (fncode);
3993 params->quick_push (lhs_addr);
3994 params->quick_push (rhs);
3995 params->quick_push (seq_cst);
3996 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3997
3998 newval = create_tmp_var_raw (nonatomic_lhs_type);
3999 TREE_ADDRESSABLE (newval) = 1;
4000 TREE_NO_WARNING (newval) = 1;
4001 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4002 NULL_TREE, NULL_TREE);
4003 SET_EXPR_LOCATION (rhs, loc);
4004 add_stmt (rhs);
4005
4006 /* Finish the compound statement. */
4007 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4008
4009 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4010 the statement and that value. */
4011 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4012 }
4013
4014cas_loop:
267bac10 4015 /* Create the variables and labels required for the op= form. */
5c4abbb8 4016 old = create_tmp_var_raw (nonatomic_lhs_type);
e51fbec3 4017 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
cc28fc7f
MP
4018 TREE_ADDRESSABLE (old) = 1;
4019 TREE_NO_WARNING (old) = 1;
267bac10 4020
5c4abbb8 4021 newval = create_tmp_var_raw (nonatomic_lhs_type);
e51fbec3 4022 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
267bac10 4023 TREE_ADDRESSABLE (newval) = 1;
2994fb91 4024 TREE_NO_WARNING (newval) = 1;
267bac10
JM
4025
4026 loop_decl = create_artificial_label (loc);
4027 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4028
4029 done_decl = create_artificial_label (loc);
4030 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4031
4032 /* __atomic_load (addr, &old, SEQ_CST). */
4033 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4034 params->quick_push (lhs_addr);
4035 params->quick_push (old_addr);
4036 params->quick_push (seq_cst);
8edbfaa6 4037 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5c4abbb8
MP
4038 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4039 NULL_TREE);
4040 add_stmt (old);
267bac10
JM
4041 params->truncate (0);
4042
4043 /* Create the expressions for floating-point environment
4044 manipulation, if required. */
4045 bool need_fenv = (flag_trapping_math
4046 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4047 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4048 if (need_fenv)
4049 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4050
4051 if (hold_call)
4052 add_stmt (hold_call);
4053
4054 /* loop: */
4055 add_stmt (loop_label);
4056
4057 /* newval = old + val; */
4058 rhs = build_binary_op (loc, modifycode, old, val, 1);
5c4abbb8 4059 rhs = c_fully_fold (rhs, false, NULL);
68fca595
MP
4060 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4061 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
4062 NULL_TREE, 0);
4063 if (rhs != error_mark_node)
4064 {
5c4abbb8
MP
4065 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4066 NULL_TREE);
267bac10
JM
4067 SET_EXPR_LOCATION (rhs, loc);
4068 add_stmt (rhs);
4069 }
4070
4071 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4072 goto done; */
4073 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4074 params->quick_push (lhs_addr);
4075 params->quick_push (old_addr);
4076 params->quick_push (newval_addr);
4077 params->quick_push (integer_zero_node);
4078 params->quick_push (seq_cst);
4079 params->quick_push (seq_cst);
8edbfaa6 4080 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
4081
4082 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4083 SET_EXPR_LOCATION (goto_stmt, loc);
4084
4085 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4086 SET_EXPR_LOCATION (stmt, loc);
4087 add_stmt (stmt);
5c4abbb8 4088
267bac10
JM
4089 if (clear_call)
4090 add_stmt (clear_call);
4091
4092 /* goto loop; */
4093 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4094 SET_EXPR_LOCATION (goto_stmt, loc);
4095 add_stmt (goto_stmt);
5c4abbb8 4096
267bac10
JM
4097 /* done: */
4098 add_stmt (done_label);
4099
4100 if (update_call)
4101 add_stmt (update_call);
4102
4103 /* Finish the compound statement. */
4104 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4105
4106 /* NEWVAL is the value that was successfully stored, return a
4107 COMPOUND_EXPR of the statement and the appropriate value. */
4108 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4109 return_old_p ? old : newval);
4110}
4111
3e4093b6
RS
4112/* Construct and perhaps optimize a tree representation
4113 for a unary operation. CODE, a tree_code, specifies the operation
4114 and XARG is the operand.
e51fbec3
MP
4115 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4116 promotions (such as from short to int).
4117 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4118 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4119 to pointers in C99.
c9f9eb5d
AH
4120
4121 LOCATION is the location of the operator. */
400fbf9f 4122
3e4093b6 4123tree
4c13ba17 4124build_unary_op (location_t location, enum tree_code code, tree xarg,
e51fbec3 4125 bool noconvert)
3e4093b6
RS
4126{
4127 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4128 tree arg = xarg;
4129 tree argtype = 0;
808d6eaa 4130 enum tree_code typecode;
3e4093b6 4131 tree val;
c9f9eb5d 4132 tree ret = error_mark_node;
8ce94e44 4133 tree eptype = NULL_TREE;
4de67c26 4134 const char *invalid_op_diag;
928c19bb
JM
4135 bool int_operands;
4136
4137 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
4138 if (int_operands)
4139 arg = remove_c_maybe_const_expr (arg);
400fbf9f 4140
808d6eaa 4141 if (code != ADDR_EXPR)
4f2e1536 4142 arg = require_complete_type (location, arg);
808d6eaa
JM
4143
4144 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
4145 if (typecode == ERROR_MARK)
4146 return error_mark_node;
4147 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4148 typecode = INTEGER_TYPE;
6c36d76b 4149
4de67c26
JM
4150 if ((invalid_op_diag
4151 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4152 {
c9f9eb5d 4153 error_at (location, invalid_op_diag);
4de67c26
JM
4154 return error_mark_node;
4155 }
4156
8ce94e44
JM
4157 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4158 {
4159 eptype = TREE_TYPE (arg);
4160 arg = TREE_OPERAND (arg, 0);
4161 }
4162
3e4093b6
RS
4163 switch (code)
4164 {
4165 case CONVERT_EXPR:
4166 /* This is used for unary plus, because a CONVERT_EXPR
4167 is enough to prevent anybody from looking inside for
4168 associativity, but won't generate any code. */
4169 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 4170 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 4171 || typecode == VECTOR_TYPE))
400fbf9f 4172 {
c9f9eb5d 4173 error_at (location, "wrong type argument to unary plus");
3e4093b6 4174 return error_mark_node;
400fbf9f 4175 }
3e4093b6
RS
4176 else if (!noconvert)
4177 arg = default_conversion (arg);
db3927fb 4178 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
4179 break;
4180
3e4093b6
RS
4181 case NEGATE_EXPR:
4182 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 4183 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
4184 || typecode == VECTOR_TYPE))
4185 {
c9f9eb5d 4186 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
4187 return error_mark_node;
4188 }
4189 else if (!noconvert)
4190 arg = default_conversion (arg);
400fbf9f
JW
4191 break;
4192
3e4093b6 4193 case BIT_NOT_EXPR:
462643f0
AP
4194 /* ~ works on integer types and non float vectors. */
4195 if (typecode == INTEGER_TYPE
4196 || (typecode == VECTOR_TYPE
4197 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 4198 {
9a2300e9
MP
4199 tree e = arg;
4200
4201 /* Warn if the expression has boolean value. */
4202 while (TREE_CODE (e) == COMPOUND_EXPR)
4203 e = TREE_OPERAND (e, 1);
4204
4205 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4206 || truth_value_p (TREE_CODE (e)))
4207 && warning_at (location, OPT_Wbool_operation,
4208 "%<~%> on a boolean expression"))
4209 {
4210 gcc_rich_location richloc (location);
4211 richloc.add_fixit_insert_before (location, "!");
4212 inform_at_rich_loc (&richloc, "did you mean to use logical "
4213 "not?");
4214 }
3e4093b6
RS
4215 if (!noconvert)
4216 arg = default_conversion (arg);
03d5b1f5 4217 }
3e4093b6 4218 else if (typecode == COMPLEX_TYPE)
400fbf9f 4219 {
3e4093b6 4220 code = CONJ_EXPR;
c1771a20 4221 pedwarn (location, OPT_Wpedantic,
fcf73884 4222 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
4223 if (!noconvert)
4224 arg = default_conversion (arg);
4225 }
4226 else
4227 {
c9f9eb5d 4228 error_at (location, "wrong type argument to bit-complement");
3e4093b6 4229 return error_mark_node;
400fbf9f
JW
4230 }
4231 break;
4232
3e4093b6 4233 case ABS_EXPR:
11017cc7 4234 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 4235 {
c9f9eb5d 4236 error_at (location, "wrong type argument to abs");
3e4093b6 4237 return error_mark_node;
400fbf9f 4238 }
3e4093b6
RS
4239 else if (!noconvert)
4240 arg = default_conversion (arg);
400fbf9f
JW
4241 break;
4242
3e4093b6
RS
4243 case CONJ_EXPR:
4244 /* Conjugating a real value is a no-op, but allow it anyway. */
4245 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4246 || typecode == COMPLEX_TYPE))
400fbf9f 4247 {
c9f9eb5d 4248 error_at (location, "wrong type argument to conjugation");
3e4093b6 4249 return error_mark_node;
400fbf9f 4250 }
3e4093b6
RS
4251 else if (!noconvert)
4252 arg = default_conversion (arg);
400fbf9f
JW
4253 break;
4254
3e4093b6 4255 case TRUTH_NOT_EXPR:
ab22c1fa 4256 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 4257 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 4258 && typecode != COMPLEX_TYPE)
400fbf9f 4259 {
c9f9eb5d
AH
4260 error_at (location,
4261 "wrong type argument to unary exclamation mark");
3e4093b6 4262 return error_mark_node;
400fbf9f 4263 }
a27d595d
JM
4264 if (int_operands)
4265 {
4266 arg = c_objc_common_truthvalue_conversion (location, xarg);
4267 arg = remove_c_maybe_const_expr (arg);
4268 }
4269 else
4270 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 4271 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
4272 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4273 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4274 location = EXPR_LOCATION (ret);
c9f9eb5d 4275 goto return_build_unary_op;
3e4093b6 4276
3e4093b6 4277 case REALPART_EXPR:
3e4093b6 4278 case IMAGPART_EXPR:
fb52b50a
NF
4279 ret = build_real_imag_expr (location, code, arg);
4280 if (ret == error_mark_node)
4281 return error_mark_node;
8ce94e44
JM
4282 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4283 eptype = TREE_TYPE (eptype);
c9f9eb5d 4284 goto return_build_unary_op;
3e4093b6
RS
4285
4286 case PREINCREMENT_EXPR:
4287 case POSTINCREMENT_EXPR:
4288 case PREDECREMENT_EXPR:
4289 case POSTDECREMENT_EXPR:
3e4093b6 4290
928c19bb
JM
4291 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4292 {
4293 tree inner = build_unary_op (location, code,
4c13ba17
MP
4294 C_MAYBE_CONST_EXPR_EXPR (arg),
4295 noconvert);
928c19bb
JM
4296 if (inner == error_mark_node)
4297 return error_mark_node;
4298 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4299 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4300 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4301 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4302 goto return_build_unary_op;
4303 }
4304
925e8657
NP
4305 /* Complain about anything that is not a true lvalue. In
4306 Objective-C, skip this check for property_refs. */
f90e8e2e 4307 if (!objc_is_property_ref (arg)
7bd11157
TT
4308 && !lvalue_or_else (location,
4309 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
4310 || code == POSTINCREMENT_EXPR)
4311 ? lv_increment
4312 : lv_decrement)))
928c19bb
JM
4313 return error_mark_node;
4314
09639a83
ILT
4315 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4316 {
4317 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4318 warning_at (location, OPT_Wc___compat,
4319 "increment of enumeration value is invalid in C++");
4320 else
4321 warning_at (location, OPT_Wc___compat,
4322 "decrement of enumeration value is invalid in C++");
4323 }
4324
9a2300e9
MP
4325 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4326 {
4327 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4328 warning_at (location, OPT_Wbool_operation,
4329 "increment of a boolean expression");
4330 else
4331 warning_at (location, OPT_Wbool_operation,
4332 "decrement of a boolean expression");
4333 }
4334
928c19bb
JM
4335 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4336 arg = c_fully_fold (arg, false, NULL);
4337
267bac10
JM
4338 bool atomic_op;
4339 atomic_op = really_atomic_lvalue (arg);
4340
3e4093b6
RS
4341 /* Increment or decrement the real part of the value,
4342 and don't change the imaginary part. */
4343 if (typecode == COMPLEX_TYPE)
400fbf9f 4344 {
3e4093b6
RS
4345 tree real, imag;
4346
c1771a20 4347 pedwarn (location, OPT_Wpedantic,
509c9d60 4348 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 4349
267bac10
JM
4350 if (!atomic_op)
4351 {
4352 arg = stabilize_reference (arg);
e51fbec3
MP
4353 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4354 true);
4355 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4356 true);
4357 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
267bac10
JM
4358 if (real == error_mark_node || imag == error_mark_node)
4359 return error_mark_node;
4360 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4361 real, imag);
4362 goto return_build_unary_op;
4363 }
400fbf9f 4364 }
3e4093b6
RS
4365
4366 /* Report invalid types. */
4367
ab22c1fa 4368 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4369 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4370 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4371 {
3e4093b6 4372 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4373 error_at (location, "wrong type argument to increment");
c22cacf3 4374 else
c9f9eb5d 4375 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4376
4377 return error_mark_node;
400fbf9f 4378 }
400fbf9f 4379
3e4093b6
RS
4380 {
4381 tree inc;
400fbf9f 4382
3e4093b6
RS
4383 argtype = TREE_TYPE (arg);
4384
4385 /* Compute the increment. */
4386
4387 if (typecode == POINTER_TYPE)
4388 {
a0e24419 4389 /* If pointer target is an incomplete type,
3e4093b6 4390 we just cannot know how to do the arithmetic. */
b70cef5d 4391 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4392 {
4393 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4394 error_at (location,
a0e24419
MP
4395 "increment of pointer to an incomplete type %qT",
4396 TREE_TYPE (argtype));
3e4093b6 4397 else
c9f9eb5d 4398 error_at (location,
a0e24419
MP
4399 "decrement of pointer to an incomplete type %qT",
4400 TREE_TYPE (argtype));
3e4093b6 4401 }
b70cef5d
JJ
4402 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4403 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4404 {
3e4093b6 4405 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4406 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4407 "wrong type argument to increment");
3e4093b6 4408 else
44d90fe1 4409 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4410 "wrong type argument to decrement");
3e4093b6
RS
4411 }
4412
b70cef5d 4413 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4414 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4415 }
b70cef5d 4416 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4417 {
4418 /* For signed fract types, we invert ++ to -- or
4419 -- to ++, and change inc from 1 to -1, because
4420 it is not possible to represent 1 in signed fract constants.
4421 For unsigned fract types, the result always overflows and
4422 we get an undefined (original) or the maximum value. */
4423 if (code == PREINCREMENT_EXPR)
4424 code = PREDECREMENT_EXPR;
4425 else if (code == PREDECREMENT_EXPR)
4426 code = PREINCREMENT_EXPR;
4427 else if (code == POSTINCREMENT_EXPR)
4428 code = POSTDECREMENT_EXPR;
4429 else /* code == POSTDECREMENT_EXPR */
4430 code = POSTINCREMENT_EXPR;
4431
4432 inc = integer_minus_one_node;
4433 inc = convert (argtype, inc);
4434 }
3e4093b6 4435 else
5be014d5 4436 {
241b71bb
TV
4437 inc = VECTOR_TYPE_P (argtype)
4438 ? build_one_cst (argtype)
4439 : integer_one_node;
5be014d5
AP
4440 inc = convert (argtype, inc);
4441 }
3e4093b6 4442
925e8657
NP
4443 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4444 need to ask Objective-C to build the increment or decrement
4445 expression for it. */
4446 if (objc_is_property_ref (arg))
f90e8e2e 4447 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4448 arg, inc);
4449
3e4093b6 4450 /* Report a read-only lvalue. */
f37acdf9 4451 if (TYPE_READONLY (argtype))
953ff289 4452 {
c02065fc 4453 readonly_error (location, arg,
953ff289
DN
4454 ((code == PREINCREMENT_EXPR
4455 || code == POSTINCREMENT_EXPR)
4456 ? lv_increment : lv_decrement));
4457 return error_mark_node;
4458 }
f37acdf9
JM
4459 else if (TREE_READONLY (arg))
4460 readonly_warning (arg,
4461 ((code == PREINCREMENT_EXPR
4462 || code == POSTINCREMENT_EXPR)
4463 ? lv_increment : lv_decrement));
3e4093b6 4464
267bac10
JM
4465 /* If the argument is atomic, use the special code sequences for
4466 atomic compound assignment. */
4467 if (atomic_op)
4468 {
4469 arg = stabilize_reference (arg);
4470 ret = build_atomic_assign (location, arg,
4471 ((code == PREINCREMENT_EXPR
4472 || code == POSTINCREMENT_EXPR)
4473 ? PLUS_EXPR
4474 : MINUS_EXPR),
4475 (FRACT_MODE_P (TYPE_MODE (argtype))
4476 ? inc
4477 : integer_one_node),
4478 (code == POSTINCREMENT_EXPR
4479 || code == POSTDECREMENT_EXPR));
4480 goto return_build_unary_op;
4481 }
4482
3e4093b6
RS
4483 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4484 val = boolean_increment (code, arg);
4485 else
53fb4de3 4486 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4487 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4488 if (TREE_CODE (val) != code)
6de9cd9a 4489 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4490 ret = val;
4491 goto return_build_unary_op;
3e4093b6
RS
4492 }
4493
4494 case ADDR_EXPR:
4495 /* Note that this operation never does default_conversion. */
4496
2b4b7036
JM
4497 /* The operand of unary '&' must be an lvalue (which excludes
4498 expressions of type void), or, in C99, the result of a [] or
4499 unary '*' operator. */
4500 if (VOID_TYPE_P (TREE_TYPE (arg))
4501 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
22d03525 4502 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
2b4b7036
JM
4503 pedwarn (location, 0, "taking address of expression of type %<void%>");
4504
3e4093b6 4505 /* Let &* cancel out to simplify resulting code. */
22d03525 4506 if (INDIRECT_REF_P (arg))
400fbf9f 4507 {
3e4093b6
RS
4508 /* Don't let this be an lvalue. */
4509 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4510 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4511 ret = TREE_OPERAND (arg, 0);
4512 goto return_build_unary_op;
400fbf9f 4513 }
1eb8759b 4514
3e4093b6
RS
4515 /* Anything not already handled and not a true memory reference
4516 or a non-lvalue array is an error. */
4c13ba17 4517 if (typecode != FUNCTION_TYPE && !noconvert
ee45a32d 4518 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4519 return error_mark_node;
b6a10c9f 4520
928c19bb
JM
4521 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4522 folding later. */
4523 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4524 {
4525 tree inner = build_unary_op (location, code,
4c13ba17
MP
4526 C_MAYBE_CONST_EXPR_EXPR (arg),
4527 noconvert);
928c19bb
JM
4528 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4529 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4530 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4531 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4532 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4533 goto return_build_unary_op;
4534 }
4535
3e4093b6
RS
4536 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4537 argtype = TREE_TYPE (arg);
400fbf9f 4538
3e4093b6 4539 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4540 to which the address will point. This is only needed
f2c1da78 4541 for function types. */
6615c446 4542 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4543 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4544 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4545 {
4546 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4547 int quals = orig_quals;
4548
4549 if (TREE_READONLY (arg))
4550 quals |= TYPE_QUAL_CONST;
4551 if (TREE_THIS_VOLATILE (arg))
4552 quals |= TYPE_QUAL_VOLATILE;
4553
f2c1da78
JM
4554 argtype = c_build_qualified_type (argtype, quals);
4555 }
400fbf9f 4556
ee45a32d
EB
4557 switch (TREE_CODE (arg))
4558 {
4559 case COMPONENT_REF:
4560 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4561 {
949505a9
MP
4562 error_at (location, "cannot take address of bit-field %qD",
4563 TREE_OPERAND (arg, 1));
ee45a32d
EB
4564 return error_mark_node;
4565 }
4566
191816a3 4567 /* fall through */
ee45a32d
EB
4568
4569 case ARRAY_REF:
4570 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4571 {
4572 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4573 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4574 {
949505a9
MP
4575 error_at (location, "cannot take address of scalar with "
4576 "reverse storage order");
ee45a32d
EB
4577 return error_mark_node;
4578 }
4579
4580 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4581 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
949505a9
MP
4582 warning_at (location, OPT_Wscalar_storage_order,
4583 "address of array with reverse scalar storage "
4584 "order requested");
ee45a32d
EB
4585 }
4586
4587 default:
4588 break;
4589 }
4590
3e4093b6
RS
4591 if (!c_mark_addressable (arg))
4592 return error_mark_node;
400fbf9f 4593
abb54d14
JM
4594 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4595 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4596
5cc200fc 4597 argtype = build_pointer_type (argtype);
5e55f99d
RH
4598
4599 /* ??? Cope with user tricks that amount to offsetof. Delete this
4600 when we have proper support for integer constant expressions. */
4601 val = get_base_address (arg);
22d03525 4602 if (val && INDIRECT_REF_P (val)
3aa2ddb8
JJ
4603 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4604 {
cf9e9959 4605 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4606 goto return_build_unary_op;
3aa2ddb8 4607 }
5e55f99d 4608
5cc200fc 4609 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4610
c9f9eb5d
AH
4611 ret = val;
4612 goto return_build_unary_op;
400fbf9f 4613
3e4093b6 4614 default:
1344f9a3 4615 gcc_unreachable ();
3e4093b6 4616 }
400fbf9f 4617
3e4093b6
RS
4618 if (argtype == 0)
4619 argtype = TREE_TYPE (arg);
928c19bb
JM
4620 if (TREE_CODE (arg) == INTEGER_CST)
4621 ret = (require_constant_value
db3927fb
AH
4622 ? fold_build1_initializer_loc (location, code, argtype, arg)
4623 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4624 else
4625 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4626 return_build_unary_op:
4627 gcc_assert (ret != error_mark_node);
928c19bb
JM
4628 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4629 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4630 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4631 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4632 ret = note_integer_operands (ret);
8ce94e44
JM
4633 if (eptype)
4634 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4635 protected_set_expr_location (ret, location);
4636 return ret;
3e4093b6 4637}
400fbf9f 4638
3e4093b6
RS
4639/* Return nonzero if REF is an lvalue valid for this language.
4640 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4641 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4642
1e4ae551 4643bool
58f9752a 4644lvalue_p (const_tree ref)
3e4093b6 4645{
58f9752a 4646 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4647
3e4093b6
RS
4648 switch (code)
4649 {
4650 case REALPART_EXPR:
4651 case IMAGPART_EXPR:
4652 case COMPONENT_REF:
4653 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4654
928c19bb
JM
4655 case C_MAYBE_CONST_EXPR:
4656 return lvalue_p (TREE_OPERAND (ref, 1));
4657
3e4093b6
RS
4658 case COMPOUND_LITERAL_EXPR:
4659 case STRING_CST:
5b73d2ab 4660 return true;
400fbf9f 4661
3e4093b6
RS
4662 case INDIRECT_REF:
4663 case ARRAY_REF:
36536d79 4664 case ARRAY_NOTATION_REF:
3e4093b6
RS
4665 case VAR_DECL:
4666 case PARM_DECL:
4667 case RESULT_DECL:
4668 case ERROR_MARK:
4669 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4670 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4671
3e4093b6 4672 case BIND_EXPR:
3e4093b6 4673 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4674
3e4093b6 4675 default:
5b73d2ab 4676 return false;
3e4093b6
RS
4677 }
4678}
400fbf9f 4679\f
f37acdf9
JM
4680/* Give a warning for storing in something that is read-only in GCC
4681 terms but not const in ISO C terms. */
4682
4683static void
4684readonly_warning (tree arg, enum lvalue_use use)
4685{
4686 switch (use)
4687 {
4688 case lv_assign:
4689 warning (0, "assignment of read-only location %qE", arg);
4690 break;
4691 case lv_increment:
4692 warning (0, "increment of read-only location %qE", arg);
4693 break;
4694 case lv_decrement:
4695 warning (0, "decrement of read-only location %qE", arg);
4696 break;
4697 default:
4698 gcc_unreachable ();
4699 }
4700 return;
4701}
4702
37dc0d8d
JM
4703
4704/* Return nonzero if REF is an lvalue valid for this language;
4705 otherwise, print an error message and return zero. USE says
7bd11157
TT
4706 how the lvalue is being used and so selects the error message.
4707 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4708
4709static int
7bd11157 4710lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4711{
4712 int win = lvalue_p (ref);
4713
4714 if (!win)
7bd11157 4715 lvalue_error (loc, use);
37dc0d8d
JM
4716
4717 return win;
4718}
3e4093b6
RS
4719\f
4720/* Mark EXP saying that we need to be able to take the
4721 address of it; it should not be allocated in a register.
4722 Returns true if successful. */
54c93c30 4723
3e4093b6
RS
4724bool
4725c_mark_addressable (tree exp)
400fbf9f 4726{
3e4093b6 4727 tree x = exp;
95602da1 4728
3e4093b6
RS
4729 while (1)
4730 switch (TREE_CODE (x))
4731 {
4732 case COMPONENT_REF:
3e4093b6
RS
4733 case ADDR_EXPR:
4734 case ARRAY_REF:
4735 case REALPART_EXPR:
4736 case IMAGPART_EXPR:
4737 x = TREE_OPERAND (x, 0);
4738 break;
95602da1 4739
3e4093b6
RS
4740 case COMPOUND_LITERAL_EXPR:
4741 case CONSTRUCTOR:
4742 TREE_ADDRESSABLE (x) = 1;
4743 return true;
95602da1 4744
3e4093b6
RS
4745 case VAR_DECL:
4746 case CONST_DECL:
4747 case PARM_DECL:
4748 case RESULT_DECL:
5baeaac0 4749 if (C_DECL_REGISTER (x)
3e4093b6
RS
4750 && DECL_NONLOCAL (x))
4751 {
62f9079a 4752 if (TREE_PUBLIC (x) || is_global_var (x))
3e4093b6 4753 {
0039fa55
AN
4754 error
4755 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4756 return false;
4757 }
509c9d60 4758 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4759 }
5baeaac0 4760 else if (C_DECL_REGISTER (x))
3e4093b6 4761 {
62f9079a 4762 if (TREE_PUBLIC (x) || is_global_var (x))
0039fa55
AN
4763 error ("address of global register variable %qD requested", x);
4764 else
4765 error ("address of register variable %qD requested", x);
4766 return false;
3e4093b6 4767 }
400fbf9f 4768
191816a3 4769 /* FALLTHRU */
3e4093b6
RS
4770 case FUNCTION_DECL:
4771 TREE_ADDRESSABLE (x) = 1;
191816a3 4772 /* FALLTHRU */
3e4093b6
RS
4773 default:
4774 return true;
4775 }
4776}
4777\f
2d2e923f
MLI
4778/* Convert EXPR to TYPE, warning about conversion problems with
4779 constants. SEMANTIC_TYPE is the type this conversion would use
4780 without excess precision. If SEMANTIC_TYPE is NULL, this function
4781 is equivalent to convert_and_check. This function is a wrapper that
4782 handles conversions that may be different than
4783 the usual ones because of excess precision. */
4784
4785static tree
68fca595
MP
4786ep_convert_and_check (location_t loc, tree type, tree expr,
4787 tree semantic_type)
2d2e923f
MLI
4788{
4789 if (TREE_TYPE (expr) == type)
4790 return expr;
4791
4792 if (!semantic_type)
68fca595 4793 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4794
4795 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4796 && TREE_TYPE (expr) != semantic_type)
4797 {
4798 /* For integers, we need to check the real conversion, not
4799 the conversion to the excess precision type. */
68fca595 4800 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4801 }
4802 /* Result type is the excess precision type, which should be
4803 large enough, so do not check. */
4804 return convert (type, expr);
4805}
4806
928c19bb
JM
4807/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4808 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4809 if folded to an integer constant then the unselected half may
4810 contain arbitrary operations not normally permitted in constant
c2255bc4 4811 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4812
4813tree
744aa42f 4814build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4815 tree op1, tree op1_original_type, tree op2,
4816 tree op2_original_type)
400fbf9f 4817{
3e4093b6
RS
4818 tree type1;
4819 tree type2;
4820 enum tree_code code1;
4821 enum tree_code code2;
4822 tree result_type = NULL;
2d2e923f 4823 tree semantic_result_type = NULL;
3e4093b6 4824 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4825 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4826 bool ifexp_int_operands;
928c19bb 4827 tree ret;
400fbf9f 4828
4d84fe7c
JM
4829 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4830 if (op1_int_operands)
4831 op1 = remove_c_maybe_const_expr (op1);
4832 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4833 if (op2_int_operands)
4834 op2 = remove_c_maybe_const_expr (op2);
4835 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4836 if (ifexp_int_operands)
4837 ifexp = remove_c_maybe_const_expr (ifexp);
4838
3e4093b6
RS
4839 /* Promote both alternatives. */
4840
4841 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4842 op1 = default_conversion (op1);
4843 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4844 op2 = default_conversion (op2);
4845
4846 if (TREE_CODE (ifexp) == ERROR_MARK
4847 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4848 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4849 return error_mark_node;
400fbf9f 4850
3e4093b6
RS
4851 type1 = TREE_TYPE (op1);
4852 code1 = TREE_CODE (type1);
4853 type2 = TREE_TYPE (op2);
4854 code2 = TREE_CODE (type2);
4855
1807ffc1
MS
4856 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4857 return error_mark_node;
4858
4859 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4860 return error_mark_node;
4861
b1adf557
JM
4862 /* C90 does not permit non-lvalue arrays in conditional expressions.
4863 In C99 they will be pointers by now. */
4864 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4865 {
744aa42f 4866 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4867 return error_mark_node;
4868 }
4869
8ce94e44
JM
4870 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4871 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4872 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4873 || code1 == COMPLEX_TYPE)
4874 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4875 || code2 == COMPLEX_TYPE))
4876 {
2d2e923f 4877 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4878 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4879 {
4880 op1 = TREE_OPERAND (op1, 0);
4881 type1 = TREE_TYPE (op1);
4882 gcc_assert (TREE_CODE (type1) == code1);
4883 }
4884 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4885 {
4886 op2 = TREE_OPERAND (op2, 0);
4887 type2 = TREE_TYPE (op2);
4888 gcc_assert (TREE_CODE (type2) == code2);
4889 }
4890 }
4891
d130ae11
ILT
4892 if (warn_cxx_compat)
4893 {
4894 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4895 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4896
4897 if (TREE_CODE (t1) == ENUMERAL_TYPE
4898 && TREE_CODE (t2) == ENUMERAL_TYPE
4899 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4900 warning_at (colon_loc, OPT_Wc___compat,
4901 ("different enum types in conditional is "
4902 "invalid in C++: %qT vs %qT"),
4903 t1, t2);
4904 }
4905
3e4093b6
RS
4906 /* Quickly detect the usual case where op1 and op2 have the same type
4907 after promotion. */
4908 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4909 {
3e4093b6
RS
4910 if (type1 == type2)
4911 result_type = type1;
4912 else
4913 result_type = TYPE_MAIN_VARIANT (type1);
4914 }
4915 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4916 || code1 == COMPLEX_TYPE)
4917 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4918 || code2 == COMPLEX_TYPE))
3e4093b6 4919 {
ccf7f880 4920 result_type = c_common_type (type1, type2);
5a578671
JM
4921 if (result_type == error_mark_node)
4922 return error_mark_node;
0a0b3574
MM
4923 do_warn_double_promotion (result_type, type1, type2,
4924 "implicit conversion from %qT to %qT to "
4925 "match other result of conditional",
4926 colon_loc);
400fbf9f 4927
3e4093b6
RS
4928 /* If -Wsign-compare, warn here if type1 and type2 have
4929 different signedness. We'll promote the signed to unsigned
4930 and later code won't know it used to be different.
4931 Do this check on the original types, so that explicit casts
4932 will be considered, but default promotions won't. */
7d882b83 4933 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4934 {
8df83eae
RK
4935 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4936 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4937
3e4093b6
RS
4938 if (unsigned_op1 ^ unsigned_op2)
4939 {
6ac01510
ILT
4940 bool ovf;
4941
3e4093b6
RS
4942 /* Do not warn if the result type is signed, since the
4943 signed type will only be chosen if it can represent
4944 all the values of the unsigned type. */
3f75a254 4945 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4946 /* OK */;
3e4093b6 4947 else
928c19bb
JM
4948 {
4949 bool op1_maybe_const = true;
4950 bool op2_maybe_const = true;
4951
4952 /* Do not warn if the signed quantity is an
4953 unsuffixed integer literal (or some static
4954 constant expression involving such literals) and
4955 it is non-negative. This warning requires the
4956 operands to be folded for best results, so do
4957 that folding in this case even without
4958 warn_sign_compare to avoid warning options
4959 possibly affecting code generation. */
f5178456
RS
4960 c_inhibit_evaluation_warnings
4961 += (ifexp == truthvalue_false_node);
928c19bb
JM
4962 op1 = c_fully_fold (op1, require_constant_value,
4963 &op1_maybe_const);
f5178456
RS
4964 c_inhibit_evaluation_warnings
4965 -= (ifexp == truthvalue_false_node);
4966
4967 c_inhibit_evaluation_warnings
4968 += (ifexp == truthvalue_true_node);
928c19bb
JM
4969 op2 = c_fully_fold (op2, require_constant_value,
4970 &op2_maybe_const);
f5178456
RS
4971 c_inhibit_evaluation_warnings
4972 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4973
4974 if (warn_sign_compare)
4975 {
4976 if ((unsigned_op2
4977 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4978 || (unsigned_op1
4979 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4980 /* OK */;
4981 else
744aa42f
ILT
4982 warning_at (colon_loc, OPT_Wsign_compare,
4983 ("signed and unsigned type in "
4984 "conditional expression"));
928c19bb
JM
4985 }
4986 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4987 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4988 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4989 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4990 }
3e4093b6
RS
4991 }
4992 }
4993 }
4994 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4995 {
fcf73884 4996 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4997 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4998 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4999 result_type = void_type_node;
5000 }
5001 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5002 {
36c5e70a
BE
5003 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5004 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5005 addr_space_t as_common;
5006
744aa42f 5007 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 5008 result_type = common_pointer_type (type1, type2);
6aa3c60d 5009 else if (null_pointer_constant_p (orig_op1))
36c5e70a 5010 result_type = type2;
6aa3c60d 5011 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
5012 result_type = type1;
5013 else if (!addr_space_superset (as1, as2, &as_common))
5014 {
5015 error_at (colon_loc, "pointers to disjoint address spaces "
5016 "used in conditional expression");
5017 return error_mark_node;
5018 }
267bac10
JM
5019 else if (VOID_TYPE_P (TREE_TYPE (type1))
5020 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 5021 {
768952be
MU
5022 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5023 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5024 & ~TYPE_QUALS (TREE_TYPE (type1))))
5025 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5026 "pointer to array loses qualifier "
5027 "in conditional expression");
5028
fcf73884 5029 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 5030 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 5031 "ISO C forbids conditional expr between "
bda67431 5032 "%<void *%> and function pointer");
3e4093b6
RS
5033 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5034 TREE_TYPE (type2)));
34a80643 5035 }
267bac10
JM
5036 else if (VOID_TYPE_P (TREE_TYPE (type2))
5037 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 5038 {
768952be
MU
5039 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5040 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5041 & ~TYPE_QUALS (TREE_TYPE (type2))))
5042 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5043 "pointer to array loses qualifier "
5044 "in conditional expression");
5045
fcf73884 5046 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 5047 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 5048 "ISO C forbids conditional expr between "
bda67431 5049 "%<void *%> and function pointer");
3e4093b6
RS
5050 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5051 TREE_TYPE (type1)));
1c2a9b35 5052 }
b581b85b
NP
5053 /* Objective-C pointer comparisons are a bit more lenient. */
5054 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5055 result_type = objc_common_type (type1, type2);
34a80643 5056 else
ab87f8c8 5057 {
36c5e70a
BE
5058 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5059
b581b85b
NP
5060 pedwarn (colon_loc, 0,
5061 "pointer type mismatch in conditional expression");
36c5e70a
BE
5062 result_type = build_pointer_type
5063 (build_qualified_type (void_type_node, qual));
ab87f8c8 5064 }
3e4093b6
RS
5065 }
5066 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5067 {
6aa3c60d 5068 if (!null_pointer_constant_p (orig_op2))
744aa42f 5069 pedwarn (colon_loc, 0,
509c9d60 5070 "pointer/integer type mismatch in conditional expression");
3e4093b6 5071 else
ab87f8c8 5072 {
3e4093b6 5073 op2 = null_pointer_node;
ab87f8c8 5074 }
3e4093b6
RS
5075 result_type = type1;
5076 }
5077 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5078 {
6aa3c60d 5079 if (!null_pointer_constant_p (orig_op1))
744aa42f 5080 pedwarn (colon_loc, 0,
509c9d60 5081 "pointer/integer type mismatch in conditional expression");
3e4093b6 5082 else
ab87f8c8 5083 {
3e4093b6 5084 op1 = null_pointer_node;
ab87f8c8 5085 }
3e4093b6
RS
5086 result_type = type2;
5087 }
1c2a9b35 5088
3e4093b6
RS
5089 if (!result_type)
5090 {
5091 if (flag_cond_mismatch)
5092 result_type = void_type_node;
5093 else
400fbf9f 5094 {
c2255bc4 5095 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 5096 return error_mark_node;
400fbf9f 5097 }
3e4093b6 5098 }
400fbf9f 5099
3e4093b6
RS
5100 /* Merge const and volatile flags of the incoming types. */
5101 result_type
5102 = build_type_variant (result_type,
afbd0665
AS
5103 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5104 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 5105
68fca595
MP
5106 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5107 semantic_result_type);
5108 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5109 semantic_result_type);
b6a10c9f 5110
928c19bb
JM
5111 if (ifexp_bcp && ifexp == truthvalue_true_node)
5112 {
5113 op2_int_operands = true;
5114 op1 = c_fully_fold (op1, require_constant_value, NULL);
5115 }
5116 if (ifexp_bcp && ifexp == truthvalue_false_node)
5117 {
5118 op1_int_operands = true;
5119 op2 = c_fully_fold (op2, require_constant_value, NULL);
5120 }
4d84fe7c 5121 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
5122 && op1_int_operands
5123 && op2_int_operands);
5124 if (int_operands)
5125 {
5126 int_const = ((ifexp == truthvalue_true_node
5127 && TREE_CODE (orig_op1) == INTEGER_CST
5128 && !TREE_OVERFLOW (orig_op1))
5129 || (ifexp == truthvalue_false_node
5130 && TREE_CODE (orig_op2) == INTEGER_CST
5131 && !TREE_OVERFLOW (orig_op2)));
5132 }
9f47c7e5
IE
5133
5134 /* Need to convert condition operand into a vector mask. */
5135 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5136 {
5137 tree vectype = TREE_TYPE (ifexp);
5138 tree elem_type = TREE_TYPE (vectype);
5139 tree zero = build_int_cst (elem_type, 0);
5140 tree zero_vec = build_vector_from_val (vectype, zero);
5141 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5142 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5143 }
5144
928c19bb 5145 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 5146 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
5147 else
5148 {
01c7ccbb
JM
5149 if (int_operands)
5150 {
f34f1c87
MP
5151 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5152 nested inside of the expression. */
5153 op1 = c_fully_fold (op1, false, NULL);
5154 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 5155 }
928c19bb
JM
5156 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5157 if (int_operands)
5158 ret = note_integer_operands (ret);
5159 }
2d2e923f
MLI
5160 if (semantic_result_type)
5161 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 5162
c2255bc4 5163 protected_set_expr_location (ret, colon_loc);
928c19bb 5164 return ret;
3e4093b6
RS
5165}
5166\f
487a92fe 5167/* Return a compound expression that performs two expressions and
c2255bc4
AH
5168 returns the value of the second of them.
5169
5170 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 5171
3e4093b6 5172tree
c2255bc4 5173build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 5174{
4d84fe7c 5175 bool expr1_int_operands, expr2_int_operands;
8ce94e44 5176 tree eptype = NULL_TREE;
928c19bb
JM
5177 tree ret;
5178
b72271b9 5179 if (flag_cilkplus
939b37da
BI
5180 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5181 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5182 {
5183 error_at (loc,
5184 "spawned function call cannot be part of a comma expression");
5185 return error_mark_node;
5186 }
4d84fe7c
JM
5187 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5188 if (expr1_int_operands)
5189 expr1 = remove_c_maybe_const_expr (expr1);
5190 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5191 if (expr2_int_operands)
5192 expr2 = remove_c_maybe_const_expr (expr2);
5193
8ce94e44
JM
5194 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5195 expr1 = TREE_OPERAND (expr1, 0);
5196 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5197 {
5198 eptype = TREE_TYPE (expr2);
5199 expr2 = TREE_OPERAND (expr2, 0);
5200 }
5201
3f75a254 5202 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
5203 {
5204 /* The left-hand operand of a comma expression is like an expression
c5409249 5205 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 5206 any side-effects, unless it was explicitly cast to (void). */
e14a6540 5207 if (warn_unused_value)
47aecf47 5208 {
e14a6540 5209 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 5210 && CONVERT_EXPR_P (expr1))
47aecf47 5211 ; /* (void) a, b */
e14a6540
JM
5212 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5213 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 5214 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
5215 ; /* (void) a, (void) b, c */
5216 else
b8698a0f 5217 warning_at (loc, OPT_Wunused_value,
c2255bc4 5218 "left-hand operand of comma expression has no effect");
47aecf47 5219 }
3e4093b6 5220 }
789eadcd
MP
5221 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5222 && warn_unused_value)
5223 {
5224 tree r = expr1;
5225 location_t cloc = loc;
5226 while (TREE_CODE (r) == COMPOUND_EXPR)
5227 {
5228 if (EXPR_HAS_LOCATION (r))
5229 cloc = EXPR_LOCATION (r);
5230 r = TREE_OPERAND (r, 1);
5231 }
5232 if (!TREE_SIDE_EFFECTS (r)
5233 && !VOID_TYPE_P (TREE_TYPE (r))
5234 && !CONVERT_EXPR_P (r))
5235 warning_at (cloc, OPT_Wunused_value,
5236 "right-hand operand of comma expression has no effect");
5237 }
400fbf9f 5238
3e4093b6
RS
5239 /* With -Wunused, we should also warn if the left-hand operand does have
5240 side-effects, but computes a value which is not used. For example, in
5241 `foo() + bar(), baz()' the result of the `+' operator is not used,
5242 so we should issue a warning. */
5243 else if (warn_unused_value)
c2255bc4 5244 warn_if_unused_value (expr1, loc);
400fbf9f 5245
e63d6886
AP
5246 if (expr2 == error_mark_node)
5247 return error_mark_node;
5248
928c19bb
JM
5249 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5250
5251 if (flag_isoc99
4d84fe7c
JM
5252 && expr1_int_operands
5253 && expr2_int_operands)
928c19bb
JM
5254 ret = note_integer_operands (ret);
5255
8ce94e44
JM
5256 if (eptype)
5257 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5258
c2255bc4 5259 protected_set_expr_location (ret, loc);
928c19bb 5260 return ret;
3e4093b6 5261}
400fbf9f 5262
67165eb3
ILT
5263/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5264 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
5265 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5266 of the cast. -Wcast-qual appeared on the command line. Named
5267 address space qualifiers are not handled here, because they result
5268 in different warnings. */
67165eb3
ILT
5269
5270static void
2ee3cb35 5271handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
5272{
5273 tree in_type = type;
5274 tree in_otype = otype;
5275 int added = 0;
5276 int discarded = 0;
5277 bool is_const;
5278
5279 /* Check that the qualifiers on IN_TYPE are a superset of the
5280 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5281 nodes is uninteresting and we stop as soon as we hit a
5282 non-POINTER_TYPE node on either type. */
5283 do
5284 {
5285 in_otype = TREE_TYPE (in_otype);
5286 in_type = TREE_TYPE (in_type);
5287
5288 /* GNU C allows cv-qualified function types. 'const' means the
5289 function is very pure, 'volatile' means it can't return. We
5290 need to warn when such qualifiers are added, not when they're
5291 taken away. */
5292 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5293 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
5294 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5295 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 5296 else
36c5e70a
BE
5297 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5298 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
5299 }
5300 while (TREE_CODE (in_type) == POINTER_TYPE
5301 && TREE_CODE (in_otype) == POINTER_TYPE);
5302
5303 if (added)
2ee3cb35
MLI
5304 warning_at (loc, OPT_Wcast_qual,
5305 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
5306
5307 if (discarded)
5308 /* There are qualifiers present in IN_OTYPE that are not present
5309 in IN_TYPE. */
2ee3cb35 5310 warning_at (loc, OPT_Wcast_qual,
7485aeea 5311 "cast discards %qv qualifier from pointer target type",
2ee3cb35 5312 discarded);
67165eb3
ILT
5313
5314 if (added || discarded)
5315 return;
5316
5317 /* A cast from **T to const **T is unsafe, because it can cause a
5318 const value to be changed with no additional warning. We only
5319 issue this warning if T is the same on both sides, and we only
5320 issue the warning if there are the same number of pointers on
5321 both sides, as otherwise the cast is clearly unsafe anyhow. A
5322 cast is unsafe when a qualifier is added at one level and const
5323 is not present at all outer levels.
5324
5325 To issue this warning, we check at each level whether the cast
5326 adds new qualifiers not already seen. We don't need to special
5327 case function types, as they won't have the same
5328 TYPE_MAIN_VARIANT. */
5329
5330 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5331 return;
5332 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5333 return;
5334
5335 in_type = type;
5336 in_otype = otype;
5337 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5338 do
5339 {
5340 in_type = TREE_TYPE (in_type);
5341 in_otype = TREE_TYPE (in_otype);
5342 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5343 && !is_const)
5344 {
2ee3cb35
MLI
5345 warning_at (loc, OPT_Wcast_qual,
5346 "to be safe all intermediate pointers in cast from "
5347 "%qT to %qT must be %<const%> qualified",
5348 otype, type);
67165eb3
ILT
5349 break;
5350 }
5351 if (is_const)
5352 is_const = TYPE_READONLY (in_type);
5353 }
5354 while (TREE_CODE (in_type) == POINTER_TYPE);
5355}
5356
b8698a0f 5357/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 5358 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 5359
3e4093b6 5360tree
c2255bc4 5361build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 5362{
8ce94e44
JM
5363 tree value;
5364
5365 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5366 expr = TREE_OPERAND (expr, 0);
5367
5368 value = expr;
400fbf9f 5369
3e4093b6
RS
5370 if (type == error_mark_node || expr == error_mark_node)
5371 return error_mark_node;
400fbf9f 5372
3e4093b6
RS
5373 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5374 only in <protocol> qualifications. But when constructing cast expressions,
5375 the protocols do matter and must be kept around. */
700686fa
ZL
5376 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5377 return build1 (NOP_EXPR, type, expr);
5378
5379 type = TYPE_MAIN_VARIANT (type);
400fbf9f 5380
3e4093b6
RS
5381 if (TREE_CODE (type) == ARRAY_TYPE)
5382 {
c2255bc4 5383 error_at (loc, "cast specifies array type");
3e4093b6
RS
5384 return error_mark_node;
5385 }
400fbf9f 5386
3e4093b6
RS
5387 if (TREE_CODE (type) == FUNCTION_TYPE)
5388 {
c2255bc4 5389 error_at (loc, "cast specifies function type");
3e4093b6
RS
5390 return error_mark_node;
5391 }
400fbf9f 5392
808d6eaa
JM
5393 if (!VOID_TYPE_P (type))
5394 {
4f2e1536 5395 value = require_complete_type (loc, value);
808d6eaa
JM
5396 if (value == error_mark_node)
5397 return error_mark_node;
5398 }
5399
3e4093b6
RS
5400 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5401 {
3e636daf 5402 if (RECORD_OR_UNION_TYPE_P (type))
c1771a20 5403 pedwarn (loc, OPT_Wpedantic,
fcf73884 5404 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5405
5406 /* Convert to remove any qualifiers from VALUE's type. */
5407 value = convert (type, value);
3e4093b6
RS
5408 }
5409 else if (TREE_CODE (type) == UNION_TYPE)
5410 {
5411 tree field;
400fbf9f 5412
910ad8de 5413 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5414 if (TREE_TYPE (field) != error_mark_node
5415 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5416 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5417 break;
5418
5419 if (field)
400fbf9f 5420 {
3e4093b6 5421 tree t;
e616f54d 5422 bool maybe_const = true;
3e4093b6 5423
c1771a20 5424 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5425 t = c_fully_fold (value, false, &maybe_const);
5426 t = build_constructor_single (type, field, t);
5427 if (!maybe_const)
5428 t = c_wrap_maybe_const (t, true);
5429 t = digest_init (loc, type, t,
bbbbb16a 5430 NULL_TREE, false, true, 0);
3e4093b6
RS
5431 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5432 return t;
400fbf9f 5433 }
c2255bc4 5434 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5435 return error_mark_node;
5436 }
5437 else
5438 {
5439 tree otype, ovalue;
400fbf9f 5440
3e4093b6 5441 if (type == void_type_node)
c2255bc4
AH
5442 {
5443 tree t = build1 (CONVERT_EXPR, type, value);
5444 SET_EXPR_LOCATION (t, loc);
5445 return t;
5446 }
400fbf9f 5447
3e4093b6 5448 otype = TREE_TYPE (value);
400fbf9f 5449
3e4093b6 5450 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5451 if (warn_cast_qual
5452 && TREE_CODE (type) == POINTER_TYPE
5453 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5454 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5455
36c5e70a
BE
5456 /* Warn about conversions between pointers to disjoint
5457 address spaces. */
5458 if (TREE_CODE (type) == POINTER_TYPE
5459 && TREE_CODE (otype) == POINTER_TYPE
5460 && !null_pointer_constant_p (value))
5461 {
5462 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5463 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5464 addr_space_t as_common;
5465
5466 if (!addr_space_superset (as_to, as_from, &as_common))
5467 {
5468 if (ADDR_SPACE_GENERIC_P (as_from))
5469 warning_at (loc, 0, "cast to %s address space pointer "
5470 "from disjoint generic address space pointer",
5471 c_addr_space_name (as_to));
5472
5473 else if (ADDR_SPACE_GENERIC_P (as_to))
5474 warning_at (loc, 0, "cast to generic address space pointer "
5475 "from disjoint %s address space pointer",
5476 c_addr_space_name (as_from));
5477
5478 else
5479 warning_at (loc, 0, "cast to %s address space pointer "
5480 "from disjoint %s address space pointer",
5481 c_addr_space_name (as_to),
5482 c_addr_space_name (as_from));
5483 }
5484 }
5485
3e4093b6 5486 /* Warn about possible alignment problems. */
3176a0c2 5487 if (STRICT_ALIGNMENT
3e4093b6
RS
5488 && TREE_CODE (type) == POINTER_TYPE
5489 && TREE_CODE (otype) == POINTER_TYPE
5490 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5491 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5492 /* Don't warn about opaque types, where the actual alignment
5493 restriction is unknown. */
3e636daf 5494 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
3e4093b6
RS
5495 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5496 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5497 warning_at (loc, OPT_Wcast_align,
5498 "cast increases required alignment of target type");
e9a25f70 5499
3176a0c2 5500 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5501 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5502 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5503 /* Unlike conversion of integers to pointers, where the
5504 warning is disabled for converting constants because
5505 of cases such as SIG_*, warn about converting constant
5506 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5507 sign extension, and a warning is appropriate. */
c2255bc4
AH
5508 warning_at (loc, OPT_Wpointer_to_int_cast,
5509 "cast from pointer to integer of different size");
400fbf9f 5510
3176a0c2 5511 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5512 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5513 warning_at (loc, OPT_Wbad_function_cast,
5514 "cast from function call of type %qT "
5515 "to non-matching type %qT", otype, type);
400fbf9f 5516
3176a0c2 5517 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5518 && TREE_CODE (otype) == INTEGER_TYPE
5519 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5520 /* Don't warn about converting any constant. */
5521 && !TREE_CONSTANT (value))
c2255bc4
AH
5522 warning_at (loc,
5523 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5524 "of different size");
400fbf9f 5525
79bedddc
SR
5526 if (warn_strict_aliasing <= 2)
5527 strict_aliasing_warning (otype, type, expr);
400fbf9f 5528
3897f229
JM
5529 /* If pedantic, warn for conversions between function and object
5530 pointer types, except for converting a null pointer constant
5531 to function pointer type. */
5532 if (pedantic
5533 && TREE_CODE (type) == POINTER_TYPE
5534 && TREE_CODE (otype) == POINTER_TYPE
5535 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5536 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5537 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5538 "conversion of function pointer to object pointer type");
3897f229
JM
5539
5540 if (pedantic
5541 && TREE_CODE (type) == POINTER_TYPE
5542 && TREE_CODE (otype) == POINTER_TYPE
5543 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5544 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5545 && !null_pointer_constant_p (value))
c1771a20 5546 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5547 "conversion of object pointer to function pointer type");
3897f229 5548
3e4093b6 5549 ovalue = value;
3e4093b6 5550 value = convert (type, value);
400fbf9f 5551
3e4093b6 5552 /* Ignore any integer overflow caused by the cast. */
928c19bb 5553 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5554 {
8bcd6380 5555 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5556 {
8bcd6380
RS
5557 if (!TREE_OVERFLOW (value))
5558 {
5559 /* Avoid clobbering a shared constant. */
5560 value = copy_node (value);
5561 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5562 }
6414bad6 5563 }
8bcd6380 5564 else if (TREE_OVERFLOW (value))
d8e1f97b 5565 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5566 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5567 }
5568 }
400fbf9f 5569
53cd18ec 5570 /* Don't let a cast be an lvalue. */
9482b620 5571 if (lvalue_p (value))
db3927fb 5572 value = non_lvalue_loc (loc, value);
e9a25f70 5573
928c19bb
JM
5574 /* Don't allow the results of casting to floating-point or complex
5575 types be confused with actual constants, or casts involving
5576 integer and pointer types other than direct integer-to-integer
5577 and integer-to-pointer be confused with integer constant
5578 expressions and null pointer constants. */
5579 if (TREE_CODE (value) == REAL_CST
5580 || TREE_CODE (value) == COMPLEX_CST
5581 || (TREE_CODE (value) == INTEGER_CST
5582 && !((TREE_CODE (expr) == INTEGER_CST
5583 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5584 || TREE_CODE (expr) == REAL_CST
5585 || TREE_CODE (expr) == COMPLEX_CST)))
5586 value = build1 (NOP_EXPR, type, value);
5587
21ba0cea 5588 protected_set_expr_location (value, loc);
3e4093b6 5589 return value;
400fbf9f
JW
5590}
5591
c2255bc4
AH
5592/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5593 location of the open paren of the cast, or the position of the cast
5594 expr. */
3e4093b6 5595tree
c2255bc4 5596c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5597{
f8893e47 5598 tree type;
928c19bb
JM
5599 tree type_expr = NULL_TREE;
5600 bool type_expr_const = true;
5601 tree ret;
3e4093b6 5602 int saved_wsp = warn_strict_prototypes;
c5c76735 5603
3e4093b6
RS
5604 /* This avoids warnings about unprototyped casts on
5605 integers. E.g. "#define SIG_DFL (void(*)())0". */
5606 if (TREE_CODE (expr) == INTEGER_CST)
5607 warn_strict_prototypes = 0;
928c19bb 5608 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5609 warn_strict_prototypes = saved_wsp;
c5c76735 5610
1807ffc1
MS
5611 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5612 && reject_gcc_builtin (expr))
5613 return error_mark_node;
5614
c2255bc4 5615 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5616 if (type_expr)
5617 {
9f33203d
JM
5618 bool inner_expr_const = true;
5619 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5620 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5621 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5622 && inner_expr_const);
c2255bc4 5623 SET_EXPR_LOCATION (ret, loc);
928c19bb 5624 }
24b97832 5625
21ba0cea
MP
5626 if (!EXPR_HAS_LOCATION (ret))
5627 protected_set_expr_location (ret, loc);
24b97832 5628
9e5b2115
PB
5629 /* C++ does not permits types to be defined in a cast, but it
5630 allows references to incomplete types. */
5631 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5632 warning_at (loc, OPT_Wc___compat,
5633 "defining a type in a cast is invalid in C++");
5634
928c19bb 5635 return ret;
400fbf9f 5636}
3e4093b6
RS
5637\f
5638/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5639 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5640 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5641 MODIFYCODE is the code for a binary operator that we use
5642 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5643 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5644 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5645 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5646
c2255bc4
AH
5647 LOCATION is the location of the MODIFYCODE operator.
5648 RHS_LOC is the location of the RHS. */
2f6e4e97 5649
3e4093b6 5650tree
32e8bb8e 5651build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5652 enum tree_code modifycode,
c2255bc4 5653 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5654{
3e4093b6
RS
5655 tree result;
5656 tree newrhs;
241f845a 5657 tree rhseval = NULL_TREE;
8ce94e44 5658 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5659 tree lhstype = TREE_TYPE (lhs);
5660 tree olhstype = lhstype;
928c19bb 5661 bool npc;
267bac10 5662 bool is_atomic_op;
e9a25f70 5663
3e4093b6 5664 /* Types that aren't fully specified cannot be used in assignments. */
4f2e1536 5665 lhs = require_complete_type (location, lhs);
e9a25f70 5666
3e4093b6
RS
5667 /* Avoid duplicate error messages from operands that had errors. */
5668 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5669 return error_mark_node;
400fbf9f 5670
4c2ecab0
JM
5671 /* Ensure an error for assigning a non-lvalue array to an array in
5672 C90. */
5673 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5674 {
5675 error_at (location, "assignment to expression with array type");
5676 return error_mark_node;
5677 }
5678
46a88c12 5679 /* For ObjC properties, defer this check. */
7bd11157 5680 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5681 return error_mark_node;
5682
267bac10
JM
5683 is_atomic_op = really_atomic_lvalue (lhs);
5684
8ce94e44
JM
5685 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5686 {
5687 rhs_semantic_type = TREE_TYPE (rhs);
5688 rhs = TREE_OPERAND (rhs, 0);
5689 }
5690
3e4093b6 5691 newrhs = rhs;
400fbf9f 5692
928c19bb
JM
5693 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5694 {
5695 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5696 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5697 rhs_origtype);
928c19bb
JM
5698 if (inner == error_mark_node)
5699 return error_mark_node;
5700 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5701 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5702 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5703 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5704 protected_set_expr_location (result, location);
5705 return result;
5706 }
5707
3e4093b6
RS
5708 /* If a binary op has been requested, combine the old LHS value with the RHS
5709 producing the value we should actually store into the LHS. */
5710
5711 if (modifycode != NOP_EXPR)
400fbf9f 5712 {
928c19bb 5713 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5714 lhs = stabilize_reference (lhs);
bbbbb16a 5715
267bac10
JM
5716 /* Construct the RHS for any non-atomic compound assignemnt. */
5717 if (!is_atomic_op)
5718 {
241f845a
JJ
5719 /* If in LHS op= RHS the RHS has side-effects, ensure they
5720 are preevaluated before the rest of the assignment expression's
5721 side-effects, because RHS could contain e.g. function calls
5722 that modify LHS. */
5723 if (TREE_SIDE_EFFECTS (rhs))
5724 {
5725 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5726 rhseval = newrhs;
5727 }
267bac10 5728 newrhs = build_binary_op (location,
241f845a 5729 modifycode, lhs, newrhs, 1);
267bac10
JM
5730
5731 /* The original type of the right hand side is no longer
5732 meaningful. */
5733 rhs_origtype = NULL_TREE;
5734 }
400fbf9f 5735 }
400fbf9f 5736
668ea4b1
IS
5737 if (c_dialect_objc ())
5738 {
46a88c12
NP
5739 /* Check if we are modifying an Objective-C property reference;
5740 if so, we need to generate setter calls. */
5741 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5742 if (result)
241f845a 5743 goto return_result;
46a88c12
NP
5744
5745 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5746 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5747 return error_mark_node;
5748 }
5749
9bf24266 5750 /* Give an error for storing in something that is 'const'. */
bbbd6700 5751
f37acdf9 5752 if (TYPE_READONLY (lhstype)
3e636daf 5753 || (RECORD_OR_UNION_TYPE_P (lhstype)
3e4093b6 5754 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5755 {
c02065fc 5756 readonly_error (location, lhs, lv_assign);
953ff289
DN
5757 return error_mark_node;
5758 }
f37acdf9
JM
5759 else if (TREE_READONLY (lhs))
5760 readonly_warning (lhs, lv_assign);
bbbd6700 5761
3e4093b6
RS
5762 /* If storing into a structure or union member,
5763 it has probably been given type `int'.
5764 Compute the type that would go with
5765 the actual amount of storage the member occupies. */
bbbd6700 5766
3e4093b6
RS
5767 if (TREE_CODE (lhs) == COMPONENT_REF
5768 && (TREE_CODE (lhstype) == INTEGER_TYPE
5769 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5770 || TREE_CODE (lhstype) == REAL_TYPE
5771 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5772 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5773
3e4093b6
RS
5774 /* If storing in a field that is in actuality a short or narrower than one,
5775 we must store in the field in its actual type. */
5776
5777 if (lhstype != TREE_TYPE (lhs))
5778 {
5779 lhs = copy_node (lhs);
5780 TREE_TYPE (lhs) = lhstype;
400fbf9f 5781 }
400fbf9f 5782
32e8bb8e
ILT
5783 /* Issue -Wc++-compat warnings about an assignment to an enum type
5784 when LHS does not have its original type. This happens for,
5785 e.g., an enum bitfield in a struct. */
5786 if (warn_cxx_compat
5787 && lhs_origtype != NULL_TREE
5788 && lhs_origtype != lhstype
5789 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5790 {
5791 tree checktype = (rhs_origtype != NULL_TREE
5792 ? rhs_origtype
5793 : TREE_TYPE (rhs));
5794 if (checktype != error_mark_node
267bac10
JM
5795 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5796 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5797 warning_at (location, OPT_Wc___compat,
5798 "enum conversion in assignment is invalid in C++");
5799 }
5800
267bac10
JM
5801 /* If the lhs is atomic, remove that qualifier. */
5802 if (is_atomic_op)
5803 {
5804 lhstype = build_qualified_type (lhstype,
5805 (TYPE_QUALS (lhstype)
5806 & ~TYPE_QUAL_ATOMIC));
5807 olhstype = build_qualified_type (olhstype,
5808 (TYPE_QUALS (lhstype)
5809 & ~TYPE_QUAL_ATOMIC));
5810 }
5811
8ce94e44
JM
5812 /* Convert new value to destination type. Fold it first, then
5813 restore any excess precision information, for the sake of
5814 conversion warnings. */
400fbf9f 5815
267bac10
JM
5816 if (!(is_atomic_op && modifycode != NOP_EXPR))
5817 {
5818 npc = null_pointer_constant_p (newrhs);
5819 newrhs = c_fully_fold (newrhs, false, NULL);
5820 if (rhs_semantic_type)
5821 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5822 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5823 rhs_origtype, ic_assign, npc,
5824 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5825 if (TREE_CODE (newrhs) == ERROR_MARK)
5826 return error_mark_node;
5827 }
400fbf9f 5828
6e955430
ZL
5829 /* Emit ObjC write barrier, if necessary. */
5830 if (c_dialect_objc () && flag_objc_gc)
5831 {
5832 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5833 if (result)
c9f9eb5d
AH
5834 {
5835 protected_set_expr_location (result, location);
241f845a 5836 goto return_result;
c9f9eb5d 5837 }
6e955430
ZL
5838 }
5839
ea4b7848 5840 /* Scan operands. */
400fbf9f 5841
267bac10
JM
5842 if (is_atomic_op)
5843 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5844 else
5845 {
5846 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5847 TREE_SIDE_EFFECTS (result) = 1;
5848 protected_set_expr_location (result, location);
5849 }
400fbf9f 5850
3e4093b6
RS
5851 /* If we got the LHS in a different type for storing in,
5852 convert the result back to the nominal type of LHS
5853 so that the value we return always has the same type
5854 as the LHS argument. */
e855c5ce 5855
3e4093b6 5856 if (olhstype == TREE_TYPE (result))
241f845a 5857 goto return_result;
c9f9eb5d 5858
68fca595
MP
5859 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5860 rhs_origtype, ic_assign, false, NULL_TREE,
5861 NULL_TREE, 0);
c9f9eb5d 5862 protected_set_expr_location (result, location);
241f845a
JJ
5863
5864return_result:
5865 if (rhseval)
5866 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5867 return result;
3e4093b6
RS
5868}
5869\f
478a1c5b
ILT
5870/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5871 This is used to implement -fplan9-extensions. */
5872
5873static bool
5874find_anonymous_field_with_type (tree struct_type, tree type)
5875{
5876 tree field;
5877 bool found;
5878
3e636daf 5879 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
478a1c5b
ILT
5880 found = false;
5881 for (field = TYPE_FIELDS (struct_type);
5882 field != NULL_TREE;
5883 field = TREE_CHAIN (field))
5884 {
267bac10
JM
5885 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5886 ? c_build_qualified_type (TREE_TYPE (field),
5887 TYPE_QUAL_ATOMIC)
5888 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5889 if (DECL_NAME (field) == NULL
267bac10 5890 && comptypes (type, fieldtype))
478a1c5b
ILT
5891 {
5892 if (found)
5893 return false;
5894 found = true;
5895 }
5896 else if (DECL_NAME (field) == NULL
3e636daf 5897 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
478a1c5b
ILT
5898 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5899 {
5900 if (found)
5901 return false;
5902 found = true;
5903 }
5904 }
5905 return found;
5906}
5907
5908/* RHS is an expression whose type is pointer to struct. If there is
5909 an anonymous field in RHS with type TYPE, then return a pointer to
5910 that field in RHS. This is used with -fplan9-extensions. This
5911 returns NULL if no conversion could be found. */
5912
5913static tree
5914convert_to_anonymous_field (location_t location, tree type, tree rhs)
5915{
5916 tree rhs_struct_type, lhs_main_type;
5917 tree field, found_field;
5918 bool found_sub_field;
5919 tree ret;
5920
5921 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5922 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
3e636daf 5923 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
478a1c5b
ILT
5924
5925 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5926 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5927 ? c_build_qualified_type (TREE_TYPE (type),
5928 TYPE_QUAL_ATOMIC)
5929 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5930
5931 found_field = NULL_TREE;
5932 found_sub_field = false;
5933 for (field = TYPE_FIELDS (rhs_struct_type);
5934 field != NULL_TREE;
5935 field = TREE_CHAIN (field))
5936 {
5937 if (DECL_NAME (field) != NULL_TREE
3e636daf 5938 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
478a1c5b 5939 continue;
267bac10
JM
5940 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5941 ? c_build_qualified_type (TREE_TYPE (field),
5942 TYPE_QUAL_ATOMIC)
5943 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5944 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5945 {
5946 if (found_field != NULL_TREE)
5947 return NULL_TREE;
5948 found_field = field;
5949 }
5950 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5951 lhs_main_type))
5952 {
5953 if (found_field != NULL_TREE)
5954 return NULL_TREE;
5955 found_field = field;
5956 found_sub_field = true;
5957 }
5958 }
5959
5960 if (found_field == NULL_TREE)
5961 return NULL_TREE;
5962
5963 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5964 build_fold_indirect_ref (rhs), found_field,
5965 NULL_TREE);
5966 ret = build_fold_addr_expr_loc (location, ret);
5967
5968 if (found_sub_field)
5969 {
5970 ret = convert_to_anonymous_field (location, type, ret);
5971 gcc_assert (ret != NULL_TREE);
5972 }
5973
5974 return ret;
5975}
5976
63bc4e87
MP
5977/* Issue an error message for a bad initializer component.
5978 GMSGID identifies the message.
5979 The component name is taken from the spelling stack. */
5980
5981static void
ea58ef42 5982error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5983{
5984 char *ofwhat;
5985
5986 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5987 error_at (loc, gmsgid);
63bc4e87
MP
5988 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5989 if (*ofwhat)
d7ff7ae5 5990 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5991}
5992
5993/* Issue a pedantic warning for a bad initializer component. OPT is
5994 the option OPT_* (from options.h) controlling this warning or 0 if
5995 it is unconditionally given. GMSGID identifies the message. The
5996 component name is taken from the spelling stack. */
5997
5998static void
79063edd 5999pedwarn_init (location_t loc, int opt, const char *gmsgid)
63bc4e87
MP
6000{
6001 char *ofwhat;
d7ff7ae5 6002 bool warned;
63bc4e87 6003
79063edd
MS
6004 /* Use the location where a macro was expanded rather than where
6005 it was defined to make sure macros defined in system headers
6006 but used incorrectly elsewhere are diagnosed. */
6007 source_location exploc = expansion_point_location_if_in_system_header (loc);
6008
63bc4e87 6009 /* The gmsgid may be a format string with %< and %>. */
79063edd 6010 warned = pedwarn (exploc, opt, gmsgid);
63bc4e87 6011 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5 6012 if (*ofwhat && warned)
79063edd 6013 inform (exploc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
6014}
6015
6016/* Issue a warning for a bad initializer component.
6017
6018 OPT is the OPT_W* value corresponding to the warning option that
6019 controls this warning. GMSGID identifies the message. The
6020 component name is taken from the spelling stack. */
6021
6022static void
6023warning_init (location_t loc, int opt, const char *gmsgid)
6024{
6025 char *ofwhat;
d7ff7ae5 6026 bool warned;
63bc4e87 6027
79063edd
MS
6028 /* Use the location where a macro was expanded rather than where
6029 it was defined to make sure macros defined in system headers
6030 but used incorrectly elsewhere are diagnosed. */
6031 source_location exploc = expansion_point_location_if_in_system_header (loc);
6032
63bc4e87 6033 /* The gmsgid may be a format string with %< and %>. */
79063edd 6034 warned = warning_at (exploc, opt, gmsgid);
63bc4e87 6035 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5 6036 if (*ofwhat && warned)
79063edd 6037 inform (exploc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
6038}
6039\f
6040/* If TYPE is an array type and EXPR is a parenthesized string
6041 constant, warn if pedantic that EXPR is being used to initialize an
6042 object of type TYPE. */
6043
6044void
d033409e 6045maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
6046{
6047 if (pedantic
6048 && TREE_CODE (type) == ARRAY_TYPE
6049 && TREE_CODE (expr.value) == STRING_CST
6050 && expr.original_code != STRING_CST)
d033409e 6051 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
6052 "array initialized from parenthesized string constant");
6053}
6054
bbbbb16a
ILT
6055/* Convert value RHS to type TYPE as preparation for an assignment to
6056 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6057 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6058 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6059 constant before any folding.
3e4093b6
RS
6060 The real work of conversion is done by `convert'.
6061 The purpose of this function is to generate error messages
6062 for assignments that are not allowed in C.
2ac2f164
JM
6063 ERRTYPE says whether it is argument passing, assignment,
6064 initialization or return.
2f6e4e97 6065
aabef2de
MP
6066 In the following example, '~' denotes where EXPR_LOC and '^' where
6067 LOCATION point to:
6068
6069 f (var); [ic_argpass]
6070 ^ ~~~
6071 x = var; [ic_assign]
6072 ^ ~~~;
6073 int x = var; [ic_init]
6074 ^^^
6075 return x; [ic_return]
6076 ^
6077
2ac2f164 6078 FUNCTION is a tree for the function being called.
3e4093b6 6079 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 6080
3e4093b6 6081static tree
81e5eca8 6082convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 6083 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
6084 bool null_pointer_constant, tree fundecl,
6085 tree function, int parmnum)
3e4093b6
RS
6086{
6087 enum tree_code codel = TREE_CODE (type);
8ce94e44 6088 tree orig_rhs = rhs;
3e4093b6
RS
6089 tree rhstype;
6090 enum tree_code coder;
2ac2f164 6091 tree rname = NULL_TREE;
58393038 6092 bool objc_ok = false;
2ac2f164 6093
1c7485af
MP
6094 /* Use the expansion point location to handle cases such as user's
6095 function returning a wrong-type macro defined in a system header. */
6096 location = expansion_point_location_if_in_system_header (location);
6097
6b4ef5c1 6098 if (errtype == ic_argpass)
2ac2f164
JM
6099 {
6100 tree selector;
6101 /* Change pointer to function to the function itself for
6102 diagnostics. */
6103 if (TREE_CODE (function) == ADDR_EXPR
6104 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6105 function = TREE_OPERAND (function, 0);
6106
6107 /* Handle an ObjC selector specially for diagnostics. */
6108 selector = objc_message_selector ();
6109 rname = function;
6110 if (selector && parmnum > 2)
6111 {
6112 rname = selector;
6113 parmnum -= 2;
6114 }
6115 }
6116
6117 /* This macro is used to emit diagnostics to ensure that all format
6118 strings are complete sentences, visible to gettext and checked at
6119 compile time. */
768952be 6120#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
6121 do { \
6122 switch (errtype) \
6123 { \
6124 case ic_argpass: \
5c1bc275 6125 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 6126 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 6127 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
6128 "expected %qT but argument is of type %qT", \
6129 type, rhstype); \
6130 break; \
1e053dfe
MLI
6131 case ic_assign: \
6132 pedwarn (LOCATION, OPT, AS); \
6133 break; \
6134 case ic_init: \
6a8f4e12 6135 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
6136 break; \
6137 case ic_return: \
d033409e 6138 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
6139 break; \
6140 default: \
6141 gcc_unreachable (); \
6142 } \
2ac2f164 6143 } while (0)
cb3ca04e 6144
49706e39
MLI
6145 /* This macro is used to emit diagnostics to ensure that all format
6146 strings are complete sentences, visible to gettext and checked at
768952be 6147 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 6148 extra parameter to enumerate qualifiers. */
768952be 6149#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
6150 do { \
6151 switch (errtype) \
6152 { \
6153 case ic_argpass: \
5c1bc275 6154 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 6155 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 6156 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
6157 "expected %qT but argument is of type %qT", \
6158 type, rhstype); \
6159 break; \
6160 case ic_assign: \
5c1bc275 6161 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
6162 break; \
6163 case ic_init: \
5c1bc275 6164 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
6165 break; \
6166 case ic_return: \
5c1bc275 6167 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
6168 break; \
6169 default: \
6170 gcc_unreachable (); \
6171 } \
6172 } while (0)
6173
768952be
MU
6174 /* This macro is used to emit diagnostics to ensure that all format
6175 strings are complete sentences, visible to gettext and checked at
6176 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6177 warning_at instead of pedwarn. */
6178#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6179 do { \
6180 switch (errtype) \
6181 { \
6182 case ic_argpass: \
6183 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6184 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6185 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6186 "expected %qT but argument is of type %qT", \
6187 type, rhstype); \
6188 break; \
6189 case ic_assign: \
6190 warning_at (LOCATION, OPT, AS, QUALS); \
6191 break; \
6192 case ic_init: \
6193 warning_at (LOCATION, OPT, IN, QUALS); \
6194 break; \
6195 case ic_return: \
6196 warning_at (LOCATION, OPT, RE, QUALS); \
6197 break; \
6198 default: \
6199 gcc_unreachable (); \
6200 } \
6201 } while (0)
6202
8ce94e44
JM
6203 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6204 rhs = TREE_OPERAND (rhs, 0);
6205
3e4093b6
RS
6206 rhstype = TREE_TYPE (rhs);
6207 coder = TREE_CODE (rhstype);
6208
6209 if (coder == ERROR_MARK)
6210 return error_mark_node;
6211
58393038
ZL
6212 if (c_dialect_objc ())
6213 {
6214 int parmno;
6215
6216 switch (errtype)
6217 {
6218 case ic_return:
6219 parmno = 0;
6220 break;
6221
6222 case ic_assign:
6223 parmno = -1;
6224 break;
6225
6226 case ic_init:
6227 parmno = -2;
6228 break;
6229
6230 default:
6231 parmno = parmnum;
6232 break;
6233 }
6234
6235 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6236 }
6237
bbbbb16a
ILT
6238 if (warn_cxx_compat)
6239 {
6240 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6241 if (checktype != error_mark_node
6242 && TREE_CODE (type) == ENUMERAL_TYPE
6243 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6244 {
768952be
MU
6245 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6246 G_("enum conversion when passing argument "
6247 "%d of %qE is invalid in C++"),
6248 G_("enum conversion in assignment is "
6249 "invalid in C++"),
6250 G_("enum conversion in initialization is "
6251 "invalid in C++"),
6252 G_("enum conversion in return is "
6253 "invalid in C++"));
bbbbb16a
ILT
6254 }
6255 }
6256
3e4093b6 6257 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 6258 return rhs;
3e4093b6
RS
6259
6260 if (coder == VOID_TYPE)
400fbf9f 6261 {
6dcc04b0
JM
6262 /* Except for passing an argument to an unprototyped function,
6263 this is a constraint violation. When passing an argument to
6264 an unprototyped function, it is compile-time undefined;
6265 making it a constraint in that case was rejected in
6266 DR#252. */
c2255bc4 6267 error_at (location, "void value not ignored as it ought to be");
3e4093b6 6268 return error_mark_node;
400fbf9f 6269 }
4f2e1536 6270 rhs = require_complete_type (location, rhs);
808d6eaa
JM
6271 if (rhs == error_mark_node)
6272 return error_mark_node;
1807ffc1
MS
6273
6274 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6275 return error_mark_node;
6276
cd192ccc
MS
6277 /* A non-reference type can convert to a reference. This handles
6278 va_start, va_copy and possibly port built-ins. */
6279 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 6280 {
3e4093b6 6281 if (!lvalue_p (rhs))
400fbf9f 6282 {
c2255bc4 6283 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 6284 return error_mark_node;
400fbf9f 6285 }
3e4093b6
RS
6286 if (!c_mark_addressable (rhs))
6287 return error_mark_node;
6288 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 6289 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6290
81e5eca8 6291 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
6292 build_pointer_type (TREE_TYPE (type)),
6293 rhs, origtype, errtype,
6294 null_pointer_constant, fundecl, function,
6295 parmnum);
cd192ccc
MS
6296 if (rhs == error_mark_node)
6297 return error_mark_node;
3e4093b6
RS
6298
6299 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 6300 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6301 return rhs;
400fbf9f 6302 }
3e4093b6 6303 /* Some types can interconvert without explicit casts. */
3274deff 6304 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 6305 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
6306 return convert (type, rhs);
6307 /* Arithmetic types all interconvert, and enum is treated like int. */
6308 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 6309 || codel == FIXED_POINT_TYPE
3e4093b6
RS
6310 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6311 || codel == BOOLEAN_TYPE)
6312 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 6313 || coder == FIXED_POINT_TYPE
3e4093b6
RS
6314 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6315 || coder == BOOLEAN_TYPE))
928c19bb
JM
6316 {
6317 tree ret;
6318 bool save = in_late_binary_op;
e5341100
JJ
6319 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6320 || (coder == REAL_TYPE
6321 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6322 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
928c19bb 6323 in_late_binary_op = true;
81e5eca8
MP
6324 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6325 ? expr_loc : location, type, orig_rhs);
e5341100 6326 in_late_binary_op = save;
928c19bb
JM
6327 return ret;
6328 }
400fbf9f 6329
79077aea
JJ
6330 /* Aggregates in different TUs might need conversion. */
6331 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6332 && codel == coder
6333 && comptypes (type, rhstype))
81e5eca8
MP
6334 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6335 ? expr_loc : location, type, rhs);
79077aea 6336
ebf0bf7f 6337 /* Conversion to a transparent union or record from its member types.
3e4093b6 6338 This applies only to function arguments. */
ebf0bf7f
JJ
6339 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6340 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 6341 && errtype == ic_argpass)
400fbf9f 6342 {
0257e383 6343 tree memb, marginal_memb = NULL_TREE;
3e4093b6 6344
910ad8de 6345 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 6346 {
0257e383 6347 tree memb_type = TREE_TYPE (memb);
400fbf9f 6348
3e4093b6 6349 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 6350 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 6351 break;
e58cd767 6352
3e4093b6
RS
6353 if (TREE_CODE (memb_type) != POINTER_TYPE)
6354 continue;
2f6e4e97 6355
3e4093b6
RS
6356 if (coder == POINTER_TYPE)
6357 {
6358 tree ttl = TREE_TYPE (memb_type);
6359 tree ttr = TREE_TYPE (rhstype);
400fbf9f 6360
3e4093b6
RS
6361 /* Any non-function converts to a [const][volatile] void *
6362 and vice versa; otherwise, targets must be the same.
6363 Meanwhile, the lhs target must have all the qualifiers of
6364 the rhs. */
267bac10
JM
6365 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6366 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6367 || comp_target_types (location, memb_type, rhstype))
3e4093b6 6368 {
267bac10
JM
6369 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6370 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 6371 /* If this type won't generate any warnings, use it. */
267bac10 6372 if (lquals == rquals
3e4093b6
RS
6373 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6374 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
6375 ? ((lquals | rquals) == rquals)
6376 : ((lquals | rquals) == lquals)))
3e4093b6 6377 break;
400fbf9f 6378
3e4093b6 6379 /* Keep looking for a better type, but remember this one. */
0257e383
RH
6380 if (!marginal_memb)
6381 marginal_memb = memb;
3e4093b6
RS
6382 }
6383 }
82bde854 6384
3e4093b6 6385 /* Can convert integer zero to any pointer type. */
928c19bb 6386 if (null_pointer_constant)
3e4093b6
RS
6387 {
6388 rhs = null_pointer_node;
6389 break;
6390 }
6391 }
400fbf9f 6392
0257e383 6393 if (memb || marginal_memb)
3e4093b6 6394 {
0257e383 6395 if (!memb)
3e4093b6
RS
6396 {
6397 /* We have only a marginally acceptable member type;
6398 it needs a warning. */
0257e383 6399 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 6400 tree ttr = TREE_TYPE (rhstype);
714a0864 6401
3e4093b6
RS
6402 /* Const and volatile mean something different for function
6403 types, so the usual warnings are not appropriate. */
6404 if (TREE_CODE (ttr) == FUNCTION_TYPE
6405 && TREE_CODE (ttl) == FUNCTION_TYPE)
6406 {
6407 /* Because const and volatile on functions are
6408 restrictions that say the function will not do
6409 certain things, it is okay to use a const or volatile
6410 function where an ordinary one is wanted, but not
6411 vice-versa. */
36c5e70a
BE
6412 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6413 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6414 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6415 OPT_Wdiscarded_qualifiers,
6416 G_("passing argument %d of %qE "
6417 "makes %q#v qualified function "
6418 "pointer from unqualified"),
6419 G_("assignment makes %q#v qualified "
6420 "function pointer from "
6421 "unqualified"),
6422 G_("initialization makes %q#v qualified "
6423 "function pointer from "
6424 "unqualified"),
6425 G_("return makes %q#v qualified function "
6426 "pointer from unqualified"),
6427 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6428 }
36c5e70a
BE
6429 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6430 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6431 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6432 OPT_Wdiscarded_qualifiers,
6433 G_("passing argument %d of %qE discards "
6434 "%qv qualifier from pointer target type"),
6435 G_("assignment discards %qv qualifier "
6436 "from pointer target type"),
6437 G_("initialization discards %qv qualifier "
6438 "from pointer target type"),
6439 G_("return discards %qv qualifier from "
6440 "pointer target type"),
6441 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6442
6443 memb = marginal_memb;
3e4093b6 6444 }
400fbf9f 6445
fcf73884 6446 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6447 pedwarn (location, OPT_Wpedantic,
fcf73884 6448 "ISO C prohibits argument conversion to union type");
0e7c47fa 6449
db3927fb 6450 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6451 return build_constructor_single (type, memb, rhs);
3e4093b6 6452 }
0e7c47fa
RK
6453 }
6454
3e4093b6
RS
6455 /* Conversions among pointers */
6456 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6457 && (coder == codel))
400fbf9f 6458 {
3e4093b6
RS
6459 tree ttl = TREE_TYPE (type);
6460 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6461 tree mvl = ttl;
6462 tree mvr = ttr;
3e4093b6 6463 bool is_opaque_pointer;
264fa2db 6464 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6465 addr_space_t asl;
6466 addr_space_t asr;
400fbf9f 6467
46df2823 6468 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6469 mvl = (TYPE_ATOMIC (mvl)
6470 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6471 TYPE_QUAL_ATOMIC)
6472 : TYPE_MAIN_VARIANT (mvl));
46df2823 6473 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6474 mvr = (TYPE_ATOMIC (mvr)
6475 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6476 TYPE_QUAL_ATOMIC)
6477 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6478 /* Opaque pointers are treated like void pointers. */
f83c7f63 6479 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6480
478a1c5b
ILT
6481 /* The Plan 9 compiler permits a pointer to a struct to be
6482 automatically converted into a pointer to an anonymous field
6483 within the struct. */
6484 if (flag_plan9_extensions
3e636daf
MP
6485 && RECORD_OR_UNION_TYPE_P (mvl)
6486 && RECORD_OR_UNION_TYPE_P (mvr)
478a1c5b
ILT
6487 && mvl != mvr)
6488 {
6489 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6490 if (new_rhs != NULL_TREE)
6491 {
6492 rhs = new_rhs;
6493 rhstype = TREE_TYPE (rhs);
6494 coder = TREE_CODE (rhstype);
6495 ttr = TREE_TYPE (rhstype);
6496 mvr = TYPE_MAIN_VARIANT (ttr);
6497 }
6498 }
6499
b7e20b53 6500 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6501 for the purpose of reducing the number of false positives, we
6502 tolerate the special case of
b7e20b53 6503
c22cacf3 6504 int *p = NULL;
b7e20b53 6505
c22cacf3 6506 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6507 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6508 warning_at (errtype == ic_argpass ? expr_loc : location,
6509 OPT_Wc___compat,
6510 "request for implicit conversion "
c2255bc4 6511 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6512
36c5e70a
BE
6513 /* See if the pointers point to incompatible address spaces. */
6514 asl = TYPE_ADDR_SPACE (ttl);
6515 asr = TYPE_ADDR_SPACE (ttr);
6516 if (!null_pointer_constant_p (rhs)
6517 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6518 {
6519 switch (errtype)
6520 {
6521 case ic_argpass:
8ffcdea8 6522 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6523 "non-enclosed address space", parmnum, rname);
6524 break;
6525 case ic_assign:
6526 error_at (location, "assignment from pointer to "
6527 "non-enclosed address space");
6528 break;
6529 case ic_init:
6530 error_at (location, "initialization from pointer to "
6531 "non-enclosed address space");
6532 break;
6533 case ic_return:
6534 error_at (location, "return from pointer to "
6535 "non-enclosed address space");
6536 break;
6537 default:
6538 gcc_unreachable ();
6539 }
6540 return error_mark_node;
6541 }
6542
7876a414
KG
6543 /* Check if the right-hand side has a format attribute but the
6544 left-hand side doesn't. */
90137d8f 6545 if (warn_suggest_attribute_format
104f8784 6546 && check_missing_format_attribute (type, rhstype))
c22cacf3 6547 {
104f8784
KG
6548 switch (errtype)
6549 {
6550 case ic_argpass:
8ffcdea8 6551 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6552 "argument %d of %qE might be "
6553 "a candidate for a format attribute",
6554 parmnum, rname);
104f8784
KG
6555 break;
6556 case ic_assign:
90137d8f 6557 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6558 "assignment left-hand side might be "
6559 "a candidate for a format attribute");
104f8784
KG
6560 break;
6561 case ic_init:
90137d8f 6562 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6563 "initialization left-hand side might be "
6564 "a candidate for a format attribute");
104f8784
KG
6565 break;
6566 case ic_return:
90137d8f 6567 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6568 "return type might be "
6569 "a candidate for a format attribute");
104f8784
KG
6570 break;
6571 default:
6572 gcc_unreachable ();
6573 }
7876a414 6574 }
c22cacf3 6575
3e4093b6
RS
6576 /* Any non-function converts to a [const][volatile] void *
6577 and vice versa; otherwise, targets must be the same.
6578 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6579 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6580 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6581 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6582 || is_opaque_pointer
f8a93a2e
JJ
6583 || ((c_common_unsigned_type (mvl)
6584 == c_common_unsigned_type (mvr))
267bac10
JM
6585 && (c_common_signed_type (mvl)
6586 == c_common_signed_type (mvr))
6587 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6588 {
768952be
MU
6589 /* Warn about loss of qualifers from pointers to arrays with
6590 qualifiers on the element type. */
6591 if (TREE_CODE (ttr) == ARRAY_TYPE)
6592 {
6593 ttr = strip_array_types (ttr);
6594 ttl = strip_array_types (ttl);
6595
6596 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6597 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6598 WARNING_FOR_QUALIFIERS (location, expr_loc,
6599 OPT_Wdiscarded_array_qualifiers,
6600 G_("passing argument %d of %qE discards "
6601 "%qv qualifier from pointer target type"),
6602 G_("assignment discards %qv qualifier "
6603 "from pointer target type"),
6604 G_("initialization discards %qv qualifier "
6605 "from pointer target type"),
6606 G_("return discards %qv qualifier from "
6607 "pointer target type"),
6608 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6609 }
6610 else if (pedantic
3e4093b6
RS
6611 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6612 ||
6613 (VOID_TYPE_P (ttr)
928c19bb 6614 && !null_pointer_constant
3e4093b6 6615 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6616 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6617 G_("ISO C forbids passing argument %d of "
6618 "%qE between function pointer "
6619 "and %<void *%>"),
6620 G_("ISO C forbids assignment between "
6621 "function pointer and %<void *%>"),
6622 G_("ISO C forbids initialization between "
6623 "function pointer and %<void *%>"),
6624 G_("ISO C forbids return between function "
6625 "pointer and %<void *%>"));
3e4093b6
RS
6626 /* Const and volatile mean something different for function types,
6627 so the usual warnings are not appropriate. */
6628 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6629 && TREE_CODE (ttl) != FUNCTION_TYPE)
6630 {
768952be
MU
6631 /* Don't warn about loss of qualifier for conversions from
6632 qualified void* to pointers to arrays with corresponding
6633 qualifier on the element type. */
6634 if (!pedantic)
6635 ttl = strip_array_types (ttl);
6636
267bac10
JM
6637 /* Assignments between atomic and non-atomic objects are OK. */
6638 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6639 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6640 {
768952be
MU
6641 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6642 OPT_Wdiscarded_qualifiers,
6643 G_("passing argument %d of %qE discards "
6644 "%qv qualifier from pointer target type"),
6645 G_("assignment discards %qv qualifier "
6646 "from pointer target type"),
6647 G_("initialization discards %qv qualifier "
6648 "from pointer target type"),
6649 G_("return discards %qv qualifier from "
6650 "pointer target type"),
6651 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6652 }
3e4093b6
RS
6653 /* If this is not a case of ignoring a mismatch in signedness,
6654 no warning. */
6655 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6656 || target_cmp)
3e4093b6
RS
6657 ;
6658 /* If there is a mismatch, do warn. */
f2fd3821 6659 else if (warn_pointer_sign)
768952be
MU
6660 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6661 G_("pointer targets in passing argument "
6662 "%d of %qE differ in signedness"),
6663 G_("pointer targets in assignment "
6664 "differ in signedness"),
6665 G_("pointer targets in initialization "
6666 "differ in signedness"),
6667 G_("pointer targets in return differ "
6668 "in signedness"));
3e4093b6
RS
6669 }
6670 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6671 && TREE_CODE (ttr) == FUNCTION_TYPE)
6672 {
6673 /* Because const and volatile on functions are restrictions
6674 that say the function will not do certain things,
6675 it is okay to use a const or volatile function
6676 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6677 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6678 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6679 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6680 OPT_Wdiscarded_qualifiers,
6681 G_("passing argument %d of %qE makes "
6682 "%q#v qualified function pointer "
6683 "from unqualified"),
6684 G_("assignment makes %q#v qualified function "
6685 "pointer from unqualified"),
6686 G_("initialization makes %q#v qualified "
6687 "function pointer from unqualified"),
6688 G_("return makes %q#v qualified function "
6689 "pointer from unqualified"),
6690 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6691 }
6692 }
6693 else
58393038
ZL
6694 /* Avoid warning about the volatile ObjC EH puts on decls. */
6695 if (!objc_ok)
768952be
MU
6696 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6697 OPT_Wincompatible_pointer_types,
6698 G_("passing argument %d of %qE from "
6699 "incompatible pointer type"),
6700 G_("assignment from incompatible pointer type"),
6701 G_("initialization from incompatible "
6702 "pointer type"),
6703 G_("return from incompatible pointer type"));
58393038 6704
3e4093b6
RS
6705 return convert (type, rhs);
6706 }
b494fd98
EB
6707 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6708 {
6dcc04b0
JM
6709 /* ??? This should not be an error when inlining calls to
6710 unprototyped functions. */
c2255bc4 6711 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6712 return error_mark_node;
6713 }
3e4093b6 6714 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6715 {
3e4093b6
RS
6716 /* An explicit constant 0 can convert to a pointer,
6717 or one that results from arithmetic, even including
6718 a cast to integer type. */
928c19bb 6719 if (!null_pointer_constant)
768952be
MU
6720 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6721 OPT_Wint_conversion,
6722 G_("passing argument %d of %qE makes "
6723 "pointer from integer without a cast"),
6724 G_("assignment makes pointer from integer "
6725 "without a cast"),
6726 G_("initialization makes pointer from "
6727 "integer without a cast"),
6728 G_("return makes pointer from integer "
6729 "without a cast"));
b3006337
EB
6730
6731 return convert (type, rhs);
400fbf9f 6732 }
3e4093b6 6733 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6734 {
768952be
MU
6735 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6736 OPT_Wint_conversion,
6737 G_("passing argument %d of %qE makes integer "
6738 "from pointer without a cast"),
6739 G_("assignment makes integer from pointer "
6740 "without a cast"),
6741 G_("initialization makes integer from pointer "
6742 "without a cast"),
6743 G_("return makes integer from pointer "
6744 "without a cast"));
3e4093b6
RS
6745 return convert (type, rhs);
6746 }
6747 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6748 {
6749 tree ret;
6750 bool save = in_late_binary_op;
6751 in_late_binary_op = true;
6752 ret = convert (type, rhs);
6753 in_late_binary_op = save;
6754 return ret;
6755 }
400fbf9f 6756
2ac2f164 6757 switch (errtype)
3e4093b6 6758 {
2ac2f164 6759 case ic_argpass:
8ffcdea8
MP
6760 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6761 rname);
a7da8b42 6762 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6763 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6764 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6765 break;
6766 case ic_assign:
c2255bc4
AH
6767 error_at (location, "incompatible types when assigning to type %qT from "
6768 "type %qT", type, rhstype);
2ac2f164
JM
6769 break;
6770 case ic_init:
c2255bc4 6771 error_at (location,
8ffcdea8 6772 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6773 type, rhstype);
2ac2f164
JM
6774 break;
6775 case ic_return:
c2255bc4 6776 error_at (location,
8ffcdea8 6777 "incompatible types when returning type %qT but %qT was "
c2255bc4 6778 "expected", rhstype, type);
2ac2f164
JM
6779 break;
6780 default:
6781 gcc_unreachable ();
400fbf9f 6782 }
53b01f59 6783
3e4093b6
RS
6784 return error_mark_node;
6785}
3e4093b6
RS
6786\f
6787/* If VALUE is a compound expr all of whose expressions are constant, then
6788 return its value. Otherwise, return error_mark_node.
15b732b2 6789
3e4093b6
RS
6790 This is for handling COMPOUND_EXPRs as initializer elements
6791 which is allowed with a warning when -pedantic is specified. */
15b732b2 6792
3e4093b6
RS
6793static tree
6794valid_compound_expr_initializer (tree value, tree endtype)
6795{
6796 if (TREE_CODE (value) == COMPOUND_EXPR)
6797 {
6798 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6799 == error_mark_node)
6800 return error_mark_node;
6801 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6802 endtype);
6803 }
116df786 6804 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6805 return error_mark_node;
6806 else
6807 return value;
15b732b2 6808}
400fbf9f 6809\f
3e4093b6
RS
6810/* Perform appropriate conversions on the initial value of a variable,
6811 store it in the declaration DECL,
6812 and print any error messages that are appropriate.
bbbbb16a 6813 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6814 If the init is invalid, store an ERROR_MARK.
6815
6816 INIT_LOC is the location of the initial value. */
400fbf9f 6817
3e4093b6 6818void
c2255bc4 6819store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6820{
3e4093b6 6821 tree value, type;
928c19bb 6822 bool npc = false;
400fbf9f 6823
3e4093b6 6824 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6825
3e4093b6
RS
6826 type = TREE_TYPE (decl);
6827 if (TREE_CODE (type) == ERROR_MARK)
6828 return;
400fbf9f 6829
3e4093b6 6830 /* Digest the specified initializer into an expression. */
400fbf9f 6831
928c19bb
JM
6832 if (init)
6833 npc = null_pointer_constant_p (init);
c2255bc4
AH
6834 value = digest_init (init_loc, type, init, origtype, npc,
6835 true, TREE_STATIC (decl));
400fbf9f 6836
3e4093b6 6837 /* Store the expression if valid; else report error. */
400fbf9f 6838
8400e75e 6839 if (!in_system_header_at (input_location)
3f75a254 6840 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6841 warning (OPT_Wtraditional, "traditional C rejects automatic "
6842 "aggregate initialization");
2f6e4e97 6843
dea63e49
JJ
6844 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6845 DECL_INITIAL (decl) = value;
400fbf9f 6846
3e4093b6
RS
6847 /* ANSI wants warnings about out-of-range constant initializers. */
6848 STRIP_TYPE_NOPS (value);
b8698a0f 6849 if (TREE_STATIC (decl))
c2658540 6850 constant_expression_warning (value);
400fbf9f 6851
3e4093b6
RS
6852 /* Check if we need to set array size from compound literal size. */
6853 if (TREE_CODE (type) == ARRAY_TYPE
6854 && TYPE_DOMAIN (type) == 0
6855 && value != error_mark_node)
400fbf9f 6856 {
3e4093b6
RS
6857 tree inside_init = init;
6858
ed248cf7 6859 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6860 inside_init = fold (inside_init);
6861
6862 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6863 {
8d9f82d5 6864 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6865
8d9f82d5 6866 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6867 {
6868 /* For int foo[] = (int [3]){1}; we need to set array size
6869 now since later on array initializer will be just the
6870 brace enclosed list of the compound literal. */
e30ecc5d 6871 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6872 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6873 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6874 layout_type (type);
8d9f82d5 6875 layout_decl (cldecl, 0);
e30ecc5d
JJ
6876 TREE_TYPE (decl)
6877 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6878 }
6879 }
400fbf9f 6880 }
3e4093b6
RS
6881}
6882\f
6883/* Methods for storing and printing names for error messages. */
400fbf9f 6884
3e4093b6
RS
6885/* Implement a spelling stack that allows components of a name to be pushed
6886 and popped. Each element on the stack is this structure. */
400fbf9f 6887
3e4093b6
RS
6888struct spelling
6889{
6890 int kind;
6891 union
400fbf9f 6892 {
a0f0ab9f 6893 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6894 const char *s;
6895 } u;
6896};
2f6e4e97 6897
3e4093b6
RS
6898#define SPELLING_STRING 1
6899#define SPELLING_MEMBER 2
6900#define SPELLING_BOUNDS 3
400fbf9f 6901
3e4093b6
RS
6902static struct spelling *spelling; /* Next stack element (unused). */
6903static struct spelling *spelling_base; /* Spelling stack base. */
6904static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6905
3e4093b6
RS
6906/* Macros to save and restore the spelling stack around push_... functions.
6907 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6908
3e4093b6
RS
6909#define SPELLING_DEPTH() (spelling - spelling_base)
6910#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6911
3e4093b6
RS
6912/* Push an element on the spelling stack with type KIND and assign VALUE
6913 to MEMBER. */
400fbf9f 6914
3e4093b6
RS
6915#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6916{ \
6917 int depth = SPELLING_DEPTH (); \
6918 \
6919 if (depth >= spelling_size) \
6920 { \
6921 spelling_size += 10; \
cca8ead2
BI
6922 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6923 spelling_size); \
3e4093b6
RS
6924 RESTORE_SPELLING_DEPTH (depth); \
6925 } \
6926 \
6927 spelling->kind = (KIND); \
6928 spelling->MEMBER = (VALUE); \
6929 spelling++; \
6930}
400fbf9f 6931
3e4093b6 6932/* Push STRING on the stack. Printed literally. */
400fbf9f 6933
3e4093b6
RS
6934static void
6935push_string (const char *string)
6936{
6937 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6938}
400fbf9f 6939
3e4093b6 6940/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6941
3e4093b6
RS
6942static void
6943push_member_name (tree decl)
6944{
6945 const char *const string
88388a52
JM
6946 = (DECL_NAME (decl)
6947 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6948 : _("<anonymous>"));
3e4093b6
RS
6949 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6950}
400fbf9f 6951
3e4093b6 6952/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6953
3e4093b6 6954static void
a0f0ab9f 6955push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6956{
6957 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6958}
bb58bec5 6959
3e4093b6 6960/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6961
3e4093b6
RS
6962static int
6963spelling_length (void)
6964{
6965 int size = 0;
6966 struct spelling *p;
400fbf9f 6967
3e4093b6
RS
6968 for (p = spelling_base; p < spelling; p++)
6969 {
6970 if (p->kind == SPELLING_BOUNDS)
6971 size += 25;
6972 else
6973 size += strlen (p->u.s) + 1;
6974 }
6975
6976 return size;
400fbf9f 6977}
400fbf9f 6978
3e4093b6 6979/* Print the spelling to BUFFER and return it. */
400fbf9f 6980
3e4093b6
RS
6981static char *
6982print_spelling (char *buffer)
400fbf9f 6983{
3e4093b6
RS
6984 char *d = buffer;
6985 struct spelling *p;
400fbf9f 6986
3e4093b6
RS
6987 for (p = spelling_base; p < spelling; p++)
6988 if (p->kind == SPELLING_BOUNDS)
6989 {
a0f0ab9f 6990 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6991 d += strlen (d);
6992 }
6993 else
6994 {
6995 const char *s;
6996 if (p->kind == SPELLING_MEMBER)
6997 *d++ = '.';
6998 for (s = p->u.s; (*d = *s++); d++)
6999 ;
7000 }
7001 *d++ = '\0';
7002 return buffer;
7003}
400fbf9f 7004
3e4093b6
RS
7005/* Digest the parser output INIT as an initializer for type TYPE.
7006 Return a C expression of type TYPE to represent the initial value.
7e842ef8 7007
bbbbb16a
ILT
7008 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7009
928c19bb
JM
7010 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7011
916c5919
JM
7012 If INIT is a string constant, STRICT_STRING is true if it is
7013 unparenthesized or we should not warn here for it being parenthesized.
7014 For other types of INIT, STRICT_STRING is not used.
7015
c2255bc4
AH
7016 INIT_LOC is the location of the INIT.
7017
3e4093b6
RS
7018 REQUIRE_CONSTANT requests an error if non-constant initializers or
7019 elements are seen. */
7e842ef8 7020
3e4093b6 7021static tree
c2255bc4
AH
7022digest_init (location_t init_loc, tree type, tree init, tree origtype,
7023 bool null_pointer_constant, bool strict_string,
7024 int require_constant)
3e4093b6
RS
7025{
7026 enum tree_code code = TREE_CODE (type);
7027 tree inside_init = init;
8ce94e44 7028 tree semantic_type = NULL_TREE;
928c19bb 7029 bool maybe_const = true;
7e842ef8 7030
3e4093b6 7031 if (type == error_mark_node
f01da1a5 7032 || !init
7a0ca710 7033 || error_operand_p (init))
3e4093b6 7034 return error_mark_node;
7e842ef8 7035
ed248cf7 7036 STRIP_TYPE_NOPS (inside_init);
7e842ef8 7037
8ce94e44
JM
7038 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7039 {
7040 semantic_type = TREE_TYPE (inside_init);
7041 inside_init = TREE_OPERAND (inside_init, 0);
7042 }
928c19bb
JM
7043 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7044 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 7045
3e4093b6
RS
7046 /* Initialization of an array of chars from a string constant
7047 optionally enclosed in braces. */
7e842ef8 7048
197463ae
JM
7049 if (code == ARRAY_TYPE && inside_init
7050 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 7051 {
267bac10
JM
7052 tree typ1
7053 = (TYPE_ATOMIC (TREE_TYPE (type))
7054 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7055 TYPE_QUAL_ATOMIC)
7056 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
7057 /* Note that an array could be both an array of character type
7058 and an array of wchar_t if wchar_t is signed char or unsigned
7059 char. */
7060 bool char_array = (typ1 == char_type_node
7061 || typ1 == signed_char_type_node
7062 || typ1 == unsigned_char_type_node);
7063 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
7064 bool char16_array = !!comptypes (typ1, char16_type_node);
7065 bool char32_array = !!comptypes (typ1, char32_type_node);
7066
7067 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 7068 {
916c5919 7069 struct c_expr expr;
c466b2cd 7070 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
7071 expr.value = inside_init;
7072 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 7073 expr.original_type = NULL;
d033409e 7074 maybe_warn_string_init (init_loc, type, expr);
916c5919 7075
a45e580b 7076 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 7077 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
7078 "initialization of a flexible array member");
7079
3e4093b6 7080 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 7081 TYPE_MAIN_VARIANT (type)))
3e4093b6 7082 return inside_init;
7e842ef8 7083
c466b2cd 7084 if (char_array)
3e4093b6 7085 {
c466b2cd
KVH
7086 if (typ2 != char_type_node)
7087 {
ea58ef42
MP
7088 error_init (init_loc, "char-array initialized from wide "
7089 "string");
c466b2cd
KVH
7090 return error_mark_node;
7091 }
3e4093b6 7092 }
c466b2cd 7093 else
3e4093b6 7094 {
c466b2cd
KVH
7095 if (typ2 == char_type_node)
7096 {
ea58ef42
MP
7097 error_init (init_loc, "wide character array initialized "
7098 "from non-wide string");
c466b2cd
KVH
7099 return error_mark_node;
7100 }
7101 else if (!comptypes(typ1, typ2))
7102 {
ea58ef42
MP
7103 error_init (init_loc, "wide character array initialized "
7104 "from incompatible wide string");
c466b2cd
KVH
7105 return error_mark_node;
7106 }
7e842ef8 7107 }
2f6e4e97 7108
3e4093b6
RS
7109 TREE_TYPE (inside_init) = type;
7110 if (TYPE_DOMAIN (type) != 0
7111 && TYPE_SIZE (type) != 0
5eb4df45
ILT
7112 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7113 {
7114 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7115
c466b2cd 7116 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
7117 because it's ok to ignore the terminating null char
7118 that is counted in the length of the constant. */
5eb4df45
ILT
7119 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7120 (len
7121 - (TYPE_PRECISION (typ1)
7122 / BITS_PER_UNIT))))
7123 pedwarn_init (init_loc, 0,
7124 ("initializer-string for array of chars "
7125 "is too long"));
7126 else if (warn_cxx_compat
7127 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7128 warning_at (init_loc, OPT_Wc___compat,
7129 ("initializer-string for array chars "
7130 "is too long for C++"));
7131 }
7e842ef8 7132
3e4093b6 7133 return inside_init;
7e842ef8 7134 }
197463ae
JM
7135 else if (INTEGRAL_TYPE_P (typ1))
7136 {
ea58ef42 7137 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
7138 "from string constant");
7139 return error_mark_node;
7140 }
7e842ef8
PE
7141 }
7142
3e4093b6
RS
7143 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7144 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7145 below and handle as a constructor. */
e89be13b 7146 if (code == VECTOR_TYPE
31521951 7147 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
00c8e9f6 7148 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
7149 && TREE_CONSTANT (inside_init))
7150 {
7151 if (TREE_CODE (inside_init) == VECTOR_CST
7152 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7153 TYPE_MAIN_VARIANT (type)))
7154 return inside_init;
7155
7156 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7157 {
4038c495
GB
7158 unsigned HOST_WIDE_INT ix;
7159 tree value;
7160 bool constant_p = true;
e89be13b
JJ
7161
7162 /* Iterate through elements and check if all constructor
7163 elements are *_CSTs. */
4038c495
GB
7164 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7165 if (!CONSTANT_CLASS_P (value))
7166 {
7167 constant_p = false;
7168 break;
7169 }
e89be13b 7170
4038c495
GB
7171 if (constant_p)
7172 return build_vector_from_ctor (type,
7173 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
7174 }
7175 }
6035d635 7176
ca085fd7
MLI
7177 if (warn_sequence_point)
7178 verify_sequence_points (inside_init);
7179
3e4093b6
RS
7180 /* Any type can be initialized
7181 from an expression of the same type, optionally with braces. */
400fbf9f 7182
3e4093b6
RS
7183 if (inside_init && TREE_TYPE (inside_init) != 0
7184 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 7185 TYPE_MAIN_VARIANT (type))
3e4093b6 7186 || (code == ARRAY_TYPE
132da1a5 7187 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7188 || (code == VECTOR_TYPE
132da1a5 7189 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7190 || (code == POINTER_TYPE
3897f229 7191 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 7192 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 7193 TREE_TYPE (type)))))
3e4093b6
RS
7194 {
7195 if (code == POINTER_TYPE)
b494fd98 7196 {
b494fd98
EB
7197 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7198 {
f2a71bbc
JM
7199 if (TREE_CODE (inside_init) == STRING_CST
7200 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
7201 inside_init = array_to_pointer_conversion
7202 (init_loc, inside_init);
f2a71bbc
JM
7203 else
7204 {
ea58ef42 7205 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
7206 return error_mark_node;
7207 }
b494fd98 7208 }
f2a71bbc 7209 }
b494fd98 7210
bae39a73
NS
7211 if (code == VECTOR_TYPE)
7212 /* Although the types are compatible, we may require a
7213 conversion. */
7214 inside_init = convert (type, inside_init);
3e4093b6 7215
ca58211b 7216 if (require_constant
3e4093b6 7217 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 7218 {
3e4093b6
RS
7219 /* As an extension, allow initializing objects with static storage
7220 duration with compound literals (which are then treated just as
ca58211b
PB
7221 the brace enclosed list they contain). Also allow this for
7222 vectors, as we can only assign them with compound literals. */
7278465e
MP
7223 if (flag_isoc99 && code != VECTOR_TYPE)
7224 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7225 "is not constant");
3e4093b6
RS
7226 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7227 inside_init = DECL_INITIAL (decl);
400fbf9f 7228 }
3e4093b6
RS
7229
7230 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7231 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 7232 {
ea58ef42
MP
7233 error_init (init_loc, "array initialized from non-constant array "
7234 "expression");
3e4093b6 7235 return error_mark_node;
400fbf9f 7236 }
400fbf9f 7237
c1771a20 7238 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
7239 -pedantic-errors is specified. In the later case, we always want
7240 an error. In the former case, we simply want a warning. */
7241 if (require_constant && pedantic
7242 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7243 {
7244 inside_init
7245 = valid_compound_expr_initializer (inside_init,
7246 TREE_TYPE (inside_init));
7247 if (inside_init == error_mark_node)
ea58ef42 7248 error_init (init_loc, "initializer element is not constant");
2f6e4e97 7249 else
c1771a20 7250 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 7251 "initializer element is not constant");
3e4093b6
RS
7252 if (flag_pedantic_errors)
7253 inside_init = error_mark_node;
7254 }
7255 else if (require_constant
116df786
RH
7256 && !initializer_constant_valid_p (inside_init,
7257 TREE_TYPE (inside_init)))
3e4093b6 7258 {
ea58ef42 7259 error_init (init_loc, "initializer element is not constant");
3e4093b6 7260 inside_init = error_mark_node;
8b40563c 7261 }
928c19bb 7262 else if (require_constant && !maybe_const)
3aa3c9fc 7263 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7264 "initializer element is not a constant expression");
f735a153 7265
90137d8f 7266 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 7267 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
7268 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7269 type, inside_init, origtype,
bbbbb16a 7270 ic_init, null_pointer_constant,
928c19bb 7271 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
7272 return inside_init;
7273 }
f735a153 7274
3e4093b6 7275 /* Handle scalar types, including conversions. */
400fbf9f 7276
ab22c1fa
CF
7277 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7278 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7279 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 7280 {
f2a71bbc
JM
7281 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7282 && (TREE_CODE (init) == STRING_CST
7283 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 7284 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
7285 if (semantic_type)
7286 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7287 inside_init);
3e4093b6 7288 inside_init
68fca595
MP
7289 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7290 inside_init, origtype, ic_init,
7291 null_pointer_constant, NULL_TREE, NULL_TREE,
7292 0);
2f6e4e97 7293
3274deff
JW
7294 /* Check to see if we have already given an error message. */
7295 if (inside_init == error_mark_node)
7296 ;
3f75a254 7297 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 7298 {
ea58ef42 7299 error_init (init_loc, "initializer element is not constant");
3e4093b6 7300 inside_init = error_mark_node;
400fbf9f 7301 }
3e4093b6 7302 else if (require_constant
116df786
RH
7303 && !initializer_constant_valid_p (inside_init,
7304 TREE_TYPE (inside_init)))
400fbf9f 7305 {
ea58ef42
MP
7306 error_init (init_loc, "initializer element is not computable at "
7307 "load time");
3e4093b6 7308 inside_init = error_mark_node;
400fbf9f 7309 }
928c19bb 7310 else if (require_constant && !maybe_const)
451b5e48 7311 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7312 "initializer element is not a constant expression");
3e4093b6
RS
7313
7314 return inside_init;
400fbf9f 7315 }
d9fc6069 7316
3e4093b6 7317 /* Come here only for records and arrays. */
d9fc6069 7318
3e4093b6 7319 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 7320 {
ea58ef42 7321 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 7322 return error_mark_node;
d9fc6069 7323 }
3e4093b6 7324
ea58ef42 7325 error_init (init_loc, "invalid initializer");
3e4093b6 7326 return error_mark_node;
d9fc6069 7327}
400fbf9f 7328\f
3e4093b6 7329/* Handle initializers that use braces. */
400fbf9f 7330
3e4093b6
RS
7331/* Type of object we are accumulating a constructor for.
7332 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7333static tree constructor_type;
400fbf9f 7334
3e4093b6
RS
7335/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7336 left to fill. */
7337static tree constructor_fields;
400fbf9f 7338
3e4093b6
RS
7339/* For an ARRAY_TYPE, this is the specified index
7340 at which to store the next element we get. */
7341static tree constructor_index;
400fbf9f 7342
3e4093b6
RS
7343/* For an ARRAY_TYPE, this is the maximum index. */
7344static tree constructor_max_index;
400fbf9f 7345
3e4093b6
RS
7346/* For a RECORD_TYPE, this is the first field not yet written out. */
7347static tree constructor_unfilled_fields;
400fbf9f 7348
3e4093b6
RS
7349/* For an ARRAY_TYPE, this is the index of the first element
7350 not yet written out. */
7351static tree constructor_unfilled_index;
895ea614 7352
3e4093b6
RS
7353/* In a RECORD_TYPE, the byte index of the next consecutive field.
7354 This is so we can generate gaps between fields, when appropriate. */
7355static tree constructor_bit_index;
10d5caec 7356
3e4093b6
RS
7357/* If we are saving up the elements rather than allocating them,
7358 this is the list of elements so far (in reverse order,
7359 most recent first). */
9771b263 7360static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 7361
3e4093b6
RS
7362/* 1 if constructor should be incrementally stored into a constructor chain,
7363 0 if all the elements should be kept in AVL tree. */
7364static int constructor_incremental;
ad47f1e5 7365
3e4093b6
RS
7366/* 1 if so far this constructor's elements are all compile-time constants. */
7367static int constructor_constant;
ad47f1e5 7368
3e4093b6
RS
7369/* 1 if so far this constructor's elements are all valid address constants. */
7370static int constructor_simple;
ad47f1e5 7371
928c19bb
JM
7372/* 1 if this constructor has an element that cannot be part of a
7373 constant expression. */
7374static int constructor_nonconst;
7375
3e4093b6
RS
7376/* 1 if this constructor is erroneous so far. */
7377static int constructor_erroneous;
d45cf215 7378
9bac5cbb
G
7379/* 1 if this constructor is the universal zero initializer { 0 }. */
7380static int constructor_zeroinit;
7381
3e4093b6
RS
7382/* Structure for managing pending initializer elements, organized as an
7383 AVL tree. */
d45cf215 7384
3e4093b6 7385struct init_node
d45cf215 7386{
3e4093b6
RS
7387 struct init_node *left, *right;
7388 struct init_node *parent;
7389 int balance;
7390 tree purpose;
7391 tree value;
bbbbb16a 7392 tree origtype;
d45cf215
RS
7393};
7394
3e4093b6
RS
7395/* Tree of pending elements at this constructor level.
7396 These are elements encountered out of order
7397 which belong at places we haven't reached yet in actually
7398 writing the output.
7399 Will never hold tree nodes across GC runs. */
7400static struct init_node *constructor_pending_elts;
d45cf215 7401
3e4093b6
RS
7402/* The SPELLING_DEPTH of this constructor. */
7403static int constructor_depth;
d45cf215 7404
3e4093b6
RS
7405/* DECL node for which an initializer is being read.
7406 0 means we are reading a constructor expression
7407 such as (struct foo) {...}. */
7408static tree constructor_decl;
d45cf215 7409
3e4093b6
RS
7410/* Nonzero if this is an initializer for a top-level decl. */
7411static int constructor_top_level;
d45cf215 7412
3e4093b6
RS
7413/* Nonzero if there were any member designators in this initializer. */
7414static int constructor_designated;
d45cf215 7415
3e4093b6
RS
7416/* Nesting depth of designator list. */
7417static int designator_depth;
d45cf215 7418
3e4093b6 7419/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7420static int designator_erroneous;
d45cf215 7421
3e4093b6
RS
7422\f
7423/* This stack has a level for each implicit or explicit level of
7424 structuring in the initializer, including the outermost one. It
7425 saves the values of most of the variables above. */
d45cf215 7426
3e4093b6
RS
7427struct constructor_range_stack;
7428
7429struct constructor_stack
d45cf215 7430{
3e4093b6
RS
7431 struct constructor_stack *next;
7432 tree type;
7433 tree fields;
7434 tree index;
7435 tree max_index;
7436 tree unfilled_index;
7437 tree unfilled_fields;
7438 tree bit_index;
9771b263 7439 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7440 struct init_node *pending_elts;
7441 int offset;
7442 int depth;
916c5919 7443 /* If value nonzero, this value should replace the entire
3e4093b6 7444 constructor at this level. */
916c5919 7445 struct c_expr replacement_value;
3e4093b6
RS
7446 struct constructor_range_stack *range_stack;
7447 char constant;
7448 char simple;
928c19bb 7449 char nonconst;
3e4093b6
RS
7450 char implicit;
7451 char erroneous;
7452 char outer;
7453 char incremental;
7454 char designated;
976d5a22 7455 int designator_depth;
3e4093b6 7456};
d45cf215 7457
802415d1 7458static struct constructor_stack *constructor_stack;
d45cf215 7459
3e4093b6
RS
7460/* This stack represents designators from some range designator up to
7461 the last designator in the list. */
d45cf215 7462
3e4093b6
RS
7463struct constructor_range_stack
7464{
7465 struct constructor_range_stack *next, *prev;
7466 struct constructor_stack *stack;
7467 tree range_start;
7468 tree index;
7469 tree range_end;
7470 tree fields;
7471};
d45cf215 7472
802415d1 7473static struct constructor_range_stack *constructor_range_stack;
d45cf215 7474
3e4093b6
RS
7475/* This stack records separate initializers that are nested.
7476 Nested initializers can't happen in ANSI C, but GNU C allows them
7477 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7478
3e4093b6 7479struct initializer_stack
d45cf215 7480{
3e4093b6
RS
7481 struct initializer_stack *next;
7482 tree decl;
3e4093b6
RS
7483 struct constructor_stack *constructor_stack;
7484 struct constructor_range_stack *constructor_range_stack;
9771b263 7485 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7486 struct spelling *spelling;
7487 struct spelling *spelling_base;
7488 int spelling_size;
7489 char top_level;
7490 char require_constant_value;
7491 char require_constant_elements;
7492};
d45cf215 7493
802415d1 7494static struct initializer_stack *initializer_stack;
3e4093b6
RS
7495\f
7496/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7497
7498void
a396f8ae 7499start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 7500{
3e4093b6 7501 const char *locus;
cceb1885 7502 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7503
3e4093b6 7504 p->decl = constructor_decl;
3e4093b6
RS
7505 p->require_constant_value = require_constant_value;
7506 p->require_constant_elements = require_constant_elements;
7507 p->constructor_stack = constructor_stack;
7508 p->constructor_range_stack = constructor_range_stack;
7509 p->elements = constructor_elements;
7510 p->spelling = spelling;
7511 p->spelling_base = spelling_base;
7512 p->spelling_size = spelling_size;
7513 p->top_level = constructor_top_level;
7514 p->next = initializer_stack;
7515 initializer_stack = p;
400fbf9f 7516
3e4093b6 7517 constructor_decl = decl;
3e4093b6
RS
7518 constructor_designated = 0;
7519 constructor_top_level = top_level;
400fbf9f 7520
6f17bbcf 7521 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7522 {
7523 require_constant_value = TREE_STATIC (decl);
7524 require_constant_elements
7525 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7526 /* For a scalar, you can always use any value to initialize,
7527 even within braces. */
296a8c2f 7528 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
88388a52 7529 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7530 }
7531 else
7532 {
7533 require_constant_value = 0;
7534 require_constant_elements = 0;
88388a52 7535 locus = _("(anonymous)");
3e4093b6 7536 }
b71c7f8a 7537
3e4093b6
RS
7538 constructor_stack = 0;
7539 constructor_range_stack = 0;
b71c7f8a 7540
9bac5cbb 7541 found_missing_braces = 0;
3e4093b6
RS
7542
7543 spelling_base = 0;
7544 spelling_size = 0;
7545 RESTORE_SPELLING_DEPTH (0);
7546
7547 if (locus)
7548 push_string (locus);
7549}
7550
7551void
7552finish_init (void)
b71c7f8a 7553{
3e4093b6 7554 struct initializer_stack *p = initializer_stack;
b71c7f8a 7555
3e4093b6
RS
7556 /* Free the whole constructor stack of this initializer. */
7557 while (constructor_stack)
7558 {
7559 struct constructor_stack *q = constructor_stack;
7560 constructor_stack = q->next;
7561 free (q);
7562 }
7563
366de0ce 7564 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7565
7566 /* Pop back to the data of the outer initializer (if any). */
36579663 7567 free (spelling_base);
3aeb3655 7568
3e4093b6 7569 constructor_decl = p->decl;
3e4093b6
RS
7570 require_constant_value = p->require_constant_value;
7571 require_constant_elements = p->require_constant_elements;
7572 constructor_stack = p->constructor_stack;
7573 constructor_range_stack = p->constructor_range_stack;
7574 constructor_elements = p->elements;
7575 spelling = p->spelling;
7576 spelling_base = p->spelling_base;
7577 spelling_size = p->spelling_size;
7578 constructor_top_level = p->top_level;
7579 initializer_stack = p->next;
7580 free (p);
b71c7f8a 7581}
400fbf9f 7582\f
3e4093b6
RS
7583/* Call here when we see the initializer is surrounded by braces.
7584 This is instead of a call to push_init_level;
7585 it is matched by a call to pop_init_level.
400fbf9f 7586
3e4093b6
RS
7587 TYPE is the type to initialize, for a constructor expression.
7588 For an initializer for a decl, TYPE is zero. */
400fbf9f 7589
3e4093b6
RS
7590void
7591really_start_incremental_init (tree type)
400fbf9f 7592{
5d038c4c 7593 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7594
3e4093b6
RS
7595 if (type == 0)
7596 type = TREE_TYPE (constructor_decl);
400fbf9f 7597
31521951 7598 if (VECTOR_TYPE_P (type)
b6fc2cdb 7599 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7600 error ("opaque vector types cannot be initialized");
400fbf9f 7601
3e4093b6
RS
7602 p->type = constructor_type;
7603 p->fields = constructor_fields;
7604 p->index = constructor_index;
7605 p->max_index = constructor_max_index;
7606 p->unfilled_index = constructor_unfilled_index;
7607 p->unfilled_fields = constructor_unfilled_fields;
7608 p->bit_index = constructor_bit_index;
7609 p->elements = constructor_elements;
7610 p->constant = constructor_constant;
7611 p->simple = constructor_simple;
928c19bb 7612 p->nonconst = constructor_nonconst;
3e4093b6
RS
7613 p->erroneous = constructor_erroneous;
7614 p->pending_elts = constructor_pending_elts;
7615 p->depth = constructor_depth;
916c5919
JM
7616 p->replacement_value.value = 0;
7617 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7618 p->replacement_value.original_type = NULL;
3e4093b6
RS
7619 p->implicit = 0;
7620 p->range_stack = 0;
7621 p->outer = 0;
7622 p->incremental = constructor_incremental;
7623 p->designated = constructor_designated;
976d5a22 7624 p->designator_depth = designator_depth;
3e4093b6
RS
7625 p->next = 0;
7626 constructor_stack = p;
b13aca19 7627
3e4093b6
RS
7628 constructor_constant = 1;
7629 constructor_simple = 1;
928c19bb 7630 constructor_nonconst = 0;
3e4093b6 7631 constructor_depth = SPELLING_DEPTH ();
9771b263 7632 constructor_elements = NULL;
3e4093b6
RS
7633 constructor_pending_elts = 0;
7634 constructor_type = type;
7635 constructor_incremental = 1;
7636 constructor_designated = 0;
9bac5cbb 7637 constructor_zeroinit = 1;
3e4093b6 7638 designator_depth = 0;
b06df647 7639 designator_erroneous = 0;
400fbf9f 7640
3e636daf 7641 if (RECORD_OR_UNION_TYPE_P (constructor_type))
400fbf9f 7642 {
3e4093b6
RS
7643 constructor_fields = TYPE_FIELDS (constructor_type);
7644 /* Skip any nameless bit fields at the beginning. */
7645 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7646 && DECL_NAME (constructor_fields) == 0)
910ad8de 7647 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7648
3e4093b6
RS
7649 constructor_unfilled_fields = constructor_fields;
7650 constructor_bit_index = bitsize_zero_node;
400fbf9f 7651 }
3e4093b6
RS
7652 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7653 {
7654 if (TYPE_DOMAIN (constructor_type))
7655 {
7656 constructor_max_index
7657 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7658
3e4093b6
RS
7659 /* Detect non-empty initializations of zero-length arrays. */
7660 if (constructor_max_index == NULL_TREE
7661 && TYPE_SIZE (constructor_type))
9a9d280e 7662 constructor_max_index = integer_minus_one_node;
400fbf9f 7663
3e4093b6
RS
7664 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7665 to initialize VLAs will cause a proper error; avoid tree
7666 checking errors as well by setting a safe value. */
7667 if (constructor_max_index
7668 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7669 constructor_max_index = integer_minus_one_node;
59c83dbf 7670
3e4093b6
RS
7671 constructor_index
7672 = convert (bitsizetype,
7673 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7674 }
3e4093b6 7675 else
493179da
JM
7676 {
7677 constructor_index = bitsize_zero_node;
7678 constructor_max_index = NULL_TREE;
7679 }
59c83dbf 7680
3e4093b6
RS
7681 constructor_unfilled_index = constructor_index;
7682 }
31521951 7683 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7684 {
7685 /* Vectors are like simple fixed-size arrays. */
7686 constructor_max_index =
c62c040f 7687 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7688 constructor_index = bitsize_zero_node;
3e4093b6
RS
7689 constructor_unfilled_index = constructor_index;
7690 }
7691 else
7692 {
7693 /* Handle the case of int x = {5}; */
7694 constructor_fields = constructor_type;
7695 constructor_unfilled_fields = constructor_type;
7696 }
7697}
7698\f
16595a1f
BS
7699/* Called when we see an open brace for a nested initializer. Finish
7700 off any pending levels with implicit braces. */
7701void
7702finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7703{
7704 while (constructor_stack->implicit)
7705 {
7706 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7707 && constructor_fields == 0)
7708 process_init_element (input_location,
7709 pop_init_level (loc, 1, braced_init_obstack),
7710 true, braced_init_obstack);
7711 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7712 && constructor_max_index
7713 && tree_int_cst_lt (constructor_max_index,
7714 constructor_index))
7715 process_init_element (input_location,
7716 pop_init_level (loc, 1, braced_init_obstack),
7717 true, braced_init_obstack);
7718 else
7719 break;
7720 }
7721}
7722
3e4093b6
RS
7723/* Push down into a subobject, for initialization.
7724 If this is for an explicit set of braces, IMPLICIT is 0.
7725 If it is because the next element belongs at a lower level,
7726 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7727
3e4093b6 7728void
ea58ef42
MP
7729push_init_level (location_t loc, int implicit,
7730 struct obstack *braced_init_obstack)
3e4093b6
RS
7731{
7732 struct constructor_stack *p;
7733 tree value = NULL_TREE;
400fbf9f 7734
3e4093b6
RS
7735 /* Unless this is an explicit brace, we need to preserve previous
7736 content if any. */
7737 if (implicit)
7738 {
3e636daf 7739 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
a1e3b3d9 7740 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7741 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7742 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7743 }
7744
5d038c4c 7745 p = XNEW (struct constructor_stack);
3e4093b6
RS
7746 p->type = constructor_type;
7747 p->fields = constructor_fields;
7748 p->index = constructor_index;
7749 p->max_index = constructor_max_index;
7750 p->unfilled_index = constructor_unfilled_index;
7751 p->unfilled_fields = constructor_unfilled_fields;
7752 p->bit_index = constructor_bit_index;
7753 p->elements = constructor_elements;
7754 p->constant = constructor_constant;
7755 p->simple = constructor_simple;
928c19bb 7756 p->nonconst = constructor_nonconst;
3e4093b6
RS
7757 p->erroneous = constructor_erroneous;
7758 p->pending_elts = constructor_pending_elts;
7759 p->depth = constructor_depth;
916c5919
JM
7760 p->replacement_value.value = 0;
7761 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7762 p->replacement_value.original_type = NULL;
3e4093b6
RS
7763 p->implicit = implicit;
7764 p->outer = 0;
7765 p->incremental = constructor_incremental;
7766 p->designated = constructor_designated;
976d5a22 7767 p->designator_depth = designator_depth;
3e4093b6
RS
7768 p->next = constructor_stack;
7769 p->range_stack = 0;
7770 constructor_stack = p;
400fbf9f 7771
3e4093b6
RS
7772 constructor_constant = 1;
7773 constructor_simple = 1;
928c19bb 7774 constructor_nonconst = 0;
3e4093b6 7775 constructor_depth = SPELLING_DEPTH ();
9771b263 7776 constructor_elements = NULL;
3e4093b6
RS
7777 constructor_incremental = 1;
7778 constructor_designated = 0;
7779 constructor_pending_elts = 0;
7780 if (!implicit)
400fbf9f 7781 {
3e4093b6
RS
7782 p->range_stack = constructor_range_stack;
7783 constructor_range_stack = 0;
7784 designator_depth = 0;
b06df647 7785 designator_erroneous = 0;
3e4093b6 7786 }
400fbf9f 7787
3e4093b6
RS
7788 /* Don't die if an entire brace-pair level is superfluous
7789 in the containing level. */
7790 if (constructor_type == 0)
7791 ;
3e636daf 7792 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
7793 {
7794 /* Don't die if there are extra init elts at the end. */
7795 if (constructor_fields == 0)
7796 constructor_type = 0;
7797 else
400fbf9f 7798 {
3e4093b6
RS
7799 constructor_type = TREE_TYPE (constructor_fields);
7800 push_member_name (constructor_fields);
7801 constructor_depth++;
400fbf9f 7802 }
6a358dcb
MP
7803 /* If upper initializer is designated, then mark this as
7804 designated too to prevent bogus warnings. */
7805 constructor_designated = p->designated;
3e4093b6
RS
7806 }
7807 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7808 {
7809 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7810 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7811 constructor_depth++;
400fbf9f
JW
7812 }
7813
3e4093b6 7814 if (constructor_type == 0)
400fbf9f 7815 {
ea58ef42 7816 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7817 constructor_fields = 0;
7818 constructor_unfilled_fields = 0;
7819 return;
400fbf9f
JW
7820 }
7821
3e4093b6
RS
7822 if (value && TREE_CODE (value) == CONSTRUCTOR)
7823 {
7824 constructor_constant = TREE_CONSTANT (value);
7825 constructor_simple = TREE_STATIC (value);
928c19bb 7826 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7827 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7828 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7829 && (TREE_CODE (constructor_type) == RECORD_TYPE
7830 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7831 set_nonincremental_init (braced_init_obstack);
3e4093b6 7832 }
400fbf9f 7833
9bac5cbb
G
7834 if (implicit == 1)
7835 found_missing_braces = 1;
400fbf9f 7836
3e636daf 7837 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
7838 {
7839 constructor_fields = TYPE_FIELDS (constructor_type);
7840 /* Skip any nameless bit fields at the beginning. */
7841 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7842 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7843 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7844
3e4093b6
RS
7845 constructor_unfilled_fields = constructor_fields;
7846 constructor_bit_index = bitsize_zero_node;
7847 }
31521951 7848 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7849 {
7850 /* Vectors are like simple fixed-size arrays. */
7851 constructor_max_index =
c62c040f
RG
7852 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7853 constructor_index = bitsize_int (0);
3e4093b6
RS
7854 constructor_unfilled_index = constructor_index;
7855 }
7856 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7857 {
7858 if (TYPE_DOMAIN (constructor_type))
7859 {
7860 constructor_max_index
7861 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7862
3e4093b6
RS
7863 /* Detect non-empty initializations of zero-length arrays. */
7864 if (constructor_max_index == NULL_TREE
7865 && TYPE_SIZE (constructor_type))
9a9d280e 7866 constructor_max_index = integer_minus_one_node;
de520661 7867
3e4093b6
RS
7868 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7869 to initialize VLAs will cause a proper error; avoid tree
7870 checking errors as well by setting a safe value. */
7871 if (constructor_max_index
7872 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7873 constructor_max_index = integer_minus_one_node;
b62acd60 7874
3e4093b6
RS
7875 constructor_index
7876 = convert (bitsizetype,
7877 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7878 }
7879 else
7880 constructor_index = bitsize_zero_node;
de520661 7881
3e4093b6
RS
7882 constructor_unfilled_index = constructor_index;
7883 if (value && TREE_CODE (value) == STRING_CST)
7884 {
7885 /* We need to split the char/wchar array into individual
7886 characters, so that we don't have to special case it
7887 everywhere. */
a1e3b3d9 7888 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7889 }
7890 }
7891 else
7892 {
b4519d39 7893 if (constructor_type != error_mark_node)
96b40f8d 7894 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7895 constructor_fields = constructor_type;
7896 constructor_unfilled_fields = constructor_type;
7897 }
7898}
8b6a5902 7899
3e4093b6 7900/* At the end of an implicit or explicit brace level,
916c5919
JM
7901 finish up that level of constructor. If a single expression
7902 with redundant braces initialized that level, return the
7903 c_expr structure for that expression. Otherwise, the original_code
7904 element is set to ERROR_MARK.
7905 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7906 from inner levels (process_init_element ignores that),
916c5919 7907 but return error_mark_node as the value from the outermost level
3e4093b6 7908 (that's what we want to put in DECL_INITIAL).
916c5919 7909 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7910
916c5919 7911struct c_expr
ea58ef42
MP
7912pop_init_level (location_t loc, int implicit,
7913 struct obstack *braced_init_obstack)
3e4093b6
RS
7914{
7915 struct constructor_stack *p;
916c5919
JM
7916 struct c_expr ret;
7917 ret.value = 0;
7918 ret.original_code = ERROR_MARK;
6866c6e8 7919 ret.original_type = NULL;
de520661 7920
3e4093b6
RS
7921 if (implicit == 0)
7922 {
7923 /* When we come to an explicit close brace,
7924 pop any inner levels that didn't have explicit braces. */
7925 while (constructor_stack->implicit)
34cf811f 7926 process_init_element (input_location,
ea58ef42 7927 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7928 true, braced_init_obstack);
366de0ce 7929 gcc_assert (!constructor_range_stack);
3e4093b6 7930 }
e5e809f4 7931
0066ef9c
RH
7932 /* Now output all pending elements. */
7933 constructor_incremental = 1;
a1e3b3d9 7934 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7935
3e4093b6 7936 p = constructor_stack;
e5e809f4 7937
3e4093b6
RS
7938 /* Error for initializing a flexible array member, or a zero-length
7939 array member in an inappropriate context. */
7940 if (constructor_type && constructor_fields
7941 && TREE_CODE (constructor_type) == ARRAY_TYPE
7942 && TYPE_DOMAIN (constructor_type)
3f75a254 7943 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7944 {
7945 /* Silently discard empty initializations. The parser will
7946 already have pedwarned for empty brackets. */
7947 if (integer_zerop (constructor_unfilled_index))
7948 constructor_type = NULL_TREE;
366de0ce 7949 else
3e4093b6 7950 {
366de0ce 7951 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7952
3e4093b6 7953 if (constructor_depth > 2)
ea58ef42 7954 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7955 else
d033409e 7956 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7957 "initialization of a flexible array member");
de520661 7958
3e4093b6
RS
7959 /* We have already issued an error message for the existence
7960 of a flexible array member not at the end of the structure.
535a42b1 7961 Discard the initializer so that we do not die later. */
910ad8de 7962 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7963 constructor_type = NULL_TREE;
7964 }
3e4093b6 7965 }
de520661 7966
7b33f0c8
MP
7967 switch (vec_safe_length (constructor_elements))
7968 {
7969 case 0:
7970 /* Initialization with { } counts as zeroinit. */
7971 constructor_zeroinit = 1;
7972 break;
7973 case 1:
7974 /* This might be zeroinit as well. */
7975 if (integer_zerop ((*constructor_elements)[0].value))
7976 constructor_zeroinit = 1;
7977 break;
7978 default:
7979 /* If the constructor has more than one element, it can't be { 0 }. */
7980 constructor_zeroinit = 0;
7981 break;
7982 }
9bac5cbb
G
7983
7984 /* Warn when some structs are initialized with direct aggregation. */
7985 if (!implicit && found_missing_braces && warn_missing_braces
7986 && !constructor_zeroinit)
7b33f0c8
MP
7987 warning_init (loc, OPT_Wmissing_braces,
7988 "missing braces around initializer");
9bac5cbb 7989
3e4093b6 7990 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7991 if (warn_missing_field_initializers
3e4093b6
RS
7992 && constructor_type
7993 && TREE_CODE (constructor_type) == RECORD_TYPE
7994 && constructor_unfilled_fields)
7995 {
7996 /* Do not warn for flexible array members or zero-length arrays. */
7997 while (constructor_unfilled_fields
3f75a254 7998 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7999 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 8000 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 8001
49819fef
AM
8002 if (constructor_unfilled_fields
8003 /* Do not warn if this level of the initializer uses member
8004 designators; it is likely to be deliberate. */
8005 && !constructor_designated
84937de2 8006 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 8007 && !constructor_zeroinit)
3e4093b6 8008 {
32397f22
MLI
8009 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8010 "missing initializer for field %qD of %qT",
8011 constructor_unfilled_fields,
8012 constructor_type))
8013 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 8014 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
8015 }
8016 }
de520661 8017
3e4093b6 8018 /* Pad out the end of the structure. */
916c5919 8019 if (p->replacement_value.value)
3e4093b6
RS
8020 /* If this closes a superfluous brace pair,
8021 just pass out the element between them. */
916c5919 8022 ret = p->replacement_value;
3e4093b6
RS
8023 else if (constructor_type == 0)
8024 ;
3e636daf 8025 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 8026 && TREE_CODE (constructor_type) != ARRAY_TYPE
31521951 8027 && !VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
8028 {
8029 /* A nonincremental scalar initializer--just return
8030 the element, after verifying there is just one. */
9771b263 8031 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
8032 {
8033 if (!constructor_erroneous)
ea58ef42 8034 error_init (loc, "empty scalar initializer");
916c5919 8035 ret.value = error_mark_node;
3e4093b6 8036 }
9771b263 8037 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 8038 {
ea58ef42 8039 error_init (loc, "extra elements in scalar initializer");
9771b263 8040 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
8041 }
8042 else
9771b263 8043 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
8044 }
8045 else
8046 {
8047 if (constructor_erroneous)
916c5919 8048 ret.value = error_mark_node;
3e4093b6
RS
8049 else
8050 {
916c5919 8051 ret.value = build_constructor (constructor_type,
4038c495 8052 constructor_elements);
3e4093b6 8053 if (constructor_constant)
51eed280 8054 TREE_CONSTANT (ret.value) = 1;
3e4093b6 8055 if (constructor_constant && constructor_simple)
916c5919 8056 TREE_STATIC (ret.value) = 1;
928c19bb
JM
8057 if (constructor_nonconst)
8058 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
8059 }
8060 }
de520661 8061
928c19bb
JM
8062 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8063 {
8064 if (constructor_nonconst)
8065 ret.original_code = C_MAYBE_CONST_EXPR;
8066 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8067 ret.original_code = ERROR_MARK;
8068 }
8069
3e4093b6
RS
8070 constructor_type = p->type;
8071 constructor_fields = p->fields;
8072 constructor_index = p->index;
8073 constructor_max_index = p->max_index;
8074 constructor_unfilled_index = p->unfilled_index;
8075 constructor_unfilled_fields = p->unfilled_fields;
8076 constructor_bit_index = p->bit_index;
8077 constructor_elements = p->elements;
8078 constructor_constant = p->constant;
8079 constructor_simple = p->simple;
928c19bb 8080 constructor_nonconst = p->nonconst;
3e4093b6
RS
8081 constructor_erroneous = p->erroneous;
8082 constructor_incremental = p->incremental;
8083 constructor_designated = p->designated;
976d5a22 8084 designator_depth = p->designator_depth;
3e4093b6
RS
8085 constructor_pending_elts = p->pending_elts;
8086 constructor_depth = p->depth;
8087 if (!p->implicit)
8088 constructor_range_stack = p->range_stack;
8089 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 8090
3e4093b6
RS
8091 constructor_stack = p->next;
8092 free (p);
b621a4dd 8093
5d5e98dc
VR
8094 if (ret.value == 0 && constructor_stack == 0)
8095 ret.value = error_mark_node;
916c5919 8096 return ret;
3e4093b6 8097}
8b6a5902 8098
3e4093b6
RS
8099/* Common handling for both array range and field name designators.
8100 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 8101
3e4093b6 8102static int
ea58ef42
MP
8103set_designator (location_t loc, int array,
8104 struct obstack *braced_init_obstack)
de520661 8105{
3e4093b6
RS
8106 tree subtype;
8107 enum tree_code subcode;
de520661 8108
3e4093b6
RS
8109 /* Don't die if an entire brace-pair level is superfluous
8110 in the containing level. */
8111 if (constructor_type == 0)
8112 return 1;
de520661 8113
366de0ce
NS
8114 /* If there were errors in this designator list already, bail out
8115 silently. */
b06df647 8116 if (designator_erroneous)
3e4093b6 8117 return 1;
e28cae4f 8118
3e4093b6
RS
8119 if (!designator_depth)
8120 {
366de0ce 8121 gcc_assert (!constructor_range_stack);
de520661 8122
3e4093b6
RS
8123 /* Designator list starts at the level of closest explicit
8124 braces. */
8125 while (constructor_stack->implicit)
34cf811f 8126 process_init_element (input_location,
ea58ef42 8127 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 8128 true, braced_init_obstack);
3e4093b6
RS
8129 constructor_designated = 1;
8130 return 0;
8131 }
de520661 8132
366de0ce 8133 switch (TREE_CODE (constructor_type))
3c3fa147 8134 {
366de0ce
NS
8135 case RECORD_TYPE:
8136 case UNION_TYPE:
3e4093b6
RS
8137 subtype = TREE_TYPE (constructor_fields);
8138 if (subtype != error_mark_node)
8139 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
8140 break;
8141 case ARRAY_TYPE:
3e4093b6 8142 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
8143 break;
8144 default:
8145 gcc_unreachable ();
de520661 8146 }
400fbf9f 8147
3e4093b6
RS
8148 subcode = TREE_CODE (subtype);
8149 if (array && subcode != ARRAY_TYPE)
8150 {
ea58ef42 8151 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
8152 return 1;
8153 }
8154 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8155 {
ea58ef42 8156 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
8157 return 1;
8158 }
d45cf215 8159
3e4093b6 8160 constructor_designated = 1;
16595a1f 8161 finish_implicit_inits (loc, braced_init_obstack);
ea58ef42 8162 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 8163 return 0;
de520661 8164}
400fbf9f 8165
3e4093b6
RS
8166/* If there are range designators in designator list, push a new designator
8167 to constructor_range_stack. RANGE_END is end of such stack range or
8168 NULL_TREE if there is no range designator at this level. */
400fbf9f 8169
3e4093b6 8170static void
a1e3b3d9 8171push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
8172{
8173 struct constructor_range_stack *p;
400fbf9f 8174
a1e3b3d9
LB
8175 p = (struct constructor_range_stack *)
8176 obstack_alloc (braced_init_obstack,
8177 sizeof (struct constructor_range_stack));
3e4093b6
RS
8178 p->prev = constructor_range_stack;
8179 p->next = 0;
8180 p->fields = constructor_fields;
8181 p->range_start = constructor_index;
8182 p->index = constructor_index;
8183 p->stack = constructor_stack;
8184 p->range_end = range_end;
8b6a5902 8185 if (constructor_range_stack)
3e4093b6
RS
8186 constructor_range_stack->next = p;
8187 constructor_range_stack = p;
de520661 8188}
400fbf9f 8189
3e4093b6
RS
8190/* Within an array initializer, specify the next index to be initialized.
8191 FIRST is that index. If LAST is nonzero, then initialize a range
8192 of indices, running from FIRST through LAST. */
5a7ec9d9 8193
de520661 8194void
ea58ef42 8195set_init_index (location_t loc, tree first, tree last,
d033409e 8196 struct obstack *braced_init_obstack)
de520661 8197{
ea58ef42 8198 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 8199 return;
de520661 8200
b06df647 8201 designator_erroneous = 1;
de520661 8202
3ea8cd06
JM
8203 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8204 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8205 {
ea58ef42 8206 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
8207 return;
8208 }
8209
2b6da65c
JM
8210 if (TREE_CODE (first) != INTEGER_CST)
8211 {
8212 first = c_fully_fold (first, false, NULL);
8213 if (TREE_CODE (first) == INTEGER_CST)
d033409e 8214 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8215 "array index in initializer is not "
8216 "an integer constant expression");
8217 }
8218
8219 if (last && TREE_CODE (last) != INTEGER_CST)
8220 {
8221 last = c_fully_fold (last, false, NULL);
8222 if (TREE_CODE (last) == INTEGER_CST)
d033409e 8223 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8224 "array index in initializer is not "
8225 "an integer constant expression");
8226 }
8227
3e4093b6 8228 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 8229 error_init (loc, "nonconstant array index in initializer");
3e4093b6 8230 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 8231 error_init (loc, "nonconstant array index in initializer");
3e4093b6 8232 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 8233 error_init (loc, "array index in non-array initializer");
622adc7e 8234 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 8235 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
8236 else if (constructor_max_index
8237 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 8238 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 8239 else
de520661 8240 {
928c19bb
JM
8241 constant_expression_warning (first);
8242 if (last)
8243 constant_expression_warning (last);
3e4093b6 8244 constructor_index = convert (bitsizetype, first);
40d3d530
JR
8245 if (tree_int_cst_lt (constructor_index, first))
8246 {
8247 constructor_index = copy_node (constructor_index);
8248 TREE_OVERFLOW (constructor_index) = 1;
8249 }
665f2503 8250
3e4093b6 8251 if (last)
2bede729 8252 {
3e4093b6
RS
8253 if (tree_int_cst_equal (first, last))
8254 last = 0;
8255 else if (tree_int_cst_lt (last, first))
8256 {
ea58ef42 8257 error_init (loc, "empty index range in initializer");
3e4093b6
RS
8258 last = 0;
8259 }
8260 else
8261 {
8262 last = convert (bitsizetype, last);
8263 if (constructor_max_index != 0
8264 && tree_int_cst_lt (constructor_max_index, last))
8265 {
ea58ef42
MP
8266 error_init (loc, "array index range in initializer exceeds "
8267 "array bounds");
3e4093b6
RS
8268 last = 0;
8269 }
8270 }
2bede729 8271 }
fed3cef0 8272
3e4093b6 8273 designator_depth++;
b06df647 8274 designator_erroneous = 0;
3e4093b6 8275 if (constructor_range_stack || last)
a1e3b3d9 8276 push_range_stack (last, braced_init_obstack);
de520661 8277 }
de520661 8278}
3e4093b6
RS
8279
8280/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 8281
de520661 8282void
f7e4f2e3 8283set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
ea58ef42 8284 struct obstack *braced_init_obstack)
de520661 8285{
0fb96aa4 8286 tree field;
94ba5069 8287
ea58ef42 8288 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
8289 return;
8290
b06df647 8291 designator_erroneous = 1;
3e4093b6 8292
3e636daf 8293 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
94ba5069 8294 {
ea58ef42 8295 error_init (loc, "field name not in record or union initializer");
3e4093b6 8296 return;
94ba5069
RS
8297 }
8298
0fb96aa4 8299 field = lookup_field (constructor_type, fieldname);
8b6a5902 8300
0fb96aa4 8301 if (field == 0)
f7e4f2e3
DM
8302 {
8303 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8304 if (guessed_id)
8305 {
264757fb
DM
8306 gcc_rich_location rich_loc (fieldname_loc);
8307 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
f7e4f2e3
DM
8308 error_at_rich_loc
8309 (&rich_loc,
8310 "%qT has no member named %qE; did you mean %qE?",
8311 constructor_type, fieldname, guessed_id);
8312 }
8313 else
8314 error_at (fieldname_loc, "%qT has no member named %qE",
8315 constructor_type, fieldname);
8316 }
3e4093b6 8317 else
0fb96aa4
JM
8318 do
8319 {
8320 constructor_fields = TREE_VALUE (field);
8321 designator_depth++;
8322 designator_erroneous = 0;
8323 if (constructor_range_stack)
8324 push_range_stack (NULL_TREE, braced_init_obstack);
8325 field = TREE_CHAIN (field);
8326 if (field)
8327 {
ea58ef42 8328 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
8329 return;
8330 }
8331 }
8332 while (field != NULL_TREE);
3e4093b6
RS
8333}
8334\f
8335/* Add a new initializer to the tree of pending initializers. PURPOSE
8336 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
8337 VALUE is the value of that index or field. If ORIGTYPE is not
8338 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
8339
8340 IMPLICIT is true if value comes from pop_init_level (1),
8341 the new initializer has been merged with the existing one
8342 and thus no warnings should be emitted about overriding an
8343 existing initializer. */
de520661 8344
3e4093b6 8345static void
96b40f8d
MP
8346add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8347 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
8348{
8349 struct init_node *p, **q, *r;
8350
8351 q = &constructor_pending_elts;
8352 p = 0;
8353
8354 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8355 {
3e4093b6 8356 while (*q != 0)
91fa3c30 8357 {
3e4093b6
RS
8358 p = *q;
8359 if (tree_int_cst_lt (purpose, p->purpose))
8360 q = &p->left;
8361 else if (tree_int_cst_lt (p->purpose, purpose))
8362 q = &p->right;
8363 else
8364 {
b295aee2
JJ
8365 if (!implicit)
8366 {
8367 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8368 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8369 "initialized field with side-effects "
8370 "overwritten");
b295aee2 8371 else if (warn_override_init)
96b40f8d
MP
8372 warning_init (loc, OPT_Woverride_init,
8373 "initialized field overwritten");
b295aee2 8374 }
3e4093b6 8375 p->value = value;
bbbbb16a 8376 p->origtype = origtype;
3e4093b6
RS
8377 return;
8378 }
91fa3c30 8379 }
de520661 8380 }
3e4093b6 8381 else
de520661 8382 {
3e4093b6 8383 tree bitpos;
400fbf9f 8384
3e4093b6
RS
8385 bitpos = bit_position (purpose);
8386 while (*q != NULL)
8387 {
8388 p = *q;
8389 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8390 q = &p->left;
8391 else if (p->purpose != purpose)
8392 q = &p->right;
8393 else
8394 {
b295aee2
JJ
8395 if (!implicit)
8396 {
8397 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8398 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8399 "initialized field with side-effects "
8400 "overwritten");
b295aee2 8401 else if (warn_override_init)
96b40f8d
MP
8402 warning_init (loc, OPT_Woverride_init,
8403 "initialized field overwritten");
b295aee2 8404 }
3e4093b6 8405 p->value = value;
bbbbb16a 8406 p->origtype = origtype;
3e4093b6
RS
8407 return;
8408 }
8409 }
91fa3c30 8410 }
b71c7f8a 8411
a1e3b3d9
LB
8412 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8413 sizeof (struct init_node));
3e4093b6
RS
8414 r->purpose = purpose;
8415 r->value = value;
bbbbb16a 8416 r->origtype = origtype;
8b6a5902 8417
3e4093b6
RS
8418 *q = r;
8419 r->parent = p;
8420 r->left = 0;
8421 r->right = 0;
8422 r->balance = 0;
b71c7f8a 8423
3e4093b6 8424 while (p)
de520661 8425 {
3e4093b6 8426 struct init_node *s;
665f2503 8427
3e4093b6 8428 if (r == p->left)
2bede729 8429 {
3e4093b6
RS
8430 if (p->balance == 0)
8431 p->balance = -1;
8432 else if (p->balance < 0)
8433 {
8434 if (r->balance < 0)
8435 {
8436 /* L rotation. */
8437 p->left = r->right;
8438 if (p->left)
8439 p->left->parent = p;
8440 r->right = p;
e7b6a0ee 8441
3e4093b6
RS
8442 p->balance = 0;
8443 r->balance = 0;
39bc99c2 8444
3e4093b6
RS
8445 s = p->parent;
8446 p->parent = r;
8447 r->parent = s;
8448 if (s)
8449 {
8450 if (s->left == p)
8451 s->left = r;
8452 else
8453 s->right = r;
8454 }
8455 else
8456 constructor_pending_elts = r;
8457 }
8458 else
8459 {
8460 /* LR rotation. */
8461 struct init_node *t = r->right;
e7b6a0ee 8462
3e4093b6
RS
8463 r->right = t->left;
8464 if (r->right)
8465 r->right->parent = r;
8466 t->left = r;
8467
8468 p->left = t->right;
8469 if (p->left)
8470 p->left->parent = p;
8471 t->right = p;
8472
8473 p->balance = t->balance < 0;
8474 r->balance = -(t->balance > 0);
8475 t->balance = 0;
8476
8477 s = p->parent;
8478 p->parent = t;
8479 r->parent = t;
8480 t->parent = s;
8481 if (s)
8482 {
8483 if (s->left == p)
8484 s->left = t;
8485 else
8486 s->right = t;
8487 }
8488 else
8489 constructor_pending_elts = t;
8490 }
8491 break;
8492 }
8493 else
8494 {
8495 /* p->balance == +1; growth of left side balances the node. */
8496 p->balance = 0;
8497 break;
8498 }
2bede729 8499 }
3e4093b6
RS
8500 else /* r == p->right */
8501 {
8502 if (p->balance == 0)
8503 /* Growth propagation from right side. */
8504 p->balance++;
8505 else if (p->balance > 0)
8506 {
8507 if (r->balance > 0)
8508 {
8509 /* R rotation. */
8510 p->right = r->left;
8511 if (p->right)
8512 p->right->parent = p;
8513 r->left = p;
8514
8515 p->balance = 0;
8516 r->balance = 0;
8517
8518 s = p->parent;
8519 p->parent = r;
8520 r->parent = s;
8521 if (s)
8522 {
8523 if (s->left == p)
8524 s->left = r;
8525 else
8526 s->right = r;
8527 }
8528 else
8529 constructor_pending_elts = r;
8530 }
8531 else /* r->balance == -1 */
8532 {
8533 /* RL rotation */
8534 struct init_node *t = r->left;
8535
8536 r->left = t->right;
8537 if (r->left)
8538 r->left->parent = r;
8539 t->right = r;
8540
8541 p->right = t->left;
8542 if (p->right)
8543 p->right->parent = p;
8544 t->left = p;
8545
8546 r->balance = (t->balance < 0);
8547 p->balance = -(t->balance > 0);
8548 t->balance = 0;
8549
8550 s = p->parent;
8551 p->parent = t;
8552 r->parent = t;
8553 t->parent = s;
8554 if (s)
8555 {
8556 if (s->left == p)
8557 s->left = t;
8558 else
8559 s->right = t;
8560 }
8561 else
8562 constructor_pending_elts = t;
8563 }
8564 break;
8565 }
8566 else
8567 {
8568 /* p->balance == -1; growth of right side balances the node. */
8569 p->balance = 0;
8570 break;
8571 }
8572 }
8573
8574 r = p;
8575 p = p->parent;
8576 }
8577}
8578
8579/* Build AVL tree from a sorted chain. */
8580
8581static void
a1e3b3d9 8582set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8583{
4038c495
GB
8584 unsigned HOST_WIDE_INT ix;
8585 tree index, value;
3e4093b6
RS
8586
8587 if (TREE_CODE (constructor_type) != RECORD_TYPE
8588 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8589 return;
8590
4038c495 8591 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8592 add_pending_init (input_location, index, value, NULL_TREE, true,
8593 braced_init_obstack);
9771b263 8594 constructor_elements = NULL;
3e4093b6
RS
8595 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8596 {
8597 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8598 /* Skip any nameless bit fields at the beginning. */
8599 while (constructor_unfilled_fields != 0
8600 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8601 && DECL_NAME (constructor_unfilled_fields) == 0)
8602 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8603
de520661 8604 }
3e4093b6 8605 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8606 {
3e4093b6
RS
8607 if (TYPE_DOMAIN (constructor_type))
8608 constructor_unfilled_index
8609 = convert (bitsizetype,
8610 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8611 else
8612 constructor_unfilled_index = bitsize_zero_node;
de520661 8613 }
3e4093b6 8614 constructor_incremental = 0;
de520661 8615}
400fbf9f 8616
3e4093b6 8617/* Build AVL tree from a string constant. */
de520661 8618
3e4093b6 8619static void
a1e3b3d9
LB
8620set_nonincremental_init_from_string (tree str,
8621 struct obstack * braced_init_obstack)
de520661 8622{
3e4093b6
RS
8623 tree value, purpose, type;
8624 HOST_WIDE_INT val[2];
8625 const char *p, *end;
8626 int byte, wchar_bytes, charwidth, bitpos;
de520661 8627
366de0ce 8628 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8629
c466b2cd 8630 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6 8631 charwidth = TYPE_PRECISION (char_type_node);
2f1364c2
JJ
8632 gcc_assert ((size_t) wchar_bytes * charwidth
8633 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
3e4093b6
RS
8634 type = TREE_TYPE (constructor_type);
8635 p = TREE_STRING_POINTER (str);
8636 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8637
3e4093b6 8638 for (purpose = bitsize_zero_node;
8824edff
JJ
8639 p < end
8640 && !(constructor_max_index
8641 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8642 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8643 {
3e4093b6 8644 if (wchar_bytes == 1)
ffc5c6a9 8645 {
807e902e
KZ
8646 val[0] = (unsigned char) *p++;
8647 val[1] = 0;
ffc5c6a9
RH
8648 }
8649 else
3e4093b6 8650 {
3e4093b6 8651 val[1] = 0;
807e902e 8652 val[0] = 0;
3e4093b6
RS
8653 for (byte = 0; byte < wchar_bytes; byte++)
8654 {
8655 if (BYTES_BIG_ENDIAN)
8656 bitpos = (wchar_bytes - byte - 1) * charwidth;
8657 else
8658 bitpos = byte * charwidth;
2f1364c2 8659 val[bitpos / HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8660 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8661 << (bitpos % HOST_BITS_PER_WIDE_INT);
8662 }
8663 }
584ef5fe 8664
8df83eae 8665 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8666 {
8667 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8668 if (bitpos < HOST_BITS_PER_WIDE_INT)
8669 {
e3fe09c1 8670 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
3e4093b6 8671 {
aa256c4a 8672 val[0] |= HOST_WIDE_INT_M1U << bitpos;
807e902e 8673 val[1] = -1;
3e4093b6
RS
8674 }
8675 }
8676 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8677 {
807e902e
KZ
8678 if (val[0] < 0)
8679 val[1] = -1;
3e4093b6 8680 }
e3fe09c1 8681 else if (val[1] & (HOST_WIDE_INT_1
3e4093b6 8682 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
aa256c4a 8683 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
3e4093b6 8684 }
ffc5c6a9 8685
807e902e
KZ
8686 value = wide_int_to_tree (type,
8687 wide_int::from_array (val, 2,
8688 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8689 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8690 braced_init_obstack);
9dfcc8db
BH
8691 }
8692
3e4093b6
RS
8693 constructor_incremental = 0;
8694}
de520661 8695
3e4093b6
RS
8696/* Return value of FIELD in pending initializer or zero if the field was
8697 not initialized yet. */
8698
8699static tree
a1e3b3d9 8700find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8701{
8702 struct init_node *p;
8703
8704 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8705 {
3e4093b6
RS
8706 if (constructor_incremental
8707 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8708 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8709
8710 p = constructor_pending_elts;
8711 while (p)
19d76e60 8712 {
3e4093b6
RS
8713 if (tree_int_cst_lt (field, p->purpose))
8714 p = p->left;
8715 else if (tree_int_cst_lt (p->purpose, field))
8716 p = p->right;
8717 else
8718 return p->value;
19d76e60 8719 }
19d76e60 8720 }
3e4093b6 8721 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8722 {
3e4093b6 8723 tree bitpos = bit_position (field);
de520661 8724
3e4093b6
RS
8725 if (constructor_incremental
8726 && (!constructor_unfilled_fields
8727 || tree_int_cst_lt (bitpos,
8728 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8729 set_nonincremental_init (braced_init_obstack);
de520661 8730
3e4093b6
RS
8731 p = constructor_pending_elts;
8732 while (p)
8733 {
8734 if (field == p->purpose)
8735 return p->value;
8736 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8737 p = p->left;
8738 else
8739 p = p->right;
8740 }
8741 }
8742 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8743 {
9771b263
DN
8744 if (!vec_safe_is_empty (constructor_elements)
8745 && (constructor_elements->last ().index == field))
8746 return constructor_elements->last ().value;
de520661 8747 }
3e4093b6 8748 return 0;
de520661
RS
8749}
8750
3e4093b6
RS
8751/* "Output" the next constructor element.
8752 At top level, really output it to assembler code now.
8753 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8754 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8755 TYPE is the data type that the containing data type wants here.
8756 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8757 If VALUE is a string constant, STRICT_STRING is true if it is
8758 unparenthesized or we should not warn here for it being parenthesized.
8759 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8760
3e4093b6
RS
8761 PENDING if non-nil means output pending elements that belong
8762 right after this element. (PENDING is normally 1;
b295aee2
JJ
8763 it is 0 while outputting pending elements, to avoid recursion.)
8764
8765 IMPLICIT is true if value comes from pop_init_level (1),
8766 the new initializer has been merged with the existing one
8767 and thus no warnings should be emitted about overriding an
8768 existing initializer. */
8b6a5902 8769
3e4093b6 8770static void
34cf811f
MP
8771output_init_element (location_t loc, tree value, tree origtype,
8772 bool strict_string, tree type, tree field, int pending,
8773 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8774{
8ce94e44 8775 tree semantic_type = NULL_TREE;
928c19bb
JM
8776 bool maybe_const = true;
8777 bool npc;
4038c495 8778
0a880880 8779 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8780 {
3e4093b6
RS
8781 constructor_erroneous = 1;
8782 return;
8b6a5902 8783 }
46bdb9cf
JM
8784 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8785 && (TREE_CODE (value) == STRING_CST
8786 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8787 && !(TREE_CODE (value) == STRING_CST
8788 && TREE_CODE (type) == ARRAY_TYPE
8789 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8790 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8791 TYPE_MAIN_VARIANT (type)))
c2255bc4 8792 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8793
3e4093b6 8794 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8795 && require_constant_value && pending)
8b6a5902 8796 {
3e4093b6
RS
8797 /* As an extension, allow initializing objects with static storage
8798 duration with compound literals (which are then treated just as
8799 the brace enclosed list they contain). */
4435bb92
MP
8800 if (flag_isoc99)
8801 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8802 "constant");
3e4093b6
RS
8803 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8804 value = DECL_INITIAL (decl);
8b6a5902
JJ
8805 }
8806
928c19bb 8807 npc = null_pointer_constant_p (value);
8ce94e44
JM
8808 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8809 {
8810 semantic_type = TREE_TYPE (value);
8811 value = TREE_OPERAND (value, 0);
8812 }
928c19bb
JM
8813 value = c_fully_fold (value, require_constant_value, &maybe_const);
8814
3e4093b6
RS
8815 if (value == error_mark_node)
8816 constructor_erroneous = 1;
8817 else if (!TREE_CONSTANT (value))
8818 constructor_constant = 0;
ee45a32d
EB
8819 else if (!initializer_constant_valid_p (value,
8820 TREE_TYPE (value),
8821 AGGREGATE_TYPE_P (constructor_type)
8822 && TYPE_REVERSE_STORAGE_ORDER
8823 (constructor_type))
3e636daf 8824 || (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6
RS
8825 && DECL_C_BIT_FIELD (field)
8826 && TREE_CODE (value) != INTEGER_CST))
8827 constructor_simple = 0;
928c19bb
JM
8828 if (!maybe_const)
8829 constructor_nonconst = 1;
3e4093b6 8830
4378d117
MS
8831 /* Digest the initializer and issue any errors about incompatible
8832 types before issuing errors about non-constant initializers. */
8833 tree new_value = value;
8834 if (semantic_type)
8835 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8836 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
8837 require_constant_value);
8838 if (new_value == error_mark_node)
8839 {
8840 constructor_erroneous = 1;
8841 return;
8842 }
8843 if (require_constant_value || require_constant_elements)
8844 constant_expression_warning (new_value);
8845
8846 /* Proceed to check the constness of the original initializer. */
116df786 8847 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8848 {
116df786
RH
8849 if (require_constant_value)
8850 {
ea58ef42 8851 error_init (loc, "initializer element is not constant");
116df786
RH
8852 value = error_mark_node;
8853 }
8854 else if (require_constant_elements)
8337d1db 8855 pedwarn (loc, OPT_Wpedantic,
509c9d60 8856 "initializer element is not computable at load time");
8b6a5902 8857 }
928c19bb
JM
8858 else if (!maybe_const
8859 && (require_constant_value || require_constant_elements))
8337d1db 8860 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8861 "initializer element is not a constant expression");
3e4093b6 8862
81f40b79
ILT
8863 /* Issue -Wc++-compat warnings about initializing a bitfield with
8864 enum type. */
8865 if (warn_cxx_compat
8866 && field != NULL_TREE
8867 && TREE_CODE (field) == FIELD_DECL
8868 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8869 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8870 != TYPE_MAIN_VARIANT (type))
8871 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8872 {
8873 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8874 if (checktype != error_mark_node
8875 && (TYPE_MAIN_VARIANT (checktype)
8876 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8877 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8878 "enum conversion in initialization is invalid in C++");
8879 }
8880
3e4093b6
RS
8881 /* If this field is empty (and not at the end of structure),
8882 don't do anything other than checking the initializer. */
8883 if (field
8884 && (TREE_TYPE (field) == error_mark_node
8885 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8886 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8887 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8888 || DECL_CHAIN (field)))))
3e4093b6
RS
8889 return;
8890
4378d117
MS
8891 /* Finally, set VALUE to the initializer value digested above. */
8892 value = new_value;
8b6a5902 8893
3e4093b6
RS
8894 /* If this element doesn't come next in sequence,
8895 put it on constructor_pending_elts. */
8896 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8897 && (!constructor_incremental
8898 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8899 {
3e4093b6
RS
8900 if (constructor_incremental
8901 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8902 set_nonincremental_init (braced_init_obstack);
3e4093b6 8903
96b40f8d 8904 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8905 braced_init_obstack);
3e4093b6 8906 return;
8b6a5902 8907 }
3e4093b6
RS
8908 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8909 && (!constructor_incremental
8910 || field != constructor_unfilled_fields))
8b6a5902 8911 {
3e4093b6
RS
8912 /* We do this for records but not for unions. In a union,
8913 no matter which field is specified, it can be initialized
8914 right away since it starts at the beginning of the union. */
8915 if (constructor_incremental)
8916 {
8917 if (!constructor_unfilled_fields)
a1e3b3d9 8918 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8919 else
8920 {
8921 tree bitpos, unfillpos;
8922
8923 bitpos = bit_position (field);
8924 unfillpos = bit_position (constructor_unfilled_fields);
8925
8926 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8927 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8928 }
8929 }
8930
96b40f8d 8931 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8932 braced_init_obstack);
3e4093b6 8933 return;
8b6a5902 8934 }
3e4093b6 8935 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8936 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8937 {
b295aee2
JJ
8938 if (!implicit)
8939 {
9771b263 8940 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
755e528f 8941 warning_init (loc, OPT_Woverride_init_side_effects,
b295aee2
JJ
8942 "initialized field with side-effects overwritten");
8943 else if (warn_override_init)
96b40f8d
MP
8944 warning_init (loc, OPT_Woverride_init,
8945 "initialized field overwritten");
b295aee2 8946 }
8b6a5902 8947
3e4093b6 8948 /* We can have just one union field set. */
9771b263 8949 constructor_elements = NULL;
3e4093b6 8950 }
8b6a5902 8951
3e4093b6
RS
8952 /* Otherwise, output this element either to
8953 constructor_elements or to the assembler file. */
8b6a5902 8954
f32682ca 8955 constructor_elt celt = {field, value};
9771b263 8956 vec_safe_push (constructor_elements, celt);
8b6a5902 8957
3e4093b6
RS
8958 /* Advance the variable that indicates sequential elements output. */
8959 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8960 constructor_unfilled_index
db3927fb
AH
8961 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8962 bitsize_one_node);
3e4093b6
RS
8963 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8964 {
8965 constructor_unfilled_fields
910ad8de 8966 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8967
3e4093b6
RS
8968 /* Skip any nameless bit fields. */
8969 while (constructor_unfilled_fields != 0
8970 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8971 && DECL_NAME (constructor_unfilled_fields) == 0)
8972 constructor_unfilled_fields =
910ad8de 8973 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8974 }
8975 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8976 constructor_unfilled_fields = 0;
de520661 8977
3e4093b6
RS
8978 /* Now output any pending elements which have become next. */
8979 if (pending)
a1e3b3d9 8980 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8981}
8b6a5902 8982
3e4093b6
RS
8983/* Output any pending elements which have become next.
8984 As we output elements, constructor_unfilled_{fields,index}
8985 advances, which may cause other elements to become next;
8986 if so, they too are output.
8b6a5902 8987
3e4093b6
RS
8988 If ALL is 0, we return when there are
8989 no more pending elements to output now.
665f2503 8990
3e4093b6
RS
8991 If ALL is 1, we output space as necessary so that
8992 we can output all the pending elements. */
3e4093b6 8993static void
a1e3b3d9 8994output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8995{
8996 struct init_node *elt = constructor_pending_elts;
8997 tree next;
de520661 8998
3e4093b6
RS
8999 retry:
9000
ba228239 9001 /* Look through the whole pending tree.
3e4093b6
RS
9002 If we find an element that should be output now,
9003 output it. Otherwise, set NEXT to the element
9004 that comes first among those still pending. */
9005
9006 next = 0;
9007 while (elt)
9008 {
9009 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 9010 {
3e4093b6
RS
9011 if (tree_int_cst_equal (elt->purpose,
9012 constructor_unfilled_index))
34cf811f
MP
9013 output_init_element (input_location, elt->value, elt->origtype,
9014 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
9015 constructor_unfilled_index, 0, false,
9016 braced_init_obstack);
3e4093b6
RS
9017 else if (tree_int_cst_lt (constructor_unfilled_index,
9018 elt->purpose))
8b6a5902 9019 {
3e4093b6
RS
9020 /* Advance to the next smaller node. */
9021 if (elt->left)
9022 elt = elt->left;
9023 else
9024 {
9025 /* We have reached the smallest node bigger than the
9026 current unfilled index. Fill the space first. */
9027 next = elt->purpose;
9028 break;
9029 }
8b6a5902 9030 }
ce662d4c
JJ
9031 else
9032 {
3e4093b6
RS
9033 /* Advance to the next bigger node. */
9034 if (elt->right)
9035 elt = elt->right;
9036 else
ce662d4c 9037 {
3e4093b6
RS
9038 /* We have reached the biggest node in a subtree. Find
9039 the parent of it, which is the next bigger node. */
9040 while (elt->parent && elt->parent->right == elt)
9041 elt = elt->parent;
9042 elt = elt->parent;
9043 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9044 elt->purpose))
9045 {
9046 next = elt->purpose;
9047 break;
9048 }
ce662d4c
JJ
9049 }
9050 }
8b6a5902 9051 }
3e636daf 9052 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
9053 {
9054 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 9055
3e4093b6
RS
9056 /* If the current record is complete we are done. */
9057 if (constructor_unfilled_fields == 0)
9058 break;
de520661 9059
3e4093b6
RS
9060 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9061 elt_bitpos = bit_position (elt->purpose);
9062 /* We can't compare fields here because there might be empty
9063 fields in between. */
9064 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9065 {
9066 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
9067 output_init_element (input_location, elt->value, elt->origtype,
9068 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
9069 elt->purpose, 0, false,
9070 braced_init_obstack);
3e4093b6
RS
9071 }
9072 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9073 {
9074 /* Advance to the next smaller node. */
9075 if (elt->left)
9076 elt = elt->left;
9077 else
9078 {
9079 /* We have reached the smallest node bigger than the
9080 current unfilled field. Fill the space first. */
9081 next = elt->purpose;
9082 break;
9083 }
9084 }
9085 else
9086 {
9087 /* Advance to the next bigger node. */
9088 if (elt->right)
9089 elt = elt->right;
9090 else
9091 {
9092 /* We have reached the biggest node in a subtree. Find
9093 the parent of it, which is the next bigger node. */
9094 while (elt->parent && elt->parent->right == elt)
9095 elt = elt->parent;
9096 elt = elt->parent;
9097 if (elt
9098 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9099 bit_position (elt->purpose))))
9100 {
9101 next = elt->purpose;
9102 break;
9103 }
9104 }
9105 }
9106 }
9107 }
de520661 9108
3e4093b6
RS
9109 /* Ordinarily return, but not if we want to output all
9110 and there are elements left. */
3f75a254 9111 if (!(all && next != 0))
e5cfb88f
RK
9112 return;
9113
3e4093b6
RS
9114 /* If it's not incremental, just skip over the gap, so that after
9115 jumping to retry we will output the next successive element. */
3e636daf 9116 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
9117 constructor_unfilled_fields = next;
9118 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9119 constructor_unfilled_index = next;
de520661 9120
3e4093b6
RS
9121 /* ELT now points to the node in the pending tree with the next
9122 initializer to output. */
9123 goto retry;
de520661
RS
9124}
9125\f
3e4093b6
RS
9126/* Add one non-braced element to the current constructor level.
9127 This adjusts the current position within the constructor's type.
9128 This may also start or terminate implicit levels
9129 to handle a partly-braced initializer.
e5e809f4 9130
3e4093b6 9131 Once this has found the correct level for the new element,
b295aee2
JJ
9132 it calls output_init_element.
9133
9134 IMPLICIT is true if value comes from pop_init_level (1),
9135 the new initializer has been merged with the existing one
9136 and thus no warnings should be emitted about overriding an
9137 existing initializer. */
3e4093b6
RS
9138
9139void
34cf811f 9140process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 9141 struct obstack * braced_init_obstack)
e5e809f4 9142{
916c5919
JM
9143 tree orig_value = value.value;
9144 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
9145 bool strict_string = value.original_code == STRING_CST;
340baef7 9146 bool was_designated = designator_depth != 0;
e5e809f4 9147
3e4093b6 9148 designator_depth = 0;
b06df647 9149 designator_erroneous = 0;
e5e809f4 9150
9bac5cbb
G
9151 if (!implicit && value.value && !integer_zerop (value.value))
9152 constructor_zeroinit = 0;
9153
3e4093b6
RS
9154 /* Handle superfluous braces around string cst as in
9155 char x[] = {"foo"}; */
9156 if (string_flag
9157 && constructor_type
340baef7 9158 && !was_designated
3e4093b6 9159 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 9160 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 9161 && integer_zerop (constructor_unfilled_index))
e5e809f4 9162 {
916c5919 9163 if (constructor_stack->replacement_value.value)
ea58ef42 9164 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
9165 constructor_stack->replacement_value = value;
9166 return;
e5e809f4 9167 }
8b6a5902 9168
916c5919 9169 if (constructor_stack->replacement_value.value != 0)
3e4093b6 9170 {
ea58ef42 9171 error_init (loc, "excess elements in struct initializer");
3e4093b6 9172 return;
e5e809f4
JL
9173 }
9174
3e4093b6
RS
9175 /* Ignore elements of a brace group if it is entirely superfluous
9176 and has already been diagnosed. */
9177 if (constructor_type == 0)
9178 return;
e5e809f4 9179
976d5a22
TT
9180 if (!implicit && warn_designated_init && !was_designated
9181 && TREE_CODE (constructor_type) == RECORD_TYPE
9182 && lookup_attribute ("designated_init",
9183 TYPE_ATTRIBUTES (constructor_type)))
9184 warning_init (loc,
9185 OPT_Wdesignated_init,
9186 "positional initialization of field "
9187 "in %<struct%> declared with %<designated_init%> attribute");
9188
3e4093b6
RS
9189 /* If we've exhausted any levels that didn't have braces,
9190 pop them now. */
9191 while (constructor_stack->implicit)
9192 {
3e636daf 9193 if (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 9194 && constructor_fields == 0)
ea58ef42
MP
9195 process_init_element (loc,
9196 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 9197 true, braced_init_obstack);
53650abe 9198 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
31521951 9199 || VECTOR_TYPE_P (constructor_type))
8824edff
JJ
9200 && constructor_max_index
9201 && tree_int_cst_lt (constructor_max_index,
9202 constructor_index))
ea58ef42
MP
9203 process_init_element (loc,
9204 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 9205 true, braced_init_obstack);
3e4093b6
RS
9206 else
9207 break;
9208 }
e5e809f4 9209
3e4093b6
RS
9210 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9211 if (constructor_range_stack)
e5e809f4 9212 {
3e4093b6
RS
9213 /* If value is a compound literal and we'll be just using its
9214 content, don't put it into a SAVE_EXPR. */
916c5919 9215 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
c3e38a03 9216 || !require_constant_value)
8ce94e44
JM
9217 {
9218 tree semantic_type = NULL_TREE;
9219 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9220 {
9221 semantic_type = TREE_TYPE (value.value);
9222 value.value = TREE_OPERAND (value.value, 0);
9223 }
9224 value.value = c_save_expr (value.value);
9225 if (semantic_type)
9226 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9227 value.value);
9228 }
3e4093b6 9229 }
e5e809f4 9230
3e4093b6
RS
9231 while (1)
9232 {
9233 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 9234 {
3e4093b6
RS
9235 tree fieldtype;
9236 enum tree_code fieldcode;
e5e809f4 9237
3e4093b6
RS
9238 if (constructor_fields == 0)
9239 {
ea58ef42 9240 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
9241 break;
9242 }
e5e809f4 9243
3e4093b6
RS
9244 fieldtype = TREE_TYPE (constructor_fields);
9245 if (fieldtype != error_mark_node)
9246 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9247 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9248
3e4093b6
RS
9249 /* Error for non-static initialization of a flexible array member. */
9250 if (fieldcode == ARRAY_TYPE
9251 && !require_constant_value
9252 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 9253 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 9254 {
ea58ef42
MP
9255 error_init (loc, "non-static initialization of a flexible "
9256 "array member");
3e4093b6
RS
9257 break;
9258 }
e5e809f4 9259
2cc901dc
MP
9260 /* Error for initialization of a flexible array member with
9261 a string constant if the structure is in an array. E.g.:
9262 struct S { int x; char y[]; };
9263 struct S s[] = { { 1, "foo" } };
9264 is invalid. */
9265 if (string_flag
9266 && fieldcode == ARRAY_TYPE
9267 && constructor_depth > 1
9268 && TYPE_SIZE (fieldtype) == NULL_TREE
9269 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9270 {
9271 bool in_array_p = false;
9272 for (struct constructor_stack *p = constructor_stack;
9273 p && p->type; p = p->next)
9274 if (TREE_CODE (p->type) == ARRAY_TYPE)
9275 {
9276 in_array_p = true;
9277 break;
9278 }
9279 if (in_array_p)
9280 {
9281 error_init (loc, "initialization of flexible array "
9282 "member in a nested context");
9283 break;
9284 }
9285 }
9286
3e4093b6 9287 /* Accept a string constant to initialize a subarray. */
916c5919 9288 if (value.value != 0
3e4093b6 9289 && fieldcode == ARRAY_TYPE
197463ae 9290 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9291 && string_flag)
916c5919 9292 value.value = orig_value;
3e4093b6
RS
9293 /* Otherwise, if we have come to a subaggregate,
9294 and we don't have an element of its type, push into it. */
0953878d 9295 else if (value.value != 0
916c5919
JM
9296 && value.value != error_mark_node
9297 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9298 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9299 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9300 {
ea58ef42 9301 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9302 continue;
9303 }
e5e809f4 9304
916c5919 9305 if (value.value)
3e4093b6
RS
9306 {
9307 push_member_name (constructor_fields);
34cf811f 9308 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9309 strict_string, fieldtype,
a1e3b3d9
LB
9310 constructor_fields, 1, implicit,
9311 braced_init_obstack);
3e4093b6 9312 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9313 }
9314 else
3e4093b6
RS
9315 /* Do the bookkeeping for an element that was
9316 directly output as a constructor. */
e5e809f4 9317 {
3e4093b6
RS
9318 /* For a record, keep track of end position of last field. */
9319 if (DECL_SIZE (constructor_fields))
c22cacf3 9320 constructor_bit_index
db3927fb
AH
9321 = size_binop_loc (input_location, PLUS_EXPR,
9322 bit_position (constructor_fields),
9323 DECL_SIZE (constructor_fields));
3e4093b6
RS
9324
9325 /* If the current field was the first one not yet written out,
9326 it isn't now, so update. */
9327 if (constructor_unfilled_fields == constructor_fields)
9328 {
910ad8de 9329 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9330 /* Skip any nameless bit fields. */
9331 while (constructor_unfilled_fields != 0
9332 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9333 && DECL_NAME (constructor_unfilled_fields) == 0)
9334 constructor_unfilled_fields =
910ad8de 9335 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 9336 }
e5e809f4 9337 }
3e4093b6 9338
910ad8de 9339 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9340 /* Skip any nameless bit fields at the beginning. */
9341 while (constructor_fields != 0
9342 && DECL_C_BIT_FIELD (constructor_fields)
9343 && DECL_NAME (constructor_fields) == 0)
910ad8de 9344 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9345 }
3e4093b6 9346 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 9347 {
3e4093b6
RS
9348 tree fieldtype;
9349 enum tree_code fieldcode;
e5e809f4 9350
3e4093b6
RS
9351 if (constructor_fields == 0)
9352 {
d033409e 9353 pedwarn_init (loc, 0,
509c9d60 9354 "excess elements in union initializer");
3e4093b6
RS
9355 break;
9356 }
e5e809f4 9357
3e4093b6
RS
9358 fieldtype = TREE_TYPE (constructor_fields);
9359 if (fieldtype != error_mark_node)
9360 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9361 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9362
3e4093b6
RS
9363 /* Warn that traditional C rejects initialization of unions.
9364 We skip the warning if the value is zero. This is done
9365 under the assumption that the zero initializer in user
9366 code appears conditioned on e.g. __STDC__ to avoid
9367 "missing initializer" warnings and relies on default
9368 initialization to zero in the traditional C case.
9369 We also skip the warning if the initializer is designated,
9370 again on the assumption that this must be conditional on
9371 __STDC__ anyway (and we've already complained about the
9372 member-designator already). */
8400e75e 9373 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
9374 && !(value.value && (integer_zerop (value.value)
9375 || real_zerop (value.value))))
3176a0c2
DD
9376 warning (OPT_Wtraditional, "traditional C rejects initialization "
9377 "of unions");
e5e809f4 9378
3e4093b6 9379 /* Accept a string constant to initialize a subarray. */
916c5919 9380 if (value.value != 0
3e4093b6 9381 && fieldcode == ARRAY_TYPE
197463ae 9382 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9383 && string_flag)
916c5919 9384 value.value = orig_value;
3e4093b6
RS
9385 /* Otherwise, if we have come to a subaggregate,
9386 and we don't have an element of its type, push into it. */
0953878d 9387 else if (value.value != 0
916c5919
JM
9388 && value.value != error_mark_node
9389 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9390 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9391 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9392 {
ea58ef42 9393 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9394 continue;
9395 }
e5e809f4 9396
916c5919 9397 if (value.value)
3e4093b6
RS
9398 {
9399 push_member_name (constructor_fields);
34cf811f 9400 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9401 strict_string, fieldtype,
a1e3b3d9
LB
9402 constructor_fields, 1, implicit,
9403 braced_init_obstack);
3e4093b6 9404 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9405 }
9406 else
3e4093b6
RS
9407 /* Do the bookkeeping for an element that was
9408 directly output as a constructor. */
e5e809f4 9409 {
3e4093b6 9410 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 9411 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9412 }
e5e809f4 9413
3e4093b6
RS
9414 constructor_fields = 0;
9415 }
9416 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9417 {
9418 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9419 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 9420
3e4093b6 9421 /* Accept a string constant to initialize a subarray. */
916c5919 9422 if (value.value != 0
3e4093b6 9423 && eltcode == ARRAY_TYPE
197463ae 9424 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 9425 && string_flag)
916c5919 9426 value.value = orig_value;
3e4093b6
RS
9427 /* Otherwise, if we have come to a subaggregate,
9428 and we don't have an element of its type, push into it. */
0953878d 9429 else if (value.value != 0
916c5919
JM
9430 && value.value != error_mark_node
9431 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9432 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9433 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9434 {
ea58ef42 9435 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9436 continue;
9437 }
8b6a5902 9438
3e4093b6
RS
9439 if (constructor_max_index != 0
9440 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9441 || integer_all_onesp (constructor_max_index)))
9442 {
d033409e 9443 pedwarn_init (loc, 0,
509c9d60 9444 "excess elements in array initializer");
3e4093b6
RS
9445 break;
9446 }
8b6a5902 9447
3e4093b6 9448 /* Now output the actual element. */
916c5919 9449 if (value.value)
3e4093b6 9450 {
ae7e9ddd 9451 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9452 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9453 strict_string, elttype,
a1e3b3d9
LB
9454 constructor_index, 1, implicit,
9455 braced_init_obstack);
3e4093b6
RS
9456 RESTORE_SPELLING_DEPTH (constructor_depth);
9457 }
2f6e4e97 9458
3e4093b6 9459 constructor_index
db3927fb
AH
9460 = size_binop_loc (input_location, PLUS_EXPR,
9461 constructor_index, bitsize_one_node);
8b6a5902 9462
916c5919 9463 if (!value.value)
3e4093b6
RS
9464 /* If we are doing the bookkeeping for an element that was
9465 directly output as a constructor, we must update
9466 constructor_unfilled_index. */
9467 constructor_unfilled_index = constructor_index;
9468 }
31521951 9469 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
9470 {
9471 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9472
c22cacf3
MS
9473 /* Do a basic check of initializer size. Note that vectors
9474 always have a fixed size derived from their type. */
3e4093b6
RS
9475 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9476 {
d033409e 9477 pedwarn_init (loc, 0,
509c9d60 9478 "excess elements in vector initializer");
3e4093b6
RS
9479 break;
9480 }
8b6a5902 9481
3e4093b6 9482 /* Now output the actual element. */
916c5919 9483 if (value.value)
53650abe
AP
9484 {
9485 if (TREE_CODE (value.value) == VECTOR_CST)
9486 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9487 output_init_element (loc, value.value, value.original_type,
53650abe 9488 strict_string, elttype,
a1e3b3d9
LB
9489 constructor_index, 1, implicit,
9490 braced_init_obstack);
53650abe 9491 }
8b6a5902 9492
3e4093b6 9493 constructor_index
db3927fb
AH
9494 = size_binop_loc (input_location,
9495 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9496
916c5919 9497 if (!value.value)
3e4093b6
RS
9498 /* If we are doing the bookkeeping for an element that was
9499 directly output as a constructor, we must update
9500 constructor_unfilled_index. */
9501 constructor_unfilled_index = constructor_index;
9502 }
8b6a5902 9503
3e4093b6
RS
9504 /* Handle the sole element allowed in a braced initializer
9505 for a scalar variable. */
b4519d39
SB
9506 else if (constructor_type != error_mark_node
9507 && constructor_fields == 0)
8b6a5902 9508 {
d033409e 9509 pedwarn_init (loc, 0,
509c9d60 9510 "excess elements in scalar initializer");
3e4093b6 9511 break;
8b6a5902
JJ
9512 }
9513 else
9514 {
916c5919 9515 if (value.value)
34cf811f 9516 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9517 strict_string, constructor_type,
a1e3b3d9
LB
9518 NULL_TREE, 1, implicit,
9519 braced_init_obstack);
3e4093b6 9520 constructor_fields = 0;
8b6a5902
JJ
9521 }
9522
3e4093b6
RS
9523 /* Handle range initializers either at this level or anywhere higher
9524 in the designator stack. */
9525 if (constructor_range_stack)
8b6a5902 9526 {
3e4093b6
RS
9527 struct constructor_range_stack *p, *range_stack;
9528 int finish = 0;
9529
9530 range_stack = constructor_range_stack;
9531 constructor_range_stack = 0;
9532 while (constructor_stack != range_stack->stack)
8b6a5902 9533 {
366de0ce 9534 gcc_assert (constructor_stack->implicit);
34cf811f 9535 process_init_element (loc,
ea58ef42
MP
9536 pop_init_level (loc, 1,
9537 braced_init_obstack),
a1e3b3d9 9538 true, braced_init_obstack);
8b6a5902 9539 }
3e4093b6
RS
9540 for (p = range_stack;
9541 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9542 p = p->prev)
8b6a5902 9543 {
366de0ce 9544 gcc_assert (constructor_stack->implicit);
34cf811f 9545 process_init_element (loc,
ea58ef42
MP
9546 pop_init_level (loc, 1,
9547 braced_init_obstack),
a1e3b3d9 9548 true, braced_init_obstack);
8b6a5902 9549 }
3e4093b6 9550
db3927fb
AH
9551 p->index = size_binop_loc (input_location,
9552 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9553 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9554 finish = 1;
9555
9556 while (1)
9557 {
9558 constructor_index = p->index;
9559 constructor_fields = p->fields;
9560 if (finish && p->range_end && p->index == p->range_start)
9561 {
9562 finish = 0;
9563 p->prev = 0;
9564 }
9565 p = p->next;
9566 if (!p)
9567 break;
16595a1f 9568 finish_implicit_inits (loc, braced_init_obstack);
ea58ef42 9569 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9570 p->stack = constructor_stack;
9571 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9572 p->index = p->range_start;
9573 }
9574
9575 if (!finish)
9576 constructor_range_stack = range_stack;
9577 continue;
8b6a5902
JJ
9578 }
9579
3e4093b6 9580 break;
8b6a5902
JJ
9581 }
9582
3e4093b6
RS
9583 constructor_range_stack = 0;
9584}
9585\f
9f0e2d86
ZW
9586/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9587 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9588 an ASM_EXPR node). */
3e4093b6 9589tree
9f0e2d86 9590build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9591{
6de9cd9a
DN
9592 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9593 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9594 return add_stmt (args);
8b6a5902
JJ
9595}
9596
9f0e2d86
ZW
9597/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9598 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9599 SIMPLE indicates whether there was anything at all after the
9600 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9601 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9602tree
c2255bc4 9603build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9604 tree clobbers, tree labels, bool simple)
e5e809f4 9605{
3e4093b6 9606 tree tail;
9f0e2d86 9607 tree args;
6de9cd9a
DN
9608 int i;
9609 const char *constraint;
74f0c611 9610 const char **oconstraints;
6de9cd9a 9611 bool allows_mem, allows_reg, is_inout;
74f0c611 9612 int ninputs, noutputs;
6de9cd9a
DN
9613
9614 ninputs = list_length (inputs);
9615 noutputs = list_length (outputs);
74f0c611
RH
9616 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9617
1c384bf1 9618 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9619
6de9cd9a
DN
9620 /* Remove output conversions that change the type but not the mode. */
9621 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9622 {
3e4093b6 9623 tree output = TREE_VALUE (tail);
74f0c611 9624
eadd3d0d
JJ
9625 output = c_fully_fold (output, false, NULL);
9626
74f0c611
RH
9627 /* ??? Really, this should not be here. Users should be using a
9628 proper lvalue, dammit. But there's a long history of using casts
9629 in the output operands. In cases like longlong.h, this becomes a
9630 primitive form of typechecking -- if the cast can be removed, then
9631 the output operand had a type of the proper width; otherwise we'll
9632 get an error. Gross, but ... */
3e4093b6 9633 STRIP_NOPS (output);
74f0c611 9634
7bd11157 9635 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9636 output = error_mark_node;
8b6a5902 9637
5544530a
PB
9638 if (output != error_mark_node
9639 && (TREE_READONLY (output)
9640 || TYPE_READONLY (TREE_TYPE (output))
3e636daf 9641 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
5544530a 9642 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9643 readonly_error (loc, output, lv_asm);
5544530a 9644
6de9cd9a 9645 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9646 oconstraints[i] = constraint;
9647
9648 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9649 &allows_mem, &allows_reg, &is_inout))
9650 {
9651 /* If the operand is going to end up in memory,
9652 mark it addressable. */
9653 if (!allows_reg && !c_mark_addressable (output))
9654 output = error_mark_node;
bae5cddf
JJ
9655 if (!(!allows_reg && allows_mem)
9656 && output != error_mark_node
9657 && VOID_TYPE_P (TREE_TYPE (output)))
9658 {
9659 error_at (loc, "invalid use of void expression");
9660 output = error_mark_node;
9661 }
74f0c611
RH
9662 }
9663 else
c22cacf3 9664 output = error_mark_node;
3e4093b6 9665
74f0c611 9666 TREE_VALUE (tail) = output;
8b6a5902 9667 }
3e4093b6 9668
74f0c611
RH
9669 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9670 {
9671 tree input;
9672
9673 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9674 input = TREE_VALUE (tail);
9675
74f0c611
RH
9676 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9677 oconstraints, &allows_mem, &allows_reg))
9678 {
9679 /* If the operand is going to end up in memory,
9680 mark it addressable. */
b4c33883
AP
9681 if (!allows_reg && allows_mem)
9682 {
eadd3d0d
JJ
9683 input = c_fully_fold (input, false, NULL);
9684
b4c33883
AP
9685 /* Strip the nops as we allow this case. FIXME, this really
9686 should be rejected or made deprecated. */
9687 STRIP_NOPS (input);
9688 if (!c_mark_addressable (input))
9689 input = error_mark_node;
bae5cddf 9690 }
eadd3d0d 9691 else
bae5cddf 9692 {
eadd3d0d
JJ
9693 struct c_expr expr;
9694 memset (&expr, 0, sizeof (expr));
9695 expr.value = input;
267bac10 9696 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9697 input = c_fully_fold (expr.value, false, NULL);
9698
9699 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9700 {
9701 error_at (loc, "invalid use of void expression");
9702 input = error_mark_node;
9703 }
bae5cddf 9704 }
74f0c611
RH
9705 }
9706 else
9707 input = error_mark_node;
9708
9709 TREE_VALUE (tail) = input;
9710 }
3e4093b6 9711
1c384bf1
RH
9712 /* ASMs with labels cannot have outputs. This should have been
9713 enforced by the parser. */
9714 gcc_assert (outputs == NULL || labels == NULL);
9715
9716 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9717
5544530a
PB
9718 /* asm statements without outputs, including simple ones, are treated
9719 as volatile. */
9720 ASM_INPUT_P (args) = simple;
9721 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9722
9f0e2d86 9723 return args;
e5e809f4 9724}
3e4093b6 9725\f
c2255bc4
AH
9726/* Generate a goto statement to LABEL. LOC is the location of the
9727 GOTO. */
506e2710
RH
9728
9729tree
c2255bc4 9730c_finish_goto_label (location_t loc, tree label)
506e2710 9731{
e1b7793c 9732 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9733 if (!decl)
9734 return NULL_TREE;
506e2710 9735 TREE_USED (decl) = 1;
c2255bc4
AH
9736 {
9737 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9738 SET_EXPR_LOCATION (t, loc);
9739 return add_stmt (t);
9740 }
506e2710
RH
9741}
9742
c2255bc4
AH
9743/* Generate a computed goto statement to EXPR. LOC is the location of
9744 the GOTO. */
506e2710
RH
9745
9746tree
c2255bc4 9747c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9748{
c2255bc4 9749 tree t;
c1771a20 9750 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9751 expr = c_fully_fold (expr, false, NULL);
506e2710 9752 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9753 t = build1 (GOTO_EXPR, void_type_node, expr);
9754 SET_EXPR_LOCATION (t, loc);
9755 return add_stmt (t);
506e2710
RH
9756}
9757
5088b058 9758/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9759 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9760 the location of the return statement, or the location of the expression,
9761 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9762 is the original type of RETVAL. */
de520661 9763
506e2710 9764tree
c2255bc4 9765c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9766{
0c9b182b
JJ
9767 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9768 bool no_warning = false;
928c19bb 9769 bool npc = false;
36536d79 9770 size_t rank = 0;
3e4093b6 9771
de8ddd5f
MP
9772 /* Use the expansion point to handle cases such as returning NULL
9773 in a function returning void. */
9774 source_location xloc = expansion_point_location_if_in_system_header (loc);
9775
3e4093b6 9776 if (TREE_THIS_VOLATILE (current_function_decl))
de8ddd5f 9777 warning_at (xloc, 0,
c2255bc4 9778 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9779
b72271b9 9780 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9781 {
9782 /* Array notations are allowed in a return statement if it is inside a
9783 built-in array notation reduction function. */
9784 if (!find_rank (loc, retval, retval, false, &rank))
9785 return error_mark_node;
9786 if (rank >= 1)
9787 {
9788 error_at (loc, "array notation expression cannot be used as a "
9789 "return value");
9790 return error_mark_node;
9791 }
9792 }
3af9c5e9 9793 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9794 {
9795 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9796 "allowed");
9797 return error_mark_node;
9798 }
928c19bb
JM
9799 if (retval)
9800 {
8ce94e44 9801 tree semantic_type = NULL_TREE;
928c19bb 9802 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9803 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9804 {
9805 semantic_type = TREE_TYPE (retval);
9806 retval = TREE_OPERAND (retval, 0);
9807 }
928c19bb 9808 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9809 if (semantic_type)
9810 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9811 }
9812
3e4093b6 9813 if (!retval)
de520661 9814 {
3e4093b6
RS
9815 current_function_returns_null = 1;
9816 if ((warn_return_type || flag_isoc99)
9817 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9818 {
94c40e19 9819 bool warned_here;
35aff4fb 9820 if (flag_isoc99)
94c40e19
DM
9821 warned_here = pedwarn
9822 (loc, 0,
9823 "%<return%> with no value, in function returning non-void");
35aff4fb 9824 else
94c40e19
DM
9825 warned_here = warning_at
9826 (loc, OPT_Wreturn_type,
9827 "%<return%> with no value, in function returning non-void");
0c9b182b 9828 no_warning = true;
94c40e19
DM
9829 if (warned_here)
9830 inform (DECL_SOURCE_LOCATION (current_function_decl),
9831 "declared here");
0c9b182b 9832 }
400fbf9f 9833 }
3e4093b6 9834 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9835 {
3e4093b6 9836 current_function_returns_null = 1;
94c40e19 9837 bool warned_here;
2397c575 9838 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
94c40e19
DM
9839 warned_here = pedwarn
9840 (xloc, 0,
9841 "%<return%> with a value, in function returning void");
b8698a0f 9842 else
94c40e19
DM
9843 warned_here = pedwarn
9844 (xloc, OPT_Wpedantic, "ISO C forbids "
9845 "%<return%> with expression, in function returning void");
9846 if (warned_here)
9847 inform (DECL_SOURCE_LOCATION (current_function_decl),
9848 "declared here");
de520661 9849 }
3e4093b6 9850 else
de520661 9851 {
68fca595
MP
9852 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9853 retval, origtype, ic_return,
c2255bc4 9854 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9855 tree res = DECL_RESULT (current_function_decl);
9856 tree inner;
9feb29df 9857 bool save;
3e4093b6
RS
9858
9859 current_function_returns_value = 1;
9860 if (t == error_mark_node)
506e2710 9861 return NULL_TREE;
3e4093b6 9862
9feb29df
JJ
9863 save = in_late_binary_op;
9864 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
9865 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9866 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9867 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9868 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9869 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9feb29df 9870 in_late_binary_op = true;
3e4093b6 9871 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9872 in_late_binary_op = save;
3e4093b6
RS
9873
9874 /* Strip any conversions, additions, and subtractions, and see if
9875 we are returning the address of a local variable. Warn if so. */
9876 while (1)
8b6a5902 9877 {
3e4093b6 9878 switch (TREE_CODE (inner))
8b6a5902 9879 {
849421a3
JJ
9880 CASE_CONVERT:
9881 case NON_LVALUE_EXPR:
3e4093b6 9882 case PLUS_EXPR:
849421a3 9883 case POINTER_PLUS_EXPR:
3e4093b6
RS
9884 inner = TREE_OPERAND (inner, 0);
9885 continue;
9886
9887 case MINUS_EXPR:
9888 /* If the second operand of the MINUS_EXPR has a pointer
9889 type (or is converted from it), this may be valid, so
9890 don't give a warning. */
9891 {
9892 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9893
3f75a254 9894 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9895 && (CONVERT_EXPR_P (op1)
9896 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9897 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9898
3e4093b6
RS
9899 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9900 break;
8b6a5902 9901
3e4093b6
RS
9902 inner = TREE_OPERAND (inner, 0);
9903 continue;
9904 }
400fbf9f 9905
3e4093b6
RS
9906 case ADDR_EXPR:
9907 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9908
6615c446 9909 while (REFERENCE_CLASS_P (inner)
22d03525 9910 && !INDIRECT_REF_P (inner))
3e4093b6 9911 inner = TREE_OPERAND (inner, 0);
8b6a5902 9912
a2f1f4c3 9913 if (DECL_P (inner)
3f75a254
JM
9914 && !DECL_EXTERNAL (inner)
9915 && !TREE_STATIC (inner)
3e4093b6 9916 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9917 {
9918 if (TREE_CODE (inner) == LABEL_DECL)
9919 warning_at (loc, OPT_Wreturn_local_addr,
9920 "function returns address of label");
9921 else
b4dfdc11
MG
9922 {
9923 warning_at (loc, OPT_Wreturn_local_addr,
9924 "function returns address of local variable");
9925 tree zero = build_zero_cst (TREE_TYPE (res));
9926 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9927 }
19fc9faa 9928 }
3e4093b6 9929 break;
8b6a5902 9930
3e4093b6
RS
9931 default:
9932 break;
9933 }
de520661 9934
3e4093b6
RS
9935 break;
9936 }
9937
53fb4de3 9938 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9939 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9940
9941 if (warn_sequence_point)
9942 verify_sequence_points (retval);
de520661 9943 }
8b6a5902 9944
c2255bc4 9945 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9946 TREE_NO_WARNING (ret_stmt) |= no_warning;
9947 return add_stmt (ret_stmt);
de520661 9948}
3e4093b6
RS
9949\f
9950struct c_switch {
604f5adf
ILT
9951 /* The SWITCH_EXPR being built. */
9952 tree switch_expr;
a6c0a76c 9953
89dbed81 9954 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9955 default conversion is applied. */
9956 tree orig_type;
9957
3e4093b6
RS
9958 /* A splay-tree mapping the low element of a case range to the high
9959 element, or NULL_TREE if there is no high element. Used to
9960 determine whether or not a new case label duplicates an old case
9961 label. We need a tree, rather than simply a hash table, because
9962 of the GNU case range extension. */
9963 splay_tree cases;
a6c0a76c 9964
e1b7793c
ILT
9965 /* The bindings at the point of the switch. This is used for
9966 warnings crossing decls when branching to a case label. */
9967 struct c_spot_bindings *bindings;
187230a7 9968
3e4093b6
RS
9969 /* The next node on the stack. */
9970 struct c_switch *next;
b155cfd9
MP
9971
9972 /* Remember whether the controlling expression had boolean type
9973 before integer promotions for the sake of -Wswitch-bool. */
9974 bool bool_cond_p;
9975
9976 /* Remember whether there was a case value that is outside the
9977 range of the ORIG_TYPE. */
9978 bool outside_range_p;
3e4093b6 9979};
400fbf9f 9980
3e4093b6
RS
9981/* A stack of the currently active switch statements. The innermost
9982 switch statement is on the top of the stack. There is no need to
9983 mark the stack for garbage collection because it is only active
9984 during the processing of the body of a function, and we never
9985 collect at that point. */
de520661 9986
506e2710 9987struct c_switch *c_switch_stack;
de520661 9988
3e4093b6 9989/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9990 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef 9991 SWITCH_COND_LOC is the location of the switch's condition.
b155cfd9 9992 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
de520661 9993
3e4093b6 9994tree
c2255bc4
AH
9995c_start_case (location_t switch_loc,
9996 location_t switch_cond_loc,
fedfecef 9997 tree exp, bool explicit_cast_p)
de520661 9998{
c58e8676 9999 tree orig_type = error_mark_node;
b155cfd9 10000 bool bool_cond_p = false;
3e4093b6 10001 struct c_switch *cs;
2f6e4e97 10002
3e4093b6 10003 if (exp != error_mark_node)
de520661 10004 {
3e4093b6
RS
10005 orig_type = TREE_TYPE (exp);
10006
c58e8676 10007 if (!INTEGRAL_TYPE_P (orig_type))
de520661 10008 {
c58e8676
VR
10009 if (orig_type != error_mark_node)
10010 {
c2255bc4 10011 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
10012 orig_type = error_mark_node;
10013 }
3e4093b6 10014 exp = integer_zero_node;
de520661 10015 }
3e4093b6 10016 else
de520661 10017 {
c58e8676 10018 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
10019 tree e = exp;
10020
10021 /* Warn if the condition has boolean value. */
10022 while (TREE_CODE (e) == COMPOUND_EXPR)
10023 e = TREE_OPERAND (e, 1);
10024
10025 if ((TREE_CODE (type) == BOOLEAN_TYPE
10026 || truth_value_p (TREE_CODE (e)))
10027 /* Explicit cast to int suppresses this warning. */
10028 && !(TREE_CODE (type) == INTEGER_TYPE
10029 && explicit_cast_p))
b155cfd9 10030 bool_cond_p = true;
8b6a5902 10031
8400e75e 10032 if (!in_system_header_at (input_location)
3e4093b6
RS
10033 && (type == long_integer_type_node
10034 || type == long_unsigned_type_node))
c2255bc4
AH
10035 warning_at (switch_cond_loc,
10036 OPT_Wtraditional, "%<long%> switch expression not "
10037 "converted to %<int%> in ISO C");
8b6a5902 10038
928c19bb 10039 exp = c_fully_fold (exp, false, NULL);
3e4093b6 10040 exp = default_conversion (exp);
ca085fd7
MLI
10041
10042 if (warn_sequence_point)
10043 verify_sequence_points (exp);
3e4093b6
RS
10044 }
10045 }
10046
604f5adf 10047 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 10048 cs = XNEW (struct c_switch);
604f5adf 10049 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 10050 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 10051 cs->orig_type = orig_type;
3e4093b6 10052 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 10053 cs->bindings = c_get_switch_bindings ();
b155cfd9
MP
10054 cs->bool_cond_p = bool_cond_p;
10055 cs->outside_range_p = false;
506e2710
RH
10056 cs->next = c_switch_stack;
10057 c_switch_stack = cs;
3e4093b6 10058
604f5adf 10059 return add_stmt (cs->switch_expr);
3e4093b6
RS
10060}
10061
c2255bc4 10062/* Process a case label at location LOC. */
3e4093b6
RS
10063
10064tree
c2255bc4 10065do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
10066{
10067 tree label = NULL_TREE;
10068
17cede2e
JM
10069 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10070 {
10071 low_value = c_fully_fold (low_value, false, NULL);
10072 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 10073 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
10074 "case label is not an integer constant expression");
10075 }
10076
10077 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10078 {
10079 high_value = c_fully_fold (high_value, false, NULL);
10080 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 10081 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
10082 "case label is not an integer constant expression");
10083 }
10084
e1b7793c 10085 if (c_switch_stack == NULL)
187230a7
JM
10086 {
10087 if (low_value)
e1b7793c 10088 error_at (loc, "case label not within a switch statement");
187230a7 10089 else
e1b7793c
ILT
10090 error_at (loc, "%<default%> label not within a switch statement");
10091 return NULL_TREE;
187230a7 10092 }
de520661 10093
e1b7793c
ILT
10094 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10095 EXPR_LOCATION (c_switch_stack->switch_expr),
10096 loc))
10097 return NULL_TREE;
10098
10099 label = c_add_case_label (loc, c_switch_stack->cases,
10100 SWITCH_COND (c_switch_stack->switch_expr),
10101 c_switch_stack->orig_type,
b155cfd9
MP
10102 low_value, high_value,
10103 &c_switch_stack->outside_range_p);
e1b7793c
ILT
10104 if (label == error_mark_node)
10105 label = NULL_TREE;
3e4093b6
RS
10106 return label;
10107}
de520661 10108
083e891e
MP
10109/* Finish the switch statement. TYPE is the original type of the
10110 controlling expression of the switch, or NULL_TREE. */
de520661 10111
3e4093b6 10112void
083e891e 10113c_finish_case (tree body, tree type)
3e4093b6 10114{
506e2710 10115 struct c_switch *cs = c_switch_stack;
fbc315db 10116 location_t switch_location;
3e4093b6 10117
604f5adf 10118 SWITCH_BODY (cs->switch_expr) = body;
325c3691 10119
6de9cd9a 10120 /* Emit warnings as needed. */
c2255bc4 10121 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 10122 c_do_switch_warnings (cs->cases, switch_location,
083e891e 10123 type ? type : TREE_TYPE (cs->switch_expr),
b155cfd9
MP
10124 SWITCH_COND (cs->switch_expr),
10125 cs->bool_cond_p, cs->outside_range_p);
6de9cd9a 10126
3e4093b6 10127 /* Pop the stack. */
506e2710 10128 c_switch_stack = cs->next;
3e4093b6 10129 splay_tree_delete (cs->cases);
e1b7793c 10130 c_release_switch_bindings (cs->bindings);
5d038c4c 10131 XDELETE (cs);
de520661 10132}
325c3691 10133\f
506e2710
RH
10134/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10135 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
99cd9857 10136 may be null. */
325c3691 10137
9e51cf9d 10138void
506e2710 10139c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
99cd9857 10140 tree else_block)
325c3691 10141{
506e2710 10142 tree stmt;
325c3691 10143
25c22937
BI
10144 /* If the condition has array notations, then the rank of the then_block and
10145 else_block must be either 0 or be equal to the rank of the condition. If
10146 the condition does not have array notations then break them up as it is
10147 broken up in a normal expression. */
b72271b9 10148 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
10149 {
10150 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10151 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10152 return;
10153 if (then_block
10154 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10155 return;
10156 if (else_block
10157 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10158 return;
10159 if (cond_rank != then_rank && then_rank != 0)
10160 {
10161 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10162 " and the then-block");
10163 return;
10164 }
10165 else if (cond_rank != else_rank && else_rank != 0)
10166 {
10167 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10168 " and the else-block");
10169 return;
10170 }
10171 }
16865eaa 10172
2214de30 10173 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 10174 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 10175 add_stmt (stmt);
325c3691
RH
10176}
10177
506e2710
RH
10178/* Emit a general-purpose loop construct. START_LOCUS is the location of
10179 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10180 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 10181 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 10182 the continue label. Everything is allowed to be NULL. */
325c3691
RH
10183
10184void
506e2710
RH
10185c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10186 tree blab, tree clab, bool cond_is_first)
325c3691 10187{
506e2710
RH
10188 tree entry = NULL, exit = NULL, t;
10189
e5e44252
AK
10190 /* In theory could forbid cilk spawn for loop increment expression,
10191 but it should work just fine. */
36536d79 10192
28af952a
RS
10193 /* If the condition is zero don't generate a loop construct. */
10194 if (cond && integer_zerop (cond))
10195 {
10196 if (cond_is_first)
10197 {
10198 t = build_and_jump (&blab);
10199 SET_EXPR_LOCATION (t, start_locus);
10200 add_stmt (t);
10201 }
10202 }
10203 else
506e2710
RH
10204 {
10205 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 10206
506e2710 10207 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
10208 out of the loop, or to the top of it. If there's no exit condition,
10209 then we just build a jump back to the top. */
506e2710 10210 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 10211
28af952a 10212 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
10213 {
10214 /* Canonicalize the loop condition to the end. This means
10215 generating a branch to the loop condition. Reuse the
10216 continue label, if possible. */
10217 if (cond_is_first)
10218 {
10219 if (incr || !clab)
10220 {
10221 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10222 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10223 }
10224 else
10225 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 10226 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
10227 add_stmt (t);
10228 }
10229
506e2710 10230 t = build_and_jump (&blab);
506e2710 10231 if (cond_is_first)
db3927fb
AH
10232 exit = fold_build3_loc (start_locus,
10233 COND_EXPR, void_type_node, cond, exit, t);
506e2710 10234 else
db3927fb
AH
10235 exit = fold_build3_loc (input_location,
10236 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3 10237 }
fc402eec
AA
10238 else
10239 {
10240 /* For the backward-goto's location of an unconditional loop
10241 use the beginning of the body, or, if there is none, the
10242 top of the loop. */
10243 location_t loc = EXPR_LOCATION (expr_first (body));
10244 if (loc == UNKNOWN_LOCATION)
10245 loc = start_locus;
10246 SET_EXPR_LOCATION (exit, loc);
10247 }
c22cacf3 10248
506e2710
RH
10249 add_stmt (top);
10250 }
c22cacf3 10251
506e2710
RH
10252 if (body)
10253 add_stmt (body);
10254 if (clab)
10255 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10256 if (incr)
10257 add_stmt (incr);
10258 if (entry)
10259 add_stmt (entry);
10260 if (exit)
10261 add_stmt (exit);
10262 if (blab)
10263 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 10264}
325c3691
RH
10265
10266tree
c2255bc4 10267c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 10268{
089efaa4 10269 bool skip;
506e2710 10270 tree label = *label_p;
325c3691 10271
089efaa4
ILT
10272 /* In switch statements break is sometimes stylistically used after
10273 a return statement. This can lead to spurious warnings about
10274 control reaching the end of a non-void function when it is
10275 inlined. Note that we are calling block_may_fallthru with
10276 language specific tree nodes; this works because
10277 block_may_fallthru returns true when given something it does not
10278 understand. */
10279 skip = !block_may_fallthru (cur_stmt_list);
10280
506e2710 10281 if (!label)
089efaa4
ILT
10282 {
10283 if (!skip)
c2255bc4 10284 *label_p = label = create_artificial_label (loc);
089efaa4 10285 }
953ff289
DN
10286 else if (TREE_CODE (label) == LABEL_DECL)
10287 ;
10288 else switch (TREE_INT_CST_LOW (label))
506e2710 10289 {
953ff289 10290 case 0:
506e2710 10291 if (is_break)
c2255bc4 10292 error_at (loc, "break statement not within loop or switch");
506e2710 10293 else
c2255bc4 10294 error_at (loc, "continue statement not within a loop");
506e2710 10295 return NULL_TREE;
953ff289
DN
10296
10297 case 1:
10298 gcc_assert (is_break);
c2255bc4 10299 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
10300 return NULL_TREE;
10301
c02065fc
AH
10302 case 2:
10303 if (is_break)
10304 error ("break statement within %<#pragma simd%> loop body");
10305 else
10306 error ("continue statement within %<#pragma simd%> loop body");
10307 return NULL_TREE;
10308
953ff289
DN
10309 default:
10310 gcc_unreachable ();
506e2710 10311 }
325c3691 10312
089efaa4
ILT
10313 if (skip)
10314 return NULL_TREE;
10315
2e28e797
JH
10316 if (!is_break)
10317 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10318
53fb4de3 10319 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
10320}
10321
506e2710 10322/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
10323
10324static void
c2255bc4 10325emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 10326{
e6b5a630
RH
10327 if (expr == error_mark_node)
10328 ;
10329 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
10330 {
10331 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 10332 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 10333 }
789eadcd
MP
10334 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10335 {
10336 tree r = expr;
10337 location_t cloc = loc;
10338 while (TREE_CODE (r) == COMPOUND_EXPR)
10339 {
10340 if (EXPR_HAS_LOCATION (r))
10341 cloc = EXPR_LOCATION (r);
10342 r = TREE_OPERAND (r, 1);
10343 }
10344 if (!TREE_SIDE_EFFECTS (r)
10345 && !VOID_TYPE_P (TREE_TYPE (r))
10346 && !CONVERT_EXPR_P (r)
cc28fc7f 10347 && !TREE_NO_WARNING (r)
789eadcd
MP
10348 && !TREE_NO_WARNING (expr))
10349 warning_at (cloc, OPT_Wunused_value,
10350 "right-hand operand of comma expression has no effect");
10351 }
27f33b15 10352 else
c2255bc4 10353 warn_if_unused_value (expr, loc);
3a5b9284
RH
10354}
10355
506e2710 10356/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
10357 diagnostics, but do not call ADD_STMT. LOC is the location of the
10358 statement. */
3a5b9284 10359
506e2710 10360tree
c2255bc4 10361c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 10362{
056928b2
JJ
10363 tree exprv;
10364
3a5b9284 10365 if (!expr)
506e2710 10366 return NULL_TREE;
3a5b9284 10367
928c19bb
JM
10368 expr = c_fully_fold (expr, false, NULL);
10369
3a5b9284
RH
10370 if (warn_sequence_point)
10371 verify_sequence_points (expr);
10372
10373 if (TREE_TYPE (expr) != error_mark_node
10374 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10375 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 10376 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
10377
10378 /* If we're not processing a statement expression, warn about unused values.
10379 Warnings for statement expressions will be emitted later, once we figure
10380 out which is the result. */
10381 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 10382 && warn_unused_value)
d06f8b75 10383 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
3a5b9284 10384
056928b2
JJ
10385 exprv = expr;
10386 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10387 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
10388 while (CONVERT_EXPR_P (exprv))
10389 exprv = TREE_OPERAND (exprv, 0);
10390 if (DECL_P (exprv)
10391 || handled_component_p (exprv)
10392 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 10393 mark_exp_read (exprv);
fa8351f8 10394
3a5b9284
RH
10395 /* If the expression is not of a type to which we cannot assign a line
10396 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 10397 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
10398 {
10399 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10400 SET_EXPR_LOCATION (expr, loc);
10401 }
506e2710
RH
10402
10403 return expr;
10404}
10405
c2255bc4
AH
10406/* Emit an expression as a statement. LOC is the location of the
10407 expression. */
506e2710
RH
10408
10409tree
c2255bc4 10410c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
10411{
10412 if (expr)
c2255bc4 10413 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
10414 else
10415 return NULL;
3a5b9284
RH
10416}
10417
10418/* Do the opposite and emit a statement as an expression. To begin,
10419 create a new binding level and return it. */
325c3691
RH
10420
10421tree
10422c_begin_stmt_expr (void)
10423{
10424 tree ret;
10425
10426 /* We must force a BLOCK for this level so that, if it is not expanded
10427 later, there is a way to turn off the entire subtree of blocks that
10428 are contained in it. */
10429 keep_next_level ();
10430 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10431
10432 c_bindings_start_stmt_expr (c_switch_stack == NULL
10433 ? NULL
10434 : c_switch_stack->bindings);
325c3691
RH
10435
10436 /* Mark the current statement list as belonging to a statement list. */
10437 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10438
10439 return ret;
10440}
10441
c2255bc4
AH
10442/* LOC is the location of the compound statement to which this body
10443 belongs. */
10444
325c3691 10445tree
c2255bc4 10446c_finish_stmt_expr (location_t loc, tree body)
325c3691 10447{
3a5b9284 10448 tree last, type, tmp, val;
325c3691
RH
10449 tree *last_p;
10450
c2255bc4 10451 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10452
10453 c_bindings_end_stmt_expr (c_switch_stack == NULL
10454 ? NULL
10455 : c_switch_stack->bindings);
325c3691 10456
3a5b9284
RH
10457 /* Locate the last statement in BODY. See c_end_compound_stmt
10458 about always returning a BIND_EXPR. */
10459 last_p = &BIND_EXPR_BODY (body);
10460 last = BIND_EXPR_BODY (body);
10461
10462 continue_searching:
325c3691
RH
10463 if (TREE_CODE (last) == STATEMENT_LIST)
10464 {
3a5b9284
RH
10465 tree_stmt_iterator i;
10466
10467 /* This can happen with degenerate cases like ({ }). No value. */
10468 if (!TREE_SIDE_EFFECTS (last))
10469 return body;
10470
10471 /* If we're supposed to generate side effects warnings, process
10472 all of the statements except the last. */
27f33b15 10473 if (warn_unused_value)
325c3691 10474 {
3a5b9284 10475 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10476 {
10477 location_t tloc;
10478 tree t = tsi_stmt (i);
10479
10480 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10481 emit_side_effect_warnings (tloc, t);
10482 }
325c3691
RH
10483 }
10484 else
3a5b9284
RH
10485 i = tsi_last (last);
10486 last_p = tsi_stmt_ptr (i);
10487 last = *last_p;
325c3691
RH
10488 }
10489
3a5b9284
RH
10490 /* If the end of the list is exception related, then the list was split
10491 by a call to push_cleanup. Continue searching. */
10492 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10493 || TREE_CODE (last) == TRY_CATCH_EXPR)
10494 {
10495 last_p = &TREE_OPERAND (last, 0);
10496 last = *last_p;
10497 goto continue_searching;
10498 }
10499
26d8af35
JM
10500 if (last == error_mark_node)
10501 return last;
10502
3a5b9284
RH
10503 /* In the case that the BIND_EXPR is not necessary, return the
10504 expression out from inside it. */
26d8af35
JM
10505 if (last == BIND_EXPR_BODY (body)
10506 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10507 {
928c19bb
JM
10508 /* Even if this looks constant, do not allow it in a constant
10509 expression. */
e5a94231 10510 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10511 /* Do not warn if the return value of a statement expression is
10512 unused. */
928c19bb 10513 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10514 return last;
10515 }
325c3691
RH
10516
10517 /* Extract the type of said expression. */
10518 type = TREE_TYPE (last);
325c3691 10519
3a5b9284
RH
10520 /* If we're not returning a value at all, then the BIND_EXPR that
10521 we already have is a fine expression to return. */
10522 if (!type || VOID_TYPE_P (type))
10523 return body;
10524
10525 /* Now that we've located the expression containing the value, it seems
10526 silly to make voidify_wrapper_expr repeat the process. Create a
10527 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10528 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10529
10530 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10531 tree_expr_nonnegative_p giving up immediately. */
10532 val = last;
10533 if (TREE_CODE (val) == NOP_EXPR
10534 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10535 val = TREE_OPERAND (val, 0);
10536
53fb4de3 10537 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10538 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10539
c2255bc4
AH
10540 {
10541 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10542 SET_EXPR_LOCATION (t, loc);
10543 return t;
10544 }
325c3691
RH
10545}
10546\f
10547/* Begin and end compound statements. This is as simple as pushing
10548 and popping new statement lists from the tree. */
10549
10550tree
10551c_begin_compound_stmt (bool do_scope)
10552{
10553 tree stmt = push_stmt_list ();
10554 if (do_scope)
4dfa0342 10555 push_scope ();
325c3691
RH
10556 return stmt;
10557}
10558
c2255bc4
AH
10559/* End a compound statement. STMT is the statement. LOC is the
10560 location of the compound statement-- this is usually the location
10561 of the opening brace. */
10562
325c3691 10563tree
c2255bc4 10564c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10565{
10566 tree block = NULL;
10567
10568 if (do_scope)
10569 {
10570 if (c_dialect_objc ())
10571 objc_clear_super_receiver ();
10572 block = pop_scope ();
10573 }
10574
10575 stmt = pop_stmt_list (stmt);
c2255bc4 10576 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10577
10578 /* If this compound statement is nested immediately inside a statement
10579 expression, then force a BIND_EXPR to be created. Otherwise we'll
10580 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10581 STATEMENT_LISTs merge, and thus we can lose track of what statement
10582 was really last. */
38e01f9e 10583 if (building_stmt_list_p ()
325c3691
RH
10584 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10585 && TREE_CODE (stmt) != BIND_EXPR)
10586 {
53fb4de3 10587 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10588 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10589 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10590 }
10591
10592 return stmt;
10593}
5a508662
RH
10594
10595/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10596 when the current scope is exited. EH_ONLY is true when this is not
10597 meant to apply to normal control flow transfer. */
10598
10599void
c2255bc4 10600push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10601{
3a5b9284
RH
10602 enum tree_code code;
10603 tree stmt, list;
10604 bool stmt_expr;
10605
10606 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10607 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10608 add_stmt (stmt);
3a5b9284
RH
10609 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10610 list = push_stmt_list ();
10611 TREE_OPERAND (stmt, 0) = list;
10612 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10613}
325c3691 10614\f
9f47c7e5
IE
10615/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10616 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10617
10618static tree
10619build_vec_cmp (tree_code code, tree type,
10620 tree arg0, tree arg1)
10621{
10622 tree zero_vec = build_zero_cst (type);
10623 tree minus_one_vec = build_minus_one_cst (type);
10624 tree cmp_type = build_same_sized_truth_vector_type (type);
10625 tree cmp = build2 (code, cmp_type, arg0, arg1);
10626 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10627}
10628
3e4093b6
RS
10629/* Build a binary-operation expression without default conversions.
10630 CODE is the kind of expression to build.
ba47d38d 10631 LOCATION is the operator's location.
3e4093b6
RS
10632 This function differs from `build' in several ways:
10633 the data type of the result is computed and recorded in it,
10634 warnings are generated if arg data types are invalid,
10635 special handling for addition and subtraction of pointers is known,
10636 and some optimization is done (operations on narrow ints
10637 are done in the narrower type when that gives the same result).
10638 Constant folding is also done before the result is returned.
de520661 10639
3e4093b6
RS
10640 Note that the operands will never have enumeral types, or function
10641 or array types, because either they will have the default conversions
10642 performed or they have both just been converted to some other type in which
10643 the arithmetic is to be done. */
10644
10645tree
ba47d38d
AH
10646build_binary_op (location_t location, enum tree_code code,
10647 tree orig_op0, tree orig_op1, int convert_p)
de520661 10648{
8ce94e44
JM
10649 tree type0, type1, orig_type0, orig_type1;
10650 tree eptype;
3e4093b6
RS
10651 enum tree_code code0, code1;
10652 tree op0, op1;
c9f9eb5d 10653 tree ret = error_mark_node;
4de67c26 10654 const char *invalid_op_diag;
4d84fe7c 10655 bool op0_int_operands, op1_int_operands;
928c19bb 10656 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10657
3e4093b6
RS
10658 /* Expression code to give to the expression when it is built.
10659 Normally this is CODE, which is what the caller asked for,
10660 but in some special cases we change it. */
10661 enum tree_code resultcode = code;
8b6a5902 10662
3e4093b6
RS
10663 /* Data type in which the computation is to be performed.
10664 In the simplest cases this is the common type of the arguments. */
10665 tree result_type = NULL;
10666
8ce94e44
JM
10667 /* When the computation is in excess precision, the type of the
10668 final EXCESS_PRECISION_EXPR. */
2d2e923f 10669 tree semantic_result_type = NULL;
8ce94e44 10670
3e4093b6
RS
10671 /* Nonzero means operands have already been type-converted
10672 in whatever way is necessary.
10673 Zero means they need to be converted to RESULT_TYPE. */
10674 int converted = 0;
10675
10676 /* Nonzero means create the expression with this type, rather than
10677 RESULT_TYPE. */
10678 tree build_type = 0;
10679
10680 /* Nonzero means after finally constructing the expression
10681 convert it to this type. */
10682 tree final_type = 0;
10683
10684 /* Nonzero if this is an operation like MIN or MAX which can
10685 safely be computed in short if both args are promoted shorts.
10686 Also implies COMMON.
10687 -1 indicates a bitwise operation; this makes a difference
10688 in the exact conditions for when it is safe to do the operation
10689 in a narrower mode. */
10690 int shorten = 0;
10691
10692 /* Nonzero if this is a comparison operation;
10693 if both args are promoted shorts, compare the original shorts.
10694 Also implies COMMON. */
10695 int short_compare = 0;
10696
10697 /* Nonzero if this is a right-shift operation, which can be computed on the
10698 original short and then promoted if the operand is a promoted short. */
10699 int short_shift = 0;
10700
10701 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10702 int common = 0;
10703
58393038
ZL
10704 /* True means types are compatible as far as ObjC is concerned. */
10705 bool objc_ok;
10706
8ce94e44
JM
10707 /* True means this is an arithmetic operation that may need excess
10708 precision. */
10709 bool may_need_excess_precision;
10710
180f8dbb
JM
10711 /* True means this is a boolean operation that converts both its
10712 operands to truth-values. */
10713 bool boolean_op = false;
10714
de5a5fa1
MP
10715 /* Remember whether we're doing / or %. */
10716 bool doing_div_or_mod = false;
10717
10718 /* Remember whether we're doing << or >>. */
10719 bool doing_shift = false;
10720
10721 /* Tree holding instrumentation expression. */
10722 tree instrument_expr = NULL;
10723
ba47d38d
AH
10724 if (location == UNKNOWN_LOCATION)
10725 location = input_location;
10726
4d84fe7c
JM
10727 op0 = orig_op0;
10728 op1 = orig_op1;
10729
10730 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10731 if (op0_int_operands)
10732 op0 = remove_c_maybe_const_expr (op0);
10733 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10734 if (op1_int_operands)
10735 op1 = remove_c_maybe_const_expr (op1);
10736 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10737 if (int_operands)
10738 {
10739 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10740 && TREE_CODE (orig_op1) == INTEGER_CST);
10741 int_const = (int_const_or_overflow
10742 && !TREE_OVERFLOW (orig_op0)
10743 && !TREE_OVERFLOW (orig_op1));
10744 }
10745 else
10746 int_const = int_const_or_overflow = false;
10747
0e3a99ae 10748 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e 10749 if (convert_p
31521951 10750 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
790e9490 10751 {
4d84fe7c
JM
10752 op0 = default_conversion (op0);
10753 op1 = default_conversion (op1);
790e9490
RS
10754 }
10755
36536d79
BI
10756 /* When Cilk Plus is enabled and there are array notations inside op0, then
10757 we check to see if there are builtin array notation functions. If
10758 so, then we take on the type of the array notation inside it. */
b72271b9 10759 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10760 orig_type0 = type0 = find_correct_array_notation_type (op0);
10761 else
10762 orig_type0 = type0 = TREE_TYPE (op0);
10763
b72271b9 10764 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10765 orig_type1 = type1 = find_correct_array_notation_type (op1);
10766 else
10767 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10768
3e4093b6
RS
10769 /* The expression codes of the data types of the arguments tell us
10770 whether the arguments are integers, floating, pointers, etc. */
10771 code0 = TREE_CODE (type0);
10772 code1 = TREE_CODE (type1);
10773
10774 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10775 STRIP_TYPE_NOPS (op0);
10776 STRIP_TYPE_NOPS (op1);
10777
10778 /* If an error was already reported for one of the arguments,
10779 avoid reporting another error. */
10780
10781 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10782 return error_mark_node;
10783
1807ffc1
MS
10784 if (code0 == POINTER_TYPE
10785 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10786 return error_mark_node;
10787
10788 if (code1 == POINTER_TYPE
10789 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10790 return error_mark_node;
10791
4de67c26
JM
10792 if ((invalid_op_diag
10793 = targetm.invalid_binary_op (code, type0, type1)))
10794 {
ba47d38d 10795 error_at (location, invalid_op_diag);
4de67c26
JM
10796 return error_mark_node;
10797 }
10798
8ce94e44
JM
10799 switch (code)
10800 {
10801 case PLUS_EXPR:
10802 case MINUS_EXPR:
10803 case MULT_EXPR:
10804 case TRUNC_DIV_EXPR:
10805 case CEIL_DIV_EXPR:
10806 case FLOOR_DIV_EXPR:
10807 case ROUND_DIV_EXPR:
10808 case EXACT_DIV_EXPR:
10809 may_need_excess_precision = true;
10810 break;
10811 default:
10812 may_need_excess_precision = false;
10813 break;
10814 }
10815 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10816 {
10817 op0 = TREE_OPERAND (op0, 0);
10818 type0 = TREE_TYPE (op0);
10819 }
10820 else if (may_need_excess_precision
10821 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10822 {
10823 type0 = eptype;
10824 op0 = convert (eptype, op0);
10825 }
10826 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10827 {
10828 op1 = TREE_OPERAND (op1, 0);
10829 type1 = TREE_TYPE (op1);
10830 }
10831 else if (may_need_excess_precision
10832 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10833 {
10834 type1 = eptype;
10835 op1 = convert (eptype, op1);
10836 }
10837
58393038
ZL
10838 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10839
0e3a99ae
AS
10840 /* In case when one of the operands of the binary operation is
10841 a vector and another is a scalar -- convert scalar to vector. */
10842 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10843 {
a212e43f
MG
10844 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10845 true);
f90e8e2e 10846
0e3a99ae
AS
10847 switch (convert_flag)
10848 {
10849 case stv_error:
10850 return error_mark_node;
10851 case stv_firstarg:
10852 {
10853 bool maybe_const = true;
10854 tree sc;
10855 sc = c_fully_fold (op0, false, &maybe_const);
10856 sc = save_expr (sc);
10857 sc = convert (TREE_TYPE (type1), sc);
10858 op0 = build_vector_from_val (type1, sc);
10859 if (!maybe_const)
10860 op0 = c_wrap_maybe_const (op0, true);
10861 orig_type0 = type0 = TREE_TYPE (op0);
10862 code0 = TREE_CODE (type0);
10863 converted = 1;
10864 break;
10865 }
10866 case stv_secondarg:
10867 {
10868 bool maybe_const = true;
10869 tree sc;
10870 sc = c_fully_fold (op1, false, &maybe_const);
10871 sc = save_expr (sc);
10872 sc = convert (TREE_TYPE (type0), sc);
10873 op1 = build_vector_from_val (type0, sc);
10874 if (!maybe_const)
54b9f838 10875 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10876 orig_type1 = type1 = TREE_TYPE (op1);
10877 code1 = TREE_CODE (type1);
10878 converted = 1;
10879 break;
10880 }
10881 default:
10882 break;
10883 }
10884 }
10885
3e4093b6 10886 switch (code)
de520661 10887 {
3e4093b6
RS
10888 case PLUS_EXPR:
10889 /* Handle the pointer + int case. */
10890 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10891 {
db3927fb 10892 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10893 goto return_build_binary_op;
10894 }
3e4093b6 10895 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10896 {
db3927fb 10897 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10898 goto return_build_binary_op;
10899 }
fe67cf58 10900 else
3e4093b6
RS
10901 common = 1;
10902 break;
400fbf9f 10903
3e4093b6
RS
10904 case MINUS_EXPR:
10905 /* Subtraction of two similar pointers.
10906 We must subtract them as integers, then divide by object size. */
10907 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10908 && comp_target_types (location, type0, type1))
c9f9eb5d 10909 {
db3927fb 10910 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10911 goto return_build_binary_op;
10912 }
3e4093b6
RS
10913 /* Handle pointer minus int. Just like pointer plus int. */
10914 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10915 {
db3927fb 10916 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10917 goto return_build_binary_op;
10918 }
3e4093b6
RS
10919 else
10920 common = 1;
10921 break;
8b6a5902 10922
3e4093b6
RS
10923 case MULT_EXPR:
10924 common = 1;
10925 break;
10926
10927 case TRUNC_DIV_EXPR:
10928 case CEIL_DIV_EXPR:
10929 case FLOOR_DIV_EXPR:
10930 case ROUND_DIV_EXPR:
10931 case EXACT_DIV_EXPR:
de5a5fa1 10932 doing_div_or_mod = true;
c9f9eb5d 10933 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10934
10935 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10936 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10937 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10938 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10939 || code1 == FIXED_POINT_TYPE
3e4093b6 10940 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10941 {
5bed876a
AH
10942 enum tree_code tcode0 = code0, tcode1 = code1;
10943
3a021db2 10944 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10945 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10946 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10947 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10948
ab22c1fa
CF
10949 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10950 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10951 resultcode = RDIV_EXPR;
10952 else
10953 /* Although it would be tempting to shorten always here, that
10954 loses on some targets, since the modulo instruction is
10955 undefined if the quotient can't be represented in the
10956 computation mode. We shorten only if unsigned or if
10957 dividing by something we know != -1. */
8df83eae 10958 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10959 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10960 && !integer_all_onesp (op1)));
3e4093b6
RS
10961 common = 1;
10962 }
10963 break;
de520661 10964
3e4093b6 10965 case BIT_AND_EXPR:
3e4093b6
RS
10966 case BIT_IOR_EXPR:
10967 case BIT_XOR_EXPR:
10968 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10969 shorten = -1;
9ef0c8d9
AP
10970 /* Allow vector types which are not floating point types. */
10971 else if (code0 == VECTOR_TYPE
10972 && code1 == VECTOR_TYPE
10973 && !VECTOR_FLOAT_TYPE_P (type0)
10974 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10975 common = 1;
10976 break;
10977
10978 case TRUNC_MOD_EXPR:
10979 case FLOOR_MOD_EXPR:
de5a5fa1 10980 doing_div_or_mod = true;
c9f9eb5d 10981 warn_for_div_by_zero (location, op1);
de520661 10982
5cfd5d9b
AP
10983 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10984 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10985 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10986 common = 1;
10987 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10988 {
10989 /* Although it would be tempting to shorten always here, that loses
10990 on some targets, since the modulo instruction is undefined if the
10991 quotient can't be represented in the computation mode. We shorten
10992 only if unsigned or if dividing by something we know != -1. */
8df83eae 10993 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10994 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10995 && !integer_all_onesp (op1)));
3e4093b6
RS
10996 common = 1;
10997 }
10998 break;
de520661 10999
3e4093b6
RS
11000 case TRUTH_ANDIF_EXPR:
11001 case TRUTH_ORIF_EXPR:
11002 case TRUTH_AND_EXPR:
11003 case TRUTH_OR_EXPR:
11004 case TRUTH_XOR_EXPR:
11005 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
11006 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11007 || code0 == FIXED_POINT_TYPE)
3e4093b6 11008 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
11009 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11010 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11011 {
11012 /* Result of these operations is always an int,
11013 but that does not mean the operands should be
11014 converted to ints! */
11015 result_type = integer_type_node;
a27d595d
JM
11016 if (op0_int_operands)
11017 {
11018 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11019 op0 = remove_c_maybe_const_expr (op0);
11020 }
11021 else
11022 op0 = c_objc_common_truthvalue_conversion (location, op0);
11023 if (op1_int_operands)
11024 {
11025 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11026 op1 = remove_c_maybe_const_expr (op1);
11027 }
11028 else
11029 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 11030 converted = 1;
180f8dbb 11031 boolean_op = true;
3e4093b6 11032 }
928c19bb
JM
11033 if (code == TRUTH_ANDIF_EXPR)
11034 {
11035 int_const_or_overflow = (int_operands
11036 && TREE_CODE (orig_op0) == INTEGER_CST
11037 && (op0 == truthvalue_false_node
11038 || TREE_CODE (orig_op1) == INTEGER_CST));
11039 int_const = (int_const_or_overflow
11040 && !TREE_OVERFLOW (orig_op0)
11041 && (op0 == truthvalue_false_node
11042 || !TREE_OVERFLOW (orig_op1)));
11043 }
11044 else if (code == TRUTH_ORIF_EXPR)
11045 {
11046 int_const_or_overflow = (int_operands
11047 && TREE_CODE (orig_op0) == INTEGER_CST
11048 && (op0 == truthvalue_true_node
11049 || TREE_CODE (orig_op1) == INTEGER_CST));
11050 int_const = (int_const_or_overflow
11051 && !TREE_OVERFLOW (orig_op0)
11052 && (op0 == truthvalue_true_node
11053 || !TREE_OVERFLOW (orig_op1)));
11054 }
3e4093b6 11055 break;
eba80994 11056
3e4093b6
RS
11057 /* Shift operations: result has same type as first operand;
11058 always convert second operand to int.
11059 Also set SHORT_SHIFT if shifting rightward. */
de520661 11060
3e4093b6 11061 case RSHIFT_EXPR:
76e2c821
JB
11062 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11063 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11064 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11065 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
11066 {
11067 result_type = type0;
11068 converted = 1;
11069 }
76e2c821
JB
11070 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11071 || code0 == VECTOR_TYPE)
451b5e48 11072 && code1 == INTEGER_TYPE)
3e4093b6 11073 {
de5a5fa1 11074 doing_shift = true;
928c19bb 11075 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 11076 {
3e4093b6 11077 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
11078 {
11079 int_const = false;
7d882b83 11080 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11081 warning_at (location, OPT_Wshift_count_negative,
11082 "right shift count is negative");
928c19bb 11083 }
76e2c821
JB
11084 else if (code0 == VECTOR_TYPE)
11085 {
11086 if (compare_tree_int (op1,
11087 TYPE_PRECISION (TREE_TYPE (type0)))
11088 >= 0)
11089 {
11090 int_const = false;
11091 if (c_inhibit_evaluation_warnings == 0)
11092 warning_at (location, OPT_Wshift_count_overflow,
11093 "right shift count >= width of vector element");
11094 }
11095 }
3e4093b6 11096 else
bbb818c6 11097 {
3f75a254 11098 if (!integer_zerop (op1))
3e4093b6
RS
11099 short_shift = 1;
11100
11101 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
11102 {
11103 int_const = false;
7d882b83 11104 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11105 warning_at (location, OPT_Wshift_count_overflow,
11106 "right shift count >= width of type");
928c19bb 11107 }
bbb818c6 11108 }
b62acd60 11109 }
de520661 11110
3e4093b6
RS
11111 /* Use the type of the value to be shifted. */
11112 result_type = type0;
3e4093b6
RS
11113 /* Avoid converting op1 to result_type later. */
11114 converted = 1;
400fbf9f 11115 }
3e4093b6 11116 break;
253b6b82 11117
3e4093b6 11118 case LSHIFT_EXPR:
76e2c821
JB
11119 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11120 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11121 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11122 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
11123 {
11124 result_type = type0;
11125 converted = 1;
11126 }
76e2c821
JB
11127 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11128 || code0 == VECTOR_TYPE)
451b5e48 11129 && code1 == INTEGER_TYPE)
3e4093b6 11130 {
de5a5fa1 11131 doing_shift = true;
0173bd2a
MP
11132 if (TREE_CODE (op0) == INTEGER_CST
11133 && tree_int_cst_sgn (op0) < 0)
11134 {
11135 /* Don't reject a left shift of a negative value in a context
11136 where a constant expression is needed in C90. */
11137 if (flag_isoc99)
11138 int_const = false;
11139 if (c_inhibit_evaluation_warnings == 0)
11140 warning_at (location, OPT_Wshift_negative_value,
11141 "left shift of negative value");
11142 }
928c19bb 11143 if (TREE_CODE (op1) == INTEGER_CST)
de520661 11144 {
3e4093b6 11145 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
11146 {
11147 int_const = false;
7d882b83 11148 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11149 warning_at (location, OPT_Wshift_count_negative,
11150 "left shift count is negative");
928c19bb 11151 }
76e2c821
JB
11152 else if (code0 == VECTOR_TYPE)
11153 {
11154 if (compare_tree_int (op1,
11155 TYPE_PRECISION (TREE_TYPE (type0)))
11156 >= 0)
11157 {
11158 int_const = false;
11159 if (c_inhibit_evaluation_warnings == 0)
11160 warning_at (location, OPT_Wshift_count_overflow,
11161 "left shift count >= width of vector element");
11162 }
11163 }
3e4093b6 11164 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
11165 {
11166 int_const = false;
7d882b83 11167 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11168 warning_at (location, OPT_Wshift_count_overflow,
11169 "left shift count >= width of type");
928c19bb 11170 }
451b5e48
MP
11171 else if (TREE_CODE (op0) == INTEGER_CST
11172 && maybe_warn_shift_overflow (location, op0, op1)
11173 && flag_isoc99)
11174 int_const = false;
94ba5069 11175 }
de520661 11176
3e4093b6
RS
11177 /* Use the type of the value to be shifted. */
11178 result_type = type0;
3e4093b6
RS
11179 /* Avoid converting op1 to result_type later. */
11180 converted = 1;
400fbf9f 11181 }
3e4093b6 11182 break;
de520661 11183
3e4093b6
RS
11184 case EQ_EXPR:
11185 case NE_EXPR:
d246ab4f
AS
11186 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11187 {
11188 tree intt;
0af94e6f 11189 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11190 {
11191 error_at (location, "comparing vectors with different "
11192 "element types");
11193 return error_mark_node;
11194 }
11195
11196 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11197 {
11198 error_at (location, "comparing vectors with different "
11199 "number of elements");
11200 return error_mark_node;
11201 }
11202
fa74a4bc
MP
11203 /* It's not precisely specified how the usual arithmetic
11204 conversions apply to the vector types. Here, we use
11205 the unsigned type if one of the operands is signed and
11206 the other one is unsigned. */
11207 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11208 {
11209 if (!TYPE_UNSIGNED (type0))
11210 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11211 else
11212 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11213 warning_at (location, OPT_Wsign_compare, "comparison between "
11214 "types %qT and %qT", type0, type1);
11215 }
11216
d246ab4f
AS
11217 /* Always construct signed integer vector type. */
11218 intt = c_common_type_for_size (GET_MODE_BITSIZE
11219 (TYPE_MODE (TREE_TYPE (type0))), 0);
11220 result_type = build_opaque_vector_type (intt,
11221 TYPE_VECTOR_SUBPARTS (type0));
11222 converted = 1;
9f47c7e5
IE
11223 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11224 goto return_build_binary_op;
d246ab4f 11225 }
ae311566 11226 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
11227 warning_at (location,
11228 OPT_Wfloat_equal,
11229 "comparing floating point with == or != is unsafe");
3e4093b6
RS
11230 /* Result of comparison is always int,
11231 but don't convert the args to int! */
11232 build_type = integer_type_node;
11233 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 11234 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 11235 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 11236 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 11237 short_compare = 1;
637f1455
SZ
11238 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11239 {
11240 if (TREE_CODE (op0) == ADDR_EXPR
de55efd5
MP
11241 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11242 && !from_macro_expansion_at (location))
637f1455
SZ
11243 {
11244 if (code == EQ_EXPR)
11245 warning_at (location,
11246 OPT_Waddress,
11247 "the comparison will always evaluate as %<false%> "
11248 "for the address of %qD will never be NULL",
11249 TREE_OPERAND (op0, 0));
11250 else
11251 warning_at (location,
11252 OPT_Waddress,
11253 "the comparison will always evaluate as %<true%> "
11254 "for the address of %qD will never be NULL",
11255 TREE_OPERAND (op0, 0));
11256 }
11257 result_type = type0;
11258 }
11259 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11260 {
11261 if (TREE_CODE (op1) == ADDR_EXPR
de55efd5
MP
11262 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11263 && !from_macro_expansion_at (location))
637f1455
SZ
11264 {
11265 if (code == EQ_EXPR)
11266 warning_at (location,
f90e8e2e 11267 OPT_Waddress,
637f1455
SZ
11268 "the comparison will always evaluate as %<false%> "
11269 "for the address of %qD will never be NULL",
11270 TREE_OPERAND (op1, 0));
11271 else
11272 warning_at (location,
11273 OPT_Waddress,
11274 "the comparison will always evaluate as %<true%> "
11275 "for the address of %qD will never be NULL",
11276 TREE_OPERAND (op1, 0));
11277 }
11278 result_type = type1;
11279 }
3e4093b6
RS
11280 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11281 {
11282 tree tt0 = TREE_TYPE (type0);
11283 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
11284 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11285 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11286 addr_space_t as_common = ADDR_SPACE_GENERIC;
11287
3e4093b6
RS
11288 /* Anything compares with void *. void * compares with anything.
11289 Otherwise, the targets must be compatible
11290 and both must be object or both incomplete. */
744aa42f 11291 if (comp_target_types (location, type0, type1))
10bc1b1b 11292 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
11293 else if (!addr_space_superset (as0, as1, &as_common))
11294 {
11295 error_at (location, "comparison of pointers to "
11296 "disjoint address spaces");
11297 return error_mark_node;
11298 }
267bac10 11299 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 11300 {
36c5e70a 11301 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 11302 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11303 "comparison of %<void *%> with function pointer");
ee2990e7 11304 }
267bac10 11305 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 11306 {
36c5e70a 11307 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 11308 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11309 "comparison of %<void *%> with function pointer");
e6834654 11310 }
3e4093b6 11311 else
58393038
ZL
11312 /* Avoid warning about the volatile ObjC EH puts on decls. */
11313 if (!objc_ok)
ba47d38d 11314 pedwarn (location, 0,
509c9d60 11315 "comparison of distinct pointer types lacks a cast");
e6834654 11316
3e4093b6 11317 if (result_type == NULL_TREE)
36c5e70a
BE
11318 {
11319 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11320 result_type = build_pointer_type
11321 (build_qualified_type (void_type_node, qual));
11322 }
e6834654 11323 }
3e4093b6 11324 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 11325 {
3e4093b6 11326 result_type = type0;
ba47d38d 11327 pedwarn (location, 0, "comparison between pointer and integer");
de520661 11328 }
3e4093b6 11329 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 11330 {
3e4093b6 11331 result_type = type1;
ba47d38d 11332 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 11333 }
04159acf
MP
11334 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11335 || truth_value_p (TREE_CODE (orig_op0)))
11336 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11337 || truth_value_p (TREE_CODE (orig_op1))))
11338 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11339 break;
8b6a5902 11340
3e4093b6
RS
11341 case LE_EXPR:
11342 case GE_EXPR:
11343 case LT_EXPR:
11344 case GT_EXPR:
d246ab4f
AS
11345 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11346 {
11347 tree intt;
0af94e6f 11348 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11349 {
11350 error_at (location, "comparing vectors with different "
11351 "element types");
11352 return error_mark_node;
11353 }
11354
11355 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11356 {
11357 error_at (location, "comparing vectors with different "
11358 "number of elements");
11359 return error_mark_node;
11360 }
11361
fa74a4bc
MP
11362 /* It's not precisely specified how the usual arithmetic
11363 conversions apply to the vector types. Here, we use
11364 the unsigned type if one of the operands is signed and
11365 the other one is unsigned. */
11366 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11367 {
11368 if (!TYPE_UNSIGNED (type0))
11369 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11370 else
11371 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11372 warning_at (location, OPT_Wsign_compare, "comparison between "
11373 "types %qT and %qT", type0, type1);
11374 }
11375
d246ab4f
AS
11376 /* Always construct signed integer vector type. */
11377 intt = c_common_type_for_size (GET_MODE_BITSIZE
11378 (TYPE_MODE (TREE_TYPE (type0))), 0);
11379 result_type = build_opaque_vector_type (intt,
11380 TYPE_VECTOR_SUBPARTS (type0));
11381 converted = 1;
9f47c7e5
IE
11382 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11383 goto return_build_binary_op;
d246ab4f 11384 }
3e4093b6 11385 build_type = integer_type_node;
ab22c1fa
CF
11386 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11387 || code0 == FIXED_POINT_TYPE)
11388 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11389 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11390 short_compare = 1;
11391 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11392 {
36c5e70a
BE
11393 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11394 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11395 addr_space_t as_common;
11396
744aa42f 11397 if (comp_target_types (location, type0, type1))
3e4093b6 11398 {
10bc1b1b 11399 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
11400 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11401 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 11402 pedwarn (location, 0,
509c9d60 11403 "comparison of complete and incomplete pointers");
fcf73884 11404 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 11405 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11406 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
11407 else if (null_pointer_constant_p (orig_op0)
11408 || null_pointer_constant_p (orig_op1))
11409 warning_at (location, OPT_Wextra,
11410 "ordered comparison of pointer with null pointer");
11411
3e4093b6 11412 }
36c5e70a
BE
11413 else if (!addr_space_superset (as0, as1, &as_common))
11414 {
11415 error_at (location, "comparison of pointers to "
11416 "disjoint address spaces");
11417 return error_mark_node;
11418 }
3e4093b6
RS
11419 else
11420 {
36c5e70a
BE
11421 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11422 result_type = build_pointer_type
11423 (build_qualified_type (void_type_node, qual));
ba47d38d 11424 pedwarn (location, 0,
509c9d60 11425 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
11426 }
11427 }
6aa3c60d 11428 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
11429 {
11430 result_type = type0;
fcf73884 11431 if (pedantic)
c1771a20 11432 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
11433 "ordered comparison of pointer with integer zero");
11434 else if (extra_warnings)
ba47d38d 11435 warning_at (location, OPT_Wextra,
d42ba3b8 11436 "ordered comparison of pointer with integer zero");
3e4093b6 11437 }
6aa3c60d 11438 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
11439 {
11440 result_type = type1;
d42ba3b8 11441 if (pedantic)
c1771a20 11442 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
11443 "ordered comparison of pointer with integer zero");
11444 else if (extra_warnings)
11445 warning_at (location, OPT_Wextra,
11446 "ordered comparison of pointer with integer zero");
3e4093b6
RS
11447 }
11448 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11449 {
11450 result_type = type0;
ba47d38d 11451 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
11452 }
11453 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11454 {
11455 result_type = type1;
ba47d38d 11456 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 11457 }
04159acf
MP
11458 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11459 || truth_value_p (TREE_CODE (orig_op0)))
11460 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11461 || truth_value_p (TREE_CODE (orig_op1))))
11462 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11463 break;
64094f6a 11464
3e4093b6 11465 default:
37b2f290 11466 gcc_unreachable ();
c9fe6f9f 11467 }
8f17b5c5 11468
e57e265b
PB
11469 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11470 return error_mark_node;
11471
5bed876a
AH
11472 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11473 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 11474 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 11475 {
745e411d
DM
11476 gcc_rich_location richloc (location);
11477 richloc.maybe_add_expr (orig_op0);
11478 richloc.maybe_add_expr (orig_op1);
11479 binary_op_error (&richloc, code, type0, type1);
5bed876a
AH
11480 return error_mark_node;
11481 }
11482
3e4093b6 11483 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 11484 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
11485 &&
11486 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 11487 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 11488 {
2ca862e9
JM
11489 bool first_complex = (code0 == COMPLEX_TYPE);
11490 bool second_complex = (code1 == COMPLEX_TYPE);
11491 int none_complex = (!first_complex && !second_complex);
39b726dd 11492
3e4093b6 11493 if (shorten || common || short_compare)
3bf6bfcc
JJ
11494 {
11495 result_type = c_common_type (type0, type1);
0a0b3574
MM
11496 do_warn_double_promotion (result_type, type0, type1,
11497 "implicit conversion from %qT to %qT "
11498 "to match other operand of binary "
11499 "expression",
11500 location);
3bf6bfcc
JJ
11501 if (result_type == error_mark_node)
11502 return error_mark_node;
11503 }
400fbf9f 11504
2ca862e9
JM
11505 if (first_complex != second_complex
11506 && (code == PLUS_EXPR
11507 || code == MINUS_EXPR
11508 || code == MULT_EXPR
11509 || (code == TRUNC_DIV_EXPR && first_complex))
11510 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11511 && flag_signed_zeros)
11512 {
11513 /* An operation on mixed real/complex operands must be
11514 handled specially, but the language-independent code can
11515 more easily optimize the plain complex arithmetic if
11516 -fno-signed-zeros. */
11517 tree real_type = TREE_TYPE (result_type);
11518 tree real, imag;
11519 if (type0 != orig_type0 || type1 != orig_type1)
11520 {
11521 gcc_assert (may_need_excess_precision && common);
2d2e923f 11522 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11523 }
11524 if (first_complex)
11525 {
11526 if (TREE_TYPE (op0) != result_type)
68fca595 11527 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11528 if (TREE_TYPE (op1) != real_type)
68fca595 11529 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11530 }
11531 else
11532 {
11533 if (TREE_TYPE (op0) != real_type)
68fca595 11534 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11535 if (TREE_TYPE (op1) != result_type)
68fca595 11536 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11537 }
11538 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11539 return error_mark_node;
11540 if (first_complex)
11541 {
11542 op0 = c_save_expr (op0);
11543 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
e51fbec3 11544 op0, true);
2ca862e9 11545 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
e51fbec3 11546 op0, true);
2ca862e9
JM
11547 switch (code)
11548 {
11549 case MULT_EXPR:
11550 case TRUNC_DIV_EXPR:
c4603e7c 11551 op1 = c_save_expr (op1);
2ca862e9
JM
11552 imag = build2 (resultcode, real_type, imag, op1);
11553 /* Fall through. */
11554 case PLUS_EXPR:
11555 case MINUS_EXPR:
11556 real = build2 (resultcode, real_type, real, op1);
11557 break;
11558 default:
11559 gcc_unreachable();
11560 }
11561 }
11562 else
11563 {
11564 op1 = c_save_expr (op1);
11565 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
e51fbec3 11566 op1, true);
2ca862e9 11567 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
e51fbec3 11568 op1, true);
2ca862e9
JM
11569 switch (code)
11570 {
11571 case MULT_EXPR:
c4603e7c 11572 op0 = c_save_expr (op0);
2ca862e9
JM
11573 imag = build2 (resultcode, real_type, op0, imag);
11574 /* Fall through. */
11575 case PLUS_EXPR:
11576 real = build2 (resultcode, real_type, op0, real);
11577 break;
11578 case MINUS_EXPR:
11579 real = build2 (resultcode, real_type, op0, real);
11580 imag = build1 (NEGATE_EXPR, real_type, imag);
11581 break;
11582 default:
11583 gcc_unreachable();
11584 }
11585 }
11586 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11587 goto return_build_binary_op;
11588 }
11589
3e4093b6
RS
11590 /* For certain operations (which identify themselves by shorten != 0)
11591 if both args were extended from the same smaller type,
11592 do the arithmetic in that type and then extend.
400fbf9f 11593
3e4093b6
RS
11594 shorten !=0 and !=1 indicates a bitwise operation.
11595 For them, this optimization is safe only if
11596 both args are zero-extended or both are sign-extended.
11597 Otherwise, we might change the result.
11598 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11599 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11600
3e4093b6
RS
11601 if (shorten && none_complex)
11602 {
3e4093b6 11603 final_type = result_type;
b8698a0f 11604 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11605 shorten == -1);
3e4093b6 11606 }
88a3dbc1 11607
3e4093b6 11608 /* Shifts can be shortened if shifting right. */
2f6e4e97 11609
3e4093b6
RS
11610 if (short_shift)
11611 {
11612 int unsigned_arg;
11613 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11614
3e4093b6 11615 final_type = result_type;
abe80e6d 11616
3e4093b6 11617 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11618 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11619
3e4093b6 11620 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11621 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11622 /* We can shorten only if the shift count is less than the
11623 number of bits in the smaller type size. */
11624 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11625 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11626 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11627 {
11628 /* Do an unsigned shift if the operand was zero-extended. */
11629 result_type
11630 = c_common_signed_or_unsigned_type (unsigned_arg,
11631 TREE_TYPE (arg0));
11632 /* Convert value-to-be-shifted to that type. */
11633 if (TREE_TYPE (op0) != result_type)
11634 op0 = convert (result_type, op0);
11635 converted = 1;
abe80e6d 11636 }
88a3dbc1
RK
11637 }
11638
3e4093b6
RS
11639 /* Comparison operations are shortened too but differently.
11640 They identify themselves by setting short_compare = 1. */
56cb9733 11641
3e4093b6
RS
11642 if (short_compare)
11643 {
11644 /* Don't write &op0, etc., because that would prevent op0
11645 from being kept in a register.
11646 Instead, make copies of the our local variables and
11647 pass the copies by reference, then copy them back afterward. */
11648 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11649 enum tree_code xresultcode = resultcode;
11650 tree val
393e8e8b
MP
11651 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11652 &xresultcode);
8f17b5c5 11653
3e4093b6 11654 if (val != 0)
c9f9eb5d
AH
11655 {
11656 ret = val;
11657 goto return_build_binary_op;
11658 }
8f17b5c5 11659
3e4093b6
RS
11660 op0 = xop0, op1 = xop1;
11661 converted = 1;
11662 resultcode = xresultcode;
8f17b5c5 11663
7d882b83 11664 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11665 {
11666 bool op0_maybe_const = true;
11667 bool op1_maybe_const = true;
11668 tree orig_op0_folded, orig_op1_folded;
11669
11670 if (in_late_binary_op)
11671 {
11672 orig_op0_folded = orig_op0;
11673 orig_op1_folded = orig_op1;
11674 }
11675 else
11676 {
11677 /* Fold for the sake of possible warnings, as in
11678 build_conditional_expr. This requires the
11679 "original" values to be folded, not just op0 and
11680 op1. */
f5178456 11681 c_inhibit_evaluation_warnings++;
928c19bb
JM
11682 op0 = c_fully_fold (op0, require_constant_value,
11683 &op0_maybe_const);
11684 op1 = c_fully_fold (op1, require_constant_value,
11685 &op1_maybe_const);
f5178456 11686 c_inhibit_evaluation_warnings--;
928c19bb
JM
11687 orig_op0_folded = c_fully_fold (orig_op0,
11688 require_constant_value,
11689 NULL);
11690 orig_op1_folded = c_fully_fold (orig_op1,
11691 require_constant_value,
11692 NULL);
11693 }
11694
11695 if (warn_sign_compare)
11696 warn_for_sign_compare (location, orig_op0_folded,
11697 orig_op1_folded, op0, op1,
11698 result_type, resultcode);
5c2f94b4 11699 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11700 {
11701 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11702 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11703 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11704 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11705 }
3e4093b6 11706 }
2ad1815d 11707 }
64094f6a 11708 }
64094f6a 11709
3e4093b6
RS
11710 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11711 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11712 Then the expression will be built.
11713 It will be given type FINAL_TYPE if that is nonzero;
11714 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11715
3e4093b6
RS
11716 if (!result_type)
11717 {
745e411d
DM
11718 gcc_rich_location richloc (location);
11719 richloc.maybe_add_expr (orig_op0);
11720 richloc.maybe_add_expr (orig_op1);
11721 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11722 return error_mark_node;
11723 }
400fbf9f 11724
3e4093b6 11725 if (build_type == NULL_TREE)
8ce94e44
JM
11726 {
11727 build_type = result_type;
180f8dbb
JM
11728 if ((type0 != orig_type0 || type1 != orig_type1)
11729 && !boolean_op)
8ce94e44
JM
11730 {
11731 gcc_assert (may_need_excess_precision && common);
2d2e923f 11732 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11733 }
11734 }
400fbf9f 11735
2d2e923f
MLI
11736 if (!converted)
11737 {
68fca595
MP
11738 op0 = ep_convert_and_check (location, result_type, op0,
11739 semantic_result_type);
11740 op1 = ep_convert_and_check (location, result_type, op1,
11741 semantic_result_type);
2d2e923f
MLI
11742
11743 /* This can happen if one operand has a vector type, and the other
11744 has a different type. */
11745 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11746 return error_mark_node;
11747 }
11748
f8ed5150
MP
11749 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11750 | SANITIZE_FLOAT_DIVIDE))
802ac282 11751 && do_ubsan_in_current_function ()
b2aaf235
MP
11752 && (doing_div_or_mod || doing_shift)
11753 && !require_constant_value)
de5a5fa1
MP
11754 {
11755 /* OP0 and/or OP1 might have side-effects. */
11756 op0 = c_save_expr (op0);
11757 op1 = c_save_expr (op1);
11758 op0 = c_fully_fold (op0, false, NULL);
11759 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11760 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11761 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11762 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11763 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11764 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11765 }
11766
c9f9eb5d 11767 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11768 if (int_const_or_overflow)
11769 ret = (require_constant_value
db3927fb
AH
11770 ? fold_build2_initializer_loc (location, resultcode, build_type,
11771 op0, op1)
11772 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11773 else
11774 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11775 if (final_type != 0)
11776 ret = convert (final_type, ret);
11777
11778 return_build_binary_op:
11779 gcc_assert (ret != error_mark_node);
928c19bb
JM
11780 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11781 ret = (int_operands
11782 ? note_integer_operands (ret)
11783 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11784 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11785 && !in_late_binary_op)
11786 ret = note_integer_operands (ret);
2d2e923f
MLI
11787 if (semantic_result_type)
11788 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11789 protected_set_expr_location (ret, location);
de5a5fa1 11790
a24d975c 11791 if (instrument_expr != NULL)
de5a5fa1
MP
11792 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11793 instrument_expr, ret);
11794
c9f9eb5d 11795 return ret;
400fbf9f 11796}
85498824
JM
11797
11798
11799/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11800 purpose. LOCATION is the source location for the expression. */
85498824
JM
11801
11802tree
ba47d38d 11803c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11804{
928c19bb
JM
11805 bool int_const, int_operands;
11806
85498824
JM
11807 switch (TREE_CODE (TREE_TYPE (expr)))
11808 {
11809 case ARRAY_TYPE:
ba47d38d 11810 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11811 return error_mark_node;
11812
11813 case RECORD_TYPE:
ba47d38d 11814 error_at (location, "used struct type value where scalar is required");
85498824
JM
11815 return error_mark_node;
11816
11817 case UNION_TYPE:
ba47d38d 11818 error_at (location, "used union type value where scalar is required");
85498824
JM
11819 return error_mark_node;
11820
04af8788
NP
11821 case VOID_TYPE:
11822 error_at (location, "void value not ignored as it ought to be");
11823 return error_mark_node;
11824
1807ffc1
MS
11825 case POINTER_TYPE:
11826 if (reject_gcc_builtin (expr))
11827 return error_mark_node;
11828 break;
11829
46bdb9cf
JM
11830 case FUNCTION_TYPE:
11831 gcc_unreachable ();
11832
d246ab4f
AS
11833 case VECTOR_TYPE:
11834 error_at (location, "used vector type where scalar is required");
11835 return error_mark_node;
11836
85498824
JM
11837 default:
11838 break;
11839 }
11840
928c19bb
JM
11841 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11842 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11843 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11844 {
11845 expr = remove_c_maybe_const_expr (expr);
11846 expr = build2 (NE_EXPR, integer_type_node, expr,
11847 convert (TREE_TYPE (expr), integer_zero_node));
11848 expr = note_integer_operands (expr);
11849 }
11850 else
11851 /* ??? Should we also give an error for vectors rather than leaving
11852 those to give errors later? */
11853 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11854
11855 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11856 {
11857 if (TREE_OVERFLOW (expr))
11858 return expr;
11859 else
11860 return note_integer_operands (expr);
11861 }
11862 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11863 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11864 return expr;
85498824 11865}
73f397d4
JM
11866\f
11867
11868/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11869 required. */
11870
11871tree
51eed280 11872c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11873{
11874 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11875 {
11876 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11877 /* Executing a compound literal inside a function reinitializes
11878 it. */
11879 if (!TREE_STATIC (decl))
11880 *se = true;
11881 return decl;
11882 }
11883 else
11884 return expr;
11885}
953ff289 11886\f
88bae6f4
TS
11887/* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11888 statement. LOC is the location of the construct. */
41dbbb37
TS
11889
11890tree
88bae6f4
TS
11891c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11892 tree clauses)
41dbbb37 11893{
88bae6f4 11894 body = c_end_compound_stmt (loc, body, true);
41dbbb37 11895
88bae6f4 11896 tree stmt = make_node (code);
41dbbb37 11897 TREE_TYPE (stmt) = void_type_node;
88bae6f4
TS
11898 OMP_BODY (stmt) = body;
11899 OMP_CLAUSES (stmt) = clauses;
41dbbb37
TS
11900 SET_EXPR_LOCATION (stmt, loc);
11901
11902 return add_stmt (stmt);
11903}
11904
11905/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11906 statement. LOC is the location of the OACC_DATA. */
11907
11908tree
11909c_finish_oacc_data (location_t loc, tree clauses, tree block)
11910{
11911 tree stmt;
11912
11913 block = c_end_compound_stmt (loc, block, true);
11914
11915 stmt = make_node (OACC_DATA);
11916 TREE_TYPE (stmt) = void_type_node;
11917 OACC_DATA_CLAUSES (stmt) = clauses;
11918 OACC_DATA_BODY (stmt) = block;
11919 SET_EXPR_LOCATION (stmt, loc);
11920
11921 return add_stmt (stmt);
11922}
11923
37d5ad46
JB
11924/* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11925 statement. LOC is the location of the OACC_HOST_DATA. */
11926
11927tree
11928c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
11929{
11930 tree stmt;
11931
11932 block = c_end_compound_stmt (loc, block, true);
11933
11934 stmt = make_node (OACC_HOST_DATA);
11935 TREE_TYPE (stmt) = void_type_node;
11936 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
11937 OACC_HOST_DATA_BODY (stmt) = block;
11938 SET_EXPR_LOCATION (stmt, loc);
11939
11940 return add_stmt (stmt);
11941}
11942
c0220ea4 11943/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11944
11945tree
11946c_begin_omp_parallel (void)
11947{
11948 tree block;
11949
11950 keep_next_level ();
11951 block = c_begin_compound_stmt (true);
11952
11953 return block;
11954}
11955
c2255bc4
AH
11956/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11957 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11958
953ff289 11959tree
c2255bc4 11960c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11961{
11962 tree stmt;
11963
c2255bc4 11964 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11965
11966 stmt = make_node (OMP_PARALLEL);
11967 TREE_TYPE (stmt) = void_type_node;
11968 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11969 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11970 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11971
11972 return add_stmt (stmt);
11973}
11974
a68ab351
JJ
11975/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11976
11977tree
11978c_begin_omp_task (void)
11979{
11980 tree block;
11981
11982 keep_next_level ();
11983 block = c_begin_compound_stmt (true);
11984
11985 return block;
11986}
11987
c2255bc4
AH
11988/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11989 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11990
11991tree
c2255bc4 11992c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11993{
11994 tree stmt;
11995
c2255bc4 11996 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11997
11998 stmt = make_node (OMP_TASK);
11999 TREE_TYPE (stmt) = void_type_node;
12000 OMP_TASK_CLAUSES (stmt) = clauses;
12001 OMP_TASK_BODY (stmt) = block;
c2255bc4 12002 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
12003
12004 return add_stmt (stmt);
12005}
12006
acf0174b
JJ
12007/* Generate GOMP_cancel call for #pragma omp cancel. */
12008
12009void
12010c_finish_omp_cancel (location_t loc, tree clauses)
12011{
12012 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12013 int mask = 0;
12014 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
12015 mask = 1;
12016 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
12017 mask = 2;
12018 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
12019 mask = 4;
12020 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
12021 mask = 8;
12022 else
12023 {
54d19c3b 12024 error_at (loc, "%<#pragma omp cancel%> must specify one of "
acf0174b
JJ
12025 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12026 "clauses");
12027 return;
12028 }
12029 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
12030 if (ifc != NULL_TREE)
12031 {
12032 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12033 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12034 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12035 build_zero_cst (type));
12036 }
12037 else
12038 ifc = boolean_true_node;
12039 tree stmt = build_call_expr_loc (loc, fn, 2,
12040 build_int_cst (integer_type_node, mask),
12041 ifc);
12042 add_stmt (stmt);
12043}
12044
12045/* Generate GOMP_cancellation_point call for
12046 #pragma omp cancellation point. */
12047
12048void
12049c_finish_omp_cancellation_point (location_t loc, tree clauses)
12050{
12051 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12052 int mask = 0;
12053 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
12054 mask = 1;
12055 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
12056 mask = 2;
12057 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
12058 mask = 4;
12059 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
12060 mask = 8;
12061 else
12062 {
54d19c3b 12063 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
acf0174b
JJ
12064 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12065 "clauses");
12066 return;
12067 }
12068 tree stmt = build_call_expr_loc (loc, fn, 1,
12069 build_int_cst (integer_type_node, mask));
12070 add_stmt (stmt);
12071}
12072
12073/* Helper function for handle_omp_array_sections. Called recursively
12074 to handle multiple array-section-subscripts. C is the clause,
12075 T current expression (initially OMP_CLAUSE_DECL), which is either
12076 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12077 expression if specified, TREE_VALUE length expression if specified,
12078 TREE_CHAIN is what it has been specified after, or some decl.
12079 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12080 set to true if any of the array-section-subscript could have length
12081 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12082 first array-section-subscript which is known not to have length
12083 of one. Given say:
12084 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12085 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12086 all are or may have length of 1, array-section-subscript [:2] is the
d9a6bd32 12087 first one known not to have length 1. For array-section-subscript
acf0174b
JJ
12088 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12089 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12090 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12091 case though, as some lengths could be zero. */
12092
12093static tree
12094handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
d9a6bd32 12095 bool &maybe_zero_len, unsigned int &first_non_one,
e46c7770 12096 enum c_omp_region_type ort)
acf0174b
JJ
12097{
12098 tree ret, low_bound, length, type;
12099 if (TREE_CODE (t) != TREE_LIST)
12100 {
7a0ca710 12101 if (error_operand_p (t))
acf0174b 12102 return error_mark_node;
d9a6bd32 12103 ret = t;
9dc5773f
JJ
12104 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12105 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12106 {
12107 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12108 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12109 return error_mark_node;
12110 }
d9a6bd32 12111 if (TREE_CODE (t) == COMPONENT_REF
e46c7770 12112 && ort == C_ORT_OMP
d9a6bd32
JJ
12113 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12114 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12115 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12116 {
12117 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12118 {
12119 error_at (OMP_CLAUSE_LOCATION (c),
12120 "bit-field %qE in %qs clause",
12121 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12122 return error_mark_node;
12123 }
12124 while (TREE_CODE (t) == COMPONENT_REF)
12125 {
12126 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12127 {
12128 error_at (OMP_CLAUSE_LOCATION (c),
12129 "%qE is a member of a union", t);
12130 return error_mark_node;
12131 }
12132 t = TREE_OPERAND (t, 0);
12133 }
12134 }
0ae9bd27 12135 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12136 {
12137 if (DECL_P (t))
12138 error_at (OMP_CLAUSE_LOCATION (c),
12139 "%qD is not a variable in %qs clause", t,
12140 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12141 else
12142 error_at (OMP_CLAUSE_LOCATION (c),
12143 "%qE is not a variable in %qs clause", t,
12144 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12145 return error_mark_node;
12146 }
12147 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
9dc5773f
JJ
12148 && TYPE_ATOMIC (TREE_TYPE (t)))
12149 {
12150 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12151 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12152 return error_mark_node;
12153 }
12154 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12155 && VAR_P (t)
12156 && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
12157 {
12158 error_at (OMP_CLAUSE_LOCATION (c),
12159 "%qD is threadprivate variable in %qs clause", t,
12160 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12161 return error_mark_node;
12162 }
9dc5773f
JJ
12163 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12164 && TYPE_ATOMIC (TREE_TYPE (t))
12165 && POINTER_TYPE_P (TREE_TYPE (t)))
12166 {
12167 /* If the array section is pointer based and the pointer
12168 itself is _Atomic qualified, we need to atomically load
12169 the pointer. */
12170 c_expr expr;
12171 memset (&expr, 0, sizeof (expr));
12172 expr.value = ret;
12173 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12174 expr, false, false);
12175 ret = expr.value;
12176 }
d9a6bd32 12177 return ret;
acf0174b
JJ
12178 }
12179
12180 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
e46c7770 12181 maybe_zero_len, first_non_one, ort);
acf0174b
JJ
12182 if (ret == error_mark_node || ret == NULL_TREE)
12183 return ret;
12184
12185 type = TREE_TYPE (ret);
12186 low_bound = TREE_PURPOSE (t);
12187 length = TREE_VALUE (t);
12188
12189 if (low_bound == error_mark_node || length == error_mark_node)
12190 return error_mark_node;
12191
12192 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12193 {
12194 error_at (OMP_CLAUSE_LOCATION (c),
12195 "low bound %qE of array section does not have integral type",
12196 low_bound);
12197 return error_mark_node;
12198 }
12199 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12200 {
12201 error_at (OMP_CLAUSE_LOCATION (c),
12202 "length %qE of array section does not have integral type",
12203 length);
12204 return error_mark_node;
12205 }
12206 if (low_bound
12207 && TREE_CODE (low_bound) == INTEGER_CST
12208 && TYPE_PRECISION (TREE_TYPE (low_bound))
12209 > TYPE_PRECISION (sizetype))
12210 low_bound = fold_convert (sizetype, low_bound);
12211 if (length
12212 && TREE_CODE (length) == INTEGER_CST
12213 && TYPE_PRECISION (TREE_TYPE (length))
12214 > TYPE_PRECISION (sizetype))
12215 length = fold_convert (sizetype, length);
12216 if (low_bound == NULL_TREE)
12217 low_bound = integer_zero_node;
12218
12219 if (length != NULL_TREE)
12220 {
12221 if (!integer_nonzerop (length))
d9a6bd32
JJ
12222 {
12223 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12224 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12225 {
12226 if (integer_zerop (length))
12227 {
12228 error_at (OMP_CLAUSE_LOCATION (c),
12229 "zero length array section in %qs clause",
12230 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12231 return error_mark_node;
12232 }
12233 }
12234 else
12235 maybe_zero_len = true;
12236 }
acf0174b
JJ
12237 if (first_non_one == types.length ()
12238 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12239 first_non_one++;
12240 }
12241 if (TREE_CODE (type) == ARRAY_TYPE)
12242 {
12243 if (length == NULL_TREE
12244 && (TYPE_DOMAIN (type) == NULL_TREE
12245 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12246 {
12247 error_at (OMP_CLAUSE_LOCATION (c),
12248 "for unknown bound array type length expression must "
12249 "be specified");
12250 return error_mark_node;
12251 }
12252 if (TREE_CODE (low_bound) == INTEGER_CST
12253 && tree_int_cst_sgn (low_bound) == -1)
12254 {
12255 error_at (OMP_CLAUSE_LOCATION (c),
12256 "negative low bound in array section in %qs clause",
12257 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12258 return error_mark_node;
12259 }
12260 if (length != NULL_TREE
12261 && TREE_CODE (length) == INTEGER_CST
12262 && tree_int_cst_sgn (length) == -1)
12263 {
12264 error_at (OMP_CLAUSE_LOCATION (c),
12265 "negative length in array section in %qs clause",
12266 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12267 return error_mark_node;
12268 }
12269 if (TYPE_DOMAIN (type)
12270 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12271 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12272 == INTEGER_CST)
12273 {
12274 tree size = size_binop (PLUS_EXPR,
12275 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12276 size_one_node);
12277 if (TREE_CODE (low_bound) == INTEGER_CST)
12278 {
12279 if (tree_int_cst_lt (size, low_bound))
12280 {
12281 error_at (OMP_CLAUSE_LOCATION (c),
12282 "low bound %qE above array section size "
12283 "in %qs clause", low_bound,
12284 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12285 return error_mark_node;
12286 }
12287 if (tree_int_cst_equal (size, low_bound))
d9a6bd32
JJ
12288 {
12289 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12290 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12291 {
12292 error_at (OMP_CLAUSE_LOCATION (c),
12293 "zero length array section in %qs clause",
12294 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12295 return error_mark_node;
12296 }
12297 maybe_zero_len = true;
12298 }
acf0174b
JJ
12299 else if (length == NULL_TREE
12300 && first_non_one == types.length ()
12301 && tree_int_cst_equal
12302 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12303 low_bound))
12304 first_non_one++;
12305 }
12306 else if (length == NULL_TREE)
12307 {
d9a6bd32
JJ
12308 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12309 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12310 maybe_zero_len = true;
acf0174b
JJ
12311 if (first_non_one == types.length ())
12312 first_non_one++;
12313 }
12314 if (length && TREE_CODE (length) == INTEGER_CST)
12315 {
12316 if (tree_int_cst_lt (size, length))
12317 {
12318 error_at (OMP_CLAUSE_LOCATION (c),
12319 "length %qE above array section size "
12320 "in %qs clause", length,
12321 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12322 return error_mark_node;
12323 }
12324 if (TREE_CODE (low_bound) == INTEGER_CST)
12325 {
12326 tree lbpluslen
12327 = size_binop (PLUS_EXPR,
12328 fold_convert (sizetype, low_bound),
12329 fold_convert (sizetype, length));
12330 if (TREE_CODE (lbpluslen) == INTEGER_CST
12331 && tree_int_cst_lt (size, lbpluslen))
12332 {
12333 error_at (OMP_CLAUSE_LOCATION (c),
12334 "high bound %qE above array section size "
12335 "in %qs clause", lbpluslen,
12336 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12337 return error_mark_node;
12338 }
12339 }
12340 }
12341 }
12342 else if (length == NULL_TREE)
12343 {
d9a6bd32
JJ
12344 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12345 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12346 maybe_zero_len = true;
acf0174b
JJ
12347 if (first_non_one == types.length ())
12348 first_non_one++;
12349 }
12350
12351 /* For [lb:] we will need to evaluate lb more than once. */
12352 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12353 {
12354 tree lb = c_save_expr (low_bound);
12355 if (lb != low_bound)
12356 {
12357 TREE_PURPOSE (t) = lb;
12358 low_bound = lb;
12359 }
12360 }
12361 }
12362 else if (TREE_CODE (type) == POINTER_TYPE)
12363 {
12364 if (length == NULL_TREE)
12365 {
12366 error_at (OMP_CLAUSE_LOCATION (c),
12367 "for pointer type length expression must be specified");
12368 return error_mark_node;
12369 }
d9a6bd32
JJ
12370 if (length != NULL_TREE
12371 && TREE_CODE (length) == INTEGER_CST
12372 && tree_int_cst_sgn (length) == -1)
12373 {
12374 error_at (OMP_CLAUSE_LOCATION (c),
12375 "negative length in array section in %qs clause",
12376 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12377 return error_mark_node;
12378 }
acf0174b
JJ
12379 /* If there is a pointer type anywhere but in the very first
12380 array-section-subscript, the array section can't be contiguous. */
12381 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12382 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12383 {
12384 error_at (OMP_CLAUSE_LOCATION (c),
12385 "array section is not contiguous in %qs clause",
12386 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12387 return error_mark_node;
12388 }
12389 }
12390 else
12391 {
12392 error_at (OMP_CLAUSE_LOCATION (c),
12393 "%qE does not have pointer or array type", ret);
12394 return error_mark_node;
12395 }
12396 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12397 types.safe_push (TREE_TYPE (ret));
12398 /* We will need to evaluate lb more than once. */
12399 tree lb = c_save_expr (low_bound);
12400 if (lb != low_bound)
12401 {
12402 TREE_PURPOSE (t) = lb;
12403 low_bound = lb;
12404 }
12405 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12406 return ret;
12407}
12408
12409/* Handle array sections for clause C. */
12410
12411static bool
e46c7770 12412handle_omp_array_sections (tree c, enum c_omp_region_type ort)
acf0174b
JJ
12413{
12414 bool maybe_zero_len = false;
12415 unsigned int first_non_one = 0;
d9a6bd32 12416 auto_vec<tree, 10> types;
acf0174b 12417 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
d9a6bd32 12418 maybe_zero_len, first_non_one,
e46c7770 12419 ort);
acf0174b 12420 if (first == error_mark_node)
d9a6bd32 12421 return true;
acf0174b 12422 if (first == NULL_TREE)
d9a6bd32 12423 return false;
acf0174b
JJ
12424 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12425 {
12426 tree t = OMP_CLAUSE_DECL (c);
12427 tree tem = NULL_TREE;
acf0174b
JJ
12428 /* Need to evaluate side effects in the length expressions
12429 if any. */
12430 while (TREE_CODE (t) == TREE_LIST)
12431 {
12432 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12433 {
12434 if (tem == NULL_TREE)
12435 tem = TREE_VALUE (t);
12436 else
12437 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12438 TREE_VALUE (t), tem);
12439 }
12440 t = TREE_CHAIN (t);
12441 }
12442 if (tem)
12443 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12444 first = c_fully_fold (first, false, NULL);
12445 OMP_CLAUSE_DECL (c) = first;
12446 }
12447 else
12448 {
12449 unsigned int num = types.length (), i;
12450 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12451 tree condition = NULL_TREE;
12452
12453 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12454 maybe_zero_len = true;
12455
12456 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12457 t = TREE_CHAIN (t))
12458 {
12459 tree low_bound = TREE_PURPOSE (t);
12460 tree length = TREE_VALUE (t);
12461
12462 i--;
12463 if (low_bound
12464 && TREE_CODE (low_bound) == INTEGER_CST
12465 && TYPE_PRECISION (TREE_TYPE (low_bound))
12466 > TYPE_PRECISION (sizetype))
12467 low_bound = fold_convert (sizetype, low_bound);
12468 if (length
12469 && TREE_CODE (length) == INTEGER_CST
12470 && TYPE_PRECISION (TREE_TYPE (length))
12471 > TYPE_PRECISION (sizetype))
12472 length = fold_convert (sizetype, length);
12473 if (low_bound == NULL_TREE)
12474 low_bound = integer_zero_node;
12475 if (!maybe_zero_len && i > first_non_one)
12476 {
12477 if (integer_nonzerop (low_bound))
12478 goto do_warn_noncontiguous;
12479 if (length != NULL_TREE
12480 && TREE_CODE (length) == INTEGER_CST
12481 && TYPE_DOMAIN (types[i])
12482 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12483 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12484 == INTEGER_CST)
12485 {
12486 tree size;
12487 size = size_binop (PLUS_EXPR,
12488 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12489 size_one_node);
12490 if (!tree_int_cst_equal (length, size))
12491 {
12492 do_warn_noncontiguous:
12493 error_at (OMP_CLAUSE_LOCATION (c),
12494 "array section is not contiguous in %qs "
12495 "clause",
12496 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
acf0174b
JJ
12497 return true;
12498 }
12499 }
12500 if (length != NULL_TREE
12501 && TREE_SIDE_EFFECTS (length))
12502 {
12503 if (side_effects == NULL_TREE)
12504 side_effects = length;
12505 else
12506 side_effects = build2 (COMPOUND_EXPR,
12507 TREE_TYPE (side_effects),
12508 length, side_effects);
12509 }
12510 }
12511 else
12512 {
12513 tree l;
12514
d9a6bd32
JJ
12515 if (i > first_non_one
12516 && ((length && integer_nonzerop (length))
12517 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
acf0174b
JJ
12518 continue;
12519 if (length)
12520 l = fold_convert (sizetype, length);
12521 else
12522 {
12523 l = size_binop (PLUS_EXPR,
12524 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12525 size_one_node);
12526 l = size_binop (MINUS_EXPR, l,
12527 fold_convert (sizetype, low_bound));
12528 }
12529 if (i > first_non_one)
12530 {
12531 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12532 size_zero_node);
12533 if (condition == NULL_TREE)
12534 condition = l;
12535 else
12536 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12537 l, condition);
12538 }
12539 else if (size == NULL_TREE)
12540 {
12541 size = size_in_bytes (TREE_TYPE (types[i]));
d9a6bd32
JJ
12542 tree eltype = TREE_TYPE (types[num - 1]);
12543 while (TREE_CODE (eltype) == ARRAY_TYPE)
12544 eltype = TREE_TYPE (eltype);
12545 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12546 {
12547 if (integer_zerop (size)
12548 || integer_zerop (size_in_bytes (eltype)))
12549 {
12550 error_at (OMP_CLAUSE_LOCATION (c),
12551 "zero length array section in %qs clause",
12552 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12553 return error_mark_node;
12554 }
12555 size = size_binop (EXACT_DIV_EXPR, size,
12556 size_in_bytes (eltype));
12557 }
acf0174b
JJ
12558 size = size_binop (MULT_EXPR, size, l);
12559 if (condition)
12560 size = fold_build3 (COND_EXPR, sizetype, condition,
12561 size, size_zero_node);
12562 }
12563 else
12564 size = size_binop (MULT_EXPR, size, l);
12565 }
12566 }
acf0174b
JJ
12567 if (side_effects)
12568 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
d9a6bd32
JJ
12569 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12570 {
12571 size = size_binop (MINUS_EXPR, size, size_one_node);
12572 size = c_fully_fold (size, false, NULL);
12573 tree index_type = build_index_type (size);
12574 tree eltype = TREE_TYPE (first);
12575 while (TREE_CODE (eltype) == ARRAY_TYPE)
12576 eltype = TREE_TYPE (eltype);
12577 tree type = build_array_type (eltype, index_type);
12578 tree ptype = build_pointer_type (eltype);
12579 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12580 t = build_fold_addr_expr (t);
e01d41e5
JJ
12581 tree t2 = build_fold_addr_expr (first);
12582 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12583 ptrdiff_type_node, t2);
12584 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12585 ptrdiff_type_node, t2,
12586 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12587 ptrdiff_type_node, t));
12588 t2 = c_fully_fold (t2, false, NULL);
12589 if (tree_fits_shwi_p (t2))
12590 t = build2 (MEM_REF, type, t,
12591 build_int_cst (ptype, tree_to_shwi (t2)));
12592 else
12593 {
12594 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12595 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12596 TREE_TYPE (t), t, t2);
12597 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12598 }
d9a6bd32
JJ
12599 OMP_CLAUSE_DECL (c) = t;
12600 return false;
12601 }
acf0174b
JJ
12602 first = c_fully_fold (first, false, NULL);
12603 OMP_CLAUSE_DECL (c) = first;
12604 if (size)
12605 size = c_fully_fold (size, false, NULL);
12606 OMP_CLAUSE_SIZE (c) = size;
d9a6bd32
JJ
12607 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12608 || (TREE_CODE (t) == COMPONENT_REF
12609 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
acf0174b 12610 return false;
41dbbb37 12611 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
e46c7770 12612 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
d9a6bd32
JJ
12613 switch (OMP_CLAUSE_MAP_KIND (c))
12614 {
12615 case GOMP_MAP_ALLOC:
12616 case GOMP_MAP_TO:
12617 case GOMP_MAP_FROM:
12618 case GOMP_MAP_TOFROM:
12619 case GOMP_MAP_ALWAYS_TO:
12620 case GOMP_MAP_ALWAYS_FROM:
12621 case GOMP_MAP_ALWAYS_TOFROM:
12622 case GOMP_MAP_RELEASE:
12623 case GOMP_MAP_DELETE:
b1c9c068
CP
12624 case GOMP_MAP_FORCE_TO:
12625 case GOMP_MAP_FORCE_FROM:
12626 case GOMP_MAP_FORCE_TOFROM:
12627 case GOMP_MAP_FORCE_PRESENT:
d9a6bd32
JJ
12628 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12629 break;
12630 default:
12631 break;
12632 }
acf0174b 12633 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
e46c7770 12634 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
e01d41e5
JJ
12635 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12636 else if (TREE_CODE (t) == COMPONENT_REF)
12637 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12638 else
12639 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12640 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12641 && !c_mark_addressable (t))
acf0174b
JJ
12642 return false;
12643 OMP_CLAUSE_DECL (c2) = t;
12644 t = build_fold_addr_expr (first);
12645 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12646 tree ptr = OMP_CLAUSE_DECL (c2);
12647 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12648 ptr = build_fold_addr_expr (ptr);
12649 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12650 ptrdiff_type_node, t,
12651 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12652 ptrdiff_type_node, ptr));
12653 t = c_fully_fold (t, false, NULL);
12654 OMP_CLAUSE_SIZE (c2) = t;
12655 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12656 OMP_CLAUSE_CHAIN (c) = c2;
12657 }
12658 return false;
12659}
12660
12661/* Helper function of finish_omp_clauses. Clone STMT as if we were making
12662 an inline call. But, remap
12663 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12664 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12665
12666static tree
12667c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12668 tree decl, tree placeholder)
12669{
12670 copy_body_data id;
b787e7a2 12671 hash_map<tree, tree> decl_map;
acf0174b 12672
b787e7a2
TS
12673 decl_map.put (omp_decl1, placeholder);
12674 decl_map.put (omp_decl2, decl);
acf0174b
JJ
12675 memset (&id, 0, sizeof (id));
12676 id.src_fn = DECL_CONTEXT (omp_decl1);
12677 id.dst_fn = current_function_decl;
12678 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 12679 id.decl_map = &decl_map;
acf0174b
JJ
12680
12681 id.copy_decl = copy_decl_no_change;
12682 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12683 id.transform_new_cfg = true;
12684 id.transform_return_to_modify = false;
12685 id.transform_lang_insert_block = NULL;
12686 id.eh_lp_nr = 0;
12687 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
12688 return stmt;
12689}
12690
12691/* Helper function of c_finish_omp_clauses, called via walk_tree.
12692 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12693
12694static tree
12695c_find_omp_placeholder_r (tree *tp, int *, void *data)
12696{
12697 if (*tp == (tree) data)
12698 return *tp;
12699 return NULL_TREE;
12700}
12701
41dbbb37 12702/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
12703 Remove any elements from the list that are invalid. */
12704
12705tree
77886428 12706c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
953ff289
DN
12707{
12708 bitmap_head generic_head, firstprivate_head, lastprivate_head;
e46c7770 12709 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
d9a6bd32
JJ
12710 tree c, t, type, *pc;
12711 tree simdlen = NULL_TREE, safelen = NULL_TREE;
acf0174b
JJ
12712 bool branch_seen = false;
12713 bool copyprivate_seen = false;
e01d41e5 12714 bool linear_variable_step_check = false;
acf0174b 12715 tree *nowait_clause = NULL;
e01d41e5
JJ
12716 bool ordered_seen = false;
12717 tree schedule_clause = NULL_TREE;
b605f663 12718 bool oacc_async = false;
953ff289
DN
12719
12720 bitmap_obstack_initialize (NULL);
12721 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12722 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12723 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 12724 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
d9a6bd32
JJ
12725 bitmap_initialize (&map_head, &bitmap_default_obstack);
12726 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
e46c7770 12727 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
953ff289 12728
b605f663
CLT
12729 if (ort & C_ORT_ACC)
12730 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12731 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12732 {
12733 oacc_async = true;
12734 break;
12735 }
9dc5773f 12736
953ff289
DN
12737 for (pc = &clauses, c = clauses; c ; c = *pc)
12738 {
12739 bool remove = false;
12740 bool need_complete = false;
12741 bool need_implicitly_determined = false;
12742
aaf46ef9 12743 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
12744 {
12745 case OMP_CLAUSE_SHARED:
953ff289
DN
12746 need_implicitly_determined = true;
12747 goto check_dup_generic;
12748
12749 case OMP_CLAUSE_PRIVATE:
953ff289
DN
12750 need_complete = true;
12751 need_implicitly_determined = true;
12752 goto check_dup_generic;
12753
12754 case OMP_CLAUSE_REDUCTION:
953ff289
DN
12755 need_implicitly_determined = true;
12756 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12757 if (TREE_CODE (t) == TREE_LIST)
12758 {
e46c7770 12759 if (handle_omp_array_sections (c, ort))
d9a6bd32
JJ
12760 {
12761 remove = true;
12762 break;
12763 }
12764
12765 t = OMP_CLAUSE_DECL (c);
12766 }
4f2e1536 12767 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
d9a6bd32
JJ
12768 if (t == error_mark_node)
12769 {
12770 remove = true;
12771 break;
12772 }
b605f663
CLT
12773 if (oacc_async)
12774 c_mark_addressable (t);
d9a6bd32
JJ
12775 type = TREE_TYPE (t);
12776 if (TREE_CODE (t) == MEM_REF)
12777 type = TREE_TYPE (type);
12778 if (TREE_CODE (type) == ARRAY_TYPE)
12779 {
12780 tree oatype = type;
12781 gcc_assert (TREE_CODE (t) != MEM_REF);
12782 while (TREE_CODE (type) == ARRAY_TYPE)
12783 type = TREE_TYPE (type);
12784 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12785 {
12786 error_at (OMP_CLAUSE_LOCATION (c),
12787 "%qD in %<reduction%> clause is a zero size array",
12788 t);
12789 remove = true;
12790 break;
12791 }
12792 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12793 TYPE_SIZE_UNIT (type));
12794 if (integer_zerop (size))
12795 {
12796 error_at (OMP_CLAUSE_LOCATION (c),
12797 "%qD in %<reduction%> clause is a zero size array",
12798 t);
12799 remove = true;
12800 break;
12801 }
12802 size = size_binop (MINUS_EXPR, size, size_one_node);
12803 tree index_type = build_index_type (size);
12804 tree atype = build_array_type (type, index_type);
12805 tree ptype = build_pointer_type (type);
12806 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12807 t = build_fold_addr_expr (t);
12808 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12809 OMP_CLAUSE_DECL (c) = t;
12810 }
9dc5773f
JJ
12811 if (TYPE_ATOMIC (type))
12812 {
12813 error_at (OMP_CLAUSE_LOCATION (c),
12814 "%<_Atomic%> %qE in %<reduction%> clause", t);
12815 remove = true;
12816 break;
12817 }
acf0174b 12818 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
d9a6bd32
JJ
12819 && (FLOAT_TYPE_P (type)
12820 || TREE_CODE (type) == COMPLEX_TYPE))
953ff289
DN
12821 {
12822 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12823 const char *r_name = NULL;
12824
12825 switch (r_code)
12826 {
12827 case PLUS_EXPR:
12828 case MULT_EXPR:
12829 case MINUS_EXPR:
652fea39 12830 break;
20906c66 12831 case MIN_EXPR:
d9a6bd32 12832 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39
JJ
12833 r_name = "min";
12834 break;
20906c66 12835 case MAX_EXPR:
d9a6bd32 12836 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39 12837 r_name = "max";
953ff289
DN
12838 break;
12839 case BIT_AND_EXPR:
12840 r_name = "&";
12841 break;
12842 case BIT_XOR_EXPR:
12843 r_name = "^";
12844 break;
12845 case BIT_IOR_EXPR:
12846 r_name = "|";
12847 break;
12848 case TRUTH_ANDIF_EXPR:
d9a6bd32 12849 if (FLOAT_TYPE_P (type))
652fea39 12850 r_name = "&&";
953ff289
DN
12851 break;
12852 case TRUTH_ORIF_EXPR:
d9a6bd32 12853 if (FLOAT_TYPE_P (type))
652fea39 12854 r_name = "||";
953ff289
DN
12855 break;
12856 default:
12857 gcc_unreachable ();
12858 }
12859 if (r_name)
12860 {
c2255bc4
AH
12861 error_at (OMP_CLAUSE_LOCATION (c),
12862 "%qE has invalid type for %<reduction(%s)%>",
12863 t, r_name);
953ff289 12864 remove = true;
ee1d5a02 12865 break;
953ff289
DN
12866 }
12867 }
acf0174b
JJ
12868 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12869 {
12870 error_at (OMP_CLAUSE_LOCATION (c),
d9a6bd32 12871 "user defined reduction not found for %qE", t);
acf0174b 12872 remove = true;
ee1d5a02 12873 break;
acf0174b
JJ
12874 }
12875 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12876 {
12877 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
d9a6bd32 12878 type = TYPE_MAIN_VARIANT (type);
acf0174b
JJ
12879 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12880 VAR_DECL, NULL_TREE, type);
d9a6bd32 12881 tree decl_placeholder = NULL_TREE;
acf0174b
JJ
12882 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12883 DECL_ARTIFICIAL (placeholder) = 1;
12884 DECL_IGNORED_P (placeholder) = 1;
d9a6bd32
JJ
12885 if (TREE_CODE (t) == MEM_REF)
12886 {
12887 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12888 VAR_DECL, NULL_TREE, type);
12889 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12890 DECL_ARTIFICIAL (decl_placeholder) = 1;
12891 DECL_IGNORED_P (decl_placeholder) = 1;
12892 }
acf0174b
JJ
12893 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12894 c_mark_addressable (placeholder);
12895 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
d9a6bd32
JJ
12896 c_mark_addressable (decl_placeholder ? decl_placeholder
12897 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12898 OMP_CLAUSE_REDUCTION_MERGE (c)
12899 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12900 TREE_VEC_ELT (list, 0),
12901 TREE_VEC_ELT (list, 1),
d9a6bd32
JJ
12902 decl_placeholder ? decl_placeholder
12903 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b
JJ
12904 OMP_CLAUSE_REDUCTION_MERGE (c)
12905 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12906 void_type_node, NULL_TREE,
d9a6bd32 12907 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
acf0174b
JJ
12908 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12909 if (TREE_VEC_LENGTH (list) == 6)
12910 {
12911 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
d9a6bd32
JJ
12912 c_mark_addressable (decl_placeholder ? decl_placeholder
12913 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12914 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12915 c_mark_addressable (placeholder);
12916 tree init = TREE_VEC_ELT (list, 5);
12917 if (init == error_mark_node)
12918 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12919 OMP_CLAUSE_REDUCTION_INIT (c)
12920 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12921 TREE_VEC_ELT (list, 3),
d9a6bd32
JJ
12922 decl_placeholder ? decl_placeholder
12923 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b 12924 if (TREE_VEC_ELT (list, 5) == error_mark_node)
d9a6bd32
JJ
12925 {
12926 tree v = decl_placeholder ? decl_placeholder : t;
12927 OMP_CLAUSE_REDUCTION_INIT (c)
12928 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12929 OMP_CLAUSE_REDUCTION_INIT (c));
12930 }
acf0174b
JJ
12931 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12932 c_find_omp_placeholder_r,
12933 placeholder, NULL))
12934 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12935 }
12936 else
12937 {
12938 tree init;
d9a6bd32
JJ
12939 tree v = decl_placeholder ? decl_placeholder : t;
12940 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12941 init = build_constructor (TREE_TYPE (v), NULL);
acf0174b 12942 else
d9a6bd32 12943 init = fold_convert (TREE_TYPE (v), integer_zero_node);
acf0174b 12944 OMP_CLAUSE_REDUCTION_INIT (c)
d9a6bd32 12945 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
acf0174b
JJ
12946 }
12947 OMP_CLAUSE_REDUCTION_INIT (c)
12948 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12949 void_type_node, NULL_TREE,
12950 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12951 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12952 }
d9a6bd32
JJ
12953 if (TREE_CODE (t) == MEM_REF)
12954 {
12955 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12956 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12957 != INTEGER_CST)
12958 {
12959 sorry ("variable length element type in array "
12960 "%<reduction%> clause");
12961 remove = true;
12962 break;
12963 }
12964 t = TREE_OPERAND (t, 0);
e01d41e5
JJ
12965 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12966 t = TREE_OPERAND (t, 0);
d9a6bd32
JJ
12967 if (TREE_CODE (t) == ADDR_EXPR)
12968 t = TREE_OPERAND (t, 0);
12969 }
12970 goto check_dup_generic_t;
953ff289
DN
12971
12972 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
12973 copyprivate_seen = true;
12974 if (nowait_clause)
12975 {
12976 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12977 "%<nowait%> clause must not be used together "
12978 "with %<copyprivate%>");
12979 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12980 nowait_clause = NULL;
12981 }
953ff289
DN
12982 goto check_dup_generic;
12983
12984 case OMP_CLAUSE_COPYIN:
953ff289 12985 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12986 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
953ff289 12987 {
c2255bc4
AH
12988 error_at (OMP_CLAUSE_LOCATION (c),
12989 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12990 remove = true;
ee1d5a02 12991 break;
953ff289
DN
12992 }
12993 goto check_dup_generic;
12994
acf0174b 12995 case OMP_CLAUSE_LINEAR:
77886428 12996 if (ort != C_ORT_OMP_DECLARE_SIMD)
d9a6bd32 12997 need_implicitly_determined = true;
acf0174b 12998 t = OMP_CLAUSE_DECL (c);
77886428 12999 if (ort != C_ORT_OMP_DECLARE_SIMD
d9a6bd32
JJ
13000 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13001 {
13002 error_at (OMP_CLAUSE_LOCATION (c),
13003 "modifier should not be specified in %<linear%> "
13004 "clause on %<simd%> or %<for%> constructs");
13005 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13006 }
77886428 13007 if (ort & C_ORT_CILK)
acf0174b 13008 {
477d4906
IV
13009 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13010 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13011 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13012 {
13013 error_at (OMP_CLAUSE_LOCATION (c),
13014 "linear clause applied to non-integral, "
13015 "non-floating, non-pointer variable with type %qT",
13016 TREE_TYPE (t));
13017 remove = true;
13018 break;
13019 }
13020 }
13021 else
13022 {
13023 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13024 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13025 {
13026 error_at (OMP_CLAUSE_LOCATION (c),
13027 "linear clause applied to non-integral non-pointer "
13028 "variable with type %qT", TREE_TYPE (t));
13029 remove = true;
13030 break;
13031 }
9dc5773f
JJ
13032 if (TYPE_ATOMIC (TREE_TYPE (t)))
13033 {
13034 error_at (OMP_CLAUSE_LOCATION (c),
13035 "%<_Atomic%> %qD in %<linear%> clause", t);
13036 remove = true;
13037 break;
13038 }
acf0174b 13039 }
77886428 13040 if (ort == C_ORT_OMP_DECLARE_SIMD)
e01d41e5
JJ
13041 {
13042 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13043 if (TREE_CODE (s) == PARM_DECL)
13044 {
13045 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13046 /* map_head bitmap is used as uniform_head if
13047 declare_simd. */
13048 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13049 linear_variable_step_check = true;
13050 goto check_dup_generic;
13051 }
13052 if (TREE_CODE (s) != INTEGER_CST)
13053 {
13054 error_at (OMP_CLAUSE_LOCATION (c),
13055 "%<linear%> clause step %qE is neither constant "
13056 "nor a parameter", s);
13057 remove = true;
13058 break;
13059 }
13060 }
acf0174b
JJ
13061 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13062 {
13063 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13064 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13065 OMP_CLAUSE_DECL (c), s);
13066 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
13067 sizetype, fold_convert (sizetype, s),
13068 fold_convert
13069 (sizetype, OMP_CLAUSE_DECL (c)));
acf0174b
JJ
13070 if (s == error_mark_node)
13071 s = size_one_node;
13072 OMP_CLAUSE_LINEAR_STEP (c) = s;
13073 }
da6f124d
JJ
13074 else
13075 OMP_CLAUSE_LINEAR_STEP (c)
13076 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
13077 goto check_dup_generic;
13078
953ff289
DN
13079 check_dup_generic:
13080 t = OMP_CLAUSE_DECL (c);
d9a6bd32 13081 check_dup_generic_t:
0ae9bd27 13082 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13083 {
c2255bc4 13084 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
13085 "%qE is not a variable in clause %qs", t,
13086 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13087 remove = true;
13088 }
e46c7770
CP
13089 else if (ort == C_ORT_ACC
13090 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13091 {
13092 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13093 {
13094 error ("%qD appears more than once in reduction clauses", t);
13095 remove = true;
13096 }
13097 else
13098 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13099 }
953ff289
DN
13100 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13101 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13102 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13103 {
c2255bc4
AH
13104 error_at (OMP_CLAUSE_LOCATION (c),
13105 "%qE appears more than once in data clauses", t);
953ff289
DN
13106 remove = true;
13107 }
e01d41e5
JJ
13108 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13109 && bitmap_bit_p (&map_head, DECL_UID (t)))
13110 {
e46c7770
CP
13111 if (ort == C_ORT_ACC)
13112 error ("%qD appears more than once in data clauses", t);
13113 else
13114 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13115 remove = true;
13116 }
953ff289
DN
13117 else
13118 bitmap_set_bit (&generic_head, DECL_UID (t));
13119 break;
13120
13121 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
13122 t = OMP_CLAUSE_DECL (c);
13123 need_complete = true;
13124 need_implicitly_determined = true;
0ae9bd27 13125 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13126 {
c2255bc4
AH
13127 error_at (OMP_CLAUSE_LOCATION (c),
13128 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
13129 remove = true;
13130 }
13131 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13132 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13133 {
c2255bc4
AH
13134 error_at (OMP_CLAUSE_LOCATION (c),
13135 "%qE appears more than once in data clauses", t);
953ff289
DN
13136 remove = true;
13137 }
e01d41e5
JJ
13138 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13139 {
e46c7770
CP
13140 if (ort == C_ORT_ACC)
13141 error ("%qD appears more than once in data clauses", t);
13142 else
13143 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13144 remove = true;
13145 }
953ff289
DN
13146 else
13147 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13148 break;
13149
13150 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
13151 t = OMP_CLAUSE_DECL (c);
13152 need_complete = true;
13153 need_implicitly_determined = true;
0ae9bd27 13154 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13155 {
c2255bc4
AH
13156 error_at (OMP_CLAUSE_LOCATION (c),
13157 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
13158 remove = true;
13159 }
13160 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13161 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13162 {
c2255bc4
AH
13163 error_at (OMP_CLAUSE_LOCATION (c),
13164 "%qE appears more than once in data clauses", t);
953ff289
DN
13165 remove = true;
13166 }
13167 else
13168 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13169 break;
13170
acf0174b
JJ
13171 case OMP_CLAUSE_ALIGNED:
13172 t = OMP_CLAUSE_DECL (c);
0ae9bd27 13173 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13174 {
13175 error_at (OMP_CLAUSE_LOCATION (c),
13176 "%qE is not a variable in %<aligned%> clause", t);
13177 remove = true;
13178 }
5a9785fb
JJ
13179 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13180 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13181 {
13182 error_at (OMP_CLAUSE_LOCATION (c),
13183 "%qE in %<aligned%> clause is neither a pointer nor "
13184 "an array", t);
13185 remove = true;
13186 }
9dc5773f
JJ
13187 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13188 {
13189 error_at (OMP_CLAUSE_LOCATION (c),
13190 "%<_Atomic%> %qD in %<aligned%> clause", t);
13191 remove = true;
13192 break;
13193 }
acf0174b
JJ
13194 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13195 {
13196 error_at (OMP_CLAUSE_LOCATION (c),
13197 "%qE appears more than once in %<aligned%> clauses",
13198 t);
13199 remove = true;
13200 }
13201 else
13202 bitmap_set_bit (&aligned_head, DECL_UID (t));
13203 break;
13204
13205 case OMP_CLAUSE_DEPEND:
13206 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
13207 if (t == NULL_TREE)
13208 {
13209 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13210 == OMP_CLAUSE_DEPEND_SOURCE);
13211 break;
13212 }
13213 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13214 {
13215 gcc_assert (TREE_CODE (t) == TREE_LIST);
13216 for (; t; t = TREE_CHAIN (t))
13217 {
13218 tree decl = TREE_VALUE (t);
13219 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13220 {
13221 tree offset = TREE_PURPOSE (t);
13222 bool neg = wi::neg_p ((wide_int) offset);
13223 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13224 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13225 neg ? MINUS_EXPR : PLUS_EXPR,
13226 decl, offset);
13227 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
13228 sizetype,
13229 fold_convert (sizetype, t2),
13230 fold_convert (sizetype, decl));
d9a6bd32
JJ
13231 if (t2 == error_mark_node)
13232 {
13233 remove = true;
13234 break;
13235 }
13236 TREE_PURPOSE (t) = t2;
13237 }
13238 }
13239 break;
13240 }
acf0174b
JJ
13241 if (TREE_CODE (t) == TREE_LIST)
13242 {
e46c7770 13243 if (handle_omp_array_sections (c, ort))
acf0174b
JJ
13244 remove = true;
13245 break;
13246 }
13247 if (t == error_mark_node)
13248 remove = true;
0ae9bd27 13249 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13250 {
13251 error_at (OMP_CLAUSE_LOCATION (c),
13252 "%qE is not a variable in %<depend%> clause", t);
13253 remove = true;
13254 }
13255 else if (!c_mark_addressable (t))
13256 remove = true;
13257 break;
13258
13259 case OMP_CLAUSE_MAP:
13260 case OMP_CLAUSE_TO:
13261 case OMP_CLAUSE_FROM:
41dbbb37 13262 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
13263 t = OMP_CLAUSE_DECL (c);
13264 if (TREE_CODE (t) == TREE_LIST)
13265 {
e46c7770 13266 if (handle_omp_array_sections (c, ort))
acf0174b
JJ
13267 remove = true;
13268 else
13269 {
13270 t = OMP_CLAUSE_DECL (c);
b17a8b07 13271 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13272 {
13273 error_at (OMP_CLAUSE_LOCATION (c),
13274 "array section does not have mappable type "
13275 "in %qs clause",
13276 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13277 remove = true;
13278 }
9dc5773f
JJ
13279 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13280 {
13281 error_at (OMP_CLAUSE_LOCATION (c),
13282 "%<_Atomic%> %qE in %qs clause", t,
13283 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13284 remove = true;
13285 }
d9a6bd32
JJ
13286 while (TREE_CODE (t) == ARRAY_REF)
13287 t = TREE_OPERAND (t, 0);
13288 if (TREE_CODE (t) == COMPONENT_REF
13289 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13290 {
13291 while (TREE_CODE (t) == COMPONENT_REF)
13292 t = TREE_OPERAND (t, 0);
13293 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13294 break;
13295 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13296 {
13297 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13298 error ("%qD appears more than once in motion"
13299 " clauses", t);
e46c7770
CP
13300 else if (ort == C_ORT_ACC)
13301 error ("%qD appears more than once in data"
13302 " clauses", t);
d9a6bd32
JJ
13303 else
13304 error ("%qD appears more than once in map"
13305 " clauses", t);
13306 remove = true;
13307 }
13308 else
13309 {
13310 bitmap_set_bit (&map_head, DECL_UID (t));
13311 bitmap_set_bit (&map_field_head, DECL_UID (t));
13312 }
13313 }
acf0174b
JJ
13314 }
13315 break;
13316 }
13317 if (t == error_mark_node)
d9a6bd32
JJ
13318 {
13319 remove = true;
13320 break;
13321 }
13322 if (TREE_CODE (t) == COMPONENT_REF
77886428 13323 && (ort & C_ORT_OMP)
d9a6bd32
JJ
13324 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13325 {
13326 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13327 {
13328 error_at (OMP_CLAUSE_LOCATION (c),
13329 "bit-field %qE in %qs clause",
13330 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13331 remove = true;
13332 }
13333 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13334 {
13335 error_at (OMP_CLAUSE_LOCATION (c),
13336 "%qE does not have a mappable type in %qs clause",
13337 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13338 remove = true;
13339 }
9dc5773f
JJ
13340 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13341 {
13342 error_at (OMP_CLAUSE_LOCATION (c),
13343 "%<_Atomic%> %qE in %qs clause", t,
13344 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13345 remove = true;
13346 }
d9a6bd32
JJ
13347 while (TREE_CODE (t) == COMPONENT_REF)
13348 {
13349 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13350 == UNION_TYPE)
13351 {
13352 error_at (OMP_CLAUSE_LOCATION (c),
13353 "%qE is a member of a union", t);
13354 remove = true;
13355 break;
13356 }
13357 t = TREE_OPERAND (t, 0);
13358 }
13359 if (remove)
13360 break;
13361 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13362 {
e01d41e5 13363 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
d9a6bd32
JJ
13364 break;
13365 }
13366 }
13367 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13368 {
13369 error_at (OMP_CLAUSE_LOCATION (c),
13370 "%qE is not a variable in %qs clause", t,
13371 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13372 remove = true;
13373 }
0ae9bd27 13374 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
13375 {
13376 error_at (OMP_CLAUSE_LOCATION (c),
13377 "%qD is threadprivate variable in %qs clause", t,
13378 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13379 remove = true;
13380 }
e4606348
JJ
13381 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13382 || (OMP_CLAUSE_MAP_KIND (c)
13383 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13384 && !c_mark_addressable (t))
acf0174b 13385 remove = true;
b17a8b07 13386 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37 13387 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
d9a6bd32
JJ
13388 || (OMP_CLAUSE_MAP_KIND (c)
13389 == GOMP_MAP_FIRSTPRIVATE_POINTER)
41dbbb37
TS
13390 || (OMP_CLAUSE_MAP_KIND (c)
13391 == GOMP_MAP_FORCE_DEVICEPTR)))
d9a6bd32 13392 && t == OMP_CLAUSE_DECL (c)
b17a8b07 13393 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13394 {
13395 error_at (OMP_CLAUSE_LOCATION (c),
13396 "%qD does not have a mappable type in %qs clause", t,
13397 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13398 remove = true;
13399 }
9dc5773f
JJ
13400 else if (TREE_TYPE (t) == error_mark_node)
13401 remove = true;
13402 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13403 {
13404 error_at (OMP_CLAUSE_LOCATION (c),
13405 "%<_Atomic%> %qE in %qs clause", t,
13406 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13407 remove = true;
13408 }
d9a6bd32
JJ
13409 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13410 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13411 {
13412 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13413 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13414 {
13415 error ("%qD appears more than once in data clauses", t);
13416 remove = true;
13417 }
e01d41e5 13418 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
d9a6bd32 13419 {
e46c7770
CP
13420 if (ort == C_ORT_ACC)
13421 error ("%qD appears more than once in data clauses", t);
13422 else
13423 error ("%qD appears both in data and map clauses", t);
e01d41e5 13424 remove = true;
d9a6bd32 13425 }
e01d41e5
JJ
13426 else
13427 bitmap_set_bit (&generic_head, DECL_UID (t));
d9a6bd32
JJ
13428 }
13429 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
acf0174b
JJ
13430 {
13431 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13432 error ("%qD appears more than once in motion clauses", t);
e46c7770
CP
13433 else if (ort == C_ORT_ACC)
13434 error ("%qD appears more than once in data clauses", t);
acf0174b
JJ
13435 else
13436 error ("%qD appears more than once in map clauses", t);
13437 remove = true;
13438 }
e01d41e5
JJ
13439 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13440 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13441 {
e46c7770
CP
13442 if (ort == C_ORT_ACC)
13443 error ("%qD appears more than once in data clauses", t);
13444 else
13445 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13446 remove = true;
13447 }
acf0174b 13448 else
d9a6bd32
JJ
13449 {
13450 bitmap_set_bit (&map_head, DECL_UID (t));
13451 if (t != OMP_CLAUSE_DECL (c)
13452 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13453 bitmap_set_bit (&map_field_head, DECL_UID (t));
13454 }
13455 break;
13456
13457 case OMP_CLAUSE_TO_DECLARE:
d9a6bd32
JJ
13458 case OMP_CLAUSE_LINK:
13459 t = OMP_CLAUSE_DECL (c);
e01d41e5
JJ
13460 if (TREE_CODE (t) == FUNCTION_DECL
13461 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13462 ;
13463 else if (!VAR_P (t))
d9a6bd32 13464 {
e01d41e5
JJ
13465 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13466 error_at (OMP_CLAUSE_LOCATION (c),
13467 "%qE is neither a variable nor a function name in "
13468 "clause %qs", t,
13469 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13470 else
13471 error_at (OMP_CLAUSE_LOCATION (c),
13472 "%qE is not a variable in clause %qs", t,
13473 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
d9a6bd32
JJ
13474 remove = true;
13475 }
13476 else if (DECL_THREAD_LOCAL_P (t))
13477 {
13478 error_at (OMP_CLAUSE_LOCATION (c),
13479 "%qD is threadprivate variable in %qs clause", t,
13480 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13481 remove = true;
13482 }
13483 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13484 {
13485 error_at (OMP_CLAUSE_LOCATION (c),
13486 "%qD does not have a mappable type in %qs clause", t,
13487 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13488 remove = true;
13489 }
e01d41e5
JJ
13490 if (remove)
13491 break;
13492 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13493 {
13494 error_at (OMP_CLAUSE_LOCATION (c),
13495 "%qE appears more than once on the same "
13496 "%<declare target%> directive", t);
13497 remove = true;
13498 }
13499 else
13500 bitmap_set_bit (&generic_head, DECL_UID (t));
acf0174b
JJ
13501 break;
13502
13503 case OMP_CLAUSE_UNIFORM:
13504 t = OMP_CLAUSE_DECL (c);
13505 if (TREE_CODE (t) != PARM_DECL)
13506 {
13507 if (DECL_P (t))
13508 error_at (OMP_CLAUSE_LOCATION (c),
13509 "%qD is not an argument in %<uniform%> clause", t);
13510 else
13511 error_at (OMP_CLAUSE_LOCATION (c),
13512 "%qE is not an argument in %<uniform%> clause", t);
13513 remove = true;
ee1d5a02 13514 break;
acf0174b 13515 }
e01d41e5
JJ
13516 /* map_head bitmap is used as uniform_head if declare_simd. */
13517 bitmap_set_bit (&map_head, DECL_UID (t));
ee1d5a02 13518 goto check_dup_generic;
acf0174b 13519
d9a6bd32
JJ
13520 case OMP_CLAUSE_IS_DEVICE_PTR:
13521 case OMP_CLAUSE_USE_DEVICE_PTR:
13522 t = OMP_CLAUSE_DECL (c);
13523 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13524 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13525 {
13526 error_at (OMP_CLAUSE_LOCATION (c),
13527 "%qs variable is neither a pointer nor an array",
13528 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13529 remove = true;
13530 }
13531 goto check_dup_generic;
13532
acf0174b
JJ
13533 case OMP_CLAUSE_NOWAIT:
13534 if (copyprivate_seen)
13535 {
13536 error_at (OMP_CLAUSE_LOCATION (c),
13537 "%<nowait%> clause must not be used together "
13538 "with %<copyprivate%>");
13539 remove = true;
13540 break;
13541 }
13542 nowait_clause = pc;
13543 pc = &OMP_CLAUSE_CHAIN (c);
13544 continue;
13545
953ff289
DN
13546 case OMP_CLAUSE_IF:
13547 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
13548 case OMP_CLAUSE_NUM_TEAMS:
13549 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 13550 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
13551 case OMP_CLAUSE_UNTIED:
13552 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
13553 case OMP_CLAUSE_FINAL:
13554 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
13555 case OMP_CLAUSE_DEVICE:
13556 case OMP_CLAUSE_DIST_SCHEDULE:
13557 case OMP_CLAUSE_PARALLEL:
13558 case OMP_CLAUSE_FOR:
13559 case OMP_CLAUSE_SECTIONS:
13560 case OMP_CLAUSE_TASKGROUP:
13561 case OMP_CLAUSE_PROC_BIND:
d9a6bd32
JJ
13562 case OMP_CLAUSE_PRIORITY:
13563 case OMP_CLAUSE_GRAINSIZE:
13564 case OMP_CLAUSE_NUM_TASKS:
13565 case OMP_CLAUSE_NOGROUP:
13566 case OMP_CLAUSE_THREADS:
13567 case OMP_CLAUSE_SIMD:
13568 case OMP_CLAUSE_HINT:
13569 case OMP_CLAUSE_DEFAULTMAP:
9a771876 13570 case OMP_CLAUSE__CILK_FOR_COUNT_:
41dbbb37
TS
13571 case OMP_CLAUSE_NUM_GANGS:
13572 case OMP_CLAUSE_NUM_WORKERS:
13573 case OMP_CLAUSE_VECTOR_LENGTH:
13574 case OMP_CLAUSE_ASYNC:
13575 case OMP_CLAUSE_WAIT:
13576 case OMP_CLAUSE_AUTO:
7a5e4956 13577 case OMP_CLAUSE_INDEPENDENT:
41dbbb37
TS
13578 case OMP_CLAUSE_SEQ:
13579 case OMP_CLAUSE_GANG:
13580 case OMP_CLAUSE_WORKER:
13581 case OMP_CLAUSE_VECTOR:
7a5e4956 13582 case OMP_CLAUSE_TILE:
acf0174b
JJ
13583 pc = &OMP_CLAUSE_CHAIN (c);
13584 continue;
13585
e01d41e5
JJ
13586 case OMP_CLAUSE_SCHEDULE:
13587 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13588 {
13589 const char *p = NULL;
13590 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13591 {
13592 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13593 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13594 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13595 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13596 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13597 default: gcc_unreachable ();
13598 }
13599 if (p)
13600 {
13601 error_at (OMP_CLAUSE_LOCATION (c),
13602 "%<nonmonotonic%> modifier specified for %qs "
13603 "schedule kind", p);
13604 OMP_CLAUSE_SCHEDULE_KIND (c)
13605 = (enum omp_clause_schedule_kind)
13606 (OMP_CLAUSE_SCHEDULE_KIND (c)
13607 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13608 }
13609 }
13610 schedule_clause = c;
13611 pc = &OMP_CLAUSE_CHAIN (c);
13612 continue;
13613
13614 case OMP_CLAUSE_ORDERED:
13615 ordered_seen = true;
13616 pc = &OMP_CLAUSE_CHAIN (c);
13617 continue;
13618
d9a6bd32
JJ
13619 case OMP_CLAUSE_SAFELEN:
13620 safelen = c;
13621 pc = &OMP_CLAUSE_CHAIN (c);
13622 continue;
13623 case OMP_CLAUSE_SIMDLEN:
13624 simdlen = c;
13625 pc = &OMP_CLAUSE_CHAIN (c);
13626 continue;
13627
acf0174b
JJ
13628 case OMP_CLAUSE_INBRANCH:
13629 case OMP_CLAUSE_NOTINBRANCH:
13630 if (branch_seen)
13631 {
13632 error_at (OMP_CLAUSE_LOCATION (c),
13633 "%<inbranch%> clause is incompatible with "
13634 "%<notinbranch%>");
13635 remove = true;
13636 break;
13637 }
13638 branch_seen = true;
953ff289
DN
13639 pc = &OMP_CLAUSE_CHAIN (c);
13640 continue;
13641
13642 default:
13643 gcc_unreachable ();
13644 }
13645
13646 if (!remove)
13647 {
13648 t = OMP_CLAUSE_DECL (c);
13649
13650 if (need_complete)
13651 {
4f2e1536 13652 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
953ff289
DN
13653 if (t == error_mark_node)
13654 remove = true;
13655 }
13656
13657 if (need_implicitly_determined)
13658 {
13659 const char *share_name = NULL;
13660
0ae9bd27 13661 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
953ff289
DN
13662 share_name = "threadprivate";
13663 else switch (c_omp_predetermined_sharing (t))
13664 {
13665 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13666 break;
13667 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
13668 /* const vars may be specified in firstprivate clause. */
13669 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13670 && TREE_READONLY (t))
13671 break;
953ff289
DN
13672 share_name = "shared";
13673 break;
13674 case OMP_CLAUSE_DEFAULT_PRIVATE:
13675 share_name = "private";
13676 break;
13677 default:
13678 gcc_unreachable ();
13679 }
13680 if (share_name)
13681 {
c2255bc4
AH
13682 error_at (OMP_CLAUSE_LOCATION (c),
13683 "%qE is predetermined %qs for %qs",
acf0174b
JJ
13684 t, share_name,
13685 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13686 remove = true;
13687 }
13688 }
13689 }
13690
13691 if (remove)
13692 *pc = OMP_CLAUSE_CHAIN (c);
13693 else
13694 pc = &OMP_CLAUSE_CHAIN (c);
13695 }
13696
d9a6bd32
JJ
13697 if (simdlen
13698 && safelen
13699 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13700 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13701 {
13702 error_at (OMP_CLAUSE_LOCATION (simdlen),
13703 "%<simdlen%> clause value is bigger than "
13704 "%<safelen%> clause value");
13705 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13706 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13707 }
13708
e01d41e5
JJ
13709 if (ordered_seen
13710 && schedule_clause
13711 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13712 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13713 {
13714 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13715 "%<nonmonotonic%> schedule modifier specified together "
13716 "with %<ordered%> clause");
13717 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13718 = (enum omp_clause_schedule_kind)
13719 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13720 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13721 }
13722
13723 if (linear_variable_step_check)
13724 for (pc = &clauses, c = clauses; c ; c = *pc)
13725 {
13726 bool remove = false;
13727 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13728 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13729 && !bitmap_bit_p (&map_head,
13730 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13731 {
13732 error_at (OMP_CLAUSE_LOCATION (c),
13733 "%<linear%> clause step is a parameter %qD not "
13734 "specified in %<uniform%> clause",
13735 OMP_CLAUSE_LINEAR_STEP (c));
13736 remove = true;
13737 }
13738
13739 if (remove)
13740 *pc = OMP_CLAUSE_CHAIN (c);
13741 else
13742 pc = &OMP_CLAUSE_CHAIN (c);
13743 }
13744
953ff289
DN
13745 bitmap_obstack_release (NULL);
13746 return clauses;
13747}
9ae165a0 13748
9dc5773f
JJ
13749/* Return code to initialize DST with a copy constructor from SRC.
13750 C doesn't have copy constructors nor assignment operators, only for
13751 _Atomic vars we need to perform __atomic_load from src into a temporary
13752 followed by __atomic_store of the temporary to dst. */
13753
13754tree
13755c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13756{
13757 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13758 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13759
13760 location_t loc = OMP_CLAUSE_LOCATION (clause);
13761 tree type = TREE_TYPE (dst);
13762 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13763 tree tmp = create_tmp_var (nonatomic_type);
13764 tree tmp_addr = build_fold_addr_expr (tmp);
13765 TREE_ADDRESSABLE (tmp) = 1;
13766 TREE_NO_WARNING (tmp) = 1;
13767 tree src_addr = build_fold_addr_expr (src);
13768 tree dst_addr = build_fold_addr_expr (dst);
13769 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
13770 vec<tree, va_gc> *params;
13771 /* Expansion of a generic atomic load may require an addition
13772 element, so allocate enough to prevent a resize. */
13773 vec_alloc (params, 4);
13774
13775 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13776 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
13777 params->quick_push (src_addr);
13778 params->quick_push (tmp_addr);
13779 params->quick_push (seq_cst);
13780 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13781
13782 vec_alloc (params, 4);
13783
13784 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13785 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
13786 params->quick_push (dst_addr);
13787 params->quick_push (tmp_addr);
13788 params->quick_push (seq_cst);
13789 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13790 return build2 (COMPOUND_EXPR, void_type_node, load, store);
13791}
13792
0a35513e
AH
13793/* Create a transaction node. */
13794
13795tree
13796c_finish_transaction (location_t loc, tree block, int flags)
13797{
13798 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13799 if (flags & TM_STMT_ATTR_OUTER)
13800 TRANSACTION_EXPR_OUTER (stmt) = 1;
13801 if (flags & TM_STMT_ATTR_RELAXED)
13802 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13803 return add_stmt (stmt);
13804}
13805
9ae165a0 13806/* Make a variant type in the proper way for C/C++, propagating qualifiers
e9e32ee6
JM
13807 down to the element type of an array. If ORIG_QUAL_TYPE is not
13808 NULL, then it should be used as the qualified type
13809 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13810 preserve information about the typedef name from which an array
13811 type was derived). */
9ae165a0
DG
13812
13813tree
e9e32ee6
JM
13814c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13815 size_t orig_qual_indirect)
9ae165a0
DG
13816{
13817 if (type == error_mark_node)
13818 return type;
13819
13820 if (TREE_CODE (type) == ARRAY_TYPE)
13821 {
13822 tree t;
13823 tree element_type = c_build_qualified_type (TREE_TYPE (type),
e9e32ee6
JM
13824 type_quals, orig_qual_type,
13825 orig_qual_indirect - 1);
9ae165a0
DG
13826
13827 /* See if we already have an identically qualified type. */
e9e32ee6
JM
13828 if (orig_qual_type && orig_qual_indirect == 0)
13829 t = orig_qual_type;
13830 else
13831 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13832 {
13833 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13834 && TYPE_NAME (t) == TYPE_NAME (type)
13835 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13836 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13837 TYPE_ATTRIBUTES (type)))
13838 break;
13839 }
9ae165a0
DG
13840 if (!t)
13841 {
13842 tree domain = TYPE_DOMAIN (type);
13843
13844 t = build_variant_type_copy (type);
13845 TREE_TYPE (t) = element_type;
13846
13847 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13848 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13849 SET_TYPE_STRUCTURAL_EQUALITY (t);
13850 else if (TYPE_CANONICAL (element_type) != element_type
13851 || (domain && TYPE_CANONICAL (domain) != domain))
13852 {
b8698a0f 13853 tree unqualified_canon
9ae165a0 13854 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 13855 domain? TYPE_CANONICAL (domain)
9ae165a0 13856 : NULL_TREE);
ee45a32d
EB
13857 if (TYPE_REVERSE_STORAGE_ORDER (type))
13858 {
13859 unqualified_canon
13860 = build_distinct_type_copy (unqualified_canon);
13861 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13862 }
b8698a0f 13863 TYPE_CANONICAL (t)
9ae165a0
DG
13864 = c_build_qualified_type (unqualified_canon, type_quals);
13865 }
13866 else
13867 TYPE_CANONICAL (t) = t;
13868 }
13869 return t;
13870 }
13871
13872 /* A restrict-qualified pointer type must be a pointer to object or
13873 incomplete type. Note that the use of POINTER_TYPE_P also allows
13874 REFERENCE_TYPEs, which is appropriate for C++. */
13875 if ((type_quals & TYPE_QUAL_RESTRICT)
13876 && (!POINTER_TYPE_P (type)
13877 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13878 {
13879 error ("invalid use of %<restrict%>");
13880 type_quals &= ~TYPE_QUAL_RESTRICT;
13881 }
13882
e9e32ee6
JM
13883 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13884 ? orig_qual_type
13885 : build_qualified_type (type, type_quals));
e922069e
JW
13886 /* A variant type does not inherit the list of incomplete vars from the
13887 type main variant. */
152ef731
JJ
13888 if (RECORD_OR_UNION_TYPE_P (var_type)
13889 && TYPE_MAIN_VARIANT (var_type) != var_type)
e922069e
JW
13890 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13891 return var_type;
9ae165a0 13892}
72b5577d
ILT
13893
13894/* Build a VA_ARG_EXPR for the C parser. */
13895
13896tree
f187980b 13897c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
72b5577d 13898{
ec3fba51
MP
13899 if (error_operand_p (type))
13900 return error_mark_node;
f187980b
EB
13901 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13902 order because it takes the address of the expression. */
13903 else if (handled_component_p (expr)
13904 && reverse_storage_order_for_component_p (expr))
13905 {
13906 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13907 return error_mark_node;
13908 }
ec3fba51 13909 else if (!COMPLETE_TYPE_P (type))
4e81b788 13910 {
f187980b 13911 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
ec3fba51 13912 "type %qT", type);
4e81b788
MP
13913 return error_mark_node;
13914 }
ec3fba51 13915 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
f187980b 13916 warning_at (loc2, OPT_Wc___compat,
ec3fba51 13917 "C++ requires promoted type, not enum type, in %<va_arg%>");
f187980b 13918 return build_va_arg (loc2, expr, type);
72b5577d 13919}
acf0174b
JJ
13920
13921/* Return truthvalue of whether T1 is the same tree structure as T2.
13922 Return 1 if they are the same. Return 0 if they are different. */
13923
13924bool
13925c_tree_equal (tree t1, tree t2)
13926{
13927 enum tree_code code1, code2;
13928
13929 if (t1 == t2)
13930 return true;
13931 if (!t1 || !t2)
13932 return false;
13933
13934 for (code1 = TREE_CODE (t1);
13935 CONVERT_EXPR_CODE_P (code1)
13936 || code1 == NON_LVALUE_EXPR;
13937 code1 = TREE_CODE (t1))
13938 t1 = TREE_OPERAND (t1, 0);
13939 for (code2 = TREE_CODE (t2);
13940 CONVERT_EXPR_CODE_P (code2)
13941 || code2 == NON_LVALUE_EXPR;
13942 code2 = TREE_CODE (t2))
13943 t2 = TREE_OPERAND (t2, 0);
13944
13945 /* They might have become equal now. */
13946 if (t1 == t2)
13947 return true;
13948
13949 if (code1 != code2)
13950 return false;
13951
13952 switch (code1)
13953 {
13954 case INTEGER_CST:
807e902e 13955 return wi::eq_p (t1, t2);
acf0174b
JJ
13956
13957 case REAL_CST:
624d31fe 13958 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
acf0174b
JJ
13959
13960 case STRING_CST:
13961 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13962 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13963 TREE_STRING_LENGTH (t1));
13964
13965 case FIXED_CST:
13966 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13967 TREE_FIXED_CST (t2));
13968
13969 case COMPLEX_CST:
13970 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13971 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13972
13973 case VECTOR_CST:
13974 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13975
13976 case CONSTRUCTOR:
13977 /* We need to do this when determining whether or not two
13978 non-type pointer to member function template arguments
13979 are the same. */
13980 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13981 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13982 return false;
13983 {
13984 tree field, value;
13985 unsigned int i;
13986 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13987 {
13988 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13989 if (!c_tree_equal (field, elt2->index)
13990 || !c_tree_equal (value, elt2->value))
13991 return false;
13992 }
13993 }
13994 return true;
13995
13996 case TREE_LIST:
13997 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13998 return false;
13999 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14000 return false;
14001 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14002
14003 case SAVE_EXPR:
14004 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14005
14006 case CALL_EXPR:
14007 {
14008 tree arg1, arg2;
14009 call_expr_arg_iterator iter1, iter2;
14010 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14011 return false;
14012 for (arg1 = first_call_expr_arg (t1, &iter1),
14013 arg2 = first_call_expr_arg (t2, &iter2);
14014 arg1 && arg2;
14015 arg1 = next_call_expr_arg (&iter1),
14016 arg2 = next_call_expr_arg (&iter2))
14017 if (!c_tree_equal (arg1, arg2))
14018 return false;
14019 if (arg1 || arg2)
14020 return false;
14021 return true;
14022 }
14023
14024 case TARGET_EXPR:
14025 {
14026 tree o1 = TREE_OPERAND (t1, 0);
14027 tree o2 = TREE_OPERAND (t2, 0);
14028
14029 /* Special case: if either target is an unallocated VAR_DECL,
14030 it means that it's going to be unified with whatever the
14031 TARGET_EXPR is really supposed to initialize, so treat it
14032 as being equivalent to anything. */
0ae9bd27 14033 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
acf0174b
JJ
14034 && !DECL_RTL_SET_P (o1))
14035 /*Nop*/;
0ae9bd27 14036 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
acf0174b
JJ
14037 && !DECL_RTL_SET_P (o2))
14038 /*Nop*/;
14039 else if (!c_tree_equal (o1, o2))
14040 return false;
14041
14042 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14043 }
14044
14045 case COMPONENT_REF:
14046 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14047 return false;
14048 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14049
14050 case PARM_DECL:
14051 case VAR_DECL:
14052 case CONST_DECL:
14053 case FIELD_DECL:
14054 case FUNCTION_DECL:
14055 case IDENTIFIER_NODE:
14056 case SSA_NAME:
14057 return false;
14058
14059 case TREE_VEC:
14060 {
14061 unsigned ix;
14062 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14063 return false;
14064 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14065 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14066 TREE_VEC_ELT (t2, ix)))
14067 return false;
14068 return true;
14069 }
14070
14071 default:
14072 break;
14073 }
14074
14075 switch (TREE_CODE_CLASS (code1))
14076 {
14077 case tcc_unary:
14078 case tcc_binary:
14079 case tcc_comparison:
14080 case tcc_expression:
14081 case tcc_vl_exp:
14082 case tcc_reference:
14083 case tcc_statement:
14084 {
14085 int i, n = TREE_OPERAND_LENGTH (t1);
14086
14087 switch (code1)
14088 {
14089 case PREINCREMENT_EXPR:
14090 case PREDECREMENT_EXPR:
14091 case POSTINCREMENT_EXPR:
14092 case POSTDECREMENT_EXPR:
14093 n = 1;
14094 break;
14095 case ARRAY_REF:
14096 n = 2;
14097 break;
14098 default:
14099 break;
14100 }
14101
14102 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14103 && n != TREE_OPERAND_LENGTH (t2))
14104 return false;
14105
14106 for (i = 0; i < n; ++i)
14107 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14108 return false;
14109
14110 return true;
14111 }
14112
14113 case tcc_type:
14114 return comptypes (t1, t2);
14115 default:
14116 gcc_unreachable ();
14117 }
14118 /* We can get here with --disable-checking. */
14119 return false;
14120}
12893402
BI
14121
14122/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14123 spawn-helper and BODY is the newly created body for FNDECL. */
14124
14125void
14126cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14127{
14128 tree list = alloc_stmt_list ();
14129 tree frame = make_cilk_frame (fndecl);
14130 tree dtor = create_cilk_function_exit (frame, false, true);
14131 add_local_decl (cfun, frame);
14132
14133 DECL_SAVED_TREE (fndecl) = list;
14134 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
14135 frame);
14136 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
14137 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
14138
14139 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
14140 append_to_statement_list (detach_expr, &body_list);
14141
14142 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14143 body = fold_build_cleanup_point_expr (void_type_node, body);
14144
14145 append_to_statement_list (body, &body_list);
14146 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14147 body_list, dtor), &list);
14148}
1807ffc1
MS
14149
14150/* Returns true when the function declaration FNDECL is implicit,
14151 introduced as a result of a call to an otherwise undeclared
14152 function, and false otherwise. */
14153
14154bool
14155c_decl_implicit (const_tree fndecl)
14156{
14157 return C_DECL_IMPLICIT (fndecl);
14158}