]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
ia64.h (ASM_OUTPUT_DWARF_OFFSET): Use parameter OFFSET, not offset.
[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 {
3e4093b6
RS
4199 if (!noconvert)
4200 arg = default_conversion (arg);
03d5b1f5 4201 }
3e4093b6 4202 else if (typecode == COMPLEX_TYPE)
400fbf9f 4203 {
3e4093b6 4204 code = CONJ_EXPR;
c1771a20 4205 pedwarn (location, OPT_Wpedantic,
fcf73884 4206 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
4207 if (!noconvert)
4208 arg = default_conversion (arg);
4209 }
4210 else
4211 {
c9f9eb5d 4212 error_at (location, "wrong type argument to bit-complement");
3e4093b6 4213 return error_mark_node;
400fbf9f
JW
4214 }
4215 break;
4216
3e4093b6 4217 case ABS_EXPR:
11017cc7 4218 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 4219 {
c9f9eb5d 4220 error_at (location, "wrong type argument to abs");
3e4093b6 4221 return error_mark_node;
400fbf9f 4222 }
3e4093b6
RS
4223 else if (!noconvert)
4224 arg = default_conversion (arg);
400fbf9f
JW
4225 break;
4226
3e4093b6
RS
4227 case CONJ_EXPR:
4228 /* Conjugating a real value is a no-op, but allow it anyway. */
4229 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4230 || typecode == COMPLEX_TYPE))
400fbf9f 4231 {
c9f9eb5d 4232 error_at (location, "wrong type argument to conjugation");
3e4093b6 4233 return error_mark_node;
400fbf9f 4234 }
3e4093b6
RS
4235 else if (!noconvert)
4236 arg = default_conversion (arg);
400fbf9f
JW
4237 break;
4238
3e4093b6 4239 case TRUTH_NOT_EXPR:
ab22c1fa 4240 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 4241 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 4242 && typecode != COMPLEX_TYPE)
400fbf9f 4243 {
c9f9eb5d
AH
4244 error_at (location,
4245 "wrong type argument to unary exclamation mark");
3e4093b6 4246 return error_mark_node;
400fbf9f 4247 }
a27d595d
JM
4248 if (int_operands)
4249 {
4250 arg = c_objc_common_truthvalue_conversion (location, xarg);
4251 arg = remove_c_maybe_const_expr (arg);
4252 }
4253 else
4254 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 4255 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
4256 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4257 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4258 location = EXPR_LOCATION (ret);
c9f9eb5d 4259 goto return_build_unary_op;
3e4093b6 4260
3e4093b6 4261 case REALPART_EXPR:
3e4093b6 4262 case IMAGPART_EXPR:
fb52b50a
NF
4263 ret = build_real_imag_expr (location, code, arg);
4264 if (ret == error_mark_node)
4265 return error_mark_node;
8ce94e44
JM
4266 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4267 eptype = TREE_TYPE (eptype);
c9f9eb5d 4268 goto return_build_unary_op;
3e4093b6
RS
4269
4270 case PREINCREMENT_EXPR:
4271 case POSTINCREMENT_EXPR:
4272 case PREDECREMENT_EXPR:
4273 case POSTDECREMENT_EXPR:
3e4093b6 4274
928c19bb
JM
4275 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4276 {
4277 tree inner = build_unary_op (location, code,
4c13ba17
MP
4278 C_MAYBE_CONST_EXPR_EXPR (arg),
4279 noconvert);
928c19bb
JM
4280 if (inner == error_mark_node)
4281 return error_mark_node;
4282 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4283 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4284 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4285 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4286 goto return_build_unary_op;
4287 }
4288
925e8657
NP
4289 /* Complain about anything that is not a true lvalue. In
4290 Objective-C, skip this check for property_refs. */
f90e8e2e 4291 if (!objc_is_property_ref (arg)
7bd11157
TT
4292 && !lvalue_or_else (location,
4293 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
4294 || code == POSTINCREMENT_EXPR)
4295 ? lv_increment
4296 : lv_decrement)))
928c19bb
JM
4297 return error_mark_node;
4298
09639a83
ILT
4299 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4300 {
4301 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4302 warning_at (location, OPT_Wc___compat,
4303 "increment of enumeration value is invalid in C++");
4304 else
4305 warning_at (location, OPT_Wc___compat,
4306 "decrement of enumeration value is invalid in C++");
4307 }
4308
928c19bb
JM
4309 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4310 arg = c_fully_fold (arg, false, NULL);
4311
267bac10
JM
4312 bool atomic_op;
4313 atomic_op = really_atomic_lvalue (arg);
4314
3e4093b6
RS
4315 /* Increment or decrement the real part of the value,
4316 and don't change the imaginary part. */
4317 if (typecode == COMPLEX_TYPE)
400fbf9f 4318 {
3e4093b6
RS
4319 tree real, imag;
4320
c1771a20 4321 pedwarn (location, OPT_Wpedantic,
509c9d60 4322 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 4323
267bac10
JM
4324 if (!atomic_op)
4325 {
4326 arg = stabilize_reference (arg);
e51fbec3
MP
4327 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4328 true);
4329 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4330 true);
4331 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
267bac10
JM
4332 if (real == error_mark_node || imag == error_mark_node)
4333 return error_mark_node;
4334 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4335 real, imag);
4336 goto return_build_unary_op;
4337 }
400fbf9f 4338 }
3e4093b6
RS
4339
4340 /* Report invalid types. */
4341
ab22c1fa 4342 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4343 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4344 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4345 {
3e4093b6 4346 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4347 error_at (location, "wrong type argument to increment");
c22cacf3 4348 else
c9f9eb5d 4349 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4350
4351 return error_mark_node;
400fbf9f 4352 }
400fbf9f 4353
3e4093b6
RS
4354 {
4355 tree inc;
400fbf9f 4356
3e4093b6
RS
4357 argtype = TREE_TYPE (arg);
4358
4359 /* Compute the increment. */
4360
4361 if (typecode == POINTER_TYPE)
4362 {
a0e24419 4363 /* If pointer target is an incomplete type,
3e4093b6 4364 we just cannot know how to do the arithmetic. */
b70cef5d 4365 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4366 {
4367 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4368 error_at (location,
a0e24419
MP
4369 "increment of pointer to an incomplete type %qT",
4370 TREE_TYPE (argtype));
3e4093b6 4371 else
c9f9eb5d 4372 error_at (location,
a0e24419
MP
4373 "decrement of pointer to an incomplete type %qT",
4374 TREE_TYPE (argtype));
3e4093b6 4375 }
b70cef5d
JJ
4376 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4377 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4378 {
3e4093b6 4379 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4380 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4381 "wrong type argument to increment");
3e4093b6 4382 else
44d90fe1 4383 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4384 "wrong type argument to decrement");
3e4093b6
RS
4385 }
4386
b70cef5d 4387 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4388 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4389 }
b70cef5d 4390 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4391 {
4392 /* For signed fract types, we invert ++ to -- or
4393 -- to ++, and change inc from 1 to -1, because
4394 it is not possible to represent 1 in signed fract constants.
4395 For unsigned fract types, the result always overflows and
4396 we get an undefined (original) or the maximum value. */
4397 if (code == PREINCREMENT_EXPR)
4398 code = PREDECREMENT_EXPR;
4399 else if (code == PREDECREMENT_EXPR)
4400 code = PREINCREMENT_EXPR;
4401 else if (code == POSTINCREMENT_EXPR)
4402 code = POSTDECREMENT_EXPR;
4403 else /* code == POSTDECREMENT_EXPR */
4404 code = POSTINCREMENT_EXPR;
4405
4406 inc = integer_minus_one_node;
4407 inc = convert (argtype, inc);
4408 }
3e4093b6 4409 else
5be014d5 4410 {
241b71bb
TV
4411 inc = VECTOR_TYPE_P (argtype)
4412 ? build_one_cst (argtype)
4413 : integer_one_node;
5be014d5
AP
4414 inc = convert (argtype, inc);
4415 }
3e4093b6 4416
925e8657
NP
4417 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4418 need to ask Objective-C to build the increment or decrement
4419 expression for it. */
4420 if (objc_is_property_ref (arg))
f90e8e2e 4421 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4422 arg, inc);
4423
3e4093b6 4424 /* Report a read-only lvalue. */
f37acdf9 4425 if (TYPE_READONLY (argtype))
953ff289 4426 {
c02065fc 4427 readonly_error (location, arg,
953ff289
DN
4428 ((code == PREINCREMENT_EXPR
4429 || code == POSTINCREMENT_EXPR)
4430 ? lv_increment : lv_decrement));
4431 return error_mark_node;
4432 }
f37acdf9
JM
4433 else if (TREE_READONLY (arg))
4434 readonly_warning (arg,
4435 ((code == PREINCREMENT_EXPR
4436 || code == POSTINCREMENT_EXPR)
4437 ? lv_increment : lv_decrement));
3e4093b6 4438
267bac10
JM
4439 /* If the argument is atomic, use the special code sequences for
4440 atomic compound assignment. */
4441 if (atomic_op)
4442 {
4443 arg = stabilize_reference (arg);
4444 ret = build_atomic_assign (location, arg,
4445 ((code == PREINCREMENT_EXPR
4446 || code == POSTINCREMENT_EXPR)
4447 ? PLUS_EXPR
4448 : MINUS_EXPR),
4449 (FRACT_MODE_P (TYPE_MODE (argtype))
4450 ? inc
4451 : integer_one_node),
4452 (code == POSTINCREMENT_EXPR
4453 || code == POSTDECREMENT_EXPR));
4454 goto return_build_unary_op;
4455 }
4456
3e4093b6
RS
4457 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4458 val = boolean_increment (code, arg);
4459 else
53fb4de3 4460 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4461 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4462 if (TREE_CODE (val) != code)
6de9cd9a 4463 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4464 ret = val;
4465 goto return_build_unary_op;
3e4093b6
RS
4466 }
4467
4468 case ADDR_EXPR:
4469 /* Note that this operation never does default_conversion. */
4470
2b4b7036
JM
4471 /* The operand of unary '&' must be an lvalue (which excludes
4472 expressions of type void), or, in C99, the result of a [] or
4473 unary '*' operator. */
4474 if (VOID_TYPE_P (TREE_TYPE (arg))
4475 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
22d03525 4476 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
2b4b7036
JM
4477 pedwarn (location, 0, "taking address of expression of type %<void%>");
4478
3e4093b6 4479 /* Let &* cancel out to simplify resulting code. */
22d03525 4480 if (INDIRECT_REF_P (arg))
400fbf9f 4481 {
3e4093b6
RS
4482 /* Don't let this be an lvalue. */
4483 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4484 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4485 ret = TREE_OPERAND (arg, 0);
4486 goto return_build_unary_op;
400fbf9f 4487 }
1eb8759b 4488
3e4093b6
RS
4489 /* Anything not already handled and not a true memory reference
4490 or a non-lvalue array is an error. */
4c13ba17 4491 if (typecode != FUNCTION_TYPE && !noconvert
ee45a32d 4492 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4493 return error_mark_node;
b6a10c9f 4494
928c19bb
JM
4495 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4496 folding later. */
4497 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4498 {
4499 tree inner = build_unary_op (location, code,
4c13ba17
MP
4500 C_MAYBE_CONST_EXPR_EXPR (arg),
4501 noconvert);
928c19bb
JM
4502 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4503 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4504 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4505 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4506 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4507 goto return_build_unary_op;
4508 }
4509
3e4093b6
RS
4510 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4511 argtype = TREE_TYPE (arg);
400fbf9f 4512
3e4093b6 4513 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4514 to which the address will point. This is only needed
f2c1da78 4515 for function types. */
6615c446 4516 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4517 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4518 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4519 {
4520 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4521 int quals = orig_quals;
4522
4523 if (TREE_READONLY (arg))
4524 quals |= TYPE_QUAL_CONST;
4525 if (TREE_THIS_VOLATILE (arg))
4526 quals |= TYPE_QUAL_VOLATILE;
4527
f2c1da78
JM
4528 argtype = c_build_qualified_type (argtype, quals);
4529 }
400fbf9f 4530
ee45a32d
EB
4531 switch (TREE_CODE (arg))
4532 {
4533 case COMPONENT_REF:
4534 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4535 {
949505a9
MP
4536 error_at (location, "cannot take address of bit-field %qD",
4537 TREE_OPERAND (arg, 1));
ee45a32d
EB
4538 return error_mark_node;
4539 }
4540
191816a3 4541 /* fall through */
ee45a32d
EB
4542
4543 case ARRAY_REF:
4544 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4545 {
4546 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4547 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4548 {
949505a9
MP
4549 error_at (location, "cannot take address of scalar with "
4550 "reverse storage order");
ee45a32d
EB
4551 return error_mark_node;
4552 }
4553
4554 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4555 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
949505a9
MP
4556 warning_at (location, OPT_Wscalar_storage_order,
4557 "address of array with reverse scalar storage "
4558 "order requested");
ee45a32d
EB
4559 }
4560
4561 default:
4562 break;
4563 }
4564
3e4093b6
RS
4565 if (!c_mark_addressable (arg))
4566 return error_mark_node;
400fbf9f 4567
abb54d14
JM
4568 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4569 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4570
5cc200fc 4571 argtype = build_pointer_type (argtype);
5e55f99d
RH
4572
4573 /* ??? Cope with user tricks that amount to offsetof. Delete this
4574 when we have proper support for integer constant expressions. */
4575 val = get_base_address (arg);
22d03525 4576 if (val && INDIRECT_REF_P (val)
3aa2ddb8
JJ
4577 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4578 {
cf9e9959 4579 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4580 goto return_build_unary_op;
3aa2ddb8 4581 }
5e55f99d 4582
5cc200fc 4583 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4584
c9f9eb5d
AH
4585 ret = val;
4586 goto return_build_unary_op;
400fbf9f 4587
3e4093b6 4588 default:
1344f9a3 4589 gcc_unreachable ();
3e4093b6 4590 }
400fbf9f 4591
3e4093b6
RS
4592 if (argtype == 0)
4593 argtype = TREE_TYPE (arg);
928c19bb
JM
4594 if (TREE_CODE (arg) == INTEGER_CST)
4595 ret = (require_constant_value
db3927fb
AH
4596 ? fold_build1_initializer_loc (location, code, argtype, arg)
4597 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4598 else
4599 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4600 return_build_unary_op:
4601 gcc_assert (ret != error_mark_node);
928c19bb
JM
4602 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4603 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4604 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4605 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4606 ret = note_integer_operands (ret);
8ce94e44
JM
4607 if (eptype)
4608 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4609 protected_set_expr_location (ret, location);
4610 return ret;
3e4093b6 4611}
400fbf9f 4612
3e4093b6
RS
4613/* Return nonzero if REF is an lvalue valid for this language.
4614 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4615 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4616
1e4ae551 4617bool
58f9752a 4618lvalue_p (const_tree ref)
3e4093b6 4619{
58f9752a 4620 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4621
3e4093b6
RS
4622 switch (code)
4623 {
4624 case REALPART_EXPR:
4625 case IMAGPART_EXPR:
4626 case COMPONENT_REF:
4627 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4628
928c19bb
JM
4629 case C_MAYBE_CONST_EXPR:
4630 return lvalue_p (TREE_OPERAND (ref, 1));
4631
3e4093b6
RS
4632 case COMPOUND_LITERAL_EXPR:
4633 case STRING_CST:
4634 return 1;
400fbf9f 4635
3e4093b6
RS
4636 case INDIRECT_REF:
4637 case ARRAY_REF:
36536d79 4638 case ARRAY_NOTATION_REF:
3e4093b6
RS
4639 case VAR_DECL:
4640 case PARM_DECL:
4641 case RESULT_DECL:
4642 case ERROR_MARK:
4643 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4644 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4645
3e4093b6 4646 case BIND_EXPR:
3e4093b6 4647 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4648
3e4093b6
RS
4649 default:
4650 return 0;
4651 }
4652}
400fbf9f 4653\f
f37acdf9
JM
4654/* Give a warning for storing in something that is read-only in GCC
4655 terms but not const in ISO C terms. */
4656
4657static void
4658readonly_warning (tree arg, enum lvalue_use use)
4659{
4660 switch (use)
4661 {
4662 case lv_assign:
4663 warning (0, "assignment of read-only location %qE", arg);
4664 break;
4665 case lv_increment:
4666 warning (0, "increment of read-only location %qE", arg);
4667 break;
4668 case lv_decrement:
4669 warning (0, "decrement of read-only location %qE", arg);
4670 break;
4671 default:
4672 gcc_unreachable ();
4673 }
4674 return;
4675}
4676
37dc0d8d
JM
4677
4678/* Return nonzero if REF is an lvalue valid for this language;
4679 otherwise, print an error message and return zero. USE says
7bd11157
TT
4680 how the lvalue is being used and so selects the error message.
4681 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4682
4683static int
7bd11157 4684lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4685{
4686 int win = lvalue_p (ref);
4687
4688 if (!win)
7bd11157 4689 lvalue_error (loc, use);
37dc0d8d
JM
4690
4691 return win;
4692}
3e4093b6
RS
4693\f
4694/* Mark EXP saying that we need to be able to take the
4695 address of it; it should not be allocated in a register.
4696 Returns true if successful. */
54c93c30 4697
3e4093b6
RS
4698bool
4699c_mark_addressable (tree exp)
400fbf9f 4700{
3e4093b6 4701 tree x = exp;
95602da1 4702
3e4093b6
RS
4703 while (1)
4704 switch (TREE_CODE (x))
4705 {
4706 case COMPONENT_REF:
3e4093b6
RS
4707 case ADDR_EXPR:
4708 case ARRAY_REF:
4709 case REALPART_EXPR:
4710 case IMAGPART_EXPR:
4711 x = TREE_OPERAND (x, 0);
4712 break;
95602da1 4713
3e4093b6
RS
4714 case COMPOUND_LITERAL_EXPR:
4715 case CONSTRUCTOR:
4716 TREE_ADDRESSABLE (x) = 1;
4717 return true;
95602da1 4718
3e4093b6
RS
4719 case VAR_DECL:
4720 case CONST_DECL:
4721 case PARM_DECL:
4722 case RESULT_DECL:
5baeaac0 4723 if (C_DECL_REGISTER (x)
3e4093b6
RS
4724 && DECL_NONLOCAL (x))
4725 {
62f9079a 4726 if (TREE_PUBLIC (x) || is_global_var (x))
3e4093b6 4727 {
0039fa55
AN
4728 error
4729 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4730 return false;
4731 }
509c9d60 4732 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4733 }
5baeaac0 4734 else if (C_DECL_REGISTER (x))
3e4093b6 4735 {
62f9079a 4736 if (TREE_PUBLIC (x) || is_global_var (x))
0039fa55
AN
4737 error ("address of global register variable %qD requested", x);
4738 else
4739 error ("address of register variable %qD requested", x);
4740 return false;
3e4093b6 4741 }
400fbf9f 4742
191816a3 4743 /* FALLTHRU */
3e4093b6
RS
4744 case FUNCTION_DECL:
4745 TREE_ADDRESSABLE (x) = 1;
191816a3 4746 /* FALLTHRU */
3e4093b6
RS
4747 default:
4748 return true;
4749 }
4750}
4751\f
2d2e923f
MLI
4752/* Convert EXPR to TYPE, warning about conversion problems with
4753 constants. SEMANTIC_TYPE is the type this conversion would use
4754 without excess precision. If SEMANTIC_TYPE is NULL, this function
4755 is equivalent to convert_and_check. This function is a wrapper that
4756 handles conversions that may be different than
4757 the usual ones because of excess precision. */
4758
4759static tree
68fca595
MP
4760ep_convert_and_check (location_t loc, tree type, tree expr,
4761 tree semantic_type)
2d2e923f
MLI
4762{
4763 if (TREE_TYPE (expr) == type)
4764 return expr;
4765
4766 if (!semantic_type)
68fca595 4767 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4768
4769 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4770 && TREE_TYPE (expr) != semantic_type)
4771 {
4772 /* For integers, we need to check the real conversion, not
4773 the conversion to the excess precision type. */
68fca595 4774 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4775 }
4776 /* Result type is the excess precision type, which should be
4777 large enough, so do not check. */
4778 return convert (type, expr);
4779}
4780
928c19bb
JM
4781/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4782 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4783 if folded to an integer constant then the unselected half may
4784 contain arbitrary operations not normally permitted in constant
c2255bc4 4785 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4786
4787tree
744aa42f 4788build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4789 tree op1, tree op1_original_type, tree op2,
4790 tree op2_original_type)
400fbf9f 4791{
3e4093b6
RS
4792 tree type1;
4793 tree type2;
4794 enum tree_code code1;
4795 enum tree_code code2;
4796 tree result_type = NULL;
2d2e923f 4797 tree semantic_result_type = NULL;
3e4093b6 4798 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4799 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4800 bool ifexp_int_operands;
928c19bb 4801 tree ret;
400fbf9f 4802
4d84fe7c
JM
4803 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4804 if (op1_int_operands)
4805 op1 = remove_c_maybe_const_expr (op1);
4806 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4807 if (op2_int_operands)
4808 op2 = remove_c_maybe_const_expr (op2);
4809 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4810 if (ifexp_int_operands)
4811 ifexp = remove_c_maybe_const_expr (ifexp);
4812
3e4093b6
RS
4813 /* Promote both alternatives. */
4814
4815 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4816 op1 = default_conversion (op1);
4817 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4818 op2 = default_conversion (op2);
4819
4820 if (TREE_CODE (ifexp) == ERROR_MARK
4821 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4822 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4823 return error_mark_node;
400fbf9f 4824
3e4093b6
RS
4825 type1 = TREE_TYPE (op1);
4826 code1 = TREE_CODE (type1);
4827 type2 = TREE_TYPE (op2);
4828 code2 = TREE_CODE (type2);
4829
1807ffc1
MS
4830 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4831 return error_mark_node;
4832
4833 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4834 return error_mark_node;
4835
b1adf557
JM
4836 /* C90 does not permit non-lvalue arrays in conditional expressions.
4837 In C99 they will be pointers by now. */
4838 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4839 {
744aa42f 4840 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4841 return error_mark_node;
4842 }
4843
8ce94e44
JM
4844 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4845 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4846 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4847 || code1 == COMPLEX_TYPE)
4848 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4849 || code2 == COMPLEX_TYPE))
4850 {
2d2e923f 4851 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4852 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4853 {
4854 op1 = TREE_OPERAND (op1, 0);
4855 type1 = TREE_TYPE (op1);
4856 gcc_assert (TREE_CODE (type1) == code1);
4857 }
4858 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4859 {
4860 op2 = TREE_OPERAND (op2, 0);
4861 type2 = TREE_TYPE (op2);
4862 gcc_assert (TREE_CODE (type2) == code2);
4863 }
4864 }
4865
d130ae11
ILT
4866 if (warn_cxx_compat)
4867 {
4868 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4869 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4870
4871 if (TREE_CODE (t1) == ENUMERAL_TYPE
4872 && TREE_CODE (t2) == ENUMERAL_TYPE
4873 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4874 warning_at (colon_loc, OPT_Wc___compat,
4875 ("different enum types in conditional is "
4876 "invalid in C++: %qT vs %qT"),
4877 t1, t2);
4878 }
4879
3e4093b6
RS
4880 /* Quickly detect the usual case where op1 and op2 have the same type
4881 after promotion. */
4882 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4883 {
3e4093b6
RS
4884 if (type1 == type2)
4885 result_type = type1;
4886 else
4887 result_type = TYPE_MAIN_VARIANT (type1);
4888 }
4889 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4890 || code1 == COMPLEX_TYPE)
4891 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4892 || code2 == COMPLEX_TYPE))
3e4093b6 4893 {
ccf7f880 4894 result_type = c_common_type (type1, type2);
5a578671
JM
4895 if (result_type == error_mark_node)
4896 return error_mark_node;
0a0b3574
MM
4897 do_warn_double_promotion (result_type, type1, type2,
4898 "implicit conversion from %qT to %qT to "
4899 "match other result of conditional",
4900 colon_loc);
400fbf9f 4901
3e4093b6
RS
4902 /* If -Wsign-compare, warn here if type1 and type2 have
4903 different signedness. We'll promote the signed to unsigned
4904 and later code won't know it used to be different.
4905 Do this check on the original types, so that explicit casts
4906 will be considered, but default promotions won't. */
7d882b83 4907 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4908 {
8df83eae
RK
4909 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4910 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4911
3e4093b6
RS
4912 if (unsigned_op1 ^ unsigned_op2)
4913 {
6ac01510
ILT
4914 bool ovf;
4915
3e4093b6
RS
4916 /* Do not warn if the result type is signed, since the
4917 signed type will only be chosen if it can represent
4918 all the values of the unsigned type. */
3f75a254 4919 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4920 /* OK */;
3e4093b6 4921 else
928c19bb
JM
4922 {
4923 bool op1_maybe_const = true;
4924 bool op2_maybe_const = true;
4925
4926 /* Do not warn if the signed quantity is an
4927 unsuffixed integer literal (or some static
4928 constant expression involving such literals) and
4929 it is non-negative. This warning requires the
4930 operands to be folded for best results, so do
4931 that folding in this case even without
4932 warn_sign_compare to avoid warning options
4933 possibly affecting code generation. */
f5178456
RS
4934 c_inhibit_evaluation_warnings
4935 += (ifexp == truthvalue_false_node);
928c19bb
JM
4936 op1 = c_fully_fold (op1, require_constant_value,
4937 &op1_maybe_const);
f5178456
RS
4938 c_inhibit_evaluation_warnings
4939 -= (ifexp == truthvalue_false_node);
4940
4941 c_inhibit_evaluation_warnings
4942 += (ifexp == truthvalue_true_node);
928c19bb
JM
4943 op2 = c_fully_fold (op2, require_constant_value,
4944 &op2_maybe_const);
f5178456
RS
4945 c_inhibit_evaluation_warnings
4946 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4947
4948 if (warn_sign_compare)
4949 {
4950 if ((unsigned_op2
4951 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4952 || (unsigned_op1
4953 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4954 /* OK */;
4955 else
744aa42f
ILT
4956 warning_at (colon_loc, OPT_Wsign_compare,
4957 ("signed and unsigned type in "
4958 "conditional expression"));
928c19bb
JM
4959 }
4960 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4961 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4962 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4963 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4964 }
3e4093b6
RS
4965 }
4966 }
4967 }
4968 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4969 {
fcf73884 4970 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4971 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4972 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4973 result_type = void_type_node;
4974 }
4975 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4976 {
36c5e70a
BE
4977 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4978 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4979 addr_space_t as_common;
4980
744aa42f 4981 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4982 result_type = common_pointer_type (type1, type2);
6aa3c60d 4983 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4984 result_type = type2;
6aa3c60d 4985 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4986 result_type = type1;
4987 else if (!addr_space_superset (as1, as2, &as_common))
4988 {
4989 error_at (colon_loc, "pointers to disjoint address spaces "
4990 "used in conditional expression");
4991 return error_mark_node;
4992 }
267bac10
JM
4993 else if (VOID_TYPE_P (TREE_TYPE (type1))
4994 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4995 {
768952be
MU
4996 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4997 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4998 & ~TYPE_QUALS (TREE_TYPE (type1))))
4999 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5000 "pointer to array loses qualifier "
5001 "in conditional expression");
5002
fcf73884 5003 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 5004 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 5005 "ISO C forbids conditional expr between "
bda67431 5006 "%<void *%> and function pointer");
3e4093b6
RS
5007 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5008 TREE_TYPE (type2)));
34a80643 5009 }
267bac10
JM
5010 else if (VOID_TYPE_P (TREE_TYPE (type2))
5011 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 5012 {
768952be
MU
5013 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5014 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5015 & ~TYPE_QUALS (TREE_TYPE (type2))))
5016 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5017 "pointer to array loses qualifier "
5018 "in conditional expression");
5019
fcf73884 5020 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 5021 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 5022 "ISO C forbids conditional expr between "
bda67431 5023 "%<void *%> and function pointer");
3e4093b6
RS
5024 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5025 TREE_TYPE (type1)));
1c2a9b35 5026 }
b581b85b
NP
5027 /* Objective-C pointer comparisons are a bit more lenient. */
5028 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5029 result_type = objc_common_type (type1, type2);
34a80643 5030 else
ab87f8c8 5031 {
36c5e70a
BE
5032 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5033
b581b85b
NP
5034 pedwarn (colon_loc, 0,
5035 "pointer type mismatch in conditional expression");
36c5e70a
BE
5036 result_type = build_pointer_type
5037 (build_qualified_type (void_type_node, qual));
ab87f8c8 5038 }
3e4093b6
RS
5039 }
5040 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5041 {
6aa3c60d 5042 if (!null_pointer_constant_p (orig_op2))
744aa42f 5043 pedwarn (colon_loc, 0,
509c9d60 5044 "pointer/integer type mismatch in conditional expression");
3e4093b6 5045 else
ab87f8c8 5046 {
3e4093b6 5047 op2 = null_pointer_node;
ab87f8c8 5048 }
3e4093b6
RS
5049 result_type = type1;
5050 }
5051 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5052 {
6aa3c60d 5053 if (!null_pointer_constant_p (orig_op1))
744aa42f 5054 pedwarn (colon_loc, 0,
509c9d60 5055 "pointer/integer type mismatch in conditional expression");
3e4093b6 5056 else
ab87f8c8 5057 {
3e4093b6 5058 op1 = null_pointer_node;
ab87f8c8 5059 }
3e4093b6
RS
5060 result_type = type2;
5061 }
1c2a9b35 5062
3e4093b6
RS
5063 if (!result_type)
5064 {
5065 if (flag_cond_mismatch)
5066 result_type = void_type_node;
5067 else
400fbf9f 5068 {
c2255bc4 5069 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 5070 return error_mark_node;
400fbf9f 5071 }
3e4093b6 5072 }
400fbf9f 5073
3e4093b6
RS
5074 /* Merge const and volatile flags of the incoming types. */
5075 result_type
5076 = build_type_variant (result_type,
afbd0665
AS
5077 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5078 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 5079
68fca595
MP
5080 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5081 semantic_result_type);
5082 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5083 semantic_result_type);
b6a10c9f 5084
928c19bb
JM
5085 if (ifexp_bcp && ifexp == truthvalue_true_node)
5086 {
5087 op2_int_operands = true;
5088 op1 = c_fully_fold (op1, require_constant_value, NULL);
5089 }
5090 if (ifexp_bcp && ifexp == truthvalue_false_node)
5091 {
5092 op1_int_operands = true;
5093 op2 = c_fully_fold (op2, require_constant_value, NULL);
5094 }
4d84fe7c 5095 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
5096 && op1_int_operands
5097 && op2_int_operands);
5098 if (int_operands)
5099 {
5100 int_const = ((ifexp == truthvalue_true_node
5101 && TREE_CODE (orig_op1) == INTEGER_CST
5102 && !TREE_OVERFLOW (orig_op1))
5103 || (ifexp == truthvalue_false_node
5104 && TREE_CODE (orig_op2) == INTEGER_CST
5105 && !TREE_OVERFLOW (orig_op2)));
5106 }
9f47c7e5
IE
5107
5108 /* Need to convert condition operand into a vector mask. */
5109 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5110 {
5111 tree vectype = TREE_TYPE (ifexp);
5112 tree elem_type = TREE_TYPE (vectype);
5113 tree zero = build_int_cst (elem_type, 0);
5114 tree zero_vec = build_vector_from_val (vectype, zero);
5115 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5116 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5117 }
5118
928c19bb 5119 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 5120 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
5121 else
5122 {
01c7ccbb
JM
5123 if (int_operands)
5124 {
f34f1c87
MP
5125 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5126 nested inside of the expression. */
5127 op1 = c_fully_fold (op1, false, NULL);
5128 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 5129 }
928c19bb
JM
5130 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5131 if (int_operands)
5132 ret = note_integer_operands (ret);
5133 }
2d2e923f
MLI
5134 if (semantic_result_type)
5135 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 5136
c2255bc4 5137 protected_set_expr_location (ret, colon_loc);
928c19bb 5138 return ret;
3e4093b6
RS
5139}
5140\f
487a92fe 5141/* Return a compound expression that performs two expressions and
c2255bc4
AH
5142 returns the value of the second of them.
5143
5144 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 5145
3e4093b6 5146tree
c2255bc4 5147build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 5148{
4d84fe7c 5149 bool expr1_int_operands, expr2_int_operands;
8ce94e44 5150 tree eptype = NULL_TREE;
928c19bb
JM
5151 tree ret;
5152
b72271b9 5153 if (flag_cilkplus
939b37da
BI
5154 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5155 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5156 {
5157 error_at (loc,
5158 "spawned function call cannot be part of a comma expression");
5159 return error_mark_node;
5160 }
4d84fe7c
JM
5161 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5162 if (expr1_int_operands)
5163 expr1 = remove_c_maybe_const_expr (expr1);
5164 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5165 if (expr2_int_operands)
5166 expr2 = remove_c_maybe_const_expr (expr2);
5167
8ce94e44
JM
5168 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5169 expr1 = TREE_OPERAND (expr1, 0);
5170 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5171 {
5172 eptype = TREE_TYPE (expr2);
5173 expr2 = TREE_OPERAND (expr2, 0);
5174 }
5175
3f75a254 5176 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
5177 {
5178 /* The left-hand operand of a comma expression is like an expression
c5409249 5179 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 5180 any side-effects, unless it was explicitly cast to (void). */
e14a6540 5181 if (warn_unused_value)
47aecf47 5182 {
e14a6540 5183 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 5184 && CONVERT_EXPR_P (expr1))
47aecf47 5185 ; /* (void) a, b */
e14a6540
JM
5186 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5187 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 5188 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
5189 ; /* (void) a, (void) b, c */
5190 else
b8698a0f 5191 warning_at (loc, OPT_Wunused_value,
c2255bc4 5192 "left-hand operand of comma expression has no effect");
47aecf47 5193 }
3e4093b6 5194 }
789eadcd
MP
5195 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5196 && warn_unused_value)
5197 {
5198 tree r = expr1;
5199 location_t cloc = loc;
5200 while (TREE_CODE (r) == COMPOUND_EXPR)
5201 {
5202 if (EXPR_HAS_LOCATION (r))
5203 cloc = EXPR_LOCATION (r);
5204 r = TREE_OPERAND (r, 1);
5205 }
5206 if (!TREE_SIDE_EFFECTS (r)
5207 && !VOID_TYPE_P (TREE_TYPE (r))
5208 && !CONVERT_EXPR_P (r))
5209 warning_at (cloc, OPT_Wunused_value,
5210 "right-hand operand of comma expression has no effect");
5211 }
400fbf9f 5212
3e4093b6
RS
5213 /* With -Wunused, we should also warn if the left-hand operand does have
5214 side-effects, but computes a value which is not used. For example, in
5215 `foo() + bar(), baz()' the result of the `+' operator is not used,
5216 so we should issue a warning. */
5217 else if (warn_unused_value)
c2255bc4 5218 warn_if_unused_value (expr1, loc);
400fbf9f 5219
e63d6886
AP
5220 if (expr2 == error_mark_node)
5221 return error_mark_node;
5222
928c19bb
JM
5223 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5224
5225 if (flag_isoc99
4d84fe7c
JM
5226 && expr1_int_operands
5227 && expr2_int_operands)
928c19bb
JM
5228 ret = note_integer_operands (ret);
5229
8ce94e44
JM
5230 if (eptype)
5231 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5232
c2255bc4 5233 protected_set_expr_location (ret, loc);
928c19bb 5234 return ret;
3e4093b6 5235}
400fbf9f 5236
67165eb3
ILT
5237/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5238 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
5239 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5240 of the cast. -Wcast-qual appeared on the command line. Named
5241 address space qualifiers are not handled here, because they result
5242 in different warnings. */
67165eb3
ILT
5243
5244static void
2ee3cb35 5245handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
5246{
5247 tree in_type = type;
5248 tree in_otype = otype;
5249 int added = 0;
5250 int discarded = 0;
5251 bool is_const;
5252
5253 /* Check that the qualifiers on IN_TYPE are a superset of the
5254 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5255 nodes is uninteresting and we stop as soon as we hit a
5256 non-POINTER_TYPE node on either type. */
5257 do
5258 {
5259 in_otype = TREE_TYPE (in_otype);
5260 in_type = TREE_TYPE (in_type);
5261
5262 /* GNU C allows cv-qualified function types. 'const' means the
5263 function is very pure, 'volatile' means it can't return. We
5264 need to warn when such qualifiers are added, not when they're
5265 taken away. */
5266 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5267 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
5268 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5269 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 5270 else
36c5e70a
BE
5271 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5272 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
5273 }
5274 while (TREE_CODE (in_type) == POINTER_TYPE
5275 && TREE_CODE (in_otype) == POINTER_TYPE);
5276
5277 if (added)
2ee3cb35
MLI
5278 warning_at (loc, OPT_Wcast_qual,
5279 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
5280
5281 if (discarded)
5282 /* There are qualifiers present in IN_OTYPE that are not present
5283 in IN_TYPE. */
2ee3cb35 5284 warning_at (loc, OPT_Wcast_qual,
7485aeea 5285 "cast discards %qv qualifier from pointer target type",
2ee3cb35 5286 discarded);
67165eb3
ILT
5287
5288 if (added || discarded)
5289 return;
5290
5291 /* A cast from **T to const **T is unsafe, because it can cause a
5292 const value to be changed with no additional warning. We only
5293 issue this warning if T is the same on both sides, and we only
5294 issue the warning if there are the same number of pointers on
5295 both sides, as otherwise the cast is clearly unsafe anyhow. A
5296 cast is unsafe when a qualifier is added at one level and const
5297 is not present at all outer levels.
5298
5299 To issue this warning, we check at each level whether the cast
5300 adds new qualifiers not already seen. We don't need to special
5301 case function types, as they won't have the same
5302 TYPE_MAIN_VARIANT. */
5303
5304 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5305 return;
5306 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5307 return;
5308
5309 in_type = type;
5310 in_otype = otype;
5311 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5312 do
5313 {
5314 in_type = TREE_TYPE (in_type);
5315 in_otype = TREE_TYPE (in_otype);
5316 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5317 && !is_const)
5318 {
2ee3cb35
MLI
5319 warning_at (loc, OPT_Wcast_qual,
5320 "to be safe all intermediate pointers in cast from "
5321 "%qT to %qT must be %<const%> qualified",
5322 otype, type);
67165eb3
ILT
5323 break;
5324 }
5325 if (is_const)
5326 is_const = TYPE_READONLY (in_type);
5327 }
5328 while (TREE_CODE (in_type) == POINTER_TYPE);
5329}
5330
b8698a0f 5331/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 5332 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 5333
3e4093b6 5334tree
c2255bc4 5335build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 5336{
8ce94e44
JM
5337 tree value;
5338
5339 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5340 expr = TREE_OPERAND (expr, 0);
5341
5342 value = expr;
400fbf9f 5343
3e4093b6
RS
5344 if (type == error_mark_node || expr == error_mark_node)
5345 return error_mark_node;
400fbf9f 5346
3e4093b6
RS
5347 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5348 only in <protocol> qualifications. But when constructing cast expressions,
5349 the protocols do matter and must be kept around. */
700686fa
ZL
5350 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5351 return build1 (NOP_EXPR, type, expr);
5352
5353 type = TYPE_MAIN_VARIANT (type);
400fbf9f 5354
3e4093b6
RS
5355 if (TREE_CODE (type) == ARRAY_TYPE)
5356 {
c2255bc4 5357 error_at (loc, "cast specifies array type");
3e4093b6
RS
5358 return error_mark_node;
5359 }
400fbf9f 5360
3e4093b6
RS
5361 if (TREE_CODE (type) == FUNCTION_TYPE)
5362 {
c2255bc4 5363 error_at (loc, "cast specifies function type");
3e4093b6
RS
5364 return error_mark_node;
5365 }
400fbf9f 5366
808d6eaa
JM
5367 if (!VOID_TYPE_P (type))
5368 {
4f2e1536 5369 value = require_complete_type (loc, value);
808d6eaa
JM
5370 if (value == error_mark_node)
5371 return error_mark_node;
5372 }
5373
3e4093b6
RS
5374 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5375 {
3e636daf 5376 if (RECORD_OR_UNION_TYPE_P (type))
c1771a20 5377 pedwarn (loc, OPT_Wpedantic,
fcf73884 5378 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5379
5380 /* Convert to remove any qualifiers from VALUE's type. */
5381 value = convert (type, value);
3e4093b6
RS
5382 }
5383 else if (TREE_CODE (type) == UNION_TYPE)
5384 {
5385 tree field;
400fbf9f 5386
910ad8de 5387 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5388 if (TREE_TYPE (field) != error_mark_node
5389 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5390 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5391 break;
5392
5393 if (field)
400fbf9f 5394 {
3e4093b6 5395 tree t;
e616f54d 5396 bool maybe_const = true;
3e4093b6 5397
c1771a20 5398 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5399 t = c_fully_fold (value, false, &maybe_const);
5400 t = build_constructor_single (type, field, t);
5401 if (!maybe_const)
5402 t = c_wrap_maybe_const (t, true);
5403 t = digest_init (loc, type, t,
bbbbb16a 5404 NULL_TREE, false, true, 0);
3e4093b6
RS
5405 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5406 return t;
400fbf9f 5407 }
c2255bc4 5408 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5409 return error_mark_node;
5410 }
5411 else
5412 {
5413 tree otype, ovalue;
400fbf9f 5414
3e4093b6 5415 if (type == void_type_node)
c2255bc4
AH
5416 {
5417 tree t = build1 (CONVERT_EXPR, type, value);
5418 SET_EXPR_LOCATION (t, loc);
5419 return t;
5420 }
400fbf9f 5421
3e4093b6 5422 otype = TREE_TYPE (value);
400fbf9f 5423
3e4093b6 5424 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5425 if (warn_cast_qual
5426 && TREE_CODE (type) == POINTER_TYPE
5427 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5428 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5429
36c5e70a
BE
5430 /* Warn about conversions between pointers to disjoint
5431 address spaces. */
5432 if (TREE_CODE (type) == POINTER_TYPE
5433 && TREE_CODE (otype) == POINTER_TYPE
5434 && !null_pointer_constant_p (value))
5435 {
5436 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5437 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5438 addr_space_t as_common;
5439
5440 if (!addr_space_superset (as_to, as_from, &as_common))
5441 {
5442 if (ADDR_SPACE_GENERIC_P (as_from))
5443 warning_at (loc, 0, "cast to %s address space pointer "
5444 "from disjoint generic address space pointer",
5445 c_addr_space_name (as_to));
5446
5447 else if (ADDR_SPACE_GENERIC_P (as_to))
5448 warning_at (loc, 0, "cast to generic address space pointer "
5449 "from disjoint %s address space pointer",
5450 c_addr_space_name (as_from));
5451
5452 else
5453 warning_at (loc, 0, "cast to %s address space pointer "
5454 "from disjoint %s address space pointer",
5455 c_addr_space_name (as_to),
5456 c_addr_space_name (as_from));
5457 }
5458 }
5459
3e4093b6 5460 /* Warn about possible alignment problems. */
3176a0c2 5461 if (STRICT_ALIGNMENT
3e4093b6
RS
5462 && TREE_CODE (type) == POINTER_TYPE
5463 && TREE_CODE (otype) == POINTER_TYPE
5464 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5465 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5466 /* Don't warn about opaque types, where the actual alignment
5467 restriction is unknown. */
3e636daf 5468 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
3e4093b6
RS
5469 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5470 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5471 warning_at (loc, OPT_Wcast_align,
5472 "cast increases required alignment of target type");
e9a25f70 5473
3176a0c2 5474 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5475 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5476 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5477 /* Unlike conversion of integers to pointers, where the
5478 warning is disabled for converting constants because
5479 of cases such as SIG_*, warn about converting constant
5480 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5481 sign extension, and a warning is appropriate. */
c2255bc4
AH
5482 warning_at (loc, OPT_Wpointer_to_int_cast,
5483 "cast from pointer to integer of different size");
400fbf9f 5484
3176a0c2 5485 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5486 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5487 warning_at (loc, OPT_Wbad_function_cast,
5488 "cast from function call of type %qT "
5489 "to non-matching type %qT", otype, type);
400fbf9f 5490
3176a0c2 5491 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5492 && TREE_CODE (otype) == INTEGER_TYPE
5493 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5494 /* Don't warn about converting any constant. */
5495 && !TREE_CONSTANT (value))
c2255bc4
AH
5496 warning_at (loc,
5497 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5498 "of different size");
400fbf9f 5499
79bedddc
SR
5500 if (warn_strict_aliasing <= 2)
5501 strict_aliasing_warning (otype, type, expr);
400fbf9f 5502
3897f229
JM
5503 /* If pedantic, warn for conversions between function and object
5504 pointer types, except for converting a null pointer constant
5505 to function pointer type. */
5506 if (pedantic
5507 && TREE_CODE (type) == POINTER_TYPE
5508 && TREE_CODE (otype) == POINTER_TYPE
5509 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5510 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5511 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5512 "conversion of function pointer to object pointer type");
3897f229
JM
5513
5514 if (pedantic
5515 && TREE_CODE (type) == POINTER_TYPE
5516 && TREE_CODE (otype) == POINTER_TYPE
5517 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5518 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5519 && !null_pointer_constant_p (value))
c1771a20 5520 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5521 "conversion of object pointer to function pointer type");
3897f229 5522
3e4093b6 5523 ovalue = value;
3e4093b6 5524 value = convert (type, value);
400fbf9f 5525
3e4093b6 5526 /* Ignore any integer overflow caused by the cast. */
928c19bb 5527 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5528 {
8bcd6380 5529 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5530 {
8bcd6380
RS
5531 if (!TREE_OVERFLOW (value))
5532 {
5533 /* Avoid clobbering a shared constant. */
5534 value = copy_node (value);
5535 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5536 }
6414bad6 5537 }
8bcd6380 5538 else if (TREE_OVERFLOW (value))
d8e1f97b 5539 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5540 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5541 }
5542 }
400fbf9f 5543
53cd18ec 5544 /* Don't let a cast be an lvalue. */
9482b620 5545 if (lvalue_p (value))
db3927fb 5546 value = non_lvalue_loc (loc, value);
e9a25f70 5547
928c19bb
JM
5548 /* Don't allow the results of casting to floating-point or complex
5549 types be confused with actual constants, or casts involving
5550 integer and pointer types other than direct integer-to-integer
5551 and integer-to-pointer be confused with integer constant
5552 expressions and null pointer constants. */
5553 if (TREE_CODE (value) == REAL_CST
5554 || TREE_CODE (value) == COMPLEX_CST
5555 || (TREE_CODE (value) == INTEGER_CST
5556 && !((TREE_CODE (expr) == INTEGER_CST
5557 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5558 || TREE_CODE (expr) == REAL_CST
5559 || TREE_CODE (expr) == COMPLEX_CST)))
5560 value = build1 (NOP_EXPR, type, value);
5561
21ba0cea 5562 protected_set_expr_location (value, loc);
3e4093b6 5563 return value;
400fbf9f
JW
5564}
5565
c2255bc4
AH
5566/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5567 location of the open paren of the cast, or the position of the cast
5568 expr. */
3e4093b6 5569tree
c2255bc4 5570c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5571{
f8893e47 5572 tree type;
928c19bb
JM
5573 tree type_expr = NULL_TREE;
5574 bool type_expr_const = true;
5575 tree ret;
3e4093b6 5576 int saved_wsp = warn_strict_prototypes;
c5c76735 5577
3e4093b6
RS
5578 /* This avoids warnings about unprototyped casts on
5579 integers. E.g. "#define SIG_DFL (void(*)())0". */
5580 if (TREE_CODE (expr) == INTEGER_CST)
5581 warn_strict_prototypes = 0;
928c19bb 5582 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5583 warn_strict_prototypes = saved_wsp;
c5c76735 5584
1807ffc1
MS
5585 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5586 && reject_gcc_builtin (expr))
5587 return error_mark_node;
5588
c2255bc4 5589 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5590 if (type_expr)
5591 {
9f33203d
JM
5592 bool inner_expr_const = true;
5593 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5594 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5595 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5596 && inner_expr_const);
c2255bc4 5597 SET_EXPR_LOCATION (ret, loc);
928c19bb 5598 }
24b97832 5599
21ba0cea
MP
5600 if (!EXPR_HAS_LOCATION (ret))
5601 protected_set_expr_location (ret, loc);
24b97832 5602
9e5b2115
PB
5603 /* C++ does not permits types to be defined in a cast, but it
5604 allows references to incomplete types. */
5605 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5606 warning_at (loc, OPT_Wc___compat,
5607 "defining a type in a cast is invalid in C++");
5608
928c19bb 5609 return ret;
400fbf9f 5610}
3e4093b6
RS
5611\f
5612/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5613 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5614 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5615 MODIFYCODE is the code for a binary operator that we use
5616 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5617 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5618 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5619 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5620
c2255bc4
AH
5621 LOCATION is the location of the MODIFYCODE operator.
5622 RHS_LOC is the location of the RHS. */
2f6e4e97 5623
3e4093b6 5624tree
32e8bb8e 5625build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5626 enum tree_code modifycode,
c2255bc4 5627 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5628{
3e4093b6
RS
5629 tree result;
5630 tree newrhs;
241f845a 5631 tree rhseval = NULL_TREE;
8ce94e44 5632 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5633 tree lhstype = TREE_TYPE (lhs);
5634 tree olhstype = lhstype;
928c19bb 5635 bool npc;
267bac10 5636 bool is_atomic_op;
e9a25f70 5637
3e4093b6 5638 /* Types that aren't fully specified cannot be used in assignments. */
4f2e1536 5639 lhs = require_complete_type (location, lhs);
e9a25f70 5640
3e4093b6
RS
5641 /* Avoid duplicate error messages from operands that had errors. */
5642 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5643 return error_mark_node;
400fbf9f 5644
4c2ecab0
JM
5645 /* Ensure an error for assigning a non-lvalue array to an array in
5646 C90. */
5647 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5648 {
5649 error_at (location, "assignment to expression with array type");
5650 return error_mark_node;
5651 }
5652
46a88c12 5653 /* For ObjC properties, defer this check. */
7bd11157 5654 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5655 return error_mark_node;
5656
267bac10
JM
5657 is_atomic_op = really_atomic_lvalue (lhs);
5658
8ce94e44
JM
5659 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5660 {
5661 rhs_semantic_type = TREE_TYPE (rhs);
5662 rhs = TREE_OPERAND (rhs, 0);
5663 }
5664
3e4093b6 5665 newrhs = rhs;
400fbf9f 5666
928c19bb
JM
5667 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5668 {
5669 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5670 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5671 rhs_origtype);
928c19bb
JM
5672 if (inner == error_mark_node)
5673 return error_mark_node;
5674 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5675 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5676 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5677 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5678 protected_set_expr_location (result, location);
5679 return result;
5680 }
5681
3e4093b6
RS
5682 /* If a binary op has been requested, combine the old LHS value with the RHS
5683 producing the value we should actually store into the LHS. */
5684
5685 if (modifycode != NOP_EXPR)
400fbf9f 5686 {
928c19bb 5687 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5688 lhs = stabilize_reference (lhs);
bbbbb16a 5689
267bac10
JM
5690 /* Construct the RHS for any non-atomic compound assignemnt. */
5691 if (!is_atomic_op)
5692 {
241f845a
JJ
5693 /* If in LHS op= RHS the RHS has side-effects, ensure they
5694 are preevaluated before the rest of the assignment expression's
5695 side-effects, because RHS could contain e.g. function calls
5696 that modify LHS. */
5697 if (TREE_SIDE_EFFECTS (rhs))
5698 {
5699 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5700 rhseval = newrhs;
5701 }
267bac10 5702 newrhs = build_binary_op (location,
241f845a 5703 modifycode, lhs, newrhs, 1);
267bac10
JM
5704
5705 /* The original type of the right hand side is no longer
5706 meaningful. */
5707 rhs_origtype = NULL_TREE;
5708 }
400fbf9f 5709 }
400fbf9f 5710
668ea4b1
IS
5711 if (c_dialect_objc ())
5712 {
46a88c12
NP
5713 /* Check if we are modifying an Objective-C property reference;
5714 if so, we need to generate setter calls. */
5715 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5716 if (result)
241f845a 5717 goto return_result;
46a88c12
NP
5718
5719 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5720 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5721 return error_mark_node;
5722 }
5723
9bf24266 5724 /* Give an error for storing in something that is 'const'. */
bbbd6700 5725
f37acdf9 5726 if (TYPE_READONLY (lhstype)
3e636daf 5727 || (RECORD_OR_UNION_TYPE_P (lhstype)
3e4093b6 5728 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5729 {
c02065fc 5730 readonly_error (location, lhs, lv_assign);
953ff289
DN
5731 return error_mark_node;
5732 }
f37acdf9
JM
5733 else if (TREE_READONLY (lhs))
5734 readonly_warning (lhs, lv_assign);
bbbd6700 5735
3e4093b6
RS
5736 /* If storing into a structure or union member,
5737 it has probably been given type `int'.
5738 Compute the type that would go with
5739 the actual amount of storage the member occupies. */
bbbd6700 5740
3e4093b6
RS
5741 if (TREE_CODE (lhs) == COMPONENT_REF
5742 && (TREE_CODE (lhstype) == INTEGER_TYPE
5743 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5744 || TREE_CODE (lhstype) == REAL_TYPE
5745 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5746 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5747
3e4093b6
RS
5748 /* If storing in a field that is in actuality a short or narrower than one,
5749 we must store in the field in its actual type. */
5750
5751 if (lhstype != TREE_TYPE (lhs))
5752 {
5753 lhs = copy_node (lhs);
5754 TREE_TYPE (lhs) = lhstype;
400fbf9f 5755 }
400fbf9f 5756
32e8bb8e
ILT
5757 /* Issue -Wc++-compat warnings about an assignment to an enum type
5758 when LHS does not have its original type. This happens for,
5759 e.g., an enum bitfield in a struct. */
5760 if (warn_cxx_compat
5761 && lhs_origtype != NULL_TREE
5762 && lhs_origtype != lhstype
5763 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5764 {
5765 tree checktype = (rhs_origtype != NULL_TREE
5766 ? rhs_origtype
5767 : TREE_TYPE (rhs));
5768 if (checktype != error_mark_node
267bac10
JM
5769 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5770 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5771 warning_at (location, OPT_Wc___compat,
5772 "enum conversion in assignment is invalid in C++");
5773 }
5774
267bac10
JM
5775 /* If the lhs is atomic, remove that qualifier. */
5776 if (is_atomic_op)
5777 {
5778 lhstype = build_qualified_type (lhstype,
5779 (TYPE_QUALS (lhstype)
5780 & ~TYPE_QUAL_ATOMIC));
5781 olhstype = build_qualified_type (olhstype,
5782 (TYPE_QUALS (lhstype)
5783 & ~TYPE_QUAL_ATOMIC));
5784 }
5785
8ce94e44
JM
5786 /* Convert new value to destination type. Fold it first, then
5787 restore any excess precision information, for the sake of
5788 conversion warnings. */
400fbf9f 5789
267bac10
JM
5790 if (!(is_atomic_op && modifycode != NOP_EXPR))
5791 {
5792 npc = null_pointer_constant_p (newrhs);
5793 newrhs = c_fully_fold (newrhs, false, NULL);
5794 if (rhs_semantic_type)
5795 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5796 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5797 rhs_origtype, ic_assign, npc,
5798 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5799 if (TREE_CODE (newrhs) == ERROR_MARK)
5800 return error_mark_node;
5801 }
400fbf9f 5802
6e955430
ZL
5803 /* Emit ObjC write barrier, if necessary. */
5804 if (c_dialect_objc () && flag_objc_gc)
5805 {
5806 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5807 if (result)
c9f9eb5d
AH
5808 {
5809 protected_set_expr_location (result, location);
241f845a 5810 goto return_result;
c9f9eb5d 5811 }
6e955430
ZL
5812 }
5813
ea4b7848 5814 /* Scan operands. */
400fbf9f 5815
267bac10
JM
5816 if (is_atomic_op)
5817 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5818 else
5819 {
5820 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5821 TREE_SIDE_EFFECTS (result) = 1;
5822 protected_set_expr_location (result, location);
5823 }
400fbf9f 5824
3e4093b6
RS
5825 /* If we got the LHS in a different type for storing in,
5826 convert the result back to the nominal type of LHS
5827 so that the value we return always has the same type
5828 as the LHS argument. */
e855c5ce 5829
3e4093b6 5830 if (olhstype == TREE_TYPE (result))
241f845a 5831 goto return_result;
c9f9eb5d 5832
68fca595
MP
5833 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5834 rhs_origtype, ic_assign, false, NULL_TREE,
5835 NULL_TREE, 0);
c9f9eb5d 5836 protected_set_expr_location (result, location);
241f845a
JJ
5837
5838return_result:
5839 if (rhseval)
5840 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5841 return result;
3e4093b6
RS
5842}
5843\f
478a1c5b
ILT
5844/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5845 This is used to implement -fplan9-extensions. */
5846
5847static bool
5848find_anonymous_field_with_type (tree struct_type, tree type)
5849{
5850 tree field;
5851 bool found;
5852
3e636daf 5853 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
478a1c5b
ILT
5854 found = false;
5855 for (field = TYPE_FIELDS (struct_type);
5856 field != NULL_TREE;
5857 field = TREE_CHAIN (field))
5858 {
267bac10
JM
5859 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5860 ? c_build_qualified_type (TREE_TYPE (field),
5861 TYPE_QUAL_ATOMIC)
5862 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5863 if (DECL_NAME (field) == NULL
267bac10 5864 && comptypes (type, fieldtype))
478a1c5b
ILT
5865 {
5866 if (found)
5867 return false;
5868 found = true;
5869 }
5870 else if (DECL_NAME (field) == NULL
3e636daf 5871 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
478a1c5b
ILT
5872 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5873 {
5874 if (found)
5875 return false;
5876 found = true;
5877 }
5878 }
5879 return found;
5880}
5881
5882/* RHS is an expression whose type is pointer to struct. If there is
5883 an anonymous field in RHS with type TYPE, then return a pointer to
5884 that field in RHS. This is used with -fplan9-extensions. This
5885 returns NULL if no conversion could be found. */
5886
5887static tree
5888convert_to_anonymous_field (location_t location, tree type, tree rhs)
5889{
5890 tree rhs_struct_type, lhs_main_type;
5891 tree field, found_field;
5892 bool found_sub_field;
5893 tree ret;
5894
5895 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5896 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
3e636daf 5897 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
478a1c5b
ILT
5898
5899 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5900 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5901 ? c_build_qualified_type (TREE_TYPE (type),
5902 TYPE_QUAL_ATOMIC)
5903 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5904
5905 found_field = NULL_TREE;
5906 found_sub_field = false;
5907 for (field = TYPE_FIELDS (rhs_struct_type);
5908 field != NULL_TREE;
5909 field = TREE_CHAIN (field))
5910 {
5911 if (DECL_NAME (field) != NULL_TREE
3e636daf 5912 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
478a1c5b 5913 continue;
267bac10
JM
5914 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5915 ? c_build_qualified_type (TREE_TYPE (field),
5916 TYPE_QUAL_ATOMIC)
5917 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5918 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5919 {
5920 if (found_field != NULL_TREE)
5921 return NULL_TREE;
5922 found_field = field;
5923 }
5924 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5925 lhs_main_type))
5926 {
5927 if (found_field != NULL_TREE)
5928 return NULL_TREE;
5929 found_field = field;
5930 found_sub_field = true;
5931 }
5932 }
5933
5934 if (found_field == NULL_TREE)
5935 return NULL_TREE;
5936
5937 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5938 build_fold_indirect_ref (rhs), found_field,
5939 NULL_TREE);
5940 ret = build_fold_addr_expr_loc (location, ret);
5941
5942 if (found_sub_field)
5943 {
5944 ret = convert_to_anonymous_field (location, type, ret);
5945 gcc_assert (ret != NULL_TREE);
5946 }
5947
5948 return ret;
5949}
5950
63bc4e87
MP
5951/* Issue an error message for a bad initializer component.
5952 GMSGID identifies the message.
5953 The component name is taken from the spelling stack. */
5954
5955static void
ea58ef42 5956error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5957{
5958 char *ofwhat;
5959
5960 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5961 error_at (loc, gmsgid);
63bc4e87
MP
5962 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5963 if (*ofwhat)
d7ff7ae5 5964 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5965}
5966
5967/* Issue a pedantic warning for a bad initializer component. OPT is
5968 the option OPT_* (from options.h) controlling this warning or 0 if
5969 it is unconditionally given. GMSGID identifies the message. The
5970 component name is taken from the spelling stack. */
5971
5972static void
79063edd 5973pedwarn_init (location_t loc, int opt, const char *gmsgid)
63bc4e87
MP
5974{
5975 char *ofwhat;
d7ff7ae5 5976 bool warned;
63bc4e87 5977
79063edd
MS
5978 /* Use the location where a macro was expanded rather than where
5979 it was defined to make sure macros defined in system headers
5980 but used incorrectly elsewhere are diagnosed. */
5981 source_location exploc = expansion_point_location_if_in_system_header (loc);
5982
63bc4e87 5983 /* The gmsgid may be a format string with %< and %>. */
79063edd 5984 warned = pedwarn (exploc, opt, gmsgid);
63bc4e87 5985 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5 5986 if (*ofwhat && warned)
79063edd 5987 inform (exploc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5988}
5989
5990/* Issue a warning for a bad initializer component.
5991
5992 OPT is the OPT_W* value corresponding to the warning option that
5993 controls this warning. GMSGID identifies the message. The
5994 component name is taken from the spelling stack. */
5995
5996static void
5997warning_init (location_t loc, int opt, const char *gmsgid)
5998{
5999 char *ofwhat;
d7ff7ae5 6000 bool warned;
63bc4e87 6001
79063edd
MS
6002 /* Use the location where a macro was expanded rather than where
6003 it was defined to make sure macros defined in system headers
6004 but used incorrectly elsewhere are diagnosed. */
6005 source_location exploc = expansion_point_location_if_in_system_header (loc);
6006
63bc4e87 6007 /* The gmsgid may be a format string with %< and %>. */
79063edd 6008 warned = warning_at (exploc, opt, gmsgid);
63bc4e87 6009 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5 6010 if (*ofwhat && warned)
79063edd 6011 inform (exploc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
6012}
6013\f
6014/* If TYPE is an array type and EXPR is a parenthesized string
6015 constant, warn if pedantic that EXPR is being used to initialize an
6016 object of type TYPE. */
6017
6018void
d033409e 6019maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
6020{
6021 if (pedantic
6022 && TREE_CODE (type) == ARRAY_TYPE
6023 && TREE_CODE (expr.value) == STRING_CST
6024 && expr.original_code != STRING_CST)
d033409e 6025 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
6026 "array initialized from parenthesized string constant");
6027}
6028
bbbbb16a
ILT
6029/* Convert value RHS to type TYPE as preparation for an assignment to
6030 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6031 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6032 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6033 constant before any folding.
3e4093b6
RS
6034 The real work of conversion is done by `convert'.
6035 The purpose of this function is to generate error messages
6036 for assignments that are not allowed in C.
2ac2f164
JM
6037 ERRTYPE says whether it is argument passing, assignment,
6038 initialization or return.
2f6e4e97 6039
aabef2de
MP
6040 In the following example, '~' denotes where EXPR_LOC and '^' where
6041 LOCATION point to:
6042
6043 f (var); [ic_argpass]
6044 ^ ~~~
6045 x = var; [ic_assign]
6046 ^ ~~~;
6047 int x = var; [ic_init]
6048 ^^^
6049 return x; [ic_return]
6050 ^
6051
2ac2f164 6052 FUNCTION is a tree for the function being called.
3e4093b6 6053 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 6054
3e4093b6 6055static tree
81e5eca8 6056convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 6057 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
6058 bool null_pointer_constant, tree fundecl,
6059 tree function, int parmnum)
3e4093b6
RS
6060{
6061 enum tree_code codel = TREE_CODE (type);
8ce94e44 6062 tree orig_rhs = rhs;
3e4093b6
RS
6063 tree rhstype;
6064 enum tree_code coder;
2ac2f164 6065 tree rname = NULL_TREE;
58393038 6066 bool objc_ok = false;
2ac2f164 6067
1c7485af
MP
6068 /* Use the expansion point location to handle cases such as user's
6069 function returning a wrong-type macro defined in a system header. */
6070 location = expansion_point_location_if_in_system_header (location);
6071
6b4ef5c1 6072 if (errtype == ic_argpass)
2ac2f164
JM
6073 {
6074 tree selector;
6075 /* Change pointer to function to the function itself for
6076 diagnostics. */
6077 if (TREE_CODE (function) == ADDR_EXPR
6078 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6079 function = TREE_OPERAND (function, 0);
6080
6081 /* Handle an ObjC selector specially for diagnostics. */
6082 selector = objc_message_selector ();
6083 rname = function;
6084 if (selector && parmnum > 2)
6085 {
6086 rname = selector;
6087 parmnum -= 2;
6088 }
6089 }
6090
6091 /* This macro is used to emit diagnostics to ensure that all format
6092 strings are complete sentences, visible to gettext and checked at
6093 compile time. */
768952be 6094#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
6095 do { \
6096 switch (errtype) \
6097 { \
6098 case ic_argpass: \
5c1bc275 6099 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 6100 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 6101 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
6102 "expected %qT but argument is of type %qT", \
6103 type, rhstype); \
6104 break; \
1e053dfe
MLI
6105 case ic_assign: \
6106 pedwarn (LOCATION, OPT, AS); \
6107 break; \
6108 case ic_init: \
6a8f4e12 6109 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
6110 break; \
6111 case ic_return: \
d033409e 6112 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
6113 break; \
6114 default: \
6115 gcc_unreachable (); \
6116 } \
2ac2f164 6117 } while (0)
cb3ca04e 6118
49706e39
MLI
6119 /* This macro is used to emit diagnostics to ensure that all format
6120 strings are complete sentences, visible to gettext and checked at
768952be 6121 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 6122 extra parameter to enumerate qualifiers. */
768952be 6123#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
6124 do { \
6125 switch (errtype) \
6126 { \
6127 case ic_argpass: \
5c1bc275 6128 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 6129 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 6130 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
6131 "expected %qT but argument is of type %qT", \
6132 type, rhstype); \
6133 break; \
6134 case ic_assign: \
5c1bc275 6135 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
6136 break; \
6137 case ic_init: \
5c1bc275 6138 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
6139 break; \
6140 case ic_return: \
5c1bc275 6141 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
6142 break; \
6143 default: \
6144 gcc_unreachable (); \
6145 } \
6146 } while (0)
6147
768952be
MU
6148 /* This macro is used to emit diagnostics to ensure that all format
6149 strings are complete sentences, visible to gettext and checked at
6150 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6151 warning_at instead of pedwarn. */
6152#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6153 do { \
6154 switch (errtype) \
6155 { \
6156 case ic_argpass: \
6157 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6158 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6159 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6160 "expected %qT but argument is of type %qT", \
6161 type, rhstype); \
6162 break; \
6163 case ic_assign: \
6164 warning_at (LOCATION, OPT, AS, QUALS); \
6165 break; \
6166 case ic_init: \
6167 warning_at (LOCATION, OPT, IN, QUALS); \
6168 break; \
6169 case ic_return: \
6170 warning_at (LOCATION, OPT, RE, QUALS); \
6171 break; \
6172 default: \
6173 gcc_unreachable (); \
6174 } \
6175 } while (0)
6176
8ce94e44
JM
6177 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6178 rhs = TREE_OPERAND (rhs, 0);
6179
3e4093b6
RS
6180 rhstype = TREE_TYPE (rhs);
6181 coder = TREE_CODE (rhstype);
6182
6183 if (coder == ERROR_MARK)
6184 return error_mark_node;
6185
58393038
ZL
6186 if (c_dialect_objc ())
6187 {
6188 int parmno;
6189
6190 switch (errtype)
6191 {
6192 case ic_return:
6193 parmno = 0;
6194 break;
6195
6196 case ic_assign:
6197 parmno = -1;
6198 break;
6199
6200 case ic_init:
6201 parmno = -2;
6202 break;
6203
6204 default:
6205 parmno = parmnum;
6206 break;
6207 }
6208
6209 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6210 }
6211
bbbbb16a
ILT
6212 if (warn_cxx_compat)
6213 {
6214 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6215 if (checktype != error_mark_node
6216 && TREE_CODE (type) == ENUMERAL_TYPE
6217 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6218 {
768952be
MU
6219 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6220 G_("enum conversion when passing argument "
6221 "%d of %qE is invalid in C++"),
6222 G_("enum conversion in assignment is "
6223 "invalid in C++"),
6224 G_("enum conversion in initialization is "
6225 "invalid in C++"),
6226 G_("enum conversion in return is "
6227 "invalid in C++"));
bbbbb16a
ILT
6228 }
6229 }
6230
3e4093b6 6231 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 6232 return rhs;
3e4093b6
RS
6233
6234 if (coder == VOID_TYPE)
400fbf9f 6235 {
6dcc04b0
JM
6236 /* Except for passing an argument to an unprototyped function,
6237 this is a constraint violation. When passing an argument to
6238 an unprototyped function, it is compile-time undefined;
6239 making it a constraint in that case was rejected in
6240 DR#252. */
c2255bc4 6241 error_at (location, "void value not ignored as it ought to be");
3e4093b6 6242 return error_mark_node;
400fbf9f 6243 }
4f2e1536 6244 rhs = require_complete_type (location, rhs);
808d6eaa
JM
6245 if (rhs == error_mark_node)
6246 return error_mark_node;
1807ffc1
MS
6247
6248 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6249 return error_mark_node;
6250
cd192ccc
MS
6251 /* A non-reference type can convert to a reference. This handles
6252 va_start, va_copy and possibly port built-ins. */
6253 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 6254 {
3e4093b6 6255 if (!lvalue_p (rhs))
400fbf9f 6256 {
c2255bc4 6257 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 6258 return error_mark_node;
400fbf9f 6259 }
3e4093b6
RS
6260 if (!c_mark_addressable (rhs))
6261 return error_mark_node;
6262 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 6263 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6264
81e5eca8 6265 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
6266 build_pointer_type (TREE_TYPE (type)),
6267 rhs, origtype, errtype,
6268 null_pointer_constant, fundecl, function,
6269 parmnum);
cd192ccc
MS
6270 if (rhs == error_mark_node)
6271 return error_mark_node;
3e4093b6
RS
6272
6273 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 6274 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6275 return rhs;
400fbf9f 6276 }
3e4093b6 6277 /* Some types can interconvert without explicit casts. */
3274deff 6278 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 6279 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
6280 return convert (type, rhs);
6281 /* Arithmetic types all interconvert, and enum is treated like int. */
6282 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 6283 || codel == FIXED_POINT_TYPE
3e4093b6
RS
6284 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6285 || codel == BOOLEAN_TYPE)
6286 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 6287 || coder == FIXED_POINT_TYPE
3e4093b6
RS
6288 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6289 || coder == BOOLEAN_TYPE))
928c19bb
JM
6290 {
6291 tree ret;
6292 bool save = in_late_binary_op;
e5341100
JJ
6293 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6294 || (coder == REAL_TYPE
6295 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6296 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
928c19bb 6297 in_late_binary_op = true;
81e5eca8
MP
6298 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6299 ? expr_loc : location, type, orig_rhs);
e5341100 6300 in_late_binary_op = save;
928c19bb
JM
6301 return ret;
6302 }
400fbf9f 6303
79077aea
JJ
6304 /* Aggregates in different TUs might need conversion. */
6305 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6306 && codel == coder
6307 && comptypes (type, rhstype))
81e5eca8
MP
6308 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6309 ? expr_loc : location, type, rhs);
79077aea 6310
ebf0bf7f 6311 /* Conversion to a transparent union or record from its member types.
3e4093b6 6312 This applies only to function arguments. */
ebf0bf7f
JJ
6313 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6314 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 6315 && errtype == ic_argpass)
400fbf9f 6316 {
0257e383 6317 tree memb, marginal_memb = NULL_TREE;
3e4093b6 6318
910ad8de 6319 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 6320 {
0257e383 6321 tree memb_type = TREE_TYPE (memb);
400fbf9f 6322
3e4093b6 6323 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 6324 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 6325 break;
e58cd767 6326
3e4093b6
RS
6327 if (TREE_CODE (memb_type) != POINTER_TYPE)
6328 continue;
2f6e4e97 6329
3e4093b6
RS
6330 if (coder == POINTER_TYPE)
6331 {
6332 tree ttl = TREE_TYPE (memb_type);
6333 tree ttr = TREE_TYPE (rhstype);
400fbf9f 6334
3e4093b6
RS
6335 /* Any non-function converts to a [const][volatile] void *
6336 and vice versa; otherwise, targets must be the same.
6337 Meanwhile, the lhs target must have all the qualifiers of
6338 the rhs. */
267bac10
JM
6339 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6340 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6341 || comp_target_types (location, memb_type, rhstype))
3e4093b6 6342 {
267bac10
JM
6343 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6344 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 6345 /* If this type won't generate any warnings, use it. */
267bac10 6346 if (lquals == rquals
3e4093b6
RS
6347 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6348 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
6349 ? ((lquals | rquals) == rquals)
6350 : ((lquals | rquals) == lquals)))
3e4093b6 6351 break;
400fbf9f 6352
3e4093b6 6353 /* Keep looking for a better type, but remember this one. */
0257e383
RH
6354 if (!marginal_memb)
6355 marginal_memb = memb;
3e4093b6
RS
6356 }
6357 }
82bde854 6358
3e4093b6 6359 /* Can convert integer zero to any pointer type. */
928c19bb 6360 if (null_pointer_constant)
3e4093b6
RS
6361 {
6362 rhs = null_pointer_node;
6363 break;
6364 }
6365 }
400fbf9f 6366
0257e383 6367 if (memb || marginal_memb)
3e4093b6 6368 {
0257e383 6369 if (!memb)
3e4093b6
RS
6370 {
6371 /* We have only a marginally acceptable member type;
6372 it needs a warning. */
0257e383 6373 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 6374 tree ttr = TREE_TYPE (rhstype);
714a0864 6375
3e4093b6
RS
6376 /* Const and volatile mean something different for function
6377 types, so the usual warnings are not appropriate. */
6378 if (TREE_CODE (ttr) == FUNCTION_TYPE
6379 && TREE_CODE (ttl) == FUNCTION_TYPE)
6380 {
6381 /* Because const and volatile on functions are
6382 restrictions that say the function will not do
6383 certain things, it is okay to use a const or volatile
6384 function where an ordinary one is wanted, but not
6385 vice-versa. */
36c5e70a
BE
6386 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6387 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6388 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6389 OPT_Wdiscarded_qualifiers,
6390 G_("passing argument %d of %qE "
6391 "makes %q#v qualified function "
6392 "pointer from unqualified"),
6393 G_("assignment makes %q#v qualified "
6394 "function pointer from "
6395 "unqualified"),
6396 G_("initialization makes %q#v qualified "
6397 "function pointer from "
6398 "unqualified"),
6399 G_("return makes %q#v qualified function "
6400 "pointer from unqualified"),
6401 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6402 }
36c5e70a
BE
6403 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6404 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6405 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6406 OPT_Wdiscarded_qualifiers,
6407 G_("passing argument %d of %qE discards "
6408 "%qv qualifier from pointer target type"),
6409 G_("assignment discards %qv qualifier "
6410 "from pointer target type"),
6411 G_("initialization discards %qv qualifier "
6412 "from pointer target type"),
6413 G_("return discards %qv qualifier from "
6414 "pointer target type"),
6415 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6416
6417 memb = marginal_memb;
3e4093b6 6418 }
400fbf9f 6419
fcf73884 6420 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6421 pedwarn (location, OPT_Wpedantic,
fcf73884 6422 "ISO C prohibits argument conversion to union type");
0e7c47fa 6423
db3927fb 6424 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6425 return build_constructor_single (type, memb, rhs);
3e4093b6 6426 }
0e7c47fa
RK
6427 }
6428
3e4093b6
RS
6429 /* Conversions among pointers */
6430 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6431 && (coder == codel))
400fbf9f 6432 {
3e4093b6
RS
6433 tree ttl = TREE_TYPE (type);
6434 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6435 tree mvl = ttl;
6436 tree mvr = ttr;
3e4093b6 6437 bool is_opaque_pointer;
264fa2db 6438 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6439 addr_space_t asl;
6440 addr_space_t asr;
400fbf9f 6441
46df2823 6442 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6443 mvl = (TYPE_ATOMIC (mvl)
6444 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6445 TYPE_QUAL_ATOMIC)
6446 : TYPE_MAIN_VARIANT (mvl));
46df2823 6447 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6448 mvr = (TYPE_ATOMIC (mvr)
6449 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6450 TYPE_QUAL_ATOMIC)
6451 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6452 /* Opaque pointers are treated like void pointers. */
f83c7f63 6453 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6454
478a1c5b
ILT
6455 /* The Plan 9 compiler permits a pointer to a struct to be
6456 automatically converted into a pointer to an anonymous field
6457 within the struct. */
6458 if (flag_plan9_extensions
3e636daf
MP
6459 && RECORD_OR_UNION_TYPE_P (mvl)
6460 && RECORD_OR_UNION_TYPE_P (mvr)
478a1c5b
ILT
6461 && mvl != mvr)
6462 {
6463 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6464 if (new_rhs != NULL_TREE)
6465 {
6466 rhs = new_rhs;
6467 rhstype = TREE_TYPE (rhs);
6468 coder = TREE_CODE (rhstype);
6469 ttr = TREE_TYPE (rhstype);
6470 mvr = TYPE_MAIN_VARIANT (ttr);
6471 }
6472 }
6473
b7e20b53 6474 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6475 for the purpose of reducing the number of false positives, we
6476 tolerate the special case of
b7e20b53 6477
c22cacf3 6478 int *p = NULL;
b7e20b53 6479
c22cacf3 6480 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6481 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6482 warning_at (errtype == ic_argpass ? expr_loc : location,
6483 OPT_Wc___compat,
6484 "request for implicit conversion "
c2255bc4 6485 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6486
36c5e70a
BE
6487 /* See if the pointers point to incompatible address spaces. */
6488 asl = TYPE_ADDR_SPACE (ttl);
6489 asr = TYPE_ADDR_SPACE (ttr);
6490 if (!null_pointer_constant_p (rhs)
6491 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6492 {
6493 switch (errtype)
6494 {
6495 case ic_argpass:
8ffcdea8 6496 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6497 "non-enclosed address space", parmnum, rname);
6498 break;
6499 case ic_assign:
6500 error_at (location, "assignment from pointer to "
6501 "non-enclosed address space");
6502 break;
6503 case ic_init:
6504 error_at (location, "initialization from pointer to "
6505 "non-enclosed address space");
6506 break;
6507 case ic_return:
6508 error_at (location, "return from pointer to "
6509 "non-enclosed address space");
6510 break;
6511 default:
6512 gcc_unreachable ();
6513 }
6514 return error_mark_node;
6515 }
6516
7876a414
KG
6517 /* Check if the right-hand side has a format attribute but the
6518 left-hand side doesn't. */
90137d8f 6519 if (warn_suggest_attribute_format
104f8784 6520 && check_missing_format_attribute (type, rhstype))
c22cacf3 6521 {
104f8784
KG
6522 switch (errtype)
6523 {
6524 case ic_argpass:
8ffcdea8 6525 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6526 "argument %d of %qE might be "
6527 "a candidate for a format attribute",
6528 parmnum, rname);
104f8784
KG
6529 break;
6530 case ic_assign:
90137d8f 6531 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6532 "assignment left-hand side might be "
6533 "a candidate for a format attribute");
104f8784
KG
6534 break;
6535 case ic_init:
90137d8f 6536 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6537 "initialization left-hand side might be "
6538 "a candidate for a format attribute");
104f8784
KG
6539 break;
6540 case ic_return:
90137d8f 6541 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6542 "return type might be "
6543 "a candidate for a format attribute");
104f8784
KG
6544 break;
6545 default:
6546 gcc_unreachable ();
6547 }
7876a414 6548 }
c22cacf3 6549
3e4093b6
RS
6550 /* Any non-function converts to a [const][volatile] void *
6551 and vice versa; otherwise, targets must be the same.
6552 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6553 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6554 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6555 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6556 || is_opaque_pointer
f8a93a2e
JJ
6557 || ((c_common_unsigned_type (mvl)
6558 == c_common_unsigned_type (mvr))
267bac10
JM
6559 && (c_common_signed_type (mvl)
6560 == c_common_signed_type (mvr))
6561 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6562 {
768952be
MU
6563 /* Warn about loss of qualifers from pointers to arrays with
6564 qualifiers on the element type. */
6565 if (TREE_CODE (ttr) == ARRAY_TYPE)
6566 {
6567 ttr = strip_array_types (ttr);
6568 ttl = strip_array_types (ttl);
6569
6570 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6571 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6572 WARNING_FOR_QUALIFIERS (location, expr_loc,
6573 OPT_Wdiscarded_array_qualifiers,
6574 G_("passing argument %d of %qE discards "
6575 "%qv qualifier from pointer target type"),
6576 G_("assignment discards %qv qualifier "
6577 "from pointer target type"),
6578 G_("initialization discards %qv qualifier "
6579 "from pointer target type"),
6580 G_("return discards %qv qualifier from "
6581 "pointer target type"),
6582 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6583 }
6584 else if (pedantic
3e4093b6
RS
6585 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6586 ||
6587 (VOID_TYPE_P (ttr)
928c19bb 6588 && !null_pointer_constant
3e4093b6 6589 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6590 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6591 G_("ISO C forbids passing argument %d of "
6592 "%qE between function pointer "
6593 "and %<void *%>"),
6594 G_("ISO C forbids assignment between "
6595 "function pointer and %<void *%>"),
6596 G_("ISO C forbids initialization between "
6597 "function pointer and %<void *%>"),
6598 G_("ISO C forbids return between function "
6599 "pointer and %<void *%>"));
3e4093b6
RS
6600 /* Const and volatile mean something different for function types,
6601 so the usual warnings are not appropriate. */
6602 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6603 && TREE_CODE (ttl) != FUNCTION_TYPE)
6604 {
768952be
MU
6605 /* Don't warn about loss of qualifier for conversions from
6606 qualified void* to pointers to arrays with corresponding
6607 qualifier on the element type. */
6608 if (!pedantic)
6609 ttl = strip_array_types (ttl);
6610
267bac10
JM
6611 /* Assignments between atomic and non-atomic objects are OK. */
6612 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6613 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6614 {
768952be
MU
6615 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6616 OPT_Wdiscarded_qualifiers,
6617 G_("passing argument %d of %qE discards "
6618 "%qv qualifier from pointer target type"),
6619 G_("assignment discards %qv qualifier "
6620 "from pointer target type"),
6621 G_("initialization discards %qv qualifier "
6622 "from pointer target type"),
6623 G_("return discards %qv qualifier from "
6624 "pointer target type"),
6625 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6626 }
3e4093b6
RS
6627 /* If this is not a case of ignoring a mismatch in signedness,
6628 no warning. */
6629 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6630 || target_cmp)
3e4093b6
RS
6631 ;
6632 /* If there is a mismatch, do warn. */
f2fd3821 6633 else if (warn_pointer_sign)
768952be
MU
6634 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6635 G_("pointer targets in passing argument "
6636 "%d of %qE differ in signedness"),
6637 G_("pointer targets in assignment "
6638 "differ in signedness"),
6639 G_("pointer targets in initialization "
6640 "differ in signedness"),
6641 G_("pointer targets in return differ "
6642 "in signedness"));
3e4093b6
RS
6643 }
6644 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6645 && TREE_CODE (ttr) == FUNCTION_TYPE)
6646 {
6647 /* Because const and volatile on functions are restrictions
6648 that say the function will not do certain things,
6649 it is okay to use a const or volatile function
6650 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6651 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6652 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6653 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6654 OPT_Wdiscarded_qualifiers,
6655 G_("passing argument %d of %qE makes "
6656 "%q#v qualified function pointer "
6657 "from unqualified"),
6658 G_("assignment makes %q#v qualified function "
6659 "pointer from unqualified"),
6660 G_("initialization makes %q#v qualified "
6661 "function pointer from unqualified"),
6662 G_("return makes %q#v qualified function "
6663 "pointer from unqualified"),
6664 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6665 }
6666 }
6667 else
58393038
ZL
6668 /* Avoid warning about the volatile ObjC EH puts on decls. */
6669 if (!objc_ok)
768952be
MU
6670 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6671 OPT_Wincompatible_pointer_types,
6672 G_("passing argument %d of %qE from "
6673 "incompatible pointer type"),
6674 G_("assignment from incompatible pointer type"),
6675 G_("initialization from incompatible "
6676 "pointer type"),
6677 G_("return from incompatible pointer type"));
58393038 6678
3e4093b6
RS
6679 return convert (type, rhs);
6680 }
b494fd98
EB
6681 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6682 {
6dcc04b0
JM
6683 /* ??? This should not be an error when inlining calls to
6684 unprototyped functions. */
c2255bc4 6685 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6686 return error_mark_node;
6687 }
3e4093b6 6688 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6689 {
3e4093b6
RS
6690 /* An explicit constant 0 can convert to a pointer,
6691 or one that results from arithmetic, even including
6692 a cast to integer type. */
928c19bb 6693 if (!null_pointer_constant)
768952be
MU
6694 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6695 OPT_Wint_conversion,
6696 G_("passing argument %d of %qE makes "
6697 "pointer from integer without a cast"),
6698 G_("assignment makes pointer from integer "
6699 "without a cast"),
6700 G_("initialization makes pointer from "
6701 "integer without a cast"),
6702 G_("return makes pointer from integer "
6703 "without a cast"));
b3006337
EB
6704
6705 return convert (type, rhs);
400fbf9f 6706 }
3e4093b6 6707 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6708 {
768952be
MU
6709 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6710 OPT_Wint_conversion,
6711 G_("passing argument %d of %qE makes integer "
6712 "from pointer without a cast"),
6713 G_("assignment makes integer from pointer "
6714 "without a cast"),
6715 G_("initialization makes integer from pointer "
6716 "without a cast"),
6717 G_("return makes integer from pointer "
6718 "without a cast"));
3e4093b6
RS
6719 return convert (type, rhs);
6720 }
6721 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6722 {
6723 tree ret;
6724 bool save = in_late_binary_op;
6725 in_late_binary_op = true;
6726 ret = convert (type, rhs);
6727 in_late_binary_op = save;
6728 return ret;
6729 }
400fbf9f 6730
2ac2f164 6731 switch (errtype)
3e4093b6 6732 {
2ac2f164 6733 case ic_argpass:
8ffcdea8
MP
6734 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6735 rname);
a7da8b42 6736 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6737 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6738 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6739 break;
6740 case ic_assign:
c2255bc4
AH
6741 error_at (location, "incompatible types when assigning to type %qT from "
6742 "type %qT", type, rhstype);
2ac2f164
JM
6743 break;
6744 case ic_init:
c2255bc4 6745 error_at (location,
8ffcdea8 6746 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6747 type, rhstype);
2ac2f164
JM
6748 break;
6749 case ic_return:
c2255bc4 6750 error_at (location,
8ffcdea8 6751 "incompatible types when returning type %qT but %qT was "
c2255bc4 6752 "expected", rhstype, type);
2ac2f164
JM
6753 break;
6754 default:
6755 gcc_unreachable ();
400fbf9f 6756 }
53b01f59 6757
3e4093b6
RS
6758 return error_mark_node;
6759}
3e4093b6
RS
6760\f
6761/* If VALUE is a compound expr all of whose expressions are constant, then
6762 return its value. Otherwise, return error_mark_node.
15b732b2 6763
3e4093b6
RS
6764 This is for handling COMPOUND_EXPRs as initializer elements
6765 which is allowed with a warning when -pedantic is specified. */
15b732b2 6766
3e4093b6
RS
6767static tree
6768valid_compound_expr_initializer (tree value, tree endtype)
6769{
6770 if (TREE_CODE (value) == COMPOUND_EXPR)
6771 {
6772 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6773 == error_mark_node)
6774 return error_mark_node;
6775 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6776 endtype);
6777 }
116df786 6778 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6779 return error_mark_node;
6780 else
6781 return value;
15b732b2 6782}
400fbf9f 6783\f
3e4093b6
RS
6784/* Perform appropriate conversions on the initial value of a variable,
6785 store it in the declaration DECL,
6786 and print any error messages that are appropriate.
bbbbb16a 6787 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6788 If the init is invalid, store an ERROR_MARK.
6789
6790 INIT_LOC is the location of the initial value. */
400fbf9f 6791
3e4093b6 6792void
c2255bc4 6793store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6794{
3e4093b6 6795 tree value, type;
928c19bb 6796 bool npc = false;
400fbf9f 6797
3e4093b6 6798 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6799
3e4093b6
RS
6800 type = TREE_TYPE (decl);
6801 if (TREE_CODE (type) == ERROR_MARK)
6802 return;
400fbf9f 6803
3e4093b6 6804 /* Digest the specified initializer into an expression. */
400fbf9f 6805
928c19bb
JM
6806 if (init)
6807 npc = null_pointer_constant_p (init);
c2255bc4
AH
6808 value = digest_init (init_loc, type, init, origtype, npc,
6809 true, TREE_STATIC (decl));
400fbf9f 6810
3e4093b6 6811 /* Store the expression if valid; else report error. */
400fbf9f 6812
8400e75e 6813 if (!in_system_header_at (input_location)
3f75a254 6814 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6815 warning (OPT_Wtraditional, "traditional C rejects automatic "
6816 "aggregate initialization");
2f6e4e97 6817
dea63e49
JJ
6818 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6819 DECL_INITIAL (decl) = value;
400fbf9f 6820
3e4093b6
RS
6821 /* ANSI wants warnings about out-of-range constant initializers. */
6822 STRIP_TYPE_NOPS (value);
b8698a0f 6823 if (TREE_STATIC (decl))
c2658540 6824 constant_expression_warning (value);
400fbf9f 6825
3e4093b6
RS
6826 /* Check if we need to set array size from compound literal size. */
6827 if (TREE_CODE (type) == ARRAY_TYPE
6828 && TYPE_DOMAIN (type) == 0
6829 && value != error_mark_node)
400fbf9f 6830 {
3e4093b6
RS
6831 tree inside_init = init;
6832
ed248cf7 6833 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6834 inside_init = fold (inside_init);
6835
6836 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6837 {
8d9f82d5 6838 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6839
8d9f82d5 6840 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6841 {
6842 /* For int foo[] = (int [3]){1}; we need to set array size
6843 now since later on array initializer will be just the
6844 brace enclosed list of the compound literal. */
e30ecc5d 6845 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6846 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6847 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6848 layout_type (type);
8d9f82d5 6849 layout_decl (cldecl, 0);
e30ecc5d
JJ
6850 TREE_TYPE (decl)
6851 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6852 }
6853 }
400fbf9f 6854 }
3e4093b6
RS
6855}
6856\f
6857/* Methods for storing and printing names for error messages. */
400fbf9f 6858
3e4093b6
RS
6859/* Implement a spelling stack that allows components of a name to be pushed
6860 and popped. Each element on the stack is this structure. */
400fbf9f 6861
3e4093b6
RS
6862struct spelling
6863{
6864 int kind;
6865 union
400fbf9f 6866 {
a0f0ab9f 6867 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6868 const char *s;
6869 } u;
6870};
2f6e4e97 6871
3e4093b6
RS
6872#define SPELLING_STRING 1
6873#define SPELLING_MEMBER 2
6874#define SPELLING_BOUNDS 3
400fbf9f 6875
3e4093b6
RS
6876static struct spelling *spelling; /* Next stack element (unused). */
6877static struct spelling *spelling_base; /* Spelling stack base. */
6878static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6879
3e4093b6
RS
6880/* Macros to save and restore the spelling stack around push_... functions.
6881 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6882
3e4093b6
RS
6883#define SPELLING_DEPTH() (spelling - spelling_base)
6884#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6885
3e4093b6
RS
6886/* Push an element on the spelling stack with type KIND and assign VALUE
6887 to MEMBER. */
400fbf9f 6888
3e4093b6
RS
6889#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6890{ \
6891 int depth = SPELLING_DEPTH (); \
6892 \
6893 if (depth >= spelling_size) \
6894 { \
6895 spelling_size += 10; \
cca8ead2
BI
6896 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6897 spelling_size); \
3e4093b6
RS
6898 RESTORE_SPELLING_DEPTH (depth); \
6899 } \
6900 \
6901 spelling->kind = (KIND); \
6902 spelling->MEMBER = (VALUE); \
6903 spelling++; \
6904}
400fbf9f 6905
3e4093b6 6906/* Push STRING on the stack. Printed literally. */
400fbf9f 6907
3e4093b6
RS
6908static void
6909push_string (const char *string)
6910{
6911 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6912}
400fbf9f 6913
3e4093b6 6914/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6915
3e4093b6
RS
6916static void
6917push_member_name (tree decl)
6918{
6919 const char *const string
88388a52
JM
6920 = (DECL_NAME (decl)
6921 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6922 : _("<anonymous>"));
3e4093b6
RS
6923 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6924}
400fbf9f 6925
3e4093b6 6926/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6927
3e4093b6 6928static void
a0f0ab9f 6929push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6930{
6931 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6932}
bb58bec5 6933
3e4093b6 6934/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6935
3e4093b6
RS
6936static int
6937spelling_length (void)
6938{
6939 int size = 0;
6940 struct spelling *p;
400fbf9f 6941
3e4093b6
RS
6942 for (p = spelling_base; p < spelling; p++)
6943 {
6944 if (p->kind == SPELLING_BOUNDS)
6945 size += 25;
6946 else
6947 size += strlen (p->u.s) + 1;
6948 }
6949
6950 return size;
400fbf9f 6951}
400fbf9f 6952
3e4093b6 6953/* Print the spelling to BUFFER and return it. */
400fbf9f 6954
3e4093b6
RS
6955static char *
6956print_spelling (char *buffer)
400fbf9f 6957{
3e4093b6
RS
6958 char *d = buffer;
6959 struct spelling *p;
400fbf9f 6960
3e4093b6
RS
6961 for (p = spelling_base; p < spelling; p++)
6962 if (p->kind == SPELLING_BOUNDS)
6963 {
a0f0ab9f 6964 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6965 d += strlen (d);
6966 }
6967 else
6968 {
6969 const char *s;
6970 if (p->kind == SPELLING_MEMBER)
6971 *d++ = '.';
6972 for (s = p->u.s; (*d = *s++); d++)
6973 ;
6974 }
6975 *d++ = '\0';
6976 return buffer;
6977}
400fbf9f 6978
3e4093b6
RS
6979/* Digest the parser output INIT as an initializer for type TYPE.
6980 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6981
bbbbb16a
ILT
6982 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6983
928c19bb
JM
6984 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6985
916c5919
JM
6986 If INIT is a string constant, STRICT_STRING is true if it is
6987 unparenthesized or we should not warn here for it being parenthesized.
6988 For other types of INIT, STRICT_STRING is not used.
6989
c2255bc4
AH
6990 INIT_LOC is the location of the INIT.
6991
3e4093b6
RS
6992 REQUIRE_CONSTANT requests an error if non-constant initializers or
6993 elements are seen. */
7e842ef8 6994
3e4093b6 6995static tree
c2255bc4
AH
6996digest_init (location_t init_loc, tree type, tree init, tree origtype,
6997 bool null_pointer_constant, bool strict_string,
6998 int require_constant)
3e4093b6
RS
6999{
7000 enum tree_code code = TREE_CODE (type);
7001 tree inside_init = init;
8ce94e44 7002 tree semantic_type = NULL_TREE;
928c19bb 7003 bool maybe_const = true;
7e842ef8 7004
3e4093b6 7005 if (type == error_mark_node
f01da1a5 7006 || !init
7a0ca710 7007 || error_operand_p (init))
3e4093b6 7008 return error_mark_node;
7e842ef8 7009
ed248cf7 7010 STRIP_TYPE_NOPS (inside_init);
7e842ef8 7011
8ce94e44
JM
7012 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7013 {
7014 semantic_type = TREE_TYPE (inside_init);
7015 inside_init = TREE_OPERAND (inside_init, 0);
7016 }
928c19bb
JM
7017 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7018 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 7019
3e4093b6
RS
7020 /* Initialization of an array of chars from a string constant
7021 optionally enclosed in braces. */
7e842ef8 7022
197463ae
JM
7023 if (code == ARRAY_TYPE && inside_init
7024 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 7025 {
267bac10
JM
7026 tree typ1
7027 = (TYPE_ATOMIC (TREE_TYPE (type))
7028 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7029 TYPE_QUAL_ATOMIC)
7030 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
7031 /* Note that an array could be both an array of character type
7032 and an array of wchar_t if wchar_t is signed char or unsigned
7033 char. */
7034 bool char_array = (typ1 == char_type_node
7035 || typ1 == signed_char_type_node
7036 || typ1 == unsigned_char_type_node);
7037 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
7038 bool char16_array = !!comptypes (typ1, char16_type_node);
7039 bool char32_array = !!comptypes (typ1, char32_type_node);
7040
7041 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 7042 {
916c5919 7043 struct c_expr expr;
c466b2cd 7044 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
7045 expr.value = inside_init;
7046 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 7047 expr.original_type = NULL;
d033409e 7048 maybe_warn_string_init (init_loc, type, expr);
916c5919 7049
a45e580b 7050 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 7051 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
7052 "initialization of a flexible array member");
7053
3e4093b6 7054 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 7055 TYPE_MAIN_VARIANT (type)))
3e4093b6 7056 return inside_init;
7e842ef8 7057
c466b2cd 7058 if (char_array)
3e4093b6 7059 {
c466b2cd
KVH
7060 if (typ2 != char_type_node)
7061 {
ea58ef42
MP
7062 error_init (init_loc, "char-array initialized from wide "
7063 "string");
c466b2cd
KVH
7064 return error_mark_node;
7065 }
3e4093b6 7066 }
c466b2cd 7067 else
3e4093b6 7068 {
c466b2cd
KVH
7069 if (typ2 == char_type_node)
7070 {
ea58ef42
MP
7071 error_init (init_loc, "wide character array initialized "
7072 "from non-wide string");
c466b2cd
KVH
7073 return error_mark_node;
7074 }
7075 else if (!comptypes(typ1, typ2))
7076 {
ea58ef42
MP
7077 error_init (init_loc, "wide character array initialized "
7078 "from incompatible wide string");
c466b2cd
KVH
7079 return error_mark_node;
7080 }
7e842ef8 7081 }
2f6e4e97 7082
3e4093b6
RS
7083 TREE_TYPE (inside_init) = type;
7084 if (TYPE_DOMAIN (type) != 0
7085 && TYPE_SIZE (type) != 0
5eb4df45
ILT
7086 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7087 {
7088 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7089
c466b2cd 7090 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
7091 because it's ok to ignore the terminating null char
7092 that is counted in the length of the constant. */
5eb4df45
ILT
7093 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7094 (len
7095 - (TYPE_PRECISION (typ1)
7096 / BITS_PER_UNIT))))
7097 pedwarn_init (init_loc, 0,
7098 ("initializer-string for array of chars "
7099 "is too long"));
7100 else if (warn_cxx_compat
7101 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7102 warning_at (init_loc, OPT_Wc___compat,
7103 ("initializer-string for array chars "
7104 "is too long for C++"));
7105 }
7e842ef8 7106
3e4093b6 7107 return inside_init;
7e842ef8 7108 }
197463ae
JM
7109 else if (INTEGRAL_TYPE_P (typ1))
7110 {
ea58ef42 7111 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
7112 "from string constant");
7113 return error_mark_node;
7114 }
7e842ef8
PE
7115 }
7116
3e4093b6
RS
7117 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7118 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7119 below and handle as a constructor. */
e89be13b 7120 if (code == VECTOR_TYPE
31521951 7121 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
00c8e9f6 7122 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
7123 && TREE_CONSTANT (inside_init))
7124 {
7125 if (TREE_CODE (inside_init) == VECTOR_CST
7126 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7127 TYPE_MAIN_VARIANT (type)))
7128 return inside_init;
7129
7130 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7131 {
4038c495
GB
7132 unsigned HOST_WIDE_INT ix;
7133 tree value;
7134 bool constant_p = true;
e89be13b
JJ
7135
7136 /* Iterate through elements and check if all constructor
7137 elements are *_CSTs. */
4038c495
GB
7138 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7139 if (!CONSTANT_CLASS_P (value))
7140 {
7141 constant_p = false;
7142 break;
7143 }
e89be13b 7144
4038c495
GB
7145 if (constant_p)
7146 return build_vector_from_ctor (type,
7147 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
7148 }
7149 }
6035d635 7150
ca085fd7
MLI
7151 if (warn_sequence_point)
7152 verify_sequence_points (inside_init);
7153
3e4093b6
RS
7154 /* Any type can be initialized
7155 from an expression of the same type, optionally with braces. */
400fbf9f 7156
3e4093b6
RS
7157 if (inside_init && TREE_TYPE (inside_init) != 0
7158 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 7159 TYPE_MAIN_VARIANT (type))
3e4093b6 7160 || (code == ARRAY_TYPE
132da1a5 7161 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7162 || (code == VECTOR_TYPE
132da1a5 7163 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7164 || (code == POINTER_TYPE
3897f229 7165 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 7166 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 7167 TREE_TYPE (type)))))
3e4093b6
RS
7168 {
7169 if (code == POINTER_TYPE)
b494fd98 7170 {
b494fd98
EB
7171 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7172 {
f2a71bbc
JM
7173 if (TREE_CODE (inside_init) == STRING_CST
7174 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
7175 inside_init = array_to_pointer_conversion
7176 (init_loc, inside_init);
f2a71bbc
JM
7177 else
7178 {
ea58ef42 7179 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
7180 return error_mark_node;
7181 }
b494fd98 7182 }
f2a71bbc 7183 }
b494fd98 7184
bae39a73
NS
7185 if (code == VECTOR_TYPE)
7186 /* Although the types are compatible, we may require a
7187 conversion. */
7188 inside_init = convert (type, inside_init);
3e4093b6 7189
ca58211b 7190 if (require_constant
3e4093b6 7191 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 7192 {
3e4093b6
RS
7193 /* As an extension, allow initializing objects with static storage
7194 duration with compound literals (which are then treated just as
ca58211b
PB
7195 the brace enclosed list they contain). Also allow this for
7196 vectors, as we can only assign them with compound literals. */
7278465e
MP
7197 if (flag_isoc99 && code != VECTOR_TYPE)
7198 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7199 "is not constant");
3e4093b6
RS
7200 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7201 inside_init = DECL_INITIAL (decl);
400fbf9f 7202 }
3e4093b6
RS
7203
7204 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7205 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 7206 {
ea58ef42
MP
7207 error_init (init_loc, "array initialized from non-constant array "
7208 "expression");
3e4093b6 7209 return error_mark_node;
400fbf9f 7210 }
400fbf9f 7211
c1771a20 7212 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
7213 -pedantic-errors is specified. In the later case, we always want
7214 an error. In the former case, we simply want a warning. */
7215 if (require_constant && pedantic
7216 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7217 {
7218 inside_init
7219 = valid_compound_expr_initializer (inside_init,
7220 TREE_TYPE (inside_init));
7221 if (inside_init == error_mark_node)
ea58ef42 7222 error_init (init_loc, "initializer element is not constant");
2f6e4e97 7223 else
c1771a20 7224 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 7225 "initializer element is not constant");
3e4093b6
RS
7226 if (flag_pedantic_errors)
7227 inside_init = error_mark_node;
7228 }
7229 else if (require_constant
116df786
RH
7230 && !initializer_constant_valid_p (inside_init,
7231 TREE_TYPE (inside_init)))
3e4093b6 7232 {
ea58ef42 7233 error_init (init_loc, "initializer element is not constant");
3e4093b6 7234 inside_init = error_mark_node;
8b40563c 7235 }
928c19bb 7236 else if (require_constant && !maybe_const)
3aa3c9fc 7237 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7238 "initializer element is not a constant expression");
f735a153 7239
90137d8f 7240 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 7241 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
7242 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7243 type, inside_init, origtype,
bbbbb16a 7244 ic_init, null_pointer_constant,
928c19bb 7245 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
7246 return inside_init;
7247 }
f735a153 7248
3e4093b6 7249 /* Handle scalar types, including conversions. */
400fbf9f 7250
ab22c1fa
CF
7251 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7252 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7253 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 7254 {
f2a71bbc
JM
7255 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7256 && (TREE_CODE (init) == STRING_CST
7257 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 7258 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
7259 if (semantic_type)
7260 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7261 inside_init);
3e4093b6 7262 inside_init
68fca595
MP
7263 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7264 inside_init, origtype, ic_init,
7265 null_pointer_constant, NULL_TREE, NULL_TREE,
7266 0);
2f6e4e97 7267
3274deff
JW
7268 /* Check to see if we have already given an error message. */
7269 if (inside_init == error_mark_node)
7270 ;
3f75a254 7271 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 7272 {
ea58ef42 7273 error_init (init_loc, "initializer element is not constant");
3e4093b6 7274 inside_init = error_mark_node;
400fbf9f 7275 }
3e4093b6 7276 else if (require_constant
116df786
RH
7277 && !initializer_constant_valid_p (inside_init,
7278 TREE_TYPE (inside_init)))
400fbf9f 7279 {
ea58ef42
MP
7280 error_init (init_loc, "initializer element is not computable at "
7281 "load time");
3e4093b6 7282 inside_init = error_mark_node;
400fbf9f 7283 }
928c19bb 7284 else if (require_constant && !maybe_const)
451b5e48 7285 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7286 "initializer element is not a constant expression");
3e4093b6
RS
7287
7288 return inside_init;
400fbf9f 7289 }
d9fc6069 7290
3e4093b6 7291 /* Come here only for records and arrays. */
d9fc6069 7292
3e4093b6 7293 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 7294 {
ea58ef42 7295 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 7296 return error_mark_node;
d9fc6069 7297 }
3e4093b6 7298
ea58ef42 7299 error_init (init_loc, "invalid initializer");
3e4093b6 7300 return error_mark_node;
d9fc6069 7301}
400fbf9f 7302\f
3e4093b6 7303/* Handle initializers that use braces. */
400fbf9f 7304
3e4093b6
RS
7305/* Type of object we are accumulating a constructor for.
7306 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7307static tree constructor_type;
400fbf9f 7308
3e4093b6
RS
7309/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7310 left to fill. */
7311static tree constructor_fields;
400fbf9f 7312
3e4093b6
RS
7313/* For an ARRAY_TYPE, this is the specified index
7314 at which to store the next element we get. */
7315static tree constructor_index;
400fbf9f 7316
3e4093b6
RS
7317/* For an ARRAY_TYPE, this is the maximum index. */
7318static tree constructor_max_index;
400fbf9f 7319
3e4093b6
RS
7320/* For a RECORD_TYPE, this is the first field not yet written out. */
7321static tree constructor_unfilled_fields;
400fbf9f 7322
3e4093b6
RS
7323/* For an ARRAY_TYPE, this is the index of the first element
7324 not yet written out. */
7325static tree constructor_unfilled_index;
895ea614 7326
3e4093b6
RS
7327/* In a RECORD_TYPE, the byte index of the next consecutive field.
7328 This is so we can generate gaps between fields, when appropriate. */
7329static tree constructor_bit_index;
10d5caec 7330
3e4093b6
RS
7331/* If we are saving up the elements rather than allocating them,
7332 this is the list of elements so far (in reverse order,
7333 most recent first). */
9771b263 7334static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 7335
3e4093b6
RS
7336/* 1 if constructor should be incrementally stored into a constructor chain,
7337 0 if all the elements should be kept in AVL tree. */
7338static int constructor_incremental;
ad47f1e5 7339
3e4093b6
RS
7340/* 1 if so far this constructor's elements are all compile-time constants. */
7341static int constructor_constant;
ad47f1e5 7342
3e4093b6
RS
7343/* 1 if so far this constructor's elements are all valid address constants. */
7344static int constructor_simple;
ad47f1e5 7345
928c19bb
JM
7346/* 1 if this constructor has an element that cannot be part of a
7347 constant expression. */
7348static int constructor_nonconst;
7349
3e4093b6
RS
7350/* 1 if this constructor is erroneous so far. */
7351static int constructor_erroneous;
d45cf215 7352
9bac5cbb
G
7353/* 1 if this constructor is the universal zero initializer { 0 }. */
7354static int constructor_zeroinit;
7355
3e4093b6
RS
7356/* Structure for managing pending initializer elements, organized as an
7357 AVL tree. */
d45cf215 7358
3e4093b6 7359struct init_node
d45cf215 7360{
3e4093b6
RS
7361 struct init_node *left, *right;
7362 struct init_node *parent;
7363 int balance;
7364 tree purpose;
7365 tree value;
bbbbb16a 7366 tree origtype;
d45cf215
RS
7367};
7368
3e4093b6
RS
7369/* Tree of pending elements at this constructor level.
7370 These are elements encountered out of order
7371 which belong at places we haven't reached yet in actually
7372 writing the output.
7373 Will never hold tree nodes across GC runs. */
7374static struct init_node *constructor_pending_elts;
d45cf215 7375
3e4093b6
RS
7376/* The SPELLING_DEPTH of this constructor. */
7377static int constructor_depth;
d45cf215 7378
3e4093b6
RS
7379/* DECL node for which an initializer is being read.
7380 0 means we are reading a constructor expression
7381 such as (struct foo) {...}. */
7382static tree constructor_decl;
d45cf215 7383
3e4093b6
RS
7384/* Nonzero if this is an initializer for a top-level decl. */
7385static int constructor_top_level;
d45cf215 7386
3e4093b6
RS
7387/* Nonzero if there were any member designators in this initializer. */
7388static int constructor_designated;
d45cf215 7389
3e4093b6
RS
7390/* Nesting depth of designator list. */
7391static int designator_depth;
d45cf215 7392
3e4093b6 7393/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7394static int designator_erroneous;
d45cf215 7395
3e4093b6
RS
7396\f
7397/* This stack has a level for each implicit or explicit level of
7398 structuring in the initializer, including the outermost one. It
7399 saves the values of most of the variables above. */
d45cf215 7400
3e4093b6
RS
7401struct constructor_range_stack;
7402
7403struct constructor_stack
d45cf215 7404{
3e4093b6
RS
7405 struct constructor_stack *next;
7406 tree type;
7407 tree fields;
7408 tree index;
7409 tree max_index;
7410 tree unfilled_index;
7411 tree unfilled_fields;
7412 tree bit_index;
9771b263 7413 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7414 struct init_node *pending_elts;
7415 int offset;
7416 int depth;
916c5919 7417 /* If value nonzero, this value should replace the entire
3e4093b6 7418 constructor at this level. */
916c5919 7419 struct c_expr replacement_value;
3e4093b6
RS
7420 struct constructor_range_stack *range_stack;
7421 char constant;
7422 char simple;
928c19bb 7423 char nonconst;
3e4093b6
RS
7424 char implicit;
7425 char erroneous;
7426 char outer;
7427 char incremental;
7428 char designated;
976d5a22 7429 int designator_depth;
3e4093b6 7430};
d45cf215 7431
802415d1 7432static struct constructor_stack *constructor_stack;
d45cf215 7433
3e4093b6
RS
7434/* This stack represents designators from some range designator up to
7435 the last designator in the list. */
d45cf215 7436
3e4093b6
RS
7437struct constructor_range_stack
7438{
7439 struct constructor_range_stack *next, *prev;
7440 struct constructor_stack *stack;
7441 tree range_start;
7442 tree index;
7443 tree range_end;
7444 tree fields;
7445};
d45cf215 7446
802415d1 7447static struct constructor_range_stack *constructor_range_stack;
d45cf215 7448
3e4093b6
RS
7449/* This stack records separate initializers that are nested.
7450 Nested initializers can't happen in ANSI C, but GNU C allows them
7451 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7452
3e4093b6 7453struct initializer_stack
d45cf215 7454{
3e4093b6
RS
7455 struct initializer_stack *next;
7456 tree decl;
3e4093b6
RS
7457 struct constructor_stack *constructor_stack;
7458 struct constructor_range_stack *constructor_range_stack;
9771b263 7459 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7460 struct spelling *spelling;
7461 struct spelling *spelling_base;
7462 int spelling_size;
7463 char top_level;
7464 char require_constant_value;
7465 char require_constant_elements;
7466};
d45cf215 7467
802415d1 7468static struct initializer_stack *initializer_stack;
3e4093b6
RS
7469\f
7470/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7471
7472void
a396f8ae 7473start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 7474{
3e4093b6 7475 const char *locus;
cceb1885 7476 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7477
3e4093b6 7478 p->decl = constructor_decl;
3e4093b6
RS
7479 p->require_constant_value = require_constant_value;
7480 p->require_constant_elements = require_constant_elements;
7481 p->constructor_stack = constructor_stack;
7482 p->constructor_range_stack = constructor_range_stack;
7483 p->elements = constructor_elements;
7484 p->spelling = spelling;
7485 p->spelling_base = spelling_base;
7486 p->spelling_size = spelling_size;
7487 p->top_level = constructor_top_level;
7488 p->next = initializer_stack;
7489 initializer_stack = p;
400fbf9f 7490
3e4093b6 7491 constructor_decl = decl;
3e4093b6
RS
7492 constructor_designated = 0;
7493 constructor_top_level = top_level;
400fbf9f 7494
6f17bbcf 7495 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7496 {
7497 require_constant_value = TREE_STATIC (decl);
7498 require_constant_elements
7499 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7500 /* For a scalar, you can always use any value to initialize,
7501 even within braces. */
296a8c2f 7502 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
88388a52 7503 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7504 }
7505 else
7506 {
7507 require_constant_value = 0;
7508 require_constant_elements = 0;
88388a52 7509 locus = _("(anonymous)");
3e4093b6 7510 }
b71c7f8a 7511
3e4093b6
RS
7512 constructor_stack = 0;
7513 constructor_range_stack = 0;
b71c7f8a 7514
9bac5cbb 7515 found_missing_braces = 0;
3e4093b6
RS
7516
7517 spelling_base = 0;
7518 spelling_size = 0;
7519 RESTORE_SPELLING_DEPTH (0);
7520
7521 if (locus)
7522 push_string (locus);
7523}
7524
7525void
7526finish_init (void)
b71c7f8a 7527{
3e4093b6 7528 struct initializer_stack *p = initializer_stack;
b71c7f8a 7529
3e4093b6
RS
7530 /* Free the whole constructor stack of this initializer. */
7531 while (constructor_stack)
7532 {
7533 struct constructor_stack *q = constructor_stack;
7534 constructor_stack = q->next;
7535 free (q);
7536 }
7537
366de0ce 7538 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7539
7540 /* Pop back to the data of the outer initializer (if any). */
36579663 7541 free (spelling_base);
3aeb3655 7542
3e4093b6 7543 constructor_decl = p->decl;
3e4093b6
RS
7544 require_constant_value = p->require_constant_value;
7545 require_constant_elements = p->require_constant_elements;
7546 constructor_stack = p->constructor_stack;
7547 constructor_range_stack = p->constructor_range_stack;
7548 constructor_elements = p->elements;
7549 spelling = p->spelling;
7550 spelling_base = p->spelling_base;
7551 spelling_size = p->spelling_size;
7552 constructor_top_level = p->top_level;
7553 initializer_stack = p->next;
7554 free (p);
b71c7f8a 7555}
400fbf9f 7556\f
3e4093b6
RS
7557/* Call here when we see the initializer is surrounded by braces.
7558 This is instead of a call to push_init_level;
7559 it is matched by a call to pop_init_level.
400fbf9f 7560
3e4093b6
RS
7561 TYPE is the type to initialize, for a constructor expression.
7562 For an initializer for a decl, TYPE is zero. */
400fbf9f 7563
3e4093b6
RS
7564void
7565really_start_incremental_init (tree type)
400fbf9f 7566{
5d038c4c 7567 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7568
3e4093b6
RS
7569 if (type == 0)
7570 type = TREE_TYPE (constructor_decl);
400fbf9f 7571
31521951 7572 if (VECTOR_TYPE_P (type)
b6fc2cdb 7573 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7574 error ("opaque vector types cannot be initialized");
400fbf9f 7575
3e4093b6
RS
7576 p->type = constructor_type;
7577 p->fields = constructor_fields;
7578 p->index = constructor_index;
7579 p->max_index = constructor_max_index;
7580 p->unfilled_index = constructor_unfilled_index;
7581 p->unfilled_fields = constructor_unfilled_fields;
7582 p->bit_index = constructor_bit_index;
7583 p->elements = constructor_elements;
7584 p->constant = constructor_constant;
7585 p->simple = constructor_simple;
928c19bb 7586 p->nonconst = constructor_nonconst;
3e4093b6
RS
7587 p->erroneous = constructor_erroneous;
7588 p->pending_elts = constructor_pending_elts;
7589 p->depth = constructor_depth;
916c5919
JM
7590 p->replacement_value.value = 0;
7591 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7592 p->replacement_value.original_type = NULL;
3e4093b6
RS
7593 p->implicit = 0;
7594 p->range_stack = 0;
7595 p->outer = 0;
7596 p->incremental = constructor_incremental;
7597 p->designated = constructor_designated;
976d5a22 7598 p->designator_depth = designator_depth;
3e4093b6
RS
7599 p->next = 0;
7600 constructor_stack = p;
b13aca19 7601
3e4093b6
RS
7602 constructor_constant = 1;
7603 constructor_simple = 1;
928c19bb 7604 constructor_nonconst = 0;
3e4093b6 7605 constructor_depth = SPELLING_DEPTH ();
9771b263 7606 constructor_elements = NULL;
3e4093b6
RS
7607 constructor_pending_elts = 0;
7608 constructor_type = type;
7609 constructor_incremental = 1;
7610 constructor_designated = 0;
9bac5cbb 7611 constructor_zeroinit = 1;
3e4093b6 7612 designator_depth = 0;
b06df647 7613 designator_erroneous = 0;
400fbf9f 7614
3e636daf 7615 if (RECORD_OR_UNION_TYPE_P (constructor_type))
400fbf9f 7616 {
3e4093b6
RS
7617 constructor_fields = TYPE_FIELDS (constructor_type);
7618 /* Skip any nameless bit fields at the beginning. */
7619 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7620 && DECL_NAME (constructor_fields) == 0)
910ad8de 7621 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7622
3e4093b6
RS
7623 constructor_unfilled_fields = constructor_fields;
7624 constructor_bit_index = bitsize_zero_node;
400fbf9f 7625 }
3e4093b6
RS
7626 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7627 {
7628 if (TYPE_DOMAIN (constructor_type))
7629 {
7630 constructor_max_index
7631 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7632
3e4093b6
RS
7633 /* Detect non-empty initializations of zero-length arrays. */
7634 if (constructor_max_index == NULL_TREE
7635 && TYPE_SIZE (constructor_type))
9a9d280e 7636 constructor_max_index = integer_minus_one_node;
400fbf9f 7637
3e4093b6
RS
7638 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7639 to initialize VLAs will cause a proper error; avoid tree
7640 checking errors as well by setting a safe value. */
7641 if (constructor_max_index
7642 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7643 constructor_max_index = integer_minus_one_node;
59c83dbf 7644
3e4093b6
RS
7645 constructor_index
7646 = convert (bitsizetype,
7647 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7648 }
3e4093b6 7649 else
493179da
JM
7650 {
7651 constructor_index = bitsize_zero_node;
7652 constructor_max_index = NULL_TREE;
7653 }
59c83dbf 7654
3e4093b6
RS
7655 constructor_unfilled_index = constructor_index;
7656 }
31521951 7657 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7658 {
7659 /* Vectors are like simple fixed-size arrays. */
7660 constructor_max_index =
c62c040f 7661 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7662 constructor_index = bitsize_zero_node;
3e4093b6
RS
7663 constructor_unfilled_index = constructor_index;
7664 }
7665 else
7666 {
7667 /* Handle the case of int x = {5}; */
7668 constructor_fields = constructor_type;
7669 constructor_unfilled_fields = constructor_type;
7670 }
7671}
7672\f
16595a1f
BS
7673/* Called when we see an open brace for a nested initializer. Finish
7674 off any pending levels with implicit braces. */
7675void
7676finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7677{
7678 while (constructor_stack->implicit)
7679 {
7680 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7681 && constructor_fields == 0)
7682 process_init_element (input_location,
7683 pop_init_level (loc, 1, braced_init_obstack),
7684 true, braced_init_obstack);
7685 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7686 && constructor_max_index
7687 && tree_int_cst_lt (constructor_max_index,
7688 constructor_index))
7689 process_init_element (input_location,
7690 pop_init_level (loc, 1, braced_init_obstack),
7691 true, braced_init_obstack);
7692 else
7693 break;
7694 }
7695}
7696
3e4093b6
RS
7697/* Push down into a subobject, for initialization.
7698 If this is for an explicit set of braces, IMPLICIT is 0.
7699 If it is because the next element belongs at a lower level,
7700 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7701
3e4093b6 7702void
ea58ef42
MP
7703push_init_level (location_t loc, int implicit,
7704 struct obstack *braced_init_obstack)
3e4093b6
RS
7705{
7706 struct constructor_stack *p;
7707 tree value = NULL_TREE;
400fbf9f 7708
3e4093b6
RS
7709 /* Unless this is an explicit brace, we need to preserve previous
7710 content if any. */
7711 if (implicit)
7712 {
3e636daf 7713 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
a1e3b3d9 7714 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7715 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7716 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7717 }
7718
5d038c4c 7719 p = XNEW (struct constructor_stack);
3e4093b6
RS
7720 p->type = constructor_type;
7721 p->fields = constructor_fields;
7722 p->index = constructor_index;
7723 p->max_index = constructor_max_index;
7724 p->unfilled_index = constructor_unfilled_index;
7725 p->unfilled_fields = constructor_unfilled_fields;
7726 p->bit_index = constructor_bit_index;
7727 p->elements = constructor_elements;
7728 p->constant = constructor_constant;
7729 p->simple = constructor_simple;
928c19bb 7730 p->nonconst = constructor_nonconst;
3e4093b6
RS
7731 p->erroneous = constructor_erroneous;
7732 p->pending_elts = constructor_pending_elts;
7733 p->depth = constructor_depth;
916c5919
JM
7734 p->replacement_value.value = 0;
7735 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7736 p->replacement_value.original_type = NULL;
3e4093b6
RS
7737 p->implicit = implicit;
7738 p->outer = 0;
7739 p->incremental = constructor_incremental;
7740 p->designated = constructor_designated;
976d5a22 7741 p->designator_depth = designator_depth;
3e4093b6
RS
7742 p->next = constructor_stack;
7743 p->range_stack = 0;
7744 constructor_stack = p;
400fbf9f 7745
3e4093b6
RS
7746 constructor_constant = 1;
7747 constructor_simple = 1;
928c19bb 7748 constructor_nonconst = 0;
3e4093b6 7749 constructor_depth = SPELLING_DEPTH ();
9771b263 7750 constructor_elements = NULL;
3e4093b6
RS
7751 constructor_incremental = 1;
7752 constructor_designated = 0;
7753 constructor_pending_elts = 0;
7754 if (!implicit)
400fbf9f 7755 {
3e4093b6
RS
7756 p->range_stack = constructor_range_stack;
7757 constructor_range_stack = 0;
7758 designator_depth = 0;
b06df647 7759 designator_erroneous = 0;
3e4093b6 7760 }
400fbf9f 7761
3e4093b6
RS
7762 /* Don't die if an entire brace-pair level is superfluous
7763 in the containing level. */
7764 if (constructor_type == 0)
7765 ;
3e636daf 7766 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
7767 {
7768 /* Don't die if there are extra init elts at the end. */
7769 if (constructor_fields == 0)
7770 constructor_type = 0;
7771 else
400fbf9f 7772 {
3e4093b6
RS
7773 constructor_type = TREE_TYPE (constructor_fields);
7774 push_member_name (constructor_fields);
7775 constructor_depth++;
400fbf9f 7776 }
6a358dcb
MP
7777 /* If upper initializer is designated, then mark this as
7778 designated too to prevent bogus warnings. */
7779 constructor_designated = p->designated;
3e4093b6
RS
7780 }
7781 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7782 {
7783 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7784 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7785 constructor_depth++;
400fbf9f
JW
7786 }
7787
3e4093b6 7788 if (constructor_type == 0)
400fbf9f 7789 {
ea58ef42 7790 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7791 constructor_fields = 0;
7792 constructor_unfilled_fields = 0;
7793 return;
400fbf9f
JW
7794 }
7795
3e4093b6
RS
7796 if (value && TREE_CODE (value) == CONSTRUCTOR)
7797 {
7798 constructor_constant = TREE_CONSTANT (value);
7799 constructor_simple = TREE_STATIC (value);
928c19bb 7800 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7801 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7802 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7803 && (TREE_CODE (constructor_type) == RECORD_TYPE
7804 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7805 set_nonincremental_init (braced_init_obstack);
3e4093b6 7806 }
400fbf9f 7807
9bac5cbb
G
7808 if (implicit == 1)
7809 found_missing_braces = 1;
400fbf9f 7810
3e636daf 7811 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
7812 {
7813 constructor_fields = TYPE_FIELDS (constructor_type);
7814 /* Skip any nameless bit fields at the beginning. */
7815 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7816 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7817 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7818
3e4093b6
RS
7819 constructor_unfilled_fields = constructor_fields;
7820 constructor_bit_index = bitsize_zero_node;
7821 }
31521951 7822 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7823 {
7824 /* Vectors are like simple fixed-size arrays. */
7825 constructor_max_index =
c62c040f
RG
7826 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7827 constructor_index = bitsize_int (0);
3e4093b6
RS
7828 constructor_unfilled_index = constructor_index;
7829 }
7830 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7831 {
7832 if (TYPE_DOMAIN (constructor_type))
7833 {
7834 constructor_max_index
7835 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7836
3e4093b6
RS
7837 /* Detect non-empty initializations of zero-length arrays. */
7838 if (constructor_max_index == NULL_TREE
7839 && TYPE_SIZE (constructor_type))
9a9d280e 7840 constructor_max_index = integer_minus_one_node;
de520661 7841
3e4093b6
RS
7842 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7843 to initialize VLAs will cause a proper error; avoid tree
7844 checking errors as well by setting a safe value. */
7845 if (constructor_max_index
7846 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7847 constructor_max_index = integer_minus_one_node;
b62acd60 7848
3e4093b6
RS
7849 constructor_index
7850 = convert (bitsizetype,
7851 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7852 }
7853 else
7854 constructor_index = bitsize_zero_node;
de520661 7855
3e4093b6
RS
7856 constructor_unfilled_index = constructor_index;
7857 if (value && TREE_CODE (value) == STRING_CST)
7858 {
7859 /* We need to split the char/wchar array into individual
7860 characters, so that we don't have to special case it
7861 everywhere. */
a1e3b3d9 7862 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7863 }
7864 }
7865 else
7866 {
b4519d39 7867 if (constructor_type != error_mark_node)
96b40f8d 7868 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7869 constructor_fields = constructor_type;
7870 constructor_unfilled_fields = constructor_type;
7871 }
7872}
8b6a5902 7873
3e4093b6 7874/* At the end of an implicit or explicit brace level,
916c5919
JM
7875 finish up that level of constructor. If a single expression
7876 with redundant braces initialized that level, return the
7877 c_expr structure for that expression. Otherwise, the original_code
7878 element is set to ERROR_MARK.
7879 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7880 from inner levels (process_init_element ignores that),
916c5919 7881 but return error_mark_node as the value from the outermost level
3e4093b6 7882 (that's what we want to put in DECL_INITIAL).
916c5919 7883 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7884
916c5919 7885struct c_expr
ea58ef42
MP
7886pop_init_level (location_t loc, int implicit,
7887 struct obstack *braced_init_obstack)
3e4093b6
RS
7888{
7889 struct constructor_stack *p;
916c5919
JM
7890 struct c_expr ret;
7891 ret.value = 0;
7892 ret.original_code = ERROR_MARK;
6866c6e8 7893 ret.original_type = NULL;
de520661 7894
3e4093b6
RS
7895 if (implicit == 0)
7896 {
7897 /* When we come to an explicit close brace,
7898 pop any inner levels that didn't have explicit braces. */
7899 while (constructor_stack->implicit)
34cf811f 7900 process_init_element (input_location,
ea58ef42 7901 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7902 true, braced_init_obstack);
366de0ce 7903 gcc_assert (!constructor_range_stack);
3e4093b6 7904 }
e5e809f4 7905
0066ef9c
RH
7906 /* Now output all pending elements. */
7907 constructor_incremental = 1;
a1e3b3d9 7908 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7909
3e4093b6 7910 p = constructor_stack;
e5e809f4 7911
3e4093b6
RS
7912 /* Error for initializing a flexible array member, or a zero-length
7913 array member in an inappropriate context. */
7914 if (constructor_type && constructor_fields
7915 && TREE_CODE (constructor_type) == ARRAY_TYPE
7916 && TYPE_DOMAIN (constructor_type)
3f75a254 7917 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7918 {
7919 /* Silently discard empty initializations. The parser will
7920 already have pedwarned for empty brackets. */
7921 if (integer_zerop (constructor_unfilled_index))
7922 constructor_type = NULL_TREE;
366de0ce 7923 else
3e4093b6 7924 {
366de0ce 7925 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7926
3e4093b6 7927 if (constructor_depth > 2)
ea58ef42 7928 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7929 else
d033409e 7930 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7931 "initialization of a flexible array member");
de520661 7932
3e4093b6
RS
7933 /* We have already issued an error message for the existence
7934 of a flexible array member not at the end of the structure.
535a42b1 7935 Discard the initializer so that we do not die later. */
910ad8de 7936 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7937 constructor_type = NULL_TREE;
7938 }
3e4093b6 7939 }
de520661 7940
7b33f0c8
MP
7941 switch (vec_safe_length (constructor_elements))
7942 {
7943 case 0:
7944 /* Initialization with { } counts as zeroinit. */
7945 constructor_zeroinit = 1;
7946 break;
7947 case 1:
7948 /* This might be zeroinit as well. */
7949 if (integer_zerop ((*constructor_elements)[0].value))
7950 constructor_zeroinit = 1;
7951 break;
7952 default:
7953 /* If the constructor has more than one element, it can't be { 0 }. */
7954 constructor_zeroinit = 0;
7955 break;
7956 }
9bac5cbb
G
7957
7958 /* Warn when some structs are initialized with direct aggregation. */
7959 if (!implicit && found_missing_braces && warn_missing_braces
7960 && !constructor_zeroinit)
7b33f0c8
MP
7961 warning_init (loc, OPT_Wmissing_braces,
7962 "missing braces around initializer");
9bac5cbb 7963
3e4093b6 7964 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7965 if (warn_missing_field_initializers
3e4093b6
RS
7966 && constructor_type
7967 && TREE_CODE (constructor_type) == RECORD_TYPE
7968 && constructor_unfilled_fields)
7969 {
7970 /* Do not warn for flexible array members or zero-length arrays. */
7971 while (constructor_unfilled_fields
3f75a254 7972 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7973 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7974 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7975
49819fef
AM
7976 if (constructor_unfilled_fields
7977 /* Do not warn if this level of the initializer uses member
7978 designators; it is likely to be deliberate. */
7979 && !constructor_designated
84937de2 7980 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 7981 && !constructor_zeroinit)
3e4093b6 7982 {
32397f22
MLI
7983 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7984 "missing initializer for field %qD of %qT",
7985 constructor_unfilled_fields,
7986 constructor_type))
7987 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7988 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7989 }
7990 }
de520661 7991
3e4093b6 7992 /* Pad out the end of the structure. */
916c5919 7993 if (p->replacement_value.value)
3e4093b6
RS
7994 /* If this closes a superfluous brace pair,
7995 just pass out the element between them. */
916c5919 7996 ret = p->replacement_value;
3e4093b6
RS
7997 else if (constructor_type == 0)
7998 ;
3e636daf 7999 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 8000 && TREE_CODE (constructor_type) != ARRAY_TYPE
31521951 8001 && !VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
8002 {
8003 /* A nonincremental scalar initializer--just return
8004 the element, after verifying there is just one. */
9771b263 8005 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
8006 {
8007 if (!constructor_erroneous)
ea58ef42 8008 error_init (loc, "empty scalar initializer");
916c5919 8009 ret.value = error_mark_node;
3e4093b6 8010 }
9771b263 8011 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 8012 {
ea58ef42 8013 error_init (loc, "extra elements in scalar initializer");
9771b263 8014 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
8015 }
8016 else
9771b263 8017 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
8018 }
8019 else
8020 {
8021 if (constructor_erroneous)
916c5919 8022 ret.value = error_mark_node;
3e4093b6
RS
8023 else
8024 {
916c5919 8025 ret.value = build_constructor (constructor_type,
4038c495 8026 constructor_elements);
3e4093b6 8027 if (constructor_constant)
51eed280 8028 TREE_CONSTANT (ret.value) = 1;
3e4093b6 8029 if (constructor_constant && constructor_simple)
916c5919 8030 TREE_STATIC (ret.value) = 1;
928c19bb
JM
8031 if (constructor_nonconst)
8032 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
8033 }
8034 }
de520661 8035
928c19bb
JM
8036 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8037 {
8038 if (constructor_nonconst)
8039 ret.original_code = C_MAYBE_CONST_EXPR;
8040 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8041 ret.original_code = ERROR_MARK;
8042 }
8043
3e4093b6
RS
8044 constructor_type = p->type;
8045 constructor_fields = p->fields;
8046 constructor_index = p->index;
8047 constructor_max_index = p->max_index;
8048 constructor_unfilled_index = p->unfilled_index;
8049 constructor_unfilled_fields = p->unfilled_fields;
8050 constructor_bit_index = p->bit_index;
8051 constructor_elements = p->elements;
8052 constructor_constant = p->constant;
8053 constructor_simple = p->simple;
928c19bb 8054 constructor_nonconst = p->nonconst;
3e4093b6
RS
8055 constructor_erroneous = p->erroneous;
8056 constructor_incremental = p->incremental;
8057 constructor_designated = p->designated;
976d5a22 8058 designator_depth = p->designator_depth;
3e4093b6
RS
8059 constructor_pending_elts = p->pending_elts;
8060 constructor_depth = p->depth;
8061 if (!p->implicit)
8062 constructor_range_stack = p->range_stack;
8063 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 8064
3e4093b6
RS
8065 constructor_stack = p->next;
8066 free (p);
b621a4dd 8067
5d5e98dc
VR
8068 if (ret.value == 0 && constructor_stack == 0)
8069 ret.value = error_mark_node;
916c5919 8070 return ret;
3e4093b6 8071}
8b6a5902 8072
3e4093b6
RS
8073/* Common handling for both array range and field name designators.
8074 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 8075
3e4093b6 8076static int
ea58ef42
MP
8077set_designator (location_t loc, int array,
8078 struct obstack *braced_init_obstack)
de520661 8079{
3e4093b6
RS
8080 tree subtype;
8081 enum tree_code subcode;
de520661 8082
3e4093b6
RS
8083 /* Don't die if an entire brace-pair level is superfluous
8084 in the containing level. */
8085 if (constructor_type == 0)
8086 return 1;
de520661 8087
366de0ce
NS
8088 /* If there were errors in this designator list already, bail out
8089 silently. */
b06df647 8090 if (designator_erroneous)
3e4093b6 8091 return 1;
e28cae4f 8092
3e4093b6
RS
8093 if (!designator_depth)
8094 {
366de0ce 8095 gcc_assert (!constructor_range_stack);
de520661 8096
3e4093b6
RS
8097 /* Designator list starts at the level of closest explicit
8098 braces. */
8099 while (constructor_stack->implicit)
34cf811f 8100 process_init_element (input_location,
ea58ef42 8101 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 8102 true, braced_init_obstack);
3e4093b6
RS
8103 constructor_designated = 1;
8104 return 0;
8105 }
de520661 8106
366de0ce 8107 switch (TREE_CODE (constructor_type))
3c3fa147 8108 {
366de0ce
NS
8109 case RECORD_TYPE:
8110 case UNION_TYPE:
3e4093b6
RS
8111 subtype = TREE_TYPE (constructor_fields);
8112 if (subtype != error_mark_node)
8113 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
8114 break;
8115 case ARRAY_TYPE:
3e4093b6 8116 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
8117 break;
8118 default:
8119 gcc_unreachable ();
de520661 8120 }
400fbf9f 8121
3e4093b6
RS
8122 subcode = TREE_CODE (subtype);
8123 if (array && subcode != ARRAY_TYPE)
8124 {
ea58ef42 8125 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
8126 return 1;
8127 }
8128 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8129 {
ea58ef42 8130 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
8131 return 1;
8132 }
d45cf215 8133
3e4093b6 8134 constructor_designated = 1;
16595a1f 8135 finish_implicit_inits (loc, braced_init_obstack);
ea58ef42 8136 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 8137 return 0;
de520661 8138}
400fbf9f 8139
3e4093b6
RS
8140/* If there are range designators in designator list, push a new designator
8141 to constructor_range_stack. RANGE_END is end of such stack range or
8142 NULL_TREE if there is no range designator at this level. */
400fbf9f 8143
3e4093b6 8144static void
a1e3b3d9 8145push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
8146{
8147 struct constructor_range_stack *p;
400fbf9f 8148
a1e3b3d9
LB
8149 p = (struct constructor_range_stack *)
8150 obstack_alloc (braced_init_obstack,
8151 sizeof (struct constructor_range_stack));
3e4093b6
RS
8152 p->prev = constructor_range_stack;
8153 p->next = 0;
8154 p->fields = constructor_fields;
8155 p->range_start = constructor_index;
8156 p->index = constructor_index;
8157 p->stack = constructor_stack;
8158 p->range_end = range_end;
8b6a5902 8159 if (constructor_range_stack)
3e4093b6
RS
8160 constructor_range_stack->next = p;
8161 constructor_range_stack = p;
de520661 8162}
400fbf9f 8163
3e4093b6
RS
8164/* Within an array initializer, specify the next index to be initialized.
8165 FIRST is that index. If LAST is nonzero, then initialize a range
8166 of indices, running from FIRST through LAST. */
5a7ec9d9 8167
de520661 8168void
ea58ef42 8169set_init_index (location_t loc, tree first, tree last,
d033409e 8170 struct obstack *braced_init_obstack)
de520661 8171{
ea58ef42 8172 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 8173 return;
de520661 8174
b06df647 8175 designator_erroneous = 1;
de520661 8176
3ea8cd06
JM
8177 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8178 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8179 {
ea58ef42 8180 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
8181 return;
8182 }
8183
2b6da65c
JM
8184 if (TREE_CODE (first) != INTEGER_CST)
8185 {
8186 first = c_fully_fold (first, false, NULL);
8187 if (TREE_CODE (first) == INTEGER_CST)
d033409e 8188 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8189 "array index in initializer is not "
8190 "an integer constant expression");
8191 }
8192
8193 if (last && TREE_CODE (last) != INTEGER_CST)
8194 {
8195 last = c_fully_fold (last, false, NULL);
8196 if (TREE_CODE (last) == INTEGER_CST)
d033409e 8197 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8198 "array index in initializer is not "
8199 "an integer constant expression");
8200 }
8201
3e4093b6 8202 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 8203 error_init (loc, "nonconstant array index in initializer");
3e4093b6 8204 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 8205 error_init (loc, "nonconstant array index in initializer");
3e4093b6 8206 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 8207 error_init (loc, "array index in non-array initializer");
622adc7e 8208 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 8209 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
8210 else if (constructor_max_index
8211 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 8212 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 8213 else
de520661 8214 {
928c19bb
JM
8215 constant_expression_warning (first);
8216 if (last)
8217 constant_expression_warning (last);
3e4093b6 8218 constructor_index = convert (bitsizetype, first);
40d3d530
JR
8219 if (tree_int_cst_lt (constructor_index, first))
8220 {
8221 constructor_index = copy_node (constructor_index);
8222 TREE_OVERFLOW (constructor_index) = 1;
8223 }
665f2503 8224
3e4093b6 8225 if (last)
2bede729 8226 {
3e4093b6
RS
8227 if (tree_int_cst_equal (first, last))
8228 last = 0;
8229 else if (tree_int_cst_lt (last, first))
8230 {
ea58ef42 8231 error_init (loc, "empty index range in initializer");
3e4093b6
RS
8232 last = 0;
8233 }
8234 else
8235 {
8236 last = convert (bitsizetype, last);
8237 if (constructor_max_index != 0
8238 && tree_int_cst_lt (constructor_max_index, last))
8239 {
ea58ef42
MP
8240 error_init (loc, "array index range in initializer exceeds "
8241 "array bounds");
3e4093b6
RS
8242 last = 0;
8243 }
8244 }
2bede729 8245 }
fed3cef0 8246
3e4093b6 8247 designator_depth++;
b06df647 8248 designator_erroneous = 0;
3e4093b6 8249 if (constructor_range_stack || last)
a1e3b3d9 8250 push_range_stack (last, braced_init_obstack);
de520661 8251 }
de520661 8252}
3e4093b6
RS
8253
8254/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 8255
de520661 8256void
f7e4f2e3 8257set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
ea58ef42 8258 struct obstack *braced_init_obstack)
de520661 8259{
0fb96aa4 8260 tree field;
94ba5069 8261
ea58ef42 8262 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
8263 return;
8264
b06df647 8265 designator_erroneous = 1;
3e4093b6 8266
3e636daf 8267 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
94ba5069 8268 {
ea58ef42 8269 error_init (loc, "field name not in record or union initializer");
3e4093b6 8270 return;
94ba5069
RS
8271 }
8272
0fb96aa4 8273 field = lookup_field (constructor_type, fieldname);
8b6a5902 8274
0fb96aa4 8275 if (field == 0)
f7e4f2e3
DM
8276 {
8277 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8278 if (guessed_id)
8279 {
264757fb
DM
8280 gcc_rich_location rich_loc (fieldname_loc);
8281 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
f7e4f2e3
DM
8282 error_at_rich_loc
8283 (&rich_loc,
8284 "%qT has no member named %qE; did you mean %qE?",
8285 constructor_type, fieldname, guessed_id);
8286 }
8287 else
8288 error_at (fieldname_loc, "%qT has no member named %qE",
8289 constructor_type, fieldname);
8290 }
3e4093b6 8291 else
0fb96aa4
JM
8292 do
8293 {
8294 constructor_fields = TREE_VALUE (field);
8295 designator_depth++;
8296 designator_erroneous = 0;
8297 if (constructor_range_stack)
8298 push_range_stack (NULL_TREE, braced_init_obstack);
8299 field = TREE_CHAIN (field);
8300 if (field)
8301 {
ea58ef42 8302 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
8303 return;
8304 }
8305 }
8306 while (field != NULL_TREE);
3e4093b6
RS
8307}
8308\f
8309/* Add a new initializer to the tree of pending initializers. PURPOSE
8310 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
8311 VALUE is the value of that index or field. If ORIGTYPE is not
8312 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
8313
8314 IMPLICIT is true if value comes from pop_init_level (1),
8315 the new initializer has been merged with the existing one
8316 and thus no warnings should be emitted about overriding an
8317 existing initializer. */
de520661 8318
3e4093b6 8319static void
96b40f8d
MP
8320add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8321 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
8322{
8323 struct init_node *p, **q, *r;
8324
8325 q = &constructor_pending_elts;
8326 p = 0;
8327
8328 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8329 {
3e4093b6 8330 while (*q != 0)
91fa3c30 8331 {
3e4093b6
RS
8332 p = *q;
8333 if (tree_int_cst_lt (purpose, p->purpose))
8334 q = &p->left;
8335 else if (tree_int_cst_lt (p->purpose, purpose))
8336 q = &p->right;
8337 else
8338 {
b295aee2
JJ
8339 if (!implicit)
8340 {
8341 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8342 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8343 "initialized field with side-effects "
8344 "overwritten");
b295aee2 8345 else if (warn_override_init)
96b40f8d
MP
8346 warning_init (loc, OPT_Woverride_init,
8347 "initialized field overwritten");
b295aee2 8348 }
3e4093b6 8349 p->value = value;
bbbbb16a 8350 p->origtype = origtype;
3e4093b6
RS
8351 return;
8352 }
91fa3c30 8353 }
de520661 8354 }
3e4093b6 8355 else
de520661 8356 {
3e4093b6 8357 tree bitpos;
400fbf9f 8358
3e4093b6
RS
8359 bitpos = bit_position (purpose);
8360 while (*q != NULL)
8361 {
8362 p = *q;
8363 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8364 q = &p->left;
8365 else if (p->purpose != purpose)
8366 q = &p->right;
8367 else
8368 {
b295aee2
JJ
8369 if (!implicit)
8370 {
8371 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8372 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8373 "initialized field with side-effects "
8374 "overwritten");
b295aee2 8375 else if (warn_override_init)
96b40f8d
MP
8376 warning_init (loc, OPT_Woverride_init,
8377 "initialized field overwritten");
b295aee2 8378 }
3e4093b6 8379 p->value = value;
bbbbb16a 8380 p->origtype = origtype;
3e4093b6
RS
8381 return;
8382 }
8383 }
91fa3c30 8384 }
b71c7f8a 8385
a1e3b3d9
LB
8386 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8387 sizeof (struct init_node));
3e4093b6
RS
8388 r->purpose = purpose;
8389 r->value = value;
bbbbb16a 8390 r->origtype = origtype;
8b6a5902 8391
3e4093b6
RS
8392 *q = r;
8393 r->parent = p;
8394 r->left = 0;
8395 r->right = 0;
8396 r->balance = 0;
b71c7f8a 8397
3e4093b6 8398 while (p)
de520661 8399 {
3e4093b6 8400 struct init_node *s;
665f2503 8401
3e4093b6 8402 if (r == p->left)
2bede729 8403 {
3e4093b6
RS
8404 if (p->balance == 0)
8405 p->balance = -1;
8406 else if (p->balance < 0)
8407 {
8408 if (r->balance < 0)
8409 {
8410 /* L rotation. */
8411 p->left = r->right;
8412 if (p->left)
8413 p->left->parent = p;
8414 r->right = p;
e7b6a0ee 8415
3e4093b6
RS
8416 p->balance = 0;
8417 r->balance = 0;
39bc99c2 8418
3e4093b6
RS
8419 s = p->parent;
8420 p->parent = r;
8421 r->parent = s;
8422 if (s)
8423 {
8424 if (s->left == p)
8425 s->left = r;
8426 else
8427 s->right = r;
8428 }
8429 else
8430 constructor_pending_elts = r;
8431 }
8432 else
8433 {
8434 /* LR rotation. */
8435 struct init_node *t = r->right;
e7b6a0ee 8436
3e4093b6
RS
8437 r->right = t->left;
8438 if (r->right)
8439 r->right->parent = r;
8440 t->left = r;
8441
8442 p->left = t->right;
8443 if (p->left)
8444 p->left->parent = p;
8445 t->right = p;
8446
8447 p->balance = t->balance < 0;
8448 r->balance = -(t->balance > 0);
8449 t->balance = 0;
8450
8451 s = p->parent;
8452 p->parent = t;
8453 r->parent = t;
8454 t->parent = s;
8455 if (s)
8456 {
8457 if (s->left == p)
8458 s->left = t;
8459 else
8460 s->right = t;
8461 }
8462 else
8463 constructor_pending_elts = t;
8464 }
8465 break;
8466 }
8467 else
8468 {
8469 /* p->balance == +1; growth of left side balances the node. */
8470 p->balance = 0;
8471 break;
8472 }
2bede729 8473 }
3e4093b6
RS
8474 else /* r == p->right */
8475 {
8476 if (p->balance == 0)
8477 /* Growth propagation from right side. */
8478 p->balance++;
8479 else if (p->balance > 0)
8480 {
8481 if (r->balance > 0)
8482 {
8483 /* R rotation. */
8484 p->right = r->left;
8485 if (p->right)
8486 p->right->parent = p;
8487 r->left = p;
8488
8489 p->balance = 0;
8490 r->balance = 0;
8491
8492 s = p->parent;
8493 p->parent = r;
8494 r->parent = s;
8495 if (s)
8496 {
8497 if (s->left == p)
8498 s->left = r;
8499 else
8500 s->right = r;
8501 }
8502 else
8503 constructor_pending_elts = r;
8504 }
8505 else /* r->balance == -1 */
8506 {
8507 /* RL rotation */
8508 struct init_node *t = r->left;
8509
8510 r->left = t->right;
8511 if (r->left)
8512 r->left->parent = r;
8513 t->right = r;
8514
8515 p->right = t->left;
8516 if (p->right)
8517 p->right->parent = p;
8518 t->left = p;
8519
8520 r->balance = (t->balance < 0);
8521 p->balance = -(t->balance > 0);
8522 t->balance = 0;
8523
8524 s = p->parent;
8525 p->parent = t;
8526 r->parent = t;
8527 t->parent = s;
8528 if (s)
8529 {
8530 if (s->left == p)
8531 s->left = t;
8532 else
8533 s->right = t;
8534 }
8535 else
8536 constructor_pending_elts = t;
8537 }
8538 break;
8539 }
8540 else
8541 {
8542 /* p->balance == -1; growth of right side balances the node. */
8543 p->balance = 0;
8544 break;
8545 }
8546 }
8547
8548 r = p;
8549 p = p->parent;
8550 }
8551}
8552
8553/* Build AVL tree from a sorted chain. */
8554
8555static void
a1e3b3d9 8556set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8557{
4038c495
GB
8558 unsigned HOST_WIDE_INT ix;
8559 tree index, value;
3e4093b6
RS
8560
8561 if (TREE_CODE (constructor_type) != RECORD_TYPE
8562 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8563 return;
8564
4038c495 8565 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8566 add_pending_init (input_location, index, value, NULL_TREE, true,
8567 braced_init_obstack);
9771b263 8568 constructor_elements = NULL;
3e4093b6
RS
8569 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8570 {
8571 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8572 /* Skip any nameless bit fields at the beginning. */
8573 while (constructor_unfilled_fields != 0
8574 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8575 && DECL_NAME (constructor_unfilled_fields) == 0)
8576 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8577
de520661 8578 }
3e4093b6 8579 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8580 {
3e4093b6
RS
8581 if (TYPE_DOMAIN (constructor_type))
8582 constructor_unfilled_index
8583 = convert (bitsizetype,
8584 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8585 else
8586 constructor_unfilled_index = bitsize_zero_node;
de520661 8587 }
3e4093b6 8588 constructor_incremental = 0;
de520661 8589}
400fbf9f 8590
3e4093b6 8591/* Build AVL tree from a string constant. */
de520661 8592
3e4093b6 8593static void
a1e3b3d9
LB
8594set_nonincremental_init_from_string (tree str,
8595 struct obstack * braced_init_obstack)
de520661 8596{
3e4093b6
RS
8597 tree value, purpose, type;
8598 HOST_WIDE_INT val[2];
8599 const char *p, *end;
8600 int byte, wchar_bytes, charwidth, bitpos;
de520661 8601
366de0ce 8602 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8603
c466b2cd 8604 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6 8605 charwidth = TYPE_PRECISION (char_type_node);
2f1364c2
JJ
8606 gcc_assert ((size_t) wchar_bytes * charwidth
8607 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
3e4093b6
RS
8608 type = TREE_TYPE (constructor_type);
8609 p = TREE_STRING_POINTER (str);
8610 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8611
3e4093b6 8612 for (purpose = bitsize_zero_node;
8824edff
JJ
8613 p < end
8614 && !(constructor_max_index
8615 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8616 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8617 {
3e4093b6 8618 if (wchar_bytes == 1)
ffc5c6a9 8619 {
807e902e
KZ
8620 val[0] = (unsigned char) *p++;
8621 val[1] = 0;
ffc5c6a9
RH
8622 }
8623 else
3e4093b6 8624 {
3e4093b6 8625 val[1] = 0;
807e902e 8626 val[0] = 0;
3e4093b6
RS
8627 for (byte = 0; byte < wchar_bytes; byte++)
8628 {
8629 if (BYTES_BIG_ENDIAN)
8630 bitpos = (wchar_bytes - byte - 1) * charwidth;
8631 else
8632 bitpos = byte * charwidth;
2f1364c2 8633 val[bitpos / HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8634 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8635 << (bitpos % HOST_BITS_PER_WIDE_INT);
8636 }
8637 }
584ef5fe 8638
8df83eae 8639 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8640 {
8641 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8642 if (bitpos < HOST_BITS_PER_WIDE_INT)
8643 {
e3fe09c1 8644 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
3e4093b6 8645 {
aa256c4a 8646 val[0] |= HOST_WIDE_INT_M1U << bitpos;
807e902e 8647 val[1] = -1;
3e4093b6
RS
8648 }
8649 }
8650 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8651 {
807e902e
KZ
8652 if (val[0] < 0)
8653 val[1] = -1;
3e4093b6 8654 }
e3fe09c1 8655 else if (val[1] & (HOST_WIDE_INT_1
3e4093b6 8656 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
aa256c4a 8657 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
3e4093b6 8658 }
ffc5c6a9 8659
807e902e
KZ
8660 value = wide_int_to_tree (type,
8661 wide_int::from_array (val, 2,
8662 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8663 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8664 braced_init_obstack);
9dfcc8db
BH
8665 }
8666
3e4093b6
RS
8667 constructor_incremental = 0;
8668}
de520661 8669
3e4093b6
RS
8670/* Return value of FIELD in pending initializer or zero if the field was
8671 not initialized yet. */
8672
8673static tree
a1e3b3d9 8674find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8675{
8676 struct init_node *p;
8677
8678 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8679 {
3e4093b6
RS
8680 if (constructor_incremental
8681 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8682 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8683
8684 p = constructor_pending_elts;
8685 while (p)
19d76e60 8686 {
3e4093b6
RS
8687 if (tree_int_cst_lt (field, p->purpose))
8688 p = p->left;
8689 else if (tree_int_cst_lt (p->purpose, field))
8690 p = p->right;
8691 else
8692 return p->value;
19d76e60 8693 }
19d76e60 8694 }
3e4093b6 8695 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8696 {
3e4093b6 8697 tree bitpos = bit_position (field);
de520661 8698
3e4093b6
RS
8699 if (constructor_incremental
8700 && (!constructor_unfilled_fields
8701 || tree_int_cst_lt (bitpos,
8702 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8703 set_nonincremental_init (braced_init_obstack);
de520661 8704
3e4093b6
RS
8705 p = constructor_pending_elts;
8706 while (p)
8707 {
8708 if (field == p->purpose)
8709 return p->value;
8710 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8711 p = p->left;
8712 else
8713 p = p->right;
8714 }
8715 }
8716 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8717 {
9771b263
DN
8718 if (!vec_safe_is_empty (constructor_elements)
8719 && (constructor_elements->last ().index == field))
8720 return constructor_elements->last ().value;
de520661 8721 }
3e4093b6 8722 return 0;
de520661
RS
8723}
8724
3e4093b6
RS
8725/* "Output" the next constructor element.
8726 At top level, really output it to assembler code now.
8727 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8728 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8729 TYPE is the data type that the containing data type wants here.
8730 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8731 If VALUE is a string constant, STRICT_STRING is true if it is
8732 unparenthesized or we should not warn here for it being parenthesized.
8733 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8734
3e4093b6
RS
8735 PENDING if non-nil means output pending elements that belong
8736 right after this element. (PENDING is normally 1;
b295aee2
JJ
8737 it is 0 while outputting pending elements, to avoid recursion.)
8738
8739 IMPLICIT is true if value comes from pop_init_level (1),
8740 the new initializer has been merged with the existing one
8741 and thus no warnings should be emitted about overriding an
8742 existing initializer. */
8b6a5902 8743
3e4093b6 8744static void
34cf811f
MP
8745output_init_element (location_t loc, tree value, tree origtype,
8746 bool strict_string, tree type, tree field, int pending,
8747 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8748{
8ce94e44 8749 tree semantic_type = NULL_TREE;
928c19bb
JM
8750 bool maybe_const = true;
8751 bool npc;
4038c495 8752
0a880880 8753 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8754 {
3e4093b6
RS
8755 constructor_erroneous = 1;
8756 return;
8b6a5902 8757 }
46bdb9cf
JM
8758 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8759 && (TREE_CODE (value) == STRING_CST
8760 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8761 && !(TREE_CODE (value) == STRING_CST
8762 && TREE_CODE (type) == ARRAY_TYPE
8763 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8764 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8765 TYPE_MAIN_VARIANT (type)))
c2255bc4 8766 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8767
3e4093b6 8768 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8769 && require_constant_value && pending)
8b6a5902 8770 {
3e4093b6
RS
8771 /* As an extension, allow initializing objects with static storage
8772 duration with compound literals (which are then treated just as
8773 the brace enclosed list they contain). */
4435bb92
MP
8774 if (flag_isoc99)
8775 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8776 "constant");
3e4093b6
RS
8777 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8778 value = DECL_INITIAL (decl);
8b6a5902
JJ
8779 }
8780
928c19bb 8781 npc = null_pointer_constant_p (value);
8ce94e44
JM
8782 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8783 {
8784 semantic_type = TREE_TYPE (value);
8785 value = TREE_OPERAND (value, 0);
8786 }
928c19bb
JM
8787 value = c_fully_fold (value, require_constant_value, &maybe_const);
8788
3e4093b6
RS
8789 if (value == error_mark_node)
8790 constructor_erroneous = 1;
8791 else if (!TREE_CONSTANT (value))
8792 constructor_constant = 0;
ee45a32d
EB
8793 else if (!initializer_constant_valid_p (value,
8794 TREE_TYPE (value),
8795 AGGREGATE_TYPE_P (constructor_type)
8796 && TYPE_REVERSE_STORAGE_ORDER
8797 (constructor_type))
3e636daf 8798 || (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6
RS
8799 && DECL_C_BIT_FIELD (field)
8800 && TREE_CODE (value) != INTEGER_CST))
8801 constructor_simple = 0;
928c19bb
JM
8802 if (!maybe_const)
8803 constructor_nonconst = 1;
3e4093b6 8804
4378d117
MS
8805 /* Digest the initializer and issue any errors about incompatible
8806 types before issuing errors about non-constant initializers. */
8807 tree new_value = value;
8808 if (semantic_type)
8809 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8810 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
8811 require_constant_value);
8812 if (new_value == error_mark_node)
8813 {
8814 constructor_erroneous = 1;
8815 return;
8816 }
8817 if (require_constant_value || require_constant_elements)
8818 constant_expression_warning (new_value);
8819
8820 /* Proceed to check the constness of the original initializer. */
116df786 8821 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8822 {
116df786
RH
8823 if (require_constant_value)
8824 {
ea58ef42 8825 error_init (loc, "initializer element is not constant");
116df786
RH
8826 value = error_mark_node;
8827 }
8828 else if (require_constant_elements)
8337d1db 8829 pedwarn (loc, OPT_Wpedantic,
509c9d60 8830 "initializer element is not computable at load time");
8b6a5902 8831 }
928c19bb
JM
8832 else if (!maybe_const
8833 && (require_constant_value || require_constant_elements))
8337d1db 8834 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8835 "initializer element is not a constant expression");
3e4093b6 8836
81f40b79
ILT
8837 /* Issue -Wc++-compat warnings about initializing a bitfield with
8838 enum type. */
8839 if (warn_cxx_compat
8840 && field != NULL_TREE
8841 && TREE_CODE (field) == FIELD_DECL
8842 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8843 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8844 != TYPE_MAIN_VARIANT (type))
8845 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8846 {
8847 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8848 if (checktype != error_mark_node
8849 && (TYPE_MAIN_VARIANT (checktype)
8850 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8851 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8852 "enum conversion in initialization is invalid in C++");
8853 }
8854
3e4093b6
RS
8855 /* If this field is empty (and not at the end of structure),
8856 don't do anything other than checking the initializer. */
8857 if (field
8858 && (TREE_TYPE (field) == error_mark_node
8859 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8860 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8861 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8862 || DECL_CHAIN (field)))))
3e4093b6
RS
8863 return;
8864
4378d117
MS
8865 /* Finally, set VALUE to the initializer value digested above. */
8866 value = new_value;
8b6a5902 8867
3e4093b6
RS
8868 /* If this element doesn't come next in sequence,
8869 put it on constructor_pending_elts. */
8870 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8871 && (!constructor_incremental
8872 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8873 {
3e4093b6
RS
8874 if (constructor_incremental
8875 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8876 set_nonincremental_init (braced_init_obstack);
3e4093b6 8877
96b40f8d 8878 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8879 braced_init_obstack);
3e4093b6 8880 return;
8b6a5902 8881 }
3e4093b6
RS
8882 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8883 && (!constructor_incremental
8884 || field != constructor_unfilled_fields))
8b6a5902 8885 {
3e4093b6
RS
8886 /* We do this for records but not for unions. In a union,
8887 no matter which field is specified, it can be initialized
8888 right away since it starts at the beginning of the union. */
8889 if (constructor_incremental)
8890 {
8891 if (!constructor_unfilled_fields)
a1e3b3d9 8892 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8893 else
8894 {
8895 tree bitpos, unfillpos;
8896
8897 bitpos = bit_position (field);
8898 unfillpos = bit_position (constructor_unfilled_fields);
8899
8900 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8901 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8902 }
8903 }
8904
96b40f8d 8905 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8906 braced_init_obstack);
3e4093b6 8907 return;
8b6a5902 8908 }
3e4093b6 8909 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8910 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8911 {
b295aee2
JJ
8912 if (!implicit)
8913 {
9771b263 8914 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
755e528f 8915 warning_init (loc, OPT_Woverride_init_side_effects,
b295aee2
JJ
8916 "initialized field with side-effects overwritten");
8917 else if (warn_override_init)
96b40f8d
MP
8918 warning_init (loc, OPT_Woverride_init,
8919 "initialized field overwritten");
b295aee2 8920 }
8b6a5902 8921
3e4093b6 8922 /* We can have just one union field set. */
9771b263 8923 constructor_elements = NULL;
3e4093b6 8924 }
8b6a5902 8925
3e4093b6
RS
8926 /* Otherwise, output this element either to
8927 constructor_elements or to the assembler file. */
8b6a5902 8928
f32682ca 8929 constructor_elt celt = {field, value};
9771b263 8930 vec_safe_push (constructor_elements, celt);
8b6a5902 8931
3e4093b6
RS
8932 /* Advance the variable that indicates sequential elements output. */
8933 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8934 constructor_unfilled_index
db3927fb
AH
8935 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8936 bitsize_one_node);
3e4093b6
RS
8937 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8938 {
8939 constructor_unfilled_fields
910ad8de 8940 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8941
3e4093b6
RS
8942 /* Skip any nameless bit fields. */
8943 while (constructor_unfilled_fields != 0
8944 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8945 && DECL_NAME (constructor_unfilled_fields) == 0)
8946 constructor_unfilled_fields =
910ad8de 8947 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8948 }
8949 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8950 constructor_unfilled_fields = 0;
de520661 8951
3e4093b6
RS
8952 /* Now output any pending elements which have become next. */
8953 if (pending)
a1e3b3d9 8954 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8955}
8b6a5902 8956
3e4093b6
RS
8957/* Output any pending elements which have become next.
8958 As we output elements, constructor_unfilled_{fields,index}
8959 advances, which may cause other elements to become next;
8960 if so, they too are output.
8b6a5902 8961
3e4093b6
RS
8962 If ALL is 0, we return when there are
8963 no more pending elements to output now.
665f2503 8964
3e4093b6
RS
8965 If ALL is 1, we output space as necessary so that
8966 we can output all the pending elements. */
3e4093b6 8967static void
a1e3b3d9 8968output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8969{
8970 struct init_node *elt = constructor_pending_elts;
8971 tree next;
de520661 8972
3e4093b6
RS
8973 retry:
8974
ba228239 8975 /* Look through the whole pending tree.
3e4093b6
RS
8976 If we find an element that should be output now,
8977 output it. Otherwise, set NEXT to the element
8978 that comes first among those still pending. */
8979
8980 next = 0;
8981 while (elt)
8982 {
8983 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8984 {
3e4093b6
RS
8985 if (tree_int_cst_equal (elt->purpose,
8986 constructor_unfilled_index))
34cf811f
MP
8987 output_init_element (input_location, elt->value, elt->origtype,
8988 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8989 constructor_unfilled_index, 0, false,
8990 braced_init_obstack);
3e4093b6
RS
8991 else if (tree_int_cst_lt (constructor_unfilled_index,
8992 elt->purpose))
8b6a5902 8993 {
3e4093b6
RS
8994 /* Advance to the next smaller node. */
8995 if (elt->left)
8996 elt = elt->left;
8997 else
8998 {
8999 /* We have reached the smallest node bigger than the
9000 current unfilled index. Fill the space first. */
9001 next = elt->purpose;
9002 break;
9003 }
8b6a5902 9004 }
ce662d4c
JJ
9005 else
9006 {
3e4093b6
RS
9007 /* Advance to the next bigger node. */
9008 if (elt->right)
9009 elt = elt->right;
9010 else
ce662d4c 9011 {
3e4093b6
RS
9012 /* We have reached the biggest node in a subtree. Find
9013 the parent of it, which is the next bigger node. */
9014 while (elt->parent && elt->parent->right == elt)
9015 elt = elt->parent;
9016 elt = elt->parent;
9017 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9018 elt->purpose))
9019 {
9020 next = elt->purpose;
9021 break;
9022 }
ce662d4c
JJ
9023 }
9024 }
8b6a5902 9025 }
3e636daf 9026 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
9027 {
9028 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 9029
3e4093b6
RS
9030 /* If the current record is complete we are done. */
9031 if (constructor_unfilled_fields == 0)
9032 break;
de520661 9033
3e4093b6
RS
9034 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9035 elt_bitpos = bit_position (elt->purpose);
9036 /* We can't compare fields here because there might be empty
9037 fields in between. */
9038 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9039 {
9040 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
9041 output_init_element (input_location, elt->value, elt->origtype,
9042 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
9043 elt->purpose, 0, false,
9044 braced_init_obstack);
3e4093b6
RS
9045 }
9046 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9047 {
9048 /* Advance to the next smaller node. */
9049 if (elt->left)
9050 elt = elt->left;
9051 else
9052 {
9053 /* We have reached the smallest node bigger than the
9054 current unfilled field. Fill the space first. */
9055 next = elt->purpose;
9056 break;
9057 }
9058 }
9059 else
9060 {
9061 /* Advance to the next bigger node. */
9062 if (elt->right)
9063 elt = elt->right;
9064 else
9065 {
9066 /* We have reached the biggest node in a subtree. Find
9067 the parent of it, which is the next bigger node. */
9068 while (elt->parent && elt->parent->right == elt)
9069 elt = elt->parent;
9070 elt = elt->parent;
9071 if (elt
9072 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9073 bit_position (elt->purpose))))
9074 {
9075 next = elt->purpose;
9076 break;
9077 }
9078 }
9079 }
9080 }
9081 }
de520661 9082
3e4093b6
RS
9083 /* Ordinarily return, but not if we want to output all
9084 and there are elements left. */
3f75a254 9085 if (!(all && next != 0))
e5cfb88f
RK
9086 return;
9087
3e4093b6
RS
9088 /* If it's not incremental, just skip over the gap, so that after
9089 jumping to retry we will output the next successive element. */
3e636daf 9090 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
9091 constructor_unfilled_fields = next;
9092 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9093 constructor_unfilled_index = next;
de520661 9094
3e4093b6
RS
9095 /* ELT now points to the node in the pending tree with the next
9096 initializer to output. */
9097 goto retry;
de520661
RS
9098}
9099\f
3e4093b6
RS
9100/* Add one non-braced element to the current constructor level.
9101 This adjusts the current position within the constructor's type.
9102 This may also start or terminate implicit levels
9103 to handle a partly-braced initializer.
e5e809f4 9104
3e4093b6 9105 Once this has found the correct level for the new element,
b295aee2
JJ
9106 it calls output_init_element.
9107
9108 IMPLICIT is true if value comes from pop_init_level (1),
9109 the new initializer has been merged with the existing one
9110 and thus no warnings should be emitted about overriding an
9111 existing initializer. */
3e4093b6
RS
9112
9113void
34cf811f 9114process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 9115 struct obstack * braced_init_obstack)
e5e809f4 9116{
916c5919
JM
9117 tree orig_value = value.value;
9118 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
9119 bool strict_string = value.original_code == STRING_CST;
340baef7 9120 bool was_designated = designator_depth != 0;
e5e809f4 9121
3e4093b6 9122 designator_depth = 0;
b06df647 9123 designator_erroneous = 0;
e5e809f4 9124
9bac5cbb
G
9125 if (!implicit && value.value && !integer_zerop (value.value))
9126 constructor_zeroinit = 0;
9127
3e4093b6
RS
9128 /* Handle superfluous braces around string cst as in
9129 char x[] = {"foo"}; */
9130 if (string_flag
9131 && constructor_type
340baef7 9132 && !was_designated
3e4093b6 9133 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 9134 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 9135 && integer_zerop (constructor_unfilled_index))
e5e809f4 9136 {
916c5919 9137 if (constructor_stack->replacement_value.value)
ea58ef42 9138 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
9139 constructor_stack->replacement_value = value;
9140 return;
e5e809f4 9141 }
8b6a5902 9142
916c5919 9143 if (constructor_stack->replacement_value.value != 0)
3e4093b6 9144 {
ea58ef42 9145 error_init (loc, "excess elements in struct initializer");
3e4093b6 9146 return;
e5e809f4
JL
9147 }
9148
3e4093b6
RS
9149 /* Ignore elements of a brace group if it is entirely superfluous
9150 and has already been diagnosed. */
9151 if (constructor_type == 0)
9152 return;
e5e809f4 9153
976d5a22
TT
9154 if (!implicit && warn_designated_init && !was_designated
9155 && TREE_CODE (constructor_type) == RECORD_TYPE
9156 && lookup_attribute ("designated_init",
9157 TYPE_ATTRIBUTES (constructor_type)))
9158 warning_init (loc,
9159 OPT_Wdesignated_init,
9160 "positional initialization of field "
9161 "in %<struct%> declared with %<designated_init%> attribute");
9162
3e4093b6
RS
9163 /* If we've exhausted any levels that didn't have braces,
9164 pop them now. */
9165 while (constructor_stack->implicit)
9166 {
3e636daf 9167 if (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 9168 && constructor_fields == 0)
ea58ef42
MP
9169 process_init_element (loc,
9170 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 9171 true, braced_init_obstack);
53650abe 9172 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
31521951 9173 || VECTOR_TYPE_P (constructor_type))
8824edff
JJ
9174 && constructor_max_index
9175 && tree_int_cst_lt (constructor_max_index,
9176 constructor_index))
ea58ef42
MP
9177 process_init_element (loc,
9178 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 9179 true, braced_init_obstack);
3e4093b6
RS
9180 else
9181 break;
9182 }
e5e809f4 9183
3e4093b6
RS
9184 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9185 if (constructor_range_stack)
e5e809f4 9186 {
3e4093b6
RS
9187 /* If value is a compound literal and we'll be just using its
9188 content, don't put it into a SAVE_EXPR. */
916c5919 9189 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
c3e38a03 9190 || !require_constant_value)
8ce94e44
JM
9191 {
9192 tree semantic_type = NULL_TREE;
9193 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9194 {
9195 semantic_type = TREE_TYPE (value.value);
9196 value.value = TREE_OPERAND (value.value, 0);
9197 }
9198 value.value = c_save_expr (value.value);
9199 if (semantic_type)
9200 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9201 value.value);
9202 }
3e4093b6 9203 }
e5e809f4 9204
3e4093b6
RS
9205 while (1)
9206 {
9207 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 9208 {
3e4093b6
RS
9209 tree fieldtype;
9210 enum tree_code fieldcode;
e5e809f4 9211
3e4093b6
RS
9212 if (constructor_fields == 0)
9213 {
ea58ef42 9214 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
9215 break;
9216 }
e5e809f4 9217
3e4093b6
RS
9218 fieldtype = TREE_TYPE (constructor_fields);
9219 if (fieldtype != error_mark_node)
9220 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9221 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9222
3e4093b6
RS
9223 /* Error for non-static initialization of a flexible array member. */
9224 if (fieldcode == ARRAY_TYPE
9225 && !require_constant_value
9226 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 9227 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 9228 {
ea58ef42
MP
9229 error_init (loc, "non-static initialization of a flexible "
9230 "array member");
3e4093b6
RS
9231 break;
9232 }
e5e809f4 9233
2cc901dc
MP
9234 /* Error for initialization of a flexible array member with
9235 a string constant if the structure is in an array. E.g.:
9236 struct S { int x; char y[]; };
9237 struct S s[] = { { 1, "foo" } };
9238 is invalid. */
9239 if (string_flag
9240 && fieldcode == ARRAY_TYPE
9241 && constructor_depth > 1
9242 && TYPE_SIZE (fieldtype) == NULL_TREE
9243 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9244 {
9245 bool in_array_p = false;
9246 for (struct constructor_stack *p = constructor_stack;
9247 p && p->type; p = p->next)
9248 if (TREE_CODE (p->type) == ARRAY_TYPE)
9249 {
9250 in_array_p = true;
9251 break;
9252 }
9253 if (in_array_p)
9254 {
9255 error_init (loc, "initialization of flexible array "
9256 "member in a nested context");
9257 break;
9258 }
9259 }
9260
3e4093b6 9261 /* Accept a string constant to initialize a subarray. */
916c5919 9262 if (value.value != 0
3e4093b6 9263 && fieldcode == ARRAY_TYPE
197463ae 9264 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9265 && string_flag)
916c5919 9266 value.value = orig_value;
3e4093b6
RS
9267 /* Otherwise, if we have come to a subaggregate,
9268 and we don't have an element of its type, push into it. */
0953878d 9269 else if (value.value != 0
916c5919
JM
9270 && value.value != error_mark_node
9271 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9272 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9273 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9274 {
ea58ef42 9275 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9276 continue;
9277 }
e5e809f4 9278
916c5919 9279 if (value.value)
3e4093b6
RS
9280 {
9281 push_member_name (constructor_fields);
34cf811f 9282 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9283 strict_string, fieldtype,
a1e3b3d9
LB
9284 constructor_fields, 1, implicit,
9285 braced_init_obstack);
3e4093b6 9286 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9287 }
9288 else
3e4093b6
RS
9289 /* Do the bookkeeping for an element that was
9290 directly output as a constructor. */
e5e809f4 9291 {
3e4093b6
RS
9292 /* For a record, keep track of end position of last field. */
9293 if (DECL_SIZE (constructor_fields))
c22cacf3 9294 constructor_bit_index
db3927fb
AH
9295 = size_binop_loc (input_location, PLUS_EXPR,
9296 bit_position (constructor_fields),
9297 DECL_SIZE (constructor_fields));
3e4093b6
RS
9298
9299 /* If the current field was the first one not yet written out,
9300 it isn't now, so update. */
9301 if (constructor_unfilled_fields == constructor_fields)
9302 {
910ad8de 9303 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9304 /* Skip any nameless bit fields. */
9305 while (constructor_unfilled_fields != 0
9306 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9307 && DECL_NAME (constructor_unfilled_fields) == 0)
9308 constructor_unfilled_fields =
910ad8de 9309 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 9310 }
e5e809f4 9311 }
3e4093b6 9312
910ad8de 9313 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9314 /* Skip any nameless bit fields at the beginning. */
9315 while (constructor_fields != 0
9316 && DECL_C_BIT_FIELD (constructor_fields)
9317 && DECL_NAME (constructor_fields) == 0)
910ad8de 9318 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9319 }
3e4093b6 9320 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 9321 {
3e4093b6
RS
9322 tree fieldtype;
9323 enum tree_code fieldcode;
e5e809f4 9324
3e4093b6
RS
9325 if (constructor_fields == 0)
9326 {
d033409e 9327 pedwarn_init (loc, 0,
509c9d60 9328 "excess elements in union initializer");
3e4093b6
RS
9329 break;
9330 }
e5e809f4 9331
3e4093b6
RS
9332 fieldtype = TREE_TYPE (constructor_fields);
9333 if (fieldtype != error_mark_node)
9334 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9335 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9336
3e4093b6
RS
9337 /* Warn that traditional C rejects initialization of unions.
9338 We skip the warning if the value is zero. This is done
9339 under the assumption that the zero initializer in user
9340 code appears conditioned on e.g. __STDC__ to avoid
9341 "missing initializer" warnings and relies on default
9342 initialization to zero in the traditional C case.
9343 We also skip the warning if the initializer is designated,
9344 again on the assumption that this must be conditional on
9345 __STDC__ anyway (and we've already complained about the
9346 member-designator already). */
8400e75e 9347 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
9348 && !(value.value && (integer_zerop (value.value)
9349 || real_zerop (value.value))))
3176a0c2
DD
9350 warning (OPT_Wtraditional, "traditional C rejects initialization "
9351 "of unions");
e5e809f4 9352
3e4093b6 9353 /* Accept a string constant to initialize a subarray. */
916c5919 9354 if (value.value != 0
3e4093b6 9355 && fieldcode == ARRAY_TYPE
197463ae 9356 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9357 && string_flag)
916c5919 9358 value.value = orig_value;
3e4093b6
RS
9359 /* Otherwise, if we have come to a subaggregate,
9360 and we don't have an element of its type, push into it. */
0953878d 9361 else if (value.value != 0
916c5919
JM
9362 && value.value != error_mark_node
9363 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9364 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9365 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9366 {
ea58ef42 9367 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9368 continue;
9369 }
e5e809f4 9370
916c5919 9371 if (value.value)
3e4093b6
RS
9372 {
9373 push_member_name (constructor_fields);
34cf811f 9374 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9375 strict_string, fieldtype,
a1e3b3d9
LB
9376 constructor_fields, 1, implicit,
9377 braced_init_obstack);
3e4093b6 9378 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9379 }
9380 else
3e4093b6
RS
9381 /* Do the bookkeeping for an element that was
9382 directly output as a constructor. */
e5e809f4 9383 {
3e4093b6 9384 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 9385 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9386 }
e5e809f4 9387
3e4093b6
RS
9388 constructor_fields = 0;
9389 }
9390 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9391 {
9392 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9393 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 9394
3e4093b6 9395 /* Accept a string constant to initialize a subarray. */
916c5919 9396 if (value.value != 0
3e4093b6 9397 && eltcode == ARRAY_TYPE
197463ae 9398 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 9399 && string_flag)
916c5919 9400 value.value = orig_value;
3e4093b6
RS
9401 /* Otherwise, if we have come to a subaggregate,
9402 and we don't have an element of its type, push into it. */
0953878d 9403 else if (value.value != 0
916c5919
JM
9404 && value.value != error_mark_node
9405 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9406 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9407 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9408 {
ea58ef42 9409 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9410 continue;
9411 }
8b6a5902 9412
3e4093b6
RS
9413 if (constructor_max_index != 0
9414 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9415 || integer_all_onesp (constructor_max_index)))
9416 {
d033409e 9417 pedwarn_init (loc, 0,
509c9d60 9418 "excess elements in array initializer");
3e4093b6
RS
9419 break;
9420 }
8b6a5902 9421
3e4093b6 9422 /* Now output the actual element. */
916c5919 9423 if (value.value)
3e4093b6 9424 {
ae7e9ddd 9425 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9426 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9427 strict_string, elttype,
a1e3b3d9
LB
9428 constructor_index, 1, implicit,
9429 braced_init_obstack);
3e4093b6
RS
9430 RESTORE_SPELLING_DEPTH (constructor_depth);
9431 }
2f6e4e97 9432
3e4093b6 9433 constructor_index
db3927fb
AH
9434 = size_binop_loc (input_location, PLUS_EXPR,
9435 constructor_index, bitsize_one_node);
8b6a5902 9436
916c5919 9437 if (!value.value)
3e4093b6
RS
9438 /* If we are doing the bookkeeping for an element that was
9439 directly output as a constructor, we must update
9440 constructor_unfilled_index. */
9441 constructor_unfilled_index = constructor_index;
9442 }
31521951 9443 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
9444 {
9445 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9446
c22cacf3
MS
9447 /* Do a basic check of initializer size. Note that vectors
9448 always have a fixed size derived from their type. */
3e4093b6
RS
9449 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9450 {
d033409e 9451 pedwarn_init (loc, 0,
509c9d60 9452 "excess elements in vector initializer");
3e4093b6
RS
9453 break;
9454 }
8b6a5902 9455
3e4093b6 9456 /* Now output the actual element. */
916c5919 9457 if (value.value)
53650abe
AP
9458 {
9459 if (TREE_CODE (value.value) == VECTOR_CST)
9460 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9461 output_init_element (loc, value.value, value.original_type,
53650abe 9462 strict_string, elttype,
a1e3b3d9
LB
9463 constructor_index, 1, implicit,
9464 braced_init_obstack);
53650abe 9465 }
8b6a5902 9466
3e4093b6 9467 constructor_index
db3927fb
AH
9468 = size_binop_loc (input_location,
9469 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9470
916c5919 9471 if (!value.value)
3e4093b6
RS
9472 /* If we are doing the bookkeeping for an element that was
9473 directly output as a constructor, we must update
9474 constructor_unfilled_index. */
9475 constructor_unfilled_index = constructor_index;
9476 }
8b6a5902 9477
3e4093b6
RS
9478 /* Handle the sole element allowed in a braced initializer
9479 for a scalar variable. */
b4519d39
SB
9480 else if (constructor_type != error_mark_node
9481 && constructor_fields == 0)
8b6a5902 9482 {
d033409e 9483 pedwarn_init (loc, 0,
509c9d60 9484 "excess elements in scalar initializer");
3e4093b6 9485 break;
8b6a5902
JJ
9486 }
9487 else
9488 {
916c5919 9489 if (value.value)
34cf811f 9490 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9491 strict_string, constructor_type,
a1e3b3d9
LB
9492 NULL_TREE, 1, implicit,
9493 braced_init_obstack);
3e4093b6 9494 constructor_fields = 0;
8b6a5902
JJ
9495 }
9496
3e4093b6
RS
9497 /* Handle range initializers either at this level or anywhere higher
9498 in the designator stack. */
9499 if (constructor_range_stack)
8b6a5902 9500 {
3e4093b6
RS
9501 struct constructor_range_stack *p, *range_stack;
9502 int finish = 0;
9503
9504 range_stack = constructor_range_stack;
9505 constructor_range_stack = 0;
9506 while (constructor_stack != range_stack->stack)
8b6a5902 9507 {
366de0ce 9508 gcc_assert (constructor_stack->implicit);
34cf811f 9509 process_init_element (loc,
ea58ef42
MP
9510 pop_init_level (loc, 1,
9511 braced_init_obstack),
a1e3b3d9 9512 true, braced_init_obstack);
8b6a5902 9513 }
3e4093b6
RS
9514 for (p = range_stack;
9515 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9516 p = p->prev)
8b6a5902 9517 {
366de0ce 9518 gcc_assert (constructor_stack->implicit);
34cf811f 9519 process_init_element (loc,
ea58ef42
MP
9520 pop_init_level (loc, 1,
9521 braced_init_obstack),
a1e3b3d9 9522 true, braced_init_obstack);
8b6a5902 9523 }
3e4093b6 9524
db3927fb
AH
9525 p->index = size_binop_loc (input_location,
9526 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9527 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9528 finish = 1;
9529
9530 while (1)
9531 {
9532 constructor_index = p->index;
9533 constructor_fields = p->fields;
9534 if (finish && p->range_end && p->index == p->range_start)
9535 {
9536 finish = 0;
9537 p->prev = 0;
9538 }
9539 p = p->next;
9540 if (!p)
9541 break;
16595a1f 9542 finish_implicit_inits (loc, braced_init_obstack);
ea58ef42 9543 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9544 p->stack = constructor_stack;
9545 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9546 p->index = p->range_start;
9547 }
9548
9549 if (!finish)
9550 constructor_range_stack = range_stack;
9551 continue;
8b6a5902
JJ
9552 }
9553
3e4093b6 9554 break;
8b6a5902
JJ
9555 }
9556
3e4093b6
RS
9557 constructor_range_stack = 0;
9558}
9559\f
9f0e2d86
ZW
9560/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9561 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9562 an ASM_EXPR node). */
3e4093b6 9563tree
9f0e2d86 9564build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9565{
6de9cd9a
DN
9566 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9567 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9568 return add_stmt (args);
8b6a5902
JJ
9569}
9570
9f0e2d86
ZW
9571/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9572 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9573 SIMPLE indicates whether there was anything at all after the
9574 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9575 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9576tree
c2255bc4 9577build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9578 tree clobbers, tree labels, bool simple)
e5e809f4 9579{
3e4093b6 9580 tree tail;
9f0e2d86 9581 tree args;
6de9cd9a
DN
9582 int i;
9583 const char *constraint;
74f0c611 9584 const char **oconstraints;
6de9cd9a 9585 bool allows_mem, allows_reg, is_inout;
74f0c611 9586 int ninputs, noutputs;
6de9cd9a
DN
9587
9588 ninputs = list_length (inputs);
9589 noutputs = list_length (outputs);
74f0c611
RH
9590 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9591
1c384bf1 9592 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9593
6de9cd9a
DN
9594 /* Remove output conversions that change the type but not the mode. */
9595 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9596 {
3e4093b6 9597 tree output = TREE_VALUE (tail);
74f0c611 9598
eadd3d0d
JJ
9599 output = c_fully_fold (output, false, NULL);
9600
74f0c611
RH
9601 /* ??? Really, this should not be here. Users should be using a
9602 proper lvalue, dammit. But there's a long history of using casts
9603 in the output operands. In cases like longlong.h, this becomes a
9604 primitive form of typechecking -- if the cast can be removed, then
9605 the output operand had a type of the proper width; otherwise we'll
9606 get an error. Gross, but ... */
3e4093b6 9607 STRIP_NOPS (output);
74f0c611 9608
7bd11157 9609 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9610 output = error_mark_node;
8b6a5902 9611
5544530a
PB
9612 if (output != error_mark_node
9613 && (TREE_READONLY (output)
9614 || TYPE_READONLY (TREE_TYPE (output))
3e636daf 9615 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
5544530a 9616 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9617 readonly_error (loc, output, lv_asm);
5544530a 9618
6de9cd9a 9619 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9620 oconstraints[i] = constraint;
9621
9622 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9623 &allows_mem, &allows_reg, &is_inout))
9624 {
9625 /* If the operand is going to end up in memory,
9626 mark it addressable. */
9627 if (!allows_reg && !c_mark_addressable (output))
9628 output = error_mark_node;
bae5cddf
JJ
9629 if (!(!allows_reg && allows_mem)
9630 && output != error_mark_node
9631 && VOID_TYPE_P (TREE_TYPE (output)))
9632 {
9633 error_at (loc, "invalid use of void expression");
9634 output = error_mark_node;
9635 }
74f0c611
RH
9636 }
9637 else
c22cacf3 9638 output = error_mark_node;
3e4093b6 9639
74f0c611 9640 TREE_VALUE (tail) = output;
8b6a5902 9641 }
3e4093b6 9642
74f0c611
RH
9643 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9644 {
9645 tree input;
9646
9647 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9648 input = TREE_VALUE (tail);
9649
74f0c611
RH
9650 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9651 oconstraints, &allows_mem, &allows_reg))
9652 {
9653 /* If the operand is going to end up in memory,
9654 mark it addressable. */
b4c33883
AP
9655 if (!allows_reg && allows_mem)
9656 {
eadd3d0d
JJ
9657 input = c_fully_fold (input, false, NULL);
9658
b4c33883
AP
9659 /* Strip the nops as we allow this case. FIXME, this really
9660 should be rejected or made deprecated. */
9661 STRIP_NOPS (input);
9662 if (!c_mark_addressable (input))
9663 input = error_mark_node;
bae5cddf 9664 }
eadd3d0d 9665 else
bae5cddf 9666 {
eadd3d0d
JJ
9667 struct c_expr expr;
9668 memset (&expr, 0, sizeof (expr));
9669 expr.value = input;
267bac10 9670 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9671 input = c_fully_fold (expr.value, false, NULL);
9672
9673 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9674 {
9675 error_at (loc, "invalid use of void expression");
9676 input = error_mark_node;
9677 }
bae5cddf 9678 }
74f0c611
RH
9679 }
9680 else
9681 input = error_mark_node;
9682
9683 TREE_VALUE (tail) = input;
9684 }
3e4093b6 9685
1c384bf1
RH
9686 /* ASMs with labels cannot have outputs. This should have been
9687 enforced by the parser. */
9688 gcc_assert (outputs == NULL || labels == NULL);
9689
9690 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9691
5544530a
PB
9692 /* asm statements without outputs, including simple ones, are treated
9693 as volatile. */
9694 ASM_INPUT_P (args) = simple;
9695 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9696
9f0e2d86 9697 return args;
e5e809f4 9698}
3e4093b6 9699\f
c2255bc4
AH
9700/* Generate a goto statement to LABEL. LOC is the location of the
9701 GOTO. */
506e2710
RH
9702
9703tree
c2255bc4 9704c_finish_goto_label (location_t loc, tree label)
506e2710 9705{
e1b7793c 9706 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9707 if (!decl)
9708 return NULL_TREE;
506e2710 9709 TREE_USED (decl) = 1;
c2255bc4
AH
9710 {
9711 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9712 SET_EXPR_LOCATION (t, loc);
9713 return add_stmt (t);
9714 }
506e2710
RH
9715}
9716
c2255bc4
AH
9717/* Generate a computed goto statement to EXPR. LOC is the location of
9718 the GOTO. */
506e2710
RH
9719
9720tree
c2255bc4 9721c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9722{
c2255bc4 9723 tree t;
c1771a20 9724 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9725 expr = c_fully_fold (expr, false, NULL);
506e2710 9726 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9727 t = build1 (GOTO_EXPR, void_type_node, expr);
9728 SET_EXPR_LOCATION (t, loc);
9729 return add_stmt (t);
506e2710
RH
9730}
9731
5088b058 9732/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9733 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9734 the location of the return statement, or the location of the expression,
9735 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9736 is the original type of RETVAL. */
de520661 9737
506e2710 9738tree
c2255bc4 9739c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9740{
0c9b182b
JJ
9741 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9742 bool no_warning = false;
928c19bb 9743 bool npc = false;
36536d79 9744 size_t rank = 0;
3e4093b6 9745
de8ddd5f
MP
9746 /* Use the expansion point to handle cases such as returning NULL
9747 in a function returning void. */
9748 source_location xloc = expansion_point_location_if_in_system_header (loc);
9749
3e4093b6 9750 if (TREE_THIS_VOLATILE (current_function_decl))
de8ddd5f 9751 warning_at (xloc, 0,
c2255bc4 9752 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9753
b72271b9 9754 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9755 {
9756 /* Array notations are allowed in a return statement if it is inside a
9757 built-in array notation reduction function. */
9758 if (!find_rank (loc, retval, retval, false, &rank))
9759 return error_mark_node;
9760 if (rank >= 1)
9761 {
9762 error_at (loc, "array notation expression cannot be used as a "
9763 "return value");
9764 return error_mark_node;
9765 }
9766 }
3af9c5e9 9767 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9768 {
9769 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9770 "allowed");
9771 return error_mark_node;
9772 }
928c19bb
JM
9773 if (retval)
9774 {
8ce94e44 9775 tree semantic_type = NULL_TREE;
928c19bb 9776 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9777 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9778 {
9779 semantic_type = TREE_TYPE (retval);
9780 retval = TREE_OPERAND (retval, 0);
9781 }
928c19bb 9782 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9783 if (semantic_type)
9784 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9785 }
9786
3e4093b6 9787 if (!retval)
de520661 9788 {
3e4093b6
RS
9789 current_function_returns_null = 1;
9790 if ((warn_return_type || flag_isoc99)
9791 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9792 {
94c40e19 9793 bool warned_here;
35aff4fb 9794 if (flag_isoc99)
94c40e19
DM
9795 warned_here = pedwarn
9796 (loc, 0,
9797 "%<return%> with no value, in function returning non-void");
35aff4fb 9798 else
94c40e19
DM
9799 warned_here = warning_at
9800 (loc, OPT_Wreturn_type,
9801 "%<return%> with no value, in function returning non-void");
0c9b182b 9802 no_warning = true;
94c40e19
DM
9803 if (warned_here)
9804 inform (DECL_SOURCE_LOCATION (current_function_decl),
9805 "declared here");
0c9b182b 9806 }
400fbf9f 9807 }
3e4093b6 9808 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9809 {
3e4093b6 9810 current_function_returns_null = 1;
94c40e19 9811 bool warned_here;
2397c575 9812 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
94c40e19
DM
9813 warned_here = pedwarn
9814 (xloc, 0,
9815 "%<return%> with a value, in function returning void");
b8698a0f 9816 else
94c40e19
DM
9817 warned_here = pedwarn
9818 (xloc, OPT_Wpedantic, "ISO C forbids "
9819 "%<return%> with expression, in function returning void");
9820 if (warned_here)
9821 inform (DECL_SOURCE_LOCATION (current_function_decl),
9822 "declared here");
de520661 9823 }
3e4093b6 9824 else
de520661 9825 {
68fca595
MP
9826 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9827 retval, origtype, ic_return,
c2255bc4 9828 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9829 tree res = DECL_RESULT (current_function_decl);
9830 tree inner;
9feb29df 9831 bool save;
3e4093b6
RS
9832
9833 current_function_returns_value = 1;
9834 if (t == error_mark_node)
506e2710 9835 return NULL_TREE;
3e4093b6 9836
9feb29df
JJ
9837 save = in_late_binary_op;
9838 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
9839 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9840 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9841 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9842 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9843 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9feb29df 9844 in_late_binary_op = true;
3e4093b6 9845 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9846 in_late_binary_op = save;
3e4093b6
RS
9847
9848 /* Strip any conversions, additions, and subtractions, and see if
9849 we are returning the address of a local variable. Warn if so. */
9850 while (1)
8b6a5902 9851 {
3e4093b6 9852 switch (TREE_CODE (inner))
8b6a5902 9853 {
849421a3
JJ
9854 CASE_CONVERT:
9855 case NON_LVALUE_EXPR:
3e4093b6 9856 case PLUS_EXPR:
849421a3 9857 case POINTER_PLUS_EXPR:
3e4093b6
RS
9858 inner = TREE_OPERAND (inner, 0);
9859 continue;
9860
9861 case MINUS_EXPR:
9862 /* If the second operand of the MINUS_EXPR has a pointer
9863 type (or is converted from it), this may be valid, so
9864 don't give a warning. */
9865 {
9866 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9867
3f75a254 9868 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9869 && (CONVERT_EXPR_P (op1)
9870 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9871 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9872
3e4093b6
RS
9873 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9874 break;
8b6a5902 9875
3e4093b6
RS
9876 inner = TREE_OPERAND (inner, 0);
9877 continue;
9878 }
400fbf9f 9879
3e4093b6
RS
9880 case ADDR_EXPR:
9881 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9882
6615c446 9883 while (REFERENCE_CLASS_P (inner)
22d03525 9884 && !INDIRECT_REF_P (inner))
3e4093b6 9885 inner = TREE_OPERAND (inner, 0);
8b6a5902 9886
a2f1f4c3 9887 if (DECL_P (inner)
3f75a254
JM
9888 && !DECL_EXTERNAL (inner)
9889 && !TREE_STATIC (inner)
3e4093b6 9890 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9891 {
9892 if (TREE_CODE (inner) == LABEL_DECL)
9893 warning_at (loc, OPT_Wreturn_local_addr,
9894 "function returns address of label");
9895 else
b4dfdc11
MG
9896 {
9897 warning_at (loc, OPT_Wreturn_local_addr,
9898 "function returns address of local variable");
9899 tree zero = build_zero_cst (TREE_TYPE (res));
9900 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9901 }
19fc9faa 9902 }
3e4093b6 9903 break;
8b6a5902 9904
3e4093b6
RS
9905 default:
9906 break;
9907 }
de520661 9908
3e4093b6
RS
9909 break;
9910 }
9911
53fb4de3 9912 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9913 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9914
9915 if (warn_sequence_point)
9916 verify_sequence_points (retval);
de520661 9917 }
8b6a5902 9918
c2255bc4 9919 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9920 TREE_NO_WARNING (ret_stmt) |= no_warning;
9921 return add_stmt (ret_stmt);
de520661 9922}
3e4093b6
RS
9923\f
9924struct c_switch {
604f5adf
ILT
9925 /* The SWITCH_EXPR being built. */
9926 tree switch_expr;
a6c0a76c 9927
89dbed81 9928 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9929 default conversion is applied. */
9930 tree orig_type;
9931
3e4093b6
RS
9932 /* A splay-tree mapping the low element of a case range to the high
9933 element, or NULL_TREE if there is no high element. Used to
9934 determine whether or not a new case label duplicates an old case
9935 label. We need a tree, rather than simply a hash table, because
9936 of the GNU case range extension. */
9937 splay_tree cases;
a6c0a76c 9938
e1b7793c
ILT
9939 /* The bindings at the point of the switch. This is used for
9940 warnings crossing decls when branching to a case label. */
9941 struct c_spot_bindings *bindings;
187230a7 9942
3e4093b6
RS
9943 /* The next node on the stack. */
9944 struct c_switch *next;
b155cfd9
MP
9945
9946 /* Remember whether the controlling expression had boolean type
9947 before integer promotions for the sake of -Wswitch-bool. */
9948 bool bool_cond_p;
9949
9950 /* Remember whether there was a case value that is outside the
9951 range of the ORIG_TYPE. */
9952 bool outside_range_p;
3e4093b6 9953};
400fbf9f 9954
3e4093b6
RS
9955/* A stack of the currently active switch statements. The innermost
9956 switch statement is on the top of the stack. There is no need to
9957 mark the stack for garbage collection because it is only active
9958 during the processing of the body of a function, and we never
9959 collect at that point. */
de520661 9960
506e2710 9961struct c_switch *c_switch_stack;
de520661 9962
3e4093b6 9963/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9964 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef 9965 SWITCH_COND_LOC is the location of the switch's condition.
b155cfd9 9966 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
de520661 9967
3e4093b6 9968tree
c2255bc4
AH
9969c_start_case (location_t switch_loc,
9970 location_t switch_cond_loc,
fedfecef 9971 tree exp, bool explicit_cast_p)
de520661 9972{
c58e8676 9973 tree orig_type = error_mark_node;
b155cfd9 9974 bool bool_cond_p = false;
3e4093b6 9975 struct c_switch *cs;
2f6e4e97 9976
3e4093b6 9977 if (exp != error_mark_node)
de520661 9978 {
3e4093b6
RS
9979 orig_type = TREE_TYPE (exp);
9980
c58e8676 9981 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9982 {
c58e8676
VR
9983 if (orig_type != error_mark_node)
9984 {
c2255bc4 9985 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9986 orig_type = error_mark_node;
9987 }
3e4093b6 9988 exp = integer_zero_node;
de520661 9989 }
3e4093b6 9990 else
de520661 9991 {
c58e8676 9992 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9993 tree e = exp;
9994
9995 /* Warn if the condition has boolean value. */
9996 while (TREE_CODE (e) == COMPOUND_EXPR)
9997 e = TREE_OPERAND (e, 1);
9998
9999 if ((TREE_CODE (type) == BOOLEAN_TYPE
10000 || truth_value_p (TREE_CODE (e)))
10001 /* Explicit cast to int suppresses this warning. */
10002 && !(TREE_CODE (type) == INTEGER_TYPE
10003 && explicit_cast_p))
b155cfd9 10004 bool_cond_p = true;
8b6a5902 10005
8400e75e 10006 if (!in_system_header_at (input_location)
3e4093b6
RS
10007 && (type == long_integer_type_node
10008 || type == long_unsigned_type_node))
c2255bc4
AH
10009 warning_at (switch_cond_loc,
10010 OPT_Wtraditional, "%<long%> switch expression not "
10011 "converted to %<int%> in ISO C");
8b6a5902 10012
928c19bb 10013 exp = c_fully_fold (exp, false, NULL);
3e4093b6 10014 exp = default_conversion (exp);
ca085fd7
MLI
10015
10016 if (warn_sequence_point)
10017 verify_sequence_points (exp);
3e4093b6
RS
10018 }
10019 }
10020
604f5adf 10021 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 10022 cs = XNEW (struct c_switch);
604f5adf 10023 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 10024 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 10025 cs->orig_type = orig_type;
3e4093b6 10026 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 10027 cs->bindings = c_get_switch_bindings ();
b155cfd9
MP
10028 cs->bool_cond_p = bool_cond_p;
10029 cs->outside_range_p = false;
506e2710
RH
10030 cs->next = c_switch_stack;
10031 c_switch_stack = cs;
3e4093b6 10032
604f5adf 10033 return add_stmt (cs->switch_expr);
3e4093b6
RS
10034}
10035
c2255bc4 10036/* Process a case label at location LOC. */
3e4093b6
RS
10037
10038tree
c2255bc4 10039do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
10040{
10041 tree label = NULL_TREE;
10042
17cede2e
JM
10043 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10044 {
10045 low_value = c_fully_fold (low_value, false, NULL);
10046 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 10047 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
10048 "case label is not an integer constant expression");
10049 }
10050
10051 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10052 {
10053 high_value = c_fully_fold (high_value, false, NULL);
10054 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 10055 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
10056 "case label is not an integer constant expression");
10057 }
10058
e1b7793c 10059 if (c_switch_stack == NULL)
187230a7
JM
10060 {
10061 if (low_value)
e1b7793c 10062 error_at (loc, "case label not within a switch statement");
187230a7 10063 else
e1b7793c
ILT
10064 error_at (loc, "%<default%> label not within a switch statement");
10065 return NULL_TREE;
187230a7 10066 }
de520661 10067
e1b7793c
ILT
10068 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10069 EXPR_LOCATION (c_switch_stack->switch_expr),
10070 loc))
10071 return NULL_TREE;
10072
10073 label = c_add_case_label (loc, c_switch_stack->cases,
10074 SWITCH_COND (c_switch_stack->switch_expr),
10075 c_switch_stack->orig_type,
b155cfd9
MP
10076 low_value, high_value,
10077 &c_switch_stack->outside_range_p);
e1b7793c
ILT
10078 if (label == error_mark_node)
10079 label = NULL_TREE;
3e4093b6
RS
10080 return label;
10081}
de520661 10082
083e891e
MP
10083/* Finish the switch statement. TYPE is the original type of the
10084 controlling expression of the switch, or NULL_TREE. */
de520661 10085
3e4093b6 10086void
083e891e 10087c_finish_case (tree body, tree type)
3e4093b6 10088{
506e2710 10089 struct c_switch *cs = c_switch_stack;
fbc315db 10090 location_t switch_location;
3e4093b6 10091
604f5adf 10092 SWITCH_BODY (cs->switch_expr) = body;
325c3691 10093
6de9cd9a 10094 /* Emit warnings as needed. */
c2255bc4 10095 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 10096 c_do_switch_warnings (cs->cases, switch_location,
083e891e 10097 type ? type : TREE_TYPE (cs->switch_expr),
b155cfd9
MP
10098 SWITCH_COND (cs->switch_expr),
10099 cs->bool_cond_p, cs->outside_range_p);
6de9cd9a 10100
3e4093b6 10101 /* Pop the stack. */
506e2710 10102 c_switch_stack = cs->next;
3e4093b6 10103 splay_tree_delete (cs->cases);
e1b7793c 10104 c_release_switch_bindings (cs->bindings);
5d038c4c 10105 XDELETE (cs);
de520661 10106}
325c3691 10107\f
506e2710
RH
10108/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10109 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
99cd9857 10110 may be null. */
325c3691 10111
9e51cf9d 10112void
506e2710 10113c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
99cd9857 10114 tree else_block)
325c3691 10115{
506e2710 10116 tree stmt;
325c3691 10117
25c22937
BI
10118 /* If the condition has array notations, then the rank of the then_block and
10119 else_block must be either 0 or be equal to the rank of the condition. If
10120 the condition does not have array notations then break them up as it is
10121 broken up in a normal expression. */
b72271b9 10122 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
10123 {
10124 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10125 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10126 return;
10127 if (then_block
10128 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10129 return;
10130 if (else_block
10131 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10132 return;
10133 if (cond_rank != then_rank && then_rank != 0)
10134 {
10135 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10136 " and the then-block");
10137 return;
10138 }
10139 else if (cond_rank != else_rank && else_rank != 0)
10140 {
10141 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10142 " and the else-block");
10143 return;
10144 }
10145 }
16865eaa 10146
2214de30 10147 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 10148 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 10149 add_stmt (stmt);
325c3691
RH
10150}
10151
506e2710
RH
10152/* Emit a general-purpose loop construct. START_LOCUS is the location of
10153 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10154 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 10155 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 10156 the continue label. Everything is allowed to be NULL. */
325c3691
RH
10157
10158void
506e2710
RH
10159c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10160 tree blab, tree clab, bool cond_is_first)
325c3691 10161{
506e2710
RH
10162 tree entry = NULL, exit = NULL, t;
10163
e5e44252
AK
10164 /* In theory could forbid cilk spawn for loop increment expression,
10165 but it should work just fine. */
36536d79 10166
28af952a
RS
10167 /* If the condition is zero don't generate a loop construct. */
10168 if (cond && integer_zerop (cond))
10169 {
10170 if (cond_is_first)
10171 {
10172 t = build_and_jump (&blab);
10173 SET_EXPR_LOCATION (t, start_locus);
10174 add_stmt (t);
10175 }
10176 }
10177 else
506e2710
RH
10178 {
10179 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 10180
506e2710 10181 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
10182 out of the loop, or to the top of it. If there's no exit condition,
10183 then we just build a jump back to the top. */
506e2710 10184 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 10185
28af952a 10186 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
10187 {
10188 /* Canonicalize the loop condition to the end. This means
10189 generating a branch to the loop condition. Reuse the
10190 continue label, if possible. */
10191 if (cond_is_first)
10192 {
10193 if (incr || !clab)
10194 {
10195 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10196 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10197 }
10198 else
10199 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 10200 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
10201 add_stmt (t);
10202 }
10203
506e2710 10204 t = build_and_jump (&blab);
506e2710 10205 if (cond_is_first)
db3927fb
AH
10206 exit = fold_build3_loc (start_locus,
10207 COND_EXPR, void_type_node, cond, exit, t);
506e2710 10208 else
db3927fb
AH
10209 exit = fold_build3_loc (input_location,
10210 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3 10211 }
fc402eec
AA
10212 else
10213 {
10214 /* For the backward-goto's location of an unconditional loop
10215 use the beginning of the body, or, if there is none, the
10216 top of the loop. */
10217 location_t loc = EXPR_LOCATION (expr_first (body));
10218 if (loc == UNKNOWN_LOCATION)
10219 loc = start_locus;
10220 SET_EXPR_LOCATION (exit, loc);
10221 }
c22cacf3 10222
506e2710
RH
10223 add_stmt (top);
10224 }
c22cacf3 10225
506e2710
RH
10226 if (body)
10227 add_stmt (body);
10228 if (clab)
10229 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10230 if (incr)
10231 add_stmt (incr);
10232 if (entry)
10233 add_stmt (entry);
10234 if (exit)
10235 add_stmt (exit);
10236 if (blab)
10237 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 10238}
325c3691
RH
10239
10240tree
c2255bc4 10241c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 10242{
089efaa4 10243 bool skip;
506e2710 10244 tree label = *label_p;
325c3691 10245
089efaa4
ILT
10246 /* In switch statements break is sometimes stylistically used after
10247 a return statement. This can lead to spurious warnings about
10248 control reaching the end of a non-void function when it is
10249 inlined. Note that we are calling block_may_fallthru with
10250 language specific tree nodes; this works because
10251 block_may_fallthru returns true when given something it does not
10252 understand. */
10253 skip = !block_may_fallthru (cur_stmt_list);
10254
506e2710 10255 if (!label)
089efaa4
ILT
10256 {
10257 if (!skip)
c2255bc4 10258 *label_p = label = create_artificial_label (loc);
089efaa4 10259 }
953ff289
DN
10260 else if (TREE_CODE (label) == LABEL_DECL)
10261 ;
10262 else switch (TREE_INT_CST_LOW (label))
506e2710 10263 {
953ff289 10264 case 0:
506e2710 10265 if (is_break)
c2255bc4 10266 error_at (loc, "break statement not within loop or switch");
506e2710 10267 else
c2255bc4 10268 error_at (loc, "continue statement not within a loop");
506e2710 10269 return NULL_TREE;
953ff289
DN
10270
10271 case 1:
10272 gcc_assert (is_break);
c2255bc4 10273 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
10274 return NULL_TREE;
10275
c02065fc
AH
10276 case 2:
10277 if (is_break)
10278 error ("break statement within %<#pragma simd%> loop body");
10279 else
10280 error ("continue statement within %<#pragma simd%> loop body");
10281 return NULL_TREE;
10282
953ff289
DN
10283 default:
10284 gcc_unreachable ();
506e2710 10285 }
325c3691 10286
089efaa4
ILT
10287 if (skip)
10288 return NULL_TREE;
10289
2e28e797
JH
10290 if (!is_break)
10291 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10292
53fb4de3 10293 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
10294}
10295
506e2710 10296/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
10297
10298static void
c2255bc4 10299emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 10300{
e6b5a630
RH
10301 if (expr == error_mark_node)
10302 ;
10303 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
10304 {
10305 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 10306 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 10307 }
789eadcd
MP
10308 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10309 {
10310 tree r = expr;
10311 location_t cloc = loc;
10312 while (TREE_CODE (r) == COMPOUND_EXPR)
10313 {
10314 if (EXPR_HAS_LOCATION (r))
10315 cloc = EXPR_LOCATION (r);
10316 r = TREE_OPERAND (r, 1);
10317 }
10318 if (!TREE_SIDE_EFFECTS (r)
10319 && !VOID_TYPE_P (TREE_TYPE (r))
10320 && !CONVERT_EXPR_P (r)
cc28fc7f 10321 && !TREE_NO_WARNING (r)
789eadcd
MP
10322 && !TREE_NO_WARNING (expr))
10323 warning_at (cloc, OPT_Wunused_value,
10324 "right-hand operand of comma expression has no effect");
10325 }
27f33b15 10326 else
c2255bc4 10327 warn_if_unused_value (expr, loc);
3a5b9284
RH
10328}
10329
506e2710 10330/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
10331 diagnostics, but do not call ADD_STMT. LOC is the location of the
10332 statement. */
3a5b9284 10333
506e2710 10334tree
c2255bc4 10335c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 10336{
056928b2
JJ
10337 tree exprv;
10338
3a5b9284 10339 if (!expr)
506e2710 10340 return NULL_TREE;
3a5b9284 10341
928c19bb
JM
10342 expr = c_fully_fold (expr, false, NULL);
10343
3a5b9284
RH
10344 if (warn_sequence_point)
10345 verify_sequence_points (expr);
10346
10347 if (TREE_TYPE (expr) != error_mark_node
10348 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10349 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 10350 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
10351
10352 /* If we're not processing a statement expression, warn about unused values.
10353 Warnings for statement expressions will be emitted later, once we figure
10354 out which is the result. */
10355 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 10356 && warn_unused_value)
d06f8b75 10357 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
3a5b9284 10358
056928b2
JJ
10359 exprv = expr;
10360 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10361 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
10362 while (CONVERT_EXPR_P (exprv))
10363 exprv = TREE_OPERAND (exprv, 0);
10364 if (DECL_P (exprv)
10365 || handled_component_p (exprv)
10366 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 10367 mark_exp_read (exprv);
fa8351f8 10368
3a5b9284
RH
10369 /* If the expression is not of a type to which we cannot assign a line
10370 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 10371 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
10372 {
10373 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10374 SET_EXPR_LOCATION (expr, loc);
10375 }
506e2710
RH
10376
10377 return expr;
10378}
10379
c2255bc4
AH
10380/* Emit an expression as a statement. LOC is the location of the
10381 expression. */
506e2710
RH
10382
10383tree
c2255bc4 10384c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
10385{
10386 if (expr)
c2255bc4 10387 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
10388 else
10389 return NULL;
3a5b9284
RH
10390}
10391
10392/* Do the opposite and emit a statement as an expression. To begin,
10393 create a new binding level and return it. */
325c3691
RH
10394
10395tree
10396c_begin_stmt_expr (void)
10397{
10398 tree ret;
10399
10400 /* We must force a BLOCK for this level so that, if it is not expanded
10401 later, there is a way to turn off the entire subtree of blocks that
10402 are contained in it. */
10403 keep_next_level ();
10404 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10405
10406 c_bindings_start_stmt_expr (c_switch_stack == NULL
10407 ? NULL
10408 : c_switch_stack->bindings);
325c3691
RH
10409
10410 /* Mark the current statement list as belonging to a statement list. */
10411 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10412
10413 return ret;
10414}
10415
c2255bc4
AH
10416/* LOC is the location of the compound statement to which this body
10417 belongs. */
10418
325c3691 10419tree
c2255bc4 10420c_finish_stmt_expr (location_t loc, tree body)
325c3691 10421{
3a5b9284 10422 tree last, type, tmp, val;
325c3691
RH
10423 tree *last_p;
10424
c2255bc4 10425 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10426
10427 c_bindings_end_stmt_expr (c_switch_stack == NULL
10428 ? NULL
10429 : c_switch_stack->bindings);
325c3691 10430
3a5b9284
RH
10431 /* Locate the last statement in BODY. See c_end_compound_stmt
10432 about always returning a BIND_EXPR. */
10433 last_p = &BIND_EXPR_BODY (body);
10434 last = BIND_EXPR_BODY (body);
10435
10436 continue_searching:
325c3691
RH
10437 if (TREE_CODE (last) == STATEMENT_LIST)
10438 {
3a5b9284
RH
10439 tree_stmt_iterator i;
10440
10441 /* This can happen with degenerate cases like ({ }). No value. */
10442 if (!TREE_SIDE_EFFECTS (last))
10443 return body;
10444
10445 /* If we're supposed to generate side effects warnings, process
10446 all of the statements except the last. */
27f33b15 10447 if (warn_unused_value)
325c3691 10448 {
3a5b9284 10449 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10450 {
10451 location_t tloc;
10452 tree t = tsi_stmt (i);
10453
10454 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10455 emit_side_effect_warnings (tloc, t);
10456 }
325c3691
RH
10457 }
10458 else
3a5b9284
RH
10459 i = tsi_last (last);
10460 last_p = tsi_stmt_ptr (i);
10461 last = *last_p;
325c3691
RH
10462 }
10463
3a5b9284
RH
10464 /* If the end of the list is exception related, then the list was split
10465 by a call to push_cleanup. Continue searching. */
10466 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10467 || TREE_CODE (last) == TRY_CATCH_EXPR)
10468 {
10469 last_p = &TREE_OPERAND (last, 0);
10470 last = *last_p;
10471 goto continue_searching;
10472 }
10473
26d8af35
JM
10474 if (last == error_mark_node)
10475 return last;
10476
3a5b9284
RH
10477 /* In the case that the BIND_EXPR is not necessary, return the
10478 expression out from inside it. */
26d8af35
JM
10479 if (last == BIND_EXPR_BODY (body)
10480 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10481 {
928c19bb
JM
10482 /* Even if this looks constant, do not allow it in a constant
10483 expression. */
e5a94231 10484 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10485 /* Do not warn if the return value of a statement expression is
10486 unused. */
928c19bb 10487 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10488 return last;
10489 }
325c3691
RH
10490
10491 /* Extract the type of said expression. */
10492 type = TREE_TYPE (last);
325c3691 10493
3a5b9284
RH
10494 /* If we're not returning a value at all, then the BIND_EXPR that
10495 we already have is a fine expression to return. */
10496 if (!type || VOID_TYPE_P (type))
10497 return body;
10498
10499 /* Now that we've located the expression containing the value, it seems
10500 silly to make voidify_wrapper_expr repeat the process. Create a
10501 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10502 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10503
10504 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10505 tree_expr_nonnegative_p giving up immediately. */
10506 val = last;
10507 if (TREE_CODE (val) == NOP_EXPR
10508 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10509 val = TREE_OPERAND (val, 0);
10510
53fb4de3 10511 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10512 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10513
c2255bc4
AH
10514 {
10515 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10516 SET_EXPR_LOCATION (t, loc);
10517 return t;
10518 }
325c3691
RH
10519}
10520\f
10521/* Begin and end compound statements. This is as simple as pushing
10522 and popping new statement lists from the tree. */
10523
10524tree
10525c_begin_compound_stmt (bool do_scope)
10526{
10527 tree stmt = push_stmt_list ();
10528 if (do_scope)
4dfa0342 10529 push_scope ();
325c3691
RH
10530 return stmt;
10531}
10532
c2255bc4
AH
10533/* End a compound statement. STMT is the statement. LOC is the
10534 location of the compound statement-- this is usually the location
10535 of the opening brace. */
10536
325c3691 10537tree
c2255bc4 10538c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10539{
10540 tree block = NULL;
10541
10542 if (do_scope)
10543 {
10544 if (c_dialect_objc ())
10545 objc_clear_super_receiver ();
10546 block = pop_scope ();
10547 }
10548
10549 stmt = pop_stmt_list (stmt);
c2255bc4 10550 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10551
10552 /* If this compound statement is nested immediately inside a statement
10553 expression, then force a BIND_EXPR to be created. Otherwise we'll
10554 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10555 STATEMENT_LISTs merge, and thus we can lose track of what statement
10556 was really last. */
38e01f9e 10557 if (building_stmt_list_p ()
325c3691
RH
10558 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10559 && TREE_CODE (stmt) != BIND_EXPR)
10560 {
53fb4de3 10561 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10562 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10563 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10564 }
10565
10566 return stmt;
10567}
5a508662
RH
10568
10569/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10570 when the current scope is exited. EH_ONLY is true when this is not
10571 meant to apply to normal control flow transfer. */
10572
10573void
c2255bc4 10574push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10575{
3a5b9284
RH
10576 enum tree_code code;
10577 tree stmt, list;
10578 bool stmt_expr;
10579
10580 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10581 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10582 add_stmt (stmt);
3a5b9284
RH
10583 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10584 list = push_stmt_list ();
10585 TREE_OPERAND (stmt, 0) = list;
10586 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10587}
325c3691 10588\f
9f47c7e5
IE
10589/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10590 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10591
10592static tree
10593build_vec_cmp (tree_code code, tree type,
10594 tree arg0, tree arg1)
10595{
10596 tree zero_vec = build_zero_cst (type);
10597 tree minus_one_vec = build_minus_one_cst (type);
10598 tree cmp_type = build_same_sized_truth_vector_type (type);
10599 tree cmp = build2 (code, cmp_type, arg0, arg1);
10600 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10601}
10602
3e4093b6
RS
10603/* Build a binary-operation expression without default conversions.
10604 CODE is the kind of expression to build.
ba47d38d 10605 LOCATION is the operator's location.
3e4093b6
RS
10606 This function differs from `build' in several ways:
10607 the data type of the result is computed and recorded in it,
10608 warnings are generated if arg data types are invalid,
10609 special handling for addition and subtraction of pointers is known,
10610 and some optimization is done (operations on narrow ints
10611 are done in the narrower type when that gives the same result).
10612 Constant folding is also done before the result is returned.
de520661 10613
3e4093b6
RS
10614 Note that the operands will never have enumeral types, or function
10615 or array types, because either they will have the default conversions
10616 performed or they have both just been converted to some other type in which
10617 the arithmetic is to be done. */
10618
10619tree
ba47d38d
AH
10620build_binary_op (location_t location, enum tree_code code,
10621 tree orig_op0, tree orig_op1, int convert_p)
de520661 10622{
8ce94e44
JM
10623 tree type0, type1, orig_type0, orig_type1;
10624 tree eptype;
3e4093b6
RS
10625 enum tree_code code0, code1;
10626 tree op0, op1;
c9f9eb5d 10627 tree ret = error_mark_node;
4de67c26 10628 const char *invalid_op_diag;
4d84fe7c 10629 bool op0_int_operands, op1_int_operands;
928c19bb 10630 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10631
3e4093b6
RS
10632 /* Expression code to give to the expression when it is built.
10633 Normally this is CODE, which is what the caller asked for,
10634 but in some special cases we change it. */
10635 enum tree_code resultcode = code;
8b6a5902 10636
3e4093b6
RS
10637 /* Data type in which the computation is to be performed.
10638 In the simplest cases this is the common type of the arguments. */
10639 tree result_type = NULL;
10640
8ce94e44
JM
10641 /* When the computation is in excess precision, the type of the
10642 final EXCESS_PRECISION_EXPR. */
2d2e923f 10643 tree semantic_result_type = NULL;
8ce94e44 10644
3e4093b6
RS
10645 /* Nonzero means operands have already been type-converted
10646 in whatever way is necessary.
10647 Zero means they need to be converted to RESULT_TYPE. */
10648 int converted = 0;
10649
10650 /* Nonzero means create the expression with this type, rather than
10651 RESULT_TYPE. */
10652 tree build_type = 0;
10653
10654 /* Nonzero means after finally constructing the expression
10655 convert it to this type. */
10656 tree final_type = 0;
10657
10658 /* Nonzero if this is an operation like MIN or MAX which can
10659 safely be computed in short if both args are promoted shorts.
10660 Also implies COMMON.
10661 -1 indicates a bitwise operation; this makes a difference
10662 in the exact conditions for when it is safe to do the operation
10663 in a narrower mode. */
10664 int shorten = 0;
10665
10666 /* Nonzero if this is a comparison operation;
10667 if both args are promoted shorts, compare the original shorts.
10668 Also implies COMMON. */
10669 int short_compare = 0;
10670
10671 /* Nonzero if this is a right-shift operation, which can be computed on the
10672 original short and then promoted if the operand is a promoted short. */
10673 int short_shift = 0;
10674
10675 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10676 int common = 0;
10677
58393038
ZL
10678 /* True means types are compatible as far as ObjC is concerned. */
10679 bool objc_ok;
10680
8ce94e44
JM
10681 /* True means this is an arithmetic operation that may need excess
10682 precision. */
10683 bool may_need_excess_precision;
10684
180f8dbb
JM
10685 /* True means this is a boolean operation that converts both its
10686 operands to truth-values. */
10687 bool boolean_op = false;
10688
de5a5fa1
MP
10689 /* Remember whether we're doing / or %. */
10690 bool doing_div_or_mod = false;
10691
10692 /* Remember whether we're doing << or >>. */
10693 bool doing_shift = false;
10694
10695 /* Tree holding instrumentation expression. */
10696 tree instrument_expr = NULL;
10697
ba47d38d
AH
10698 if (location == UNKNOWN_LOCATION)
10699 location = input_location;
10700
4d84fe7c
JM
10701 op0 = orig_op0;
10702 op1 = orig_op1;
10703
10704 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10705 if (op0_int_operands)
10706 op0 = remove_c_maybe_const_expr (op0);
10707 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10708 if (op1_int_operands)
10709 op1 = remove_c_maybe_const_expr (op1);
10710 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10711 if (int_operands)
10712 {
10713 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10714 && TREE_CODE (orig_op1) == INTEGER_CST);
10715 int_const = (int_const_or_overflow
10716 && !TREE_OVERFLOW (orig_op0)
10717 && !TREE_OVERFLOW (orig_op1));
10718 }
10719 else
10720 int_const = int_const_or_overflow = false;
10721
0e3a99ae 10722 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e 10723 if (convert_p
31521951 10724 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
790e9490 10725 {
4d84fe7c
JM
10726 op0 = default_conversion (op0);
10727 op1 = default_conversion (op1);
790e9490
RS
10728 }
10729
36536d79
BI
10730 /* When Cilk Plus is enabled and there are array notations inside op0, then
10731 we check to see if there are builtin array notation functions. If
10732 so, then we take on the type of the array notation inside it. */
b72271b9 10733 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10734 orig_type0 = type0 = find_correct_array_notation_type (op0);
10735 else
10736 orig_type0 = type0 = TREE_TYPE (op0);
10737
b72271b9 10738 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10739 orig_type1 = type1 = find_correct_array_notation_type (op1);
10740 else
10741 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10742
3e4093b6
RS
10743 /* The expression codes of the data types of the arguments tell us
10744 whether the arguments are integers, floating, pointers, etc. */
10745 code0 = TREE_CODE (type0);
10746 code1 = TREE_CODE (type1);
10747
10748 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10749 STRIP_TYPE_NOPS (op0);
10750 STRIP_TYPE_NOPS (op1);
10751
10752 /* If an error was already reported for one of the arguments,
10753 avoid reporting another error. */
10754
10755 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10756 return error_mark_node;
10757
1807ffc1
MS
10758 if (code0 == POINTER_TYPE
10759 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10760 return error_mark_node;
10761
10762 if (code1 == POINTER_TYPE
10763 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10764 return error_mark_node;
10765
4de67c26
JM
10766 if ((invalid_op_diag
10767 = targetm.invalid_binary_op (code, type0, type1)))
10768 {
ba47d38d 10769 error_at (location, invalid_op_diag);
4de67c26
JM
10770 return error_mark_node;
10771 }
10772
8ce94e44
JM
10773 switch (code)
10774 {
10775 case PLUS_EXPR:
10776 case MINUS_EXPR:
10777 case MULT_EXPR:
10778 case TRUNC_DIV_EXPR:
10779 case CEIL_DIV_EXPR:
10780 case FLOOR_DIV_EXPR:
10781 case ROUND_DIV_EXPR:
10782 case EXACT_DIV_EXPR:
10783 may_need_excess_precision = true;
10784 break;
10785 default:
10786 may_need_excess_precision = false;
10787 break;
10788 }
10789 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10790 {
10791 op0 = TREE_OPERAND (op0, 0);
10792 type0 = TREE_TYPE (op0);
10793 }
10794 else if (may_need_excess_precision
10795 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10796 {
10797 type0 = eptype;
10798 op0 = convert (eptype, op0);
10799 }
10800 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10801 {
10802 op1 = TREE_OPERAND (op1, 0);
10803 type1 = TREE_TYPE (op1);
10804 }
10805 else if (may_need_excess_precision
10806 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10807 {
10808 type1 = eptype;
10809 op1 = convert (eptype, op1);
10810 }
10811
58393038
ZL
10812 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10813
0e3a99ae
AS
10814 /* In case when one of the operands of the binary operation is
10815 a vector and another is a scalar -- convert scalar to vector. */
10816 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10817 {
a212e43f
MG
10818 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10819 true);
f90e8e2e 10820
0e3a99ae
AS
10821 switch (convert_flag)
10822 {
10823 case stv_error:
10824 return error_mark_node;
10825 case stv_firstarg:
10826 {
10827 bool maybe_const = true;
10828 tree sc;
10829 sc = c_fully_fold (op0, false, &maybe_const);
10830 sc = save_expr (sc);
10831 sc = convert (TREE_TYPE (type1), sc);
10832 op0 = build_vector_from_val (type1, sc);
10833 if (!maybe_const)
10834 op0 = c_wrap_maybe_const (op0, true);
10835 orig_type0 = type0 = TREE_TYPE (op0);
10836 code0 = TREE_CODE (type0);
10837 converted = 1;
10838 break;
10839 }
10840 case stv_secondarg:
10841 {
10842 bool maybe_const = true;
10843 tree sc;
10844 sc = c_fully_fold (op1, false, &maybe_const);
10845 sc = save_expr (sc);
10846 sc = convert (TREE_TYPE (type0), sc);
10847 op1 = build_vector_from_val (type0, sc);
10848 if (!maybe_const)
54b9f838 10849 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10850 orig_type1 = type1 = TREE_TYPE (op1);
10851 code1 = TREE_CODE (type1);
10852 converted = 1;
10853 break;
10854 }
10855 default:
10856 break;
10857 }
10858 }
10859
3e4093b6 10860 switch (code)
de520661 10861 {
3e4093b6
RS
10862 case PLUS_EXPR:
10863 /* Handle the pointer + int case. */
10864 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10865 {
db3927fb 10866 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10867 goto return_build_binary_op;
10868 }
3e4093b6 10869 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10870 {
db3927fb 10871 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10872 goto return_build_binary_op;
10873 }
fe67cf58 10874 else
3e4093b6
RS
10875 common = 1;
10876 break;
400fbf9f 10877
3e4093b6
RS
10878 case MINUS_EXPR:
10879 /* Subtraction of two similar pointers.
10880 We must subtract them as integers, then divide by object size. */
10881 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10882 && comp_target_types (location, type0, type1))
c9f9eb5d 10883 {
db3927fb 10884 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10885 goto return_build_binary_op;
10886 }
3e4093b6
RS
10887 /* Handle pointer minus int. Just like pointer plus int. */
10888 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10889 {
db3927fb 10890 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10891 goto return_build_binary_op;
10892 }
3e4093b6
RS
10893 else
10894 common = 1;
10895 break;
8b6a5902 10896
3e4093b6
RS
10897 case MULT_EXPR:
10898 common = 1;
10899 break;
10900
10901 case TRUNC_DIV_EXPR:
10902 case CEIL_DIV_EXPR:
10903 case FLOOR_DIV_EXPR:
10904 case ROUND_DIV_EXPR:
10905 case EXACT_DIV_EXPR:
de5a5fa1 10906 doing_div_or_mod = true;
c9f9eb5d 10907 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10908
10909 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10910 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10911 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10912 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10913 || code1 == FIXED_POINT_TYPE
3e4093b6 10914 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10915 {
5bed876a
AH
10916 enum tree_code tcode0 = code0, tcode1 = code1;
10917
3a021db2 10918 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10919 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10920 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10921 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10922
ab22c1fa
CF
10923 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10924 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10925 resultcode = RDIV_EXPR;
10926 else
10927 /* Although it would be tempting to shorten always here, that
10928 loses on some targets, since the modulo instruction is
10929 undefined if the quotient can't be represented in the
10930 computation mode. We shorten only if unsigned or if
10931 dividing by something we know != -1. */
8df83eae 10932 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10933 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10934 && !integer_all_onesp (op1)));
3e4093b6
RS
10935 common = 1;
10936 }
10937 break;
de520661 10938
3e4093b6 10939 case BIT_AND_EXPR:
3e4093b6
RS
10940 case BIT_IOR_EXPR:
10941 case BIT_XOR_EXPR:
10942 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10943 shorten = -1;
9ef0c8d9
AP
10944 /* Allow vector types which are not floating point types. */
10945 else if (code0 == VECTOR_TYPE
10946 && code1 == VECTOR_TYPE
10947 && !VECTOR_FLOAT_TYPE_P (type0)
10948 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10949 common = 1;
10950 break;
10951
10952 case TRUNC_MOD_EXPR:
10953 case FLOOR_MOD_EXPR:
de5a5fa1 10954 doing_div_or_mod = true;
c9f9eb5d 10955 warn_for_div_by_zero (location, op1);
de520661 10956
5cfd5d9b
AP
10957 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10958 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10959 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10960 common = 1;
10961 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10962 {
10963 /* Although it would be tempting to shorten always here, that loses
10964 on some targets, since the modulo instruction is undefined if the
10965 quotient can't be represented in the computation mode. We shorten
10966 only if unsigned or if dividing by something we know != -1. */
8df83eae 10967 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10968 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10969 && !integer_all_onesp (op1)));
3e4093b6
RS
10970 common = 1;
10971 }
10972 break;
de520661 10973
3e4093b6
RS
10974 case TRUTH_ANDIF_EXPR:
10975 case TRUTH_ORIF_EXPR:
10976 case TRUTH_AND_EXPR:
10977 case TRUTH_OR_EXPR:
10978 case TRUTH_XOR_EXPR:
10979 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10980 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10981 || code0 == FIXED_POINT_TYPE)
3e4093b6 10982 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10983 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10984 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10985 {
10986 /* Result of these operations is always an int,
10987 but that does not mean the operands should be
10988 converted to ints! */
10989 result_type = integer_type_node;
a27d595d
JM
10990 if (op0_int_operands)
10991 {
10992 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10993 op0 = remove_c_maybe_const_expr (op0);
10994 }
10995 else
10996 op0 = c_objc_common_truthvalue_conversion (location, op0);
10997 if (op1_int_operands)
10998 {
10999 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11000 op1 = remove_c_maybe_const_expr (op1);
11001 }
11002 else
11003 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 11004 converted = 1;
180f8dbb 11005 boolean_op = true;
3e4093b6 11006 }
928c19bb
JM
11007 if (code == TRUTH_ANDIF_EXPR)
11008 {
11009 int_const_or_overflow = (int_operands
11010 && TREE_CODE (orig_op0) == INTEGER_CST
11011 && (op0 == truthvalue_false_node
11012 || TREE_CODE (orig_op1) == INTEGER_CST));
11013 int_const = (int_const_or_overflow
11014 && !TREE_OVERFLOW (orig_op0)
11015 && (op0 == truthvalue_false_node
11016 || !TREE_OVERFLOW (orig_op1)));
11017 }
11018 else if (code == TRUTH_ORIF_EXPR)
11019 {
11020 int_const_or_overflow = (int_operands
11021 && TREE_CODE (orig_op0) == INTEGER_CST
11022 && (op0 == truthvalue_true_node
11023 || TREE_CODE (orig_op1) == INTEGER_CST));
11024 int_const = (int_const_or_overflow
11025 && !TREE_OVERFLOW (orig_op0)
11026 && (op0 == truthvalue_true_node
11027 || !TREE_OVERFLOW (orig_op1)));
11028 }
3e4093b6 11029 break;
eba80994 11030
3e4093b6
RS
11031 /* Shift operations: result has same type as first operand;
11032 always convert second operand to int.
11033 Also set SHORT_SHIFT if shifting rightward. */
de520661 11034
3e4093b6 11035 case RSHIFT_EXPR:
76e2c821
JB
11036 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11037 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11038 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11039 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
11040 {
11041 result_type = type0;
11042 converted = 1;
11043 }
76e2c821
JB
11044 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11045 || code0 == VECTOR_TYPE)
451b5e48 11046 && code1 == INTEGER_TYPE)
3e4093b6 11047 {
de5a5fa1 11048 doing_shift = true;
928c19bb 11049 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 11050 {
3e4093b6 11051 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
11052 {
11053 int_const = false;
7d882b83 11054 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11055 warning_at (location, OPT_Wshift_count_negative,
11056 "right shift count is negative");
928c19bb 11057 }
76e2c821
JB
11058 else if (code0 == VECTOR_TYPE)
11059 {
11060 if (compare_tree_int (op1,
11061 TYPE_PRECISION (TREE_TYPE (type0)))
11062 >= 0)
11063 {
11064 int_const = false;
11065 if (c_inhibit_evaluation_warnings == 0)
11066 warning_at (location, OPT_Wshift_count_overflow,
11067 "right shift count >= width of vector element");
11068 }
11069 }
3e4093b6 11070 else
bbb818c6 11071 {
3f75a254 11072 if (!integer_zerop (op1))
3e4093b6
RS
11073 short_shift = 1;
11074
11075 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
11076 {
11077 int_const = false;
7d882b83 11078 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11079 warning_at (location, OPT_Wshift_count_overflow,
11080 "right shift count >= width of type");
928c19bb 11081 }
bbb818c6 11082 }
b62acd60 11083 }
de520661 11084
3e4093b6
RS
11085 /* Use the type of the value to be shifted. */
11086 result_type = type0;
3e4093b6
RS
11087 /* Avoid converting op1 to result_type later. */
11088 converted = 1;
400fbf9f 11089 }
3e4093b6 11090 break;
253b6b82 11091
3e4093b6 11092 case LSHIFT_EXPR:
76e2c821
JB
11093 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11094 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11095 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11096 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
11097 {
11098 result_type = type0;
11099 converted = 1;
11100 }
76e2c821
JB
11101 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11102 || code0 == VECTOR_TYPE)
451b5e48 11103 && code1 == INTEGER_TYPE)
3e4093b6 11104 {
de5a5fa1 11105 doing_shift = true;
0173bd2a
MP
11106 if (TREE_CODE (op0) == INTEGER_CST
11107 && tree_int_cst_sgn (op0) < 0)
11108 {
11109 /* Don't reject a left shift of a negative value in a context
11110 where a constant expression is needed in C90. */
11111 if (flag_isoc99)
11112 int_const = false;
11113 if (c_inhibit_evaluation_warnings == 0)
11114 warning_at (location, OPT_Wshift_negative_value,
11115 "left shift of negative value");
11116 }
928c19bb 11117 if (TREE_CODE (op1) == INTEGER_CST)
de520661 11118 {
3e4093b6 11119 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
11120 {
11121 int_const = false;
7d882b83 11122 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11123 warning_at (location, OPT_Wshift_count_negative,
11124 "left shift count is negative");
928c19bb 11125 }
76e2c821
JB
11126 else if (code0 == VECTOR_TYPE)
11127 {
11128 if (compare_tree_int (op1,
11129 TYPE_PRECISION (TREE_TYPE (type0)))
11130 >= 0)
11131 {
11132 int_const = false;
11133 if (c_inhibit_evaluation_warnings == 0)
11134 warning_at (location, OPT_Wshift_count_overflow,
11135 "left shift count >= width of vector element");
11136 }
11137 }
3e4093b6 11138 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
11139 {
11140 int_const = false;
7d882b83 11141 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11142 warning_at (location, OPT_Wshift_count_overflow,
11143 "left shift count >= width of type");
928c19bb 11144 }
451b5e48
MP
11145 else if (TREE_CODE (op0) == INTEGER_CST
11146 && maybe_warn_shift_overflow (location, op0, op1)
11147 && flag_isoc99)
11148 int_const = false;
94ba5069 11149 }
de520661 11150
3e4093b6
RS
11151 /* Use the type of the value to be shifted. */
11152 result_type = type0;
3e4093b6
RS
11153 /* Avoid converting op1 to result_type later. */
11154 converted = 1;
400fbf9f 11155 }
3e4093b6 11156 break;
de520661 11157
3e4093b6
RS
11158 case EQ_EXPR:
11159 case NE_EXPR:
d246ab4f
AS
11160 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11161 {
11162 tree intt;
0af94e6f 11163 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11164 {
11165 error_at (location, "comparing vectors with different "
11166 "element types");
11167 return error_mark_node;
11168 }
11169
11170 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11171 {
11172 error_at (location, "comparing vectors with different "
11173 "number of elements");
11174 return error_mark_node;
11175 }
11176
fa74a4bc
MP
11177 /* It's not precisely specified how the usual arithmetic
11178 conversions apply to the vector types. Here, we use
11179 the unsigned type if one of the operands is signed and
11180 the other one is unsigned. */
11181 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11182 {
11183 if (!TYPE_UNSIGNED (type0))
11184 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11185 else
11186 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11187 warning_at (location, OPT_Wsign_compare, "comparison between "
11188 "types %qT and %qT", type0, type1);
11189 }
11190
d246ab4f
AS
11191 /* Always construct signed integer vector type. */
11192 intt = c_common_type_for_size (GET_MODE_BITSIZE
11193 (TYPE_MODE (TREE_TYPE (type0))), 0);
11194 result_type = build_opaque_vector_type (intt,
11195 TYPE_VECTOR_SUBPARTS (type0));
11196 converted = 1;
9f47c7e5
IE
11197 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11198 goto return_build_binary_op;
d246ab4f 11199 }
ae311566 11200 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
11201 warning_at (location,
11202 OPT_Wfloat_equal,
11203 "comparing floating point with == or != is unsafe");
3e4093b6
RS
11204 /* Result of comparison is always int,
11205 but don't convert the args to int! */
11206 build_type = integer_type_node;
11207 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 11208 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 11209 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 11210 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 11211 short_compare = 1;
637f1455
SZ
11212 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11213 {
11214 if (TREE_CODE (op0) == ADDR_EXPR
de55efd5
MP
11215 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11216 && !from_macro_expansion_at (location))
637f1455
SZ
11217 {
11218 if (code == EQ_EXPR)
11219 warning_at (location,
11220 OPT_Waddress,
11221 "the comparison will always evaluate as %<false%> "
11222 "for the address of %qD will never be NULL",
11223 TREE_OPERAND (op0, 0));
11224 else
11225 warning_at (location,
11226 OPT_Waddress,
11227 "the comparison will always evaluate as %<true%> "
11228 "for the address of %qD will never be NULL",
11229 TREE_OPERAND (op0, 0));
11230 }
11231 result_type = type0;
11232 }
11233 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11234 {
11235 if (TREE_CODE (op1) == ADDR_EXPR
de55efd5
MP
11236 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11237 && !from_macro_expansion_at (location))
637f1455
SZ
11238 {
11239 if (code == EQ_EXPR)
11240 warning_at (location,
f90e8e2e 11241 OPT_Waddress,
637f1455
SZ
11242 "the comparison will always evaluate as %<false%> "
11243 "for the address of %qD will never be NULL",
11244 TREE_OPERAND (op1, 0));
11245 else
11246 warning_at (location,
11247 OPT_Waddress,
11248 "the comparison will always evaluate as %<true%> "
11249 "for the address of %qD will never be NULL",
11250 TREE_OPERAND (op1, 0));
11251 }
11252 result_type = type1;
11253 }
3e4093b6
RS
11254 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11255 {
11256 tree tt0 = TREE_TYPE (type0);
11257 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
11258 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11259 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11260 addr_space_t as_common = ADDR_SPACE_GENERIC;
11261
3e4093b6
RS
11262 /* Anything compares with void *. void * compares with anything.
11263 Otherwise, the targets must be compatible
11264 and both must be object or both incomplete. */
744aa42f 11265 if (comp_target_types (location, type0, type1))
10bc1b1b 11266 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
11267 else if (!addr_space_superset (as0, as1, &as_common))
11268 {
11269 error_at (location, "comparison of pointers to "
11270 "disjoint address spaces");
11271 return error_mark_node;
11272 }
267bac10 11273 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 11274 {
36c5e70a 11275 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 11276 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11277 "comparison of %<void *%> with function pointer");
ee2990e7 11278 }
267bac10 11279 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 11280 {
36c5e70a 11281 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 11282 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11283 "comparison of %<void *%> with function pointer");
e6834654 11284 }
3e4093b6 11285 else
58393038
ZL
11286 /* Avoid warning about the volatile ObjC EH puts on decls. */
11287 if (!objc_ok)
ba47d38d 11288 pedwarn (location, 0,
509c9d60 11289 "comparison of distinct pointer types lacks a cast");
e6834654 11290
3e4093b6 11291 if (result_type == NULL_TREE)
36c5e70a
BE
11292 {
11293 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11294 result_type = build_pointer_type
11295 (build_qualified_type (void_type_node, qual));
11296 }
e6834654 11297 }
3e4093b6 11298 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 11299 {
3e4093b6 11300 result_type = type0;
ba47d38d 11301 pedwarn (location, 0, "comparison between pointer and integer");
de520661 11302 }
3e4093b6 11303 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 11304 {
3e4093b6 11305 result_type = type1;
ba47d38d 11306 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 11307 }
04159acf
MP
11308 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11309 || truth_value_p (TREE_CODE (orig_op0)))
11310 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11311 || truth_value_p (TREE_CODE (orig_op1))))
11312 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11313 break;
8b6a5902 11314
3e4093b6
RS
11315 case LE_EXPR:
11316 case GE_EXPR:
11317 case LT_EXPR:
11318 case GT_EXPR:
d246ab4f
AS
11319 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11320 {
11321 tree intt;
0af94e6f 11322 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11323 {
11324 error_at (location, "comparing vectors with different "
11325 "element types");
11326 return error_mark_node;
11327 }
11328
11329 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11330 {
11331 error_at (location, "comparing vectors with different "
11332 "number of elements");
11333 return error_mark_node;
11334 }
11335
fa74a4bc
MP
11336 /* It's not precisely specified how the usual arithmetic
11337 conversions apply to the vector types. Here, we use
11338 the unsigned type if one of the operands is signed and
11339 the other one is unsigned. */
11340 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11341 {
11342 if (!TYPE_UNSIGNED (type0))
11343 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11344 else
11345 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11346 warning_at (location, OPT_Wsign_compare, "comparison between "
11347 "types %qT and %qT", type0, type1);
11348 }
11349
d246ab4f
AS
11350 /* Always construct signed integer vector type. */
11351 intt = c_common_type_for_size (GET_MODE_BITSIZE
11352 (TYPE_MODE (TREE_TYPE (type0))), 0);
11353 result_type = build_opaque_vector_type (intt,
11354 TYPE_VECTOR_SUBPARTS (type0));
11355 converted = 1;
9f47c7e5
IE
11356 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11357 goto return_build_binary_op;
d246ab4f 11358 }
3e4093b6 11359 build_type = integer_type_node;
ab22c1fa
CF
11360 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11361 || code0 == FIXED_POINT_TYPE)
11362 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11363 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11364 short_compare = 1;
11365 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11366 {
36c5e70a
BE
11367 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11368 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11369 addr_space_t as_common;
11370
744aa42f 11371 if (comp_target_types (location, type0, type1))
3e4093b6 11372 {
10bc1b1b 11373 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
11374 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11375 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 11376 pedwarn (location, 0,
509c9d60 11377 "comparison of complete and incomplete pointers");
fcf73884 11378 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 11379 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11380 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
11381 else if (null_pointer_constant_p (orig_op0)
11382 || null_pointer_constant_p (orig_op1))
11383 warning_at (location, OPT_Wextra,
11384 "ordered comparison of pointer with null pointer");
11385
3e4093b6 11386 }
36c5e70a
BE
11387 else if (!addr_space_superset (as0, as1, &as_common))
11388 {
11389 error_at (location, "comparison of pointers to "
11390 "disjoint address spaces");
11391 return error_mark_node;
11392 }
3e4093b6
RS
11393 else
11394 {
36c5e70a
BE
11395 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11396 result_type = build_pointer_type
11397 (build_qualified_type (void_type_node, qual));
ba47d38d 11398 pedwarn (location, 0,
509c9d60 11399 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
11400 }
11401 }
6aa3c60d 11402 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
11403 {
11404 result_type = type0;
fcf73884 11405 if (pedantic)
c1771a20 11406 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
11407 "ordered comparison of pointer with integer zero");
11408 else if (extra_warnings)
ba47d38d 11409 warning_at (location, OPT_Wextra,
d42ba3b8 11410 "ordered comparison of pointer with integer zero");
3e4093b6 11411 }
6aa3c60d 11412 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
11413 {
11414 result_type = type1;
d42ba3b8 11415 if (pedantic)
c1771a20 11416 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
11417 "ordered comparison of pointer with integer zero");
11418 else if (extra_warnings)
11419 warning_at (location, OPT_Wextra,
11420 "ordered comparison of pointer with integer zero");
3e4093b6
RS
11421 }
11422 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11423 {
11424 result_type = type0;
ba47d38d 11425 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
11426 }
11427 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11428 {
11429 result_type = type1;
ba47d38d 11430 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 11431 }
04159acf
MP
11432 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11433 || truth_value_p (TREE_CODE (orig_op0)))
11434 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11435 || truth_value_p (TREE_CODE (orig_op1))))
11436 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11437 break;
64094f6a 11438
3e4093b6 11439 default:
37b2f290 11440 gcc_unreachable ();
c9fe6f9f 11441 }
8f17b5c5 11442
e57e265b
PB
11443 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11444 return error_mark_node;
11445
5bed876a
AH
11446 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11447 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 11448 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 11449 {
745e411d
DM
11450 gcc_rich_location richloc (location);
11451 richloc.maybe_add_expr (orig_op0);
11452 richloc.maybe_add_expr (orig_op1);
11453 binary_op_error (&richloc, code, type0, type1);
5bed876a
AH
11454 return error_mark_node;
11455 }
11456
3e4093b6 11457 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 11458 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
11459 &&
11460 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 11461 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 11462 {
2ca862e9
JM
11463 bool first_complex = (code0 == COMPLEX_TYPE);
11464 bool second_complex = (code1 == COMPLEX_TYPE);
11465 int none_complex = (!first_complex && !second_complex);
39b726dd 11466
3e4093b6 11467 if (shorten || common || short_compare)
3bf6bfcc
JJ
11468 {
11469 result_type = c_common_type (type0, type1);
0a0b3574
MM
11470 do_warn_double_promotion (result_type, type0, type1,
11471 "implicit conversion from %qT to %qT "
11472 "to match other operand of binary "
11473 "expression",
11474 location);
3bf6bfcc
JJ
11475 if (result_type == error_mark_node)
11476 return error_mark_node;
11477 }
400fbf9f 11478
2ca862e9
JM
11479 if (first_complex != second_complex
11480 && (code == PLUS_EXPR
11481 || code == MINUS_EXPR
11482 || code == MULT_EXPR
11483 || (code == TRUNC_DIV_EXPR && first_complex))
11484 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11485 && flag_signed_zeros)
11486 {
11487 /* An operation on mixed real/complex operands must be
11488 handled specially, but the language-independent code can
11489 more easily optimize the plain complex arithmetic if
11490 -fno-signed-zeros. */
11491 tree real_type = TREE_TYPE (result_type);
11492 tree real, imag;
11493 if (type0 != orig_type0 || type1 != orig_type1)
11494 {
11495 gcc_assert (may_need_excess_precision && common);
2d2e923f 11496 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11497 }
11498 if (first_complex)
11499 {
11500 if (TREE_TYPE (op0) != result_type)
68fca595 11501 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11502 if (TREE_TYPE (op1) != real_type)
68fca595 11503 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11504 }
11505 else
11506 {
11507 if (TREE_TYPE (op0) != real_type)
68fca595 11508 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11509 if (TREE_TYPE (op1) != result_type)
68fca595 11510 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11511 }
11512 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11513 return error_mark_node;
11514 if (first_complex)
11515 {
11516 op0 = c_save_expr (op0);
11517 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
e51fbec3 11518 op0, true);
2ca862e9 11519 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
e51fbec3 11520 op0, true);
2ca862e9
JM
11521 switch (code)
11522 {
11523 case MULT_EXPR:
11524 case TRUNC_DIV_EXPR:
c4603e7c 11525 op1 = c_save_expr (op1);
2ca862e9
JM
11526 imag = build2 (resultcode, real_type, imag, op1);
11527 /* Fall through. */
11528 case PLUS_EXPR:
11529 case MINUS_EXPR:
11530 real = build2 (resultcode, real_type, real, op1);
11531 break;
11532 default:
11533 gcc_unreachable();
11534 }
11535 }
11536 else
11537 {
11538 op1 = c_save_expr (op1);
11539 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
e51fbec3 11540 op1, true);
2ca862e9 11541 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
e51fbec3 11542 op1, true);
2ca862e9
JM
11543 switch (code)
11544 {
11545 case MULT_EXPR:
c4603e7c 11546 op0 = c_save_expr (op0);
2ca862e9
JM
11547 imag = build2 (resultcode, real_type, op0, imag);
11548 /* Fall through. */
11549 case PLUS_EXPR:
11550 real = build2 (resultcode, real_type, op0, real);
11551 break;
11552 case MINUS_EXPR:
11553 real = build2 (resultcode, real_type, op0, real);
11554 imag = build1 (NEGATE_EXPR, real_type, imag);
11555 break;
11556 default:
11557 gcc_unreachable();
11558 }
11559 }
11560 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11561 goto return_build_binary_op;
11562 }
11563
3e4093b6
RS
11564 /* For certain operations (which identify themselves by shorten != 0)
11565 if both args were extended from the same smaller type,
11566 do the arithmetic in that type and then extend.
400fbf9f 11567
3e4093b6
RS
11568 shorten !=0 and !=1 indicates a bitwise operation.
11569 For them, this optimization is safe only if
11570 both args are zero-extended or both are sign-extended.
11571 Otherwise, we might change the result.
11572 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11573 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11574
3e4093b6
RS
11575 if (shorten && none_complex)
11576 {
3e4093b6 11577 final_type = result_type;
b8698a0f 11578 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11579 shorten == -1);
3e4093b6 11580 }
88a3dbc1 11581
3e4093b6 11582 /* Shifts can be shortened if shifting right. */
2f6e4e97 11583
3e4093b6
RS
11584 if (short_shift)
11585 {
11586 int unsigned_arg;
11587 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11588
3e4093b6 11589 final_type = result_type;
abe80e6d 11590
3e4093b6 11591 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11592 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11593
3e4093b6 11594 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11595 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11596 /* We can shorten only if the shift count is less than the
11597 number of bits in the smaller type size. */
11598 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11599 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11600 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11601 {
11602 /* Do an unsigned shift if the operand was zero-extended. */
11603 result_type
11604 = c_common_signed_or_unsigned_type (unsigned_arg,
11605 TREE_TYPE (arg0));
11606 /* Convert value-to-be-shifted to that type. */
11607 if (TREE_TYPE (op0) != result_type)
11608 op0 = convert (result_type, op0);
11609 converted = 1;
abe80e6d 11610 }
88a3dbc1
RK
11611 }
11612
3e4093b6
RS
11613 /* Comparison operations are shortened too but differently.
11614 They identify themselves by setting short_compare = 1. */
56cb9733 11615
3e4093b6
RS
11616 if (short_compare)
11617 {
11618 /* Don't write &op0, etc., because that would prevent op0
11619 from being kept in a register.
11620 Instead, make copies of the our local variables and
11621 pass the copies by reference, then copy them back afterward. */
11622 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11623 enum tree_code xresultcode = resultcode;
11624 tree val
393e8e8b
MP
11625 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11626 &xresultcode);
8f17b5c5 11627
3e4093b6 11628 if (val != 0)
c9f9eb5d
AH
11629 {
11630 ret = val;
11631 goto return_build_binary_op;
11632 }
8f17b5c5 11633
3e4093b6
RS
11634 op0 = xop0, op1 = xop1;
11635 converted = 1;
11636 resultcode = xresultcode;
8f17b5c5 11637
7d882b83 11638 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11639 {
11640 bool op0_maybe_const = true;
11641 bool op1_maybe_const = true;
11642 tree orig_op0_folded, orig_op1_folded;
11643
11644 if (in_late_binary_op)
11645 {
11646 orig_op0_folded = orig_op0;
11647 orig_op1_folded = orig_op1;
11648 }
11649 else
11650 {
11651 /* Fold for the sake of possible warnings, as in
11652 build_conditional_expr. This requires the
11653 "original" values to be folded, not just op0 and
11654 op1. */
f5178456 11655 c_inhibit_evaluation_warnings++;
928c19bb
JM
11656 op0 = c_fully_fold (op0, require_constant_value,
11657 &op0_maybe_const);
11658 op1 = c_fully_fold (op1, require_constant_value,
11659 &op1_maybe_const);
f5178456 11660 c_inhibit_evaluation_warnings--;
928c19bb
JM
11661 orig_op0_folded = c_fully_fold (orig_op0,
11662 require_constant_value,
11663 NULL);
11664 orig_op1_folded = c_fully_fold (orig_op1,
11665 require_constant_value,
11666 NULL);
11667 }
11668
11669 if (warn_sign_compare)
11670 warn_for_sign_compare (location, orig_op0_folded,
11671 orig_op1_folded, op0, op1,
11672 result_type, resultcode);
5c2f94b4 11673 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11674 {
11675 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11676 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11677 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11678 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11679 }
3e4093b6 11680 }
2ad1815d 11681 }
64094f6a 11682 }
64094f6a 11683
3e4093b6
RS
11684 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11685 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11686 Then the expression will be built.
11687 It will be given type FINAL_TYPE if that is nonzero;
11688 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11689
3e4093b6
RS
11690 if (!result_type)
11691 {
745e411d
DM
11692 gcc_rich_location richloc (location);
11693 richloc.maybe_add_expr (orig_op0);
11694 richloc.maybe_add_expr (orig_op1);
11695 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11696 return error_mark_node;
11697 }
400fbf9f 11698
3e4093b6 11699 if (build_type == NULL_TREE)
8ce94e44
JM
11700 {
11701 build_type = result_type;
180f8dbb
JM
11702 if ((type0 != orig_type0 || type1 != orig_type1)
11703 && !boolean_op)
8ce94e44
JM
11704 {
11705 gcc_assert (may_need_excess_precision && common);
2d2e923f 11706 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11707 }
11708 }
400fbf9f 11709
2d2e923f
MLI
11710 if (!converted)
11711 {
68fca595
MP
11712 op0 = ep_convert_and_check (location, result_type, op0,
11713 semantic_result_type);
11714 op1 = ep_convert_and_check (location, result_type, op1,
11715 semantic_result_type);
2d2e923f
MLI
11716
11717 /* This can happen if one operand has a vector type, and the other
11718 has a different type. */
11719 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11720 return error_mark_node;
11721 }
11722
f8ed5150
MP
11723 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11724 | SANITIZE_FLOAT_DIVIDE))
802ac282 11725 && do_ubsan_in_current_function ()
b2aaf235
MP
11726 && (doing_div_or_mod || doing_shift)
11727 && !require_constant_value)
de5a5fa1
MP
11728 {
11729 /* OP0 and/or OP1 might have side-effects. */
11730 op0 = c_save_expr (op0);
11731 op1 = c_save_expr (op1);
11732 op0 = c_fully_fold (op0, false, NULL);
11733 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11734 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11735 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11736 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11737 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11738 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11739 }
11740
c9f9eb5d 11741 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11742 if (int_const_or_overflow)
11743 ret = (require_constant_value
db3927fb
AH
11744 ? fold_build2_initializer_loc (location, resultcode, build_type,
11745 op0, op1)
11746 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11747 else
11748 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11749 if (final_type != 0)
11750 ret = convert (final_type, ret);
11751
11752 return_build_binary_op:
11753 gcc_assert (ret != error_mark_node);
928c19bb
JM
11754 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11755 ret = (int_operands
11756 ? note_integer_operands (ret)
11757 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11758 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11759 && !in_late_binary_op)
11760 ret = note_integer_operands (ret);
2d2e923f
MLI
11761 if (semantic_result_type)
11762 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11763 protected_set_expr_location (ret, location);
de5a5fa1 11764
a24d975c 11765 if (instrument_expr != NULL)
de5a5fa1
MP
11766 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11767 instrument_expr, ret);
11768
c9f9eb5d 11769 return ret;
400fbf9f 11770}
85498824
JM
11771
11772
11773/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11774 purpose. LOCATION is the source location for the expression. */
85498824
JM
11775
11776tree
ba47d38d 11777c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11778{
928c19bb
JM
11779 bool int_const, int_operands;
11780
85498824
JM
11781 switch (TREE_CODE (TREE_TYPE (expr)))
11782 {
11783 case ARRAY_TYPE:
ba47d38d 11784 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11785 return error_mark_node;
11786
11787 case RECORD_TYPE:
ba47d38d 11788 error_at (location, "used struct type value where scalar is required");
85498824
JM
11789 return error_mark_node;
11790
11791 case UNION_TYPE:
ba47d38d 11792 error_at (location, "used union type value where scalar is required");
85498824
JM
11793 return error_mark_node;
11794
04af8788
NP
11795 case VOID_TYPE:
11796 error_at (location, "void value not ignored as it ought to be");
11797 return error_mark_node;
11798
1807ffc1
MS
11799 case POINTER_TYPE:
11800 if (reject_gcc_builtin (expr))
11801 return error_mark_node;
11802 break;
11803
46bdb9cf
JM
11804 case FUNCTION_TYPE:
11805 gcc_unreachable ();
11806
d246ab4f
AS
11807 case VECTOR_TYPE:
11808 error_at (location, "used vector type where scalar is required");
11809 return error_mark_node;
11810
85498824
JM
11811 default:
11812 break;
11813 }
11814
928c19bb
JM
11815 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11816 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11817 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11818 {
11819 expr = remove_c_maybe_const_expr (expr);
11820 expr = build2 (NE_EXPR, integer_type_node, expr,
11821 convert (TREE_TYPE (expr), integer_zero_node));
11822 expr = note_integer_operands (expr);
11823 }
11824 else
11825 /* ??? Should we also give an error for vectors rather than leaving
11826 those to give errors later? */
11827 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11828
11829 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11830 {
11831 if (TREE_OVERFLOW (expr))
11832 return expr;
11833 else
11834 return note_integer_operands (expr);
11835 }
11836 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11837 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11838 return expr;
85498824 11839}
73f397d4
JM
11840\f
11841
11842/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11843 required. */
11844
11845tree
51eed280 11846c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11847{
11848 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11849 {
11850 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11851 /* Executing a compound literal inside a function reinitializes
11852 it. */
11853 if (!TREE_STATIC (decl))
11854 *se = true;
11855 return decl;
11856 }
11857 else
11858 return expr;
11859}
953ff289 11860\f
88bae6f4
TS
11861/* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11862 statement. LOC is the location of the construct. */
41dbbb37
TS
11863
11864tree
88bae6f4
TS
11865c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11866 tree clauses)
41dbbb37 11867{
88bae6f4 11868 body = c_end_compound_stmt (loc, body, true);
41dbbb37 11869
88bae6f4 11870 tree stmt = make_node (code);
41dbbb37 11871 TREE_TYPE (stmt) = void_type_node;
88bae6f4
TS
11872 OMP_BODY (stmt) = body;
11873 OMP_CLAUSES (stmt) = clauses;
41dbbb37
TS
11874 SET_EXPR_LOCATION (stmt, loc);
11875
11876 return add_stmt (stmt);
11877}
11878
11879/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11880 statement. LOC is the location of the OACC_DATA. */
11881
11882tree
11883c_finish_oacc_data (location_t loc, tree clauses, tree block)
11884{
11885 tree stmt;
11886
11887 block = c_end_compound_stmt (loc, block, true);
11888
11889 stmt = make_node (OACC_DATA);
11890 TREE_TYPE (stmt) = void_type_node;
11891 OACC_DATA_CLAUSES (stmt) = clauses;
11892 OACC_DATA_BODY (stmt) = block;
11893 SET_EXPR_LOCATION (stmt, loc);
11894
11895 return add_stmt (stmt);
11896}
11897
37d5ad46
JB
11898/* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11899 statement. LOC is the location of the OACC_HOST_DATA. */
11900
11901tree
11902c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
11903{
11904 tree stmt;
11905
11906 block = c_end_compound_stmt (loc, block, true);
11907
11908 stmt = make_node (OACC_HOST_DATA);
11909 TREE_TYPE (stmt) = void_type_node;
11910 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
11911 OACC_HOST_DATA_BODY (stmt) = block;
11912 SET_EXPR_LOCATION (stmt, loc);
11913
11914 return add_stmt (stmt);
11915}
11916
c0220ea4 11917/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11918
11919tree
11920c_begin_omp_parallel (void)
11921{
11922 tree block;
11923
11924 keep_next_level ();
11925 block = c_begin_compound_stmt (true);
11926
11927 return block;
11928}
11929
c2255bc4
AH
11930/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11931 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11932
953ff289 11933tree
c2255bc4 11934c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11935{
11936 tree stmt;
11937
c2255bc4 11938 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11939
11940 stmt = make_node (OMP_PARALLEL);
11941 TREE_TYPE (stmt) = void_type_node;
11942 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11943 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11944 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11945
11946 return add_stmt (stmt);
11947}
11948
a68ab351
JJ
11949/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11950
11951tree
11952c_begin_omp_task (void)
11953{
11954 tree block;
11955
11956 keep_next_level ();
11957 block = c_begin_compound_stmt (true);
11958
11959 return block;
11960}
11961
c2255bc4
AH
11962/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11963 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11964
11965tree
c2255bc4 11966c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11967{
11968 tree stmt;
11969
c2255bc4 11970 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11971
11972 stmt = make_node (OMP_TASK);
11973 TREE_TYPE (stmt) = void_type_node;
11974 OMP_TASK_CLAUSES (stmt) = clauses;
11975 OMP_TASK_BODY (stmt) = block;
c2255bc4 11976 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11977
11978 return add_stmt (stmt);
11979}
11980
acf0174b
JJ
11981/* Generate GOMP_cancel call for #pragma omp cancel. */
11982
11983void
11984c_finish_omp_cancel (location_t loc, tree clauses)
11985{
11986 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11987 int mask = 0;
11988 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11989 mask = 1;
11990 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11991 mask = 2;
11992 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11993 mask = 4;
11994 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11995 mask = 8;
11996 else
11997 {
54d19c3b 11998 error_at (loc, "%<#pragma omp cancel%> must specify one of "
acf0174b
JJ
11999 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12000 "clauses");
12001 return;
12002 }
12003 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
12004 if (ifc != NULL_TREE)
12005 {
12006 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12007 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12008 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12009 build_zero_cst (type));
12010 }
12011 else
12012 ifc = boolean_true_node;
12013 tree stmt = build_call_expr_loc (loc, fn, 2,
12014 build_int_cst (integer_type_node, mask),
12015 ifc);
12016 add_stmt (stmt);
12017}
12018
12019/* Generate GOMP_cancellation_point call for
12020 #pragma omp cancellation point. */
12021
12022void
12023c_finish_omp_cancellation_point (location_t loc, tree clauses)
12024{
12025 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12026 int mask = 0;
12027 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
12028 mask = 1;
12029 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
12030 mask = 2;
12031 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
12032 mask = 4;
12033 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
12034 mask = 8;
12035 else
12036 {
54d19c3b 12037 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
acf0174b
JJ
12038 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12039 "clauses");
12040 return;
12041 }
12042 tree stmt = build_call_expr_loc (loc, fn, 1,
12043 build_int_cst (integer_type_node, mask));
12044 add_stmt (stmt);
12045}
12046
12047/* Helper function for handle_omp_array_sections. Called recursively
12048 to handle multiple array-section-subscripts. C is the clause,
12049 T current expression (initially OMP_CLAUSE_DECL), which is either
12050 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12051 expression if specified, TREE_VALUE length expression if specified,
12052 TREE_CHAIN is what it has been specified after, or some decl.
12053 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12054 set to true if any of the array-section-subscript could have length
12055 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12056 first array-section-subscript which is known not to have length
12057 of one. Given say:
12058 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12059 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12060 all are or may have length of 1, array-section-subscript [:2] is the
d9a6bd32 12061 first one known not to have length 1. For array-section-subscript
acf0174b
JJ
12062 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12063 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12064 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12065 case though, as some lengths could be zero. */
12066
12067static tree
12068handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
d9a6bd32 12069 bool &maybe_zero_len, unsigned int &first_non_one,
e46c7770 12070 enum c_omp_region_type ort)
acf0174b
JJ
12071{
12072 tree ret, low_bound, length, type;
12073 if (TREE_CODE (t) != TREE_LIST)
12074 {
7a0ca710 12075 if (error_operand_p (t))
acf0174b 12076 return error_mark_node;
d9a6bd32 12077 ret = t;
9dc5773f
JJ
12078 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12079 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12080 {
12081 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12082 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12083 return error_mark_node;
12084 }
d9a6bd32 12085 if (TREE_CODE (t) == COMPONENT_REF
e46c7770 12086 && ort == C_ORT_OMP
d9a6bd32
JJ
12087 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12088 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12089 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12090 {
12091 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12092 {
12093 error_at (OMP_CLAUSE_LOCATION (c),
12094 "bit-field %qE in %qs clause",
12095 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12096 return error_mark_node;
12097 }
12098 while (TREE_CODE (t) == COMPONENT_REF)
12099 {
12100 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12101 {
12102 error_at (OMP_CLAUSE_LOCATION (c),
12103 "%qE is a member of a union", t);
12104 return error_mark_node;
12105 }
12106 t = TREE_OPERAND (t, 0);
12107 }
12108 }
0ae9bd27 12109 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12110 {
12111 if (DECL_P (t))
12112 error_at (OMP_CLAUSE_LOCATION (c),
12113 "%qD is not a variable in %qs clause", t,
12114 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12115 else
12116 error_at (OMP_CLAUSE_LOCATION (c),
12117 "%qE is not a variable in %qs clause", t,
12118 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12119 return error_mark_node;
12120 }
12121 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
9dc5773f
JJ
12122 && TYPE_ATOMIC (TREE_TYPE (t)))
12123 {
12124 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12125 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12126 return error_mark_node;
12127 }
12128 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12129 && VAR_P (t)
12130 && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
12131 {
12132 error_at (OMP_CLAUSE_LOCATION (c),
12133 "%qD is threadprivate variable in %qs clause", t,
12134 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12135 return error_mark_node;
12136 }
9dc5773f
JJ
12137 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12138 && TYPE_ATOMIC (TREE_TYPE (t))
12139 && POINTER_TYPE_P (TREE_TYPE (t)))
12140 {
12141 /* If the array section is pointer based and the pointer
12142 itself is _Atomic qualified, we need to atomically load
12143 the pointer. */
12144 c_expr expr;
12145 memset (&expr, 0, sizeof (expr));
12146 expr.value = ret;
12147 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12148 expr, false, false);
12149 ret = expr.value;
12150 }
d9a6bd32 12151 return ret;
acf0174b
JJ
12152 }
12153
12154 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
e46c7770 12155 maybe_zero_len, first_non_one, ort);
acf0174b
JJ
12156 if (ret == error_mark_node || ret == NULL_TREE)
12157 return ret;
12158
12159 type = TREE_TYPE (ret);
12160 low_bound = TREE_PURPOSE (t);
12161 length = TREE_VALUE (t);
12162
12163 if (low_bound == error_mark_node || length == error_mark_node)
12164 return error_mark_node;
12165
12166 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12167 {
12168 error_at (OMP_CLAUSE_LOCATION (c),
12169 "low bound %qE of array section does not have integral type",
12170 low_bound);
12171 return error_mark_node;
12172 }
12173 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12174 {
12175 error_at (OMP_CLAUSE_LOCATION (c),
12176 "length %qE of array section does not have integral type",
12177 length);
12178 return error_mark_node;
12179 }
12180 if (low_bound
12181 && TREE_CODE (low_bound) == INTEGER_CST
12182 && TYPE_PRECISION (TREE_TYPE (low_bound))
12183 > TYPE_PRECISION (sizetype))
12184 low_bound = fold_convert (sizetype, low_bound);
12185 if (length
12186 && TREE_CODE (length) == INTEGER_CST
12187 && TYPE_PRECISION (TREE_TYPE (length))
12188 > TYPE_PRECISION (sizetype))
12189 length = fold_convert (sizetype, length);
12190 if (low_bound == NULL_TREE)
12191 low_bound = integer_zero_node;
12192
12193 if (length != NULL_TREE)
12194 {
12195 if (!integer_nonzerop (length))
d9a6bd32
JJ
12196 {
12197 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12198 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12199 {
12200 if (integer_zerop (length))
12201 {
12202 error_at (OMP_CLAUSE_LOCATION (c),
12203 "zero length array section in %qs clause",
12204 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12205 return error_mark_node;
12206 }
12207 }
12208 else
12209 maybe_zero_len = true;
12210 }
acf0174b
JJ
12211 if (first_non_one == types.length ()
12212 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12213 first_non_one++;
12214 }
12215 if (TREE_CODE (type) == ARRAY_TYPE)
12216 {
12217 if (length == NULL_TREE
12218 && (TYPE_DOMAIN (type) == NULL_TREE
12219 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12220 {
12221 error_at (OMP_CLAUSE_LOCATION (c),
12222 "for unknown bound array type length expression must "
12223 "be specified");
12224 return error_mark_node;
12225 }
12226 if (TREE_CODE (low_bound) == INTEGER_CST
12227 && tree_int_cst_sgn (low_bound) == -1)
12228 {
12229 error_at (OMP_CLAUSE_LOCATION (c),
12230 "negative low bound in array section in %qs clause",
12231 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12232 return error_mark_node;
12233 }
12234 if (length != NULL_TREE
12235 && TREE_CODE (length) == INTEGER_CST
12236 && tree_int_cst_sgn (length) == -1)
12237 {
12238 error_at (OMP_CLAUSE_LOCATION (c),
12239 "negative length in array section in %qs clause",
12240 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12241 return error_mark_node;
12242 }
12243 if (TYPE_DOMAIN (type)
12244 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12245 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12246 == INTEGER_CST)
12247 {
12248 tree size = size_binop (PLUS_EXPR,
12249 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12250 size_one_node);
12251 if (TREE_CODE (low_bound) == INTEGER_CST)
12252 {
12253 if (tree_int_cst_lt (size, low_bound))
12254 {
12255 error_at (OMP_CLAUSE_LOCATION (c),
12256 "low bound %qE above array section size "
12257 "in %qs clause", low_bound,
12258 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12259 return error_mark_node;
12260 }
12261 if (tree_int_cst_equal (size, low_bound))
d9a6bd32
JJ
12262 {
12263 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12264 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12265 {
12266 error_at (OMP_CLAUSE_LOCATION (c),
12267 "zero length array section in %qs clause",
12268 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12269 return error_mark_node;
12270 }
12271 maybe_zero_len = true;
12272 }
acf0174b
JJ
12273 else if (length == NULL_TREE
12274 && first_non_one == types.length ()
12275 && tree_int_cst_equal
12276 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12277 low_bound))
12278 first_non_one++;
12279 }
12280 else if (length == NULL_TREE)
12281 {
d9a6bd32
JJ
12282 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12283 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12284 maybe_zero_len = true;
acf0174b
JJ
12285 if (first_non_one == types.length ())
12286 first_non_one++;
12287 }
12288 if (length && TREE_CODE (length) == INTEGER_CST)
12289 {
12290 if (tree_int_cst_lt (size, length))
12291 {
12292 error_at (OMP_CLAUSE_LOCATION (c),
12293 "length %qE above array section size "
12294 "in %qs clause", length,
12295 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12296 return error_mark_node;
12297 }
12298 if (TREE_CODE (low_bound) == INTEGER_CST)
12299 {
12300 tree lbpluslen
12301 = size_binop (PLUS_EXPR,
12302 fold_convert (sizetype, low_bound),
12303 fold_convert (sizetype, length));
12304 if (TREE_CODE (lbpluslen) == INTEGER_CST
12305 && tree_int_cst_lt (size, lbpluslen))
12306 {
12307 error_at (OMP_CLAUSE_LOCATION (c),
12308 "high bound %qE above array section size "
12309 "in %qs clause", lbpluslen,
12310 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12311 return error_mark_node;
12312 }
12313 }
12314 }
12315 }
12316 else if (length == NULL_TREE)
12317 {
d9a6bd32
JJ
12318 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12319 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12320 maybe_zero_len = true;
acf0174b
JJ
12321 if (first_non_one == types.length ())
12322 first_non_one++;
12323 }
12324
12325 /* For [lb:] we will need to evaluate lb more than once. */
12326 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12327 {
12328 tree lb = c_save_expr (low_bound);
12329 if (lb != low_bound)
12330 {
12331 TREE_PURPOSE (t) = lb;
12332 low_bound = lb;
12333 }
12334 }
12335 }
12336 else if (TREE_CODE (type) == POINTER_TYPE)
12337 {
12338 if (length == NULL_TREE)
12339 {
12340 error_at (OMP_CLAUSE_LOCATION (c),
12341 "for pointer type length expression must be specified");
12342 return error_mark_node;
12343 }
d9a6bd32
JJ
12344 if (length != NULL_TREE
12345 && TREE_CODE (length) == INTEGER_CST
12346 && tree_int_cst_sgn (length) == -1)
12347 {
12348 error_at (OMP_CLAUSE_LOCATION (c),
12349 "negative length in array section in %qs clause",
12350 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12351 return error_mark_node;
12352 }
acf0174b
JJ
12353 /* If there is a pointer type anywhere but in the very first
12354 array-section-subscript, the array section can't be contiguous. */
12355 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12356 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12357 {
12358 error_at (OMP_CLAUSE_LOCATION (c),
12359 "array section is not contiguous in %qs clause",
12360 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12361 return error_mark_node;
12362 }
12363 }
12364 else
12365 {
12366 error_at (OMP_CLAUSE_LOCATION (c),
12367 "%qE does not have pointer or array type", ret);
12368 return error_mark_node;
12369 }
12370 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12371 types.safe_push (TREE_TYPE (ret));
12372 /* We will need to evaluate lb more than once. */
12373 tree lb = c_save_expr (low_bound);
12374 if (lb != low_bound)
12375 {
12376 TREE_PURPOSE (t) = lb;
12377 low_bound = lb;
12378 }
12379 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12380 return ret;
12381}
12382
12383/* Handle array sections for clause C. */
12384
12385static bool
e46c7770 12386handle_omp_array_sections (tree c, enum c_omp_region_type ort)
acf0174b
JJ
12387{
12388 bool maybe_zero_len = false;
12389 unsigned int first_non_one = 0;
d9a6bd32 12390 auto_vec<tree, 10> types;
acf0174b 12391 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
d9a6bd32 12392 maybe_zero_len, first_non_one,
e46c7770 12393 ort);
acf0174b 12394 if (first == error_mark_node)
d9a6bd32 12395 return true;
acf0174b 12396 if (first == NULL_TREE)
d9a6bd32 12397 return false;
acf0174b
JJ
12398 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12399 {
12400 tree t = OMP_CLAUSE_DECL (c);
12401 tree tem = NULL_TREE;
acf0174b
JJ
12402 /* Need to evaluate side effects in the length expressions
12403 if any. */
12404 while (TREE_CODE (t) == TREE_LIST)
12405 {
12406 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12407 {
12408 if (tem == NULL_TREE)
12409 tem = TREE_VALUE (t);
12410 else
12411 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12412 TREE_VALUE (t), tem);
12413 }
12414 t = TREE_CHAIN (t);
12415 }
12416 if (tem)
12417 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12418 first = c_fully_fold (first, false, NULL);
12419 OMP_CLAUSE_DECL (c) = first;
12420 }
12421 else
12422 {
12423 unsigned int num = types.length (), i;
12424 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12425 tree condition = NULL_TREE;
12426
12427 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12428 maybe_zero_len = true;
12429
12430 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12431 t = TREE_CHAIN (t))
12432 {
12433 tree low_bound = TREE_PURPOSE (t);
12434 tree length = TREE_VALUE (t);
12435
12436 i--;
12437 if (low_bound
12438 && TREE_CODE (low_bound) == INTEGER_CST
12439 && TYPE_PRECISION (TREE_TYPE (low_bound))
12440 > TYPE_PRECISION (sizetype))
12441 low_bound = fold_convert (sizetype, low_bound);
12442 if (length
12443 && TREE_CODE (length) == INTEGER_CST
12444 && TYPE_PRECISION (TREE_TYPE (length))
12445 > TYPE_PRECISION (sizetype))
12446 length = fold_convert (sizetype, length);
12447 if (low_bound == NULL_TREE)
12448 low_bound = integer_zero_node;
12449 if (!maybe_zero_len && i > first_non_one)
12450 {
12451 if (integer_nonzerop (low_bound))
12452 goto do_warn_noncontiguous;
12453 if (length != NULL_TREE
12454 && TREE_CODE (length) == INTEGER_CST
12455 && TYPE_DOMAIN (types[i])
12456 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12457 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12458 == INTEGER_CST)
12459 {
12460 tree size;
12461 size = size_binop (PLUS_EXPR,
12462 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12463 size_one_node);
12464 if (!tree_int_cst_equal (length, size))
12465 {
12466 do_warn_noncontiguous:
12467 error_at (OMP_CLAUSE_LOCATION (c),
12468 "array section is not contiguous in %qs "
12469 "clause",
12470 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
acf0174b
JJ
12471 return true;
12472 }
12473 }
12474 if (length != NULL_TREE
12475 && TREE_SIDE_EFFECTS (length))
12476 {
12477 if (side_effects == NULL_TREE)
12478 side_effects = length;
12479 else
12480 side_effects = build2 (COMPOUND_EXPR,
12481 TREE_TYPE (side_effects),
12482 length, side_effects);
12483 }
12484 }
12485 else
12486 {
12487 tree l;
12488
d9a6bd32
JJ
12489 if (i > first_non_one
12490 && ((length && integer_nonzerop (length))
12491 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
acf0174b
JJ
12492 continue;
12493 if (length)
12494 l = fold_convert (sizetype, length);
12495 else
12496 {
12497 l = size_binop (PLUS_EXPR,
12498 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12499 size_one_node);
12500 l = size_binop (MINUS_EXPR, l,
12501 fold_convert (sizetype, low_bound));
12502 }
12503 if (i > first_non_one)
12504 {
12505 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12506 size_zero_node);
12507 if (condition == NULL_TREE)
12508 condition = l;
12509 else
12510 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12511 l, condition);
12512 }
12513 else if (size == NULL_TREE)
12514 {
12515 size = size_in_bytes (TREE_TYPE (types[i]));
d9a6bd32
JJ
12516 tree eltype = TREE_TYPE (types[num - 1]);
12517 while (TREE_CODE (eltype) == ARRAY_TYPE)
12518 eltype = TREE_TYPE (eltype);
12519 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12520 {
12521 if (integer_zerop (size)
12522 || integer_zerop (size_in_bytes (eltype)))
12523 {
12524 error_at (OMP_CLAUSE_LOCATION (c),
12525 "zero length array section in %qs clause",
12526 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12527 return error_mark_node;
12528 }
12529 size = size_binop (EXACT_DIV_EXPR, size,
12530 size_in_bytes (eltype));
12531 }
acf0174b
JJ
12532 size = size_binop (MULT_EXPR, size, l);
12533 if (condition)
12534 size = fold_build3 (COND_EXPR, sizetype, condition,
12535 size, size_zero_node);
12536 }
12537 else
12538 size = size_binop (MULT_EXPR, size, l);
12539 }
12540 }
acf0174b
JJ
12541 if (side_effects)
12542 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
d9a6bd32
JJ
12543 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12544 {
12545 size = size_binop (MINUS_EXPR, size, size_one_node);
12546 size = c_fully_fold (size, false, NULL);
12547 tree index_type = build_index_type (size);
12548 tree eltype = TREE_TYPE (first);
12549 while (TREE_CODE (eltype) == ARRAY_TYPE)
12550 eltype = TREE_TYPE (eltype);
12551 tree type = build_array_type (eltype, index_type);
12552 tree ptype = build_pointer_type (eltype);
12553 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12554 t = build_fold_addr_expr (t);
e01d41e5
JJ
12555 tree t2 = build_fold_addr_expr (first);
12556 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12557 ptrdiff_type_node, t2);
12558 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12559 ptrdiff_type_node, t2,
12560 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12561 ptrdiff_type_node, t));
12562 t2 = c_fully_fold (t2, false, NULL);
12563 if (tree_fits_shwi_p (t2))
12564 t = build2 (MEM_REF, type, t,
12565 build_int_cst (ptype, tree_to_shwi (t2)));
12566 else
12567 {
12568 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12569 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12570 TREE_TYPE (t), t, t2);
12571 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12572 }
d9a6bd32
JJ
12573 OMP_CLAUSE_DECL (c) = t;
12574 return false;
12575 }
acf0174b
JJ
12576 first = c_fully_fold (first, false, NULL);
12577 OMP_CLAUSE_DECL (c) = first;
12578 if (size)
12579 size = c_fully_fold (size, false, NULL);
12580 OMP_CLAUSE_SIZE (c) = size;
d9a6bd32
JJ
12581 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12582 || (TREE_CODE (t) == COMPONENT_REF
12583 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
acf0174b 12584 return false;
41dbbb37 12585 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
e46c7770 12586 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
d9a6bd32
JJ
12587 switch (OMP_CLAUSE_MAP_KIND (c))
12588 {
12589 case GOMP_MAP_ALLOC:
12590 case GOMP_MAP_TO:
12591 case GOMP_MAP_FROM:
12592 case GOMP_MAP_TOFROM:
12593 case GOMP_MAP_ALWAYS_TO:
12594 case GOMP_MAP_ALWAYS_FROM:
12595 case GOMP_MAP_ALWAYS_TOFROM:
12596 case GOMP_MAP_RELEASE:
12597 case GOMP_MAP_DELETE:
b1c9c068
CP
12598 case GOMP_MAP_FORCE_TO:
12599 case GOMP_MAP_FORCE_FROM:
12600 case GOMP_MAP_FORCE_TOFROM:
12601 case GOMP_MAP_FORCE_PRESENT:
d9a6bd32
JJ
12602 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12603 break;
12604 default:
12605 break;
12606 }
acf0174b 12607 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
e46c7770 12608 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
e01d41e5
JJ
12609 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12610 else if (TREE_CODE (t) == COMPONENT_REF)
12611 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12612 else
12613 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12614 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12615 && !c_mark_addressable (t))
acf0174b
JJ
12616 return false;
12617 OMP_CLAUSE_DECL (c2) = t;
12618 t = build_fold_addr_expr (first);
12619 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12620 tree ptr = OMP_CLAUSE_DECL (c2);
12621 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12622 ptr = build_fold_addr_expr (ptr);
12623 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12624 ptrdiff_type_node, t,
12625 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12626 ptrdiff_type_node, ptr));
12627 t = c_fully_fold (t, false, NULL);
12628 OMP_CLAUSE_SIZE (c2) = t;
12629 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12630 OMP_CLAUSE_CHAIN (c) = c2;
12631 }
12632 return false;
12633}
12634
12635/* Helper function of finish_omp_clauses. Clone STMT as if we were making
12636 an inline call. But, remap
12637 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12638 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12639
12640static tree
12641c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12642 tree decl, tree placeholder)
12643{
12644 copy_body_data id;
b787e7a2 12645 hash_map<tree, tree> decl_map;
acf0174b 12646
b787e7a2
TS
12647 decl_map.put (omp_decl1, placeholder);
12648 decl_map.put (omp_decl2, decl);
acf0174b
JJ
12649 memset (&id, 0, sizeof (id));
12650 id.src_fn = DECL_CONTEXT (omp_decl1);
12651 id.dst_fn = current_function_decl;
12652 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 12653 id.decl_map = &decl_map;
acf0174b
JJ
12654
12655 id.copy_decl = copy_decl_no_change;
12656 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12657 id.transform_new_cfg = true;
12658 id.transform_return_to_modify = false;
12659 id.transform_lang_insert_block = NULL;
12660 id.eh_lp_nr = 0;
12661 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
12662 return stmt;
12663}
12664
12665/* Helper function of c_finish_omp_clauses, called via walk_tree.
12666 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12667
12668static tree
12669c_find_omp_placeholder_r (tree *tp, int *, void *data)
12670{
12671 if (*tp == (tree) data)
12672 return *tp;
12673 return NULL_TREE;
12674}
12675
41dbbb37 12676/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
12677 Remove any elements from the list that are invalid. */
12678
12679tree
77886428 12680c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
953ff289
DN
12681{
12682 bitmap_head generic_head, firstprivate_head, lastprivate_head;
e46c7770 12683 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
d9a6bd32
JJ
12684 tree c, t, type, *pc;
12685 tree simdlen = NULL_TREE, safelen = NULL_TREE;
acf0174b
JJ
12686 bool branch_seen = false;
12687 bool copyprivate_seen = false;
e01d41e5 12688 bool linear_variable_step_check = false;
acf0174b 12689 tree *nowait_clause = NULL;
e01d41e5
JJ
12690 bool ordered_seen = false;
12691 tree schedule_clause = NULL_TREE;
b605f663 12692 bool oacc_async = false;
953ff289
DN
12693
12694 bitmap_obstack_initialize (NULL);
12695 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12696 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12697 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 12698 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
d9a6bd32
JJ
12699 bitmap_initialize (&map_head, &bitmap_default_obstack);
12700 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
e46c7770 12701 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
953ff289 12702
b605f663
CLT
12703 if (ort & C_ORT_ACC)
12704 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12705 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12706 {
12707 oacc_async = true;
12708 break;
12709 }
9dc5773f 12710
953ff289
DN
12711 for (pc = &clauses, c = clauses; c ; c = *pc)
12712 {
12713 bool remove = false;
12714 bool need_complete = false;
12715 bool need_implicitly_determined = false;
12716
aaf46ef9 12717 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
12718 {
12719 case OMP_CLAUSE_SHARED:
953ff289
DN
12720 need_implicitly_determined = true;
12721 goto check_dup_generic;
12722
12723 case OMP_CLAUSE_PRIVATE:
953ff289
DN
12724 need_complete = true;
12725 need_implicitly_determined = true;
12726 goto check_dup_generic;
12727
12728 case OMP_CLAUSE_REDUCTION:
953ff289
DN
12729 need_implicitly_determined = true;
12730 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12731 if (TREE_CODE (t) == TREE_LIST)
12732 {
e46c7770 12733 if (handle_omp_array_sections (c, ort))
d9a6bd32
JJ
12734 {
12735 remove = true;
12736 break;
12737 }
12738
12739 t = OMP_CLAUSE_DECL (c);
12740 }
4f2e1536 12741 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
d9a6bd32
JJ
12742 if (t == error_mark_node)
12743 {
12744 remove = true;
12745 break;
12746 }
b605f663
CLT
12747 if (oacc_async)
12748 c_mark_addressable (t);
d9a6bd32
JJ
12749 type = TREE_TYPE (t);
12750 if (TREE_CODE (t) == MEM_REF)
12751 type = TREE_TYPE (type);
12752 if (TREE_CODE (type) == ARRAY_TYPE)
12753 {
12754 tree oatype = type;
12755 gcc_assert (TREE_CODE (t) != MEM_REF);
12756 while (TREE_CODE (type) == ARRAY_TYPE)
12757 type = TREE_TYPE (type);
12758 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12759 {
12760 error_at (OMP_CLAUSE_LOCATION (c),
12761 "%qD in %<reduction%> clause is a zero size array",
12762 t);
12763 remove = true;
12764 break;
12765 }
12766 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12767 TYPE_SIZE_UNIT (type));
12768 if (integer_zerop (size))
12769 {
12770 error_at (OMP_CLAUSE_LOCATION (c),
12771 "%qD in %<reduction%> clause is a zero size array",
12772 t);
12773 remove = true;
12774 break;
12775 }
12776 size = size_binop (MINUS_EXPR, size, size_one_node);
12777 tree index_type = build_index_type (size);
12778 tree atype = build_array_type (type, index_type);
12779 tree ptype = build_pointer_type (type);
12780 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12781 t = build_fold_addr_expr (t);
12782 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12783 OMP_CLAUSE_DECL (c) = t;
12784 }
9dc5773f
JJ
12785 if (TYPE_ATOMIC (type))
12786 {
12787 error_at (OMP_CLAUSE_LOCATION (c),
12788 "%<_Atomic%> %qE in %<reduction%> clause", t);
12789 remove = true;
12790 break;
12791 }
acf0174b 12792 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
d9a6bd32
JJ
12793 && (FLOAT_TYPE_P (type)
12794 || TREE_CODE (type) == COMPLEX_TYPE))
953ff289
DN
12795 {
12796 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12797 const char *r_name = NULL;
12798
12799 switch (r_code)
12800 {
12801 case PLUS_EXPR:
12802 case MULT_EXPR:
12803 case MINUS_EXPR:
652fea39 12804 break;
20906c66 12805 case MIN_EXPR:
d9a6bd32 12806 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39
JJ
12807 r_name = "min";
12808 break;
20906c66 12809 case MAX_EXPR:
d9a6bd32 12810 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39 12811 r_name = "max";
953ff289
DN
12812 break;
12813 case BIT_AND_EXPR:
12814 r_name = "&";
12815 break;
12816 case BIT_XOR_EXPR:
12817 r_name = "^";
12818 break;
12819 case BIT_IOR_EXPR:
12820 r_name = "|";
12821 break;
12822 case TRUTH_ANDIF_EXPR:
d9a6bd32 12823 if (FLOAT_TYPE_P (type))
652fea39 12824 r_name = "&&";
953ff289
DN
12825 break;
12826 case TRUTH_ORIF_EXPR:
d9a6bd32 12827 if (FLOAT_TYPE_P (type))
652fea39 12828 r_name = "||";
953ff289
DN
12829 break;
12830 default:
12831 gcc_unreachable ();
12832 }
12833 if (r_name)
12834 {
c2255bc4
AH
12835 error_at (OMP_CLAUSE_LOCATION (c),
12836 "%qE has invalid type for %<reduction(%s)%>",
12837 t, r_name);
953ff289 12838 remove = true;
ee1d5a02 12839 break;
953ff289
DN
12840 }
12841 }
acf0174b
JJ
12842 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12843 {
12844 error_at (OMP_CLAUSE_LOCATION (c),
d9a6bd32 12845 "user defined reduction not found for %qE", t);
acf0174b 12846 remove = true;
ee1d5a02 12847 break;
acf0174b
JJ
12848 }
12849 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12850 {
12851 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
d9a6bd32 12852 type = TYPE_MAIN_VARIANT (type);
acf0174b
JJ
12853 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12854 VAR_DECL, NULL_TREE, type);
d9a6bd32 12855 tree decl_placeholder = NULL_TREE;
acf0174b
JJ
12856 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12857 DECL_ARTIFICIAL (placeholder) = 1;
12858 DECL_IGNORED_P (placeholder) = 1;
d9a6bd32
JJ
12859 if (TREE_CODE (t) == MEM_REF)
12860 {
12861 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12862 VAR_DECL, NULL_TREE, type);
12863 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12864 DECL_ARTIFICIAL (decl_placeholder) = 1;
12865 DECL_IGNORED_P (decl_placeholder) = 1;
12866 }
acf0174b
JJ
12867 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12868 c_mark_addressable (placeholder);
12869 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
d9a6bd32
JJ
12870 c_mark_addressable (decl_placeholder ? decl_placeholder
12871 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12872 OMP_CLAUSE_REDUCTION_MERGE (c)
12873 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12874 TREE_VEC_ELT (list, 0),
12875 TREE_VEC_ELT (list, 1),
d9a6bd32
JJ
12876 decl_placeholder ? decl_placeholder
12877 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b
JJ
12878 OMP_CLAUSE_REDUCTION_MERGE (c)
12879 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12880 void_type_node, NULL_TREE,
d9a6bd32 12881 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
acf0174b
JJ
12882 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12883 if (TREE_VEC_LENGTH (list) == 6)
12884 {
12885 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
d9a6bd32
JJ
12886 c_mark_addressable (decl_placeholder ? decl_placeholder
12887 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12888 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12889 c_mark_addressable (placeholder);
12890 tree init = TREE_VEC_ELT (list, 5);
12891 if (init == error_mark_node)
12892 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12893 OMP_CLAUSE_REDUCTION_INIT (c)
12894 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12895 TREE_VEC_ELT (list, 3),
d9a6bd32
JJ
12896 decl_placeholder ? decl_placeholder
12897 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b 12898 if (TREE_VEC_ELT (list, 5) == error_mark_node)
d9a6bd32
JJ
12899 {
12900 tree v = decl_placeholder ? decl_placeholder : t;
12901 OMP_CLAUSE_REDUCTION_INIT (c)
12902 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12903 OMP_CLAUSE_REDUCTION_INIT (c));
12904 }
acf0174b
JJ
12905 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12906 c_find_omp_placeholder_r,
12907 placeholder, NULL))
12908 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12909 }
12910 else
12911 {
12912 tree init;
d9a6bd32
JJ
12913 tree v = decl_placeholder ? decl_placeholder : t;
12914 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12915 init = build_constructor (TREE_TYPE (v), NULL);
acf0174b 12916 else
d9a6bd32 12917 init = fold_convert (TREE_TYPE (v), integer_zero_node);
acf0174b 12918 OMP_CLAUSE_REDUCTION_INIT (c)
d9a6bd32 12919 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
acf0174b
JJ
12920 }
12921 OMP_CLAUSE_REDUCTION_INIT (c)
12922 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12923 void_type_node, NULL_TREE,
12924 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12925 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12926 }
d9a6bd32
JJ
12927 if (TREE_CODE (t) == MEM_REF)
12928 {
12929 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12930 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12931 != INTEGER_CST)
12932 {
12933 sorry ("variable length element type in array "
12934 "%<reduction%> clause");
12935 remove = true;
12936 break;
12937 }
12938 t = TREE_OPERAND (t, 0);
e01d41e5
JJ
12939 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12940 t = TREE_OPERAND (t, 0);
d9a6bd32
JJ
12941 if (TREE_CODE (t) == ADDR_EXPR)
12942 t = TREE_OPERAND (t, 0);
12943 }
12944 goto check_dup_generic_t;
953ff289
DN
12945
12946 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
12947 copyprivate_seen = true;
12948 if (nowait_clause)
12949 {
12950 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12951 "%<nowait%> clause must not be used together "
12952 "with %<copyprivate%>");
12953 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12954 nowait_clause = NULL;
12955 }
953ff289
DN
12956 goto check_dup_generic;
12957
12958 case OMP_CLAUSE_COPYIN:
953ff289 12959 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12960 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
953ff289 12961 {
c2255bc4
AH
12962 error_at (OMP_CLAUSE_LOCATION (c),
12963 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12964 remove = true;
ee1d5a02 12965 break;
953ff289
DN
12966 }
12967 goto check_dup_generic;
12968
acf0174b 12969 case OMP_CLAUSE_LINEAR:
77886428 12970 if (ort != C_ORT_OMP_DECLARE_SIMD)
d9a6bd32 12971 need_implicitly_determined = true;
acf0174b 12972 t = OMP_CLAUSE_DECL (c);
77886428 12973 if (ort != C_ORT_OMP_DECLARE_SIMD
d9a6bd32
JJ
12974 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
12975 {
12976 error_at (OMP_CLAUSE_LOCATION (c),
12977 "modifier should not be specified in %<linear%> "
12978 "clause on %<simd%> or %<for%> constructs");
12979 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
12980 }
77886428 12981 if (ort & C_ORT_CILK)
acf0174b 12982 {
477d4906
IV
12983 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12984 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
12985 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12986 {
12987 error_at (OMP_CLAUSE_LOCATION (c),
12988 "linear clause applied to non-integral, "
12989 "non-floating, non-pointer variable with type %qT",
12990 TREE_TYPE (t));
12991 remove = true;
12992 break;
12993 }
12994 }
12995 else
12996 {
12997 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12998 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12999 {
13000 error_at (OMP_CLAUSE_LOCATION (c),
13001 "linear clause applied to non-integral non-pointer "
13002 "variable with type %qT", TREE_TYPE (t));
13003 remove = true;
13004 break;
13005 }
9dc5773f
JJ
13006 if (TYPE_ATOMIC (TREE_TYPE (t)))
13007 {
13008 error_at (OMP_CLAUSE_LOCATION (c),
13009 "%<_Atomic%> %qD in %<linear%> clause", t);
13010 remove = true;
13011 break;
13012 }
acf0174b 13013 }
77886428 13014 if (ort == C_ORT_OMP_DECLARE_SIMD)
e01d41e5
JJ
13015 {
13016 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13017 if (TREE_CODE (s) == PARM_DECL)
13018 {
13019 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13020 /* map_head bitmap is used as uniform_head if
13021 declare_simd. */
13022 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13023 linear_variable_step_check = true;
13024 goto check_dup_generic;
13025 }
13026 if (TREE_CODE (s) != INTEGER_CST)
13027 {
13028 error_at (OMP_CLAUSE_LOCATION (c),
13029 "%<linear%> clause step %qE is neither constant "
13030 "nor a parameter", s);
13031 remove = true;
13032 break;
13033 }
13034 }
acf0174b
JJ
13035 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13036 {
13037 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13038 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13039 OMP_CLAUSE_DECL (c), s);
13040 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
13041 sizetype, fold_convert (sizetype, s),
13042 fold_convert
13043 (sizetype, OMP_CLAUSE_DECL (c)));
acf0174b
JJ
13044 if (s == error_mark_node)
13045 s = size_one_node;
13046 OMP_CLAUSE_LINEAR_STEP (c) = s;
13047 }
da6f124d
JJ
13048 else
13049 OMP_CLAUSE_LINEAR_STEP (c)
13050 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
13051 goto check_dup_generic;
13052
953ff289
DN
13053 check_dup_generic:
13054 t = OMP_CLAUSE_DECL (c);
d9a6bd32 13055 check_dup_generic_t:
0ae9bd27 13056 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13057 {
c2255bc4 13058 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
13059 "%qE is not a variable in clause %qs", t,
13060 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13061 remove = true;
13062 }
e46c7770
CP
13063 else if (ort == C_ORT_ACC
13064 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13065 {
13066 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13067 {
13068 error ("%qD appears more than once in reduction clauses", t);
13069 remove = true;
13070 }
13071 else
13072 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13073 }
953ff289
DN
13074 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13075 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13076 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13077 {
c2255bc4
AH
13078 error_at (OMP_CLAUSE_LOCATION (c),
13079 "%qE appears more than once in data clauses", t);
953ff289
DN
13080 remove = true;
13081 }
e01d41e5
JJ
13082 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13083 && bitmap_bit_p (&map_head, DECL_UID (t)))
13084 {
e46c7770
CP
13085 if (ort == C_ORT_ACC)
13086 error ("%qD appears more than once in data clauses", t);
13087 else
13088 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13089 remove = true;
13090 }
953ff289
DN
13091 else
13092 bitmap_set_bit (&generic_head, DECL_UID (t));
13093 break;
13094
13095 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
13096 t = OMP_CLAUSE_DECL (c);
13097 need_complete = true;
13098 need_implicitly_determined = true;
0ae9bd27 13099 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13100 {
c2255bc4
AH
13101 error_at (OMP_CLAUSE_LOCATION (c),
13102 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
13103 remove = true;
13104 }
13105 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13106 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13107 {
c2255bc4
AH
13108 error_at (OMP_CLAUSE_LOCATION (c),
13109 "%qE appears more than once in data clauses", t);
953ff289
DN
13110 remove = true;
13111 }
e01d41e5
JJ
13112 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13113 {
e46c7770
CP
13114 if (ort == C_ORT_ACC)
13115 error ("%qD appears more than once in data clauses", t);
13116 else
13117 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13118 remove = true;
13119 }
953ff289
DN
13120 else
13121 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13122 break;
13123
13124 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
13125 t = OMP_CLAUSE_DECL (c);
13126 need_complete = true;
13127 need_implicitly_determined = true;
0ae9bd27 13128 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13129 {
c2255bc4
AH
13130 error_at (OMP_CLAUSE_LOCATION (c),
13131 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
13132 remove = true;
13133 }
13134 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13135 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13136 {
c2255bc4
AH
13137 error_at (OMP_CLAUSE_LOCATION (c),
13138 "%qE appears more than once in data clauses", t);
953ff289
DN
13139 remove = true;
13140 }
13141 else
13142 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13143 break;
13144
acf0174b
JJ
13145 case OMP_CLAUSE_ALIGNED:
13146 t = OMP_CLAUSE_DECL (c);
0ae9bd27 13147 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13148 {
13149 error_at (OMP_CLAUSE_LOCATION (c),
13150 "%qE is not a variable in %<aligned%> clause", t);
13151 remove = true;
13152 }
5a9785fb
JJ
13153 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13154 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13155 {
13156 error_at (OMP_CLAUSE_LOCATION (c),
13157 "%qE in %<aligned%> clause is neither a pointer nor "
13158 "an array", t);
13159 remove = true;
13160 }
9dc5773f
JJ
13161 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13162 {
13163 error_at (OMP_CLAUSE_LOCATION (c),
13164 "%<_Atomic%> %qD in %<aligned%> clause", t);
13165 remove = true;
13166 break;
13167 }
acf0174b
JJ
13168 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13169 {
13170 error_at (OMP_CLAUSE_LOCATION (c),
13171 "%qE appears more than once in %<aligned%> clauses",
13172 t);
13173 remove = true;
13174 }
13175 else
13176 bitmap_set_bit (&aligned_head, DECL_UID (t));
13177 break;
13178
13179 case OMP_CLAUSE_DEPEND:
13180 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
13181 if (t == NULL_TREE)
13182 {
13183 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13184 == OMP_CLAUSE_DEPEND_SOURCE);
13185 break;
13186 }
13187 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13188 {
13189 gcc_assert (TREE_CODE (t) == TREE_LIST);
13190 for (; t; t = TREE_CHAIN (t))
13191 {
13192 tree decl = TREE_VALUE (t);
13193 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13194 {
13195 tree offset = TREE_PURPOSE (t);
13196 bool neg = wi::neg_p ((wide_int) offset);
13197 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13198 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13199 neg ? MINUS_EXPR : PLUS_EXPR,
13200 decl, offset);
13201 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
13202 sizetype,
13203 fold_convert (sizetype, t2),
13204 fold_convert (sizetype, decl));
d9a6bd32
JJ
13205 if (t2 == error_mark_node)
13206 {
13207 remove = true;
13208 break;
13209 }
13210 TREE_PURPOSE (t) = t2;
13211 }
13212 }
13213 break;
13214 }
acf0174b
JJ
13215 if (TREE_CODE (t) == TREE_LIST)
13216 {
e46c7770 13217 if (handle_omp_array_sections (c, ort))
acf0174b
JJ
13218 remove = true;
13219 break;
13220 }
13221 if (t == error_mark_node)
13222 remove = true;
0ae9bd27 13223 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13224 {
13225 error_at (OMP_CLAUSE_LOCATION (c),
13226 "%qE is not a variable in %<depend%> clause", t);
13227 remove = true;
13228 }
13229 else if (!c_mark_addressable (t))
13230 remove = true;
13231 break;
13232
13233 case OMP_CLAUSE_MAP:
13234 case OMP_CLAUSE_TO:
13235 case OMP_CLAUSE_FROM:
41dbbb37 13236 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
13237 t = OMP_CLAUSE_DECL (c);
13238 if (TREE_CODE (t) == TREE_LIST)
13239 {
e46c7770 13240 if (handle_omp_array_sections (c, ort))
acf0174b
JJ
13241 remove = true;
13242 else
13243 {
13244 t = OMP_CLAUSE_DECL (c);
b17a8b07 13245 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13246 {
13247 error_at (OMP_CLAUSE_LOCATION (c),
13248 "array section does not have mappable type "
13249 "in %qs clause",
13250 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13251 remove = true;
13252 }
9dc5773f
JJ
13253 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13254 {
13255 error_at (OMP_CLAUSE_LOCATION (c),
13256 "%<_Atomic%> %qE in %qs clause", t,
13257 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13258 remove = true;
13259 }
d9a6bd32
JJ
13260 while (TREE_CODE (t) == ARRAY_REF)
13261 t = TREE_OPERAND (t, 0);
13262 if (TREE_CODE (t) == COMPONENT_REF
13263 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13264 {
13265 while (TREE_CODE (t) == COMPONENT_REF)
13266 t = TREE_OPERAND (t, 0);
13267 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13268 break;
13269 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13270 {
13271 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13272 error ("%qD appears more than once in motion"
13273 " clauses", t);
e46c7770
CP
13274 else if (ort == C_ORT_ACC)
13275 error ("%qD appears more than once in data"
13276 " clauses", t);
d9a6bd32
JJ
13277 else
13278 error ("%qD appears more than once in map"
13279 " clauses", t);
13280 remove = true;
13281 }
13282 else
13283 {
13284 bitmap_set_bit (&map_head, DECL_UID (t));
13285 bitmap_set_bit (&map_field_head, DECL_UID (t));
13286 }
13287 }
acf0174b
JJ
13288 }
13289 break;
13290 }
13291 if (t == error_mark_node)
d9a6bd32
JJ
13292 {
13293 remove = true;
13294 break;
13295 }
13296 if (TREE_CODE (t) == COMPONENT_REF
77886428 13297 && (ort & C_ORT_OMP)
d9a6bd32
JJ
13298 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13299 {
13300 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13301 {
13302 error_at (OMP_CLAUSE_LOCATION (c),
13303 "bit-field %qE in %qs clause",
13304 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13305 remove = true;
13306 }
13307 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13308 {
13309 error_at (OMP_CLAUSE_LOCATION (c),
13310 "%qE does not have a mappable type in %qs clause",
13311 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13312 remove = true;
13313 }
9dc5773f
JJ
13314 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13315 {
13316 error_at (OMP_CLAUSE_LOCATION (c),
13317 "%<_Atomic%> %qE in %qs clause", t,
13318 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13319 remove = true;
13320 }
d9a6bd32
JJ
13321 while (TREE_CODE (t) == COMPONENT_REF)
13322 {
13323 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13324 == UNION_TYPE)
13325 {
13326 error_at (OMP_CLAUSE_LOCATION (c),
13327 "%qE is a member of a union", t);
13328 remove = true;
13329 break;
13330 }
13331 t = TREE_OPERAND (t, 0);
13332 }
13333 if (remove)
13334 break;
13335 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13336 {
e01d41e5 13337 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
d9a6bd32
JJ
13338 break;
13339 }
13340 }
13341 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13342 {
13343 error_at (OMP_CLAUSE_LOCATION (c),
13344 "%qE is not a variable in %qs clause", t,
13345 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13346 remove = true;
13347 }
0ae9bd27 13348 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
13349 {
13350 error_at (OMP_CLAUSE_LOCATION (c),
13351 "%qD is threadprivate variable in %qs clause", t,
13352 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13353 remove = true;
13354 }
e4606348
JJ
13355 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13356 || (OMP_CLAUSE_MAP_KIND (c)
13357 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13358 && !c_mark_addressable (t))
acf0174b 13359 remove = true;
b17a8b07 13360 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37 13361 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
d9a6bd32
JJ
13362 || (OMP_CLAUSE_MAP_KIND (c)
13363 == GOMP_MAP_FIRSTPRIVATE_POINTER)
41dbbb37
TS
13364 || (OMP_CLAUSE_MAP_KIND (c)
13365 == GOMP_MAP_FORCE_DEVICEPTR)))
d9a6bd32 13366 && t == OMP_CLAUSE_DECL (c)
b17a8b07 13367 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13368 {
13369 error_at (OMP_CLAUSE_LOCATION (c),
13370 "%qD does not have a mappable type in %qs clause", t,
13371 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13372 remove = true;
13373 }
9dc5773f
JJ
13374 else if (TREE_TYPE (t) == error_mark_node)
13375 remove = true;
13376 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13377 {
13378 error_at (OMP_CLAUSE_LOCATION (c),
13379 "%<_Atomic%> %qE in %qs clause", t,
13380 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13381 remove = true;
13382 }
d9a6bd32
JJ
13383 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13384 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13385 {
13386 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13387 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13388 {
13389 error ("%qD appears more than once in data clauses", t);
13390 remove = true;
13391 }
e01d41e5 13392 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
d9a6bd32 13393 {
e46c7770
CP
13394 if (ort == C_ORT_ACC)
13395 error ("%qD appears more than once in data clauses", t);
13396 else
13397 error ("%qD appears both in data and map clauses", t);
e01d41e5 13398 remove = true;
d9a6bd32 13399 }
e01d41e5
JJ
13400 else
13401 bitmap_set_bit (&generic_head, DECL_UID (t));
d9a6bd32
JJ
13402 }
13403 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
acf0174b
JJ
13404 {
13405 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13406 error ("%qD appears more than once in motion clauses", t);
e46c7770
CP
13407 else if (ort == C_ORT_ACC)
13408 error ("%qD appears more than once in data clauses", t);
acf0174b
JJ
13409 else
13410 error ("%qD appears more than once in map clauses", t);
13411 remove = true;
13412 }
e01d41e5
JJ
13413 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13414 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13415 {
e46c7770
CP
13416 if (ort == C_ORT_ACC)
13417 error ("%qD appears more than once in data clauses", t);
13418 else
13419 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13420 remove = true;
13421 }
acf0174b 13422 else
d9a6bd32
JJ
13423 {
13424 bitmap_set_bit (&map_head, DECL_UID (t));
13425 if (t != OMP_CLAUSE_DECL (c)
13426 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13427 bitmap_set_bit (&map_field_head, DECL_UID (t));
13428 }
13429 break;
13430
13431 case OMP_CLAUSE_TO_DECLARE:
d9a6bd32
JJ
13432 case OMP_CLAUSE_LINK:
13433 t = OMP_CLAUSE_DECL (c);
e01d41e5
JJ
13434 if (TREE_CODE (t) == FUNCTION_DECL
13435 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13436 ;
13437 else if (!VAR_P (t))
d9a6bd32 13438 {
e01d41e5
JJ
13439 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13440 error_at (OMP_CLAUSE_LOCATION (c),
13441 "%qE is neither a variable nor a function name in "
13442 "clause %qs", t,
13443 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13444 else
13445 error_at (OMP_CLAUSE_LOCATION (c),
13446 "%qE is not a variable in clause %qs", t,
13447 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
d9a6bd32
JJ
13448 remove = true;
13449 }
13450 else if (DECL_THREAD_LOCAL_P (t))
13451 {
13452 error_at (OMP_CLAUSE_LOCATION (c),
13453 "%qD is threadprivate variable in %qs clause", t,
13454 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13455 remove = true;
13456 }
13457 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13458 {
13459 error_at (OMP_CLAUSE_LOCATION (c),
13460 "%qD does not have a mappable type in %qs clause", t,
13461 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13462 remove = true;
13463 }
e01d41e5
JJ
13464 if (remove)
13465 break;
13466 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13467 {
13468 error_at (OMP_CLAUSE_LOCATION (c),
13469 "%qE appears more than once on the same "
13470 "%<declare target%> directive", t);
13471 remove = true;
13472 }
13473 else
13474 bitmap_set_bit (&generic_head, DECL_UID (t));
acf0174b
JJ
13475 break;
13476
13477 case OMP_CLAUSE_UNIFORM:
13478 t = OMP_CLAUSE_DECL (c);
13479 if (TREE_CODE (t) != PARM_DECL)
13480 {
13481 if (DECL_P (t))
13482 error_at (OMP_CLAUSE_LOCATION (c),
13483 "%qD is not an argument in %<uniform%> clause", t);
13484 else
13485 error_at (OMP_CLAUSE_LOCATION (c),
13486 "%qE is not an argument in %<uniform%> clause", t);
13487 remove = true;
ee1d5a02 13488 break;
acf0174b 13489 }
e01d41e5
JJ
13490 /* map_head bitmap is used as uniform_head if declare_simd. */
13491 bitmap_set_bit (&map_head, DECL_UID (t));
ee1d5a02 13492 goto check_dup_generic;
acf0174b 13493
d9a6bd32
JJ
13494 case OMP_CLAUSE_IS_DEVICE_PTR:
13495 case OMP_CLAUSE_USE_DEVICE_PTR:
13496 t = OMP_CLAUSE_DECL (c);
13497 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13498 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13499 {
13500 error_at (OMP_CLAUSE_LOCATION (c),
13501 "%qs variable is neither a pointer nor an array",
13502 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13503 remove = true;
13504 }
13505 goto check_dup_generic;
13506
acf0174b
JJ
13507 case OMP_CLAUSE_NOWAIT:
13508 if (copyprivate_seen)
13509 {
13510 error_at (OMP_CLAUSE_LOCATION (c),
13511 "%<nowait%> clause must not be used together "
13512 "with %<copyprivate%>");
13513 remove = true;
13514 break;
13515 }
13516 nowait_clause = pc;
13517 pc = &OMP_CLAUSE_CHAIN (c);
13518 continue;
13519
953ff289
DN
13520 case OMP_CLAUSE_IF:
13521 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
13522 case OMP_CLAUSE_NUM_TEAMS:
13523 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 13524 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
13525 case OMP_CLAUSE_UNTIED:
13526 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
13527 case OMP_CLAUSE_FINAL:
13528 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
13529 case OMP_CLAUSE_DEVICE:
13530 case OMP_CLAUSE_DIST_SCHEDULE:
13531 case OMP_CLAUSE_PARALLEL:
13532 case OMP_CLAUSE_FOR:
13533 case OMP_CLAUSE_SECTIONS:
13534 case OMP_CLAUSE_TASKGROUP:
13535 case OMP_CLAUSE_PROC_BIND:
d9a6bd32
JJ
13536 case OMP_CLAUSE_PRIORITY:
13537 case OMP_CLAUSE_GRAINSIZE:
13538 case OMP_CLAUSE_NUM_TASKS:
13539 case OMP_CLAUSE_NOGROUP:
13540 case OMP_CLAUSE_THREADS:
13541 case OMP_CLAUSE_SIMD:
13542 case OMP_CLAUSE_HINT:
13543 case OMP_CLAUSE_DEFAULTMAP:
9a771876 13544 case OMP_CLAUSE__CILK_FOR_COUNT_:
41dbbb37
TS
13545 case OMP_CLAUSE_NUM_GANGS:
13546 case OMP_CLAUSE_NUM_WORKERS:
13547 case OMP_CLAUSE_VECTOR_LENGTH:
13548 case OMP_CLAUSE_ASYNC:
13549 case OMP_CLAUSE_WAIT:
13550 case OMP_CLAUSE_AUTO:
7a5e4956 13551 case OMP_CLAUSE_INDEPENDENT:
41dbbb37
TS
13552 case OMP_CLAUSE_SEQ:
13553 case OMP_CLAUSE_GANG:
13554 case OMP_CLAUSE_WORKER:
13555 case OMP_CLAUSE_VECTOR:
7a5e4956 13556 case OMP_CLAUSE_TILE:
acf0174b
JJ
13557 pc = &OMP_CLAUSE_CHAIN (c);
13558 continue;
13559
e01d41e5
JJ
13560 case OMP_CLAUSE_SCHEDULE:
13561 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13562 {
13563 const char *p = NULL;
13564 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13565 {
13566 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13567 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13568 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13569 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13570 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13571 default: gcc_unreachable ();
13572 }
13573 if (p)
13574 {
13575 error_at (OMP_CLAUSE_LOCATION (c),
13576 "%<nonmonotonic%> modifier specified for %qs "
13577 "schedule kind", p);
13578 OMP_CLAUSE_SCHEDULE_KIND (c)
13579 = (enum omp_clause_schedule_kind)
13580 (OMP_CLAUSE_SCHEDULE_KIND (c)
13581 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13582 }
13583 }
13584 schedule_clause = c;
13585 pc = &OMP_CLAUSE_CHAIN (c);
13586 continue;
13587
13588 case OMP_CLAUSE_ORDERED:
13589 ordered_seen = true;
13590 pc = &OMP_CLAUSE_CHAIN (c);
13591 continue;
13592
d9a6bd32
JJ
13593 case OMP_CLAUSE_SAFELEN:
13594 safelen = c;
13595 pc = &OMP_CLAUSE_CHAIN (c);
13596 continue;
13597 case OMP_CLAUSE_SIMDLEN:
13598 simdlen = c;
13599 pc = &OMP_CLAUSE_CHAIN (c);
13600 continue;
13601
acf0174b
JJ
13602 case OMP_CLAUSE_INBRANCH:
13603 case OMP_CLAUSE_NOTINBRANCH:
13604 if (branch_seen)
13605 {
13606 error_at (OMP_CLAUSE_LOCATION (c),
13607 "%<inbranch%> clause is incompatible with "
13608 "%<notinbranch%>");
13609 remove = true;
13610 break;
13611 }
13612 branch_seen = true;
953ff289
DN
13613 pc = &OMP_CLAUSE_CHAIN (c);
13614 continue;
13615
13616 default:
13617 gcc_unreachable ();
13618 }
13619
13620 if (!remove)
13621 {
13622 t = OMP_CLAUSE_DECL (c);
13623
13624 if (need_complete)
13625 {
4f2e1536 13626 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
953ff289
DN
13627 if (t == error_mark_node)
13628 remove = true;
13629 }
13630
13631 if (need_implicitly_determined)
13632 {
13633 const char *share_name = NULL;
13634
0ae9bd27 13635 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
953ff289
DN
13636 share_name = "threadprivate";
13637 else switch (c_omp_predetermined_sharing (t))
13638 {
13639 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13640 break;
13641 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
13642 /* const vars may be specified in firstprivate clause. */
13643 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13644 && TREE_READONLY (t))
13645 break;
953ff289
DN
13646 share_name = "shared";
13647 break;
13648 case OMP_CLAUSE_DEFAULT_PRIVATE:
13649 share_name = "private";
13650 break;
13651 default:
13652 gcc_unreachable ();
13653 }
13654 if (share_name)
13655 {
c2255bc4
AH
13656 error_at (OMP_CLAUSE_LOCATION (c),
13657 "%qE is predetermined %qs for %qs",
acf0174b
JJ
13658 t, share_name,
13659 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13660 remove = true;
13661 }
13662 }
13663 }
13664
13665 if (remove)
13666 *pc = OMP_CLAUSE_CHAIN (c);
13667 else
13668 pc = &OMP_CLAUSE_CHAIN (c);
13669 }
13670
d9a6bd32
JJ
13671 if (simdlen
13672 && safelen
13673 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13674 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13675 {
13676 error_at (OMP_CLAUSE_LOCATION (simdlen),
13677 "%<simdlen%> clause value is bigger than "
13678 "%<safelen%> clause value");
13679 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13680 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13681 }
13682
e01d41e5
JJ
13683 if (ordered_seen
13684 && schedule_clause
13685 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13686 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13687 {
13688 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13689 "%<nonmonotonic%> schedule modifier specified together "
13690 "with %<ordered%> clause");
13691 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13692 = (enum omp_clause_schedule_kind)
13693 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13694 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13695 }
13696
13697 if (linear_variable_step_check)
13698 for (pc = &clauses, c = clauses; c ; c = *pc)
13699 {
13700 bool remove = false;
13701 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13702 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13703 && !bitmap_bit_p (&map_head,
13704 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13705 {
13706 error_at (OMP_CLAUSE_LOCATION (c),
13707 "%<linear%> clause step is a parameter %qD not "
13708 "specified in %<uniform%> clause",
13709 OMP_CLAUSE_LINEAR_STEP (c));
13710 remove = true;
13711 }
13712
13713 if (remove)
13714 *pc = OMP_CLAUSE_CHAIN (c);
13715 else
13716 pc = &OMP_CLAUSE_CHAIN (c);
13717 }
13718
953ff289
DN
13719 bitmap_obstack_release (NULL);
13720 return clauses;
13721}
9ae165a0 13722
9dc5773f
JJ
13723/* Return code to initialize DST with a copy constructor from SRC.
13724 C doesn't have copy constructors nor assignment operators, only for
13725 _Atomic vars we need to perform __atomic_load from src into a temporary
13726 followed by __atomic_store of the temporary to dst. */
13727
13728tree
13729c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13730{
13731 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13732 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13733
13734 location_t loc = OMP_CLAUSE_LOCATION (clause);
13735 tree type = TREE_TYPE (dst);
13736 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13737 tree tmp = create_tmp_var (nonatomic_type);
13738 tree tmp_addr = build_fold_addr_expr (tmp);
13739 TREE_ADDRESSABLE (tmp) = 1;
13740 TREE_NO_WARNING (tmp) = 1;
13741 tree src_addr = build_fold_addr_expr (src);
13742 tree dst_addr = build_fold_addr_expr (dst);
13743 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
13744 vec<tree, va_gc> *params;
13745 /* Expansion of a generic atomic load may require an addition
13746 element, so allocate enough to prevent a resize. */
13747 vec_alloc (params, 4);
13748
13749 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13750 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
13751 params->quick_push (src_addr);
13752 params->quick_push (tmp_addr);
13753 params->quick_push (seq_cst);
13754 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13755
13756 vec_alloc (params, 4);
13757
13758 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13759 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
13760 params->quick_push (dst_addr);
13761 params->quick_push (tmp_addr);
13762 params->quick_push (seq_cst);
13763 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13764 return build2 (COMPOUND_EXPR, void_type_node, load, store);
13765}
13766
0a35513e
AH
13767/* Create a transaction node. */
13768
13769tree
13770c_finish_transaction (location_t loc, tree block, int flags)
13771{
13772 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13773 if (flags & TM_STMT_ATTR_OUTER)
13774 TRANSACTION_EXPR_OUTER (stmt) = 1;
13775 if (flags & TM_STMT_ATTR_RELAXED)
13776 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13777 return add_stmt (stmt);
13778}
13779
9ae165a0 13780/* Make a variant type in the proper way for C/C++, propagating qualifiers
e9e32ee6
JM
13781 down to the element type of an array. If ORIG_QUAL_TYPE is not
13782 NULL, then it should be used as the qualified type
13783 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13784 preserve information about the typedef name from which an array
13785 type was derived). */
9ae165a0
DG
13786
13787tree
e9e32ee6
JM
13788c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13789 size_t orig_qual_indirect)
9ae165a0
DG
13790{
13791 if (type == error_mark_node)
13792 return type;
13793
13794 if (TREE_CODE (type) == ARRAY_TYPE)
13795 {
13796 tree t;
13797 tree element_type = c_build_qualified_type (TREE_TYPE (type),
e9e32ee6
JM
13798 type_quals, orig_qual_type,
13799 orig_qual_indirect - 1);
9ae165a0
DG
13800
13801 /* See if we already have an identically qualified type. */
e9e32ee6
JM
13802 if (orig_qual_type && orig_qual_indirect == 0)
13803 t = orig_qual_type;
13804 else
13805 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13806 {
13807 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13808 && TYPE_NAME (t) == TYPE_NAME (type)
13809 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13810 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13811 TYPE_ATTRIBUTES (type)))
13812 break;
13813 }
9ae165a0
DG
13814 if (!t)
13815 {
13816 tree domain = TYPE_DOMAIN (type);
13817
13818 t = build_variant_type_copy (type);
13819 TREE_TYPE (t) = element_type;
13820
13821 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13822 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13823 SET_TYPE_STRUCTURAL_EQUALITY (t);
13824 else if (TYPE_CANONICAL (element_type) != element_type
13825 || (domain && TYPE_CANONICAL (domain) != domain))
13826 {
b8698a0f 13827 tree unqualified_canon
9ae165a0 13828 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 13829 domain? TYPE_CANONICAL (domain)
9ae165a0 13830 : NULL_TREE);
ee45a32d
EB
13831 if (TYPE_REVERSE_STORAGE_ORDER (type))
13832 {
13833 unqualified_canon
13834 = build_distinct_type_copy (unqualified_canon);
13835 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13836 }
b8698a0f 13837 TYPE_CANONICAL (t)
9ae165a0
DG
13838 = c_build_qualified_type (unqualified_canon, type_quals);
13839 }
13840 else
13841 TYPE_CANONICAL (t) = t;
13842 }
13843 return t;
13844 }
13845
13846 /* A restrict-qualified pointer type must be a pointer to object or
13847 incomplete type. Note that the use of POINTER_TYPE_P also allows
13848 REFERENCE_TYPEs, which is appropriate for C++. */
13849 if ((type_quals & TYPE_QUAL_RESTRICT)
13850 && (!POINTER_TYPE_P (type)
13851 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13852 {
13853 error ("invalid use of %<restrict%>");
13854 type_quals &= ~TYPE_QUAL_RESTRICT;
13855 }
13856
e9e32ee6
JM
13857 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13858 ? orig_qual_type
13859 : build_qualified_type (type, type_quals));
e922069e
JW
13860 /* A variant type does not inherit the list of incomplete vars from the
13861 type main variant. */
152ef731
JJ
13862 if (RECORD_OR_UNION_TYPE_P (var_type)
13863 && TYPE_MAIN_VARIANT (var_type) != var_type)
e922069e
JW
13864 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13865 return var_type;
9ae165a0 13866}
72b5577d
ILT
13867
13868/* Build a VA_ARG_EXPR for the C parser. */
13869
13870tree
f187980b 13871c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
72b5577d 13872{
ec3fba51
MP
13873 if (error_operand_p (type))
13874 return error_mark_node;
f187980b
EB
13875 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13876 order because it takes the address of the expression. */
13877 else if (handled_component_p (expr)
13878 && reverse_storage_order_for_component_p (expr))
13879 {
13880 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13881 return error_mark_node;
13882 }
ec3fba51 13883 else if (!COMPLETE_TYPE_P (type))
4e81b788 13884 {
f187980b 13885 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
ec3fba51 13886 "type %qT", type);
4e81b788
MP
13887 return error_mark_node;
13888 }
ec3fba51 13889 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
f187980b 13890 warning_at (loc2, OPT_Wc___compat,
ec3fba51 13891 "C++ requires promoted type, not enum type, in %<va_arg%>");
f187980b 13892 return build_va_arg (loc2, expr, type);
72b5577d 13893}
acf0174b
JJ
13894
13895/* Return truthvalue of whether T1 is the same tree structure as T2.
13896 Return 1 if they are the same. Return 0 if they are different. */
13897
13898bool
13899c_tree_equal (tree t1, tree t2)
13900{
13901 enum tree_code code1, code2;
13902
13903 if (t1 == t2)
13904 return true;
13905 if (!t1 || !t2)
13906 return false;
13907
13908 for (code1 = TREE_CODE (t1);
13909 CONVERT_EXPR_CODE_P (code1)
13910 || code1 == NON_LVALUE_EXPR;
13911 code1 = TREE_CODE (t1))
13912 t1 = TREE_OPERAND (t1, 0);
13913 for (code2 = TREE_CODE (t2);
13914 CONVERT_EXPR_CODE_P (code2)
13915 || code2 == NON_LVALUE_EXPR;
13916 code2 = TREE_CODE (t2))
13917 t2 = TREE_OPERAND (t2, 0);
13918
13919 /* They might have become equal now. */
13920 if (t1 == t2)
13921 return true;
13922
13923 if (code1 != code2)
13924 return false;
13925
13926 switch (code1)
13927 {
13928 case INTEGER_CST:
807e902e 13929 return wi::eq_p (t1, t2);
acf0174b
JJ
13930
13931 case REAL_CST:
624d31fe 13932 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
acf0174b
JJ
13933
13934 case STRING_CST:
13935 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13936 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13937 TREE_STRING_LENGTH (t1));
13938
13939 case FIXED_CST:
13940 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13941 TREE_FIXED_CST (t2));
13942
13943 case COMPLEX_CST:
13944 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13945 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13946
13947 case VECTOR_CST:
13948 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13949
13950 case CONSTRUCTOR:
13951 /* We need to do this when determining whether or not two
13952 non-type pointer to member function template arguments
13953 are the same. */
13954 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13955 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13956 return false;
13957 {
13958 tree field, value;
13959 unsigned int i;
13960 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13961 {
13962 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13963 if (!c_tree_equal (field, elt2->index)
13964 || !c_tree_equal (value, elt2->value))
13965 return false;
13966 }
13967 }
13968 return true;
13969
13970 case TREE_LIST:
13971 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13972 return false;
13973 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
13974 return false;
13975 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
13976
13977 case SAVE_EXPR:
13978 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13979
13980 case CALL_EXPR:
13981 {
13982 tree arg1, arg2;
13983 call_expr_arg_iterator iter1, iter2;
13984 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
13985 return false;
13986 for (arg1 = first_call_expr_arg (t1, &iter1),
13987 arg2 = first_call_expr_arg (t2, &iter2);
13988 arg1 && arg2;
13989 arg1 = next_call_expr_arg (&iter1),
13990 arg2 = next_call_expr_arg (&iter2))
13991 if (!c_tree_equal (arg1, arg2))
13992 return false;
13993 if (arg1 || arg2)
13994 return false;
13995 return true;
13996 }
13997
13998 case TARGET_EXPR:
13999 {
14000 tree o1 = TREE_OPERAND (t1, 0);
14001 tree o2 = TREE_OPERAND (t2, 0);
14002
14003 /* Special case: if either target is an unallocated VAR_DECL,
14004 it means that it's going to be unified with whatever the
14005 TARGET_EXPR is really supposed to initialize, so treat it
14006 as being equivalent to anything. */
0ae9bd27 14007 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
acf0174b
JJ
14008 && !DECL_RTL_SET_P (o1))
14009 /*Nop*/;
0ae9bd27 14010 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
acf0174b
JJ
14011 && !DECL_RTL_SET_P (o2))
14012 /*Nop*/;
14013 else if (!c_tree_equal (o1, o2))
14014 return false;
14015
14016 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14017 }
14018
14019 case COMPONENT_REF:
14020 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14021 return false;
14022 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14023
14024 case PARM_DECL:
14025 case VAR_DECL:
14026 case CONST_DECL:
14027 case FIELD_DECL:
14028 case FUNCTION_DECL:
14029 case IDENTIFIER_NODE:
14030 case SSA_NAME:
14031 return false;
14032
14033 case TREE_VEC:
14034 {
14035 unsigned ix;
14036 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14037 return false;
14038 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14039 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14040 TREE_VEC_ELT (t2, ix)))
14041 return false;
14042 return true;
14043 }
14044
14045 default:
14046 break;
14047 }
14048
14049 switch (TREE_CODE_CLASS (code1))
14050 {
14051 case tcc_unary:
14052 case tcc_binary:
14053 case tcc_comparison:
14054 case tcc_expression:
14055 case tcc_vl_exp:
14056 case tcc_reference:
14057 case tcc_statement:
14058 {
14059 int i, n = TREE_OPERAND_LENGTH (t1);
14060
14061 switch (code1)
14062 {
14063 case PREINCREMENT_EXPR:
14064 case PREDECREMENT_EXPR:
14065 case POSTINCREMENT_EXPR:
14066 case POSTDECREMENT_EXPR:
14067 n = 1;
14068 break;
14069 case ARRAY_REF:
14070 n = 2;
14071 break;
14072 default:
14073 break;
14074 }
14075
14076 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14077 && n != TREE_OPERAND_LENGTH (t2))
14078 return false;
14079
14080 for (i = 0; i < n; ++i)
14081 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14082 return false;
14083
14084 return true;
14085 }
14086
14087 case tcc_type:
14088 return comptypes (t1, t2);
14089 default:
14090 gcc_unreachable ();
14091 }
14092 /* We can get here with --disable-checking. */
14093 return false;
14094}
12893402
BI
14095
14096/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14097 spawn-helper and BODY is the newly created body for FNDECL. */
14098
14099void
14100cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14101{
14102 tree list = alloc_stmt_list ();
14103 tree frame = make_cilk_frame (fndecl);
14104 tree dtor = create_cilk_function_exit (frame, false, true);
14105 add_local_decl (cfun, frame);
14106
14107 DECL_SAVED_TREE (fndecl) = list;
14108 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
14109 frame);
14110 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
14111 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
14112
14113 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
14114 append_to_statement_list (detach_expr, &body_list);
14115
14116 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14117 body = fold_build_cleanup_point_expr (void_type_node, body);
14118
14119 append_to_statement_list (body, &body_list);
14120 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14121 body_list, dtor), &list);
14122}
1807ffc1
MS
14123
14124/* Returns true when the function declaration FNDECL is implicit,
14125 introduced as a result of a call to an otherwise undeclared
14126 function, and false otherwise. */
14127
14128bool
14129c_decl_implicit (const_tree fndecl)
14130{
14131 return C_DECL_IMPLICIT (fndecl);
14132}