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