]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
re PR c++/77830 (internal compiler error: in output_constructor_regular_field, at...
[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"
4d0cdd0c 29#include "memmodel.h"
2adfab87
AM
30#include "target.h"
31#include "function.h"
32#include "bitmap.h"
2adfab87
AM
33#include "c-tree.h"
34#include "gimple-expr.h"
35#include "predict.h"
d8a2d370
DN
36#include "stor-layout.h"
37#include "trans-mem.h"
38#include "varasm.h"
39#include "stmt.h"
e57e265b 40#include "langhooks.h"
29cc57cf 41#include "c-lang.h"
ab87f8c8 42#include "intl.h"
325c3691 43#include "tree-iterator.h"
45b0be94 44#include "gimplify.h"
acf0174b 45#include "tree-inline.h"
629b3d75 46#include "omp-general.h"
61d3ce20 47#include "c-family/c-objc.h"
de5a5fa1 48#include "c-family/c-ubsan.h"
12893402 49#include "cilk.h"
41dbbb37 50#include "gomp-constants.h"
6a3f203c 51#include "spellcheck-tree.h"
745e411d 52#include "gcc-rich-location.h"
325c3691 53
2ac2f164
JM
54/* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
56enum impl_conv {
57 ic_argpass,
58 ic_assign,
59 ic_init,
60 ic_return
61};
62
bc4b653b
JM
63/* The level of nesting inside "__alignof__". */
64int in_alignof;
65
66/* The level of nesting inside "sizeof". */
67int in_sizeof;
68
69/* The level of nesting inside "typeof". */
70int in_typeof;
400fbf9f 71
1a4049e7
JJ
72/* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74tree c_last_sizeof_arg;
75
9bac5cbb
G
76/* Nonzero if we might need to print a "missing braces around
77 initializer" message within this initializer. */
78static int found_missing_braces;
103b7b17 79
bf730f15
RS
80static int require_constant_value;
81static int require_constant_elements;
82
58f9752a 83static bool null_pointer_constant_p (const_tree);
f55ade6e 84static tree qualify_type (tree, tree);
dc5027f4
JM
85static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
86 bool *);
744aa42f 87static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
88static int function_types_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 91static tree lookup_field (tree, tree);
81e5eca8
MP
92static int convert_arguments (location_t, vec<location_t>, tree,
93 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
94 tree);
db3927fb 95static tree pointer_diff (location_t, tree, tree);
68fca595 96static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 97 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
98static tree valid_compound_expr_initializer (tree, tree);
99static void push_string (const char *);
100static void push_member_name (tree);
f55ade6e
AJ
101static int spelling_length (void);
102static char *print_spelling (char *);
96b40f8d 103static void warning_init (location_t, int, const char *);
c2255bc4 104static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
105static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
106 bool, struct obstack *);
a1e3b3d9 107static void output_pending_init_elements (int, struct obstack *);
ea58ef42 108static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 109static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
110static void add_pending_init (location_t, tree, tree, tree, bool,
111 struct obstack *);
a1e3b3d9
LB
112static void set_nonincremental_init (struct obstack *);
113static void set_nonincremental_init_from_string (tree, struct obstack *);
114static tree find_init_member (tree, struct obstack *);
f37acdf9 115static void readonly_warning (tree, enum lvalue_use);
7bd11157 116static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 117static void record_maybe_used_decl (tree);
dc5027f4 118static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
119\f
120/* Return true if EXP is a null pointer constant, false otherwise. */
121
122static bool
58f9752a 123null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
124{
125 /* This should really operate on c_expr structures, but they aren't
126 yet available everywhere required. */
127 tree type = TREE_TYPE (expr);
128 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 129 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
130 && integer_zerop (expr)
131 && (INTEGRAL_TYPE_P (type)
132 || (TREE_CODE (type) == POINTER_TYPE
133 && VOID_TYPE_P (TREE_TYPE (type))
134 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
135}
928c19bb
JM
136
137/* EXPR may appear in an unevaluated part of an integer constant
138 expression, but not in an evaluated part. Wrap it in a
139 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
140 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
141
142static tree
143note_integer_operands (tree expr)
144{
145 tree ret;
146 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
147 {
148 ret = copy_node (expr);
149 TREE_OVERFLOW (ret) = 1;
150 }
151 else
152 {
153 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
154 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
155 }
156 return ret;
157}
158
4d84fe7c
JM
159/* Having checked whether EXPR may appear in an unevaluated part of an
160 integer constant expression and found that it may, remove any
161 C_MAYBE_CONST_EXPR noting this fact and return the resulting
162 expression. */
163
164static inline tree
165remove_c_maybe_const_expr (tree expr)
166{
167 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
168 return C_MAYBE_CONST_EXPR_EXPR (expr);
169 else
170 return expr;
171}
172
f13c9b2c
AP
173\f/* This is a cache to hold if two types are compatible or not. */
174
175struct tagged_tu_seen_cache {
176 const struct tagged_tu_seen_cache * next;
58f9752a
KG
177 const_tree t1;
178 const_tree t2;
f13c9b2c
AP
179 /* The return value of tagged_types_tu_compatible_p if we had seen
180 these two types already. */
181 int val;
182};
183
184static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
185static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
186
4f2e1536
MP
187/* Do `exp = require_complete_type (loc, exp);' to make sure exp
188 does not have an incomplete type. (That includes void types.)
189 LOC is the location of the use. */
400fbf9f
JW
190
191tree
4f2e1536 192require_complete_type (location_t loc, tree value)
400fbf9f
JW
193{
194 tree type = TREE_TYPE (value);
195
7a0ca710 196 if (error_operand_p (value))
ea0f786b
CB
197 return error_mark_node;
198
400fbf9f 199 /* First, detect a valid value with a complete type. */
d0f062fb 200 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
201 return value;
202
4f2e1536 203 c_incomplete_type_error (loc, value, type);
400fbf9f
JW
204 return error_mark_node;
205}
206
207/* Print an error message for invalid use of an incomplete type.
208 VALUE is the expression that was used (or 0 if that isn't known)
4f2e1536
MP
209 and TYPE is the type that was invalid. LOC is the location for
210 the error. */
400fbf9f
JW
211
212void
4f2e1536 213c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
400fbf9f 214{
400fbf9f
JW
215 /* Avoid duplicate error message. */
216 if (TREE_CODE (type) == ERROR_MARK)
217 return;
218
0ae9bd27 219 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
4f2e1536 220 error_at (loc, "%qD has an incomplete type %qT", value, type);
400fbf9f
JW
221 else
222 {
223 retry:
224 /* We must print an error message. Be clever about what it says. */
225
226 switch (TREE_CODE (type))
227 {
228 case RECORD_TYPE:
400fbf9f 229 case UNION_TYPE:
400fbf9f 230 case ENUMERAL_TYPE:
400fbf9f
JW
231 break;
232
233 case VOID_TYPE:
4f2e1536 234 error_at (loc, "invalid use of void expression");
400fbf9f
JW
235 return;
236
237 case ARRAY_TYPE:
238 if (TYPE_DOMAIN (type))
239 {
fba78abb
RH
240 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
241 {
4f2e1536 242 error_at (loc, "invalid use of flexible array member");
fba78abb
RH
243 return;
244 }
400fbf9f
JW
245 type = TREE_TYPE (type);
246 goto retry;
247 }
4f2e1536 248 error_at (loc, "invalid use of array with unspecified bounds");
400fbf9f
JW
249 return;
250
251 default:
366de0ce 252 gcc_unreachable ();
400fbf9f
JW
253 }
254
255 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
4f2e1536 256 error_at (loc, "invalid use of undefined type %qT", type);
400fbf9f
JW
257 else
258 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
4f2e1536 259 error_at (loc, "invalid use of incomplete typedef %qT", type);
400fbf9f
JW
260 }
261}
262
ab393bf1
NB
263/* Given a type, apply default promotions wrt unnamed function
264 arguments and return the new type. */
265
266tree
2f6e4e97 267c_type_promotes_to (tree type)
ab393bf1 268{
267bac10 269 tree ret = NULL_TREE;
ab393bf1 270
267bac10
JM
271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 ret = double_type_node;
273 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
274 {
275 /* Preserve unsignedness if not really getting any wider. */
8df83eae 276 if (TYPE_UNSIGNED (type)
c22cacf3 277 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
278 ret = unsigned_type_node;
279 else
280 ret = integer_type_node;
ab393bf1
NB
281 }
282
267bac10
JM
283 if (ret != NULL_TREE)
284 return (TYPE_ATOMIC (type)
285 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
286 : ret);
287
ab393bf1
NB
288 return type;
289}
290
36c5e70a
BE
291/* Return true if between two named address spaces, whether there is a superset
292 named address space that encompasses both address spaces. If there is a
293 superset, return which address space is the superset. */
294
295static bool
296addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
297{
298 if (as1 == as2)
299 {
300 *common = as1;
301 return true;
302 }
303 else if (targetm.addr_space.subset_p (as1, as2))
304 {
305 *common = as2;
306 return true;
307 }
308 else if (targetm.addr_space.subset_p (as2, as1))
309 {
310 *common = as1;
311 return true;
312 }
313 else
314 return false;
315}
316
400fbf9f
JW
317/* Return a variant of TYPE which has all the type qualifiers of LIKE
318 as well as those of TYPE. */
319
320static tree
2f6e4e97 321qualify_type (tree type, tree like)
400fbf9f 322{
36c5e70a
BE
323 addr_space_t as_type = TYPE_ADDR_SPACE (type);
324 addr_space_t as_like = TYPE_ADDR_SPACE (like);
325 addr_space_t as_common;
326
327 /* If the two named address spaces are different, determine the common
328 superset address space. If there isn't one, raise an error. */
329 if (!addr_space_superset (as_type, as_like, &as_common))
330 {
331 as_common = as_type;
332 error ("%qT and %qT are in disjoint named address spaces",
333 type, like);
334 }
335
2f6e4e97 336 return c_build_qualified_type (type,
36c5e70a 337 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 338 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 339 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 340}
52ffd86e
MS
341
342/* Return true iff the given tree T is a variable length array. */
343
344bool
ac7d7749 345c_vla_type_p (const_tree t)
52ffd86e
MS
346{
347 if (TREE_CODE (t) == ARRAY_TYPE
348 && C_TYPE_VARIABLE_SIZE (t))
349 return true;
350 return false;
351}
400fbf9f 352\f
10bc1b1b 353/* Return the composite type of two compatible types.
5305f6d7 354
10bc1b1b
JM
355 We assume that comptypes has already been done and returned
356 nonzero; if that isn't so, this may crash. In particular, we
357 assume that qualifiers match. */
400fbf9f
JW
358
359tree
10bc1b1b 360composite_type (tree t1, tree t2)
400fbf9f 361{
b3694847
SS
362 enum tree_code code1;
363 enum tree_code code2;
4b027d16 364 tree attributes;
400fbf9f
JW
365
366 /* Save time if the two types are the same. */
367
368 if (t1 == t2) return t1;
369
370 /* If one type is nonsense, use the other. */
371 if (t1 == error_mark_node)
372 return t2;
373 if (t2 == error_mark_node)
374 return t1;
375
10bc1b1b
JM
376 code1 = TREE_CODE (t1);
377 code2 = TREE_CODE (t2);
378
d9525bec 379 /* Merge the attributes. */
5fd9b178 380 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 381
10bc1b1b
JM
382 /* If one is an enumerated type and the other is the compatible
383 integer type, the composite type might be either of the two
384 (DR#013 question 3). For consistency, use the enumerated type as
385 the composite type. */
400fbf9f 386
10bc1b1b
JM
387 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
388 return t1;
389 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
390 return t2;
75326e8c 391
366de0ce 392 gcc_assert (code1 == code2);
b6a10c9f 393
400fbf9f
JW
394 switch (code1)
395 {
400fbf9f 396 case POINTER_TYPE:
10bc1b1b 397 /* For two pointers, do this recursively on the target type. */
400fbf9f 398 {
3932261a
MM
399 tree pointed_to_1 = TREE_TYPE (t1);
400 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 401 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 402 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
403 t1 = build_type_attribute_variant (t1, attributes);
404 return qualify_type (t1, t2);
400fbf9f 405 }
400fbf9f
JW
406
407 case ARRAY_TYPE:
408 {
10bc1b1b 409 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
410 int quals;
411 tree unqual_elt;
ca8bdb78
JM
412 tree d1 = TYPE_DOMAIN (t1);
413 tree d2 = TYPE_DOMAIN (t2);
414 bool d1_variable, d2_variable;
415 bool d1_zero, d2_zero;
f6294de7 416 bool t1_complete, t2_complete;
46df2823 417
de46b2fe 418 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
419 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
420 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 421
f6294de7
JM
422 t1_complete = COMPLETE_TYPE_P (t1);
423 t2_complete = COMPLETE_TYPE_P (t2);
424
ca8bdb78
JM
425 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
426 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
427
428 d1_variable = (!d1_zero
429 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
430 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
431 d2_variable = (!d2_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
434 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
435 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 436
400fbf9f 437 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
438 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
439 && (d2_variable || d2_zero || !d1_variable))
4b027d16 440 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
441 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
442 && (d1_variable || d1_zero || !d2_variable))
4b027d16 443 return build_type_attribute_variant (t2, attributes);
c22cacf3 444
de46b2fe
AP
445 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
446 return build_type_attribute_variant (t1, attributes);
447 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
448 return build_type_attribute_variant (t2, attributes);
c22cacf3 449
46df2823
JM
450 /* Merge the element types, and have a size if either arg has
451 one. We may have qualifiers on the element types. To set
452 up TYPE_MAIN_VARIANT correctly, we need to form the
453 composite of the unqualified types and add the qualifiers
454 back at the end. */
455 quals = TYPE_QUALS (strip_array_types (elt));
456 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
457 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
458 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
459 && (d2_variable
460 || d2_zero
461 || !d1_variable))
462 ? t1
463 : t2));
f6294de7
JM
464 /* Ensure a composite type involving a zero-length array type
465 is a zero-length type not an incomplete type. */
466 if (d1_zero && d2_zero
467 && (t1_complete || t2_complete)
468 && !COMPLETE_TYPE_P (t1))
469 {
470 TYPE_SIZE (t1) = bitsize_zero_node;
471 TYPE_SIZE_UNIT (t1) = size_zero_node;
472 }
46df2823 473 t1 = c_build_qualified_type (t1, quals);
de46b2fe 474 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
475 }
476
fcb99e7b
JJ
477 case ENUMERAL_TYPE:
478 case RECORD_TYPE:
479 case UNION_TYPE:
480 if (attributes != NULL)
481 {
482 /* Try harder not to create a new aggregate type. */
483 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
484 return t1;
485 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
486 return t2;
487 }
488 return build_type_attribute_variant (t1, attributes);
489
400fbf9f
JW
490 case FUNCTION_TYPE:
491 /* Function types: prefer the one that specified arg types.
492 If both do, merge the arg types. Also merge the return types. */
493 {
10bc1b1b 494 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
495 tree p1 = TYPE_ARG_TYPES (t1);
496 tree p2 = TYPE_ARG_TYPES (t2);
497 int len;
498 tree newargs, n;
499 int i;
500
501 /* Save space: see if the result is identical to one of the args. */
3f75a254 502 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 503 return build_type_attribute_variant (t1, attributes);
3f75a254 504 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 505 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
506
507 /* Simple way if one arg fails to specify argument types. */
508 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 509 {
fe7080d2
AP
510 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
511 t1 = build_type_attribute_variant (t1, attributes);
512 return qualify_type (t1, t2);
4b027d16 513 }
400fbf9f 514 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
515 {
516 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
517 t1 = build_type_attribute_variant (t1, attributes);
518 return qualify_type (t1, t2);
4b027d16 519 }
400fbf9f
JW
520
521 /* If both args specify argument types, we must merge the two
522 lists, argument by argument. */
2f4e8f2b 523
fcc2b74f
JJ
524 for (len = 0, newargs = p1;
525 newargs && newargs != void_list_node;
526 len++, newargs = TREE_CHAIN (newargs))
527 ;
400fbf9f
JW
528
529 for (i = 0; i < len; i++)
8d9bfdc5 530 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
531
532 n = newargs;
533
fcc2b74f 534 for (; p1 && p1 != void_list_node;
400fbf9f
JW
535 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
536 {
537 /* A null type means arg type is not specified.
538 Take whatever the other function type has. */
539 if (TREE_VALUE (p1) == 0)
540 {
541 TREE_VALUE (n) = TREE_VALUE (p2);
542 goto parm_done;
543 }
544 if (TREE_VALUE (p2) == 0)
545 {
546 TREE_VALUE (n) = TREE_VALUE (p1);
547 goto parm_done;
548 }
2f6e4e97 549
400fbf9f
JW
550 /* Given wait (union {union wait *u; int *i} *)
551 and wait (union wait *),
552 prefer union wait * as type of parm. */
553 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
554 && TREE_VALUE (p1) != TREE_VALUE (p2))
555 {
556 tree memb;
58cb41e6
JJ
557 tree mv2 = TREE_VALUE (p2);
558 if (mv2 && mv2 != error_mark_node
559 && TREE_CODE (mv2) != ARRAY_TYPE)
560 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 561 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 562 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
563 {
564 tree mv3 = TREE_TYPE (memb);
565 if (mv3 && mv3 != error_mark_node
566 && TREE_CODE (mv3) != ARRAY_TYPE)
567 mv3 = TYPE_MAIN_VARIANT (mv3);
568 if (comptypes (mv3, mv2))
569 {
570 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
571 TREE_VALUE (p2));
c1771a20 572 pedwarn (input_location, OPT_Wpedantic,
fcf73884 573 "function types not truly compatible in ISO C");
58cb41e6
JJ
574 goto parm_done;
575 }
576 }
400fbf9f
JW
577 }
578 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
579 && TREE_VALUE (p2) != TREE_VALUE (p1))
580 {
581 tree memb;
58cb41e6
JJ
582 tree mv1 = TREE_VALUE (p1);
583 if (mv1 && mv1 != error_mark_node
584 && TREE_CODE (mv1) != ARRAY_TYPE)
585 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 586 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 587 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
588 {
589 tree mv3 = TREE_TYPE (memb);
590 if (mv3 && mv3 != error_mark_node
591 && TREE_CODE (mv3) != ARRAY_TYPE)
592 mv3 = TYPE_MAIN_VARIANT (mv3);
593 if (comptypes (mv3, mv1))
594 {
595 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
596 TREE_VALUE (p1));
c1771a20 597 pedwarn (input_location, OPT_Wpedantic,
fcf73884 598 "function types not truly compatible in ISO C");
58cb41e6
JJ
599 goto parm_done;
600 }
601 }
400fbf9f 602 }
10bc1b1b 603 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
604 parm_done: ;
605 }
606
4b027d16 607 t1 = build_function_type (valtype, newargs);
fe7080d2 608 t1 = qualify_type (t1, t2);
400fbf9f 609 }
295844f6 610 /* FALLTHRU */
400fbf9f
JW
611
612 default:
4b027d16 613 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
614 }
615
616}
10bc1b1b
JM
617
618/* Return the type of a conditional expression between pointers to
619 possibly differently qualified versions of compatible types.
620
621 We assume that comp_target_types has already been done and returned
622 nonzero; if that isn't so, this may crash. */
623
624static tree
625common_pointer_type (tree t1, tree t2)
626{
627 tree attributes;
46df2823
JM
628 tree pointed_to_1, mv1;
629 tree pointed_to_2, mv2;
10bc1b1b 630 tree target;
eb1387a0 631 unsigned target_quals;
36c5e70a
BE
632 addr_space_t as1, as2, as_common;
633 int quals1, quals2;
10bc1b1b
JM
634
635 /* Save time if the two types are the same. */
636
637 if (t1 == t2) return t1;
638
639 /* If one type is nonsense, use the other. */
640 if (t1 == error_mark_node)
641 return t2;
642 if (t2 == error_mark_node)
643 return t1;
644
366de0ce 645 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 646 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
647
648 /* Merge the attributes. */
649 attributes = targetm.merge_type_attributes (t1, t2);
650
651 /* Find the composite type of the target types, and combine the
46df2823
JM
652 qualifiers of the two types' targets. Do not lose qualifiers on
653 array element types by taking the TYPE_MAIN_VARIANT. */
654 mv1 = pointed_to_1 = TREE_TYPE (t1);
655 mv2 = pointed_to_2 = TREE_TYPE (t2);
656 if (TREE_CODE (mv1) != ARRAY_TYPE)
657 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
658 if (TREE_CODE (mv2) != ARRAY_TYPE)
659 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
660 target = composite_type (mv1, mv2);
eb1387a0 661
768952be
MU
662 /* Strip array types to get correct qualifier for pointers to arrays */
663 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
664 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
665
eb1387a0
RG
666 /* For function types do not merge const qualifiers, but drop them
667 if used inconsistently. The middle-end uses these to mark const
668 and noreturn functions. */
669 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 670 target_quals = (quals1 & quals2);
eb1387a0 671 else
36c5e70a
BE
672 target_quals = (quals1 | quals2);
673
674 /* If the two named address spaces are different, determine the common
675 superset address space. This is guaranteed to exist due to the
676 assumption that comp_target_type returned non-zero. */
677 as1 = TYPE_ADDR_SPACE (pointed_to_1);
678 as2 = TYPE_ADDR_SPACE (pointed_to_2);
679 if (!addr_space_superset (as1, as2, &as_common))
680 gcc_unreachable ();
681
682 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
683
eb1387a0 684 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
685 return build_type_attribute_variant (t1, attributes);
686}
687
688/* Return the common type for two arithmetic types under the usual
689 arithmetic conversions. The default conversions have already been
690 applied, and enumerated types converted to their compatible integer
691 types. The resulting type is unqualified and has no attributes.
692
693 This is the type for the result of most arithmetic operations
694 if the operands have the given two types. */
695
ccf7f880
JJ
696static tree
697c_common_type (tree t1, tree t2)
10bc1b1b
JM
698{
699 enum tree_code code1;
700 enum tree_code code2;
701
702 /* If one type is nonsense, use the other. */
703 if (t1 == error_mark_node)
704 return t2;
705 if (t2 == error_mark_node)
706 return t1;
707
708 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
709 t1 = TYPE_MAIN_VARIANT (t1);
710
711 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
712 t2 = TYPE_MAIN_VARIANT (t2);
713
714 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
715 t1 = build_type_attribute_variant (t1, NULL_TREE);
716
717 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
718 t2 = build_type_attribute_variant (t2, NULL_TREE);
719
720 /* Save time if the two types are the same. */
721
722 if (t1 == t2) return t1;
723
724 code1 = TREE_CODE (t1);
725 code2 = TREE_CODE (t2);
726
366de0ce 727 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
728 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
729 || code1 == INTEGER_TYPE);
366de0ce 730 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
731 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
732 || code2 == INTEGER_TYPE);
10bc1b1b 733
5fc89bfd
JJ
734 /* When one operand is a decimal float type, the other operand cannot be
735 a generic float type or a complex type. We also disallow vector types
736 here. */
737 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
738 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
739 {
740 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
741 {
742 error ("can%'t mix operands of decimal float and vector types");
743 return error_mark_node;
744 }
745 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
746 {
747 error ("can%'t mix operands of decimal float and complex types");
748 return error_mark_node;
749 }
750 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
751 {
752 error ("can%'t mix operands of decimal float and other float types");
753 return error_mark_node;
754 }
755 }
756
10bc1b1b
JM
757 /* If one type is a vector type, return that type. (How the usual
758 arithmetic conversions apply to the vector types extension is not
759 precisely specified.) */
760 if (code1 == VECTOR_TYPE)
761 return t1;
762
763 if (code2 == VECTOR_TYPE)
764 return t2;
765
766 /* If one type is complex, form the common type of the non-complex
767 components, then make that complex. Use T1 or T2 if it is the
768 required type. */
769 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
770 {
771 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
772 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 773 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
774
775 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
776 return t1;
777 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
778 return t2;
779 else
780 return build_complex_type (subtype);
781 }
782
783 /* If only one is real, use it as the result. */
784
785 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
786 return t1;
787
788 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
789 return t2;
790
9a8ce21f
JG
791 /* If both are real and either are decimal floating point types, use
792 the decimal floating point type with the greater precision. */
793
794 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
795 {
796 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
797 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
798 return dfloat128_type_node;
799 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
800 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
801 return dfloat64_type_node;
802 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
803 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
804 return dfloat32_type_node;
805 }
806
ab22c1fa
CF
807 /* Deal with fixed-point types. */
808 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
809 {
810 unsigned int unsignedp = 0, satp = 0;
ef4bddc2 811 machine_mode m1, m2;
ab22c1fa
CF
812 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
813
814 m1 = TYPE_MODE (t1);
815 m2 = TYPE_MODE (t2);
816
817 /* If one input type is saturating, the result type is saturating. */
818 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
819 satp = 1;
820
821 /* If both fixed-point types are unsigned, the result type is unsigned.
822 When mixing fixed-point and integer types, follow the sign of the
823 fixed-point type.
824 Otherwise, the result type is signed. */
825 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
826 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
827 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
828 && TYPE_UNSIGNED (t1))
829 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
830 && TYPE_UNSIGNED (t2)))
831 unsignedp = 1;
832
833 /* The result type is signed. */
834 if (unsignedp == 0)
835 {
836 /* If the input type is unsigned, we need to convert to the
837 signed type. */
838 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
839 {
d75d71e0 840 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
841 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
842 mclass = MODE_FRACT;
843 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
844 mclass = MODE_ACCUM;
845 else
846 gcc_unreachable ();
847 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
848 }
849 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
850 {
d75d71e0 851 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
852 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
853 mclass = MODE_FRACT;
854 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
855 mclass = MODE_ACCUM;
856 else
857 gcc_unreachable ();
858 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
859 }
860 }
861
862 if (code1 == FIXED_POINT_TYPE)
863 {
864 fbit1 = GET_MODE_FBIT (m1);
865 ibit1 = GET_MODE_IBIT (m1);
866 }
867 else
868 {
869 fbit1 = 0;
870 /* Signed integers need to subtract one sign bit. */
871 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
872 }
873
874 if (code2 == FIXED_POINT_TYPE)
875 {
876 fbit2 = GET_MODE_FBIT (m2);
877 ibit2 = GET_MODE_IBIT (m2);
878 }
879 else
880 {
881 fbit2 = 0;
882 /* Signed integers need to subtract one sign bit. */
883 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
884 }
885
886 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
887 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
888 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
889 satp);
890 }
891
10bc1b1b
JM
892 /* Both real or both integers; use the one with greater precision. */
893
894 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
895 return t1;
896 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
897 return t2;
898
899 /* Same precision. Prefer long longs to longs to ints when the
900 same precision, following the C99 rules on integer type rank
901 (which are equivalent to the C90 rules for C90 types). */
902
903 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
904 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
905 return long_long_unsigned_type_node;
906
907 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
908 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
909 {
910 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
911 return long_long_unsigned_type_node;
912 else
c22cacf3 913 return long_long_integer_type_node;
10bc1b1b
JM
914 }
915
916 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
918 return long_unsigned_type_node;
919
920 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
921 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
922 {
923 /* But preserve unsignedness from the other type,
924 since long cannot hold all the values of an unsigned int. */
925 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
926 return long_unsigned_type_node;
927 else
928 return long_integer_type_node;
929 }
930
c65699ef
JM
931 /* For floating types of the same TYPE_PRECISION (which we here
932 assume means either the same set of values, or sets of values
933 neither a subset of the other, with behavior being undefined in
934 the latter case), follow the rules from TS 18661-3: prefer
935 interchange types _FloatN, then standard types long double,
936 double, float, then extended types _FloatNx. For extended types,
937 check them starting with _Float128x as that seems most consistent
938 in spirit with preferring long double to double; for interchange
939 types, also check in that order for consistency although it's not
940 possible for more than one of them to have the same
941 precision. */
942 tree mv1 = TYPE_MAIN_VARIANT (t1);
943 tree mv2 = TYPE_MAIN_VARIANT (t2);
944
945 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
946 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
947 return FLOATN_TYPE_NODE (i);
948
10bc1b1b 949 /* Likewise, prefer long double to double even if same size. */
c65699ef 950 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
10bc1b1b
JM
951 return long_double_type_node;
952
2531a1d9
JR
953 /* Likewise, prefer double to float even if same size.
954 We got a couple of embedded targets with 32 bit doubles, and the
955 pdp11 might have 64 bit floats. */
c65699ef 956 if (mv1 == double_type_node || mv2 == double_type_node)
2531a1d9
JR
957 return double_type_node;
958
c65699ef
JM
959 if (mv1 == float_type_node || mv2 == float_type_node)
960 return float_type_node;
961
962 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
963 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
964 return FLOATNX_TYPE_NODE (i);
965
10bc1b1b
JM
966 /* Otherwise prefer the unsigned one. */
967
968 if (TYPE_UNSIGNED (t1))
969 return t1;
970 else
971 return t2;
972}
400fbf9f 973\f
5922c215
JM
974/* Wrapper around c_common_type that is used by c-common.c and other
975 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
976 are allowed here and are converted to their compatible integer types.
977 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
978 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
979tree
980common_type (tree t1, tree t2)
981{
982 if (TREE_CODE (t1) == ENUMERAL_TYPE)
983 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
984 if (TREE_CODE (t2) == ENUMERAL_TYPE)
985 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
986
987 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
988 if (TREE_CODE (t1) == BOOLEAN_TYPE
989 && TREE_CODE (t2) == BOOLEAN_TYPE)
990 return boolean_type_node;
991
992 /* If either type is BOOLEAN_TYPE, then return the other. */
993 if (TREE_CODE (t1) == BOOLEAN_TYPE)
994 return t2;
995 if (TREE_CODE (t2) == BOOLEAN_TYPE)
996 return t1;
997
ccf7f880
JJ
998 return c_common_type (t1, t2);
999}
f13c9b2c 1000
400fbf9f
JW
1001/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1002 or various other operations. Return 2 if they are compatible
1003 but a warning may be needed if you use them together. */
1004
1005int
132da1a5 1006comptypes (tree type1, tree type2)
f13c9b2c
AP
1007{
1008 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1009 int val;
1010
dc5027f4 1011 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
1012 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1013
1014 return val;
1015}
1016
1017/* Like comptypes, but if it returns non-zero because enum and int are
1018 compatible, it sets *ENUM_AND_INT_P to true. */
1019
1020static int
1021comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1022{
1023 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1024 int val;
1025
dc5027f4
JM
1026 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1027 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1028
1029 return val;
1030}
1031
1032/* Like comptypes, but if it returns nonzero for different types, it
1033 sets *DIFFERENT_TYPES_P to true. */
1034
1035int
1036comptypes_check_different_types (tree type1, tree type2,
1037 bool *different_types_p)
1038{
1039 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1040 int val;
1041
1042 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1043 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1044
f13c9b2c 1045 return val;
c22cacf3
MS
1046}
1047\f
f13c9b2c
AP
1048/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1049 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1050 but a warning may be needed if you use them together. If
1051 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1052 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1053 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1054 NULL, and the types are compatible but different enough not to be
48b0b196 1055 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1056 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1057 false, but may or may not be set if the types are incompatible.
1058 This differs from comptypes, in that we don't free the seen
1059 types. */
f13c9b2c
AP
1060
1061static int
dc5027f4
JM
1062comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1063 bool *different_types_p)
400fbf9f 1064{
58f9752a
KG
1065 const_tree t1 = type1;
1066 const_tree t2 = type2;
4b027d16 1067 int attrval, val;
400fbf9f
JW
1068
1069 /* Suppress errors caused by previously reported errors. */
1070
8d47dfc5
RH
1071 if (t1 == t2 || !t1 || !t2
1072 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1073 return 1;
1074
bca63328
JM
1075 /* Enumerated types are compatible with integer types, but this is
1076 not transitive: two enumerated types in the same translation unit
1077 are compatible with each other only if they are the same type. */
400fbf9f 1078
bca63328 1079 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1080 {
1081 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1082 if (TREE_CODE (t2) != VOID_TYPE)
1083 {
1084 if (enum_and_int_p != NULL)
1085 *enum_and_int_p = true;
1086 if (different_types_p != NULL)
1087 *different_types_p = true;
1088 }
744aa42f 1089 }
bca63328 1090 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1091 {
1092 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1093 if (TREE_CODE (t1) != VOID_TYPE)
1094 {
1095 if (enum_and_int_p != NULL)
1096 *enum_and_int_p = true;
1097 if (different_types_p != NULL)
1098 *different_types_p = true;
1099 }
744aa42f 1100 }
400fbf9f
JW
1101
1102 if (t1 == t2)
1103 return 1;
1104
1105 /* Different classes of types can't be compatible. */
1106
3aeb3655
EC
1107 if (TREE_CODE (t1) != TREE_CODE (t2))
1108 return 0;
400fbf9f 1109
118a3a8b 1110 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1111
3932261a 1112 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1113 return 0;
1114
08632da2
RS
1115 /* Allow for two different type nodes which have essentially the same
1116 definition. Note that we already checked for equality of the type
38e01259 1117 qualifiers (just above). */
400fbf9f 1118
46df2823
JM
1119 if (TREE_CODE (t1) != ARRAY_TYPE
1120 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1121 return 1;
1122
4b027d16 1123 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1124 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1125 return 0;
1126
1127 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1128 val = 0;
1129
400fbf9f
JW
1130 switch (TREE_CODE (t1))
1131 {
5545a907
MP
1132 case INTEGER_TYPE:
1133 case FIXED_POINT_TYPE:
1134 case REAL_TYPE:
1135 /* With these nodes, we can't determine type equivalence by
1136 looking at what is stored in the nodes themselves, because
1137 two nodes might have different TYPE_MAIN_VARIANTs but still
1138 represent the same type. For example, wchar_t and int could
1139 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1140 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1141 and are distinct types. On the other hand, int and the
1142 following typedef
1143
1144 typedef int INT __attribute((may_alias));
1145
1146 have identical properties, different TYPE_MAIN_VARIANTs, but
1147 represent the same type. The canonical type system keeps
1148 track of equivalence in this case, so we fall back on it. */
1149 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1150
400fbf9f 1151 case POINTER_TYPE:
5545a907
MP
1152 /* Do not remove mode information. */
1153 if (TYPE_MODE (t1) != TYPE_MODE (t2))
106f5de5 1154 break;
4b027d16 1155 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1156 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1157 enum_and_int_p, different_types_p));
4b027d16 1158 break;
400fbf9f
JW
1159
1160 case FUNCTION_TYPE:
dc5027f4
JM
1161 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1162 different_types_p);
4b027d16 1163 break;
400fbf9f
JW
1164
1165 case ARRAY_TYPE:
1166 {
400fbf9f
JW
1167 tree d1 = TYPE_DOMAIN (t1);
1168 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1169 bool d1_variable, d2_variable;
1170 bool d1_zero, d2_zero;
4b027d16 1171 val = 1;
400fbf9f
JW
1172
1173 /* Target types must match incl. qualifiers. */
1174 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1175 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1176 enum_and_int_p,
1177 different_types_p)))
400fbf9f
JW
1178 return 0;
1179
dc5027f4
JM
1180 if (different_types_p != NULL
1181 && (d1 == 0) != (d2 == 0))
1182 *different_types_p = true;
400fbf9f 1183 /* Sizes must match unless one is missing or variable. */
3f85558f 1184 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1185 break;
400fbf9f 1186
3f75a254
JM
1187 d1_zero = !TYPE_MAX_VALUE (d1);
1188 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1189
3f75a254 1190 d1_variable = (!d1_zero
3f85558f
RH
1191 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1192 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1193 d2_variable = (!d2_zero
3f85558f
RH
1194 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1195 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1196 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1197 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1198
dc5027f4
JM
1199 if (different_types_p != NULL
1200 && d1_variable != d2_variable)
1201 *different_types_p = true;
3f85558f
RH
1202 if (d1_variable || d2_variable)
1203 break;
1204 if (d1_zero && d2_zero)
1205 break;
1206 if (d1_zero || d2_zero
3f75a254
JM
1207 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1208 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1209 val = 0;
1210
c22cacf3 1211 break;
400fbf9f
JW
1212 }
1213
d1bd0ded 1214 case ENUMERAL_TYPE:
58393038 1215 case RECORD_TYPE:
d1bd0ded 1216 case UNION_TYPE:
766beae1 1217 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1218 {
fcb99e7b
JJ
1219 tree a1 = TYPE_ATTRIBUTES (t1);
1220 tree a2 = TYPE_ATTRIBUTES (t2);
1221
1222 if (! attribute_list_contained (a1, a2)
1223 && ! attribute_list_contained (a2, a1))
1224 break;
1225
f13c9b2c 1226 if (attrval != 2)
dc5027f4
JM
1227 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1228 different_types_p);
1229 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1230 different_types_p);
f13c9b2c 1231 }
4b027d16 1232 break;
e9a25f70 1233
62e1dfcf 1234 case VECTOR_TYPE:
744aa42f
ILT
1235 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1236 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1237 enum_and_int_p, different_types_p));
62e1dfcf
NC
1238 break;
1239
e9a25f70
JL
1240 default:
1241 break;
400fbf9f 1242 }
4b027d16 1243 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1244}
1245
36c5e70a
BE
1246/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1247 their qualifiers, except for named address spaces. If the pointers point to
1248 different named addresses, then we must determine if one address space is a
1249 subset of the other. */
400fbf9f
JW
1250
1251static int
744aa42f 1252comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1253{
392202b0 1254 int val;
768952be 1255 int val_ped;
36c5e70a
BE
1256 tree mvl = TREE_TYPE (ttl);
1257 tree mvr = TREE_TYPE (ttr);
1258 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1259 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1260 addr_space_t as_common;
744aa42f 1261 bool enum_and_int_p;
8b40563c 1262
36c5e70a
BE
1263 /* Fail if pointers point to incompatible address spaces. */
1264 if (!addr_space_superset (asl, asr, &as_common))
1265 return 0;
1266
768952be
MU
1267 /* For pedantic record result of comptypes on arrays before losing
1268 qualifiers on the element type below. */
1269 val_ped = 1;
1270
1271 if (TREE_CODE (mvl) == ARRAY_TYPE
1272 && TREE_CODE (mvr) == ARRAY_TYPE)
1273 val_ped = comptypes (mvl, mvr);
1274
1275 /* Qualifiers on element types of array types that are
1276 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1277
1278 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1279 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1280 : TYPE_MAIN_VARIANT (mvl));
1281
1282 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1283 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1284 : TYPE_MAIN_VARIANT (mvr));
1285
744aa42f
ILT
1286 enum_and_int_p = false;
1287 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1288
768952be
MU
1289 if (val == 1 && val_ped != 1)
1290 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1291 "are incompatible in ISO C");
1292
fcf73884 1293 if (val == 2)
c1771a20 1294 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1295
1296 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1297 warning_at (location, OPT_Wc___compat,
1298 "pointer target types incompatible in C++");
1299
400fbf9f
JW
1300 return val;
1301}
1302\f
1303/* Subroutines of `comptypes'. */
1304
f75fbaf7
ZW
1305/* Determine whether two trees derive from the same translation unit.
1306 If the CONTEXT chain ends in a null, that tree's context is still
1307 being parsed, so if two trees have context chains ending in null,
766beae1 1308 they're in the same translation unit. */
f75fbaf7 1309int
58f9752a 1310same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1311{
1312 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1313 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1314 {
6615c446
JO
1315 case tcc_declaration:
1316 t1 = DECL_CONTEXT (t1); break;
1317 case tcc_type:
1318 t1 = TYPE_CONTEXT (t1); break;
1319 case tcc_exceptional:
1320 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1321 default: gcc_unreachable ();
766beae1
ZW
1322 }
1323
1324 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1325 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1326 {
6615c446
JO
1327 case tcc_declaration:
1328 t2 = DECL_CONTEXT (t2); break;
1329 case tcc_type:
1330 t2 = TYPE_CONTEXT (t2); break;
1331 case tcc_exceptional:
1332 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1333 default: gcc_unreachable ();
766beae1
ZW
1334 }
1335
1336 return t1 == t2;
1337}
1338
f13c9b2c 1339/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1340
f13c9b2c 1341static struct tagged_tu_seen_cache *
58f9752a 1342alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1343{
cceb1885 1344 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1345 tu->next = tagged_tu_seen_base;
1346 tu->t1 = t1;
1347 tu->t2 = t2;
c22cacf3 1348
f13c9b2c 1349 tagged_tu_seen_base = tu;
c22cacf3 1350
f13c9b2c
AP
1351 /* The C standard says that two structures in different translation
1352 units are compatible with each other only if the types of their
1353 fields are compatible (among other things). We assume that they
1354 are compatible until proven otherwise when building the cache.
1355 An example where this can occur is:
1356 struct a
1357 {
1358 struct a *next;
1359 };
1360 If we are comparing this against a similar struct in another TU,
c83eecad 1361 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1362 loop. */
1363 tu->val = 1;
1364 return tu;
1365}
d1bd0ded 1366
f13c9b2c 1367/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1368
f13c9b2c
AP
1369static void
1370free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1371{
1372 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1373 while (tu != tu_til)
1374 {
741ac903
KG
1375 const struct tagged_tu_seen_cache *const tu1
1376 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1377 tu = tu1->next;
b1d5455a 1378 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1379 }
1380 tagged_tu_seen_base = tu_til;
1381}
d1bd0ded
GK
1382
1383/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1384 compatible. If the two types are not the same (which has been
1385 checked earlier), this can only happen when multiple translation
1386 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1387 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1388 comptypes_internal. */
d1bd0ded
GK
1389
1390static int
744aa42f 1391tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1392 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1393{
1394 tree s1, s2;
1395 bool needs_warning = false;
3aeb3655 1396
d1bd0ded
GK
1397 /* We have to verify that the tags of the types are the same. This
1398 is harder than it looks because this may be a typedef, so we have
1399 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1400 typedef...
1401 In the case of compiler-created builtin structs the TYPE_DECL
1402 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1403 while (TYPE_NAME (t1)
1404 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1405 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1406 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1407
dea984dc
ILT
1408 while (TYPE_NAME (t2)
1409 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1410 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1411 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1412
1413 /* C90 didn't have the requirement that the two tags be the same. */
1414 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1415 return 0;
3aeb3655 1416
d1bd0ded
GK
1417 /* C90 didn't say what happened if one or both of the types were
1418 incomplete; we choose to follow C99 rules here, which is that they
1419 are compatible. */
1420 if (TYPE_SIZE (t1) == NULL
1421 || TYPE_SIZE (t2) == NULL)
1422 return 1;
3aeb3655 1423
d1bd0ded 1424 {
f13c9b2c 1425 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1426 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1427 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1428 return tts_i->val;
d1bd0ded 1429 }
3aeb3655 1430
d1bd0ded
GK
1431 switch (TREE_CODE (t1))
1432 {
1433 case ENUMERAL_TYPE:
1434 {
f13c9b2c 1435 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1436 /* Speed up the case where the type values are in the same order. */
1437 tree tv1 = TYPE_VALUES (t1);
1438 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1439
c22cacf3 1440 if (tv1 == tv2)
f13c9b2c
AP
1441 {
1442 return 1;
1443 }
3aeb3655 1444
c22cacf3
MS
1445 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1446 {
1447 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1448 break;
1449 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1450 {
c22cacf3 1451 tu->val = 0;
f13c9b2c
AP
1452 return 0;
1453 }
c22cacf3 1454 }
3aeb3655 1455
c22cacf3 1456 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1457 {
1458 return 1;
1459 }
c22cacf3 1460 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1461 {
1462 tu->val = 0;
1463 return 0;
1464 }
3aeb3655 1465
d1bd0ded 1466 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1467 {
1468 tu->val = 0;
1469 return 0;
1470 }
3aeb3655 1471
d1bd0ded
GK
1472 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1473 {
1474 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1475 if (s2 == NULL
1476 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1477 {
1478 tu->val = 0;
1479 return 0;
1480 }
d1bd0ded
GK
1481 }
1482 return 1;
1483 }
1484
1485 case UNION_TYPE:
1486 {
f13c9b2c 1487 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1488 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1489 {
1490 tu->val = 0;
1491 return 0;
1492 }
c22cacf3 1493
f13c9b2c
AP
1494 /* Speed up the common case where the fields are in the same order. */
1495 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1496 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1497 {
1498 int result;
c22cacf3 1499
3ae4d3cc 1500 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1501 break;
744aa42f 1502 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1503 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1504
1505 if (result != 1 && !DECL_NAME (s1))
1506 break;
f13c9b2c
AP
1507 if (result == 0)
1508 {
1509 tu->val = 0;
1510 return 0;
1511 }
1512 if (result == 2)
1513 needs_warning = true;
1514
1515 if (TREE_CODE (s1) == FIELD_DECL
1516 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1517 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1518 {
1519 tu->val = 0;
1520 return 0;
1521 }
1522 }
1523 if (!s1 && !s2)
1524 {
1525 tu->val = needs_warning ? 2 : 1;
1526 return tu->val;
1527 }
d1bd0ded 1528
910ad8de 1529 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1530 {
1531 bool ok = false;
3aeb3655 1532
910ad8de 1533 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1534 if (DECL_NAME (s1) == DECL_NAME (s2))
1535 {
1536 int result;
1537
744aa42f 1538 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1539 enum_and_int_p,
1540 different_types_p);
3ae4d3cc
AO
1541
1542 if (result != 1 && !DECL_NAME (s1))
1543 continue;
1544 if (result == 0)
1545 {
1546 tu->val = 0;
1547 return 0;
1548 }
1549 if (result == 2)
1550 needs_warning = true;
1551
1552 if (TREE_CODE (s1) == FIELD_DECL
1553 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1554 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1555 break;
3ae4d3cc
AO
1556
1557 ok = true;
1558 break;
1559 }
3f75a254 1560 if (!ok)
f13c9b2c
AP
1561 {
1562 tu->val = 0;
1563 return 0;
1564 }
d1bd0ded 1565 }
f13c9b2c
AP
1566 tu->val = needs_warning ? 2 : 10;
1567 return tu->val;
d1bd0ded
GK
1568 }
1569
1570 case RECORD_TYPE:
1571 {
c22cacf3 1572 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1573
1574 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1575 s1 && s2;
910ad8de 1576 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1577 {
1578 int result;
1579 if (TREE_CODE (s1) != TREE_CODE (s2)
1580 || DECL_NAME (s1) != DECL_NAME (s2))
1581 break;
744aa42f 1582 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1583 enum_and_int_p, different_types_p);
d1bd0ded
GK
1584 if (result == 0)
1585 break;
1586 if (result == 2)
1587 needs_warning = true;
3aeb3655 1588
d1bd0ded
GK
1589 if (TREE_CODE (s1) == FIELD_DECL
1590 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1591 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1592 break;
1593 }
d1bd0ded 1594 if (s1 && s2)
f13c9b2c
AP
1595 tu->val = 0;
1596 else
1597 tu->val = needs_warning ? 2 : 1;
1598 return tu->val;
d1bd0ded
GK
1599 }
1600
1601 default:
366de0ce 1602 gcc_unreachable ();
d1bd0ded
GK
1603 }
1604}
1605
400fbf9f
JW
1606/* Return 1 if two function types F1 and F2 are compatible.
1607 If either type specifies no argument types,
1608 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1609 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1610 the other must specify that number of self-promoting arg types.
744aa42f 1611 Otherwise, the argument types must match.
dc5027f4 1612 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1613
1614static int
744aa42f 1615function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1616 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1617{
1618 tree args1, args2;
1619 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1620 int val = 1;
1621 int val1;
a6fdc086
GK
1622 tree ret1, ret2;
1623
1624 ret1 = TREE_TYPE (f1);
1625 ret2 = TREE_TYPE (f2);
1626
e508a019
JM
1627 /* 'volatile' qualifiers on a function's return type used to mean
1628 the function is noreturn. */
1629 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1630 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1631 if (TYPE_VOLATILE (ret1))
1632 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1633 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1634 if (TYPE_VOLATILE (ret2))
1635 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1636 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1637 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1638 if (val == 0)
400fbf9f
JW
1639 return 0;
1640
1641 args1 = TYPE_ARG_TYPES (f1);
1642 args2 = TYPE_ARG_TYPES (f2);
1643
dc5027f4
JM
1644 if (different_types_p != NULL
1645 && (args1 == 0) != (args2 == 0))
1646 *different_types_p = true;
1647
400fbf9f
JW
1648 /* An unspecified parmlist matches any specified parmlist
1649 whose argument types don't need default promotions. */
1650
1651 if (args1 == 0)
1652 {
1653 if (!self_promoting_args_p (args2))
1654 return 0;
1655 /* If one of these types comes from a non-prototype fn definition,
1656 compare that with the other type's arglist.
3176a0c2 1657 If they don't match, ask for a warning (but no error). */
400fbf9f 1658 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1659 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1660 enum_and_int_p, different_types_p))
400fbf9f
JW
1661 val = 2;
1662 return val;
1663 }
1664 if (args2 == 0)
1665 {
1666 if (!self_promoting_args_p (args1))
1667 return 0;
1668 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1669 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1670 enum_and_int_p, different_types_p))
400fbf9f
JW
1671 val = 2;
1672 return val;
1673 }
1674
1675 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1676 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1677 different_types_p);
400fbf9f
JW
1678 return val1 != 1 ? val1 : val;
1679}
1680
744aa42f
ILT
1681/* Check two lists of types for compatibility, returning 0 for
1682 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1683 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1684 comptypes_internal. */
400fbf9f
JW
1685
1686static int
744aa42f 1687type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1688 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1689{
1690 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1691 int val = 1;
9d5f3e49 1692 int newval = 0;
400fbf9f
JW
1693
1694 while (1)
1695 {
46df2823 1696 tree a1, mv1, a2, mv2;
400fbf9f
JW
1697 if (args1 == 0 && args2 == 0)
1698 return val;
1699 /* If one list is shorter than the other,
1700 they fail to match. */
1701 if (args1 == 0 || args2 == 0)
1702 return 0;
46df2823
JM
1703 mv1 = a1 = TREE_VALUE (args1);
1704 mv2 = a2 = TREE_VALUE (args2);
1705 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1706 mv1 = (TYPE_ATOMIC (mv1)
1707 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1708 TYPE_QUAL_ATOMIC)
1709 : TYPE_MAIN_VARIANT (mv1));
46df2823 1710 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1711 mv2 = (TYPE_ATOMIC (mv2)
1712 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1713 TYPE_QUAL_ATOMIC)
1714 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1715 /* A null pointer instead of a type
1716 means there is supposed to be an argument
1717 but nothing is specified about what type it has.
1718 So match anything that self-promotes. */
dc5027f4
JM
1719 if (different_types_p != NULL
1720 && (a1 == 0) != (a2 == 0))
1721 *different_types_p = true;
46df2823 1722 if (a1 == 0)
400fbf9f 1723 {
46df2823 1724 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1725 return 0;
1726 }
46df2823 1727 else if (a2 == 0)
400fbf9f 1728 {
46df2823 1729 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1730 return 0;
1731 }
8f5b6d29 1732 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1733 else if (TREE_CODE (a1) == ERROR_MARK
1734 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1735 ;
dc5027f4
JM
1736 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1737 different_types_p)))
400fbf9f 1738 {
dc5027f4
JM
1739 if (different_types_p != NULL)
1740 *different_types_p = true;
400fbf9f
JW
1741 /* Allow wait (union {union wait *u; int *i} *)
1742 and wait (union wait *) to be compatible. */
46df2823
JM
1743 if (TREE_CODE (a1) == UNION_TYPE
1744 && (TYPE_NAME (a1) == 0
ebf0bf7f 1745 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1746 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1747 && tree_int_cst_equal (TYPE_SIZE (a1),
1748 TYPE_SIZE (a2)))
400fbf9f
JW
1749 {
1750 tree memb;
46df2823 1751 for (memb = TYPE_FIELDS (a1);
910ad8de 1752 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1753 {
1754 tree mv3 = TREE_TYPE (memb);
1755 if (mv3 && mv3 != error_mark_node
1756 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1757 mv3 = (TYPE_ATOMIC (mv3)
1758 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1759 TYPE_QUAL_ATOMIC)
1760 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1761 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1762 different_types_p))
58cb41e6
JJ
1763 break;
1764 }
400fbf9f
JW
1765 if (memb == 0)
1766 return 0;
1767 }
46df2823
JM
1768 else if (TREE_CODE (a2) == UNION_TYPE
1769 && (TYPE_NAME (a2) == 0
ebf0bf7f 1770 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1771 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1772 && tree_int_cst_equal (TYPE_SIZE (a2),
1773 TYPE_SIZE (a1)))
400fbf9f
JW
1774 {
1775 tree memb;
46df2823 1776 for (memb = TYPE_FIELDS (a2);
910ad8de 1777 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1778 {
1779 tree mv3 = TREE_TYPE (memb);
1780 if (mv3 && mv3 != error_mark_node
1781 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1782 mv3 = (TYPE_ATOMIC (mv3)
1783 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1784 TYPE_QUAL_ATOMIC)
1785 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1786 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1787 different_types_p))
58cb41e6
JJ
1788 break;
1789 }
400fbf9f
JW
1790 if (memb == 0)
1791 return 0;
1792 }
1793 else
1794 return 0;
1795 }
1796
1797 /* comptypes said ok, but record if it said to warn. */
1798 if (newval > val)
1799 val = newval;
1800
1801 args1 = TREE_CHAIN (args1);
1802 args2 = TREE_CHAIN (args2);
1803 }
1804}
400fbf9f 1805\f
a0e24419
MP
1806/* Compute the size to increment a pointer by. When a function type or void
1807 type or incomplete type is passed, size_one_node is returned.
1808 This function does not emit any diagnostics; the caller is responsible
1809 for that. */
400fbf9f 1810
4e2fb7de 1811static tree
58f9752a 1812c_size_in_bytes (const_tree type)
400fbf9f
JW
1813{
1814 enum tree_code code = TREE_CODE (type);
1815
a0e24419
MP
1816 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1817 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1818 return size_one_node;
1819
400fbf9f 1820 /* Convert in case a char is more than one unit. */
db3927fb
AH
1821 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1822 size_int (TYPE_PRECISION (char_type_node)
1823 / BITS_PER_UNIT));
400fbf9f 1824}
400fbf9f 1825\f
400fbf9f
JW
1826/* Return either DECL or its known constant value (if it has one). */
1827
56cb9733 1828tree
2f6e4e97 1829decl_constant_value (tree decl)
400fbf9f 1830{
a7c1916a 1831 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1832 in a place where a variable is invalid. Note that DECL_INITIAL
1833 isn't valid for a PARM_DECL. */
a7c1916a 1834 current_function_decl != 0
4f976745 1835 && TREE_CODE (decl) != PARM_DECL
3f75a254 1836 && !TREE_THIS_VOLATILE (decl)
83bab8db 1837 && TREE_READONLY (decl)
400fbf9f
JW
1838 && DECL_INITIAL (decl) != 0
1839 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1840 /* This is invalid if initial value is not constant.
1841 If it has either a function call, a memory reference,
1842 or a variable, then re-evaluating it could give different results. */
1843 && TREE_CONSTANT (DECL_INITIAL (decl))
1844 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1845 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1846 return DECL_INITIAL (decl);
1847 return decl;
1848}
1849
f2a71bbc
JM
1850/* Convert the array expression EXP to a pointer. */
1851static tree
c2255bc4 1852array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1853{
f2a71bbc 1854 tree orig_exp = exp;
207bf485 1855 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1856 tree adr;
1857 tree restype = TREE_TYPE (type);
1858 tree ptrtype;
207bf485 1859
f2a71bbc 1860 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1861
f2a71bbc 1862 STRIP_TYPE_NOPS (exp);
207bf485 1863
487a92fe
JM
1864 if (TREE_NO_WARNING (orig_exp))
1865 TREE_NO_WARNING (exp) = 1;
207bf485 1866
f2a71bbc
JM
1867 ptrtype = build_pointer_type (restype);
1868
22d03525 1869 if (INDIRECT_REF_P (exp))
f2a71bbc
JM
1870 return convert (ptrtype, TREE_OPERAND (exp, 0));
1871
1f37c583
JM
1872 /* In C++ array compound literals are temporary objects unless they are
1873 const or appear in namespace scope, so they are destroyed too soon
1874 to use them for much of anything (c++/53220). */
1875 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1876 {
1877 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1878 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1879 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1880 "converting an array compound literal to a pointer "
1881 "is ill-formed in C++");
1882 }
1883
e51fbec3 1884 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
f2a71bbc
JM
1885 return convert (ptrtype, adr);
1886}
207bf485 1887
f2a71bbc
JM
1888/* Convert the function expression EXP to a pointer. */
1889static tree
c2255bc4 1890function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1891{
1892 tree orig_exp = exp;
207bf485 1893
f2a71bbc 1894 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1895
f2a71bbc 1896 STRIP_TYPE_NOPS (exp);
207bf485 1897
f2a71bbc
JM
1898 if (TREE_NO_WARNING (orig_exp))
1899 TREE_NO_WARNING (exp) = 1;
207bf485 1900
e51fbec3 1901 return build_unary_op (loc, ADDR_EXPR, exp, false);
f2a71bbc 1902}
207bf485 1903
ebfbbdc5
JJ
1904/* Mark EXP as read, not just set, for set but not used -Wunused
1905 warning purposes. */
1906
1907void
1908mark_exp_read (tree exp)
1909{
1910 switch (TREE_CODE (exp))
1911 {
1912 case VAR_DECL:
1913 case PARM_DECL:
1914 DECL_READ_P (exp) = 1;
1915 break;
1916 case ARRAY_REF:
1917 case COMPONENT_REF:
1918 case MODIFY_EXPR:
1919 case REALPART_EXPR:
1920 case IMAGPART_EXPR:
1921 CASE_CONVERT:
1922 case ADDR_EXPR:
557e8c49 1923 case VIEW_CONVERT_EXPR:
ebfbbdc5
JJ
1924 mark_exp_read (TREE_OPERAND (exp, 0));
1925 break;
1926 case COMPOUND_EXPR:
82c3c067 1927 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1928 mark_exp_read (TREE_OPERAND (exp, 1));
1929 break;
1930 default:
1931 break;
1932 }
1933}
1934
f2a71bbc
JM
1935/* Perform the default conversion of arrays and functions to pointers.
1936 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1937 return EXP.
1938
1939 LOC is the location of the expression. */
f2a71bbc
JM
1940
1941struct c_expr
c2255bc4 1942default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1943{
1944 tree orig_exp = exp.value;
1945 tree type = TREE_TYPE (exp.value);
1946 enum tree_code code = TREE_CODE (type);
1947
1948 switch (code)
1949 {
1950 case ARRAY_TYPE:
1951 {
1952 bool not_lvalue = false;
1953 bool lvalue_array_p;
1954
1955 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1956 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1957 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1958 {
1959 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1960 not_lvalue = true;
1961 exp.value = TREE_OPERAND (exp.value, 0);
1962 }
1963
1964 if (TREE_NO_WARNING (orig_exp))
1965 TREE_NO_WARNING (exp.value) = 1;
1966
1967 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1968 if (!flag_isoc99 && !lvalue_array_p)
1969 {
1970 /* Before C99, non-lvalue arrays do not decay to pointers.
1971 Normally, using such an array would be invalid; but it can
1972 be used correctly inside sizeof or as a statement expression.
1973 Thus, do not give an error here; an error will result later. */
1974 return exp;
1975 }
1976
c2255bc4 1977 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1978 }
1979 break;
1980 case FUNCTION_TYPE:
c2255bc4 1981 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1982 break;
1983 default:
f2a71bbc 1984 break;
207bf485 1985 }
f2a71bbc 1986
207bf485
JM
1987 return exp;
1988}
1989
ebfbbdc5
JJ
1990struct c_expr
1991default_function_array_read_conversion (location_t loc, struct c_expr exp)
1992{
1993 mark_exp_read (exp.value);
1994 return default_function_array_conversion (loc, exp);
1995}
522ddfa2 1996
267bac10
JM
1997/* Return whether EXPR should be treated as an atomic lvalue for the
1998 purposes of load and store handling. */
1999
2000static bool
2001really_atomic_lvalue (tree expr)
2002{
7a0ca710 2003 if (error_operand_p (expr))
267bac10
JM
2004 return false;
2005 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2006 return false;
2007 if (!lvalue_p (expr))
2008 return false;
2009
2010 /* Ignore _Atomic on register variables, since their addresses can't
2011 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2012 sequences wouldn't work. Ignore _Atomic on structures containing
2013 bit-fields, since accessing elements of atomic structures or
2014 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2015 it's undefined at translation time or execution time, and the
2016 normal atomic sequences again wouldn't work. */
2017 while (handled_component_p (expr))
2018 {
2019 if (TREE_CODE (expr) == COMPONENT_REF
2020 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2021 return false;
2022 expr = TREE_OPERAND (expr, 0);
2023 }
2024 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2025 return false;
2026 return true;
2027}
2028
2029/* Convert expression EXP (location LOC) from lvalue to rvalue,
2030 including converting functions and arrays to pointers if CONVERT_P.
2031 If READ_P, also mark the expression as having been read. */
2032
2033struct c_expr
2034convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2035 bool convert_p, bool read_p)
2036{
2037 if (read_p)
2038 mark_exp_read (exp.value);
2039 if (convert_p)
2040 exp = default_function_array_conversion (loc, exp);
2041 if (really_atomic_lvalue (exp.value))
2042 {
2043 vec<tree, va_gc> *params;
2044 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2045 tree expr_type = TREE_TYPE (exp.value);
e51fbec3 2046 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
267bac10
JM
2047 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2048
2049 gcc_assert (TYPE_ATOMIC (expr_type));
2050
2051 /* Expansion of a generic atomic load may require an addition
2052 element, so allocate enough to prevent a resize. */
2053 vec_alloc (params, 4);
2054
2055 /* Remove the qualifiers for the rest of the expressions and
2056 create the VAL temp variable to hold the RHS. */
2057 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
5c4abbb8 2058 tmp = create_tmp_var_raw (nonatomic_type);
e51fbec3 2059 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
267bac10 2060 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2061 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2062
2063 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2064 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2065 params->quick_push (expr_addr);
2066 params->quick_push (tmp_addr);
2067 params->quick_push (seq_cst);
8edbfaa6 2068 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2069
cc28fc7f
MP
2070 /* EXPR is always read. */
2071 mark_exp_read (exp.value);
2072
267bac10 2073 /* Return tmp which contains the value loaded. */
5c4abbb8
MP
2074 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2075 NULL_TREE, NULL_TREE);
267bac10
JM
2076 }
2077 return exp;
2078}
2079
522ddfa2
JM
2080/* EXP is an expression of integer type. Apply the integer promotions
2081 to it and return the promoted value. */
400fbf9f
JW
2082
2083tree
522ddfa2 2084perform_integral_promotions (tree exp)
400fbf9f 2085{
b3694847
SS
2086 tree type = TREE_TYPE (exp);
2087 enum tree_code code = TREE_CODE (type);
400fbf9f 2088
522ddfa2 2089 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2090
400fbf9f
JW
2091 /* Normally convert enums to int,
2092 but convert wide enums to something wider. */
2093 if (code == ENUMERAL_TYPE)
2094 {
b0c48229
NB
2095 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2096 TYPE_PRECISION (integer_type_node)),
2097 ((TYPE_PRECISION (type)
2098 >= TYPE_PRECISION (integer_type_node))
8df83eae 2099 && TYPE_UNSIGNED (type)));
05bccae2 2100
400fbf9f
JW
2101 return convert (type, exp);
2102 }
2103
522ddfa2
JM
2104 /* ??? This should no longer be needed now bit-fields have their
2105 proper types. */
9753f113 2106 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2107 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2108 /* If it's thinner than an int, promote it like a
d72040f5 2109 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2110 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2111 TYPE_PRECISION (integer_type_node)))
f458d1d5 2112 return convert (integer_type_node, exp);
9753f113 2113
d72040f5 2114 if (c_promoting_integer_type_p (type))
400fbf9f 2115 {
f458d1d5 2116 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2117 if (TYPE_UNSIGNED (type)
f458d1d5 2118 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2119 return convert (unsigned_type_node, exp);
05bccae2 2120
400fbf9f
JW
2121 return convert (integer_type_node, exp);
2122 }
05bccae2 2123
522ddfa2
JM
2124 return exp;
2125}
2126
2127
2128/* Perform default promotions for C data used in expressions.
46bdb9cf 2129 Enumeral types or short or char are converted to int.
522ddfa2
JM
2130 In addition, manifest constants symbols are replaced by their values. */
2131
2132tree
2133default_conversion (tree exp)
2134{
2135 tree orig_exp;
2136 tree type = TREE_TYPE (exp);
2137 enum tree_code code = TREE_CODE (type);
40449a90 2138 tree promoted_type;
522ddfa2 2139
ebfbbdc5
JJ
2140 mark_exp_read (exp);
2141
46bdb9cf
JM
2142 /* Functions and arrays have been converted during parsing. */
2143 gcc_assert (code != FUNCTION_TYPE);
2144 if (code == ARRAY_TYPE)
2145 return exp;
522ddfa2
JM
2146
2147 /* Constants can be used directly unless they're not loadable. */
2148 if (TREE_CODE (exp) == CONST_DECL)
2149 exp = DECL_INITIAL (exp);
2150
522ddfa2
JM
2151 /* Strip no-op conversions. */
2152 orig_exp = exp;
2153 STRIP_TYPE_NOPS (exp);
2154
2155 if (TREE_NO_WARNING (orig_exp))
2156 TREE_NO_WARNING (exp) = 1;
2157
400fbf9f
JW
2158 if (code == VOID_TYPE)
2159 {
5436fa2e
MP
2160 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2161 "void value not ignored as it ought to be");
400fbf9f
JW
2162 return error_mark_node;
2163 }
808d6eaa 2164
4f2e1536 2165 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
808d6eaa
JM
2166 if (exp == error_mark_node)
2167 return error_mark_node;
2168
40449a90
SL
2169 promoted_type = targetm.promoted_type (type);
2170 if (promoted_type)
2171 return convert (promoted_type, exp);
2172
808d6eaa
JM
2173 if (INTEGRAL_TYPE_P (type))
2174 return perform_integral_promotions (exp);
2175
400fbf9f
JW
2176 return exp;
2177}
2178\f
0fb96aa4 2179/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2180
2181 If the component name is not found, returns NULL_TREE. Otherwise,
2182 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2183 stepping down the chain to the component, which is in the last
2184 TREE_VALUE of the list. Normally the list is of length one, but if
2185 the component is embedded within (nested) anonymous structures or
2186 unions, the list steps down the chain to the component. */
2f6e4e97 2187
2f2d13da 2188static tree
0fb96aa4 2189lookup_field (tree type, tree component)
2f2d13da
DE
2190{
2191 tree field;
2192
2193 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2194 to the field elements. Use a binary search on this array to quickly
2195 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2196 will always be set for structures which have many elements. */
2197
22a0b85f 2198 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2199 {
2200 int bot, top, half;
d07605f5 2201 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2202
2203 field = TYPE_FIELDS (type);
2204 bot = 0;
d07605f5 2205 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2206 while (top - bot > 1)
2207 {
2f2d13da
DE
2208 half = (top - bot + 1) >> 1;
2209 field = field_array[bot+half];
2210
2211 if (DECL_NAME (field) == NULL_TREE)
2212 {
2213 /* Step through all anon unions in linear fashion. */
2214 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2215 {
2f2d13da 2216 field = field_array[bot++];
3e636daf 2217 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
19d76e60 2218 {
0fb96aa4 2219 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2220
2221 if (anon)
2222 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2223
2224 /* The Plan 9 compiler permits referring
2225 directly to an anonymous struct/union field
2226 using a typedef name. */
2227 if (flag_plan9_extensions
2228 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2229 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2230 == TYPE_DECL)
2231 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2232 == component))
2233 break;
2f6e4e97 2234 }
2f2d13da
DE
2235 }
2236
2237 /* Entire record is only anon unions. */
2238 if (bot > top)
2239 return NULL_TREE;
2240
2241 /* Restart the binary search, with new lower bound. */
2242 continue;
2243 }
2244
e8b87aac 2245 if (DECL_NAME (field) == component)
2f2d13da 2246 break;
e8b87aac 2247 if (DECL_NAME (field) < component)
2f2d13da
DE
2248 bot += half;
2249 else
2250 top = bot + half;
2251 }
2252
2253 if (DECL_NAME (field_array[bot]) == component)
2254 field = field_array[bot];
2255 else if (DECL_NAME (field) != component)
e9b2c823 2256 return NULL_TREE;
2f2d13da
DE
2257 }
2258 else
2259 {
910ad8de 2260 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2261 {
e9b2c823 2262 if (DECL_NAME (field) == NULL_TREE
3e636daf 2263 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2f2d13da 2264 {
0fb96aa4 2265 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2266
e9b2c823
NB
2267 if (anon)
2268 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2269
2270 /* The Plan 9 compiler permits referring directly to an
2271 anonymous struct/union field using a typedef
2272 name. */
2273 if (flag_plan9_extensions
2274 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2275 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2276 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2277 == component))
2278 break;
2f2d13da
DE
2279 }
2280
2281 if (DECL_NAME (field) == component)
2282 break;
2283 }
e9b2c823
NB
2284
2285 if (field == NULL_TREE)
2286 return NULL_TREE;
2f2d13da
DE
2287 }
2288
e9b2c823 2289 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2290}
2291
277fe616
DM
2292/* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2293
2294static void
2295lookup_field_fuzzy_find_candidates (tree type, tree component,
2296 vec<tree> *candidates)
2297{
2298 tree field;
2299 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2300 {
2301 if (DECL_NAME (field) == NULL_TREE
3e636daf
MP
2302 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2303 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2304 candidates);
277fe616
DM
2305
2306 if (DECL_NAME (field))
2307 candidates->safe_push (DECL_NAME (field));
2308 }
2309}
2310
2311/* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2312 rather than returning a TREE_LIST for an exact match. */
2313
2314static tree
2315lookup_field_fuzzy (tree type, tree component)
2316{
2317 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2318
2319 /* First, gather a list of candidates. */
2320 auto_vec <tree> candidates;
2321
2322 lookup_field_fuzzy_find_candidates (type, component,
2323 &candidates);
2324
8ece8dfb 2325 return find_closest_identifier (component, &candidates);
277fe616
DM
2326}
2327
a87a86e1
DM
2328/* Support function for build_component_ref's error-handling.
2329
2330 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2331 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2332
2333static bool
2334should_suggest_deref_p (tree datum_type)
2335{
2336 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2337 allows "." for ptrs; we could be handling a failed attempt
2338 to access a property. */
2339 if (c_dialect_objc ())
2340 return false;
2341
2342 /* Only suggest it for pointers... */
2343 if (TREE_CODE (datum_type) != POINTER_TYPE)
2344 return false;
2345
2346 /* ...to structs/unions. */
2347 tree underlying_type = TREE_TYPE (datum_type);
2348 enum tree_code code = TREE_CODE (underlying_type);
2349 if (code == RECORD_TYPE || code == UNION_TYPE)
2350 return true;
2351 else
2352 return false;
2353}
2354
c2255bc4
AH
2355/* Make an expression to refer to the COMPONENT field of structure or
2356 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
6ffd47b7
DM
2357 location of the COMPONENT_REF. COMPONENT_LOC is the location
2358 of COMPONENT. */
400fbf9f
JW
2359
2360tree
6ffd47b7
DM
2361build_component_ref (location_t loc, tree datum, tree component,
2362 location_t component_loc)
400fbf9f 2363{
b3694847
SS
2364 tree type = TREE_TYPE (datum);
2365 enum tree_code code = TREE_CODE (type);
2366 tree field = NULL;
2367 tree ref;
1e57bf47 2368 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2369
7a3ea201
RH
2370 if (!objc_is_public (datum, component))
2371 return error_mark_node;
2372
46a88c12 2373 /* Detect Objective-C property syntax object.property. */
668ea4b1 2374 if (c_dialect_objc ()
46a88c12 2375 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2376 return ref;
2377
400fbf9f
JW
2378 /* See if there is a field or component with name COMPONENT. */
2379
2380 if (code == RECORD_TYPE || code == UNION_TYPE)
2381 {
d0f062fb 2382 if (!COMPLETE_TYPE_P (type))
400fbf9f 2383 {
4f2e1536 2384 c_incomplete_type_error (loc, NULL_TREE, type);
400fbf9f
JW
2385 return error_mark_node;
2386 }
2387
0fb96aa4 2388 field = lookup_field (type, component);
400fbf9f
JW
2389
2390 if (!field)
2391 {
277fe616
DM
2392 tree guessed_id = lookup_field_fuzzy (type, component);
2393 if (guessed_id)
6ffd47b7
DM
2394 {
2395 /* Attempt to provide a fixit replacement hint, if
2396 we have a valid range for the component. */
2397 location_t reported_loc
2398 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
264757fb 2399 gcc_rich_location rich_loc (reported_loc);
6ffd47b7 2400 if (component_loc != UNKNOWN_LOCATION)
264757fb 2401 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
6ffd47b7
DM
2402 error_at_rich_loc
2403 (&rich_loc,
2404 "%qT has no member named %qE; did you mean %qE?",
2405 type, component, guessed_id);
2406 }
277fe616
DM
2407 else
2408 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2409 return error_mark_node;
2410 }
400fbf9f 2411
fda5652f
MP
2412 /* Accessing elements of atomic structures or unions is undefined
2413 behavior (C11 6.5.2.3#5). */
2414 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2415 {
2416 if (code == RECORD_TYPE)
2417 warning_at (loc, 0, "accessing a member %qE of an atomic "
2418 "structure %qE", component, datum);
2419 else
2420 warning_at (loc, 0, "accessing a member %qE of an atomic "
2421 "union %qE", component, datum);
2422 }
2423
e9b2c823
NB
2424 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2425 This might be better solved in future the way the C++ front
2426 end does it - by giving the anonymous entities each a
2427 separate name and type, and then have build_component_ref
2428 recursively call itself. We can't do that here. */
46ea50cb 2429 do
19d76e60 2430 {
e9b2c823 2431 tree subdatum = TREE_VALUE (field);
efed193e
JM
2432 int quals;
2433 tree subtype;
1e57bf47 2434 bool use_datum_quals;
e9b2c823
NB
2435
2436 if (TREE_TYPE (subdatum) == error_mark_node)
2437 return error_mark_node;
2438
1e57bf47
JM
2439 /* If this is an rvalue, it does not have qualifiers in C
2440 standard terms and we must avoid propagating such
2441 qualifiers down to a non-lvalue array that is then
2442 converted to a pointer. */
2443 use_datum_quals = (datum_lvalue
2444 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2445
efed193e 2446 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2447 if (use_datum_quals)
2448 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2449 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2450
2451 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2452 NULL_TREE);
c2255bc4 2453 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2454 if (TREE_READONLY (subdatum)
2455 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2456 TREE_READONLY (ref) = 1;
1e57bf47
JM
2457 if (TREE_THIS_VOLATILE (subdatum)
2458 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2459 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2460
2461 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2462 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2463
19d76e60 2464 datum = ref;
46ea50cb
RS
2465
2466 field = TREE_CHAIN (field);
19d76e60 2467 }
46ea50cb 2468 while (field);
19d76e60 2469
400fbf9f
JW
2470 return ref;
2471 }
a87a86e1
DM
2472 else if (should_suggest_deref_p (type))
2473 {
2474 /* Special-case the error message for "ptr.field" for the case
2475 where the user has confused "." vs "->". */
2476 rich_location richloc (line_table, loc);
2477 /* "loc" should be the "." token. */
f9087798 2478 richloc.add_fixit_replace ("->");
a87a86e1
DM
2479 error_at_rich_loc (&richloc,
2480 "%qE is a pointer; did you mean to use %<->%>?",
2481 datum);
2482 return error_mark_node;
2483 }
400fbf9f 2484 else if (code != ERROR_MARK)
c2255bc4
AH
2485 error_at (loc,
2486 "request for member %qE in something not a structure or union",
2487 component);
400fbf9f
JW
2488
2489 return error_mark_node;
2490}
2491\f
2492/* Given an expression PTR for a pointer, return an expression
2493 for the value pointed to.
6a3799eb
AH
2494 ERRORSTRING is the name of the operator to appear in error messages.
2495
2496 LOC is the location to use for the generated tree. */
400fbf9f
JW
2497
2498tree
dd865ef6 2499build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2500{
b3694847
SS
2501 tree pointer = default_conversion (ptr);
2502 tree type = TREE_TYPE (pointer);
6a3799eb 2503 tree ref;
400fbf9f
JW
2504
2505 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2506 {
1043771b 2507 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2508 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2509 {
2510 /* If a warning is issued, mark it to avoid duplicates from
2511 the backend. This only needs to be done at
2512 warn_strict_aliasing > 2. */
2513 if (warn_strict_aliasing > 2)
2514 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2515 type, TREE_OPERAND (pointer, 0)))
2516 TREE_NO_WARNING (pointer) = 1;
2517 }
2518
870cc33b 2519 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2520 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2521 == TREE_TYPE (type)))
6a3799eb
AH
2522 {
2523 ref = TREE_OPERAND (pointer, 0);
2524 protected_set_expr_location (ref, loc);
2525 return ref;
2526 }
870cc33b
RS
2527 else
2528 {
2529 tree t = TREE_TYPE (type);
46df2823 2530
984dfd8c 2531 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2532
baae9b65 2533 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2534 {
d9b7be2e
MP
2535 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2536 {
2537 error_at (loc, "dereferencing pointer to incomplete type "
2538 "%qT", t);
2539 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2540 }
870cc33b
RS
2541 return error_mark_node;
2542 }
7d882b83 2543 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2544 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2545
2546 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2547 so that we get the proper error message if the result is used
2548 to assign to. Also, &* is supposed to be a no-op.
2549 And ANSI C seems to specify that the type of the result
2550 should be the const type. */
2551 /* A de-reference of a pointer to const is not a const. It is valid
2552 to change it via some other pointer. */
2553 TREE_READONLY (ref) = TYPE_READONLY (t);
2554 TREE_SIDE_EFFECTS (ref)
271bd540 2555 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2556 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2557 protected_set_expr_location (ref, loc);
870cc33b
RS
2558 return ref;
2559 }
2560 }
400fbf9f 2561 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2562 invalid_indirection_error (loc, type, errstring);
2563
400fbf9f
JW
2564 return error_mark_node;
2565}
2566
2567/* This handles expressions of the form "a[i]", which denotes
2568 an array reference.
2569
2570 This is logically equivalent in C to *(a+i), but we may do it differently.
2571 If A is a variable or a member, we generate a primitive ARRAY_REF.
2572 This avoids forcing the array out of registers, and can work on
2573 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2574 by functions).
2575
30cd1c5d
AS
2576 For vector types, allow vector[i] but not i[vector], and create
2577 *(((type*)&vectortype) + i) for the expression.
2578
6a3799eb 2579 LOC is the location to use for the returned expression. */
400fbf9f
JW
2580
2581tree
c2255bc4 2582build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2583{
6a3799eb 2584 tree ret;
a4ab7973 2585 bool swapped = false;
400fbf9f
JW
2586 if (TREE_TYPE (array) == error_mark_node
2587 || TREE_TYPE (index) == error_mark_node)
2588 return error_mark_node;
2589
b72271b9 2590 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2591 {
2592 size_t rank = 0;
2593 if (!find_rank (loc, index, index, true, &rank))
2594 return error_mark_node;
2595 if (rank > 1)
2596 {
2597 error_at (loc, "rank of the array's index is greater than 1");
2598 return error_mark_node;
2599 }
2600 }
a4ab7973 2601 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2602 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2603 /* Allow vector[index] but not index[vector]. */
31521951 2604 && !VECTOR_TYPE_P (TREE_TYPE (array)))
400fbf9f 2605 {
a4ab7973
JM
2606 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2607 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2608 {
f90e8e2e 2609 error_at (loc,
30cd1c5d
AS
2610 "subscripted value is neither array nor pointer nor vector");
2611
fdeefd49
RS
2612 return error_mark_node;
2613 }
fab27f52 2614 std::swap (array, index);
a4ab7973
JM
2615 swapped = true;
2616 }
2617
2618 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2619 {
a63068b6 2620 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2621 return error_mark_node;
2622 }
2623
2624 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2625 {
a63068b6 2626 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2627 return error_mark_node;
2628 }
2629
ff6b6641
GDR
2630 /* ??? Existing practice has been to warn only when the char
2631 index is syntactically the index, not for char[array]. */
2632 if (!swapped)
5bd012f8 2633 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2634
2635 /* Apply default promotions *after* noticing character types. */
2636 index = default_conversion (index);
f406ae1f
MP
2637 if (index == error_mark_node)
2638 return error_mark_node;
a4ab7973
JM
2639
2640 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2641
f17a223d
RB
2642 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2643 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
a4ab7973
JM
2644
2645 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2646 {
e4d35515 2647 tree rval, type;
fdeefd49 2648
400fbf9f
JW
2649 /* An array that is indexed by a non-constant
2650 cannot be stored in a register; we must be able to do
2651 address arithmetic on its address.
2652 Likewise an array of elements of variable size. */
2653 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2654 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2655 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2656 {
dffd7eb6 2657 if (!c_mark_addressable (array))
400fbf9f
JW
2658 return error_mark_node;
2659 }
e6d52559
JW
2660 /* An array that is indexed by a constant value which is not within
2661 the array bounds cannot be stored in a register either; because we
2662 would get a crash in store_bit_field/extract_bit_field when trying
2663 to access a non-existent part of the register. */
2664 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2665 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2666 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2667 {
dffd7eb6 2668 if (!c_mark_addressable (array))
e6d52559
JW
2669 return error_mark_node;
2670 }
400fbf9f 2671
f17a223d
RB
2672 if ((pedantic || warn_c90_c99_compat)
2673 && ! was_vector)
400fbf9f
JW
2674 {
2675 tree foo = array;
2676 while (TREE_CODE (foo) == COMPONENT_REF)
2677 foo = TREE_OPERAND (foo, 0);
0ae9bd27 2678 if (VAR_P (foo) && C_DECL_REGISTER (foo))
c1771a20 2679 pedwarn (loc, OPT_Wpedantic,
fcf73884 2680 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2681 else if (!lvalue_p (foo))
2682 pedwarn_c90 (loc, OPT_Wpedantic,
2683 "ISO C90 forbids subscripting non-lvalue "
2684 "array");
400fbf9f
JW
2685 }
2686
46df2823 2687 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2688 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2689 /* Array ref is const/volatile if the array elements are
c22cacf3 2690 or if the array is. */
400fbf9f
JW
2691 TREE_READONLY (rval)
2692 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2693 | TREE_READONLY (array));
2694 TREE_SIDE_EFFECTS (rval)
2695 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2696 | TREE_SIDE_EFFECTS (array));
2697 TREE_THIS_VOLATILE (rval)
2698 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2699 /* This was added by rms on 16 Nov 91.
2f6e4e97 2700 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2701 in an inline function.
2702 Hope it doesn't break something else. */
2703 | TREE_THIS_VOLATILE (array));
4f2e1536 2704 ret = require_complete_type (loc, rval);
6a3799eb 2705 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2706 if (non_lvalue)
2707 ret = non_lvalue_loc (loc, ret);
6a3799eb 2708 return ret;
400fbf9f 2709 }
a4ab7973
JM
2710 else
2711 {
2712 tree ar = default_conversion (array);
400fbf9f 2713
a4ab7973
JM
2714 if (ar == error_mark_node)
2715 return ar;
400fbf9f 2716
a4ab7973
JM
2717 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2718 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2719
aa7da51a
JJ
2720 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2721 index, 0),
2722 RO_ARRAY_INDEXING);
2723 if (non_lvalue)
2724 ret = non_lvalue_loc (loc, ret);
2725 return ret;
a4ab7973 2726 }
400fbf9f
JW
2727}
2728\f
7e585d16 2729/* Build an external reference to identifier ID. FUN indicates
766beb40 2730 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2731 location of the identifier. This sets *TYPE to the type of the
2732 identifier, which is not the same as the type of the returned value
2733 for CONST_DECLs defined as enum constants. If the type of the
2734 identifier is not available, *TYPE is set to NULL. */
7e585d16 2735tree
c2255bc4 2736build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2737{
2738 tree ref;
2739 tree decl = lookup_name (id);
16b34ad6
ZL
2740
2741 /* In Objective-C, an instance variable (ivar) may be preferred to
2742 whatever lookup_name() found. */
2743 decl = objc_lookup_ivar (decl, id);
7e585d16 2744
6866c6e8 2745 *type = NULL;
339a28b9 2746 if (decl && decl != error_mark_node)
6866c6e8
ILT
2747 {
2748 ref = decl;
2749 *type = TREE_TYPE (ref);
2750 }
339a28b9
ZW
2751 else if (fun)
2752 /* Implicit function declaration. */
c2255bc4 2753 ref = implicitly_declare (loc, id);
339a28b9
ZW
2754 else if (decl == error_mark_node)
2755 /* Don't complain about something that's already been
2756 complained about. */
2757 return error_mark_node;
2758 else
2759 {
c2255bc4 2760 undeclared_variable (loc, id);
339a28b9
ZW
2761 return error_mark_node;
2762 }
7e585d16
ZW
2763
2764 if (TREE_TYPE (ref) == error_mark_node)
2765 return error_mark_node;
2766
339a28b9 2767 if (TREE_DEPRECATED (ref))
9b86d6bb 2768 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2769
ad960f56 2770 /* Recursive call does not count as usage. */
b8698a0f 2771 if (ref != current_function_decl)
ad960f56 2772 {
ad960f56
MLI
2773 TREE_USED (ref) = 1;
2774 }
7e585d16 2775
bc4b653b
JM
2776 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2777 {
2778 if (!in_sizeof && !in_typeof)
2779 C_DECL_USED (ref) = 1;
2780 else if (DECL_INITIAL (ref) == 0
2781 && DECL_EXTERNAL (ref)
2782 && !TREE_PUBLIC (ref))
2783 record_maybe_used_decl (ref);
2784 }
2785
7e585d16
ZW
2786 if (TREE_CODE (ref) == CONST_DECL)
2787 {
6193b8b7 2788 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2789
2790 if (warn_cxx_compat
2791 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2792 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2793 {
2794 warning_at (loc, OPT_Wc___compat,
2795 ("enum constant defined in struct or union "
2796 "is not visible in C++"));
2797 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2798 }
2799
7e585d16
ZW
2800 ref = DECL_INITIAL (ref);
2801 TREE_CONSTANT (ref) = 1;
2802 }
6a29edea 2803 else if (current_function_decl != 0
4b1e44be 2804 && !DECL_FILE_SCOPE_P (current_function_decl)
21b634ae
MP
2805 && (VAR_OR_FUNCTION_DECL_P (ref)
2806 || TREE_CODE (ref) == PARM_DECL))
6a29edea
EB
2807 {
2808 tree context = decl_function_context (ref);
2f6e4e97 2809
6a29edea
EB
2810 if (context != 0 && context != current_function_decl)
2811 DECL_NONLOCAL (ref) = 1;
2812 }
71113fcd
GK
2813 /* C99 6.7.4p3: An inline definition of a function with external
2814 linkage ... shall not contain a reference to an identifier with
2815 internal linkage. */
2816 else if (current_function_decl != 0
2817 && DECL_DECLARED_INLINE_P (current_function_decl)
2818 && DECL_EXTERNAL (current_function_decl)
2819 && VAR_OR_FUNCTION_DECL_P (ref)
0ae9bd27 2820 && (!VAR_P (ref) || TREE_STATIC (ref))
1033ffa8
JJ
2821 && ! TREE_PUBLIC (ref)
2822 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2823 record_inline_static (loc, current_function_decl, ref,
2824 csi_internal);
7e585d16
ZW
2825
2826 return ref;
2827}
2828
bc4b653b
JM
2829/* Record details of decls possibly used inside sizeof or typeof. */
2830struct maybe_used_decl
2831{
2832 /* The decl. */
2833 tree decl;
2834 /* The level seen at (in_sizeof + in_typeof). */
2835 int level;
2836 /* The next one at this level or above, or NULL. */
2837 struct maybe_used_decl *next;
2838};
2839
2840static struct maybe_used_decl *maybe_used_decls;
2841
2842/* Record that DECL, an undefined static function reference seen
2843 inside sizeof or typeof, might be used if the operand of sizeof is
2844 a VLA type or the operand of typeof is a variably modified
2845 type. */
2846
4e2fb7de 2847static void
bc4b653b
JM
2848record_maybe_used_decl (tree decl)
2849{
2850 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2851 t->decl = decl;
2852 t->level = in_sizeof + in_typeof;
2853 t->next = maybe_used_decls;
2854 maybe_used_decls = t;
2855}
2856
2857/* Pop the stack of decls possibly used inside sizeof or typeof. If
2858 USED is false, just discard them. If it is true, mark them used
2859 (if no longer inside sizeof or typeof) or move them to the next
2860 level up (if still inside sizeof or typeof). */
2861
2862void
2863pop_maybe_used (bool used)
2864{
2865 struct maybe_used_decl *p = maybe_used_decls;
2866 int cur_level = in_sizeof + in_typeof;
2867 while (p && p->level > cur_level)
2868 {
2869 if (used)
2870 {
2871 if (cur_level == 0)
2872 C_DECL_USED (p->decl) = 1;
2873 else
2874 p->level = cur_level;
2875 }
2876 p = p->next;
2877 }
2878 if (!used || cur_level == 0)
2879 maybe_used_decls = p;
2880}
2881
2882/* Return the result of sizeof applied to EXPR. */
2883
2884struct c_expr
c2255bc4 2885c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2886{
2887 struct c_expr ret;
ad97f4be
JM
2888 if (expr.value == error_mark_node)
2889 {
2890 ret.value = error_mark_node;
2891 ret.original_code = ERROR_MARK;
6866c6e8 2892 ret.original_type = NULL;
ad97f4be
JM
2893 pop_maybe_used (false);
2894 }
2895 else
2896 {
928c19bb 2897 bool expr_const_operands = true;
773ec47f
MP
2898
2899 if (TREE_CODE (expr.value) == PARM_DECL
2900 && C_ARRAY_PARAMETER (expr.value))
2901 {
2902 if (warning_at (loc, OPT_Wsizeof_array_argument,
2903 "%<sizeof%> on array function parameter %qE will "
2904 "return size of %qT", expr.value,
2905 expr.original_type))
2906 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2907 }
928c19bb
JM
2908 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2909 &expr_const_operands);
c2255bc4 2910 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2911 c_last_sizeof_arg = expr.value;
2912 ret.original_code = SIZEOF_EXPR;
6866c6e8 2913 ret.original_type = NULL;
928c19bb 2914 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2915 {
2916 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2917 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2918 folded_expr, ret.value);
2919 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2920 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2921 }
928c19bb 2922 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2923 }
bc4b653b
JM
2924 return ret;
2925}
2926
2927/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2928 name passed to sizeof (rather than the type itself). LOC is the
2929 location of the original expression. */
bc4b653b
JM
2930
2931struct c_expr
c2255bc4 2932c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2933{
2934 tree type;
2935 struct c_expr ret;
928c19bb
JM
2936 tree type_expr = NULL_TREE;
2937 bool type_expr_const = true;
2938 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2939 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2940 c_last_sizeof_arg = type;
2941 ret.original_code = SIZEOF_EXPR;
6866c6e8 2942 ret.original_type = NULL;
24070fcb
JM
2943 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2944 && c_vla_type_p (type))
928c19bb 2945 {
24070fcb
JM
2946 /* If the type is a [*] array, it is a VLA but is represented as
2947 having a size of zero. In such a case we must ensure that
2948 the result of sizeof does not get folded to a constant by
2949 c_fully_fold, because if the size is evaluated the result is
2950 not constant and so constraints on zero or negative size
2951 arrays must not be applied when this sizeof call is inside
2952 another array declarator. */
2953 if (!type_expr)
2954 type_expr = integer_zero_node;
928c19bb
JM
2955 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2956 type_expr, ret.value);
2957 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2958 }
16464cc1
VR
2959 pop_maybe_used (type != error_mark_node
2960 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2961 return ret;
2962}
2963
400fbf9f 2964/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2965 The function call is at LOC.
400fbf9f
JW
2966 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2967 TREE_VALUE of each node is a parameter-expression.
2968 FUNCTION's data type may be a function type or a pointer-to-function. */
2969
2970tree
c2255bc4 2971build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2972{
9771b263 2973 vec<tree, va_gc> *v;
bbbbb16a
ILT
2974 tree ret;
2975
9771b263 2976 vec_alloc (v, list_length (params));
bbbbb16a 2977 for (; params; params = TREE_CHAIN (params))
9771b263 2978 v->quick_push (TREE_VALUE (params));
8edbfaa6 2979 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2980 vec_free (v);
bbbbb16a
ILT
2981 return ret;
2982}
2983
ae52741c
MLI
2984/* Give a note about the location of the declaration of DECL. */
2985
c7b70a3c
MP
2986static void
2987inform_declaration (tree decl)
ae52741c 2988{
c7b70a3c 2989 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
ae52741c
MLI
2990 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2991}
2992
bbbbb16a
ILT
2993/* Build a function call to function FUNCTION with parameters PARAMS.
2994 ORIGTYPES, if not NULL, is a vector of types; each element is
2995 either NULL or the original type of the corresponding element in
2996 PARAMS. The original type may differ from TREE_TYPE of the
2997 parameter for enums. FUNCTION's data type may be a function type
2998 or pointer-to-function. This function changes the elements of
2999 PARAMS. */
3000
3001tree
81e5eca8
MP
3002build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3003 tree function, vec<tree, va_gc> *params,
9771b263 3004 vec<tree, va_gc> *origtypes)
400fbf9f 3005{
b3694847 3006 tree fntype, fundecl = 0;
4977bab6 3007 tree name = NULL_TREE, result;
c96f4f73 3008 tree tem;
94a0dd7b
SL
3009 int nargs;
3010 tree *argarray;
b8698a0f 3011
400fbf9f 3012
fc76e425 3013 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 3014 STRIP_TYPE_NOPS (function);
400fbf9f
JW
3015
3016 /* Convert anything with function type to a pointer-to-function. */
3017 if (TREE_CODE (function) == FUNCTION_DECL)
3018 {
3019 name = DECL_NAME (function);
0a35513e
AH
3020
3021 if (flag_tm)
3022 tm_malloc_replacement (function);
a5eadacc 3023 fundecl = function;
86951993
AM
3024 /* Atomic functions have type checking/casting already done. They are
3025 often rewritten and don't match the original parameter list. */
3026 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3027 origtypes = NULL;
36536d79 3028
b72271b9 3029 if (flag_cilkplus
36536d79
BI
3030 && is_cilkplus_reduce_builtin (function))
3031 origtypes = NULL;
400fbf9f 3032 }
f2a71bbc 3033 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 3034 function = function_to_pointer_conversion (loc, function);
400fbf9f 3035
6e955430
ZL
3036 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3037 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
3038 if (params && !params->is_empty ())
3039 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 3040
928c19bb
JM
3041 function = c_fully_fold (function, false, NULL);
3042
400fbf9f
JW
3043 fntype = TREE_TYPE (function);
3044
3045 if (TREE_CODE (fntype) == ERROR_MARK)
3046 return error_mark_node;
3047
3048 if (!(TREE_CODE (fntype) == POINTER_TYPE
3049 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3050 {
ae52741c
MLI
3051 if (!flag_diagnostics_show_caret)
3052 error_at (loc,
3053 "called object %qE is not a function or function pointer",
3054 function);
3055 else if (DECL_P (function))
3056 {
3057 error_at (loc,
3058 "called object %qD is not a function or function pointer",
3059 function);
3060 inform_declaration (function);
3061 }
3062 else
3063 error_at (loc,
3064 "called object is not a function or function pointer");
400fbf9f
JW
3065 return error_mark_node;
3066 }
3067
5ce89b2e
JM
3068 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3069 current_function_returns_abnormally = 1;
3070
400fbf9f
JW
3071 /* fntype now gets the type of function pointed to. */
3072 fntype = TREE_TYPE (fntype);
3073
ab4194da
JM
3074 /* Convert the parameters to the types declared in the
3075 function prototype, or apply default promotions. */
3076
81e5eca8
MP
3077 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3078 origtypes, function, fundecl);
ab4194da
JM
3079 if (nargs < 0)
3080 return error_mark_node;
3081
c96f4f73 3082 /* Check that the function is called through a compatible prototype.
fa337f3a 3083 If it is not, warn. */
1043771b 3084 if (CONVERT_EXPR_P (function)
c96f4f73
EB
3085 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3086 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 3087 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
3088 {
3089 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
3090
3091 /* This situation leads to run-time undefined behavior. We can't,
3092 therefore, simply error unless we can prove that all possible
3093 executions of the program must execute the code. */
fa337f3a 3094 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 3095
fa337f3a
RB
3096 if (VOID_TYPE_P (return_type)
3097 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3098 pedwarn (loc, 0,
3099 "function with qualified void return type called");
3100 }
c96f4f73 3101
9771b263 3102 argarray = vec_safe_address (params);
bbbbb16a 3103
83322951
RG
3104 /* Check that arguments to builtin functions match the expectations. */
3105 if (fundecl
3106 && DECL_BUILT_IN (fundecl)
3107 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
79ce98bc
MP
3108 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3109 argarray))
83322951 3110 return error_mark_node;
400fbf9f 3111
83322951 3112 /* Check that the arguments to the function are valid. */
3342fd71 3113 check_function_arguments (loc, fntype, nargs, argarray);
400fbf9f 3114
928c19bb
JM
3115 if (name != NULL_TREE
3116 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 3117 {
928c19bb 3118 if (require_constant_value)
b8698a0f 3119 result =
db3927fb
AH
3120 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3121 function, nargs, argarray);
928c19bb 3122 else
db3927fb
AH
3123 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3124 function, nargs, argarray);
928c19bb
JM
3125 if (TREE_CODE (result) == NOP_EXPR
3126 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3127 STRIP_TYPE_NOPS (result);
bf730f15
RS
3128 }
3129 else
db3927fb
AH
3130 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3131 function, nargs, argarray);
b0b3afb2 3132
ab4c578f
MP
3133 /* In this improbable scenario, a nested function returns a VM type.
3134 Create a TARGET_EXPR so that the call always has a LHS, much as
3135 what the C++ FE does for functions returning non-PODs. */
3136 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3137 {
3138 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3139 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3140 NULL_TREE, NULL_TREE);
3141 }
3142
71653180 3143 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
3144 {
3145 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 3146 pedwarn (loc, 0,
3ce62965
JM
3147 "function with qualified void return type called");
3148 return result;
3149 }
4f2e1536 3150 return require_complete_type (loc, result);
400fbf9f 3151}
8edbfaa6
JJ
3152
3153/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3154
3155tree
3156c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3157 tree function, vec<tree, va_gc> *params,
3158 vec<tree, va_gc> *origtypes)
3159{
3160 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3161 STRIP_TYPE_NOPS (function);
3162
3163 /* Convert anything with function type to a pointer-to-function. */
3164 if (TREE_CODE (function) == FUNCTION_DECL)
3165 {
3166 /* Implement type-directed function overloading for builtins.
3167 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3168 handle all the type checking. The result is a complete expression
3169 that implements this function call. */
3170 tree tem = resolve_overloaded_builtin (loc, function, params);
3171 if (tem)
3172 return tem;
3173 }
3174 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3175}
400fbf9f 3176\f
bbbbb16a
ILT
3177/* Convert the argument expressions in the vector VALUES
3178 to the types in the list TYPELIST.
400fbf9f
JW
3179
3180 If TYPELIST is exhausted, or when an element has NULL as its type,
3181 perform the default conversions.
3182
bbbbb16a
ILT
3183 ORIGTYPES is the original types of the expressions in VALUES. This
3184 holds the type of enum values which have been converted to integral
3185 types. It may be NULL.
400fbf9f 3186
03dafa61
JM
3187 FUNCTION is a tree for the called function. It is used only for
3188 error messages, where it is formatted with %qE.
400fbf9f
JW
3189
3190 This is also where warnings about wrong number of args are generated.
3191
81e5eca8
MP
3192 ARG_LOC are locations of function arguments (if any).
3193
94a0dd7b 3194 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3195 than the length of VALUES in some error situations), or -1 on
3196 failure. */
94a0dd7b
SL
3197
3198static int
81e5eca8
MP
3199convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3200 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3201 tree function, tree fundecl)
400fbf9f 3202{
bbbbb16a
ILT
3203 tree typetail, val;
3204 unsigned int parmnum;
06302a02 3205 bool error_args = false;
b5d32c25 3206 const bool type_generic = fundecl
81e5eca8 3207 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3208 bool type_generic_remove_excess_precision = false;
44a845ca 3209 bool type_generic_overflow_p = false;
03dafa61 3210 tree selector;
03dafa61 3211
2ac2f164
JM
3212 /* Change pointer to function to the function itself for
3213 diagnostics. */
03dafa61
JM
3214 if (TREE_CODE (function) == ADDR_EXPR
3215 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3216 function = TREE_OPERAND (function, 0);
03dafa61
JM
3217
3218 /* Handle an ObjC selector specially for diagnostics. */
3219 selector = objc_message_selector ();
400fbf9f 3220
8ce94e44
JM
3221 /* For type-generic built-in functions, determine whether excess
3222 precision should be removed (classification) or not
3223 (comparison). */
3224 if (type_generic
3225 && DECL_BUILT_IN (fundecl)
3226 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3227 {
3228 switch (DECL_FUNCTION_CODE (fundecl))
3229 {
3230 case BUILT_IN_ISFINITE:
3231 case BUILT_IN_ISINF:
3232 case BUILT_IN_ISINF_SIGN:
3233 case BUILT_IN_ISNAN:
3234 case BUILT_IN_ISNORMAL:
3235 case BUILT_IN_FPCLASSIFY:
3236 type_generic_remove_excess_precision = true;
3237 break;
3238
44a845ca
MS
3239 case BUILT_IN_ADD_OVERFLOW_P:
3240 case BUILT_IN_SUB_OVERFLOW_P:
3241 case BUILT_IN_MUL_OVERFLOW_P:
3242 /* The last argument of these type-generic builtins
3243 should not be promoted. */
3244 type_generic_overflow_p = true;
3245 break;
3246
8ce94e44 3247 default:
8ce94e44
JM
3248 break;
3249 }
3250 }
b72271b9 3251 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3252 return vec_safe_length (values);
8ce94e44 3253
400fbf9f 3254 /* Scan the given expressions and types, producing individual
bbbbb16a 3255 converted arguments. */
400fbf9f 3256
bbbbb16a 3257 for (typetail = typelist, parmnum = 0;
9771b263 3258 values && values->iterate (parmnum, &val);
bbbbb16a 3259 ++parmnum)
400fbf9f 3260 {
b3694847 3261 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3262 tree valtype = TREE_TYPE (val);
03dafa61
JM
3263 tree rname = function;
3264 int argnum = parmnum + 1;
4d3e6fae 3265 const char *invalid_func_diag;
8ce94e44 3266 bool excess_precision = false;
928c19bb 3267 bool npc;
bbbbb16a 3268 tree parmval;
5c1bc275
MP
3269 /* Some __atomic_* builtins have additional hidden argument at
3270 position 0. */
3271 location_t ploc
3272 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3273 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3274 : input_location;
400fbf9f
JW
3275
3276 if (type == void_type_node)
3277 {
19dc6d01 3278 if (selector)
68fca595 3279 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3280 else
68fca595 3281 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3282 inform_declaration (fundecl);
d38f7dce 3283 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3284 }
3285
03dafa61
JM
3286 if (selector && argnum > 2)
3287 {
3288 rname = selector;
3289 argnum -= 2;
3290 }
3291
928c19bb 3292 npc = null_pointer_constant_p (val);
8ce94e44
JM
3293
3294 /* If there is excess precision and a prototype, convert once to
3295 the required type rather than converting via the semantic
3296 type. Likewise without a prototype a float value represented
3297 as long double should be converted once to double. But for
3298 type-generic classification functions excess precision must
3299 be removed here. */
3300 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3301 && (type || !type_generic || !type_generic_remove_excess_precision))
3302 {
3303 val = TREE_OPERAND (val, 0);
3304 excess_precision = true;
3305 }
928c19bb 3306 val = c_fully_fold (val, false, NULL);
ed248cf7 3307 STRIP_TYPE_NOPS (val);
400fbf9f 3308
4f2e1536 3309 val = require_complete_type (ploc, val);
400fbf9f 3310
c65699ef
JM
3311 /* Some floating-point arguments must be promoted to double when
3312 no type is specified by a prototype. This applies to
3313 arguments of type float, and to architecture-specific types
3314 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3315 bool promote_float_arg = false;
3316 if (type == NULL_TREE
3317 && TREE_CODE (valtype) == REAL_TYPE
3318 && (TYPE_PRECISION (valtype)
3319 <= TYPE_PRECISION (double_type_node))
3320 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3321 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3322 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3323 {
3324 /* Promote this argument, unless it has a _FloatN or
3325 _FloatNx type. */
3326 promote_float_arg = true;
3327 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3328 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3329 {
3330 promote_float_arg = false;
3331 break;
3332 }
3333 }
3334
400fbf9f
JW
3335 if (type != 0)
3336 {
3337 /* Formal parm type is specified by a function prototype. */
400fbf9f 3338
20913689 3339 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3340 {
5c1bc275
MP
3341 error_at (ploc, "type of formal parameter %d is incomplete",
3342 parmnum + 1);
400fbf9f
JW
3343 parmval = val;
3344 }
3345 else
3346 {
bbbbb16a
ILT
3347 tree origtype;
3348
d45cf215
RS
3349 /* Optionally warn about conversions that
3350 differ from the default conversions. */
05170031 3351 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3352 {
e3a64162 3353 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3354
aae43c5f 3355 if (INTEGRAL_TYPE_P (type)
8ce94e44 3356 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3357 warning_at (ploc, OPT_Wtraditional_conversion,
3358 "passing argument %d of %qE as integer rather "
3359 "than floating due to prototype",
3360 argnum, rname);
03829ad2 3361 if (INTEGRAL_TYPE_P (type)
8ce94e44 3362 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3363 warning_at (ploc, OPT_Wtraditional_conversion,
3364 "passing argument %d of %qE as integer rather "
3365 "than complex due to prototype",
3366 argnum, rname);
aae43c5f 3367 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3368 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3369 warning_at (ploc, OPT_Wtraditional_conversion,
3370 "passing argument %d of %qE as complex rather "
3371 "than floating due to prototype",
3372 argnum, rname);
400fbf9f 3373 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3374 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3375 warning_at (ploc, OPT_Wtraditional_conversion,
3376 "passing argument %d of %qE as floating rather "
3377 "than integer due to prototype",
3378 argnum, rname);
03829ad2 3379 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3380 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3381 warning_at (ploc, OPT_Wtraditional_conversion,
3382 "passing argument %d of %qE as complex rather "
3383 "than integer due to prototype",
3384 argnum, rname);
aae43c5f 3385 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3386 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3387 warning_at (ploc, OPT_Wtraditional_conversion,
3388 "passing argument %d of %qE as floating rather "
3389 "than complex due to prototype",
3390 argnum, rname);
aae43c5f
RK
3391 /* ??? At some point, messages should be written about
3392 conversions between complex types, but that's too messy
3393 to do now. */
d45cf215 3394 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3395 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3396 {
3397 /* Warn if any argument is passed as `float',
047de90b 3398 since without a prototype it would be `double'. */
9a8ce21f
JG
3399 if (formal_prec == TYPE_PRECISION (float_type_node)
3400 && type != dfloat32_type_node)
5c1bc275
MP
3401 warning_at (ploc, 0,
3402 "passing argument %d of %qE as %<float%> "
3403 "rather than %<double%> due to prototype",
3404 argnum, rname);
9a8ce21f
JG
3405
3406 /* Warn if mismatch between argument and prototype
3407 for decimal float types. Warn of conversions with
3408 binary float types and of precision narrowing due to
3409 prototype. */
8ce94e44 3410 else if (type != valtype
9a8ce21f
JG
3411 && (type == dfloat32_type_node
3412 || type == dfloat64_type_node
c22cacf3 3413 || type == dfloat128_type_node
8ce94e44
JM
3414 || valtype == dfloat32_type_node
3415 || valtype == dfloat64_type_node
3416 || valtype == dfloat128_type_node)
c22cacf3 3417 && (formal_prec
8ce94e44 3418 <= TYPE_PRECISION (valtype)
9a8ce21f 3419 || (type == dfloat128_type_node
8ce94e44 3420 && (valtype
c22cacf3 3421 != dfloat64_type_node
8ce94e44 3422 && (valtype
9a8ce21f
JG
3423 != dfloat32_type_node)))
3424 || (type == dfloat64_type_node
8ce94e44 3425 && (valtype
9a8ce21f 3426 != dfloat32_type_node))))
5c1bc275
MP
3427 warning_at (ploc, 0,
3428 "passing argument %d of %qE as %qT "
3429 "rather than %qT due to prototype",
3430 argnum, rname, type, valtype);
9a8ce21f 3431
d45cf215 3432 }
3ed56f8a
KG
3433 /* Detect integer changing in width or signedness.
3434 These warnings are only activated with
05170031
MLI
3435 -Wtraditional-conversion, not with -Wtraditional. */
3436 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3437 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3438 {
d45cf215
RS
3439 tree would_have_been = default_conversion (val);
3440 tree type1 = TREE_TYPE (would_have_been);
3441
754a4d82 3442 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3443 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3444 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3445 /* No warning if function asks for enum
3446 and the actual arg is that enum type. */
3447 ;
3448 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3449 warning_at (ploc, OPT_Wtraditional_conversion,
3450 "passing argument %d of %qE "
3451 "with different width due to prototype",
3452 argnum, rname);
8df83eae 3453 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3454 ;
800cd3b9
RS
3455 /* Don't complain if the formal parameter type
3456 is an enum, because we can't tell now whether
3457 the value was an enum--even the same enum. */
3458 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3459 ;
400fbf9f
JW
3460 else if (TREE_CODE (val) == INTEGER_CST
3461 && int_fits_type_p (val, type))
3462 /* Change in signedness doesn't matter
3463 if a constant value is unaffected. */
3464 ;
ce9895ae
RS
3465 /* If the value is extended from a narrower
3466 unsigned type, it doesn't matter whether we
3467 pass it as signed or unsigned; the value
3468 certainly is the same either way. */
8ce94e44
JM
3469 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3470 && TYPE_UNSIGNED (valtype))
ce9895ae 3471 ;
8df83eae 3472 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3473 warning_at (ploc, OPT_Wtraditional_conversion,
3474 "passing argument %d of %qE "
3475 "as unsigned due to prototype",
3476 argnum, rname);
3ed56f8a 3477 else
5c1bc275
MP
3478 warning_at (ploc, OPT_Wtraditional_conversion,
3479 "passing argument %d of %qE "
3480 "as signed due to prototype",
3481 argnum, rname);
400fbf9f
JW
3482 }
3483 }
3484
8ce94e44
JM
3485 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3486 sake of better warnings from convert_and_check. */
3487 if (excess_precision)
3488 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3489 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3490 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3491 val, origtype, ic_argpass,
3492 npc, fundecl, function,
2ac2f164 3493 parmnum + 1);
2f6e4e97 3494
61f71b34 3495 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3496 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3497 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3498 parmval = default_conversion (parmval);
400fbf9f 3499 }
400fbf9f 3500 }
c65699ef 3501 else if (promote_float_arg)
b5d32c25
KG
3502 {
3503 if (type_generic)
bbbbb16a 3504 parmval = val;
b5d32c25 3505 else
0a0b3574
MM
3506 {
3507 /* Convert `float' to `double'. */
3508 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3509 warning_at (ploc, OPT_Wdouble_promotion,
3510 "implicit conversion from %qT to %qT when passing "
3511 "argument to function",
3512 valtype, double_type_node);
0a0b3574
MM
3513 parmval = convert (double_type_node, val);
3514 }
b5d32c25 3515 }
44a845ca
MS
3516 else if ((excess_precision && !type_generic)
3517 || (type_generic_overflow_p && parmnum == 2))
8ce94e44 3518 /* A "double" argument with excess precision being passed
44a845ca
MS
3519 without a prototype or in variable arguments.
3520 The last argument of __builtin_*_overflow_p should not be
3521 promoted. */
bbbbb16a 3522 parmval = convert (valtype, val);
c22cacf3
MS
3523 else if ((invalid_func_diag =
3524 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3525 {
3526 error (invalid_func_diag);
94a0dd7b 3527 return -1;
4d3e6fae 3528 }
1807ffc1
MS
3529 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3530 {
3531 return -1;
3532 }
400fbf9f
JW
3533 else
3534 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3535 parmval = default_conversion (val);
3536
9771b263 3537 (*values)[parmnum] = parmval;
06302a02
JJ
3538 if (parmval == error_mark_node)
3539 error_args = true;
400fbf9f
JW
3540
3541 if (typetail)
3542 typetail = TREE_CHAIN (typetail);
3543 }
3544
9771b263 3545 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3546
400fbf9f 3547 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3548 {
68fca595 3549 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3550 inform_declaration (fundecl);
3551 return -1;
3789b316 3552 }
400fbf9f 3553
06302a02 3554 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3555}
3556\f
43f6dfd3
RS
3557/* This is the entry point used by the parser to build unary operators
3558 in the input. CODE, a tree_code, specifies the unary operator, and
3559 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3560 CONVERT_EXPR for code.
3561
3562 LOC is the location to use for the tree generated.
3563*/
43f6dfd3
RS
3564
3565struct c_expr
c2255bc4 3566parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3567{
3568 struct c_expr result;
3569
100d537d 3570 result.original_code = code;
6866c6e8
ILT
3571 result.original_type = NULL;
3572
1807ffc1
MS
3573 if (reject_gcc_builtin (arg.value))
3574 {
3575 result.value = error_mark_node;
3576 }
3577 else
3578 {
e51fbec3 3579 result.value = build_unary_op (loc, code, arg.value, false);
1807ffc1 3580
e9892350
JG
3581 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3582 overflow_warning (loc, result.value);
1807ffc1 3583 }
59c0753d 3584
ebedc9a3
DM
3585 /* We are typically called when parsing a prefix token at LOC acting on
3586 ARG. Reflect this by updating the source range of the result to
3587 start at LOC and end at the end of ARG. */
3588 set_c_expr_source_range (&result,
3589 loc, arg.get_finish ());
3590
43f6dfd3
RS
3591 return result;
3592}
3593
3594/* This is the entry point used by the parser to build binary operators
3595 in the input. CODE, a tree_code, specifies the binary operator, and
3596 ARG1 and ARG2 are the operands. In addition to constructing the
3597 expression, we check for operands that were written with other binary
ba47d38d
AH
3598 operators in a way that is likely to confuse the user.
3599
3600 LOCATION is the location of the binary operator. */
edc7c4ec 3601
487a92fe 3602struct c_expr
ba47d38d
AH
3603parser_build_binary_op (location_t location, enum tree_code code,
3604 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3605{
487a92fe 3606 struct c_expr result;
400fbf9f 3607
487a92fe
JM
3608 enum tree_code code1 = arg1.original_code;
3609 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3610 tree type1 = (arg1.original_type
3611 ? arg1.original_type
3612 : TREE_TYPE (arg1.value));
3613 tree type2 = (arg2.original_type
3614 ? arg2.original_type
3615 : TREE_TYPE (arg2.value));
400fbf9f 3616
ba47d38d
AH
3617 result.value = build_binary_op (location, code,
3618 arg1.value, arg2.value, 1);
487a92fe 3619 result.original_code = code;
6866c6e8 3620 result.original_type = NULL;
58bf601b 3621
487a92fe 3622 if (TREE_CODE (result.value) == ERROR_MARK)
8a40fef3
DM
3623 {
3624 set_c_expr_source_range (&result,
3625 arg1.get_start (),
3626 arg2.get_finish ());
3627 return result;
3628 }
400fbf9f 3629
ba47d38d
AH
3630 if (location != UNKNOWN_LOCATION)
3631 protected_set_expr_location (result.value, location);
3632
ebedc9a3
DM
3633 set_c_expr_source_range (&result,
3634 arg1.get_start (),
3635 arg2.get_finish ());
3636
400fbf9f 3637 /* Check for cases such as x+y<<z which users are likely
487a92fe 3638 to misinterpret. */
400fbf9f 3639 if (warn_parentheses)
393e8e8b
MP
3640 warn_about_parentheses (location, code, code1, arg1.value, code2,
3641 arg2.value);
001af587 3642
ca409efd 3643 if (warn_logical_op)
393e8e8b 3644 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3645 code1, arg1.value, code2, arg2.value);
63a08740 3646
05b28fd6 3647 if (warn_tautological_compare)
433068cc
MP
3648 {
3649 tree lhs = arg1.value;
3650 tree rhs = arg2.value;
3651 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3652 {
3653 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3654 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3655 lhs = NULL_TREE;
3656 else
3657 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3658 }
3659 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3660 {
3661 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3662 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3663 rhs = NULL_TREE;
3664 else
3665 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3666 }
3667 if (lhs != NULL_TREE && rhs != NULL_TREE)
3668 warn_tautological_cmp (location, code, lhs, rhs);
3669 }
05b28fd6 3670
742938c9 3671 if (warn_logical_not_paren
7ccb1a11 3672 && TREE_CODE_CLASS (code) == tcc_comparison
47c2554f 3673 && code1 == TRUTH_NOT_EXPR
01177669
JJ
3674 && code2 != TRUTH_NOT_EXPR
3675 /* Avoid warning for !!x == y. */
3676 && (TREE_CODE (arg1.value) != NE_EXPR
3677 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3678 {
3679 /* Avoid warning for !b == y where b has _Bool type. */
3680 tree t = integer_zero_node;
3681 if (TREE_CODE (arg1.value) == EQ_EXPR
3682 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3683 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3684 {
3685 t = TREE_OPERAND (arg1.value, 0);
3686 do
3687 {
3688 if (TREE_TYPE (t) != integer_type_node)
3689 break;
3690 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3691 t = C_MAYBE_CONST_EXPR_EXPR (t);
3692 else if (CONVERT_EXPR_P (t))
3693 t = TREE_OPERAND (t, 0);
3694 else
3695 break;
3696 }
3697 while (1);
3698 }
3699 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
ebef225f 3700 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
01177669 3701 }
742938c9 3702
e994a705
RS
3703 /* Warn about comparisons against string literals, with the exception
3704 of testing for equality or inequality of a string literal with NULL. */
3705 if (code == EQ_EXPR || code == NE_EXPR)
3706 {
66756373
JJ
3707 if ((code1 == STRING_CST
3708 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3709 || (code2 == STRING_CST
3710 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
c2255bc4
AH
3711 warning_at (location, OPT_Waddress,
3712 "comparison with string literal results in unspecified behavior");
e994a705
RS
3713 }
3714 else if (TREE_CODE_CLASS (code) == tcc_comparison
3715 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3716 warning_at (location, OPT_Waddress,
3717 "comparison with string literal results in unspecified behavior");
e994a705 3718
b8698a0f
L
3719 if (TREE_OVERFLOW_P (result.value)
3720 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3721 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3722 overflow_warning (location, result.value);
400fbf9f 3723
6866c6e8
ILT
3724 /* Warn about comparisons of different enum types. */
3725 if (warn_enum_compare
3726 && TREE_CODE_CLASS (code) == tcc_comparison
3727 && TREE_CODE (type1) == ENUMERAL_TYPE
3728 && TREE_CODE (type2) == ENUMERAL_TYPE
3729 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3730 warning_at (location, OPT_Wenum_compare,
3731 "comparison between %qT and %qT",
3732 type1, type2);
3733
400fbf9f
JW
3734 return result;
3735}
3e4093b6 3736\f
3e4093b6
RS
3737/* Return a tree for the difference of pointers OP0 and OP1.
3738 The resulting tree has type int. */
293c9fdd 3739
3e4093b6 3740static tree
db3927fb 3741pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3742{
3e4093b6 3743 tree restype = ptrdiff_type_node;
36c5e70a 3744 tree result, inttype;
400fbf9f 3745
36c5e70a
BE
3746 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3747 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3748 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3749 tree orig_op1 = op1;
400fbf9f 3750
36c5e70a
BE
3751 /* If the operands point into different address spaces, we need to
3752 explicitly convert them to pointers into the common address space
3753 before we can subtract the numerical address values. */
3754 if (as0 != as1)
3755 {
3756 addr_space_t as_common;
3757 tree common_type;
3758
3759 /* Determine the common superset address space. This is guaranteed
3760 to exist because the caller verified that comp_target_types
3761 returned non-zero. */
3762 if (!addr_space_superset (as0, as1, &as_common))
3763 gcc_unreachable ();
3764
3765 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3766 op0 = convert (common_type, op0);
3767 op1 = convert (common_type, op1);
3768 }
3769
3770 /* Determine integer type to perform computations in. This will usually
3771 be the same as the result type (ptrdiff_t), but may need to be a wider
3772 type if pointers for the address space are wider than ptrdiff_t. */
3773 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3774 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3775 else
3776 inttype = restype;
3777
fcf73884 3778 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3779 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3780 "pointer of type %<void *%> used in subtraction");
3781 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3782 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3783 "pointer to a function used in subtraction");
400fbf9f 3784
3e4093b6
RS
3785 /* First do the subtraction as integers;
3786 then drop through to build the divide operator.
3787 Do not do default conversions on the minus operator
3788 in case restype is a short type. */
400fbf9f 3789
db3927fb 3790 op0 = build_binary_op (loc,
36c5e70a
BE
3791 MINUS_EXPR, convert (inttype, op0),
3792 convert (inttype, op1), 0);
3e4093b6
RS
3793 /* This generates an error if op1 is pointer to incomplete type. */
3794 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3795 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3796
3e4093b6 3797 op1 = c_size_in_bytes (target_type);
400fbf9f 3798
f04dda30
MP
3799 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3800 error_at (loc, "arithmetic on pointer to an empty aggregate");
3801
3e4093b6 3802 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3803 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3804 op0, convert (inttype, op1));
3805
3806 /* Convert to final result type if necessary. */
3807 return convert (restype, result);
3e4093b6
RS
3808}
3809\f
2fe0a208
MP
3810/* Expand atomic compound assignments into an appropriate sequence as
3811 specified by the C11 standard section 6.5.16.2.
3812
267bac10
JM
3813 _Atomic T1 E1
3814 T2 E2
3815 E1 op= E2
3816
3817 This sequence is used for all types for which these operations are
3818 supported.
3819
3820 In addition, built-in versions of the 'fe' prefixed routines may
3821 need to be invoked for floating point (real, complex or vector) when
3822 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3823
3824 T1 newval;
3825 T1 old;
3826 T1 *addr
3827 T2 val
3828 fenv_t fenv
3829
3830 addr = &E1;
3831 val = (E2);
3832 __atomic_load (addr, &old, SEQ_CST);
3833 feholdexcept (&fenv);
3834loop:
3835 newval = old op val;
3836 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3837 SEQ_CST))
3838 goto done;
3839 feclearexcept (FE_ALL_EXCEPT);
3840 goto loop:
3841done:
3842 feupdateenv (&fenv);
3843
2fe0a208
MP
3844 The compiler will issue the __atomic_fetch_* built-in when possible,
3845 otherwise it will generate the generic form of the atomic operations.
3846 This requires temp(s) and has their address taken. The atomic processing
3847 is smart enough to figure out when the size of an object can utilize
3848 a lock-free version, and convert the built-in call to the appropriate
3849 lock-free routine. The optimizers will then dispose of any temps that
3850 are no longer required, and lock-free implementations are utilized as
3851 long as there is target support for the required size.
267bac10
JM
3852
3853 If the operator is NOP_EXPR, then this is a simple assignment, and
3854 an __atomic_store is issued to perform the assignment rather than
2fe0a208 3855 the above loop. */
267bac10
JM
3856
3857/* Build an atomic assignment at LOC, expanding into the proper
3858 sequence to store LHS MODIFYCODE= RHS. Return a value representing
2fe0a208 3859 the result of the operation, unless RETURN_OLD_P, in which case
267bac10
JM
3860 return the old value of LHS (this is only for postincrement and
3861 postdecrement). */
2fe0a208 3862
267bac10
JM
3863static tree
3864build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3865 tree rhs, bool return_old_p)
3866{
3867 tree fndecl, func_call;
3868 vec<tree, va_gc> *params;
3869 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3870 tree old, old_addr;
3871 tree compound_stmt;
3872 tree stmt, goto_stmt;
3873 tree loop_label, loop_decl, done_label, done_decl;
3874
3875 tree lhs_type = TREE_TYPE (lhs);
e51fbec3 3876 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
267bac10
JM
3877 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3878 tree rhs_type = TREE_TYPE (rhs);
3879
3880 gcc_assert (TYPE_ATOMIC (lhs_type));
3881
3882 if (return_old_p)
3883 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3884
3885 /* Allocate enough vector items for a compare_exchange. */
3886 vec_alloc (params, 6);
3887
3888 /* Create a compound statement to hold the sequence of statements
3889 with a loop. */
3890 compound_stmt = c_begin_compound_stmt (false);
3891
3892 /* Fold the RHS if it hasn't already been folded. */
3893 if (modifycode != NOP_EXPR)
3894 rhs = c_fully_fold (rhs, false, NULL);
3895
3896 /* Remove the qualifiers for the rest of the expressions and create
3897 the VAL temp variable to hold the RHS. */
3898 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3899 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5c4abbb8 3900 val = create_tmp_var_raw (nonatomic_rhs_type);
267bac10 3901 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3902 TREE_NO_WARNING (val) = 1;
5c4abbb8
MP
3903 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3904 NULL_TREE);
267bac10
JM
3905 SET_EXPR_LOCATION (rhs, loc);
3906 add_stmt (rhs);
3907
3908 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3909 an atomic_store. */
3910 if (modifycode == NOP_EXPR)
3911 {
3912 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
e51fbec3 3913 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
267bac10
JM
3914 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3915 params->quick_push (lhs_addr);
3916 params->quick_push (rhs);
3917 params->quick_push (seq_cst);
8edbfaa6 3918 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3919 add_stmt (func_call);
3920
3921 /* Finish the compound statement. */
3922 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3923
3924 /* VAL is the value which was stored, return a COMPOUND_STMT of
3925 the statement and that value. */
3926 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3927 }
3928
2fe0a208
MP
3929 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3930 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3931 isn't applicable for such builtins. ??? Do we want to handle enums? */
3932 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3933 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3934 {
3935 built_in_function fncode;
3936 switch (modifycode)
3937 {
3938 case PLUS_EXPR:
3939 case POINTER_PLUS_EXPR:
3940 fncode = (return_old_p
3941 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3942 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3943 break;
3944 case MINUS_EXPR:
3945 fncode = (return_old_p
3946 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3947 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3948 break;
3949 case BIT_AND_EXPR:
3950 fncode = (return_old_p
3951 ? BUILT_IN_ATOMIC_FETCH_AND_N
3952 : BUILT_IN_ATOMIC_AND_FETCH_N);
3953 break;
3954 case BIT_IOR_EXPR:
3955 fncode = (return_old_p
3956 ? BUILT_IN_ATOMIC_FETCH_OR_N
3957 : BUILT_IN_ATOMIC_OR_FETCH_N);
3958 break;
3959 case BIT_XOR_EXPR:
3960 fncode = (return_old_p
3961 ? BUILT_IN_ATOMIC_FETCH_XOR_N
3962 : BUILT_IN_ATOMIC_XOR_FETCH_N);
3963 break;
3964 default:
3965 goto cas_loop;
3966 }
3967
3968 /* We can only use "_1" through "_16" variants of the atomic fetch
3969 built-ins. */
3970 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
3971 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
3972 goto cas_loop;
3973
3974 /* If this is a pointer type, we need to multiply by the size of
3975 the pointer target type. */
3976 if (POINTER_TYPE_P (lhs_type))
3977 {
3978 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
3979 /* ??? This would introduce -Wdiscarded-qualifiers
3980 warning: __atomic_fetch_* expect volatile void *
3981 type as the first argument. (Assignments between
3982 atomic and non-atomic objects are OK.) */
3983 || TYPE_RESTRICT (lhs_type))
3984 goto cas_loop;
3985 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
3986 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
3987 convert (ptrdiff_type_node, rhs),
3988 convert (ptrdiff_type_node, sz));
3989 }
3990
3991 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
3992 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
3993 fndecl = builtin_decl_explicit (fncode);
3994 params->quick_push (lhs_addr);
3995 params->quick_push (rhs);
3996 params->quick_push (seq_cst);
3997 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3998
3999 newval = create_tmp_var_raw (nonatomic_lhs_type);
4000 TREE_ADDRESSABLE (newval) = 1;
4001 TREE_NO_WARNING (newval) = 1;
4002 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4003 NULL_TREE, NULL_TREE);
4004 SET_EXPR_LOCATION (rhs, loc);
4005 add_stmt (rhs);
4006
4007 /* Finish the compound statement. */
4008 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4009
4010 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4011 the statement and that value. */
4012 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4013 }
4014
4015cas_loop:
267bac10 4016 /* Create the variables and labels required for the op= form. */
5c4abbb8 4017 old = create_tmp_var_raw (nonatomic_lhs_type);
e51fbec3 4018 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
cc28fc7f
MP
4019 TREE_ADDRESSABLE (old) = 1;
4020 TREE_NO_WARNING (old) = 1;
267bac10 4021
5c4abbb8 4022 newval = create_tmp_var_raw (nonatomic_lhs_type);
e51fbec3 4023 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
267bac10 4024 TREE_ADDRESSABLE (newval) = 1;
2994fb91 4025 TREE_NO_WARNING (newval) = 1;
267bac10
JM
4026
4027 loop_decl = create_artificial_label (loc);
4028 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4029
4030 done_decl = create_artificial_label (loc);
4031 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4032
4033 /* __atomic_load (addr, &old, SEQ_CST). */
4034 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4035 params->quick_push (lhs_addr);
4036 params->quick_push (old_addr);
4037 params->quick_push (seq_cst);
8edbfaa6 4038 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5c4abbb8
MP
4039 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4040 NULL_TREE);
4041 add_stmt (old);
267bac10
JM
4042 params->truncate (0);
4043
4044 /* Create the expressions for floating-point environment
4045 manipulation, if required. */
4046 bool need_fenv = (flag_trapping_math
4047 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4048 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4049 if (need_fenv)
4050 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4051
4052 if (hold_call)
4053 add_stmt (hold_call);
4054
4055 /* loop: */
4056 add_stmt (loop_label);
4057
4058 /* newval = old + val; */
4059 rhs = build_binary_op (loc, modifycode, old, val, 1);
5c4abbb8 4060 rhs = c_fully_fold (rhs, false, NULL);
68fca595
MP
4061 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4062 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
4063 NULL_TREE, 0);
4064 if (rhs != error_mark_node)
4065 {
5c4abbb8
MP
4066 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4067 NULL_TREE);
267bac10
JM
4068 SET_EXPR_LOCATION (rhs, loc);
4069 add_stmt (rhs);
4070 }
4071
4072 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4073 goto done; */
4074 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4075 params->quick_push (lhs_addr);
4076 params->quick_push (old_addr);
4077 params->quick_push (newval_addr);
4078 params->quick_push (integer_zero_node);
4079 params->quick_push (seq_cst);
4080 params->quick_push (seq_cst);
8edbfaa6 4081 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
4082
4083 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4084 SET_EXPR_LOCATION (goto_stmt, loc);
4085
4086 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4087 SET_EXPR_LOCATION (stmt, loc);
4088 add_stmt (stmt);
5c4abbb8 4089
267bac10
JM
4090 if (clear_call)
4091 add_stmt (clear_call);
4092
4093 /* goto loop; */
4094 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4095 SET_EXPR_LOCATION (goto_stmt, loc);
4096 add_stmt (goto_stmt);
5c4abbb8 4097
267bac10
JM
4098 /* done: */
4099 add_stmt (done_label);
4100
4101 if (update_call)
4102 add_stmt (update_call);
4103
4104 /* Finish the compound statement. */
4105 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4106
4107 /* NEWVAL is the value that was successfully stored, return a
4108 COMPOUND_EXPR of the statement and the appropriate value. */
4109 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4110 return_old_p ? old : newval);
4111}
4112
3e4093b6
RS
4113/* Construct and perhaps optimize a tree representation
4114 for a unary operation. CODE, a tree_code, specifies the operation
4115 and XARG is the operand.
e51fbec3
MP
4116 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4117 promotions (such as from short to int).
4118 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4119 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4120 to pointers in C99.
c9f9eb5d
AH
4121
4122 LOCATION is the location of the operator. */
400fbf9f 4123
3e4093b6 4124tree
4c13ba17 4125build_unary_op (location_t location, enum tree_code code, tree xarg,
e51fbec3 4126 bool noconvert)
3e4093b6
RS
4127{
4128 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4129 tree arg = xarg;
4130 tree argtype = 0;
808d6eaa 4131 enum tree_code typecode;
3e4093b6 4132 tree val;
c9f9eb5d 4133 tree ret = error_mark_node;
8ce94e44 4134 tree eptype = NULL_TREE;
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 {
9a2300e9
MP
4200 tree e = arg;
4201
4202 /* Warn if the expression has boolean value. */
4203 while (TREE_CODE (e) == COMPOUND_EXPR)
4204 e = TREE_OPERAND (e, 1);
4205
4206 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4207 || truth_value_p (TREE_CODE (e)))
4208 && warning_at (location, OPT_Wbool_operation,
4209 "%<~%> on a boolean expression"))
4210 {
4211 gcc_rich_location richloc (location);
4212 richloc.add_fixit_insert_before (location, "!");
4213 inform_at_rich_loc (&richloc, "did you mean to use logical "
4214 "not?");
4215 }
3e4093b6
RS
4216 if (!noconvert)
4217 arg = default_conversion (arg);
03d5b1f5 4218 }
3e4093b6 4219 else if (typecode == COMPLEX_TYPE)
400fbf9f 4220 {
3e4093b6 4221 code = CONJ_EXPR;
c1771a20 4222 pedwarn (location, OPT_Wpedantic,
fcf73884 4223 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
4224 if (!noconvert)
4225 arg = default_conversion (arg);
4226 }
4227 else
4228 {
c9f9eb5d 4229 error_at (location, "wrong type argument to bit-complement");
3e4093b6 4230 return error_mark_node;
400fbf9f
JW
4231 }
4232 break;
4233
3e4093b6 4234 case ABS_EXPR:
11017cc7 4235 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 4236 {
c9f9eb5d 4237 error_at (location, "wrong type argument to abs");
3e4093b6 4238 return error_mark_node;
400fbf9f 4239 }
3e4093b6
RS
4240 else if (!noconvert)
4241 arg = default_conversion (arg);
400fbf9f
JW
4242 break;
4243
3e4093b6
RS
4244 case CONJ_EXPR:
4245 /* Conjugating a real value is a no-op, but allow it anyway. */
4246 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4247 || typecode == COMPLEX_TYPE))
400fbf9f 4248 {
c9f9eb5d 4249 error_at (location, "wrong type argument to conjugation");
3e4093b6 4250 return error_mark_node;
400fbf9f 4251 }
3e4093b6
RS
4252 else if (!noconvert)
4253 arg = default_conversion (arg);
400fbf9f
JW
4254 break;
4255
3e4093b6 4256 case TRUTH_NOT_EXPR:
ab22c1fa 4257 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 4258 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 4259 && typecode != COMPLEX_TYPE)
400fbf9f 4260 {
c9f9eb5d
AH
4261 error_at (location,
4262 "wrong type argument to unary exclamation mark");
3e4093b6 4263 return error_mark_node;
400fbf9f 4264 }
a27d595d
JM
4265 if (int_operands)
4266 {
4267 arg = c_objc_common_truthvalue_conversion (location, xarg);
4268 arg = remove_c_maybe_const_expr (arg);
4269 }
4270 else
4271 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 4272 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
4273 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4274 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4275 location = EXPR_LOCATION (ret);
c9f9eb5d 4276 goto return_build_unary_op;
3e4093b6 4277
3e4093b6 4278 case REALPART_EXPR:
3e4093b6 4279 case IMAGPART_EXPR:
fb52b50a
NF
4280 ret = build_real_imag_expr (location, code, arg);
4281 if (ret == error_mark_node)
4282 return error_mark_node;
8ce94e44
JM
4283 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4284 eptype = TREE_TYPE (eptype);
c9f9eb5d 4285 goto return_build_unary_op;
3e4093b6
RS
4286
4287 case PREINCREMENT_EXPR:
4288 case POSTINCREMENT_EXPR:
4289 case PREDECREMENT_EXPR:
4290 case POSTDECREMENT_EXPR:
3e4093b6 4291
928c19bb
JM
4292 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4293 {
4294 tree inner = build_unary_op (location, code,
4c13ba17
MP
4295 C_MAYBE_CONST_EXPR_EXPR (arg),
4296 noconvert);
928c19bb
JM
4297 if (inner == error_mark_node)
4298 return error_mark_node;
4299 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4300 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4301 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4302 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4303 goto return_build_unary_op;
4304 }
4305
925e8657
NP
4306 /* Complain about anything that is not a true lvalue. In
4307 Objective-C, skip this check for property_refs. */
f90e8e2e 4308 if (!objc_is_property_ref (arg)
7bd11157
TT
4309 && !lvalue_or_else (location,
4310 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
4311 || code == POSTINCREMENT_EXPR)
4312 ? lv_increment
4313 : lv_decrement)))
928c19bb
JM
4314 return error_mark_node;
4315
09639a83
ILT
4316 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4317 {
4318 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4319 warning_at (location, OPT_Wc___compat,
4320 "increment of enumeration value is invalid in C++");
4321 else
4322 warning_at (location, OPT_Wc___compat,
4323 "decrement of enumeration value is invalid in C++");
4324 }
4325
9a2300e9
MP
4326 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4327 {
4328 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4329 warning_at (location, OPT_Wbool_operation,
4330 "increment of a boolean expression");
4331 else
4332 warning_at (location, OPT_Wbool_operation,
4333 "decrement of a boolean expression");
4334 }
4335
928c19bb
JM
4336 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4337 arg = c_fully_fold (arg, false, NULL);
4338
267bac10
JM
4339 bool atomic_op;
4340 atomic_op = really_atomic_lvalue (arg);
4341
3e4093b6
RS
4342 /* Increment or decrement the real part of the value,
4343 and don't change the imaginary part. */
4344 if (typecode == COMPLEX_TYPE)
400fbf9f 4345 {
3e4093b6
RS
4346 tree real, imag;
4347
c1771a20 4348 pedwarn (location, OPT_Wpedantic,
509c9d60 4349 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 4350
267bac10
JM
4351 if (!atomic_op)
4352 {
4353 arg = stabilize_reference (arg);
e51fbec3
MP
4354 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4355 true);
4356 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4357 true);
4358 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
267bac10
JM
4359 if (real == error_mark_node || imag == error_mark_node)
4360 return error_mark_node;
4361 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4362 real, imag);
4363 goto return_build_unary_op;
4364 }
400fbf9f 4365 }
3e4093b6
RS
4366
4367 /* Report invalid types. */
4368
ab22c1fa 4369 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4370 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4371 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4372 {
3e4093b6 4373 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4374 error_at (location, "wrong type argument to increment");
c22cacf3 4375 else
c9f9eb5d 4376 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4377
4378 return error_mark_node;
400fbf9f 4379 }
400fbf9f 4380
3e4093b6
RS
4381 {
4382 tree inc;
400fbf9f 4383
3e4093b6
RS
4384 argtype = TREE_TYPE (arg);
4385
4386 /* Compute the increment. */
4387
4388 if (typecode == POINTER_TYPE)
4389 {
a0e24419 4390 /* If pointer target is an incomplete type,
3e4093b6 4391 we just cannot know how to do the arithmetic. */
b70cef5d 4392 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4393 {
4394 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4395 error_at (location,
a0e24419
MP
4396 "increment of pointer to an incomplete type %qT",
4397 TREE_TYPE (argtype));
3e4093b6 4398 else
c9f9eb5d 4399 error_at (location,
a0e24419
MP
4400 "decrement of pointer to an incomplete type %qT",
4401 TREE_TYPE (argtype));
3e4093b6 4402 }
b70cef5d
JJ
4403 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4404 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4405 {
3e4093b6 4406 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4407 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4408 "wrong type argument to increment");
3e4093b6 4409 else
44d90fe1 4410 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4411 "wrong type argument to decrement");
3e4093b6
RS
4412 }
4413
b70cef5d 4414 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4415 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4416 }
b70cef5d 4417 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4418 {
4419 /* For signed fract types, we invert ++ to -- or
4420 -- to ++, and change inc from 1 to -1, because
4421 it is not possible to represent 1 in signed fract constants.
4422 For unsigned fract types, the result always overflows and
4423 we get an undefined (original) or the maximum value. */
4424 if (code == PREINCREMENT_EXPR)
4425 code = PREDECREMENT_EXPR;
4426 else if (code == PREDECREMENT_EXPR)
4427 code = PREINCREMENT_EXPR;
4428 else if (code == POSTINCREMENT_EXPR)
4429 code = POSTDECREMENT_EXPR;
4430 else /* code == POSTDECREMENT_EXPR */
4431 code = POSTINCREMENT_EXPR;
4432
4433 inc = integer_minus_one_node;
4434 inc = convert (argtype, inc);
4435 }
3e4093b6 4436 else
5be014d5 4437 {
241b71bb
TV
4438 inc = VECTOR_TYPE_P (argtype)
4439 ? build_one_cst (argtype)
4440 : integer_one_node;
5be014d5
AP
4441 inc = convert (argtype, inc);
4442 }
3e4093b6 4443
925e8657
NP
4444 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4445 need to ask Objective-C to build the increment or decrement
4446 expression for it. */
4447 if (objc_is_property_ref (arg))
f90e8e2e 4448 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4449 arg, inc);
4450
3e4093b6 4451 /* Report a read-only lvalue. */
f37acdf9 4452 if (TYPE_READONLY (argtype))
953ff289 4453 {
c02065fc 4454 readonly_error (location, arg,
953ff289
DN
4455 ((code == PREINCREMENT_EXPR
4456 || code == POSTINCREMENT_EXPR)
4457 ? lv_increment : lv_decrement));
4458 return error_mark_node;
4459 }
f37acdf9
JM
4460 else if (TREE_READONLY (arg))
4461 readonly_warning (arg,
4462 ((code == PREINCREMENT_EXPR
4463 || code == POSTINCREMENT_EXPR)
4464 ? lv_increment : lv_decrement));
3e4093b6 4465
267bac10
JM
4466 /* If the argument is atomic, use the special code sequences for
4467 atomic compound assignment. */
4468 if (atomic_op)
4469 {
4470 arg = stabilize_reference (arg);
4471 ret = build_atomic_assign (location, arg,
4472 ((code == PREINCREMENT_EXPR
4473 || code == POSTINCREMENT_EXPR)
4474 ? PLUS_EXPR
4475 : MINUS_EXPR),
4476 (FRACT_MODE_P (TYPE_MODE (argtype))
4477 ? inc
4478 : integer_one_node),
4479 (code == POSTINCREMENT_EXPR
4480 || code == POSTDECREMENT_EXPR));
4481 goto return_build_unary_op;
4482 }
4483
3e4093b6
RS
4484 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4485 val = boolean_increment (code, arg);
4486 else
53fb4de3 4487 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4488 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4489 if (TREE_CODE (val) != code)
6de9cd9a 4490 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4491 ret = val;
4492 goto return_build_unary_op;
3e4093b6
RS
4493 }
4494
4495 case ADDR_EXPR:
4496 /* Note that this operation never does default_conversion. */
4497
2b4b7036
JM
4498 /* The operand of unary '&' must be an lvalue (which excludes
4499 expressions of type void), or, in C99, the result of a [] or
4500 unary '*' operator. */
4501 if (VOID_TYPE_P (TREE_TYPE (arg))
4502 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
22d03525 4503 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
2b4b7036
JM
4504 pedwarn (location, 0, "taking address of expression of type %<void%>");
4505
3e4093b6 4506 /* Let &* cancel out to simplify resulting code. */
22d03525 4507 if (INDIRECT_REF_P (arg))
400fbf9f 4508 {
3e4093b6
RS
4509 /* Don't let this be an lvalue. */
4510 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4511 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4512 ret = TREE_OPERAND (arg, 0);
4513 goto return_build_unary_op;
400fbf9f 4514 }
1eb8759b 4515
3e4093b6
RS
4516 /* Anything not already handled and not a true memory reference
4517 or a non-lvalue array is an error. */
4c13ba17 4518 if (typecode != FUNCTION_TYPE && !noconvert
ee45a32d 4519 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4520 return error_mark_node;
b6a10c9f 4521
928c19bb
JM
4522 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4523 folding later. */
4524 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4525 {
4526 tree inner = build_unary_op (location, code,
4c13ba17
MP
4527 C_MAYBE_CONST_EXPR_EXPR (arg),
4528 noconvert);
928c19bb
JM
4529 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4530 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4531 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4532 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4533 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4534 goto return_build_unary_op;
4535 }
4536
3e4093b6
RS
4537 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4538 argtype = TREE_TYPE (arg);
400fbf9f 4539
3e4093b6 4540 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4541 to which the address will point. This is only needed
f2c1da78 4542 for function types. */
6615c446 4543 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4544 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4545 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4546 {
4547 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4548 int quals = orig_quals;
4549
4550 if (TREE_READONLY (arg))
4551 quals |= TYPE_QUAL_CONST;
4552 if (TREE_THIS_VOLATILE (arg))
4553 quals |= TYPE_QUAL_VOLATILE;
4554
f2c1da78
JM
4555 argtype = c_build_qualified_type (argtype, quals);
4556 }
400fbf9f 4557
ee45a32d
EB
4558 switch (TREE_CODE (arg))
4559 {
4560 case COMPONENT_REF:
4561 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4562 {
949505a9
MP
4563 error_at (location, "cannot take address of bit-field %qD",
4564 TREE_OPERAND (arg, 1));
ee45a32d
EB
4565 return error_mark_node;
4566 }
4567
191816a3 4568 /* fall through */
ee45a32d
EB
4569
4570 case ARRAY_REF:
4571 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4572 {
4573 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4574 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4575 {
949505a9
MP
4576 error_at (location, "cannot take address of scalar with "
4577 "reverse storage order");
ee45a32d
EB
4578 return error_mark_node;
4579 }
4580
4581 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4582 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
949505a9
MP
4583 warning_at (location, OPT_Wscalar_storage_order,
4584 "address of array with reverse scalar storage "
4585 "order requested");
ee45a32d
EB
4586 }
4587
4588 default:
4589 break;
4590 }
4591
3e4093b6
RS
4592 if (!c_mark_addressable (arg))
4593 return error_mark_node;
400fbf9f 4594
abb54d14
JM
4595 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4596 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4597
5cc200fc 4598 argtype = build_pointer_type (argtype);
5e55f99d
RH
4599
4600 /* ??? Cope with user tricks that amount to offsetof. Delete this
4601 when we have proper support for integer constant expressions. */
4602 val = get_base_address (arg);
22d03525 4603 if (val && INDIRECT_REF_P (val)
3aa2ddb8
JJ
4604 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4605 {
cf9e9959 4606 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4607 goto return_build_unary_op;
3aa2ddb8 4608 }
5e55f99d 4609
5cc200fc 4610 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4611
c9f9eb5d
AH
4612 ret = val;
4613 goto return_build_unary_op;
400fbf9f 4614
3e4093b6 4615 default:
1344f9a3 4616 gcc_unreachable ();
3e4093b6 4617 }
400fbf9f 4618
3e4093b6
RS
4619 if (argtype == 0)
4620 argtype = TREE_TYPE (arg);
928c19bb
JM
4621 if (TREE_CODE (arg) == INTEGER_CST)
4622 ret = (require_constant_value
db3927fb
AH
4623 ? fold_build1_initializer_loc (location, code, argtype, arg)
4624 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4625 else
4626 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4627 return_build_unary_op:
4628 gcc_assert (ret != error_mark_node);
928c19bb
JM
4629 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4630 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4631 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4632 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4633 ret = note_integer_operands (ret);
8ce94e44
JM
4634 if (eptype)
4635 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4636 protected_set_expr_location (ret, location);
4637 return ret;
3e4093b6 4638}
400fbf9f 4639
3e4093b6
RS
4640/* Return nonzero if REF is an lvalue valid for this language.
4641 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4642 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4643
1e4ae551 4644bool
58f9752a 4645lvalue_p (const_tree ref)
3e4093b6 4646{
58f9752a 4647 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4648
3e4093b6
RS
4649 switch (code)
4650 {
4651 case REALPART_EXPR:
4652 case IMAGPART_EXPR:
4653 case COMPONENT_REF:
4654 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4655
928c19bb
JM
4656 case C_MAYBE_CONST_EXPR:
4657 return lvalue_p (TREE_OPERAND (ref, 1));
4658
3e4093b6
RS
4659 case COMPOUND_LITERAL_EXPR:
4660 case STRING_CST:
5b73d2ab 4661 return true;
400fbf9f 4662
3e4093b6
RS
4663 case INDIRECT_REF:
4664 case ARRAY_REF:
36536d79 4665 case ARRAY_NOTATION_REF:
3e4093b6
RS
4666 case VAR_DECL:
4667 case PARM_DECL:
4668 case RESULT_DECL:
4669 case ERROR_MARK:
4670 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4671 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4672
3e4093b6 4673 case BIND_EXPR:
3e4093b6 4674 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4675
3e4093b6 4676 default:
5b73d2ab 4677 return false;
3e4093b6
RS
4678 }
4679}
400fbf9f 4680\f
f37acdf9
JM
4681/* Give a warning for storing in something that is read-only in GCC
4682 terms but not const in ISO C terms. */
4683
4684static void
4685readonly_warning (tree arg, enum lvalue_use use)
4686{
4687 switch (use)
4688 {
4689 case lv_assign:
4690 warning (0, "assignment of read-only location %qE", arg);
4691 break;
4692 case lv_increment:
4693 warning (0, "increment of read-only location %qE", arg);
4694 break;
4695 case lv_decrement:
4696 warning (0, "decrement of read-only location %qE", arg);
4697 break;
4698 default:
4699 gcc_unreachable ();
4700 }
4701 return;
4702}
4703
37dc0d8d
JM
4704
4705/* Return nonzero if REF is an lvalue valid for this language;
4706 otherwise, print an error message and return zero. USE says
7bd11157
TT
4707 how the lvalue is being used and so selects the error message.
4708 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4709
4710static int
7bd11157 4711lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4712{
4713 int win = lvalue_p (ref);
4714
4715 if (!win)
7bd11157 4716 lvalue_error (loc, use);
37dc0d8d
JM
4717
4718 return win;
4719}
3e4093b6
RS
4720\f
4721/* Mark EXP saying that we need to be able to take the
4722 address of it; it should not be allocated in a register.
4723 Returns true if successful. */
54c93c30 4724
3e4093b6
RS
4725bool
4726c_mark_addressable (tree exp)
400fbf9f 4727{
3e4093b6 4728 tree x = exp;
95602da1 4729
3e4093b6
RS
4730 while (1)
4731 switch (TREE_CODE (x))
4732 {
4733 case COMPONENT_REF:
3e4093b6
RS
4734 case ADDR_EXPR:
4735 case ARRAY_REF:
4736 case REALPART_EXPR:
4737 case IMAGPART_EXPR:
4738 x = TREE_OPERAND (x, 0);
4739 break;
95602da1 4740
3e4093b6
RS
4741 case COMPOUND_LITERAL_EXPR:
4742 case CONSTRUCTOR:
4743 TREE_ADDRESSABLE (x) = 1;
4744 return true;
95602da1 4745
3e4093b6
RS
4746 case VAR_DECL:
4747 case CONST_DECL:
4748 case PARM_DECL:
4749 case RESULT_DECL:
5baeaac0 4750 if (C_DECL_REGISTER (x)
3e4093b6
RS
4751 && DECL_NONLOCAL (x))
4752 {
62f9079a 4753 if (TREE_PUBLIC (x) || is_global_var (x))
3e4093b6 4754 {
0039fa55
AN
4755 error
4756 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4757 return false;
4758 }
509c9d60 4759 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4760 }
5baeaac0 4761 else if (C_DECL_REGISTER (x))
3e4093b6 4762 {
62f9079a 4763 if (TREE_PUBLIC (x) || is_global_var (x))
0039fa55
AN
4764 error ("address of global register variable %qD requested", x);
4765 else
4766 error ("address of register variable %qD requested", x);
4767 return false;
3e4093b6 4768 }
400fbf9f 4769
191816a3 4770 /* FALLTHRU */
3e4093b6
RS
4771 case FUNCTION_DECL:
4772 TREE_ADDRESSABLE (x) = 1;
191816a3 4773 /* FALLTHRU */
3e4093b6
RS
4774 default:
4775 return true;
4776 }
4777}
4778\f
2d2e923f
MLI
4779/* Convert EXPR to TYPE, warning about conversion problems with
4780 constants. SEMANTIC_TYPE is the type this conversion would use
4781 without excess precision. If SEMANTIC_TYPE is NULL, this function
4782 is equivalent to convert_and_check. This function is a wrapper that
4783 handles conversions that may be different than
4784 the usual ones because of excess precision. */
4785
4786static tree
68fca595
MP
4787ep_convert_and_check (location_t loc, tree type, tree expr,
4788 tree semantic_type)
2d2e923f
MLI
4789{
4790 if (TREE_TYPE (expr) == type)
4791 return expr;
4792
4793 if (!semantic_type)
68fca595 4794 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4795
4796 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4797 && TREE_TYPE (expr) != semantic_type)
4798 {
4799 /* For integers, we need to check the real conversion, not
4800 the conversion to the excess precision type. */
68fca595 4801 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4802 }
4803 /* Result type is the excess precision type, which should be
4804 large enough, so do not check. */
4805 return convert (type, expr);
4806}
4807
928c19bb
JM
4808/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4809 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4810 if folded to an integer constant then the unselected half may
4811 contain arbitrary operations not normally permitted in constant
c2255bc4 4812 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4813
4814tree
744aa42f 4815build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4816 tree op1, tree op1_original_type, tree op2,
4817 tree op2_original_type)
400fbf9f 4818{
3e4093b6
RS
4819 tree type1;
4820 tree type2;
4821 enum tree_code code1;
4822 enum tree_code code2;
4823 tree result_type = NULL;
2d2e923f 4824 tree semantic_result_type = NULL;
3e4093b6 4825 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4826 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4827 bool ifexp_int_operands;
928c19bb 4828 tree ret;
400fbf9f 4829
4d84fe7c
JM
4830 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4831 if (op1_int_operands)
4832 op1 = remove_c_maybe_const_expr (op1);
4833 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4834 if (op2_int_operands)
4835 op2 = remove_c_maybe_const_expr (op2);
4836 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4837 if (ifexp_int_operands)
4838 ifexp = remove_c_maybe_const_expr (ifexp);
4839
3e4093b6
RS
4840 /* Promote both alternatives. */
4841
4842 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4843 op1 = default_conversion (op1);
4844 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4845 op2 = default_conversion (op2);
4846
4847 if (TREE_CODE (ifexp) == ERROR_MARK
4848 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4849 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4850 return error_mark_node;
400fbf9f 4851
3e4093b6
RS
4852 type1 = TREE_TYPE (op1);
4853 code1 = TREE_CODE (type1);
4854 type2 = TREE_TYPE (op2);
4855 code2 = TREE_CODE (type2);
4856
1807ffc1
MS
4857 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4858 return error_mark_node;
4859
4860 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4861 return error_mark_node;
4862
b1adf557
JM
4863 /* C90 does not permit non-lvalue arrays in conditional expressions.
4864 In C99 they will be pointers by now. */
4865 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4866 {
744aa42f 4867 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4868 return error_mark_node;
4869 }
4870
8ce94e44
JM
4871 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4872 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4873 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4874 || code1 == COMPLEX_TYPE)
4875 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4876 || code2 == COMPLEX_TYPE))
4877 {
2d2e923f 4878 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4879 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4880 {
4881 op1 = TREE_OPERAND (op1, 0);
4882 type1 = TREE_TYPE (op1);
4883 gcc_assert (TREE_CODE (type1) == code1);
4884 }
4885 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4886 {
4887 op2 = TREE_OPERAND (op2, 0);
4888 type2 = TREE_TYPE (op2);
4889 gcc_assert (TREE_CODE (type2) == code2);
4890 }
4891 }
4892
d130ae11
ILT
4893 if (warn_cxx_compat)
4894 {
4895 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4896 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4897
4898 if (TREE_CODE (t1) == ENUMERAL_TYPE
4899 && TREE_CODE (t2) == ENUMERAL_TYPE
4900 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4901 warning_at (colon_loc, OPT_Wc___compat,
4902 ("different enum types in conditional is "
4903 "invalid in C++: %qT vs %qT"),
4904 t1, t2);
4905 }
4906
3e4093b6
RS
4907 /* Quickly detect the usual case where op1 and op2 have the same type
4908 after promotion. */
4909 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4910 {
3e4093b6
RS
4911 if (type1 == type2)
4912 result_type = type1;
4913 else
4914 result_type = TYPE_MAIN_VARIANT (type1);
4915 }
4916 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4917 || code1 == COMPLEX_TYPE)
4918 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4919 || code2 == COMPLEX_TYPE))
3e4093b6 4920 {
ccf7f880 4921 result_type = c_common_type (type1, type2);
5a578671
JM
4922 if (result_type == error_mark_node)
4923 return error_mark_node;
0a0b3574
MM
4924 do_warn_double_promotion (result_type, type1, type2,
4925 "implicit conversion from %qT to %qT to "
4926 "match other result of conditional",
4927 colon_loc);
400fbf9f 4928
3e4093b6
RS
4929 /* If -Wsign-compare, warn here if type1 and type2 have
4930 different signedness. We'll promote the signed to unsigned
4931 and later code won't know it used to be different.
4932 Do this check on the original types, so that explicit casts
4933 will be considered, but default promotions won't. */
7d882b83 4934 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4935 {
8df83eae
RK
4936 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4937 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4938
3e4093b6
RS
4939 if (unsigned_op1 ^ unsigned_op2)
4940 {
6ac01510
ILT
4941 bool ovf;
4942
3e4093b6
RS
4943 /* Do not warn if the result type is signed, since the
4944 signed type will only be chosen if it can represent
4945 all the values of the unsigned type. */
3f75a254 4946 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4947 /* OK */;
3e4093b6 4948 else
928c19bb
JM
4949 {
4950 bool op1_maybe_const = true;
4951 bool op2_maybe_const = true;
4952
4953 /* Do not warn if the signed quantity is an
4954 unsuffixed integer literal (or some static
4955 constant expression involving such literals) and
4956 it is non-negative. This warning requires the
4957 operands to be folded for best results, so do
4958 that folding in this case even without
4959 warn_sign_compare to avoid warning options
4960 possibly affecting code generation. */
f5178456
RS
4961 c_inhibit_evaluation_warnings
4962 += (ifexp == truthvalue_false_node);
928c19bb
JM
4963 op1 = c_fully_fold (op1, require_constant_value,
4964 &op1_maybe_const);
f5178456
RS
4965 c_inhibit_evaluation_warnings
4966 -= (ifexp == truthvalue_false_node);
4967
4968 c_inhibit_evaluation_warnings
4969 += (ifexp == truthvalue_true_node);
928c19bb
JM
4970 op2 = c_fully_fold (op2, require_constant_value,
4971 &op2_maybe_const);
f5178456
RS
4972 c_inhibit_evaluation_warnings
4973 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4974
4975 if (warn_sign_compare)
4976 {
4977 if ((unsigned_op2
4978 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4979 || (unsigned_op1
4980 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4981 /* OK */;
4982 else
744aa42f
ILT
4983 warning_at (colon_loc, OPT_Wsign_compare,
4984 ("signed and unsigned type in "
4985 "conditional expression"));
928c19bb
JM
4986 }
4987 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4988 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4989 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4990 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4991 }
3e4093b6
RS
4992 }
4993 }
4994 }
4995 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4996 {
fcf73884 4997 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4998 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4999 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
5000 result_type = void_type_node;
5001 }
5002 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5003 {
36c5e70a
BE
5004 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5005 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5006 addr_space_t as_common;
5007
744aa42f 5008 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 5009 result_type = common_pointer_type (type1, type2);
6aa3c60d 5010 else if (null_pointer_constant_p (orig_op1))
36c5e70a 5011 result_type = type2;
6aa3c60d 5012 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
5013 result_type = type1;
5014 else if (!addr_space_superset (as1, as2, &as_common))
5015 {
5016 error_at (colon_loc, "pointers to disjoint address spaces "
5017 "used in conditional expression");
5018 return error_mark_node;
5019 }
267bac10
JM
5020 else if (VOID_TYPE_P (TREE_TYPE (type1))
5021 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 5022 {
768952be
MU
5023 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5024 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5025 & ~TYPE_QUALS (TREE_TYPE (type1))))
5026 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5027 "pointer to array loses qualifier "
5028 "in conditional expression");
5029
fcf73884 5030 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 5031 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 5032 "ISO C forbids conditional expr between "
bda67431 5033 "%<void *%> and function pointer");
3e4093b6
RS
5034 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5035 TREE_TYPE (type2)));
34a80643 5036 }
267bac10
JM
5037 else if (VOID_TYPE_P (TREE_TYPE (type2))
5038 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 5039 {
768952be
MU
5040 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5041 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5042 & ~TYPE_QUALS (TREE_TYPE (type2))))
5043 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5044 "pointer to array loses qualifier "
5045 "in conditional expression");
5046
fcf73884 5047 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 5048 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 5049 "ISO C forbids conditional expr between "
bda67431 5050 "%<void *%> and function pointer");
3e4093b6
RS
5051 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5052 TREE_TYPE (type1)));
1c2a9b35 5053 }
b581b85b
NP
5054 /* Objective-C pointer comparisons are a bit more lenient. */
5055 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5056 result_type = objc_common_type (type1, type2);
34a80643 5057 else
ab87f8c8 5058 {
36c5e70a
BE
5059 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5060
b581b85b
NP
5061 pedwarn (colon_loc, 0,
5062 "pointer type mismatch in conditional expression");
36c5e70a
BE
5063 result_type = build_pointer_type
5064 (build_qualified_type (void_type_node, qual));
ab87f8c8 5065 }
3e4093b6
RS
5066 }
5067 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5068 {
6aa3c60d 5069 if (!null_pointer_constant_p (orig_op2))
744aa42f 5070 pedwarn (colon_loc, 0,
509c9d60 5071 "pointer/integer type mismatch in conditional expression");
3e4093b6 5072 else
ab87f8c8 5073 {
3e4093b6 5074 op2 = null_pointer_node;
ab87f8c8 5075 }
3e4093b6
RS
5076 result_type = type1;
5077 }
5078 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5079 {
6aa3c60d 5080 if (!null_pointer_constant_p (orig_op1))
744aa42f 5081 pedwarn (colon_loc, 0,
509c9d60 5082 "pointer/integer type mismatch in conditional expression");
3e4093b6 5083 else
ab87f8c8 5084 {
3e4093b6 5085 op1 = null_pointer_node;
ab87f8c8 5086 }
3e4093b6
RS
5087 result_type = type2;
5088 }
1c2a9b35 5089
3e4093b6
RS
5090 if (!result_type)
5091 {
5092 if (flag_cond_mismatch)
5093 result_type = void_type_node;
5094 else
400fbf9f 5095 {
c2255bc4 5096 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 5097 return error_mark_node;
400fbf9f 5098 }
3e4093b6 5099 }
400fbf9f 5100
3e4093b6
RS
5101 /* Merge const and volatile flags of the incoming types. */
5102 result_type
5103 = build_type_variant (result_type,
afbd0665
AS
5104 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5105 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 5106
68fca595
MP
5107 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5108 semantic_result_type);
5109 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5110 semantic_result_type);
b6a10c9f 5111
928c19bb
JM
5112 if (ifexp_bcp && ifexp == truthvalue_true_node)
5113 {
5114 op2_int_operands = true;
5115 op1 = c_fully_fold (op1, require_constant_value, NULL);
5116 }
5117 if (ifexp_bcp && ifexp == truthvalue_false_node)
5118 {
5119 op1_int_operands = true;
5120 op2 = c_fully_fold (op2, require_constant_value, NULL);
5121 }
4d84fe7c 5122 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
5123 && op1_int_operands
5124 && op2_int_operands);
5125 if (int_operands)
5126 {
5127 int_const = ((ifexp == truthvalue_true_node
5128 && TREE_CODE (orig_op1) == INTEGER_CST
5129 && !TREE_OVERFLOW (orig_op1))
5130 || (ifexp == truthvalue_false_node
5131 && TREE_CODE (orig_op2) == INTEGER_CST
5132 && !TREE_OVERFLOW (orig_op2)));
5133 }
9f47c7e5
IE
5134
5135 /* Need to convert condition operand into a vector mask. */
5136 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5137 {
5138 tree vectype = TREE_TYPE (ifexp);
5139 tree elem_type = TREE_TYPE (vectype);
5140 tree zero = build_int_cst (elem_type, 0);
5141 tree zero_vec = build_vector_from_val (vectype, zero);
5142 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5143 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5144 }
5145
928c19bb 5146 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 5147 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
5148 else
5149 {
01c7ccbb
JM
5150 if (int_operands)
5151 {
f34f1c87
MP
5152 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5153 nested inside of the expression. */
5154 op1 = c_fully_fold (op1, false, NULL);
5155 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 5156 }
928c19bb
JM
5157 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5158 if (int_operands)
5159 ret = note_integer_operands (ret);
5160 }
2d2e923f
MLI
5161 if (semantic_result_type)
5162 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 5163
c2255bc4 5164 protected_set_expr_location (ret, colon_loc);
928c19bb 5165 return ret;
3e4093b6
RS
5166}
5167\f
487a92fe 5168/* Return a compound expression that performs two expressions and
c2255bc4
AH
5169 returns the value of the second of them.
5170
5171 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 5172
3e4093b6 5173tree
c2255bc4 5174build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 5175{
4d84fe7c 5176 bool expr1_int_operands, expr2_int_operands;
8ce94e44 5177 tree eptype = NULL_TREE;
928c19bb
JM
5178 tree ret;
5179
b72271b9 5180 if (flag_cilkplus
939b37da
BI
5181 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5182 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5183 {
5184 error_at (loc,
5185 "spawned function call cannot be part of a comma expression");
5186 return error_mark_node;
5187 }
4d84fe7c
JM
5188 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5189 if (expr1_int_operands)
5190 expr1 = remove_c_maybe_const_expr (expr1);
5191 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5192 if (expr2_int_operands)
5193 expr2 = remove_c_maybe_const_expr (expr2);
5194
8ce94e44
JM
5195 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5196 expr1 = TREE_OPERAND (expr1, 0);
5197 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5198 {
5199 eptype = TREE_TYPE (expr2);
5200 expr2 = TREE_OPERAND (expr2, 0);
5201 }
5202
3f75a254 5203 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
5204 {
5205 /* The left-hand operand of a comma expression is like an expression
c5409249 5206 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 5207 any side-effects, unless it was explicitly cast to (void). */
e14a6540 5208 if (warn_unused_value)
47aecf47 5209 {
e14a6540 5210 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 5211 && CONVERT_EXPR_P (expr1))
47aecf47 5212 ; /* (void) a, b */
e14a6540
JM
5213 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5214 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 5215 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
5216 ; /* (void) a, (void) b, c */
5217 else
b8698a0f 5218 warning_at (loc, OPT_Wunused_value,
c2255bc4 5219 "left-hand operand of comma expression has no effect");
47aecf47 5220 }
3e4093b6 5221 }
789eadcd
MP
5222 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5223 && warn_unused_value)
5224 {
5225 tree r = expr1;
5226 location_t cloc = loc;
5227 while (TREE_CODE (r) == COMPOUND_EXPR)
5228 {
5229 if (EXPR_HAS_LOCATION (r))
5230 cloc = EXPR_LOCATION (r);
5231 r = TREE_OPERAND (r, 1);
5232 }
5233 if (!TREE_SIDE_EFFECTS (r)
5234 && !VOID_TYPE_P (TREE_TYPE (r))
5235 && !CONVERT_EXPR_P (r))
5236 warning_at (cloc, OPT_Wunused_value,
5237 "right-hand operand of comma expression has no effect");
5238 }
400fbf9f 5239
3e4093b6
RS
5240 /* With -Wunused, we should also warn if the left-hand operand does have
5241 side-effects, but computes a value which is not used. For example, in
5242 `foo() + bar(), baz()' the result of the `+' operator is not used,
5243 so we should issue a warning. */
5244 else if (warn_unused_value)
c2255bc4 5245 warn_if_unused_value (expr1, loc);
400fbf9f 5246
e63d6886
AP
5247 if (expr2 == error_mark_node)
5248 return error_mark_node;
5249
928c19bb
JM
5250 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5251
5252 if (flag_isoc99
4d84fe7c
JM
5253 && expr1_int_operands
5254 && expr2_int_operands)
928c19bb
JM
5255 ret = note_integer_operands (ret);
5256
8ce94e44
JM
5257 if (eptype)
5258 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5259
c2255bc4 5260 protected_set_expr_location (ret, loc);
928c19bb 5261 return ret;
3e4093b6 5262}
400fbf9f 5263
67165eb3
ILT
5264/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5265 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
5266 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5267 of the cast. -Wcast-qual appeared on the command line. Named
5268 address space qualifiers are not handled here, because they result
5269 in different warnings. */
67165eb3
ILT
5270
5271static void
2ee3cb35 5272handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
5273{
5274 tree in_type = type;
5275 tree in_otype = otype;
5276 int added = 0;
5277 int discarded = 0;
5278 bool is_const;
5279
5280 /* Check that the qualifiers on IN_TYPE are a superset of the
5281 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5282 nodes is uninteresting and we stop as soon as we hit a
5283 non-POINTER_TYPE node on either type. */
5284 do
5285 {
5286 in_otype = TREE_TYPE (in_otype);
5287 in_type = TREE_TYPE (in_type);
5288
5289 /* GNU C allows cv-qualified function types. 'const' means the
5290 function is very pure, 'volatile' means it can't return. We
5291 need to warn when such qualifiers are added, not when they're
5292 taken away. */
5293 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5294 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
5295 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5296 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 5297 else
36c5e70a
BE
5298 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5299 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
5300 }
5301 while (TREE_CODE (in_type) == POINTER_TYPE
5302 && TREE_CODE (in_otype) == POINTER_TYPE);
5303
5304 if (added)
2ee3cb35
MLI
5305 warning_at (loc, OPT_Wcast_qual,
5306 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
5307
5308 if (discarded)
5309 /* There are qualifiers present in IN_OTYPE that are not present
5310 in IN_TYPE. */
2ee3cb35 5311 warning_at (loc, OPT_Wcast_qual,
7485aeea 5312 "cast discards %qv qualifier from pointer target type",
2ee3cb35 5313 discarded);
67165eb3
ILT
5314
5315 if (added || discarded)
5316 return;
5317
5318 /* A cast from **T to const **T is unsafe, because it can cause a
5319 const value to be changed with no additional warning. We only
5320 issue this warning if T is the same on both sides, and we only
5321 issue the warning if there are the same number of pointers on
5322 both sides, as otherwise the cast is clearly unsafe anyhow. A
5323 cast is unsafe when a qualifier is added at one level and const
5324 is not present at all outer levels.
5325
5326 To issue this warning, we check at each level whether the cast
5327 adds new qualifiers not already seen. We don't need to special
5328 case function types, as they won't have the same
5329 TYPE_MAIN_VARIANT. */
5330
5331 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5332 return;
5333 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5334 return;
5335
5336 in_type = type;
5337 in_otype = otype;
5338 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5339 do
5340 {
5341 in_type = TREE_TYPE (in_type);
5342 in_otype = TREE_TYPE (in_otype);
5343 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5344 && !is_const)
5345 {
2ee3cb35
MLI
5346 warning_at (loc, OPT_Wcast_qual,
5347 "to be safe all intermediate pointers in cast from "
5348 "%qT to %qT must be %<const%> qualified",
5349 otype, type);
67165eb3
ILT
5350 break;
5351 }
5352 if (is_const)
5353 is_const = TYPE_READONLY (in_type);
5354 }
5355 while (TREE_CODE (in_type) == POINTER_TYPE);
5356}
5357
b8698a0f 5358/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 5359 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 5360
3e4093b6 5361tree
c2255bc4 5362build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 5363{
8ce94e44
JM
5364 tree value;
5365
5366 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5367 expr = TREE_OPERAND (expr, 0);
5368
5369 value = expr;
400fbf9f 5370
3e4093b6
RS
5371 if (type == error_mark_node || expr == error_mark_node)
5372 return error_mark_node;
400fbf9f 5373
3e4093b6
RS
5374 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5375 only in <protocol> qualifications. But when constructing cast expressions,
5376 the protocols do matter and must be kept around. */
700686fa
ZL
5377 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5378 return build1 (NOP_EXPR, type, expr);
5379
5380 type = TYPE_MAIN_VARIANT (type);
400fbf9f 5381
3e4093b6
RS
5382 if (TREE_CODE (type) == ARRAY_TYPE)
5383 {
c2255bc4 5384 error_at (loc, "cast specifies array type");
3e4093b6
RS
5385 return error_mark_node;
5386 }
400fbf9f 5387
3e4093b6
RS
5388 if (TREE_CODE (type) == FUNCTION_TYPE)
5389 {
c2255bc4 5390 error_at (loc, "cast specifies function type");
3e4093b6
RS
5391 return error_mark_node;
5392 }
400fbf9f 5393
808d6eaa
JM
5394 if (!VOID_TYPE_P (type))
5395 {
4f2e1536 5396 value = require_complete_type (loc, value);
808d6eaa
JM
5397 if (value == error_mark_node)
5398 return error_mark_node;
5399 }
5400
3e4093b6
RS
5401 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5402 {
3e636daf 5403 if (RECORD_OR_UNION_TYPE_P (type))
c1771a20 5404 pedwarn (loc, OPT_Wpedantic,
fcf73884 5405 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5406
5407 /* Convert to remove any qualifiers from VALUE's type. */
5408 value = convert (type, value);
3e4093b6
RS
5409 }
5410 else if (TREE_CODE (type) == UNION_TYPE)
5411 {
5412 tree field;
400fbf9f 5413
910ad8de 5414 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5415 if (TREE_TYPE (field) != error_mark_node
5416 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5417 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5418 break;
5419
5420 if (field)
400fbf9f 5421 {
3e4093b6 5422 tree t;
e616f54d 5423 bool maybe_const = true;
3e4093b6 5424
c1771a20 5425 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5426 t = c_fully_fold (value, false, &maybe_const);
5427 t = build_constructor_single (type, field, t);
5428 if (!maybe_const)
5429 t = c_wrap_maybe_const (t, true);
5430 t = digest_init (loc, type, t,
bbbbb16a 5431 NULL_TREE, false, true, 0);
3e4093b6
RS
5432 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5433 return t;
400fbf9f 5434 }
c2255bc4 5435 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5436 return error_mark_node;
5437 }
5438 else
5439 {
5440 tree otype, ovalue;
400fbf9f 5441
3e4093b6 5442 if (type == void_type_node)
c2255bc4
AH
5443 {
5444 tree t = build1 (CONVERT_EXPR, type, value);
5445 SET_EXPR_LOCATION (t, loc);
5446 return t;
5447 }
400fbf9f 5448
3e4093b6 5449 otype = TREE_TYPE (value);
400fbf9f 5450
3e4093b6 5451 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5452 if (warn_cast_qual
5453 && TREE_CODE (type) == POINTER_TYPE
5454 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5455 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5456
36c5e70a
BE
5457 /* Warn about conversions between pointers to disjoint
5458 address spaces. */
5459 if (TREE_CODE (type) == POINTER_TYPE
5460 && TREE_CODE (otype) == POINTER_TYPE
5461 && !null_pointer_constant_p (value))
5462 {
5463 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5464 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5465 addr_space_t as_common;
5466
5467 if (!addr_space_superset (as_to, as_from, &as_common))
5468 {
5469 if (ADDR_SPACE_GENERIC_P (as_from))
5470 warning_at (loc, 0, "cast to %s address space pointer "
5471 "from disjoint generic address space pointer",
5472 c_addr_space_name (as_to));
5473
5474 else if (ADDR_SPACE_GENERIC_P (as_to))
5475 warning_at (loc, 0, "cast to generic address space pointer "
5476 "from disjoint %s address space pointer",
5477 c_addr_space_name (as_from));
5478
5479 else
5480 warning_at (loc, 0, "cast to %s address space pointer "
5481 "from disjoint %s address space pointer",
5482 c_addr_space_name (as_to),
5483 c_addr_space_name (as_from));
5484 }
5485 }
5486
3e4093b6 5487 /* Warn about possible alignment problems. */
3176a0c2 5488 if (STRICT_ALIGNMENT
3e4093b6
RS
5489 && TREE_CODE (type) == POINTER_TYPE
5490 && TREE_CODE (otype) == POINTER_TYPE
5491 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5492 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5493 /* Don't warn about opaque types, where the actual alignment
5494 restriction is unknown. */
3e636daf 5495 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
3e4093b6
RS
5496 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5497 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5498 warning_at (loc, OPT_Wcast_align,
5499 "cast increases required alignment of target type");
e9a25f70 5500
3176a0c2 5501 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5502 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5503 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5504 /* Unlike conversion of integers to pointers, where the
5505 warning is disabled for converting constants because
5506 of cases such as SIG_*, warn about converting constant
5507 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5508 sign extension, and a warning is appropriate. */
c2255bc4
AH
5509 warning_at (loc, OPT_Wpointer_to_int_cast,
5510 "cast from pointer to integer of different size");
400fbf9f 5511
3176a0c2 5512 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5513 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5514 warning_at (loc, OPT_Wbad_function_cast,
5515 "cast from function call of type %qT "
5516 "to non-matching type %qT", otype, type);
400fbf9f 5517
3176a0c2 5518 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5519 && TREE_CODE (otype) == INTEGER_TYPE
5520 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5521 /* Don't warn about converting any constant. */
5522 && !TREE_CONSTANT (value))
c2255bc4
AH
5523 warning_at (loc,
5524 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5525 "of different size");
400fbf9f 5526
79bedddc
SR
5527 if (warn_strict_aliasing <= 2)
5528 strict_aliasing_warning (otype, type, expr);
400fbf9f 5529
3897f229
JM
5530 /* If pedantic, warn for conversions between function and object
5531 pointer types, except for converting a null pointer constant
5532 to function pointer type. */
5533 if (pedantic
5534 && TREE_CODE (type) == POINTER_TYPE
5535 && TREE_CODE (otype) == POINTER_TYPE
5536 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5537 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5538 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5539 "conversion of function pointer to object pointer type");
3897f229
JM
5540
5541 if (pedantic
5542 && TREE_CODE (type) == POINTER_TYPE
5543 && TREE_CODE (otype) == POINTER_TYPE
5544 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5545 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5546 && !null_pointer_constant_p (value))
c1771a20 5547 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5548 "conversion of object pointer to function pointer type");
3897f229 5549
3e4093b6 5550 ovalue = value;
3e4093b6 5551 value = convert (type, value);
400fbf9f 5552
3e4093b6 5553 /* Ignore any integer overflow caused by the cast. */
928c19bb 5554 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5555 {
8bcd6380 5556 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5557 {
8bcd6380
RS
5558 if (!TREE_OVERFLOW (value))
5559 {
5560 /* Avoid clobbering a shared constant. */
5561 value = copy_node (value);
5562 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5563 }
6414bad6 5564 }
8bcd6380 5565 else if (TREE_OVERFLOW (value))
d8e1f97b 5566 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5567 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5568 }
5569 }
400fbf9f 5570
53cd18ec 5571 /* Don't let a cast be an lvalue. */
9482b620 5572 if (lvalue_p (value))
db3927fb 5573 value = non_lvalue_loc (loc, value);
e9a25f70 5574
928c19bb
JM
5575 /* Don't allow the results of casting to floating-point or complex
5576 types be confused with actual constants, or casts involving
5577 integer and pointer types other than direct integer-to-integer
5578 and integer-to-pointer be confused with integer constant
5579 expressions and null pointer constants. */
5580 if (TREE_CODE (value) == REAL_CST
5581 || TREE_CODE (value) == COMPLEX_CST
5582 || (TREE_CODE (value) == INTEGER_CST
5583 && !((TREE_CODE (expr) == INTEGER_CST
5584 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5585 || TREE_CODE (expr) == REAL_CST
5586 || TREE_CODE (expr) == COMPLEX_CST)))
5587 value = build1 (NOP_EXPR, type, value);
5588
21ba0cea 5589 protected_set_expr_location (value, loc);
3e4093b6 5590 return value;
400fbf9f
JW
5591}
5592
c2255bc4
AH
5593/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5594 location of the open paren of the cast, or the position of the cast
5595 expr. */
3e4093b6 5596tree
c2255bc4 5597c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5598{
f8893e47 5599 tree type;
928c19bb
JM
5600 tree type_expr = NULL_TREE;
5601 bool type_expr_const = true;
5602 tree ret;
3e4093b6 5603 int saved_wsp = warn_strict_prototypes;
c5c76735 5604
3e4093b6
RS
5605 /* This avoids warnings about unprototyped casts on
5606 integers. E.g. "#define SIG_DFL (void(*)())0". */
5607 if (TREE_CODE (expr) == INTEGER_CST)
5608 warn_strict_prototypes = 0;
928c19bb 5609 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5610 warn_strict_prototypes = saved_wsp;
c5c76735 5611
1807ffc1
MS
5612 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5613 && reject_gcc_builtin (expr))
5614 return error_mark_node;
5615
c2255bc4 5616 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5617 if (type_expr)
5618 {
9f33203d
JM
5619 bool inner_expr_const = true;
5620 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5621 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5622 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5623 && inner_expr_const);
c2255bc4 5624 SET_EXPR_LOCATION (ret, loc);
928c19bb 5625 }
24b97832 5626
21ba0cea
MP
5627 if (!EXPR_HAS_LOCATION (ret))
5628 protected_set_expr_location (ret, loc);
24b97832 5629
9e5b2115
PB
5630 /* C++ does not permits types to be defined in a cast, but it
5631 allows references to incomplete types. */
5632 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5633 warning_at (loc, OPT_Wc___compat,
5634 "defining a type in a cast is invalid in C++");
5635
928c19bb 5636 return ret;
400fbf9f 5637}
3e4093b6
RS
5638\f
5639/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5640 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5641 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5642 MODIFYCODE is the code for a binary operator that we use
5643 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5644 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5645 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5646 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5647
c2255bc4
AH
5648 LOCATION is the location of the MODIFYCODE operator.
5649 RHS_LOC is the location of the RHS. */
2f6e4e97 5650
3e4093b6 5651tree
32e8bb8e 5652build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5653 enum tree_code modifycode,
c2255bc4 5654 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5655{
3e4093b6
RS
5656 tree result;
5657 tree newrhs;
241f845a 5658 tree rhseval = NULL_TREE;
8ce94e44 5659 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5660 tree lhstype = TREE_TYPE (lhs);
5661 tree olhstype = lhstype;
928c19bb 5662 bool npc;
267bac10 5663 bool is_atomic_op;
e9a25f70 5664
3e4093b6 5665 /* Types that aren't fully specified cannot be used in assignments. */
4f2e1536 5666 lhs = require_complete_type (location, lhs);
e9a25f70 5667
3e4093b6
RS
5668 /* Avoid duplicate error messages from operands that had errors. */
5669 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5670 return error_mark_node;
400fbf9f 5671
4c2ecab0
JM
5672 /* Ensure an error for assigning a non-lvalue array to an array in
5673 C90. */
5674 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5675 {
5676 error_at (location, "assignment to expression with array type");
5677 return error_mark_node;
5678 }
5679
46a88c12 5680 /* For ObjC properties, defer this check. */
7bd11157 5681 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5682 return error_mark_node;
5683
267bac10
JM
5684 is_atomic_op = really_atomic_lvalue (lhs);
5685
8ce94e44
JM
5686 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5687 {
5688 rhs_semantic_type = TREE_TYPE (rhs);
5689 rhs = TREE_OPERAND (rhs, 0);
5690 }
5691
3e4093b6 5692 newrhs = rhs;
400fbf9f 5693
928c19bb
JM
5694 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5695 {
5696 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5697 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5698 rhs_origtype);
928c19bb
JM
5699 if (inner == error_mark_node)
5700 return error_mark_node;
5701 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5702 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5703 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5704 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5705 protected_set_expr_location (result, location);
5706 return result;
5707 }
5708
3e4093b6
RS
5709 /* If a binary op has been requested, combine the old LHS value with the RHS
5710 producing the value we should actually store into the LHS. */
5711
5712 if (modifycode != NOP_EXPR)
400fbf9f 5713 {
928c19bb 5714 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5715 lhs = stabilize_reference (lhs);
bbbbb16a 5716
267bac10
JM
5717 /* Construct the RHS for any non-atomic compound assignemnt. */
5718 if (!is_atomic_op)
5719 {
241f845a
JJ
5720 /* If in LHS op= RHS the RHS has side-effects, ensure they
5721 are preevaluated before the rest of the assignment expression's
5722 side-effects, because RHS could contain e.g. function calls
5723 that modify LHS. */
5724 if (TREE_SIDE_EFFECTS (rhs))
5725 {
5726 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5727 rhseval = newrhs;
5728 }
267bac10 5729 newrhs = build_binary_op (location,
241f845a 5730 modifycode, lhs, newrhs, 1);
267bac10
JM
5731
5732 /* The original type of the right hand side is no longer
5733 meaningful. */
5734 rhs_origtype = NULL_TREE;
5735 }
400fbf9f 5736 }
400fbf9f 5737
668ea4b1
IS
5738 if (c_dialect_objc ())
5739 {
46a88c12
NP
5740 /* Check if we are modifying an Objective-C property reference;
5741 if so, we need to generate setter calls. */
5742 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5743 if (result)
241f845a 5744 goto return_result;
46a88c12
NP
5745
5746 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5747 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5748 return error_mark_node;
5749 }
5750
9bf24266 5751 /* Give an error for storing in something that is 'const'. */
bbbd6700 5752
f37acdf9 5753 if (TYPE_READONLY (lhstype)
3e636daf 5754 || (RECORD_OR_UNION_TYPE_P (lhstype)
3e4093b6 5755 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5756 {
c02065fc 5757 readonly_error (location, lhs, lv_assign);
953ff289
DN
5758 return error_mark_node;
5759 }
f37acdf9
JM
5760 else if (TREE_READONLY (lhs))
5761 readonly_warning (lhs, lv_assign);
bbbd6700 5762
3e4093b6
RS
5763 /* If storing into a structure or union member,
5764 it has probably been given type `int'.
5765 Compute the type that would go with
5766 the actual amount of storage the member occupies. */
bbbd6700 5767
3e4093b6
RS
5768 if (TREE_CODE (lhs) == COMPONENT_REF
5769 && (TREE_CODE (lhstype) == INTEGER_TYPE
5770 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5771 || TREE_CODE (lhstype) == REAL_TYPE
5772 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5773 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5774
3e4093b6
RS
5775 /* If storing in a field that is in actuality a short or narrower than one,
5776 we must store in the field in its actual type. */
5777
5778 if (lhstype != TREE_TYPE (lhs))
5779 {
5780 lhs = copy_node (lhs);
5781 TREE_TYPE (lhs) = lhstype;
400fbf9f 5782 }
400fbf9f 5783
32e8bb8e
ILT
5784 /* Issue -Wc++-compat warnings about an assignment to an enum type
5785 when LHS does not have its original type. This happens for,
5786 e.g., an enum bitfield in a struct. */
5787 if (warn_cxx_compat
5788 && lhs_origtype != NULL_TREE
5789 && lhs_origtype != lhstype
5790 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5791 {
5792 tree checktype = (rhs_origtype != NULL_TREE
5793 ? rhs_origtype
5794 : TREE_TYPE (rhs));
5795 if (checktype != error_mark_node
267bac10
JM
5796 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5797 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5798 warning_at (location, OPT_Wc___compat,
5799 "enum conversion in assignment is invalid in C++");
5800 }
5801
267bac10
JM
5802 /* If the lhs is atomic, remove that qualifier. */
5803 if (is_atomic_op)
5804 {
5805 lhstype = build_qualified_type (lhstype,
5806 (TYPE_QUALS (lhstype)
5807 & ~TYPE_QUAL_ATOMIC));
5808 olhstype = build_qualified_type (olhstype,
5809 (TYPE_QUALS (lhstype)
5810 & ~TYPE_QUAL_ATOMIC));
5811 }
5812
8ce94e44
JM
5813 /* Convert new value to destination type. Fold it first, then
5814 restore any excess precision information, for the sake of
5815 conversion warnings. */
400fbf9f 5816
267bac10
JM
5817 if (!(is_atomic_op && modifycode != NOP_EXPR))
5818 {
5819 npc = null_pointer_constant_p (newrhs);
5820 newrhs = c_fully_fold (newrhs, false, NULL);
5821 if (rhs_semantic_type)
5822 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5823 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5824 rhs_origtype, ic_assign, npc,
5825 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5826 if (TREE_CODE (newrhs) == ERROR_MARK)
5827 return error_mark_node;
5828 }
400fbf9f 5829
6e955430
ZL
5830 /* Emit ObjC write barrier, if necessary. */
5831 if (c_dialect_objc () && flag_objc_gc)
5832 {
5833 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5834 if (result)
c9f9eb5d
AH
5835 {
5836 protected_set_expr_location (result, location);
241f845a 5837 goto return_result;
c9f9eb5d 5838 }
6e955430
ZL
5839 }
5840
ea4b7848 5841 /* Scan operands. */
400fbf9f 5842
267bac10
JM
5843 if (is_atomic_op)
5844 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5845 else
5846 {
5847 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5848 TREE_SIDE_EFFECTS (result) = 1;
5849 protected_set_expr_location (result, location);
5850 }
400fbf9f 5851
3e4093b6
RS
5852 /* If we got the LHS in a different type for storing in,
5853 convert the result back to the nominal type of LHS
5854 so that the value we return always has the same type
5855 as the LHS argument. */
e855c5ce 5856
3e4093b6 5857 if (olhstype == TREE_TYPE (result))
241f845a 5858 goto return_result;
c9f9eb5d 5859
68fca595
MP
5860 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5861 rhs_origtype, ic_assign, false, NULL_TREE,
5862 NULL_TREE, 0);
c9f9eb5d 5863 protected_set_expr_location (result, location);
241f845a
JJ
5864
5865return_result:
5866 if (rhseval)
5867 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5868 return result;
3e4093b6
RS
5869}
5870\f
478a1c5b
ILT
5871/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5872 This is used to implement -fplan9-extensions. */
5873
5874static bool
5875find_anonymous_field_with_type (tree struct_type, tree type)
5876{
5877 tree field;
5878 bool found;
5879
3e636daf 5880 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
478a1c5b
ILT
5881 found = false;
5882 for (field = TYPE_FIELDS (struct_type);
5883 field != NULL_TREE;
5884 field = TREE_CHAIN (field))
5885 {
267bac10
JM
5886 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5887 ? c_build_qualified_type (TREE_TYPE (field),
5888 TYPE_QUAL_ATOMIC)
5889 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5890 if (DECL_NAME (field) == NULL
267bac10 5891 && comptypes (type, fieldtype))
478a1c5b
ILT
5892 {
5893 if (found)
5894 return false;
5895 found = true;
5896 }
5897 else if (DECL_NAME (field) == NULL
3e636daf 5898 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
478a1c5b
ILT
5899 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5900 {
5901 if (found)
5902 return false;
5903 found = true;
5904 }
5905 }
5906 return found;
5907}
5908
5909/* RHS is an expression whose type is pointer to struct. If there is
5910 an anonymous field in RHS with type TYPE, then return a pointer to
5911 that field in RHS. This is used with -fplan9-extensions. This
5912 returns NULL if no conversion could be found. */
5913
5914static tree
5915convert_to_anonymous_field (location_t location, tree type, tree rhs)
5916{
5917 tree rhs_struct_type, lhs_main_type;
5918 tree field, found_field;
5919 bool found_sub_field;
5920 tree ret;
5921
5922 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5923 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
3e636daf 5924 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
478a1c5b
ILT
5925
5926 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5927 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5928 ? c_build_qualified_type (TREE_TYPE (type),
5929 TYPE_QUAL_ATOMIC)
5930 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5931
5932 found_field = NULL_TREE;
5933 found_sub_field = false;
5934 for (field = TYPE_FIELDS (rhs_struct_type);
5935 field != NULL_TREE;
5936 field = TREE_CHAIN (field))
5937 {
5938 if (DECL_NAME (field) != NULL_TREE
3e636daf 5939 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
478a1c5b 5940 continue;
267bac10
JM
5941 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5942 ? c_build_qualified_type (TREE_TYPE (field),
5943 TYPE_QUAL_ATOMIC)
5944 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5945 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5946 {
5947 if (found_field != NULL_TREE)
5948 return NULL_TREE;
5949 found_field = field;
5950 }
5951 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5952 lhs_main_type))
5953 {
5954 if (found_field != NULL_TREE)
5955 return NULL_TREE;
5956 found_field = field;
5957 found_sub_field = true;
5958 }
5959 }
5960
5961 if (found_field == NULL_TREE)
5962 return NULL_TREE;
5963
5964 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5965 build_fold_indirect_ref (rhs), found_field,
5966 NULL_TREE);
5967 ret = build_fold_addr_expr_loc (location, ret);
5968
5969 if (found_sub_field)
5970 {
5971 ret = convert_to_anonymous_field (location, type, ret);
5972 gcc_assert (ret != NULL_TREE);
5973 }
5974
5975 return ret;
5976}
5977
63bc4e87
MP
5978/* Issue an error message for a bad initializer component.
5979 GMSGID identifies the message.
5980 The component name is taken from the spelling stack. */
5981
5982static void
ea58ef42 5983error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5984{
5985 char *ofwhat;
5986
5987 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5988 error_at (loc, gmsgid);
63bc4e87
MP
5989 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5990 if (*ofwhat)
d7ff7ae5 5991 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5992}
5993
5994/* Issue a pedantic warning for a bad initializer component. OPT is
5995 the option OPT_* (from options.h) controlling this warning or 0 if
5996 it is unconditionally given. GMSGID identifies the message. The
5997 component name is taken from the spelling stack. */
5998
5999static void
79063edd 6000pedwarn_init (location_t loc, int opt, const char *gmsgid)
63bc4e87
MP
6001{
6002 char *ofwhat;
d7ff7ae5 6003 bool warned;
63bc4e87 6004
79063edd
MS
6005 /* Use the location where a macro was expanded rather than where
6006 it was defined to make sure macros defined in system headers
6007 but used incorrectly elsewhere are diagnosed. */
6008 source_location exploc = expansion_point_location_if_in_system_header (loc);
6009
63bc4e87 6010 /* The gmsgid may be a format string with %< and %>. */
79063edd 6011 warned = pedwarn (exploc, opt, gmsgid);
63bc4e87 6012 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5 6013 if (*ofwhat && warned)
79063edd 6014 inform (exploc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
6015}
6016
6017/* Issue a warning for a bad initializer component.
6018
6019 OPT is the OPT_W* value corresponding to the warning option that
6020 controls this warning. GMSGID identifies the message. The
6021 component name is taken from the spelling stack. */
6022
6023static void
6024warning_init (location_t loc, int opt, const char *gmsgid)
6025{
6026 char *ofwhat;
d7ff7ae5 6027 bool warned;
63bc4e87 6028
79063edd
MS
6029 /* Use the location where a macro was expanded rather than where
6030 it was defined to make sure macros defined in system headers
6031 but used incorrectly elsewhere are diagnosed. */
6032 source_location exploc = expansion_point_location_if_in_system_header (loc);
6033
63bc4e87 6034 /* The gmsgid may be a format string with %< and %>. */
79063edd 6035 warned = warning_at (exploc, opt, gmsgid);
63bc4e87 6036 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5 6037 if (*ofwhat && warned)
79063edd 6038 inform (exploc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
6039}
6040\f
6041/* If TYPE is an array type and EXPR is a parenthesized string
6042 constant, warn if pedantic that EXPR is being used to initialize an
6043 object of type TYPE. */
6044
6045void
d033409e 6046maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
6047{
6048 if (pedantic
6049 && TREE_CODE (type) == ARRAY_TYPE
6050 && TREE_CODE (expr.value) == STRING_CST
6051 && expr.original_code != STRING_CST)
d033409e 6052 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
6053 "array initialized from parenthesized string constant");
6054}
6055
bbbbb16a
ILT
6056/* Convert value RHS to type TYPE as preparation for an assignment to
6057 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6058 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6059 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6060 constant before any folding.
3e4093b6
RS
6061 The real work of conversion is done by `convert'.
6062 The purpose of this function is to generate error messages
6063 for assignments that are not allowed in C.
2ac2f164
JM
6064 ERRTYPE says whether it is argument passing, assignment,
6065 initialization or return.
2f6e4e97 6066
aabef2de
MP
6067 In the following example, '~' denotes where EXPR_LOC and '^' where
6068 LOCATION point to:
6069
6070 f (var); [ic_argpass]
6071 ^ ~~~
6072 x = var; [ic_assign]
6073 ^ ~~~;
6074 int x = var; [ic_init]
6075 ^^^
6076 return x; [ic_return]
6077 ^
6078
2ac2f164 6079 FUNCTION is a tree for the function being called.
3e4093b6 6080 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 6081
3e4093b6 6082static tree
81e5eca8 6083convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 6084 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
6085 bool null_pointer_constant, tree fundecl,
6086 tree function, int parmnum)
3e4093b6
RS
6087{
6088 enum tree_code codel = TREE_CODE (type);
8ce94e44 6089 tree orig_rhs = rhs;
3e4093b6
RS
6090 tree rhstype;
6091 enum tree_code coder;
2ac2f164 6092 tree rname = NULL_TREE;
58393038 6093 bool objc_ok = false;
2ac2f164 6094
1c7485af
MP
6095 /* Use the expansion point location to handle cases such as user's
6096 function returning a wrong-type macro defined in a system header. */
6097 location = expansion_point_location_if_in_system_header (location);
6098
6b4ef5c1 6099 if (errtype == ic_argpass)
2ac2f164
JM
6100 {
6101 tree selector;
6102 /* Change pointer to function to the function itself for
6103 diagnostics. */
6104 if (TREE_CODE (function) == ADDR_EXPR
6105 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6106 function = TREE_OPERAND (function, 0);
6107
6108 /* Handle an ObjC selector specially for diagnostics. */
6109 selector = objc_message_selector ();
6110 rname = function;
6111 if (selector && parmnum > 2)
6112 {
6113 rname = selector;
6114 parmnum -= 2;
6115 }
6116 }
6117
6118 /* This macro is used to emit diagnostics to ensure that all format
6119 strings are complete sentences, visible to gettext and checked at
6120 compile time. */
768952be 6121#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
6122 do { \
6123 switch (errtype) \
6124 { \
6125 case ic_argpass: \
5c1bc275 6126 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 6127 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 6128 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
6129 "expected %qT but argument is of type %qT", \
6130 type, rhstype); \
6131 break; \
1e053dfe
MLI
6132 case ic_assign: \
6133 pedwarn (LOCATION, OPT, AS); \
6134 break; \
6135 case ic_init: \
6a8f4e12 6136 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
6137 break; \
6138 case ic_return: \
d033409e 6139 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
6140 break; \
6141 default: \
6142 gcc_unreachable (); \
6143 } \
2ac2f164 6144 } while (0)
cb3ca04e 6145
49706e39
MLI
6146 /* This macro is used to emit diagnostics to ensure that all format
6147 strings are complete sentences, visible to gettext and checked at
768952be 6148 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 6149 extra parameter to enumerate qualifiers. */
768952be 6150#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
6151 do { \
6152 switch (errtype) \
6153 { \
6154 case ic_argpass: \
5c1bc275 6155 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 6156 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 6157 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
6158 "expected %qT but argument is of type %qT", \
6159 type, rhstype); \
6160 break; \
6161 case ic_assign: \
5c1bc275 6162 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
6163 break; \
6164 case ic_init: \
5c1bc275 6165 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
6166 break; \
6167 case ic_return: \
5c1bc275 6168 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
6169 break; \
6170 default: \
6171 gcc_unreachable (); \
6172 } \
6173 } while (0)
6174
768952be
MU
6175 /* This macro is used to emit diagnostics to ensure that all format
6176 strings are complete sentences, visible to gettext and checked at
6177 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6178 warning_at instead of pedwarn. */
6179#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6180 do { \
6181 switch (errtype) \
6182 { \
6183 case ic_argpass: \
6184 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6185 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6186 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6187 "expected %qT but argument is of type %qT", \
6188 type, rhstype); \
6189 break; \
6190 case ic_assign: \
6191 warning_at (LOCATION, OPT, AS, QUALS); \
6192 break; \
6193 case ic_init: \
6194 warning_at (LOCATION, OPT, IN, QUALS); \
6195 break; \
6196 case ic_return: \
6197 warning_at (LOCATION, OPT, RE, QUALS); \
6198 break; \
6199 default: \
6200 gcc_unreachable (); \
6201 } \
6202 } while (0)
6203
8ce94e44
JM
6204 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6205 rhs = TREE_OPERAND (rhs, 0);
6206
3e4093b6
RS
6207 rhstype = TREE_TYPE (rhs);
6208 coder = TREE_CODE (rhstype);
6209
6210 if (coder == ERROR_MARK)
6211 return error_mark_node;
6212
58393038
ZL
6213 if (c_dialect_objc ())
6214 {
6215 int parmno;
6216
6217 switch (errtype)
6218 {
6219 case ic_return:
6220 parmno = 0;
6221 break;
6222
6223 case ic_assign:
6224 parmno = -1;
6225 break;
6226
6227 case ic_init:
6228 parmno = -2;
6229 break;
6230
6231 default:
6232 parmno = parmnum;
6233 break;
6234 }
6235
6236 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6237 }
6238
bbbbb16a
ILT
6239 if (warn_cxx_compat)
6240 {
6241 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6242 if (checktype != error_mark_node
6243 && TREE_CODE (type) == ENUMERAL_TYPE
6244 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6245 {
768952be
MU
6246 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6247 G_("enum conversion when passing argument "
6248 "%d of %qE is invalid in C++"),
6249 G_("enum conversion in assignment is "
6250 "invalid in C++"),
6251 G_("enum conversion in initialization is "
6252 "invalid in C++"),
6253 G_("enum conversion in return is "
6254 "invalid in C++"));
bbbbb16a
ILT
6255 }
6256 }
6257
3e4093b6 6258 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 6259 return rhs;
3e4093b6
RS
6260
6261 if (coder == VOID_TYPE)
400fbf9f 6262 {
6dcc04b0
JM
6263 /* Except for passing an argument to an unprototyped function,
6264 this is a constraint violation. When passing an argument to
6265 an unprototyped function, it is compile-time undefined;
6266 making it a constraint in that case was rejected in
6267 DR#252. */
c2255bc4 6268 error_at (location, "void value not ignored as it ought to be");
3e4093b6 6269 return error_mark_node;
400fbf9f 6270 }
4f2e1536 6271 rhs = require_complete_type (location, rhs);
808d6eaa
JM
6272 if (rhs == error_mark_node)
6273 return error_mark_node;
1807ffc1
MS
6274
6275 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6276 return error_mark_node;
6277
cd192ccc
MS
6278 /* A non-reference type can convert to a reference. This handles
6279 va_start, va_copy and possibly port built-ins. */
6280 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 6281 {
3e4093b6 6282 if (!lvalue_p (rhs))
400fbf9f 6283 {
c2255bc4 6284 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 6285 return error_mark_node;
400fbf9f 6286 }
3e4093b6
RS
6287 if (!c_mark_addressable (rhs))
6288 return error_mark_node;
6289 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 6290 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6291
81e5eca8 6292 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
6293 build_pointer_type (TREE_TYPE (type)),
6294 rhs, origtype, errtype,
6295 null_pointer_constant, fundecl, function,
6296 parmnum);
cd192ccc
MS
6297 if (rhs == error_mark_node)
6298 return error_mark_node;
3e4093b6
RS
6299
6300 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 6301 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6302 return rhs;
400fbf9f 6303 }
3e4093b6 6304 /* Some types can interconvert without explicit casts. */
3274deff 6305 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 6306 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
6307 return convert (type, rhs);
6308 /* Arithmetic types all interconvert, and enum is treated like int. */
6309 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 6310 || codel == FIXED_POINT_TYPE
3e4093b6
RS
6311 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6312 || codel == BOOLEAN_TYPE)
6313 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 6314 || coder == FIXED_POINT_TYPE
3e4093b6
RS
6315 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6316 || coder == BOOLEAN_TYPE))
928c19bb
JM
6317 {
6318 tree ret;
6319 bool save = in_late_binary_op;
e5341100
JJ
6320 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6321 || (coder == REAL_TYPE
6322 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6323 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
928c19bb 6324 in_late_binary_op = true;
81e5eca8
MP
6325 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6326 ? expr_loc : location, type, orig_rhs);
e5341100 6327 in_late_binary_op = save;
928c19bb
JM
6328 return ret;
6329 }
400fbf9f 6330
79077aea
JJ
6331 /* Aggregates in different TUs might need conversion. */
6332 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6333 && codel == coder
6334 && comptypes (type, rhstype))
81e5eca8
MP
6335 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6336 ? expr_loc : location, type, rhs);
79077aea 6337
ebf0bf7f 6338 /* Conversion to a transparent union or record from its member types.
3e4093b6 6339 This applies only to function arguments. */
ebf0bf7f
JJ
6340 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6341 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 6342 && errtype == ic_argpass)
400fbf9f 6343 {
0257e383 6344 tree memb, marginal_memb = NULL_TREE;
3e4093b6 6345
910ad8de 6346 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 6347 {
0257e383 6348 tree memb_type = TREE_TYPE (memb);
400fbf9f 6349
3e4093b6 6350 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 6351 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 6352 break;
e58cd767 6353
3e4093b6
RS
6354 if (TREE_CODE (memb_type) != POINTER_TYPE)
6355 continue;
2f6e4e97 6356
3e4093b6
RS
6357 if (coder == POINTER_TYPE)
6358 {
6359 tree ttl = TREE_TYPE (memb_type);
6360 tree ttr = TREE_TYPE (rhstype);
400fbf9f 6361
3e4093b6
RS
6362 /* Any non-function converts to a [const][volatile] void *
6363 and vice versa; otherwise, targets must be the same.
6364 Meanwhile, the lhs target must have all the qualifiers of
6365 the rhs. */
267bac10
JM
6366 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6367 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6368 || comp_target_types (location, memb_type, rhstype))
3e4093b6 6369 {
267bac10
JM
6370 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6371 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 6372 /* If this type won't generate any warnings, use it. */
267bac10 6373 if (lquals == rquals
3e4093b6
RS
6374 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6375 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
6376 ? ((lquals | rquals) == rquals)
6377 : ((lquals | rquals) == lquals)))
3e4093b6 6378 break;
400fbf9f 6379
3e4093b6 6380 /* Keep looking for a better type, but remember this one. */
0257e383
RH
6381 if (!marginal_memb)
6382 marginal_memb = memb;
3e4093b6
RS
6383 }
6384 }
82bde854 6385
3e4093b6 6386 /* Can convert integer zero to any pointer type. */
928c19bb 6387 if (null_pointer_constant)
3e4093b6
RS
6388 {
6389 rhs = null_pointer_node;
6390 break;
6391 }
6392 }
400fbf9f 6393
0257e383 6394 if (memb || marginal_memb)
3e4093b6 6395 {
0257e383 6396 if (!memb)
3e4093b6
RS
6397 {
6398 /* We have only a marginally acceptable member type;
6399 it needs a warning. */
0257e383 6400 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 6401 tree ttr = TREE_TYPE (rhstype);
714a0864 6402
3e4093b6
RS
6403 /* Const and volatile mean something different for function
6404 types, so the usual warnings are not appropriate. */
6405 if (TREE_CODE (ttr) == FUNCTION_TYPE
6406 && TREE_CODE (ttl) == FUNCTION_TYPE)
6407 {
6408 /* Because const and volatile on functions are
6409 restrictions that say the function will not do
6410 certain things, it is okay to use a const or volatile
6411 function where an ordinary one is wanted, but not
6412 vice-versa. */
36c5e70a
BE
6413 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6414 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6415 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6416 OPT_Wdiscarded_qualifiers,
6417 G_("passing argument %d of %qE "
6418 "makes %q#v qualified function "
6419 "pointer from unqualified"),
6420 G_("assignment makes %q#v qualified "
6421 "function pointer from "
6422 "unqualified"),
6423 G_("initialization makes %q#v qualified "
6424 "function pointer from "
6425 "unqualified"),
6426 G_("return makes %q#v qualified function "
6427 "pointer from unqualified"),
6428 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6429 }
36c5e70a
BE
6430 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6431 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6432 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6433 OPT_Wdiscarded_qualifiers,
6434 G_("passing argument %d of %qE discards "
6435 "%qv qualifier from pointer target type"),
6436 G_("assignment discards %qv qualifier "
6437 "from pointer target type"),
6438 G_("initialization discards %qv qualifier "
6439 "from pointer target type"),
6440 G_("return discards %qv qualifier from "
6441 "pointer target type"),
6442 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6443
6444 memb = marginal_memb;
3e4093b6 6445 }
400fbf9f 6446
fcf73884 6447 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6448 pedwarn (location, OPT_Wpedantic,
fcf73884 6449 "ISO C prohibits argument conversion to union type");
0e7c47fa 6450
db3927fb 6451 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6452 return build_constructor_single (type, memb, rhs);
3e4093b6 6453 }
0e7c47fa
RK
6454 }
6455
3e4093b6
RS
6456 /* Conversions among pointers */
6457 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6458 && (coder == codel))
400fbf9f 6459 {
3e4093b6
RS
6460 tree ttl = TREE_TYPE (type);
6461 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6462 tree mvl = ttl;
6463 tree mvr = ttr;
3e4093b6 6464 bool is_opaque_pointer;
264fa2db 6465 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6466 addr_space_t asl;
6467 addr_space_t asr;
400fbf9f 6468
46df2823 6469 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6470 mvl = (TYPE_ATOMIC (mvl)
6471 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6472 TYPE_QUAL_ATOMIC)
6473 : TYPE_MAIN_VARIANT (mvl));
46df2823 6474 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6475 mvr = (TYPE_ATOMIC (mvr)
6476 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6477 TYPE_QUAL_ATOMIC)
6478 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6479 /* Opaque pointers are treated like void pointers. */
f83c7f63 6480 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6481
478a1c5b
ILT
6482 /* The Plan 9 compiler permits a pointer to a struct to be
6483 automatically converted into a pointer to an anonymous field
6484 within the struct. */
6485 if (flag_plan9_extensions
3e636daf
MP
6486 && RECORD_OR_UNION_TYPE_P (mvl)
6487 && RECORD_OR_UNION_TYPE_P (mvr)
478a1c5b
ILT
6488 && mvl != mvr)
6489 {
6490 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6491 if (new_rhs != NULL_TREE)
6492 {
6493 rhs = new_rhs;
6494 rhstype = TREE_TYPE (rhs);
6495 coder = TREE_CODE (rhstype);
6496 ttr = TREE_TYPE (rhstype);
6497 mvr = TYPE_MAIN_VARIANT (ttr);
6498 }
6499 }
6500
b7e20b53 6501 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6502 for the purpose of reducing the number of false positives, we
6503 tolerate the special case of
b7e20b53 6504
c22cacf3 6505 int *p = NULL;
b7e20b53 6506
c22cacf3 6507 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6508 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6509 warning_at (errtype == ic_argpass ? expr_loc : location,
6510 OPT_Wc___compat,
6511 "request for implicit conversion "
c2255bc4 6512 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6513
36c5e70a
BE
6514 /* See if the pointers point to incompatible address spaces. */
6515 asl = TYPE_ADDR_SPACE (ttl);
6516 asr = TYPE_ADDR_SPACE (ttr);
6517 if (!null_pointer_constant_p (rhs)
6518 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6519 {
6520 switch (errtype)
6521 {
6522 case ic_argpass:
8ffcdea8 6523 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6524 "non-enclosed address space", parmnum, rname);
6525 break;
6526 case ic_assign:
6527 error_at (location, "assignment from pointer to "
6528 "non-enclosed address space");
6529 break;
6530 case ic_init:
6531 error_at (location, "initialization from pointer to "
6532 "non-enclosed address space");
6533 break;
6534 case ic_return:
6535 error_at (location, "return from pointer to "
6536 "non-enclosed address space");
6537 break;
6538 default:
6539 gcc_unreachable ();
6540 }
6541 return error_mark_node;
6542 }
6543
7876a414
KG
6544 /* Check if the right-hand side has a format attribute but the
6545 left-hand side doesn't. */
90137d8f 6546 if (warn_suggest_attribute_format
104f8784 6547 && check_missing_format_attribute (type, rhstype))
c22cacf3 6548 {
104f8784
KG
6549 switch (errtype)
6550 {
6551 case ic_argpass:
8ffcdea8 6552 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6553 "argument %d of %qE might be "
6554 "a candidate for a format attribute",
6555 parmnum, rname);
104f8784
KG
6556 break;
6557 case ic_assign:
90137d8f 6558 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6559 "assignment left-hand side might be "
6560 "a candidate for a format attribute");
104f8784
KG
6561 break;
6562 case ic_init:
90137d8f 6563 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6564 "initialization left-hand side might be "
6565 "a candidate for a format attribute");
104f8784
KG
6566 break;
6567 case ic_return:
90137d8f 6568 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6569 "return type might be "
6570 "a candidate for a format attribute");
104f8784
KG
6571 break;
6572 default:
6573 gcc_unreachable ();
6574 }
7876a414 6575 }
c22cacf3 6576
3e4093b6
RS
6577 /* Any non-function converts to a [const][volatile] void *
6578 and vice versa; otherwise, targets must be the same.
6579 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6580 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6581 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6582 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6583 || is_opaque_pointer
f8a93a2e
JJ
6584 || ((c_common_unsigned_type (mvl)
6585 == c_common_unsigned_type (mvr))
267bac10
JM
6586 && (c_common_signed_type (mvl)
6587 == c_common_signed_type (mvr))
6588 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6589 {
768952be
MU
6590 /* Warn about loss of qualifers from pointers to arrays with
6591 qualifiers on the element type. */
6592 if (TREE_CODE (ttr) == ARRAY_TYPE)
6593 {
6594 ttr = strip_array_types (ttr);
6595 ttl = strip_array_types (ttl);
6596
6597 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6598 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6599 WARNING_FOR_QUALIFIERS (location, expr_loc,
6600 OPT_Wdiscarded_array_qualifiers,
6601 G_("passing argument %d of %qE discards "
6602 "%qv qualifier from pointer target type"),
6603 G_("assignment discards %qv qualifier "
6604 "from pointer target type"),
6605 G_("initialization discards %qv qualifier "
6606 "from pointer target type"),
6607 G_("return discards %qv qualifier from "
6608 "pointer target type"),
6609 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6610 }
6611 else if (pedantic
3e4093b6
RS
6612 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6613 ||
6614 (VOID_TYPE_P (ttr)
928c19bb 6615 && !null_pointer_constant
3e4093b6 6616 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6617 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6618 G_("ISO C forbids passing argument %d of "
6619 "%qE between function pointer "
6620 "and %<void *%>"),
6621 G_("ISO C forbids assignment between "
6622 "function pointer and %<void *%>"),
6623 G_("ISO C forbids initialization between "
6624 "function pointer and %<void *%>"),
6625 G_("ISO C forbids return between function "
6626 "pointer and %<void *%>"));
3e4093b6
RS
6627 /* Const and volatile mean something different for function types,
6628 so the usual warnings are not appropriate. */
6629 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6630 && TREE_CODE (ttl) != FUNCTION_TYPE)
6631 {
768952be
MU
6632 /* Don't warn about loss of qualifier for conversions from
6633 qualified void* to pointers to arrays with corresponding
6634 qualifier on the element type. */
6635 if (!pedantic)
6636 ttl = strip_array_types (ttl);
6637
267bac10
JM
6638 /* Assignments between atomic and non-atomic objects are OK. */
6639 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6640 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6641 {
768952be
MU
6642 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6643 OPT_Wdiscarded_qualifiers,
6644 G_("passing argument %d of %qE discards "
6645 "%qv qualifier from pointer target type"),
6646 G_("assignment discards %qv qualifier "
6647 "from pointer target type"),
6648 G_("initialization discards %qv qualifier "
6649 "from pointer target type"),
6650 G_("return discards %qv qualifier from "
6651 "pointer target type"),
6652 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6653 }
3e4093b6
RS
6654 /* If this is not a case of ignoring a mismatch in signedness,
6655 no warning. */
6656 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6657 || target_cmp)
3e4093b6
RS
6658 ;
6659 /* If there is a mismatch, do warn. */
f2fd3821 6660 else if (warn_pointer_sign)
768952be
MU
6661 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6662 G_("pointer targets in passing argument "
6663 "%d of %qE differ in signedness"),
6664 G_("pointer targets in assignment "
6665 "differ in signedness"),
6666 G_("pointer targets in initialization "
6667 "differ in signedness"),
6668 G_("pointer targets in return differ "
6669 "in signedness"));
3e4093b6
RS
6670 }
6671 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6672 && TREE_CODE (ttr) == FUNCTION_TYPE)
6673 {
6674 /* Because const and volatile on functions are restrictions
6675 that say the function will not do certain things,
6676 it is okay to use a const or volatile function
6677 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6678 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6679 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6680 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6681 OPT_Wdiscarded_qualifiers,
6682 G_("passing argument %d of %qE makes "
6683 "%q#v qualified function pointer "
6684 "from unqualified"),
6685 G_("assignment makes %q#v qualified function "
6686 "pointer from unqualified"),
6687 G_("initialization makes %q#v qualified "
6688 "function pointer from unqualified"),
6689 G_("return makes %q#v qualified function "
6690 "pointer from unqualified"),
6691 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6692 }
6693 }
6694 else
58393038
ZL
6695 /* Avoid warning about the volatile ObjC EH puts on decls. */
6696 if (!objc_ok)
768952be
MU
6697 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6698 OPT_Wincompatible_pointer_types,
6699 G_("passing argument %d of %qE from "
6700 "incompatible pointer type"),
6701 G_("assignment from incompatible pointer type"),
6702 G_("initialization from incompatible "
6703 "pointer type"),
6704 G_("return from incompatible pointer type"));
58393038 6705
3e4093b6
RS
6706 return convert (type, rhs);
6707 }
b494fd98
EB
6708 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6709 {
6dcc04b0
JM
6710 /* ??? This should not be an error when inlining calls to
6711 unprototyped functions. */
c2255bc4 6712 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6713 return error_mark_node;
6714 }
3e4093b6 6715 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6716 {
3e4093b6
RS
6717 /* An explicit constant 0 can convert to a pointer,
6718 or one that results from arithmetic, even including
6719 a cast to integer type. */
928c19bb 6720 if (!null_pointer_constant)
768952be
MU
6721 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6722 OPT_Wint_conversion,
6723 G_("passing argument %d of %qE makes "
6724 "pointer from integer without a cast"),
6725 G_("assignment makes pointer from integer "
6726 "without a cast"),
6727 G_("initialization makes pointer from "
6728 "integer without a cast"),
6729 G_("return makes pointer from integer "
6730 "without a cast"));
b3006337
EB
6731
6732 return convert (type, rhs);
400fbf9f 6733 }
3e4093b6 6734 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6735 {
768952be
MU
6736 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6737 OPT_Wint_conversion,
6738 G_("passing argument %d of %qE makes integer "
6739 "from pointer without a cast"),
6740 G_("assignment makes integer from pointer "
6741 "without a cast"),
6742 G_("initialization makes integer from pointer "
6743 "without a cast"),
6744 G_("return makes integer from pointer "
6745 "without a cast"));
3e4093b6
RS
6746 return convert (type, rhs);
6747 }
6748 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6749 {
6750 tree ret;
6751 bool save = in_late_binary_op;
6752 in_late_binary_op = true;
6753 ret = convert (type, rhs);
6754 in_late_binary_op = save;
6755 return ret;
6756 }
400fbf9f 6757
2ac2f164 6758 switch (errtype)
3e4093b6 6759 {
2ac2f164 6760 case ic_argpass:
8ffcdea8
MP
6761 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6762 rname);
a7da8b42 6763 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6764 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6765 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6766 break;
6767 case ic_assign:
c2255bc4
AH
6768 error_at (location, "incompatible types when assigning to type %qT from "
6769 "type %qT", type, rhstype);
2ac2f164
JM
6770 break;
6771 case ic_init:
c2255bc4 6772 error_at (location,
8ffcdea8 6773 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6774 type, rhstype);
2ac2f164
JM
6775 break;
6776 case ic_return:
c2255bc4 6777 error_at (location,
8ffcdea8 6778 "incompatible types when returning type %qT but %qT was "
c2255bc4 6779 "expected", rhstype, type);
2ac2f164
JM
6780 break;
6781 default:
6782 gcc_unreachable ();
400fbf9f 6783 }
53b01f59 6784
3e4093b6
RS
6785 return error_mark_node;
6786}
3e4093b6
RS
6787\f
6788/* If VALUE is a compound expr all of whose expressions are constant, then
6789 return its value. Otherwise, return error_mark_node.
15b732b2 6790
3e4093b6
RS
6791 This is for handling COMPOUND_EXPRs as initializer elements
6792 which is allowed with a warning when -pedantic is specified. */
15b732b2 6793
3e4093b6
RS
6794static tree
6795valid_compound_expr_initializer (tree value, tree endtype)
6796{
6797 if (TREE_CODE (value) == COMPOUND_EXPR)
6798 {
6799 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6800 == error_mark_node)
6801 return error_mark_node;
6802 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6803 endtype);
6804 }
116df786 6805 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6806 return error_mark_node;
6807 else
6808 return value;
15b732b2 6809}
400fbf9f 6810\f
3e4093b6
RS
6811/* Perform appropriate conversions on the initial value of a variable,
6812 store it in the declaration DECL,
6813 and print any error messages that are appropriate.
bbbbb16a 6814 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6815 If the init is invalid, store an ERROR_MARK.
6816
6817 INIT_LOC is the location of the initial value. */
400fbf9f 6818
3e4093b6 6819void
c2255bc4 6820store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6821{
3e4093b6 6822 tree value, type;
928c19bb 6823 bool npc = false;
400fbf9f 6824
3e4093b6 6825 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6826
3e4093b6
RS
6827 type = TREE_TYPE (decl);
6828 if (TREE_CODE (type) == ERROR_MARK)
6829 return;
400fbf9f 6830
3e4093b6 6831 /* Digest the specified initializer into an expression. */
400fbf9f 6832
928c19bb
JM
6833 if (init)
6834 npc = null_pointer_constant_p (init);
c2255bc4
AH
6835 value = digest_init (init_loc, type, init, origtype, npc,
6836 true, TREE_STATIC (decl));
400fbf9f 6837
3e4093b6 6838 /* Store the expression if valid; else report error. */
400fbf9f 6839
8400e75e 6840 if (!in_system_header_at (input_location)
3f75a254 6841 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6842 warning (OPT_Wtraditional, "traditional C rejects automatic "
6843 "aggregate initialization");
2f6e4e97 6844
dea63e49
JJ
6845 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6846 DECL_INITIAL (decl) = value;
400fbf9f 6847
3e4093b6
RS
6848 /* ANSI wants warnings about out-of-range constant initializers. */
6849 STRIP_TYPE_NOPS (value);
b8698a0f 6850 if (TREE_STATIC (decl))
c2658540 6851 constant_expression_warning (value);
400fbf9f 6852
3e4093b6
RS
6853 /* Check if we need to set array size from compound literal size. */
6854 if (TREE_CODE (type) == ARRAY_TYPE
6855 && TYPE_DOMAIN (type) == 0
6856 && value != error_mark_node)
400fbf9f 6857 {
3e4093b6
RS
6858 tree inside_init = init;
6859
ed248cf7 6860 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6861 inside_init = fold (inside_init);
6862
6863 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6864 {
8d9f82d5 6865 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6866
8d9f82d5 6867 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6868 {
6869 /* For int foo[] = (int [3]){1}; we need to set array size
6870 now since later on array initializer will be just the
6871 brace enclosed list of the compound literal. */
e30ecc5d 6872 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6873 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6874 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6875 layout_type (type);
8d9f82d5 6876 layout_decl (cldecl, 0);
e30ecc5d
JJ
6877 TREE_TYPE (decl)
6878 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6879 }
6880 }
400fbf9f 6881 }
3e4093b6
RS
6882}
6883\f
6884/* Methods for storing and printing names for error messages. */
400fbf9f 6885
3e4093b6
RS
6886/* Implement a spelling stack that allows components of a name to be pushed
6887 and popped. Each element on the stack is this structure. */
400fbf9f 6888
3e4093b6
RS
6889struct spelling
6890{
6891 int kind;
6892 union
400fbf9f 6893 {
a0f0ab9f 6894 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6895 const char *s;
6896 } u;
6897};
2f6e4e97 6898
3e4093b6
RS
6899#define SPELLING_STRING 1
6900#define SPELLING_MEMBER 2
6901#define SPELLING_BOUNDS 3
400fbf9f 6902
3e4093b6
RS
6903static struct spelling *spelling; /* Next stack element (unused). */
6904static struct spelling *spelling_base; /* Spelling stack base. */
6905static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6906
3e4093b6
RS
6907/* Macros to save and restore the spelling stack around push_... functions.
6908 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6909
3e4093b6
RS
6910#define SPELLING_DEPTH() (spelling - spelling_base)
6911#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6912
3e4093b6
RS
6913/* Push an element on the spelling stack with type KIND and assign VALUE
6914 to MEMBER. */
400fbf9f 6915
3e4093b6
RS
6916#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6917{ \
6918 int depth = SPELLING_DEPTH (); \
6919 \
6920 if (depth >= spelling_size) \
6921 { \
6922 spelling_size += 10; \
cca8ead2
BI
6923 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6924 spelling_size); \
3e4093b6
RS
6925 RESTORE_SPELLING_DEPTH (depth); \
6926 } \
6927 \
6928 spelling->kind = (KIND); \
6929 spelling->MEMBER = (VALUE); \
6930 spelling++; \
6931}
400fbf9f 6932
3e4093b6 6933/* Push STRING on the stack. Printed literally. */
400fbf9f 6934
3e4093b6
RS
6935static void
6936push_string (const char *string)
6937{
6938 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6939}
400fbf9f 6940
3e4093b6 6941/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6942
3e4093b6
RS
6943static void
6944push_member_name (tree decl)
6945{
6946 const char *const string
88388a52
JM
6947 = (DECL_NAME (decl)
6948 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6949 : _("<anonymous>"));
3e4093b6
RS
6950 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6951}
400fbf9f 6952
3e4093b6 6953/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6954
3e4093b6 6955static void
a0f0ab9f 6956push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6957{
6958 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6959}
bb58bec5 6960
3e4093b6 6961/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6962
3e4093b6
RS
6963static int
6964spelling_length (void)
6965{
6966 int size = 0;
6967 struct spelling *p;
400fbf9f 6968
3e4093b6
RS
6969 for (p = spelling_base; p < spelling; p++)
6970 {
6971 if (p->kind == SPELLING_BOUNDS)
6972 size += 25;
6973 else
6974 size += strlen (p->u.s) + 1;
6975 }
6976
6977 return size;
400fbf9f 6978}
400fbf9f 6979
3e4093b6 6980/* Print the spelling to BUFFER and return it. */
400fbf9f 6981
3e4093b6
RS
6982static char *
6983print_spelling (char *buffer)
400fbf9f 6984{
3e4093b6
RS
6985 char *d = buffer;
6986 struct spelling *p;
400fbf9f 6987
3e4093b6
RS
6988 for (p = spelling_base; p < spelling; p++)
6989 if (p->kind == SPELLING_BOUNDS)
6990 {
a0f0ab9f 6991 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6992 d += strlen (d);
6993 }
6994 else
6995 {
6996 const char *s;
6997 if (p->kind == SPELLING_MEMBER)
6998 *d++ = '.';
6999 for (s = p->u.s; (*d = *s++); d++)
7000 ;
7001 }
7002 *d++ = '\0';
7003 return buffer;
7004}
400fbf9f 7005
3e4093b6
RS
7006/* Digest the parser output INIT as an initializer for type TYPE.
7007 Return a C expression of type TYPE to represent the initial value.
7e842ef8 7008
bbbbb16a
ILT
7009 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7010
928c19bb
JM
7011 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7012
916c5919
JM
7013 If INIT is a string constant, STRICT_STRING is true if it is
7014 unparenthesized or we should not warn here for it being parenthesized.
7015 For other types of INIT, STRICT_STRING is not used.
7016
c2255bc4
AH
7017 INIT_LOC is the location of the INIT.
7018
3e4093b6
RS
7019 REQUIRE_CONSTANT requests an error if non-constant initializers or
7020 elements are seen. */
7e842ef8 7021
3e4093b6 7022static tree
c2255bc4
AH
7023digest_init (location_t init_loc, tree type, tree init, tree origtype,
7024 bool null_pointer_constant, bool strict_string,
7025 int require_constant)
3e4093b6
RS
7026{
7027 enum tree_code code = TREE_CODE (type);
7028 tree inside_init = init;
8ce94e44 7029 tree semantic_type = NULL_TREE;
928c19bb 7030 bool maybe_const = true;
7e842ef8 7031
3e4093b6 7032 if (type == error_mark_node
f01da1a5 7033 || !init
7a0ca710 7034 || error_operand_p (init))
3e4093b6 7035 return error_mark_node;
7e842ef8 7036
ed248cf7 7037 STRIP_TYPE_NOPS (inside_init);
7e842ef8 7038
8ce94e44
JM
7039 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7040 {
7041 semantic_type = TREE_TYPE (inside_init);
7042 inside_init = TREE_OPERAND (inside_init, 0);
7043 }
928c19bb
JM
7044 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7045 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 7046
3e4093b6
RS
7047 /* Initialization of an array of chars from a string constant
7048 optionally enclosed in braces. */
7e842ef8 7049
197463ae
JM
7050 if (code == ARRAY_TYPE && inside_init
7051 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 7052 {
267bac10
JM
7053 tree typ1
7054 = (TYPE_ATOMIC (TREE_TYPE (type))
7055 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7056 TYPE_QUAL_ATOMIC)
7057 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
7058 /* Note that an array could be both an array of character type
7059 and an array of wchar_t if wchar_t is signed char or unsigned
7060 char. */
7061 bool char_array = (typ1 == char_type_node
7062 || typ1 == signed_char_type_node
7063 || typ1 == unsigned_char_type_node);
7064 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
7065 bool char16_array = !!comptypes (typ1, char16_type_node);
7066 bool char32_array = !!comptypes (typ1, char32_type_node);
7067
7068 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 7069 {
916c5919 7070 struct c_expr expr;
c466b2cd 7071 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
7072 expr.value = inside_init;
7073 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 7074 expr.original_type = NULL;
d033409e 7075 maybe_warn_string_init (init_loc, type, expr);
916c5919 7076
a45e580b 7077 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 7078 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
7079 "initialization of a flexible array member");
7080
3e4093b6 7081 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 7082 TYPE_MAIN_VARIANT (type)))
3e4093b6 7083 return inside_init;
7e842ef8 7084
c466b2cd 7085 if (char_array)
3e4093b6 7086 {
c466b2cd
KVH
7087 if (typ2 != char_type_node)
7088 {
ea58ef42
MP
7089 error_init (init_loc, "char-array initialized from wide "
7090 "string");
c466b2cd
KVH
7091 return error_mark_node;
7092 }
3e4093b6 7093 }
c466b2cd 7094 else
3e4093b6 7095 {
c466b2cd
KVH
7096 if (typ2 == char_type_node)
7097 {
ea58ef42
MP
7098 error_init (init_loc, "wide character array initialized "
7099 "from non-wide string");
c466b2cd
KVH
7100 return error_mark_node;
7101 }
7102 else if (!comptypes(typ1, typ2))
7103 {
ea58ef42
MP
7104 error_init (init_loc, "wide character array initialized "
7105 "from incompatible wide string");
c466b2cd
KVH
7106 return error_mark_node;
7107 }
7e842ef8 7108 }
2f6e4e97 7109
3e4093b6
RS
7110 TREE_TYPE (inside_init) = type;
7111 if (TYPE_DOMAIN (type) != 0
7112 && TYPE_SIZE (type) != 0
5eb4df45
ILT
7113 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7114 {
7115 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7116
c466b2cd 7117 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
7118 because it's ok to ignore the terminating null char
7119 that is counted in the length of the constant. */
5eb4df45
ILT
7120 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7121 (len
7122 - (TYPE_PRECISION (typ1)
7123 / BITS_PER_UNIT))))
7124 pedwarn_init (init_loc, 0,
7125 ("initializer-string for array of chars "
7126 "is too long"));
7127 else if (warn_cxx_compat
7128 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7129 warning_at (init_loc, OPT_Wc___compat,
7130 ("initializer-string for array chars "
7131 "is too long for C++"));
7132 }
7e842ef8 7133
3e4093b6 7134 return inside_init;
7e842ef8 7135 }
197463ae
JM
7136 else if (INTEGRAL_TYPE_P (typ1))
7137 {
ea58ef42 7138 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
7139 "from string constant");
7140 return error_mark_node;
7141 }
7e842ef8
PE
7142 }
7143
3e4093b6
RS
7144 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7145 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7146 below and handle as a constructor. */
e89be13b 7147 if (code == VECTOR_TYPE
31521951 7148 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
00c8e9f6 7149 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
7150 && TREE_CONSTANT (inside_init))
7151 {
7152 if (TREE_CODE (inside_init) == VECTOR_CST
7153 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7154 TYPE_MAIN_VARIANT (type)))
7155 return inside_init;
7156
7157 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7158 {
4038c495
GB
7159 unsigned HOST_WIDE_INT ix;
7160 tree value;
7161 bool constant_p = true;
e89be13b
JJ
7162
7163 /* Iterate through elements and check if all constructor
7164 elements are *_CSTs. */
4038c495
GB
7165 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7166 if (!CONSTANT_CLASS_P (value))
7167 {
7168 constant_p = false;
7169 break;
7170 }
e89be13b 7171
4038c495
GB
7172 if (constant_p)
7173 return build_vector_from_ctor (type,
7174 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
7175 }
7176 }
6035d635 7177
ca085fd7
MLI
7178 if (warn_sequence_point)
7179 verify_sequence_points (inside_init);
7180
3e4093b6
RS
7181 /* Any type can be initialized
7182 from an expression of the same type, optionally with braces. */
400fbf9f 7183
3e4093b6
RS
7184 if (inside_init && TREE_TYPE (inside_init) != 0
7185 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 7186 TYPE_MAIN_VARIANT (type))
3e4093b6 7187 || (code == ARRAY_TYPE
132da1a5 7188 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7189 || (code == VECTOR_TYPE
132da1a5 7190 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7191 || (code == POINTER_TYPE
3897f229 7192 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 7193 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 7194 TREE_TYPE (type)))))
3e4093b6
RS
7195 {
7196 if (code == POINTER_TYPE)
b494fd98 7197 {
b494fd98
EB
7198 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7199 {
f2a71bbc
JM
7200 if (TREE_CODE (inside_init) == STRING_CST
7201 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
7202 inside_init = array_to_pointer_conversion
7203 (init_loc, inside_init);
f2a71bbc
JM
7204 else
7205 {
ea58ef42 7206 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
7207 return error_mark_node;
7208 }
b494fd98 7209 }
f2a71bbc 7210 }
b494fd98 7211
bae39a73
NS
7212 if (code == VECTOR_TYPE)
7213 /* Although the types are compatible, we may require a
7214 conversion. */
7215 inside_init = convert (type, inside_init);
3e4093b6 7216
ca58211b 7217 if (require_constant
3e4093b6 7218 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 7219 {
3e4093b6
RS
7220 /* As an extension, allow initializing objects with static storage
7221 duration with compound literals (which are then treated just as
ca58211b
PB
7222 the brace enclosed list they contain). Also allow this for
7223 vectors, as we can only assign them with compound literals. */
7278465e
MP
7224 if (flag_isoc99 && code != VECTOR_TYPE)
7225 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7226 "is not constant");
3e4093b6
RS
7227 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7228 inside_init = DECL_INITIAL (decl);
400fbf9f 7229 }
3e4093b6
RS
7230
7231 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7232 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 7233 {
ea58ef42
MP
7234 error_init (init_loc, "array initialized from non-constant array "
7235 "expression");
3e4093b6 7236 return error_mark_node;
400fbf9f 7237 }
400fbf9f 7238
c1771a20 7239 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
7240 -pedantic-errors is specified. In the later case, we always want
7241 an error. In the former case, we simply want a warning. */
7242 if (require_constant && pedantic
7243 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7244 {
7245 inside_init
7246 = valid_compound_expr_initializer (inside_init,
7247 TREE_TYPE (inside_init));
7248 if (inside_init == error_mark_node)
ea58ef42 7249 error_init (init_loc, "initializer element is not constant");
2f6e4e97 7250 else
c1771a20 7251 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 7252 "initializer element is not constant");
3e4093b6
RS
7253 if (flag_pedantic_errors)
7254 inside_init = error_mark_node;
7255 }
7256 else if (require_constant
116df786
RH
7257 && !initializer_constant_valid_p (inside_init,
7258 TREE_TYPE (inside_init)))
3e4093b6 7259 {
ea58ef42 7260 error_init (init_loc, "initializer element is not constant");
3e4093b6 7261 inside_init = error_mark_node;
8b40563c 7262 }
928c19bb 7263 else if (require_constant && !maybe_const)
3aa3c9fc 7264 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7265 "initializer element is not a constant expression");
f735a153 7266
90137d8f 7267 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 7268 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
7269 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7270 type, inside_init, origtype,
bbbbb16a 7271 ic_init, null_pointer_constant,
928c19bb 7272 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
7273 return inside_init;
7274 }
f735a153 7275
3e4093b6 7276 /* Handle scalar types, including conversions. */
400fbf9f 7277
ab22c1fa
CF
7278 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7279 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7280 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 7281 {
f2a71bbc
JM
7282 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7283 && (TREE_CODE (init) == STRING_CST
7284 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 7285 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
7286 if (semantic_type)
7287 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7288 inside_init);
3e4093b6 7289 inside_init
68fca595
MP
7290 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7291 inside_init, origtype, ic_init,
7292 null_pointer_constant, NULL_TREE, NULL_TREE,
7293 0);
2f6e4e97 7294
3274deff
JW
7295 /* Check to see if we have already given an error message. */
7296 if (inside_init == error_mark_node)
7297 ;
3f75a254 7298 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 7299 {
ea58ef42 7300 error_init (init_loc, "initializer element is not constant");
3e4093b6 7301 inside_init = error_mark_node;
400fbf9f 7302 }
3e4093b6 7303 else if (require_constant
116df786
RH
7304 && !initializer_constant_valid_p (inside_init,
7305 TREE_TYPE (inside_init)))
400fbf9f 7306 {
ea58ef42
MP
7307 error_init (init_loc, "initializer element is not computable at "
7308 "load time");
3e4093b6 7309 inside_init = error_mark_node;
400fbf9f 7310 }
928c19bb 7311 else if (require_constant && !maybe_const)
451b5e48 7312 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7313 "initializer element is not a constant expression");
3e4093b6
RS
7314
7315 return inside_init;
400fbf9f 7316 }
d9fc6069 7317
3e4093b6 7318 /* Come here only for records and arrays. */
d9fc6069 7319
3e4093b6 7320 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 7321 {
ea58ef42 7322 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 7323 return error_mark_node;
d9fc6069 7324 }
3e4093b6 7325
ea58ef42 7326 error_init (init_loc, "invalid initializer");
3e4093b6 7327 return error_mark_node;
d9fc6069 7328}
400fbf9f 7329\f
3e4093b6 7330/* Handle initializers that use braces. */
400fbf9f 7331
3e4093b6
RS
7332/* Type of object we are accumulating a constructor for.
7333 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7334static tree constructor_type;
400fbf9f 7335
3e4093b6
RS
7336/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7337 left to fill. */
7338static tree constructor_fields;
400fbf9f 7339
3e4093b6
RS
7340/* For an ARRAY_TYPE, this is the specified index
7341 at which to store the next element we get. */
7342static tree constructor_index;
400fbf9f 7343
3e4093b6
RS
7344/* For an ARRAY_TYPE, this is the maximum index. */
7345static tree constructor_max_index;
400fbf9f 7346
3e4093b6
RS
7347/* For a RECORD_TYPE, this is the first field not yet written out. */
7348static tree constructor_unfilled_fields;
400fbf9f 7349
3e4093b6
RS
7350/* For an ARRAY_TYPE, this is the index of the first element
7351 not yet written out. */
7352static tree constructor_unfilled_index;
895ea614 7353
3e4093b6
RS
7354/* In a RECORD_TYPE, the byte index of the next consecutive field.
7355 This is so we can generate gaps between fields, when appropriate. */
7356static tree constructor_bit_index;
10d5caec 7357
3e4093b6
RS
7358/* If we are saving up the elements rather than allocating them,
7359 this is the list of elements so far (in reverse order,
7360 most recent first). */
9771b263 7361static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 7362
3e4093b6
RS
7363/* 1 if constructor should be incrementally stored into a constructor chain,
7364 0 if all the elements should be kept in AVL tree. */
7365static int constructor_incremental;
ad47f1e5 7366
3e4093b6
RS
7367/* 1 if so far this constructor's elements are all compile-time constants. */
7368static int constructor_constant;
ad47f1e5 7369
3e4093b6
RS
7370/* 1 if so far this constructor's elements are all valid address constants. */
7371static int constructor_simple;
ad47f1e5 7372
928c19bb
JM
7373/* 1 if this constructor has an element that cannot be part of a
7374 constant expression. */
7375static int constructor_nonconst;
7376
3e4093b6
RS
7377/* 1 if this constructor is erroneous so far. */
7378static int constructor_erroneous;
d45cf215 7379
9bac5cbb
G
7380/* 1 if this constructor is the universal zero initializer { 0 }. */
7381static int constructor_zeroinit;
7382
3e4093b6
RS
7383/* Structure for managing pending initializer elements, organized as an
7384 AVL tree. */
d45cf215 7385
3e4093b6 7386struct init_node
d45cf215 7387{
3e4093b6
RS
7388 struct init_node *left, *right;
7389 struct init_node *parent;
7390 int balance;
7391 tree purpose;
7392 tree value;
bbbbb16a 7393 tree origtype;
d45cf215
RS
7394};
7395
3e4093b6
RS
7396/* Tree of pending elements at this constructor level.
7397 These are elements encountered out of order
7398 which belong at places we haven't reached yet in actually
7399 writing the output.
7400 Will never hold tree nodes across GC runs. */
7401static struct init_node *constructor_pending_elts;
d45cf215 7402
3e4093b6
RS
7403/* The SPELLING_DEPTH of this constructor. */
7404static int constructor_depth;
d45cf215 7405
3e4093b6
RS
7406/* DECL node for which an initializer is being read.
7407 0 means we are reading a constructor expression
7408 such as (struct foo) {...}. */
7409static tree constructor_decl;
d45cf215 7410
3e4093b6
RS
7411/* Nonzero if this is an initializer for a top-level decl. */
7412static int constructor_top_level;
d45cf215 7413
3e4093b6
RS
7414/* Nonzero if there were any member designators in this initializer. */
7415static int constructor_designated;
d45cf215 7416
3e4093b6
RS
7417/* Nesting depth of designator list. */
7418static int designator_depth;
d45cf215 7419
3e4093b6 7420/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7421static int designator_erroneous;
d45cf215 7422
3e4093b6
RS
7423\f
7424/* This stack has a level for each implicit or explicit level of
7425 structuring in the initializer, including the outermost one. It
7426 saves the values of most of the variables above. */
d45cf215 7427
3e4093b6
RS
7428struct constructor_range_stack;
7429
7430struct constructor_stack
d45cf215 7431{
3e4093b6
RS
7432 struct constructor_stack *next;
7433 tree type;
7434 tree fields;
7435 tree index;
7436 tree max_index;
7437 tree unfilled_index;
7438 tree unfilled_fields;
7439 tree bit_index;
9771b263 7440 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7441 struct init_node *pending_elts;
7442 int offset;
7443 int depth;
916c5919 7444 /* If value nonzero, this value should replace the entire
3e4093b6 7445 constructor at this level. */
916c5919 7446 struct c_expr replacement_value;
3e4093b6
RS
7447 struct constructor_range_stack *range_stack;
7448 char constant;
7449 char simple;
928c19bb 7450 char nonconst;
3e4093b6
RS
7451 char implicit;
7452 char erroneous;
7453 char outer;
7454 char incremental;
7455 char designated;
976d5a22 7456 int designator_depth;
3e4093b6 7457};
d45cf215 7458
802415d1 7459static struct constructor_stack *constructor_stack;
d45cf215 7460
3e4093b6
RS
7461/* This stack represents designators from some range designator up to
7462 the last designator in the list. */
d45cf215 7463
3e4093b6
RS
7464struct constructor_range_stack
7465{
7466 struct constructor_range_stack *next, *prev;
7467 struct constructor_stack *stack;
7468 tree range_start;
7469 tree index;
7470 tree range_end;
7471 tree fields;
7472};
d45cf215 7473
802415d1 7474static struct constructor_range_stack *constructor_range_stack;
d45cf215 7475
3e4093b6
RS
7476/* This stack records separate initializers that are nested.
7477 Nested initializers can't happen in ANSI C, but GNU C allows them
7478 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7479
3e4093b6 7480struct initializer_stack
d45cf215 7481{
3e4093b6
RS
7482 struct initializer_stack *next;
7483 tree decl;
3e4093b6
RS
7484 struct constructor_stack *constructor_stack;
7485 struct constructor_range_stack *constructor_range_stack;
9771b263 7486 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7487 struct spelling *spelling;
7488 struct spelling *spelling_base;
7489 int spelling_size;
7490 char top_level;
7491 char require_constant_value;
7492 char require_constant_elements;
7493};
d45cf215 7494
802415d1 7495static struct initializer_stack *initializer_stack;
3e4093b6
RS
7496\f
7497/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7498
7499void
a396f8ae 7500start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 7501{
3e4093b6 7502 const char *locus;
cceb1885 7503 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7504
3e4093b6 7505 p->decl = constructor_decl;
3e4093b6
RS
7506 p->require_constant_value = require_constant_value;
7507 p->require_constant_elements = require_constant_elements;
7508 p->constructor_stack = constructor_stack;
7509 p->constructor_range_stack = constructor_range_stack;
7510 p->elements = constructor_elements;
7511 p->spelling = spelling;
7512 p->spelling_base = spelling_base;
7513 p->spelling_size = spelling_size;
7514 p->top_level = constructor_top_level;
7515 p->next = initializer_stack;
7516 initializer_stack = p;
400fbf9f 7517
3e4093b6 7518 constructor_decl = decl;
3e4093b6
RS
7519 constructor_designated = 0;
7520 constructor_top_level = top_level;
400fbf9f 7521
6f17bbcf 7522 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7523 {
7524 require_constant_value = TREE_STATIC (decl);
7525 require_constant_elements
7526 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7527 /* For a scalar, you can always use any value to initialize,
7528 even within braces. */
296a8c2f 7529 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
88388a52 7530 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7531 }
7532 else
7533 {
7534 require_constant_value = 0;
7535 require_constant_elements = 0;
88388a52 7536 locus = _("(anonymous)");
3e4093b6 7537 }
b71c7f8a 7538
3e4093b6
RS
7539 constructor_stack = 0;
7540 constructor_range_stack = 0;
b71c7f8a 7541
9bac5cbb 7542 found_missing_braces = 0;
3e4093b6
RS
7543
7544 spelling_base = 0;
7545 spelling_size = 0;
7546 RESTORE_SPELLING_DEPTH (0);
7547
7548 if (locus)
7549 push_string (locus);
7550}
7551
7552void
7553finish_init (void)
b71c7f8a 7554{
3e4093b6 7555 struct initializer_stack *p = initializer_stack;
b71c7f8a 7556
3e4093b6
RS
7557 /* Free the whole constructor stack of this initializer. */
7558 while (constructor_stack)
7559 {
7560 struct constructor_stack *q = constructor_stack;
7561 constructor_stack = q->next;
7562 free (q);
7563 }
7564
366de0ce 7565 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7566
7567 /* Pop back to the data of the outer initializer (if any). */
36579663 7568 free (spelling_base);
3aeb3655 7569
3e4093b6 7570 constructor_decl = p->decl;
3e4093b6
RS
7571 require_constant_value = p->require_constant_value;
7572 require_constant_elements = p->require_constant_elements;
7573 constructor_stack = p->constructor_stack;
7574 constructor_range_stack = p->constructor_range_stack;
7575 constructor_elements = p->elements;
7576 spelling = p->spelling;
7577 spelling_base = p->spelling_base;
7578 spelling_size = p->spelling_size;
7579 constructor_top_level = p->top_level;
7580 initializer_stack = p->next;
7581 free (p);
b71c7f8a 7582}
400fbf9f 7583\f
3e4093b6
RS
7584/* Call here when we see the initializer is surrounded by braces.
7585 This is instead of a call to push_init_level;
7586 it is matched by a call to pop_init_level.
400fbf9f 7587
3e4093b6
RS
7588 TYPE is the type to initialize, for a constructor expression.
7589 For an initializer for a decl, TYPE is zero. */
400fbf9f 7590
3e4093b6
RS
7591void
7592really_start_incremental_init (tree type)
400fbf9f 7593{
5d038c4c 7594 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7595
3e4093b6
RS
7596 if (type == 0)
7597 type = TREE_TYPE (constructor_decl);
400fbf9f 7598
31521951 7599 if (VECTOR_TYPE_P (type)
b6fc2cdb 7600 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7601 error ("opaque vector types cannot be initialized");
400fbf9f 7602
3e4093b6
RS
7603 p->type = constructor_type;
7604 p->fields = constructor_fields;
7605 p->index = constructor_index;
7606 p->max_index = constructor_max_index;
7607 p->unfilled_index = constructor_unfilled_index;
7608 p->unfilled_fields = constructor_unfilled_fields;
7609 p->bit_index = constructor_bit_index;
7610 p->elements = constructor_elements;
7611 p->constant = constructor_constant;
7612 p->simple = constructor_simple;
928c19bb 7613 p->nonconst = constructor_nonconst;
3e4093b6
RS
7614 p->erroneous = constructor_erroneous;
7615 p->pending_elts = constructor_pending_elts;
7616 p->depth = constructor_depth;
916c5919
JM
7617 p->replacement_value.value = 0;
7618 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7619 p->replacement_value.original_type = NULL;
3e4093b6
RS
7620 p->implicit = 0;
7621 p->range_stack = 0;
7622 p->outer = 0;
7623 p->incremental = constructor_incremental;
7624 p->designated = constructor_designated;
976d5a22 7625 p->designator_depth = designator_depth;
3e4093b6
RS
7626 p->next = 0;
7627 constructor_stack = p;
b13aca19 7628
3e4093b6
RS
7629 constructor_constant = 1;
7630 constructor_simple = 1;
928c19bb 7631 constructor_nonconst = 0;
3e4093b6 7632 constructor_depth = SPELLING_DEPTH ();
9771b263 7633 constructor_elements = NULL;
3e4093b6
RS
7634 constructor_pending_elts = 0;
7635 constructor_type = type;
7636 constructor_incremental = 1;
7637 constructor_designated = 0;
9bac5cbb 7638 constructor_zeroinit = 1;
3e4093b6 7639 designator_depth = 0;
b06df647 7640 designator_erroneous = 0;
400fbf9f 7641
3e636daf 7642 if (RECORD_OR_UNION_TYPE_P (constructor_type))
400fbf9f 7643 {
3e4093b6
RS
7644 constructor_fields = TYPE_FIELDS (constructor_type);
7645 /* Skip any nameless bit fields at the beginning. */
7646 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7647 && DECL_NAME (constructor_fields) == 0)
910ad8de 7648 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7649
3e4093b6
RS
7650 constructor_unfilled_fields = constructor_fields;
7651 constructor_bit_index = bitsize_zero_node;
400fbf9f 7652 }
3e4093b6
RS
7653 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7654 {
7655 if (TYPE_DOMAIN (constructor_type))
7656 {
7657 constructor_max_index
7658 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7659
3e4093b6
RS
7660 /* Detect non-empty initializations of zero-length arrays. */
7661 if (constructor_max_index == NULL_TREE
7662 && TYPE_SIZE (constructor_type))
9a9d280e 7663 constructor_max_index = integer_minus_one_node;
400fbf9f 7664
3e4093b6
RS
7665 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7666 to initialize VLAs will cause a proper error; avoid tree
7667 checking errors as well by setting a safe value. */
7668 if (constructor_max_index
7669 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7670 constructor_max_index = integer_minus_one_node;
59c83dbf 7671
3e4093b6
RS
7672 constructor_index
7673 = convert (bitsizetype,
7674 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7675 }
3e4093b6 7676 else
493179da
JM
7677 {
7678 constructor_index = bitsize_zero_node;
7679 constructor_max_index = NULL_TREE;
7680 }
59c83dbf 7681
3e4093b6
RS
7682 constructor_unfilled_index = constructor_index;
7683 }
31521951 7684 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7685 {
7686 /* Vectors are like simple fixed-size arrays. */
7687 constructor_max_index =
c62c040f 7688 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7689 constructor_index = bitsize_zero_node;
3e4093b6
RS
7690 constructor_unfilled_index = constructor_index;
7691 }
7692 else
7693 {
7694 /* Handle the case of int x = {5}; */
7695 constructor_fields = constructor_type;
7696 constructor_unfilled_fields = constructor_type;
7697 }
7698}
7699\f
16595a1f
BS
7700/* Called when we see an open brace for a nested initializer. Finish
7701 off any pending levels with implicit braces. */
7702void
7703finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7704{
7705 while (constructor_stack->implicit)
7706 {
7707 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7708 && constructor_fields == 0)
7709 process_init_element (input_location,
7710 pop_init_level (loc, 1, braced_init_obstack),
7711 true, braced_init_obstack);
7712 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7713 && constructor_max_index
7714 && tree_int_cst_lt (constructor_max_index,
7715 constructor_index))
7716 process_init_element (input_location,
7717 pop_init_level (loc, 1, braced_init_obstack),
7718 true, braced_init_obstack);
7719 else
7720 break;
7721 }
7722}
7723
3e4093b6
RS
7724/* Push down into a subobject, for initialization.
7725 If this is for an explicit set of braces, IMPLICIT is 0.
7726 If it is because the next element belongs at a lower level,
7727 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7728
3e4093b6 7729void
ea58ef42
MP
7730push_init_level (location_t loc, int implicit,
7731 struct obstack *braced_init_obstack)
3e4093b6
RS
7732{
7733 struct constructor_stack *p;
7734 tree value = NULL_TREE;
400fbf9f 7735
3e4093b6
RS
7736 /* Unless this is an explicit brace, we need to preserve previous
7737 content if any. */
7738 if (implicit)
7739 {
3e636daf 7740 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
a1e3b3d9 7741 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7742 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7743 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7744 }
7745
5d038c4c 7746 p = XNEW (struct constructor_stack);
3e4093b6
RS
7747 p->type = constructor_type;
7748 p->fields = constructor_fields;
7749 p->index = constructor_index;
7750 p->max_index = constructor_max_index;
7751 p->unfilled_index = constructor_unfilled_index;
7752 p->unfilled_fields = constructor_unfilled_fields;
7753 p->bit_index = constructor_bit_index;
7754 p->elements = constructor_elements;
7755 p->constant = constructor_constant;
7756 p->simple = constructor_simple;
928c19bb 7757 p->nonconst = constructor_nonconst;
3e4093b6
RS
7758 p->erroneous = constructor_erroneous;
7759 p->pending_elts = constructor_pending_elts;
7760 p->depth = constructor_depth;
916c5919
JM
7761 p->replacement_value.value = 0;
7762 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7763 p->replacement_value.original_type = NULL;
3e4093b6
RS
7764 p->implicit = implicit;
7765 p->outer = 0;
7766 p->incremental = constructor_incremental;
7767 p->designated = constructor_designated;
976d5a22 7768 p->designator_depth = designator_depth;
3e4093b6
RS
7769 p->next = constructor_stack;
7770 p->range_stack = 0;
7771 constructor_stack = p;
400fbf9f 7772
3e4093b6
RS
7773 constructor_constant = 1;
7774 constructor_simple = 1;
928c19bb 7775 constructor_nonconst = 0;
3e4093b6 7776 constructor_depth = SPELLING_DEPTH ();
9771b263 7777 constructor_elements = NULL;
3e4093b6
RS
7778 constructor_incremental = 1;
7779 constructor_designated = 0;
7780 constructor_pending_elts = 0;
7781 if (!implicit)
400fbf9f 7782 {
3e4093b6
RS
7783 p->range_stack = constructor_range_stack;
7784 constructor_range_stack = 0;
7785 designator_depth = 0;
b06df647 7786 designator_erroneous = 0;
3e4093b6 7787 }
400fbf9f 7788
3e4093b6
RS
7789 /* Don't die if an entire brace-pair level is superfluous
7790 in the containing level. */
7791 if (constructor_type == 0)
7792 ;
3e636daf 7793 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
7794 {
7795 /* Don't die if there are extra init elts at the end. */
7796 if (constructor_fields == 0)
7797 constructor_type = 0;
7798 else
400fbf9f 7799 {
3e4093b6
RS
7800 constructor_type = TREE_TYPE (constructor_fields);
7801 push_member_name (constructor_fields);
7802 constructor_depth++;
400fbf9f 7803 }
6a358dcb
MP
7804 /* If upper initializer is designated, then mark this as
7805 designated too to prevent bogus warnings. */
7806 constructor_designated = p->designated;
3e4093b6
RS
7807 }
7808 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7809 {
7810 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7811 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7812 constructor_depth++;
400fbf9f
JW
7813 }
7814
3e4093b6 7815 if (constructor_type == 0)
400fbf9f 7816 {
ea58ef42 7817 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7818 constructor_fields = 0;
7819 constructor_unfilled_fields = 0;
7820 return;
400fbf9f
JW
7821 }
7822
3e4093b6
RS
7823 if (value && TREE_CODE (value) == CONSTRUCTOR)
7824 {
7825 constructor_constant = TREE_CONSTANT (value);
7826 constructor_simple = TREE_STATIC (value);
928c19bb 7827 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7828 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7829 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7830 && (TREE_CODE (constructor_type) == RECORD_TYPE
7831 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7832 set_nonincremental_init (braced_init_obstack);
3e4093b6 7833 }
400fbf9f 7834
9bac5cbb
G
7835 if (implicit == 1)
7836 found_missing_braces = 1;
400fbf9f 7837
3e636daf 7838 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
7839 {
7840 constructor_fields = TYPE_FIELDS (constructor_type);
7841 /* Skip any nameless bit fields at the beginning. */
7842 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7843 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7844 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7845
3e4093b6
RS
7846 constructor_unfilled_fields = constructor_fields;
7847 constructor_bit_index = bitsize_zero_node;
7848 }
31521951 7849 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7850 {
7851 /* Vectors are like simple fixed-size arrays. */
7852 constructor_max_index =
c62c040f
RG
7853 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7854 constructor_index = bitsize_int (0);
3e4093b6
RS
7855 constructor_unfilled_index = constructor_index;
7856 }
7857 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7858 {
7859 if (TYPE_DOMAIN (constructor_type))
7860 {
7861 constructor_max_index
7862 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7863
3e4093b6
RS
7864 /* Detect non-empty initializations of zero-length arrays. */
7865 if (constructor_max_index == NULL_TREE
7866 && TYPE_SIZE (constructor_type))
9a9d280e 7867 constructor_max_index = integer_minus_one_node;
de520661 7868
3e4093b6
RS
7869 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7870 to initialize VLAs will cause a proper error; avoid tree
7871 checking errors as well by setting a safe value. */
7872 if (constructor_max_index
7873 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7874 constructor_max_index = integer_minus_one_node;
b62acd60 7875
3e4093b6
RS
7876 constructor_index
7877 = convert (bitsizetype,
7878 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7879 }
7880 else
7881 constructor_index = bitsize_zero_node;
de520661 7882
3e4093b6
RS
7883 constructor_unfilled_index = constructor_index;
7884 if (value && TREE_CODE (value) == STRING_CST)
7885 {
7886 /* We need to split the char/wchar array into individual
7887 characters, so that we don't have to special case it
7888 everywhere. */
a1e3b3d9 7889 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7890 }
7891 }
7892 else
7893 {
b4519d39 7894 if (constructor_type != error_mark_node)
96b40f8d 7895 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7896 constructor_fields = constructor_type;
7897 constructor_unfilled_fields = constructor_type;
7898 }
7899}
8b6a5902 7900
3e4093b6 7901/* At the end of an implicit or explicit brace level,
916c5919
JM
7902 finish up that level of constructor. If a single expression
7903 with redundant braces initialized that level, return the
7904 c_expr structure for that expression. Otherwise, the original_code
7905 element is set to ERROR_MARK.
7906 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7907 from inner levels (process_init_element ignores that),
916c5919 7908 but return error_mark_node as the value from the outermost level
3e4093b6 7909 (that's what we want to put in DECL_INITIAL).
916c5919 7910 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7911
916c5919 7912struct c_expr
ea58ef42
MP
7913pop_init_level (location_t loc, int implicit,
7914 struct obstack *braced_init_obstack)
3e4093b6
RS
7915{
7916 struct constructor_stack *p;
916c5919
JM
7917 struct c_expr ret;
7918 ret.value = 0;
7919 ret.original_code = ERROR_MARK;
6866c6e8 7920 ret.original_type = NULL;
de520661 7921
3e4093b6
RS
7922 if (implicit == 0)
7923 {
7924 /* When we come to an explicit close brace,
7925 pop any inner levels that didn't have explicit braces. */
7926 while (constructor_stack->implicit)
34cf811f 7927 process_init_element (input_location,
ea58ef42 7928 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7929 true, braced_init_obstack);
366de0ce 7930 gcc_assert (!constructor_range_stack);
3e4093b6 7931 }
e5e809f4 7932
0066ef9c
RH
7933 /* Now output all pending elements. */
7934 constructor_incremental = 1;
a1e3b3d9 7935 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7936
3e4093b6 7937 p = constructor_stack;
e5e809f4 7938
3e4093b6
RS
7939 /* Error for initializing a flexible array member, or a zero-length
7940 array member in an inappropriate context. */
7941 if (constructor_type && constructor_fields
7942 && TREE_CODE (constructor_type) == ARRAY_TYPE
7943 && TYPE_DOMAIN (constructor_type)
3f75a254 7944 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7945 {
7946 /* Silently discard empty initializations. The parser will
7947 already have pedwarned for empty brackets. */
7948 if (integer_zerop (constructor_unfilled_index))
7949 constructor_type = NULL_TREE;
366de0ce 7950 else
3e4093b6 7951 {
366de0ce 7952 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7953
3e4093b6 7954 if (constructor_depth > 2)
ea58ef42 7955 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7956 else
d033409e 7957 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7958 "initialization of a flexible array member");
de520661 7959
3e4093b6
RS
7960 /* We have already issued an error message for the existence
7961 of a flexible array member not at the end of the structure.
535a42b1 7962 Discard the initializer so that we do not die later. */
910ad8de 7963 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7964 constructor_type = NULL_TREE;
7965 }
3e4093b6 7966 }
de520661 7967
7b33f0c8
MP
7968 switch (vec_safe_length (constructor_elements))
7969 {
7970 case 0:
7971 /* Initialization with { } counts as zeroinit. */
7972 constructor_zeroinit = 1;
7973 break;
7974 case 1:
7975 /* This might be zeroinit as well. */
7976 if (integer_zerop ((*constructor_elements)[0].value))
7977 constructor_zeroinit = 1;
7978 break;
7979 default:
7980 /* If the constructor has more than one element, it can't be { 0 }. */
7981 constructor_zeroinit = 0;
7982 break;
7983 }
9bac5cbb
G
7984
7985 /* Warn when some structs are initialized with direct aggregation. */
7986 if (!implicit && found_missing_braces && warn_missing_braces
7987 && !constructor_zeroinit)
7b33f0c8
MP
7988 warning_init (loc, OPT_Wmissing_braces,
7989 "missing braces around initializer");
9bac5cbb 7990
3e4093b6 7991 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7992 if (warn_missing_field_initializers
3e4093b6
RS
7993 && constructor_type
7994 && TREE_CODE (constructor_type) == RECORD_TYPE
7995 && constructor_unfilled_fields)
7996 {
7997 /* Do not warn for flexible array members or zero-length arrays. */
7998 while (constructor_unfilled_fields
3f75a254 7999 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 8000 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 8001 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 8002
49819fef
AM
8003 if (constructor_unfilled_fields
8004 /* Do not warn if this level of the initializer uses member
8005 designators; it is likely to be deliberate. */
8006 && !constructor_designated
84937de2 8007 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 8008 && !constructor_zeroinit)
3e4093b6 8009 {
32397f22
MLI
8010 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8011 "missing initializer for field %qD of %qT",
8012 constructor_unfilled_fields,
8013 constructor_type))
8014 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 8015 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
8016 }
8017 }
de520661 8018
3e4093b6 8019 /* Pad out the end of the structure. */
916c5919 8020 if (p->replacement_value.value)
3e4093b6
RS
8021 /* If this closes a superfluous brace pair,
8022 just pass out the element between them. */
916c5919 8023 ret = p->replacement_value;
3e4093b6
RS
8024 else if (constructor_type == 0)
8025 ;
3e636daf 8026 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 8027 && TREE_CODE (constructor_type) != ARRAY_TYPE
31521951 8028 && !VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
8029 {
8030 /* A nonincremental scalar initializer--just return
8031 the element, after verifying there is just one. */
9771b263 8032 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
8033 {
8034 if (!constructor_erroneous)
ea58ef42 8035 error_init (loc, "empty scalar initializer");
916c5919 8036 ret.value = error_mark_node;
3e4093b6 8037 }
9771b263 8038 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 8039 {
ea58ef42 8040 error_init (loc, "extra elements in scalar initializer");
9771b263 8041 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
8042 }
8043 else
9771b263 8044 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
8045 }
8046 else
8047 {
8048 if (constructor_erroneous)
916c5919 8049 ret.value = error_mark_node;
3e4093b6
RS
8050 else
8051 {
916c5919 8052 ret.value = build_constructor (constructor_type,
4038c495 8053 constructor_elements);
3e4093b6 8054 if (constructor_constant)
51eed280 8055 TREE_CONSTANT (ret.value) = 1;
3e4093b6 8056 if (constructor_constant && constructor_simple)
916c5919 8057 TREE_STATIC (ret.value) = 1;
928c19bb
JM
8058 if (constructor_nonconst)
8059 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
8060 }
8061 }
de520661 8062
928c19bb
JM
8063 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8064 {
8065 if (constructor_nonconst)
8066 ret.original_code = C_MAYBE_CONST_EXPR;
8067 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8068 ret.original_code = ERROR_MARK;
8069 }
8070
3e4093b6
RS
8071 constructor_type = p->type;
8072 constructor_fields = p->fields;
8073 constructor_index = p->index;
8074 constructor_max_index = p->max_index;
8075 constructor_unfilled_index = p->unfilled_index;
8076 constructor_unfilled_fields = p->unfilled_fields;
8077 constructor_bit_index = p->bit_index;
8078 constructor_elements = p->elements;
8079 constructor_constant = p->constant;
8080 constructor_simple = p->simple;
928c19bb 8081 constructor_nonconst = p->nonconst;
3e4093b6
RS
8082 constructor_erroneous = p->erroneous;
8083 constructor_incremental = p->incremental;
8084 constructor_designated = p->designated;
976d5a22 8085 designator_depth = p->designator_depth;
3e4093b6
RS
8086 constructor_pending_elts = p->pending_elts;
8087 constructor_depth = p->depth;
8088 if (!p->implicit)
8089 constructor_range_stack = p->range_stack;
8090 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 8091
3e4093b6
RS
8092 constructor_stack = p->next;
8093 free (p);
b621a4dd 8094
5d5e98dc
VR
8095 if (ret.value == 0 && constructor_stack == 0)
8096 ret.value = error_mark_node;
916c5919 8097 return ret;
3e4093b6 8098}
8b6a5902 8099
3e4093b6
RS
8100/* Common handling for both array range and field name designators.
8101 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 8102
3e4093b6 8103static int
ea58ef42
MP
8104set_designator (location_t loc, int array,
8105 struct obstack *braced_init_obstack)
de520661 8106{
3e4093b6
RS
8107 tree subtype;
8108 enum tree_code subcode;
de520661 8109
3e4093b6
RS
8110 /* Don't die if an entire brace-pair level is superfluous
8111 in the containing level. */
8112 if (constructor_type == 0)
8113 return 1;
de520661 8114
366de0ce
NS
8115 /* If there were errors in this designator list already, bail out
8116 silently. */
b06df647 8117 if (designator_erroneous)
3e4093b6 8118 return 1;
e28cae4f 8119
3e4093b6
RS
8120 if (!designator_depth)
8121 {
366de0ce 8122 gcc_assert (!constructor_range_stack);
de520661 8123
3e4093b6
RS
8124 /* Designator list starts at the level of closest explicit
8125 braces. */
8126 while (constructor_stack->implicit)
34cf811f 8127 process_init_element (input_location,
ea58ef42 8128 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 8129 true, braced_init_obstack);
3e4093b6
RS
8130 constructor_designated = 1;
8131 return 0;
8132 }
de520661 8133
366de0ce 8134 switch (TREE_CODE (constructor_type))
3c3fa147 8135 {
366de0ce
NS
8136 case RECORD_TYPE:
8137 case UNION_TYPE:
3e4093b6
RS
8138 subtype = TREE_TYPE (constructor_fields);
8139 if (subtype != error_mark_node)
8140 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
8141 break;
8142 case ARRAY_TYPE:
3e4093b6 8143 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
8144 break;
8145 default:
8146 gcc_unreachable ();
de520661 8147 }
400fbf9f 8148
3e4093b6
RS
8149 subcode = TREE_CODE (subtype);
8150 if (array && subcode != ARRAY_TYPE)
8151 {
ea58ef42 8152 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
8153 return 1;
8154 }
8155 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8156 {
ea58ef42 8157 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
8158 return 1;
8159 }
d45cf215 8160
3e4093b6 8161 constructor_designated = 1;
16595a1f 8162 finish_implicit_inits (loc, braced_init_obstack);
ea58ef42 8163 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 8164 return 0;
de520661 8165}
400fbf9f 8166
3e4093b6
RS
8167/* If there are range designators in designator list, push a new designator
8168 to constructor_range_stack. RANGE_END is end of such stack range or
8169 NULL_TREE if there is no range designator at this level. */
400fbf9f 8170
3e4093b6 8171static void
a1e3b3d9 8172push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
8173{
8174 struct constructor_range_stack *p;
400fbf9f 8175
a1e3b3d9
LB
8176 p = (struct constructor_range_stack *)
8177 obstack_alloc (braced_init_obstack,
8178 sizeof (struct constructor_range_stack));
3e4093b6
RS
8179 p->prev = constructor_range_stack;
8180 p->next = 0;
8181 p->fields = constructor_fields;
8182 p->range_start = constructor_index;
8183 p->index = constructor_index;
8184 p->stack = constructor_stack;
8185 p->range_end = range_end;
8b6a5902 8186 if (constructor_range_stack)
3e4093b6
RS
8187 constructor_range_stack->next = p;
8188 constructor_range_stack = p;
de520661 8189}
400fbf9f 8190
3e4093b6
RS
8191/* Within an array initializer, specify the next index to be initialized.
8192 FIRST is that index. If LAST is nonzero, then initialize a range
8193 of indices, running from FIRST through LAST. */
5a7ec9d9 8194
de520661 8195void
ea58ef42 8196set_init_index (location_t loc, tree first, tree last,
d033409e 8197 struct obstack *braced_init_obstack)
de520661 8198{
ea58ef42 8199 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 8200 return;
de520661 8201
b06df647 8202 designator_erroneous = 1;
de520661 8203
3ea8cd06
JM
8204 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8205 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8206 {
ea58ef42 8207 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
8208 return;
8209 }
8210
2b6da65c
JM
8211 if (TREE_CODE (first) != INTEGER_CST)
8212 {
8213 first = c_fully_fold (first, false, NULL);
8214 if (TREE_CODE (first) == INTEGER_CST)
d033409e 8215 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8216 "array index in initializer is not "
8217 "an integer constant expression");
8218 }
8219
8220 if (last && TREE_CODE (last) != INTEGER_CST)
8221 {
8222 last = c_fully_fold (last, false, NULL);
8223 if (TREE_CODE (last) == INTEGER_CST)
d033409e 8224 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8225 "array index in initializer is not "
8226 "an integer constant expression");
8227 }
8228
3e4093b6 8229 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 8230 error_init (loc, "nonconstant array index in initializer");
3e4093b6 8231 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 8232 error_init (loc, "nonconstant array index in initializer");
3e4093b6 8233 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 8234 error_init (loc, "array index in non-array initializer");
622adc7e 8235 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 8236 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
8237 else if (constructor_max_index
8238 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 8239 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 8240 else
de520661 8241 {
928c19bb
JM
8242 constant_expression_warning (first);
8243 if (last)
8244 constant_expression_warning (last);
3e4093b6 8245 constructor_index = convert (bitsizetype, first);
40d3d530
JR
8246 if (tree_int_cst_lt (constructor_index, first))
8247 {
8248 constructor_index = copy_node (constructor_index);
8249 TREE_OVERFLOW (constructor_index) = 1;
8250 }
665f2503 8251
3e4093b6 8252 if (last)
2bede729 8253 {
3e4093b6
RS
8254 if (tree_int_cst_equal (first, last))
8255 last = 0;
8256 else if (tree_int_cst_lt (last, first))
8257 {
ea58ef42 8258 error_init (loc, "empty index range in initializer");
3e4093b6
RS
8259 last = 0;
8260 }
8261 else
8262 {
8263 last = convert (bitsizetype, last);
8264 if (constructor_max_index != 0
8265 && tree_int_cst_lt (constructor_max_index, last))
8266 {
ea58ef42
MP
8267 error_init (loc, "array index range in initializer exceeds "
8268 "array bounds");
3e4093b6
RS
8269 last = 0;
8270 }
8271 }
2bede729 8272 }
fed3cef0 8273
3e4093b6 8274 designator_depth++;
b06df647 8275 designator_erroneous = 0;
3e4093b6 8276 if (constructor_range_stack || last)
a1e3b3d9 8277 push_range_stack (last, braced_init_obstack);
de520661 8278 }
de520661 8279}
3e4093b6
RS
8280
8281/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 8282
de520661 8283void
f7e4f2e3 8284set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
ea58ef42 8285 struct obstack *braced_init_obstack)
de520661 8286{
0fb96aa4 8287 tree field;
94ba5069 8288
ea58ef42 8289 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
8290 return;
8291
b06df647 8292 designator_erroneous = 1;
3e4093b6 8293
3e636daf 8294 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
94ba5069 8295 {
ea58ef42 8296 error_init (loc, "field name not in record or union initializer");
3e4093b6 8297 return;
94ba5069
RS
8298 }
8299
0fb96aa4 8300 field = lookup_field (constructor_type, fieldname);
8b6a5902 8301
0fb96aa4 8302 if (field == 0)
f7e4f2e3
DM
8303 {
8304 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8305 if (guessed_id)
8306 {
264757fb
DM
8307 gcc_rich_location rich_loc (fieldname_loc);
8308 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
f7e4f2e3
DM
8309 error_at_rich_loc
8310 (&rich_loc,
8311 "%qT has no member named %qE; did you mean %qE?",
8312 constructor_type, fieldname, guessed_id);
8313 }
8314 else
8315 error_at (fieldname_loc, "%qT has no member named %qE",
8316 constructor_type, fieldname);
8317 }
3e4093b6 8318 else
0fb96aa4
JM
8319 do
8320 {
8321 constructor_fields = TREE_VALUE (field);
8322 designator_depth++;
8323 designator_erroneous = 0;
8324 if (constructor_range_stack)
8325 push_range_stack (NULL_TREE, braced_init_obstack);
8326 field = TREE_CHAIN (field);
8327 if (field)
8328 {
ea58ef42 8329 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
8330 return;
8331 }
8332 }
8333 while (field != NULL_TREE);
3e4093b6
RS
8334}
8335\f
8336/* Add a new initializer to the tree of pending initializers. PURPOSE
8337 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
8338 VALUE is the value of that index or field. If ORIGTYPE is not
8339 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
8340
8341 IMPLICIT is true if value comes from pop_init_level (1),
8342 the new initializer has been merged with the existing one
8343 and thus no warnings should be emitted about overriding an
8344 existing initializer. */
de520661 8345
3e4093b6 8346static void
96b40f8d
MP
8347add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8348 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
8349{
8350 struct init_node *p, **q, *r;
8351
8352 q = &constructor_pending_elts;
8353 p = 0;
8354
8355 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8356 {
3e4093b6 8357 while (*q != 0)
91fa3c30 8358 {
3e4093b6
RS
8359 p = *q;
8360 if (tree_int_cst_lt (purpose, p->purpose))
8361 q = &p->left;
8362 else if (tree_int_cst_lt (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 }
91fa3c30 8380 }
de520661 8381 }
3e4093b6 8382 else
de520661 8383 {
3e4093b6 8384 tree bitpos;
400fbf9f 8385
3e4093b6
RS
8386 bitpos = bit_position (purpose);
8387 while (*q != NULL)
8388 {
8389 p = *q;
8390 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8391 q = &p->left;
8392 else if (p->purpose != purpose)
8393 q = &p->right;
8394 else
8395 {
b295aee2
JJ
8396 if (!implicit)
8397 {
8398 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8399 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8400 "initialized field with side-effects "
8401 "overwritten");
b295aee2 8402 else if (warn_override_init)
96b40f8d
MP
8403 warning_init (loc, OPT_Woverride_init,
8404 "initialized field overwritten");
b295aee2 8405 }
3e4093b6 8406 p->value = value;
bbbbb16a 8407 p->origtype = origtype;
3e4093b6
RS
8408 return;
8409 }
8410 }
91fa3c30 8411 }
b71c7f8a 8412
a1e3b3d9
LB
8413 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8414 sizeof (struct init_node));
3e4093b6
RS
8415 r->purpose = purpose;
8416 r->value = value;
bbbbb16a 8417 r->origtype = origtype;
8b6a5902 8418
3e4093b6
RS
8419 *q = r;
8420 r->parent = p;
8421 r->left = 0;
8422 r->right = 0;
8423 r->balance = 0;
b71c7f8a 8424
3e4093b6 8425 while (p)
de520661 8426 {
3e4093b6 8427 struct init_node *s;
665f2503 8428
3e4093b6 8429 if (r == p->left)
2bede729 8430 {
3e4093b6
RS
8431 if (p->balance == 0)
8432 p->balance = -1;
8433 else if (p->balance < 0)
8434 {
8435 if (r->balance < 0)
8436 {
8437 /* L rotation. */
8438 p->left = r->right;
8439 if (p->left)
8440 p->left->parent = p;
8441 r->right = p;
e7b6a0ee 8442
3e4093b6
RS
8443 p->balance = 0;
8444 r->balance = 0;
39bc99c2 8445
3e4093b6
RS
8446 s = p->parent;
8447 p->parent = r;
8448 r->parent = s;
8449 if (s)
8450 {
8451 if (s->left == p)
8452 s->left = r;
8453 else
8454 s->right = r;
8455 }
8456 else
8457 constructor_pending_elts = r;
8458 }
8459 else
8460 {
8461 /* LR rotation. */
8462 struct init_node *t = r->right;
e7b6a0ee 8463
3e4093b6
RS
8464 r->right = t->left;
8465 if (r->right)
8466 r->right->parent = r;
8467 t->left = r;
8468
8469 p->left = t->right;
8470 if (p->left)
8471 p->left->parent = p;
8472 t->right = p;
8473
8474 p->balance = t->balance < 0;
8475 r->balance = -(t->balance > 0);
8476 t->balance = 0;
8477
8478 s = p->parent;
8479 p->parent = t;
8480 r->parent = t;
8481 t->parent = s;
8482 if (s)
8483 {
8484 if (s->left == p)
8485 s->left = t;
8486 else
8487 s->right = t;
8488 }
8489 else
8490 constructor_pending_elts = t;
8491 }
8492 break;
8493 }
8494 else
8495 {
8496 /* p->balance == +1; growth of left side balances the node. */
8497 p->balance = 0;
8498 break;
8499 }
2bede729 8500 }
3e4093b6
RS
8501 else /* r == p->right */
8502 {
8503 if (p->balance == 0)
8504 /* Growth propagation from right side. */
8505 p->balance++;
8506 else if (p->balance > 0)
8507 {
8508 if (r->balance > 0)
8509 {
8510 /* R rotation. */
8511 p->right = r->left;
8512 if (p->right)
8513 p->right->parent = p;
8514 r->left = p;
8515
8516 p->balance = 0;
8517 r->balance = 0;
8518
8519 s = p->parent;
8520 p->parent = r;
8521 r->parent = s;
8522 if (s)
8523 {
8524 if (s->left == p)
8525 s->left = r;
8526 else
8527 s->right = r;
8528 }
8529 else
8530 constructor_pending_elts = r;
8531 }
8532 else /* r->balance == -1 */
8533 {
8534 /* RL rotation */
8535 struct init_node *t = r->left;
8536
8537 r->left = t->right;
8538 if (r->left)
8539 r->left->parent = r;
8540 t->right = r;
8541
8542 p->right = t->left;
8543 if (p->right)
8544 p->right->parent = p;
8545 t->left = p;
8546
8547 r->balance = (t->balance < 0);
8548 p->balance = -(t->balance > 0);
8549 t->balance = 0;
8550
8551 s = p->parent;
8552 p->parent = t;
8553 r->parent = t;
8554 t->parent = s;
8555 if (s)
8556 {
8557 if (s->left == p)
8558 s->left = t;
8559 else
8560 s->right = t;
8561 }
8562 else
8563 constructor_pending_elts = t;
8564 }
8565 break;
8566 }
8567 else
8568 {
8569 /* p->balance == -1; growth of right side balances the node. */
8570 p->balance = 0;
8571 break;
8572 }
8573 }
8574
8575 r = p;
8576 p = p->parent;
8577 }
8578}
8579
8580/* Build AVL tree from a sorted chain. */
8581
8582static void
a1e3b3d9 8583set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8584{
4038c495
GB
8585 unsigned HOST_WIDE_INT ix;
8586 tree index, value;
3e4093b6
RS
8587
8588 if (TREE_CODE (constructor_type) != RECORD_TYPE
8589 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8590 return;
8591
4038c495 8592 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8593 add_pending_init (input_location, index, value, NULL_TREE, true,
8594 braced_init_obstack);
9771b263 8595 constructor_elements = NULL;
3e4093b6
RS
8596 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8597 {
8598 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8599 /* Skip any nameless bit fields at the beginning. */
8600 while (constructor_unfilled_fields != 0
8601 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8602 && DECL_NAME (constructor_unfilled_fields) == 0)
8603 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8604
de520661 8605 }
3e4093b6 8606 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8607 {
3e4093b6
RS
8608 if (TYPE_DOMAIN (constructor_type))
8609 constructor_unfilled_index
8610 = convert (bitsizetype,
8611 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8612 else
8613 constructor_unfilled_index = bitsize_zero_node;
de520661 8614 }
3e4093b6 8615 constructor_incremental = 0;
de520661 8616}
400fbf9f 8617
3e4093b6 8618/* Build AVL tree from a string constant. */
de520661 8619
3e4093b6 8620static void
a1e3b3d9
LB
8621set_nonincremental_init_from_string (tree str,
8622 struct obstack * braced_init_obstack)
de520661 8623{
3e4093b6
RS
8624 tree value, purpose, type;
8625 HOST_WIDE_INT val[2];
8626 const char *p, *end;
8627 int byte, wchar_bytes, charwidth, bitpos;
de520661 8628
366de0ce 8629 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8630
c466b2cd 8631 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6 8632 charwidth = TYPE_PRECISION (char_type_node);
2f1364c2
JJ
8633 gcc_assert ((size_t) wchar_bytes * charwidth
8634 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
3e4093b6
RS
8635 type = TREE_TYPE (constructor_type);
8636 p = TREE_STRING_POINTER (str);
8637 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8638
3e4093b6 8639 for (purpose = bitsize_zero_node;
8824edff
JJ
8640 p < end
8641 && !(constructor_max_index
8642 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8643 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8644 {
3e4093b6 8645 if (wchar_bytes == 1)
ffc5c6a9 8646 {
807e902e
KZ
8647 val[0] = (unsigned char) *p++;
8648 val[1] = 0;
ffc5c6a9
RH
8649 }
8650 else
3e4093b6 8651 {
3e4093b6 8652 val[1] = 0;
807e902e 8653 val[0] = 0;
3e4093b6
RS
8654 for (byte = 0; byte < wchar_bytes; byte++)
8655 {
8656 if (BYTES_BIG_ENDIAN)
8657 bitpos = (wchar_bytes - byte - 1) * charwidth;
8658 else
8659 bitpos = byte * charwidth;
2f1364c2 8660 val[bitpos / HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8661 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8662 << (bitpos % HOST_BITS_PER_WIDE_INT);
8663 }
8664 }
584ef5fe 8665
8df83eae 8666 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8667 {
8668 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8669 if (bitpos < HOST_BITS_PER_WIDE_INT)
8670 {
e3fe09c1 8671 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
3e4093b6 8672 {
aa256c4a 8673 val[0] |= HOST_WIDE_INT_M1U << bitpos;
807e902e 8674 val[1] = -1;
3e4093b6
RS
8675 }
8676 }
8677 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8678 {
807e902e
KZ
8679 if (val[0] < 0)
8680 val[1] = -1;
3e4093b6 8681 }
e3fe09c1 8682 else if (val[1] & (HOST_WIDE_INT_1
3e4093b6 8683 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
aa256c4a 8684 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
3e4093b6 8685 }
ffc5c6a9 8686
807e902e
KZ
8687 value = wide_int_to_tree (type,
8688 wide_int::from_array (val, 2,
8689 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8690 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8691 braced_init_obstack);
9dfcc8db
BH
8692 }
8693
3e4093b6
RS
8694 constructor_incremental = 0;
8695}
de520661 8696
3e4093b6
RS
8697/* Return value of FIELD in pending initializer or zero if the field was
8698 not initialized yet. */
8699
8700static tree
a1e3b3d9 8701find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8702{
8703 struct init_node *p;
8704
8705 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8706 {
3e4093b6
RS
8707 if (constructor_incremental
8708 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8709 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8710
8711 p = constructor_pending_elts;
8712 while (p)
19d76e60 8713 {
3e4093b6
RS
8714 if (tree_int_cst_lt (field, p->purpose))
8715 p = p->left;
8716 else if (tree_int_cst_lt (p->purpose, field))
8717 p = p->right;
8718 else
8719 return p->value;
19d76e60 8720 }
19d76e60 8721 }
3e4093b6 8722 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8723 {
3e4093b6 8724 tree bitpos = bit_position (field);
de520661 8725
3e4093b6
RS
8726 if (constructor_incremental
8727 && (!constructor_unfilled_fields
8728 || tree_int_cst_lt (bitpos,
8729 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8730 set_nonincremental_init (braced_init_obstack);
de520661 8731
3e4093b6
RS
8732 p = constructor_pending_elts;
8733 while (p)
8734 {
8735 if (field == p->purpose)
8736 return p->value;
8737 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8738 p = p->left;
8739 else
8740 p = p->right;
8741 }
8742 }
8743 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8744 {
9771b263
DN
8745 if (!vec_safe_is_empty (constructor_elements)
8746 && (constructor_elements->last ().index == field))
8747 return constructor_elements->last ().value;
de520661 8748 }
3e4093b6 8749 return 0;
de520661
RS
8750}
8751
3e4093b6
RS
8752/* "Output" the next constructor element.
8753 At top level, really output it to assembler code now.
8754 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8755 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8756 TYPE is the data type that the containing data type wants here.
8757 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8758 If VALUE is a string constant, STRICT_STRING is true if it is
8759 unparenthesized or we should not warn here for it being parenthesized.
8760 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8761
3e4093b6
RS
8762 PENDING if non-nil means output pending elements that belong
8763 right after this element. (PENDING is normally 1;
b295aee2
JJ
8764 it is 0 while outputting pending elements, to avoid recursion.)
8765
8766 IMPLICIT is true if value comes from pop_init_level (1),
8767 the new initializer has been merged with the existing one
8768 and thus no warnings should be emitted about overriding an
8769 existing initializer. */
8b6a5902 8770
3e4093b6 8771static void
34cf811f
MP
8772output_init_element (location_t loc, tree value, tree origtype,
8773 bool strict_string, tree type, tree field, int pending,
8774 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8775{
8ce94e44 8776 tree semantic_type = NULL_TREE;
928c19bb
JM
8777 bool maybe_const = true;
8778 bool npc;
4038c495 8779
0a880880 8780 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8781 {
3e4093b6
RS
8782 constructor_erroneous = 1;
8783 return;
8b6a5902 8784 }
46bdb9cf
JM
8785 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8786 && (TREE_CODE (value) == STRING_CST
8787 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8788 && !(TREE_CODE (value) == STRING_CST
8789 && TREE_CODE (type) == ARRAY_TYPE
8790 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8791 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8792 TYPE_MAIN_VARIANT (type)))
c2255bc4 8793 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8794
3e4093b6 8795 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8796 && require_constant_value && pending)
8b6a5902 8797 {
3e4093b6
RS
8798 /* As an extension, allow initializing objects with static storage
8799 duration with compound literals (which are then treated just as
8800 the brace enclosed list they contain). */
4435bb92
MP
8801 if (flag_isoc99)
8802 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8803 "constant");
3e4093b6
RS
8804 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8805 value = DECL_INITIAL (decl);
8b6a5902
JJ
8806 }
8807
928c19bb 8808 npc = null_pointer_constant_p (value);
8ce94e44
JM
8809 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8810 {
8811 semantic_type = TREE_TYPE (value);
8812 value = TREE_OPERAND (value, 0);
8813 }
928c19bb
JM
8814 value = c_fully_fold (value, require_constant_value, &maybe_const);
8815
3e4093b6
RS
8816 if (value == error_mark_node)
8817 constructor_erroneous = 1;
8818 else if (!TREE_CONSTANT (value))
8819 constructor_constant = 0;
ee45a32d
EB
8820 else if (!initializer_constant_valid_p (value,
8821 TREE_TYPE (value),
8822 AGGREGATE_TYPE_P (constructor_type)
8823 && TYPE_REVERSE_STORAGE_ORDER
8824 (constructor_type))
3e636daf 8825 || (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6
RS
8826 && DECL_C_BIT_FIELD (field)
8827 && TREE_CODE (value) != INTEGER_CST))
8828 constructor_simple = 0;
928c19bb
JM
8829 if (!maybe_const)
8830 constructor_nonconst = 1;
3e4093b6 8831
4378d117
MS
8832 /* Digest the initializer and issue any errors about incompatible
8833 types before issuing errors about non-constant initializers. */
8834 tree new_value = value;
8835 if (semantic_type)
8836 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8837 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
8838 require_constant_value);
8839 if (new_value == error_mark_node)
8840 {
8841 constructor_erroneous = 1;
8842 return;
8843 }
8844 if (require_constant_value || require_constant_elements)
8845 constant_expression_warning (new_value);
8846
8847 /* Proceed to check the constness of the original initializer. */
116df786 8848 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8849 {
116df786
RH
8850 if (require_constant_value)
8851 {
ea58ef42 8852 error_init (loc, "initializer element is not constant");
116df786
RH
8853 value = error_mark_node;
8854 }
8855 else if (require_constant_elements)
8337d1db 8856 pedwarn (loc, OPT_Wpedantic,
509c9d60 8857 "initializer element is not computable at load time");
8b6a5902 8858 }
928c19bb
JM
8859 else if (!maybe_const
8860 && (require_constant_value || require_constant_elements))
8337d1db 8861 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8862 "initializer element is not a constant expression");
3e4093b6 8863
81f40b79
ILT
8864 /* Issue -Wc++-compat warnings about initializing a bitfield with
8865 enum type. */
8866 if (warn_cxx_compat
8867 && field != NULL_TREE
8868 && TREE_CODE (field) == FIELD_DECL
8869 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8870 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8871 != TYPE_MAIN_VARIANT (type))
8872 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8873 {
8874 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8875 if (checktype != error_mark_node
8876 && (TYPE_MAIN_VARIANT (checktype)
8877 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8878 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8879 "enum conversion in initialization is invalid in C++");
8880 }
8881
3e4093b6
RS
8882 /* If this field is empty (and not at the end of structure),
8883 don't do anything other than checking the initializer. */
8884 if (field
8885 && (TREE_TYPE (field) == error_mark_node
8886 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8887 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8888 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8889 || DECL_CHAIN (field)))))
3e4093b6
RS
8890 return;
8891
4378d117
MS
8892 /* Finally, set VALUE to the initializer value digested above. */
8893 value = new_value;
8b6a5902 8894
3e4093b6
RS
8895 /* If this element doesn't come next in sequence,
8896 put it on constructor_pending_elts. */
8897 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8898 && (!constructor_incremental
8899 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8900 {
3e4093b6
RS
8901 if (constructor_incremental
8902 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8903 set_nonincremental_init (braced_init_obstack);
3e4093b6 8904
96b40f8d 8905 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8906 braced_init_obstack);
3e4093b6 8907 return;
8b6a5902 8908 }
3e4093b6
RS
8909 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8910 && (!constructor_incremental
8911 || field != constructor_unfilled_fields))
8b6a5902 8912 {
3e4093b6
RS
8913 /* We do this for records but not for unions. In a union,
8914 no matter which field is specified, it can be initialized
8915 right away since it starts at the beginning of the union. */
8916 if (constructor_incremental)
8917 {
8918 if (!constructor_unfilled_fields)
a1e3b3d9 8919 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8920 else
8921 {
8922 tree bitpos, unfillpos;
8923
8924 bitpos = bit_position (field);
8925 unfillpos = bit_position (constructor_unfilled_fields);
8926
8927 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8928 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8929 }
8930 }
8931
96b40f8d 8932 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8933 braced_init_obstack);
3e4093b6 8934 return;
8b6a5902 8935 }
3e4093b6 8936 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8937 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8938 {
b295aee2
JJ
8939 if (!implicit)
8940 {
9771b263 8941 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
755e528f 8942 warning_init (loc, OPT_Woverride_init_side_effects,
b295aee2
JJ
8943 "initialized field with side-effects overwritten");
8944 else if (warn_override_init)
96b40f8d
MP
8945 warning_init (loc, OPT_Woverride_init,
8946 "initialized field overwritten");
b295aee2 8947 }
8b6a5902 8948
3e4093b6 8949 /* We can have just one union field set. */
9771b263 8950 constructor_elements = NULL;
3e4093b6 8951 }
8b6a5902 8952
3e4093b6
RS
8953 /* Otherwise, output this element either to
8954 constructor_elements or to the assembler file. */
8b6a5902 8955
f32682ca 8956 constructor_elt celt = {field, value};
9771b263 8957 vec_safe_push (constructor_elements, celt);
8b6a5902 8958
3e4093b6
RS
8959 /* Advance the variable that indicates sequential elements output. */
8960 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8961 constructor_unfilled_index
db3927fb
AH
8962 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8963 bitsize_one_node);
3e4093b6
RS
8964 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8965 {
8966 constructor_unfilled_fields
910ad8de 8967 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8968
3e4093b6
RS
8969 /* Skip any nameless bit fields. */
8970 while (constructor_unfilled_fields != 0
8971 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8972 && DECL_NAME (constructor_unfilled_fields) == 0)
8973 constructor_unfilled_fields =
910ad8de 8974 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8975 }
8976 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8977 constructor_unfilled_fields = 0;
de520661 8978
3e4093b6
RS
8979 /* Now output any pending elements which have become next. */
8980 if (pending)
a1e3b3d9 8981 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8982}
8b6a5902 8983
3e4093b6
RS
8984/* Output any pending elements which have become next.
8985 As we output elements, constructor_unfilled_{fields,index}
8986 advances, which may cause other elements to become next;
8987 if so, they too are output.
8b6a5902 8988
3e4093b6
RS
8989 If ALL is 0, we return when there are
8990 no more pending elements to output now.
665f2503 8991
3e4093b6
RS
8992 If ALL is 1, we output space as necessary so that
8993 we can output all the pending elements. */
3e4093b6 8994static void
a1e3b3d9 8995output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8996{
8997 struct init_node *elt = constructor_pending_elts;
8998 tree next;
de520661 8999
3e4093b6
RS
9000 retry:
9001
ba228239 9002 /* Look through the whole pending tree.
3e4093b6
RS
9003 If we find an element that should be output now,
9004 output it. Otherwise, set NEXT to the element
9005 that comes first among those still pending. */
9006
9007 next = 0;
9008 while (elt)
9009 {
9010 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 9011 {
3e4093b6
RS
9012 if (tree_int_cst_equal (elt->purpose,
9013 constructor_unfilled_index))
34cf811f
MP
9014 output_init_element (input_location, elt->value, elt->origtype,
9015 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
9016 constructor_unfilled_index, 0, false,
9017 braced_init_obstack);
3e4093b6
RS
9018 else if (tree_int_cst_lt (constructor_unfilled_index,
9019 elt->purpose))
8b6a5902 9020 {
3e4093b6
RS
9021 /* Advance to the next smaller node. */
9022 if (elt->left)
9023 elt = elt->left;
9024 else
9025 {
9026 /* We have reached the smallest node bigger than the
9027 current unfilled index. Fill the space first. */
9028 next = elt->purpose;
9029 break;
9030 }
8b6a5902 9031 }
ce662d4c
JJ
9032 else
9033 {
3e4093b6
RS
9034 /* Advance to the next bigger node. */
9035 if (elt->right)
9036 elt = elt->right;
9037 else
ce662d4c 9038 {
3e4093b6
RS
9039 /* We have reached the biggest node in a subtree. Find
9040 the parent of it, which is the next bigger node. */
9041 while (elt->parent && elt->parent->right == elt)
9042 elt = elt->parent;
9043 elt = elt->parent;
9044 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9045 elt->purpose))
9046 {
9047 next = elt->purpose;
9048 break;
9049 }
ce662d4c
JJ
9050 }
9051 }
8b6a5902 9052 }
3e636daf 9053 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
9054 {
9055 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 9056
3e4093b6
RS
9057 /* If the current record is complete we are done. */
9058 if (constructor_unfilled_fields == 0)
9059 break;
de520661 9060
3e4093b6
RS
9061 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9062 elt_bitpos = bit_position (elt->purpose);
9063 /* We can't compare fields here because there might be empty
9064 fields in between. */
9065 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9066 {
9067 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
9068 output_init_element (input_location, elt->value, elt->origtype,
9069 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
9070 elt->purpose, 0, false,
9071 braced_init_obstack);
3e4093b6
RS
9072 }
9073 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9074 {
9075 /* Advance to the next smaller node. */
9076 if (elt->left)
9077 elt = elt->left;
9078 else
9079 {
9080 /* We have reached the smallest node bigger than the
9081 current unfilled field. Fill the space first. */
9082 next = elt->purpose;
9083 break;
9084 }
9085 }
9086 else
9087 {
9088 /* Advance to the next bigger node. */
9089 if (elt->right)
9090 elt = elt->right;
9091 else
9092 {
9093 /* We have reached the biggest node in a subtree. Find
9094 the parent of it, which is the next bigger node. */
9095 while (elt->parent && elt->parent->right == elt)
9096 elt = elt->parent;
9097 elt = elt->parent;
9098 if (elt
9099 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9100 bit_position (elt->purpose))))
9101 {
9102 next = elt->purpose;
9103 break;
9104 }
9105 }
9106 }
9107 }
9108 }
de520661 9109
3e4093b6
RS
9110 /* Ordinarily return, but not if we want to output all
9111 and there are elements left. */
3f75a254 9112 if (!(all && next != 0))
e5cfb88f
RK
9113 return;
9114
3e4093b6
RS
9115 /* If it's not incremental, just skip over the gap, so that after
9116 jumping to retry we will output the next successive element. */
3e636daf 9117 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
9118 constructor_unfilled_fields = next;
9119 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9120 constructor_unfilled_index = next;
de520661 9121
3e4093b6
RS
9122 /* ELT now points to the node in the pending tree with the next
9123 initializer to output. */
9124 goto retry;
de520661
RS
9125}
9126\f
3e4093b6
RS
9127/* Add one non-braced element to the current constructor level.
9128 This adjusts the current position within the constructor's type.
9129 This may also start or terminate implicit levels
9130 to handle a partly-braced initializer.
e5e809f4 9131
3e4093b6 9132 Once this has found the correct level for the new element,
b295aee2
JJ
9133 it calls output_init_element.
9134
9135 IMPLICIT is true if value comes from pop_init_level (1),
9136 the new initializer has been merged with the existing one
9137 and thus no warnings should be emitted about overriding an
9138 existing initializer. */
3e4093b6
RS
9139
9140void
34cf811f 9141process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 9142 struct obstack * braced_init_obstack)
e5e809f4 9143{
916c5919
JM
9144 tree orig_value = value.value;
9145 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
9146 bool strict_string = value.original_code == STRING_CST;
340baef7 9147 bool was_designated = designator_depth != 0;
e5e809f4 9148
3e4093b6 9149 designator_depth = 0;
b06df647 9150 designator_erroneous = 0;
e5e809f4 9151
9bac5cbb
G
9152 if (!implicit && value.value && !integer_zerop (value.value))
9153 constructor_zeroinit = 0;
9154
3e4093b6
RS
9155 /* Handle superfluous braces around string cst as in
9156 char x[] = {"foo"}; */
9157 if (string_flag
9158 && constructor_type
340baef7 9159 && !was_designated
3e4093b6 9160 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 9161 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 9162 && integer_zerop (constructor_unfilled_index))
e5e809f4 9163 {
916c5919 9164 if (constructor_stack->replacement_value.value)
ea58ef42 9165 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
9166 constructor_stack->replacement_value = value;
9167 return;
e5e809f4 9168 }
8b6a5902 9169
916c5919 9170 if (constructor_stack->replacement_value.value != 0)
3e4093b6 9171 {
ea58ef42 9172 error_init (loc, "excess elements in struct initializer");
3e4093b6 9173 return;
e5e809f4
JL
9174 }
9175
3e4093b6
RS
9176 /* Ignore elements of a brace group if it is entirely superfluous
9177 and has already been diagnosed. */
9178 if (constructor_type == 0)
9179 return;
e5e809f4 9180
976d5a22
TT
9181 if (!implicit && warn_designated_init && !was_designated
9182 && TREE_CODE (constructor_type) == RECORD_TYPE
9183 && lookup_attribute ("designated_init",
9184 TYPE_ATTRIBUTES (constructor_type)))
9185 warning_init (loc,
9186 OPT_Wdesignated_init,
9187 "positional initialization of field "
9188 "in %<struct%> declared with %<designated_init%> attribute");
9189
3e4093b6
RS
9190 /* If we've exhausted any levels that didn't have braces,
9191 pop them now. */
9192 while (constructor_stack->implicit)
9193 {
3e636daf 9194 if (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 9195 && constructor_fields == 0)
ea58ef42
MP
9196 process_init_element (loc,
9197 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 9198 true, braced_init_obstack);
53650abe 9199 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
31521951 9200 || VECTOR_TYPE_P (constructor_type))
8824edff
JJ
9201 && constructor_max_index
9202 && tree_int_cst_lt (constructor_max_index,
9203 constructor_index))
ea58ef42
MP
9204 process_init_element (loc,
9205 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 9206 true, braced_init_obstack);
3e4093b6
RS
9207 else
9208 break;
9209 }
e5e809f4 9210
3e4093b6
RS
9211 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9212 if (constructor_range_stack)
e5e809f4 9213 {
3e4093b6
RS
9214 /* If value is a compound literal and we'll be just using its
9215 content, don't put it into a SAVE_EXPR. */
916c5919 9216 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
c3e38a03 9217 || !require_constant_value)
8ce94e44
JM
9218 {
9219 tree semantic_type = NULL_TREE;
9220 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9221 {
9222 semantic_type = TREE_TYPE (value.value);
9223 value.value = TREE_OPERAND (value.value, 0);
9224 }
9225 value.value = c_save_expr (value.value);
9226 if (semantic_type)
9227 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9228 value.value);
9229 }
3e4093b6 9230 }
e5e809f4 9231
3e4093b6
RS
9232 while (1)
9233 {
9234 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 9235 {
3e4093b6
RS
9236 tree fieldtype;
9237 enum tree_code fieldcode;
e5e809f4 9238
3e4093b6
RS
9239 if (constructor_fields == 0)
9240 {
ea58ef42 9241 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
9242 break;
9243 }
e5e809f4 9244
3e4093b6
RS
9245 fieldtype = TREE_TYPE (constructor_fields);
9246 if (fieldtype != error_mark_node)
9247 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9248 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9249
3e4093b6
RS
9250 /* Error for non-static initialization of a flexible array member. */
9251 if (fieldcode == ARRAY_TYPE
9252 && !require_constant_value
9253 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 9254 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 9255 {
ea58ef42
MP
9256 error_init (loc, "non-static initialization of a flexible "
9257 "array member");
3e4093b6
RS
9258 break;
9259 }
e5e809f4 9260
2cc901dc
MP
9261 /* Error for initialization of a flexible array member with
9262 a string constant if the structure is in an array. E.g.:
9263 struct S { int x; char y[]; };
9264 struct S s[] = { { 1, "foo" } };
9265 is invalid. */
9266 if (string_flag
9267 && fieldcode == ARRAY_TYPE
9268 && constructor_depth > 1
9269 && TYPE_SIZE (fieldtype) == NULL_TREE
9270 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9271 {
9272 bool in_array_p = false;
9273 for (struct constructor_stack *p = constructor_stack;
9274 p && p->type; p = p->next)
9275 if (TREE_CODE (p->type) == ARRAY_TYPE)
9276 {
9277 in_array_p = true;
9278 break;
9279 }
9280 if (in_array_p)
9281 {
9282 error_init (loc, "initialization of flexible array "
9283 "member in a nested context");
9284 break;
9285 }
9286 }
9287
3e4093b6 9288 /* Accept a string constant to initialize a subarray. */
916c5919 9289 if (value.value != 0
3e4093b6 9290 && fieldcode == ARRAY_TYPE
197463ae 9291 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9292 && string_flag)
916c5919 9293 value.value = orig_value;
3e4093b6
RS
9294 /* Otherwise, if we have come to a subaggregate,
9295 and we don't have an element of its type, push into it. */
0953878d 9296 else if (value.value != 0
916c5919
JM
9297 && value.value != error_mark_node
9298 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9299 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9300 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9301 {
ea58ef42 9302 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9303 continue;
9304 }
e5e809f4 9305
916c5919 9306 if (value.value)
3e4093b6
RS
9307 {
9308 push_member_name (constructor_fields);
34cf811f 9309 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9310 strict_string, fieldtype,
a1e3b3d9
LB
9311 constructor_fields, 1, implicit,
9312 braced_init_obstack);
3e4093b6 9313 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9314 }
9315 else
3e4093b6
RS
9316 /* Do the bookkeeping for an element that was
9317 directly output as a constructor. */
e5e809f4 9318 {
3e4093b6
RS
9319 /* For a record, keep track of end position of last field. */
9320 if (DECL_SIZE (constructor_fields))
c22cacf3 9321 constructor_bit_index
db3927fb
AH
9322 = size_binop_loc (input_location, PLUS_EXPR,
9323 bit_position (constructor_fields),
9324 DECL_SIZE (constructor_fields));
3e4093b6
RS
9325
9326 /* If the current field was the first one not yet written out,
9327 it isn't now, so update. */
9328 if (constructor_unfilled_fields == constructor_fields)
9329 {
910ad8de 9330 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9331 /* Skip any nameless bit fields. */
9332 while (constructor_unfilled_fields != 0
9333 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9334 && DECL_NAME (constructor_unfilled_fields) == 0)
9335 constructor_unfilled_fields =
910ad8de 9336 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 9337 }
e5e809f4 9338 }
3e4093b6 9339
910ad8de 9340 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9341 /* Skip any nameless bit fields at the beginning. */
9342 while (constructor_fields != 0
9343 && DECL_C_BIT_FIELD (constructor_fields)
9344 && DECL_NAME (constructor_fields) == 0)
910ad8de 9345 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9346 }
3e4093b6 9347 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 9348 {
3e4093b6
RS
9349 tree fieldtype;
9350 enum tree_code fieldcode;
e5e809f4 9351
3e4093b6
RS
9352 if (constructor_fields == 0)
9353 {
d033409e 9354 pedwarn_init (loc, 0,
509c9d60 9355 "excess elements in union initializer");
3e4093b6
RS
9356 break;
9357 }
e5e809f4 9358
3e4093b6
RS
9359 fieldtype = TREE_TYPE (constructor_fields);
9360 if (fieldtype != error_mark_node)
9361 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9362 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9363
3e4093b6
RS
9364 /* Warn that traditional C rejects initialization of unions.
9365 We skip the warning if the value is zero. This is done
9366 under the assumption that the zero initializer in user
9367 code appears conditioned on e.g. __STDC__ to avoid
9368 "missing initializer" warnings and relies on default
9369 initialization to zero in the traditional C case.
9370 We also skip the warning if the initializer is designated,
9371 again on the assumption that this must be conditional on
9372 __STDC__ anyway (and we've already complained about the
9373 member-designator already). */
8400e75e 9374 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
9375 && !(value.value && (integer_zerop (value.value)
9376 || real_zerop (value.value))))
3176a0c2
DD
9377 warning (OPT_Wtraditional, "traditional C rejects initialization "
9378 "of unions");
e5e809f4 9379
3e4093b6 9380 /* Accept a string constant to initialize a subarray. */
916c5919 9381 if (value.value != 0
3e4093b6 9382 && fieldcode == ARRAY_TYPE
197463ae 9383 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9384 && string_flag)
916c5919 9385 value.value = orig_value;
3e4093b6
RS
9386 /* Otherwise, if we have come to a subaggregate,
9387 and we don't have an element of its type, push into it. */
0953878d 9388 else if (value.value != 0
916c5919
JM
9389 && value.value != error_mark_node
9390 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9391 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9392 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9393 {
ea58ef42 9394 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9395 continue;
9396 }
e5e809f4 9397
916c5919 9398 if (value.value)
3e4093b6
RS
9399 {
9400 push_member_name (constructor_fields);
34cf811f 9401 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9402 strict_string, fieldtype,
a1e3b3d9
LB
9403 constructor_fields, 1, implicit,
9404 braced_init_obstack);
3e4093b6 9405 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9406 }
9407 else
3e4093b6
RS
9408 /* Do the bookkeeping for an element that was
9409 directly output as a constructor. */
e5e809f4 9410 {
3e4093b6 9411 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 9412 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9413 }
e5e809f4 9414
3e4093b6
RS
9415 constructor_fields = 0;
9416 }
9417 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9418 {
9419 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9420 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 9421
3e4093b6 9422 /* Accept a string constant to initialize a subarray. */
916c5919 9423 if (value.value != 0
3e4093b6 9424 && eltcode == ARRAY_TYPE
197463ae 9425 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 9426 && string_flag)
916c5919 9427 value.value = orig_value;
3e4093b6
RS
9428 /* Otherwise, if we have come to a subaggregate,
9429 and we don't have an element of its type, push into it. */
0953878d 9430 else if (value.value != 0
916c5919
JM
9431 && value.value != error_mark_node
9432 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9433 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9434 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9435 {
ea58ef42 9436 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9437 continue;
9438 }
8b6a5902 9439
3e4093b6
RS
9440 if (constructor_max_index != 0
9441 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9442 || integer_all_onesp (constructor_max_index)))
9443 {
d033409e 9444 pedwarn_init (loc, 0,
509c9d60 9445 "excess elements in array initializer");
3e4093b6
RS
9446 break;
9447 }
8b6a5902 9448
3e4093b6 9449 /* Now output the actual element. */
916c5919 9450 if (value.value)
3e4093b6 9451 {
ae7e9ddd 9452 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9453 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9454 strict_string, elttype,
a1e3b3d9
LB
9455 constructor_index, 1, implicit,
9456 braced_init_obstack);
3e4093b6
RS
9457 RESTORE_SPELLING_DEPTH (constructor_depth);
9458 }
2f6e4e97 9459
3e4093b6 9460 constructor_index
db3927fb
AH
9461 = size_binop_loc (input_location, PLUS_EXPR,
9462 constructor_index, bitsize_one_node);
8b6a5902 9463
916c5919 9464 if (!value.value)
3e4093b6
RS
9465 /* If we are doing the bookkeeping for an element that was
9466 directly output as a constructor, we must update
9467 constructor_unfilled_index. */
9468 constructor_unfilled_index = constructor_index;
9469 }
31521951 9470 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
9471 {
9472 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9473
c22cacf3
MS
9474 /* Do a basic check of initializer size. Note that vectors
9475 always have a fixed size derived from their type. */
3e4093b6
RS
9476 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9477 {
d033409e 9478 pedwarn_init (loc, 0,
509c9d60 9479 "excess elements in vector initializer");
3e4093b6
RS
9480 break;
9481 }
8b6a5902 9482
3e4093b6 9483 /* Now output the actual element. */
916c5919 9484 if (value.value)
53650abe
AP
9485 {
9486 if (TREE_CODE (value.value) == VECTOR_CST)
9487 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9488 output_init_element (loc, value.value, value.original_type,
53650abe 9489 strict_string, elttype,
a1e3b3d9
LB
9490 constructor_index, 1, implicit,
9491 braced_init_obstack);
53650abe 9492 }
8b6a5902 9493
3e4093b6 9494 constructor_index
db3927fb
AH
9495 = size_binop_loc (input_location,
9496 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9497
916c5919 9498 if (!value.value)
3e4093b6
RS
9499 /* If we are doing the bookkeeping for an element that was
9500 directly output as a constructor, we must update
9501 constructor_unfilled_index. */
9502 constructor_unfilled_index = constructor_index;
9503 }
8b6a5902 9504
3e4093b6
RS
9505 /* Handle the sole element allowed in a braced initializer
9506 for a scalar variable. */
b4519d39
SB
9507 else if (constructor_type != error_mark_node
9508 && constructor_fields == 0)
8b6a5902 9509 {
d033409e 9510 pedwarn_init (loc, 0,
509c9d60 9511 "excess elements in scalar initializer");
3e4093b6 9512 break;
8b6a5902
JJ
9513 }
9514 else
9515 {
916c5919 9516 if (value.value)
34cf811f 9517 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9518 strict_string, constructor_type,
a1e3b3d9
LB
9519 NULL_TREE, 1, implicit,
9520 braced_init_obstack);
3e4093b6 9521 constructor_fields = 0;
8b6a5902
JJ
9522 }
9523
3e4093b6
RS
9524 /* Handle range initializers either at this level or anywhere higher
9525 in the designator stack. */
9526 if (constructor_range_stack)
8b6a5902 9527 {
3e4093b6
RS
9528 struct constructor_range_stack *p, *range_stack;
9529 int finish = 0;
9530
9531 range_stack = constructor_range_stack;
9532 constructor_range_stack = 0;
9533 while (constructor_stack != range_stack->stack)
8b6a5902 9534 {
366de0ce 9535 gcc_assert (constructor_stack->implicit);
34cf811f 9536 process_init_element (loc,
ea58ef42
MP
9537 pop_init_level (loc, 1,
9538 braced_init_obstack),
a1e3b3d9 9539 true, braced_init_obstack);
8b6a5902 9540 }
3e4093b6
RS
9541 for (p = range_stack;
9542 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9543 p = p->prev)
8b6a5902 9544 {
366de0ce 9545 gcc_assert (constructor_stack->implicit);
34cf811f 9546 process_init_element (loc,
ea58ef42
MP
9547 pop_init_level (loc, 1,
9548 braced_init_obstack),
a1e3b3d9 9549 true, braced_init_obstack);
8b6a5902 9550 }
3e4093b6 9551
db3927fb
AH
9552 p->index = size_binop_loc (input_location,
9553 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9554 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9555 finish = 1;
9556
9557 while (1)
9558 {
9559 constructor_index = p->index;
9560 constructor_fields = p->fields;
9561 if (finish && p->range_end && p->index == p->range_start)
9562 {
9563 finish = 0;
9564 p->prev = 0;
9565 }
9566 p = p->next;
9567 if (!p)
9568 break;
16595a1f 9569 finish_implicit_inits (loc, braced_init_obstack);
ea58ef42 9570 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9571 p->stack = constructor_stack;
9572 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9573 p->index = p->range_start;
9574 }
9575
9576 if (!finish)
9577 constructor_range_stack = range_stack;
9578 continue;
8b6a5902
JJ
9579 }
9580
3e4093b6 9581 break;
8b6a5902
JJ
9582 }
9583
3e4093b6
RS
9584 constructor_range_stack = 0;
9585}
9586\f
9f0e2d86
ZW
9587/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9588 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9589 an ASM_EXPR node). */
3e4093b6 9590tree
9f0e2d86 9591build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9592{
6de9cd9a
DN
9593 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9594 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9595 return add_stmt (args);
8b6a5902
JJ
9596}
9597
9f0e2d86
ZW
9598/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9599 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9600 SIMPLE indicates whether there was anything at all after the
9601 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9602 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9603tree
c2255bc4 9604build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9605 tree clobbers, tree labels, bool simple)
e5e809f4 9606{
3e4093b6 9607 tree tail;
9f0e2d86 9608 tree args;
6de9cd9a
DN
9609 int i;
9610 const char *constraint;
74f0c611 9611 const char **oconstraints;
6de9cd9a 9612 bool allows_mem, allows_reg, is_inout;
74f0c611 9613 int ninputs, noutputs;
6de9cd9a
DN
9614
9615 ninputs = list_length (inputs);
9616 noutputs = list_length (outputs);
74f0c611
RH
9617 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9618
1c384bf1 9619 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9620
6de9cd9a
DN
9621 /* Remove output conversions that change the type but not the mode. */
9622 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9623 {
3e4093b6 9624 tree output = TREE_VALUE (tail);
74f0c611 9625
eadd3d0d
JJ
9626 output = c_fully_fold (output, false, NULL);
9627
74f0c611
RH
9628 /* ??? Really, this should not be here. Users should be using a
9629 proper lvalue, dammit. But there's a long history of using casts
9630 in the output operands. In cases like longlong.h, this becomes a
9631 primitive form of typechecking -- if the cast can be removed, then
9632 the output operand had a type of the proper width; otherwise we'll
9633 get an error. Gross, but ... */
3e4093b6 9634 STRIP_NOPS (output);
74f0c611 9635
7bd11157 9636 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9637 output = error_mark_node;
8b6a5902 9638
5544530a
PB
9639 if (output != error_mark_node
9640 && (TREE_READONLY (output)
9641 || TYPE_READONLY (TREE_TYPE (output))
3e636daf 9642 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
5544530a 9643 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9644 readonly_error (loc, output, lv_asm);
5544530a 9645
6de9cd9a 9646 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9647 oconstraints[i] = constraint;
9648
9649 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9650 &allows_mem, &allows_reg, &is_inout))
9651 {
9652 /* If the operand is going to end up in memory,
9653 mark it addressable. */
9654 if (!allows_reg && !c_mark_addressable (output))
9655 output = error_mark_node;
bae5cddf
JJ
9656 if (!(!allows_reg && allows_mem)
9657 && output != error_mark_node
9658 && VOID_TYPE_P (TREE_TYPE (output)))
9659 {
9660 error_at (loc, "invalid use of void expression");
9661 output = error_mark_node;
9662 }
74f0c611
RH
9663 }
9664 else
c22cacf3 9665 output = error_mark_node;
3e4093b6 9666
74f0c611 9667 TREE_VALUE (tail) = output;
8b6a5902 9668 }
3e4093b6 9669
74f0c611
RH
9670 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9671 {
9672 tree input;
9673
9674 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9675 input = TREE_VALUE (tail);
9676
74f0c611
RH
9677 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9678 oconstraints, &allows_mem, &allows_reg))
9679 {
9680 /* If the operand is going to end up in memory,
9681 mark it addressable. */
b4c33883
AP
9682 if (!allows_reg && allows_mem)
9683 {
eadd3d0d
JJ
9684 input = c_fully_fold (input, false, NULL);
9685
b4c33883
AP
9686 /* Strip the nops as we allow this case. FIXME, this really
9687 should be rejected or made deprecated. */
9688 STRIP_NOPS (input);
9689 if (!c_mark_addressable (input))
9690 input = error_mark_node;
bae5cddf 9691 }
eadd3d0d 9692 else
bae5cddf 9693 {
eadd3d0d
JJ
9694 struct c_expr expr;
9695 memset (&expr, 0, sizeof (expr));
9696 expr.value = input;
267bac10 9697 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9698 input = c_fully_fold (expr.value, false, NULL);
9699
9700 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9701 {
9702 error_at (loc, "invalid use of void expression");
9703 input = error_mark_node;
9704 }
bae5cddf 9705 }
74f0c611
RH
9706 }
9707 else
9708 input = error_mark_node;
9709
9710 TREE_VALUE (tail) = input;
9711 }
3e4093b6 9712
1c384bf1
RH
9713 /* ASMs with labels cannot have outputs. This should have been
9714 enforced by the parser. */
9715 gcc_assert (outputs == NULL || labels == NULL);
9716
9717 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9718
5544530a
PB
9719 /* asm statements without outputs, including simple ones, are treated
9720 as volatile. */
9721 ASM_INPUT_P (args) = simple;
9722 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9723
9f0e2d86 9724 return args;
e5e809f4 9725}
3e4093b6 9726\f
c2255bc4
AH
9727/* Generate a goto statement to LABEL. LOC is the location of the
9728 GOTO. */
506e2710
RH
9729
9730tree
c2255bc4 9731c_finish_goto_label (location_t loc, tree label)
506e2710 9732{
e1b7793c 9733 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9734 if (!decl)
9735 return NULL_TREE;
506e2710 9736 TREE_USED (decl) = 1;
c2255bc4
AH
9737 {
9738 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9739 SET_EXPR_LOCATION (t, loc);
9740 return add_stmt (t);
9741 }
506e2710
RH
9742}
9743
c2255bc4
AH
9744/* Generate a computed goto statement to EXPR. LOC is the location of
9745 the GOTO. */
506e2710
RH
9746
9747tree
c2255bc4 9748c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9749{
c2255bc4 9750 tree t;
c1771a20 9751 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9752 expr = c_fully_fold (expr, false, NULL);
506e2710 9753 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9754 t = build1 (GOTO_EXPR, void_type_node, expr);
9755 SET_EXPR_LOCATION (t, loc);
9756 return add_stmt (t);
506e2710
RH
9757}
9758
5088b058 9759/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9760 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9761 the location of the return statement, or the location of the expression,
9762 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9763 is the original type of RETVAL. */
de520661 9764
506e2710 9765tree
c2255bc4 9766c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9767{
0c9b182b
JJ
9768 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9769 bool no_warning = false;
928c19bb 9770 bool npc = false;
36536d79 9771 size_t rank = 0;
3e4093b6 9772
de8ddd5f
MP
9773 /* Use the expansion point to handle cases such as returning NULL
9774 in a function returning void. */
9775 source_location xloc = expansion_point_location_if_in_system_header (loc);
9776
3e4093b6 9777 if (TREE_THIS_VOLATILE (current_function_decl))
de8ddd5f 9778 warning_at (xloc, 0,
c2255bc4 9779 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9780
b72271b9 9781 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9782 {
9783 /* Array notations are allowed in a return statement if it is inside a
9784 built-in array notation reduction function. */
9785 if (!find_rank (loc, retval, retval, false, &rank))
9786 return error_mark_node;
9787 if (rank >= 1)
9788 {
9789 error_at (loc, "array notation expression cannot be used as a "
9790 "return value");
9791 return error_mark_node;
9792 }
9793 }
3af9c5e9 9794 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9795 {
9796 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9797 "allowed");
9798 return error_mark_node;
9799 }
928c19bb
JM
9800 if (retval)
9801 {
8ce94e44 9802 tree semantic_type = NULL_TREE;
928c19bb 9803 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9804 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9805 {
9806 semantic_type = TREE_TYPE (retval);
9807 retval = TREE_OPERAND (retval, 0);
9808 }
928c19bb 9809 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9810 if (semantic_type)
9811 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9812 }
9813
3e4093b6 9814 if (!retval)
de520661 9815 {
3e4093b6
RS
9816 current_function_returns_null = 1;
9817 if ((warn_return_type || flag_isoc99)
9818 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9819 {
94c40e19 9820 bool warned_here;
35aff4fb 9821 if (flag_isoc99)
94c40e19
DM
9822 warned_here = pedwarn
9823 (loc, 0,
9824 "%<return%> with no value, in function returning non-void");
35aff4fb 9825 else
94c40e19
DM
9826 warned_here = warning_at
9827 (loc, OPT_Wreturn_type,
9828 "%<return%> with no value, in function returning non-void");
0c9b182b 9829 no_warning = true;
94c40e19
DM
9830 if (warned_here)
9831 inform (DECL_SOURCE_LOCATION (current_function_decl),
9832 "declared here");
0c9b182b 9833 }
400fbf9f 9834 }
3e4093b6 9835 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9836 {
3e4093b6 9837 current_function_returns_null = 1;
94c40e19 9838 bool warned_here;
2397c575 9839 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
94c40e19
DM
9840 warned_here = pedwarn
9841 (xloc, 0,
9842 "%<return%> with a value, in function returning void");
b8698a0f 9843 else
94c40e19
DM
9844 warned_here = pedwarn
9845 (xloc, OPT_Wpedantic, "ISO C forbids "
9846 "%<return%> with expression, in function returning void");
9847 if (warned_here)
9848 inform (DECL_SOURCE_LOCATION (current_function_decl),
9849 "declared here");
de520661 9850 }
3e4093b6 9851 else
de520661 9852 {
68fca595
MP
9853 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9854 retval, origtype, ic_return,
c2255bc4 9855 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9856 tree res = DECL_RESULT (current_function_decl);
9857 tree inner;
9feb29df 9858 bool save;
3e4093b6
RS
9859
9860 current_function_returns_value = 1;
9861 if (t == error_mark_node)
506e2710 9862 return NULL_TREE;
3e4093b6 9863
9feb29df
JJ
9864 save = in_late_binary_op;
9865 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
9866 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9867 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9868 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9869 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9870 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9feb29df 9871 in_late_binary_op = true;
3e4093b6 9872 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9873 in_late_binary_op = save;
3e4093b6
RS
9874
9875 /* Strip any conversions, additions, and subtractions, and see if
9876 we are returning the address of a local variable. Warn if so. */
9877 while (1)
8b6a5902 9878 {
3e4093b6 9879 switch (TREE_CODE (inner))
8b6a5902 9880 {
849421a3
JJ
9881 CASE_CONVERT:
9882 case NON_LVALUE_EXPR:
3e4093b6 9883 case PLUS_EXPR:
849421a3 9884 case POINTER_PLUS_EXPR:
3e4093b6
RS
9885 inner = TREE_OPERAND (inner, 0);
9886 continue;
9887
9888 case MINUS_EXPR:
9889 /* If the second operand of the MINUS_EXPR has a pointer
9890 type (or is converted from it), this may be valid, so
9891 don't give a warning. */
9892 {
9893 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9894
3f75a254 9895 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9896 && (CONVERT_EXPR_P (op1)
9897 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9898 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9899
3e4093b6
RS
9900 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9901 break;
8b6a5902 9902
3e4093b6
RS
9903 inner = TREE_OPERAND (inner, 0);
9904 continue;
9905 }
400fbf9f 9906
3e4093b6
RS
9907 case ADDR_EXPR:
9908 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9909
6615c446 9910 while (REFERENCE_CLASS_P (inner)
22d03525 9911 && !INDIRECT_REF_P (inner))
3e4093b6 9912 inner = TREE_OPERAND (inner, 0);
8b6a5902 9913
a2f1f4c3 9914 if (DECL_P (inner)
3f75a254
JM
9915 && !DECL_EXTERNAL (inner)
9916 && !TREE_STATIC (inner)
3e4093b6 9917 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9918 {
9919 if (TREE_CODE (inner) == LABEL_DECL)
9920 warning_at (loc, OPT_Wreturn_local_addr,
9921 "function returns address of label");
9922 else
b4dfdc11
MG
9923 {
9924 warning_at (loc, OPT_Wreturn_local_addr,
9925 "function returns address of local variable");
9926 tree zero = build_zero_cst (TREE_TYPE (res));
9927 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9928 }
19fc9faa 9929 }
3e4093b6 9930 break;
8b6a5902 9931
3e4093b6
RS
9932 default:
9933 break;
9934 }
de520661 9935
3e4093b6
RS
9936 break;
9937 }
9938
53fb4de3 9939 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9940 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9941
9942 if (warn_sequence_point)
9943 verify_sequence_points (retval);
de520661 9944 }
8b6a5902 9945
c2255bc4 9946 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9947 TREE_NO_WARNING (ret_stmt) |= no_warning;
9948 return add_stmt (ret_stmt);
de520661 9949}
3e4093b6
RS
9950\f
9951struct c_switch {
604f5adf
ILT
9952 /* The SWITCH_EXPR being built. */
9953 tree switch_expr;
a6c0a76c 9954
89dbed81 9955 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9956 default conversion is applied. */
9957 tree orig_type;
9958
3e4093b6
RS
9959 /* A splay-tree mapping the low element of a case range to the high
9960 element, or NULL_TREE if there is no high element. Used to
9961 determine whether or not a new case label duplicates an old case
9962 label. We need a tree, rather than simply a hash table, because
9963 of the GNU case range extension. */
9964 splay_tree cases;
a6c0a76c 9965
e1b7793c
ILT
9966 /* The bindings at the point of the switch. This is used for
9967 warnings crossing decls when branching to a case label. */
9968 struct c_spot_bindings *bindings;
187230a7 9969
3e4093b6
RS
9970 /* The next node on the stack. */
9971 struct c_switch *next;
b155cfd9
MP
9972
9973 /* Remember whether the controlling expression had boolean type
9974 before integer promotions for the sake of -Wswitch-bool. */
9975 bool bool_cond_p;
9976
9977 /* Remember whether there was a case value that is outside the
9978 range of the ORIG_TYPE. */
9979 bool outside_range_p;
3e4093b6 9980};
400fbf9f 9981
3e4093b6
RS
9982/* A stack of the currently active switch statements. The innermost
9983 switch statement is on the top of the stack. There is no need to
9984 mark the stack for garbage collection because it is only active
9985 during the processing of the body of a function, and we never
9986 collect at that point. */
de520661 9987
506e2710 9988struct c_switch *c_switch_stack;
de520661 9989
3e4093b6 9990/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9991 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef 9992 SWITCH_COND_LOC is the location of the switch's condition.
b155cfd9 9993 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
de520661 9994
3e4093b6 9995tree
c2255bc4
AH
9996c_start_case (location_t switch_loc,
9997 location_t switch_cond_loc,
fedfecef 9998 tree exp, bool explicit_cast_p)
de520661 9999{
c58e8676 10000 tree orig_type = error_mark_node;
b155cfd9 10001 bool bool_cond_p = false;
3e4093b6 10002 struct c_switch *cs;
2f6e4e97 10003
3e4093b6 10004 if (exp != error_mark_node)
de520661 10005 {
3e4093b6
RS
10006 orig_type = TREE_TYPE (exp);
10007
c58e8676 10008 if (!INTEGRAL_TYPE_P (orig_type))
de520661 10009 {
c58e8676
VR
10010 if (orig_type != error_mark_node)
10011 {
c2255bc4 10012 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
10013 orig_type = error_mark_node;
10014 }
3e4093b6 10015 exp = integer_zero_node;
de520661 10016 }
3e4093b6 10017 else
de520661 10018 {
c58e8676 10019 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
10020 tree e = exp;
10021
10022 /* Warn if the condition has boolean value. */
10023 while (TREE_CODE (e) == COMPOUND_EXPR)
10024 e = TREE_OPERAND (e, 1);
10025
10026 if ((TREE_CODE (type) == BOOLEAN_TYPE
10027 || truth_value_p (TREE_CODE (e)))
10028 /* Explicit cast to int suppresses this warning. */
10029 && !(TREE_CODE (type) == INTEGER_TYPE
10030 && explicit_cast_p))
b155cfd9 10031 bool_cond_p = true;
8b6a5902 10032
8400e75e 10033 if (!in_system_header_at (input_location)
3e4093b6
RS
10034 && (type == long_integer_type_node
10035 || type == long_unsigned_type_node))
c2255bc4
AH
10036 warning_at (switch_cond_loc,
10037 OPT_Wtraditional, "%<long%> switch expression not "
10038 "converted to %<int%> in ISO C");
8b6a5902 10039
928c19bb 10040 exp = c_fully_fold (exp, false, NULL);
3e4093b6 10041 exp = default_conversion (exp);
ca085fd7
MLI
10042
10043 if (warn_sequence_point)
10044 verify_sequence_points (exp);
3e4093b6
RS
10045 }
10046 }
10047
604f5adf 10048 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 10049 cs = XNEW (struct c_switch);
604f5adf 10050 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 10051 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 10052 cs->orig_type = orig_type;
3e4093b6 10053 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 10054 cs->bindings = c_get_switch_bindings ();
b155cfd9
MP
10055 cs->bool_cond_p = bool_cond_p;
10056 cs->outside_range_p = false;
506e2710
RH
10057 cs->next = c_switch_stack;
10058 c_switch_stack = cs;
3e4093b6 10059
604f5adf 10060 return add_stmt (cs->switch_expr);
3e4093b6
RS
10061}
10062
c2255bc4 10063/* Process a case label at location LOC. */
3e4093b6
RS
10064
10065tree
c2255bc4 10066do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
10067{
10068 tree label = NULL_TREE;
10069
17cede2e
JM
10070 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10071 {
10072 low_value = c_fully_fold (low_value, false, NULL);
10073 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 10074 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
10075 "case label is not an integer constant expression");
10076 }
10077
10078 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10079 {
10080 high_value = c_fully_fold (high_value, false, NULL);
10081 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 10082 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
10083 "case label is not an integer constant expression");
10084 }
10085
e1b7793c 10086 if (c_switch_stack == NULL)
187230a7
JM
10087 {
10088 if (low_value)
e1b7793c 10089 error_at (loc, "case label not within a switch statement");
187230a7 10090 else
e1b7793c
ILT
10091 error_at (loc, "%<default%> label not within a switch statement");
10092 return NULL_TREE;
187230a7 10093 }
de520661 10094
e1b7793c
ILT
10095 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10096 EXPR_LOCATION (c_switch_stack->switch_expr),
10097 loc))
10098 return NULL_TREE;
10099
10100 label = c_add_case_label (loc, c_switch_stack->cases,
10101 SWITCH_COND (c_switch_stack->switch_expr),
10102 c_switch_stack->orig_type,
b155cfd9
MP
10103 low_value, high_value,
10104 &c_switch_stack->outside_range_p);
e1b7793c
ILT
10105 if (label == error_mark_node)
10106 label = NULL_TREE;
3e4093b6
RS
10107 return label;
10108}
de520661 10109
083e891e
MP
10110/* Finish the switch statement. TYPE is the original type of the
10111 controlling expression of the switch, or NULL_TREE. */
de520661 10112
3e4093b6 10113void
083e891e 10114c_finish_case (tree body, tree type)
3e4093b6 10115{
506e2710 10116 struct c_switch *cs = c_switch_stack;
fbc315db 10117 location_t switch_location;
3e4093b6 10118
604f5adf 10119 SWITCH_BODY (cs->switch_expr) = body;
325c3691 10120
6de9cd9a 10121 /* Emit warnings as needed. */
c2255bc4 10122 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 10123 c_do_switch_warnings (cs->cases, switch_location,
083e891e 10124 type ? type : TREE_TYPE (cs->switch_expr),
b155cfd9
MP
10125 SWITCH_COND (cs->switch_expr),
10126 cs->bool_cond_p, cs->outside_range_p);
6de9cd9a 10127
3e4093b6 10128 /* Pop the stack. */
506e2710 10129 c_switch_stack = cs->next;
3e4093b6 10130 splay_tree_delete (cs->cases);
e1b7793c 10131 c_release_switch_bindings (cs->bindings);
5d038c4c 10132 XDELETE (cs);
de520661 10133}
325c3691 10134\f
506e2710
RH
10135/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10136 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
99cd9857 10137 may be null. */
325c3691 10138
9e51cf9d 10139void
506e2710 10140c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
99cd9857 10141 tree else_block)
325c3691 10142{
506e2710 10143 tree stmt;
325c3691 10144
25c22937
BI
10145 /* If the condition has array notations, then the rank of the then_block and
10146 else_block must be either 0 or be equal to the rank of the condition. If
10147 the condition does not have array notations then break them up as it is
10148 broken up in a normal expression. */
b72271b9 10149 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
10150 {
10151 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10152 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10153 return;
10154 if (then_block
10155 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10156 return;
10157 if (else_block
10158 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10159 return;
10160 if (cond_rank != then_rank && then_rank != 0)
10161 {
10162 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10163 " and the then-block");
10164 return;
10165 }
10166 else if (cond_rank != else_rank && else_rank != 0)
10167 {
10168 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10169 " and the else-block");
10170 return;
10171 }
10172 }
16865eaa 10173
2214de30 10174 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 10175 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 10176 add_stmt (stmt);
325c3691
RH
10177}
10178
506e2710
RH
10179/* Emit a general-purpose loop construct. START_LOCUS is the location of
10180 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10181 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 10182 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 10183 the continue label. Everything is allowed to be NULL. */
325c3691
RH
10184
10185void
506e2710
RH
10186c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10187 tree blab, tree clab, bool cond_is_first)
325c3691 10188{
506e2710
RH
10189 tree entry = NULL, exit = NULL, t;
10190
e5e44252
AK
10191 /* In theory could forbid cilk spawn for loop increment expression,
10192 but it should work just fine. */
36536d79 10193
28af952a
RS
10194 /* If the condition is zero don't generate a loop construct. */
10195 if (cond && integer_zerop (cond))
10196 {
10197 if (cond_is_first)
10198 {
10199 t = build_and_jump (&blab);
10200 SET_EXPR_LOCATION (t, start_locus);
10201 add_stmt (t);
10202 }
10203 }
10204 else
506e2710
RH
10205 {
10206 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 10207
506e2710 10208 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
10209 out of the loop, or to the top of it. If there's no exit condition,
10210 then we just build a jump back to the top. */
506e2710 10211 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 10212
28af952a 10213 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
10214 {
10215 /* Canonicalize the loop condition to the end. This means
10216 generating a branch to the loop condition. Reuse the
10217 continue label, if possible. */
10218 if (cond_is_first)
10219 {
10220 if (incr || !clab)
10221 {
10222 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10223 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10224 }
10225 else
10226 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 10227 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
10228 add_stmt (t);
10229 }
10230
506e2710 10231 t = build_and_jump (&blab);
506e2710 10232 if (cond_is_first)
db3927fb
AH
10233 exit = fold_build3_loc (start_locus,
10234 COND_EXPR, void_type_node, cond, exit, t);
506e2710 10235 else
db3927fb
AH
10236 exit = fold_build3_loc (input_location,
10237 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3 10238 }
fc402eec
AA
10239 else
10240 {
10241 /* For the backward-goto's location of an unconditional loop
10242 use the beginning of the body, or, if there is none, the
10243 top of the loop. */
10244 location_t loc = EXPR_LOCATION (expr_first (body));
10245 if (loc == UNKNOWN_LOCATION)
10246 loc = start_locus;
10247 SET_EXPR_LOCATION (exit, loc);
10248 }
c22cacf3 10249
506e2710
RH
10250 add_stmt (top);
10251 }
c22cacf3 10252
506e2710
RH
10253 if (body)
10254 add_stmt (body);
10255 if (clab)
10256 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10257 if (incr)
10258 add_stmt (incr);
10259 if (entry)
10260 add_stmt (entry);
10261 if (exit)
10262 add_stmt (exit);
10263 if (blab)
10264 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 10265}
325c3691
RH
10266
10267tree
c2255bc4 10268c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 10269{
089efaa4 10270 bool skip;
506e2710 10271 tree label = *label_p;
325c3691 10272
089efaa4
ILT
10273 /* In switch statements break is sometimes stylistically used after
10274 a return statement. This can lead to spurious warnings about
10275 control reaching the end of a non-void function when it is
10276 inlined. Note that we are calling block_may_fallthru with
10277 language specific tree nodes; this works because
10278 block_may_fallthru returns true when given something it does not
10279 understand. */
10280 skip = !block_may_fallthru (cur_stmt_list);
10281
506e2710 10282 if (!label)
089efaa4
ILT
10283 {
10284 if (!skip)
c2255bc4 10285 *label_p = label = create_artificial_label (loc);
089efaa4 10286 }
953ff289
DN
10287 else if (TREE_CODE (label) == LABEL_DECL)
10288 ;
10289 else switch (TREE_INT_CST_LOW (label))
506e2710 10290 {
953ff289 10291 case 0:
506e2710 10292 if (is_break)
c2255bc4 10293 error_at (loc, "break statement not within loop or switch");
506e2710 10294 else
c2255bc4 10295 error_at (loc, "continue statement not within a loop");
506e2710 10296 return NULL_TREE;
953ff289
DN
10297
10298 case 1:
10299 gcc_assert (is_break);
c2255bc4 10300 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
10301 return NULL_TREE;
10302
c02065fc
AH
10303 case 2:
10304 if (is_break)
10305 error ("break statement within %<#pragma simd%> loop body");
10306 else
10307 error ("continue statement within %<#pragma simd%> loop body");
10308 return NULL_TREE;
10309
953ff289
DN
10310 default:
10311 gcc_unreachable ();
506e2710 10312 }
325c3691 10313
089efaa4
ILT
10314 if (skip)
10315 return NULL_TREE;
10316
2e28e797
JH
10317 if (!is_break)
10318 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10319
53fb4de3 10320 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
10321}
10322
506e2710 10323/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
10324
10325static void
c2255bc4 10326emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 10327{
e6b5a630
RH
10328 if (expr == error_mark_node)
10329 ;
10330 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
10331 {
10332 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 10333 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 10334 }
789eadcd
MP
10335 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10336 {
10337 tree r = expr;
10338 location_t cloc = loc;
10339 while (TREE_CODE (r) == COMPOUND_EXPR)
10340 {
10341 if (EXPR_HAS_LOCATION (r))
10342 cloc = EXPR_LOCATION (r);
10343 r = TREE_OPERAND (r, 1);
10344 }
10345 if (!TREE_SIDE_EFFECTS (r)
10346 && !VOID_TYPE_P (TREE_TYPE (r))
10347 && !CONVERT_EXPR_P (r)
cc28fc7f 10348 && !TREE_NO_WARNING (r)
789eadcd
MP
10349 && !TREE_NO_WARNING (expr))
10350 warning_at (cloc, OPT_Wunused_value,
10351 "right-hand operand of comma expression has no effect");
10352 }
27f33b15 10353 else
c2255bc4 10354 warn_if_unused_value (expr, loc);
3a5b9284
RH
10355}
10356
506e2710 10357/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
10358 diagnostics, but do not call ADD_STMT. LOC is the location of the
10359 statement. */
3a5b9284 10360
506e2710 10361tree
c2255bc4 10362c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 10363{
056928b2
JJ
10364 tree exprv;
10365
3a5b9284 10366 if (!expr)
506e2710 10367 return NULL_TREE;
3a5b9284 10368
928c19bb
JM
10369 expr = c_fully_fold (expr, false, NULL);
10370
3a5b9284
RH
10371 if (warn_sequence_point)
10372 verify_sequence_points (expr);
10373
10374 if (TREE_TYPE (expr) != error_mark_node
10375 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10376 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 10377 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
10378
10379 /* If we're not processing a statement expression, warn about unused values.
10380 Warnings for statement expressions will be emitted later, once we figure
10381 out which is the result. */
10382 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 10383 && warn_unused_value)
d06f8b75 10384 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
3a5b9284 10385
056928b2
JJ
10386 exprv = expr;
10387 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10388 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
10389 while (CONVERT_EXPR_P (exprv))
10390 exprv = TREE_OPERAND (exprv, 0);
10391 if (DECL_P (exprv)
10392 || handled_component_p (exprv)
10393 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 10394 mark_exp_read (exprv);
fa8351f8 10395
3a5b9284
RH
10396 /* If the expression is not of a type to which we cannot assign a line
10397 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 10398 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
10399 {
10400 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10401 SET_EXPR_LOCATION (expr, loc);
10402 }
506e2710
RH
10403
10404 return expr;
10405}
10406
c2255bc4
AH
10407/* Emit an expression as a statement. LOC is the location of the
10408 expression. */
506e2710
RH
10409
10410tree
c2255bc4 10411c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
10412{
10413 if (expr)
c2255bc4 10414 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
10415 else
10416 return NULL;
3a5b9284
RH
10417}
10418
10419/* Do the opposite and emit a statement as an expression. To begin,
10420 create a new binding level and return it. */
325c3691
RH
10421
10422tree
10423c_begin_stmt_expr (void)
10424{
10425 tree ret;
10426
10427 /* We must force a BLOCK for this level so that, if it is not expanded
10428 later, there is a way to turn off the entire subtree of blocks that
10429 are contained in it. */
10430 keep_next_level ();
10431 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10432
10433 c_bindings_start_stmt_expr (c_switch_stack == NULL
10434 ? NULL
10435 : c_switch_stack->bindings);
325c3691
RH
10436
10437 /* Mark the current statement list as belonging to a statement list. */
10438 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10439
10440 return ret;
10441}
10442
c2255bc4
AH
10443/* LOC is the location of the compound statement to which this body
10444 belongs. */
10445
325c3691 10446tree
c2255bc4 10447c_finish_stmt_expr (location_t loc, tree body)
325c3691 10448{
3a5b9284 10449 tree last, type, tmp, val;
325c3691
RH
10450 tree *last_p;
10451
c2255bc4 10452 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10453
10454 c_bindings_end_stmt_expr (c_switch_stack == NULL
10455 ? NULL
10456 : c_switch_stack->bindings);
325c3691 10457
3a5b9284
RH
10458 /* Locate the last statement in BODY. See c_end_compound_stmt
10459 about always returning a BIND_EXPR. */
10460 last_p = &BIND_EXPR_BODY (body);
10461 last = BIND_EXPR_BODY (body);
10462
10463 continue_searching:
325c3691
RH
10464 if (TREE_CODE (last) == STATEMENT_LIST)
10465 {
3a5b9284
RH
10466 tree_stmt_iterator i;
10467
10468 /* This can happen with degenerate cases like ({ }). No value. */
10469 if (!TREE_SIDE_EFFECTS (last))
10470 return body;
10471
10472 /* If we're supposed to generate side effects warnings, process
10473 all of the statements except the last. */
27f33b15 10474 if (warn_unused_value)
325c3691 10475 {
3a5b9284 10476 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10477 {
10478 location_t tloc;
10479 tree t = tsi_stmt (i);
10480
10481 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10482 emit_side_effect_warnings (tloc, t);
10483 }
325c3691
RH
10484 }
10485 else
3a5b9284
RH
10486 i = tsi_last (last);
10487 last_p = tsi_stmt_ptr (i);
10488 last = *last_p;
325c3691
RH
10489 }
10490
3a5b9284
RH
10491 /* If the end of the list is exception related, then the list was split
10492 by a call to push_cleanup. Continue searching. */
10493 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10494 || TREE_CODE (last) == TRY_CATCH_EXPR)
10495 {
10496 last_p = &TREE_OPERAND (last, 0);
10497 last = *last_p;
10498 goto continue_searching;
10499 }
10500
26d8af35
JM
10501 if (last == error_mark_node)
10502 return last;
10503
3a5b9284
RH
10504 /* In the case that the BIND_EXPR is not necessary, return the
10505 expression out from inside it. */
26d8af35
JM
10506 if (last == BIND_EXPR_BODY (body)
10507 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10508 {
928c19bb
JM
10509 /* Even if this looks constant, do not allow it in a constant
10510 expression. */
e5a94231 10511 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10512 /* Do not warn if the return value of a statement expression is
10513 unused. */
928c19bb 10514 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10515 return last;
10516 }
325c3691
RH
10517
10518 /* Extract the type of said expression. */
10519 type = TREE_TYPE (last);
325c3691 10520
3a5b9284
RH
10521 /* If we're not returning a value at all, then the BIND_EXPR that
10522 we already have is a fine expression to return. */
10523 if (!type || VOID_TYPE_P (type))
10524 return body;
10525
10526 /* Now that we've located the expression containing the value, it seems
10527 silly to make voidify_wrapper_expr repeat the process. Create a
10528 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10529 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10530
10531 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10532 tree_expr_nonnegative_p giving up immediately. */
10533 val = last;
10534 if (TREE_CODE (val) == NOP_EXPR
10535 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10536 val = TREE_OPERAND (val, 0);
10537
53fb4de3 10538 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10539 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10540
c2255bc4
AH
10541 {
10542 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10543 SET_EXPR_LOCATION (t, loc);
10544 return t;
10545 }
325c3691
RH
10546}
10547\f
10548/* Begin and end compound statements. This is as simple as pushing
10549 and popping new statement lists from the tree. */
10550
10551tree
10552c_begin_compound_stmt (bool do_scope)
10553{
10554 tree stmt = push_stmt_list ();
10555 if (do_scope)
4dfa0342 10556 push_scope ();
325c3691
RH
10557 return stmt;
10558}
10559
c2255bc4
AH
10560/* End a compound statement. STMT is the statement. LOC is the
10561 location of the compound statement-- this is usually the location
10562 of the opening brace. */
10563
325c3691 10564tree
c2255bc4 10565c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10566{
10567 tree block = NULL;
10568
10569 if (do_scope)
10570 {
10571 if (c_dialect_objc ())
10572 objc_clear_super_receiver ();
10573 block = pop_scope ();
10574 }
10575
10576 stmt = pop_stmt_list (stmt);
c2255bc4 10577 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10578
10579 /* If this compound statement is nested immediately inside a statement
10580 expression, then force a BIND_EXPR to be created. Otherwise we'll
10581 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10582 STATEMENT_LISTs merge, and thus we can lose track of what statement
10583 was really last. */
38e01f9e 10584 if (building_stmt_list_p ()
325c3691
RH
10585 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10586 && TREE_CODE (stmt) != BIND_EXPR)
10587 {
53fb4de3 10588 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10589 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10590 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10591 }
10592
10593 return stmt;
10594}
5a508662
RH
10595
10596/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10597 when the current scope is exited. EH_ONLY is true when this is not
10598 meant to apply to normal control flow transfer. */
10599
10600void
c2255bc4 10601push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10602{
3a5b9284
RH
10603 enum tree_code code;
10604 tree stmt, list;
10605 bool stmt_expr;
10606
10607 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10608 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10609 add_stmt (stmt);
3a5b9284
RH
10610 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10611 list = push_stmt_list ();
10612 TREE_OPERAND (stmt, 0) = list;
10613 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10614}
325c3691 10615\f
9f47c7e5
IE
10616/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10617 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10618
10619static tree
10620build_vec_cmp (tree_code code, tree type,
10621 tree arg0, tree arg1)
10622{
10623 tree zero_vec = build_zero_cst (type);
10624 tree minus_one_vec = build_minus_one_cst (type);
10625 tree cmp_type = build_same_sized_truth_vector_type (type);
10626 tree cmp = build2 (code, cmp_type, arg0, arg1);
10627 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10628}
10629
3e4093b6
RS
10630/* Build a binary-operation expression without default conversions.
10631 CODE is the kind of expression to build.
ba47d38d 10632 LOCATION is the operator's location.
3e4093b6
RS
10633 This function differs from `build' in several ways:
10634 the data type of the result is computed and recorded in it,
10635 warnings are generated if arg data types are invalid,
10636 special handling for addition and subtraction of pointers is known,
10637 and some optimization is done (operations on narrow ints
10638 are done in the narrower type when that gives the same result).
10639 Constant folding is also done before the result is returned.
de520661 10640
3e4093b6
RS
10641 Note that the operands will never have enumeral types, or function
10642 or array types, because either they will have the default conversions
10643 performed or they have both just been converted to some other type in which
10644 the arithmetic is to be done. */
10645
10646tree
ba47d38d
AH
10647build_binary_op (location_t location, enum tree_code code,
10648 tree orig_op0, tree orig_op1, int convert_p)
de520661 10649{
8ce94e44
JM
10650 tree type0, type1, orig_type0, orig_type1;
10651 tree eptype;
3e4093b6
RS
10652 enum tree_code code0, code1;
10653 tree op0, op1;
c9f9eb5d 10654 tree ret = error_mark_node;
4de67c26 10655 const char *invalid_op_diag;
4d84fe7c 10656 bool op0_int_operands, op1_int_operands;
928c19bb 10657 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10658
3e4093b6
RS
10659 /* Expression code to give to the expression when it is built.
10660 Normally this is CODE, which is what the caller asked for,
10661 but in some special cases we change it. */
10662 enum tree_code resultcode = code;
8b6a5902 10663
3e4093b6
RS
10664 /* Data type in which the computation is to be performed.
10665 In the simplest cases this is the common type of the arguments. */
10666 tree result_type = NULL;
10667
8ce94e44
JM
10668 /* When the computation is in excess precision, the type of the
10669 final EXCESS_PRECISION_EXPR. */
2d2e923f 10670 tree semantic_result_type = NULL;
8ce94e44 10671
3e4093b6
RS
10672 /* Nonzero means operands have already been type-converted
10673 in whatever way is necessary.
10674 Zero means they need to be converted to RESULT_TYPE. */
10675 int converted = 0;
10676
10677 /* Nonzero means create the expression with this type, rather than
10678 RESULT_TYPE. */
10679 tree build_type = 0;
10680
10681 /* Nonzero means after finally constructing the expression
10682 convert it to this type. */
10683 tree final_type = 0;
10684
10685 /* Nonzero if this is an operation like MIN or MAX which can
10686 safely be computed in short if both args are promoted shorts.
10687 Also implies COMMON.
10688 -1 indicates a bitwise operation; this makes a difference
10689 in the exact conditions for when it is safe to do the operation
10690 in a narrower mode. */
10691 int shorten = 0;
10692
10693 /* Nonzero if this is a comparison operation;
10694 if both args are promoted shorts, compare the original shorts.
10695 Also implies COMMON. */
10696 int short_compare = 0;
10697
10698 /* Nonzero if this is a right-shift operation, which can be computed on the
10699 original short and then promoted if the operand is a promoted short. */
10700 int short_shift = 0;
10701
10702 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10703 int common = 0;
10704
58393038
ZL
10705 /* True means types are compatible as far as ObjC is concerned. */
10706 bool objc_ok;
10707
8ce94e44
JM
10708 /* True means this is an arithmetic operation that may need excess
10709 precision. */
10710 bool may_need_excess_precision;
10711
180f8dbb
JM
10712 /* True means this is a boolean operation that converts both its
10713 operands to truth-values. */
10714 bool boolean_op = false;
10715
de5a5fa1
MP
10716 /* Remember whether we're doing / or %. */
10717 bool doing_div_or_mod = false;
10718
10719 /* Remember whether we're doing << or >>. */
10720 bool doing_shift = false;
10721
10722 /* Tree holding instrumentation expression. */
10723 tree instrument_expr = NULL;
10724
ba47d38d
AH
10725 if (location == UNKNOWN_LOCATION)
10726 location = input_location;
10727
4d84fe7c
JM
10728 op0 = orig_op0;
10729 op1 = orig_op1;
10730
10731 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10732 if (op0_int_operands)
10733 op0 = remove_c_maybe_const_expr (op0);
10734 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10735 if (op1_int_operands)
10736 op1 = remove_c_maybe_const_expr (op1);
10737 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10738 if (int_operands)
10739 {
10740 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10741 && TREE_CODE (orig_op1) == INTEGER_CST);
10742 int_const = (int_const_or_overflow
10743 && !TREE_OVERFLOW (orig_op0)
10744 && !TREE_OVERFLOW (orig_op1));
10745 }
10746 else
10747 int_const = int_const_or_overflow = false;
10748
0e3a99ae 10749 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e 10750 if (convert_p
31521951 10751 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
790e9490 10752 {
4d84fe7c
JM
10753 op0 = default_conversion (op0);
10754 op1 = default_conversion (op1);
790e9490
RS
10755 }
10756
36536d79
BI
10757 /* When Cilk Plus is enabled and there are array notations inside op0, then
10758 we check to see if there are builtin array notation functions. If
10759 so, then we take on the type of the array notation inside it. */
b72271b9 10760 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10761 orig_type0 = type0 = find_correct_array_notation_type (op0);
10762 else
10763 orig_type0 = type0 = TREE_TYPE (op0);
10764
b72271b9 10765 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10766 orig_type1 = type1 = find_correct_array_notation_type (op1);
10767 else
10768 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10769
3e4093b6
RS
10770 /* The expression codes of the data types of the arguments tell us
10771 whether the arguments are integers, floating, pointers, etc. */
10772 code0 = TREE_CODE (type0);
10773 code1 = TREE_CODE (type1);
10774
10775 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10776 STRIP_TYPE_NOPS (op0);
10777 STRIP_TYPE_NOPS (op1);
10778
10779 /* If an error was already reported for one of the arguments,
10780 avoid reporting another error. */
10781
10782 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10783 return error_mark_node;
10784
1807ffc1
MS
10785 if (code0 == POINTER_TYPE
10786 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10787 return error_mark_node;
10788
10789 if (code1 == POINTER_TYPE
10790 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10791 return error_mark_node;
10792
4de67c26
JM
10793 if ((invalid_op_diag
10794 = targetm.invalid_binary_op (code, type0, type1)))
10795 {
ba47d38d 10796 error_at (location, invalid_op_diag);
4de67c26
JM
10797 return error_mark_node;
10798 }
10799
8ce94e44
JM
10800 switch (code)
10801 {
10802 case PLUS_EXPR:
10803 case MINUS_EXPR:
10804 case MULT_EXPR:
10805 case TRUNC_DIV_EXPR:
10806 case CEIL_DIV_EXPR:
10807 case FLOOR_DIV_EXPR:
10808 case ROUND_DIV_EXPR:
10809 case EXACT_DIV_EXPR:
10810 may_need_excess_precision = true;
10811 break;
10812 default:
10813 may_need_excess_precision = false;
10814 break;
10815 }
10816 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10817 {
10818 op0 = TREE_OPERAND (op0, 0);
10819 type0 = TREE_TYPE (op0);
10820 }
10821 else if (may_need_excess_precision
10822 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10823 {
10824 type0 = eptype;
10825 op0 = convert (eptype, op0);
10826 }
10827 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10828 {
10829 op1 = TREE_OPERAND (op1, 0);
10830 type1 = TREE_TYPE (op1);
10831 }
10832 else if (may_need_excess_precision
10833 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10834 {
10835 type1 = eptype;
10836 op1 = convert (eptype, op1);
10837 }
10838
58393038
ZL
10839 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10840
0e3a99ae
AS
10841 /* In case when one of the operands of the binary operation is
10842 a vector and another is a scalar -- convert scalar to vector. */
10843 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10844 {
a212e43f
MG
10845 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10846 true);
f90e8e2e 10847
0e3a99ae
AS
10848 switch (convert_flag)
10849 {
10850 case stv_error:
10851 return error_mark_node;
10852 case stv_firstarg:
10853 {
10854 bool maybe_const = true;
10855 tree sc;
10856 sc = c_fully_fold (op0, false, &maybe_const);
10857 sc = save_expr (sc);
10858 sc = convert (TREE_TYPE (type1), sc);
10859 op0 = build_vector_from_val (type1, sc);
10860 if (!maybe_const)
10861 op0 = c_wrap_maybe_const (op0, true);
10862 orig_type0 = type0 = TREE_TYPE (op0);
10863 code0 = TREE_CODE (type0);
10864 converted = 1;
10865 break;
10866 }
10867 case stv_secondarg:
10868 {
10869 bool maybe_const = true;
10870 tree sc;
10871 sc = c_fully_fold (op1, false, &maybe_const);
10872 sc = save_expr (sc);
10873 sc = convert (TREE_TYPE (type0), sc);
10874 op1 = build_vector_from_val (type0, sc);
10875 if (!maybe_const)
54b9f838 10876 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10877 orig_type1 = type1 = TREE_TYPE (op1);
10878 code1 = TREE_CODE (type1);
10879 converted = 1;
10880 break;
10881 }
10882 default:
10883 break;
10884 }
10885 }
10886
3e4093b6 10887 switch (code)
de520661 10888 {
3e4093b6
RS
10889 case PLUS_EXPR:
10890 /* Handle the pointer + int case. */
10891 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10892 {
db3927fb 10893 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10894 goto return_build_binary_op;
10895 }
3e4093b6 10896 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10897 {
db3927fb 10898 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10899 goto return_build_binary_op;
10900 }
fe67cf58 10901 else
3e4093b6
RS
10902 common = 1;
10903 break;
400fbf9f 10904
3e4093b6
RS
10905 case MINUS_EXPR:
10906 /* Subtraction of two similar pointers.
10907 We must subtract them as integers, then divide by object size. */
10908 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10909 && comp_target_types (location, type0, type1))
c9f9eb5d 10910 {
db3927fb 10911 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10912 goto return_build_binary_op;
10913 }
3e4093b6
RS
10914 /* Handle pointer minus int. Just like pointer plus int. */
10915 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10916 {
db3927fb 10917 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10918 goto return_build_binary_op;
10919 }
3e4093b6
RS
10920 else
10921 common = 1;
10922 break;
8b6a5902 10923
3e4093b6
RS
10924 case MULT_EXPR:
10925 common = 1;
10926 break;
10927
10928 case TRUNC_DIV_EXPR:
10929 case CEIL_DIV_EXPR:
10930 case FLOOR_DIV_EXPR:
10931 case ROUND_DIV_EXPR:
10932 case EXACT_DIV_EXPR:
de5a5fa1 10933 doing_div_or_mod = true;
c9f9eb5d 10934 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10935
10936 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10937 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10938 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10939 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10940 || code1 == FIXED_POINT_TYPE
3e4093b6 10941 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10942 {
5bed876a
AH
10943 enum tree_code tcode0 = code0, tcode1 = code1;
10944
3a021db2 10945 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10946 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10947 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10948 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10949
ab22c1fa
CF
10950 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10951 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10952 resultcode = RDIV_EXPR;
10953 else
10954 /* Although it would be tempting to shorten always here, that
10955 loses on some targets, since the modulo instruction is
10956 undefined if the quotient can't be represented in the
10957 computation mode. We shorten only if unsigned or if
10958 dividing by something we know != -1. */
8df83eae 10959 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10960 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10961 && !integer_all_onesp (op1)));
3e4093b6
RS
10962 common = 1;
10963 }
10964 break;
de520661 10965
3e4093b6 10966 case BIT_AND_EXPR:
3e4093b6
RS
10967 case BIT_IOR_EXPR:
10968 case BIT_XOR_EXPR:
10969 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10970 shorten = -1;
9ef0c8d9
AP
10971 /* Allow vector types which are not floating point types. */
10972 else if (code0 == VECTOR_TYPE
10973 && code1 == VECTOR_TYPE
10974 && !VECTOR_FLOAT_TYPE_P (type0)
10975 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10976 common = 1;
10977 break;
10978
10979 case TRUNC_MOD_EXPR:
10980 case FLOOR_MOD_EXPR:
de5a5fa1 10981 doing_div_or_mod = true;
c9f9eb5d 10982 warn_for_div_by_zero (location, op1);
de520661 10983
5cfd5d9b
AP
10984 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10985 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10986 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10987 common = 1;
10988 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10989 {
10990 /* Although it would be tempting to shorten always here, that loses
10991 on some targets, since the modulo instruction is undefined if the
10992 quotient can't be represented in the computation mode. We shorten
10993 only if unsigned or if dividing by something we know != -1. */
8df83eae 10994 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10995 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10996 && !integer_all_onesp (op1)));
3e4093b6
RS
10997 common = 1;
10998 }
10999 break;
de520661 11000
3e4093b6
RS
11001 case TRUTH_ANDIF_EXPR:
11002 case TRUTH_ORIF_EXPR:
11003 case TRUTH_AND_EXPR:
11004 case TRUTH_OR_EXPR:
11005 case TRUTH_XOR_EXPR:
11006 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
11007 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11008 || code0 == FIXED_POINT_TYPE)
3e4093b6 11009 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
11010 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11011 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11012 {
11013 /* Result of these operations is always an int,
11014 but that does not mean the operands should be
11015 converted to ints! */
11016 result_type = integer_type_node;
a27d595d
JM
11017 if (op0_int_operands)
11018 {
11019 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11020 op0 = remove_c_maybe_const_expr (op0);
11021 }
11022 else
11023 op0 = c_objc_common_truthvalue_conversion (location, op0);
11024 if (op1_int_operands)
11025 {
11026 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11027 op1 = remove_c_maybe_const_expr (op1);
11028 }
11029 else
11030 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 11031 converted = 1;
180f8dbb 11032 boolean_op = true;
3e4093b6 11033 }
928c19bb
JM
11034 if (code == TRUTH_ANDIF_EXPR)
11035 {
11036 int_const_or_overflow = (int_operands
11037 && TREE_CODE (orig_op0) == INTEGER_CST
11038 && (op0 == truthvalue_false_node
11039 || TREE_CODE (orig_op1) == INTEGER_CST));
11040 int_const = (int_const_or_overflow
11041 && !TREE_OVERFLOW (orig_op0)
11042 && (op0 == truthvalue_false_node
11043 || !TREE_OVERFLOW (orig_op1)));
11044 }
11045 else if (code == TRUTH_ORIF_EXPR)
11046 {
11047 int_const_or_overflow = (int_operands
11048 && TREE_CODE (orig_op0) == INTEGER_CST
11049 && (op0 == truthvalue_true_node
11050 || TREE_CODE (orig_op1) == INTEGER_CST));
11051 int_const = (int_const_or_overflow
11052 && !TREE_OVERFLOW (orig_op0)
11053 && (op0 == truthvalue_true_node
11054 || !TREE_OVERFLOW (orig_op1)));
11055 }
3e4093b6 11056 break;
eba80994 11057
3e4093b6
RS
11058 /* Shift operations: result has same type as first operand;
11059 always convert second operand to int.
11060 Also set SHORT_SHIFT if shifting rightward. */
de520661 11061
3e4093b6 11062 case RSHIFT_EXPR:
76e2c821
JB
11063 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11064 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11065 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11066 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
11067 {
11068 result_type = type0;
11069 converted = 1;
11070 }
76e2c821
JB
11071 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11072 || code0 == VECTOR_TYPE)
451b5e48 11073 && code1 == INTEGER_TYPE)
3e4093b6 11074 {
de5a5fa1 11075 doing_shift = true;
928c19bb 11076 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 11077 {
3e4093b6 11078 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
11079 {
11080 int_const = false;
7d882b83 11081 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11082 warning_at (location, OPT_Wshift_count_negative,
11083 "right shift count is negative");
928c19bb 11084 }
76e2c821
JB
11085 else if (code0 == VECTOR_TYPE)
11086 {
11087 if (compare_tree_int (op1,
11088 TYPE_PRECISION (TREE_TYPE (type0)))
11089 >= 0)
11090 {
11091 int_const = false;
11092 if (c_inhibit_evaluation_warnings == 0)
11093 warning_at (location, OPT_Wshift_count_overflow,
11094 "right shift count >= width of vector element");
11095 }
11096 }
3e4093b6 11097 else
bbb818c6 11098 {
3f75a254 11099 if (!integer_zerop (op1))
3e4093b6
RS
11100 short_shift = 1;
11101
11102 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
11103 {
11104 int_const = false;
7d882b83 11105 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11106 warning_at (location, OPT_Wshift_count_overflow,
11107 "right shift count >= width of type");
928c19bb 11108 }
bbb818c6 11109 }
b62acd60 11110 }
de520661 11111
3e4093b6
RS
11112 /* Use the type of the value to be shifted. */
11113 result_type = type0;
3e4093b6
RS
11114 /* Avoid converting op1 to result_type later. */
11115 converted = 1;
400fbf9f 11116 }
3e4093b6 11117 break;
253b6b82 11118
3e4093b6 11119 case LSHIFT_EXPR:
76e2c821
JB
11120 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11121 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11122 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11123 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
11124 {
11125 result_type = type0;
11126 converted = 1;
11127 }
76e2c821
JB
11128 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11129 || code0 == VECTOR_TYPE)
451b5e48 11130 && code1 == INTEGER_TYPE)
3e4093b6 11131 {
de5a5fa1 11132 doing_shift = true;
0173bd2a
MP
11133 if (TREE_CODE (op0) == INTEGER_CST
11134 && tree_int_cst_sgn (op0) < 0)
11135 {
11136 /* Don't reject a left shift of a negative value in a context
11137 where a constant expression is needed in C90. */
11138 if (flag_isoc99)
11139 int_const = false;
11140 if (c_inhibit_evaluation_warnings == 0)
11141 warning_at (location, OPT_Wshift_negative_value,
11142 "left shift of negative value");
11143 }
928c19bb 11144 if (TREE_CODE (op1) == INTEGER_CST)
de520661 11145 {
3e4093b6 11146 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
11147 {
11148 int_const = false;
7d882b83 11149 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11150 warning_at (location, OPT_Wshift_count_negative,
11151 "left shift count is negative");
928c19bb 11152 }
76e2c821
JB
11153 else if (code0 == VECTOR_TYPE)
11154 {
11155 if (compare_tree_int (op1,
11156 TYPE_PRECISION (TREE_TYPE (type0)))
11157 >= 0)
11158 {
11159 int_const = false;
11160 if (c_inhibit_evaluation_warnings == 0)
11161 warning_at (location, OPT_Wshift_count_overflow,
11162 "left shift count >= width of vector element");
11163 }
11164 }
3e4093b6 11165 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
11166 {
11167 int_const = false;
7d882b83 11168 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11169 warning_at (location, OPT_Wshift_count_overflow,
11170 "left shift count >= width of type");
928c19bb 11171 }
451b5e48
MP
11172 else if (TREE_CODE (op0) == INTEGER_CST
11173 && maybe_warn_shift_overflow (location, op0, op1)
11174 && flag_isoc99)
11175 int_const = false;
94ba5069 11176 }
de520661 11177
3e4093b6
RS
11178 /* Use the type of the value to be shifted. */
11179 result_type = type0;
3e4093b6
RS
11180 /* Avoid converting op1 to result_type later. */
11181 converted = 1;
400fbf9f 11182 }
3e4093b6 11183 break;
de520661 11184
3e4093b6
RS
11185 case EQ_EXPR:
11186 case NE_EXPR:
d246ab4f
AS
11187 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11188 {
11189 tree intt;
0af94e6f 11190 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11191 {
11192 error_at (location, "comparing vectors with different "
11193 "element types");
11194 return error_mark_node;
11195 }
11196
11197 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11198 {
11199 error_at (location, "comparing vectors with different "
11200 "number of elements");
11201 return error_mark_node;
11202 }
11203
fa74a4bc
MP
11204 /* It's not precisely specified how the usual arithmetic
11205 conversions apply to the vector types. Here, we use
11206 the unsigned type if one of the operands is signed and
11207 the other one is unsigned. */
11208 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11209 {
11210 if (!TYPE_UNSIGNED (type0))
11211 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11212 else
11213 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11214 warning_at (location, OPT_Wsign_compare, "comparison between "
11215 "types %qT and %qT", type0, type1);
11216 }
11217
d246ab4f
AS
11218 /* Always construct signed integer vector type. */
11219 intt = c_common_type_for_size (GET_MODE_BITSIZE
11220 (TYPE_MODE (TREE_TYPE (type0))), 0);
11221 result_type = build_opaque_vector_type (intt,
11222 TYPE_VECTOR_SUBPARTS (type0));
11223 converted = 1;
9f47c7e5
IE
11224 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11225 goto return_build_binary_op;
d246ab4f 11226 }
ae311566 11227 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
11228 warning_at (location,
11229 OPT_Wfloat_equal,
11230 "comparing floating point with == or != is unsafe");
3e4093b6
RS
11231 /* Result of comparison is always int,
11232 but don't convert the args to int! */
11233 build_type = integer_type_node;
11234 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 11235 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 11236 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 11237 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 11238 short_compare = 1;
637f1455
SZ
11239 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11240 {
11241 if (TREE_CODE (op0) == ADDR_EXPR
de55efd5
MP
11242 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11243 && !from_macro_expansion_at (location))
637f1455
SZ
11244 {
11245 if (code == EQ_EXPR)
11246 warning_at (location,
11247 OPT_Waddress,
11248 "the comparison will always evaluate as %<false%> "
11249 "for the address of %qD will never be NULL",
11250 TREE_OPERAND (op0, 0));
11251 else
11252 warning_at (location,
11253 OPT_Waddress,
11254 "the comparison will always evaluate as %<true%> "
11255 "for the address of %qD will never be NULL",
11256 TREE_OPERAND (op0, 0));
11257 }
11258 result_type = type0;
11259 }
11260 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11261 {
11262 if (TREE_CODE (op1) == ADDR_EXPR
de55efd5
MP
11263 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11264 && !from_macro_expansion_at (location))
637f1455
SZ
11265 {
11266 if (code == EQ_EXPR)
11267 warning_at (location,
f90e8e2e 11268 OPT_Waddress,
637f1455
SZ
11269 "the comparison will always evaluate as %<false%> "
11270 "for the address of %qD will never be NULL",
11271 TREE_OPERAND (op1, 0));
11272 else
11273 warning_at (location,
11274 OPT_Waddress,
11275 "the comparison will always evaluate as %<true%> "
11276 "for the address of %qD will never be NULL",
11277 TREE_OPERAND (op1, 0));
11278 }
11279 result_type = type1;
11280 }
3e4093b6
RS
11281 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11282 {
11283 tree tt0 = TREE_TYPE (type0);
11284 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
11285 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11286 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11287 addr_space_t as_common = ADDR_SPACE_GENERIC;
11288
3e4093b6
RS
11289 /* Anything compares with void *. void * compares with anything.
11290 Otherwise, the targets must be compatible
11291 and both must be object or both incomplete. */
744aa42f 11292 if (comp_target_types (location, type0, type1))
10bc1b1b 11293 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
11294 else if (!addr_space_superset (as0, as1, &as_common))
11295 {
11296 error_at (location, "comparison of pointers to "
11297 "disjoint address spaces");
11298 return error_mark_node;
11299 }
267bac10 11300 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 11301 {
36c5e70a 11302 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 11303 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11304 "comparison of %<void *%> with function pointer");
ee2990e7 11305 }
267bac10 11306 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 11307 {
36c5e70a 11308 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 11309 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11310 "comparison of %<void *%> with function pointer");
e6834654 11311 }
3e4093b6 11312 else
58393038
ZL
11313 /* Avoid warning about the volatile ObjC EH puts on decls. */
11314 if (!objc_ok)
ba47d38d 11315 pedwarn (location, 0,
509c9d60 11316 "comparison of distinct pointer types lacks a cast");
e6834654 11317
3e4093b6 11318 if (result_type == NULL_TREE)
36c5e70a
BE
11319 {
11320 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11321 result_type = build_pointer_type
11322 (build_qualified_type (void_type_node, qual));
11323 }
e6834654 11324 }
3e4093b6 11325 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 11326 {
3e4093b6 11327 result_type = type0;
ba47d38d 11328 pedwarn (location, 0, "comparison between pointer and integer");
de520661 11329 }
3e4093b6 11330 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 11331 {
3e4093b6 11332 result_type = type1;
ba47d38d 11333 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 11334 }
04159acf
MP
11335 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11336 || truth_value_p (TREE_CODE (orig_op0)))
11337 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11338 || truth_value_p (TREE_CODE (orig_op1))))
11339 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11340 break;
8b6a5902 11341
3e4093b6
RS
11342 case LE_EXPR:
11343 case GE_EXPR:
11344 case LT_EXPR:
11345 case GT_EXPR:
d246ab4f
AS
11346 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11347 {
11348 tree intt;
0af94e6f 11349 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11350 {
11351 error_at (location, "comparing vectors with different "
11352 "element types");
11353 return error_mark_node;
11354 }
11355
11356 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11357 {
11358 error_at (location, "comparing vectors with different "
11359 "number of elements");
11360 return error_mark_node;
11361 }
11362
fa74a4bc
MP
11363 /* It's not precisely specified how the usual arithmetic
11364 conversions apply to the vector types. Here, we use
11365 the unsigned type if one of the operands is signed and
11366 the other one is unsigned. */
11367 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11368 {
11369 if (!TYPE_UNSIGNED (type0))
11370 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11371 else
11372 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11373 warning_at (location, OPT_Wsign_compare, "comparison between "
11374 "types %qT and %qT", type0, type1);
11375 }
11376
d246ab4f
AS
11377 /* Always construct signed integer vector type. */
11378 intt = c_common_type_for_size (GET_MODE_BITSIZE
11379 (TYPE_MODE (TREE_TYPE (type0))), 0);
11380 result_type = build_opaque_vector_type (intt,
11381 TYPE_VECTOR_SUBPARTS (type0));
11382 converted = 1;
9f47c7e5
IE
11383 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11384 goto return_build_binary_op;
d246ab4f 11385 }
3e4093b6 11386 build_type = integer_type_node;
ab22c1fa
CF
11387 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11388 || code0 == FIXED_POINT_TYPE)
11389 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11390 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11391 short_compare = 1;
11392 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11393 {
36c5e70a
BE
11394 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11395 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11396 addr_space_t as_common;
11397
744aa42f 11398 if (comp_target_types (location, type0, type1))
3e4093b6 11399 {
10bc1b1b 11400 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
11401 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11402 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 11403 pedwarn (location, 0,
509c9d60 11404 "comparison of complete and incomplete pointers");
fcf73884 11405 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 11406 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11407 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
11408 else if (null_pointer_constant_p (orig_op0)
11409 || null_pointer_constant_p (orig_op1))
11410 warning_at (location, OPT_Wextra,
11411 "ordered comparison of pointer with null pointer");
11412
3e4093b6 11413 }
36c5e70a
BE
11414 else if (!addr_space_superset (as0, as1, &as_common))
11415 {
11416 error_at (location, "comparison of pointers to "
11417 "disjoint address spaces");
11418 return error_mark_node;
11419 }
3e4093b6
RS
11420 else
11421 {
36c5e70a
BE
11422 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11423 result_type = build_pointer_type
11424 (build_qualified_type (void_type_node, qual));
ba47d38d 11425 pedwarn (location, 0,
509c9d60 11426 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
11427 }
11428 }
6aa3c60d 11429 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
11430 {
11431 result_type = type0;
fcf73884 11432 if (pedantic)
c1771a20 11433 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
11434 "ordered comparison of pointer with integer zero");
11435 else if (extra_warnings)
ba47d38d 11436 warning_at (location, OPT_Wextra,
d42ba3b8 11437 "ordered comparison of pointer with integer zero");
3e4093b6 11438 }
6aa3c60d 11439 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
11440 {
11441 result_type = type1;
d42ba3b8 11442 if (pedantic)
c1771a20 11443 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
11444 "ordered comparison of pointer with integer zero");
11445 else if (extra_warnings)
11446 warning_at (location, OPT_Wextra,
11447 "ordered comparison of pointer with integer zero");
3e4093b6
RS
11448 }
11449 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11450 {
11451 result_type = type0;
ba47d38d 11452 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
11453 }
11454 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11455 {
11456 result_type = type1;
ba47d38d 11457 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 11458 }
04159acf
MP
11459 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11460 || truth_value_p (TREE_CODE (orig_op0)))
11461 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11462 || truth_value_p (TREE_CODE (orig_op1))))
11463 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11464 break;
64094f6a 11465
3e4093b6 11466 default:
37b2f290 11467 gcc_unreachable ();
c9fe6f9f 11468 }
8f17b5c5 11469
e57e265b
PB
11470 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11471 return error_mark_node;
11472
5bed876a
AH
11473 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11474 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 11475 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 11476 {
745e411d
DM
11477 gcc_rich_location richloc (location);
11478 richloc.maybe_add_expr (orig_op0);
11479 richloc.maybe_add_expr (orig_op1);
11480 binary_op_error (&richloc, code, type0, type1);
5bed876a
AH
11481 return error_mark_node;
11482 }
11483
3e4093b6 11484 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 11485 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
11486 &&
11487 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 11488 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 11489 {
2ca862e9
JM
11490 bool first_complex = (code0 == COMPLEX_TYPE);
11491 bool second_complex = (code1 == COMPLEX_TYPE);
11492 int none_complex = (!first_complex && !second_complex);
39b726dd 11493
3e4093b6 11494 if (shorten || common || short_compare)
3bf6bfcc
JJ
11495 {
11496 result_type = c_common_type (type0, type1);
0a0b3574
MM
11497 do_warn_double_promotion (result_type, type0, type1,
11498 "implicit conversion from %qT to %qT "
11499 "to match other operand of binary "
11500 "expression",
11501 location);
3bf6bfcc
JJ
11502 if (result_type == error_mark_node)
11503 return error_mark_node;
11504 }
400fbf9f 11505
2ca862e9
JM
11506 if (first_complex != second_complex
11507 && (code == PLUS_EXPR
11508 || code == MINUS_EXPR
11509 || code == MULT_EXPR
11510 || (code == TRUNC_DIV_EXPR && first_complex))
11511 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11512 && flag_signed_zeros)
11513 {
11514 /* An operation on mixed real/complex operands must be
11515 handled specially, but the language-independent code can
11516 more easily optimize the plain complex arithmetic if
11517 -fno-signed-zeros. */
11518 tree real_type = TREE_TYPE (result_type);
11519 tree real, imag;
11520 if (type0 != orig_type0 || type1 != orig_type1)
11521 {
11522 gcc_assert (may_need_excess_precision && common);
2d2e923f 11523 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11524 }
11525 if (first_complex)
11526 {
11527 if (TREE_TYPE (op0) != result_type)
68fca595 11528 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11529 if (TREE_TYPE (op1) != real_type)
68fca595 11530 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11531 }
11532 else
11533 {
11534 if (TREE_TYPE (op0) != real_type)
68fca595 11535 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11536 if (TREE_TYPE (op1) != result_type)
68fca595 11537 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11538 }
11539 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11540 return error_mark_node;
11541 if (first_complex)
11542 {
11543 op0 = c_save_expr (op0);
11544 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
e51fbec3 11545 op0, true);
2ca862e9 11546 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
e51fbec3 11547 op0, true);
2ca862e9
JM
11548 switch (code)
11549 {
11550 case MULT_EXPR:
11551 case TRUNC_DIV_EXPR:
c4603e7c 11552 op1 = c_save_expr (op1);
2ca862e9
JM
11553 imag = build2 (resultcode, real_type, imag, op1);
11554 /* Fall through. */
11555 case PLUS_EXPR:
11556 case MINUS_EXPR:
11557 real = build2 (resultcode, real_type, real, op1);
11558 break;
11559 default:
11560 gcc_unreachable();
11561 }
11562 }
11563 else
11564 {
11565 op1 = c_save_expr (op1);
11566 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
e51fbec3 11567 op1, true);
2ca862e9 11568 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
e51fbec3 11569 op1, true);
2ca862e9
JM
11570 switch (code)
11571 {
11572 case MULT_EXPR:
c4603e7c 11573 op0 = c_save_expr (op0);
2ca862e9
JM
11574 imag = build2 (resultcode, real_type, op0, imag);
11575 /* Fall through. */
11576 case PLUS_EXPR:
11577 real = build2 (resultcode, real_type, op0, real);
11578 break;
11579 case MINUS_EXPR:
11580 real = build2 (resultcode, real_type, op0, real);
11581 imag = build1 (NEGATE_EXPR, real_type, imag);
11582 break;
11583 default:
11584 gcc_unreachable();
11585 }
11586 }
11587 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11588 goto return_build_binary_op;
11589 }
11590
3e4093b6
RS
11591 /* For certain operations (which identify themselves by shorten != 0)
11592 if both args were extended from the same smaller type,
11593 do the arithmetic in that type and then extend.
400fbf9f 11594
3e4093b6
RS
11595 shorten !=0 and !=1 indicates a bitwise operation.
11596 For them, this optimization is safe only if
11597 both args are zero-extended or both are sign-extended.
11598 Otherwise, we might change the result.
11599 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11600 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11601
3e4093b6
RS
11602 if (shorten && none_complex)
11603 {
3e4093b6 11604 final_type = result_type;
b8698a0f 11605 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11606 shorten == -1);
3e4093b6 11607 }
88a3dbc1 11608
3e4093b6 11609 /* Shifts can be shortened if shifting right. */
2f6e4e97 11610
3e4093b6
RS
11611 if (short_shift)
11612 {
11613 int unsigned_arg;
11614 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11615
3e4093b6 11616 final_type = result_type;
abe80e6d 11617
3e4093b6 11618 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11619 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11620
3e4093b6 11621 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11622 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11623 /* We can shorten only if the shift count is less than the
11624 number of bits in the smaller type size. */
11625 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11626 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11627 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11628 {
11629 /* Do an unsigned shift if the operand was zero-extended. */
11630 result_type
11631 = c_common_signed_or_unsigned_type (unsigned_arg,
11632 TREE_TYPE (arg0));
11633 /* Convert value-to-be-shifted to that type. */
11634 if (TREE_TYPE (op0) != result_type)
11635 op0 = convert (result_type, op0);
11636 converted = 1;
abe80e6d 11637 }
88a3dbc1
RK
11638 }
11639
3e4093b6
RS
11640 /* Comparison operations are shortened too but differently.
11641 They identify themselves by setting short_compare = 1. */
56cb9733 11642
3e4093b6
RS
11643 if (short_compare)
11644 {
11645 /* Don't write &op0, etc., because that would prevent op0
11646 from being kept in a register.
11647 Instead, make copies of the our local variables and
11648 pass the copies by reference, then copy them back afterward. */
11649 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11650 enum tree_code xresultcode = resultcode;
11651 tree val
393e8e8b
MP
11652 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11653 &xresultcode);
8f17b5c5 11654
3e4093b6 11655 if (val != 0)
c9f9eb5d
AH
11656 {
11657 ret = val;
11658 goto return_build_binary_op;
11659 }
8f17b5c5 11660
3e4093b6
RS
11661 op0 = xop0, op1 = xop1;
11662 converted = 1;
11663 resultcode = xresultcode;
8f17b5c5 11664
7d882b83 11665 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11666 {
11667 bool op0_maybe_const = true;
11668 bool op1_maybe_const = true;
11669 tree orig_op0_folded, orig_op1_folded;
11670
11671 if (in_late_binary_op)
11672 {
11673 orig_op0_folded = orig_op0;
11674 orig_op1_folded = orig_op1;
11675 }
11676 else
11677 {
11678 /* Fold for the sake of possible warnings, as in
11679 build_conditional_expr. This requires the
11680 "original" values to be folded, not just op0 and
11681 op1. */
f5178456 11682 c_inhibit_evaluation_warnings++;
928c19bb
JM
11683 op0 = c_fully_fold (op0, require_constant_value,
11684 &op0_maybe_const);
11685 op1 = c_fully_fold (op1, require_constant_value,
11686 &op1_maybe_const);
f5178456 11687 c_inhibit_evaluation_warnings--;
928c19bb
JM
11688 orig_op0_folded = c_fully_fold (orig_op0,
11689 require_constant_value,
11690 NULL);
11691 orig_op1_folded = c_fully_fold (orig_op1,
11692 require_constant_value,
11693 NULL);
11694 }
11695
11696 if (warn_sign_compare)
11697 warn_for_sign_compare (location, orig_op0_folded,
11698 orig_op1_folded, op0, op1,
11699 result_type, resultcode);
5c2f94b4 11700 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11701 {
11702 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11703 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11704 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11705 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11706 }
3e4093b6 11707 }
2ad1815d 11708 }
64094f6a 11709 }
64094f6a 11710
3e4093b6
RS
11711 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11712 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11713 Then the expression will be built.
11714 It will be given type FINAL_TYPE if that is nonzero;
11715 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11716
3e4093b6
RS
11717 if (!result_type)
11718 {
745e411d
DM
11719 gcc_rich_location richloc (location);
11720 richloc.maybe_add_expr (orig_op0);
11721 richloc.maybe_add_expr (orig_op1);
11722 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11723 return error_mark_node;
11724 }
400fbf9f 11725
3e4093b6 11726 if (build_type == NULL_TREE)
8ce94e44
JM
11727 {
11728 build_type = result_type;
180f8dbb
JM
11729 if ((type0 != orig_type0 || type1 != orig_type1)
11730 && !boolean_op)
8ce94e44
JM
11731 {
11732 gcc_assert (may_need_excess_precision && common);
2d2e923f 11733 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11734 }
11735 }
400fbf9f 11736
2d2e923f
MLI
11737 if (!converted)
11738 {
68fca595
MP
11739 op0 = ep_convert_and_check (location, result_type, op0,
11740 semantic_result_type);
11741 op1 = ep_convert_and_check (location, result_type, op1,
11742 semantic_result_type);
2d2e923f
MLI
11743
11744 /* This can happen if one operand has a vector type, and the other
11745 has a different type. */
11746 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11747 return error_mark_node;
11748 }
11749
f8ed5150
MP
11750 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11751 | SANITIZE_FLOAT_DIVIDE))
802ac282 11752 && do_ubsan_in_current_function ()
b2aaf235
MP
11753 && (doing_div_or_mod || doing_shift)
11754 && !require_constant_value)
de5a5fa1
MP
11755 {
11756 /* OP0 and/or OP1 might have side-effects. */
11757 op0 = c_save_expr (op0);
11758 op1 = c_save_expr (op1);
11759 op0 = c_fully_fold (op0, false, NULL);
11760 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11761 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11762 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11763 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11764 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11765 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11766 }
11767
c9f9eb5d 11768 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11769 if (int_const_or_overflow)
11770 ret = (require_constant_value
db3927fb
AH
11771 ? fold_build2_initializer_loc (location, resultcode, build_type,
11772 op0, op1)
11773 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11774 else
11775 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11776 if (final_type != 0)
11777 ret = convert (final_type, ret);
11778
11779 return_build_binary_op:
11780 gcc_assert (ret != error_mark_node);
928c19bb
JM
11781 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11782 ret = (int_operands
11783 ? note_integer_operands (ret)
11784 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11785 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11786 && !in_late_binary_op)
11787 ret = note_integer_operands (ret);
2d2e923f
MLI
11788 if (semantic_result_type)
11789 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11790 protected_set_expr_location (ret, location);
de5a5fa1 11791
a24d975c 11792 if (instrument_expr != NULL)
de5a5fa1
MP
11793 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11794 instrument_expr, ret);
11795
c9f9eb5d 11796 return ret;
400fbf9f 11797}
85498824
JM
11798
11799
11800/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11801 purpose. LOCATION is the source location for the expression. */
85498824
JM
11802
11803tree
ba47d38d 11804c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11805{
928c19bb
JM
11806 bool int_const, int_operands;
11807
85498824
JM
11808 switch (TREE_CODE (TREE_TYPE (expr)))
11809 {
11810 case ARRAY_TYPE:
ba47d38d 11811 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11812 return error_mark_node;
11813
11814 case RECORD_TYPE:
ba47d38d 11815 error_at (location, "used struct type value where scalar is required");
85498824
JM
11816 return error_mark_node;
11817
11818 case UNION_TYPE:
ba47d38d 11819 error_at (location, "used union type value where scalar is required");
85498824
JM
11820 return error_mark_node;
11821
04af8788
NP
11822 case VOID_TYPE:
11823 error_at (location, "void value not ignored as it ought to be");
11824 return error_mark_node;
11825
1807ffc1
MS
11826 case POINTER_TYPE:
11827 if (reject_gcc_builtin (expr))
11828 return error_mark_node;
11829 break;
11830
46bdb9cf
JM
11831 case FUNCTION_TYPE:
11832 gcc_unreachable ();
11833
d246ab4f
AS
11834 case VECTOR_TYPE:
11835 error_at (location, "used vector type where scalar is required");
11836 return error_mark_node;
11837
85498824
JM
11838 default:
11839 break;
11840 }
11841
928c19bb
JM
11842 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11843 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11844 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11845 {
11846 expr = remove_c_maybe_const_expr (expr);
11847 expr = build2 (NE_EXPR, integer_type_node, expr,
11848 convert (TREE_TYPE (expr), integer_zero_node));
11849 expr = note_integer_operands (expr);
11850 }
11851 else
11852 /* ??? Should we also give an error for vectors rather than leaving
11853 those to give errors later? */
11854 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11855
11856 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11857 {
11858 if (TREE_OVERFLOW (expr))
11859 return expr;
11860 else
11861 return note_integer_operands (expr);
11862 }
11863 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11864 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11865 return expr;
85498824 11866}
73f397d4
JM
11867\f
11868
11869/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11870 required. */
11871
11872tree
51eed280 11873c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11874{
11875 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11876 {
11877 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11878 /* Executing a compound literal inside a function reinitializes
11879 it. */
11880 if (!TREE_STATIC (decl))
11881 *se = true;
11882 return decl;
11883 }
11884 else
11885 return expr;
11886}
953ff289 11887\f
88bae6f4
TS
11888/* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11889 statement. LOC is the location of the construct. */
41dbbb37
TS
11890
11891tree
88bae6f4
TS
11892c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11893 tree clauses)
41dbbb37 11894{
88bae6f4 11895 body = c_end_compound_stmt (loc, body, true);
41dbbb37 11896
88bae6f4 11897 tree stmt = make_node (code);
41dbbb37 11898 TREE_TYPE (stmt) = void_type_node;
88bae6f4
TS
11899 OMP_BODY (stmt) = body;
11900 OMP_CLAUSES (stmt) = clauses;
41dbbb37
TS
11901 SET_EXPR_LOCATION (stmt, loc);
11902
11903 return add_stmt (stmt);
11904}
11905
11906/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11907 statement. LOC is the location of the OACC_DATA. */
11908
11909tree
11910c_finish_oacc_data (location_t loc, tree clauses, tree block)
11911{
11912 tree stmt;
11913
11914 block = c_end_compound_stmt (loc, block, true);
11915
11916 stmt = make_node (OACC_DATA);
11917 TREE_TYPE (stmt) = void_type_node;
11918 OACC_DATA_CLAUSES (stmt) = clauses;
11919 OACC_DATA_BODY (stmt) = block;
11920 SET_EXPR_LOCATION (stmt, loc);
11921
11922 return add_stmt (stmt);
11923}
11924
37d5ad46
JB
11925/* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11926 statement. LOC is the location of the OACC_HOST_DATA. */
11927
11928tree
11929c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
11930{
11931 tree stmt;
11932
11933 block = c_end_compound_stmt (loc, block, true);
11934
11935 stmt = make_node (OACC_HOST_DATA);
11936 TREE_TYPE (stmt) = void_type_node;
11937 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
11938 OACC_HOST_DATA_BODY (stmt) = block;
11939 SET_EXPR_LOCATION (stmt, loc);
11940
11941 return add_stmt (stmt);
11942}
11943
c0220ea4 11944/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11945
11946tree
11947c_begin_omp_parallel (void)
11948{
11949 tree block;
11950
11951 keep_next_level ();
11952 block = c_begin_compound_stmt (true);
11953
11954 return block;
11955}
11956
c2255bc4
AH
11957/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11958 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11959
953ff289 11960tree
c2255bc4 11961c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11962{
11963 tree stmt;
11964
c2255bc4 11965 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11966
11967 stmt = make_node (OMP_PARALLEL);
11968 TREE_TYPE (stmt) = void_type_node;
11969 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11970 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11971 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11972
11973 return add_stmt (stmt);
11974}
11975
a68ab351
JJ
11976/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11977
11978tree
11979c_begin_omp_task (void)
11980{
11981 tree block;
11982
11983 keep_next_level ();
11984 block = c_begin_compound_stmt (true);
11985
11986 return block;
11987}
11988
c2255bc4
AH
11989/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11990 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11991
11992tree
c2255bc4 11993c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11994{
11995 tree stmt;
11996
c2255bc4 11997 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11998
11999 stmt = make_node (OMP_TASK);
12000 TREE_TYPE (stmt) = void_type_node;
12001 OMP_TASK_CLAUSES (stmt) = clauses;
12002 OMP_TASK_BODY (stmt) = block;
c2255bc4 12003 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
12004
12005 return add_stmt (stmt);
12006}
12007
acf0174b
JJ
12008/* Generate GOMP_cancel call for #pragma omp cancel. */
12009
12010void
12011c_finish_omp_cancel (location_t loc, tree clauses)
12012{
12013 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12014 int mask = 0;
629b3d75 12015 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
acf0174b 12016 mask = 1;
629b3d75 12017 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
acf0174b 12018 mask = 2;
629b3d75 12019 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
acf0174b 12020 mask = 4;
629b3d75 12021 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
acf0174b
JJ
12022 mask = 8;
12023 else
12024 {
54d19c3b 12025 error_at (loc, "%<#pragma omp cancel%> must specify one of "
acf0174b
JJ
12026 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12027 "clauses");
12028 return;
12029 }
629b3d75 12030 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
acf0174b
JJ
12031 if (ifc != NULL_TREE)
12032 {
12033 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12034 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12035 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12036 build_zero_cst (type));
12037 }
12038 else
12039 ifc = boolean_true_node;
12040 tree stmt = build_call_expr_loc (loc, fn, 2,
12041 build_int_cst (integer_type_node, mask),
12042 ifc);
12043 add_stmt (stmt);
12044}
12045
12046/* Generate GOMP_cancellation_point call for
12047 #pragma omp cancellation point. */
12048
12049void
12050c_finish_omp_cancellation_point (location_t loc, tree clauses)
12051{
12052 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12053 int mask = 0;
629b3d75 12054 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
acf0174b 12055 mask = 1;
629b3d75 12056 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
acf0174b 12057 mask = 2;
629b3d75 12058 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
acf0174b 12059 mask = 4;
629b3d75 12060 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
acf0174b
JJ
12061 mask = 8;
12062 else
12063 {
54d19c3b 12064 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
acf0174b
JJ
12065 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12066 "clauses");
12067 return;
12068 }
12069 tree stmt = build_call_expr_loc (loc, fn, 1,
12070 build_int_cst (integer_type_node, mask));
12071 add_stmt (stmt);
12072}
12073
12074/* Helper function for handle_omp_array_sections. Called recursively
12075 to handle multiple array-section-subscripts. C is the clause,
12076 T current expression (initially OMP_CLAUSE_DECL), which is either
12077 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12078 expression if specified, TREE_VALUE length expression if specified,
12079 TREE_CHAIN is what it has been specified after, or some decl.
12080 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12081 set to true if any of the array-section-subscript could have length
12082 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12083 first array-section-subscript which is known not to have length
12084 of one. Given say:
12085 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12086 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12087 all are or may have length of 1, array-section-subscript [:2] is the
d9a6bd32 12088 first one known not to have length 1. For array-section-subscript
acf0174b
JJ
12089 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12090 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12091 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12092 case though, as some lengths could be zero. */
12093
12094static tree
12095handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
d9a6bd32 12096 bool &maybe_zero_len, unsigned int &first_non_one,
e46c7770 12097 enum c_omp_region_type ort)
acf0174b
JJ
12098{
12099 tree ret, low_bound, length, type;
12100 if (TREE_CODE (t) != TREE_LIST)
12101 {
7a0ca710 12102 if (error_operand_p (t))
acf0174b 12103 return error_mark_node;
d9a6bd32 12104 ret = t;
9dc5773f
JJ
12105 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12106 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12107 {
12108 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12109 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12110 return error_mark_node;
12111 }
d9a6bd32 12112 if (TREE_CODE (t) == COMPONENT_REF
e46c7770 12113 && ort == C_ORT_OMP
d9a6bd32
JJ
12114 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12115 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12116 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12117 {
12118 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12119 {
12120 error_at (OMP_CLAUSE_LOCATION (c),
12121 "bit-field %qE in %qs clause",
12122 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12123 return error_mark_node;
12124 }
12125 while (TREE_CODE (t) == COMPONENT_REF)
12126 {
12127 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12128 {
12129 error_at (OMP_CLAUSE_LOCATION (c),
12130 "%qE is a member of a union", t);
12131 return error_mark_node;
12132 }
12133 t = TREE_OPERAND (t, 0);
12134 }
12135 }
0ae9bd27 12136 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12137 {
12138 if (DECL_P (t))
12139 error_at (OMP_CLAUSE_LOCATION (c),
12140 "%qD is not a variable in %qs clause", t,
12141 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12142 else
12143 error_at (OMP_CLAUSE_LOCATION (c),
12144 "%qE is not a variable in %qs clause", t,
12145 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12146 return error_mark_node;
12147 }
12148 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
9dc5773f
JJ
12149 && TYPE_ATOMIC (TREE_TYPE (t)))
12150 {
12151 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12152 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12153 return error_mark_node;
12154 }
12155 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12156 && VAR_P (t)
12157 && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
12158 {
12159 error_at (OMP_CLAUSE_LOCATION (c),
12160 "%qD is threadprivate variable in %qs clause", t,
12161 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12162 return error_mark_node;
12163 }
9dc5773f
JJ
12164 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12165 && TYPE_ATOMIC (TREE_TYPE (t))
12166 && POINTER_TYPE_P (TREE_TYPE (t)))
12167 {
12168 /* If the array section is pointer based and the pointer
12169 itself is _Atomic qualified, we need to atomically load
12170 the pointer. */
12171 c_expr expr;
12172 memset (&expr, 0, sizeof (expr));
12173 expr.value = ret;
12174 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12175 expr, false, false);
12176 ret = expr.value;
12177 }
d9a6bd32 12178 return ret;
acf0174b
JJ
12179 }
12180
12181 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
e46c7770 12182 maybe_zero_len, first_non_one, ort);
acf0174b
JJ
12183 if (ret == error_mark_node || ret == NULL_TREE)
12184 return ret;
12185
12186 type = TREE_TYPE (ret);
12187 low_bound = TREE_PURPOSE (t);
12188 length = TREE_VALUE (t);
12189
12190 if (low_bound == error_mark_node || length == error_mark_node)
12191 return error_mark_node;
12192
12193 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12194 {
12195 error_at (OMP_CLAUSE_LOCATION (c),
12196 "low bound %qE of array section does not have integral type",
12197 low_bound);
12198 return error_mark_node;
12199 }
12200 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12201 {
12202 error_at (OMP_CLAUSE_LOCATION (c),
12203 "length %qE of array section does not have integral type",
12204 length);
12205 return error_mark_node;
12206 }
12207 if (low_bound
12208 && TREE_CODE (low_bound) == INTEGER_CST
12209 && TYPE_PRECISION (TREE_TYPE (low_bound))
12210 > TYPE_PRECISION (sizetype))
12211 low_bound = fold_convert (sizetype, low_bound);
12212 if (length
12213 && TREE_CODE (length) == INTEGER_CST
12214 && TYPE_PRECISION (TREE_TYPE (length))
12215 > TYPE_PRECISION (sizetype))
12216 length = fold_convert (sizetype, length);
12217 if (low_bound == NULL_TREE)
12218 low_bound = integer_zero_node;
12219
12220 if (length != NULL_TREE)
12221 {
12222 if (!integer_nonzerop (length))
d9a6bd32
JJ
12223 {
12224 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12225 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12226 {
12227 if (integer_zerop (length))
12228 {
12229 error_at (OMP_CLAUSE_LOCATION (c),
12230 "zero length array section in %qs clause",
12231 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12232 return error_mark_node;
12233 }
12234 }
12235 else
12236 maybe_zero_len = true;
12237 }
acf0174b
JJ
12238 if (first_non_one == types.length ()
12239 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12240 first_non_one++;
12241 }
12242 if (TREE_CODE (type) == ARRAY_TYPE)
12243 {
12244 if (length == NULL_TREE
12245 && (TYPE_DOMAIN (type) == NULL_TREE
12246 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12247 {
12248 error_at (OMP_CLAUSE_LOCATION (c),
12249 "for unknown bound array type length expression must "
12250 "be specified");
12251 return error_mark_node;
12252 }
12253 if (TREE_CODE (low_bound) == INTEGER_CST
12254 && tree_int_cst_sgn (low_bound) == -1)
12255 {
12256 error_at (OMP_CLAUSE_LOCATION (c),
12257 "negative low bound in array section in %qs clause",
12258 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12259 return error_mark_node;
12260 }
12261 if (length != NULL_TREE
12262 && TREE_CODE (length) == INTEGER_CST
12263 && tree_int_cst_sgn (length) == -1)
12264 {
12265 error_at (OMP_CLAUSE_LOCATION (c),
12266 "negative length in array section in %qs clause",
12267 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12268 return error_mark_node;
12269 }
12270 if (TYPE_DOMAIN (type)
12271 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12272 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12273 == INTEGER_CST)
12274 {
12275 tree size = size_binop (PLUS_EXPR,
12276 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12277 size_one_node);
12278 if (TREE_CODE (low_bound) == INTEGER_CST)
12279 {
12280 if (tree_int_cst_lt (size, low_bound))
12281 {
12282 error_at (OMP_CLAUSE_LOCATION (c),
12283 "low bound %qE above array section size "
12284 "in %qs clause", low_bound,
12285 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12286 return error_mark_node;
12287 }
12288 if (tree_int_cst_equal (size, low_bound))
d9a6bd32
JJ
12289 {
12290 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12291 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12292 {
12293 error_at (OMP_CLAUSE_LOCATION (c),
12294 "zero length array section in %qs clause",
12295 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12296 return error_mark_node;
12297 }
12298 maybe_zero_len = true;
12299 }
acf0174b
JJ
12300 else if (length == NULL_TREE
12301 && first_non_one == types.length ()
12302 && tree_int_cst_equal
12303 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12304 low_bound))
12305 first_non_one++;
12306 }
12307 else if (length == NULL_TREE)
12308 {
d9a6bd32
JJ
12309 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12310 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12311 maybe_zero_len = true;
acf0174b
JJ
12312 if (first_non_one == types.length ())
12313 first_non_one++;
12314 }
12315 if (length && TREE_CODE (length) == INTEGER_CST)
12316 {
12317 if (tree_int_cst_lt (size, length))
12318 {
12319 error_at (OMP_CLAUSE_LOCATION (c),
12320 "length %qE above array section size "
12321 "in %qs clause", length,
12322 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12323 return error_mark_node;
12324 }
12325 if (TREE_CODE (low_bound) == INTEGER_CST)
12326 {
12327 tree lbpluslen
12328 = size_binop (PLUS_EXPR,
12329 fold_convert (sizetype, low_bound),
12330 fold_convert (sizetype, length));
12331 if (TREE_CODE (lbpluslen) == INTEGER_CST
12332 && tree_int_cst_lt (size, lbpluslen))
12333 {
12334 error_at (OMP_CLAUSE_LOCATION (c),
12335 "high bound %qE above array section size "
12336 "in %qs clause", lbpluslen,
12337 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12338 return error_mark_node;
12339 }
12340 }
12341 }
12342 }
12343 else if (length == NULL_TREE)
12344 {
d9a6bd32
JJ
12345 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12346 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12347 maybe_zero_len = true;
acf0174b
JJ
12348 if (first_non_one == types.length ())
12349 first_non_one++;
12350 }
12351
12352 /* For [lb:] we will need to evaluate lb more than once. */
12353 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12354 {
12355 tree lb = c_save_expr (low_bound);
12356 if (lb != low_bound)
12357 {
12358 TREE_PURPOSE (t) = lb;
12359 low_bound = lb;
12360 }
12361 }
12362 }
12363 else if (TREE_CODE (type) == POINTER_TYPE)
12364 {
12365 if (length == NULL_TREE)
12366 {
12367 error_at (OMP_CLAUSE_LOCATION (c),
12368 "for pointer type length expression must be specified");
12369 return error_mark_node;
12370 }
d9a6bd32
JJ
12371 if (length != NULL_TREE
12372 && TREE_CODE (length) == INTEGER_CST
12373 && tree_int_cst_sgn (length) == -1)
12374 {
12375 error_at (OMP_CLAUSE_LOCATION (c),
12376 "negative length in array section in %qs clause",
12377 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12378 return error_mark_node;
12379 }
acf0174b
JJ
12380 /* If there is a pointer type anywhere but in the very first
12381 array-section-subscript, the array section can't be contiguous. */
12382 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12383 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12384 {
12385 error_at (OMP_CLAUSE_LOCATION (c),
12386 "array section is not contiguous in %qs clause",
12387 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12388 return error_mark_node;
12389 }
12390 }
12391 else
12392 {
12393 error_at (OMP_CLAUSE_LOCATION (c),
12394 "%qE does not have pointer or array type", ret);
12395 return error_mark_node;
12396 }
12397 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12398 types.safe_push (TREE_TYPE (ret));
12399 /* We will need to evaluate lb more than once. */
12400 tree lb = c_save_expr (low_bound);
12401 if (lb != low_bound)
12402 {
12403 TREE_PURPOSE (t) = lb;
12404 low_bound = lb;
12405 }
12406 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12407 return ret;
12408}
12409
12410/* Handle array sections for clause C. */
12411
12412static bool
e46c7770 12413handle_omp_array_sections (tree c, enum c_omp_region_type ort)
acf0174b
JJ
12414{
12415 bool maybe_zero_len = false;
12416 unsigned int first_non_one = 0;
d9a6bd32 12417 auto_vec<tree, 10> types;
acf0174b 12418 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
d9a6bd32 12419 maybe_zero_len, first_non_one,
e46c7770 12420 ort);
acf0174b 12421 if (first == error_mark_node)
d9a6bd32 12422 return true;
acf0174b 12423 if (first == NULL_TREE)
d9a6bd32 12424 return false;
acf0174b
JJ
12425 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12426 {
12427 tree t = OMP_CLAUSE_DECL (c);
12428 tree tem = NULL_TREE;
acf0174b
JJ
12429 /* Need to evaluate side effects in the length expressions
12430 if any. */
12431 while (TREE_CODE (t) == TREE_LIST)
12432 {
12433 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12434 {
12435 if (tem == NULL_TREE)
12436 tem = TREE_VALUE (t);
12437 else
12438 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12439 TREE_VALUE (t), tem);
12440 }
12441 t = TREE_CHAIN (t);
12442 }
12443 if (tem)
12444 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12445 first = c_fully_fold (first, false, NULL);
12446 OMP_CLAUSE_DECL (c) = first;
12447 }
12448 else
12449 {
12450 unsigned int num = types.length (), i;
12451 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12452 tree condition = NULL_TREE;
12453
12454 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12455 maybe_zero_len = true;
12456
12457 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12458 t = TREE_CHAIN (t))
12459 {
12460 tree low_bound = TREE_PURPOSE (t);
12461 tree length = TREE_VALUE (t);
12462
12463 i--;
12464 if (low_bound
12465 && TREE_CODE (low_bound) == INTEGER_CST
12466 && TYPE_PRECISION (TREE_TYPE (low_bound))
12467 > TYPE_PRECISION (sizetype))
12468 low_bound = fold_convert (sizetype, low_bound);
12469 if (length
12470 && TREE_CODE (length) == INTEGER_CST
12471 && TYPE_PRECISION (TREE_TYPE (length))
12472 > TYPE_PRECISION (sizetype))
12473 length = fold_convert (sizetype, length);
12474 if (low_bound == NULL_TREE)
12475 low_bound = integer_zero_node;
12476 if (!maybe_zero_len && i > first_non_one)
12477 {
12478 if (integer_nonzerop (low_bound))
12479 goto do_warn_noncontiguous;
12480 if (length != NULL_TREE
12481 && TREE_CODE (length) == INTEGER_CST
12482 && TYPE_DOMAIN (types[i])
12483 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12484 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12485 == INTEGER_CST)
12486 {
12487 tree size;
12488 size = size_binop (PLUS_EXPR,
12489 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12490 size_one_node);
12491 if (!tree_int_cst_equal (length, size))
12492 {
12493 do_warn_noncontiguous:
12494 error_at (OMP_CLAUSE_LOCATION (c),
12495 "array section is not contiguous in %qs "
12496 "clause",
12497 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
acf0174b
JJ
12498 return true;
12499 }
12500 }
12501 if (length != NULL_TREE
12502 && TREE_SIDE_EFFECTS (length))
12503 {
12504 if (side_effects == NULL_TREE)
12505 side_effects = length;
12506 else
12507 side_effects = build2 (COMPOUND_EXPR,
12508 TREE_TYPE (side_effects),
12509 length, side_effects);
12510 }
12511 }
12512 else
12513 {
12514 tree l;
12515
d9a6bd32
JJ
12516 if (i > first_non_one
12517 && ((length && integer_nonzerop (length))
12518 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
acf0174b
JJ
12519 continue;
12520 if (length)
12521 l = fold_convert (sizetype, length);
12522 else
12523 {
12524 l = size_binop (PLUS_EXPR,
12525 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12526 size_one_node);
12527 l = size_binop (MINUS_EXPR, l,
12528 fold_convert (sizetype, low_bound));
12529 }
12530 if (i > first_non_one)
12531 {
12532 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12533 size_zero_node);
12534 if (condition == NULL_TREE)
12535 condition = l;
12536 else
12537 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12538 l, condition);
12539 }
12540 else if (size == NULL_TREE)
12541 {
12542 size = size_in_bytes (TREE_TYPE (types[i]));
d9a6bd32
JJ
12543 tree eltype = TREE_TYPE (types[num - 1]);
12544 while (TREE_CODE (eltype) == ARRAY_TYPE)
12545 eltype = TREE_TYPE (eltype);
12546 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12547 {
12548 if (integer_zerop (size)
12549 || integer_zerop (size_in_bytes (eltype)))
12550 {
12551 error_at (OMP_CLAUSE_LOCATION (c),
12552 "zero length array section in %qs clause",
12553 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12554 return error_mark_node;
12555 }
12556 size = size_binop (EXACT_DIV_EXPR, size,
12557 size_in_bytes (eltype));
12558 }
acf0174b
JJ
12559 size = size_binop (MULT_EXPR, size, l);
12560 if (condition)
12561 size = fold_build3 (COND_EXPR, sizetype, condition,
12562 size, size_zero_node);
12563 }
12564 else
12565 size = size_binop (MULT_EXPR, size, l);
12566 }
12567 }
acf0174b
JJ
12568 if (side_effects)
12569 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
d9a6bd32
JJ
12570 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12571 {
12572 size = size_binop (MINUS_EXPR, size, size_one_node);
12573 size = c_fully_fold (size, false, NULL);
12574 tree index_type = build_index_type (size);
12575 tree eltype = TREE_TYPE (first);
12576 while (TREE_CODE (eltype) == ARRAY_TYPE)
12577 eltype = TREE_TYPE (eltype);
12578 tree type = build_array_type (eltype, index_type);
12579 tree ptype = build_pointer_type (eltype);
12580 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12581 t = build_fold_addr_expr (t);
e01d41e5
JJ
12582 tree t2 = build_fold_addr_expr (first);
12583 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12584 ptrdiff_type_node, t2);
12585 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12586 ptrdiff_type_node, t2,
12587 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12588 ptrdiff_type_node, t));
12589 t2 = c_fully_fold (t2, false, NULL);
12590 if (tree_fits_shwi_p (t2))
12591 t = build2 (MEM_REF, type, t,
12592 build_int_cst (ptype, tree_to_shwi (t2)));
12593 else
12594 {
12595 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12596 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12597 TREE_TYPE (t), t, t2);
12598 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12599 }
d9a6bd32
JJ
12600 OMP_CLAUSE_DECL (c) = t;
12601 return false;
12602 }
acf0174b
JJ
12603 first = c_fully_fold (first, false, NULL);
12604 OMP_CLAUSE_DECL (c) = first;
12605 if (size)
12606 size = c_fully_fold (size, false, NULL);
12607 OMP_CLAUSE_SIZE (c) = size;
d9a6bd32
JJ
12608 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12609 || (TREE_CODE (t) == COMPONENT_REF
12610 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
acf0174b 12611 return false;
41dbbb37 12612 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
e46c7770 12613 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
d9a6bd32
JJ
12614 switch (OMP_CLAUSE_MAP_KIND (c))
12615 {
12616 case GOMP_MAP_ALLOC:
12617 case GOMP_MAP_TO:
12618 case GOMP_MAP_FROM:
12619 case GOMP_MAP_TOFROM:
12620 case GOMP_MAP_ALWAYS_TO:
12621 case GOMP_MAP_ALWAYS_FROM:
12622 case GOMP_MAP_ALWAYS_TOFROM:
12623 case GOMP_MAP_RELEASE:
12624 case GOMP_MAP_DELETE:
b1c9c068
CP
12625 case GOMP_MAP_FORCE_TO:
12626 case GOMP_MAP_FORCE_FROM:
12627 case GOMP_MAP_FORCE_TOFROM:
12628 case GOMP_MAP_FORCE_PRESENT:
d9a6bd32
JJ
12629 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12630 break;
12631 default:
12632 break;
12633 }
acf0174b 12634 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
e46c7770 12635 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
e01d41e5
JJ
12636 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12637 else if (TREE_CODE (t) == COMPONENT_REF)
12638 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12639 else
12640 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12641 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12642 && !c_mark_addressable (t))
acf0174b
JJ
12643 return false;
12644 OMP_CLAUSE_DECL (c2) = t;
12645 t = build_fold_addr_expr (first);
12646 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12647 tree ptr = OMP_CLAUSE_DECL (c2);
12648 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12649 ptr = build_fold_addr_expr (ptr);
12650 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12651 ptrdiff_type_node, t,
12652 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12653 ptrdiff_type_node, ptr));
12654 t = c_fully_fold (t, false, NULL);
12655 OMP_CLAUSE_SIZE (c2) = t;
12656 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12657 OMP_CLAUSE_CHAIN (c) = c2;
12658 }
12659 return false;
12660}
12661
12662/* Helper function of finish_omp_clauses. Clone STMT as if we were making
12663 an inline call. But, remap
12664 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12665 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12666
12667static tree
12668c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12669 tree decl, tree placeholder)
12670{
12671 copy_body_data id;
b787e7a2 12672 hash_map<tree, tree> decl_map;
acf0174b 12673
b787e7a2
TS
12674 decl_map.put (omp_decl1, placeholder);
12675 decl_map.put (omp_decl2, decl);
acf0174b
JJ
12676 memset (&id, 0, sizeof (id));
12677 id.src_fn = DECL_CONTEXT (omp_decl1);
12678 id.dst_fn = current_function_decl;
12679 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 12680 id.decl_map = &decl_map;
acf0174b
JJ
12681
12682 id.copy_decl = copy_decl_no_change;
12683 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12684 id.transform_new_cfg = true;
12685 id.transform_return_to_modify = false;
12686 id.transform_lang_insert_block = NULL;
12687 id.eh_lp_nr = 0;
12688 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
12689 return stmt;
12690}
12691
12692/* Helper function of c_finish_omp_clauses, called via walk_tree.
12693 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12694
12695static tree
12696c_find_omp_placeholder_r (tree *tp, int *, void *data)
12697{
12698 if (*tp == (tree) data)
12699 return *tp;
12700 return NULL_TREE;
12701}
12702
41dbbb37 12703/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
12704 Remove any elements from the list that are invalid. */
12705
12706tree
77886428 12707c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
953ff289
DN
12708{
12709 bitmap_head generic_head, firstprivate_head, lastprivate_head;
e46c7770 12710 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
d9a6bd32
JJ
12711 tree c, t, type, *pc;
12712 tree simdlen = NULL_TREE, safelen = NULL_TREE;
acf0174b
JJ
12713 bool branch_seen = false;
12714 bool copyprivate_seen = false;
e01d41e5 12715 bool linear_variable_step_check = false;
acf0174b 12716 tree *nowait_clause = NULL;
e01d41e5
JJ
12717 bool ordered_seen = false;
12718 tree schedule_clause = NULL_TREE;
b605f663 12719 bool oacc_async = false;
953ff289
DN
12720
12721 bitmap_obstack_initialize (NULL);
12722 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12723 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12724 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 12725 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
d9a6bd32
JJ
12726 bitmap_initialize (&map_head, &bitmap_default_obstack);
12727 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
e46c7770 12728 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
953ff289 12729
b605f663
CLT
12730 if (ort & C_ORT_ACC)
12731 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12732 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12733 {
12734 oacc_async = true;
12735 break;
12736 }
9dc5773f 12737
953ff289
DN
12738 for (pc = &clauses, c = clauses; c ; c = *pc)
12739 {
12740 bool remove = false;
12741 bool need_complete = false;
12742 bool need_implicitly_determined = false;
12743
aaf46ef9 12744 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
12745 {
12746 case OMP_CLAUSE_SHARED:
953ff289
DN
12747 need_implicitly_determined = true;
12748 goto check_dup_generic;
12749
12750 case OMP_CLAUSE_PRIVATE:
953ff289
DN
12751 need_complete = true;
12752 need_implicitly_determined = true;
12753 goto check_dup_generic;
12754
12755 case OMP_CLAUSE_REDUCTION:
953ff289
DN
12756 need_implicitly_determined = true;
12757 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12758 if (TREE_CODE (t) == TREE_LIST)
12759 {
e46c7770 12760 if (handle_omp_array_sections (c, ort))
d9a6bd32
JJ
12761 {
12762 remove = true;
12763 break;
12764 }
12765
12766 t = OMP_CLAUSE_DECL (c);
12767 }
4f2e1536 12768 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
d9a6bd32
JJ
12769 if (t == error_mark_node)
12770 {
12771 remove = true;
12772 break;
12773 }
b605f663
CLT
12774 if (oacc_async)
12775 c_mark_addressable (t);
d9a6bd32
JJ
12776 type = TREE_TYPE (t);
12777 if (TREE_CODE (t) == MEM_REF)
12778 type = TREE_TYPE (type);
12779 if (TREE_CODE (type) == ARRAY_TYPE)
12780 {
12781 tree oatype = type;
12782 gcc_assert (TREE_CODE (t) != MEM_REF);
12783 while (TREE_CODE (type) == ARRAY_TYPE)
12784 type = TREE_TYPE (type);
12785 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12786 {
12787 error_at (OMP_CLAUSE_LOCATION (c),
12788 "%qD in %<reduction%> clause is a zero size array",
12789 t);
12790 remove = true;
12791 break;
12792 }
12793 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12794 TYPE_SIZE_UNIT (type));
12795 if (integer_zerop (size))
12796 {
12797 error_at (OMP_CLAUSE_LOCATION (c),
12798 "%qD in %<reduction%> clause is a zero size array",
12799 t);
12800 remove = true;
12801 break;
12802 }
12803 size = size_binop (MINUS_EXPR, size, size_one_node);
12804 tree index_type = build_index_type (size);
12805 tree atype = build_array_type (type, index_type);
12806 tree ptype = build_pointer_type (type);
12807 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12808 t = build_fold_addr_expr (t);
12809 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12810 OMP_CLAUSE_DECL (c) = t;
12811 }
9dc5773f
JJ
12812 if (TYPE_ATOMIC (type))
12813 {
12814 error_at (OMP_CLAUSE_LOCATION (c),
12815 "%<_Atomic%> %qE in %<reduction%> clause", t);
12816 remove = true;
12817 break;
12818 }
acf0174b 12819 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
d9a6bd32
JJ
12820 && (FLOAT_TYPE_P (type)
12821 || TREE_CODE (type) == COMPLEX_TYPE))
953ff289
DN
12822 {
12823 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12824 const char *r_name = NULL;
12825
12826 switch (r_code)
12827 {
12828 case PLUS_EXPR:
12829 case MULT_EXPR:
12830 case MINUS_EXPR:
652fea39 12831 break;
20906c66 12832 case MIN_EXPR:
d9a6bd32 12833 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39
JJ
12834 r_name = "min";
12835 break;
20906c66 12836 case MAX_EXPR:
d9a6bd32 12837 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39 12838 r_name = "max";
953ff289
DN
12839 break;
12840 case BIT_AND_EXPR:
12841 r_name = "&";
12842 break;
12843 case BIT_XOR_EXPR:
12844 r_name = "^";
12845 break;
12846 case BIT_IOR_EXPR:
12847 r_name = "|";
12848 break;
12849 case TRUTH_ANDIF_EXPR:
d9a6bd32 12850 if (FLOAT_TYPE_P (type))
652fea39 12851 r_name = "&&";
953ff289
DN
12852 break;
12853 case TRUTH_ORIF_EXPR:
d9a6bd32 12854 if (FLOAT_TYPE_P (type))
652fea39 12855 r_name = "||";
953ff289
DN
12856 break;
12857 default:
12858 gcc_unreachable ();
12859 }
12860 if (r_name)
12861 {
c2255bc4
AH
12862 error_at (OMP_CLAUSE_LOCATION (c),
12863 "%qE has invalid type for %<reduction(%s)%>",
12864 t, r_name);
953ff289 12865 remove = true;
ee1d5a02 12866 break;
953ff289
DN
12867 }
12868 }
acf0174b
JJ
12869 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12870 {
12871 error_at (OMP_CLAUSE_LOCATION (c),
d9a6bd32 12872 "user defined reduction not found for %qE", t);
acf0174b 12873 remove = true;
ee1d5a02 12874 break;
acf0174b
JJ
12875 }
12876 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12877 {
12878 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
d9a6bd32 12879 type = TYPE_MAIN_VARIANT (type);
acf0174b
JJ
12880 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12881 VAR_DECL, NULL_TREE, type);
d9a6bd32 12882 tree decl_placeholder = NULL_TREE;
acf0174b
JJ
12883 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12884 DECL_ARTIFICIAL (placeholder) = 1;
12885 DECL_IGNORED_P (placeholder) = 1;
d9a6bd32
JJ
12886 if (TREE_CODE (t) == MEM_REF)
12887 {
12888 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12889 VAR_DECL, NULL_TREE, type);
12890 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12891 DECL_ARTIFICIAL (decl_placeholder) = 1;
12892 DECL_IGNORED_P (decl_placeholder) = 1;
12893 }
acf0174b
JJ
12894 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12895 c_mark_addressable (placeholder);
12896 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
d9a6bd32
JJ
12897 c_mark_addressable (decl_placeholder ? decl_placeholder
12898 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12899 OMP_CLAUSE_REDUCTION_MERGE (c)
12900 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12901 TREE_VEC_ELT (list, 0),
12902 TREE_VEC_ELT (list, 1),
d9a6bd32
JJ
12903 decl_placeholder ? decl_placeholder
12904 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b
JJ
12905 OMP_CLAUSE_REDUCTION_MERGE (c)
12906 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12907 void_type_node, NULL_TREE,
d9a6bd32 12908 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
acf0174b
JJ
12909 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12910 if (TREE_VEC_LENGTH (list) == 6)
12911 {
12912 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
d9a6bd32
JJ
12913 c_mark_addressable (decl_placeholder ? decl_placeholder
12914 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12915 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12916 c_mark_addressable (placeholder);
12917 tree init = TREE_VEC_ELT (list, 5);
12918 if (init == error_mark_node)
12919 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12920 OMP_CLAUSE_REDUCTION_INIT (c)
12921 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12922 TREE_VEC_ELT (list, 3),
d9a6bd32
JJ
12923 decl_placeholder ? decl_placeholder
12924 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b 12925 if (TREE_VEC_ELT (list, 5) == error_mark_node)
d9a6bd32
JJ
12926 {
12927 tree v = decl_placeholder ? decl_placeholder : t;
12928 OMP_CLAUSE_REDUCTION_INIT (c)
12929 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12930 OMP_CLAUSE_REDUCTION_INIT (c));
12931 }
acf0174b
JJ
12932 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12933 c_find_omp_placeholder_r,
12934 placeholder, NULL))
12935 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12936 }
12937 else
12938 {
12939 tree init;
d9a6bd32
JJ
12940 tree v = decl_placeholder ? decl_placeholder : t;
12941 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12942 init = build_constructor (TREE_TYPE (v), NULL);
acf0174b 12943 else
d9a6bd32 12944 init = fold_convert (TREE_TYPE (v), integer_zero_node);
acf0174b 12945 OMP_CLAUSE_REDUCTION_INIT (c)
d9a6bd32 12946 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
acf0174b
JJ
12947 }
12948 OMP_CLAUSE_REDUCTION_INIT (c)
12949 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12950 void_type_node, NULL_TREE,
12951 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12952 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12953 }
d9a6bd32
JJ
12954 if (TREE_CODE (t) == MEM_REF)
12955 {
12956 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12957 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12958 != INTEGER_CST)
12959 {
12960 sorry ("variable length element type in array "
12961 "%<reduction%> clause");
12962 remove = true;
12963 break;
12964 }
12965 t = TREE_OPERAND (t, 0);
e01d41e5
JJ
12966 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12967 t = TREE_OPERAND (t, 0);
d9a6bd32
JJ
12968 if (TREE_CODE (t) == ADDR_EXPR)
12969 t = TREE_OPERAND (t, 0);
12970 }
12971 goto check_dup_generic_t;
953ff289
DN
12972
12973 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
12974 copyprivate_seen = true;
12975 if (nowait_clause)
12976 {
12977 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12978 "%<nowait%> clause must not be used together "
12979 "with %<copyprivate%>");
12980 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12981 nowait_clause = NULL;
12982 }
953ff289
DN
12983 goto check_dup_generic;
12984
12985 case OMP_CLAUSE_COPYIN:
953ff289 12986 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12987 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
953ff289 12988 {
c2255bc4
AH
12989 error_at (OMP_CLAUSE_LOCATION (c),
12990 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12991 remove = true;
ee1d5a02 12992 break;
953ff289
DN
12993 }
12994 goto check_dup_generic;
12995
acf0174b 12996 case OMP_CLAUSE_LINEAR:
77886428 12997 if (ort != C_ORT_OMP_DECLARE_SIMD)
d9a6bd32 12998 need_implicitly_determined = true;
acf0174b 12999 t = OMP_CLAUSE_DECL (c);
77886428 13000 if (ort != C_ORT_OMP_DECLARE_SIMD
d9a6bd32
JJ
13001 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13002 {
13003 error_at (OMP_CLAUSE_LOCATION (c),
13004 "modifier should not be specified in %<linear%> "
13005 "clause on %<simd%> or %<for%> constructs");
13006 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13007 }
77886428 13008 if (ort & C_ORT_CILK)
acf0174b 13009 {
477d4906
IV
13010 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13011 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13012 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13013 {
13014 error_at (OMP_CLAUSE_LOCATION (c),
13015 "linear clause applied to non-integral, "
13016 "non-floating, non-pointer variable with type %qT",
13017 TREE_TYPE (t));
13018 remove = true;
13019 break;
13020 }
13021 }
13022 else
13023 {
13024 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13025 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13026 {
13027 error_at (OMP_CLAUSE_LOCATION (c),
13028 "linear clause applied to non-integral non-pointer "
13029 "variable with type %qT", TREE_TYPE (t));
13030 remove = true;
13031 break;
13032 }
9dc5773f
JJ
13033 if (TYPE_ATOMIC (TREE_TYPE (t)))
13034 {
13035 error_at (OMP_CLAUSE_LOCATION (c),
13036 "%<_Atomic%> %qD in %<linear%> clause", t);
13037 remove = true;
13038 break;
13039 }
acf0174b 13040 }
77886428 13041 if (ort == C_ORT_OMP_DECLARE_SIMD)
e01d41e5
JJ
13042 {
13043 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13044 if (TREE_CODE (s) == PARM_DECL)
13045 {
13046 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13047 /* map_head bitmap is used as uniform_head if
13048 declare_simd. */
13049 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13050 linear_variable_step_check = true;
13051 goto check_dup_generic;
13052 }
13053 if (TREE_CODE (s) != INTEGER_CST)
13054 {
13055 error_at (OMP_CLAUSE_LOCATION (c),
13056 "%<linear%> clause step %qE is neither constant "
13057 "nor a parameter", s);
13058 remove = true;
13059 break;
13060 }
13061 }
acf0174b
JJ
13062 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13063 {
13064 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13065 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13066 OMP_CLAUSE_DECL (c), s);
13067 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
13068 sizetype, fold_convert (sizetype, s),
13069 fold_convert
13070 (sizetype, OMP_CLAUSE_DECL (c)));
acf0174b
JJ
13071 if (s == error_mark_node)
13072 s = size_one_node;
13073 OMP_CLAUSE_LINEAR_STEP (c) = s;
13074 }
da6f124d
JJ
13075 else
13076 OMP_CLAUSE_LINEAR_STEP (c)
13077 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
13078 goto check_dup_generic;
13079
953ff289
DN
13080 check_dup_generic:
13081 t = OMP_CLAUSE_DECL (c);
d9a6bd32 13082 check_dup_generic_t:
0ae9bd27 13083 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13084 {
c2255bc4 13085 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
13086 "%qE is not a variable in clause %qs", t,
13087 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13088 remove = true;
13089 }
e46c7770
CP
13090 else if (ort == C_ORT_ACC
13091 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13092 {
13093 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13094 {
13095 error ("%qD appears more than once in reduction clauses", t);
13096 remove = true;
13097 }
13098 else
13099 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13100 }
953ff289
DN
13101 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13102 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13103 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13104 {
c2255bc4
AH
13105 error_at (OMP_CLAUSE_LOCATION (c),
13106 "%qE appears more than once in data clauses", t);
953ff289
DN
13107 remove = true;
13108 }
e01d41e5
JJ
13109 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13110 && bitmap_bit_p (&map_head, DECL_UID (t)))
13111 {
e46c7770
CP
13112 if (ort == C_ORT_ACC)
13113 error ("%qD appears more than once in data clauses", t);
13114 else
13115 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13116 remove = true;
13117 }
953ff289
DN
13118 else
13119 bitmap_set_bit (&generic_head, DECL_UID (t));
13120 break;
13121
13122 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
13123 t = OMP_CLAUSE_DECL (c);
13124 need_complete = true;
13125 need_implicitly_determined = true;
0ae9bd27 13126 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13127 {
c2255bc4
AH
13128 error_at (OMP_CLAUSE_LOCATION (c),
13129 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
13130 remove = true;
13131 }
13132 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13133 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13134 {
c2255bc4
AH
13135 error_at (OMP_CLAUSE_LOCATION (c),
13136 "%qE appears more than once in data clauses", t);
953ff289
DN
13137 remove = true;
13138 }
e01d41e5
JJ
13139 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13140 {
e46c7770
CP
13141 if (ort == C_ORT_ACC)
13142 error ("%qD appears more than once in data clauses", t);
13143 else
13144 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13145 remove = true;
13146 }
953ff289
DN
13147 else
13148 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13149 break;
13150
13151 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
13152 t = OMP_CLAUSE_DECL (c);
13153 need_complete = true;
13154 need_implicitly_determined = true;
0ae9bd27 13155 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13156 {
c2255bc4
AH
13157 error_at (OMP_CLAUSE_LOCATION (c),
13158 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
13159 remove = true;
13160 }
13161 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13162 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13163 {
c2255bc4
AH
13164 error_at (OMP_CLAUSE_LOCATION (c),
13165 "%qE appears more than once in data clauses", t);
953ff289
DN
13166 remove = true;
13167 }
13168 else
13169 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13170 break;
13171
acf0174b
JJ
13172 case OMP_CLAUSE_ALIGNED:
13173 t = OMP_CLAUSE_DECL (c);
0ae9bd27 13174 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13175 {
13176 error_at (OMP_CLAUSE_LOCATION (c),
13177 "%qE is not a variable in %<aligned%> clause", t);
13178 remove = true;
13179 }
5a9785fb
JJ
13180 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13181 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13182 {
13183 error_at (OMP_CLAUSE_LOCATION (c),
13184 "%qE in %<aligned%> clause is neither a pointer nor "
13185 "an array", t);
13186 remove = true;
13187 }
9dc5773f
JJ
13188 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13189 {
13190 error_at (OMP_CLAUSE_LOCATION (c),
13191 "%<_Atomic%> %qD in %<aligned%> clause", t);
13192 remove = true;
13193 break;
13194 }
acf0174b
JJ
13195 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13196 {
13197 error_at (OMP_CLAUSE_LOCATION (c),
13198 "%qE appears more than once in %<aligned%> clauses",
13199 t);
13200 remove = true;
13201 }
13202 else
13203 bitmap_set_bit (&aligned_head, DECL_UID (t));
13204 break;
13205
13206 case OMP_CLAUSE_DEPEND:
13207 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
13208 if (t == NULL_TREE)
13209 {
13210 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13211 == OMP_CLAUSE_DEPEND_SOURCE);
13212 break;
13213 }
13214 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13215 {
13216 gcc_assert (TREE_CODE (t) == TREE_LIST);
13217 for (; t; t = TREE_CHAIN (t))
13218 {
13219 tree decl = TREE_VALUE (t);
13220 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13221 {
13222 tree offset = TREE_PURPOSE (t);
13223 bool neg = wi::neg_p ((wide_int) offset);
13224 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13225 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13226 neg ? MINUS_EXPR : PLUS_EXPR,
13227 decl, offset);
13228 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
13229 sizetype,
13230 fold_convert (sizetype, t2),
13231 fold_convert (sizetype, decl));
d9a6bd32
JJ
13232 if (t2 == error_mark_node)
13233 {
13234 remove = true;
13235 break;
13236 }
13237 TREE_PURPOSE (t) = t2;
13238 }
13239 }
13240 break;
13241 }
acf0174b
JJ
13242 if (TREE_CODE (t) == TREE_LIST)
13243 {
e46c7770 13244 if (handle_omp_array_sections (c, ort))
acf0174b
JJ
13245 remove = true;
13246 break;
13247 }
13248 if (t == error_mark_node)
13249 remove = true;
0ae9bd27 13250 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13251 {
13252 error_at (OMP_CLAUSE_LOCATION (c),
13253 "%qE is not a variable in %<depend%> clause", t);
13254 remove = true;
13255 }
13256 else if (!c_mark_addressable (t))
13257 remove = true;
13258 break;
13259
13260 case OMP_CLAUSE_MAP:
13261 case OMP_CLAUSE_TO:
13262 case OMP_CLAUSE_FROM:
41dbbb37 13263 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
13264 t = OMP_CLAUSE_DECL (c);
13265 if (TREE_CODE (t) == TREE_LIST)
13266 {
e46c7770 13267 if (handle_omp_array_sections (c, ort))
acf0174b
JJ
13268 remove = true;
13269 else
13270 {
13271 t = OMP_CLAUSE_DECL (c);
b17a8b07 13272 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13273 {
13274 error_at (OMP_CLAUSE_LOCATION (c),
13275 "array section does not have mappable type "
13276 "in %qs clause",
13277 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13278 remove = true;
13279 }
9dc5773f
JJ
13280 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13281 {
13282 error_at (OMP_CLAUSE_LOCATION (c),
13283 "%<_Atomic%> %qE in %qs clause", t,
13284 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13285 remove = true;
13286 }
d9a6bd32
JJ
13287 while (TREE_CODE (t) == ARRAY_REF)
13288 t = TREE_OPERAND (t, 0);
13289 if (TREE_CODE (t) == COMPONENT_REF
13290 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13291 {
13292 while (TREE_CODE (t) == COMPONENT_REF)
13293 t = TREE_OPERAND (t, 0);
13294 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13295 break;
13296 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13297 {
13298 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13299 error ("%qD appears more than once in motion"
13300 " clauses", t);
e46c7770
CP
13301 else if (ort == C_ORT_ACC)
13302 error ("%qD appears more than once in data"
13303 " clauses", t);
d9a6bd32
JJ
13304 else
13305 error ("%qD appears more than once in map"
13306 " clauses", t);
13307 remove = true;
13308 }
13309 else
13310 {
13311 bitmap_set_bit (&map_head, DECL_UID (t));
13312 bitmap_set_bit (&map_field_head, DECL_UID (t));
13313 }
13314 }
acf0174b
JJ
13315 }
13316 break;
13317 }
13318 if (t == error_mark_node)
d9a6bd32
JJ
13319 {
13320 remove = true;
13321 break;
13322 }
13323 if (TREE_CODE (t) == COMPONENT_REF
77886428 13324 && (ort & C_ORT_OMP)
d9a6bd32
JJ
13325 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13326 {
13327 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13328 {
13329 error_at (OMP_CLAUSE_LOCATION (c),
13330 "bit-field %qE in %qs clause",
13331 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13332 remove = true;
13333 }
13334 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13335 {
13336 error_at (OMP_CLAUSE_LOCATION (c),
13337 "%qE does not have a mappable type in %qs clause",
13338 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13339 remove = true;
13340 }
9dc5773f
JJ
13341 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13342 {
13343 error_at (OMP_CLAUSE_LOCATION (c),
13344 "%<_Atomic%> %qE in %qs clause", t,
13345 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13346 remove = true;
13347 }
d9a6bd32
JJ
13348 while (TREE_CODE (t) == COMPONENT_REF)
13349 {
13350 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13351 == UNION_TYPE)
13352 {
13353 error_at (OMP_CLAUSE_LOCATION (c),
13354 "%qE is a member of a union", t);
13355 remove = true;
13356 break;
13357 }
13358 t = TREE_OPERAND (t, 0);
13359 }
13360 if (remove)
13361 break;
13362 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13363 {
e01d41e5 13364 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
d9a6bd32
JJ
13365 break;
13366 }
13367 }
13368 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13369 {
13370 error_at (OMP_CLAUSE_LOCATION (c),
13371 "%qE is not a variable in %qs clause", t,
13372 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13373 remove = true;
13374 }
0ae9bd27 13375 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
13376 {
13377 error_at (OMP_CLAUSE_LOCATION (c),
13378 "%qD is threadprivate variable in %qs clause", t,
13379 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13380 remove = true;
13381 }
e4606348
JJ
13382 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13383 || (OMP_CLAUSE_MAP_KIND (c)
13384 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13385 && !c_mark_addressable (t))
acf0174b 13386 remove = true;
b17a8b07 13387 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37 13388 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
d9a6bd32
JJ
13389 || (OMP_CLAUSE_MAP_KIND (c)
13390 == GOMP_MAP_FIRSTPRIVATE_POINTER)
41dbbb37
TS
13391 || (OMP_CLAUSE_MAP_KIND (c)
13392 == GOMP_MAP_FORCE_DEVICEPTR)))
d9a6bd32 13393 && t == OMP_CLAUSE_DECL (c)
b17a8b07 13394 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13395 {
13396 error_at (OMP_CLAUSE_LOCATION (c),
13397 "%qD does not have a mappable type in %qs clause", t,
13398 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13399 remove = true;
13400 }
9dc5773f
JJ
13401 else if (TREE_TYPE (t) == error_mark_node)
13402 remove = true;
13403 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13404 {
13405 error_at (OMP_CLAUSE_LOCATION (c),
13406 "%<_Atomic%> %qE in %qs clause", t,
13407 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13408 remove = true;
13409 }
d9a6bd32
JJ
13410 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13411 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13412 {
13413 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13414 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13415 {
13416 error ("%qD appears more than once in data clauses", t);
13417 remove = true;
13418 }
e01d41e5 13419 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
d9a6bd32 13420 {
e46c7770
CP
13421 if (ort == C_ORT_ACC)
13422 error ("%qD appears more than once in data clauses", t);
13423 else
13424 error ("%qD appears both in data and map clauses", t);
e01d41e5 13425 remove = true;
d9a6bd32 13426 }
e01d41e5
JJ
13427 else
13428 bitmap_set_bit (&generic_head, DECL_UID (t));
d9a6bd32
JJ
13429 }
13430 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
acf0174b
JJ
13431 {
13432 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13433 error ("%qD appears more than once in motion clauses", t);
e46c7770
CP
13434 else if (ort == C_ORT_ACC)
13435 error ("%qD appears more than once in data clauses", t);
acf0174b
JJ
13436 else
13437 error ("%qD appears more than once in map clauses", t);
13438 remove = true;
13439 }
e01d41e5
JJ
13440 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13441 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13442 {
e46c7770
CP
13443 if (ort == C_ORT_ACC)
13444 error ("%qD appears more than once in data clauses", t);
13445 else
13446 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13447 remove = true;
13448 }
acf0174b 13449 else
d9a6bd32
JJ
13450 {
13451 bitmap_set_bit (&map_head, DECL_UID (t));
13452 if (t != OMP_CLAUSE_DECL (c)
13453 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13454 bitmap_set_bit (&map_field_head, DECL_UID (t));
13455 }
13456 break;
13457
13458 case OMP_CLAUSE_TO_DECLARE:
d9a6bd32
JJ
13459 case OMP_CLAUSE_LINK:
13460 t = OMP_CLAUSE_DECL (c);
e01d41e5
JJ
13461 if (TREE_CODE (t) == FUNCTION_DECL
13462 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13463 ;
13464 else if (!VAR_P (t))
d9a6bd32 13465 {
e01d41e5
JJ
13466 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13467 error_at (OMP_CLAUSE_LOCATION (c),
13468 "%qE is neither a variable nor a function name in "
13469 "clause %qs", t,
13470 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13471 else
13472 error_at (OMP_CLAUSE_LOCATION (c),
13473 "%qE is not a variable in clause %qs", t,
13474 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
d9a6bd32
JJ
13475 remove = true;
13476 }
13477 else if (DECL_THREAD_LOCAL_P (t))
13478 {
13479 error_at (OMP_CLAUSE_LOCATION (c),
13480 "%qD is threadprivate variable in %qs clause", t,
13481 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13482 remove = true;
13483 }
13484 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13485 {
13486 error_at (OMP_CLAUSE_LOCATION (c),
13487 "%qD does not have a mappable type in %qs clause", t,
13488 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13489 remove = true;
13490 }
e01d41e5
JJ
13491 if (remove)
13492 break;
13493 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13494 {
13495 error_at (OMP_CLAUSE_LOCATION (c),
13496 "%qE appears more than once on the same "
13497 "%<declare target%> directive", t);
13498 remove = true;
13499 }
13500 else
13501 bitmap_set_bit (&generic_head, DECL_UID (t));
acf0174b
JJ
13502 break;
13503
13504 case OMP_CLAUSE_UNIFORM:
13505 t = OMP_CLAUSE_DECL (c);
13506 if (TREE_CODE (t) != PARM_DECL)
13507 {
13508 if (DECL_P (t))
13509 error_at (OMP_CLAUSE_LOCATION (c),
13510 "%qD is not an argument in %<uniform%> clause", t);
13511 else
13512 error_at (OMP_CLAUSE_LOCATION (c),
13513 "%qE is not an argument in %<uniform%> clause", t);
13514 remove = true;
ee1d5a02 13515 break;
acf0174b 13516 }
e01d41e5
JJ
13517 /* map_head bitmap is used as uniform_head if declare_simd. */
13518 bitmap_set_bit (&map_head, DECL_UID (t));
ee1d5a02 13519 goto check_dup_generic;
acf0174b 13520
d9a6bd32
JJ
13521 case OMP_CLAUSE_IS_DEVICE_PTR:
13522 case OMP_CLAUSE_USE_DEVICE_PTR:
13523 t = OMP_CLAUSE_DECL (c);
13524 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13525 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13526 {
13527 error_at (OMP_CLAUSE_LOCATION (c),
13528 "%qs variable is neither a pointer nor an array",
13529 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13530 remove = true;
13531 }
13532 goto check_dup_generic;
13533
acf0174b
JJ
13534 case OMP_CLAUSE_NOWAIT:
13535 if (copyprivate_seen)
13536 {
13537 error_at (OMP_CLAUSE_LOCATION (c),
13538 "%<nowait%> clause must not be used together "
13539 "with %<copyprivate%>");
13540 remove = true;
13541 break;
13542 }
13543 nowait_clause = pc;
13544 pc = &OMP_CLAUSE_CHAIN (c);
13545 continue;
13546
953ff289
DN
13547 case OMP_CLAUSE_IF:
13548 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
13549 case OMP_CLAUSE_NUM_TEAMS:
13550 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 13551 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
13552 case OMP_CLAUSE_UNTIED:
13553 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
13554 case OMP_CLAUSE_FINAL:
13555 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
13556 case OMP_CLAUSE_DEVICE:
13557 case OMP_CLAUSE_DIST_SCHEDULE:
13558 case OMP_CLAUSE_PARALLEL:
13559 case OMP_CLAUSE_FOR:
13560 case OMP_CLAUSE_SECTIONS:
13561 case OMP_CLAUSE_TASKGROUP:
13562 case OMP_CLAUSE_PROC_BIND:
d9a6bd32
JJ
13563 case OMP_CLAUSE_PRIORITY:
13564 case OMP_CLAUSE_GRAINSIZE:
13565 case OMP_CLAUSE_NUM_TASKS:
13566 case OMP_CLAUSE_NOGROUP:
13567 case OMP_CLAUSE_THREADS:
13568 case OMP_CLAUSE_SIMD:
13569 case OMP_CLAUSE_HINT:
13570 case OMP_CLAUSE_DEFAULTMAP:
9a771876 13571 case OMP_CLAUSE__CILK_FOR_COUNT_:
41dbbb37
TS
13572 case OMP_CLAUSE_NUM_GANGS:
13573 case OMP_CLAUSE_NUM_WORKERS:
13574 case OMP_CLAUSE_VECTOR_LENGTH:
13575 case OMP_CLAUSE_ASYNC:
13576 case OMP_CLAUSE_WAIT:
13577 case OMP_CLAUSE_AUTO:
7a5e4956 13578 case OMP_CLAUSE_INDEPENDENT:
41dbbb37
TS
13579 case OMP_CLAUSE_SEQ:
13580 case OMP_CLAUSE_GANG:
13581 case OMP_CLAUSE_WORKER:
13582 case OMP_CLAUSE_VECTOR:
7a5e4956 13583 case OMP_CLAUSE_TILE:
acf0174b
JJ
13584 pc = &OMP_CLAUSE_CHAIN (c);
13585 continue;
13586
e01d41e5
JJ
13587 case OMP_CLAUSE_SCHEDULE:
13588 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13589 {
13590 const char *p = NULL;
13591 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13592 {
13593 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13594 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13595 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13596 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13597 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13598 default: gcc_unreachable ();
13599 }
13600 if (p)
13601 {
13602 error_at (OMP_CLAUSE_LOCATION (c),
13603 "%<nonmonotonic%> modifier specified for %qs "
13604 "schedule kind", p);
13605 OMP_CLAUSE_SCHEDULE_KIND (c)
13606 = (enum omp_clause_schedule_kind)
13607 (OMP_CLAUSE_SCHEDULE_KIND (c)
13608 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13609 }
13610 }
13611 schedule_clause = c;
13612 pc = &OMP_CLAUSE_CHAIN (c);
13613 continue;
13614
13615 case OMP_CLAUSE_ORDERED:
13616 ordered_seen = true;
13617 pc = &OMP_CLAUSE_CHAIN (c);
13618 continue;
13619
d9a6bd32
JJ
13620 case OMP_CLAUSE_SAFELEN:
13621 safelen = c;
13622 pc = &OMP_CLAUSE_CHAIN (c);
13623 continue;
13624 case OMP_CLAUSE_SIMDLEN:
13625 simdlen = c;
13626 pc = &OMP_CLAUSE_CHAIN (c);
13627 continue;
13628
acf0174b
JJ
13629 case OMP_CLAUSE_INBRANCH:
13630 case OMP_CLAUSE_NOTINBRANCH:
13631 if (branch_seen)
13632 {
13633 error_at (OMP_CLAUSE_LOCATION (c),
13634 "%<inbranch%> clause is incompatible with "
13635 "%<notinbranch%>");
13636 remove = true;
13637 break;
13638 }
13639 branch_seen = true;
953ff289
DN
13640 pc = &OMP_CLAUSE_CHAIN (c);
13641 continue;
13642
13643 default:
13644 gcc_unreachable ();
13645 }
13646
13647 if (!remove)
13648 {
13649 t = OMP_CLAUSE_DECL (c);
13650
13651 if (need_complete)
13652 {
4f2e1536 13653 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
953ff289
DN
13654 if (t == error_mark_node)
13655 remove = true;
13656 }
13657
13658 if (need_implicitly_determined)
13659 {
13660 const char *share_name = NULL;
13661
0ae9bd27 13662 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
953ff289
DN
13663 share_name = "threadprivate";
13664 else switch (c_omp_predetermined_sharing (t))
13665 {
13666 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13667 break;
13668 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
13669 /* const vars may be specified in firstprivate clause. */
13670 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13671 && TREE_READONLY (t))
13672 break;
953ff289
DN
13673 share_name = "shared";
13674 break;
13675 case OMP_CLAUSE_DEFAULT_PRIVATE:
13676 share_name = "private";
13677 break;
13678 default:
13679 gcc_unreachable ();
13680 }
13681 if (share_name)
13682 {
c2255bc4
AH
13683 error_at (OMP_CLAUSE_LOCATION (c),
13684 "%qE is predetermined %qs for %qs",
acf0174b
JJ
13685 t, share_name,
13686 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13687 remove = true;
13688 }
13689 }
13690 }
13691
13692 if (remove)
13693 *pc = OMP_CLAUSE_CHAIN (c);
13694 else
13695 pc = &OMP_CLAUSE_CHAIN (c);
13696 }
13697
d9a6bd32
JJ
13698 if (simdlen
13699 && safelen
13700 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13701 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13702 {
13703 error_at (OMP_CLAUSE_LOCATION (simdlen),
13704 "%<simdlen%> clause value is bigger than "
13705 "%<safelen%> clause value");
13706 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13707 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13708 }
13709
e01d41e5
JJ
13710 if (ordered_seen
13711 && schedule_clause
13712 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13713 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13714 {
13715 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13716 "%<nonmonotonic%> schedule modifier specified together "
13717 "with %<ordered%> clause");
13718 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13719 = (enum omp_clause_schedule_kind)
13720 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13721 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13722 }
13723
13724 if (linear_variable_step_check)
13725 for (pc = &clauses, c = clauses; c ; c = *pc)
13726 {
13727 bool remove = false;
13728 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13729 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13730 && !bitmap_bit_p (&map_head,
13731 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13732 {
13733 error_at (OMP_CLAUSE_LOCATION (c),
13734 "%<linear%> clause step is a parameter %qD not "
13735 "specified in %<uniform%> clause",
13736 OMP_CLAUSE_LINEAR_STEP (c));
13737 remove = true;
13738 }
13739
13740 if (remove)
13741 *pc = OMP_CLAUSE_CHAIN (c);
13742 else
13743 pc = &OMP_CLAUSE_CHAIN (c);
13744 }
13745
953ff289
DN
13746 bitmap_obstack_release (NULL);
13747 return clauses;
13748}
9ae165a0 13749
9dc5773f
JJ
13750/* Return code to initialize DST with a copy constructor from SRC.
13751 C doesn't have copy constructors nor assignment operators, only for
13752 _Atomic vars we need to perform __atomic_load from src into a temporary
13753 followed by __atomic_store of the temporary to dst. */
13754
13755tree
13756c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13757{
13758 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13759 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13760
13761 location_t loc = OMP_CLAUSE_LOCATION (clause);
13762 tree type = TREE_TYPE (dst);
13763 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13764 tree tmp = create_tmp_var (nonatomic_type);
13765 tree tmp_addr = build_fold_addr_expr (tmp);
13766 TREE_ADDRESSABLE (tmp) = 1;
13767 TREE_NO_WARNING (tmp) = 1;
13768 tree src_addr = build_fold_addr_expr (src);
13769 tree dst_addr = build_fold_addr_expr (dst);
13770 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
13771 vec<tree, va_gc> *params;
13772 /* Expansion of a generic atomic load may require an addition
13773 element, so allocate enough to prevent a resize. */
13774 vec_alloc (params, 4);
13775
13776 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13777 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
13778 params->quick_push (src_addr);
13779 params->quick_push (tmp_addr);
13780 params->quick_push (seq_cst);
13781 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13782
13783 vec_alloc (params, 4);
13784
13785 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13786 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
13787 params->quick_push (dst_addr);
13788 params->quick_push (tmp_addr);
13789 params->quick_push (seq_cst);
13790 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13791 return build2 (COMPOUND_EXPR, void_type_node, load, store);
13792}
13793
0a35513e
AH
13794/* Create a transaction node. */
13795
13796tree
13797c_finish_transaction (location_t loc, tree block, int flags)
13798{
13799 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13800 if (flags & TM_STMT_ATTR_OUTER)
13801 TRANSACTION_EXPR_OUTER (stmt) = 1;
13802 if (flags & TM_STMT_ATTR_RELAXED)
13803 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13804 return add_stmt (stmt);
13805}
13806
9ae165a0 13807/* Make a variant type in the proper way for C/C++, propagating qualifiers
e9e32ee6
JM
13808 down to the element type of an array. If ORIG_QUAL_TYPE is not
13809 NULL, then it should be used as the qualified type
13810 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13811 preserve information about the typedef name from which an array
13812 type was derived). */
9ae165a0
DG
13813
13814tree
e9e32ee6
JM
13815c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13816 size_t orig_qual_indirect)
9ae165a0
DG
13817{
13818 if (type == error_mark_node)
13819 return type;
13820
13821 if (TREE_CODE (type) == ARRAY_TYPE)
13822 {
13823 tree t;
13824 tree element_type = c_build_qualified_type (TREE_TYPE (type),
e9e32ee6
JM
13825 type_quals, orig_qual_type,
13826 orig_qual_indirect - 1);
9ae165a0
DG
13827
13828 /* See if we already have an identically qualified type. */
e9e32ee6
JM
13829 if (orig_qual_type && orig_qual_indirect == 0)
13830 t = orig_qual_type;
13831 else
13832 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13833 {
13834 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13835 && TYPE_NAME (t) == TYPE_NAME (type)
13836 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13837 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13838 TYPE_ATTRIBUTES (type)))
13839 break;
13840 }
9ae165a0
DG
13841 if (!t)
13842 {
13843 tree domain = TYPE_DOMAIN (type);
13844
13845 t = build_variant_type_copy (type);
13846 TREE_TYPE (t) = element_type;
13847
13848 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13849 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13850 SET_TYPE_STRUCTURAL_EQUALITY (t);
13851 else if (TYPE_CANONICAL (element_type) != element_type
13852 || (domain && TYPE_CANONICAL (domain) != domain))
13853 {
b8698a0f 13854 tree unqualified_canon
9ae165a0 13855 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 13856 domain? TYPE_CANONICAL (domain)
9ae165a0 13857 : NULL_TREE);
ee45a32d
EB
13858 if (TYPE_REVERSE_STORAGE_ORDER (type))
13859 {
13860 unqualified_canon
13861 = build_distinct_type_copy (unqualified_canon);
13862 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13863 }
b8698a0f 13864 TYPE_CANONICAL (t)
9ae165a0
DG
13865 = c_build_qualified_type (unqualified_canon, type_quals);
13866 }
13867 else
13868 TYPE_CANONICAL (t) = t;
13869 }
13870 return t;
13871 }
13872
13873 /* A restrict-qualified pointer type must be a pointer to object or
13874 incomplete type. Note that the use of POINTER_TYPE_P also allows
13875 REFERENCE_TYPEs, which is appropriate for C++. */
13876 if ((type_quals & TYPE_QUAL_RESTRICT)
13877 && (!POINTER_TYPE_P (type)
13878 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13879 {
13880 error ("invalid use of %<restrict%>");
13881 type_quals &= ~TYPE_QUAL_RESTRICT;
13882 }
13883
e9e32ee6
JM
13884 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13885 ? orig_qual_type
13886 : build_qualified_type (type, type_quals));
e922069e
JW
13887 /* A variant type does not inherit the list of incomplete vars from the
13888 type main variant. */
152ef731
JJ
13889 if (RECORD_OR_UNION_TYPE_P (var_type)
13890 && TYPE_MAIN_VARIANT (var_type) != var_type)
e922069e
JW
13891 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13892 return var_type;
9ae165a0 13893}
72b5577d
ILT
13894
13895/* Build a VA_ARG_EXPR for the C parser. */
13896
13897tree
f187980b 13898c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
72b5577d 13899{
ec3fba51
MP
13900 if (error_operand_p (type))
13901 return error_mark_node;
f187980b
EB
13902 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13903 order because it takes the address of the expression. */
13904 else if (handled_component_p (expr)
13905 && reverse_storage_order_for_component_p (expr))
13906 {
13907 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13908 return error_mark_node;
13909 }
ec3fba51 13910 else if (!COMPLETE_TYPE_P (type))
4e81b788 13911 {
f187980b 13912 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
ec3fba51 13913 "type %qT", type);
4e81b788
MP
13914 return error_mark_node;
13915 }
ec3fba51 13916 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
f187980b 13917 warning_at (loc2, OPT_Wc___compat,
ec3fba51 13918 "C++ requires promoted type, not enum type, in %<va_arg%>");
f187980b 13919 return build_va_arg (loc2, expr, type);
72b5577d 13920}
acf0174b
JJ
13921
13922/* Return truthvalue of whether T1 is the same tree structure as T2.
13923 Return 1 if they are the same. Return 0 if they are different. */
13924
13925bool
13926c_tree_equal (tree t1, tree t2)
13927{
13928 enum tree_code code1, code2;
13929
13930 if (t1 == t2)
13931 return true;
13932 if (!t1 || !t2)
13933 return false;
13934
13935 for (code1 = TREE_CODE (t1);
13936 CONVERT_EXPR_CODE_P (code1)
13937 || code1 == NON_LVALUE_EXPR;
13938 code1 = TREE_CODE (t1))
13939 t1 = TREE_OPERAND (t1, 0);
13940 for (code2 = TREE_CODE (t2);
13941 CONVERT_EXPR_CODE_P (code2)
13942 || code2 == NON_LVALUE_EXPR;
13943 code2 = TREE_CODE (t2))
13944 t2 = TREE_OPERAND (t2, 0);
13945
13946 /* They might have become equal now. */
13947 if (t1 == t2)
13948 return true;
13949
13950 if (code1 != code2)
13951 return false;
13952
13953 switch (code1)
13954 {
13955 case INTEGER_CST:
807e902e 13956 return wi::eq_p (t1, t2);
acf0174b
JJ
13957
13958 case REAL_CST:
624d31fe 13959 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
acf0174b
JJ
13960
13961 case STRING_CST:
13962 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13963 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13964 TREE_STRING_LENGTH (t1));
13965
13966 case FIXED_CST:
13967 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13968 TREE_FIXED_CST (t2));
13969
13970 case COMPLEX_CST:
13971 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13972 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13973
13974 case VECTOR_CST:
13975 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13976
13977 case CONSTRUCTOR:
13978 /* We need to do this when determining whether or not two
13979 non-type pointer to member function template arguments
13980 are the same. */
13981 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13982 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13983 return false;
13984 {
13985 tree field, value;
13986 unsigned int i;
13987 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13988 {
13989 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13990 if (!c_tree_equal (field, elt2->index)
13991 || !c_tree_equal (value, elt2->value))
13992 return false;
13993 }
13994 }
13995 return true;
13996
13997 case TREE_LIST:
13998 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13999 return false;
14000 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14001 return false;
14002 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14003
14004 case SAVE_EXPR:
14005 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14006
14007 case CALL_EXPR:
14008 {
14009 tree arg1, arg2;
14010 call_expr_arg_iterator iter1, iter2;
14011 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14012 return false;
14013 for (arg1 = first_call_expr_arg (t1, &iter1),
14014 arg2 = first_call_expr_arg (t2, &iter2);
14015 arg1 && arg2;
14016 arg1 = next_call_expr_arg (&iter1),
14017 arg2 = next_call_expr_arg (&iter2))
14018 if (!c_tree_equal (arg1, arg2))
14019 return false;
14020 if (arg1 || arg2)
14021 return false;
14022 return true;
14023 }
14024
14025 case TARGET_EXPR:
14026 {
14027 tree o1 = TREE_OPERAND (t1, 0);
14028 tree o2 = TREE_OPERAND (t2, 0);
14029
14030 /* Special case: if either target is an unallocated VAR_DECL,
14031 it means that it's going to be unified with whatever the
14032 TARGET_EXPR is really supposed to initialize, so treat it
14033 as being equivalent to anything. */
0ae9bd27 14034 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
acf0174b
JJ
14035 && !DECL_RTL_SET_P (o1))
14036 /*Nop*/;
0ae9bd27 14037 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
acf0174b
JJ
14038 && !DECL_RTL_SET_P (o2))
14039 /*Nop*/;
14040 else if (!c_tree_equal (o1, o2))
14041 return false;
14042
14043 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14044 }
14045
14046 case COMPONENT_REF:
14047 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14048 return false;
14049 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14050
14051 case PARM_DECL:
14052 case VAR_DECL:
14053 case CONST_DECL:
14054 case FIELD_DECL:
14055 case FUNCTION_DECL:
14056 case IDENTIFIER_NODE:
14057 case SSA_NAME:
14058 return false;
14059
14060 case TREE_VEC:
14061 {
14062 unsigned ix;
14063 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14064 return false;
14065 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14066 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14067 TREE_VEC_ELT (t2, ix)))
14068 return false;
14069 return true;
14070 }
14071
14072 default:
14073 break;
14074 }
14075
14076 switch (TREE_CODE_CLASS (code1))
14077 {
14078 case tcc_unary:
14079 case tcc_binary:
14080 case tcc_comparison:
14081 case tcc_expression:
14082 case tcc_vl_exp:
14083 case tcc_reference:
14084 case tcc_statement:
14085 {
14086 int i, n = TREE_OPERAND_LENGTH (t1);
14087
14088 switch (code1)
14089 {
14090 case PREINCREMENT_EXPR:
14091 case PREDECREMENT_EXPR:
14092 case POSTINCREMENT_EXPR:
14093 case POSTDECREMENT_EXPR:
14094 n = 1;
14095 break;
14096 case ARRAY_REF:
14097 n = 2;
14098 break;
14099 default:
14100 break;
14101 }
14102
14103 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14104 && n != TREE_OPERAND_LENGTH (t2))
14105 return false;
14106
14107 for (i = 0; i < n; ++i)
14108 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14109 return false;
14110
14111 return true;
14112 }
14113
14114 case tcc_type:
14115 return comptypes (t1, t2);
14116 default:
14117 gcc_unreachable ();
14118 }
14119 /* We can get here with --disable-checking. */
14120 return false;
14121}
12893402
BI
14122
14123/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14124 spawn-helper and BODY is the newly created body for FNDECL. */
14125
14126void
14127cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14128{
14129 tree list = alloc_stmt_list ();
14130 tree frame = make_cilk_frame (fndecl);
14131 tree dtor = create_cilk_function_exit (frame, false, true);
14132 add_local_decl (cfun, frame);
14133
14134 DECL_SAVED_TREE (fndecl) = list;
14135 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
14136 frame);
14137 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
14138 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
14139
14140 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
14141 append_to_statement_list (detach_expr, &body_list);
14142
14143 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14144 body = fold_build_cleanup_point_expr (void_type_node, body);
14145
14146 append_to_statement_list (body, &body_list);
14147 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14148 body_list, dtor), &list);
14149}
1807ffc1
MS
14150
14151/* Returns true when the function declaration FNDECL is implicit,
14152 introduced as a result of a call to an otherwise undeclared
14153 function, and false otherwise. */
14154
14155bool
14156c_decl_implicit (const_tree fndecl)
14157{
14158 return C_DECL_IMPLICIT (fndecl);
14159}