]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
libstdc++/71320 Add or remove file permissions correctly
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
818ab71a 2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
400fbf9f 3
1322177d 4This file is part of GCC.
400fbf9f 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
400fbf9f 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
400fbf9f
JW
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
400fbf9f
JW
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
5088b058 24 and some optimization. */
400fbf9f
JW
25
26#include "config.h"
670ee920 27#include "system.h"
4977bab6 28#include "coretypes.h"
2adfab87
AM
29#include "target.h"
30#include "function.h"
31#include "bitmap.h"
2adfab87
AM
32#include "c-tree.h"
33#include "gimple-expr.h"
34#include "predict.h"
d8a2d370
DN
35#include "stor-layout.h"
36#include "trans-mem.h"
37#include "varasm.h"
38#include "stmt.h"
e57e265b 39#include "langhooks.h"
29cc57cf 40#include "c-lang.h"
ab87f8c8 41#include "intl.h"
325c3691 42#include "tree-iterator.h"
45b0be94 43#include "gimplify.h"
acf0174b 44#include "tree-inline.h"
0645c1a2 45#include "omp-low.h"
61d3ce20 46#include "c-family/c-objc.h"
de5a5fa1 47#include "c-family/c-ubsan.h"
12893402 48#include "cilk.h"
41dbbb37 49#include "gomp-constants.h"
277fe616 50#include "spellcheck.h"
745e411d 51#include "gcc-rich-location.h"
325c3691 52
2ac2f164
JM
53/* Possible cases of implicit bad conversions. Used to select
54 diagnostic messages in convert_for_assignment. */
55enum impl_conv {
56 ic_argpass,
57 ic_assign,
58 ic_init,
59 ic_return
60};
61
bc4b653b
JM
62/* The level of nesting inside "__alignof__". */
63int in_alignof;
64
65/* The level of nesting inside "sizeof". */
66int in_sizeof;
67
68/* The level of nesting inside "typeof". */
69int in_typeof;
400fbf9f 70
1a4049e7
JJ
71/* The argument of last parsed sizeof expression, only to be tested
72 if expr.original_code == SIZEOF_EXPR. */
73tree c_last_sizeof_arg;
74
9bac5cbb
G
75/* Nonzero if we might need to print a "missing braces around
76 initializer" message within this initializer. */
77static int found_missing_braces;
103b7b17 78
bf730f15
RS
79static int require_constant_value;
80static int require_constant_elements;
81
58f9752a 82static bool null_pointer_constant_p (const_tree);
f55ade6e 83static tree qualify_type (tree, tree);
dc5027f4
JM
84static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
85 bool *);
744aa42f 86static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
87static int function_types_compatible_p (const_tree, const_tree, bool *,
88 bool *);
89static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 90static tree lookup_field (tree, tree);
81e5eca8
MP
91static int convert_arguments (location_t, vec<location_t>, tree,
92 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
93 tree);
db3927fb 94static tree pointer_diff (location_t, tree, tree);
68fca595 95static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 96 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
97static tree valid_compound_expr_initializer (tree, tree);
98static void push_string (const char *);
99static void push_member_name (tree);
f55ade6e
AJ
100static int spelling_length (void);
101static char *print_spelling (char *);
96b40f8d 102static void warning_init (location_t, int, const char *);
c2255bc4 103static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
104static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
105 bool, struct obstack *);
a1e3b3d9 106static void output_pending_init_elements (int, struct obstack *);
ea58ef42 107static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 108static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
109static void add_pending_init (location_t, tree, tree, tree, bool,
110 struct obstack *);
a1e3b3d9
LB
111static void set_nonincremental_init (struct obstack *);
112static void set_nonincremental_init_from_string (tree, struct obstack *);
113static tree find_init_member (tree, struct obstack *);
f37acdf9 114static void readonly_warning (tree, enum lvalue_use);
7bd11157 115static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 116static void record_maybe_used_decl (tree);
dc5027f4 117static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
118\f
119/* Return true if EXP is a null pointer constant, false otherwise. */
120
121static bool
58f9752a 122null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
123{
124 /* This should really operate on c_expr structures, but they aren't
125 yet available everywhere required. */
126 tree type = TREE_TYPE (expr);
127 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 128 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
129 && integer_zerop (expr)
130 && (INTEGRAL_TYPE_P (type)
131 || (TREE_CODE (type) == POINTER_TYPE
132 && VOID_TYPE_P (TREE_TYPE (type))
133 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
134}
928c19bb
JM
135
136/* EXPR may appear in an unevaluated part of an integer constant
137 expression, but not in an evaluated part. Wrap it in a
138 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
139 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
140
141static tree
142note_integer_operands (tree expr)
143{
144 tree ret;
145 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
146 {
147 ret = copy_node (expr);
148 TREE_OVERFLOW (ret) = 1;
149 }
150 else
151 {
152 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
153 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
154 }
155 return ret;
156}
157
4d84fe7c
JM
158/* Having checked whether EXPR may appear in an unevaluated part of an
159 integer constant expression and found that it may, remove any
160 C_MAYBE_CONST_EXPR noting this fact and return the resulting
161 expression. */
162
163static inline tree
164remove_c_maybe_const_expr (tree expr)
165{
166 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
167 return C_MAYBE_CONST_EXPR_EXPR (expr);
168 else
169 return expr;
170}
171
f13c9b2c
AP
172\f/* This is a cache to hold if two types are compatible or not. */
173
174struct tagged_tu_seen_cache {
175 const struct tagged_tu_seen_cache * next;
58f9752a
KG
176 const_tree t1;
177 const_tree t2;
f13c9b2c
AP
178 /* The return value of tagged_types_tu_compatible_p if we had seen
179 these two types already. */
180 int val;
181};
182
183static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
184static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
185
4f2e1536
MP
186/* Do `exp = require_complete_type (loc, exp);' to make sure exp
187 does not have an incomplete type. (That includes void types.)
188 LOC is the location of the use. */
400fbf9f
JW
189
190tree
4f2e1536 191require_complete_type (location_t loc, tree value)
400fbf9f
JW
192{
193 tree type = TREE_TYPE (value);
194
7a0ca710 195 if (error_operand_p (value))
ea0f786b
CB
196 return error_mark_node;
197
400fbf9f 198 /* First, detect a valid value with a complete type. */
d0f062fb 199 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
200 return value;
201
4f2e1536 202 c_incomplete_type_error (loc, value, type);
400fbf9f
JW
203 return error_mark_node;
204}
205
206/* Print an error message for invalid use of an incomplete type.
207 VALUE is the expression that was used (or 0 if that isn't known)
4f2e1536
MP
208 and TYPE is the type that was invalid. LOC is the location for
209 the error. */
400fbf9f
JW
210
211void
4f2e1536 212c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
400fbf9f 213{
400fbf9f
JW
214 /* Avoid duplicate error message. */
215 if (TREE_CODE (type) == ERROR_MARK)
216 return;
217
0ae9bd27 218 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
4f2e1536 219 error_at (loc, "%qD has an incomplete type %qT", value, type);
400fbf9f
JW
220 else
221 {
222 retry:
223 /* We must print an error message. Be clever about what it says. */
224
225 switch (TREE_CODE (type))
226 {
227 case RECORD_TYPE:
400fbf9f 228 case UNION_TYPE:
400fbf9f 229 case ENUMERAL_TYPE:
400fbf9f
JW
230 break;
231
232 case VOID_TYPE:
4f2e1536 233 error_at (loc, "invalid use of void expression");
400fbf9f
JW
234 return;
235
236 case ARRAY_TYPE:
237 if (TYPE_DOMAIN (type))
238 {
fba78abb
RH
239 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
240 {
4f2e1536 241 error_at (loc, "invalid use of flexible array member");
fba78abb
RH
242 return;
243 }
400fbf9f
JW
244 type = TREE_TYPE (type);
245 goto retry;
246 }
4f2e1536 247 error_at (loc, "invalid use of array with unspecified bounds");
400fbf9f
JW
248 return;
249
250 default:
366de0ce 251 gcc_unreachable ();
400fbf9f
JW
252 }
253
254 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
4f2e1536 255 error_at (loc, "invalid use of undefined type %qT", type);
400fbf9f
JW
256 else
257 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
4f2e1536 258 error_at (loc, "invalid use of incomplete typedef %qT", type);
400fbf9f
JW
259 }
260}
261
ab393bf1
NB
262/* Given a type, apply default promotions wrt unnamed function
263 arguments and return the new type. */
264
265tree
2f6e4e97 266c_type_promotes_to (tree type)
ab393bf1 267{
267bac10 268 tree ret = NULL_TREE;
ab393bf1 269
267bac10
JM
270 if (TYPE_MAIN_VARIANT (type) == float_type_node)
271 ret = double_type_node;
272 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
273 {
274 /* Preserve unsignedness if not really getting any wider. */
8df83eae 275 if (TYPE_UNSIGNED (type)
c22cacf3 276 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
277 ret = unsigned_type_node;
278 else
279 ret = integer_type_node;
ab393bf1
NB
280 }
281
267bac10
JM
282 if (ret != NULL_TREE)
283 return (TYPE_ATOMIC (type)
284 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
285 : ret);
286
ab393bf1
NB
287 return type;
288}
289
36c5e70a
BE
290/* Return true if between two named address spaces, whether there is a superset
291 named address space that encompasses both address spaces. If there is a
292 superset, return which address space is the superset. */
293
294static bool
295addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
296{
297 if (as1 == as2)
298 {
299 *common = as1;
300 return true;
301 }
302 else if (targetm.addr_space.subset_p (as1, as2))
303 {
304 *common = as2;
305 return true;
306 }
307 else if (targetm.addr_space.subset_p (as2, as1))
308 {
309 *common = as1;
310 return true;
311 }
312 else
313 return false;
314}
315
400fbf9f
JW
316/* Return a variant of TYPE which has all the type qualifiers of LIKE
317 as well as those of TYPE. */
318
319static tree
2f6e4e97 320qualify_type (tree type, tree like)
400fbf9f 321{
36c5e70a
BE
322 addr_space_t as_type = TYPE_ADDR_SPACE (type);
323 addr_space_t as_like = TYPE_ADDR_SPACE (like);
324 addr_space_t as_common;
325
326 /* If the two named address spaces are different, determine the common
327 superset address space. If there isn't one, raise an error. */
328 if (!addr_space_superset (as_type, as_like, &as_common))
329 {
330 as_common = as_type;
331 error ("%qT and %qT are in disjoint named address spaces",
332 type, like);
333 }
334
2f6e4e97 335 return c_build_qualified_type (type,
36c5e70a 336 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 337 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 338 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 339}
52ffd86e
MS
340
341/* Return true iff the given tree T is a variable length array. */
342
343bool
ac7d7749 344c_vla_type_p (const_tree t)
52ffd86e
MS
345{
346 if (TREE_CODE (t) == ARRAY_TYPE
347 && C_TYPE_VARIABLE_SIZE (t))
348 return true;
349 return false;
350}
400fbf9f 351\f
10bc1b1b 352/* Return the composite type of two compatible types.
5305f6d7 353
10bc1b1b
JM
354 We assume that comptypes has already been done and returned
355 nonzero; if that isn't so, this may crash. In particular, we
356 assume that qualifiers match. */
400fbf9f
JW
357
358tree
10bc1b1b 359composite_type (tree t1, tree t2)
400fbf9f 360{
b3694847
SS
361 enum tree_code code1;
362 enum tree_code code2;
4b027d16 363 tree attributes;
400fbf9f
JW
364
365 /* Save time if the two types are the same. */
366
367 if (t1 == t2) return t1;
368
369 /* If one type is nonsense, use the other. */
370 if (t1 == error_mark_node)
371 return t2;
372 if (t2 == error_mark_node)
373 return t1;
374
10bc1b1b
JM
375 code1 = TREE_CODE (t1);
376 code2 = TREE_CODE (t2);
377
d9525bec 378 /* Merge the attributes. */
5fd9b178 379 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 380
10bc1b1b
JM
381 /* If one is an enumerated type and the other is the compatible
382 integer type, the composite type might be either of the two
383 (DR#013 question 3). For consistency, use the enumerated type as
384 the composite type. */
400fbf9f 385
10bc1b1b
JM
386 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
387 return t1;
388 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
389 return t2;
75326e8c 390
366de0ce 391 gcc_assert (code1 == code2);
b6a10c9f 392
400fbf9f
JW
393 switch (code1)
394 {
400fbf9f 395 case POINTER_TYPE:
10bc1b1b 396 /* For two pointers, do this recursively on the target type. */
400fbf9f 397 {
3932261a
MM
398 tree pointed_to_1 = TREE_TYPE (t1);
399 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 400 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 401 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
402 t1 = build_type_attribute_variant (t1, attributes);
403 return qualify_type (t1, t2);
400fbf9f 404 }
400fbf9f
JW
405
406 case ARRAY_TYPE:
407 {
10bc1b1b 408 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
409 int quals;
410 tree unqual_elt;
ca8bdb78
JM
411 tree d1 = TYPE_DOMAIN (t1);
412 tree d2 = TYPE_DOMAIN (t2);
413 bool d1_variable, d2_variable;
414 bool d1_zero, d2_zero;
f6294de7 415 bool t1_complete, t2_complete;
46df2823 416
de46b2fe 417 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
418 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
419 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 420
f6294de7
JM
421 t1_complete = COMPLETE_TYPE_P (t1);
422 t2_complete = COMPLETE_TYPE_P (t2);
423
ca8bdb78
JM
424 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
425 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
426
427 d1_variable = (!d1_zero
428 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
429 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
430 d2_variable = (!d2_zero
431 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
432 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
433 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
434 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 435
400fbf9f 436 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
437 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
438 && (d2_variable || d2_zero || !d1_variable))
4b027d16 439 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
440 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
441 && (d1_variable || d1_zero || !d2_variable))
4b027d16 442 return build_type_attribute_variant (t2, attributes);
c22cacf3 443
de46b2fe
AP
444 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
445 return build_type_attribute_variant (t1, attributes);
446 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
447 return build_type_attribute_variant (t2, attributes);
c22cacf3 448
46df2823
JM
449 /* Merge the element types, and have a size if either arg has
450 one. We may have qualifiers on the element types. To set
451 up TYPE_MAIN_VARIANT correctly, we need to form the
452 composite of the unqualified types and add the qualifiers
453 back at the end. */
454 quals = TYPE_QUALS (strip_array_types (elt));
455 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
456 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
457 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
458 && (d2_variable
459 || d2_zero
460 || !d1_variable))
461 ? t1
462 : t2));
f6294de7
JM
463 /* Ensure a composite type involving a zero-length array type
464 is a zero-length type not an incomplete type. */
465 if (d1_zero && d2_zero
466 && (t1_complete || t2_complete)
467 && !COMPLETE_TYPE_P (t1))
468 {
469 TYPE_SIZE (t1) = bitsize_zero_node;
470 TYPE_SIZE_UNIT (t1) = size_zero_node;
471 }
46df2823 472 t1 = c_build_qualified_type (t1, quals);
de46b2fe 473 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
474 }
475
fcb99e7b
JJ
476 case ENUMERAL_TYPE:
477 case RECORD_TYPE:
478 case UNION_TYPE:
479 if (attributes != NULL)
480 {
481 /* Try harder not to create a new aggregate type. */
482 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
483 return t1;
484 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
485 return t2;
486 }
487 return build_type_attribute_variant (t1, attributes);
488
400fbf9f
JW
489 case FUNCTION_TYPE:
490 /* Function types: prefer the one that specified arg types.
491 If both do, merge the arg types. Also merge the return types. */
492 {
10bc1b1b 493 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
494 tree p1 = TYPE_ARG_TYPES (t1);
495 tree p2 = TYPE_ARG_TYPES (t2);
496 int len;
497 tree newargs, n;
498 int i;
499
500 /* Save space: see if the result is identical to one of the args. */
3f75a254 501 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 502 return build_type_attribute_variant (t1, attributes);
3f75a254 503 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 504 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
505
506 /* Simple way if one arg fails to specify argument types. */
507 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 508 {
fe7080d2
AP
509 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
510 t1 = build_type_attribute_variant (t1, attributes);
511 return qualify_type (t1, t2);
4b027d16 512 }
400fbf9f 513 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
514 {
515 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
516 t1 = build_type_attribute_variant (t1, attributes);
517 return qualify_type (t1, t2);
4b027d16 518 }
400fbf9f
JW
519
520 /* If both args specify argument types, we must merge the two
521 lists, argument by argument. */
2f4e8f2b 522
fcc2b74f
JJ
523 for (len = 0, newargs = p1;
524 newargs && newargs != void_list_node;
525 len++, newargs = TREE_CHAIN (newargs))
526 ;
400fbf9f
JW
527
528 for (i = 0; i < len; i++)
8d9bfdc5 529 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
530
531 n = newargs;
532
fcc2b74f 533 for (; p1 && p1 != void_list_node;
400fbf9f
JW
534 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
535 {
536 /* A null type means arg type is not specified.
537 Take whatever the other function type has. */
538 if (TREE_VALUE (p1) == 0)
539 {
540 TREE_VALUE (n) = TREE_VALUE (p2);
541 goto parm_done;
542 }
543 if (TREE_VALUE (p2) == 0)
544 {
545 TREE_VALUE (n) = TREE_VALUE (p1);
546 goto parm_done;
547 }
2f6e4e97 548
400fbf9f
JW
549 /* Given wait (union {union wait *u; int *i} *)
550 and wait (union wait *),
551 prefer union wait * as type of parm. */
552 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
553 && TREE_VALUE (p1) != TREE_VALUE (p2))
554 {
555 tree memb;
58cb41e6
JJ
556 tree mv2 = TREE_VALUE (p2);
557 if (mv2 && mv2 != error_mark_node
558 && TREE_CODE (mv2) != ARRAY_TYPE)
559 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 560 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 561 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
562 {
563 tree mv3 = TREE_TYPE (memb);
564 if (mv3 && mv3 != error_mark_node
565 && TREE_CODE (mv3) != ARRAY_TYPE)
566 mv3 = TYPE_MAIN_VARIANT (mv3);
567 if (comptypes (mv3, mv2))
568 {
569 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
570 TREE_VALUE (p2));
c1771a20 571 pedwarn (input_location, OPT_Wpedantic,
fcf73884 572 "function types not truly compatible in ISO C");
58cb41e6
JJ
573 goto parm_done;
574 }
575 }
400fbf9f
JW
576 }
577 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
578 && TREE_VALUE (p2) != TREE_VALUE (p1))
579 {
580 tree memb;
58cb41e6
JJ
581 tree mv1 = TREE_VALUE (p1);
582 if (mv1 && mv1 != error_mark_node
583 && TREE_CODE (mv1) != ARRAY_TYPE)
584 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 585 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 586 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
587 {
588 tree mv3 = TREE_TYPE (memb);
589 if (mv3 && mv3 != error_mark_node
590 && TREE_CODE (mv3) != ARRAY_TYPE)
591 mv3 = TYPE_MAIN_VARIANT (mv3);
592 if (comptypes (mv3, mv1))
593 {
594 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
595 TREE_VALUE (p1));
c1771a20 596 pedwarn (input_location, OPT_Wpedantic,
fcf73884 597 "function types not truly compatible in ISO C");
58cb41e6
JJ
598 goto parm_done;
599 }
600 }
400fbf9f 601 }
10bc1b1b 602 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
603 parm_done: ;
604 }
605
4b027d16 606 t1 = build_function_type (valtype, newargs);
fe7080d2 607 t1 = qualify_type (t1, t2);
0f41302f 608 /* ... falls through ... */
400fbf9f
JW
609 }
610
611 default:
4b027d16 612 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
613 }
614
615}
10bc1b1b
JM
616
617/* Return the type of a conditional expression between pointers to
618 possibly differently qualified versions of compatible types.
619
620 We assume that comp_target_types has already been done and returned
621 nonzero; if that isn't so, this may crash. */
622
623static tree
624common_pointer_type (tree t1, tree t2)
625{
626 tree attributes;
46df2823
JM
627 tree pointed_to_1, mv1;
628 tree pointed_to_2, mv2;
10bc1b1b 629 tree target;
eb1387a0 630 unsigned target_quals;
36c5e70a
BE
631 addr_space_t as1, as2, as_common;
632 int quals1, quals2;
10bc1b1b
JM
633
634 /* Save time if the two types are the same. */
635
636 if (t1 == t2) return t1;
637
638 /* If one type is nonsense, use the other. */
639 if (t1 == error_mark_node)
640 return t2;
641 if (t2 == error_mark_node)
642 return t1;
643
366de0ce 644 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 645 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
646
647 /* Merge the attributes. */
648 attributes = targetm.merge_type_attributes (t1, t2);
649
650 /* Find the composite type of the target types, and combine the
46df2823
JM
651 qualifiers of the two types' targets. Do not lose qualifiers on
652 array element types by taking the TYPE_MAIN_VARIANT. */
653 mv1 = pointed_to_1 = TREE_TYPE (t1);
654 mv2 = pointed_to_2 = TREE_TYPE (t2);
655 if (TREE_CODE (mv1) != ARRAY_TYPE)
656 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
657 if (TREE_CODE (mv2) != ARRAY_TYPE)
658 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
659 target = composite_type (mv1, mv2);
eb1387a0 660
768952be
MU
661 /* Strip array types to get correct qualifier for pointers to arrays */
662 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
663 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
664
eb1387a0
RG
665 /* For function types do not merge const qualifiers, but drop them
666 if used inconsistently. The middle-end uses these to mark const
667 and noreturn functions. */
668 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 669 target_quals = (quals1 & quals2);
eb1387a0 670 else
36c5e70a
BE
671 target_quals = (quals1 | quals2);
672
673 /* If the two named address spaces are different, determine the common
674 superset address space. This is guaranteed to exist due to the
675 assumption that comp_target_type returned non-zero. */
676 as1 = TYPE_ADDR_SPACE (pointed_to_1);
677 as2 = TYPE_ADDR_SPACE (pointed_to_2);
678 if (!addr_space_superset (as1, as2, &as_common))
679 gcc_unreachable ();
680
681 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
682
eb1387a0 683 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
684 return build_type_attribute_variant (t1, attributes);
685}
686
687/* Return the common type for two arithmetic types under the usual
688 arithmetic conversions. The default conversions have already been
689 applied, and enumerated types converted to their compatible integer
690 types. The resulting type is unqualified and has no attributes.
691
692 This is the type for the result of most arithmetic operations
693 if the operands have the given two types. */
694
ccf7f880
JJ
695static tree
696c_common_type (tree t1, tree t2)
10bc1b1b
JM
697{
698 enum tree_code code1;
699 enum tree_code code2;
700
701 /* If one type is nonsense, use the other. */
702 if (t1 == error_mark_node)
703 return t2;
704 if (t2 == error_mark_node)
705 return t1;
706
707 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
708 t1 = TYPE_MAIN_VARIANT (t1);
709
710 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
711 t2 = TYPE_MAIN_VARIANT (t2);
712
713 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
714 t1 = build_type_attribute_variant (t1, NULL_TREE);
715
716 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
717 t2 = build_type_attribute_variant (t2, NULL_TREE);
718
719 /* Save time if the two types are the same. */
720
721 if (t1 == t2) return t1;
722
723 code1 = TREE_CODE (t1);
724 code2 = TREE_CODE (t2);
725
366de0ce 726 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
727 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
728 || code1 == INTEGER_TYPE);
366de0ce 729 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
730 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
731 || code2 == INTEGER_TYPE);
10bc1b1b 732
5fc89bfd
JJ
733 /* When one operand is a decimal float type, the other operand cannot be
734 a generic float type or a complex type. We also disallow vector types
735 here. */
736 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
737 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
738 {
739 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
740 {
741 error ("can%'t mix operands of decimal float and vector types");
742 return error_mark_node;
743 }
744 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
745 {
746 error ("can%'t mix operands of decimal float and complex types");
747 return error_mark_node;
748 }
749 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
750 {
751 error ("can%'t mix operands of decimal float and other float types");
752 return error_mark_node;
753 }
754 }
755
10bc1b1b
JM
756 /* If one type is a vector type, return that type. (How the usual
757 arithmetic conversions apply to the vector types extension is not
758 precisely specified.) */
759 if (code1 == VECTOR_TYPE)
760 return t1;
761
762 if (code2 == VECTOR_TYPE)
763 return t2;
764
765 /* If one type is complex, form the common type of the non-complex
766 components, then make that complex. Use T1 or T2 if it is the
767 required type. */
768 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
769 {
770 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
771 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 772 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
773
774 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
775 return t1;
776 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
777 return t2;
778 else
779 return build_complex_type (subtype);
780 }
781
782 /* If only one is real, use it as the result. */
783
784 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
785 return t1;
786
787 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
788 return t2;
789
9a8ce21f
JG
790 /* If both are real and either are decimal floating point types, use
791 the decimal floating point type with the greater precision. */
792
793 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
794 {
795 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
796 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
797 return dfloat128_type_node;
798 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
799 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
800 return dfloat64_type_node;
801 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
802 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
803 return dfloat32_type_node;
804 }
805
ab22c1fa
CF
806 /* Deal with fixed-point types. */
807 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
808 {
809 unsigned int unsignedp = 0, satp = 0;
ef4bddc2 810 machine_mode m1, m2;
ab22c1fa
CF
811 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
812
813 m1 = TYPE_MODE (t1);
814 m2 = TYPE_MODE (t2);
815
816 /* If one input type is saturating, the result type is saturating. */
817 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
818 satp = 1;
819
820 /* If both fixed-point types are unsigned, the result type is unsigned.
821 When mixing fixed-point and integer types, follow the sign of the
822 fixed-point type.
823 Otherwise, the result type is signed. */
824 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
825 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
826 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
827 && TYPE_UNSIGNED (t1))
828 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
829 && TYPE_UNSIGNED (t2)))
830 unsignedp = 1;
831
832 /* The result type is signed. */
833 if (unsignedp == 0)
834 {
835 /* If the input type is unsigned, we need to convert to the
836 signed type. */
837 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
838 {
d75d71e0 839 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
840 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
841 mclass = MODE_FRACT;
842 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
843 mclass = MODE_ACCUM;
844 else
845 gcc_unreachable ();
846 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
847 }
848 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
849 {
d75d71e0 850 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
851 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
852 mclass = MODE_FRACT;
853 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
854 mclass = MODE_ACCUM;
855 else
856 gcc_unreachable ();
857 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
858 }
859 }
860
861 if (code1 == FIXED_POINT_TYPE)
862 {
863 fbit1 = GET_MODE_FBIT (m1);
864 ibit1 = GET_MODE_IBIT (m1);
865 }
866 else
867 {
868 fbit1 = 0;
869 /* Signed integers need to subtract one sign bit. */
870 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
871 }
872
873 if (code2 == FIXED_POINT_TYPE)
874 {
875 fbit2 = GET_MODE_FBIT (m2);
876 ibit2 = GET_MODE_IBIT (m2);
877 }
878 else
879 {
880 fbit2 = 0;
881 /* Signed integers need to subtract one sign bit. */
882 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
883 }
884
885 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
886 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
887 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
888 satp);
889 }
890
10bc1b1b
JM
891 /* Both real or both integers; use the one with greater precision. */
892
893 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
894 return t1;
895 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
896 return t2;
897
898 /* Same precision. Prefer long longs to longs to ints when the
899 same precision, following the C99 rules on integer type rank
900 (which are equivalent to the C90 rules for C90 types). */
901
902 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
903 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
904 return long_long_unsigned_type_node;
905
906 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
907 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
908 {
909 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
910 return long_long_unsigned_type_node;
911 else
c22cacf3 912 return long_long_integer_type_node;
10bc1b1b
JM
913 }
914
915 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
916 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
917 return long_unsigned_type_node;
918
919 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
920 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
921 {
922 /* But preserve unsignedness from the other type,
923 since long cannot hold all the values of an unsigned int. */
924 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
925 return long_unsigned_type_node;
926 else
927 return long_integer_type_node;
928 }
929
930 /* Likewise, prefer long double to double even if same size. */
931 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
932 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
933 return long_double_type_node;
934
2531a1d9
JR
935 /* Likewise, prefer double to float even if same size.
936 We got a couple of embedded targets with 32 bit doubles, and the
937 pdp11 might have 64 bit floats. */
938 if (TYPE_MAIN_VARIANT (t1) == double_type_node
939 || TYPE_MAIN_VARIANT (t2) == double_type_node)
940 return double_type_node;
941
10bc1b1b
JM
942 /* Otherwise prefer the unsigned one. */
943
944 if (TYPE_UNSIGNED (t1))
945 return t1;
946 else
947 return t2;
948}
400fbf9f 949\f
5922c215
JM
950/* Wrapper around c_common_type that is used by c-common.c and other
951 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
952 are allowed here and are converted to their compatible integer types.
953 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
954 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
955tree
956common_type (tree t1, tree t2)
957{
958 if (TREE_CODE (t1) == ENUMERAL_TYPE)
959 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
960 if (TREE_CODE (t2) == ENUMERAL_TYPE)
961 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
962
963 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
964 if (TREE_CODE (t1) == BOOLEAN_TYPE
965 && TREE_CODE (t2) == BOOLEAN_TYPE)
966 return boolean_type_node;
967
968 /* If either type is BOOLEAN_TYPE, then return the other. */
969 if (TREE_CODE (t1) == BOOLEAN_TYPE)
970 return t2;
971 if (TREE_CODE (t2) == BOOLEAN_TYPE)
972 return t1;
973
ccf7f880
JJ
974 return c_common_type (t1, t2);
975}
f13c9b2c 976
400fbf9f
JW
977/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
978 or various other operations. Return 2 if they are compatible
979 but a warning may be needed if you use them together. */
980
981int
132da1a5 982comptypes (tree type1, tree type2)
f13c9b2c
AP
983{
984 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
985 int val;
986
dc5027f4 987 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
988 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
989
990 return val;
991}
992
993/* Like comptypes, but if it returns non-zero because enum and int are
994 compatible, it sets *ENUM_AND_INT_P to true. */
995
996static int
997comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
998{
999 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1000 int val;
1001
dc5027f4
JM
1002 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1003 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1004
1005 return val;
1006}
1007
1008/* Like comptypes, but if it returns nonzero for different types, it
1009 sets *DIFFERENT_TYPES_P to true. */
1010
1011int
1012comptypes_check_different_types (tree type1, tree type2,
1013 bool *different_types_p)
1014{
1015 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1016 int val;
1017
1018 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1019 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1020
f13c9b2c 1021 return val;
c22cacf3
MS
1022}
1023\f
f13c9b2c
AP
1024/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1025 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1026 but a warning may be needed if you use them together. If
1027 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1028 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1029 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1030 NULL, and the types are compatible but different enough not to be
48b0b196 1031 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1032 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1033 false, but may or may not be set if the types are incompatible.
1034 This differs from comptypes, in that we don't free the seen
1035 types. */
f13c9b2c
AP
1036
1037static int
dc5027f4
JM
1038comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1039 bool *different_types_p)
400fbf9f 1040{
58f9752a
KG
1041 const_tree t1 = type1;
1042 const_tree t2 = type2;
4b027d16 1043 int attrval, val;
400fbf9f
JW
1044
1045 /* Suppress errors caused by previously reported errors. */
1046
8d47dfc5
RH
1047 if (t1 == t2 || !t1 || !t2
1048 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1049 return 1;
1050
bca63328
JM
1051 /* Enumerated types are compatible with integer types, but this is
1052 not transitive: two enumerated types in the same translation unit
1053 are compatible with each other only if they are the same type. */
400fbf9f 1054
bca63328 1055 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1056 {
1057 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1058 if (TREE_CODE (t2) != VOID_TYPE)
1059 {
1060 if (enum_and_int_p != NULL)
1061 *enum_and_int_p = true;
1062 if (different_types_p != NULL)
1063 *different_types_p = true;
1064 }
744aa42f 1065 }
bca63328 1066 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1067 {
1068 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1069 if (TREE_CODE (t1) != VOID_TYPE)
1070 {
1071 if (enum_and_int_p != NULL)
1072 *enum_and_int_p = true;
1073 if (different_types_p != NULL)
1074 *different_types_p = true;
1075 }
744aa42f 1076 }
400fbf9f
JW
1077
1078 if (t1 == t2)
1079 return 1;
1080
1081 /* Different classes of types can't be compatible. */
1082
3aeb3655
EC
1083 if (TREE_CODE (t1) != TREE_CODE (t2))
1084 return 0;
400fbf9f 1085
118a3a8b 1086 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1087
3932261a 1088 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1089 return 0;
1090
08632da2
RS
1091 /* Allow for two different type nodes which have essentially the same
1092 definition. Note that we already checked for equality of the type
38e01259 1093 qualifiers (just above). */
400fbf9f 1094
46df2823
JM
1095 if (TREE_CODE (t1) != ARRAY_TYPE
1096 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1097 return 1;
1098
4b027d16 1099 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1100 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1101 return 0;
1102
1103 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1104 val = 0;
1105
400fbf9f
JW
1106 switch (TREE_CODE (t1))
1107 {
1108 case POINTER_TYPE:
106f5de5
UW
1109 /* Do not remove mode or aliasing information. */
1110 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1111 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1112 break;
4b027d16 1113 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1114 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1115 enum_and_int_p, different_types_p));
4b027d16 1116 break;
400fbf9f
JW
1117
1118 case FUNCTION_TYPE:
dc5027f4
JM
1119 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1120 different_types_p);
4b027d16 1121 break;
400fbf9f
JW
1122
1123 case ARRAY_TYPE:
1124 {
400fbf9f
JW
1125 tree d1 = TYPE_DOMAIN (t1);
1126 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1127 bool d1_variable, d2_variable;
1128 bool d1_zero, d2_zero;
4b027d16 1129 val = 1;
400fbf9f
JW
1130
1131 /* Target types must match incl. qualifiers. */
1132 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1133 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1134 enum_and_int_p,
1135 different_types_p)))
400fbf9f
JW
1136 return 0;
1137
dc5027f4
JM
1138 if (different_types_p != NULL
1139 && (d1 == 0) != (d2 == 0))
1140 *different_types_p = true;
400fbf9f 1141 /* Sizes must match unless one is missing or variable. */
3f85558f 1142 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1143 break;
400fbf9f 1144
3f75a254
JM
1145 d1_zero = !TYPE_MAX_VALUE (d1);
1146 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1147
3f75a254 1148 d1_variable = (!d1_zero
3f85558f
RH
1149 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1150 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1151 d2_variable = (!d2_zero
3f85558f
RH
1152 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1153 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1154 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1155 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1156
dc5027f4
JM
1157 if (different_types_p != NULL
1158 && d1_variable != d2_variable)
1159 *different_types_p = true;
3f85558f
RH
1160 if (d1_variable || d2_variable)
1161 break;
1162 if (d1_zero && d2_zero)
1163 break;
1164 if (d1_zero || d2_zero
3f75a254
JM
1165 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1166 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1167 val = 0;
1168
c22cacf3 1169 break;
400fbf9f
JW
1170 }
1171
d1bd0ded 1172 case ENUMERAL_TYPE:
58393038 1173 case RECORD_TYPE:
d1bd0ded 1174 case UNION_TYPE:
766beae1 1175 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1176 {
fcb99e7b
JJ
1177 tree a1 = TYPE_ATTRIBUTES (t1);
1178 tree a2 = TYPE_ATTRIBUTES (t2);
1179
1180 if (! attribute_list_contained (a1, a2)
1181 && ! attribute_list_contained (a2, a1))
1182 break;
1183
f13c9b2c 1184 if (attrval != 2)
dc5027f4
JM
1185 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1186 different_types_p);
1187 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1188 different_types_p);
f13c9b2c 1189 }
4b027d16 1190 break;
e9a25f70 1191
62e1dfcf 1192 case VECTOR_TYPE:
744aa42f
ILT
1193 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1194 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1195 enum_and_int_p, different_types_p));
62e1dfcf
NC
1196 break;
1197
e9a25f70
JL
1198 default:
1199 break;
400fbf9f 1200 }
4b027d16 1201 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1202}
1203
36c5e70a
BE
1204/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1205 their qualifiers, except for named address spaces. If the pointers point to
1206 different named addresses, then we must determine if one address space is a
1207 subset of the other. */
400fbf9f
JW
1208
1209static int
744aa42f 1210comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1211{
392202b0 1212 int val;
768952be 1213 int val_ped;
36c5e70a
BE
1214 tree mvl = TREE_TYPE (ttl);
1215 tree mvr = TREE_TYPE (ttr);
1216 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1217 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1218 addr_space_t as_common;
744aa42f 1219 bool enum_and_int_p;
8b40563c 1220
36c5e70a
BE
1221 /* Fail if pointers point to incompatible address spaces. */
1222 if (!addr_space_superset (asl, asr, &as_common))
1223 return 0;
1224
768952be
MU
1225 /* For pedantic record result of comptypes on arrays before losing
1226 qualifiers on the element type below. */
1227 val_ped = 1;
1228
1229 if (TREE_CODE (mvl) == ARRAY_TYPE
1230 && TREE_CODE (mvr) == ARRAY_TYPE)
1231 val_ped = comptypes (mvl, mvr);
1232
1233 /* Qualifiers on element types of array types that are
1234 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1235
1236 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1237 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1238 : TYPE_MAIN_VARIANT (mvl));
1239
1240 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1241 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1242 : TYPE_MAIN_VARIANT (mvr));
1243
744aa42f
ILT
1244 enum_and_int_p = false;
1245 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1246
768952be
MU
1247 if (val == 1 && val_ped != 1)
1248 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1249 "are incompatible in ISO C");
1250
fcf73884 1251 if (val == 2)
c1771a20 1252 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1253
1254 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1255 warning_at (location, OPT_Wc___compat,
1256 "pointer target types incompatible in C++");
1257
400fbf9f
JW
1258 return val;
1259}
1260\f
1261/* Subroutines of `comptypes'. */
1262
f75fbaf7
ZW
1263/* Determine whether two trees derive from the same translation unit.
1264 If the CONTEXT chain ends in a null, that tree's context is still
1265 being parsed, so if two trees have context chains ending in null,
766beae1 1266 they're in the same translation unit. */
f75fbaf7 1267int
58f9752a 1268same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1269{
1270 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1271 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1272 {
6615c446
JO
1273 case tcc_declaration:
1274 t1 = DECL_CONTEXT (t1); break;
1275 case tcc_type:
1276 t1 = TYPE_CONTEXT (t1); break;
1277 case tcc_exceptional:
1278 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1279 default: gcc_unreachable ();
766beae1
ZW
1280 }
1281
1282 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1283 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1284 {
6615c446
JO
1285 case tcc_declaration:
1286 t2 = DECL_CONTEXT (t2); break;
1287 case tcc_type:
1288 t2 = TYPE_CONTEXT (t2); break;
1289 case tcc_exceptional:
1290 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1291 default: gcc_unreachable ();
766beae1
ZW
1292 }
1293
1294 return t1 == t2;
1295}
1296
f13c9b2c 1297/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1298
f13c9b2c 1299static struct tagged_tu_seen_cache *
58f9752a 1300alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1301{
cceb1885 1302 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1303 tu->next = tagged_tu_seen_base;
1304 tu->t1 = t1;
1305 tu->t2 = t2;
c22cacf3 1306
f13c9b2c 1307 tagged_tu_seen_base = tu;
c22cacf3 1308
f13c9b2c
AP
1309 /* The C standard says that two structures in different translation
1310 units are compatible with each other only if the types of their
1311 fields are compatible (among other things). We assume that they
1312 are compatible until proven otherwise when building the cache.
1313 An example where this can occur is:
1314 struct a
1315 {
1316 struct a *next;
1317 };
1318 If we are comparing this against a similar struct in another TU,
c83eecad 1319 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1320 loop. */
1321 tu->val = 1;
1322 return tu;
1323}
d1bd0ded 1324
f13c9b2c 1325/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1326
f13c9b2c
AP
1327static void
1328free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1329{
1330 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1331 while (tu != tu_til)
1332 {
741ac903
KG
1333 const struct tagged_tu_seen_cache *const tu1
1334 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1335 tu = tu1->next;
b1d5455a 1336 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1337 }
1338 tagged_tu_seen_base = tu_til;
1339}
d1bd0ded
GK
1340
1341/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1342 compatible. If the two types are not the same (which has been
1343 checked earlier), this can only happen when multiple translation
1344 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1345 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1346 comptypes_internal. */
d1bd0ded
GK
1347
1348static int
744aa42f 1349tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1350 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1351{
1352 tree s1, s2;
1353 bool needs_warning = false;
3aeb3655 1354
d1bd0ded
GK
1355 /* We have to verify that the tags of the types are the same. This
1356 is harder than it looks because this may be a typedef, so we have
1357 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1358 typedef...
1359 In the case of compiler-created builtin structs the TYPE_DECL
1360 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1361 while (TYPE_NAME (t1)
1362 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1363 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1364 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1365
dea984dc
ILT
1366 while (TYPE_NAME (t2)
1367 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1368 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1369 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1370
1371 /* C90 didn't have the requirement that the two tags be the same. */
1372 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1373 return 0;
3aeb3655 1374
d1bd0ded
GK
1375 /* C90 didn't say what happened if one or both of the types were
1376 incomplete; we choose to follow C99 rules here, which is that they
1377 are compatible. */
1378 if (TYPE_SIZE (t1) == NULL
1379 || TYPE_SIZE (t2) == NULL)
1380 return 1;
3aeb3655 1381
d1bd0ded 1382 {
f13c9b2c 1383 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1384 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1385 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1386 return tts_i->val;
d1bd0ded 1387 }
3aeb3655 1388
d1bd0ded
GK
1389 switch (TREE_CODE (t1))
1390 {
1391 case ENUMERAL_TYPE:
1392 {
f13c9b2c 1393 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1394 /* Speed up the case where the type values are in the same order. */
1395 tree tv1 = TYPE_VALUES (t1);
1396 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1397
c22cacf3 1398 if (tv1 == tv2)
f13c9b2c
AP
1399 {
1400 return 1;
1401 }
3aeb3655 1402
c22cacf3
MS
1403 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1404 {
1405 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1406 break;
1407 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1408 {
c22cacf3 1409 tu->val = 0;
f13c9b2c
AP
1410 return 0;
1411 }
c22cacf3 1412 }
3aeb3655 1413
c22cacf3 1414 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1415 {
1416 return 1;
1417 }
c22cacf3 1418 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1419 {
1420 tu->val = 0;
1421 return 0;
1422 }
3aeb3655 1423
d1bd0ded 1424 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1425 {
1426 tu->val = 0;
1427 return 0;
1428 }
3aeb3655 1429
d1bd0ded
GK
1430 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1431 {
1432 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1433 if (s2 == NULL
1434 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1435 {
1436 tu->val = 0;
1437 return 0;
1438 }
d1bd0ded
GK
1439 }
1440 return 1;
1441 }
1442
1443 case UNION_TYPE:
1444 {
f13c9b2c 1445 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1446 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1447 {
1448 tu->val = 0;
1449 return 0;
1450 }
c22cacf3 1451
f13c9b2c
AP
1452 /* Speed up the common case where the fields are in the same order. */
1453 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1454 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1455 {
1456 int result;
c22cacf3 1457
3ae4d3cc 1458 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1459 break;
744aa42f 1460 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1461 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1462
1463 if (result != 1 && !DECL_NAME (s1))
1464 break;
f13c9b2c
AP
1465 if (result == 0)
1466 {
1467 tu->val = 0;
1468 return 0;
1469 }
1470 if (result == 2)
1471 needs_warning = true;
1472
1473 if (TREE_CODE (s1) == FIELD_DECL
1474 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1475 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1476 {
1477 tu->val = 0;
1478 return 0;
1479 }
1480 }
1481 if (!s1 && !s2)
1482 {
1483 tu->val = needs_warning ? 2 : 1;
1484 return tu->val;
1485 }
d1bd0ded 1486
910ad8de 1487 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1488 {
1489 bool ok = false;
3aeb3655 1490
910ad8de 1491 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1492 if (DECL_NAME (s1) == DECL_NAME (s2))
1493 {
1494 int result;
1495
744aa42f 1496 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1497 enum_and_int_p,
1498 different_types_p);
3ae4d3cc
AO
1499
1500 if (result != 1 && !DECL_NAME (s1))
1501 continue;
1502 if (result == 0)
1503 {
1504 tu->val = 0;
1505 return 0;
1506 }
1507 if (result == 2)
1508 needs_warning = true;
1509
1510 if (TREE_CODE (s1) == FIELD_DECL
1511 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1512 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1513 break;
3ae4d3cc
AO
1514
1515 ok = true;
1516 break;
1517 }
3f75a254 1518 if (!ok)
f13c9b2c
AP
1519 {
1520 tu->val = 0;
1521 return 0;
1522 }
d1bd0ded 1523 }
f13c9b2c
AP
1524 tu->val = needs_warning ? 2 : 10;
1525 return tu->val;
d1bd0ded
GK
1526 }
1527
1528 case RECORD_TYPE:
1529 {
c22cacf3 1530 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1531
1532 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1533 s1 && s2;
910ad8de 1534 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1535 {
1536 int result;
1537 if (TREE_CODE (s1) != TREE_CODE (s2)
1538 || DECL_NAME (s1) != DECL_NAME (s2))
1539 break;
744aa42f 1540 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1541 enum_and_int_p, different_types_p);
d1bd0ded
GK
1542 if (result == 0)
1543 break;
1544 if (result == 2)
1545 needs_warning = true;
3aeb3655 1546
d1bd0ded
GK
1547 if (TREE_CODE (s1) == FIELD_DECL
1548 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1549 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1550 break;
1551 }
d1bd0ded 1552 if (s1 && s2)
f13c9b2c
AP
1553 tu->val = 0;
1554 else
1555 tu->val = needs_warning ? 2 : 1;
1556 return tu->val;
d1bd0ded
GK
1557 }
1558
1559 default:
366de0ce 1560 gcc_unreachable ();
d1bd0ded
GK
1561 }
1562}
1563
400fbf9f
JW
1564/* Return 1 if two function types F1 and F2 are compatible.
1565 If either type specifies no argument types,
1566 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1567 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1568 the other must specify that number of self-promoting arg types.
744aa42f 1569 Otherwise, the argument types must match.
dc5027f4 1570 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1571
1572static int
744aa42f 1573function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1574 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1575{
1576 tree args1, args2;
1577 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1578 int val = 1;
1579 int val1;
a6fdc086
GK
1580 tree ret1, ret2;
1581
1582 ret1 = TREE_TYPE (f1);
1583 ret2 = TREE_TYPE (f2);
1584
e508a019
JM
1585 /* 'volatile' qualifiers on a function's return type used to mean
1586 the function is noreturn. */
1587 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1588 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1589 if (TYPE_VOLATILE (ret1))
1590 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1591 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1592 if (TYPE_VOLATILE (ret2))
1593 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1594 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1595 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1596 if (val == 0)
400fbf9f
JW
1597 return 0;
1598
1599 args1 = TYPE_ARG_TYPES (f1);
1600 args2 = TYPE_ARG_TYPES (f2);
1601
dc5027f4
JM
1602 if (different_types_p != NULL
1603 && (args1 == 0) != (args2 == 0))
1604 *different_types_p = true;
1605
400fbf9f
JW
1606 /* An unspecified parmlist matches any specified parmlist
1607 whose argument types don't need default promotions. */
1608
1609 if (args1 == 0)
1610 {
1611 if (!self_promoting_args_p (args2))
1612 return 0;
1613 /* If one of these types comes from a non-prototype fn definition,
1614 compare that with the other type's arglist.
3176a0c2 1615 If they don't match, ask for a warning (but no error). */
400fbf9f 1616 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1617 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1618 enum_and_int_p, different_types_p))
400fbf9f
JW
1619 val = 2;
1620 return val;
1621 }
1622 if (args2 == 0)
1623 {
1624 if (!self_promoting_args_p (args1))
1625 return 0;
1626 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1627 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1628 enum_and_int_p, different_types_p))
400fbf9f
JW
1629 val = 2;
1630 return val;
1631 }
1632
1633 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1634 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1635 different_types_p);
400fbf9f
JW
1636 return val1 != 1 ? val1 : val;
1637}
1638
744aa42f
ILT
1639/* Check two lists of types for compatibility, returning 0 for
1640 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1641 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1642 comptypes_internal. */
400fbf9f
JW
1643
1644static int
744aa42f 1645type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1646 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1647{
1648 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1649 int val = 1;
9d5f3e49 1650 int newval = 0;
400fbf9f
JW
1651
1652 while (1)
1653 {
46df2823 1654 tree a1, mv1, a2, mv2;
400fbf9f
JW
1655 if (args1 == 0 && args2 == 0)
1656 return val;
1657 /* If one list is shorter than the other,
1658 they fail to match. */
1659 if (args1 == 0 || args2 == 0)
1660 return 0;
46df2823
JM
1661 mv1 = a1 = TREE_VALUE (args1);
1662 mv2 = a2 = TREE_VALUE (args2);
1663 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1664 mv1 = (TYPE_ATOMIC (mv1)
1665 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1666 TYPE_QUAL_ATOMIC)
1667 : TYPE_MAIN_VARIANT (mv1));
46df2823 1668 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1669 mv2 = (TYPE_ATOMIC (mv2)
1670 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1671 TYPE_QUAL_ATOMIC)
1672 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1673 /* A null pointer instead of a type
1674 means there is supposed to be an argument
1675 but nothing is specified about what type it has.
1676 So match anything that self-promotes. */
dc5027f4
JM
1677 if (different_types_p != NULL
1678 && (a1 == 0) != (a2 == 0))
1679 *different_types_p = true;
46df2823 1680 if (a1 == 0)
400fbf9f 1681 {
46df2823 1682 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1683 return 0;
1684 }
46df2823 1685 else if (a2 == 0)
400fbf9f 1686 {
46df2823 1687 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1688 return 0;
1689 }
8f5b6d29 1690 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1691 else if (TREE_CODE (a1) == ERROR_MARK
1692 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1693 ;
dc5027f4
JM
1694 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1695 different_types_p)))
400fbf9f 1696 {
dc5027f4
JM
1697 if (different_types_p != NULL)
1698 *different_types_p = true;
400fbf9f
JW
1699 /* Allow wait (union {union wait *u; int *i} *)
1700 and wait (union wait *) to be compatible. */
46df2823
JM
1701 if (TREE_CODE (a1) == UNION_TYPE
1702 && (TYPE_NAME (a1) == 0
ebf0bf7f 1703 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1704 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1705 && tree_int_cst_equal (TYPE_SIZE (a1),
1706 TYPE_SIZE (a2)))
400fbf9f
JW
1707 {
1708 tree memb;
46df2823 1709 for (memb = TYPE_FIELDS (a1);
910ad8de 1710 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1711 {
1712 tree mv3 = TREE_TYPE (memb);
1713 if (mv3 && mv3 != error_mark_node
1714 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1715 mv3 = (TYPE_ATOMIC (mv3)
1716 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1717 TYPE_QUAL_ATOMIC)
1718 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1719 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1720 different_types_p))
58cb41e6
JJ
1721 break;
1722 }
400fbf9f
JW
1723 if (memb == 0)
1724 return 0;
1725 }
46df2823
JM
1726 else if (TREE_CODE (a2) == UNION_TYPE
1727 && (TYPE_NAME (a2) == 0
ebf0bf7f 1728 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1729 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1730 && tree_int_cst_equal (TYPE_SIZE (a2),
1731 TYPE_SIZE (a1)))
400fbf9f
JW
1732 {
1733 tree memb;
46df2823 1734 for (memb = TYPE_FIELDS (a2);
910ad8de 1735 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1736 {
1737 tree mv3 = TREE_TYPE (memb);
1738 if (mv3 && mv3 != error_mark_node
1739 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1740 mv3 = (TYPE_ATOMIC (mv3)
1741 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1742 TYPE_QUAL_ATOMIC)
1743 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1744 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1745 different_types_p))
58cb41e6
JJ
1746 break;
1747 }
400fbf9f
JW
1748 if (memb == 0)
1749 return 0;
1750 }
1751 else
1752 return 0;
1753 }
1754
1755 /* comptypes said ok, but record if it said to warn. */
1756 if (newval > val)
1757 val = newval;
1758
1759 args1 = TREE_CHAIN (args1);
1760 args2 = TREE_CHAIN (args2);
1761 }
1762}
400fbf9f 1763\f
a0e24419
MP
1764/* Compute the size to increment a pointer by. When a function type or void
1765 type or incomplete type is passed, size_one_node is returned.
1766 This function does not emit any diagnostics; the caller is responsible
1767 for that. */
400fbf9f 1768
4e2fb7de 1769static tree
58f9752a 1770c_size_in_bytes (const_tree type)
400fbf9f
JW
1771{
1772 enum tree_code code = TREE_CODE (type);
1773
a0e24419
MP
1774 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1775 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1776 return size_one_node;
1777
400fbf9f 1778 /* Convert in case a char is more than one unit. */
db3927fb
AH
1779 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1780 size_int (TYPE_PRECISION (char_type_node)
1781 / BITS_PER_UNIT));
400fbf9f 1782}
400fbf9f 1783\f
400fbf9f
JW
1784/* Return either DECL or its known constant value (if it has one). */
1785
56cb9733 1786tree
2f6e4e97 1787decl_constant_value (tree decl)
400fbf9f 1788{
a7c1916a 1789 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1790 in a place where a variable is invalid. Note that DECL_INITIAL
1791 isn't valid for a PARM_DECL. */
a7c1916a 1792 current_function_decl != 0
4f976745 1793 && TREE_CODE (decl) != PARM_DECL
3f75a254 1794 && !TREE_THIS_VOLATILE (decl)
83bab8db 1795 && TREE_READONLY (decl)
400fbf9f
JW
1796 && DECL_INITIAL (decl) != 0
1797 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1798 /* This is invalid if initial value is not constant.
1799 If it has either a function call, a memory reference,
1800 or a variable, then re-evaluating it could give different results. */
1801 && TREE_CONSTANT (DECL_INITIAL (decl))
1802 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1803 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1804 return DECL_INITIAL (decl);
1805 return decl;
1806}
1807
f2a71bbc
JM
1808/* Convert the array expression EXP to a pointer. */
1809static tree
c2255bc4 1810array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1811{
f2a71bbc 1812 tree orig_exp = exp;
207bf485 1813 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1814 tree adr;
1815 tree restype = TREE_TYPE (type);
1816 tree ptrtype;
207bf485 1817
f2a71bbc 1818 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1819
f2a71bbc 1820 STRIP_TYPE_NOPS (exp);
207bf485 1821
487a92fe
JM
1822 if (TREE_NO_WARNING (orig_exp))
1823 TREE_NO_WARNING (exp) = 1;
207bf485 1824
f2a71bbc
JM
1825 ptrtype = build_pointer_type (restype);
1826
22d03525 1827 if (INDIRECT_REF_P (exp))
f2a71bbc
JM
1828 return convert (ptrtype, TREE_OPERAND (exp, 0));
1829
1f37c583
JM
1830 /* In C++ array compound literals are temporary objects unless they are
1831 const or appear in namespace scope, so they are destroyed too soon
1832 to use them for much of anything (c++/53220). */
1833 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1834 {
1835 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1836 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1837 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1838 "converting an array compound literal to a pointer "
1839 "is ill-formed in C++");
1840 }
1841
c2255bc4 1842 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1843 return convert (ptrtype, adr);
1844}
207bf485 1845
f2a71bbc
JM
1846/* Convert the function expression EXP to a pointer. */
1847static tree
c2255bc4 1848function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1849{
1850 tree orig_exp = exp;
207bf485 1851
f2a71bbc 1852 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1853
f2a71bbc 1854 STRIP_TYPE_NOPS (exp);
207bf485 1855
f2a71bbc
JM
1856 if (TREE_NO_WARNING (orig_exp))
1857 TREE_NO_WARNING (exp) = 1;
207bf485 1858
c2255bc4 1859 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1860}
207bf485 1861
ebfbbdc5
JJ
1862/* Mark EXP as read, not just set, for set but not used -Wunused
1863 warning purposes. */
1864
1865void
1866mark_exp_read (tree exp)
1867{
1868 switch (TREE_CODE (exp))
1869 {
1870 case VAR_DECL:
1871 case PARM_DECL:
1872 DECL_READ_P (exp) = 1;
1873 break;
1874 case ARRAY_REF:
1875 case COMPONENT_REF:
1876 case MODIFY_EXPR:
1877 case REALPART_EXPR:
1878 case IMAGPART_EXPR:
1879 CASE_CONVERT:
1880 case ADDR_EXPR:
1881 mark_exp_read (TREE_OPERAND (exp, 0));
1882 break;
1883 case COMPOUND_EXPR:
82c3c067 1884 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1885 mark_exp_read (TREE_OPERAND (exp, 1));
1886 break;
1887 default:
1888 break;
1889 }
1890}
1891
f2a71bbc
JM
1892/* Perform the default conversion of arrays and functions to pointers.
1893 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1894 return EXP.
1895
1896 LOC is the location of the expression. */
f2a71bbc
JM
1897
1898struct c_expr
c2255bc4 1899default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1900{
1901 tree orig_exp = exp.value;
1902 tree type = TREE_TYPE (exp.value);
1903 enum tree_code code = TREE_CODE (type);
1904
1905 switch (code)
1906 {
1907 case ARRAY_TYPE:
1908 {
1909 bool not_lvalue = false;
1910 bool lvalue_array_p;
1911
1912 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1913 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1914 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1915 {
1916 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1917 not_lvalue = true;
1918 exp.value = TREE_OPERAND (exp.value, 0);
1919 }
1920
1921 if (TREE_NO_WARNING (orig_exp))
1922 TREE_NO_WARNING (exp.value) = 1;
1923
1924 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1925 if (!flag_isoc99 && !lvalue_array_p)
1926 {
1927 /* Before C99, non-lvalue arrays do not decay to pointers.
1928 Normally, using such an array would be invalid; but it can
1929 be used correctly inside sizeof or as a statement expression.
1930 Thus, do not give an error here; an error will result later. */
1931 return exp;
1932 }
1933
c2255bc4 1934 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1935 }
1936 break;
1937 case FUNCTION_TYPE:
c2255bc4 1938 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1939 break;
1940 default:
f2a71bbc 1941 break;
207bf485 1942 }
f2a71bbc 1943
207bf485
JM
1944 return exp;
1945}
1946
ebfbbdc5
JJ
1947struct c_expr
1948default_function_array_read_conversion (location_t loc, struct c_expr exp)
1949{
1950 mark_exp_read (exp.value);
1951 return default_function_array_conversion (loc, exp);
1952}
522ddfa2 1953
267bac10
JM
1954/* Return whether EXPR should be treated as an atomic lvalue for the
1955 purposes of load and store handling. */
1956
1957static bool
1958really_atomic_lvalue (tree expr)
1959{
7a0ca710 1960 if (error_operand_p (expr))
267bac10
JM
1961 return false;
1962 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1963 return false;
1964 if (!lvalue_p (expr))
1965 return false;
1966
1967 /* Ignore _Atomic on register variables, since their addresses can't
1968 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1969 sequences wouldn't work. Ignore _Atomic on structures containing
1970 bit-fields, since accessing elements of atomic structures or
1971 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1972 it's undefined at translation time or execution time, and the
1973 normal atomic sequences again wouldn't work. */
1974 while (handled_component_p (expr))
1975 {
1976 if (TREE_CODE (expr) == COMPONENT_REF
1977 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1978 return false;
1979 expr = TREE_OPERAND (expr, 0);
1980 }
1981 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1982 return false;
1983 return true;
1984}
1985
1986/* Convert expression EXP (location LOC) from lvalue to rvalue,
1987 including converting functions and arrays to pointers if CONVERT_P.
1988 If READ_P, also mark the expression as having been read. */
1989
1990struct c_expr
1991convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1992 bool convert_p, bool read_p)
1993{
1994 if (read_p)
1995 mark_exp_read (exp.value);
1996 if (convert_p)
1997 exp = default_function_array_conversion (loc, exp);
1998 if (really_atomic_lvalue (exp.value))
1999 {
2000 vec<tree, va_gc> *params;
2001 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2002 tree expr_type = TREE_TYPE (exp.value);
2003 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2004 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2005
2006 gcc_assert (TYPE_ATOMIC (expr_type));
2007
2008 /* Expansion of a generic atomic load may require an addition
2009 element, so allocate enough to prevent a resize. */
2010 vec_alloc (params, 4);
2011
2012 /* Remove the qualifiers for the rest of the expressions and
2013 create the VAL temp variable to hold the RHS. */
2014 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
5c4abbb8 2015 tmp = create_tmp_var_raw (nonatomic_type);
267bac10
JM
2016 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2017 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2018 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2019
2020 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2021 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2022 params->quick_push (expr_addr);
2023 params->quick_push (tmp_addr);
2024 params->quick_push (seq_cst);
8edbfaa6 2025 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2026
cc28fc7f
MP
2027 /* EXPR is always read. */
2028 mark_exp_read (exp.value);
2029
267bac10 2030 /* Return tmp which contains the value loaded. */
5c4abbb8
MP
2031 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2032 NULL_TREE, NULL_TREE);
267bac10
JM
2033 }
2034 return exp;
2035}
2036
522ddfa2
JM
2037/* EXP is an expression of integer type. Apply the integer promotions
2038 to it and return the promoted value. */
400fbf9f
JW
2039
2040tree
522ddfa2 2041perform_integral_promotions (tree exp)
400fbf9f 2042{
b3694847
SS
2043 tree type = TREE_TYPE (exp);
2044 enum tree_code code = TREE_CODE (type);
400fbf9f 2045
522ddfa2 2046 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2047
400fbf9f
JW
2048 /* Normally convert enums to int,
2049 but convert wide enums to something wider. */
2050 if (code == ENUMERAL_TYPE)
2051 {
b0c48229
NB
2052 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2053 TYPE_PRECISION (integer_type_node)),
2054 ((TYPE_PRECISION (type)
2055 >= TYPE_PRECISION (integer_type_node))
8df83eae 2056 && TYPE_UNSIGNED (type)));
05bccae2 2057
400fbf9f
JW
2058 return convert (type, exp);
2059 }
2060
522ddfa2
JM
2061 /* ??? This should no longer be needed now bit-fields have their
2062 proper types. */
9753f113 2063 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2064 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2065 /* If it's thinner than an int, promote it like a
d72040f5 2066 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2067 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2068 TYPE_PRECISION (integer_type_node)))
f458d1d5 2069 return convert (integer_type_node, exp);
9753f113 2070
d72040f5 2071 if (c_promoting_integer_type_p (type))
400fbf9f 2072 {
f458d1d5 2073 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2074 if (TYPE_UNSIGNED (type)
f458d1d5 2075 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2076 return convert (unsigned_type_node, exp);
05bccae2 2077
400fbf9f
JW
2078 return convert (integer_type_node, exp);
2079 }
05bccae2 2080
522ddfa2
JM
2081 return exp;
2082}
2083
2084
2085/* Perform default promotions for C data used in expressions.
46bdb9cf 2086 Enumeral types or short or char are converted to int.
522ddfa2
JM
2087 In addition, manifest constants symbols are replaced by their values. */
2088
2089tree
2090default_conversion (tree exp)
2091{
2092 tree orig_exp;
2093 tree type = TREE_TYPE (exp);
2094 enum tree_code code = TREE_CODE (type);
40449a90 2095 tree promoted_type;
522ddfa2 2096
ebfbbdc5
JJ
2097 mark_exp_read (exp);
2098
46bdb9cf
JM
2099 /* Functions and arrays have been converted during parsing. */
2100 gcc_assert (code != FUNCTION_TYPE);
2101 if (code == ARRAY_TYPE)
2102 return exp;
522ddfa2
JM
2103
2104 /* Constants can be used directly unless they're not loadable. */
2105 if (TREE_CODE (exp) == CONST_DECL)
2106 exp = DECL_INITIAL (exp);
2107
522ddfa2
JM
2108 /* Strip no-op conversions. */
2109 orig_exp = exp;
2110 STRIP_TYPE_NOPS (exp);
2111
2112 if (TREE_NO_WARNING (orig_exp))
2113 TREE_NO_WARNING (exp) = 1;
2114
400fbf9f
JW
2115 if (code == VOID_TYPE)
2116 {
5436fa2e
MP
2117 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2118 "void value not ignored as it ought to be");
400fbf9f
JW
2119 return error_mark_node;
2120 }
808d6eaa 2121
4f2e1536 2122 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
808d6eaa
JM
2123 if (exp == error_mark_node)
2124 return error_mark_node;
2125
40449a90
SL
2126 promoted_type = targetm.promoted_type (type);
2127 if (promoted_type)
2128 return convert (promoted_type, exp);
2129
808d6eaa
JM
2130 if (INTEGRAL_TYPE_P (type))
2131 return perform_integral_promotions (exp);
2132
400fbf9f
JW
2133 return exp;
2134}
2135\f
0fb96aa4 2136/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2137
2138 If the component name is not found, returns NULL_TREE. Otherwise,
2139 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2140 stepping down the chain to the component, which is in the last
2141 TREE_VALUE of the list. Normally the list is of length one, but if
2142 the component is embedded within (nested) anonymous structures or
2143 unions, the list steps down the chain to the component. */
2f6e4e97 2144
2f2d13da 2145static tree
0fb96aa4 2146lookup_field (tree type, tree component)
2f2d13da
DE
2147{
2148 tree field;
2149
2150 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2151 to the field elements. Use a binary search on this array to quickly
2152 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2153 will always be set for structures which have many elements. */
2154
22a0b85f 2155 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2156 {
2157 int bot, top, half;
d07605f5 2158 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2159
2160 field = TYPE_FIELDS (type);
2161 bot = 0;
d07605f5 2162 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2163 while (top - bot > 1)
2164 {
2f2d13da
DE
2165 half = (top - bot + 1) >> 1;
2166 field = field_array[bot+half];
2167
2168 if (DECL_NAME (field) == NULL_TREE)
2169 {
2170 /* Step through all anon unions in linear fashion. */
2171 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2172 {
2f2d13da 2173 field = field_array[bot++];
3e636daf 2174 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
19d76e60 2175 {
0fb96aa4 2176 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2177
2178 if (anon)
2179 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2180
2181 /* The Plan 9 compiler permits referring
2182 directly to an anonymous struct/union field
2183 using a typedef name. */
2184 if (flag_plan9_extensions
2185 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2186 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2187 == TYPE_DECL)
2188 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2189 == component))
2190 break;
2f6e4e97 2191 }
2f2d13da
DE
2192 }
2193
2194 /* Entire record is only anon unions. */
2195 if (bot > top)
2196 return NULL_TREE;
2197
2198 /* Restart the binary search, with new lower bound. */
2199 continue;
2200 }
2201
e8b87aac 2202 if (DECL_NAME (field) == component)
2f2d13da 2203 break;
e8b87aac 2204 if (DECL_NAME (field) < component)
2f2d13da
DE
2205 bot += half;
2206 else
2207 top = bot + half;
2208 }
2209
2210 if (DECL_NAME (field_array[bot]) == component)
2211 field = field_array[bot];
2212 else if (DECL_NAME (field) != component)
e9b2c823 2213 return NULL_TREE;
2f2d13da
DE
2214 }
2215 else
2216 {
910ad8de 2217 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2218 {
e9b2c823 2219 if (DECL_NAME (field) == NULL_TREE
3e636daf 2220 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2f2d13da 2221 {
0fb96aa4 2222 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2223
e9b2c823
NB
2224 if (anon)
2225 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2226
2227 /* The Plan 9 compiler permits referring directly to an
2228 anonymous struct/union field using a typedef
2229 name. */
2230 if (flag_plan9_extensions
2231 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2232 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2233 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2234 == component))
2235 break;
2f2d13da
DE
2236 }
2237
2238 if (DECL_NAME (field) == component)
2239 break;
2240 }
e9b2c823
NB
2241
2242 if (field == NULL_TREE)
2243 return NULL_TREE;
2f2d13da
DE
2244 }
2245
e9b2c823 2246 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2247}
2248
277fe616
DM
2249/* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2250
2251static void
2252lookup_field_fuzzy_find_candidates (tree type, tree component,
2253 vec<tree> *candidates)
2254{
2255 tree field;
2256 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2257 {
2258 if (DECL_NAME (field) == NULL_TREE
3e636daf
MP
2259 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2260 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2261 candidates);
277fe616
DM
2262
2263 if (DECL_NAME (field))
2264 candidates->safe_push (DECL_NAME (field));
2265 }
2266}
2267
2268/* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2269 rather than returning a TREE_LIST for an exact match. */
2270
2271static tree
2272lookup_field_fuzzy (tree type, tree component)
2273{
2274 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2275
2276 /* First, gather a list of candidates. */
2277 auto_vec <tree> candidates;
2278
2279 lookup_field_fuzzy_find_candidates (type, component,
2280 &candidates);
2281
8ece8dfb 2282 return find_closest_identifier (component, &candidates);
277fe616
DM
2283}
2284
a87a86e1
DM
2285/* Support function for build_component_ref's error-handling.
2286
2287 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2288 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2289
2290static bool
2291should_suggest_deref_p (tree datum_type)
2292{
2293 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2294 allows "." for ptrs; we could be handling a failed attempt
2295 to access a property. */
2296 if (c_dialect_objc ())
2297 return false;
2298
2299 /* Only suggest it for pointers... */
2300 if (TREE_CODE (datum_type) != POINTER_TYPE)
2301 return false;
2302
2303 /* ...to structs/unions. */
2304 tree underlying_type = TREE_TYPE (datum_type);
2305 enum tree_code code = TREE_CODE (underlying_type);
2306 if (code == RECORD_TYPE || code == UNION_TYPE)
2307 return true;
2308 else
2309 return false;
2310}
2311
c2255bc4
AH
2312/* Make an expression to refer to the COMPONENT field of structure or
2313 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2314 location of the COMPONENT_REF. */
400fbf9f
JW
2315
2316tree
c2255bc4 2317build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2318{
b3694847
SS
2319 tree type = TREE_TYPE (datum);
2320 enum tree_code code = TREE_CODE (type);
2321 tree field = NULL;
2322 tree ref;
1e57bf47 2323 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2324
7a3ea201
RH
2325 if (!objc_is_public (datum, component))
2326 return error_mark_node;
2327
46a88c12 2328 /* Detect Objective-C property syntax object.property. */
668ea4b1 2329 if (c_dialect_objc ()
46a88c12 2330 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2331 return ref;
2332
400fbf9f
JW
2333 /* See if there is a field or component with name COMPONENT. */
2334
2335 if (code == RECORD_TYPE || code == UNION_TYPE)
2336 {
d0f062fb 2337 if (!COMPLETE_TYPE_P (type))
400fbf9f 2338 {
4f2e1536 2339 c_incomplete_type_error (loc, NULL_TREE, type);
400fbf9f
JW
2340 return error_mark_node;
2341 }
2342
0fb96aa4 2343 field = lookup_field (type, component);
400fbf9f
JW
2344
2345 if (!field)
2346 {
277fe616
DM
2347 tree guessed_id = lookup_field_fuzzy (type, component);
2348 if (guessed_id)
2349 error_at (loc, "%qT has no member named %qE; did you mean %qE?",
2350 type, component, guessed_id);
2351 else
2352 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2353 return error_mark_node;
2354 }
400fbf9f 2355
fda5652f
MP
2356 /* Accessing elements of atomic structures or unions is undefined
2357 behavior (C11 6.5.2.3#5). */
2358 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2359 {
2360 if (code == RECORD_TYPE)
2361 warning_at (loc, 0, "accessing a member %qE of an atomic "
2362 "structure %qE", component, datum);
2363 else
2364 warning_at (loc, 0, "accessing a member %qE of an atomic "
2365 "union %qE", component, datum);
2366 }
2367
e9b2c823
NB
2368 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2369 This might be better solved in future the way the C++ front
2370 end does it - by giving the anonymous entities each a
2371 separate name and type, and then have build_component_ref
2372 recursively call itself. We can't do that here. */
46ea50cb 2373 do
19d76e60 2374 {
e9b2c823 2375 tree subdatum = TREE_VALUE (field);
efed193e
JM
2376 int quals;
2377 tree subtype;
1e57bf47 2378 bool use_datum_quals;
e9b2c823
NB
2379
2380 if (TREE_TYPE (subdatum) == error_mark_node)
2381 return error_mark_node;
2382
1e57bf47
JM
2383 /* If this is an rvalue, it does not have qualifiers in C
2384 standard terms and we must avoid propagating such
2385 qualifiers down to a non-lvalue array that is then
2386 converted to a pointer. */
2387 use_datum_quals = (datum_lvalue
2388 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2389
efed193e 2390 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2391 if (use_datum_quals)
2392 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2393 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2394
2395 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2396 NULL_TREE);
c2255bc4 2397 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2398 if (TREE_READONLY (subdatum)
2399 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2400 TREE_READONLY (ref) = 1;
1e57bf47
JM
2401 if (TREE_THIS_VOLATILE (subdatum)
2402 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2403 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2404
2405 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2406 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2407
19d76e60 2408 datum = ref;
46ea50cb
RS
2409
2410 field = TREE_CHAIN (field);
19d76e60 2411 }
46ea50cb 2412 while (field);
19d76e60 2413
400fbf9f
JW
2414 return ref;
2415 }
a87a86e1
DM
2416 else if (should_suggest_deref_p (type))
2417 {
2418 /* Special-case the error message for "ptr.field" for the case
2419 where the user has confused "." vs "->". */
2420 rich_location richloc (line_table, loc);
2421 /* "loc" should be the "." token. */
2422 richloc.add_fixit_replace (source_range::from_location (loc), "->");
2423 error_at_rich_loc (&richloc,
2424 "%qE is a pointer; did you mean to use %<->%>?",
2425 datum);
2426 return error_mark_node;
2427 }
400fbf9f 2428 else if (code != ERROR_MARK)
c2255bc4
AH
2429 error_at (loc,
2430 "request for member %qE in something not a structure or union",
2431 component);
400fbf9f
JW
2432
2433 return error_mark_node;
2434}
2435\f
2436/* Given an expression PTR for a pointer, return an expression
2437 for the value pointed to.
6a3799eb
AH
2438 ERRORSTRING is the name of the operator to appear in error messages.
2439
2440 LOC is the location to use for the generated tree. */
400fbf9f
JW
2441
2442tree
dd865ef6 2443build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2444{
b3694847
SS
2445 tree pointer = default_conversion (ptr);
2446 tree type = TREE_TYPE (pointer);
6a3799eb 2447 tree ref;
400fbf9f
JW
2448
2449 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2450 {
1043771b 2451 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2452 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2453 {
2454 /* If a warning is issued, mark it to avoid duplicates from
2455 the backend. This only needs to be done at
2456 warn_strict_aliasing > 2. */
2457 if (warn_strict_aliasing > 2)
2458 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2459 type, TREE_OPERAND (pointer, 0)))
2460 TREE_NO_WARNING (pointer) = 1;
2461 }
2462
870cc33b 2463 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2464 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2465 == TREE_TYPE (type)))
6a3799eb
AH
2466 {
2467 ref = TREE_OPERAND (pointer, 0);
2468 protected_set_expr_location (ref, loc);
2469 return ref;
2470 }
870cc33b
RS
2471 else
2472 {
2473 tree t = TREE_TYPE (type);
46df2823 2474
984dfd8c 2475 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2476
baae9b65 2477 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2478 {
d9b7be2e
MP
2479 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2480 {
2481 error_at (loc, "dereferencing pointer to incomplete type "
2482 "%qT", t);
2483 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2484 }
870cc33b
RS
2485 return error_mark_node;
2486 }
7d882b83 2487 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2488 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2489
2490 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2491 so that we get the proper error message if the result is used
2492 to assign to. Also, &* is supposed to be a no-op.
2493 And ANSI C seems to specify that the type of the result
2494 should be the const type. */
2495 /* A de-reference of a pointer to const is not a const. It is valid
2496 to change it via some other pointer. */
2497 TREE_READONLY (ref) = TYPE_READONLY (t);
2498 TREE_SIDE_EFFECTS (ref)
271bd540 2499 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2500 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2501 protected_set_expr_location (ref, loc);
870cc33b
RS
2502 return ref;
2503 }
2504 }
400fbf9f 2505 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2506 invalid_indirection_error (loc, type, errstring);
2507
400fbf9f
JW
2508 return error_mark_node;
2509}
2510
2511/* This handles expressions of the form "a[i]", which denotes
2512 an array reference.
2513
2514 This is logically equivalent in C to *(a+i), but we may do it differently.
2515 If A is a variable or a member, we generate a primitive ARRAY_REF.
2516 This avoids forcing the array out of registers, and can work on
2517 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2518 by functions).
2519
30cd1c5d
AS
2520 For vector types, allow vector[i] but not i[vector], and create
2521 *(((type*)&vectortype) + i) for the expression.
2522
6a3799eb 2523 LOC is the location to use for the returned expression. */
400fbf9f
JW
2524
2525tree
c2255bc4 2526build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2527{
6a3799eb 2528 tree ret;
a4ab7973 2529 bool swapped = false;
400fbf9f
JW
2530 if (TREE_TYPE (array) == error_mark_node
2531 || TREE_TYPE (index) == error_mark_node)
2532 return error_mark_node;
2533
b72271b9 2534 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2535 {
2536 size_t rank = 0;
2537 if (!find_rank (loc, index, index, true, &rank))
2538 return error_mark_node;
2539 if (rank > 1)
2540 {
2541 error_at (loc, "rank of the array's index is greater than 1");
2542 return error_mark_node;
2543 }
2544 }
a4ab7973 2545 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2546 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2547 /* Allow vector[index] but not index[vector]. */
31521951 2548 && !VECTOR_TYPE_P (TREE_TYPE (array)))
400fbf9f 2549 {
a4ab7973
JM
2550 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2551 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2552 {
f90e8e2e 2553 error_at (loc,
30cd1c5d
AS
2554 "subscripted value is neither array nor pointer nor vector");
2555
fdeefd49
RS
2556 return error_mark_node;
2557 }
fab27f52 2558 std::swap (array, index);
a4ab7973
JM
2559 swapped = true;
2560 }
2561
2562 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2563 {
a63068b6 2564 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2565 return error_mark_node;
2566 }
2567
2568 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2569 {
a63068b6 2570 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2571 return error_mark_node;
2572 }
2573
ff6b6641
GDR
2574 /* ??? Existing practice has been to warn only when the char
2575 index is syntactically the index, not for char[array]. */
2576 if (!swapped)
5bd012f8 2577 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2578
2579 /* Apply default promotions *after* noticing character types. */
2580 index = default_conversion (index);
f406ae1f
MP
2581 if (index == error_mark_node)
2582 return error_mark_node;
a4ab7973
JM
2583
2584 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2585
f17a223d
RB
2586 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2587 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
a4ab7973
JM
2588
2589 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2590 {
e4d35515 2591 tree rval, type;
fdeefd49 2592
400fbf9f
JW
2593 /* An array that is indexed by a non-constant
2594 cannot be stored in a register; we must be able to do
2595 address arithmetic on its address.
2596 Likewise an array of elements of variable size. */
2597 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2598 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2599 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2600 {
dffd7eb6 2601 if (!c_mark_addressable (array))
400fbf9f
JW
2602 return error_mark_node;
2603 }
e6d52559
JW
2604 /* An array that is indexed by a constant value which is not within
2605 the array bounds cannot be stored in a register either; because we
2606 would get a crash in store_bit_field/extract_bit_field when trying
2607 to access a non-existent part of the register. */
2608 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2609 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2610 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2611 {
dffd7eb6 2612 if (!c_mark_addressable (array))
e6d52559
JW
2613 return error_mark_node;
2614 }
400fbf9f 2615
f17a223d
RB
2616 if ((pedantic || warn_c90_c99_compat)
2617 && ! was_vector)
400fbf9f
JW
2618 {
2619 tree foo = array;
2620 while (TREE_CODE (foo) == COMPONENT_REF)
2621 foo = TREE_OPERAND (foo, 0);
0ae9bd27 2622 if (VAR_P (foo) && C_DECL_REGISTER (foo))
c1771a20 2623 pedwarn (loc, OPT_Wpedantic,
fcf73884 2624 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2625 else if (!lvalue_p (foo))
2626 pedwarn_c90 (loc, OPT_Wpedantic,
2627 "ISO C90 forbids subscripting non-lvalue "
2628 "array");
400fbf9f
JW
2629 }
2630
46df2823 2631 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2632 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2633 /* Array ref is const/volatile if the array elements are
c22cacf3 2634 or if the array is. */
400fbf9f
JW
2635 TREE_READONLY (rval)
2636 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2637 | TREE_READONLY (array));
2638 TREE_SIDE_EFFECTS (rval)
2639 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2640 | TREE_SIDE_EFFECTS (array));
2641 TREE_THIS_VOLATILE (rval)
2642 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2643 /* This was added by rms on 16 Nov 91.
2f6e4e97 2644 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2645 in an inline function.
2646 Hope it doesn't break something else. */
2647 | TREE_THIS_VOLATILE (array));
4f2e1536 2648 ret = require_complete_type (loc, rval);
6a3799eb 2649 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2650 if (non_lvalue)
2651 ret = non_lvalue_loc (loc, ret);
6a3799eb 2652 return ret;
400fbf9f 2653 }
a4ab7973
JM
2654 else
2655 {
2656 tree ar = default_conversion (array);
400fbf9f 2657
a4ab7973
JM
2658 if (ar == error_mark_node)
2659 return ar;
400fbf9f 2660
a4ab7973
JM
2661 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2662 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2663
aa7da51a
JJ
2664 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2665 index, 0),
2666 RO_ARRAY_INDEXING);
2667 if (non_lvalue)
2668 ret = non_lvalue_loc (loc, ret);
2669 return ret;
a4ab7973 2670 }
400fbf9f
JW
2671}
2672\f
7e585d16 2673/* Build an external reference to identifier ID. FUN indicates
766beb40 2674 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2675 location of the identifier. This sets *TYPE to the type of the
2676 identifier, which is not the same as the type of the returned value
2677 for CONST_DECLs defined as enum constants. If the type of the
2678 identifier is not available, *TYPE is set to NULL. */
7e585d16 2679tree
c2255bc4 2680build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2681{
2682 tree ref;
2683 tree decl = lookup_name (id);
16b34ad6
ZL
2684
2685 /* In Objective-C, an instance variable (ivar) may be preferred to
2686 whatever lookup_name() found. */
2687 decl = objc_lookup_ivar (decl, id);
7e585d16 2688
6866c6e8 2689 *type = NULL;
339a28b9 2690 if (decl && decl != error_mark_node)
6866c6e8
ILT
2691 {
2692 ref = decl;
2693 *type = TREE_TYPE (ref);
2694 }
339a28b9
ZW
2695 else if (fun)
2696 /* Implicit function declaration. */
c2255bc4 2697 ref = implicitly_declare (loc, id);
339a28b9
ZW
2698 else if (decl == error_mark_node)
2699 /* Don't complain about something that's already been
2700 complained about. */
2701 return error_mark_node;
2702 else
2703 {
c2255bc4 2704 undeclared_variable (loc, id);
339a28b9
ZW
2705 return error_mark_node;
2706 }
7e585d16
ZW
2707
2708 if (TREE_TYPE (ref) == error_mark_node)
2709 return error_mark_node;
2710
339a28b9 2711 if (TREE_DEPRECATED (ref))
9b86d6bb 2712 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2713
ad960f56 2714 /* Recursive call does not count as usage. */
b8698a0f 2715 if (ref != current_function_decl)
ad960f56 2716 {
ad960f56
MLI
2717 TREE_USED (ref) = 1;
2718 }
7e585d16 2719
bc4b653b
JM
2720 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2721 {
2722 if (!in_sizeof && !in_typeof)
2723 C_DECL_USED (ref) = 1;
2724 else if (DECL_INITIAL (ref) == 0
2725 && DECL_EXTERNAL (ref)
2726 && !TREE_PUBLIC (ref))
2727 record_maybe_used_decl (ref);
2728 }
2729
7e585d16
ZW
2730 if (TREE_CODE (ref) == CONST_DECL)
2731 {
6193b8b7 2732 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2733
2734 if (warn_cxx_compat
2735 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2736 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2737 {
2738 warning_at (loc, OPT_Wc___compat,
2739 ("enum constant defined in struct or union "
2740 "is not visible in C++"));
2741 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2742 }
2743
7e585d16
ZW
2744 ref = DECL_INITIAL (ref);
2745 TREE_CONSTANT (ref) = 1;
2746 }
6a29edea 2747 else if (current_function_decl != 0
4b1e44be 2748 && !DECL_FILE_SCOPE_P (current_function_decl)
21b634ae
MP
2749 && (VAR_OR_FUNCTION_DECL_P (ref)
2750 || TREE_CODE (ref) == PARM_DECL))
6a29edea
EB
2751 {
2752 tree context = decl_function_context (ref);
2f6e4e97 2753
6a29edea
EB
2754 if (context != 0 && context != current_function_decl)
2755 DECL_NONLOCAL (ref) = 1;
2756 }
71113fcd
GK
2757 /* C99 6.7.4p3: An inline definition of a function with external
2758 linkage ... shall not contain a reference to an identifier with
2759 internal linkage. */
2760 else if (current_function_decl != 0
2761 && DECL_DECLARED_INLINE_P (current_function_decl)
2762 && DECL_EXTERNAL (current_function_decl)
2763 && VAR_OR_FUNCTION_DECL_P (ref)
0ae9bd27 2764 && (!VAR_P (ref) || TREE_STATIC (ref))
1033ffa8
JJ
2765 && ! TREE_PUBLIC (ref)
2766 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2767 record_inline_static (loc, current_function_decl, ref,
2768 csi_internal);
7e585d16
ZW
2769
2770 return ref;
2771}
2772
bc4b653b
JM
2773/* Record details of decls possibly used inside sizeof or typeof. */
2774struct maybe_used_decl
2775{
2776 /* The decl. */
2777 tree decl;
2778 /* The level seen at (in_sizeof + in_typeof). */
2779 int level;
2780 /* The next one at this level or above, or NULL. */
2781 struct maybe_used_decl *next;
2782};
2783
2784static struct maybe_used_decl *maybe_used_decls;
2785
2786/* Record that DECL, an undefined static function reference seen
2787 inside sizeof or typeof, might be used if the operand of sizeof is
2788 a VLA type or the operand of typeof is a variably modified
2789 type. */
2790
4e2fb7de 2791static void
bc4b653b
JM
2792record_maybe_used_decl (tree decl)
2793{
2794 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2795 t->decl = decl;
2796 t->level = in_sizeof + in_typeof;
2797 t->next = maybe_used_decls;
2798 maybe_used_decls = t;
2799}
2800
2801/* Pop the stack of decls possibly used inside sizeof or typeof. If
2802 USED is false, just discard them. If it is true, mark them used
2803 (if no longer inside sizeof or typeof) or move them to the next
2804 level up (if still inside sizeof or typeof). */
2805
2806void
2807pop_maybe_used (bool used)
2808{
2809 struct maybe_used_decl *p = maybe_used_decls;
2810 int cur_level = in_sizeof + in_typeof;
2811 while (p && p->level > cur_level)
2812 {
2813 if (used)
2814 {
2815 if (cur_level == 0)
2816 C_DECL_USED (p->decl) = 1;
2817 else
2818 p->level = cur_level;
2819 }
2820 p = p->next;
2821 }
2822 if (!used || cur_level == 0)
2823 maybe_used_decls = p;
2824}
2825
2826/* Return the result of sizeof applied to EXPR. */
2827
2828struct c_expr
c2255bc4 2829c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2830{
2831 struct c_expr ret;
ad97f4be
JM
2832 if (expr.value == error_mark_node)
2833 {
2834 ret.value = error_mark_node;
2835 ret.original_code = ERROR_MARK;
6866c6e8 2836 ret.original_type = NULL;
ad97f4be
JM
2837 pop_maybe_used (false);
2838 }
2839 else
2840 {
928c19bb 2841 bool expr_const_operands = true;
773ec47f
MP
2842
2843 if (TREE_CODE (expr.value) == PARM_DECL
2844 && C_ARRAY_PARAMETER (expr.value))
2845 {
2846 if (warning_at (loc, OPT_Wsizeof_array_argument,
2847 "%<sizeof%> on array function parameter %qE will "
2848 "return size of %qT", expr.value,
2849 expr.original_type))
2850 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2851 }
928c19bb
JM
2852 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2853 &expr_const_operands);
c2255bc4 2854 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2855 c_last_sizeof_arg = expr.value;
2856 ret.original_code = SIZEOF_EXPR;
6866c6e8 2857 ret.original_type = NULL;
928c19bb 2858 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2859 {
2860 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2861 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2862 folded_expr, ret.value);
2863 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2864 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2865 }
928c19bb 2866 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2867 }
bc4b653b
JM
2868 return ret;
2869}
2870
2871/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2872 name passed to sizeof (rather than the type itself). LOC is the
2873 location of the original expression. */
bc4b653b
JM
2874
2875struct c_expr
c2255bc4 2876c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2877{
2878 tree type;
2879 struct c_expr ret;
928c19bb
JM
2880 tree type_expr = NULL_TREE;
2881 bool type_expr_const = true;
2882 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2883 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2884 c_last_sizeof_arg = type;
2885 ret.original_code = SIZEOF_EXPR;
6866c6e8 2886 ret.original_type = NULL;
24070fcb
JM
2887 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2888 && c_vla_type_p (type))
928c19bb 2889 {
24070fcb
JM
2890 /* If the type is a [*] array, it is a VLA but is represented as
2891 having a size of zero. In such a case we must ensure that
2892 the result of sizeof does not get folded to a constant by
2893 c_fully_fold, because if the size is evaluated the result is
2894 not constant and so constraints on zero or negative size
2895 arrays must not be applied when this sizeof call is inside
2896 another array declarator. */
2897 if (!type_expr)
2898 type_expr = integer_zero_node;
928c19bb
JM
2899 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2900 type_expr, ret.value);
2901 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2902 }
16464cc1
VR
2903 pop_maybe_used (type != error_mark_node
2904 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2905 return ret;
2906}
2907
400fbf9f 2908/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2909 The function call is at LOC.
400fbf9f
JW
2910 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2911 TREE_VALUE of each node is a parameter-expression.
2912 FUNCTION's data type may be a function type or a pointer-to-function. */
2913
2914tree
c2255bc4 2915build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2916{
9771b263 2917 vec<tree, va_gc> *v;
bbbbb16a
ILT
2918 tree ret;
2919
9771b263 2920 vec_alloc (v, list_length (params));
bbbbb16a 2921 for (; params; params = TREE_CHAIN (params))
9771b263 2922 v->quick_push (TREE_VALUE (params));
8edbfaa6 2923 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2924 vec_free (v);
bbbbb16a
ILT
2925 return ret;
2926}
2927
ae52741c
MLI
2928/* Give a note about the location of the declaration of DECL. */
2929
c7b70a3c
MP
2930static void
2931inform_declaration (tree decl)
ae52741c 2932{
c7b70a3c 2933 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
ae52741c
MLI
2934 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2935}
2936
bbbbb16a
ILT
2937/* Build a function call to function FUNCTION with parameters PARAMS.
2938 ORIGTYPES, if not NULL, is a vector of types; each element is
2939 either NULL or the original type of the corresponding element in
2940 PARAMS. The original type may differ from TREE_TYPE of the
2941 parameter for enums. FUNCTION's data type may be a function type
2942 or pointer-to-function. This function changes the elements of
2943 PARAMS. */
2944
2945tree
81e5eca8
MP
2946build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2947 tree function, vec<tree, va_gc> *params,
9771b263 2948 vec<tree, va_gc> *origtypes)
400fbf9f 2949{
b3694847 2950 tree fntype, fundecl = 0;
4977bab6 2951 tree name = NULL_TREE, result;
c96f4f73 2952 tree tem;
94a0dd7b
SL
2953 int nargs;
2954 tree *argarray;
b8698a0f 2955
400fbf9f 2956
fc76e425 2957 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2958 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2959
2960 /* Convert anything with function type to a pointer-to-function. */
2961 if (TREE_CODE (function) == FUNCTION_DECL)
2962 {
2963 name = DECL_NAME (function);
0a35513e
AH
2964
2965 if (flag_tm)
2966 tm_malloc_replacement (function);
a5eadacc 2967 fundecl = function;
86951993
AM
2968 /* Atomic functions have type checking/casting already done. They are
2969 often rewritten and don't match the original parameter list. */
2970 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2971 origtypes = NULL;
36536d79 2972
b72271b9 2973 if (flag_cilkplus
36536d79
BI
2974 && is_cilkplus_reduce_builtin (function))
2975 origtypes = NULL;
400fbf9f 2976 }
f2a71bbc 2977 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2978 function = function_to_pointer_conversion (loc, function);
400fbf9f 2979
6e955430
ZL
2980 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2981 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2982 if (params && !params->is_empty ())
2983 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2984
928c19bb
JM
2985 function = c_fully_fold (function, false, NULL);
2986
400fbf9f
JW
2987 fntype = TREE_TYPE (function);
2988
2989 if (TREE_CODE (fntype) == ERROR_MARK)
2990 return error_mark_node;
2991
2992 if (!(TREE_CODE (fntype) == POINTER_TYPE
2993 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2994 {
ae52741c
MLI
2995 if (!flag_diagnostics_show_caret)
2996 error_at (loc,
2997 "called object %qE is not a function or function pointer",
2998 function);
2999 else if (DECL_P (function))
3000 {
3001 error_at (loc,
3002 "called object %qD is not a function or function pointer",
3003 function);
3004 inform_declaration (function);
3005 }
3006 else
3007 error_at (loc,
3008 "called object is not a function or function pointer");
400fbf9f
JW
3009 return error_mark_node;
3010 }
3011
5ce89b2e
JM
3012 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3013 current_function_returns_abnormally = 1;
3014
400fbf9f
JW
3015 /* fntype now gets the type of function pointed to. */
3016 fntype = TREE_TYPE (fntype);
3017
ab4194da
JM
3018 /* Convert the parameters to the types declared in the
3019 function prototype, or apply default promotions. */
3020
81e5eca8
MP
3021 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3022 origtypes, function, fundecl);
ab4194da
JM
3023 if (nargs < 0)
3024 return error_mark_node;
3025
c96f4f73 3026 /* Check that the function is called through a compatible prototype.
fa337f3a 3027 If it is not, warn. */
1043771b 3028 if (CONVERT_EXPR_P (function)
c96f4f73
EB
3029 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3030 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 3031 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
3032 {
3033 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
3034
3035 /* This situation leads to run-time undefined behavior. We can't,
3036 therefore, simply error unless we can prove that all possible
3037 executions of the program must execute the code. */
fa337f3a 3038 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 3039
fa337f3a
RB
3040 if (VOID_TYPE_P (return_type)
3041 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3042 pedwarn (loc, 0,
3043 "function with qualified void return type called");
3044 }
c96f4f73 3045
9771b263 3046 argarray = vec_safe_address (params);
bbbbb16a 3047
83322951
RG
3048 /* Check that arguments to builtin functions match the expectations. */
3049 if (fundecl
3050 && DECL_BUILT_IN (fundecl)
3051 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
79ce98bc
MP
3052 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3053 argarray))
83322951 3054 return error_mark_node;
400fbf9f 3055
83322951 3056 /* Check that the arguments to the function are valid. */
3342fd71 3057 check_function_arguments (loc, fntype, nargs, argarray);
400fbf9f 3058
928c19bb
JM
3059 if (name != NULL_TREE
3060 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 3061 {
928c19bb 3062 if (require_constant_value)
b8698a0f 3063 result =
db3927fb
AH
3064 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3065 function, nargs, argarray);
928c19bb 3066 else
db3927fb
AH
3067 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3068 function, nargs, argarray);
928c19bb
JM
3069 if (TREE_CODE (result) == NOP_EXPR
3070 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3071 STRIP_TYPE_NOPS (result);
bf730f15
RS
3072 }
3073 else
db3927fb
AH
3074 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3075 function, nargs, argarray);
b0b3afb2 3076
ab4c578f
MP
3077 /* In this improbable scenario, a nested function returns a VM type.
3078 Create a TARGET_EXPR so that the call always has a LHS, much as
3079 what the C++ FE does for functions returning non-PODs. */
3080 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3081 {
3082 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3083 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3084 NULL_TREE, NULL_TREE);
3085 }
3086
71653180 3087 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
3088 {
3089 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 3090 pedwarn (loc, 0,
3ce62965
JM
3091 "function with qualified void return type called");
3092 return result;
3093 }
4f2e1536 3094 return require_complete_type (loc, result);
400fbf9f 3095}
8edbfaa6
JJ
3096
3097/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3098
3099tree
3100c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3101 tree function, vec<tree, va_gc> *params,
3102 vec<tree, va_gc> *origtypes)
3103{
3104 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3105 STRIP_TYPE_NOPS (function);
3106
3107 /* Convert anything with function type to a pointer-to-function. */
3108 if (TREE_CODE (function) == FUNCTION_DECL)
3109 {
3110 /* Implement type-directed function overloading for builtins.
3111 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3112 handle all the type checking. The result is a complete expression
3113 that implements this function call. */
3114 tree tem = resolve_overloaded_builtin (loc, function, params);
3115 if (tem)
3116 return tem;
3117 }
3118 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3119}
400fbf9f 3120\f
bbbbb16a
ILT
3121/* Convert the argument expressions in the vector VALUES
3122 to the types in the list TYPELIST.
400fbf9f
JW
3123
3124 If TYPELIST is exhausted, or when an element has NULL as its type,
3125 perform the default conversions.
3126
bbbbb16a
ILT
3127 ORIGTYPES is the original types of the expressions in VALUES. This
3128 holds the type of enum values which have been converted to integral
3129 types. It may be NULL.
400fbf9f 3130
03dafa61
JM
3131 FUNCTION is a tree for the called function. It is used only for
3132 error messages, where it is formatted with %qE.
400fbf9f
JW
3133
3134 This is also where warnings about wrong number of args are generated.
3135
81e5eca8
MP
3136 ARG_LOC are locations of function arguments (if any).
3137
94a0dd7b 3138 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3139 than the length of VALUES in some error situations), or -1 on
3140 failure. */
94a0dd7b
SL
3141
3142static int
81e5eca8
MP
3143convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3144 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3145 tree function, tree fundecl)
400fbf9f 3146{
bbbbb16a
ILT
3147 tree typetail, val;
3148 unsigned int parmnum;
06302a02 3149 bool error_args = false;
b5d32c25 3150 const bool type_generic = fundecl
81e5eca8 3151 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3152 bool type_generic_remove_excess_precision = false;
03dafa61 3153 tree selector;
03dafa61 3154
2ac2f164
JM
3155 /* Change pointer to function to the function itself for
3156 diagnostics. */
03dafa61
JM
3157 if (TREE_CODE (function) == ADDR_EXPR
3158 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3159 function = TREE_OPERAND (function, 0);
03dafa61
JM
3160
3161 /* Handle an ObjC selector specially for diagnostics. */
3162 selector = objc_message_selector ();
400fbf9f 3163
8ce94e44
JM
3164 /* For type-generic built-in functions, determine whether excess
3165 precision should be removed (classification) or not
3166 (comparison). */
3167 if (type_generic
3168 && DECL_BUILT_IN (fundecl)
3169 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3170 {
3171 switch (DECL_FUNCTION_CODE (fundecl))
3172 {
3173 case BUILT_IN_ISFINITE:
3174 case BUILT_IN_ISINF:
3175 case BUILT_IN_ISINF_SIGN:
3176 case BUILT_IN_ISNAN:
3177 case BUILT_IN_ISNORMAL:
3178 case BUILT_IN_FPCLASSIFY:
3179 type_generic_remove_excess_precision = true;
3180 break;
3181
3182 default:
3183 type_generic_remove_excess_precision = false;
3184 break;
3185 }
3186 }
b72271b9 3187 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3188 return vec_safe_length (values);
8ce94e44 3189
400fbf9f 3190 /* Scan the given expressions and types, producing individual
bbbbb16a 3191 converted arguments. */
400fbf9f 3192
bbbbb16a 3193 for (typetail = typelist, parmnum = 0;
9771b263 3194 values && values->iterate (parmnum, &val);
bbbbb16a 3195 ++parmnum)
400fbf9f 3196 {
b3694847 3197 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3198 tree valtype = TREE_TYPE (val);
03dafa61
JM
3199 tree rname = function;
3200 int argnum = parmnum + 1;
4d3e6fae 3201 const char *invalid_func_diag;
8ce94e44 3202 bool excess_precision = false;
928c19bb 3203 bool npc;
bbbbb16a 3204 tree parmval;
5c1bc275
MP
3205 /* Some __atomic_* builtins have additional hidden argument at
3206 position 0. */
3207 location_t ploc
3208 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3209 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3210 : input_location;
400fbf9f
JW
3211
3212 if (type == void_type_node)
3213 {
19dc6d01 3214 if (selector)
68fca595 3215 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3216 else
68fca595 3217 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3218 inform_declaration (fundecl);
d38f7dce 3219 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3220 }
3221
03dafa61
JM
3222 if (selector && argnum > 2)
3223 {
3224 rname = selector;
3225 argnum -= 2;
3226 }
3227
928c19bb 3228 npc = null_pointer_constant_p (val);
8ce94e44
JM
3229
3230 /* If there is excess precision and a prototype, convert once to
3231 the required type rather than converting via the semantic
3232 type. Likewise without a prototype a float value represented
3233 as long double should be converted once to double. But for
3234 type-generic classification functions excess precision must
3235 be removed here. */
3236 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3237 && (type || !type_generic || !type_generic_remove_excess_precision))
3238 {
3239 val = TREE_OPERAND (val, 0);
3240 excess_precision = true;
3241 }
928c19bb 3242 val = c_fully_fold (val, false, NULL);
ed248cf7 3243 STRIP_TYPE_NOPS (val);
400fbf9f 3244
4f2e1536 3245 val = require_complete_type (ploc, val);
400fbf9f
JW
3246
3247 if (type != 0)
3248 {
3249 /* Formal parm type is specified by a function prototype. */
400fbf9f 3250
20913689 3251 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3252 {
5c1bc275
MP
3253 error_at (ploc, "type of formal parameter %d is incomplete",
3254 parmnum + 1);
400fbf9f
JW
3255 parmval = val;
3256 }
3257 else
3258 {
bbbbb16a
ILT
3259 tree origtype;
3260
d45cf215
RS
3261 /* Optionally warn about conversions that
3262 differ from the default conversions. */
05170031 3263 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3264 {
e3a64162 3265 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3266
aae43c5f 3267 if (INTEGRAL_TYPE_P (type)
8ce94e44 3268 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3269 warning_at (ploc, OPT_Wtraditional_conversion,
3270 "passing argument %d of %qE as integer rather "
3271 "than floating due to prototype",
3272 argnum, rname);
03829ad2 3273 if (INTEGRAL_TYPE_P (type)
8ce94e44 3274 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3275 warning_at (ploc, OPT_Wtraditional_conversion,
3276 "passing argument %d of %qE as integer rather "
3277 "than complex due to prototype",
3278 argnum, rname);
aae43c5f 3279 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3280 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3281 warning_at (ploc, OPT_Wtraditional_conversion,
3282 "passing argument %d of %qE as complex rather "
3283 "than floating due to prototype",
3284 argnum, rname);
400fbf9f 3285 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3286 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3287 warning_at (ploc, OPT_Wtraditional_conversion,
3288 "passing argument %d of %qE as floating rather "
3289 "than integer due to prototype",
3290 argnum, rname);
03829ad2 3291 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3292 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3293 warning_at (ploc, OPT_Wtraditional_conversion,
3294 "passing argument %d of %qE as complex rather "
3295 "than integer due to prototype",
3296 argnum, rname);
aae43c5f 3297 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3298 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3299 warning_at (ploc, OPT_Wtraditional_conversion,
3300 "passing argument %d of %qE as floating rather "
3301 "than complex due to prototype",
3302 argnum, rname);
aae43c5f
RK
3303 /* ??? At some point, messages should be written about
3304 conversions between complex types, but that's too messy
3305 to do now. */
d45cf215 3306 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3307 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3308 {
3309 /* Warn if any argument is passed as `float',
047de90b 3310 since without a prototype it would be `double'. */
9a8ce21f
JG
3311 if (formal_prec == TYPE_PRECISION (float_type_node)
3312 && type != dfloat32_type_node)
5c1bc275
MP
3313 warning_at (ploc, 0,
3314 "passing argument %d of %qE as %<float%> "
3315 "rather than %<double%> due to prototype",
3316 argnum, rname);
9a8ce21f
JG
3317
3318 /* Warn if mismatch between argument and prototype
3319 for decimal float types. Warn of conversions with
3320 binary float types and of precision narrowing due to
3321 prototype. */
8ce94e44 3322 else if (type != valtype
9a8ce21f
JG
3323 && (type == dfloat32_type_node
3324 || type == dfloat64_type_node
c22cacf3 3325 || type == dfloat128_type_node
8ce94e44
JM
3326 || valtype == dfloat32_type_node
3327 || valtype == dfloat64_type_node
3328 || valtype == dfloat128_type_node)
c22cacf3 3329 && (formal_prec
8ce94e44 3330 <= TYPE_PRECISION (valtype)
9a8ce21f 3331 || (type == dfloat128_type_node
8ce94e44 3332 && (valtype
c22cacf3 3333 != dfloat64_type_node
8ce94e44 3334 && (valtype
9a8ce21f
JG
3335 != dfloat32_type_node)))
3336 || (type == dfloat64_type_node
8ce94e44 3337 && (valtype
9a8ce21f 3338 != dfloat32_type_node))))
5c1bc275
MP
3339 warning_at (ploc, 0,
3340 "passing argument %d of %qE as %qT "
3341 "rather than %qT due to prototype",
3342 argnum, rname, type, valtype);
9a8ce21f 3343
d45cf215 3344 }
3ed56f8a
KG
3345 /* Detect integer changing in width or signedness.
3346 These warnings are only activated with
05170031
MLI
3347 -Wtraditional-conversion, not with -Wtraditional. */
3348 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3349 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3350 {
d45cf215
RS
3351 tree would_have_been = default_conversion (val);
3352 tree type1 = TREE_TYPE (would_have_been);
3353
754a4d82 3354 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3355 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3356 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3357 /* No warning if function asks for enum
3358 and the actual arg is that enum type. */
3359 ;
3360 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3361 warning_at (ploc, OPT_Wtraditional_conversion,
3362 "passing argument %d of %qE "
3363 "with different width due to prototype",
3364 argnum, rname);
8df83eae 3365 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3366 ;
800cd3b9
RS
3367 /* Don't complain if the formal parameter type
3368 is an enum, because we can't tell now whether
3369 the value was an enum--even the same enum. */
3370 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3371 ;
400fbf9f
JW
3372 else if (TREE_CODE (val) == INTEGER_CST
3373 && int_fits_type_p (val, type))
3374 /* Change in signedness doesn't matter
3375 if a constant value is unaffected. */
3376 ;
ce9895ae
RS
3377 /* If the value is extended from a narrower
3378 unsigned type, it doesn't matter whether we
3379 pass it as signed or unsigned; the value
3380 certainly is the same either way. */
8ce94e44
JM
3381 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3382 && TYPE_UNSIGNED (valtype))
ce9895ae 3383 ;
8df83eae 3384 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3385 warning_at (ploc, OPT_Wtraditional_conversion,
3386 "passing argument %d of %qE "
3387 "as unsigned due to prototype",
3388 argnum, rname);
3ed56f8a 3389 else
5c1bc275
MP
3390 warning_at (ploc, OPT_Wtraditional_conversion,
3391 "passing argument %d of %qE "
3392 "as signed due to prototype",
3393 argnum, rname);
400fbf9f
JW
3394 }
3395 }
3396
8ce94e44
JM
3397 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3398 sake of better warnings from convert_and_check. */
3399 if (excess_precision)
3400 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3401 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3402 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3403 val, origtype, ic_argpass,
3404 npc, fundecl, function,
2ac2f164 3405 parmnum + 1);
2f6e4e97 3406
61f71b34 3407 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3408 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3409 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3410 parmval = default_conversion (parmval);
400fbf9f 3411 }
400fbf9f 3412 }
8ce94e44
JM
3413 else if (TREE_CODE (valtype) == REAL_TYPE
3414 && (TYPE_PRECISION (valtype)
2531a1d9 3415 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3416 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3417 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3418 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3419 {
3420 if (type_generic)
bbbbb16a 3421 parmval = val;
b5d32c25 3422 else
0a0b3574
MM
3423 {
3424 /* Convert `float' to `double'. */
3425 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3426 warning_at (ploc, OPT_Wdouble_promotion,
3427 "implicit conversion from %qT to %qT when passing "
3428 "argument to function",
3429 valtype, double_type_node);
0a0b3574
MM
3430 parmval = convert (double_type_node, val);
3431 }
b5d32c25 3432 }
8ce94e44
JM
3433 else if (excess_precision && !type_generic)
3434 /* A "double" argument with excess precision being passed
3435 without a prototype or in variable arguments. */
bbbbb16a 3436 parmval = convert (valtype, val);
c22cacf3
MS
3437 else if ((invalid_func_diag =
3438 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3439 {
3440 error (invalid_func_diag);
94a0dd7b 3441 return -1;
4d3e6fae 3442 }
1807ffc1
MS
3443 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3444 {
3445 return -1;
3446 }
400fbf9f
JW
3447 else
3448 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3449 parmval = default_conversion (val);
3450
9771b263 3451 (*values)[parmnum] = parmval;
06302a02
JJ
3452 if (parmval == error_mark_node)
3453 error_args = true;
400fbf9f
JW
3454
3455 if (typetail)
3456 typetail = TREE_CHAIN (typetail);
3457 }
3458
9771b263 3459 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3460
400fbf9f 3461 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3462 {
68fca595 3463 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3464 inform_declaration (fundecl);
3465 return -1;
3789b316 3466 }
400fbf9f 3467
06302a02 3468 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3469}
3470\f
43f6dfd3
RS
3471/* This is the entry point used by the parser to build unary operators
3472 in the input. CODE, a tree_code, specifies the unary operator, and
3473 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3474 CONVERT_EXPR for code.
3475
3476 LOC is the location to use for the tree generated.
3477*/
43f6dfd3
RS
3478
3479struct c_expr
c2255bc4 3480parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3481{
3482 struct c_expr result;
3483
100d537d 3484 result.original_code = code;
6866c6e8
ILT
3485 result.original_type = NULL;
3486
1807ffc1
MS
3487 if (reject_gcc_builtin (arg.value))
3488 {
3489 result.value = error_mark_node;
3490 }
3491 else
3492 {
3493 result.value = build_unary_op (loc, code, arg.value, 0);
3494
e9892350
JG
3495 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3496 overflow_warning (loc, result.value);
1807ffc1 3497 }
59c0753d 3498
ebedc9a3
DM
3499 /* We are typically called when parsing a prefix token at LOC acting on
3500 ARG. Reflect this by updating the source range of the result to
3501 start at LOC and end at the end of ARG. */
3502 set_c_expr_source_range (&result,
3503 loc, arg.get_finish ());
3504
43f6dfd3
RS
3505 return result;
3506}
3507
3508/* This is the entry point used by the parser to build binary operators
3509 in the input. CODE, a tree_code, specifies the binary operator, and
3510 ARG1 and ARG2 are the operands. In addition to constructing the
3511 expression, we check for operands that were written with other binary
ba47d38d
AH
3512 operators in a way that is likely to confuse the user.
3513
3514 LOCATION is the location of the binary operator. */
edc7c4ec 3515
487a92fe 3516struct c_expr
ba47d38d
AH
3517parser_build_binary_op (location_t location, enum tree_code code,
3518 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3519{
487a92fe 3520 struct c_expr result;
400fbf9f 3521
487a92fe
JM
3522 enum tree_code code1 = arg1.original_code;
3523 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3524 tree type1 = (arg1.original_type
3525 ? arg1.original_type
3526 : TREE_TYPE (arg1.value));
3527 tree type2 = (arg2.original_type
3528 ? arg2.original_type
3529 : TREE_TYPE (arg2.value));
400fbf9f 3530
ba47d38d
AH
3531 result.value = build_binary_op (location, code,
3532 arg1.value, arg2.value, 1);
487a92fe 3533 result.original_code = code;
6866c6e8 3534 result.original_type = NULL;
58bf601b 3535
487a92fe 3536 if (TREE_CODE (result.value) == ERROR_MARK)
8a40fef3
DM
3537 {
3538 set_c_expr_source_range (&result,
3539 arg1.get_start (),
3540 arg2.get_finish ());
3541 return result;
3542 }
400fbf9f 3543
ba47d38d
AH
3544 if (location != UNKNOWN_LOCATION)
3545 protected_set_expr_location (result.value, location);
3546
ebedc9a3
DM
3547 set_c_expr_source_range (&result,
3548 arg1.get_start (),
3549 arg2.get_finish ());
3550
400fbf9f 3551 /* Check for cases such as x+y<<z which users are likely
487a92fe 3552 to misinterpret. */
400fbf9f 3553 if (warn_parentheses)
393e8e8b
MP
3554 warn_about_parentheses (location, code, code1, arg1.value, code2,
3555 arg2.value);
001af587 3556
ca409efd 3557 if (warn_logical_op)
393e8e8b 3558 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3559 code1, arg1.value, code2, arg2.value);
63a08740 3560
05b28fd6 3561 if (warn_tautological_compare)
433068cc
MP
3562 {
3563 tree lhs = arg1.value;
3564 tree rhs = arg2.value;
3565 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3566 {
3567 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3568 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3569 lhs = NULL_TREE;
3570 else
3571 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3572 }
3573 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3574 {
3575 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3576 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3577 rhs = NULL_TREE;
3578 else
3579 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3580 }
3581 if (lhs != NULL_TREE && rhs != NULL_TREE)
3582 warn_tautological_cmp (location, code, lhs, rhs);
3583 }
05b28fd6 3584
742938c9 3585 if (warn_logical_not_paren
7ccb1a11 3586 && TREE_CODE_CLASS (code) == tcc_comparison
47c2554f 3587 && code1 == TRUTH_NOT_EXPR
01177669
JJ
3588 && code2 != TRUTH_NOT_EXPR
3589 /* Avoid warning for !!x == y. */
3590 && (TREE_CODE (arg1.value) != NE_EXPR
3591 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3592 {
3593 /* Avoid warning for !b == y where b has _Bool type. */
3594 tree t = integer_zero_node;
3595 if (TREE_CODE (arg1.value) == EQ_EXPR
3596 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3597 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3598 {
3599 t = TREE_OPERAND (arg1.value, 0);
3600 do
3601 {
3602 if (TREE_TYPE (t) != integer_type_node)
3603 break;
3604 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3605 t = C_MAYBE_CONST_EXPR_EXPR (t);
3606 else if (CONVERT_EXPR_P (t))
3607 t = TREE_OPERAND (t, 0);
3608 else
3609 break;
3610 }
3611 while (1);
3612 }
3613 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3614 warn_logical_not_parentheses (location, code, arg2.value);
3615 }
742938c9 3616
e994a705
RS
3617 /* Warn about comparisons against string literals, with the exception
3618 of testing for equality or inequality of a string literal with NULL. */
3619 if (code == EQ_EXPR || code == NE_EXPR)
3620 {
66756373
JJ
3621 if ((code1 == STRING_CST
3622 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3623 || (code2 == STRING_CST
3624 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
c2255bc4
AH
3625 warning_at (location, OPT_Waddress,
3626 "comparison with string literal results in unspecified behavior");
e994a705
RS
3627 }
3628 else if (TREE_CODE_CLASS (code) == tcc_comparison
3629 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3630 warning_at (location, OPT_Waddress,
3631 "comparison with string literal results in unspecified behavior");
e994a705 3632
b8698a0f
L
3633 if (TREE_OVERFLOW_P (result.value)
3634 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3635 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3636 overflow_warning (location, result.value);
400fbf9f 3637
6866c6e8
ILT
3638 /* Warn about comparisons of different enum types. */
3639 if (warn_enum_compare
3640 && TREE_CODE_CLASS (code) == tcc_comparison
3641 && TREE_CODE (type1) == ENUMERAL_TYPE
3642 && TREE_CODE (type2) == ENUMERAL_TYPE
3643 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3644 warning_at (location, OPT_Wenum_compare,
3645 "comparison between %qT and %qT",
3646 type1, type2);
3647
400fbf9f
JW
3648 return result;
3649}
3e4093b6 3650\f
3e4093b6
RS
3651/* Return a tree for the difference of pointers OP0 and OP1.
3652 The resulting tree has type int. */
293c9fdd 3653
3e4093b6 3654static tree
db3927fb 3655pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3656{
3e4093b6 3657 tree restype = ptrdiff_type_node;
36c5e70a 3658 tree result, inttype;
400fbf9f 3659
36c5e70a
BE
3660 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3661 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3662 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3663 tree orig_op1 = op1;
400fbf9f 3664
36c5e70a
BE
3665 /* If the operands point into different address spaces, we need to
3666 explicitly convert them to pointers into the common address space
3667 before we can subtract the numerical address values. */
3668 if (as0 != as1)
3669 {
3670 addr_space_t as_common;
3671 tree common_type;
3672
3673 /* Determine the common superset address space. This is guaranteed
3674 to exist because the caller verified that comp_target_types
3675 returned non-zero. */
3676 if (!addr_space_superset (as0, as1, &as_common))
3677 gcc_unreachable ();
3678
3679 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3680 op0 = convert (common_type, op0);
3681 op1 = convert (common_type, op1);
3682 }
3683
3684 /* Determine integer type to perform computations in. This will usually
3685 be the same as the result type (ptrdiff_t), but may need to be a wider
3686 type if pointers for the address space are wider than ptrdiff_t. */
3687 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3688 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3689 else
3690 inttype = restype;
3691
fcf73884 3692 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3693 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3694 "pointer of type %<void *%> used in subtraction");
3695 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3696 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3697 "pointer to a function used in subtraction");
400fbf9f 3698
3e4093b6
RS
3699 /* First do the subtraction as integers;
3700 then drop through to build the divide operator.
3701 Do not do default conversions on the minus operator
3702 in case restype is a short type. */
400fbf9f 3703
db3927fb 3704 op0 = build_binary_op (loc,
36c5e70a
BE
3705 MINUS_EXPR, convert (inttype, op0),
3706 convert (inttype, op1), 0);
3e4093b6
RS
3707 /* This generates an error if op1 is pointer to incomplete type. */
3708 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3709 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3710
3e4093b6 3711 op1 = c_size_in_bytes (target_type);
400fbf9f 3712
f04dda30
MP
3713 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3714 error_at (loc, "arithmetic on pointer to an empty aggregate");
3715
3e4093b6 3716 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3717 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3718 op0, convert (inttype, op1));
3719
3720 /* Convert to final result type if necessary. */
3721 return convert (restype, result);
3e4093b6
RS
3722}
3723\f
2fe0a208
MP
3724/* Expand atomic compound assignments into an appropriate sequence as
3725 specified by the C11 standard section 6.5.16.2.
3726
267bac10
JM
3727 _Atomic T1 E1
3728 T2 E2
3729 E1 op= E2
3730
3731 This sequence is used for all types for which these operations are
3732 supported.
3733
3734 In addition, built-in versions of the 'fe' prefixed routines may
3735 need to be invoked for floating point (real, complex or vector) when
3736 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3737
3738 T1 newval;
3739 T1 old;
3740 T1 *addr
3741 T2 val
3742 fenv_t fenv
3743
3744 addr = &E1;
3745 val = (E2);
3746 __atomic_load (addr, &old, SEQ_CST);
3747 feholdexcept (&fenv);
3748loop:
3749 newval = old op val;
3750 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3751 SEQ_CST))
3752 goto done;
3753 feclearexcept (FE_ALL_EXCEPT);
3754 goto loop:
3755done:
3756 feupdateenv (&fenv);
3757
2fe0a208
MP
3758 The compiler will issue the __atomic_fetch_* built-in when possible,
3759 otherwise it will generate the generic form of the atomic operations.
3760 This requires temp(s) and has their address taken. The atomic processing
3761 is smart enough to figure out when the size of an object can utilize
3762 a lock-free version, and convert the built-in call to the appropriate
3763 lock-free routine. The optimizers will then dispose of any temps that
3764 are no longer required, and lock-free implementations are utilized as
3765 long as there is target support for the required size.
267bac10
JM
3766
3767 If the operator is NOP_EXPR, then this is a simple assignment, and
3768 an __atomic_store is issued to perform the assignment rather than
2fe0a208 3769 the above loop. */
267bac10
JM
3770
3771/* Build an atomic assignment at LOC, expanding into the proper
3772 sequence to store LHS MODIFYCODE= RHS. Return a value representing
2fe0a208 3773 the result of the operation, unless RETURN_OLD_P, in which case
267bac10
JM
3774 return the old value of LHS (this is only for postincrement and
3775 postdecrement). */
2fe0a208 3776
267bac10
JM
3777static tree
3778build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3779 tree rhs, bool return_old_p)
3780{
3781 tree fndecl, func_call;
3782 vec<tree, va_gc> *params;
3783 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3784 tree old, old_addr;
3785 tree compound_stmt;
3786 tree stmt, goto_stmt;
3787 tree loop_label, loop_decl, done_label, done_decl;
3788
3789 tree lhs_type = TREE_TYPE (lhs);
3790 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3791 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3792 tree rhs_type = TREE_TYPE (rhs);
3793
3794 gcc_assert (TYPE_ATOMIC (lhs_type));
3795
3796 if (return_old_p)
3797 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3798
3799 /* Allocate enough vector items for a compare_exchange. */
3800 vec_alloc (params, 6);
3801
3802 /* Create a compound statement to hold the sequence of statements
3803 with a loop. */
3804 compound_stmt = c_begin_compound_stmt (false);
3805
3806 /* Fold the RHS if it hasn't already been folded. */
3807 if (modifycode != NOP_EXPR)
3808 rhs = c_fully_fold (rhs, false, NULL);
3809
3810 /* Remove the qualifiers for the rest of the expressions and create
3811 the VAL temp variable to hold the RHS. */
3812 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3813 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5c4abbb8 3814 val = create_tmp_var_raw (nonatomic_rhs_type);
267bac10 3815 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3816 TREE_NO_WARNING (val) = 1;
5c4abbb8
MP
3817 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3818 NULL_TREE);
267bac10
JM
3819 SET_EXPR_LOCATION (rhs, loc);
3820 add_stmt (rhs);
3821
3822 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3823 an atomic_store. */
3824 if (modifycode == NOP_EXPR)
3825 {
3826 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3827 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3828 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3829 params->quick_push (lhs_addr);
3830 params->quick_push (rhs);
3831 params->quick_push (seq_cst);
8edbfaa6 3832 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3833 add_stmt (func_call);
3834
3835 /* Finish the compound statement. */
3836 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3837
3838 /* VAL is the value which was stored, return a COMPOUND_STMT of
3839 the statement and that value. */
3840 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3841 }
3842
2fe0a208
MP
3843 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3844 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3845 isn't applicable for such builtins. ??? Do we want to handle enums? */
3846 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3847 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3848 {
3849 built_in_function fncode;
3850 switch (modifycode)
3851 {
3852 case PLUS_EXPR:
3853 case POINTER_PLUS_EXPR:
3854 fncode = (return_old_p
3855 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3856 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3857 break;
3858 case MINUS_EXPR:
3859 fncode = (return_old_p
3860 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3861 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3862 break;
3863 case BIT_AND_EXPR:
3864 fncode = (return_old_p
3865 ? BUILT_IN_ATOMIC_FETCH_AND_N
3866 : BUILT_IN_ATOMIC_AND_FETCH_N);
3867 break;
3868 case BIT_IOR_EXPR:
3869 fncode = (return_old_p
3870 ? BUILT_IN_ATOMIC_FETCH_OR_N
3871 : BUILT_IN_ATOMIC_OR_FETCH_N);
3872 break;
3873 case BIT_XOR_EXPR:
3874 fncode = (return_old_p
3875 ? BUILT_IN_ATOMIC_FETCH_XOR_N
3876 : BUILT_IN_ATOMIC_XOR_FETCH_N);
3877 break;
3878 default:
3879 goto cas_loop;
3880 }
3881
3882 /* We can only use "_1" through "_16" variants of the atomic fetch
3883 built-ins. */
3884 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
3885 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
3886 goto cas_loop;
3887
3888 /* If this is a pointer type, we need to multiply by the size of
3889 the pointer target type. */
3890 if (POINTER_TYPE_P (lhs_type))
3891 {
3892 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
3893 /* ??? This would introduce -Wdiscarded-qualifiers
3894 warning: __atomic_fetch_* expect volatile void *
3895 type as the first argument. (Assignments between
3896 atomic and non-atomic objects are OK.) */
3897 || TYPE_RESTRICT (lhs_type))
3898 goto cas_loop;
3899 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
3900 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
3901 convert (ptrdiff_type_node, rhs),
3902 convert (ptrdiff_type_node, sz));
3903 }
3904
3905 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
3906 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
3907 fndecl = builtin_decl_explicit (fncode);
3908 params->quick_push (lhs_addr);
3909 params->quick_push (rhs);
3910 params->quick_push (seq_cst);
3911 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3912
3913 newval = create_tmp_var_raw (nonatomic_lhs_type);
3914 TREE_ADDRESSABLE (newval) = 1;
3915 TREE_NO_WARNING (newval) = 1;
3916 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
3917 NULL_TREE, NULL_TREE);
3918 SET_EXPR_LOCATION (rhs, loc);
3919 add_stmt (rhs);
3920
3921 /* Finish the compound statement. */
3922 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3923
3924 /* NEWVAL 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, newval);
3927 }
3928
3929cas_loop:
267bac10 3930 /* Create the variables and labels required for the op= form. */
5c4abbb8 3931 old = create_tmp_var_raw (nonatomic_lhs_type);
267bac10 3932 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3933 TREE_ADDRESSABLE (old) = 1;
3934 TREE_NO_WARNING (old) = 1;
267bac10 3935
5c4abbb8 3936 newval = create_tmp_var_raw (nonatomic_lhs_type);
267bac10
JM
3937 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3938 TREE_ADDRESSABLE (newval) = 1;
2994fb91 3939 TREE_NO_WARNING (newval) = 1;
267bac10
JM
3940
3941 loop_decl = create_artificial_label (loc);
3942 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3943
3944 done_decl = create_artificial_label (loc);
3945 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3946
3947 /* __atomic_load (addr, &old, SEQ_CST). */
3948 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3949 params->quick_push (lhs_addr);
3950 params->quick_push (old_addr);
3951 params->quick_push (seq_cst);
8edbfaa6 3952 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5c4abbb8
MP
3953 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3954 NULL_TREE);
3955 add_stmt (old);
267bac10
JM
3956 params->truncate (0);
3957
3958 /* Create the expressions for floating-point environment
3959 manipulation, if required. */
3960 bool need_fenv = (flag_trapping_math
3961 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3962 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3963 if (need_fenv)
3964 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3965
3966 if (hold_call)
3967 add_stmt (hold_call);
3968
3969 /* loop: */
3970 add_stmt (loop_label);
3971
3972 /* newval = old + val; */
3973 rhs = build_binary_op (loc, modifycode, old, val, 1);
5c4abbb8 3974 rhs = c_fully_fold (rhs, false, NULL);
68fca595
MP
3975 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3976 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3977 NULL_TREE, 0);
3978 if (rhs != error_mark_node)
3979 {
5c4abbb8
MP
3980 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3981 NULL_TREE);
267bac10
JM
3982 SET_EXPR_LOCATION (rhs, loc);
3983 add_stmt (rhs);
3984 }
3985
3986 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3987 goto done; */
3988 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3989 params->quick_push (lhs_addr);
3990 params->quick_push (old_addr);
3991 params->quick_push (newval_addr);
3992 params->quick_push (integer_zero_node);
3993 params->quick_push (seq_cst);
3994 params->quick_push (seq_cst);
8edbfaa6 3995 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3996
3997 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3998 SET_EXPR_LOCATION (goto_stmt, loc);
3999
4000 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4001 SET_EXPR_LOCATION (stmt, loc);
4002 add_stmt (stmt);
5c4abbb8 4003
267bac10
JM
4004 if (clear_call)
4005 add_stmt (clear_call);
4006
4007 /* goto loop; */
4008 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4009 SET_EXPR_LOCATION (goto_stmt, loc);
4010 add_stmt (goto_stmt);
5c4abbb8 4011
267bac10
JM
4012 /* done: */
4013 add_stmt (done_label);
4014
4015 if (update_call)
4016 add_stmt (update_call);
4017
4018 /* Finish the compound statement. */
4019 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4020
4021 /* NEWVAL is the value that was successfully stored, return a
4022 COMPOUND_EXPR of the statement and the appropriate value. */
4023 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4024 return_old_p ? old : newval);
4025}
4026
3e4093b6
RS
4027/* Construct and perhaps optimize a tree representation
4028 for a unary operation. CODE, a tree_code, specifies the operation
4029 and XARG is the operand.
4030 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
4031 the default promotions (such as from short to int).
4032 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
4033 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
4034 arrays to pointers in C99.
4035
4036 LOCATION is the location of the operator. */
400fbf9f 4037
3e4093b6 4038tree
c9f9eb5d
AH
4039build_unary_op (location_t location,
4040 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
4041{
4042 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4043 tree arg = xarg;
4044 tree argtype = 0;
808d6eaa 4045 enum tree_code typecode;
3e4093b6 4046 tree val;
c9f9eb5d 4047 tree ret = error_mark_node;
8ce94e44 4048 tree eptype = NULL_TREE;
3e4093b6 4049 int noconvert = flag;
4de67c26 4050 const char *invalid_op_diag;
928c19bb
JM
4051 bool int_operands;
4052
4053 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
4054 if (int_operands)
4055 arg = remove_c_maybe_const_expr (arg);
400fbf9f 4056
808d6eaa 4057 if (code != ADDR_EXPR)
4f2e1536 4058 arg = require_complete_type (location, arg);
808d6eaa
JM
4059
4060 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
4061 if (typecode == ERROR_MARK)
4062 return error_mark_node;
4063 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4064 typecode = INTEGER_TYPE;
6c36d76b 4065
4de67c26
JM
4066 if ((invalid_op_diag
4067 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4068 {
c9f9eb5d 4069 error_at (location, invalid_op_diag);
4de67c26
JM
4070 return error_mark_node;
4071 }
4072
8ce94e44
JM
4073 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4074 {
4075 eptype = TREE_TYPE (arg);
4076 arg = TREE_OPERAND (arg, 0);
4077 }
4078
3e4093b6
RS
4079 switch (code)
4080 {
4081 case CONVERT_EXPR:
4082 /* This is used for unary plus, because a CONVERT_EXPR
4083 is enough to prevent anybody from looking inside for
4084 associativity, but won't generate any code. */
4085 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 4086 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 4087 || typecode == VECTOR_TYPE))
400fbf9f 4088 {
c9f9eb5d 4089 error_at (location, "wrong type argument to unary plus");
3e4093b6 4090 return error_mark_node;
400fbf9f 4091 }
3e4093b6
RS
4092 else if (!noconvert)
4093 arg = default_conversion (arg);
db3927fb 4094 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
4095 break;
4096
3e4093b6
RS
4097 case NEGATE_EXPR:
4098 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 4099 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
4100 || typecode == VECTOR_TYPE))
4101 {
c9f9eb5d 4102 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
4103 return error_mark_node;
4104 }
4105 else if (!noconvert)
4106 arg = default_conversion (arg);
400fbf9f
JW
4107 break;
4108
3e4093b6 4109 case BIT_NOT_EXPR:
462643f0
AP
4110 /* ~ works on integer types and non float vectors. */
4111 if (typecode == INTEGER_TYPE
4112 || (typecode == VECTOR_TYPE
4113 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 4114 {
3e4093b6
RS
4115 if (!noconvert)
4116 arg = default_conversion (arg);
03d5b1f5 4117 }
3e4093b6 4118 else if (typecode == COMPLEX_TYPE)
400fbf9f 4119 {
3e4093b6 4120 code = CONJ_EXPR;
c1771a20 4121 pedwarn (location, OPT_Wpedantic,
fcf73884 4122 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
4123 if (!noconvert)
4124 arg = default_conversion (arg);
4125 }
4126 else
4127 {
c9f9eb5d 4128 error_at (location, "wrong type argument to bit-complement");
3e4093b6 4129 return error_mark_node;
400fbf9f
JW
4130 }
4131 break;
4132
3e4093b6 4133 case ABS_EXPR:
11017cc7 4134 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 4135 {
c9f9eb5d 4136 error_at (location, "wrong type argument to abs");
3e4093b6 4137 return error_mark_node;
400fbf9f 4138 }
3e4093b6
RS
4139 else if (!noconvert)
4140 arg = default_conversion (arg);
400fbf9f
JW
4141 break;
4142
3e4093b6
RS
4143 case CONJ_EXPR:
4144 /* Conjugating a real value is a no-op, but allow it anyway. */
4145 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4146 || typecode == COMPLEX_TYPE))
400fbf9f 4147 {
c9f9eb5d 4148 error_at (location, "wrong type argument to conjugation");
3e4093b6 4149 return error_mark_node;
400fbf9f 4150 }
3e4093b6
RS
4151 else if (!noconvert)
4152 arg = default_conversion (arg);
400fbf9f
JW
4153 break;
4154
3e4093b6 4155 case TRUTH_NOT_EXPR:
ab22c1fa 4156 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 4157 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 4158 && typecode != COMPLEX_TYPE)
400fbf9f 4159 {
c9f9eb5d
AH
4160 error_at (location,
4161 "wrong type argument to unary exclamation mark");
3e4093b6 4162 return error_mark_node;
400fbf9f 4163 }
a27d595d
JM
4164 if (int_operands)
4165 {
4166 arg = c_objc_common_truthvalue_conversion (location, xarg);
4167 arg = remove_c_maybe_const_expr (arg);
4168 }
4169 else
4170 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 4171 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
4172 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4173 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4174 location = EXPR_LOCATION (ret);
c9f9eb5d 4175 goto return_build_unary_op;
3e4093b6 4176
3e4093b6 4177 case REALPART_EXPR:
3e4093b6 4178 case IMAGPART_EXPR:
fb52b50a
NF
4179 ret = build_real_imag_expr (location, code, arg);
4180 if (ret == error_mark_node)
4181 return error_mark_node;
8ce94e44
JM
4182 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4183 eptype = TREE_TYPE (eptype);
c9f9eb5d 4184 goto return_build_unary_op;
3e4093b6
RS
4185
4186 case PREINCREMENT_EXPR:
4187 case POSTINCREMENT_EXPR:
4188 case PREDECREMENT_EXPR:
4189 case POSTDECREMENT_EXPR:
3e4093b6 4190
928c19bb
JM
4191 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4192 {
4193 tree inner = build_unary_op (location, code,
4194 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4195 if (inner == error_mark_node)
4196 return error_mark_node;
4197 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4198 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4199 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4200 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4201 goto return_build_unary_op;
4202 }
4203
925e8657
NP
4204 /* Complain about anything that is not a true lvalue. In
4205 Objective-C, skip this check for property_refs. */
f90e8e2e 4206 if (!objc_is_property_ref (arg)
7bd11157
TT
4207 && !lvalue_or_else (location,
4208 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
4209 || code == POSTINCREMENT_EXPR)
4210 ? lv_increment
4211 : lv_decrement)))
928c19bb
JM
4212 return error_mark_node;
4213
09639a83
ILT
4214 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4215 {
4216 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4217 warning_at (location, OPT_Wc___compat,
4218 "increment of enumeration value is invalid in C++");
4219 else
4220 warning_at (location, OPT_Wc___compat,
4221 "decrement of enumeration value is invalid in C++");
4222 }
4223
928c19bb
JM
4224 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4225 arg = c_fully_fold (arg, false, NULL);
4226
267bac10
JM
4227 bool atomic_op;
4228 atomic_op = really_atomic_lvalue (arg);
4229
3e4093b6
RS
4230 /* Increment or decrement the real part of the value,
4231 and don't change the imaginary part. */
4232 if (typecode == COMPLEX_TYPE)
400fbf9f 4233 {
3e4093b6
RS
4234 tree real, imag;
4235
c1771a20 4236 pedwarn (location, OPT_Wpedantic,
509c9d60 4237 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 4238
267bac10
JM
4239 if (!atomic_op)
4240 {
4241 arg = stabilize_reference (arg);
4242 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4243 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4244 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4245 if (real == error_mark_node || imag == error_mark_node)
4246 return error_mark_node;
4247 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4248 real, imag);
4249 goto return_build_unary_op;
4250 }
400fbf9f 4251 }
3e4093b6
RS
4252
4253 /* Report invalid types. */
4254
ab22c1fa 4255 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4256 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4257 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4258 {
3e4093b6 4259 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4260 error_at (location, "wrong type argument to increment");
c22cacf3 4261 else
c9f9eb5d 4262 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4263
4264 return error_mark_node;
400fbf9f 4265 }
400fbf9f 4266
3e4093b6
RS
4267 {
4268 tree inc;
400fbf9f 4269
3e4093b6
RS
4270 argtype = TREE_TYPE (arg);
4271
4272 /* Compute the increment. */
4273
4274 if (typecode == POINTER_TYPE)
4275 {
a0e24419 4276 /* If pointer target is an incomplete type,
3e4093b6 4277 we just cannot know how to do the arithmetic. */
b70cef5d 4278 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4279 {
4280 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4281 error_at (location,
a0e24419
MP
4282 "increment of pointer to an incomplete type %qT",
4283 TREE_TYPE (argtype));
3e4093b6 4284 else
c9f9eb5d 4285 error_at (location,
a0e24419
MP
4286 "decrement of pointer to an incomplete type %qT",
4287 TREE_TYPE (argtype));
3e4093b6 4288 }
b70cef5d
JJ
4289 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4290 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4291 {
3e4093b6 4292 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4293 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4294 "wrong type argument to increment");
3e4093b6 4295 else
44d90fe1 4296 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4297 "wrong type argument to decrement");
3e4093b6
RS
4298 }
4299
b70cef5d 4300 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4301 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4302 }
b70cef5d 4303 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4304 {
4305 /* For signed fract types, we invert ++ to -- or
4306 -- to ++, and change inc from 1 to -1, because
4307 it is not possible to represent 1 in signed fract constants.
4308 For unsigned fract types, the result always overflows and
4309 we get an undefined (original) or the maximum value. */
4310 if (code == PREINCREMENT_EXPR)
4311 code = PREDECREMENT_EXPR;
4312 else if (code == PREDECREMENT_EXPR)
4313 code = PREINCREMENT_EXPR;
4314 else if (code == POSTINCREMENT_EXPR)
4315 code = POSTDECREMENT_EXPR;
4316 else /* code == POSTDECREMENT_EXPR */
4317 code = POSTINCREMENT_EXPR;
4318
4319 inc = integer_minus_one_node;
4320 inc = convert (argtype, inc);
4321 }
3e4093b6 4322 else
5be014d5 4323 {
241b71bb
TV
4324 inc = VECTOR_TYPE_P (argtype)
4325 ? build_one_cst (argtype)
4326 : integer_one_node;
5be014d5
AP
4327 inc = convert (argtype, inc);
4328 }
3e4093b6 4329
925e8657
NP
4330 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4331 need to ask Objective-C to build the increment or decrement
4332 expression for it. */
4333 if (objc_is_property_ref (arg))
f90e8e2e 4334 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4335 arg, inc);
4336
3e4093b6 4337 /* Report a read-only lvalue. */
f37acdf9 4338 if (TYPE_READONLY (argtype))
953ff289 4339 {
c02065fc 4340 readonly_error (location, arg,
953ff289
DN
4341 ((code == PREINCREMENT_EXPR
4342 || code == POSTINCREMENT_EXPR)
4343 ? lv_increment : lv_decrement));
4344 return error_mark_node;
4345 }
f37acdf9
JM
4346 else if (TREE_READONLY (arg))
4347 readonly_warning (arg,
4348 ((code == PREINCREMENT_EXPR
4349 || code == POSTINCREMENT_EXPR)
4350 ? lv_increment : lv_decrement));
3e4093b6 4351
267bac10
JM
4352 /* If the argument is atomic, use the special code sequences for
4353 atomic compound assignment. */
4354 if (atomic_op)
4355 {
4356 arg = stabilize_reference (arg);
4357 ret = build_atomic_assign (location, arg,
4358 ((code == PREINCREMENT_EXPR
4359 || code == POSTINCREMENT_EXPR)
4360 ? PLUS_EXPR
4361 : MINUS_EXPR),
4362 (FRACT_MODE_P (TYPE_MODE (argtype))
4363 ? inc
4364 : integer_one_node),
4365 (code == POSTINCREMENT_EXPR
4366 || code == POSTDECREMENT_EXPR));
4367 goto return_build_unary_op;
4368 }
4369
3e4093b6
RS
4370 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4371 val = boolean_increment (code, arg);
4372 else
53fb4de3 4373 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4374 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4375 if (TREE_CODE (val) != code)
6de9cd9a 4376 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4377 ret = val;
4378 goto return_build_unary_op;
3e4093b6
RS
4379 }
4380
4381 case ADDR_EXPR:
4382 /* Note that this operation never does default_conversion. */
4383
2b4b7036
JM
4384 /* The operand of unary '&' must be an lvalue (which excludes
4385 expressions of type void), or, in C99, the result of a [] or
4386 unary '*' operator. */
4387 if (VOID_TYPE_P (TREE_TYPE (arg))
4388 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
22d03525 4389 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
2b4b7036
JM
4390 pedwarn (location, 0, "taking address of expression of type %<void%>");
4391
3e4093b6 4392 /* Let &* cancel out to simplify resulting code. */
22d03525 4393 if (INDIRECT_REF_P (arg))
400fbf9f 4394 {
3e4093b6
RS
4395 /* Don't let this be an lvalue. */
4396 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4397 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4398 ret = TREE_OPERAND (arg, 0);
4399 goto return_build_unary_op;
400fbf9f 4400 }
1eb8759b 4401
3e4093b6
RS
4402 /* Anything not already handled and not a true memory reference
4403 or a non-lvalue array is an error. */
ee45a32d
EB
4404 if (typecode != FUNCTION_TYPE && !flag
4405 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4406 return error_mark_node;
b6a10c9f 4407
928c19bb
JM
4408 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4409 folding later. */
4410 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4411 {
4412 tree inner = build_unary_op (location, code,
4413 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4414 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4415 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4416 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4417 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4418 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4419 goto return_build_unary_op;
4420 }
4421
3e4093b6
RS
4422 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4423 argtype = TREE_TYPE (arg);
400fbf9f 4424
3e4093b6 4425 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4426 to which the address will point. This is only needed
f2c1da78 4427 for function types. */
6615c446 4428 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4429 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4430 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4431 {
4432 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4433 int quals = orig_quals;
4434
4435 if (TREE_READONLY (arg))
4436 quals |= TYPE_QUAL_CONST;
4437 if (TREE_THIS_VOLATILE (arg))
4438 quals |= TYPE_QUAL_VOLATILE;
4439
f2c1da78
JM
4440 argtype = c_build_qualified_type (argtype, quals);
4441 }
400fbf9f 4442
ee45a32d
EB
4443 switch (TREE_CODE (arg))
4444 {
4445 case COMPONENT_REF:
4446 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4447 {
949505a9
MP
4448 error_at (location, "cannot take address of bit-field %qD",
4449 TREE_OPERAND (arg, 1));
ee45a32d
EB
4450 return error_mark_node;
4451 }
4452
4453 /* ... fall through ... */
4454
4455 case ARRAY_REF:
4456 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4457 {
4458 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4459 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4460 {
949505a9
MP
4461 error_at (location, "cannot take address of scalar with "
4462 "reverse storage order");
ee45a32d
EB
4463 return error_mark_node;
4464 }
4465
4466 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4467 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
949505a9
MP
4468 warning_at (location, OPT_Wscalar_storage_order,
4469 "address of array with reverse scalar storage "
4470 "order requested");
ee45a32d
EB
4471 }
4472
4473 default:
4474 break;
4475 }
4476
3e4093b6
RS
4477 if (!c_mark_addressable (arg))
4478 return error_mark_node;
400fbf9f 4479
abb54d14
JM
4480 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4481 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4482
5cc200fc 4483 argtype = build_pointer_type (argtype);
5e55f99d
RH
4484
4485 /* ??? Cope with user tricks that amount to offsetof. Delete this
4486 when we have proper support for integer constant expressions. */
4487 val = get_base_address (arg);
22d03525 4488 if (val && INDIRECT_REF_P (val)
3aa2ddb8
JJ
4489 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4490 {
cf9e9959 4491 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4492 goto return_build_unary_op;
3aa2ddb8 4493 }
5e55f99d 4494
5cc200fc 4495 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4496
c9f9eb5d
AH
4497 ret = val;
4498 goto return_build_unary_op;
400fbf9f 4499
3e4093b6 4500 default:
1344f9a3 4501 gcc_unreachable ();
3e4093b6 4502 }
400fbf9f 4503
3e4093b6
RS
4504 if (argtype == 0)
4505 argtype = TREE_TYPE (arg);
928c19bb
JM
4506 if (TREE_CODE (arg) == INTEGER_CST)
4507 ret = (require_constant_value
db3927fb
AH
4508 ? fold_build1_initializer_loc (location, code, argtype, arg)
4509 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4510 else
4511 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4512 return_build_unary_op:
4513 gcc_assert (ret != error_mark_node);
928c19bb
JM
4514 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4515 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4516 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4517 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4518 ret = note_integer_operands (ret);
8ce94e44
JM
4519 if (eptype)
4520 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4521 protected_set_expr_location (ret, location);
4522 return ret;
3e4093b6 4523}
400fbf9f 4524
3e4093b6
RS
4525/* Return nonzero if REF is an lvalue valid for this language.
4526 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4527 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4528
1e4ae551 4529bool
58f9752a 4530lvalue_p (const_tree ref)
3e4093b6 4531{
58f9752a 4532 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4533
3e4093b6
RS
4534 switch (code)
4535 {
4536 case REALPART_EXPR:
4537 case IMAGPART_EXPR:
4538 case COMPONENT_REF:
4539 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4540
928c19bb
JM
4541 case C_MAYBE_CONST_EXPR:
4542 return lvalue_p (TREE_OPERAND (ref, 1));
4543
3e4093b6
RS
4544 case COMPOUND_LITERAL_EXPR:
4545 case STRING_CST:
4546 return 1;
400fbf9f 4547
3e4093b6
RS
4548 case INDIRECT_REF:
4549 case ARRAY_REF:
36536d79 4550 case ARRAY_NOTATION_REF:
3e4093b6
RS
4551 case VAR_DECL:
4552 case PARM_DECL:
4553 case RESULT_DECL:
4554 case ERROR_MARK:
4555 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4556 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4557
3e4093b6 4558 case BIND_EXPR:
3e4093b6 4559 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4560
3e4093b6
RS
4561 default:
4562 return 0;
4563 }
4564}
400fbf9f 4565\f
f37acdf9
JM
4566/* Give a warning for storing in something that is read-only in GCC
4567 terms but not const in ISO C terms. */
4568
4569static void
4570readonly_warning (tree arg, enum lvalue_use use)
4571{
4572 switch (use)
4573 {
4574 case lv_assign:
4575 warning (0, "assignment of read-only location %qE", arg);
4576 break;
4577 case lv_increment:
4578 warning (0, "increment of read-only location %qE", arg);
4579 break;
4580 case lv_decrement:
4581 warning (0, "decrement of read-only location %qE", arg);
4582 break;
4583 default:
4584 gcc_unreachable ();
4585 }
4586 return;
4587}
4588
37dc0d8d
JM
4589
4590/* Return nonzero if REF is an lvalue valid for this language;
4591 otherwise, print an error message and return zero. USE says
7bd11157
TT
4592 how the lvalue is being used and so selects the error message.
4593 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4594
4595static int
7bd11157 4596lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4597{
4598 int win = lvalue_p (ref);
4599
4600 if (!win)
7bd11157 4601 lvalue_error (loc, use);
37dc0d8d
JM
4602
4603 return win;
4604}
3e4093b6
RS
4605\f
4606/* Mark EXP saying that we need to be able to take the
4607 address of it; it should not be allocated in a register.
4608 Returns true if successful. */
54c93c30 4609
3e4093b6
RS
4610bool
4611c_mark_addressable (tree exp)
400fbf9f 4612{
3e4093b6 4613 tree x = exp;
95602da1 4614
3e4093b6
RS
4615 while (1)
4616 switch (TREE_CODE (x))
4617 {
4618 case COMPONENT_REF:
3e4093b6
RS
4619 case ADDR_EXPR:
4620 case ARRAY_REF:
4621 case REALPART_EXPR:
4622 case IMAGPART_EXPR:
4623 x = TREE_OPERAND (x, 0);
4624 break;
95602da1 4625
3e4093b6
RS
4626 case COMPOUND_LITERAL_EXPR:
4627 case CONSTRUCTOR:
4628 TREE_ADDRESSABLE (x) = 1;
4629 return true;
95602da1 4630
3e4093b6
RS
4631 case VAR_DECL:
4632 case CONST_DECL:
4633 case PARM_DECL:
4634 case RESULT_DECL:
5baeaac0 4635 if (C_DECL_REGISTER (x)
3e4093b6
RS
4636 && DECL_NONLOCAL (x))
4637 {
62f9079a 4638 if (TREE_PUBLIC (x) || is_global_var (x))
3e4093b6 4639 {
0039fa55
AN
4640 error
4641 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4642 return false;
4643 }
509c9d60 4644 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4645 }
5baeaac0 4646 else if (C_DECL_REGISTER (x))
3e4093b6 4647 {
62f9079a 4648 if (TREE_PUBLIC (x) || is_global_var (x))
0039fa55
AN
4649 error ("address of global register variable %qD requested", x);
4650 else
4651 error ("address of register variable %qD requested", x);
4652 return false;
3e4093b6 4653 }
400fbf9f 4654
3e4093b6
RS
4655 /* drops in */
4656 case FUNCTION_DECL:
4657 TREE_ADDRESSABLE (x) = 1;
4658 /* drops out */
4659 default:
4660 return true;
4661 }
4662}
4663\f
2d2e923f
MLI
4664/* Convert EXPR to TYPE, warning about conversion problems with
4665 constants. SEMANTIC_TYPE is the type this conversion would use
4666 without excess precision. If SEMANTIC_TYPE is NULL, this function
4667 is equivalent to convert_and_check. This function is a wrapper that
4668 handles conversions that may be different than
4669 the usual ones because of excess precision. */
4670
4671static tree
68fca595
MP
4672ep_convert_and_check (location_t loc, tree type, tree expr,
4673 tree semantic_type)
2d2e923f
MLI
4674{
4675 if (TREE_TYPE (expr) == type)
4676 return expr;
4677
4678 if (!semantic_type)
68fca595 4679 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4680
4681 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4682 && TREE_TYPE (expr) != semantic_type)
4683 {
4684 /* For integers, we need to check the real conversion, not
4685 the conversion to the excess precision type. */
68fca595 4686 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4687 }
4688 /* Result type is the excess precision type, which should be
4689 large enough, so do not check. */
4690 return convert (type, expr);
4691}
4692
928c19bb
JM
4693/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4694 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4695 if folded to an integer constant then the unselected half may
4696 contain arbitrary operations not normally permitted in constant
c2255bc4 4697 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4698
4699tree
744aa42f 4700build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4701 tree op1, tree op1_original_type, tree op2,
4702 tree op2_original_type)
400fbf9f 4703{
3e4093b6
RS
4704 tree type1;
4705 tree type2;
4706 enum tree_code code1;
4707 enum tree_code code2;
4708 tree result_type = NULL;
2d2e923f 4709 tree semantic_result_type = NULL;
3e4093b6 4710 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4711 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4712 bool ifexp_int_operands;
928c19bb 4713 tree ret;
400fbf9f 4714
4d84fe7c
JM
4715 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4716 if (op1_int_operands)
4717 op1 = remove_c_maybe_const_expr (op1);
4718 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4719 if (op2_int_operands)
4720 op2 = remove_c_maybe_const_expr (op2);
4721 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4722 if (ifexp_int_operands)
4723 ifexp = remove_c_maybe_const_expr (ifexp);
4724
3e4093b6
RS
4725 /* Promote both alternatives. */
4726
4727 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4728 op1 = default_conversion (op1);
4729 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4730 op2 = default_conversion (op2);
4731
4732 if (TREE_CODE (ifexp) == ERROR_MARK
4733 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4734 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4735 return error_mark_node;
400fbf9f 4736
3e4093b6
RS
4737 type1 = TREE_TYPE (op1);
4738 code1 = TREE_CODE (type1);
4739 type2 = TREE_TYPE (op2);
4740 code2 = TREE_CODE (type2);
4741
1807ffc1
MS
4742 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4743 return error_mark_node;
4744
4745 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4746 return error_mark_node;
4747
b1adf557
JM
4748 /* C90 does not permit non-lvalue arrays in conditional expressions.
4749 In C99 they will be pointers by now. */
4750 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4751 {
744aa42f 4752 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4753 return error_mark_node;
4754 }
4755
8ce94e44
JM
4756 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4757 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4758 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4759 || code1 == COMPLEX_TYPE)
4760 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4761 || code2 == COMPLEX_TYPE))
4762 {
2d2e923f 4763 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4764 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4765 {
4766 op1 = TREE_OPERAND (op1, 0);
4767 type1 = TREE_TYPE (op1);
4768 gcc_assert (TREE_CODE (type1) == code1);
4769 }
4770 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4771 {
4772 op2 = TREE_OPERAND (op2, 0);
4773 type2 = TREE_TYPE (op2);
4774 gcc_assert (TREE_CODE (type2) == code2);
4775 }
4776 }
4777
d130ae11
ILT
4778 if (warn_cxx_compat)
4779 {
4780 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4781 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4782
4783 if (TREE_CODE (t1) == ENUMERAL_TYPE
4784 && TREE_CODE (t2) == ENUMERAL_TYPE
4785 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4786 warning_at (colon_loc, OPT_Wc___compat,
4787 ("different enum types in conditional is "
4788 "invalid in C++: %qT vs %qT"),
4789 t1, t2);
4790 }
4791
3e4093b6
RS
4792 /* Quickly detect the usual case where op1 and op2 have the same type
4793 after promotion. */
4794 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4795 {
3e4093b6
RS
4796 if (type1 == type2)
4797 result_type = type1;
4798 else
4799 result_type = TYPE_MAIN_VARIANT (type1);
4800 }
4801 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4802 || code1 == COMPLEX_TYPE)
4803 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4804 || code2 == COMPLEX_TYPE))
3e4093b6 4805 {
ccf7f880 4806 result_type = c_common_type (type1, type2);
0a0b3574
MM
4807 do_warn_double_promotion (result_type, type1, type2,
4808 "implicit conversion from %qT to %qT to "
4809 "match other result of conditional",
4810 colon_loc);
400fbf9f 4811
3e4093b6
RS
4812 /* If -Wsign-compare, warn here if type1 and type2 have
4813 different signedness. We'll promote the signed to unsigned
4814 and later code won't know it used to be different.
4815 Do this check on the original types, so that explicit casts
4816 will be considered, but default promotions won't. */
7d882b83 4817 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4818 {
8df83eae
RK
4819 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4820 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4821
3e4093b6
RS
4822 if (unsigned_op1 ^ unsigned_op2)
4823 {
6ac01510
ILT
4824 bool ovf;
4825
3e4093b6
RS
4826 /* Do not warn if the result type is signed, since the
4827 signed type will only be chosen if it can represent
4828 all the values of the unsigned type. */
3f75a254 4829 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4830 /* OK */;
3e4093b6 4831 else
928c19bb
JM
4832 {
4833 bool op1_maybe_const = true;
4834 bool op2_maybe_const = true;
4835
4836 /* Do not warn if the signed quantity is an
4837 unsuffixed integer literal (or some static
4838 constant expression involving such literals) and
4839 it is non-negative. This warning requires the
4840 operands to be folded for best results, so do
4841 that folding in this case even without
4842 warn_sign_compare to avoid warning options
4843 possibly affecting code generation. */
f5178456
RS
4844 c_inhibit_evaluation_warnings
4845 += (ifexp == truthvalue_false_node);
928c19bb
JM
4846 op1 = c_fully_fold (op1, require_constant_value,
4847 &op1_maybe_const);
f5178456
RS
4848 c_inhibit_evaluation_warnings
4849 -= (ifexp == truthvalue_false_node);
4850
4851 c_inhibit_evaluation_warnings
4852 += (ifexp == truthvalue_true_node);
928c19bb
JM
4853 op2 = c_fully_fold (op2, require_constant_value,
4854 &op2_maybe_const);
f5178456
RS
4855 c_inhibit_evaluation_warnings
4856 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4857
4858 if (warn_sign_compare)
4859 {
4860 if ((unsigned_op2
4861 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4862 || (unsigned_op1
4863 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4864 /* OK */;
4865 else
744aa42f
ILT
4866 warning_at (colon_loc, OPT_Wsign_compare,
4867 ("signed and unsigned type in "
4868 "conditional expression"));
928c19bb
JM
4869 }
4870 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4871 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4872 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4873 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4874 }
3e4093b6
RS
4875 }
4876 }
4877 }
4878 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4879 {
fcf73884 4880 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4881 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4882 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4883 result_type = void_type_node;
4884 }
4885 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4886 {
36c5e70a
BE
4887 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4888 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4889 addr_space_t as_common;
4890
744aa42f 4891 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4892 result_type = common_pointer_type (type1, type2);
6aa3c60d 4893 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4894 result_type = type2;
6aa3c60d 4895 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4896 result_type = type1;
4897 else if (!addr_space_superset (as1, as2, &as_common))
4898 {
4899 error_at (colon_loc, "pointers to disjoint address spaces "
4900 "used in conditional expression");
4901 return error_mark_node;
4902 }
267bac10
JM
4903 else if (VOID_TYPE_P (TREE_TYPE (type1))
4904 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4905 {
768952be
MU
4906 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4907 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4908 & ~TYPE_QUALS (TREE_TYPE (type1))))
4909 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4910 "pointer to array loses qualifier "
4911 "in conditional expression");
4912
fcf73884 4913 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4914 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4915 "ISO C forbids conditional expr between "
bda67431 4916 "%<void *%> and function pointer");
3e4093b6
RS
4917 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4918 TREE_TYPE (type2)));
34a80643 4919 }
267bac10
JM
4920 else if (VOID_TYPE_P (TREE_TYPE (type2))
4921 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4922 {
768952be
MU
4923 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4924 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4925 & ~TYPE_QUALS (TREE_TYPE (type2))))
4926 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4927 "pointer to array loses qualifier "
4928 "in conditional expression");
4929
fcf73884 4930 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4931 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4932 "ISO C forbids conditional expr between "
bda67431 4933 "%<void *%> and function pointer");
3e4093b6
RS
4934 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4935 TREE_TYPE (type1)));
1c2a9b35 4936 }
b581b85b
NP
4937 /* Objective-C pointer comparisons are a bit more lenient. */
4938 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4939 result_type = objc_common_type (type1, type2);
34a80643 4940 else
ab87f8c8 4941 {
36c5e70a
BE
4942 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4943
b581b85b
NP
4944 pedwarn (colon_loc, 0,
4945 "pointer type mismatch in conditional expression");
36c5e70a
BE
4946 result_type = build_pointer_type
4947 (build_qualified_type (void_type_node, qual));
ab87f8c8 4948 }
3e4093b6
RS
4949 }
4950 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4951 {
6aa3c60d 4952 if (!null_pointer_constant_p (orig_op2))
744aa42f 4953 pedwarn (colon_loc, 0,
509c9d60 4954 "pointer/integer type mismatch in conditional expression");
3e4093b6 4955 else
ab87f8c8 4956 {
3e4093b6 4957 op2 = null_pointer_node;
ab87f8c8 4958 }
3e4093b6
RS
4959 result_type = type1;
4960 }
4961 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4962 {
6aa3c60d 4963 if (!null_pointer_constant_p (orig_op1))
744aa42f 4964 pedwarn (colon_loc, 0,
509c9d60 4965 "pointer/integer type mismatch in conditional expression");
3e4093b6 4966 else
ab87f8c8 4967 {
3e4093b6 4968 op1 = null_pointer_node;
ab87f8c8 4969 }
3e4093b6
RS
4970 result_type = type2;
4971 }
1c2a9b35 4972
3e4093b6
RS
4973 if (!result_type)
4974 {
4975 if (flag_cond_mismatch)
4976 result_type = void_type_node;
4977 else
400fbf9f 4978 {
c2255bc4 4979 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4980 return error_mark_node;
400fbf9f 4981 }
3e4093b6 4982 }
400fbf9f 4983
3e4093b6
RS
4984 /* Merge const and volatile flags of the incoming types. */
4985 result_type
4986 = build_type_variant (result_type,
afbd0665
AS
4987 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4988 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4989
68fca595
MP
4990 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4991 semantic_result_type);
4992 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4993 semantic_result_type);
b6a10c9f 4994
928c19bb
JM
4995 if (ifexp_bcp && ifexp == truthvalue_true_node)
4996 {
4997 op2_int_operands = true;
4998 op1 = c_fully_fold (op1, require_constant_value, NULL);
4999 }
5000 if (ifexp_bcp && ifexp == truthvalue_false_node)
5001 {
5002 op1_int_operands = true;
5003 op2 = c_fully_fold (op2, require_constant_value, NULL);
5004 }
4d84fe7c 5005 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
5006 && op1_int_operands
5007 && op2_int_operands);
5008 if (int_operands)
5009 {
5010 int_const = ((ifexp == truthvalue_true_node
5011 && TREE_CODE (orig_op1) == INTEGER_CST
5012 && !TREE_OVERFLOW (orig_op1))
5013 || (ifexp == truthvalue_false_node
5014 && TREE_CODE (orig_op2) == INTEGER_CST
5015 && !TREE_OVERFLOW (orig_op2)));
5016 }
9f47c7e5
IE
5017
5018 /* Need to convert condition operand into a vector mask. */
5019 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5020 {
5021 tree vectype = TREE_TYPE (ifexp);
5022 tree elem_type = TREE_TYPE (vectype);
5023 tree zero = build_int_cst (elem_type, 0);
5024 tree zero_vec = build_vector_from_val (vectype, zero);
5025 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5026 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5027 }
5028
928c19bb 5029 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 5030 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
5031 else
5032 {
01c7ccbb
JM
5033 if (int_operands)
5034 {
f34f1c87
MP
5035 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5036 nested inside of the expression. */
5037 op1 = c_fully_fold (op1, false, NULL);
5038 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 5039 }
928c19bb
JM
5040 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5041 if (int_operands)
5042 ret = note_integer_operands (ret);
5043 }
2d2e923f
MLI
5044 if (semantic_result_type)
5045 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 5046
c2255bc4 5047 protected_set_expr_location (ret, colon_loc);
928c19bb 5048 return ret;
3e4093b6
RS
5049}
5050\f
487a92fe 5051/* Return a compound expression that performs two expressions and
c2255bc4
AH
5052 returns the value of the second of them.
5053
5054 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 5055
3e4093b6 5056tree
c2255bc4 5057build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 5058{
4d84fe7c 5059 bool expr1_int_operands, expr2_int_operands;
8ce94e44 5060 tree eptype = NULL_TREE;
928c19bb
JM
5061 tree ret;
5062
b72271b9 5063 if (flag_cilkplus
939b37da
BI
5064 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5065 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5066 {
5067 error_at (loc,
5068 "spawned function call cannot be part of a comma expression");
5069 return error_mark_node;
5070 }
4d84fe7c
JM
5071 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5072 if (expr1_int_operands)
5073 expr1 = remove_c_maybe_const_expr (expr1);
5074 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5075 if (expr2_int_operands)
5076 expr2 = remove_c_maybe_const_expr (expr2);
5077
8ce94e44
JM
5078 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5079 expr1 = TREE_OPERAND (expr1, 0);
5080 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5081 {
5082 eptype = TREE_TYPE (expr2);
5083 expr2 = TREE_OPERAND (expr2, 0);
5084 }
5085
3f75a254 5086 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
5087 {
5088 /* The left-hand operand of a comma expression is like an expression
c5409249 5089 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 5090 any side-effects, unless it was explicitly cast to (void). */
e14a6540 5091 if (warn_unused_value)
47aecf47 5092 {
e14a6540 5093 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 5094 && CONVERT_EXPR_P (expr1))
47aecf47 5095 ; /* (void) a, b */
e14a6540
JM
5096 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5097 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 5098 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
5099 ; /* (void) a, (void) b, c */
5100 else
b8698a0f 5101 warning_at (loc, OPT_Wunused_value,
c2255bc4 5102 "left-hand operand of comma expression has no effect");
47aecf47 5103 }
3e4093b6 5104 }
789eadcd
MP
5105 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5106 && warn_unused_value)
5107 {
5108 tree r = expr1;
5109 location_t cloc = loc;
5110 while (TREE_CODE (r) == COMPOUND_EXPR)
5111 {
5112 if (EXPR_HAS_LOCATION (r))
5113 cloc = EXPR_LOCATION (r);
5114 r = TREE_OPERAND (r, 1);
5115 }
5116 if (!TREE_SIDE_EFFECTS (r)
5117 && !VOID_TYPE_P (TREE_TYPE (r))
5118 && !CONVERT_EXPR_P (r))
5119 warning_at (cloc, OPT_Wunused_value,
5120 "right-hand operand of comma expression has no effect");
5121 }
400fbf9f 5122
3e4093b6
RS
5123 /* With -Wunused, we should also warn if the left-hand operand does have
5124 side-effects, but computes a value which is not used. For example, in
5125 `foo() + bar(), baz()' the result of the `+' operator is not used,
5126 so we should issue a warning. */
5127 else if (warn_unused_value)
c2255bc4 5128 warn_if_unused_value (expr1, loc);
400fbf9f 5129
e63d6886
AP
5130 if (expr2 == error_mark_node)
5131 return error_mark_node;
5132
928c19bb
JM
5133 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5134
5135 if (flag_isoc99
4d84fe7c
JM
5136 && expr1_int_operands
5137 && expr2_int_operands)
928c19bb
JM
5138 ret = note_integer_operands (ret);
5139
8ce94e44
JM
5140 if (eptype)
5141 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5142
c2255bc4 5143 protected_set_expr_location (ret, loc);
928c19bb 5144 return ret;
3e4093b6 5145}
400fbf9f 5146
67165eb3
ILT
5147/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5148 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
5149 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5150 of the cast. -Wcast-qual appeared on the command line. Named
5151 address space qualifiers are not handled here, because they result
5152 in different warnings. */
67165eb3
ILT
5153
5154static void
2ee3cb35 5155handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
5156{
5157 tree in_type = type;
5158 tree in_otype = otype;
5159 int added = 0;
5160 int discarded = 0;
5161 bool is_const;
5162
5163 /* Check that the qualifiers on IN_TYPE are a superset of the
5164 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5165 nodes is uninteresting and we stop as soon as we hit a
5166 non-POINTER_TYPE node on either type. */
5167 do
5168 {
5169 in_otype = TREE_TYPE (in_otype);
5170 in_type = TREE_TYPE (in_type);
5171
5172 /* GNU C allows cv-qualified function types. 'const' means the
5173 function is very pure, 'volatile' means it can't return. We
5174 need to warn when such qualifiers are added, not when they're
5175 taken away. */
5176 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5177 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
5178 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5179 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 5180 else
36c5e70a
BE
5181 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5182 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
5183 }
5184 while (TREE_CODE (in_type) == POINTER_TYPE
5185 && TREE_CODE (in_otype) == POINTER_TYPE);
5186
5187 if (added)
2ee3cb35
MLI
5188 warning_at (loc, OPT_Wcast_qual,
5189 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
5190
5191 if (discarded)
5192 /* There are qualifiers present in IN_OTYPE that are not present
5193 in IN_TYPE. */
2ee3cb35 5194 warning_at (loc, OPT_Wcast_qual,
7485aeea 5195 "cast discards %qv qualifier from pointer target type",
2ee3cb35 5196 discarded);
67165eb3
ILT
5197
5198 if (added || discarded)
5199 return;
5200
5201 /* A cast from **T to const **T is unsafe, because it can cause a
5202 const value to be changed with no additional warning. We only
5203 issue this warning if T is the same on both sides, and we only
5204 issue the warning if there are the same number of pointers on
5205 both sides, as otherwise the cast is clearly unsafe anyhow. A
5206 cast is unsafe when a qualifier is added at one level and const
5207 is not present at all outer levels.
5208
5209 To issue this warning, we check at each level whether the cast
5210 adds new qualifiers not already seen. We don't need to special
5211 case function types, as they won't have the same
5212 TYPE_MAIN_VARIANT. */
5213
5214 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5215 return;
5216 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5217 return;
5218
5219 in_type = type;
5220 in_otype = otype;
5221 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5222 do
5223 {
5224 in_type = TREE_TYPE (in_type);
5225 in_otype = TREE_TYPE (in_otype);
5226 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5227 && !is_const)
5228 {
2ee3cb35
MLI
5229 warning_at (loc, OPT_Wcast_qual,
5230 "to be safe all intermediate pointers in cast from "
5231 "%qT to %qT must be %<const%> qualified",
5232 otype, type);
67165eb3
ILT
5233 break;
5234 }
5235 if (is_const)
5236 is_const = TYPE_READONLY (in_type);
5237 }
5238 while (TREE_CODE (in_type) == POINTER_TYPE);
5239}
5240
b8698a0f 5241/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 5242 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 5243
3e4093b6 5244tree
c2255bc4 5245build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 5246{
8ce94e44
JM
5247 tree value;
5248
5249 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5250 expr = TREE_OPERAND (expr, 0);
5251
5252 value = expr;
400fbf9f 5253
3e4093b6
RS
5254 if (type == error_mark_node || expr == error_mark_node)
5255 return error_mark_node;
400fbf9f 5256
3e4093b6
RS
5257 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5258 only in <protocol> qualifications. But when constructing cast expressions,
5259 the protocols do matter and must be kept around. */
700686fa
ZL
5260 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5261 return build1 (NOP_EXPR, type, expr);
5262
5263 type = TYPE_MAIN_VARIANT (type);
400fbf9f 5264
3e4093b6
RS
5265 if (TREE_CODE (type) == ARRAY_TYPE)
5266 {
c2255bc4 5267 error_at (loc, "cast specifies array type");
3e4093b6
RS
5268 return error_mark_node;
5269 }
400fbf9f 5270
3e4093b6
RS
5271 if (TREE_CODE (type) == FUNCTION_TYPE)
5272 {
c2255bc4 5273 error_at (loc, "cast specifies function type");
3e4093b6
RS
5274 return error_mark_node;
5275 }
400fbf9f 5276
808d6eaa
JM
5277 if (!VOID_TYPE_P (type))
5278 {
4f2e1536 5279 value = require_complete_type (loc, value);
808d6eaa
JM
5280 if (value == error_mark_node)
5281 return error_mark_node;
5282 }
5283
3e4093b6
RS
5284 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5285 {
3e636daf 5286 if (RECORD_OR_UNION_TYPE_P (type))
c1771a20 5287 pedwarn (loc, OPT_Wpedantic,
fcf73884 5288 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5289
5290 /* Convert to remove any qualifiers from VALUE's type. */
5291 value = convert (type, value);
3e4093b6
RS
5292 }
5293 else if (TREE_CODE (type) == UNION_TYPE)
5294 {
5295 tree field;
400fbf9f 5296
910ad8de 5297 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5298 if (TREE_TYPE (field) != error_mark_node
5299 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5300 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5301 break;
5302
5303 if (field)
400fbf9f 5304 {
3e4093b6 5305 tree t;
e616f54d 5306 bool maybe_const = true;
3e4093b6 5307
c1771a20 5308 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5309 t = c_fully_fold (value, false, &maybe_const);
5310 t = build_constructor_single (type, field, t);
5311 if (!maybe_const)
5312 t = c_wrap_maybe_const (t, true);
5313 t = digest_init (loc, type, t,
bbbbb16a 5314 NULL_TREE, false, true, 0);
3e4093b6
RS
5315 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5316 return t;
400fbf9f 5317 }
c2255bc4 5318 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5319 return error_mark_node;
5320 }
5321 else
5322 {
5323 tree otype, ovalue;
400fbf9f 5324
3e4093b6 5325 if (type == void_type_node)
c2255bc4
AH
5326 {
5327 tree t = build1 (CONVERT_EXPR, type, value);
5328 SET_EXPR_LOCATION (t, loc);
5329 return t;
5330 }
400fbf9f 5331
3e4093b6 5332 otype = TREE_TYPE (value);
400fbf9f 5333
3e4093b6 5334 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5335 if (warn_cast_qual
5336 && TREE_CODE (type) == POINTER_TYPE
5337 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5338 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5339
36c5e70a
BE
5340 /* Warn about conversions between pointers to disjoint
5341 address spaces. */
5342 if (TREE_CODE (type) == POINTER_TYPE
5343 && TREE_CODE (otype) == POINTER_TYPE
5344 && !null_pointer_constant_p (value))
5345 {
5346 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5347 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5348 addr_space_t as_common;
5349
5350 if (!addr_space_superset (as_to, as_from, &as_common))
5351 {
5352 if (ADDR_SPACE_GENERIC_P (as_from))
5353 warning_at (loc, 0, "cast to %s address space pointer "
5354 "from disjoint generic address space pointer",
5355 c_addr_space_name (as_to));
5356
5357 else if (ADDR_SPACE_GENERIC_P (as_to))
5358 warning_at (loc, 0, "cast to generic address space pointer "
5359 "from disjoint %s address space pointer",
5360 c_addr_space_name (as_from));
5361
5362 else
5363 warning_at (loc, 0, "cast to %s address space pointer "
5364 "from disjoint %s address space pointer",
5365 c_addr_space_name (as_to),
5366 c_addr_space_name (as_from));
5367 }
5368 }
5369
3e4093b6 5370 /* Warn about possible alignment problems. */
3176a0c2 5371 if (STRICT_ALIGNMENT
3e4093b6
RS
5372 && TREE_CODE (type) == POINTER_TYPE
5373 && TREE_CODE (otype) == POINTER_TYPE
5374 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5375 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5376 /* Don't warn about opaque types, where the actual alignment
5377 restriction is unknown. */
3e636daf 5378 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
3e4093b6
RS
5379 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5380 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5381 warning_at (loc, OPT_Wcast_align,
5382 "cast increases required alignment of target type");
e9a25f70 5383
3176a0c2 5384 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5385 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5386 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5387 /* Unlike conversion of integers to pointers, where the
5388 warning is disabled for converting constants because
5389 of cases such as SIG_*, warn about converting constant
5390 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5391 sign extension, and a warning is appropriate. */
c2255bc4
AH
5392 warning_at (loc, OPT_Wpointer_to_int_cast,
5393 "cast from pointer to integer of different size");
400fbf9f 5394
3176a0c2 5395 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5396 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5397 warning_at (loc, OPT_Wbad_function_cast,
5398 "cast from function call of type %qT "
5399 "to non-matching type %qT", otype, type);
400fbf9f 5400
3176a0c2 5401 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5402 && TREE_CODE (otype) == INTEGER_TYPE
5403 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5404 /* Don't warn about converting any constant. */
5405 && !TREE_CONSTANT (value))
c2255bc4
AH
5406 warning_at (loc,
5407 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5408 "of different size");
400fbf9f 5409
79bedddc
SR
5410 if (warn_strict_aliasing <= 2)
5411 strict_aliasing_warning (otype, type, expr);
400fbf9f 5412
3897f229
JM
5413 /* If pedantic, warn for conversions between function and object
5414 pointer types, except for converting a null pointer constant
5415 to function pointer type. */
5416 if (pedantic
5417 && TREE_CODE (type) == POINTER_TYPE
5418 && TREE_CODE (otype) == POINTER_TYPE
5419 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5420 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5421 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5422 "conversion of function pointer to object pointer type");
3897f229
JM
5423
5424 if (pedantic
5425 && TREE_CODE (type) == POINTER_TYPE
5426 && TREE_CODE (otype) == POINTER_TYPE
5427 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5428 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5429 && !null_pointer_constant_p (value))
c1771a20 5430 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5431 "conversion of object pointer to function pointer type");
3897f229 5432
3e4093b6 5433 ovalue = value;
3e4093b6 5434 value = convert (type, value);
400fbf9f 5435
3e4093b6 5436 /* Ignore any integer overflow caused by the cast. */
928c19bb 5437 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5438 {
8bcd6380 5439 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5440 {
8bcd6380
RS
5441 if (!TREE_OVERFLOW (value))
5442 {
5443 /* Avoid clobbering a shared constant. */
5444 value = copy_node (value);
5445 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5446 }
6414bad6 5447 }
8bcd6380 5448 else if (TREE_OVERFLOW (value))
d8e1f97b 5449 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5450 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5451 }
5452 }
400fbf9f 5453
53cd18ec 5454 /* Don't let a cast be an lvalue. */
9482b620 5455 if (lvalue_p (value))
db3927fb 5456 value = non_lvalue_loc (loc, value);
e9a25f70 5457
928c19bb
JM
5458 /* Don't allow the results of casting to floating-point or complex
5459 types be confused with actual constants, or casts involving
5460 integer and pointer types other than direct integer-to-integer
5461 and integer-to-pointer be confused with integer constant
5462 expressions and null pointer constants. */
5463 if (TREE_CODE (value) == REAL_CST
5464 || TREE_CODE (value) == COMPLEX_CST
5465 || (TREE_CODE (value) == INTEGER_CST
5466 && !((TREE_CODE (expr) == INTEGER_CST
5467 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5468 || TREE_CODE (expr) == REAL_CST
5469 || TREE_CODE (expr) == COMPLEX_CST)))
5470 value = build1 (NOP_EXPR, type, value);
5471
21ba0cea 5472 protected_set_expr_location (value, loc);
3e4093b6 5473 return value;
400fbf9f
JW
5474}
5475
c2255bc4
AH
5476/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5477 location of the open paren of the cast, or the position of the cast
5478 expr. */
3e4093b6 5479tree
c2255bc4 5480c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5481{
f8893e47 5482 tree type;
928c19bb
JM
5483 tree type_expr = NULL_TREE;
5484 bool type_expr_const = true;
5485 tree ret;
3e4093b6 5486 int saved_wsp = warn_strict_prototypes;
c5c76735 5487
3e4093b6
RS
5488 /* This avoids warnings about unprototyped casts on
5489 integers. E.g. "#define SIG_DFL (void(*)())0". */
5490 if (TREE_CODE (expr) == INTEGER_CST)
5491 warn_strict_prototypes = 0;
928c19bb 5492 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5493 warn_strict_prototypes = saved_wsp;
c5c76735 5494
1807ffc1
MS
5495 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5496 && reject_gcc_builtin (expr))
5497 return error_mark_node;
5498
c2255bc4 5499 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5500 if (type_expr)
5501 {
9f33203d
JM
5502 bool inner_expr_const = true;
5503 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5504 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5505 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5506 && inner_expr_const);
c2255bc4 5507 SET_EXPR_LOCATION (ret, loc);
928c19bb 5508 }
24b97832 5509
21ba0cea
MP
5510 if (!EXPR_HAS_LOCATION (ret))
5511 protected_set_expr_location (ret, loc);
24b97832 5512
9e5b2115
PB
5513 /* C++ does not permits types to be defined in a cast, but it
5514 allows references to incomplete types. */
5515 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5516 warning_at (loc, OPT_Wc___compat,
5517 "defining a type in a cast is invalid in C++");
5518
928c19bb 5519 return ret;
400fbf9f 5520}
3e4093b6
RS
5521\f
5522/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5523 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5524 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5525 MODIFYCODE is the code for a binary operator that we use
5526 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5527 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5528 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5529 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5530
c2255bc4
AH
5531 LOCATION is the location of the MODIFYCODE operator.
5532 RHS_LOC is the location of the RHS. */
2f6e4e97 5533
3e4093b6 5534tree
32e8bb8e 5535build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5536 enum tree_code modifycode,
c2255bc4 5537 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5538{
3e4093b6
RS
5539 tree result;
5540 tree newrhs;
241f845a 5541 tree rhseval = NULL_TREE;
8ce94e44 5542 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5543 tree lhstype = TREE_TYPE (lhs);
5544 tree olhstype = lhstype;
928c19bb 5545 bool npc;
267bac10 5546 bool is_atomic_op;
e9a25f70 5547
3e4093b6 5548 /* Types that aren't fully specified cannot be used in assignments. */
4f2e1536 5549 lhs = require_complete_type (location, lhs);
e9a25f70 5550
3e4093b6
RS
5551 /* Avoid duplicate error messages from operands that had errors. */
5552 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5553 return error_mark_node;
400fbf9f 5554
4c2ecab0
JM
5555 /* Ensure an error for assigning a non-lvalue array to an array in
5556 C90. */
5557 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5558 {
5559 error_at (location, "assignment to expression with array type");
5560 return error_mark_node;
5561 }
5562
46a88c12 5563 /* For ObjC properties, defer this check. */
7bd11157 5564 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5565 return error_mark_node;
5566
267bac10
JM
5567 is_atomic_op = really_atomic_lvalue (lhs);
5568
8ce94e44
JM
5569 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5570 {
5571 rhs_semantic_type = TREE_TYPE (rhs);
5572 rhs = TREE_OPERAND (rhs, 0);
5573 }
5574
3e4093b6 5575 newrhs = rhs;
400fbf9f 5576
928c19bb
JM
5577 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5578 {
5579 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5580 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5581 rhs_origtype);
928c19bb
JM
5582 if (inner == error_mark_node)
5583 return error_mark_node;
5584 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5585 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5586 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5587 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5588 protected_set_expr_location (result, location);
5589 return result;
5590 }
5591
3e4093b6
RS
5592 /* If a binary op has been requested, combine the old LHS value with the RHS
5593 producing the value we should actually store into the LHS. */
5594
5595 if (modifycode != NOP_EXPR)
400fbf9f 5596 {
928c19bb 5597 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5598 lhs = stabilize_reference (lhs);
bbbbb16a 5599
267bac10
JM
5600 /* Construct the RHS for any non-atomic compound assignemnt. */
5601 if (!is_atomic_op)
5602 {
241f845a
JJ
5603 /* If in LHS op= RHS the RHS has side-effects, ensure they
5604 are preevaluated before the rest of the assignment expression's
5605 side-effects, because RHS could contain e.g. function calls
5606 that modify LHS. */
5607 if (TREE_SIDE_EFFECTS (rhs))
5608 {
5609 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5610 rhseval = newrhs;
5611 }
267bac10 5612 newrhs = build_binary_op (location,
241f845a 5613 modifycode, lhs, newrhs, 1);
267bac10
JM
5614
5615 /* The original type of the right hand side is no longer
5616 meaningful. */
5617 rhs_origtype = NULL_TREE;
5618 }
400fbf9f 5619 }
400fbf9f 5620
668ea4b1
IS
5621 if (c_dialect_objc ())
5622 {
46a88c12
NP
5623 /* Check if we are modifying an Objective-C property reference;
5624 if so, we need to generate setter calls. */
5625 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5626 if (result)
241f845a 5627 goto return_result;
46a88c12
NP
5628
5629 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5630 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5631 return error_mark_node;
5632 }
5633
9bf24266 5634 /* Give an error for storing in something that is 'const'. */
bbbd6700 5635
f37acdf9 5636 if (TYPE_READONLY (lhstype)
3e636daf 5637 || (RECORD_OR_UNION_TYPE_P (lhstype)
3e4093b6 5638 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5639 {
c02065fc 5640 readonly_error (location, lhs, lv_assign);
953ff289
DN
5641 return error_mark_node;
5642 }
f37acdf9
JM
5643 else if (TREE_READONLY (lhs))
5644 readonly_warning (lhs, lv_assign);
bbbd6700 5645
3e4093b6
RS
5646 /* If storing into a structure or union member,
5647 it has probably been given type `int'.
5648 Compute the type that would go with
5649 the actual amount of storage the member occupies. */
bbbd6700 5650
3e4093b6
RS
5651 if (TREE_CODE (lhs) == COMPONENT_REF
5652 && (TREE_CODE (lhstype) == INTEGER_TYPE
5653 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5654 || TREE_CODE (lhstype) == REAL_TYPE
5655 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5656 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5657
3e4093b6
RS
5658 /* If storing in a field that is in actuality a short or narrower than one,
5659 we must store in the field in its actual type. */
5660
5661 if (lhstype != TREE_TYPE (lhs))
5662 {
5663 lhs = copy_node (lhs);
5664 TREE_TYPE (lhs) = lhstype;
400fbf9f 5665 }
400fbf9f 5666
32e8bb8e
ILT
5667 /* Issue -Wc++-compat warnings about an assignment to an enum type
5668 when LHS does not have its original type. This happens for,
5669 e.g., an enum bitfield in a struct. */
5670 if (warn_cxx_compat
5671 && lhs_origtype != NULL_TREE
5672 && lhs_origtype != lhstype
5673 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5674 {
5675 tree checktype = (rhs_origtype != NULL_TREE
5676 ? rhs_origtype
5677 : TREE_TYPE (rhs));
5678 if (checktype != error_mark_node
267bac10
JM
5679 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5680 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5681 warning_at (location, OPT_Wc___compat,
5682 "enum conversion in assignment is invalid in C++");
5683 }
5684
267bac10
JM
5685 /* If the lhs is atomic, remove that qualifier. */
5686 if (is_atomic_op)
5687 {
5688 lhstype = build_qualified_type (lhstype,
5689 (TYPE_QUALS (lhstype)
5690 & ~TYPE_QUAL_ATOMIC));
5691 olhstype = build_qualified_type (olhstype,
5692 (TYPE_QUALS (lhstype)
5693 & ~TYPE_QUAL_ATOMIC));
5694 }
5695
8ce94e44
JM
5696 /* Convert new value to destination type. Fold it first, then
5697 restore any excess precision information, for the sake of
5698 conversion warnings. */
400fbf9f 5699
267bac10
JM
5700 if (!(is_atomic_op && modifycode != NOP_EXPR))
5701 {
5702 npc = null_pointer_constant_p (newrhs);
5703 newrhs = c_fully_fold (newrhs, false, NULL);
5704 if (rhs_semantic_type)
5705 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5706 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5707 rhs_origtype, ic_assign, npc,
5708 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5709 if (TREE_CODE (newrhs) == ERROR_MARK)
5710 return error_mark_node;
5711 }
400fbf9f 5712
6e955430
ZL
5713 /* Emit ObjC write barrier, if necessary. */
5714 if (c_dialect_objc () && flag_objc_gc)
5715 {
5716 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5717 if (result)
c9f9eb5d
AH
5718 {
5719 protected_set_expr_location (result, location);
241f845a 5720 goto return_result;
c9f9eb5d 5721 }
6e955430
ZL
5722 }
5723
ea4b7848 5724 /* Scan operands. */
400fbf9f 5725
267bac10
JM
5726 if (is_atomic_op)
5727 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5728 else
5729 {
5730 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5731 TREE_SIDE_EFFECTS (result) = 1;
5732 protected_set_expr_location (result, location);
5733 }
400fbf9f 5734
3e4093b6
RS
5735 /* If we got the LHS in a different type for storing in,
5736 convert the result back to the nominal type of LHS
5737 so that the value we return always has the same type
5738 as the LHS argument. */
e855c5ce 5739
3e4093b6 5740 if (olhstype == TREE_TYPE (result))
241f845a 5741 goto return_result;
c9f9eb5d 5742
68fca595
MP
5743 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5744 rhs_origtype, ic_assign, false, NULL_TREE,
5745 NULL_TREE, 0);
c9f9eb5d 5746 protected_set_expr_location (result, location);
241f845a
JJ
5747
5748return_result:
5749 if (rhseval)
5750 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5751 return result;
3e4093b6
RS
5752}
5753\f
478a1c5b
ILT
5754/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5755 This is used to implement -fplan9-extensions. */
5756
5757static bool
5758find_anonymous_field_with_type (tree struct_type, tree type)
5759{
5760 tree field;
5761 bool found;
5762
3e636daf 5763 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
478a1c5b
ILT
5764 found = false;
5765 for (field = TYPE_FIELDS (struct_type);
5766 field != NULL_TREE;
5767 field = TREE_CHAIN (field))
5768 {
267bac10
JM
5769 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5770 ? c_build_qualified_type (TREE_TYPE (field),
5771 TYPE_QUAL_ATOMIC)
5772 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5773 if (DECL_NAME (field) == NULL
267bac10 5774 && comptypes (type, fieldtype))
478a1c5b
ILT
5775 {
5776 if (found)
5777 return false;
5778 found = true;
5779 }
5780 else if (DECL_NAME (field) == NULL
3e636daf 5781 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
478a1c5b
ILT
5782 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5783 {
5784 if (found)
5785 return false;
5786 found = true;
5787 }
5788 }
5789 return found;
5790}
5791
5792/* RHS is an expression whose type is pointer to struct. If there is
5793 an anonymous field in RHS with type TYPE, then return a pointer to
5794 that field in RHS. This is used with -fplan9-extensions. This
5795 returns NULL if no conversion could be found. */
5796
5797static tree
5798convert_to_anonymous_field (location_t location, tree type, tree rhs)
5799{
5800 tree rhs_struct_type, lhs_main_type;
5801 tree field, found_field;
5802 bool found_sub_field;
5803 tree ret;
5804
5805 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5806 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
3e636daf 5807 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
478a1c5b
ILT
5808
5809 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5810 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5811 ? c_build_qualified_type (TREE_TYPE (type),
5812 TYPE_QUAL_ATOMIC)
5813 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5814
5815 found_field = NULL_TREE;
5816 found_sub_field = false;
5817 for (field = TYPE_FIELDS (rhs_struct_type);
5818 field != NULL_TREE;
5819 field = TREE_CHAIN (field))
5820 {
5821 if (DECL_NAME (field) != NULL_TREE
3e636daf 5822 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
478a1c5b 5823 continue;
267bac10
JM
5824 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5825 ? c_build_qualified_type (TREE_TYPE (field),
5826 TYPE_QUAL_ATOMIC)
5827 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5828 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5829 {
5830 if (found_field != NULL_TREE)
5831 return NULL_TREE;
5832 found_field = field;
5833 }
5834 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5835 lhs_main_type))
5836 {
5837 if (found_field != NULL_TREE)
5838 return NULL_TREE;
5839 found_field = field;
5840 found_sub_field = true;
5841 }
5842 }
5843
5844 if (found_field == NULL_TREE)
5845 return NULL_TREE;
5846
5847 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5848 build_fold_indirect_ref (rhs), found_field,
5849 NULL_TREE);
5850 ret = build_fold_addr_expr_loc (location, ret);
5851
5852 if (found_sub_field)
5853 {
5854 ret = convert_to_anonymous_field (location, type, ret);
5855 gcc_assert (ret != NULL_TREE);
5856 }
5857
5858 return ret;
5859}
5860
63bc4e87
MP
5861/* Issue an error message for a bad initializer component.
5862 GMSGID identifies the message.
5863 The component name is taken from the spelling stack. */
5864
5865static void
ea58ef42 5866error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5867{
5868 char *ofwhat;
5869
5870 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5871 error_at (loc, gmsgid);
63bc4e87
MP
5872 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5873 if (*ofwhat)
d7ff7ae5 5874 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5875}
5876
5877/* Issue a pedantic warning for a bad initializer component. OPT is
5878 the option OPT_* (from options.h) controlling this warning or 0 if
5879 it is unconditionally given. GMSGID identifies the message. The
5880 component name is taken from the spelling stack. */
5881
5882static void
79063edd 5883pedwarn_init (location_t loc, int opt, const char *gmsgid)
63bc4e87
MP
5884{
5885 char *ofwhat;
d7ff7ae5 5886 bool warned;
63bc4e87 5887
79063edd
MS
5888 /* Use the location where a macro was expanded rather than where
5889 it was defined to make sure macros defined in system headers
5890 but used incorrectly elsewhere are diagnosed. */
5891 source_location exploc = expansion_point_location_if_in_system_header (loc);
5892
63bc4e87 5893 /* The gmsgid may be a format string with %< and %>. */
79063edd 5894 warned = pedwarn (exploc, opt, gmsgid);
63bc4e87 5895 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5 5896 if (*ofwhat && warned)
79063edd 5897 inform (exploc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5898}
5899
5900/* Issue a warning for a bad initializer component.
5901
5902 OPT is the OPT_W* value corresponding to the warning option that
5903 controls this warning. GMSGID identifies the message. The
5904 component name is taken from the spelling stack. */
5905
5906static void
5907warning_init (location_t loc, int opt, const char *gmsgid)
5908{
5909 char *ofwhat;
d7ff7ae5 5910 bool warned;
63bc4e87 5911
79063edd
MS
5912 /* Use the location where a macro was expanded rather than where
5913 it was defined to make sure macros defined in system headers
5914 but used incorrectly elsewhere are diagnosed. */
5915 source_location exploc = expansion_point_location_if_in_system_header (loc);
5916
63bc4e87 5917 /* The gmsgid may be a format string with %< and %>. */
79063edd 5918 warned = warning_at (exploc, opt, gmsgid);
63bc4e87 5919 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5 5920 if (*ofwhat && warned)
79063edd 5921 inform (exploc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5922}
5923\f
5924/* If TYPE is an array type and EXPR is a parenthesized string
5925 constant, warn if pedantic that EXPR is being used to initialize an
5926 object of type TYPE. */
5927
5928void
d033409e 5929maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
5930{
5931 if (pedantic
5932 && TREE_CODE (type) == ARRAY_TYPE
5933 && TREE_CODE (expr.value) == STRING_CST
5934 && expr.original_code != STRING_CST)
d033409e 5935 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
5936 "array initialized from parenthesized string constant");
5937}
5938
bbbbb16a
ILT
5939/* Convert value RHS to type TYPE as preparation for an assignment to
5940 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5941 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5942 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5943 constant before any folding.
3e4093b6
RS
5944 The real work of conversion is done by `convert'.
5945 The purpose of this function is to generate error messages
5946 for assignments that are not allowed in C.
2ac2f164
JM
5947 ERRTYPE says whether it is argument passing, assignment,
5948 initialization or return.
2f6e4e97 5949
aabef2de
MP
5950 In the following example, '~' denotes where EXPR_LOC and '^' where
5951 LOCATION point to:
5952
5953 f (var); [ic_argpass]
5954 ^ ~~~
5955 x = var; [ic_assign]
5956 ^ ~~~;
5957 int x = var; [ic_init]
5958 ^^^
5959 return x; [ic_return]
5960 ^
5961
2ac2f164 5962 FUNCTION is a tree for the function being called.
3e4093b6 5963 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5964
3e4093b6 5965static tree
81e5eca8 5966convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5967 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5968 bool null_pointer_constant, tree fundecl,
5969 tree function, int parmnum)
3e4093b6
RS
5970{
5971 enum tree_code codel = TREE_CODE (type);
8ce94e44 5972 tree orig_rhs = rhs;
3e4093b6
RS
5973 tree rhstype;
5974 enum tree_code coder;
2ac2f164 5975 tree rname = NULL_TREE;
58393038 5976 bool objc_ok = false;
2ac2f164 5977
1c7485af
MP
5978 /* Use the expansion point location to handle cases such as user's
5979 function returning a wrong-type macro defined in a system header. */
5980 location = expansion_point_location_if_in_system_header (location);
5981
6b4ef5c1 5982 if (errtype == ic_argpass)
2ac2f164
JM
5983 {
5984 tree selector;
5985 /* Change pointer to function to the function itself for
5986 diagnostics. */
5987 if (TREE_CODE (function) == ADDR_EXPR
5988 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5989 function = TREE_OPERAND (function, 0);
5990
5991 /* Handle an ObjC selector specially for diagnostics. */
5992 selector = objc_message_selector ();
5993 rname = function;
5994 if (selector && parmnum > 2)
5995 {
5996 rname = selector;
5997 parmnum -= 2;
5998 }
5999 }
6000
6001 /* This macro is used to emit diagnostics to ensure that all format
6002 strings are complete sentences, visible to gettext and checked at
6003 compile time. */
768952be 6004#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
6005 do { \
6006 switch (errtype) \
6007 { \
6008 case ic_argpass: \
5c1bc275 6009 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 6010 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 6011 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
6012 "expected %qT but argument is of type %qT", \
6013 type, rhstype); \
6014 break; \
1e053dfe
MLI
6015 case ic_assign: \
6016 pedwarn (LOCATION, OPT, AS); \
6017 break; \
6018 case ic_init: \
6a8f4e12 6019 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
6020 break; \
6021 case ic_return: \
d033409e 6022 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
6023 break; \
6024 default: \
6025 gcc_unreachable (); \
6026 } \
2ac2f164 6027 } while (0)
cb3ca04e 6028
49706e39
MLI
6029 /* This macro is used to emit diagnostics to ensure that all format
6030 strings are complete sentences, visible to gettext and checked at
768952be 6031 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 6032 extra parameter to enumerate qualifiers. */
768952be 6033#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
6034 do { \
6035 switch (errtype) \
6036 { \
6037 case ic_argpass: \
5c1bc275 6038 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 6039 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 6040 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
6041 "expected %qT but argument is of type %qT", \
6042 type, rhstype); \
6043 break; \
6044 case ic_assign: \
5c1bc275 6045 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
6046 break; \
6047 case ic_init: \
5c1bc275 6048 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
6049 break; \
6050 case ic_return: \
5c1bc275 6051 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
6052 break; \
6053 default: \
6054 gcc_unreachable (); \
6055 } \
6056 } while (0)
6057
768952be
MU
6058 /* This macro is used to emit diagnostics to ensure that all format
6059 strings are complete sentences, visible to gettext and checked at
6060 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6061 warning_at instead of pedwarn. */
6062#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6063 do { \
6064 switch (errtype) \
6065 { \
6066 case ic_argpass: \
6067 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6068 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6069 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6070 "expected %qT but argument is of type %qT", \
6071 type, rhstype); \
6072 break; \
6073 case ic_assign: \
6074 warning_at (LOCATION, OPT, AS, QUALS); \
6075 break; \
6076 case ic_init: \
6077 warning_at (LOCATION, OPT, IN, QUALS); \
6078 break; \
6079 case ic_return: \
6080 warning_at (LOCATION, OPT, RE, QUALS); \
6081 break; \
6082 default: \
6083 gcc_unreachable (); \
6084 } \
6085 } while (0)
6086
8ce94e44
JM
6087 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6088 rhs = TREE_OPERAND (rhs, 0);
6089
3e4093b6
RS
6090 rhstype = TREE_TYPE (rhs);
6091 coder = TREE_CODE (rhstype);
6092
6093 if (coder == ERROR_MARK)
6094 return error_mark_node;
6095
58393038
ZL
6096 if (c_dialect_objc ())
6097 {
6098 int parmno;
6099
6100 switch (errtype)
6101 {
6102 case ic_return:
6103 parmno = 0;
6104 break;
6105
6106 case ic_assign:
6107 parmno = -1;
6108 break;
6109
6110 case ic_init:
6111 parmno = -2;
6112 break;
6113
6114 default:
6115 parmno = parmnum;
6116 break;
6117 }
6118
6119 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6120 }
6121
bbbbb16a
ILT
6122 if (warn_cxx_compat)
6123 {
6124 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6125 if (checktype != error_mark_node
6126 && TREE_CODE (type) == ENUMERAL_TYPE
6127 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6128 {
768952be
MU
6129 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6130 G_("enum conversion when passing argument "
6131 "%d of %qE is invalid in C++"),
6132 G_("enum conversion in assignment is "
6133 "invalid in C++"),
6134 G_("enum conversion in initialization is "
6135 "invalid in C++"),
6136 G_("enum conversion in return is "
6137 "invalid in C++"));
bbbbb16a
ILT
6138 }
6139 }
6140
3e4093b6 6141 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 6142 return rhs;
3e4093b6
RS
6143
6144 if (coder == VOID_TYPE)
400fbf9f 6145 {
6dcc04b0
JM
6146 /* Except for passing an argument to an unprototyped function,
6147 this is a constraint violation. When passing an argument to
6148 an unprototyped function, it is compile-time undefined;
6149 making it a constraint in that case was rejected in
6150 DR#252. */
c2255bc4 6151 error_at (location, "void value not ignored as it ought to be");
3e4093b6 6152 return error_mark_node;
400fbf9f 6153 }
4f2e1536 6154 rhs = require_complete_type (location, rhs);
808d6eaa
JM
6155 if (rhs == error_mark_node)
6156 return error_mark_node;
1807ffc1
MS
6157
6158 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6159 return error_mark_node;
6160
cd192ccc
MS
6161 /* A non-reference type can convert to a reference. This handles
6162 va_start, va_copy and possibly port built-ins. */
6163 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 6164 {
3e4093b6 6165 if (!lvalue_p (rhs))
400fbf9f 6166 {
c2255bc4 6167 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 6168 return error_mark_node;
400fbf9f 6169 }
3e4093b6
RS
6170 if (!c_mark_addressable (rhs))
6171 return error_mark_node;
6172 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 6173 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6174
81e5eca8 6175 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
6176 build_pointer_type (TREE_TYPE (type)),
6177 rhs, origtype, errtype,
6178 null_pointer_constant, fundecl, function,
6179 parmnum);
cd192ccc
MS
6180 if (rhs == error_mark_node)
6181 return error_mark_node;
3e4093b6
RS
6182
6183 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 6184 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6185 return rhs;
400fbf9f 6186 }
3e4093b6 6187 /* Some types can interconvert without explicit casts. */
3274deff 6188 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 6189 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
6190 return convert (type, rhs);
6191 /* Arithmetic types all interconvert, and enum is treated like int. */
6192 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 6193 || codel == FIXED_POINT_TYPE
3e4093b6
RS
6194 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6195 || codel == BOOLEAN_TYPE)
6196 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 6197 || coder == FIXED_POINT_TYPE
3e4093b6
RS
6198 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6199 || coder == BOOLEAN_TYPE))
928c19bb
JM
6200 {
6201 tree ret;
6202 bool save = in_late_binary_op;
e5341100
JJ
6203 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6204 || (coder == REAL_TYPE
6205 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6206 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
928c19bb 6207 in_late_binary_op = true;
81e5eca8
MP
6208 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6209 ? expr_loc : location, type, orig_rhs);
e5341100 6210 in_late_binary_op = save;
928c19bb
JM
6211 return ret;
6212 }
400fbf9f 6213
79077aea
JJ
6214 /* Aggregates in different TUs might need conversion. */
6215 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6216 && codel == coder
6217 && comptypes (type, rhstype))
81e5eca8
MP
6218 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6219 ? expr_loc : location, type, rhs);
79077aea 6220
ebf0bf7f 6221 /* Conversion to a transparent union or record from its member types.
3e4093b6 6222 This applies only to function arguments. */
ebf0bf7f
JJ
6223 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6224 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 6225 && errtype == ic_argpass)
400fbf9f 6226 {
0257e383 6227 tree memb, marginal_memb = NULL_TREE;
3e4093b6 6228
910ad8de 6229 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 6230 {
0257e383 6231 tree memb_type = TREE_TYPE (memb);
400fbf9f 6232
3e4093b6 6233 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 6234 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 6235 break;
e58cd767 6236
3e4093b6
RS
6237 if (TREE_CODE (memb_type) != POINTER_TYPE)
6238 continue;
2f6e4e97 6239
3e4093b6
RS
6240 if (coder == POINTER_TYPE)
6241 {
6242 tree ttl = TREE_TYPE (memb_type);
6243 tree ttr = TREE_TYPE (rhstype);
400fbf9f 6244
3e4093b6
RS
6245 /* Any non-function converts to a [const][volatile] void *
6246 and vice versa; otherwise, targets must be the same.
6247 Meanwhile, the lhs target must have all the qualifiers of
6248 the rhs. */
267bac10
JM
6249 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6250 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6251 || comp_target_types (location, memb_type, rhstype))
3e4093b6 6252 {
267bac10
JM
6253 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6254 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 6255 /* If this type won't generate any warnings, use it. */
267bac10 6256 if (lquals == rquals
3e4093b6
RS
6257 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6258 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
6259 ? ((lquals | rquals) == rquals)
6260 : ((lquals | rquals) == lquals)))
3e4093b6 6261 break;
400fbf9f 6262
3e4093b6 6263 /* Keep looking for a better type, but remember this one. */
0257e383
RH
6264 if (!marginal_memb)
6265 marginal_memb = memb;
3e4093b6
RS
6266 }
6267 }
82bde854 6268
3e4093b6 6269 /* Can convert integer zero to any pointer type. */
928c19bb 6270 if (null_pointer_constant)
3e4093b6
RS
6271 {
6272 rhs = null_pointer_node;
6273 break;
6274 }
6275 }
400fbf9f 6276
0257e383 6277 if (memb || marginal_memb)
3e4093b6 6278 {
0257e383 6279 if (!memb)
3e4093b6
RS
6280 {
6281 /* We have only a marginally acceptable member type;
6282 it needs a warning. */
0257e383 6283 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 6284 tree ttr = TREE_TYPE (rhstype);
714a0864 6285
3e4093b6
RS
6286 /* Const and volatile mean something different for function
6287 types, so the usual warnings are not appropriate. */
6288 if (TREE_CODE (ttr) == FUNCTION_TYPE
6289 && TREE_CODE (ttl) == FUNCTION_TYPE)
6290 {
6291 /* Because const and volatile on functions are
6292 restrictions that say the function will not do
6293 certain things, it is okay to use a const or volatile
6294 function where an ordinary one is wanted, but not
6295 vice-versa. */
36c5e70a
BE
6296 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6297 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6298 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6299 OPT_Wdiscarded_qualifiers,
6300 G_("passing argument %d of %qE "
6301 "makes %q#v qualified function "
6302 "pointer from unqualified"),
6303 G_("assignment makes %q#v qualified "
6304 "function pointer from "
6305 "unqualified"),
6306 G_("initialization makes %q#v qualified "
6307 "function pointer from "
6308 "unqualified"),
6309 G_("return makes %q#v qualified function "
6310 "pointer from unqualified"),
6311 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6312 }
36c5e70a
BE
6313 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6314 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6315 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6316 OPT_Wdiscarded_qualifiers,
6317 G_("passing argument %d of %qE discards "
6318 "%qv qualifier from pointer target type"),
6319 G_("assignment discards %qv qualifier "
6320 "from pointer target type"),
6321 G_("initialization discards %qv qualifier "
6322 "from pointer target type"),
6323 G_("return discards %qv qualifier from "
6324 "pointer target type"),
6325 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6326
6327 memb = marginal_memb;
3e4093b6 6328 }
400fbf9f 6329
fcf73884 6330 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6331 pedwarn (location, OPT_Wpedantic,
fcf73884 6332 "ISO C prohibits argument conversion to union type");
0e7c47fa 6333
db3927fb 6334 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6335 return build_constructor_single (type, memb, rhs);
3e4093b6 6336 }
0e7c47fa
RK
6337 }
6338
3e4093b6
RS
6339 /* Conversions among pointers */
6340 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6341 && (coder == codel))
400fbf9f 6342 {
3e4093b6
RS
6343 tree ttl = TREE_TYPE (type);
6344 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6345 tree mvl = ttl;
6346 tree mvr = ttr;
3e4093b6 6347 bool is_opaque_pointer;
264fa2db 6348 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6349 addr_space_t asl;
6350 addr_space_t asr;
400fbf9f 6351
46df2823 6352 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6353 mvl = (TYPE_ATOMIC (mvl)
6354 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6355 TYPE_QUAL_ATOMIC)
6356 : TYPE_MAIN_VARIANT (mvl));
46df2823 6357 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6358 mvr = (TYPE_ATOMIC (mvr)
6359 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6360 TYPE_QUAL_ATOMIC)
6361 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6362 /* Opaque pointers are treated like void pointers. */
f83c7f63 6363 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6364
478a1c5b
ILT
6365 /* The Plan 9 compiler permits a pointer to a struct to be
6366 automatically converted into a pointer to an anonymous field
6367 within the struct. */
6368 if (flag_plan9_extensions
3e636daf
MP
6369 && RECORD_OR_UNION_TYPE_P (mvl)
6370 && RECORD_OR_UNION_TYPE_P (mvr)
478a1c5b
ILT
6371 && mvl != mvr)
6372 {
6373 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6374 if (new_rhs != NULL_TREE)
6375 {
6376 rhs = new_rhs;
6377 rhstype = TREE_TYPE (rhs);
6378 coder = TREE_CODE (rhstype);
6379 ttr = TREE_TYPE (rhstype);
6380 mvr = TYPE_MAIN_VARIANT (ttr);
6381 }
6382 }
6383
b7e20b53 6384 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6385 for the purpose of reducing the number of false positives, we
6386 tolerate the special case of
b7e20b53 6387
c22cacf3 6388 int *p = NULL;
b7e20b53 6389
c22cacf3 6390 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6391 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6392 warning_at (errtype == ic_argpass ? expr_loc : location,
6393 OPT_Wc___compat,
6394 "request for implicit conversion "
c2255bc4 6395 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6396
36c5e70a
BE
6397 /* See if the pointers point to incompatible address spaces. */
6398 asl = TYPE_ADDR_SPACE (ttl);
6399 asr = TYPE_ADDR_SPACE (ttr);
6400 if (!null_pointer_constant_p (rhs)
6401 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6402 {
6403 switch (errtype)
6404 {
6405 case ic_argpass:
8ffcdea8 6406 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6407 "non-enclosed address space", parmnum, rname);
6408 break;
6409 case ic_assign:
6410 error_at (location, "assignment from pointer to "
6411 "non-enclosed address space");
6412 break;
6413 case ic_init:
6414 error_at (location, "initialization from pointer to "
6415 "non-enclosed address space");
6416 break;
6417 case ic_return:
6418 error_at (location, "return from pointer to "
6419 "non-enclosed address space");
6420 break;
6421 default:
6422 gcc_unreachable ();
6423 }
6424 return error_mark_node;
6425 }
6426
7876a414
KG
6427 /* Check if the right-hand side has a format attribute but the
6428 left-hand side doesn't. */
90137d8f 6429 if (warn_suggest_attribute_format
104f8784 6430 && check_missing_format_attribute (type, rhstype))
c22cacf3 6431 {
104f8784
KG
6432 switch (errtype)
6433 {
6434 case ic_argpass:
8ffcdea8 6435 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6436 "argument %d of %qE might be "
6437 "a candidate for a format attribute",
6438 parmnum, rname);
104f8784
KG
6439 break;
6440 case ic_assign:
90137d8f 6441 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6442 "assignment left-hand side might be "
6443 "a candidate for a format attribute");
104f8784
KG
6444 break;
6445 case ic_init:
90137d8f 6446 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6447 "initialization left-hand side might be "
6448 "a candidate for a format attribute");
104f8784
KG
6449 break;
6450 case ic_return:
90137d8f 6451 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6452 "return type might be "
6453 "a candidate for a format attribute");
104f8784
KG
6454 break;
6455 default:
6456 gcc_unreachable ();
6457 }
7876a414 6458 }
c22cacf3 6459
3e4093b6
RS
6460 /* Any non-function converts to a [const][volatile] void *
6461 and vice versa; otherwise, targets must be the same.
6462 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6463 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6464 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6465 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6466 || is_opaque_pointer
f8a93a2e
JJ
6467 || ((c_common_unsigned_type (mvl)
6468 == c_common_unsigned_type (mvr))
267bac10
JM
6469 && (c_common_signed_type (mvl)
6470 == c_common_signed_type (mvr))
6471 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6472 {
768952be
MU
6473 /* Warn about loss of qualifers from pointers to arrays with
6474 qualifiers on the element type. */
6475 if (TREE_CODE (ttr) == ARRAY_TYPE)
6476 {
6477 ttr = strip_array_types (ttr);
6478 ttl = strip_array_types (ttl);
6479
6480 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6481 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6482 WARNING_FOR_QUALIFIERS (location, expr_loc,
6483 OPT_Wdiscarded_array_qualifiers,
6484 G_("passing argument %d of %qE discards "
6485 "%qv qualifier from pointer target type"),
6486 G_("assignment discards %qv qualifier "
6487 "from pointer target type"),
6488 G_("initialization discards %qv qualifier "
6489 "from pointer target type"),
6490 G_("return discards %qv qualifier from "
6491 "pointer target type"),
6492 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6493 }
6494 else if (pedantic
3e4093b6
RS
6495 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6496 ||
6497 (VOID_TYPE_P (ttr)
928c19bb 6498 && !null_pointer_constant
3e4093b6 6499 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6500 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6501 G_("ISO C forbids passing argument %d of "
6502 "%qE between function pointer "
6503 "and %<void *%>"),
6504 G_("ISO C forbids assignment between "
6505 "function pointer and %<void *%>"),
6506 G_("ISO C forbids initialization between "
6507 "function pointer and %<void *%>"),
6508 G_("ISO C forbids return between function "
6509 "pointer and %<void *%>"));
3e4093b6
RS
6510 /* Const and volatile mean something different for function types,
6511 so the usual warnings are not appropriate. */
6512 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6513 && TREE_CODE (ttl) != FUNCTION_TYPE)
6514 {
768952be
MU
6515 /* Don't warn about loss of qualifier for conversions from
6516 qualified void* to pointers to arrays with corresponding
6517 qualifier on the element type. */
6518 if (!pedantic)
6519 ttl = strip_array_types (ttl);
6520
267bac10
JM
6521 /* Assignments between atomic and non-atomic objects are OK. */
6522 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6523 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6524 {
768952be
MU
6525 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6526 OPT_Wdiscarded_qualifiers,
6527 G_("passing argument %d of %qE discards "
6528 "%qv qualifier from pointer target type"),
6529 G_("assignment discards %qv qualifier "
6530 "from pointer target type"),
6531 G_("initialization discards %qv qualifier "
6532 "from pointer target type"),
6533 G_("return discards %qv qualifier from "
6534 "pointer target type"),
6535 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6536 }
3e4093b6
RS
6537 /* If this is not a case of ignoring a mismatch in signedness,
6538 no warning. */
6539 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6540 || target_cmp)
3e4093b6
RS
6541 ;
6542 /* If there is a mismatch, do warn. */
f2fd3821 6543 else if (warn_pointer_sign)
768952be
MU
6544 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6545 G_("pointer targets in passing argument "
6546 "%d of %qE differ in signedness"),
6547 G_("pointer targets in assignment "
6548 "differ in signedness"),
6549 G_("pointer targets in initialization "
6550 "differ in signedness"),
6551 G_("pointer targets in return differ "
6552 "in signedness"));
3e4093b6
RS
6553 }
6554 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6555 && TREE_CODE (ttr) == FUNCTION_TYPE)
6556 {
6557 /* Because const and volatile on functions are restrictions
6558 that say the function will not do certain things,
6559 it is okay to use a const or volatile function
6560 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6561 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6562 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6563 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6564 OPT_Wdiscarded_qualifiers,
6565 G_("passing argument %d of %qE makes "
6566 "%q#v qualified function pointer "
6567 "from unqualified"),
6568 G_("assignment makes %q#v qualified function "
6569 "pointer from unqualified"),
6570 G_("initialization makes %q#v qualified "
6571 "function pointer from unqualified"),
6572 G_("return makes %q#v qualified function "
6573 "pointer from unqualified"),
6574 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6575 }
6576 }
6577 else
58393038
ZL
6578 /* Avoid warning about the volatile ObjC EH puts on decls. */
6579 if (!objc_ok)
768952be
MU
6580 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6581 OPT_Wincompatible_pointer_types,
6582 G_("passing argument %d of %qE from "
6583 "incompatible pointer type"),
6584 G_("assignment from incompatible pointer type"),
6585 G_("initialization from incompatible "
6586 "pointer type"),
6587 G_("return from incompatible pointer type"));
58393038 6588
3e4093b6
RS
6589 return convert (type, rhs);
6590 }
b494fd98
EB
6591 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6592 {
6dcc04b0
JM
6593 /* ??? This should not be an error when inlining calls to
6594 unprototyped functions. */
c2255bc4 6595 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6596 return error_mark_node;
6597 }
3e4093b6 6598 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6599 {
3e4093b6
RS
6600 /* An explicit constant 0 can convert to a pointer,
6601 or one that results from arithmetic, even including
6602 a cast to integer type. */
928c19bb 6603 if (!null_pointer_constant)
768952be
MU
6604 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6605 OPT_Wint_conversion,
6606 G_("passing argument %d of %qE makes "
6607 "pointer from integer without a cast"),
6608 G_("assignment makes pointer from integer "
6609 "without a cast"),
6610 G_("initialization makes pointer from "
6611 "integer without a cast"),
6612 G_("return makes pointer from integer "
6613 "without a cast"));
b3006337
EB
6614
6615 return convert (type, rhs);
400fbf9f 6616 }
3e4093b6 6617 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6618 {
768952be
MU
6619 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6620 OPT_Wint_conversion,
6621 G_("passing argument %d of %qE makes integer "
6622 "from pointer without a cast"),
6623 G_("assignment makes integer from pointer "
6624 "without a cast"),
6625 G_("initialization makes integer from pointer "
6626 "without a cast"),
6627 G_("return makes integer from pointer "
6628 "without a cast"));
3e4093b6
RS
6629 return convert (type, rhs);
6630 }
6631 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6632 {
6633 tree ret;
6634 bool save = in_late_binary_op;
6635 in_late_binary_op = true;
6636 ret = convert (type, rhs);
6637 in_late_binary_op = save;
6638 return ret;
6639 }
400fbf9f 6640
2ac2f164 6641 switch (errtype)
3e4093b6 6642 {
2ac2f164 6643 case ic_argpass:
8ffcdea8
MP
6644 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6645 rname);
a7da8b42 6646 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6647 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6648 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6649 break;
6650 case ic_assign:
c2255bc4
AH
6651 error_at (location, "incompatible types when assigning to type %qT from "
6652 "type %qT", type, rhstype);
2ac2f164
JM
6653 break;
6654 case ic_init:
c2255bc4 6655 error_at (location,
8ffcdea8 6656 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6657 type, rhstype);
2ac2f164
JM
6658 break;
6659 case ic_return:
c2255bc4 6660 error_at (location,
8ffcdea8 6661 "incompatible types when returning type %qT but %qT was "
c2255bc4 6662 "expected", rhstype, type);
2ac2f164
JM
6663 break;
6664 default:
6665 gcc_unreachable ();
400fbf9f 6666 }
53b01f59 6667
3e4093b6
RS
6668 return error_mark_node;
6669}
3e4093b6
RS
6670\f
6671/* If VALUE is a compound expr all of whose expressions are constant, then
6672 return its value. Otherwise, return error_mark_node.
15b732b2 6673
3e4093b6
RS
6674 This is for handling COMPOUND_EXPRs as initializer elements
6675 which is allowed with a warning when -pedantic is specified. */
15b732b2 6676
3e4093b6
RS
6677static tree
6678valid_compound_expr_initializer (tree value, tree endtype)
6679{
6680 if (TREE_CODE (value) == COMPOUND_EXPR)
6681 {
6682 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6683 == error_mark_node)
6684 return error_mark_node;
6685 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6686 endtype);
6687 }
116df786 6688 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6689 return error_mark_node;
6690 else
6691 return value;
15b732b2 6692}
400fbf9f 6693\f
3e4093b6
RS
6694/* Perform appropriate conversions on the initial value of a variable,
6695 store it in the declaration DECL,
6696 and print any error messages that are appropriate.
bbbbb16a 6697 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6698 If the init is invalid, store an ERROR_MARK.
6699
6700 INIT_LOC is the location of the initial value. */
400fbf9f 6701
3e4093b6 6702void
c2255bc4 6703store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6704{
3e4093b6 6705 tree value, type;
928c19bb 6706 bool npc = false;
400fbf9f 6707
3e4093b6 6708 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6709
3e4093b6
RS
6710 type = TREE_TYPE (decl);
6711 if (TREE_CODE (type) == ERROR_MARK)
6712 return;
400fbf9f 6713
3e4093b6 6714 /* Digest the specified initializer into an expression. */
400fbf9f 6715
928c19bb
JM
6716 if (init)
6717 npc = null_pointer_constant_p (init);
c2255bc4
AH
6718 value = digest_init (init_loc, type, init, origtype, npc,
6719 true, TREE_STATIC (decl));
400fbf9f 6720
3e4093b6 6721 /* Store the expression if valid; else report error. */
400fbf9f 6722
8400e75e 6723 if (!in_system_header_at (input_location)
3f75a254 6724 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6725 warning (OPT_Wtraditional, "traditional C rejects automatic "
6726 "aggregate initialization");
2f6e4e97 6727
dea63e49
JJ
6728 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6729 DECL_INITIAL (decl) = value;
400fbf9f 6730
3e4093b6
RS
6731 /* ANSI wants warnings about out-of-range constant initializers. */
6732 STRIP_TYPE_NOPS (value);
b8698a0f 6733 if (TREE_STATIC (decl))
c2658540 6734 constant_expression_warning (value);
400fbf9f 6735
3e4093b6
RS
6736 /* Check if we need to set array size from compound literal size. */
6737 if (TREE_CODE (type) == ARRAY_TYPE
6738 && TYPE_DOMAIN (type) == 0
6739 && value != error_mark_node)
400fbf9f 6740 {
3e4093b6
RS
6741 tree inside_init = init;
6742
ed248cf7 6743 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6744 inside_init = fold (inside_init);
6745
6746 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6747 {
8d9f82d5 6748 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6749
8d9f82d5 6750 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6751 {
6752 /* For int foo[] = (int [3]){1}; we need to set array size
6753 now since later on array initializer will be just the
6754 brace enclosed list of the compound literal. */
e30ecc5d 6755 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6756 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6757 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6758 layout_type (type);
8d9f82d5 6759 layout_decl (cldecl, 0);
e30ecc5d
JJ
6760 TREE_TYPE (decl)
6761 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6762 }
6763 }
400fbf9f 6764 }
3e4093b6
RS
6765}
6766\f
6767/* Methods for storing and printing names for error messages. */
400fbf9f 6768
3e4093b6
RS
6769/* Implement a spelling stack that allows components of a name to be pushed
6770 and popped. Each element on the stack is this structure. */
400fbf9f 6771
3e4093b6
RS
6772struct spelling
6773{
6774 int kind;
6775 union
400fbf9f 6776 {
a0f0ab9f 6777 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6778 const char *s;
6779 } u;
6780};
2f6e4e97 6781
3e4093b6
RS
6782#define SPELLING_STRING 1
6783#define SPELLING_MEMBER 2
6784#define SPELLING_BOUNDS 3
400fbf9f 6785
3e4093b6
RS
6786static struct spelling *spelling; /* Next stack element (unused). */
6787static struct spelling *spelling_base; /* Spelling stack base. */
6788static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6789
3e4093b6
RS
6790/* Macros to save and restore the spelling stack around push_... functions.
6791 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6792
3e4093b6
RS
6793#define SPELLING_DEPTH() (spelling - spelling_base)
6794#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6795
3e4093b6
RS
6796/* Push an element on the spelling stack with type KIND and assign VALUE
6797 to MEMBER. */
400fbf9f 6798
3e4093b6
RS
6799#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6800{ \
6801 int depth = SPELLING_DEPTH (); \
6802 \
6803 if (depth >= spelling_size) \
6804 { \
6805 spelling_size += 10; \
cca8ead2
BI
6806 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6807 spelling_size); \
3e4093b6
RS
6808 RESTORE_SPELLING_DEPTH (depth); \
6809 } \
6810 \
6811 spelling->kind = (KIND); \
6812 spelling->MEMBER = (VALUE); \
6813 spelling++; \
6814}
400fbf9f 6815
3e4093b6 6816/* Push STRING on the stack. Printed literally. */
400fbf9f 6817
3e4093b6
RS
6818static void
6819push_string (const char *string)
6820{
6821 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6822}
400fbf9f 6823
3e4093b6 6824/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6825
3e4093b6
RS
6826static void
6827push_member_name (tree decl)
6828{
6829 const char *const string
88388a52
JM
6830 = (DECL_NAME (decl)
6831 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6832 : _("<anonymous>"));
3e4093b6
RS
6833 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6834}
400fbf9f 6835
3e4093b6 6836/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6837
3e4093b6 6838static void
a0f0ab9f 6839push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6840{
6841 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6842}
bb58bec5 6843
3e4093b6 6844/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6845
3e4093b6
RS
6846static int
6847spelling_length (void)
6848{
6849 int size = 0;
6850 struct spelling *p;
400fbf9f 6851
3e4093b6
RS
6852 for (p = spelling_base; p < spelling; p++)
6853 {
6854 if (p->kind == SPELLING_BOUNDS)
6855 size += 25;
6856 else
6857 size += strlen (p->u.s) + 1;
6858 }
6859
6860 return size;
400fbf9f 6861}
400fbf9f 6862
3e4093b6 6863/* Print the spelling to BUFFER and return it. */
400fbf9f 6864
3e4093b6
RS
6865static char *
6866print_spelling (char *buffer)
400fbf9f 6867{
3e4093b6
RS
6868 char *d = buffer;
6869 struct spelling *p;
400fbf9f 6870
3e4093b6
RS
6871 for (p = spelling_base; p < spelling; p++)
6872 if (p->kind == SPELLING_BOUNDS)
6873 {
a0f0ab9f 6874 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6875 d += strlen (d);
6876 }
6877 else
6878 {
6879 const char *s;
6880 if (p->kind == SPELLING_MEMBER)
6881 *d++ = '.';
6882 for (s = p->u.s; (*d = *s++); d++)
6883 ;
6884 }
6885 *d++ = '\0';
6886 return buffer;
6887}
400fbf9f 6888
3e4093b6
RS
6889/* Digest the parser output INIT as an initializer for type TYPE.
6890 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6891
bbbbb16a
ILT
6892 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6893
928c19bb
JM
6894 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6895
916c5919
JM
6896 If INIT is a string constant, STRICT_STRING is true if it is
6897 unparenthesized or we should not warn here for it being parenthesized.
6898 For other types of INIT, STRICT_STRING is not used.
6899
c2255bc4
AH
6900 INIT_LOC is the location of the INIT.
6901
3e4093b6
RS
6902 REQUIRE_CONSTANT requests an error if non-constant initializers or
6903 elements are seen. */
7e842ef8 6904
3e4093b6 6905static tree
c2255bc4
AH
6906digest_init (location_t init_loc, tree type, tree init, tree origtype,
6907 bool null_pointer_constant, bool strict_string,
6908 int require_constant)
3e4093b6
RS
6909{
6910 enum tree_code code = TREE_CODE (type);
6911 tree inside_init = init;
8ce94e44 6912 tree semantic_type = NULL_TREE;
928c19bb 6913 bool maybe_const = true;
7e842ef8 6914
3e4093b6 6915 if (type == error_mark_node
f01da1a5 6916 || !init
7a0ca710 6917 || error_operand_p (init))
3e4093b6 6918 return error_mark_node;
7e842ef8 6919
ed248cf7 6920 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6921
8ce94e44
JM
6922 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6923 {
6924 semantic_type = TREE_TYPE (inside_init);
6925 inside_init = TREE_OPERAND (inside_init, 0);
6926 }
928c19bb
JM
6927 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6928 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6929
3e4093b6
RS
6930 /* Initialization of an array of chars from a string constant
6931 optionally enclosed in braces. */
7e842ef8 6932
197463ae
JM
6933 if (code == ARRAY_TYPE && inside_init
6934 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6935 {
267bac10
JM
6936 tree typ1
6937 = (TYPE_ATOMIC (TREE_TYPE (type))
6938 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6939 TYPE_QUAL_ATOMIC)
6940 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6941 /* Note that an array could be both an array of character type
6942 and an array of wchar_t if wchar_t is signed char or unsigned
6943 char. */
6944 bool char_array = (typ1 == char_type_node
6945 || typ1 == signed_char_type_node
6946 || typ1 == unsigned_char_type_node);
6947 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6948 bool char16_array = !!comptypes (typ1, char16_type_node);
6949 bool char32_array = !!comptypes (typ1, char32_type_node);
6950
6951 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6952 {
916c5919 6953 struct c_expr expr;
c466b2cd 6954 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6955 expr.value = inside_init;
6956 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6957 expr.original_type = NULL;
d033409e 6958 maybe_warn_string_init (init_loc, type, expr);
916c5919 6959
a45e580b 6960 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6961 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6962 "initialization of a flexible array member");
6963
3e4093b6 6964 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6965 TYPE_MAIN_VARIANT (type)))
3e4093b6 6966 return inside_init;
7e842ef8 6967
c466b2cd 6968 if (char_array)
3e4093b6 6969 {
c466b2cd
KVH
6970 if (typ2 != char_type_node)
6971 {
ea58ef42
MP
6972 error_init (init_loc, "char-array initialized from wide "
6973 "string");
c466b2cd
KVH
6974 return error_mark_node;
6975 }
3e4093b6 6976 }
c466b2cd 6977 else
3e4093b6 6978 {
c466b2cd
KVH
6979 if (typ2 == char_type_node)
6980 {
ea58ef42
MP
6981 error_init (init_loc, "wide character array initialized "
6982 "from non-wide string");
c466b2cd
KVH
6983 return error_mark_node;
6984 }
6985 else if (!comptypes(typ1, typ2))
6986 {
ea58ef42
MP
6987 error_init (init_loc, "wide character array initialized "
6988 "from incompatible wide string");
c466b2cd
KVH
6989 return error_mark_node;
6990 }
7e842ef8 6991 }
2f6e4e97 6992
3e4093b6
RS
6993 TREE_TYPE (inside_init) = type;
6994 if (TYPE_DOMAIN (type) != 0
6995 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6996 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6997 {
6998 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6999
c466b2cd 7000 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
7001 because it's ok to ignore the terminating null char
7002 that is counted in the length of the constant. */
5eb4df45
ILT
7003 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7004 (len
7005 - (TYPE_PRECISION (typ1)
7006 / BITS_PER_UNIT))))
7007 pedwarn_init (init_loc, 0,
7008 ("initializer-string for array of chars "
7009 "is too long"));
7010 else if (warn_cxx_compat
7011 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7012 warning_at (init_loc, OPT_Wc___compat,
7013 ("initializer-string for array chars "
7014 "is too long for C++"));
7015 }
7e842ef8 7016
3e4093b6 7017 return inside_init;
7e842ef8 7018 }
197463ae
JM
7019 else if (INTEGRAL_TYPE_P (typ1))
7020 {
ea58ef42 7021 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
7022 "from string constant");
7023 return error_mark_node;
7024 }
7e842ef8
PE
7025 }
7026
3e4093b6
RS
7027 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7028 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7029 below and handle as a constructor. */
e89be13b 7030 if (code == VECTOR_TYPE
31521951 7031 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
00c8e9f6 7032 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
7033 && TREE_CONSTANT (inside_init))
7034 {
7035 if (TREE_CODE (inside_init) == VECTOR_CST
7036 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7037 TYPE_MAIN_VARIANT (type)))
7038 return inside_init;
7039
7040 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7041 {
4038c495
GB
7042 unsigned HOST_WIDE_INT ix;
7043 tree value;
7044 bool constant_p = true;
e89be13b
JJ
7045
7046 /* Iterate through elements and check if all constructor
7047 elements are *_CSTs. */
4038c495
GB
7048 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7049 if (!CONSTANT_CLASS_P (value))
7050 {
7051 constant_p = false;
7052 break;
7053 }
e89be13b 7054
4038c495
GB
7055 if (constant_p)
7056 return build_vector_from_ctor (type,
7057 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
7058 }
7059 }
6035d635 7060
ca085fd7
MLI
7061 if (warn_sequence_point)
7062 verify_sequence_points (inside_init);
7063
3e4093b6
RS
7064 /* Any type can be initialized
7065 from an expression of the same type, optionally with braces. */
400fbf9f 7066
3e4093b6
RS
7067 if (inside_init && TREE_TYPE (inside_init) != 0
7068 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 7069 TYPE_MAIN_VARIANT (type))
3e4093b6 7070 || (code == ARRAY_TYPE
132da1a5 7071 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7072 || (code == VECTOR_TYPE
132da1a5 7073 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7074 || (code == POINTER_TYPE
3897f229 7075 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 7076 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 7077 TREE_TYPE (type)))))
3e4093b6
RS
7078 {
7079 if (code == POINTER_TYPE)
b494fd98 7080 {
b494fd98
EB
7081 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7082 {
f2a71bbc
JM
7083 if (TREE_CODE (inside_init) == STRING_CST
7084 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
7085 inside_init = array_to_pointer_conversion
7086 (init_loc, inside_init);
f2a71bbc
JM
7087 else
7088 {
ea58ef42 7089 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
7090 return error_mark_node;
7091 }
b494fd98 7092 }
f2a71bbc 7093 }
b494fd98 7094
bae39a73
NS
7095 if (code == VECTOR_TYPE)
7096 /* Although the types are compatible, we may require a
7097 conversion. */
7098 inside_init = convert (type, inside_init);
3e4093b6 7099
ca58211b 7100 if (require_constant
3e4093b6 7101 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 7102 {
3e4093b6
RS
7103 /* As an extension, allow initializing objects with static storage
7104 duration with compound literals (which are then treated just as
ca58211b
PB
7105 the brace enclosed list they contain). Also allow this for
7106 vectors, as we can only assign them with compound literals. */
7278465e
MP
7107 if (flag_isoc99 && code != VECTOR_TYPE)
7108 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7109 "is not constant");
3e4093b6
RS
7110 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7111 inside_init = DECL_INITIAL (decl);
400fbf9f 7112 }
3e4093b6
RS
7113
7114 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7115 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 7116 {
ea58ef42
MP
7117 error_init (init_loc, "array initialized from non-constant array "
7118 "expression");
3e4093b6 7119 return error_mark_node;
400fbf9f 7120 }
400fbf9f 7121
c1771a20 7122 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
7123 -pedantic-errors is specified. In the later case, we always want
7124 an error. In the former case, we simply want a warning. */
7125 if (require_constant && pedantic
7126 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7127 {
7128 inside_init
7129 = valid_compound_expr_initializer (inside_init,
7130 TREE_TYPE (inside_init));
7131 if (inside_init == error_mark_node)
ea58ef42 7132 error_init (init_loc, "initializer element is not constant");
2f6e4e97 7133 else
c1771a20 7134 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 7135 "initializer element is not constant");
3e4093b6
RS
7136 if (flag_pedantic_errors)
7137 inside_init = error_mark_node;
7138 }
7139 else if (require_constant
116df786
RH
7140 && !initializer_constant_valid_p (inside_init,
7141 TREE_TYPE (inside_init)))
3e4093b6 7142 {
ea58ef42 7143 error_init (init_loc, "initializer element is not constant");
3e4093b6 7144 inside_init = error_mark_node;
8b40563c 7145 }
928c19bb 7146 else if (require_constant && !maybe_const)
3aa3c9fc 7147 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7148 "initializer element is not a constant expression");
f735a153 7149
90137d8f 7150 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 7151 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
7152 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7153 type, inside_init, origtype,
bbbbb16a 7154 ic_init, null_pointer_constant,
928c19bb 7155 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
7156 return inside_init;
7157 }
f735a153 7158
3e4093b6 7159 /* Handle scalar types, including conversions. */
400fbf9f 7160
ab22c1fa
CF
7161 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7162 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7163 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 7164 {
f2a71bbc
JM
7165 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7166 && (TREE_CODE (init) == STRING_CST
7167 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 7168 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
7169 if (semantic_type)
7170 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7171 inside_init);
3e4093b6 7172 inside_init
68fca595
MP
7173 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7174 inside_init, origtype, ic_init,
7175 null_pointer_constant, NULL_TREE, NULL_TREE,
7176 0);
2f6e4e97 7177
3274deff
JW
7178 /* Check to see if we have already given an error message. */
7179 if (inside_init == error_mark_node)
7180 ;
3f75a254 7181 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 7182 {
ea58ef42 7183 error_init (init_loc, "initializer element is not constant");
3e4093b6 7184 inside_init = error_mark_node;
400fbf9f 7185 }
3e4093b6 7186 else if (require_constant
116df786
RH
7187 && !initializer_constant_valid_p (inside_init,
7188 TREE_TYPE (inside_init)))
400fbf9f 7189 {
ea58ef42
MP
7190 error_init (init_loc, "initializer element is not computable at "
7191 "load time");
3e4093b6 7192 inside_init = error_mark_node;
400fbf9f 7193 }
928c19bb 7194 else if (require_constant && !maybe_const)
451b5e48 7195 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7196 "initializer element is not a constant expression");
3e4093b6
RS
7197
7198 return inside_init;
400fbf9f 7199 }
d9fc6069 7200
3e4093b6 7201 /* Come here only for records and arrays. */
d9fc6069 7202
3e4093b6 7203 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 7204 {
ea58ef42 7205 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 7206 return error_mark_node;
d9fc6069 7207 }
3e4093b6 7208
ea58ef42 7209 error_init (init_loc, "invalid initializer");
3e4093b6 7210 return error_mark_node;
d9fc6069 7211}
400fbf9f 7212\f
3e4093b6 7213/* Handle initializers that use braces. */
400fbf9f 7214
3e4093b6
RS
7215/* Type of object we are accumulating a constructor for.
7216 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7217static tree constructor_type;
400fbf9f 7218
3e4093b6
RS
7219/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7220 left to fill. */
7221static tree constructor_fields;
400fbf9f 7222
3e4093b6
RS
7223/* For an ARRAY_TYPE, this is the specified index
7224 at which to store the next element we get. */
7225static tree constructor_index;
400fbf9f 7226
3e4093b6
RS
7227/* For an ARRAY_TYPE, this is the maximum index. */
7228static tree constructor_max_index;
400fbf9f 7229
3e4093b6
RS
7230/* For a RECORD_TYPE, this is the first field not yet written out. */
7231static tree constructor_unfilled_fields;
400fbf9f 7232
3e4093b6
RS
7233/* For an ARRAY_TYPE, this is the index of the first element
7234 not yet written out. */
7235static tree constructor_unfilled_index;
895ea614 7236
3e4093b6
RS
7237/* In a RECORD_TYPE, the byte index of the next consecutive field.
7238 This is so we can generate gaps between fields, when appropriate. */
7239static tree constructor_bit_index;
10d5caec 7240
3e4093b6
RS
7241/* If we are saving up the elements rather than allocating them,
7242 this is the list of elements so far (in reverse order,
7243 most recent first). */
9771b263 7244static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 7245
3e4093b6
RS
7246/* 1 if constructor should be incrementally stored into a constructor chain,
7247 0 if all the elements should be kept in AVL tree. */
7248static int constructor_incremental;
ad47f1e5 7249
3e4093b6
RS
7250/* 1 if so far this constructor's elements are all compile-time constants. */
7251static int constructor_constant;
ad47f1e5 7252
3e4093b6
RS
7253/* 1 if so far this constructor's elements are all valid address constants. */
7254static int constructor_simple;
ad47f1e5 7255
928c19bb
JM
7256/* 1 if this constructor has an element that cannot be part of a
7257 constant expression. */
7258static int constructor_nonconst;
7259
3e4093b6
RS
7260/* 1 if this constructor is erroneous so far. */
7261static int constructor_erroneous;
d45cf215 7262
9bac5cbb
G
7263/* 1 if this constructor is the universal zero initializer { 0 }. */
7264static int constructor_zeroinit;
7265
3e4093b6
RS
7266/* Structure for managing pending initializer elements, organized as an
7267 AVL tree. */
d45cf215 7268
3e4093b6 7269struct init_node
d45cf215 7270{
3e4093b6
RS
7271 struct init_node *left, *right;
7272 struct init_node *parent;
7273 int balance;
7274 tree purpose;
7275 tree value;
bbbbb16a 7276 tree origtype;
d45cf215
RS
7277};
7278
3e4093b6
RS
7279/* Tree of pending elements at this constructor level.
7280 These are elements encountered out of order
7281 which belong at places we haven't reached yet in actually
7282 writing the output.
7283 Will never hold tree nodes across GC runs. */
7284static struct init_node *constructor_pending_elts;
d45cf215 7285
3e4093b6
RS
7286/* The SPELLING_DEPTH of this constructor. */
7287static int constructor_depth;
d45cf215 7288
3e4093b6
RS
7289/* DECL node for which an initializer is being read.
7290 0 means we are reading a constructor expression
7291 such as (struct foo) {...}. */
7292static tree constructor_decl;
d45cf215 7293
3e4093b6
RS
7294/* Nonzero if this is an initializer for a top-level decl. */
7295static int constructor_top_level;
d45cf215 7296
3e4093b6
RS
7297/* Nonzero if there were any member designators in this initializer. */
7298static int constructor_designated;
d45cf215 7299
3e4093b6
RS
7300/* Nesting depth of designator list. */
7301static int designator_depth;
d45cf215 7302
3e4093b6 7303/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7304static int designator_erroneous;
d45cf215 7305
3e4093b6
RS
7306\f
7307/* This stack has a level for each implicit or explicit level of
7308 structuring in the initializer, including the outermost one. It
7309 saves the values of most of the variables above. */
d45cf215 7310
3e4093b6
RS
7311struct constructor_range_stack;
7312
7313struct constructor_stack
d45cf215 7314{
3e4093b6
RS
7315 struct constructor_stack *next;
7316 tree type;
7317 tree fields;
7318 tree index;
7319 tree max_index;
7320 tree unfilled_index;
7321 tree unfilled_fields;
7322 tree bit_index;
9771b263 7323 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7324 struct init_node *pending_elts;
7325 int offset;
7326 int depth;
916c5919 7327 /* If value nonzero, this value should replace the entire
3e4093b6 7328 constructor at this level. */
916c5919 7329 struct c_expr replacement_value;
3e4093b6
RS
7330 struct constructor_range_stack *range_stack;
7331 char constant;
7332 char simple;
928c19bb 7333 char nonconst;
3e4093b6
RS
7334 char implicit;
7335 char erroneous;
7336 char outer;
7337 char incremental;
7338 char designated;
976d5a22 7339 int designator_depth;
3e4093b6 7340};
d45cf215 7341
802415d1 7342static struct constructor_stack *constructor_stack;
d45cf215 7343
3e4093b6
RS
7344/* This stack represents designators from some range designator up to
7345 the last designator in the list. */
d45cf215 7346
3e4093b6
RS
7347struct constructor_range_stack
7348{
7349 struct constructor_range_stack *next, *prev;
7350 struct constructor_stack *stack;
7351 tree range_start;
7352 tree index;
7353 tree range_end;
7354 tree fields;
7355};
d45cf215 7356
802415d1 7357static struct constructor_range_stack *constructor_range_stack;
d45cf215 7358
3e4093b6
RS
7359/* This stack records separate initializers that are nested.
7360 Nested initializers can't happen in ANSI C, but GNU C allows them
7361 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7362
3e4093b6 7363struct initializer_stack
d45cf215 7364{
3e4093b6
RS
7365 struct initializer_stack *next;
7366 tree decl;
3e4093b6
RS
7367 struct constructor_stack *constructor_stack;
7368 struct constructor_range_stack *constructor_range_stack;
9771b263 7369 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7370 struct spelling *spelling;
7371 struct spelling *spelling_base;
7372 int spelling_size;
7373 char top_level;
7374 char require_constant_value;
7375 char require_constant_elements;
7376};
d45cf215 7377
802415d1 7378static struct initializer_stack *initializer_stack;
3e4093b6
RS
7379\f
7380/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7381
7382void
a396f8ae 7383start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 7384{
3e4093b6 7385 const char *locus;
cceb1885 7386 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7387
3e4093b6 7388 p->decl = constructor_decl;
3e4093b6
RS
7389 p->require_constant_value = require_constant_value;
7390 p->require_constant_elements = require_constant_elements;
7391 p->constructor_stack = constructor_stack;
7392 p->constructor_range_stack = constructor_range_stack;
7393 p->elements = constructor_elements;
7394 p->spelling = spelling;
7395 p->spelling_base = spelling_base;
7396 p->spelling_size = spelling_size;
7397 p->top_level = constructor_top_level;
7398 p->next = initializer_stack;
7399 initializer_stack = p;
400fbf9f 7400
3e4093b6 7401 constructor_decl = decl;
3e4093b6
RS
7402 constructor_designated = 0;
7403 constructor_top_level = top_level;
400fbf9f 7404
6f17bbcf 7405 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7406 {
7407 require_constant_value = TREE_STATIC (decl);
7408 require_constant_elements
7409 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7410 /* For a scalar, you can always use any value to initialize,
7411 even within braces. */
296a8c2f 7412 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
88388a52 7413 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7414 }
7415 else
7416 {
7417 require_constant_value = 0;
7418 require_constant_elements = 0;
88388a52 7419 locus = _("(anonymous)");
3e4093b6 7420 }
b71c7f8a 7421
3e4093b6
RS
7422 constructor_stack = 0;
7423 constructor_range_stack = 0;
b71c7f8a 7424
9bac5cbb 7425 found_missing_braces = 0;
3e4093b6
RS
7426
7427 spelling_base = 0;
7428 spelling_size = 0;
7429 RESTORE_SPELLING_DEPTH (0);
7430
7431 if (locus)
7432 push_string (locus);
7433}
7434
7435void
7436finish_init (void)
b71c7f8a 7437{
3e4093b6 7438 struct initializer_stack *p = initializer_stack;
b71c7f8a 7439
3e4093b6
RS
7440 /* Free the whole constructor stack of this initializer. */
7441 while (constructor_stack)
7442 {
7443 struct constructor_stack *q = constructor_stack;
7444 constructor_stack = q->next;
7445 free (q);
7446 }
7447
366de0ce 7448 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7449
7450 /* Pop back to the data of the outer initializer (if any). */
36579663 7451 free (spelling_base);
3aeb3655 7452
3e4093b6 7453 constructor_decl = p->decl;
3e4093b6
RS
7454 require_constant_value = p->require_constant_value;
7455 require_constant_elements = p->require_constant_elements;
7456 constructor_stack = p->constructor_stack;
7457 constructor_range_stack = p->constructor_range_stack;
7458 constructor_elements = p->elements;
7459 spelling = p->spelling;
7460 spelling_base = p->spelling_base;
7461 spelling_size = p->spelling_size;
7462 constructor_top_level = p->top_level;
7463 initializer_stack = p->next;
7464 free (p);
b71c7f8a 7465}
400fbf9f 7466\f
3e4093b6
RS
7467/* Call here when we see the initializer is surrounded by braces.
7468 This is instead of a call to push_init_level;
7469 it is matched by a call to pop_init_level.
400fbf9f 7470
3e4093b6
RS
7471 TYPE is the type to initialize, for a constructor expression.
7472 For an initializer for a decl, TYPE is zero. */
400fbf9f 7473
3e4093b6
RS
7474void
7475really_start_incremental_init (tree type)
400fbf9f 7476{
5d038c4c 7477 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7478
3e4093b6
RS
7479 if (type == 0)
7480 type = TREE_TYPE (constructor_decl);
400fbf9f 7481
31521951 7482 if (VECTOR_TYPE_P (type)
b6fc2cdb 7483 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7484 error ("opaque vector types cannot be initialized");
400fbf9f 7485
3e4093b6
RS
7486 p->type = constructor_type;
7487 p->fields = constructor_fields;
7488 p->index = constructor_index;
7489 p->max_index = constructor_max_index;
7490 p->unfilled_index = constructor_unfilled_index;
7491 p->unfilled_fields = constructor_unfilled_fields;
7492 p->bit_index = constructor_bit_index;
7493 p->elements = constructor_elements;
7494 p->constant = constructor_constant;
7495 p->simple = constructor_simple;
928c19bb 7496 p->nonconst = constructor_nonconst;
3e4093b6
RS
7497 p->erroneous = constructor_erroneous;
7498 p->pending_elts = constructor_pending_elts;
7499 p->depth = constructor_depth;
916c5919
JM
7500 p->replacement_value.value = 0;
7501 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7502 p->replacement_value.original_type = NULL;
3e4093b6
RS
7503 p->implicit = 0;
7504 p->range_stack = 0;
7505 p->outer = 0;
7506 p->incremental = constructor_incremental;
7507 p->designated = constructor_designated;
976d5a22 7508 p->designator_depth = designator_depth;
3e4093b6
RS
7509 p->next = 0;
7510 constructor_stack = p;
b13aca19 7511
3e4093b6
RS
7512 constructor_constant = 1;
7513 constructor_simple = 1;
928c19bb 7514 constructor_nonconst = 0;
3e4093b6 7515 constructor_depth = SPELLING_DEPTH ();
9771b263 7516 constructor_elements = NULL;
3e4093b6
RS
7517 constructor_pending_elts = 0;
7518 constructor_type = type;
7519 constructor_incremental = 1;
7520 constructor_designated = 0;
9bac5cbb 7521 constructor_zeroinit = 1;
3e4093b6 7522 designator_depth = 0;
b06df647 7523 designator_erroneous = 0;
400fbf9f 7524
3e636daf 7525 if (RECORD_OR_UNION_TYPE_P (constructor_type))
400fbf9f 7526 {
3e4093b6
RS
7527 constructor_fields = TYPE_FIELDS (constructor_type);
7528 /* Skip any nameless bit fields at the beginning. */
7529 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7530 && DECL_NAME (constructor_fields) == 0)
910ad8de 7531 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7532
3e4093b6
RS
7533 constructor_unfilled_fields = constructor_fields;
7534 constructor_bit_index = bitsize_zero_node;
400fbf9f 7535 }
3e4093b6
RS
7536 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7537 {
7538 if (TYPE_DOMAIN (constructor_type))
7539 {
7540 constructor_max_index
7541 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7542
3e4093b6
RS
7543 /* Detect non-empty initializations of zero-length arrays. */
7544 if (constructor_max_index == NULL_TREE
7545 && TYPE_SIZE (constructor_type))
9a9d280e 7546 constructor_max_index = integer_minus_one_node;
400fbf9f 7547
3e4093b6
RS
7548 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7549 to initialize VLAs will cause a proper error; avoid tree
7550 checking errors as well by setting a safe value. */
7551 if (constructor_max_index
7552 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7553 constructor_max_index = integer_minus_one_node;
59c83dbf 7554
3e4093b6
RS
7555 constructor_index
7556 = convert (bitsizetype,
7557 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7558 }
3e4093b6 7559 else
493179da
JM
7560 {
7561 constructor_index = bitsize_zero_node;
7562 constructor_max_index = NULL_TREE;
7563 }
59c83dbf 7564
3e4093b6
RS
7565 constructor_unfilled_index = constructor_index;
7566 }
31521951 7567 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7568 {
7569 /* Vectors are like simple fixed-size arrays. */
7570 constructor_max_index =
c62c040f 7571 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7572 constructor_index = bitsize_zero_node;
3e4093b6
RS
7573 constructor_unfilled_index = constructor_index;
7574 }
7575 else
7576 {
7577 /* Handle the case of int x = {5}; */
7578 constructor_fields = constructor_type;
7579 constructor_unfilled_fields = constructor_type;
7580 }
7581}
7582\f
16595a1f
BS
7583/* Called when we see an open brace for a nested initializer. Finish
7584 off any pending levels with implicit braces. */
7585void
7586finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7587{
7588 while (constructor_stack->implicit)
7589 {
7590 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7591 && constructor_fields == 0)
7592 process_init_element (input_location,
7593 pop_init_level (loc, 1, braced_init_obstack),
7594 true, braced_init_obstack);
7595 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7596 && constructor_max_index
7597 && tree_int_cst_lt (constructor_max_index,
7598 constructor_index))
7599 process_init_element (input_location,
7600 pop_init_level (loc, 1, braced_init_obstack),
7601 true, braced_init_obstack);
7602 else
7603 break;
7604 }
7605}
7606
3e4093b6
RS
7607/* Push down into a subobject, for initialization.
7608 If this is for an explicit set of braces, IMPLICIT is 0.
7609 If it is because the next element belongs at a lower level,
7610 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7611
3e4093b6 7612void
ea58ef42
MP
7613push_init_level (location_t loc, int implicit,
7614 struct obstack *braced_init_obstack)
3e4093b6
RS
7615{
7616 struct constructor_stack *p;
7617 tree value = NULL_TREE;
400fbf9f 7618
3e4093b6
RS
7619 /* Unless this is an explicit brace, we need to preserve previous
7620 content if any. */
7621 if (implicit)
7622 {
3e636daf 7623 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
a1e3b3d9 7624 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7625 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7626 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7627 }
7628
5d038c4c 7629 p = XNEW (struct constructor_stack);
3e4093b6
RS
7630 p->type = constructor_type;
7631 p->fields = constructor_fields;
7632 p->index = constructor_index;
7633 p->max_index = constructor_max_index;
7634 p->unfilled_index = constructor_unfilled_index;
7635 p->unfilled_fields = constructor_unfilled_fields;
7636 p->bit_index = constructor_bit_index;
7637 p->elements = constructor_elements;
7638 p->constant = constructor_constant;
7639 p->simple = constructor_simple;
928c19bb 7640 p->nonconst = constructor_nonconst;
3e4093b6
RS
7641 p->erroneous = constructor_erroneous;
7642 p->pending_elts = constructor_pending_elts;
7643 p->depth = constructor_depth;
916c5919
JM
7644 p->replacement_value.value = 0;
7645 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7646 p->replacement_value.original_type = NULL;
3e4093b6
RS
7647 p->implicit = implicit;
7648 p->outer = 0;
7649 p->incremental = constructor_incremental;
7650 p->designated = constructor_designated;
976d5a22 7651 p->designator_depth = designator_depth;
3e4093b6
RS
7652 p->next = constructor_stack;
7653 p->range_stack = 0;
7654 constructor_stack = p;
400fbf9f 7655
3e4093b6
RS
7656 constructor_constant = 1;
7657 constructor_simple = 1;
928c19bb 7658 constructor_nonconst = 0;
3e4093b6 7659 constructor_depth = SPELLING_DEPTH ();
9771b263 7660 constructor_elements = NULL;
3e4093b6
RS
7661 constructor_incremental = 1;
7662 constructor_designated = 0;
7663 constructor_pending_elts = 0;
7664 if (!implicit)
400fbf9f 7665 {
3e4093b6
RS
7666 p->range_stack = constructor_range_stack;
7667 constructor_range_stack = 0;
7668 designator_depth = 0;
b06df647 7669 designator_erroneous = 0;
3e4093b6 7670 }
400fbf9f 7671
3e4093b6
RS
7672 /* Don't die if an entire brace-pair level is superfluous
7673 in the containing level. */
7674 if (constructor_type == 0)
7675 ;
3e636daf 7676 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
7677 {
7678 /* Don't die if there are extra init elts at the end. */
7679 if (constructor_fields == 0)
7680 constructor_type = 0;
7681 else
400fbf9f 7682 {
3e4093b6
RS
7683 constructor_type = TREE_TYPE (constructor_fields);
7684 push_member_name (constructor_fields);
7685 constructor_depth++;
400fbf9f 7686 }
6a358dcb
MP
7687 /* If upper initializer is designated, then mark this as
7688 designated too to prevent bogus warnings. */
7689 constructor_designated = p->designated;
3e4093b6
RS
7690 }
7691 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7692 {
7693 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7694 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7695 constructor_depth++;
400fbf9f
JW
7696 }
7697
3e4093b6 7698 if (constructor_type == 0)
400fbf9f 7699 {
ea58ef42 7700 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7701 constructor_fields = 0;
7702 constructor_unfilled_fields = 0;
7703 return;
400fbf9f
JW
7704 }
7705
3e4093b6
RS
7706 if (value && TREE_CODE (value) == CONSTRUCTOR)
7707 {
7708 constructor_constant = TREE_CONSTANT (value);
7709 constructor_simple = TREE_STATIC (value);
928c19bb 7710 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7711 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7712 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7713 && (TREE_CODE (constructor_type) == RECORD_TYPE
7714 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7715 set_nonincremental_init (braced_init_obstack);
3e4093b6 7716 }
400fbf9f 7717
9bac5cbb
G
7718 if (implicit == 1)
7719 found_missing_braces = 1;
400fbf9f 7720
3e636daf 7721 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
7722 {
7723 constructor_fields = TYPE_FIELDS (constructor_type);
7724 /* Skip any nameless bit fields at the beginning. */
7725 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7726 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7727 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7728
3e4093b6
RS
7729 constructor_unfilled_fields = constructor_fields;
7730 constructor_bit_index = bitsize_zero_node;
7731 }
31521951 7732 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7733 {
7734 /* Vectors are like simple fixed-size arrays. */
7735 constructor_max_index =
c62c040f
RG
7736 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7737 constructor_index = bitsize_int (0);
3e4093b6
RS
7738 constructor_unfilled_index = constructor_index;
7739 }
7740 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7741 {
7742 if (TYPE_DOMAIN (constructor_type))
7743 {
7744 constructor_max_index
7745 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7746
3e4093b6
RS
7747 /* Detect non-empty initializations of zero-length arrays. */
7748 if (constructor_max_index == NULL_TREE
7749 && TYPE_SIZE (constructor_type))
9a9d280e 7750 constructor_max_index = integer_minus_one_node;
de520661 7751
3e4093b6
RS
7752 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7753 to initialize VLAs will cause a proper error; avoid tree
7754 checking errors as well by setting a safe value. */
7755 if (constructor_max_index
7756 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7757 constructor_max_index = integer_minus_one_node;
b62acd60 7758
3e4093b6
RS
7759 constructor_index
7760 = convert (bitsizetype,
7761 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7762 }
7763 else
7764 constructor_index = bitsize_zero_node;
de520661 7765
3e4093b6
RS
7766 constructor_unfilled_index = constructor_index;
7767 if (value && TREE_CODE (value) == STRING_CST)
7768 {
7769 /* We need to split the char/wchar array into individual
7770 characters, so that we don't have to special case it
7771 everywhere. */
a1e3b3d9 7772 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7773 }
7774 }
7775 else
7776 {
b4519d39 7777 if (constructor_type != error_mark_node)
96b40f8d 7778 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7779 constructor_fields = constructor_type;
7780 constructor_unfilled_fields = constructor_type;
7781 }
7782}
8b6a5902 7783
3e4093b6 7784/* At the end of an implicit or explicit brace level,
916c5919
JM
7785 finish up that level of constructor. If a single expression
7786 with redundant braces initialized that level, return the
7787 c_expr structure for that expression. Otherwise, the original_code
7788 element is set to ERROR_MARK.
7789 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7790 from inner levels (process_init_element ignores that),
916c5919 7791 but return error_mark_node as the value from the outermost level
3e4093b6 7792 (that's what we want to put in DECL_INITIAL).
916c5919 7793 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7794
916c5919 7795struct c_expr
ea58ef42
MP
7796pop_init_level (location_t loc, int implicit,
7797 struct obstack *braced_init_obstack)
3e4093b6
RS
7798{
7799 struct constructor_stack *p;
916c5919
JM
7800 struct c_expr ret;
7801 ret.value = 0;
7802 ret.original_code = ERROR_MARK;
6866c6e8 7803 ret.original_type = NULL;
de520661 7804
3e4093b6
RS
7805 if (implicit == 0)
7806 {
7807 /* When we come to an explicit close brace,
7808 pop any inner levels that didn't have explicit braces. */
7809 while (constructor_stack->implicit)
34cf811f 7810 process_init_element (input_location,
ea58ef42 7811 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7812 true, braced_init_obstack);
366de0ce 7813 gcc_assert (!constructor_range_stack);
3e4093b6 7814 }
e5e809f4 7815
0066ef9c
RH
7816 /* Now output all pending elements. */
7817 constructor_incremental = 1;
a1e3b3d9 7818 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7819
3e4093b6 7820 p = constructor_stack;
e5e809f4 7821
3e4093b6
RS
7822 /* Error for initializing a flexible array member, or a zero-length
7823 array member in an inappropriate context. */
7824 if (constructor_type && constructor_fields
7825 && TREE_CODE (constructor_type) == ARRAY_TYPE
7826 && TYPE_DOMAIN (constructor_type)
3f75a254 7827 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7828 {
7829 /* Silently discard empty initializations. The parser will
7830 already have pedwarned for empty brackets. */
7831 if (integer_zerop (constructor_unfilled_index))
7832 constructor_type = NULL_TREE;
366de0ce 7833 else
3e4093b6 7834 {
366de0ce 7835 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7836
3e4093b6 7837 if (constructor_depth > 2)
ea58ef42 7838 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7839 else
d033409e 7840 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7841 "initialization of a flexible array member");
de520661 7842
3e4093b6
RS
7843 /* We have already issued an error message for the existence
7844 of a flexible array member not at the end of the structure.
535a42b1 7845 Discard the initializer so that we do not die later. */
910ad8de 7846 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7847 constructor_type = NULL_TREE;
7848 }
3e4093b6 7849 }
de520661 7850
7b33f0c8
MP
7851 switch (vec_safe_length (constructor_elements))
7852 {
7853 case 0:
7854 /* Initialization with { } counts as zeroinit. */
7855 constructor_zeroinit = 1;
7856 break;
7857 case 1:
7858 /* This might be zeroinit as well. */
7859 if (integer_zerop ((*constructor_elements)[0].value))
7860 constructor_zeroinit = 1;
7861 break;
7862 default:
7863 /* If the constructor has more than one element, it can't be { 0 }. */
7864 constructor_zeroinit = 0;
7865 break;
7866 }
9bac5cbb
G
7867
7868 /* Warn when some structs are initialized with direct aggregation. */
7869 if (!implicit && found_missing_braces && warn_missing_braces
7870 && !constructor_zeroinit)
7b33f0c8
MP
7871 warning_init (loc, OPT_Wmissing_braces,
7872 "missing braces around initializer");
9bac5cbb 7873
3e4093b6 7874 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7875 if (warn_missing_field_initializers
3e4093b6
RS
7876 && constructor_type
7877 && TREE_CODE (constructor_type) == RECORD_TYPE
7878 && constructor_unfilled_fields)
7879 {
7880 /* Do not warn for flexible array members or zero-length arrays. */
7881 while (constructor_unfilled_fields
3f75a254 7882 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7883 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7884 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7885
49819fef
AM
7886 if (constructor_unfilled_fields
7887 /* Do not warn if this level of the initializer uses member
7888 designators; it is likely to be deliberate. */
7889 && !constructor_designated
84937de2 7890 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 7891 && !constructor_zeroinit)
3e4093b6 7892 {
32397f22
MLI
7893 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7894 "missing initializer for field %qD of %qT",
7895 constructor_unfilled_fields,
7896 constructor_type))
7897 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7898 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7899 }
7900 }
de520661 7901
3e4093b6 7902 /* Pad out the end of the structure. */
916c5919 7903 if (p->replacement_value.value)
3e4093b6
RS
7904 /* If this closes a superfluous brace pair,
7905 just pass out the element between them. */
916c5919 7906 ret = p->replacement_value;
3e4093b6
RS
7907 else if (constructor_type == 0)
7908 ;
3e636daf 7909 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 7910 && TREE_CODE (constructor_type) != ARRAY_TYPE
31521951 7911 && !VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7912 {
7913 /* A nonincremental scalar initializer--just return
7914 the element, after verifying there is just one. */
9771b263 7915 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7916 {
7917 if (!constructor_erroneous)
ea58ef42 7918 error_init (loc, "empty scalar initializer");
916c5919 7919 ret.value = error_mark_node;
3e4093b6 7920 }
9771b263 7921 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 7922 {
ea58ef42 7923 error_init (loc, "extra elements in scalar initializer");
9771b263 7924 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7925 }
7926 else
9771b263 7927 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7928 }
7929 else
7930 {
7931 if (constructor_erroneous)
916c5919 7932 ret.value = error_mark_node;
3e4093b6
RS
7933 else
7934 {
916c5919 7935 ret.value = build_constructor (constructor_type,
4038c495 7936 constructor_elements);
3e4093b6 7937 if (constructor_constant)
51eed280 7938 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7939 if (constructor_constant && constructor_simple)
916c5919 7940 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7941 if (constructor_nonconst)
7942 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7943 }
7944 }
de520661 7945
928c19bb
JM
7946 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7947 {
7948 if (constructor_nonconst)
7949 ret.original_code = C_MAYBE_CONST_EXPR;
7950 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7951 ret.original_code = ERROR_MARK;
7952 }
7953
3e4093b6
RS
7954 constructor_type = p->type;
7955 constructor_fields = p->fields;
7956 constructor_index = p->index;
7957 constructor_max_index = p->max_index;
7958 constructor_unfilled_index = p->unfilled_index;
7959 constructor_unfilled_fields = p->unfilled_fields;
7960 constructor_bit_index = p->bit_index;
7961 constructor_elements = p->elements;
7962 constructor_constant = p->constant;
7963 constructor_simple = p->simple;
928c19bb 7964 constructor_nonconst = p->nonconst;
3e4093b6
RS
7965 constructor_erroneous = p->erroneous;
7966 constructor_incremental = p->incremental;
7967 constructor_designated = p->designated;
976d5a22 7968 designator_depth = p->designator_depth;
3e4093b6
RS
7969 constructor_pending_elts = p->pending_elts;
7970 constructor_depth = p->depth;
7971 if (!p->implicit)
7972 constructor_range_stack = p->range_stack;
7973 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7974
3e4093b6
RS
7975 constructor_stack = p->next;
7976 free (p);
b621a4dd 7977
5d5e98dc
VR
7978 if (ret.value == 0 && constructor_stack == 0)
7979 ret.value = error_mark_node;
916c5919 7980 return ret;
3e4093b6 7981}
8b6a5902 7982
3e4093b6
RS
7983/* Common handling for both array range and field name designators.
7984 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7985
3e4093b6 7986static int
ea58ef42
MP
7987set_designator (location_t loc, int array,
7988 struct obstack *braced_init_obstack)
de520661 7989{
3e4093b6
RS
7990 tree subtype;
7991 enum tree_code subcode;
de520661 7992
3e4093b6
RS
7993 /* Don't die if an entire brace-pair level is superfluous
7994 in the containing level. */
7995 if (constructor_type == 0)
7996 return 1;
de520661 7997
366de0ce
NS
7998 /* If there were errors in this designator list already, bail out
7999 silently. */
b06df647 8000 if (designator_erroneous)
3e4093b6 8001 return 1;
e28cae4f 8002
3e4093b6
RS
8003 if (!designator_depth)
8004 {
366de0ce 8005 gcc_assert (!constructor_range_stack);
de520661 8006
3e4093b6
RS
8007 /* Designator list starts at the level of closest explicit
8008 braces. */
8009 while (constructor_stack->implicit)
34cf811f 8010 process_init_element (input_location,
ea58ef42 8011 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 8012 true, braced_init_obstack);
3e4093b6
RS
8013 constructor_designated = 1;
8014 return 0;
8015 }
de520661 8016
366de0ce 8017 switch (TREE_CODE (constructor_type))
3c3fa147 8018 {
366de0ce
NS
8019 case RECORD_TYPE:
8020 case UNION_TYPE:
3e4093b6
RS
8021 subtype = TREE_TYPE (constructor_fields);
8022 if (subtype != error_mark_node)
8023 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
8024 break;
8025 case ARRAY_TYPE:
3e4093b6 8026 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
8027 break;
8028 default:
8029 gcc_unreachable ();
de520661 8030 }
400fbf9f 8031
3e4093b6
RS
8032 subcode = TREE_CODE (subtype);
8033 if (array && subcode != ARRAY_TYPE)
8034 {
ea58ef42 8035 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
8036 return 1;
8037 }
8038 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8039 {
ea58ef42 8040 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
8041 return 1;
8042 }
d45cf215 8043
3e4093b6 8044 constructor_designated = 1;
16595a1f 8045 finish_implicit_inits (loc, braced_init_obstack);
ea58ef42 8046 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 8047 return 0;
de520661 8048}
400fbf9f 8049
3e4093b6
RS
8050/* If there are range designators in designator list, push a new designator
8051 to constructor_range_stack. RANGE_END is end of such stack range or
8052 NULL_TREE if there is no range designator at this level. */
400fbf9f 8053
3e4093b6 8054static void
a1e3b3d9 8055push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
8056{
8057 struct constructor_range_stack *p;
400fbf9f 8058
a1e3b3d9
LB
8059 p = (struct constructor_range_stack *)
8060 obstack_alloc (braced_init_obstack,
8061 sizeof (struct constructor_range_stack));
3e4093b6
RS
8062 p->prev = constructor_range_stack;
8063 p->next = 0;
8064 p->fields = constructor_fields;
8065 p->range_start = constructor_index;
8066 p->index = constructor_index;
8067 p->stack = constructor_stack;
8068 p->range_end = range_end;
8b6a5902 8069 if (constructor_range_stack)
3e4093b6
RS
8070 constructor_range_stack->next = p;
8071 constructor_range_stack = p;
de520661 8072}
400fbf9f 8073
3e4093b6
RS
8074/* Within an array initializer, specify the next index to be initialized.
8075 FIRST is that index. If LAST is nonzero, then initialize a range
8076 of indices, running from FIRST through LAST. */
5a7ec9d9 8077
de520661 8078void
ea58ef42 8079set_init_index (location_t loc, tree first, tree last,
d033409e 8080 struct obstack *braced_init_obstack)
de520661 8081{
ea58ef42 8082 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 8083 return;
de520661 8084
b06df647 8085 designator_erroneous = 1;
de520661 8086
3ea8cd06
JM
8087 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8088 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8089 {
ea58ef42 8090 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
8091 return;
8092 }
8093
2b6da65c
JM
8094 if (TREE_CODE (first) != INTEGER_CST)
8095 {
8096 first = c_fully_fold (first, false, NULL);
8097 if (TREE_CODE (first) == INTEGER_CST)
d033409e 8098 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8099 "array index in initializer is not "
8100 "an integer constant expression");
8101 }
8102
8103 if (last && TREE_CODE (last) != INTEGER_CST)
8104 {
8105 last = c_fully_fold (last, false, NULL);
8106 if (TREE_CODE (last) == INTEGER_CST)
d033409e 8107 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8108 "array index in initializer is not "
8109 "an integer constant expression");
8110 }
8111
3e4093b6 8112 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 8113 error_init (loc, "nonconstant array index in initializer");
3e4093b6 8114 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 8115 error_init (loc, "nonconstant array index in initializer");
3e4093b6 8116 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 8117 error_init (loc, "array index in non-array initializer");
622adc7e 8118 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 8119 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
8120 else if (constructor_max_index
8121 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 8122 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 8123 else
de520661 8124 {
928c19bb
JM
8125 constant_expression_warning (first);
8126 if (last)
8127 constant_expression_warning (last);
3e4093b6 8128 constructor_index = convert (bitsizetype, first);
40d3d530
JR
8129 if (tree_int_cst_lt (constructor_index, first))
8130 {
8131 constructor_index = copy_node (constructor_index);
8132 TREE_OVERFLOW (constructor_index) = 1;
8133 }
665f2503 8134
3e4093b6 8135 if (last)
2bede729 8136 {
3e4093b6
RS
8137 if (tree_int_cst_equal (first, last))
8138 last = 0;
8139 else if (tree_int_cst_lt (last, first))
8140 {
ea58ef42 8141 error_init (loc, "empty index range in initializer");
3e4093b6
RS
8142 last = 0;
8143 }
8144 else
8145 {
8146 last = convert (bitsizetype, last);
8147 if (constructor_max_index != 0
8148 && tree_int_cst_lt (constructor_max_index, last))
8149 {
ea58ef42
MP
8150 error_init (loc, "array index range in initializer exceeds "
8151 "array bounds");
3e4093b6
RS
8152 last = 0;
8153 }
8154 }
2bede729 8155 }
fed3cef0 8156
3e4093b6 8157 designator_depth++;
b06df647 8158 designator_erroneous = 0;
3e4093b6 8159 if (constructor_range_stack || last)
a1e3b3d9 8160 push_range_stack (last, braced_init_obstack);
de520661 8161 }
de520661 8162}
3e4093b6
RS
8163
8164/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 8165
de520661 8166void
ea58ef42
MP
8167set_init_label (location_t loc, tree fieldname,
8168 struct obstack *braced_init_obstack)
de520661 8169{
0fb96aa4 8170 tree field;
94ba5069 8171
ea58ef42 8172 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
8173 return;
8174
b06df647 8175 designator_erroneous = 1;
3e4093b6 8176
3e636daf 8177 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
94ba5069 8178 {
ea58ef42 8179 error_init (loc, "field name not in record or union initializer");
3e4093b6 8180 return;
94ba5069
RS
8181 }
8182
0fb96aa4 8183 field = lookup_field (constructor_type, fieldname);
8b6a5902 8184
0fb96aa4 8185 if (field == 0)
9babc352 8186 error_at (loc, "unknown field %qE specified in initializer", fieldname);
3e4093b6 8187 else
0fb96aa4
JM
8188 do
8189 {
8190 constructor_fields = TREE_VALUE (field);
8191 designator_depth++;
8192 designator_erroneous = 0;
8193 if (constructor_range_stack)
8194 push_range_stack (NULL_TREE, braced_init_obstack);
8195 field = TREE_CHAIN (field);
8196 if (field)
8197 {
ea58ef42 8198 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
8199 return;
8200 }
8201 }
8202 while (field != NULL_TREE);
3e4093b6
RS
8203}
8204\f
8205/* Add a new initializer to the tree of pending initializers. PURPOSE
8206 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
8207 VALUE is the value of that index or field. If ORIGTYPE is not
8208 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
8209
8210 IMPLICIT is true if value comes from pop_init_level (1),
8211 the new initializer has been merged with the existing one
8212 and thus no warnings should be emitted about overriding an
8213 existing initializer. */
de520661 8214
3e4093b6 8215static void
96b40f8d
MP
8216add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8217 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
8218{
8219 struct init_node *p, **q, *r;
8220
8221 q = &constructor_pending_elts;
8222 p = 0;
8223
8224 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8225 {
3e4093b6 8226 while (*q != 0)
91fa3c30 8227 {
3e4093b6
RS
8228 p = *q;
8229 if (tree_int_cst_lt (purpose, p->purpose))
8230 q = &p->left;
8231 else if (tree_int_cst_lt (p->purpose, purpose))
8232 q = &p->right;
8233 else
8234 {
b295aee2
JJ
8235 if (!implicit)
8236 {
8237 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8238 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8239 "initialized field with side-effects "
8240 "overwritten");
b295aee2 8241 else if (warn_override_init)
96b40f8d
MP
8242 warning_init (loc, OPT_Woverride_init,
8243 "initialized field overwritten");
b295aee2 8244 }
3e4093b6 8245 p->value = value;
bbbbb16a 8246 p->origtype = origtype;
3e4093b6
RS
8247 return;
8248 }
91fa3c30 8249 }
de520661 8250 }
3e4093b6 8251 else
de520661 8252 {
3e4093b6 8253 tree bitpos;
400fbf9f 8254
3e4093b6
RS
8255 bitpos = bit_position (purpose);
8256 while (*q != NULL)
8257 {
8258 p = *q;
8259 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8260 q = &p->left;
8261 else if (p->purpose != purpose)
8262 q = &p->right;
8263 else
8264 {
b295aee2
JJ
8265 if (!implicit)
8266 {
8267 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8268 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8269 "initialized field with side-effects "
8270 "overwritten");
b295aee2 8271 else if (warn_override_init)
96b40f8d
MP
8272 warning_init (loc, OPT_Woverride_init,
8273 "initialized field overwritten");
b295aee2 8274 }
3e4093b6 8275 p->value = value;
bbbbb16a 8276 p->origtype = origtype;
3e4093b6
RS
8277 return;
8278 }
8279 }
91fa3c30 8280 }
b71c7f8a 8281
a1e3b3d9
LB
8282 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8283 sizeof (struct init_node));
3e4093b6
RS
8284 r->purpose = purpose;
8285 r->value = value;
bbbbb16a 8286 r->origtype = origtype;
8b6a5902 8287
3e4093b6
RS
8288 *q = r;
8289 r->parent = p;
8290 r->left = 0;
8291 r->right = 0;
8292 r->balance = 0;
b71c7f8a 8293
3e4093b6 8294 while (p)
de520661 8295 {
3e4093b6 8296 struct init_node *s;
665f2503 8297
3e4093b6 8298 if (r == p->left)
2bede729 8299 {
3e4093b6
RS
8300 if (p->balance == 0)
8301 p->balance = -1;
8302 else if (p->balance < 0)
8303 {
8304 if (r->balance < 0)
8305 {
8306 /* L rotation. */
8307 p->left = r->right;
8308 if (p->left)
8309 p->left->parent = p;
8310 r->right = p;
e7b6a0ee 8311
3e4093b6
RS
8312 p->balance = 0;
8313 r->balance = 0;
39bc99c2 8314
3e4093b6
RS
8315 s = p->parent;
8316 p->parent = r;
8317 r->parent = s;
8318 if (s)
8319 {
8320 if (s->left == p)
8321 s->left = r;
8322 else
8323 s->right = r;
8324 }
8325 else
8326 constructor_pending_elts = r;
8327 }
8328 else
8329 {
8330 /* LR rotation. */
8331 struct init_node *t = r->right;
e7b6a0ee 8332
3e4093b6
RS
8333 r->right = t->left;
8334 if (r->right)
8335 r->right->parent = r;
8336 t->left = r;
8337
8338 p->left = t->right;
8339 if (p->left)
8340 p->left->parent = p;
8341 t->right = p;
8342
8343 p->balance = t->balance < 0;
8344 r->balance = -(t->balance > 0);
8345 t->balance = 0;
8346
8347 s = p->parent;
8348 p->parent = t;
8349 r->parent = t;
8350 t->parent = s;
8351 if (s)
8352 {
8353 if (s->left == p)
8354 s->left = t;
8355 else
8356 s->right = t;
8357 }
8358 else
8359 constructor_pending_elts = t;
8360 }
8361 break;
8362 }
8363 else
8364 {
8365 /* p->balance == +1; growth of left side balances the node. */
8366 p->balance = 0;
8367 break;
8368 }
2bede729 8369 }
3e4093b6
RS
8370 else /* r == p->right */
8371 {
8372 if (p->balance == 0)
8373 /* Growth propagation from right side. */
8374 p->balance++;
8375 else if (p->balance > 0)
8376 {
8377 if (r->balance > 0)
8378 {
8379 /* R rotation. */
8380 p->right = r->left;
8381 if (p->right)
8382 p->right->parent = p;
8383 r->left = p;
8384
8385 p->balance = 0;
8386 r->balance = 0;
8387
8388 s = p->parent;
8389 p->parent = r;
8390 r->parent = s;
8391 if (s)
8392 {
8393 if (s->left == p)
8394 s->left = r;
8395 else
8396 s->right = r;
8397 }
8398 else
8399 constructor_pending_elts = r;
8400 }
8401 else /* r->balance == -1 */
8402 {
8403 /* RL rotation */
8404 struct init_node *t = r->left;
8405
8406 r->left = t->right;
8407 if (r->left)
8408 r->left->parent = r;
8409 t->right = r;
8410
8411 p->right = t->left;
8412 if (p->right)
8413 p->right->parent = p;
8414 t->left = p;
8415
8416 r->balance = (t->balance < 0);
8417 p->balance = -(t->balance > 0);
8418 t->balance = 0;
8419
8420 s = p->parent;
8421 p->parent = t;
8422 r->parent = t;
8423 t->parent = s;
8424 if (s)
8425 {
8426 if (s->left == p)
8427 s->left = t;
8428 else
8429 s->right = t;
8430 }
8431 else
8432 constructor_pending_elts = t;
8433 }
8434 break;
8435 }
8436 else
8437 {
8438 /* p->balance == -1; growth of right side balances the node. */
8439 p->balance = 0;
8440 break;
8441 }
8442 }
8443
8444 r = p;
8445 p = p->parent;
8446 }
8447}
8448
8449/* Build AVL tree from a sorted chain. */
8450
8451static void
a1e3b3d9 8452set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8453{
4038c495
GB
8454 unsigned HOST_WIDE_INT ix;
8455 tree index, value;
3e4093b6
RS
8456
8457 if (TREE_CODE (constructor_type) != RECORD_TYPE
8458 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8459 return;
8460
4038c495 8461 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8462 add_pending_init (input_location, index, value, NULL_TREE, true,
8463 braced_init_obstack);
9771b263 8464 constructor_elements = NULL;
3e4093b6
RS
8465 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8466 {
8467 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8468 /* Skip any nameless bit fields at the beginning. */
8469 while (constructor_unfilled_fields != 0
8470 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8471 && DECL_NAME (constructor_unfilled_fields) == 0)
8472 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8473
de520661 8474 }
3e4093b6 8475 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8476 {
3e4093b6
RS
8477 if (TYPE_DOMAIN (constructor_type))
8478 constructor_unfilled_index
8479 = convert (bitsizetype,
8480 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8481 else
8482 constructor_unfilled_index = bitsize_zero_node;
de520661 8483 }
3e4093b6 8484 constructor_incremental = 0;
de520661 8485}
400fbf9f 8486
3e4093b6 8487/* Build AVL tree from a string constant. */
de520661 8488
3e4093b6 8489static void
a1e3b3d9
LB
8490set_nonincremental_init_from_string (tree str,
8491 struct obstack * braced_init_obstack)
de520661 8492{
3e4093b6
RS
8493 tree value, purpose, type;
8494 HOST_WIDE_INT val[2];
8495 const char *p, *end;
8496 int byte, wchar_bytes, charwidth, bitpos;
de520661 8497
366de0ce 8498 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8499
c466b2cd 8500 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8501 charwidth = TYPE_PRECISION (char_type_node);
8502 type = TREE_TYPE (constructor_type);
8503 p = TREE_STRING_POINTER (str);
8504 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8505
3e4093b6 8506 for (purpose = bitsize_zero_node;
8824edff
JJ
8507 p < end
8508 && !(constructor_max_index
8509 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8510 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8511 {
3e4093b6 8512 if (wchar_bytes == 1)
ffc5c6a9 8513 {
807e902e
KZ
8514 val[0] = (unsigned char) *p++;
8515 val[1] = 0;
ffc5c6a9
RH
8516 }
8517 else
3e4093b6 8518 {
3e4093b6 8519 val[1] = 0;
807e902e 8520 val[0] = 0;
3e4093b6
RS
8521 for (byte = 0; byte < wchar_bytes; byte++)
8522 {
8523 if (BYTES_BIG_ENDIAN)
8524 bitpos = (wchar_bytes - byte - 1) * charwidth;
8525 else
8526 bitpos = byte * charwidth;
807e902e 8527 val[bitpos % HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8528 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8529 << (bitpos % HOST_BITS_PER_WIDE_INT);
8530 }
8531 }
584ef5fe 8532
8df83eae 8533 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8534 {
8535 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8536 if (bitpos < HOST_BITS_PER_WIDE_INT)
8537 {
807e902e 8538 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
3e4093b6 8539 {
aa256c4a 8540 val[0] |= HOST_WIDE_INT_M1U << bitpos;
807e902e 8541 val[1] = -1;
3e4093b6
RS
8542 }
8543 }
8544 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8545 {
807e902e
KZ
8546 if (val[0] < 0)
8547 val[1] = -1;
3e4093b6 8548 }
807e902e 8549 else if (val[1] & (((HOST_WIDE_INT) 1)
3e4093b6 8550 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
aa256c4a 8551 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
3e4093b6 8552 }
ffc5c6a9 8553
807e902e
KZ
8554 value = wide_int_to_tree (type,
8555 wide_int::from_array (val, 2,
8556 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8557 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8558 braced_init_obstack);
9dfcc8db
BH
8559 }
8560
3e4093b6
RS
8561 constructor_incremental = 0;
8562}
de520661 8563
3e4093b6
RS
8564/* Return value of FIELD in pending initializer or zero if the field was
8565 not initialized yet. */
8566
8567static tree
a1e3b3d9 8568find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8569{
8570 struct init_node *p;
8571
8572 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8573 {
3e4093b6
RS
8574 if (constructor_incremental
8575 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8576 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8577
8578 p = constructor_pending_elts;
8579 while (p)
19d76e60 8580 {
3e4093b6
RS
8581 if (tree_int_cst_lt (field, p->purpose))
8582 p = p->left;
8583 else if (tree_int_cst_lt (p->purpose, field))
8584 p = p->right;
8585 else
8586 return p->value;
19d76e60 8587 }
19d76e60 8588 }
3e4093b6 8589 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8590 {
3e4093b6 8591 tree bitpos = bit_position (field);
de520661 8592
3e4093b6
RS
8593 if (constructor_incremental
8594 && (!constructor_unfilled_fields
8595 || tree_int_cst_lt (bitpos,
8596 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8597 set_nonincremental_init (braced_init_obstack);
de520661 8598
3e4093b6
RS
8599 p = constructor_pending_elts;
8600 while (p)
8601 {
8602 if (field == p->purpose)
8603 return p->value;
8604 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8605 p = p->left;
8606 else
8607 p = p->right;
8608 }
8609 }
8610 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8611 {
9771b263
DN
8612 if (!vec_safe_is_empty (constructor_elements)
8613 && (constructor_elements->last ().index == field))
8614 return constructor_elements->last ().value;
de520661 8615 }
3e4093b6 8616 return 0;
de520661
RS
8617}
8618
3e4093b6
RS
8619/* "Output" the next constructor element.
8620 At top level, really output it to assembler code now.
8621 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8622 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8623 TYPE is the data type that the containing data type wants here.
8624 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8625 If VALUE is a string constant, STRICT_STRING is true if it is
8626 unparenthesized or we should not warn here for it being parenthesized.
8627 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8628
3e4093b6
RS
8629 PENDING if non-nil means output pending elements that belong
8630 right after this element. (PENDING is normally 1;
b295aee2
JJ
8631 it is 0 while outputting pending elements, to avoid recursion.)
8632
8633 IMPLICIT is true if value comes from pop_init_level (1),
8634 the new initializer has been merged with the existing one
8635 and thus no warnings should be emitted about overriding an
8636 existing initializer. */
8b6a5902 8637
3e4093b6 8638static void
34cf811f
MP
8639output_init_element (location_t loc, tree value, tree origtype,
8640 bool strict_string, tree type, tree field, int pending,
8641 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8642{
8ce94e44 8643 tree semantic_type = NULL_TREE;
928c19bb
JM
8644 bool maybe_const = true;
8645 bool npc;
4038c495 8646
0a880880 8647 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8648 {
3e4093b6
RS
8649 constructor_erroneous = 1;
8650 return;
8b6a5902 8651 }
46bdb9cf
JM
8652 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8653 && (TREE_CODE (value) == STRING_CST
8654 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8655 && !(TREE_CODE (value) == STRING_CST
8656 && TREE_CODE (type) == ARRAY_TYPE
8657 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8658 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8659 TYPE_MAIN_VARIANT (type)))
c2255bc4 8660 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8661
3e4093b6 8662 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8663 && require_constant_value && pending)
8b6a5902 8664 {
3e4093b6
RS
8665 /* As an extension, allow initializing objects with static storage
8666 duration with compound literals (which are then treated just as
8667 the brace enclosed list they contain). */
4435bb92
MP
8668 if (flag_isoc99)
8669 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8670 "constant");
3e4093b6
RS
8671 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8672 value = DECL_INITIAL (decl);
8b6a5902
JJ
8673 }
8674
928c19bb 8675 npc = null_pointer_constant_p (value);
8ce94e44
JM
8676 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8677 {
8678 semantic_type = TREE_TYPE (value);
8679 value = TREE_OPERAND (value, 0);
8680 }
928c19bb
JM
8681 value = c_fully_fold (value, require_constant_value, &maybe_const);
8682
3e4093b6
RS
8683 if (value == error_mark_node)
8684 constructor_erroneous = 1;
8685 else if (!TREE_CONSTANT (value))
8686 constructor_constant = 0;
ee45a32d
EB
8687 else if (!initializer_constant_valid_p (value,
8688 TREE_TYPE (value),
8689 AGGREGATE_TYPE_P (constructor_type)
8690 && TYPE_REVERSE_STORAGE_ORDER
8691 (constructor_type))
3e636daf 8692 || (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6
RS
8693 && DECL_C_BIT_FIELD (field)
8694 && TREE_CODE (value) != INTEGER_CST))
8695 constructor_simple = 0;
928c19bb
JM
8696 if (!maybe_const)
8697 constructor_nonconst = 1;
3e4093b6 8698
116df786 8699 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8700 {
116df786
RH
8701 if (require_constant_value)
8702 {
ea58ef42 8703 error_init (loc, "initializer element is not constant");
116df786
RH
8704 value = error_mark_node;
8705 }
8706 else if (require_constant_elements)
8337d1db 8707 pedwarn (loc, OPT_Wpedantic,
509c9d60 8708 "initializer element is not computable at load time");
8b6a5902 8709 }
928c19bb
JM
8710 else if (!maybe_const
8711 && (require_constant_value || require_constant_elements))
8337d1db 8712 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8713 "initializer element is not a constant expression");
3e4093b6 8714
81f40b79
ILT
8715 /* Issue -Wc++-compat warnings about initializing a bitfield with
8716 enum type. */
8717 if (warn_cxx_compat
8718 && field != NULL_TREE
8719 && TREE_CODE (field) == FIELD_DECL
8720 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8721 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8722 != TYPE_MAIN_VARIANT (type))
8723 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8724 {
8725 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8726 if (checktype != error_mark_node
8727 && (TYPE_MAIN_VARIANT (checktype)
8728 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8729 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8730 "enum conversion in initialization is invalid in C++");
8731 }
8732
3e4093b6
RS
8733 /* If this field is empty (and not at the end of structure),
8734 don't do anything other than checking the initializer. */
8735 if (field
8736 && (TREE_TYPE (field) == error_mark_node
8737 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8738 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8739 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8740 || DECL_CHAIN (field)))))
3e4093b6
RS
8741 return;
8742
8ce94e44
JM
8743 if (semantic_type)
8744 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8745 value = digest_init (loc, type, value, origtype, npc, strict_string,
8746 require_constant_value);
3e4093b6 8747 if (value == error_mark_node)
8b6a5902 8748 {
3e4093b6
RS
8749 constructor_erroneous = 1;
8750 return;
8b6a5902 8751 }
928c19bb
JM
8752 if (require_constant_value || require_constant_elements)
8753 constant_expression_warning (value);
8b6a5902 8754
3e4093b6
RS
8755 /* If this element doesn't come next in sequence,
8756 put it on constructor_pending_elts. */
8757 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8758 && (!constructor_incremental
8759 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8760 {
3e4093b6
RS
8761 if (constructor_incremental
8762 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8763 set_nonincremental_init (braced_init_obstack);
3e4093b6 8764
96b40f8d 8765 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8766 braced_init_obstack);
3e4093b6 8767 return;
8b6a5902 8768 }
3e4093b6
RS
8769 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8770 && (!constructor_incremental
8771 || field != constructor_unfilled_fields))
8b6a5902 8772 {
3e4093b6
RS
8773 /* We do this for records but not for unions. In a union,
8774 no matter which field is specified, it can be initialized
8775 right away since it starts at the beginning of the union. */
8776 if (constructor_incremental)
8777 {
8778 if (!constructor_unfilled_fields)
a1e3b3d9 8779 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8780 else
8781 {
8782 tree bitpos, unfillpos;
8783
8784 bitpos = bit_position (field);
8785 unfillpos = bit_position (constructor_unfilled_fields);
8786
8787 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8788 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8789 }
8790 }
8791
96b40f8d 8792 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8793 braced_init_obstack);
3e4093b6 8794 return;
8b6a5902 8795 }
3e4093b6 8796 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8797 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8798 {
b295aee2
JJ
8799 if (!implicit)
8800 {
9771b263 8801 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
755e528f 8802 warning_init (loc, OPT_Woverride_init_side_effects,
b295aee2
JJ
8803 "initialized field with side-effects overwritten");
8804 else if (warn_override_init)
96b40f8d
MP
8805 warning_init (loc, OPT_Woverride_init,
8806 "initialized field overwritten");
b295aee2 8807 }
8b6a5902 8808
3e4093b6 8809 /* We can have just one union field set. */
9771b263 8810 constructor_elements = NULL;
3e4093b6 8811 }
8b6a5902 8812
3e4093b6
RS
8813 /* Otherwise, output this element either to
8814 constructor_elements or to the assembler file. */
8b6a5902 8815
f32682ca 8816 constructor_elt celt = {field, value};
9771b263 8817 vec_safe_push (constructor_elements, celt);
8b6a5902 8818
3e4093b6
RS
8819 /* Advance the variable that indicates sequential elements output. */
8820 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8821 constructor_unfilled_index
db3927fb
AH
8822 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8823 bitsize_one_node);
3e4093b6
RS
8824 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8825 {
8826 constructor_unfilled_fields
910ad8de 8827 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8828
3e4093b6
RS
8829 /* Skip any nameless bit fields. */
8830 while (constructor_unfilled_fields != 0
8831 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8832 && DECL_NAME (constructor_unfilled_fields) == 0)
8833 constructor_unfilled_fields =
910ad8de 8834 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8835 }
8836 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8837 constructor_unfilled_fields = 0;
de520661 8838
3e4093b6
RS
8839 /* Now output any pending elements which have become next. */
8840 if (pending)
a1e3b3d9 8841 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8842}
8b6a5902 8843
3e4093b6
RS
8844/* Output any pending elements which have become next.
8845 As we output elements, constructor_unfilled_{fields,index}
8846 advances, which may cause other elements to become next;
8847 if so, they too are output.
8b6a5902 8848
3e4093b6
RS
8849 If ALL is 0, we return when there are
8850 no more pending elements to output now.
665f2503 8851
3e4093b6
RS
8852 If ALL is 1, we output space as necessary so that
8853 we can output all the pending elements. */
3e4093b6 8854static void
a1e3b3d9 8855output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8856{
8857 struct init_node *elt = constructor_pending_elts;
8858 tree next;
de520661 8859
3e4093b6
RS
8860 retry:
8861
ba228239 8862 /* Look through the whole pending tree.
3e4093b6
RS
8863 If we find an element that should be output now,
8864 output it. Otherwise, set NEXT to the element
8865 that comes first among those still pending. */
8866
8867 next = 0;
8868 while (elt)
8869 {
8870 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8871 {
3e4093b6
RS
8872 if (tree_int_cst_equal (elt->purpose,
8873 constructor_unfilled_index))
34cf811f
MP
8874 output_init_element (input_location, elt->value, elt->origtype,
8875 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8876 constructor_unfilled_index, 0, false,
8877 braced_init_obstack);
3e4093b6
RS
8878 else if (tree_int_cst_lt (constructor_unfilled_index,
8879 elt->purpose))
8b6a5902 8880 {
3e4093b6
RS
8881 /* Advance to the next smaller node. */
8882 if (elt->left)
8883 elt = elt->left;
8884 else
8885 {
8886 /* We have reached the smallest node bigger than the
8887 current unfilled index. Fill the space first. */
8888 next = elt->purpose;
8889 break;
8890 }
8b6a5902 8891 }
ce662d4c
JJ
8892 else
8893 {
3e4093b6
RS
8894 /* Advance to the next bigger node. */
8895 if (elt->right)
8896 elt = elt->right;
8897 else
ce662d4c 8898 {
3e4093b6
RS
8899 /* We have reached the biggest node in a subtree. Find
8900 the parent of it, which is the next bigger node. */
8901 while (elt->parent && elt->parent->right == elt)
8902 elt = elt->parent;
8903 elt = elt->parent;
8904 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8905 elt->purpose))
8906 {
8907 next = elt->purpose;
8908 break;
8909 }
ce662d4c
JJ
8910 }
8911 }
8b6a5902 8912 }
3e636daf 8913 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
8914 {
8915 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8916
3e4093b6
RS
8917 /* If the current record is complete we are done. */
8918 if (constructor_unfilled_fields == 0)
8919 break;
de520661 8920
3e4093b6
RS
8921 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8922 elt_bitpos = bit_position (elt->purpose);
8923 /* We can't compare fields here because there might be empty
8924 fields in between. */
8925 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8926 {
8927 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8928 output_init_element (input_location, elt->value, elt->origtype,
8929 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8930 elt->purpose, 0, false,
8931 braced_init_obstack);
3e4093b6
RS
8932 }
8933 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8934 {
8935 /* Advance to the next smaller node. */
8936 if (elt->left)
8937 elt = elt->left;
8938 else
8939 {
8940 /* We have reached the smallest node bigger than the
8941 current unfilled field. Fill the space first. */
8942 next = elt->purpose;
8943 break;
8944 }
8945 }
8946 else
8947 {
8948 /* Advance to the next bigger node. */
8949 if (elt->right)
8950 elt = elt->right;
8951 else
8952 {
8953 /* We have reached the biggest node in a subtree. Find
8954 the parent of it, which is the next bigger node. */
8955 while (elt->parent && elt->parent->right == elt)
8956 elt = elt->parent;
8957 elt = elt->parent;
8958 if (elt
8959 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8960 bit_position (elt->purpose))))
8961 {
8962 next = elt->purpose;
8963 break;
8964 }
8965 }
8966 }
8967 }
8968 }
de520661 8969
3e4093b6
RS
8970 /* Ordinarily return, but not if we want to output all
8971 and there are elements left. */
3f75a254 8972 if (!(all && next != 0))
e5cfb88f
RK
8973 return;
8974
3e4093b6
RS
8975 /* If it's not incremental, just skip over the gap, so that after
8976 jumping to retry we will output the next successive element. */
3e636daf 8977 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
8978 constructor_unfilled_fields = next;
8979 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8980 constructor_unfilled_index = next;
de520661 8981
3e4093b6
RS
8982 /* ELT now points to the node in the pending tree with the next
8983 initializer to output. */
8984 goto retry;
de520661
RS
8985}
8986\f
3e4093b6
RS
8987/* Add one non-braced element to the current constructor level.
8988 This adjusts the current position within the constructor's type.
8989 This may also start or terminate implicit levels
8990 to handle a partly-braced initializer.
e5e809f4 8991
3e4093b6 8992 Once this has found the correct level for the new element,
b295aee2
JJ
8993 it calls output_init_element.
8994
8995 IMPLICIT is true if value comes from pop_init_level (1),
8996 the new initializer has been merged with the existing one
8997 and thus no warnings should be emitted about overriding an
8998 existing initializer. */
3e4093b6
RS
8999
9000void
34cf811f 9001process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 9002 struct obstack * braced_init_obstack)
e5e809f4 9003{
916c5919
JM
9004 tree orig_value = value.value;
9005 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
9006 bool strict_string = value.original_code == STRING_CST;
340baef7 9007 bool was_designated = designator_depth != 0;
e5e809f4 9008
3e4093b6 9009 designator_depth = 0;
b06df647 9010 designator_erroneous = 0;
e5e809f4 9011
9bac5cbb
G
9012 if (!implicit && value.value && !integer_zerop (value.value))
9013 constructor_zeroinit = 0;
9014
3e4093b6
RS
9015 /* Handle superfluous braces around string cst as in
9016 char x[] = {"foo"}; */
9017 if (string_flag
9018 && constructor_type
340baef7 9019 && !was_designated
3e4093b6 9020 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 9021 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 9022 && integer_zerop (constructor_unfilled_index))
e5e809f4 9023 {
916c5919 9024 if (constructor_stack->replacement_value.value)
ea58ef42 9025 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
9026 constructor_stack->replacement_value = value;
9027 return;
e5e809f4 9028 }
8b6a5902 9029
916c5919 9030 if (constructor_stack->replacement_value.value != 0)
3e4093b6 9031 {
ea58ef42 9032 error_init (loc, "excess elements in struct initializer");
3e4093b6 9033 return;
e5e809f4
JL
9034 }
9035
3e4093b6
RS
9036 /* Ignore elements of a brace group if it is entirely superfluous
9037 and has already been diagnosed. */
9038 if (constructor_type == 0)
9039 return;
e5e809f4 9040
976d5a22
TT
9041 if (!implicit && warn_designated_init && !was_designated
9042 && TREE_CODE (constructor_type) == RECORD_TYPE
9043 && lookup_attribute ("designated_init",
9044 TYPE_ATTRIBUTES (constructor_type)))
9045 warning_init (loc,
9046 OPT_Wdesignated_init,
9047 "positional initialization of field "
9048 "in %<struct%> declared with %<designated_init%> attribute");
9049
3e4093b6
RS
9050 /* If we've exhausted any levels that didn't have braces,
9051 pop them now. */
9052 while (constructor_stack->implicit)
9053 {
3e636daf 9054 if (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 9055 && constructor_fields == 0)
ea58ef42
MP
9056 process_init_element (loc,
9057 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 9058 true, braced_init_obstack);
53650abe 9059 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
31521951 9060 || VECTOR_TYPE_P (constructor_type))
8824edff
JJ
9061 && constructor_max_index
9062 && tree_int_cst_lt (constructor_max_index,
9063 constructor_index))
ea58ef42
MP
9064 process_init_element (loc,
9065 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 9066 true, braced_init_obstack);
3e4093b6
RS
9067 else
9068 break;
9069 }
e5e809f4 9070
3e4093b6
RS
9071 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9072 if (constructor_range_stack)
e5e809f4 9073 {
3e4093b6
RS
9074 /* If value is a compound literal and we'll be just using its
9075 content, don't put it into a SAVE_EXPR. */
916c5919 9076 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
c3e38a03 9077 || !require_constant_value)
8ce94e44
JM
9078 {
9079 tree semantic_type = NULL_TREE;
9080 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9081 {
9082 semantic_type = TREE_TYPE (value.value);
9083 value.value = TREE_OPERAND (value.value, 0);
9084 }
9085 value.value = c_save_expr (value.value);
9086 if (semantic_type)
9087 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9088 value.value);
9089 }
3e4093b6 9090 }
e5e809f4 9091
3e4093b6
RS
9092 while (1)
9093 {
9094 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 9095 {
3e4093b6
RS
9096 tree fieldtype;
9097 enum tree_code fieldcode;
e5e809f4 9098
3e4093b6
RS
9099 if (constructor_fields == 0)
9100 {
ea58ef42 9101 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
9102 break;
9103 }
e5e809f4 9104
3e4093b6
RS
9105 fieldtype = TREE_TYPE (constructor_fields);
9106 if (fieldtype != error_mark_node)
9107 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9108 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9109
3e4093b6
RS
9110 /* Error for non-static initialization of a flexible array member. */
9111 if (fieldcode == ARRAY_TYPE
9112 && !require_constant_value
9113 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 9114 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 9115 {
ea58ef42
MP
9116 error_init (loc, "non-static initialization of a flexible "
9117 "array member");
3e4093b6
RS
9118 break;
9119 }
e5e809f4 9120
2cc901dc
MP
9121 /* Error for initialization of a flexible array member with
9122 a string constant if the structure is in an array. E.g.:
9123 struct S { int x; char y[]; };
9124 struct S s[] = { { 1, "foo" } };
9125 is invalid. */
9126 if (string_flag
9127 && fieldcode == ARRAY_TYPE
9128 && constructor_depth > 1
9129 && TYPE_SIZE (fieldtype) == NULL_TREE
9130 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9131 {
9132 bool in_array_p = false;
9133 for (struct constructor_stack *p = constructor_stack;
9134 p && p->type; p = p->next)
9135 if (TREE_CODE (p->type) == ARRAY_TYPE)
9136 {
9137 in_array_p = true;
9138 break;
9139 }
9140 if (in_array_p)
9141 {
9142 error_init (loc, "initialization of flexible array "
9143 "member in a nested context");
9144 break;
9145 }
9146 }
9147
3e4093b6 9148 /* Accept a string constant to initialize a subarray. */
916c5919 9149 if (value.value != 0
3e4093b6 9150 && fieldcode == ARRAY_TYPE
197463ae 9151 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9152 && string_flag)
916c5919 9153 value.value = orig_value;
3e4093b6
RS
9154 /* Otherwise, if we have come to a subaggregate,
9155 and we don't have an element of its type, push into it. */
0953878d 9156 else if (value.value != 0
916c5919
JM
9157 && value.value != error_mark_node
9158 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9159 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9160 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9161 {
ea58ef42 9162 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9163 continue;
9164 }
e5e809f4 9165
916c5919 9166 if (value.value)
3e4093b6
RS
9167 {
9168 push_member_name (constructor_fields);
34cf811f 9169 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9170 strict_string, fieldtype,
a1e3b3d9
LB
9171 constructor_fields, 1, implicit,
9172 braced_init_obstack);
3e4093b6 9173 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9174 }
9175 else
3e4093b6
RS
9176 /* Do the bookkeeping for an element that was
9177 directly output as a constructor. */
e5e809f4 9178 {
3e4093b6
RS
9179 /* For a record, keep track of end position of last field. */
9180 if (DECL_SIZE (constructor_fields))
c22cacf3 9181 constructor_bit_index
db3927fb
AH
9182 = size_binop_loc (input_location, PLUS_EXPR,
9183 bit_position (constructor_fields),
9184 DECL_SIZE (constructor_fields));
3e4093b6
RS
9185
9186 /* If the current field was the first one not yet written out,
9187 it isn't now, so update. */
9188 if (constructor_unfilled_fields == constructor_fields)
9189 {
910ad8de 9190 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9191 /* Skip any nameless bit fields. */
9192 while (constructor_unfilled_fields != 0
9193 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9194 && DECL_NAME (constructor_unfilled_fields) == 0)
9195 constructor_unfilled_fields =
910ad8de 9196 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 9197 }
e5e809f4 9198 }
3e4093b6 9199
910ad8de 9200 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9201 /* Skip any nameless bit fields at the beginning. */
9202 while (constructor_fields != 0
9203 && DECL_C_BIT_FIELD (constructor_fields)
9204 && DECL_NAME (constructor_fields) == 0)
910ad8de 9205 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9206 }
3e4093b6 9207 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 9208 {
3e4093b6
RS
9209 tree fieldtype;
9210 enum tree_code fieldcode;
e5e809f4 9211
3e4093b6
RS
9212 if (constructor_fields == 0)
9213 {
d033409e 9214 pedwarn_init (loc, 0,
509c9d60 9215 "excess elements in union initializer");
3e4093b6
RS
9216 break;
9217 }
e5e809f4 9218
3e4093b6
RS
9219 fieldtype = TREE_TYPE (constructor_fields);
9220 if (fieldtype != error_mark_node)
9221 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9222 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9223
3e4093b6
RS
9224 /* Warn that traditional C rejects initialization of unions.
9225 We skip the warning if the value is zero. This is done
9226 under the assumption that the zero initializer in user
9227 code appears conditioned on e.g. __STDC__ to avoid
9228 "missing initializer" warnings and relies on default
9229 initialization to zero in the traditional C case.
9230 We also skip the warning if the initializer is designated,
9231 again on the assumption that this must be conditional on
9232 __STDC__ anyway (and we've already complained about the
9233 member-designator already). */
8400e75e 9234 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
9235 && !(value.value && (integer_zerop (value.value)
9236 || real_zerop (value.value))))
3176a0c2
DD
9237 warning (OPT_Wtraditional, "traditional C rejects initialization "
9238 "of unions");
e5e809f4 9239
3e4093b6 9240 /* Accept a string constant to initialize a subarray. */
916c5919 9241 if (value.value != 0
3e4093b6 9242 && fieldcode == ARRAY_TYPE
197463ae 9243 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9244 && string_flag)
916c5919 9245 value.value = orig_value;
3e4093b6
RS
9246 /* Otherwise, if we have come to a subaggregate,
9247 and we don't have an element of its type, push into it. */
0953878d 9248 else if (value.value != 0
916c5919
JM
9249 && value.value != error_mark_node
9250 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9251 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9252 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9253 {
ea58ef42 9254 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9255 continue;
9256 }
e5e809f4 9257
916c5919 9258 if (value.value)
3e4093b6
RS
9259 {
9260 push_member_name (constructor_fields);
34cf811f 9261 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9262 strict_string, fieldtype,
a1e3b3d9
LB
9263 constructor_fields, 1, implicit,
9264 braced_init_obstack);
3e4093b6 9265 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9266 }
9267 else
3e4093b6
RS
9268 /* Do the bookkeeping for an element that was
9269 directly output as a constructor. */
e5e809f4 9270 {
3e4093b6 9271 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 9272 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9273 }
e5e809f4 9274
3e4093b6
RS
9275 constructor_fields = 0;
9276 }
9277 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9278 {
9279 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9280 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 9281
3e4093b6 9282 /* Accept a string constant to initialize a subarray. */
916c5919 9283 if (value.value != 0
3e4093b6 9284 && eltcode == ARRAY_TYPE
197463ae 9285 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 9286 && string_flag)
916c5919 9287 value.value = orig_value;
3e4093b6
RS
9288 /* Otherwise, if we have come to a subaggregate,
9289 and we don't have an element of its type, push into it. */
0953878d 9290 else if (value.value != 0
916c5919
JM
9291 && value.value != error_mark_node
9292 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9293 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9294 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9295 {
ea58ef42 9296 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9297 continue;
9298 }
8b6a5902 9299
3e4093b6
RS
9300 if (constructor_max_index != 0
9301 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9302 || integer_all_onesp (constructor_max_index)))
9303 {
d033409e 9304 pedwarn_init (loc, 0,
509c9d60 9305 "excess elements in array initializer");
3e4093b6
RS
9306 break;
9307 }
8b6a5902 9308
3e4093b6 9309 /* Now output the actual element. */
916c5919 9310 if (value.value)
3e4093b6 9311 {
ae7e9ddd 9312 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9313 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9314 strict_string, elttype,
a1e3b3d9
LB
9315 constructor_index, 1, implicit,
9316 braced_init_obstack);
3e4093b6
RS
9317 RESTORE_SPELLING_DEPTH (constructor_depth);
9318 }
2f6e4e97 9319
3e4093b6 9320 constructor_index
db3927fb
AH
9321 = size_binop_loc (input_location, PLUS_EXPR,
9322 constructor_index, bitsize_one_node);
8b6a5902 9323
916c5919 9324 if (!value.value)
3e4093b6
RS
9325 /* If we are doing the bookkeeping for an element that was
9326 directly output as a constructor, we must update
9327 constructor_unfilled_index. */
9328 constructor_unfilled_index = constructor_index;
9329 }
31521951 9330 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
9331 {
9332 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9333
c22cacf3
MS
9334 /* Do a basic check of initializer size. Note that vectors
9335 always have a fixed size derived from their type. */
3e4093b6
RS
9336 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9337 {
d033409e 9338 pedwarn_init (loc, 0,
509c9d60 9339 "excess elements in vector initializer");
3e4093b6
RS
9340 break;
9341 }
8b6a5902 9342
3e4093b6 9343 /* Now output the actual element. */
916c5919 9344 if (value.value)
53650abe
AP
9345 {
9346 if (TREE_CODE (value.value) == VECTOR_CST)
9347 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9348 output_init_element (loc, value.value, value.original_type,
53650abe 9349 strict_string, elttype,
a1e3b3d9
LB
9350 constructor_index, 1, implicit,
9351 braced_init_obstack);
53650abe 9352 }
8b6a5902 9353
3e4093b6 9354 constructor_index
db3927fb
AH
9355 = size_binop_loc (input_location,
9356 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9357
916c5919 9358 if (!value.value)
3e4093b6
RS
9359 /* If we are doing the bookkeeping for an element that was
9360 directly output as a constructor, we must update
9361 constructor_unfilled_index. */
9362 constructor_unfilled_index = constructor_index;
9363 }
8b6a5902 9364
3e4093b6
RS
9365 /* Handle the sole element allowed in a braced initializer
9366 for a scalar variable. */
b4519d39
SB
9367 else if (constructor_type != error_mark_node
9368 && constructor_fields == 0)
8b6a5902 9369 {
d033409e 9370 pedwarn_init (loc, 0,
509c9d60 9371 "excess elements in scalar initializer");
3e4093b6 9372 break;
8b6a5902
JJ
9373 }
9374 else
9375 {
916c5919 9376 if (value.value)
34cf811f 9377 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9378 strict_string, constructor_type,
a1e3b3d9
LB
9379 NULL_TREE, 1, implicit,
9380 braced_init_obstack);
3e4093b6 9381 constructor_fields = 0;
8b6a5902
JJ
9382 }
9383
3e4093b6
RS
9384 /* Handle range initializers either at this level or anywhere higher
9385 in the designator stack. */
9386 if (constructor_range_stack)
8b6a5902 9387 {
3e4093b6
RS
9388 struct constructor_range_stack *p, *range_stack;
9389 int finish = 0;
9390
9391 range_stack = constructor_range_stack;
9392 constructor_range_stack = 0;
9393 while (constructor_stack != range_stack->stack)
8b6a5902 9394 {
366de0ce 9395 gcc_assert (constructor_stack->implicit);
34cf811f 9396 process_init_element (loc,
ea58ef42
MP
9397 pop_init_level (loc, 1,
9398 braced_init_obstack),
a1e3b3d9 9399 true, braced_init_obstack);
8b6a5902 9400 }
3e4093b6
RS
9401 for (p = range_stack;
9402 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9403 p = p->prev)
8b6a5902 9404 {
366de0ce 9405 gcc_assert (constructor_stack->implicit);
34cf811f 9406 process_init_element (loc,
ea58ef42
MP
9407 pop_init_level (loc, 1,
9408 braced_init_obstack),
a1e3b3d9 9409 true, braced_init_obstack);
8b6a5902 9410 }
3e4093b6 9411
db3927fb
AH
9412 p->index = size_binop_loc (input_location,
9413 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9414 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9415 finish = 1;
9416
9417 while (1)
9418 {
9419 constructor_index = p->index;
9420 constructor_fields = p->fields;
9421 if (finish && p->range_end && p->index == p->range_start)
9422 {
9423 finish = 0;
9424 p->prev = 0;
9425 }
9426 p = p->next;
9427 if (!p)
9428 break;
16595a1f 9429 finish_implicit_inits (loc, braced_init_obstack);
ea58ef42 9430 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9431 p->stack = constructor_stack;
9432 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9433 p->index = p->range_start;
9434 }
9435
9436 if (!finish)
9437 constructor_range_stack = range_stack;
9438 continue;
8b6a5902
JJ
9439 }
9440
3e4093b6 9441 break;
8b6a5902
JJ
9442 }
9443
3e4093b6
RS
9444 constructor_range_stack = 0;
9445}
9446\f
9f0e2d86
ZW
9447/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9448 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9449 an ASM_EXPR node). */
3e4093b6 9450tree
9f0e2d86 9451build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9452{
6de9cd9a
DN
9453 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9454 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9455 return add_stmt (args);
8b6a5902
JJ
9456}
9457
9f0e2d86
ZW
9458/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9459 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9460 SIMPLE indicates whether there was anything at all after the
9461 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9462 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9463tree
c2255bc4 9464build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9465 tree clobbers, tree labels, bool simple)
e5e809f4 9466{
3e4093b6 9467 tree tail;
9f0e2d86 9468 tree args;
6de9cd9a
DN
9469 int i;
9470 const char *constraint;
74f0c611 9471 const char **oconstraints;
6de9cd9a 9472 bool allows_mem, allows_reg, is_inout;
74f0c611 9473 int ninputs, noutputs;
6de9cd9a
DN
9474
9475 ninputs = list_length (inputs);
9476 noutputs = list_length (outputs);
74f0c611
RH
9477 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9478
1c384bf1 9479 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9480
6de9cd9a
DN
9481 /* Remove output conversions that change the type but not the mode. */
9482 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9483 {
3e4093b6 9484 tree output = TREE_VALUE (tail);
74f0c611 9485
eadd3d0d
JJ
9486 output = c_fully_fold (output, false, NULL);
9487
74f0c611
RH
9488 /* ??? Really, this should not be here. Users should be using a
9489 proper lvalue, dammit. But there's a long history of using casts
9490 in the output operands. In cases like longlong.h, this becomes a
9491 primitive form of typechecking -- if the cast can be removed, then
9492 the output operand had a type of the proper width; otherwise we'll
9493 get an error. Gross, but ... */
3e4093b6 9494 STRIP_NOPS (output);
74f0c611 9495
7bd11157 9496 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9497 output = error_mark_node;
8b6a5902 9498
5544530a
PB
9499 if (output != error_mark_node
9500 && (TREE_READONLY (output)
9501 || TYPE_READONLY (TREE_TYPE (output))
3e636daf 9502 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
5544530a 9503 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9504 readonly_error (loc, output, lv_asm);
5544530a 9505
6de9cd9a 9506 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9507 oconstraints[i] = constraint;
9508
9509 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9510 &allows_mem, &allows_reg, &is_inout))
9511 {
9512 /* If the operand is going to end up in memory,
9513 mark it addressable. */
9514 if (!allows_reg && !c_mark_addressable (output))
9515 output = error_mark_node;
bae5cddf
JJ
9516 if (!(!allows_reg && allows_mem)
9517 && output != error_mark_node
9518 && VOID_TYPE_P (TREE_TYPE (output)))
9519 {
9520 error_at (loc, "invalid use of void expression");
9521 output = error_mark_node;
9522 }
74f0c611
RH
9523 }
9524 else
c22cacf3 9525 output = error_mark_node;
3e4093b6 9526
74f0c611 9527 TREE_VALUE (tail) = output;
8b6a5902 9528 }
3e4093b6 9529
74f0c611
RH
9530 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9531 {
9532 tree input;
9533
9534 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9535 input = TREE_VALUE (tail);
9536
74f0c611
RH
9537 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9538 oconstraints, &allows_mem, &allows_reg))
9539 {
9540 /* If the operand is going to end up in memory,
9541 mark it addressable. */
b4c33883
AP
9542 if (!allows_reg && allows_mem)
9543 {
eadd3d0d
JJ
9544 input = c_fully_fold (input, false, NULL);
9545
b4c33883
AP
9546 /* Strip the nops as we allow this case. FIXME, this really
9547 should be rejected or made deprecated. */
9548 STRIP_NOPS (input);
9549 if (!c_mark_addressable (input))
9550 input = error_mark_node;
bae5cddf 9551 }
eadd3d0d 9552 else
bae5cddf 9553 {
eadd3d0d
JJ
9554 struct c_expr expr;
9555 memset (&expr, 0, sizeof (expr));
9556 expr.value = input;
267bac10 9557 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9558 input = c_fully_fold (expr.value, false, NULL);
9559
9560 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9561 {
9562 error_at (loc, "invalid use of void expression");
9563 input = error_mark_node;
9564 }
bae5cddf 9565 }
74f0c611
RH
9566 }
9567 else
9568 input = error_mark_node;
9569
9570 TREE_VALUE (tail) = input;
9571 }
3e4093b6 9572
1c384bf1
RH
9573 /* ASMs with labels cannot have outputs. This should have been
9574 enforced by the parser. */
9575 gcc_assert (outputs == NULL || labels == NULL);
9576
9577 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9578
5544530a
PB
9579 /* asm statements without outputs, including simple ones, are treated
9580 as volatile. */
9581 ASM_INPUT_P (args) = simple;
9582 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9583
9f0e2d86 9584 return args;
e5e809f4 9585}
3e4093b6 9586\f
c2255bc4
AH
9587/* Generate a goto statement to LABEL. LOC is the location of the
9588 GOTO. */
506e2710
RH
9589
9590tree
c2255bc4 9591c_finish_goto_label (location_t loc, tree label)
506e2710 9592{
e1b7793c 9593 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9594 if (!decl)
9595 return NULL_TREE;
506e2710 9596 TREE_USED (decl) = 1;
c2255bc4
AH
9597 {
9598 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9599 SET_EXPR_LOCATION (t, loc);
9600 return add_stmt (t);
9601 }
506e2710
RH
9602}
9603
c2255bc4
AH
9604/* Generate a computed goto statement to EXPR. LOC is the location of
9605 the GOTO. */
506e2710
RH
9606
9607tree
c2255bc4 9608c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9609{
c2255bc4 9610 tree t;
c1771a20 9611 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9612 expr = c_fully_fold (expr, false, NULL);
506e2710 9613 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9614 t = build1 (GOTO_EXPR, void_type_node, expr);
9615 SET_EXPR_LOCATION (t, loc);
9616 return add_stmt (t);
506e2710
RH
9617}
9618
5088b058 9619/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9620 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9621 the location of the return statement, or the location of the expression,
9622 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9623 is the original type of RETVAL. */
de520661 9624
506e2710 9625tree
c2255bc4 9626c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9627{
0c9b182b
JJ
9628 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9629 bool no_warning = false;
928c19bb 9630 bool npc = false;
36536d79 9631 size_t rank = 0;
3e4093b6 9632
de8ddd5f
MP
9633 /* Use the expansion point to handle cases such as returning NULL
9634 in a function returning void. */
9635 source_location xloc = expansion_point_location_if_in_system_header (loc);
9636
3e4093b6 9637 if (TREE_THIS_VOLATILE (current_function_decl))
de8ddd5f 9638 warning_at (xloc, 0,
c2255bc4 9639 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9640
b72271b9 9641 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9642 {
9643 /* Array notations are allowed in a return statement if it is inside a
9644 built-in array notation reduction function. */
9645 if (!find_rank (loc, retval, retval, false, &rank))
9646 return error_mark_node;
9647 if (rank >= 1)
9648 {
9649 error_at (loc, "array notation expression cannot be used as a "
9650 "return value");
9651 return error_mark_node;
9652 }
9653 }
3af9c5e9 9654 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9655 {
9656 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9657 "allowed");
9658 return error_mark_node;
9659 }
928c19bb
JM
9660 if (retval)
9661 {
8ce94e44 9662 tree semantic_type = NULL_TREE;
928c19bb 9663 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9664 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9665 {
9666 semantic_type = TREE_TYPE (retval);
9667 retval = TREE_OPERAND (retval, 0);
9668 }
928c19bb 9669 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9670 if (semantic_type)
9671 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9672 }
9673
3e4093b6 9674 if (!retval)
de520661 9675 {
3e4093b6
RS
9676 current_function_returns_null = 1;
9677 if ((warn_return_type || flag_isoc99)
9678 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9679 {
94c40e19 9680 bool warned_here;
35aff4fb 9681 if (flag_isoc99)
94c40e19
DM
9682 warned_here = pedwarn
9683 (loc, 0,
9684 "%<return%> with no value, in function returning non-void");
35aff4fb 9685 else
94c40e19
DM
9686 warned_here = warning_at
9687 (loc, OPT_Wreturn_type,
9688 "%<return%> with no value, in function returning non-void");
0c9b182b 9689 no_warning = true;
94c40e19
DM
9690 if (warned_here)
9691 inform (DECL_SOURCE_LOCATION (current_function_decl),
9692 "declared here");
0c9b182b 9693 }
400fbf9f 9694 }
3e4093b6 9695 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9696 {
3e4093b6 9697 current_function_returns_null = 1;
94c40e19 9698 bool warned_here;
2397c575 9699 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
94c40e19
DM
9700 warned_here = pedwarn
9701 (xloc, 0,
9702 "%<return%> with a value, in function returning void");
b8698a0f 9703 else
94c40e19
DM
9704 warned_here = pedwarn
9705 (xloc, OPT_Wpedantic, "ISO C forbids "
9706 "%<return%> with expression, in function returning void");
9707 if (warned_here)
9708 inform (DECL_SOURCE_LOCATION (current_function_decl),
9709 "declared here");
de520661 9710 }
3e4093b6 9711 else
de520661 9712 {
68fca595
MP
9713 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9714 retval, origtype, ic_return,
c2255bc4 9715 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9716 tree res = DECL_RESULT (current_function_decl);
9717 tree inner;
9feb29df 9718 bool save;
3e4093b6
RS
9719
9720 current_function_returns_value = 1;
9721 if (t == error_mark_node)
506e2710 9722 return NULL_TREE;
3e4093b6 9723
9feb29df
JJ
9724 save = in_late_binary_op;
9725 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
9726 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9727 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9728 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9729 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9730 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9feb29df 9731 in_late_binary_op = true;
3e4093b6 9732 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9733 in_late_binary_op = save;
3e4093b6
RS
9734
9735 /* Strip any conversions, additions, and subtractions, and see if
9736 we are returning the address of a local variable. Warn if so. */
9737 while (1)
8b6a5902 9738 {
3e4093b6 9739 switch (TREE_CODE (inner))
8b6a5902 9740 {
849421a3
JJ
9741 CASE_CONVERT:
9742 case NON_LVALUE_EXPR:
3e4093b6 9743 case PLUS_EXPR:
849421a3 9744 case POINTER_PLUS_EXPR:
3e4093b6
RS
9745 inner = TREE_OPERAND (inner, 0);
9746 continue;
9747
9748 case MINUS_EXPR:
9749 /* If the second operand of the MINUS_EXPR has a pointer
9750 type (or is converted from it), this may be valid, so
9751 don't give a warning. */
9752 {
9753 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9754
3f75a254 9755 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9756 && (CONVERT_EXPR_P (op1)
9757 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9758 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9759
3e4093b6
RS
9760 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9761 break;
8b6a5902 9762
3e4093b6
RS
9763 inner = TREE_OPERAND (inner, 0);
9764 continue;
9765 }
400fbf9f 9766
3e4093b6
RS
9767 case ADDR_EXPR:
9768 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9769
6615c446 9770 while (REFERENCE_CLASS_P (inner)
22d03525 9771 && !INDIRECT_REF_P (inner))
3e4093b6 9772 inner = TREE_OPERAND (inner, 0);
8b6a5902 9773
a2f1f4c3 9774 if (DECL_P (inner)
3f75a254
JM
9775 && !DECL_EXTERNAL (inner)
9776 && !TREE_STATIC (inner)
3e4093b6 9777 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9778 {
9779 if (TREE_CODE (inner) == LABEL_DECL)
9780 warning_at (loc, OPT_Wreturn_local_addr,
9781 "function returns address of label");
9782 else
b4dfdc11
MG
9783 {
9784 warning_at (loc, OPT_Wreturn_local_addr,
9785 "function returns address of local variable");
9786 tree zero = build_zero_cst (TREE_TYPE (res));
9787 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9788 }
19fc9faa 9789 }
3e4093b6 9790 break;
8b6a5902 9791
3e4093b6
RS
9792 default:
9793 break;
9794 }
de520661 9795
3e4093b6
RS
9796 break;
9797 }
9798
53fb4de3 9799 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9800 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9801
9802 if (warn_sequence_point)
9803 verify_sequence_points (retval);
de520661 9804 }
8b6a5902 9805
c2255bc4 9806 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9807 TREE_NO_WARNING (ret_stmt) |= no_warning;
9808 return add_stmt (ret_stmt);
de520661 9809}
3e4093b6
RS
9810\f
9811struct c_switch {
604f5adf
ILT
9812 /* The SWITCH_EXPR being built. */
9813 tree switch_expr;
a6c0a76c 9814
89dbed81 9815 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9816 default conversion is applied. */
9817 tree orig_type;
9818
3e4093b6
RS
9819 /* A splay-tree mapping the low element of a case range to the high
9820 element, or NULL_TREE if there is no high element. Used to
9821 determine whether or not a new case label duplicates an old case
9822 label. We need a tree, rather than simply a hash table, because
9823 of the GNU case range extension. */
9824 splay_tree cases;
a6c0a76c 9825
e1b7793c
ILT
9826 /* The bindings at the point of the switch. This is used for
9827 warnings crossing decls when branching to a case label. */
9828 struct c_spot_bindings *bindings;
187230a7 9829
3e4093b6
RS
9830 /* The next node on the stack. */
9831 struct c_switch *next;
b155cfd9
MP
9832
9833 /* Remember whether the controlling expression had boolean type
9834 before integer promotions for the sake of -Wswitch-bool. */
9835 bool bool_cond_p;
9836
9837 /* Remember whether there was a case value that is outside the
9838 range of the ORIG_TYPE. */
9839 bool outside_range_p;
3e4093b6 9840};
400fbf9f 9841
3e4093b6
RS
9842/* A stack of the currently active switch statements. The innermost
9843 switch statement is on the top of the stack. There is no need to
9844 mark the stack for garbage collection because it is only active
9845 during the processing of the body of a function, and we never
9846 collect at that point. */
de520661 9847
506e2710 9848struct c_switch *c_switch_stack;
de520661 9849
3e4093b6 9850/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9851 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef 9852 SWITCH_COND_LOC is the location of the switch's condition.
b155cfd9 9853 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
de520661 9854
3e4093b6 9855tree
c2255bc4
AH
9856c_start_case (location_t switch_loc,
9857 location_t switch_cond_loc,
fedfecef 9858 tree exp, bool explicit_cast_p)
de520661 9859{
c58e8676 9860 tree orig_type = error_mark_node;
b155cfd9 9861 bool bool_cond_p = false;
3e4093b6 9862 struct c_switch *cs;
2f6e4e97 9863
3e4093b6 9864 if (exp != error_mark_node)
de520661 9865 {
3e4093b6
RS
9866 orig_type = TREE_TYPE (exp);
9867
c58e8676 9868 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9869 {
c58e8676
VR
9870 if (orig_type != error_mark_node)
9871 {
c2255bc4 9872 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9873 orig_type = error_mark_node;
9874 }
3e4093b6 9875 exp = integer_zero_node;
de520661 9876 }
3e4093b6 9877 else
de520661 9878 {
c58e8676 9879 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9880 tree e = exp;
9881
9882 /* Warn if the condition has boolean value. */
9883 while (TREE_CODE (e) == COMPOUND_EXPR)
9884 e = TREE_OPERAND (e, 1);
9885
9886 if ((TREE_CODE (type) == BOOLEAN_TYPE
9887 || truth_value_p (TREE_CODE (e)))
9888 /* Explicit cast to int suppresses this warning. */
9889 && !(TREE_CODE (type) == INTEGER_TYPE
9890 && explicit_cast_p))
b155cfd9 9891 bool_cond_p = true;
8b6a5902 9892
8400e75e 9893 if (!in_system_header_at (input_location)
3e4093b6
RS
9894 && (type == long_integer_type_node
9895 || type == long_unsigned_type_node))
c2255bc4
AH
9896 warning_at (switch_cond_loc,
9897 OPT_Wtraditional, "%<long%> switch expression not "
9898 "converted to %<int%> in ISO C");
8b6a5902 9899
928c19bb 9900 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9901 exp = default_conversion (exp);
ca085fd7
MLI
9902
9903 if (warn_sequence_point)
9904 verify_sequence_points (exp);
3e4093b6
RS
9905 }
9906 }
9907
604f5adf 9908 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9909 cs = XNEW (struct c_switch);
604f5adf 9910 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9911 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9912 cs->orig_type = orig_type;
3e4093b6 9913 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9914 cs->bindings = c_get_switch_bindings ();
b155cfd9
MP
9915 cs->bool_cond_p = bool_cond_p;
9916 cs->outside_range_p = false;
506e2710
RH
9917 cs->next = c_switch_stack;
9918 c_switch_stack = cs;
3e4093b6 9919
604f5adf 9920 return add_stmt (cs->switch_expr);
3e4093b6
RS
9921}
9922
c2255bc4 9923/* Process a case label at location LOC. */
3e4093b6
RS
9924
9925tree
c2255bc4 9926do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9927{
9928 tree label = NULL_TREE;
9929
17cede2e
JM
9930 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9931 {
9932 low_value = c_fully_fold (low_value, false, NULL);
9933 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 9934 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
9935 "case label is not an integer constant expression");
9936 }
9937
9938 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9939 {
9940 high_value = c_fully_fold (high_value, false, NULL);
9941 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9942 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9943 "case label is not an integer constant expression");
9944 }
9945
e1b7793c 9946 if (c_switch_stack == NULL)
187230a7
JM
9947 {
9948 if (low_value)
e1b7793c 9949 error_at (loc, "case label not within a switch statement");
187230a7 9950 else
e1b7793c
ILT
9951 error_at (loc, "%<default%> label not within a switch statement");
9952 return NULL_TREE;
187230a7 9953 }
de520661 9954
e1b7793c
ILT
9955 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9956 EXPR_LOCATION (c_switch_stack->switch_expr),
9957 loc))
9958 return NULL_TREE;
9959
9960 label = c_add_case_label (loc, c_switch_stack->cases,
9961 SWITCH_COND (c_switch_stack->switch_expr),
9962 c_switch_stack->orig_type,
b155cfd9
MP
9963 low_value, high_value,
9964 &c_switch_stack->outside_range_p);
e1b7793c
ILT
9965 if (label == error_mark_node)
9966 label = NULL_TREE;
3e4093b6
RS
9967 return label;
9968}
de520661 9969
083e891e
MP
9970/* Finish the switch statement. TYPE is the original type of the
9971 controlling expression of the switch, or NULL_TREE. */
de520661 9972
3e4093b6 9973void
083e891e 9974c_finish_case (tree body, tree type)
3e4093b6 9975{
506e2710 9976 struct c_switch *cs = c_switch_stack;
fbc315db 9977 location_t switch_location;
3e4093b6 9978
604f5adf 9979 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9980
6de9cd9a 9981 /* Emit warnings as needed. */
c2255bc4 9982 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 9983 c_do_switch_warnings (cs->cases, switch_location,
083e891e 9984 type ? type : TREE_TYPE (cs->switch_expr),
b155cfd9
MP
9985 SWITCH_COND (cs->switch_expr),
9986 cs->bool_cond_p, cs->outside_range_p);
6de9cd9a 9987
3e4093b6 9988 /* Pop the stack. */
506e2710 9989 c_switch_stack = cs->next;
3e4093b6 9990 splay_tree_delete (cs->cases);
e1b7793c 9991 c_release_switch_bindings (cs->bindings);
5d038c4c 9992 XDELETE (cs);
de520661 9993}
325c3691 9994\f
506e2710
RH
9995/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9996 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
99cd9857 9997 may be null. */
325c3691 9998
9e51cf9d 9999void
506e2710 10000c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
99cd9857 10001 tree else_block)
325c3691 10002{
506e2710 10003 tree stmt;
325c3691 10004
25c22937
BI
10005 /* If the condition has array notations, then the rank of the then_block and
10006 else_block must be either 0 or be equal to the rank of the condition. If
10007 the condition does not have array notations then break them up as it is
10008 broken up in a normal expression. */
b72271b9 10009 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
10010 {
10011 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10012 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10013 return;
10014 if (then_block
10015 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10016 return;
10017 if (else_block
10018 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10019 return;
10020 if (cond_rank != then_rank && then_rank != 0)
10021 {
10022 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10023 " and the then-block");
10024 return;
10025 }
10026 else if (cond_rank != else_rank && else_rank != 0)
10027 {
10028 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10029 " and the else-block");
10030 return;
10031 }
10032 }
16865eaa 10033
2214de30 10034 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 10035 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 10036 add_stmt (stmt);
325c3691
RH
10037}
10038
506e2710
RH
10039/* Emit a general-purpose loop construct. START_LOCUS is the location of
10040 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10041 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 10042 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 10043 the continue label. Everything is allowed to be NULL. */
325c3691
RH
10044
10045void
506e2710
RH
10046c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10047 tree blab, tree clab, bool cond_is_first)
325c3691 10048{
506e2710
RH
10049 tree entry = NULL, exit = NULL, t;
10050
e5e44252
AK
10051 /* In theory could forbid cilk spawn for loop increment expression,
10052 but it should work just fine. */
36536d79 10053
28af952a
RS
10054 /* If the condition is zero don't generate a loop construct. */
10055 if (cond && integer_zerop (cond))
10056 {
10057 if (cond_is_first)
10058 {
10059 t = build_and_jump (&blab);
10060 SET_EXPR_LOCATION (t, start_locus);
10061 add_stmt (t);
10062 }
10063 }
10064 else
506e2710
RH
10065 {
10066 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 10067
506e2710 10068 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
10069 out of the loop, or to the top of it. If there's no exit condition,
10070 then we just build a jump back to the top. */
506e2710 10071 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 10072
28af952a 10073 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
10074 {
10075 /* Canonicalize the loop condition to the end. This means
10076 generating a branch to the loop condition. Reuse the
10077 continue label, if possible. */
10078 if (cond_is_first)
10079 {
10080 if (incr || !clab)
10081 {
10082 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10083 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10084 }
10085 else
10086 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 10087 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
10088 add_stmt (t);
10089 }
10090
506e2710 10091 t = build_and_jump (&blab);
506e2710 10092 if (cond_is_first)
db3927fb
AH
10093 exit = fold_build3_loc (start_locus,
10094 COND_EXPR, void_type_node, cond, exit, t);
506e2710 10095 else
db3927fb
AH
10096 exit = fold_build3_loc (input_location,
10097 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3 10098 }
fc402eec
AA
10099 else
10100 {
10101 /* For the backward-goto's location of an unconditional loop
10102 use the beginning of the body, or, if there is none, the
10103 top of the loop. */
10104 location_t loc = EXPR_LOCATION (expr_first (body));
10105 if (loc == UNKNOWN_LOCATION)
10106 loc = start_locus;
10107 SET_EXPR_LOCATION (exit, loc);
10108 }
c22cacf3 10109
506e2710
RH
10110 add_stmt (top);
10111 }
c22cacf3 10112
506e2710
RH
10113 if (body)
10114 add_stmt (body);
10115 if (clab)
10116 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10117 if (incr)
10118 add_stmt (incr);
10119 if (entry)
10120 add_stmt (entry);
10121 if (exit)
10122 add_stmt (exit);
10123 if (blab)
10124 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 10125}
325c3691
RH
10126
10127tree
c2255bc4 10128c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 10129{
089efaa4 10130 bool skip;
506e2710 10131 tree label = *label_p;
325c3691 10132
089efaa4
ILT
10133 /* In switch statements break is sometimes stylistically used after
10134 a return statement. This can lead to spurious warnings about
10135 control reaching the end of a non-void function when it is
10136 inlined. Note that we are calling block_may_fallthru with
10137 language specific tree nodes; this works because
10138 block_may_fallthru returns true when given something it does not
10139 understand. */
10140 skip = !block_may_fallthru (cur_stmt_list);
10141
506e2710 10142 if (!label)
089efaa4
ILT
10143 {
10144 if (!skip)
c2255bc4 10145 *label_p = label = create_artificial_label (loc);
089efaa4 10146 }
953ff289
DN
10147 else if (TREE_CODE (label) == LABEL_DECL)
10148 ;
10149 else switch (TREE_INT_CST_LOW (label))
506e2710 10150 {
953ff289 10151 case 0:
506e2710 10152 if (is_break)
c2255bc4 10153 error_at (loc, "break statement not within loop or switch");
506e2710 10154 else
c2255bc4 10155 error_at (loc, "continue statement not within a loop");
506e2710 10156 return NULL_TREE;
953ff289
DN
10157
10158 case 1:
10159 gcc_assert (is_break);
c2255bc4 10160 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
10161 return NULL_TREE;
10162
c02065fc
AH
10163 case 2:
10164 if (is_break)
10165 error ("break statement within %<#pragma simd%> loop body");
10166 else
10167 error ("continue statement within %<#pragma simd%> loop body");
10168 return NULL_TREE;
10169
953ff289
DN
10170 default:
10171 gcc_unreachable ();
506e2710 10172 }
325c3691 10173
089efaa4
ILT
10174 if (skip)
10175 return NULL_TREE;
10176
2e28e797
JH
10177 if (!is_break)
10178 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10179
53fb4de3 10180 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
10181}
10182
506e2710 10183/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
10184
10185static void
c2255bc4 10186emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 10187{
e6b5a630
RH
10188 if (expr == error_mark_node)
10189 ;
10190 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
10191 {
10192 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 10193 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 10194 }
789eadcd
MP
10195 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10196 {
10197 tree r = expr;
10198 location_t cloc = loc;
10199 while (TREE_CODE (r) == COMPOUND_EXPR)
10200 {
10201 if (EXPR_HAS_LOCATION (r))
10202 cloc = EXPR_LOCATION (r);
10203 r = TREE_OPERAND (r, 1);
10204 }
10205 if (!TREE_SIDE_EFFECTS (r)
10206 && !VOID_TYPE_P (TREE_TYPE (r))
10207 && !CONVERT_EXPR_P (r)
cc28fc7f 10208 && !TREE_NO_WARNING (r)
789eadcd
MP
10209 && !TREE_NO_WARNING (expr))
10210 warning_at (cloc, OPT_Wunused_value,
10211 "right-hand operand of comma expression has no effect");
10212 }
27f33b15 10213 else
c2255bc4 10214 warn_if_unused_value (expr, loc);
3a5b9284
RH
10215}
10216
506e2710 10217/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
10218 diagnostics, but do not call ADD_STMT. LOC is the location of the
10219 statement. */
3a5b9284 10220
506e2710 10221tree
c2255bc4 10222c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 10223{
056928b2
JJ
10224 tree exprv;
10225
3a5b9284 10226 if (!expr)
506e2710 10227 return NULL_TREE;
3a5b9284 10228
928c19bb
JM
10229 expr = c_fully_fold (expr, false, NULL);
10230
3a5b9284
RH
10231 if (warn_sequence_point)
10232 verify_sequence_points (expr);
10233
10234 if (TREE_TYPE (expr) != error_mark_node
10235 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10236 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 10237 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
10238
10239 /* If we're not processing a statement expression, warn about unused values.
10240 Warnings for statement expressions will be emitted later, once we figure
10241 out which is the result. */
10242 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 10243 && warn_unused_value)
d06f8b75 10244 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
3a5b9284 10245
056928b2
JJ
10246 exprv = expr;
10247 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10248 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
10249 while (CONVERT_EXPR_P (exprv))
10250 exprv = TREE_OPERAND (exprv, 0);
10251 if (DECL_P (exprv)
10252 || handled_component_p (exprv)
10253 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 10254 mark_exp_read (exprv);
fa8351f8 10255
3a5b9284
RH
10256 /* If the expression is not of a type to which we cannot assign a line
10257 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 10258 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
10259 {
10260 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10261 SET_EXPR_LOCATION (expr, loc);
10262 }
506e2710
RH
10263
10264 return expr;
10265}
10266
c2255bc4
AH
10267/* Emit an expression as a statement. LOC is the location of the
10268 expression. */
506e2710
RH
10269
10270tree
c2255bc4 10271c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
10272{
10273 if (expr)
c2255bc4 10274 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
10275 else
10276 return NULL;
3a5b9284
RH
10277}
10278
10279/* Do the opposite and emit a statement as an expression. To begin,
10280 create a new binding level and return it. */
325c3691
RH
10281
10282tree
10283c_begin_stmt_expr (void)
10284{
10285 tree ret;
10286
10287 /* We must force a BLOCK for this level so that, if it is not expanded
10288 later, there is a way to turn off the entire subtree of blocks that
10289 are contained in it. */
10290 keep_next_level ();
10291 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10292
10293 c_bindings_start_stmt_expr (c_switch_stack == NULL
10294 ? NULL
10295 : c_switch_stack->bindings);
325c3691
RH
10296
10297 /* Mark the current statement list as belonging to a statement list. */
10298 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10299
10300 return ret;
10301}
10302
c2255bc4
AH
10303/* LOC is the location of the compound statement to which this body
10304 belongs. */
10305
325c3691 10306tree
c2255bc4 10307c_finish_stmt_expr (location_t loc, tree body)
325c3691 10308{
3a5b9284 10309 tree last, type, tmp, val;
325c3691
RH
10310 tree *last_p;
10311
c2255bc4 10312 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10313
10314 c_bindings_end_stmt_expr (c_switch_stack == NULL
10315 ? NULL
10316 : c_switch_stack->bindings);
325c3691 10317
3a5b9284
RH
10318 /* Locate the last statement in BODY. See c_end_compound_stmt
10319 about always returning a BIND_EXPR. */
10320 last_p = &BIND_EXPR_BODY (body);
10321 last = BIND_EXPR_BODY (body);
10322
10323 continue_searching:
325c3691
RH
10324 if (TREE_CODE (last) == STATEMENT_LIST)
10325 {
3a5b9284
RH
10326 tree_stmt_iterator i;
10327
10328 /* This can happen with degenerate cases like ({ }). No value. */
10329 if (!TREE_SIDE_EFFECTS (last))
10330 return body;
10331
10332 /* If we're supposed to generate side effects warnings, process
10333 all of the statements except the last. */
27f33b15 10334 if (warn_unused_value)
325c3691 10335 {
3a5b9284 10336 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10337 {
10338 location_t tloc;
10339 tree t = tsi_stmt (i);
10340
10341 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10342 emit_side_effect_warnings (tloc, t);
10343 }
325c3691
RH
10344 }
10345 else
3a5b9284
RH
10346 i = tsi_last (last);
10347 last_p = tsi_stmt_ptr (i);
10348 last = *last_p;
325c3691
RH
10349 }
10350
3a5b9284
RH
10351 /* If the end of the list is exception related, then the list was split
10352 by a call to push_cleanup. Continue searching. */
10353 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10354 || TREE_CODE (last) == TRY_CATCH_EXPR)
10355 {
10356 last_p = &TREE_OPERAND (last, 0);
10357 last = *last_p;
10358 goto continue_searching;
10359 }
10360
26d8af35
JM
10361 if (last == error_mark_node)
10362 return last;
10363
3a5b9284
RH
10364 /* In the case that the BIND_EXPR is not necessary, return the
10365 expression out from inside it. */
26d8af35
JM
10366 if (last == BIND_EXPR_BODY (body)
10367 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10368 {
928c19bb
JM
10369 /* Even if this looks constant, do not allow it in a constant
10370 expression. */
e5a94231 10371 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10372 /* Do not warn if the return value of a statement expression is
10373 unused. */
928c19bb 10374 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10375 return last;
10376 }
325c3691
RH
10377
10378 /* Extract the type of said expression. */
10379 type = TREE_TYPE (last);
325c3691 10380
3a5b9284
RH
10381 /* If we're not returning a value at all, then the BIND_EXPR that
10382 we already have is a fine expression to return. */
10383 if (!type || VOID_TYPE_P (type))
10384 return body;
10385
10386 /* Now that we've located the expression containing the value, it seems
10387 silly to make voidify_wrapper_expr repeat the process. Create a
10388 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10389 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10390
10391 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10392 tree_expr_nonnegative_p giving up immediately. */
10393 val = last;
10394 if (TREE_CODE (val) == NOP_EXPR
10395 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10396 val = TREE_OPERAND (val, 0);
10397
53fb4de3 10398 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10399 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10400
c2255bc4
AH
10401 {
10402 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10403 SET_EXPR_LOCATION (t, loc);
10404 return t;
10405 }
325c3691
RH
10406}
10407\f
10408/* Begin and end compound statements. This is as simple as pushing
10409 and popping new statement lists from the tree. */
10410
10411tree
10412c_begin_compound_stmt (bool do_scope)
10413{
10414 tree stmt = push_stmt_list ();
10415 if (do_scope)
4dfa0342 10416 push_scope ();
325c3691
RH
10417 return stmt;
10418}
10419
c2255bc4
AH
10420/* End a compound statement. STMT is the statement. LOC is the
10421 location of the compound statement-- this is usually the location
10422 of the opening brace. */
10423
325c3691 10424tree
c2255bc4 10425c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10426{
10427 tree block = NULL;
10428
10429 if (do_scope)
10430 {
10431 if (c_dialect_objc ())
10432 objc_clear_super_receiver ();
10433 block = pop_scope ();
10434 }
10435
10436 stmt = pop_stmt_list (stmt);
c2255bc4 10437 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10438
10439 /* If this compound statement is nested immediately inside a statement
10440 expression, then force a BIND_EXPR to be created. Otherwise we'll
10441 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10442 STATEMENT_LISTs merge, and thus we can lose track of what statement
10443 was really last. */
38e01f9e 10444 if (building_stmt_list_p ()
325c3691
RH
10445 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10446 && TREE_CODE (stmt) != BIND_EXPR)
10447 {
53fb4de3 10448 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10449 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10450 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10451 }
10452
10453 return stmt;
10454}
5a508662
RH
10455
10456/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10457 when the current scope is exited. EH_ONLY is true when this is not
10458 meant to apply to normal control flow transfer. */
10459
10460void
c2255bc4 10461push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10462{
3a5b9284
RH
10463 enum tree_code code;
10464 tree stmt, list;
10465 bool stmt_expr;
10466
10467 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10468 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10469 add_stmt (stmt);
3a5b9284
RH
10470 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10471 list = push_stmt_list ();
10472 TREE_OPERAND (stmt, 0) = list;
10473 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10474}
325c3691 10475\f
9f47c7e5
IE
10476/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10477 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10478
10479static tree
10480build_vec_cmp (tree_code code, tree type,
10481 tree arg0, tree arg1)
10482{
10483 tree zero_vec = build_zero_cst (type);
10484 tree minus_one_vec = build_minus_one_cst (type);
10485 tree cmp_type = build_same_sized_truth_vector_type (type);
10486 tree cmp = build2 (code, cmp_type, arg0, arg1);
10487 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10488}
10489
3e4093b6
RS
10490/* Build a binary-operation expression without default conversions.
10491 CODE is the kind of expression to build.
ba47d38d 10492 LOCATION is the operator's location.
3e4093b6
RS
10493 This function differs from `build' in several ways:
10494 the data type of the result is computed and recorded in it,
10495 warnings are generated if arg data types are invalid,
10496 special handling for addition and subtraction of pointers is known,
10497 and some optimization is done (operations on narrow ints
10498 are done in the narrower type when that gives the same result).
10499 Constant folding is also done before the result is returned.
de520661 10500
3e4093b6
RS
10501 Note that the operands will never have enumeral types, or function
10502 or array types, because either they will have the default conversions
10503 performed or they have both just been converted to some other type in which
10504 the arithmetic is to be done. */
10505
10506tree
ba47d38d
AH
10507build_binary_op (location_t location, enum tree_code code,
10508 tree orig_op0, tree orig_op1, int convert_p)
de520661 10509{
8ce94e44
JM
10510 tree type0, type1, orig_type0, orig_type1;
10511 tree eptype;
3e4093b6
RS
10512 enum tree_code code0, code1;
10513 tree op0, op1;
c9f9eb5d 10514 tree ret = error_mark_node;
4de67c26 10515 const char *invalid_op_diag;
4d84fe7c 10516 bool op0_int_operands, op1_int_operands;
928c19bb 10517 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10518
3e4093b6
RS
10519 /* Expression code to give to the expression when it is built.
10520 Normally this is CODE, which is what the caller asked for,
10521 but in some special cases we change it. */
10522 enum tree_code resultcode = code;
8b6a5902 10523
3e4093b6
RS
10524 /* Data type in which the computation is to be performed.
10525 In the simplest cases this is the common type of the arguments. */
10526 tree result_type = NULL;
10527
8ce94e44
JM
10528 /* When the computation is in excess precision, the type of the
10529 final EXCESS_PRECISION_EXPR. */
2d2e923f 10530 tree semantic_result_type = NULL;
8ce94e44 10531
3e4093b6
RS
10532 /* Nonzero means operands have already been type-converted
10533 in whatever way is necessary.
10534 Zero means they need to be converted to RESULT_TYPE. */
10535 int converted = 0;
10536
10537 /* Nonzero means create the expression with this type, rather than
10538 RESULT_TYPE. */
10539 tree build_type = 0;
10540
10541 /* Nonzero means after finally constructing the expression
10542 convert it to this type. */
10543 tree final_type = 0;
10544
10545 /* Nonzero if this is an operation like MIN or MAX which can
10546 safely be computed in short if both args are promoted shorts.
10547 Also implies COMMON.
10548 -1 indicates a bitwise operation; this makes a difference
10549 in the exact conditions for when it is safe to do the operation
10550 in a narrower mode. */
10551 int shorten = 0;
10552
10553 /* Nonzero if this is a comparison operation;
10554 if both args are promoted shorts, compare the original shorts.
10555 Also implies COMMON. */
10556 int short_compare = 0;
10557
10558 /* Nonzero if this is a right-shift operation, which can be computed on the
10559 original short and then promoted if the operand is a promoted short. */
10560 int short_shift = 0;
10561
10562 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10563 int common = 0;
10564
58393038
ZL
10565 /* True means types are compatible as far as ObjC is concerned. */
10566 bool objc_ok;
10567
8ce94e44
JM
10568 /* True means this is an arithmetic operation that may need excess
10569 precision. */
10570 bool may_need_excess_precision;
10571
180f8dbb
JM
10572 /* True means this is a boolean operation that converts both its
10573 operands to truth-values. */
10574 bool boolean_op = false;
10575
de5a5fa1
MP
10576 /* Remember whether we're doing / or %. */
10577 bool doing_div_or_mod = false;
10578
10579 /* Remember whether we're doing << or >>. */
10580 bool doing_shift = false;
10581
10582 /* Tree holding instrumentation expression. */
10583 tree instrument_expr = NULL;
10584
ba47d38d
AH
10585 if (location == UNKNOWN_LOCATION)
10586 location = input_location;
10587
4d84fe7c
JM
10588 op0 = orig_op0;
10589 op1 = orig_op1;
10590
10591 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10592 if (op0_int_operands)
10593 op0 = remove_c_maybe_const_expr (op0);
10594 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10595 if (op1_int_operands)
10596 op1 = remove_c_maybe_const_expr (op1);
10597 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10598 if (int_operands)
10599 {
10600 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10601 && TREE_CODE (orig_op1) == INTEGER_CST);
10602 int_const = (int_const_or_overflow
10603 && !TREE_OVERFLOW (orig_op0)
10604 && !TREE_OVERFLOW (orig_op1));
10605 }
10606 else
10607 int_const = int_const_or_overflow = false;
10608
0e3a99ae 10609 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e 10610 if (convert_p
31521951 10611 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
790e9490 10612 {
4d84fe7c
JM
10613 op0 = default_conversion (op0);
10614 op1 = default_conversion (op1);
790e9490
RS
10615 }
10616
36536d79
BI
10617 /* When Cilk Plus is enabled and there are array notations inside op0, then
10618 we check to see if there are builtin array notation functions. If
10619 so, then we take on the type of the array notation inside it. */
b72271b9 10620 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10621 orig_type0 = type0 = find_correct_array_notation_type (op0);
10622 else
10623 orig_type0 = type0 = TREE_TYPE (op0);
10624
b72271b9 10625 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10626 orig_type1 = type1 = find_correct_array_notation_type (op1);
10627 else
10628 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10629
3e4093b6
RS
10630 /* The expression codes of the data types of the arguments tell us
10631 whether the arguments are integers, floating, pointers, etc. */
10632 code0 = TREE_CODE (type0);
10633 code1 = TREE_CODE (type1);
10634
10635 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10636 STRIP_TYPE_NOPS (op0);
10637 STRIP_TYPE_NOPS (op1);
10638
10639 /* If an error was already reported for one of the arguments,
10640 avoid reporting another error. */
10641
10642 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10643 return error_mark_node;
10644
1807ffc1
MS
10645 if (code0 == POINTER_TYPE
10646 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10647 return error_mark_node;
10648
10649 if (code1 == POINTER_TYPE
10650 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10651 return error_mark_node;
10652
4de67c26
JM
10653 if ((invalid_op_diag
10654 = targetm.invalid_binary_op (code, type0, type1)))
10655 {
ba47d38d 10656 error_at (location, invalid_op_diag);
4de67c26
JM
10657 return error_mark_node;
10658 }
10659
8ce94e44
JM
10660 switch (code)
10661 {
10662 case PLUS_EXPR:
10663 case MINUS_EXPR:
10664 case MULT_EXPR:
10665 case TRUNC_DIV_EXPR:
10666 case CEIL_DIV_EXPR:
10667 case FLOOR_DIV_EXPR:
10668 case ROUND_DIV_EXPR:
10669 case EXACT_DIV_EXPR:
10670 may_need_excess_precision = true;
10671 break;
10672 default:
10673 may_need_excess_precision = false;
10674 break;
10675 }
10676 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10677 {
10678 op0 = TREE_OPERAND (op0, 0);
10679 type0 = TREE_TYPE (op0);
10680 }
10681 else if (may_need_excess_precision
10682 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10683 {
10684 type0 = eptype;
10685 op0 = convert (eptype, op0);
10686 }
10687 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10688 {
10689 op1 = TREE_OPERAND (op1, 0);
10690 type1 = TREE_TYPE (op1);
10691 }
10692 else if (may_need_excess_precision
10693 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10694 {
10695 type1 = eptype;
10696 op1 = convert (eptype, op1);
10697 }
10698
58393038
ZL
10699 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10700
0e3a99ae
AS
10701 /* In case when one of the operands of the binary operation is
10702 a vector and another is a scalar -- convert scalar to vector. */
10703 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10704 {
a212e43f
MG
10705 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10706 true);
f90e8e2e 10707
0e3a99ae
AS
10708 switch (convert_flag)
10709 {
10710 case stv_error:
10711 return error_mark_node;
10712 case stv_firstarg:
10713 {
10714 bool maybe_const = true;
10715 tree sc;
10716 sc = c_fully_fold (op0, false, &maybe_const);
10717 sc = save_expr (sc);
10718 sc = convert (TREE_TYPE (type1), sc);
10719 op0 = build_vector_from_val (type1, sc);
10720 if (!maybe_const)
10721 op0 = c_wrap_maybe_const (op0, true);
10722 orig_type0 = type0 = TREE_TYPE (op0);
10723 code0 = TREE_CODE (type0);
10724 converted = 1;
10725 break;
10726 }
10727 case stv_secondarg:
10728 {
10729 bool maybe_const = true;
10730 tree sc;
10731 sc = c_fully_fold (op1, false, &maybe_const);
10732 sc = save_expr (sc);
10733 sc = convert (TREE_TYPE (type0), sc);
10734 op1 = build_vector_from_val (type0, sc);
10735 if (!maybe_const)
54b9f838 10736 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10737 orig_type1 = type1 = TREE_TYPE (op1);
10738 code1 = TREE_CODE (type1);
10739 converted = 1;
10740 break;
10741 }
10742 default:
10743 break;
10744 }
10745 }
10746
3e4093b6 10747 switch (code)
de520661 10748 {
3e4093b6
RS
10749 case PLUS_EXPR:
10750 /* Handle the pointer + int case. */
10751 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10752 {
db3927fb 10753 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10754 goto return_build_binary_op;
10755 }
3e4093b6 10756 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10757 {
db3927fb 10758 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10759 goto return_build_binary_op;
10760 }
fe67cf58 10761 else
3e4093b6
RS
10762 common = 1;
10763 break;
400fbf9f 10764
3e4093b6
RS
10765 case MINUS_EXPR:
10766 /* Subtraction of two similar pointers.
10767 We must subtract them as integers, then divide by object size. */
10768 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10769 && comp_target_types (location, type0, type1))
c9f9eb5d 10770 {
db3927fb 10771 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10772 goto return_build_binary_op;
10773 }
3e4093b6
RS
10774 /* Handle pointer minus int. Just like pointer plus int. */
10775 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10776 {
db3927fb 10777 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10778 goto return_build_binary_op;
10779 }
3e4093b6
RS
10780 else
10781 common = 1;
10782 break;
8b6a5902 10783
3e4093b6
RS
10784 case MULT_EXPR:
10785 common = 1;
10786 break;
10787
10788 case TRUNC_DIV_EXPR:
10789 case CEIL_DIV_EXPR:
10790 case FLOOR_DIV_EXPR:
10791 case ROUND_DIV_EXPR:
10792 case EXACT_DIV_EXPR:
de5a5fa1 10793 doing_div_or_mod = true;
c9f9eb5d 10794 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10795
10796 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10797 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10798 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10799 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10800 || code1 == FIXED_POINT_TYPE
3e4093b6 10801 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10802 {
5bed876a
AH
10803 enum tree_code tcode0 = code0, tcode1 = code1;
10804
3a021db2 10805 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10806 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10807 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10808 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10809
ab22c1fa
CF
10810 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10811 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10812 resultcode = RDIV_EXPR;
10813 else
10814 /* Although it would be tempting to shorten always here, that
10815 loses on some targets, since the modulo instruction is
10816 undefined if the quotient can't be represented in the
10817 computation mode. We shorten only if unsigned or if
10818 dividing by something we know != -1. */
8df83eae 10819 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10820 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10821 && !integer_all_onesp (op1)));
3e4093b6
RS
10822 common = 1;
10823 }
10824 break;
de520661 10825
3e4093b6 10826 case BIT_AND_EXPR:
3e4093b6
RS
10827 case BIT_IOR_EXPR:
10828 case BIT_XOR_EXPR:
10829 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10830 shorten = -1;
9ef0c8d9
AP
10831 /* Allow vector types which are not floating point types. */
10832 else if (code0 == VECTOR_TYPE
10833 && code1 == VECTOR_TYPE
10834 && !VECTOR_FLOAT_TYPE_P (type0)
10835 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10836 common = 1;
10837 break;
10838
10839 case TRUNC_MOD_EXPR:
10840 case FLOOR_MOD_EXPR:
de5a5fa1 10841 doing_div_or_mod = true;
c9f9eb5d 10842 warn_for_div_by_zero (location, op1);
de520661 10843
5cfd5d9b
AP
10844 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10845 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10846 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10847 common = 1;
10848 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10849 {
10850 /* Although it would be tempting to shorten always here, that loses
10851 on some targets, since the modulo instruction is undefined if the
10852 quotient can't be represented in the computation mode. We shorten
10853 only if unsigned or if dividing by something we know != -1. */
8df83eae 10854 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10855 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10856 && !integer_all_onesp (op1)));
3e4093b6
RS
10857 common = 1;
10858 }
10859 break;
de520661 10860
3e4093b6
RS
10861 case TRUTH_ANDIF_EXPR:
10862 case TRUTH_ORIF_EXPR:
10863 case TRUTH_AND_EXPR:
10864 case TRUTH_OR_EXPR:
10865 case TRUTH_XOR_EXPR:
10866 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10867 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10868 || code0 == FIXED_POINT_TYPE)
3e4093b6 10869 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10870 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10871 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10872 {
10873 /* Result of these operations is always an int,
10874 but that does not mean the operands should be
10875 converted to ints! */
10876 result_type = integer_type_node;
a27d595d
JM
10877 if (op0_int_operands)
10878 {
10879 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10880 op0 = remove_c_maybe_const_expr (op0);
10881 }
10882 else
10883 op0 = c_objc_common_truthvalue_conversion (location, op0);
10884 if (op1_int_operands)
10885 {
10886 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10887 op1 = remove_c_maybe_const_expr (op1);
10888 }
10889 else
10890 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10891 converted = 1;
180f8dbb 10892 boolean_op = true;
3e4093b6 10893 }
928c19bb
JM
10894 if (code == TRUTH_ANDIF_EXPR)
10895 {
10896 int_const_or_overflow = (int_operands
10897 && TREE_CODE (orig_op0) == INTEGER_CST
10898 && (op0 == truthvalue_false_node
10899 || TREE_CODE (orig_op1) == INTEGER_CST));
10900 int_const = (int_const_or_overflow
10901 && !TREE_OVERFLOW (orig_op0)
10902 && (op0 == truthvalue_false_node
10903 || !TREE_OVERFLOW (orig_op1)));
10904 }
10905 else if (code == TRUTH_ORIF_EXPR)
10906 {
10907 int_const_or_overflow = (int_operands
10908 && TREE_CODE (orig_op0) == INTEGER_CST
10909 && (op0 == truthvalue_true_node
10910 || TREE_CODE (orig_op1) == INTEGER_CST));
10911 int_const = (int_const_or_overflow
10912 && !TREE_OVERFLOW (orig_op0)
10913 && (op0 == truthvalue_true_node
10914 || !TREE_OVERFLOW (orig_op1)));
10915 }
3e4093b6 10916 break;
eba80994 10917
3e4093b6
RS
10918 /* Shift operations: result has same type as first operand;
10919 always convert second operand to int.
10920 Also set SHORT_SHIFT if shifting rightward. */
de520661 10921
3e4093b6 10922 case RSHIFT_EXPR:
f87bd04b
AS
10923 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10924 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10925 {
10926 result_type = type0;
10927 converted = 1;
10928 }
10929 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
451b5e48
MP
10930 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10931 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10932 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
10933 {
10934 result_type = type0;
10935 converted = 1;
10936 }
10937 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
451b5e48 10938 && code1 == INTEGER_TYPE)
3e4093b6 10939 {
de5a5fa1 10940 doing_shift = true;
928c19bb 10941 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10942 {
3e4093b6 10943 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10944 {
10945 int_const = false;
7d882b83 10946 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10947 warning_at (location, OPT_Wshift_count_negative,
10948 "right shift count is negative");
928c19bb 10949 }
3e4093b6 10950 else
bbb818c6 10951 {
3f75a254 10952 if (!integer_zerop (op1))
3e4093b6
RS
10953 short_shift = 1;
10954
10955 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10956 {
10957 int_const = false;
7d882b83 10958 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10959 warning_at (location, OPT_Wshift_count_overflow,
10960 "right shift count >= width of type");
928c19bb 10961 }
bbb818c6 10962 }
b62acd60 10963 }
de520661 10964
3e4093b6
RS
10965 /* Use the type of the value to be shifted. */
10966 result_type = type0;
3e4093b6
RS
10967 /* Avoid converting op1 to result_type later. */
10968 converted = 1;
400fbf9f 10969 }
3e4093b6 10970 break;
253b6b82 10971
3e4093b6 10972 case LSHIFT_EXPR:
f87bd04b
AS
10973 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10974 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10975 {
10976 result_type = type0;
10977 converted = 1;
10978 }
10979 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
451b5e48
MP
10980 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10981 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10982 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
10983 {
10984 result_type = type0;
10985 converted = 1;
10986 }
10987 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
451b5e48 10988 && code1 == INTEGER_TYPE)
3e4093b6 10989 {
de5a5fa1 10990 doing_shift = true;
0173bd2a
MP
10991 if (TREE_CODE (op0) == INTEGER_CST
10992 && tree_int_cst_sgn (op0) < 0)
10993 {
10994 /* Don't reject a left shift of a negative value in a context
10995 where a constant expression is needed in C90. */
10996 if (flag_isoc99)
10997 int_const = false;
10998 if (c_inhibit_evaluation_warnings == 0)
10999 warning_at (location, OPT_Wshift_negative_value,
11000 "left shift of negative value");
11001 }
928c19bb 11002 if (TREE_CODE (op1) == INTEGER_CST)
de520661 11003 {
3e4093b6 11004 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
11005 {
11006 int_const = false;
7d882b83 11007 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11008 warning_at (location, OPT_Wshift_count_negative,
11009 "left shift count is negative");
928c19bb 11010 }
3e4093b6 11011 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
11012 {
11013 int_const = false;
7d882b83 11014 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11015 warning_at (location, OPT_Wshift_count_overflow,
11016 "left shift count >= width of type");
928c19bb 11017 }
451b5e48
MP
11018 else if (TREE_CODE (op0) == INTEGER_CST
11019 && maybe_warn_shift_overflow (location, op0, op1)
11020 && flag_isoc99)
11021 int_const = false;
94ba5069 11022 }
de520661 11023
3e4093b6
RS
11024 /* Use the type of the value to be shifted. */
11025 result_type = type0;
3e4093b6
RS
11026 /* Avoid converting op1 to result_type later. */
11027 converted = 1;
400fbf9f 11028 }
3e4093b6 11029 break;
de520661 11030
3e4093b6
RS
11031 case EQ_EXPR:
11032 case NE_EXPR:
d246ab4f
AS
11033 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11034 {
11035 tree intt;
0af94e6f 11036 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11037 {
11038 error_at (location, "comparing vectors with different "
11039 "element types");
11040 return error_mark_node;
11041 }
11042
11043 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11044 {
11045 error_at (location, "comparing vectors with different "
11046 "number of elements");
11047 return error_mark_node;
11048 }
11049
fa74a4bc
MP
11050 /* It's not precisely specified how the usual arithmetic
11051 conversions apply to the vector types. Here, we use
11052 the unsigned type if one of the operands is signed and
11053 the other one is unsigned. */
11054 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11055 {
11056 if (!TYPE_UNSIGNED (type0))
11057 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11058 else
11059 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11060 warning_at (location, OPT_Wsign_compare, "comparison between "
11061 "types %qT and %qT", type0, type1);
11062 }
11063
d246ab4f
AS
11064 /* Always construct signed integer vector type. */
11065 intt = c_common_type_for_size (GET_MODE_BITSIZE
11066 (TYPE_MODE (TREE_TYPE (type0))), 0);
11067 result_type = build_opaque_vector_type (intt,
11068 TYPE_VECTOR_SUBPARTS (type0));
11069 converted = 1;
9f47c7e5
IE
11070 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11071 goto return_build_binary_op;
d246ab4f 11072 }
ae311566 11073 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
11074 warning_at (location,
11075 OPT_Wfloat_equal,
11076 "comparing floating point with == or != is unsafe");
3e4093b6
RS
11077 /* Result of comparison is always int,
11078 but don't convert the args to int! */
11079 build_type = integer_type_node;
11080 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 11081 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 11082 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 11083 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 11084 short_compare = 1;
637f1455
SZ
11085 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11086 {
11087 if (TREE_CODE (op0) == ADDR_EXPR
de55efd5
MP
11088 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11089 && !from_macro_expansion_at (location))
637f1455
SZ
11090 {
11091 if (code == EQ_EXPR)
11092 warning_at (location,
11093 OPT_Waddress,
11094 "the comparison will always evaluate as %<false%> "
11095 "for the address of %qD will never be NULL",
11096 TREE_OPERAND (op0, 0));
11097 else
11098 warning_at (location,
11099 OPT_Waddress,
11100 "the comparison will always evaluate as %<true%> "
11101 "for the address of %qD will never be NULL",
11102 TREE_OPERAND (op0, 0));
11103 }
11104 result_type = type0;
11105 }
11106 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11107 {
11108 if (TREE_CODE (op1) == ADDR_EXPR
de55efd5
MP
11109 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11110 && !from_macro_expansion_at (location))
637f1455
SZ
11111 {
11112 if (code == EQ_EXPR)
11113 warning_at (location,
f90e8e2e 11114 OPT_Waddress,
637f1455
SZ
11115 "the comparison will always evaluate as %<false%> "
11116 "for the address of %qD will never be NULL",
11117 TREE_OPERAND (op1, 0));
11118 else
11119 warning_at (location,
11120 OPT_Waddress,
11121 "the comparison will always evaluate as %<true%> "
11122 "for the address of %qD will never be NULL",
11123 TREE_OPERAND (op1, 0));
11124 }
11125 result_type = type1;
11126 }
3e4093b6
RS
11127 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11128 {
11129 tree tt0 = TREE_TYPE (type0);
11130 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
11131 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11132 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11133 addr_space_t as_common = ADDR_SPACE_GENERIC;
11134
3e4093b6
RS
11135 /* Anything compares with void *. void * compares with anything.
11136 Otherwise, the targets must be compatible
11137 and both must be object or both incomplete. */
744aa42f 11138 if (comp_target_types (location, type0, type1))
10bc1b1b 11139 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
11140 else if (!addr_space_superset (as0, as1, &as_common))
11141 {
11142 error_at (location, "comparison of pointers to "
11143 "disjoint address spaces");
11144 return error_mark_node;
11145 }
267bac10 11146 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 11147 {
36c5e70a 11148 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 11149 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11150 "comparison of %<void *%> with function pointer");
ee2990e7 11151 }
267bac10 11152 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 11153 {
36c5e70a 11154 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 11155 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11156 "comparison of %<void *%> with function pointer");
e6834654 11157 }
3e4093b6 11158 else
58393038
ZL
11159 /* Avoid warning about the volatile ObjC EH puts on decls. */
11160 if (!objc_ok)
ba47d38d 11161 pedwarn (location, 0,
509c9d60 11162 "comparison of distinct pointer types lacks a cast");
e6834654 11163
3e4093b6 11164 if (result_type == NULL_TREE)
36c5e70a
BE
11165 {
11166 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11167 result_type = build_pointer_type
11168 (build_qualified_type (void_type_node, qual));
11169 }
e6834654 11170 }
3e4093b6 11171 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 11172 {
3e4093b6 11173 result_type = type0;
ba47d38d 11174 pedwarn (location, 0, "comparison between pointer and integer");
de520661 11175 }
3e4093b6 11176 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 11177 {
3e4093b6 11178 result_type = type1;
ba47d38d 11179 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 11180 }
04159acf
MP
11181 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11182 || truth_value_p (TREE_CODE (orig_op0)))
11183 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11184 || truth_value_p (TREE_CODE (orig_op1))))
11185 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11186 break;
8b6a5902 11187
3e4093b6
RS
11188 case LE_EXPR:
11189 case GE_EXPR:
11190 case LT_EXPR:
11191 case GT_EXPR:
d246ab4f
AS
11192 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11193 {
11194 tree intt;
0af94e6f 11195 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11196 {
11197 error_at (location, "comparing vectors with different "
11198 "element types");
11199 return error_mark_node;
11200 }
11201
11202 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11203 {
11204 error_at (location, "comparing vectors with different "
11205 "number of elements");
11206 return error_mark_node;
11207 }
11208
fa74a4bc
MP
11209 /* It's not precisely specified how the usual arithmetic
11210 conversions apply to the vector types. Here, we use
11211 the unsigned type if one of the operands is signed and
11212 the other one is unsigned. */
11213 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11214 {
11215 if (!TYPE_UNSIGNED (type0))
11216 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11217 else
11218 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11219 warning_at (location, OPT_Wsign_compare, "comparison between "
11220 "types %qT and %qT", type0, type1);
11221 }
11222
d246ab4f
AS
11223 /* Always construct signed integer vector type. */
11224 intt = c_common_type_for_size (GET_MODE_BITSIZE
11225 (TYPE_MODE (TREE_TYPE (type0))), 0);
11226 result_type = build_opaque_vector_type (intt,
11227 TYPE_VECTOR_SUBPARTS (type0));
11228 converted = 1;
9f47c7e5
IE
11229 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11230 goto return_build_binary_op;
d246ab4f 11231 }
3e4093b6 11232 build_type = integer_type_node;
ab22c1fa
CF
11233 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11234 || code0 == FIXED_POINT_TYPE)
11235 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11236 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11237 short_compare = 1;
11238 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11239 {
36c5e70a
BE
11240 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11241 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11242 addr_space_t as_common;
11243
744aa42f 11244 if (comp_target_types (location, type0, type1))
3e4093b6 11245 {
10bc1b1b 11246 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
11247 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11248 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 11249 pedwarn (location, 0,
509c9d60 11250 "comparison of complete and incomplete pointers");
fcf73884 11251 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 11252 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11253 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
11254 else if (null_pointer_constant_p (orig_op0)
11255 || null_pointer_constant_p (orig_op1))
11256 warning_at (location, OPT_Wextra,
11257 "ordered comparison of pointer with null pointer");
11258
3e4093b6 11259 }
36c5e70a
BE
11260 else if (!addr_space_superset (as0, as1, &as_common))
11261 {
11262 error_at (location, "comparison of pointers to "
11263 "disjoint address spaces");
11264 return error_mark_node;
11265 }
3e4093b6
RS
11266 else
11267 {
36c5e70a
BE
11268 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11269 result_type = build_pointer_type
11270 (build_qualified_type (void_type_node, qual));
ba47d38d 11271 pedwarn (location, 0,
509c9d60 11272 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
11273 }
11274 }
6aa3c60d 11275 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
11276 {
11277 result_type = type0;
fcf73884 11278 if (pedantic)
c1771a20 11279 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
11280 "ordered comparison of pointer with integer zero");
11281 else if (extra_warnings)
ba47d38d 11282 warning_at (location, OPT_Wextra,
d42ba3b8 11283 "ordered comparison of pointer with integer zero");
3e4093b6 11284 }
6aa3c60d 11285 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
11286 {
11287 result_type = type1;
d42ba3b8 11288 if (pedantic)
c1771a20 11289 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
11290 "ordered comparison of pointer with integer zero");
11291 else if (extra_warnings)
11292 warning_at (location, OPT_Wextra,
11293 "ordered comparison of pointer with integer zero");
3e4093b6
RS
11294 }
11295 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11296 {
11297 result_type = type0;
ba47d38d 11298 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
11299 }
11300 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11301 {
11302 result_type = type1;
ba47d38d 11303 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 11304 }
04159acf
MP
11305 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11306 || truth_value_p (TREE_CODE (orig_op0)))
11307 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11308 || truth_value_p (TREE_CODE (orig_op1))))
11309 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11310 break;
64094f6a 11311
3e4093b6 11312 default:
37b2f290 11313 gcc_unreachable ();
c9fe6f9f 11314 }
8f17b5c5 11315
e57e265b
PB
11316 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11317 return error_mark_node;
11318
5bed876a
AH
11319 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11320 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 11321 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 11322 {
745e411d
DM
11323 gcc_rich_location richloc (location);
11324 richloc.maybe_add_expr (orig_op0);
11325 richloc.maybe_add_expr (orig_op1);
11326 binary_op_error (&richloc, code, type0, type1);
5bed876a
AH
11327 return error_mark_node;
11328 }
11329
3e4093b6 11330 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 11331 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
11332 &&
11333 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 11334 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 11335 {
2ca862e9
JM
11336 bool first_complex = (code0 == COMPLEX_TYPE);
11337 bool second_complex = (code1 == COMPLEX_TYPE);
11338 int none_complex = (!first_complex && !second_complex);
39b726dd 11339
3e4093b6 11340 if (shorten || common || short_compare)
3bf6bfcc
JJ
11341 {
11342 result_type = c_common_type (type0, type1);
0a0b3574
MM
11343 do_warn_double_promotion (result_type, type0, type1,
11344 "implicit conversion from %qT to %qT "
11345 "to match other operand of binary "
11346 "expression",
11347 location);
3bf6bfcc
JJ
11348 if (result_type == error_mark_node)
11349 return error_mark_node;
11350 }
400fbf9f 11351
2ca862e9
JM
11352 if (first_complex != second_complex
11353 && (code == PLUS_EXPR
11354 || code == MINUS_EXPR
11355 || code == MULT_EXPR
11356 || (code == TRUNC_DIV_EXPR && first_complex))
11357 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11358 && flag_signed_zeros)
11359 {
11360 /* An operation on mixed real/complex operands must be
11361 handled specially, but the language-independent code can
11362 more easily optimize the plain complex arithmetic if
11363 -fno-signed-zeros. */
11364 tree real_type = TREE_TYPE (result_type);
11365 tree real, imag;
11366 if (type0 != orig_type0 || type1 != orig_type1)
11367 {
11368 gcc_assert (may_need_excess_precision && common);
2d2e923f 11369 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11370 }
11371 if (first_complex)
11372 {
11373 if (TREE_TYPE (op0) != result_type)
68fca595 11374 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11375 if (TREE_TYPE (op1) != real_type)
68fca595 11376 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11377 }
11378 else
11379 {
11380 if (TREE_TYPE (op0) != real_type)
68fca595 11381 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11382 if (TREE_TYPE (op1) != result_type)
68fca595 11383 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11384 }
11385 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11386 return error_mark_node;
11387 if (first_complex)
11388 {
11389 op0 = c_save_expr (op0);
11390 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11391 op0, 1);
11392 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11393 op0, 1);
11394 switch (code)
11395 {
11396 case MULT_EXPR:
11397 case TRUNC_DIV_EXPR:
c4603e7c 11398 op1 = c_save_expr (op1);
2ca862e9
JM
11399 imag = build2 (resultcode, real_type, imag, op1);
11400 /* Fall through. */
11401 case PLUS_EXPR:
11402 case MINUS_EXPR:
11403 real = build2 (resultcode, real_type, real, op1);
11404 break;
11405 default:
11406 gcc_unreachable();
11407 }
11408 }
11409 else
11410 {
11411 op1 = c_save_expr (op1);
11412 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11413 op1, 1);
11414 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11415 op1, 1);
11416 switch (code)
11417 {
11418 case MULT_EXPR:
c4603e7c 11419 op0 = c_save_expr (op0);
2ca862e9
JM
11420 imag = build2 (resultcode, real_type, op0, imag);
11421 /* Fall through. */
11422 case PLUS_EXPR:
11423 real = build2 (resultcode, real_type, op0, real);
11424 break;
11425 case MINUS_EXPR:
11426 real = build2 (resultcode, real_type, op0, real);
11427 imag = build1 (NEGATE_EXPR, real_type, imag);
11428 break;
11429 default:
11430 gcc_unreachable();
11431 }
11432 }
11433 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11434 goto return_build_binary_op;
11435 }
11436
3e4093b6
RS
11437 /* For certain operations (which identify themselves by shorten != 0)
11438 if both args were extended from the same smaller type,
11439 do the arithmetic in that type and then extend.
400fbf9f 11440
3e4093b6
RS
11441 shorten !=0 and !=1 indicates a bitwise operation.
11442 For them, this optimization is safe only if
11443 both args are zero-extended or both are sign-extended.
11444 Otherwise, we might change the result.
11445 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11446 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11447
3e4093b6
RS
11448 if (shorten && none_complex)
11449 {
3e4093b6 11450 final_type = result_type;
b8698a0f 11451 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11452 shorten == -1);
3e4093b6 11453 }
88a3dbc1 11454
3e4093b6 11455 /* Shifts can be shortened if shifting right. */
2f6e4e97 11456
3e4093b6
RS
11457 if (short_shift)
11458 {
11459 int unsigned_arg;
11460 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11461
3e4093b6 11462 final_type = result_type;
abe80e6d 11463
3e4093b6 11464 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11465 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11466
3e4093b6 11467 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11468 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11469 /* We can shorten only if the shift count is less than the
11470 number of bits in the smaller type size. */
11471 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11472 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11473 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11474 {
11475 /* Do an unsigned shift if the operand was zero-extended. */
11476 result_type
11477 = c_common_signed_or_unsigned_type (unsigned_arg,
11478 TREE_TYPE (arg0));
11479 /* Convert value-to-be-shifted to that type. */
11480 if (TREE_TYPE (op0) != result_type)
11481 op0 = convert (result_type, op0);
11482 converted = 1;
abe80e6d 11483 }
88a3dbc1
RK
11484 }
11485
3e4093b6
RS
11486 /* Comparison operations are shortened too but differently.
11487 They identify themselves by setting short_compare = 1. */
56cb9733 11488
3e4093b6
RS
11489 if (short_compare)
11490 {
11491 /* Don't write &op0, etc., because that would prevent op0
11492 from being kept in a register.
11493 Instead, make copies of the our local variables and
11494 pass the copies by reference, then copy them back afterward. */
11495 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11496 enum tree_code xresultcode = resultcode;
11497 tree val
393e8e8b
MP
11498 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11499 &xresultcode);
8f17b5c5 11500
3e4093b6 11501 if (val != 0)
c9f9eb5d
AH
11502 {
11503 ret = val;
11504 goto return_build_binary_op;
11505 }
8f17b5c5 11506
3e4093b6
RS
11507 op0 = xop0, op1 = xop1;
11508 converted = 1;
11509 resultcode = xresultcode;
8f17b5c5 11510
7d882b83 11511 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11512 {
11513 bool op0_maybe_const = true;
11514 bool op1_maybe_const = true;
11515 tree orig_op0_folded, orig_op1_folded;
11516
11517 if (in_late_binary_op)
11518 {
11519 orig_op0_folded = orig_op0;
11520 orig_op1_folded = orig_op1;
11521 }
11522 else
11523 {
11524 /* Fold for the sake of possible warnings, as in
11525 build_conditional_expr. This requires the
11526 "original" values to be folded, not just op0 and
11527 op1. */
f5178456 11528 c_inhibit_evaluation_warnings++;
928c19bb
JM
11529 op0 = c_fully_fold (op0, require_constant_value,
11530 &op0_maybe_const);
11531 op1 = c_fully_fold (op1, require_constant_value,
11532 &op1_maybe_const);
f5178456 11533 c_inhibit_evaluation_warnings--;
928c19bb
JM
11534 orig_op0_folded = c_fully_fold (orig_op0,
11535 require_constant_value,
11536 NULL);
11537 orig_op1_folded = c_fully_fold (orig_op1,
11538 require_constant_value,
11539 NULL);
11540 }
11541
11542 if (warn_sign_compare)
11543 warn_for_sign_compare (location, orig_op0_folded,
11544 orig_op1_folded, op0, op1,
11545 result_type, resultcode);
5c2f94b4 11546 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11547 {
11548 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11549 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11550 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11551 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11552 }
3e4093b6 11553 }
2ad1815d 11554 }
64094f6a 11555 }
64094f6a 11556
3e4093b6
RS
11557 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11558 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11559 Then the expression will be built.
11560 It will be given type FINAL_TYPE if that is nonzero;
11561 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11562
3e4093b6
RS
11563 if (!result_type)
11564 {
745e411d
DM
11565 gcc_rich_location richloc (location);
11566 richloc.maybe_add_expr (orig_op0);
11567 richloc.maybe_add_expr (orig_op1);
11568 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11569 return error_mark_node;
11570 }
400fbf9f 11571
3e4093b6 11572 if (build_type == NULL_TREE)
8ce94e44
JM
11573 {
11574 build_type = result_type;
180f8dbb
JM
11575 if ((type0 != orig_type0 || type1 != orig_type1)
11576 && !boolean_op)
8ce94e44
JM
11577 {
11578 gcc_assert (may_need_excess_precision && common);
2d2e923f 11579 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11580 }
11581 }
400fbf9f 11582
2d2e923f
MLI
11583 if (!converted)
11584 {
68fca595
MP
11585 op0 = ep_convert_and_check (location, result_type, op0,
11586 semantic_result_type);
11587 op1 = ep_convert_and_check (location, result_type, op1,
11588 semantic_result_type);
2d2e923f
MLI
11589
11590 /* This can happen if one operand has a vector type, and the other
11591 has a different type. */
11592 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11593 return error_mark_node;
11594 }
11595
f8ed5150
MP
11596 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11597 | SANITIZE_FLOAT_DIVIDE))
802ac282 11598 && do_ubsan_in_current_function ()
b2aaf235
MP
11599 && (doing_div_or_mod || doing_shift)
11600 && !require_constant_value)
de5a5fa1
MP
11601 {
11602 /* OP0 and/or OP1 might have side-effects. */
11603 op0 = c_save_expr (op0);
11604 op1 = c_save_expr (op1);
11605 op0 = c_fully_fold (op0, false, NULL);
11606 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11607 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11608 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11609 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11610 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11611 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11612 }
11613
c9f9eb5d 11614 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11615 if (int_const_or_overflow)
11616 ret = (require_constant_value
db3927fb
AH
11617 ? fold_build2_initializer_loc (location, resultcode, build_type,
11618 op0, op1)
11619 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11620 else
11621 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11622 if (final_type != 0)
11623 ret = convert (final_type, ret);
11624
11625 return_build_binary_op:
11626 gcc_assert (ret != error_mark_node);
928c19bb
JM
11627 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11628 ret = (int_operands
11629 ? note_integer_operands (ret)
11630 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11631 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11632 && !in_late_binary_op)
11633 ret = note_integer_operands (ret);
2d2e923f
MLI
11634 if (semantic_result_type)
11635 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11636 protected_set_expr_location (ret, location);
de5a5fa1 11637
a24d975c 11638 if (instrument_expr != NULL)
de5a5fa1
MP
11639 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11640 instrument_expr, ret);
11641
c9f9eb5d 11642 return ret;
400fbf9f 11643}
85498824
JM
11644
11645
11646/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11647 purpose. LOCATION is the source location for the expression. */
85498824
JM
11648
11649tree
ba47d38d 11650c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11651{
928c19bb
JM
11652 bool int_const, int_operands;
11653
85498824
JM
11654 switch (TREE_CODE (TREE_TYPE (expr)))
11655 {
11656 case ARRAY_TYPE:
ba47d38d 11657 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11658 return error_mark_node;
11659
11660 case RECORD_TYPE:
ba47d38d 11661 error_at (location, "used struct type value where scalar is required");
85498824
JM
11662 return error_mark_node;
11663
11664 case UNION_TYPE:
ba47d38d 11665 error_at (location, "used union type value where scalar is required");
85498824
JM
11666 return error_mark_node;
11667
04af8788
NP
11668 case VOID_TYPE:
11669 error_at (location, "void value not ignored as it ought to be");
11670 return error_mark_node;
11671
1807ffc1
MS
11672 case POINTER_TYPE:
11673 if (reject_gcc_builtin (expr))
11674 return error_mark_node;
11675 break;
11676
46bdb9cf
JM
11677 case FUNCTION_TYPE:
11678 gcc_unreachable ();
11679
d246ab4f
AS
11680 case VECTOR_TYPE:
11681 error_at (location, "used vector type where scalar is required");
11682 return error_mark_node;
11683
85498824
JM
11684 default:
11685 break;
11686 }
11687
928c19bb
JM
11688 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11689 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11690 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11691 {
11692 expr = remove_c_maybe_const_expr (expr);
11693 expr = build2 (NE_EXPR, integer_type_node, expr,
11694 convert (TREE_TYPE (expr), integer_zero_node));
11695 expr = note_integer_operands (expr);
11696 }
11697 else
11698 /* ??? Should we also give an error for vectors rather than leaving
11699 those to give errors later? */
11700 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11701
11702 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11703 {
11704 if (TREE_OVERFLOW (expr))
11705 return expr;
11706 else
11707 return note_integer_operands (expr);
11708 }
11709 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11710 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11711 return expr;
85498824 11712}
73f397d4
JM
11713\f
11714
11715/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11716 required. */
11717
11718tree
51eed280 11719c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11720{
11721 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11722 {
11723 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11724 /* Executing a compound literal inside a function reinitializes
11725 it. */
11726 if (!TREE_STATIC (decl))
11727 *se = true;
11728 return decl;
11729 }
11730 else
11731 return expr;
11732}
953ff289 11733\f
88bae6f4
TS
11734/* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11735 statement. LOC is the location of the construct. */
41dbbb37
TS
11736
11737tree
88bae6f4
TS
11738c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11739 tree clauses)
41dbbb37 11740{
88bae6f4 11741 body = c_end_compound_stmt (loc, body, true);
41dbbb37 11742
88bae6f4 11743 tree stmt = make_node (code);
41dbbb37 11744 TREE_TYPE (stmt) = void_type_node;
88bae6f4
TS
11745 OMP_BODY (stmt) = body;
11746 OMP_CLAUSES (stmt) = clauses;
41dbbb37
TS
11747 SET_EXPR_LOCATION (stmt, loc);
11748
11749 return add_stmt (stmt);
11750}
11751
11752/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11753 statement. LOC is the location of the OACC_DATA. */
11754
11755tree
11756c_finish_oacc_data (location_t loc, tree clauses, tree block)
11757{
11758 tree stmt;
11759
11760 block = c_end_compound_stmt (loc, block, true);
11761
11762 stmt = make_node (OACC_DATA);
11763 TREE_TYPE (stmt) = void_type_node;
11764 OACC_DATA_CLAUSES (stmt) = clauses;
11765 OACC_DATA_BODY (stmt) = block;
11766 SET_EXPR_LOCATION (stmt, loc);
11767
11768 return add_stmt (stmt);
11769}
11770
37d5ad46
JB
11771/* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11772 statement. LOC is the location of the OACC_HOST_DATA. */
11773
11774tree
11775c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
11776{
11777 tree stmt;
11778
11779 block = c_end_compound_stmt (loc, block, true);
11780
11781 stmt = make_node (OACC_HOST_DATA);
11782 TREE_TYPE (stmt) = void_type_node;
11783 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
11784 OACC_HOST_DATA_BODY (stmt) = block;
11785 SET_EXPR_LOCATION (stmt, loc);
11786
11787 return add_stmt (stmt);
11788}
11789
c0220ea4 11790/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11791
11792tree
11793c_begin_omp_parallel (void)
11794{
11795 tree block;
11796
11797 keep_next_level ();
11798 block = c_begin_compound_stmt (true);
11799
11800 return block;
11801}
11802
c2255bc4
AH
11803/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11804 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11805
953ff289 11806tree
c2255bc4 11807c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11808{
11809 tree stmt;
11810
c2255bc4 11811 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11812
11813 stmt = make_node (OMP_PARALLEL);
11814 TREE_TYPE (stmt) = void_type_node;
11815 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11816 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11817 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11818
11819 return add_stmt (stmt);
11820}
11821
a68ab351
JJ
11822/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11823
11824tree
11825c_begin_omp_task (void)
11826{
11827 tree block;
11828
11829 keep_next_level ();
11830 block = c_begin_compound_stmt (true);
11831
11832 return block;
11833}
11834
c2255bc4
AH
11835/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11836 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11837
11838tree
c2255bc4 11839c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11840{
11841 tree stmt;
11842
c2255bc4 11843 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11844
11845 stmt = make_node (OMP_TASK);
11846 TREE_TYPE (stmt) = void_type_node;
11847 OMP_TASK_CLAUSES (stmt) = clauses;
11848 OMP_TASK_BODY (stmt) = block;
c2255bc4 11849 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11850
11851 return add_stmt (stmt);
11852}
11853
acf0174b
JJ
11854/* Generate GOMP_cancel call for #pragma omp cancel. */
11855
11856void
11857c_finish_omp_cancel (location_t loc, tree clauses)
11858{
11859 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11860 int mask = 0;
11861 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11862 mask = 1;
11863 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11864 mask = 2;
11865 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11866 mask = 4;
11867 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11868 mask = 8;
11869 else
11870 {
11871 error_at (loc, "%<#pragma omp cancel must specify one of "
11872 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11873 "clauses");
11874 return;
11875 }
11876 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11877 if (ifc != NULL_TREE)
11878 {
11879 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11880 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11881 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11882 build_zero_cst (type));
11883 }
11884 else
11885 ifc = boolean_true_node;
11886 tree stmt = build_call_expr_loc (loc, fn, 2,
11887 build_int_cst (integer_type_node, mask),
11888 ifc);
11889 add_stmt (stmt);
11890}
11891
11892/* Generate GOMP_cancellation_point call for
11893 #pragma omp cancellation point. */
11894
11895void
11896c_finish_omp_cancellation_point (location_t loc, tree clauses)
11897{
11898 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11899 int mask = 0;
11900 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11901 mask = 1;
11902 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11903 mask = 2;
11904 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11905 mask = 4;
11906 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11907 mask = 8;
11908 else
11909 {
11910 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11911 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11912 "clauses");
11913 return;
11914 }
11915 tree stmt = build_call_expr_loc (loc, fn, 1,
11916 build_int_cst (integer_type_node, mask));
11917 add_stmt (stmt);
11918}
11919
11920/* Helper function for handle_omp_array_sections. Called recursively
11921 to handle multiple array-section-subscripts. C is the clause,
11922 T current expression (initially OMP_CLAUSE_DECL), which is either
11923 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11924 expression if specified, TREE_VALUE length expression if specified,
11925 TREE_CHAIN is what it has been specified after, or some decl.
11926 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11927 set to true if any of the array-section-subscript could have length
11928 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11929 first array-section-subscript which is known not to have length
11930 of one. Given say:
11931 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11932 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11933 all are or may have length of 1, array-section-subscript [:2] is the
d9a6bd32 11934 first one known not to have length 1. For array-section-subscript
acf0174b
JJ
11935 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11936 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11937 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11938 case though, as some lengths could be zero. */
11939
11940static tree
11941handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
d9a6bd32 11942 bool &maybe_zero_len, unsigned int &first_non_one,
e46c7770 11943 enum c_omp_region_type ort)
acf0174b
JJ
11944{
11945 tree ret, low_bound, length, type;
11946 if (TREE_CODE (t) != TREE_LIST)
11947 {
7a0ca710 11948 if (error_operand_p (t))
acf0174b 11949 return error_mark_node;
d9a6bd32
JJ
11950 ret = t;
11951 if (TREE_CODE (t) == COMPONENT_REF
e46c7770 11952 && ort == C_ORT_OMP
d9a6bd32
JJ
11953 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
11954 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
11955 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
11956 {
11957 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
11958 {
11959 error_at (OMP_CLAUSE_LOCATION (c),
11960 "bit-field %qE in %qs clause",
11961 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11962 return error_mark_node;
11963 }
11964 while (TREE_CODE (t) == COMPONENT_REF)
11965 {
11966 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
11967 {
11968 error_at (OMP_CLAUSE_LOCATION (c),
11969 "%qE is a member of a union", t);
11970 return error_mark_node;
11971 }
11972 t = TREE_OPERAND (t, 0);
11973 }
11974 }
0ae9bd27 11975 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
11976 {
11977 if (DECL_P (t))
11978 error_at (OMP_CLAUSE_LOCATION (c),
11979 "%qD is not a variable in %qs clause", t,
11980 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11981 else
11982 error_at (OMP_CLAUSE_LOCATION (c),
11983 "%qE is not a variable in %qs clause", t,
11984 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11985 return error_mark_node;
11986 }
11987 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
0ae9bd27 11988 && VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
11989 {
11990 error_at (OMP_CLAUSE_LOCATION (c),
11991 "%qD is threadprivate variable in %qs clause", t,
11992 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11993 return error_mark_node;
11994 }
d9a6bd32 11995 return ret;
acf0174b
JJ
11996 }
11997
11998 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
e46c7770 11999 maybe_zero_len, first_non_one, ort);
acf0174b
JJ
12000 if (ret == error_mark_node || ret == NULL_TREE)
12001 return ret;
12002
12003 type = TREE_TYPE (ret);
12004 low_bound = TREE_PURPOSE (t);
12005 length = TREE_VALUE (t);
12006
12007 if (low_bound == error_mark_node || length == error_mark_node)
12008 return error_mark_node;
12009
12010 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12011 {
12012 error_at (OMP_CLAUSE_LOCATION (c),
12013 "low bound %qE of array section does not have integral type",
12014 low_bound);
12015 return error_mark_node;
12016 }
12017 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12018 {
12019 error_at (OMP_CLAUSE_LOCATION (c),
12020 "length %qE of array section does not have integral type",
12021 length);
12022 return error_mark_node;
12023 }
12024 if (low_bound
12025 && TREE_CODE (low_bound) == INTEGER_CST
12026 && TYPE_PRECISION (TREE_TYPE (low_bound))
12027 > TYPE_PRECISION (sizetype))
12028 low_bound = fold_convert (sizetype, low_bound);
12029 if (length
12030 && TREE_CODE (length) == INTEGER_CST
12031 && TYPE_PRECISION (TREE_TYPE (length))
12032 > TYPE_PRECISION (sizetype))
12033 length = fold_convert (sizetype, length);
12034 if (low_bound == NULL_TREE)
12035 low_bound = integer_zero_node;
12036
12037 if (length != NULL_TREE)
12038 {
12039 if (!integer_nonzerop (length))
d9a6bd32
JJ
12040 {
12041 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12042 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12043 {
12044 if (integer_zerop (length))
12045 {
12046 error_at (OMP_CLAUSE_LOCATION (c),
12047 "zero length array section in %qs clause",
12048 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12049 return error_mark_node;
12050 }
12051 }
12052 else
12053 maybe_zero_len = true;
12054 }
acf0174b
JJ
12055 if (first_non_one == types.length ()
12056 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12057 first_non_one++;
12058 }
12059 if (TREE_CODE (type) == ARRAY_TYPE)
12060 {
12061 if (length == NULL_TREE
12062 && (TYPE_DOMAIN (type) == NULL_TREE
12063 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12064 {
12065 error_at (OMP_CLAUSE_LOCATION (c),
12066 "for unknown bound array type length expression must "
12067 "be specified");
12068 return error_mark_node;
12069 }
12070 if (TREE_CODE (low_bound) == INTEGER_CST
12071 && tree_int_cst_sgn (low_bound) == -1)
12072 {
12073 error_at (OMP_CLAUSE_LOCATION (c),
12074 "negative low bound in array section in %qs clause",
12075 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12076 return error_mark_node;
12077 }
12078 if (length != NULL_TREE
12079 && TREE_CODE (length) == INTEGER_CST
12080 && tree_int_cst_sgn (length) == -1)
12081 {
12082 error_at (OMP_CLAUSE_LOCATION (c),
12083 "negative length in array section in %qs clause",
12084 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12085 return error_mark_node;
12086 }
12087 if (TYPE_DOMAIN (type)
12088 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12089 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12090 == INTEGER_CST)
12091 {
12092 tree size = size_binop (PLUS_EXPR,
12093 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12094 size_one_node);
12095 if (TREE_CODE (low_bound) == INTEGER_CST)
12096 {
12097 if (tree_int_cst_lt (size, low_bound))
12098 {
12099 error_at (OMP_CLAUSE_LOCATION (c),
12100 "low bound %qE above array section size "
12101 "in %qs clause", low_bound,
12102 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12103 return error_mark_node;
12104 }
12105 if (tree_int_cst_equal (size, low_bound))
d9a6bd32
JJ
12106 {
12107 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12108 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12109 {
12110 error_at (OMP_CLAUSE_LOCATION (c),
12111 "zero length array section in %qs clause",
12112 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12113 return error_mark_node;
12114 }
12115 maybe_zero_len = true;
12116 }
acf0174b
JJ
12117 else if (length == NULL_TREE
12118 && first_non_one == types.length ()
12119 && tree_int_cst_equal
12120 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12121 low_bound))
12122 first_non_one++;
12123 }
12124 else if (length == NULL_TREE)
12125 {
d9a6bd32
JJ
12126 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12127 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12128 maybe_zero_len = true;
acf0174b
JJ
12129 if (first_non_one == types.length ())
12130 first_non_one++;
12131 }
12132 if (length && TREE_CODE (length) == INTEGER_CST)
12133 {
12134 if (tree_int_cst_lt (size, length))
12135 {
12136 error_at (OMP_CLAUSE_LOCATION (c),
12137 "length %qE above array section size "
12138 "in %qs clause", length,
12139 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12140 return error_mark_node;
12141 }
12142 if (TREE_CODE (low_bound) == INTEGER_CST)
12143 {
12144 tree lbpluslen
12145 = size_binop (PLUS_EXPR,
12146 fold_convert (sizetype, low_bound),
12147 fold_convert (sizetype, length));
12148 if (TREE_CODE (lbpluslen) == INTEGER_CST
12149 && tree_int_cst_lt (size, lbpluslen))
12150 {
12151 error_at (OMP_CLAUSE_LOCATION (c),
12152 "high bound %qE above array section size "
12153 "in %qs clause", lbpluslen,
12154 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12155 return error_mark_node;
12156 }
12157 }
12158 }
12159 }
12160 else if (length == NULL_TREE)
12161 {
d9a6bd32
JJ
12162 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12163 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12164 maybe_zero_len = true;
acf0174b
JJ
12165 if (first_non_one == types.length ())
12166 first_non_one++;
12167 }
12168
12169 /* For [lb:] we will need to evaluate lb more than once. */
12170 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12171 {
12172 tree lb = c_save_expr (low_bound);
12173 if (lb != low_bound)
12174 {
12175 TREE_PURPOSE (t) = lb;
12176 low_bound = lb;
12177 }
12178 }
12179 }
12180 else if (TREE_CODE (type) == POINTER_TYPE)
12181 {
12182 if (length == NULL_TREE)
12183 {
12184 error_at (OMP_CLAUSE_LOCATION (c),
12185 "for pointer type length expression must be specified");
12186 return error_mark_node;
12187 }
d9a6bd32
JJ
12188 if (length != NULL_TREE
12189 && TREE_CODE (length) == INTEGER_CST
12190 && tree_int_cst_sgn (length) == -1)
12191 {
12192 error_at (OMP_CLAUSE_LOCATION (c),
12193 "negative length in array section in %qs clause",
12194 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12195 return error_mark_node;
12196 }
acf0174b
JJ
12197 /* If there is a pointer type anywhere but in the very first
12198 array-section-subscript, the array section can't be contiguous. */
12199 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12200 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12201 {
12202 error_at (OMP_CLAUSE_LOCATION (c),
12203 "array section is not contiguous in %qs clause",
12204 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12205 return error_mark_node;
12206 }
12207 }
12208 else
12209 {
12210 error_at (OMP_CLAUSE_LOCATION (c),
12211 "%qE does not have pointer or array type", ret);
12212 return error_mark_node;
12213 }
12214 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12215 types.safe_push (TREE_TYPE (ret));
12216 /* We will need to evaluate lb more than once. */
12217 tree lb = c_save_expr (low_bound);
12218 if (lb != low_bound)
12219 {
12220 TREE_PURPOSE (t) = lb;
12221 low_bound = lb;
12222 }
12223 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12224 return ret;
12225}
12226
12227/* Handle array sections for clause C. */
12228
12229static bool
e46c7770 12230handle_omp_array_sections (tree c, enum c_omp_region_type ort)
acf0174b
JJ
12231{
12232 bool maybe_zero_len = false;
12233 unsigned int first_non_one = 0;
d9a6bd32 12234 auto_vec<tree, 10> types;
acf0174b 12235 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
d9a6bd32 12236 maybe_zero_len, first_non_one,
e46c7770 12237 ort);
acf0174b 12238 if (first == error_mark_node)
d9a6bd32 12239 return true;
acf0174b 12240 if (first == NULL_TREE)
d9a6bd32 12241 return false;
acf0174b
JJ
12242 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12243 {
12244 tree t = OMP_CLAUSE_DECL (c);
12245 tree tem = NULL_TREE;
acf0174b
JJ
12246 /* Need to evaluate side effects in the length expressions
12247 if any. */
12248 while (TREE_CODE (t) == TREE_LIST)
12249 {
12250 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12251 {
12252 if (tem == NULL_TREE)
12253 tem = TREE_VALUE (t);
12254 else
12255 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12256 TREE_VALUE (t), tem);
12257 }
12258 t = TREE_CHAIN (t);
12259 }
12260 if (tem)
12261 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12262 first = c_fully_fold (first, false, NULL);
12263 OMP_CLAUSE_DECL (c) = first;
12264 }
12265 else
12266 {
12267 unsigned int num = types.length (), i;
12268 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12269 tree condition = NULL_TREE;
12270
12271 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12272 maybe_zero_len = true;
12273
12274 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12275 t = TREE_CHAIN (t))
12276 {
12277 tree low_bound = TREE_PURPOSE (t);
12278 tree length = TREE_VALUE (t);
12279
12280 i--;
12281 if (low_bound
12282 && TREE_CODE (low_bound) == INTEGER_CST
12283 && TYPE_PRECISION (TREE_TYPE (low_bound))
12284 > TYPE_PRECISION (sizetype))
12285 low_bound = fold_convert (sizetype, low_bound);
12286 if (length
12287 && TREE_CODE (length) == INTEGER_CST
12288 && TYPE_PRECISION (TREE_TYPE (length))
12289 > TYPE_PRECISION (sizetype))
12290 length = fold_convert (sizetype, length);
12291 if (low_bound == NULL_TREE)
12292 low_bound = integer_zero_node;
12293 if (!maybe_zero_len && i > first_non_one)
12294 {
12295 if (integer_nonzerop (low_bound))
12296 goto do_warn_noncontiguous;
12297 if (length != NULL_TREE
12298 && TREE_CODE (length) == INTEGER_CST
12299 && TYPE_DOMAIN (types[i])
12300 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12301 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12302 == INTEGER_CST)
12303 {
12304 tree size;
12305 size = size_binop (PLUS_EXPR,
12306 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12307 size_one_node);
12308 if (!tree_int_cst_equal (length, size))
12309 {
12310 do_warn_noncontiguous:
12311 error_at (OMP_CLAUSE_LOCATION (c),
12312 "array section is not contiguous in %qs "
12313 "clause",
12314 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
acf0174b
JJ
12315 return true;
12316 }
12317 }
12318 if (length != NULL_TREE
12319 && TREE_SIDE_EFFECTS (length))
12320 {
12321 if (side_effects == NULL_TREE)
12322 side_effects = length;
12323 else
12324 side_effects = build2 (COMPOUND_EXPR,
12325 TREE_TYPE (side_effects),
12326 length, side_effects);
12327 }
12328 }
12329 else
12330 {
12331 tree l;
12332
d9a6bd32
JJ
12333 if (i > first_non_one
12334 && ((length && integer_nonzerop (length))
12335 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
acf0174b
JJ
12336 continue;
12337 if (length)
12338 l = fold_convert (sizetype, length);
12339 else
12340 {
12341 l = size_binop (PLUS_EXPR,
12342 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12343 size_one_node);
12344 l = size_binop (MINUS_EXPR, l,
12345 fold_convert (sizetype, low_bound));
12346 }
12347 if (i > first_non_one)
12348 {
12349 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12350 size_zero_node);
12351 if (condition == NULL_TREE)
12352 condition = l;
12353 else
12354 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12355 l, condition);
12356 }
12357 else if (size == NULL_TREE)
12358 {
12359 size = size_in_bytes (TREE_TYPE (types[i]));
d9a6bd32
JJ
12360 tree eltype = TREE_TYPE (types[num - 1]);
12361 while (TREE_CODE (eltype) == ARRAY_TYPE)
12362 eltype = TREE_TYPE (eltype);
12363 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12364 {
12365 if (integer_zerop (size)
12366 || integer_zerop (size_in_bytes (eltype)))
12367 {
12368 error_at (OMP_CLAUSE_LOCATION (c),
12369 "zero length array section in %qs clause",
12370 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12371 return error_mark_node;
12372 }
12373 size = size_binop (EXACT_DIV_EXPR, size,
12374 size_in_bytes (eltype));
12375 }
acf0174b
JJ
12376 size = size_binop (MULT_EXPR, size, l);
12377 if (condition)
12378 size = fold_build3 (COND_EXPR, sizetype, condition,
12379 size, size_zero_node);
12380 }
12381 else
12382 size = size_binop (MULT_EXPR, size, l);
12383 }
12384 }
acf0174b
JJ
12385 if (side_effects)
12386 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
d9a6bd32
JJ
12387 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12388 {
12389 size = size_binop (MINUS_EXPR, size, size_one_node);
12390 size = c_fully_fold (size, false, NULL);
12391 tree index_type = build_index_type (size);
12392 tree eltype = TREE_TYPE (first);
12393 while (TREE_CODE (eltype) == ARRAY_TYPE)
12394 eltype = TREE_TYPE (eltype);
12395 tree type = build_array_type (eltype, index_type);
12396 tree ptype = build_pointer_type (eltype);
12397 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12398 t = build_fold_addr_expr (t);
e01d41e5
JJ
12399 tree t2 = build_fold_addr_expr (first);
12400 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12401 ptrdiff_type_node, t2);
12402 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12403 ptrdiff_type_node, t2,
12404 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12405 ptrdiff_type_node, t));
12406 t2 = c_fully_fold (t2, false, NULL);
12407 if (tree_fits_shwi_p (t2))
12408 t = build2 (MEM_REF, type, t,
12409 build_int_cst (ptype, tree_to_shwi (t2)));
12410 else
12411 {
12412 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12413 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12414 TREE_TYPE (t), t, t2);
12415 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12416 }
d9a6bd32
JJ
12417 OMP_CLAUSE_DECL (c) = t;
12418 return false;
12419 }
acf0174b
JJ
12420 first = c_fully_fold (first, false, NULL);
12421 OMP_CLAUSE_DECL (c) = first;
12422 if (size)
12423 size = c_fully_fold (size, false, NULL);
12424 OMP_CLAUSE_SIZE (c) = size;
d9a6bd32
JJ
12425 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12426 || (TREE_CODE (t) == COMPONENT_REF
12427 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
acf0174b 12428 return false;
41dbbb37 12429 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
e46c7770 12430 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
d9a6bd32
JJ
12431 switch (OMP_CLAUSE_MAP_KIND (c))
12432 {
12433 case GOMP_MAP_ALLOC:
12434 case GOMP_MAP_TO:
12435 case GOMP_MAP_FROM:
12436 case GOMP_MAP_TOFROM:
12437 case GOMP_MAP_ALWAYS_TO:
12438 case GOMP_MAP_ALWAYS_FROM:
12439 case GOMP_MAP_ALWAYS_TOFROM:
12440 case GOMP_MAP_RELEASE:
12441 case GOMP_MAP_DELETE:
12442 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12443 break;
12444 default:
12445 break;
12446 }
acf0174b 12447 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
e46c7770 12448 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
e01d41e5
JJ
12449 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12450 else if (TREE_CODE (t) == COMPONENT_REF)
12451 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12452 else
12453 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12454 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12455 && !c_mark_addressable (t))
acf0174b
JJ
12456 return false;
12457 OMP_CLAUSE_DECL (c2) = t;
12458 t = build_fold_addr_expr (first);
12459 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12460 tree ptr = OMP_CLAUSE_DECL (c2);
12461 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12462 ptr = build_fold_addr_expr (ptr);
12463 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12464 ptrdiff_type_node, t,
12465 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12466 ptrdiff_type_node, ptr));
12467 t = c_fully_fold (t, false, NULL);
12468 OMP_CLAUSE_SIZE (c2) = t;
12469 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12470 OMP_CLAUSE_CHAIN (c) = c2;
12471 }
12472 return false;
12473}
12474
12475/* Helper function of finish_omp_clauses. Clone STMT as if we were making
12476 an inline call. But, remap
12477 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12478 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12479
12480static tree
12481c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12482 tree decl, tree placeholder)
12483{
12484 copy_body_data id;
b787e7a2 12485 hash_map<tree, tree> decl_map;
acf0174b 12486
b787e7a2
TS
12487 decl_map.put (omp_decl1, placeholder);
12488 decl_map.put (omp_decl2, decl);
acf0174b
JJ
12489 memset (&id, 0, sizeof (id));
12490 id.src_fn = DECL_CONTEXT (omp_decl1);
12491 id.dst_fn = current_function_decl;
12492 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 12493 id.decl_map = &decl_map;
acf0174b
JJ
12494
12495 id.copy_decl = copy_decl_no_change;
12496 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12497 id.transform_new_cfg = true;
12498 id.transform_return_to_modify = false;
12499 id.transform_lang_insert_block = NULL;
12500 id.eh_lp_nr = 0;
12501 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
12502 return stmt;
12503}
12504
12505/* Helper function of c_finish_omp_clauses, called via walk_tree.
12506 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12507
12508static tree
12509c_find_omp_placeholder_r (tree *tp, int *, void *data)
12510{
12511 if (*tp == (tree) data)
12512 return *tp;
12513 return NULL_TREE;
12514}
12515
41dbbb37 12516/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
12517 Remove any elements from the list that are invalid. */
12518
12519tree
77886428 12520c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
953ff289
DN
12521{
12522 bitmap_head generic_head, firstprivate_head, lastprivate_head;
e46c7770 12523 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
d9a6bd32
JJ
12524 tree c, t, type, *pc;
12525 tree simdlen = NULL_TREE, safelen = NULL_TREE;
acf0174b
JJ
12526 bool branch_seen = false;
12527 bool copyprivate_seen = false;
e01d41e5 12528 bool linear_variable_step_check = false;
acf0174b 12529 tree *nowait_clause = NULL;
e01d41e5
JJ
12530 bool ordered_seen = false;
12531 tree schedule_clause = NULL_TREE;
b605f663 12532 bool oacc_async = false;
953ff289
DN
12533
12534 bitmap_obstack_initialize (NULL);
12535 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12536 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12537 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 12538 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
d9a6bd32
JJ
12539 bitmap_initialize (&map_head, &bitmap_default_obstack);
12540 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
e46c7770 12541 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
953ff289 12542
b605f663
CLT
12543 if (ort & C_ORT_ACC)
12544 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12545 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12546 {
12547 oacc_async = true;
12548 break;
12549 }
12550
953ff289
DN
12551 for (pc = &clauses, c = clauses; c ; c = *pc)
12552 {
12553 bool remove = false;
12554 bool need_complete = false;
12555 bool need_implicitly_determined = false;
12556
aaf46ef9 12557 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
12558 {
12559 case OMP_CLAUSE_SHARED:
953ff289
DN
12560 need_implicitly_determined = true;
12561 goto check_dup_generic;
12562
12563 case OMP_CLAUSE_PRIVATE:
953ff289
DN
12564 need_complete = true;
12565 need_implicitly_determined = true;
12566 goto check_dup_generic;
12567
12568 case OMP_CLAUSE_REDUCTION:
953ff289
DN
12569 need_implicitly_determined = true;
12570 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12571 if (TREE_CODE (t) == TREE_LIST)
12572 {
e46c7770 12573 if (handle_omp_array_sections (c, ort))
d9a6bd32
JJ
12574 {
12575 remove = true;
12576 break;
12577 }
12578
12579 t = OMP_CLAUSE_DECL (c);
12580 }
4f2e1536 12581 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
d9a6bd32
JJ
12582 if (t == error_mark_node)
12583 {
12584 remove = true;
12585 break;
12586 }
b605f663
CLT
12587 if (oacc_async)
12588 c_mark_addressable (t);
d9a6bd32
JJ
12589 type = TREE_TYPE (t);
12590 if (TREE_CODE (t) == MEM_REF)
12591 type = TREE_TYPE (type);
12592 if (TREE_CODE (type) == ARRAY_TYPE)
12593 {
12594 tree oatype = type;
12595 gcc_assert (TREE_CODE (t) != MEM_REF);
12596 while (TREE_CODE (type) == ARRAY_TYPE)
12597 type = TREE_TYPE (type);
12598 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12599 {
12600 error_at (OMP_CLAUSE_LOCATION (c),
12601 "%qD in %<reduction%> clause is a zero size array",
12602 t);
12603 remove = true;
12604 break;
12605 }
12606 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12607 TYPE_SIZE_UNIT (type));
12608 if (integer_zerop (size))
12609 {
12610 error_at (OMP_CLAUSE_LOCATION (c),
12611 "%qD in %<reduction%> clause is a zero size array",
12612 t);
12613 remove = true;
12614 break;
12615 }
12616 size = size_binop (MINUS_EXPR, size, size_one_node);
12617 tree index_type = build_index_type (size);
12618 tree atype = build_array_type (type, index_type);
12619 tree ptype = build_pointer_type (type);
12620 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12621 t = build_fold_addr_expr (t);
12622 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12623 OMP_CLAUSE_DECL (c) = t;
12624 }
acf0174b 12625 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
d9a6bd32
JJ
12626 && (FLOAT_TYPE_P (type)
12627 || TREE_CODE (type) == COMPLEX_TYPE))
953ff289
DN
12628 {
12629 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12630 const char *r_name = NULL;
12631
12632 switch (r_code)
12633 {
12634 case PLUS_EXPR:
12635 case MULT_EXPR:
12636 case MINUS_EXPR:
652fea39 12637 break;
20906c66 12638 case MIN_EXPR:
d9a6bd32 12639 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39
JJ
12640 r_name = "min";
12641 break;
20906c66 12642 case MAX_EXPR:
d9a6bd32 12643 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39 12644 r_name = "max";
953ff289
DN
12645 break;
12646 case BIT_AND_EXPR:
12647 r_name = "&";
12648 break;
12649 case BIT_XOR_EXPR:
12650 r_name = "^";
12651 break;
12652 case BIT_IOR_EXPR:
12653 r_name = "|";
12654 break;
12655 case TRUTH_ANDIF_EXPR:
d9a6bd32 12656 if (FLOAT_TYPE_P (type))
652fea39 12657 r_name = "&&";
953ff289
DN
12658 break;
12659 case TRUTH_ORIF_EXPR:
d9a6bd32 12660 if (FLOAT_TYPE_P (type))
652fea39 12661 r_name = "||";
953ff289
DN
12662 break;
12663 default:
12664 gcc_unreachable ();
12665 }
12666 if (r_name)
12667 {
c2255bc4
AH
12668 error_at (OMP_CLAUSE_LOCATION (c),
12669 "%qE has invalid type for %<reduction(%s)%>",
12670 t, r_name);
953ff289 12671 remove = true;
ee1d5a02 12672 break;
953ff289
DN
12673 }
12674 }
acf0174b
JJ
12675 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12676 {
12677 error_at (OMP_CLAUSE_LOCATION (c),
d9a6bd32 12678 "user defined reduction not found for %qE", t);
acf0174b 12679 remove = true;
ee1d5a02 12680 break;
acf0174b
JJ
12681 }
12682 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12683 {
12684 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
d9a6bd32 12685 type = TYPE_MAIN_VARIANT (type);
acf0174b
JJ
12686 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12687 VAR_DECL, NULL_TREE, type);
d9a6bd32 12688 tree decl_placeholder = NULL_TREE;
acf0174b
JJ
12689 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12690 DECL_ARTIFICIAL (placeholder) = 1;
12691 DECL_IGNORED_P (placeholder) = 1;
d9a6bd32
JJ
12692 if (TREE_CODE (t) == MEM_REF)
12693 {
12694 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12695 VAR_DECL, NULL_TREE, type);
12696 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12697 DECL_ARTIFICIAL (decl_placeholder) = 1;
12698 DECL_IGNORED_P (decl_placeholder) = 1;
12699 }
acf0174b
JJ
12700 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12701 c_mark_addressable (placeholder);
12702 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
d9a6bd32
JJ
12703 c_mark_addressable (decl_placeholder ? decl_placeholder
12704 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12705 OMP_CLAUSE_REDUCTION_MERGE (c)
12706 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12707 TREE_VEC_ELT (list, 0),
12708 TREE_VEC_ELT (list, 1),
d9a6bd32
JJ
12709 decl_placeholder ? decl_placeholder
12710 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b
JJ
12711 OMP_CLAUSE_REDUCTION_MERGE (c)
12712 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12713 void_type_node, NULL_TREE,
d9a6bd32 12714 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
acf0174b
JJ
12715 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12716 if (TREE_VEC_LENGTH (list) == 6)
12717 {
12718 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
d9a6bd32
JJ
12719 c_mark_addressable (decl_placeholder ? decl_placeholder
12720 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12721 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12722 c_mark_addressable (placeholder);
12723 tree init = TREE_VEC_ELT (list, 5);
12724 if (init == error_mark_node)
12725 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12726 OMP_CLAUSE_REDUCTION_INIT (c)
12727 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12728 TREE_VEC_ELT (list, 3),
d9a6bd32
JJ
12729 decl_placeholder ? decl_placeholder
12730 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b 12731 if (TREE_VEC_ELT (list, 5) == error_mark_node)
d9a6bd32
JJ
12732 {
12733 tree v = decl_placeholder ? decl_placeholder : t;
12734 OMP_CLAUSE_REDUCTION_INIT (c)
12735 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12736 OMP_CLAUSE_REDUCTION_INIT (c));
12737 }
acf0174b
JJ
12738 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12739 c_find_omp_placeholder_r,
12740 placeholder, NULL))
12741 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12742 }
12743 else
12744 {
12745 tree init;
d9a6bd32
JJ
12746 tree v = decl_placeholder ? decl_placeholder : t;
12747 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12748 init = build_constructor (TREE_TYPE (v), NULL);
acf0174b 12749 else
d9a6bd32 12750 init = fold_convert (TREE_TYPE (v), integer_zero_node);
acf0174b 12751 OMP_CLAUSE_REDUCTION_INIT (c)
d9a6bd32 12752 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
acf0174b
JJ
12753 }
12754 OMP_CLAUSE_REDUCTION_INIT (c)
12755 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12756 void_type_node, NULL_TREE,
12757 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12758 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12759 }
d9a6bd32
JJ
12760 if (TREE_CODE (t) == MEM_REF)
12761 {
12762 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12763 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12764 != INTEGER_CST)
12765 {
12766 sorry ("variable length element type in array "
12767 "%<reduction%> clause");
12768 remove = true;
12769 break;
12770 }
12771 t = TREE_OPERAND (t, 0);
e01d41e5
JJ
12772 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12773 t = TREE_OPERAND (t, 0);
d9a6bd32
JJ
12774 if (TREE_CODE (t) == ADDR_EXPR)
12775 t = TREE_OPERAND (t, 0);
12776 }
12777 goto check_dup_generic_t;
953ff289
DN
12778
12779 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
12780 copyprivate_seen = true;
12781 if (nowait_clause)
12782 {
12783 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12784 "%<nowait%> clause must not be used together "
12785 "with %<copyprivate%>");
12786 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12787 nowait_clause = NULL;
12788 }
953ff289
DN
12789 goto check_dup_generic;
12790
12791 case OMP_CLAUSE_COPYIN:
953ff289 12792 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12793 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
953ff289 12794 {
c2255bc4
AH
12795 error_at (OMP_CLAUSE_LOCATION (c),
12796 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12797 remove = true;
ee1d5a02 12798 break;
953ff289
DN
12799 }
12800 goto check_dup_generic;
12801
acf0174b 12802 case OMP_CLAUSE_LINEAR:
77886428 12803 if (ort != C_ORT_OMP_DECLARE_SIMD)
d9a6bd32 12804 need_implicitly_determined = true;
acf0174b 12805 t = OMP_CLAUSE_DECL (c);
77886428 12806 if (ort != C_ORT_OMP_DECLARE_SIMD
d9a6bd32
JJ
12807 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
12808 {
12809 error_at (OMP_CLAUSE_LOCATION (c),
12810 "modifier should not be specified in %<linear%> "
12811 "clause on %<simd%> or %<for%> constructs");
12812 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
12813 }
77886428 12814 if (ort & C_ORT_CILK)
acf0174b 12815 {
477d4906
IV
12816 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12817 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
12818 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12819 {
12820 error_at (OMP_CLAUSE_LOCATION (c),
12821 "linear clause applied to non-integral, "
12822 "non-floating, non-pointer variable with type %qT",
12823 TREE_TYPE (t));
12824 remove = true;
12825 break;
12826 }
12827 }
12828 else
12829 {
12830 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12831 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12832 {
12833 error_at (OMP_CLAUSE_LOCATION (c),
12834 "linear clause applied to non-integral non-pointer "
12835 "variable with type %qT", TREE_TYPE (t));
12836 remove = true;
12837 break;
12838 }
acf0174b 12839 }
77886428 12840 if (ort == C_ORT_OMP_DECLARE_SIMD)
e01d41e5
JJ
12841 {
12842 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12843 if (TREE_CODE (s) == PARM_DECL)
12844 {
12845 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
12846 /* map_head bitmap is used as uniform_head if
12847 declare_simd. */
12848 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
12849 linear_variable_step_check = true;
12850 goto check_dup_generic;
12851 }
12852 if (TREE_CODE (s) != INTEGER_CST)
12853 {
12854 error_at (OMP_CLAUSE_LOCATION (c),
12855 "%<linear%> clause step %qE is neither constant "
12856 "nor a parameter", s);
12857 remove = true;
12858 break;
12859 }
12860 }
acf0174b
JJ
12861 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12862 {
12863 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12864 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12865 OMP_CLAUSE_DECL (c), s);
12866 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
12867 sizetype, fold_convert (sizetype, s),
12868 fold_convert
12869 (sizetype, OMP_CLAUSE_DECL (c)));
acf0174b
JJ
12870 if (s == error_mark_node)
12871 s = size_one_node;
12872 OMP_CLAUSE_LINEAR_STEP (c) = s;
12873 }
da6f124d
JJ
12874 else
12875 OMP_CLAUSE_LINEAR_STEP (c)
12876 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
12877 goto check_dup_generic;
12878
953ff289
DN
12879 check_dup_generic:
12880 t = OMP_CLAUSE_DECL (c);
d9a6bd32 12881 check_dup_generic_t:
0ae9bd27 12882 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12883 {
c2255bc4 12884 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
12885 "%qE is not a variable in clause %qs", t,
12886 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12887 remove = true;
12888 }
e46c7770
CP
12889 else if (ort == C_ORT_ACC
12890 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12891 {
12892 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
12893 {
12894 error ("%qD appears more than once in reduction clauses", t);
12895 remove = true;
12896 }
12897 else
12898 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
12899 }
953ff289
DN
12900 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12901 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12902 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12903 {
c2255bc4
AH
12904 error_at (OMP_CLAUSE_LOCATION (c),
12905 "%qE appears more than once in data clauses", t);
953ff289
DN
12906 remove = true;
12907 }
e01d41e5
JJ
12908 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12909 && bitmap_bit_p (&map_head, DECL_UID (t)))
12910 {
e46c7770
CP
12911 if (ort == C_ORT_ACC)
12912 error ("%qD appears more than once in data clauses", t);
12913 else
12914 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
12915 remove = true;
12916 }
953ff289
DN
12917 else
12918 bitmap_set_bit (&generic_head, DECL_UID (t));
12919 break;
12920
12921 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
12922 t = OMP_CLAUSE_DECL (c);
12923 need_complete = true;
12924 need_implicitly_determined = true;
0ae9bd27 12925 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12926 {
c2255bc4
AH
12927 error_at (OMP_CLAUSE_LOCATION (c),
12928 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
12929 remove = true;
12930 }
12931 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12932 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12933 {
c2255bc4
AH
12934 error_at (OMP_CLAUSE_LOCATION (c),
12935 "%qE appears more than once in data clauses", t);
953ff289
DN
12936 remove = true;
12937 }
e01d41e5
JJ
12938 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
12939 {
e46c7770
CP
12940 if (ort == C_ORT_ACC)
12941 error ("%qD appears more than once in data clauses", t);
12942 else
12943 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
12944 remove = true;
12945 }
953ff289
DN
12946 else
12947 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12948 break;
12949
12950 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
12951 t = OMP_CLAUSE_DECL (c);
12952 need_complete = true;
12953 need_implicitly_determined = true;
0ae9bd27 12954 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12955 {
c2255bc4
AH
12956 error_at (OMP_CLAUSE_LOCATION (c),
12957 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
12958 remove = true;
12959 }
12960 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12961 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12962 {
c2255bc4
AH
12963 error_at (OMP_CLAUSE_LOCATION (c),
12964 "%qE appears more than once in data clauses", t);
953ff289
DN
12965 remove = true;
12966 }
12967 else
12968 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12969 break;
12970
acf0174b
JJ
12971 case OMP_CLAUSE_ALIGNED:
12972 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12973 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12974 {
12975 error_at (OMP_CLAUSE_LOCATION (c),
12976 "%qE is not a variable in %<aligned%> clause", t);
12977 remove = true;
12978 }
5a9785fb
JJ
12979 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12980 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12981 {
12982 error_at (OMP_CLAUSE_LOCATION (c),
12983 "%qE in %<aligned%> clause is neither a pointer nor "
12984 "an array", t);
12985 remove = true;
12986 }
acf0174b
JJ
12987 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12988 {
12989 error_at (OMP_CLAUSE_LOCATION (c),
12990 "%qE appears more than once in %<aligned%> clauses",
12991 t);
12992 remove = true;
12993 }
12994 else
12995 bitmap_set_bit (&aligned_head, DECL_UID (t));
12996 break;
12997
12998 case OMP_CLAUSE_DEPEND:
12999 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
13000 if (t == NULL_TREE)
13001 {
13002 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13003 == OMP_CLAUSE_DEPEND_SOURCE);
13004 break;
13005 }
13006 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13007 {
13008 gcc_assert (TREE_CODE (t) == TREE_LIST);
13009 for (; t; t = TREE_CHAIN (t))
13010 {
13011 tree decl = TREE_VALUE (t);
13012 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13013 {
13014 tree offset = TREE_PURPOSE (t);
13015 bool neg = wi::neg_p ((wide_int) offset);
13016 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13017 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13018 neg ? MINUS_EXPR : PLUS_EXPR,
13019 decl, offset);
13020 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
13021 sizetype,
13022 fold_convert (sizetype, t2),
13023 fold_convert (sizetype, decl));
d9a6bd32
JJ
13024 if (t2 == error_mark_node)
13025 {
13026 remove = true;
13027 break;
13028 }
13029 TREE_PURPOSE (t) = t2;
13030 }
13031 }
13032 break;
13033 }
acf0174b
JJ
13034 if (TREE_CODE (t) == TREE_LIST)
13035 {
e46c7770 13036 if (handle_omp_array_sections (c, ort))
acf0174b
JJ
13037 remove = true;
13038 break;
13039 }
13040 if (t == error_mark_node)
13041 remove = true;
0ae9bd27 13042 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13043 {
13044 error_at (OMP_CLAUSE_LOCATION (c),
13045 "%qE is not a variable in %<depend%> clause", t);
13046 remove = true;
13047 }
13048 else if (!c_mark_addressable (t))
13049 remove = true;
13050 break;
13051
13052 case OMP_CLAUSE_MAP:
13053 case OMP_CLAUSE_TO:
13054 case OMP_CLAUSE_FROM:
41dbbb37 13055 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
13056 t = OMP_CLAUSE_DECL (c);
13057 if (TREE_CODE (t) == TREE_LIST)
13058 {
e46c7770 13059 if (handle_omp_array_sections (c, ort))
acf0174b
JJ
13060 remove = true;
13061 else
13062 {
13063 t = OMP_CLAUSE_DECL (c);
b17a8b07 13064 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13065 {
13066 error_at (OMP_CLAUSE_LOCATION (c),
13067 "array section does not have mappable type "
13068 "in %qs clause",
13069 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13070 remove = true;
13071 }
d9a6bd32
JJ
13072 while (TREE_CODE (t) == ARRAY_REF)
13073 t = TREE_OPERAND (t, 0);
13074 if (TREE_CODE (t) == COMPONENT_REF
13075 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13076 {
13077 while (TREE_CODE (t) == COMPONENT_REF)
13078 t = TREE_OPERAND (t, 0);
13079 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13080 break;
13081 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13082 {
13083 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13084 error ("%qD appears more than once in motion"
13085 " clauses", t);
e46c7770
CP
13086 else if (ort == C_ORT_ACC)
13087 error ("%qD appears more than once in data"
13088 " clauses", t);
d9a6bd32
JJ
13089 else
13090 error ("%qD appears more than once in map"
13091 " clauses", t);
13092 remove = true;
13093 }
13094 else
13095 {
13096 bitmap_set_bit (&map_head, DECL_UID (t));
13097 bitmap_set_bit (&map_field_head, DECL_UID (t));
13098 }
13099 }
acf0174b
JJ
13100 }
13101 break;
13102 }
13103 if (t == error_mark_node)
d9a6bd32
JJ
13104 {
13105 remove = true;
13106 break;
13107 }
13108 if (TREE_CODE (t) == COMPONENT_REF
77886428 13109 && (ort & C_ORT_OMP)
d9a6bd32
JJ
13110 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13111 {
13112 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13113 {
13114 error_at (OMP_CLAUSE_LOCATION (c),
13115 "bit-field %qE in %qs clause",
13116 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13117 remove = true;
13118 }
13119 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13120 {
13121 error_at (OMP_CLAUSE_LOCATION (c),
13122 "%qE does not have a mappable type in %qs clause",
13123 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13124 remove = true;
13125 }
13126 while (TREE_CODE (t) == COMPONENT_REF)
13127 {
13128 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13129 == UNION_TYPE)
13130 {
13131 error_at (OMP_CLAUSE_LOCATION (c),
13132 "%qE is a member of a union", t);
13133 remove = true;
13134 break;
13135 }
13136 t = TREE_OPERAND (t, 0);
13137 }
13138 if (remove)
13139 break;
13140 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13141 {
e01d41e5 13142 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
d9a6bd32
JJ
13143 break;
13144 }
13145 }
13146 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13147 {
13148 error_at (OMP_CLAUSE_LOCATION (c),
13149 "%qE is not a variable in %qs clause", t,
13150 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13151 remove = true;
13152 }
0ae9bd27 13153 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
13154 {
13155 error_at (OMP_CLAUSE_LOCATION (c),
13156 "%qD is threadprivate variable in %qs clause", t,
13157 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13158 remove = true;
13159 }
e4606348
JJ
13160 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13161 || (OMP_CLAUSE_MAP_KIND (c)
13162 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13163 && !c_mark_addressable (t))
acf0174b 13164 remove = true;
b17a8b07 13165 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37 13166 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
d9a6bd32
JJ
13167 || (OMP_CLAUSE_MAP_KIND (c)
13168 == GOMP_MAP_FIRSTPRIVATE_POINTER)
41dbbb37
TS
13169 || (OMP_CLAUSE_MAP_KIND (c)
13170 == GOMP_MAP_FORCE_DEVICEPTR)))
d9a6bd32 13171 && t == OMP_CLAUSE_DECL (c)
b17a8b07 13172 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13173 {
13174 error_at (OMP_CLAUSE_LOCATION (c),
13175 "%qD does not have a mappable type in %qs clause", t,
13176 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13177 remove = true;
13178 }
d9a6bd32
JJ
13179 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13180 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13181 {
13182 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13183 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13184 {
13185 error ("%qD appears more than once in data clauses", t);
13186 remove = true;
13187 }
e01d41e5 13188 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
d9a6bd32 13189 {
e46c7770
CP
13190 if (ort == C_ORT_ACC)
13191 error ("%qD appears more than once in data clauses", t);
13192 else
13193 error ("%qD appears both in data and map clauses", t);
e01d41e5 13194 remove = true;
d9a6bd32 13195 }
e01d41e5
JJ
13196 else
13197 bitmap_set_bit (&generic_head, DECL_UID (t));
d9a6bd32
JJ
13198 }
13199 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
acf0174b
JJ
13200 {
13201 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13202 error ("%qD appears more than once in motion clauses", t);
e46c7770
CP
13203 else if (ort == C_ORT_ACC)
13204 error ("%qD appears more than once in data clauses", t);
acf0174b
JJ
13205 else
13206 error ("%qD appears more than once in map clauses", t);
13207 remove = true;
13208 }
e01d41e5
JJ
13209 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13210 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13211 {
e46c7770
CP
13212 if (ort == C_ORT_ACC)
13213 error ("%qD appears more than once in data clauses", t);
13214 else
13215 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13216 remove = true;
13217 }
acf0174b 13218 else
d9a6bd32
JJ
13219 {
13220 bitmap_set_bit (&map_head, DECL_UID (t));
13221 if (t != OMP_CLAUSE_DECL (c)
13222 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13223 bitmap_set_bit (&map_field_head, DECL_UID (t));
13224 }
13225 break;
13226
13227 case OMP_CLAUSE_TO_DECLARE:
d9a6bd32
JJ
13228 case OMP_CLAUSE_LINK:
13229 t = OMP_CLAUSE_DECL (c);
e01d41e5
JJ
13230 if (TREE_CODE (t) == FUNCTION_DECL
13231 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13232 ;
13233 else if (!VAR_P (t))
d9a6bd32 13234 {
e01d41e5
JJ
13235 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13236 error_at (OMP_CLAUSE_LOCATION (c),
13237 "%qE is neither a variable nor a function name in "
13238 "clause %qs", t,
13239 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13240 else
13241 error_at (OMP_CLAUSE_LOCATION (c),
13242 "%qE is not a variable in clause %qs", t,
13243 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
d9a6bd32
JJ
13244 remove = true;
13245 }
13246 else if (DECL_THREAD_LOCAL_P (t))
13247 {
13248 error_at (OMP_CLAUSE_LOCATION (c),
13249 "%qD is threadprivate variable in %qs clause", t,
13250 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13251 remove = true;
13252 }
13253 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13254 {
13255 error_at (OMP_CLAUSE_LOCATION (c),
13256 "%qD does not have a mappable type in %qs clause", t,
13257 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13258 remove = true;
13259 }
e01d41e5
JJ
13260 if (remove)
13261 break;
13262 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13263 {
13264 error_at (OMP_CLAUSE_LOCATION (c),
13265 "%qE appears more than once on the same "
13266 "%<declare target%> directive", t);
13267 remove = true;
13268 }
13269 else
13270 bitmap_set_bit (&generic_head, DECL_UID (t));
acf0174b
JJ
13271 break;
13272
13273 case OMP_CLAUSE_UNIFORM:
13274 t = OMP_CLAUSE_DECL (c);
13275 if (TREE_CODE (t) != PARM_DECL)
13276 {
13277 if (DECL_P (t))
13278 error_at (OMP_CLAUSE_LOCATION (c),
13279 "%qD is not an argument in %<uniform%> clause", t);
13280 else
13281 error_at (OMP_CLAUSE_LOCATION (c),
13282 "%qE is not an argument in %<uniform%> clause", t);
13283 remove = true;
ee1d5a02 13284 break;
acf0174b 13285 }
e01d41e5
JJ
13286 /* map_head bitmap is used as uniform_head if declare_simd. */
13287 bitmap_set_bit (&map_head, DECL_UID (t));
ee1d5a02 13288 goto check_dup_generic;
acf0174b 13289
d9a6bd32
JJ
13290 case OMP_CLAUSE_IS_DEVICE_PTR:
13291 case OMP_CLAUSE_USE_DEVICE_PTR:
13292 t = OMP_CLAUSE_DECL (c);
13293 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13294 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13295 {
13296 error_at (OMP_CLAUSE_LOCATION (c),
13297 "%qs variable is neither a pointer nor an array",
13298 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13299 remove = true;
13300 }
13301 goto check_dup_generic;
13302
acf0174b
JJ
13303 case OMP_CLAUSE_NOWAIT:
13304 if (copyprivate_seen)
13305 {
13306 error_at (OMP_CLAUSE_LOCATION (c),
13307 "%<nowait%> clause must not be used together "
13308 "with %<copyprivate%>");
13309 remove = true;
13310 break;
13311 }
13312 nowait_clause = pc;
13313 pc = &OMP_CLAUSE_CHAIN (c);
13314 continue;
13315
953ff289
DN
13316 case OMP_CLAUSE_IF:
13317 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
13318 case OMP_CLAUSE_NUM_TEAMS:
13319 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 13320 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
13321 case OMP_CLAUSE_UNTIED:
13322 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
13323 case OMP_CLAUSE_FINAL:
13324 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
13325 case OMP_CLAUSE_DEVICE:
13326 case OMP_CLAUSE_DIST_SCHEDULE:
13327 case OMP_CLAUSE_PARALLEL:
13328 case OMP_CLAUSE_FOR:
13329 case OMP_CLAUSE_SECTIONS:
13330 case OMP_CLAUSE_TASKGROUP:
13331 case OMP_CLAUSE_PROC_BIND:
d9a6bd32
JJ
13332 case OMP_CLAUSE_PRIORITY:
13333 case OMP_CLAUSE_GRAINSIZE:
13334 case OMP_CLAUSE_NUM_TASKS:
13335 case OMP_CLAUSE_NOGROUP:
13336 case OMP_CLAUSE_THREADS:
13337 case OMP_CLAUSE_SIMD:
13338 case OMP_CLAUSE_HINT:
13339 case OMP_CLAUSE_DEFAULTMAP:
9a771876 13340 case OMP_CLAUSE__CILK_FOR_COUNT_:
41dbbb37
TS
13341 case OMP_CLAUSE_NUM_GANGS:
13342 case OMP_CLAUSE_NUM_WORKERS:
13343 case OMP_CLAUSE_VECTOR_LENGTH:
13344 case OMP_CLAUSE_ASYNC:
13345 case OMP_CLAUSE_WAIT:
13346 case OMP_CLAUSE_AUTO:
7a5e4956 13347 case OMP_CLAUSE_INDEPENDENT:
41dbbb37
TS
13348 case OMP_CLAUSE_SEQ:
13349 case OMP_CLAUSE_GANG:
13350 case OMP_CLAUSE_WORKER:
13351 case OMP_CLAUSE_VECTOR:
7a5e4956 13352 case OMP_CLAUSE_TILE:
acf0174b
JJ
13353 pc = &OMP_CLAUSE_CHAIN (c);
13354 continue;
13355
e01d41e5
JJ
13356 case OMP_CLAUSE_SCHEDULE:
13357 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13358 {
13359 const char *p = NULL;
13360 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13361 {
13362 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13363 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13364 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13365 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13366 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13367 default: gcc_unreachable ();
13368 }
13369 if (p)
13370 {
13371 error_at (OMP_CLAUSE_LOCATION (c),
13372 "%<nonmonotonic%> modifier specified for %qs "
13373 "schedule kind", p);
13374 OMP_CLAUSE_SCHEDULE_KIND (c)
13375 = (enum omp_clause_schedule_kind)
13376 (OMP_CLAUSE_SCHEDULE_KIND (c)
13377 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13378 }
13379 }
13380 schedule_clause = c;
13381 pc = &OMP_CLAUSE_CHAIN (c);
13382 continue;
13383
13384 case OMP_CLAUSE_ORDERED:
13385 ordered_seen = true;
13386 pc = &OMP_CLAUSE_CHAIN (c);
13387 continue;
13388
d9a6bd32
JJ
13389 case OMP_CLAUSE_SAFELEN:
13390 safelen = c;
13391 pc = &OMP_CLAUSE_CHAIN (c);
13392 continue;
13393 case OMP_CLAUSE_SIMDLEN:
13394 simdlen = c;
13395 pc = &OMP_CLAUSE_CHAIN (c);
13396 continue;
13397
acf0174b
JJ
13398 case OMP_CLAUSE_INBRANCH:
13399 case OMP_CLAUSE_NOTINBRANCH:
13400 if (branch_seen)
13401 {
13402 error_at (OMP_CLAUSE_LOCATION (c),
13403 "%<inbranch%> clause is incompatible with "
13404 "%<notinbranch%>");
13405 remove = true;
13406 break;
13407 }
13408 branch_seen = true;
953ff289
DN
13409 pc = &OMP_CLAUSE_CHAIN (c);
13410 continue;
13411
13412 default:
13413 gcc_unreachable ();
13414 }
13415
13416 if (!remove)
13417 {
13418 t = OMP_CLAUSE_DECL (c);
13419
13420 if (need_complete)
13421 {
4f2e1536 13422 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
953ff289
DN
13423 if (t == error_mark_node)
13424 remove = true;
13425 }
13426
13427 if (need_implicitly_determined)
13428 {
13429 const char *share_name = NULL;
13430
0ae9bd27 13431 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
953ff289
DN
13432 share_name = "threadprivate";
13433 else switch (c_omp_predetermined_sharing (t))
13434 {
13435 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13436 break;
13437 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
13438 /* const vars may be specified in firstprivate clause. */
13439 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13440 && TREE_READONLY (t))
13441 break;
953ff289
DN
13442 share_name = "shared";
13443 break;
13444 case OMP_CLAUSE_DEFAULT_PRIVATE:
13445 share_name = "private";
13446 break;
13447 default:
13448 gcc_unreachable ();
13449 }
13450 if (share_name)
13451 {
c2255bc4
AH
13452 error_at (OMP_CLAUSE_LOCATION (c),
13453 "%qE is predetermined %qs for %qs",
acf0174b
JJ
13454 t, share_name,
13455 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13456 remove = true;
13457 }
13458 }
13459 }
13460
13461 if (remove)
13462 *pc = OMP_CLAUSE_CHAIN (c);
13463 else
13464 pc = &OMP_CLAUSE_CHAIN (c);
13465 }
13466
d9a6bd32
JJ
13467 if (simdlen
13468 && safelen
13469 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13470 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13471 {
13472 error_at (OMP_CLAUSE_LOCATION (simdlen),
13473 "%<simdlen%> clause value is bigger than "
13474 "%<safelen%> clause value");
13475 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13476 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13477 }
13478
e01d41e5
JJ
13479 if (ordered_seen
13480 && schedule_clause
13481 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13482 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13483 {
13484 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13485 "%<nonmonotonic%> schedule modifier specified together "
13486 "with %<ordered%> clause");
13487 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13488 = (enum omp_clause_schedule_kind)
13489 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13490 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13491 }
13492
13493 if (linear_variable_step_check)
13494 for (pc = &clauses, c = clauses; c ; c = *pc)
13495 {
13496 bool remove = false;
13497 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13498 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13499 && !bitmap_bit_p (&map_head,
13500 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13501 {
13502 error_at (OMP_CLAUSE_LOCATION (c),
13503 "%<linear%> clause step is a parameter %qD not "
13504 "specified in %<uniform%> clause",
13505 OMP_CLAUSE_LINEAR_STEP (c));
13506 remove = true;
13507 }
13508
13509 if (remove)
13510 *pc = OMP_CLAUSE_CHAIN (c);
13511 else
13512 pc = &OMP_CLAUSE_CHAIN (c);
13513 }
13514
953ff289
DN
13515 bitmap_obstack_release (NULL);
13516 return clauses;
13517}
9ae165a0 13518
0a35513e
AH
13519/* Create a transaction node. */
13520
13521tree
13522c_finish_transaction (location_t loc, tree block, int flags)
13523{
13524 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13525 if (flags & TM_STMT_ATTR_OUTER)
13526 TRANSACTION_EXPR_OUTER (stmt) = 1;
13527 if (flags & TM_STMT_ATTR_RELAXED)
13528 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13529 return add_stmt (stmt);
13530}
13531
9ae165a0 13532/* Make a variant type in the proper way for C/C++, propagating qualifiers
e9e32ee6
JM
13533 down to the element type of an array. If ORIG_QUAL_TYPE is not
13534 NULL, then it should be used as the qualified type
13535 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13536 preserve information about the typedef name from which an array
13537 type was derived). */
9ae165a0
DG
13538
13539tree
e9e32ee6
JM
13540c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13541 size_t orig_qual_indirect)
9ae165a0
DG
13542{
13543 if (type == error_mark_node)
13544 return type;
13545
13546 if (TREE_CODE (type) == ARRAY_TYPE)
13547 {
13548 tree t;
13549 tree element_type = c_build_qualified_type (TREE_TYPE (type),
e9e32ee6
JM
13550 type_quals, orig_qual_type,
13551 orig_qual_indirect - 1);
9ae165a0
DG
13552
13553 /* See if we already have an identically qualified type. */
e9e32ee6
JM
13554 if (orig_qual_type && orig_qual_indirect == 0)
13555 t = orig_qual_type;
13556 else
13557 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13558 {
13559 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13560 && TYPE_NAME (t) == TYPE_NAME (type)
13561 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13562 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13563 TYPE_ATTRIBUTES (type)))
13564 break;
13565 }
9ae165a0
DG
13566 if (!t)
13567 {
13568 tree domain = TYPE_DOMAIN (type);
13569
13570 t = build_variant_type_copy (type);
13571 TREE_TYPE (t) = element_type;
13572
13573 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13574 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13575 SET_TYPE_STRUCTURAL_EQUALITY (t);
13576 else if (TYPE_CANONICAL (element_type) != element_type
13577 || (domain && TYPE_CANONICAL (domain) != domain))
13578 {
b8698a0f 13579 tree unqualified_canon
9ae165a0 13580 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 13581 domain? TYPE_CANONICAL (domain)
9ae165a0 13582 : NULL_TREE);
ee45a32d
EB
13583 if (TYPE_REVERSE_STORAGE_ORDER (type))
13584 {
13585 unqualified_canon
13586 = build_distinct_type_copy (unqualified_canon);
13587 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13588 }
b8698a0f 13589 TYPE_CANONICAL (t)
9ae165a0
DG
13590 = c_build_qualified_type (unqualified_canon, type_quals);
13591 }
13592 else
13593 TYPE_CANONICAL (t) = t;
13594 }
13595 return t;
13596 }
13597
13598 /* A restrict-qualified pointer type must be a pointer to object or
13599 incomplete type. Note that the use of POINTER_TYPE_P also allows
13600 REFERENCE_TYPEs, which is appropriate for C++. */
13601 if ((type_quals & TYPE_QUAL_RESTRICT)
13602 && (!POINTER_TYPE_P (type)
13603 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13604 {
13605 error ("invalid use of %<restrict%>");
13606 type_quals &= ~TYPE_QUAL_RESTRICT;
13607 }
13608
e9e32ee6
JM
13609 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13610 ? orig_qual_type
13611 : build_qualified_type (type, type_quals));
e922069e
JW
13612 /* A variant type does not inherit the list of incomplete vars from the
13613 type main variant. */
3e636daf 13614 if (RECORD_OR_UNION_TYPE_P (var_type))
e922069e
JW
13615 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13616 return var_type;
9ae165a0 13617}
72b5577d
ILT
13618
13619/* Build a VA_ARG_EXPR for the C parser. */
13620
13621tree
f187980b 13622c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
72b5577d 13623{
ec3fba51
MP
13624 if (error_operand_p (type))
13625 return error_mark_node;
f187980b
EB
13626 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13627 order because it takes the address of the expression. */
13628 else if (handled_component_p (expr)
13629 && reverse_storage_order_for_component_p (expr))
13630 {
13631 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13632 return error_mark_node;
13633 }
ec3fba51 13634 else if (!COMPLETE_TYPE_P (type))
4e81b788 13635 {
f187980b 13636 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
ec3fba51 13637 "type %qT", type);
4e81b788
MP
13638 return error_mark_node;
13639 }
ec3fba51 13640 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
f187980b 13641 warning_at (loc2, OPT_Wc___compat,
ec3fba51 13642 "C++ requires promoted type, not enum type, in %<va_arg%>");
f187980b 13643 return build_va_arg (loc2, expr, type);
72b5577d 13644}
acf0174b
JJ
13645
13646/* Return truthvalue of whether T1 is the same tree structure as T2.
13647 Return 1 if they are the same. Return 0 if they are different. */
13648
13649bool
13650c_tree_equal (tree t1, tree t2)
13651{
13652 enum tree_code code1, code2;
13653
13654 if (t1 == t2)
13655 return true;
13656 if (!t1 || !t2)
13657 return false;
13658
13659 for (code1 = TREE_CODE (t1);
13660 CONVERT_EXPR_CODE_P (code1)
13661 || code1 == NON_LVALUE_EXPR;
13662 code1 = TREE_CODE (t1))
13663 t1 = TREE_OPERAND (t1, 0);
13664 for (code2 = TREE_CODE (t2);
13665 CONVERT_EXPR_CODE_P (code2)
13666 || code2 == NON_LVALUE_EXPR;
13667 code2 = TREE_CODE (t2))
13668 t2 = TREE_OPERAND (t2, 0);
13669
13670 /* They might have become equal now. */
13671 if (t1 == t2)
13672 return true;
13673
13674 if (code1 != code2)
13675 return false;
13676
13677 switch (code1)
13678 {
13679 case INTEGER_CST:
807e902e 13680 return wi::eq_p (t1, t2);
acf0174b
JJ
13681
13682 case REAL_CST:
624d31fe 13683 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
acf0174b
JJ
13684
13685 case STRING_CST:
13686 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13687 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13688 TREE_STRING_LENGTH (t1));
13689
13690 case FIXED_CST:
13691 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13692 TREE_FIXED_CST (t2));
13693
13694 case COMPLEX_CST:
13695 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13696 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13697
13698 case VECTOR_CST:
13699 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13700
13701 case CONSTRUCTOR:
13702 /* We need to do this when determining whether or not two
13703 non-type pointer to member function template arguments
13704 are the same. */
13705 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13706 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13707 return false;
13708 {
13709 tree field, value;
13710 unsigned int i;
13711 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13712 {
13713 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13714 if (!c_tree_equal (field, elt2->index)
13715 || !c_tree_equal (value, elt2->value))
13716 return false;
13717 }
13718 }
13719 return true;
13720
13721 case TREE_LIST:
13722 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13723 return false;
13724 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
13725 return false;
13726 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
13727
13728 case SAVE_EXPR:
13729 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13730
13731 case CALL_EXPR:
13732 {
13733 tree arg1, arg2;
13734 call_expr_arg_iterator iter1, iter2;
13735 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
13736 return false;
13737 for (arg1 = first_call_expr_arg (t1, &iter1),
13738 arg2 = first_call_expr_arg (t2, &iter2);
13739 arg1 && arg2;
13740 arg1 = next_call_expr_arg (&iter1),
13741 arg2 = next_call_expr_arg (&iter2))
13742 if (!c_tree_equal (arg1, arg2))
13743 return false;
13744 if (arg1 || arg2)
13745 return false;
13746 return true;
13747 }
13748
13749 case TARGET_EXPR:
13750 {
13751 tree o1 = TREE_OPERAND (t1, 0);
13752 tree o2 = TREE_OPERAND (t2, 0);
13753
13754 /* Special case: if either target is an unallocated VAR_DECL,
13755 it means that it's going to be unified with whatever the
13756 TARGET_EXPR is really supposed to initialize, so treat it
13757 as being equivalent to anything. */
0ae9bd27 13758 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
acf0174b
JJ
13759 && !DECL_RTL_SET_P (o1))
13760 /*Nop*/;
0ae9bd27 13761 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
acf0174b
JJ
13762 && !DECL_RTL_SET_P (o2))
13763 /*Nop*/;
13764 else if (!c_tree_equal (o1, o2))
13765 return false;
13766
13767 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
13768 }
13769
13770 case COMPONENT_REF:
13771 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
13772 return false;
13773 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13774
13775 case PARM_DECL:
13776 case VAR_DECL:
13777 case CONST_DECL:
13778 case FIELD_DECL:
13779 case FUNCTION_DECL:
13780 case IDENTIFIER_NODE:
13781 case SSA_NAME:
13782 return false;
13783
13784 case TREE_VEC:
13785 {
13786 unsigned ix;
13787 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
13788 return false;
13789 for (ix = TREE_VEC_LENGTH (t1); ix--;)
13790 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
13791 TREE_VEC_ELT (t2, ix)))
13792 return false;
13793 return true;
13794 }
13795
13796 default:
13797 break;
13798 }
13799
13800 switch (TREE_CODE_CLASS (code1))
13801 {
13802 case tcc_unary:
13803 case tcc_binary:
13804 case tcc_comparison:
13805 case tcc_expression:
13806 case tcc_vl_exp:
13807 case tcc_reference:
13808 case tcc_statement:
13809 {
13810 int i, n = TREE_OPERAND_LENGTH (t1);
13811
13812 switch (code1)
13813 {
13814 case PREINCREMENT_EXPR:
13815 case PREDECREMENT_EXPR:
13816 case POSTINCREMENT_EXPR:
13817 case POSTDECREMENT_EXPR:
13818 n = 1;
13819 break;
13820 case ARRAY_REF:
13821 n = 2;
13822 break;
13823 default:
13824 break;
13825 }
13826
13827 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
13828 && n != TREE_OPERAND_LENGTH (t2))
13829 return false;
13830
13831 for (i = 0; i < n; ++i)
13832 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
13833 return false;
13834
13835 return true;
13836 }
13837
13838 case tcc_type:
13839 return comptypes (t1, t2);
13840 default:
13841 gcc_unreachable ();
13842 }
13843 /* We can get here with --disable-checking. */
13844 return false;
13845}
12893402
BI
13846
13847/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
13848 spawn-helper and BODY is the newly created body for FNDECL. */
13849
13850void
13851cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
13852{
13853 tree list = alloc_stmt_list ();
13854 tree frame = make_cilk_frame (fndecl);
13855 tree dtor = create_cilk_function_exit (frame, false, true);
13856 add_local_decl (cfun, frame);
13857
13858 DECL_SAVED_TREE (fndecl) = list;
13859 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
13860 frame);
13861 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
13862 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
13863
13864 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
13865 append_to_statement_list (detach_expr, &body_list);
13866
13867 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
13868 body = fold_build_cleanup_point_expr (void_type_node, body);
13869
13870 append_to_statement_list (body, &body_list);
13871 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
13872 body_list, dtor), &list);
13873}
1807ffc1
MS
13874
13875/* Returns true when the function declaration FNDECL is implicit,
13876 introduced as a result of a call to an otherwise undeclared
13877 function, and false otherwise. */
13878
13879bool
13880c_decl_implicit (const_tree fndecl)
13881{
13882 return C_DECL_IMPLICIT (fndecl);
13883}