]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
re PR middle-end/69156 (gcc.dg/pr59630.c: ICE: verify_gimple failed: non-trivial...
[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
400fbf9f
JW
186/* Do `exp = require_complete_type (exp);' to make sure exp
187 does not have an incomplete type. (That includes void types.) */
188
189tree
2f6e4e97 190require_complete_type (tree value)
400fbf9f
JW
191{
192 tree type = TREE_TYPE (value);
193
7a0ca710 194 if (error_operand_p (value))
ea0f786b
CB
195 return error_mark_node;
196
400fbf9f 197 /* First, detect a valid value with a complete type. */
d0f062fb 198 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
199 return value;
200
7a228918 201 c_incomplete_type_error (value, type);
400fbf9f
JW
202 return error_mark_node;
203}
204
205/* Print an error message for invalid use of an incomplete type.
206 VALUE is the expression that was used (or 0 if that isn't known)
207 and TYPE is the type that was invalid. */
208
209void
ac7d7749 210c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 211{
400fbf9f
JW
212 /* Avoid duplicate error message. */
213 if (TREE_CODE (type) == ERROR_MARK)
214 return;
215
0ae9bd27 216 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
ac9f18db 217 error ("%qD has an incomplete type %qT", value, type);
400fbf9f
JW
218 else
219 {
220 retry:
221 /* We must print an error message. Be clever about what it says. */
222
223 switch (TREE_CODE (type))
224 {
225 case RECORD_TYPE:
400fbf9f 226 case UNION_TYPE:
400fbf9f 227 case ENUMERAL_TYPE:
400fbf9f
JW
228 break;
229
230 case VOID_TYPE:
231 error ("invalid use of void expression");
232 return;
233
234 case ARRAY_TYPE:
235 if (TYPE_DOMAIN (type))
236 {
fba78abb
RH
237 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
238 {
239 error ("invalid use of flexible array member");
240 return;
241 }
400fbf9f
JW
242 type = TREE_TYPE (type);
243 goto retry;
244 }
245 error ("invalid use of array with unspecified bounds");
246 return;
247
248 default:
366de0ce 249 gcc_unreachable ();
400fbf9f
JW
250 }
251
252 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
ac9f18db 253 error ("invalid use of undefined type %qT", type);
400fbf9f
JW
254 else
255 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
ac9f18db 256 error ("invalid use of incomplete typedef %qT", type);
400fbf9f
JW
257 }
258}
259
ab393bf1
NB
260/* Given a type, apply default promotions wrt unnamed function
261 arguments and return the new type. */
262
263tree
2f6e4e97 264c_type_promotes_to (tree type)
ab393bf1 265{
267bac10 266 tree ret = NULL_TREE;
ab393bf1 267
267bac10
JM
268 if (TYPE_MAIN_VARIANT (type) == float_type_node)
269 ret = double_type_node;
270 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
271 {
272 /* Preserve unsignedness if not really getting any wider. */
8df83eae 273 if (TYPE_UNSIGNED (type)
c22cacf3 274 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
275 ret = unsigned_type_node;
276 else
277 ret = integer_type_node;
ab393bf1
NB
278 }
279
267bac10
JM
280 if (ret != NULL_TREE)
281 return (TYPE_ATOMIC (type)
282 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
283 : ret);
284
ab393bf1
NB
285 return type;
286}
287
36c5e70a
BE
288/* Return true if between two named address spaces, whether there is a superset
289 named address space that encompasses both address spaces. If there is a
290 superset, return which address space is the superset. */
291
292static bool
293addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
294{
295 if (as1 == as2)
296 {
297 *common = as1;
298 return true;
299 }
300 else if (targetm.addr_space.subset_p (as1, as2))
301 {
302 *common = as2;
303 return true;
304 }
305 else if (targetm.addr_space.subset_p (as2, as1))
306 {
307 *common = as1;
308 return true;
309 }
310 else
311 return false;
312}
313
400fbf9f
JW
314/* Return a variant of TYPE which has all the type qualifiers of LIKE
315 as well as those of TYPE. */
316
317static tree
2f6e4e97 318qualify_type (tree type, tree like)
400fbf9f 319{
36c5e70a
BE
320 addr_space_t as_type = TYPE_ADDR_SPACE (type);
321 addr_space_t as_like = TYPE_ADDR_SPACE (like);
322 addr_space_t as_common;
323
324 /* If the two named address spaces are different, determine the common
325 superset address space. If there isn't one, raise an error. */
326 if (!addr_space_superset (as_type, as_like, &as_common))
327 {
328 as_common = as_type;
329 error ("%qT and %qT are in disjoint named address spaces",
330 type, like);
331 }
332
2f6e4e97 333 return c_build_qualified_type (type,
36c5e70a 334 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 335 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 336 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 337}
52ffd86e
MS
338
339/* Return true iff the given tree T is a variable length array. */
340
341bool
ac7d7749 342c_vla_type_p (const_tree t)
52ffd86e
MS
343{
344 if (TREE_CODE (t) == ARRAY_TYPE
345 && C_TYPE_VARIABLE_SIZE (t))
346 return true;
347 return false;
348}
400fbf9f 349\f
10bc1b1b 350/* Return the composite type of two compatible types.
5305f6d7 351
10bc1b1b
JM
352 We assume that comptypes has already been done and returned
353 nonzero; if that isn't so, this may crash. In particular, we
354 assume that qualifiers match. */
400fbf9f
JW
355
356tree
10bc1b1b 357composite_type (tree t1, tree t2)
400fbf9f 358{
b3694847
SS
359 enum tree_code code1;
360 enum tree_code code2;
4b027d16 361 tree attributes;
400fbf9f
JW
362
363 /* Save time if the two types are the same. */
364
365 if (t1 == t2) return t1;
366
367 /* If one type is nonsense, use the other. */
368 if (t1 == error_mark_node)
369 return t2;
370 if (t2 == error_mark_node)
371 return t1;
372
10bc1b1b
JM
373 code1 = TREE_CODE (t1);
374 code2 = TREE_CODE (t2);
375
d9525bec 376 /* Merge the attributes. */
5fd9b178 377 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 378
10bc1b1b
JM
379 /* If one is an enumerated type and the other is the compatible
380 integer type, the composite type might be either of the two
381 (DR#013 question 3). For consistency, use the enumerated type as
382 the composite type. */
400fbf9f 383
10bc1b1b
JM
384 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
385 return t1;
386 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
387 return t2;
75326e8c 388
366de0ce 389 gcc_assert (code1 == code2);
b6a10c9f 390
400fbf9f
JW
391 switch (code1)
392 {
400fbf9f 393 case POINTER_TYPE:
10bc1b1b 394 /* For two pointers, do this recursively on the target type. */
400fbf9f 395 {
3932261a
MM
396 tree pointed_to_1 = TREE_TYPE (t1);
397 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 398 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 399 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
400 t1 = build_type_attribute_variant (t1, attributes);
401 return qualify_type (t1, t2);
400fbf9f 402 }
400fbf9f
JW
403
404 case ARRAY_TYPE:
405 {
10bc1b1b 406 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
407 int quals;
408 tree unqual_elt;
ca8bdb78
JM
409 tree d1 = TYPE_DOMAIN (t1);
410 tree d2 = TYPE_DOMAIN (t2);
411 bool d1_variable, d2_variable;
412 bool d1_zero, d2_zero;
f6294de7 413 bool t1_complete, t2_complete;
46df2823 414
de46b2fe 415 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
416 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
417 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 418
f6294de7
JM
419 t1_complete = COMPLETE_TYPE_P (t1);
420 t2_complete = COMPLETE_TYPE_P (t2);
421
ca8bdb78
JM
422 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
423 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
424
425 d1_variable = (!d1_zero
426 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
427 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
428 d2_variable = (!d2_zero
429 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
430 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
431 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
432 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 433
400fbf9f 434 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
435 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
436 && (d2_variable || d2_zero || !d1_variable))
4b027d16 437 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
438 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
439 && (d1_variable || d1_zero || !d2_variable))
4b027d16 440 return build_type_attribute_variant (t2, attributes);
c22cacf3 441
de46b2fe
AP
442 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443 return build_type_attribute_variant (t1, attributes);
444 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
445 return build_type_attribute_variant (t2, attributes);
c22cacf3 446
46df2823
JM
447 /* Merge the element types, and have a size if either arg has
448 one. We may have qualifiers on the element types. To set
449 up TYPE_MAIN_VARIANT correctly, we need to form the
450 composite of the unqualified types and add the qualifiers
451 back at the end. */
452 quals = TYPE_QUALS (strip_array_types (elt));
453 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
454 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
455 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
456 && (d2_variable
457 || d2_zero
458 || !d1_variable))
459 ? t1
460 : t2));
f6294de7
JM
461 /* Ensure a composite type involving a zero-length array type
462 is a zero-length type not an incomplete type. */
463 if (d1_zero && d2_zero
464 && (t1_complete || t2_complete)
465 && !COMPLETE_TYPE_P (t1))
466 {
467 TYPE_SIZE (t1) = bitsize_zero_node;
468 TYPE_SIZE_UNIT (t1) = size_zero_node;
469 }
46df2823 470 t1 = c_build_qualified_type (t1, quals);
de46b2fe 471 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
472 }
473
fcb99e7b
JJ
474 case ENUMERAL_TYPE:
475 case RECORD_TYPE:
476 case UNION_TYPE:
477 if (attributes != NULL)
478 {
479 /* Try harder not to create a new aggregate type. */
480 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
481 return t1;
482 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
483 return t2;
484 }
485 return build_type_attribute_variant (t1, attributes);
486
400fbf9f
JW
487 case FUNCTION_TYPE:
488 /* Function types: prefer the one that specified arg types.
489 If both do, merge the arg types. Also merge the return types. */
490 {
10bc1b1b 491 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
492 tree p1 = TYPE_ARG_TYPES (t1);
493 tree p2 = TYPE_ARG_TYPES (t2);
494 int len;
495 tree newargs, n;
496 int i;
497
498 /* Save space: see if the result is identical to one of the args. */
3f75a254 499 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 500 return build_type_attribute_variant (t1, attributes);
3f75a254 501 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 502 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
503
504 /* Simple way if one arg fails to specify argument types. */
505 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 506 {
fe7080d2
AP
507 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
508 t1 = build_type_attribute_variant (t1, attributes);
509 return qualify_type (t1, t2);
4b027d16 510 }
400fbf9f 511 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
512 {
513 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
514 t1 = build_type_attribute_variant (t1, attributes);
515 return qualify_type (t1, t2);
4b027d16 516 }
400fbf9f
JW
517
518 /* If both args specify argument types, we must merge the two
519 lists, argument by argument. */
2f4e8f2b 520
400fbf9f
JW
521 len = list_length (p1);
522 newargs = 0;
523
524 for (i = 0; i < len; i++)
8d9bfdc5 525 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
526
527 n = newargs;
528
529 for (; p1;
530 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
531 {
532 /* A null type means arg type is not specified.
533 Take whatever the other function type has. */
534 if (TREE_VALUE (p1) == 0)
535 {
536 TREE_VALUE (n) = TREE_VALUE (p2);
537 goto parm_done;
538 }
539 if (TREE_VALUE (p2) == 0)
540 {
541 TREE_VALUE (n) = TREE_VALUE (p1);
542 goto parm_done;
543 }
2f6e4e97 544
400fbf9f
JW
545 /* Given wait (union {union wait *u; int *i} *)
546 and wait (union wait *),
547 prefer union wait * as type of parm. */
548 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
549 && TREE_VALUE (p1) != TREE_VALUE (p2))
550 {
551 tree memb;
58cb41e6
JJ
552 tree mv2 = TREE_VALUE (p2);
553 if (mv2 && mv2 != error_mark_node
554 && TREE_CODE (mv2) != ARRAY_TYPE)
555 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 556 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 557 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
558 {
559 tree mv3 = TREE_TYPE (memb);
560 if (mv3 && mv3 != error_mark_node
561 && TREE_CODE (mv3) != ARRAY_TYPE)
562 mv3 = TYPE_MAIN_VARIANT (mv3);
563 if (comptypes (mv3, mv2))
564 {
565 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
566 TREE_VALUE (p2));
c1771a20 567 pedwarn (input_location, OPT_Wpedantic,
fcf73884 568 "function types not truly compatible in ISO C");
58cb41e6
JJ
569 goto parm_done;
570 }
571 }
400fbf9f
JW
572 }
573 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
574 && TREE_VALUE (p2) != TREE_VALUE (p1))
575 {
576 tree memb;
58cb41e6
JJ
577 tree mv1 = TREE_VALUE (p1);
578 if (mv1 && mv1 != error_mark_node
579 && TREE_CODE (mv1) != ARRAY_TYPE)
580 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 581 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 582 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
583 {
584 tree mv3 = TREE_TYPE (memb);
585 if (mv3 && mv3 != error_mark_node
586 && TREE_CODE (mv3) != ARRAY_TYPE)
587 mv3 = TYPE_MAIN_VARIANT (mv3);
588 if (comptypes (mv3, mv1))
589 {
590 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
591 TREE_VALUE (p1));
c1771a20 592 pedwarn (input_location, OPT_Wpedantic,
fcf73884 593 "function types not truly compatible in ISO C");
58cb41e6
JJ
594 goto parm_done;
595 }
596 }
400fbf9f 597 }
10bc1b1b 598 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
599 parm_done: ;
600 }
601
4b027d16 602 t1 = build_function_type (valtype, newargs);
fe7080d2 603 t1 = qualify_type (t1, t2);
0f41302f 604 /* ... falls through ... */
400fbf9f
JW
605 }
606
607 default:
4b027d16 608 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
609 }
610
611}
10bc1b1b
JM
612
613/* Return the type of a conditional expression between pointers to
614 possibly differently qualified versions of compatible types.
615
616 We assume that comp_target_types has already been done and returned
617 nonzero; if that isn't so, this may crash. */
618
619static tree
620common_pointer_type (tree t1, tree t2)
621{
622 tree attributes;
46df2823
JM
623 tree pointed_to_1, mv1;
624 tree pointed_to_2, mv2;
10bc1b1b 625 tree target;
eb1387a0 626 unsigned target_quals;
36c5e70a
BE
627 addr_space_t as1, as2, as_common;
628 int quals1, quals2;
10bc1b1b
JM
629
630 /* Save time if the two types are the same. */
631
632 if (t1 == t2) return t1;
633
634 /* If one type is nonsense, use the other. */
635 if (t1 == error_mark_node)
636 return t2;
637 if (t2 == error_mark_node)
638 return t1;
639
366de0ce 640 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 641 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
642
643 /* Merge the attributes. */
644 attributes = targetm.merge_type_attributes (t1, t2);
645
646 /* Find the composite type of the target types, and combine the
46df2823
JM
647 qualifiers of the two types' targets. Do not lose qualifiers on
648 array element types by taking the TYPE_MAIN_VARIANT. */
649 mv1 = pointed_to_1 = TREE_TYPE (t1);
650 mv2 = pointed_to_2 = TREE_TYPE (t2);
651 if (TREE_CODE (mv1) != ARRAY_TYPE)
652 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
653 if (TREE_CODE (mv2) != ARRAY_TYPE)
654 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
655 target = composite_type (mv1, mv2);
eb1387a0 656
768952be
MU
657 /* Strip array types to get correct qualifier for pointers to arrays */
658 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
659 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
660
eb1387a0
RG
661 /* For function types do not merge const qualifiers, but drop them
662 if used inconsistently. The middle-end uses these to mark const
663 and noreturn functions. */
664 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 665 target_quals = (quals1 & quals2);
eb1387a0 666 else
36c5e70a
BE
667 target_quals = (quals1 | quals2);
668
669 /* If the two named address spaces are different, determine the common
670 superset address space. This is guaranteed to exist due to the
671 assumption that comp_target_type returned non-zero. */
672 as1 = TYPE_ADDR_SPACE (pointed_to_1);
673 as2 = TYPE_ADDR_SPACE (pointed_to_2);
674 if (!addr_space_superset (as1, as2, &as_common))
675 gcc_unreachable ();
676
677 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
678
eb1387a0 679 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
680 return build_type_attribute_variant (t1, attributes);
681}
682
683/* Return the common type for two arithmetic types under the usual
684 arithmetic conversions. The default conversions have already been
685 applied, and enumerated types converted to their compatible integer
686 types. The resulting type is unqualified and has no attributes.
687
688 This is the type for the result of most arithmetic operations
689 if the operands have the given two types. */
690
ccf7f880
JJ
691static tree
692c_common_type (tree t1, tree t2)
10bc1b1b
JM
693{
694 enum tree_code code1;
695 enum tree_code code2;
696
697 /* If one type is nonsense, use the other. */
698 if (t1 == error_mark_node)
699 return t2;
700 if (t2 == error_mark_node)
701 return t1;
702
703 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
704 t1 = TYPE_MAIN_VARIANT (t1);
705
706 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
707 t2 = TYPE_MAIN_VARIANT (t2);
708
709 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
710 t1 = build_type_attribute_variant (t1, NULL_TREE);
711
712 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
713 t2 = build_type_attribute_variant (t2, NULL_TREE);
714
715 /* Save time if the two types are the same. */
716
717 if (t1 == t2) return t1;
718
719 code1 = TREE_CODE (t1);
720 code2 = TREE_CODE (t2);
721
366de0ce 722 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
723 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
724 || code1 == INTEGER_TYPE);
366de0ce 725 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
726 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
727 || code2 == INTEGER_TYPE);
10bc1b1b 728
5fc89bfd
JJ
729 /* When one operand is a decimal float type, the other operand cannot be
730 a generic float type or a complex type. We also disallow vector types
731 here. */
732 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
733 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
734 {
735 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
736 {
737 error ("can%'t mix operands of decimal float and vector types");
738 return error_mark_node;
739 }
740 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
741 {
742 error ("can%'t mix operands of decimal float and complex types");
743 return error_mark_node;
744 }
745 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
746 {
747 error ("can%'t mix operands of decimal float and other float types");
748 return error_mark_node;
749 }
750 }
751
10bc1b1b
JM
752 /* If one type is a vector type, return that type. (How the usual
753 arithmetic conversions apply to the vector types extension is not
754 precisely specified.) */
755 if (code1 == VECTOR_TYPE)
756 return t1;
757
758 if (code2 == VECTOR_TYPE)
759 return t2;
760
761 /* If one type is complex, form the common type of the non-complex
762 components, then make that complex. Use T1 or T2 if it is the
763 required type. */
764 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
765 {
766 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
767 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 768 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
769
770 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
771 return t1;
772 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
773 return t2;
774 else
775 return build_complex_type (subtype);
776 }
777
778 /* If only one is real, use it as the result. */
779
780 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
781 return t1;
782
783 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
784 return t2;
785
9a8ce21f
JG
786 /* If both are real and either are decimal floating point types, use
787 the decimal floating point type with the greater precision. */
788
789 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
790 {
791 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
792 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
793 return dfloat128_type_node;
794 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
795 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
796 return dfloat64_type_node;
797 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
798 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
799 return dfloat32_type_node;
800 }
801
ab22c1fa
CF
802 /* Deal with fixed-point types. */
803 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
804 {
805 unsigned int unsignedp = 0, satp = 0;
ef4bddc2 806 machine_mode m1, m2;
ab22c1fa
CF
807 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
808
809 m1 = TYPE_MODE (t1);
810 m2 = TYPE_MODE (t2);
811
812 /* If one input type is saturating, the result type is saturating. */
813 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
814 satp = 1;
815
816 /* If both fixed-point types are unsigned, the result type is unsigned.
817 When mixing fixed-point and integer types, follow the sign of the
818 fixed-point type.
819 Otherwise, the result type is signed. */
820 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
821 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
822 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
823 && TYPE_UNSIGNED (t1))
824 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
825 && TYPE_UNSIGNED (t2)))
826 unsignedp = 1;
827
828 /* The result type is signed. */
829 if (unsignedp == 0)
830 {
831 /* If the input type is unsigned, we need to convert to the
832 signed type. */
833 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
834 {
d75d71e0 835 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
836 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
837 mclass = MODE_FRACT;
838 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
839 mclass = MODE_ACCUM;
840 else
841 gcc_unreachable ();
842 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
843 }
844 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
845 {
d75d71e0 846 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
847 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
848 mclass = MODE_FRACT;
849 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
850 mclass = MODE_ACCUM;
851 else
852 gcc_unreachable ();
853 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
854 }
855 }
856
857 if (code1 == FIXED_POINT_TYPE)
858 {
859 fbit1 = GET_MODE_FBIT (m1);
860 ibit1 = GET_MODE_IBIT (m1);
861 }
862 else
863 {
864 fbit1 = 0;
865 /* Signed integers need to subtract one sign bit. */
866 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
867 }
868
869 if (code2 == FIXED_POINT_TYPE)
870 {
871 fbit2 = GET_MODE_FBIT (m2);
872 ibit2 = GET_MODE_IBIT (m2);
873 }
874 else
875 {
876 fbit2 = 0;
877 /* Signed integers need to subtract one sign bit. */
878 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
879 }
880
881 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
882 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
883 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
884 satp);
885 }
886
10bc1b1b
JM
887 /* Both real or both integers; use the one with greater precision. */
888
889 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
890 return t1;
891 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
892 return t2;
893
894 /* Same precision. Prefer long longs to longs to ints when the
895 same precision, following the C99 rules on integer type rank
896 (which are equivalent to the C90 rules for C90 types). */
897
898 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
899 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
900 return long_long_unsigned_type_node;
901
902 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
903 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
904 {
905 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
906 return long_long_unsigned_type_node;
907 else
c22cacf3 908 return long_long_integer_type_node;
10bc1b1b
JM
909 }
910
911 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
912 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
913 return long_unsigned_type_node;
914
915 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
916 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
917 {
918 /* But preserve unsignedness from the other type,
919 since long cannot hold all the values of an unsigned int. */
920 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
921 return long_unsigned_type_node;
922 else
923 return long_integer_type_node;
924 }
925
926 /* Likewise, prefer long double to double even if same size. */
927 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
928 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
929 return long_double_type_node;
930
2531a1d9
JR
931 /* Likewise, prefer double to float even if same size.
932 We got a couple of embedded targets with 32 bit doubles, and the
933 pdp11 might have 64 bit floats. */
934 if (TYPE_MAIN_VARIANT (t1) == double_type_node
935 || TYPE_MAIN_VARIANT (t2) == double_type_node)
936 return double_type_node;
937
10bc1b1b
JM
938 /* Otherwise prefer the unsigned one. */
939
940 if (TYPE_UNSIGNED (t1))
941 return t1;
942 else
943 return t2;
944}
400fbf9f 945\f
5922c215
JM
946/* Wrapper around c_common_type that is used by c-common.c and other
947 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
948 are allowed here and are converted to their compatible integer types.
949 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
950 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
951tree
952common_type (tree t1, tree t2)
953{
954 if (TREE_CODE (t1) == ENUMERAL_TYPE)
955 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
956 if (TREE_CODE (t2) == ENUMERAL_TYPE)
957 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
958
959 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
960 if (TREE_CODE (t1) == BOOLEAN_TYPE
961 && TREE_CODE (t2) == BOOLEAN_TYPE)
962 return boolean_type_node;
963
964 /* If either type is BOOLEAN_TYPE, then return the other. */
965 if (TREE_CODE (t1) == BOOLEAN_TYPE)
966 return t2;
967 if (TREE_CODE (t2) == BOOLEAN_TYPE)
968 return t1;
969
ccf7f880
JJ
970 return c_common_type (t1, t2);
971}
f13c9b2c 972
400fbf9f
JW
973/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
974 or various other operations. Return 2 if they are compatible
975 but a warning may be needed if you use them together. */
976
977int
132da1a5 978comptypes (tree type1, tree type2)
f13c9b2c
AP
979{
980 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
981 int val;
982
dc5027f4 983 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
984 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
985
986 return val;
987}
988
989/* Like comptypes, but if it returns non-zero because enum and int are
990 compatible, it sets *ENUM_AND_INT_P to true. */
991
992static int
993comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
994{
995 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
996 int val;
997
dc5027f4
JM
998 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
999 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1000
1001 return val;
1002}
1003
1004/* Like comptypes, but if it returns nonzero for different types, it
1005 sets *DIFFERENT_TYPES_P to true. */
1006
1007int
1008comptypes_check_different_types (tree type1, tree type2,
1009 bool *different_types_p)
1010{
1011 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1012 int val;
1013
1014 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1015 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1016
f13c9b2c 1017 return val;
c22cacf3
MS
1018}
1019\f
f13c9b2c
AP
1020/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1021 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1022 but a warning may be needed if you use them together. If
1023 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1024 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1025 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1026 NULL, and the types are compatible but different enough not to be
48b0b196 1027 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1028 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1029 false, but may or may not be set if the types are incompatible.
1030 This differs from comptypes, in that we don't free the seen
1031 types. */
f13c9b2c
AP
1032
1033static int
dc5027f4
JM
1034comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1035 bool *different_types_p)
400fbf9f 1036{
58f9752a
KG
1037 const_tree t1 = type1;
1038 const_tree t2 = type2;
4b027d16 1039 int attrval, val;
400fbf9f
JW
1040
1041 /* Suppress errors caused by previously reported errors. */
1042
8d47dfc5
RH
1043 if (t1 == t2 || !t1 || !t2
1044 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1045 return 1;
1046
bca63328
JM
1047 /* Enumerated types are compatible with integer types, but this is
1048 not transitive: two enumerated types in the same translation unit
1049 are compatible with each other only if they are the same type. */
400fbf9f 1050
bca63328 1051 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1052 {
1053 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1054 if (TREE_CODE (t2) != VOID_TYPE)
1055 {
1056 if (enum_and_int_p != NULL)
1057 *enum_and_int_p = true;
1058 if (different_types_p != NULL)
1059 *different_types_p = true;
1060 }
744aa42f 1061 }
bca63328 1062 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1063 {
1064 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1065 if (TREE_CODE (t1) != VOID_TYPE)
1066 {
1067 if (enum_and_int_p != NULL)
1068 *enum_and_int_p = true;
1069 if (different_types_p != NULL)
1070 *different_types_p = true;
1071 }
744aa42f 1072 }
400fbf9f
JW
1073
1074 if (t1 == t2)
1075 return 1;
1076
1077 /* Different classes of types can't be compatible. */
1078
3aeb3655
EC
1079 if (TREE_CODE (t1) != TREE_CODE (t2))
1080 return 0;
400fbf9f 1081
118a3a8b 1082 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1083
3932261a 1084 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1085 return 0;
1086
08632da2
RS
1087 /* Allow for two different type nodes which have essentially the same
1088 definition. Note that we already checked for equality of the type
38e01259 1089 qualifiers (just above). */
400fbf9f 1090
46df2823
JM
1091 if (TREE_CODE (t1) != ARRAY_TYPE
1092 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1093 return 1;
1094
4b027d16 1095 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1096 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1097 return 0;
1098
1099 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1100 val = 0;
1101
400fbf9f
JW
1102 switch (TREE_CODE (t1))
1103 {
1104 case POINTER_TYPE:
106f5de5
UW
1105 /* Do not remove mode or aliasing information. */
1106 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1107 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1108 break;
4b027d16 1109 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1110 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1111 enum_and_int_p, different_types_p));
4b027d16 1112 break;
400fbf9f
JW
1113
1114 case FUNCTION_TYPE:
dc5027f4
JM
1115 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1116 different_types_p);
4b027d16 1117 break;
400fbf9f
JW
1118
1119 case ARRAY_TYPE:
1120 {
400fbf9f
JW
1121 tree d1 = TYPE_DOMAIN (t1);
1122 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1123 bool d1_variable, d2_variable;
1124 bool d1_zero, d2_zero;
4b027d16 1125 val = 1;
400fbf9f
JW
1126
1127 /* Target types must match incl. qualifiers. */
1128 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1129 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1130 enum_and_int_p,
1131 different_types_p)))
400fbf9f
JW
1132 return 0;
1133
dc5027f4
JM
1134 if (different_types_p != NULL
1135 && (d1 == 0) != (d2 == 0))
1136 *different_types_p = true;
400fbf9f 1137 /* Sizes must match unless one is missing or variable. */
3f85558f 1138 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1139 break;
400fbf9f 1140
3f75a254
JM
1141 d1_zero = !TYPE_MAX_VALUE (d1);
1142 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1143
3f75a254 1144 d1_variable = (!d1_zero
3f85558f
RH
1145 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1146 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1147 d2_variable = (!d2_zero
3f85558f
RH
1148 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1149 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1150 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1151 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1152
dc5027f4
JM
1153 if (different_types_p != NULL
1154 && d1_variable != d2_variable)
1155 *different_types_p = true;
3f85558f
RH
1156 if (d1_variable || d2_variable)
1157 break;
1158 if (d1_zero && d2_zero)
1159 break;
1160 if (d1_zero || d2_zero
3f75a254
JM
1161 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1162 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1163 val = 0;
1164
c22cacf3 1165 break;
400fbf9f
JW
1166 }
1167
d1bd0ded 1168 case ENUMERAL_TYPE:
58393038 1169 case RECORD_TYPE:
d1bd0ded 1170 case UNION_TYPE:
766beae1 1171 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1172 {
fcb99e7b
JJ
1173 tree a1 = TYPE_ATTRIBUTES (t1);
1174 tree a2 = TYPE_ATTRIBUTES (t2);
1175
1176 if (! attribute_list_contained (a1, a2)
1177 && ! attribute_list_contained (a2, a1))
1178 break;
1179
f13c9b2c 1180 if (attrval != 2)
dc5027f4
JM
1181 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1182 different_types_p);
1183 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1184 different_types_p);
f13c9b2c 1185 }
4b027d16 1186 break;
e9a25f70 1187
62e1dfcf 1188 case VECTOR_TYPE:
744aa42f
ILT
1189 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1190 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1191 enum_and_int_p, different_types_p));
62e1dfcf
NC
1192 break;
1193
e9a25f70
JL
1194 default:
1195 break;
400fbf9f 1196 }
4b027d16 1197 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1198}
1199
36c5e70a
BE
1200/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1201 their qualifiers, except for named address spaces. If the pointers point to
1202 different named addresses, then we must determine if one address space is a
1203 subset of the other. */
400fbf9f
JW
1204
1205static int
744aa42f 1206comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1207{
392202b0 1208 int val;
768952be 1209 int val_ped;
36c5e70a
BE
1210 tree mvl = TREE_TYPE (ttl);
1211 tree mvr = TREE_TYPE (ttr);
1212 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1213 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1214 addr_space_t as_common;
744aa42f 1215 bool enum_and_int_p;
8b40563c 1216
36c5e70a
BE
1217 /* Fail if pointers point to incompatible address spaces. */
1218 if (!addr_space_superset (asl, asr, &as_common))
1219 return 0;
1220
768952be
MU
1221 /* For pedantic record result of comptypes on arrays before losing
1222 qualifiers on the element type below. */
1223 val_ped = 1;
1224
1225 if (TREE_CODE (mvl) == ARRAY_TYPE
1226 && TREE_CODE (mvr) == ARRAY_TYPE)
1227 val_ped = comptypes (mvl, mvr);
1228
1229 /* Qualifiers on element types of array types that are
1230 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1231
1232 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1233 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1234 : TYPE_MAIN_VARIANT (mvl));
1235
1236 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1237 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1238 : TYPE_MAIN_VARIANT (mvr));
1239
744aa42f
ILT
1240 enum_and_int_p = false;
1241 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1242
768952be
MU
1243 if (val == 1 && val_ped != 1)
1244 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1245 "are incompatible in ISO C");
1246
fcf73884 1247 if (val == 2)
c1771a20 1248 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1249
1250 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1251 warning_at (location, OPT_Wc___compat,
1252 "pointer target types incompatible in C++");
1253
400fbf9f
JW
1254 return val;
1255}
1256\f
1257/* Subroutines of `comptypes'. */
1258
f75fbaf7
ZW
1259/* Determine whether two trees derive from the same translation unit.
1260 If the CONTEXT chain ends in a null, that tree's context is still
1261 being parsed, so if two trees have context chains ending in null,
766beae1 1262 they're in the same translation unit. */
f75fbaf7 1263int
58f9752a 1264same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1265{
1266 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1267 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1268 {
6615c446
JO
1269 case tcc_declaration:
1270 t1 = DECL_CONTEXT (t1); break;
1271 case tcc_type:
1272 t1 = TYPE_CONTEXT (t1); break;
1273 case tcc_exceptional:
1274 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1275 default: gcc_unreachable ();
766beae1
ZW
1276 }
1277
1278 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1279 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1280 {
6615c446
JO
1281 case tcc_declaration:
1282 t2 = DECL_CONTEXT (t2); break;
1283 case tcc_type:
1284 t2 = TYPE_CONTEXT (t2); break;
1285 case tcc_exceptional:
1286 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1287 default: gcc_unreachable ();
766beae1
ZW
1288 }
1289
1290 return t1 == t2;
1291}
1292
f13c9b2c 1293/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1294
f13c9b2c 1295static struct tagged_tu_seen_cache *
58f9752a 1296alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1297{
cceb1885 1298 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1299 tu->next = tagged_tu_seen_base;
1300 tu->t1 = t1;
1301 tu->t2 = t2;
c22cacf3 1302
f13c9b2c 1303 tagged_tu_seen_base = tu;
c22cacf3 1304
f13c9b2c
AP
1305 /* The C standard says that two structures in different translation
1306 units are compatible with each other only if the types of their
1307 fields are compatible (among other things). We assume that they
1308 are compatible until proven otherwise when building the cache.
1309 An example where this can occur is:
1310 struct a
1311 {
1312 struct a *next;
1313 };
1314 If we are comparing this against a similar struct in another TU,
c83eecad 1315 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1316 loop. */
1317 tu->val = 1;
1318 return tu;
1319}
d1bd0ded 1320
f13c9b2c 1321/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1322
f13c9b2c
AP
1323static void
1324free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1325{
1326 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1327 while (tu != tu_til)
1328 {
741ac903
KG
1329 const struct tagged_tu_seen_cache *const tu1
1330 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1331 tu = tu1->next;
b1d5455a 1332 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1333 }
1334 tagged_tu_seen_base = tu_til;
1335}
d1bd0ded
GK
1336
1337/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1338 compatible. If the two types are not the same (which has been
1339 checked earlier), this can only happen when multiple translation
1340 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1341 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1342 comptypes_internal. */
d1bd0ded
GK
1343
1344static int
744aa42f 1345tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1346 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1347{
1348 tree s1, s2;
1349 bool needs_warning = false;
3aeb3655 1350
d1bd0ded
GK
1351 /* We have to verify that the tags of the types are the same. This
1352 is harder than it looks because this may be a typedef, so we have
1353 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1354 typedef...
1355 In the case of compiler-created builtin structs the TYPE_DECL
1356 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1357 while (TYPE_NAME (t1)
1358 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1359 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1360 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1361
dea984dc
ILT
1362 while (TYPE_NAME (t2)
1363 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1364 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1365 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1366
1367 /* C90 didn't have the requirement that the two tags be the same. */
1368 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1369 return 0;
3aeb3655 1370
d1bd0ded
GK
1371 /* C90 didn't say what happened if one or both of the types were
1372 incomplete; we choose to follow C99 rules here, which is that they
1373 are compatible. */
1374 if (TYPE_SIZE (t1) == NULL
1375 || TYPE_SIZE (t2) == NULL)
1376 return 1;
3aeb3655 1377
d1bd0ded 1378 {
f13c9b2c 1379 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1380 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1381 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1382 return tts_i->val;
d1bd0ded 1383 }
3aeb3655 1384
d1bd0ded
GK
1385 switch (TREE_CODE (t1))
1386 {
1387 case ENUMERAL_TYPE:
1388 {
f13c9b2c 1389 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1390 /* Speed up the case where the type values are in the same order. */
1391 tree tv1 = TYPE_VALUES (t1);
1392 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1393
c22cacf3 1394 if (tv1 == tv2)
f13c9b2c
AP
1395 {
1396 return 1;
1397 }
3aeb3655 1398
c22cacf3
MS
1399 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1400 {
1401 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1402 break;
1403 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1404 {
c22cacf3 1405 tu->val = 0;
f13c9b2c
AP
1406 return 0;
1407 }
c22cacf3 1408 }
3aeb3655 1409
c22cacf3 1410 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1411 {
1412 return 1;
1413 }
c22cacf3 1414 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1415 {
1416 tu->val = 0;
1417 return 0;
1418 }
3aeb3655 1419
d1bd0ded 1420 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1421 {
1422 tu->val = 0;
1423 return 0;
1424 }
3aeb3655 1425
d1bd0ded
GK
1426 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1427 {
1428 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1429 if (s2 == NULL
1430 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1431 {
1432 tu->val = 0;
1433 return 0;
1434 }
d1bd0ded
GK
1435 }
1436 return 1;
1437 }
1438
1439 case UNION_TYPE:
1440 {
f13c9b2c 1441 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1442 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1443 {
1444 tu->val = 0;
1445 return 0;
1446 }
c22cacf3 1447
f13c9b2c
AP
1448 /* Speed up the common case where the fields are in the same order. */
1449 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1450 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1451 {
1452 int result;
c22cacf3 1453
3ae4d3cc 1454 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1455 break;
744aa42f 1456 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1457 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1458
1459 if (result != 1 && !DECL_NAME (s1))
1460 break;
f13c9b2c
AP
1461 if (result == 0)
1462 {
1463 tu->val = 0;
1464 return 0;
1465 }
1466 if (result == 2)
1467 needs_warning = true;
1468
1469 if (TREE_CODE (s1) == FIELD_DECL
1470 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1471 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1472 {
1473 tu->val = 0;
1474 return 0;
1475 }
1476 }
1477 if (!s1 && !s2)
1478 {
1479 tu->val = needs_warning ? 2 : 1;
1480 return tu->val;
1481 }
d1bd0ded 1482
910ad8de 1483 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1484 {
1485 bool ok = false;
3aeb3655 1486
910ad8de 1487 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1488 if (DECL_NAME (s1) == DECL_NAME (s2))
1489 {
1490 int result;
1491
744aa42f 1492 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1493 enum_and_int_p,
1494 different_types_p);
3ae4d3cc
AO
1495
1496 if (result != 1 && !DECL_NAME (s1))
1497 continue;
1498 if (result == 0)
1499 {
1500 tu->val = 0;
1501 return 0;
1502 }
1503 if (result == 2)
1504 needs_warning = true;
1505
1506 if (TREE_CODE (s1) == FIELD_DECL
1507 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1508 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1509 break;
3ae4d3cc
AO
1510
1511 ok = true;
1512 break;
1513 }
3f75a254 1514 if (!ok)
f13c9b2c
AP
1515 {
1516 tu->val = 0;
1517 return 0;
1518 }
d1bd0ded 1519 }
f13c9b2c
AP
1520 tu->val = needs_warning ? 2 : 10;
1521 return tu->val;
d1bd0ded
GK
1522 }
1523
1524 case RECORD_TYPE:
1525 {
c22cacf3 1526 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1527
1528 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1529 s1 && s2;
910ad8de 1530 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1531 {
1532 int result;
1533 if (TREE_CODE (s1) != TREE_CODE (s2)
1534 || DECL_NAME (s1) != DECL_NAME (s2))
1535 break;
744aa42f 1536 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1537 enum_and_int_p, different_types_p);
d1bd0ded
GK
1538 if (result == 0)
1539 break;
1540 if (result == 2)
1541 needs_warning = true;
3aeb3655 1542
d1bd0ded
GK
1543 if (TREE_CODE (s1) == FIELD_DECL
1544 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1545 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1546 break;
1547 }
d1bd0ded 1548 if (s1 && s2)
f13c9b2c
AP
1549 tu->val = 0;
1550 else
1551 tu->val = needs_warning ? 2 : 1;
1552 return tu->val;
d1bd0ded
GK
1553 }
1554
1555 default:
366de0ce 1556 gcc_unreachable ();
d1bd0ded
GK
1557 }
1558}
1559
400fbf9f
JW
1560/* Return 1 if two function types F1 and F2 are compatible.
1561 If either type specifies no argument types,
1562 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1563 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1564 the other must specify that number of self-promoting arg types.
744aa42f 1565 Otherwise, the argument types must match.
dc5027f4 1566 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1567
1568static int
744aa42f 1569function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1570 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1571{
1572 tree args1, args2;
1573 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1574 int val = 1;
1575 int val1;
a6fdc086
GK
1576 tree ret1, ret2;
1577
1578 ret1 = TREE_TYPE (f1);
1579 ret2 = TREE_TYPE (f2);
1580
e508a019
JM
1581 /* 'volatile' qualifiers on a function's return type used to mean
1582 the function is noreturn. */
1583 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1584 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1585 if (TYPE_VOLATILE (ret1))
1586 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1587 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1588 if (TYPE_VOLATILE (ret2))
1589 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1590 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1591 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1592 if (val == 0)
400fbf9f
JW
1593 return 0;
1594
1595 args1 = TYPE_ARG_TYPES (f1);
1596 args2 = TYPE_ARG_TYPES (f2);
1597
dc5027f4
JM
1598 if (different_types_p != NULL
1599 && (args1 == 0) != (args2 == 0))
1600 *different_types_p = true;
1601
400fbf9f
JW
1602 /* An unspecified parmlist matches any specified parmlist
1603 whose argument types don't need default promotions. */
1604
1605 if (args1 == 0)
1606 {
1607 if (!self_promoting_args_p (args2))
1608 return 0;
1609 /* If one of these types comes from a non-prototype fn definition,
1610 compare that with the other type's arglist.
3176a0c2 1611 If they don't match, ask for a warning (but no error). */
400fbf9f 1612 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1613 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1614 enum_and_int_p, different_types_p))
400fbf9f
JW
1615 val = 2;
1616 return val;
1617 }
1618 if (args2 == 0)
1619 {
1620 if (!self_promoting_args_p (args1))
1621 return 0;
1622 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1623 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1624 enum_and_int_p, different_types_p))
400fbf9f
JW
1625 val = 2;
1626 return val;
1627 }
1628
1629 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1630 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1631 different_types_p);
400fbf9f
JW
1632 return val1 != 1 ? val1 : val;
1633}
1634
744aa42f
ILT
1635/* Check two lists of types for compatibility, returning 0 for
1636 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1637 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1638 comptypes_internal. */
400fbf9f
JW
1639
1640static int
744aa42f 1641type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1642 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1643{
1644 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1645 int val = 1;
9d5f3e49 1646 int newval = 0;
400fbf9f
JW
1647
1648 while (1)
1649 {
46df2823 1650 tree a1, mv1, a2, mv2;
400fbf9f
JW
1651 if (args1 == 0 && args2 == 0)
1652 return val;
1653 /* If one list is shorter than the other,
1654 they fail to match. */
1655 if (args1 == 0 || args2 == 0)
1656 return 0;
46df2823
JM
1657 mv1 = a1 = TREE_VALUE (args1);
1658 mv2 = a2 = TREE_VALUE (args2);
1659 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1660 mv1 = (TYPE_ATOMIC (mv1)
1661 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1662 TYPE_QUAL_ATOMIC)
1663 : TYPE_MAIN_VARIANT (mv1));
46df2823 1664 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1665 mv2 = (TYPE_ATOMIC (mv2)
1666 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1667 TYPE_QUAL_ATOMIC)
1668 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1669 /* A null pointer instead of a type
1670 means there is supposed to be an argument
1671 but nothing is specified about what type it has.
1672 So match anything that self-promotes. */
dc5027f4
JM
1673 if (different_types_p != NULL
1674 && (a1 == 0) != (a2 == 0))
1675 *different_types_p = true;
46df2823 1676 if (a1 == 0)
400fbf9f 1677 {
46df2823 1678 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1679 return 0;
1680 }
46df2823 1681 else if (a2 == 0)
400fbf9f 1682 {
46df2823 1683 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1684 return 0;
1685 }
8f5b6d29 1686 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1687 else if (TREE_CODE (a1) == ERROR_MARK
1688 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1689 ;
dc5027f4
JM
1690 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1691 different_types_p)))
400fbf9f 1692 {
dc5027f4
JM
1693 if (different_types_p != NULL)
1694 *different_types_p = true;
400fbf9f
JW
1695 /* Allow wait (union {union wait *u; int *i} *)
1696 and wait (union wait *) to be compatible. */
46df2823
JM
1697 if (TREE_CODE (a1) == UNION_TYPE
1698 && (TYPE_NAME (a1) == 0
ebf0bf7f 1699 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1700 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1701 && tree_int_cst_equal (TYPE_SIZE (a1),
1702 TYPE_SIZE (a2)))
400fbf9f
JW
1703 {
1704 tree memb;
46df2823 1705 for (memb = TYPE_FIELDS (a1);
910ad8de 1706 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1707 {
1708 tree mv3 = TREE_TYPE (memb);
1709 if (mv3 && mv3 != error_mark_node
1710 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1711 mv3 = (TYPE_ATOMIC (mv3)
1712 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1713 TYPE_QUAL_ATOMIC)
1714 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1715 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1716 different_types_p))
58cb41e6
JJ
1717 break;
1718 }
400fbf9f
JW
1719 if (memb == 0)
1720 return 0;
1721 }
46df2823
JM
1722 else if (TREE_CODE (a2) == UNION_TYPE
1723 && (TYPE_NAME (a2) == 0
ebf0bf7f 1724 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1725 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1726 && tree_int_cst_equal (TYPE_SIZE (a2),
1727 TYPE_SIZE (a1)))
400fbf9f
JW
1728 {
1729 tree memb;
46df2823 1730 for (memb = TYPE_FIELDS (a2);
910ad8de 1731 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1732 {
1733 tree mv3 = TREE_TYPE (memb);
1734 if (mv3 && mv3 != error_mark_node
1735 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1736 mv3 = (TYPE_ATOMIC (mv3)
1737 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1738 TYPE_QUAL_ATOMIC)
1739 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1740 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1741 different_types_p))
58cb41e6
JJ
1742 break;
1743 }
400fbf9f
JW
1744 if (memb == 0)
1745 return 0;
1746 }
1747 else
1748 return 0;
1749 }
1750
1751 /* comptypes said ok, but record if it said to warn. */
1752 if (newval > val)
1753 val = newval;
1754
1755 args1 = TREE_CHAIN (args1);
1756 args2 = TREE_CHAIN (args2);
1757 }
1758}
400fbf9f 1759\f
a0e24419
MP
1760/* Compute the size to increment a pointer by. When a function type or void
1761 type or incomplete type is passed, size_one_node is returned.
1762 This function does not emit any diagnostics; the caller is responsible
1763 for that. */
400fbf9f 1764
4e2fb7de 1765static tree
58f9752a 1766c_size_in_bytes (const_tree type)
400fbf9f
JW
1767{
1768 enum tree_code code = TREE_CODE (type);
1769
a0e24419
MP
1770 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1771 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1772 return size_one_node;
1773
400fbf9f 1774 /* Convert in case a char is more than one unit. */
db3927fb
AH
1775 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1776 size_int (TYPE_PRECISION (char_type_node)
1777 / BITS_PER_UNIT));
400fbf9f 1778}
400fbf9f 1779\f
400fbf9f
JW
1780/* Return either DECL or its known constant value (if it has one). */
1781
56cb9733 1782tree
2f6e4e97 1783decl_constant_value (tree decl)
400fbf9f 1784{
a7c1916a 1785 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1786 in a place where a variable is invalid. Note that DECL_INITIAL
1787 isn't valid for a PARM_DECL. */
a7c1916a 1788 current_function_decl != 0
4f976745 1789 && TREE_CODE (decl) != PARM_DECL
3f75a254 1790 && !TREE_THIS_VOLATILE (decl)
83bab8db 1791 && TREE_READONLY (decl)
400fbf9f
JW
1792 && DECL_INITIAL (decl) != 0
1793 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1794 /* This is invalid if initial value is not constant.
1795 If it has either a function call, a memory reference,
1796 or a variable, then re-evaluating it could give different results. */
1797 && TREE_CONSTANT (DECL_INITIAL (decl))
1798 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1799 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1800 return DECL_INITIAL (decl);
1801 return decl;
1802}
1803
f2a71bbc
JM
1804/* Convert the array expression EXP to a pointer. */
1805static tree
c2255bc4 1806array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1807{
f2a71bbc 1808 tree orig_exp = exp;
207bf485 1809 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1810 tree adr;
1811 tree restype = TREE_TYPE (type);
1812 tree ptrtype;
207bf485 1813
f2a71bbc 1814 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1815
f2a71bbc 1816 STRIP_TYPE_NOPS (exp);
207bf485 1817
487a92fe
JM
1818 if (TREE_NO_WARNING (orig_exp))
1819 TREE_NO_WARNING (exp) = 1;
207bf485 1820
f2a71bbc
JM
1821 ptrtype = build_pointer_type (restype);
1822
22d03525 1823 if (INDIRECT_REF_P (exp))
f2a71bbc
JM
1824 return convert (ptrtype, TREE_OPERAND (exp, 0));
1825
1f37c583
JM
1826 /* In C++ array compound literals are temporary objects unless they are
1827 const or appear in namespace scope, so they are destroyed too soon
1828 to use them for much of anything (c++/53220). */
1829 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1830 {
1831 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1832 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1833 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1834 "converting an array compound literal to a pointer "
1835 "is ill-formed in C++");
1836 }
1837
c2255bc4 1838 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1839 return convert (ptrtype, adr);
1840}
207bf485 1841
f2a71bbc
JM
1842/* Convert the function expression EXP to a pointer. */
1843static tree
c2255bc4 1844function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1845{
1846 tree orig_exp = exp;
207bf485 1847
f2a71bbc 1848 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1849
f2a71bbc 1850 STRIP_TYPE_NOPS (exp);
207bf485 1851
f2a71bbc
JM
1852 if (TREE_NO_WARNING (orig_exp))
1853 TREE_NO_WARNING (exp) = 1;
207bf485 1854
c2255bc4 1855 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1856}
207bf485 1857
ebfbbdc5
JJ
1858/* Mark EXP as read, not just set, for set but not used -Wunused
1859 warning purposes. */
1860
1861void
1862mark_exp_read (tree exp)
1863{
1864 switch (TREE_CODE (exp))
1865 {
1866 case VAR_DECL:
1867 case PARM_DECL:
1868 DECL_READ_P (exp) = 1;
1869 break;
1870 case ARRAY_REF:
1871 case COMPONENT_REF:
1872 case MODIFY_EXPR:
1873 case REALPART_EXPR:
1874 case IMAGPART_EXPR:
1875 CASE_CONVERT:
1876 case ADDR_EXPR:
1877 mark_exp_read (TREE_OPERAND (exp, 0));
1878 break;
1879 case COMPOUND_EXPR:
82c3c067 1880 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1881 mark_exp_read (TREE_OPERAND (exp, 1));
1882 break;
1883 default:
1884 break;
1885 }
1886}
1887
f2a71bbc
JM
1888/* Perform the default conversion of arrays and functions to pointers.
1889 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1890 return EXP.
1891
1892 LOC is the location of the expression. */
f2a71bbc
JM
1893
1894struct c_expr
c2255bc4 1895default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1896{
1897 tree orig_exp = exp.value;
1898 tree type = TREE_TYPE (exp.value);
1899 enum tree_code code = TREE_CODE (type);
1900
1901 switch (code)
1902 {
1903 case ARRAY_TYPE:
1904 {
1905 bool not_lvalue = false;
1906 bool lvalue_array_p;
1907
1908 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1909 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1910 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1911 {
1912 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1913 not_lvalue = true;
1914 exp.value = TREE_OPERAND (exp.value, 0);
1915 }
1916
1917 if (TREE_NO_WARNING (orig_exp))
1918 TREE_NO_WARNING (exp.value) = 1;
1919
1920 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1921 if (!flag_isoc99 && !lvalue_array_p)
1922 {
1923 /* Before C99, non-lvalue arrays do not decay to pointers.
1924 Normally, using such an array would be invalid; but it can
1925 be used correctly inside sizeof or as a statement expression.
1926 Thus, do not give an error here; an error will result later. */
1927 return exp;
1928 }
1929
c2255bc4 1930 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1931 }
1932 break;
1933 case FUNCTION_TYPE:
c2255bc4 1934 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1935 break;
1936 default:
f2a71bbc 1937 break;
207bf485 1938 }
f2a71bbc 1939
207bf485
JM
1940 return exp;
1941}
1942
ebfbbdc5
JJ
1943struct c_expr
1944default_function_array_read_conversion (location_t loc, struct c_expr exp)
1945{
1946 mark_exp_read (exp.value);
1947 return default_function_array_conversion (loc, exp);
1948}
522ddfa2 1949
267bac10
JM
1950/* Return whether EXPR should be treated as an atomic lvalue for the
1951 purposes of load and store handling. */
1952
1953static bool
1954really_atomic_lvalue (tree expr)
1955{
7a0ca710 1956 if (error_operand_p (expr))
267bac10
JM
1957 return false;
1958 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1959 return false;
1960 if (!lvalue_p (expr))
1961 return false;
1962
1963 /* Ignore _Atomic on register variables, since their addresses can't
1964 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1965 sequences wouldn't work. Ignore _Atomic on structures containing
1966 bit-fields, since accessing elements of atomic structures or
1967 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1968 it's undefined at translation time or execution time, and the
1969 normal atomic sequences again wouldn't work. */
1970 while (handled_component_p (expr))
1971 {
1972 if (TREE_CODE (expr) == COMPONENT_REF
1973 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1974 return false;
1975 expr = TREE_OPERAND (expr, 0);
1976 }
1977 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1978 return false;
1979 return true;
1980}
1981
1982/* Convert expression EXP (location LOC) from lvalue to rvalue,
1983 including converting functions and arrays to pointers if CONVERT_P.
1984 If READ_P, also mark the expression as having been read. */
1985
1986struct c_expr
1987convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1988 bool convert_p, bool read_p)
1989{
1990 if (read_p)
1991 mark_exp_read (exp.value);
1992 if (convert_p)
1993 exp = default_function_array_conversion (loc, exp);
1994 if (really_atomic_lvalue (exp.value))
1995 {
1996 vec<tree, va_gc> *params;
1997 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1998 tree expr_type = TREE_TYPE (exp.value);
1999 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2000 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2001
2002 gcc_assert (TYPE_ATOMIC (expr_type));
2003
2004 /* Expansion of a generic atomic load may require an addition
2005 element, so allocate enough to prevent a resize. */
2006 vec_alloc (params, 4);
2007
2008 /* Remove the qualifiers for the rest of the expressions and
2009 create the VAL temp variable to hold the RHS. */
2010 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
5c4abbb8 2011 tmp = create_tmp_var_raw (nonatomic_type);
267bac10
JM
2012 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2013 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2014 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2015
2016 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2017 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2018 params->quick_push (expr_addr);
2019 params->quick_push (tmp_addr);
2020 params->quick_push (seq_cst);
8edbfaa6 2021 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2022
cc28fc7f
MP
2023 /* EXPR is always read. */
2024 mark_exp_read (exp.value);
2025
267bac10 2026 /* Return tmp which contains the value loaded. */
5c4abbb8
MP
2027 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2028 NULL_TREE, NULL_TREE);
267bac10
JM
2029 }
2030 return exp;
2031}
2032
522ddfa2
JM
2033/* EXP is an expression of integer type. Apply the integer promotions
2034 to it and return the promoted value. */
400fbf9f
JW
2035
2036tree
522ddfa2 2037perform_integral_promotions (tree exp)
400fbf9f 2038{
b3694847
SS
2039 tree type = TREE_TYPE (exp);
2040 enum tree_code code = TREE_CODE (type);
400fbf9f 2041
522ddfa2 2042 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2043
400fbf9f
JW
2044 /* Normally convert enums to int,
2045 but convert wide enums to something wider. */
2046 if (code == ENUMERAL_TYPE)
2047 {
b0c48229
NB
2048 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2049 TYPE_PRECISION (integer_type_node)),
2050 ((TYPE_PRECISION (type)
2051 >= TYPE_PRECISION (integer_type_node))
8df83eae 2052 && TYPE_UNSIGNED (type)));
05bccae2 2053
400fbf9f
JW
2054 return convert (type, exp);
2055 }
2056
522ddfa2
JM
2057 /* ??? This should no longer be needed now bit-fields have their
2058 proper types. */
9753f113 2059 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2060 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2061 /* If it's thinner than an int, promote it like a
d72040f5 2062 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2063 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2064 TYPE_PRECISION (integer_type_node)))
f458d1d5 2065 return convert (integer_type_node, exp);
9753f113 2066
d72040f5 2067 if (c_promoting_integer_type_p (type))
400fbf9f 2068 {
f458d1d5 2069 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2070 if (TYPE_UNSIGNED (type)
f458d1d5 2071 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2072 return convert (unsigned_type_node, exp);
05bccae2 2073
400fbf9f
JW
2074 return convert (integer_type_node, exp);
2075 }
05bccae2 2076
522ddfa2
JM
2077 return exp;
2078}
2079
2080
2081/* Perform default promotions for C data used in expressions.
46bdb9cf 2082 Enumeral types or short or char are converted to int.
522ddfa2
JM
2083 In addition, manifest constants symbols are replaced by their values. */
2084
2085tree
2086default_conversion (tree exp)
2087{
2088 tree orig_exp;
2089 tree type = TREE_TYPE (exp);
2090 enum tree_code code = TREE_CODE (type);
40449a90 2091 tree promoted_type;
522ddfa2 2092
ebfbbdc5
JJ
2093 mark_exp_read (exp);
2094
46bdb9cf
JM
2095 /* Functions and arrays have been converted during parsing. */
2096 gcc_assert (code != FUNCTION_TYPE);
2097 if (code == ARRAY_TYPE)
2098 return exp;
522ddfa2
JM
2099
2100 /* Constants can be used directly unless they're not loadable. */
2101 if (TREE_CODE (exp) == CONST_DECL)
2102 exp = DECL_INITIAL (exp);
2103
522ddfa2
JM
2104 /* Strip no-op conversions. */
2105 orig_exp = exp;
2106 STRIP_TYPE_NOPS (exp);
2107
2108 if (TREE_NO_WARNING (orig_exp))
2109 TREE_NO_WARNING (exp) = 1;
2110
400fbf9f
JW
2111 if (code == VOID_TYPE)
2112 {
5436fa2e
MP
2113 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2114 "void value not ignored as it ought to be");
400fbf9f
JW
2115 return error_mark_node;
2116 }
808d6eaa
JM
2117
2118 exp = require_complete_type (exp);
2119 if (exp == error_mark_node)
2120 return error_mark_node;
2121
40449a90
SL
2122 promoted_type = targetm.promoted_type (type);
2123 if (promoted_type)
2124 return convert (promoted_type, exp);
2125
808d6eaa
JM
2126 if (INTEGRAL_TYPE_P (type))
2127 return perform_integral_promotions (exp);
2128
400fbf9f
JW
2129 return exp;
2130}
2131\f
0fb96aa4 2132/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2133
2134 If the component name is not found, returns NULL_TREE. Otherwise,
2135 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2136 stepping down the chain to the component, which is in the last
2137 TREE_VALUE of the list. Normally the list is of length one, but if
2138 the component is embedded within (nested) anonymous structures or
2139 unions, the list steps down the chain to the component. */
2f6e4e97 2140
2f2d13da 2141static tree
0fb96aa4 2142lookup_field (tree type, tree component)
2f2d13da
DE
2143{
2144 tree field;
2145
2146 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2147 to the field elements. Use a binary search on this array to quickly
2148 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2149 will always be set for structures which have many elements. */
2150
22a0b85f 2151 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2152 {
2153 int bot, top, half;
d07605f5 2154 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2155
2156 field = TYPE_FIELDS (type);
2157 bot = 0;
d07605f5 2158 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2159 while (top - bot > 1)
2160 {
2f2d13da
DE
2161 half = (top - bot + 1) >> 1;
2162 field = field_array[bot+half];
2163
2164 if (DECL_NAME (field) == NULL_TREE)
2165 {
2166 /* Step through all anon unions in linear fashion. */
2167 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2168 {
2f2d13da 2169 field = field_array[bot++];
3e636daf 2170 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
19d76e60 2171 {
0fb96aa4 2172 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2173
2174 if (anon)
2175 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2176
2177 /* The Plan 9 compiler permits referring
2178 directly to an anonymous struct/union field
2179 using a typedef name. */
2180 if (flag_plan9_extensions
2181 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2182 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2183 == TYPE_DECL)
2184 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2185 == component))
2186 break;
2f6e4e97 2187 }
2f2d13da
DE
2188 }
2189
2190 /* Entire record is only anon unions. */
2191 if (bot > top)
2192 return NULL_TREE;
2193
2194 /* Restart the binary search, with new lower bound. */
2195 continue;
2196 }
2197
e8b87aac 2198 if (DECL_NAME (field) == component)
2f2d13da 2199 break;
e8b87aac 2200 if (DECL_NAME (field) < component)
2f2d13da
DE
2201 bot += half;
2202 else
2203 top = bot + half;
2204 }
2205
2206 if (DECL_NAME (field_array[bot]) == component)
2207 field = field_array[bot];
2208 else if (DECL_NAME (field) != component)
e9b2c823 2209 return NULL_TREE;
2f2d13da
DE
2210 }
2211 else
2212 {
910ad8de 2213 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2214 {
e9b2c823 2215 if (DECL_NAME (field) == NULL_TREE
3e636daf 2216 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2f2d13da 2217 {
0fb96aa4 2218 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2219
e9b2c823
NB
2220 if (anon)
2221 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2222
2223 /* The Plan 9 compiler permits referring directly to an
2224 anonymous struct/union field using a typedef
2225 name. */
2226 if (flag_plan9_extensions
2227 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2228 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2229 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2230 == component))
2231 break;
2f2d13da
DE
2232 }
2233
2234 if (DECL_NAME (field) == component)
2235 break;
2236 }
e9b2c823
NB
2237
2238 if (field == NULL_TREE)
2239 return NULL_TREE;
2f2d13da
DE
2240 }
2241
e9b2c823 2242 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2243}
2244
277fe616
DM
2245/* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2246
2247static void
2248lookup_field_fuzzy_find_candidates (tree type, tree component,
2249 vec<tree> *candidates)
2250{
2251 tree field;
2252 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2253 {
2254 if (DECL_NAME (field) == NULL_TREE
3e636daf
MP
2255 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2256 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2257 candidates);
277fe616
DM
2258
2259 if (DECL_NAME (field))
2260 candidates->safe_push (DECL_NAME (field));
2261 }
2262}
2263
2264/* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2265 rather than returning a TREE_LIST for an exact match. */
2266
2267static tree
2268lookup_field_fuzzy (tree type, tree component)
2269{
2270 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2271
2272 /* First, gather a list of candidates. */
2273 auto_vec <tree> candidates;
2274
2275 lookup_field_fuzzy_find_candidates (type, component,
2276 &candidates);
2277
8ece8dfb 2278 return find_closest_identifier (component, &candidates);
277fe616
DM
2279}
2280
a87a86e1
DM
2281/* Support function for build_component_ref's error-handling.
2282
2283 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2284 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2285
2286static bool
2287should_suggest_deref_p (tree datum_type)
2288{
2289 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2290 allows "." for ptrs; we could be handling a failed attempt
2291 to access a property. */
2292 if (c_dialect_objc ())
2293 return false;
2294
2295 /* Only suggest it for pointers... */
2296 if (TREE_CODE (datum_type) != POINTER_TYPE)
2297 return false;
2298
2299 /* ...to structs/unions. */
2300 tree underlying_type = TREE_TYPE (datum_type);
2301 enum tree_code code = TREE_CODE (underlying_type);
2302 if (code == RECORD_TYPE || code == UNION_TYPE)
2303 return true;
2304 else
2305 return false;
2306}
2307
c2255bc4
AH
2308/* Make an expression to refer to the COMPONENT field of structure or
2309 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2310 location of the COMPONENT_REF. */
400fbf9f
JW
2311
2312tree
c2255bc4 2313build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2314{
b3694847
SS
2315 tree type = TREE_TYPE (datum);
2316 enum tree_code code = TREE_CODE (type);
2317 tree field = NULL;
2318 tree ref;
1e57bf47 2319 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2320
7a3ea201
RH
2321 if (!objc_is_public (datum, component))
2322 return error_mark_node;
2323
46a88c12 2324 /* Detect Objective-C property syntax object.property. */
668ea4b1 2325 if (c_dialect_objc ()
46a88c12 2326 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2327 return ref;
2328
400fbf9f
JW
2329 /* See if there is a field or component with name COMPONENT. */
2330
2331 if (code == RECORD_TYPE || code == UNION_TYPE)
2332 {
d0f062fb 2333 if (!COMPLETE_TYPE_P (type))
400fbf9f 2334 {
7a228918 2335 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2336 return error_mark_node;
2337 }
2338
0fb96aa4 2339 field = lookup_field (type, component);
400fbf9f
JW
2340
2341 if (!field)
2342 {
277fe616
DM
2343 tree guessed_id = lookup_field_fuzzy (type, component);
2344 if (guessed_id)
2345 error_at (loc, "%qT has no member named %qE; did you mean %qE?",
2346 type, component, guessed_id);
2347 else
2348 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2349 return error_mark_node;
2350 }
400fbf9f 2351
fda5652f
MP
2352 /* Accessing elements of atomic structures or unions is undefined
2353 behavior (C11 6.5.2.3#5). */
2354 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2355 {
2356 if (code == RECORD_TYPE)
2357 warning_at (loc, 0, "accessing a member %qE of an atomic "
2358 "structure %qE", component, datum);
2359 else
2360 warning_at (loc, 0, "accessing a member %qE of an atomic "
2361 "union %qE", component, datum);
2362 }
2363
e9b2c823
NB
2364 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2365 This might be better solved in future the way the C++ front
2366 end does it - by giving the anonymous entities each a
2367 separate name and type, and then have build_component_ref
2368 recursively call itself. We can't do that here. */
46ea50cb 2369 do
19d76e60 2370 {
e9b2c823 2371 tree subdatum = TREE_VALUE (field);
efed193e
JM
2372 int quals;
2373 tree subtype;
1e57bf47 2374 bool use_datum_quals;
e9b2c823
NB
2375
2376 if (TREE_TYPE (subdatum) == error_mark_node)
2377 return error_mark_node;
2378
1e57bf47
JM
2379 /* If this is an rvalue, it does not have qualifiers in C
2380 standard terms and we must avoid propagating such
2381 qualifiers down to a non-lvalue array that is then
2382 converted to a pointer. */
2383 use_datum_quals = (datum_lvalue
2384 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2385
efed193e 2386 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2387 if (use_datum_quals)
2388 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2389 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2390
2391 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2392 NULL_TREE);
c2255bc4 2393 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2394 if (TREE_READONLY (subdatum)
2395 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2396 TREE_READONLY (ref) = 1;
1e57bf47
JM
2397 if (TREE_THIS_VOLATILE (subdatum)
2398 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2399 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2400
2401 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2402 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2403
19d76e60 2404 datum = ref;
46ea50cb
RS
2405
2406 field = TREE_CHAIN (field);
19d76e60 2407 }
46ea50cb 2408 while (field);
19d76e60 2409
400fbf9f
JW
2410 return ref;
2411 }
a87a86e1
DM
2412 else if (should_suggest_deref_p (type))
2413 {
2414 /* Special-case the error message for "ptr.field" for the case
2415 where the user has confused "." vs "->". */
2416 rich_location richloc (line_table, loc);
2417 /* "loc" should be the "." token. */
2418 richloc.add_fixit_replace (source_range::from_location (loc), "->");
2419 error_at_rich_loc (&richloc,
2420 "%qE is a pointer; did you mean to use %<->%>?",
2421 datum);
2422 return error_mark_node;
2423 }
400fbf9f 2424 else if (code != ERROR_MARK)
c2255bc4
AH
2425 error_at (loc,
2426 "request for member %qE in something not a structure or union",
2427 component);
400fbf9f
JW
2428
2429 return error_mark_node;
2430}
2431\f
2432/* Given an expression PTR for a pointer, return an expression
2433 for the value pointed to.
6a3799eb
AH
2434 ERRORSTRING is the name of the operator to appear in error messages.
2435
2436 LOC is the location to use for the generated tree. */
400fbf9f
JW
2437
2438tree
dd865ef6 2439build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2440{
b3694847
SS
2441 tree pointer = default_conversion (ptr);
2442 tree type = TREE_TYPE (pointer);
6a3799eb 2443 tree ref;
400fbf9f
JW
2444
2445 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2446 {
1043771b 2447 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2448 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2449 {
2450 /* If a warning is issued, mark it to avoid duplicates from
2451 the backend. This only needs to be done at
2452 warn_strict_aliasing > 2. */
2453 if (warn_strict_aliasing > 2)
2454 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2455 type, TREE_OPERAND (pointer, 0)))
2456 TREE_NO_WARNING (pointer) = 1;
2457 }
2458
870cc33b 2459 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2460 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2461 == TREE_TYPE (type)))
6a3799eb
AH
2462 {
2463 ref = TREE_OPERAND (pointer, 0);
2464 protected_set_expr_location (ref, loc);
2465 return ref;
2466 }
870cc33b
RS
2467 else
2468 {
2469 tree t = TREE_TYPE (type);
46df2823 2470
984dfd8c 2471 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2472
baae9b65 2473 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2474 {
d9b7be2e
MP
2475 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2476 {
2477 error_at (loc, "dereferencing pointer to incomplete type "
2478 "%qT", t);
2479 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2480 }
870cc33b
RS
2481 return error_mark_node;
2482 }
7d882b83 2483 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2484 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2485
2486 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2487 so that we get the proper error message if the result is used
2488 to assign to. Also, &* is supposed to be a no-op.
2489 And ANSI C seems to specify that the type of the result
2490 should be the const type. */
2491 /* A de-reference of a pointer to const is not a const. It is valid
2492 to change it via some other pointer. */
2493 TREE_READONLY (ref) = TYPE_READONLY (t);
2494 TREE_SIDE_EFFECTS (ref)
271bd540 2495 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2496 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2497 protected_set_expr_location (ref, loc);
870cc33b
RS
2498 return ref;
2499 }
2500 }
400fbf9f 2501 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2502 invalid_indirection_error (loc, type, errstring);
2503
400fbf9f
JW
2504 return error_mark_node;
2505}
2506
2507/* This handles expressions of the form "a[i]", which denotes
2508 an array reference.
2509
2510 This is logically equivalent in C to *(a+i), but we may do it differently.
2511 If A is a variable or a member, we generate a primitive ARRAY_REF.
2512 This avoids forcing the array out of registers, and can work on
2513 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2514 by functions).
2515
30cd1c5d
AS
2516 For vector types, allow vector[i] but not i[vector], and create
2517 *(((type*)&vectortype) + i) for the expression.
2518
6a3799eb 2519 LOC is the location to use for the returned expression. */
400fbf9f
JW
2520
2521tree
c2255bc4 2522build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2523{
6a3799eb 2524 tree ret;
a4ab7973 2525 bool swapped = false;
400fbf9f
JW
2526 if (TREE_TYPE (array) == error_mark_node
2527 || TREE_TYPE (index) == error_mark_node)
2528 return error_mark_node;
2529
b72271b9 2530 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2531 {
2532 size_t rank = 0;
2533 if (!find_rank (loc, index, index, true, &rank))
2534 return error_mark_node;
2535 if (rank > 1)
2536 {
2537 error_at (loc, "rank of the array's index is greater than 1");
2538 return error_mark_node;
2539 }
2540 }
a4ab7973 2541 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2542 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2543 /* Allow vector[index] but not index[vector]. */
31521951 2544 && !VECTOR_TYPE_P (TREE_TYPE (array)))
400fbf9f 2545 {
a4ab7973
JM
2546 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2547 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2548 {
f90e8e2e 2549 error_at (loc,
30cd1c5d
AS
2550 "subscripted value is neither array nor pointer nor vector");
2551
fdeefd49
RS
2552 return error_mark_node;
2553 }
fab27f52 2554 std::swap (array, index);
a4ab7973
JM
2555 swapped = true;
2556 }
2557
2558 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2559 {
a63068b6 2560 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2561 return error_mark_node;
2562 }
2563
2564 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2565 {
a63068b6 2566 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2567 return error_mark_node;
2568 }
2569
ff6b6641
GDR
2570 /* ??? Existing practice has been to warn only when the char
2571 index is syntactically the index, not for char[array]. */
2572 if (!swapped)
5bd012f8 2573 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2574
2575 /* Apply default promotions *after* noticing character types. */
2576 index = default_conversion (index);
f406ae1f
MP
2577 if (index == error_mark_node)
2578 return error_mark_node;
a4ab7973
JM
2579
2580 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2581
aa7da51a
JJ
2582 bool non_lvalue
2583 = convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2584
2585 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2586 {
e4d35515 2587 tree rval, type;
fdeefd49 2588
400fbf9f
JW
2589 /* An array that is indexed by a non-constant
2590 cannot be stored in a register; we must be able to do
2591 address arithmetic on its address.
2592 Likewise an array of elements of variable size. */
2593 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2594 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2595 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2596 {
dffd7eb6 2597 if (!c_mark_addressable (array))
400fbf9f
JW
2598 return error_mark_node;
2599 }
e6d52559
JW
2600 /* An array that is indexed by a constant value which is not within
2601 the array bounds cannot be stored in a register either; because we
2602 would get a crash in store_bit_field/extract_bit_field when trying
2603 to access a non-existent part of the register. */
2604 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2605 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2606 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2607 {
dffd7eb6 2608 if (!c_mark_addressable (array))
e6d52559
JW
2609 return error_mark_node;
2610 }
400fbf9f 2611
f3bede71 2612 if (pedantic || warn_c90_c99_compat)
400fbf9f
JW
2613 {
2614 tree foo = array;
2615 while (TREE_CODE (foo) == COMPONENT_REF)
2616 foo = TREE_OPERAND (foo, 0);
0ae9bd27 2617 if (VAR_P (foo) && C_DECL_REGISTER (foo))
c1771a20 2618 pedwarn (loc, OPT_Wpedantic,
fcf73884 2619 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2620 else if (!lvalue_p (foo))
2621 pedwarn_c90 (loc, OPT_Wpedantic,
2622 "ISO C90 forbids subscripting non-lvalue "
2623 "array");
400fbf9f
JW
2624 }
2625
46df2823 2626 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2627 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2628 /* Array ref is const/volatile if the array elements are
c22cacf3 2629 or if the array is. */
400fbf9f
JW
2630 TREE_READONLY (rval)
2631 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2632 | TREE_READONLY (array));
2633 TREE_SIDE_EFFECTS (rval)
2634 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2635 | TREE_SIDE_EFFECTS (array));
2636 TREE_THIS_VOLATILE (rval)
2637 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2638 /* This was added by rms on 16 Nov 91.
2f6e4e97 2639 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2640 in an inline function.
2641 Hope it doesn't break something else. */
2642 | TREE_THIS_VOLATILE (array));
928c19bb 2643 ret = require_complete_type (rval);
6a3799eb 2644 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2645 if (non_lvalue)
2646 ret = non_lvalue_loc (loc, ret);
6a3799eb 2647 return ret;
400fbf9f 2648 }
a4ab7973
JM
2649 else
2650 {
2651 tree ar = default_conversion (array);
400fbf9f 2652
a4ab7973
JM
2653 if (ar == error_mark_node)
2654 return ar;
400fbf9f 2655
a4ab7973
JM
2656 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2657 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2658
aa7da51a
JJ
2659 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2660 index, 0),
2661 RO_ARRAY_INDEXING);
2662 if (non_lvalue)
2663 ret = non_lvalue_loc (loc, ret);
2664 return ret;
a4ab7973 2665 }
400fbf9f
JW
2666}
2667\f
7e585d16 2668/* Build an external reference to identifier ID. FUN indicates
766beb40 2669 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2670 location of the identifier. This sets *TYPE to the type of the
2671 identifier, which is not the same as the type of the returned value
2672 for CONST_DECLs defined as enum constants. If the type of the
2673 identifier is not available, *TYPE is set to NULL. */
7e585d16 2674tree
c2255bc4 2675build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2676{
2677 tree ref;
2678 tree decl = lookup_name (id);
16b34ad6
ZL
2679
2680 /* In Objective-C, an instance variable (ivar) may be preferred to
2681 whatever lookup_name() found. */
2682 decl = objc_lookup_ivar (decl, id);
7e585d16 2683
6866c6e8 2684 *type = NULL;
339a28b9 2685 if (decl && decl != error_mark_node)
6866c6e8
ILT
2686 {
2687 ref = decl;
2688 *type = TREE_TYPE (ref);
2689 }
339a28b9
ZW
2690 else if (fun)
2691 /* Implicit function declaration. */
c2255bc4 2692 ref = implicitly_declare (loc, id);
339a28b9
ZW
2693 else if (decl == error_mark_node)
2694 /* Don't complain about something that's already been
2695 complained about. */
2696 return error_mark_node;
2697 else
2698 {
c2255bc4 2699 undeclared_variable (loc, id);
339a28b9
ZW
2700 return error_mark_node;
2701 }
7e585d16
ZW
2702
2703 if (TREE_TYPE (ref) == error_mark_node)
2704 return error_mark_node;
2705
339a28b9 2706 if (TREE_DEPRECATED (ref))
9b86d6bb 2707 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2708
ad960f56 2709 /* Recursive call does not count as usage. */
b8698a0f 2710 if (ref != current_function_decl)
ad960f56 2711 {
ad960f56
MLI
2712 TREE_USED (ref) = 1;
2713 }
7e585d16 2714
bc4b653b
JM
2715 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2716 {
2717 if (!in_sizeof && !in_typeof)
2718 C_DECL_USED (ref) = 1;
2719 else if (DECL_INITIAL (ref) == 0
2720 && DECL_EXTERNAL (ref)
2721 && !TREE_PUBLIC (ref))
2722 record_maybe_used_decl (ref);
2723 }
2724
7e585d16
ZW
2725 if (TREE_CODE (ref) == CONST_DECL)
2726 {
6193b8b7 2727 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2728
2729 if (warn_cxx_compat
2730 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2731 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2732 {
2733 warning_at (loc, OPT_Wc___compat,
2734 ("enum constant defined in struct or union "
2735 "is not visible in C++"));
2736 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2737 }
2738
7e585d16
ZW
2739 ref = DECL_INITIAL (ref);
2740 TREE_CONSTANT (ref) = 1;
2741 }
6a29edea 2742 else if (current_function_decl != 0
4b1e44be 2743 && !DECL_FILE_SCOPE_P (current_function_decl)
21b634ae
MP
2744 && (VAR_OR_FUNCTION_DECL_P (ref)
2745 || TREE_CODE (ref) == PARM_DECL))
6a29edea
EB
2746 {
2747 tree context = decl_function_context (ref);
2f6e4e97 2748
6a29edea
EB
2749 if (context != 0 && context != current_function_decl)
2750 DECL_NONLOCAL (ref) = 1;
2751 }
71113fcd
GK
2752 /* C99 6.7.4p3: An inline definition of a function with external
2753 linkage ... shall not contain a reference to an identifier with
2754 internal linkage. */
2755 else if (current_function_decl != 0
2756 && DECL_DECLARED_INLINE_P (current_function_decl)
2757 && DECL_EXTERNAL (current_function_decl)
2758 && VAR_OR_FUNCTION_DECL_P (ref)
0ae9bd27 2759 && (!VAR_P (ref) || TREE_STATIC (ref))
1033ffa8
JJ
2760 && ! TREE_PUBLIC (ref)
2761 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2762 record_inline_static (loc, current_function_decl, ref,
2763 csi_internal);
7e585d16
ZW
2764
2765 return ref;
2766}
2767
bc4b653b
JM
2768/* Record details of decls possibly used inside sizeof or typeof. */
2769struct maybe_used_decl
2770{
2771 /* The decl. */
2772 tree decl;
2773 /* The level seen at (in_sizeof + in_typeof). */
2774 int level;
2775 /* The next one at this level or above, or NULL. */
2776 struct maybe_used_decl *next;
2777};
2778
2779static struct maybe_used_decl *maybe_used_decls;
2780
2781/* Record that DECL, an undefined static function reference seen
2782 inside sizeof or typeof, might be used if the operand of sizeof is
2783 a VLA type or the operand of typeof is a variably modified
2784 type. */
2785
4e2fb7de 2786static void
bc4b653b
JM
2787record_maybe_used_decl (tree decl)
2788{
2789 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2790 t->decl = decl;
2791 t->level = in_sizeof + in_typeof;
2792 t->next = maybe_used_decls;
2793 maybe_used_decls = t;
2794}
2795
2796/* Pop the stack of decls possibly used inside sizeof or typeof. If
2797 USED is false, just discard them. If it is true, mark them used
2798 (if no longer inside sizeof or typeof) or move them to the next
2799 level up (if still inside sizeof or typeof). */
2800
2801void
2802pop_maybe_used (bool used)
2803{
2804 struct maybe_used_decl *p = maybe_used_decls;
2805 int cur_level = in_sizeof + in_typeof;
2806 while (p && p->level > cur_level)
2807 {
2808 if (used)
2809 {
2810 if (cur_level == 0)
2811 C_DECL_USED (p->decl) = 1;
2812 else
2813 p->level = cur_level;
2814 }
2815 p = p->next;
2816 }
2817 if (!used || cur_level == 0)
2818 maybe_used_decls = p;
2819}
2820
2821/* Return the result of sizeof applied to EXPR. */
2822
2823struct c_expr
c2255bc4 2824c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2825{
2826 struct c_expr ret;
ad97f4be
JM
2827 if (expr.value == error_mark_node)
2828 {
2829 ret.value = error_mark_node;
2830 ret.original_code = ERROR_MARK;
6866c6e8 2831 ret.original_type = NULL;
ad97f4be
JM
2832 pop_maybe_used (false);
2833 }
2834 else
2835 {
928c19bb 2836 bool expr_const_operands = true;
773ec47f
MP
2837
2838 if (TREE_CODE (expr.value) == PARM_DECL
2839 && C_ARRAY_PARAMETER (expr.value))
2840 {
2841 if (warning_at (loc, OPT_Wsizeof_array_argument,
2842 "%<sizeof%> on array function parameter %qE will "
2843 "return size of %qT", expr.value,
2844 expr.original_type))
2845 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2846 }
928c19bb
JM
2847 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2848 &expr_const_operands);
c2255bc4 2849 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2850 c_last_sizeof_arg = expr.value;
2851 ret.original_code = SIZEOF_EXPR;
6866c6e8 2852 ret.original_type = NULL;
928c19bb 2853 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2854 {
2855 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2856 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2857 folded_expr, ret.value);
2858 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2859 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2860 }
928c19bb 2861 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2862 }
bc4b653b
JM
2863 return ret;
2864}
2865
2866/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2867 name passed to sizeof (rather than the type itself). LOC is the
2868 location of the original expression. */
bc4b653b
JM
2869
2870struct c_expr
c2255bc4 2871c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2872{
2873 tree type;
2874 struct c_expr ret;
928c19bb
JM
2875 tree type_expr = NULL_TREE;
2876 bool type_expr_const = true;
2877 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2878 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2879 c_last_sizeof_arg = type;
2880 ret.original_code = SIZEOF_EXPR;
6866c6e8 2881 ret.original_type = NULL;
24070fcb
JM
2882 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2883 && c_vla_type_p (type))
928c19bb 2884 {
24070fcb
JM
2885 /* If the type is a [*] array, it is a VLA but is represented as
2886 having a size of zero. In such a case we must ensure that
2887 the result of sizeof does not get folded to a constant by
2888 c_fully_fold, because if the size is evaluated the result is
2889 not constant and so constraints on zero or negative size
2890 arrays must not be applied when this sizeof call is inside
2891 another array declarator. */
2892 if (!type_expr)
2893 type_expr = integer_zero_node;
928c19bb
JM
2894 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2895 type_expr, ret.value);
2896 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2897 }
16464cc1
VR
2898 pop_maybe_used (type != error_mark_node
2899 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2900 return ret;
2901}
2902
400fbf9f 2903/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2904 The function call is at LOC.
400fbf9f
JW
2905 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2906 TREE_VALUE of each node is a parameter-expression.
2907 FUNCTION's data type may be a function type or a pointer-to-function. */
2908
2909tree
c2255bc4 2910build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2911{
9771b263 2912 vec<tree, va_gc> *v;
bbbbb16a
ILT
2913 tree ret;
2914
9771b263 2915 vec_alloc (v, list_length (params));
bbbbb16a 2916 for (; params; params = TREE_CHAIN (params))
9771b263 2917 v->quick_push (TREE_VALUE (params));
8edbfaa6 2918 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2919 vec_free (v);
bbbbb16a
ILT
2920 return ret;
2921}
2922
ae52741c
MLI
2923/* Give a note about the location of the declaration of DECL. */
2924
c7b70a3c
MP
2925static void
2926inform_declaration (tree decl)
ae52741c 2927{
c7b70a3c 2928 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
ae52741c
MLI
2929 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2930}
2931
bbbbb16a
ILT
2932/* Build a function call to function FUNCTION with parameters PARAMS.
2933 ORIGTYPES, if not NULL, is a vector of types; each element is
2934 either NULL or the original type of the corresponding element in
2935 PARAMS. The original type may differ from TREE_TYPE of the
2936 parameter for enums. FUNCTION's data type may be a function type
2937 or pointer-to-function. This function changes the elements of
2938 PARAMS. */
2939
2940tree
81e5eca8
MP
2941build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2942 tree function, vec<tree, va_gc> *params,
9771b263 2943 vec<tree, va_gc> *origtypes)
400fbf9f 2944{
b3694847 2945 tree fntype, fundecl = 0;
4977bab6 2946 tree name = NULL_TREE, result;
c96f4f73 2947 tree tem;
94a0dd7b
SL
2948 int nargs;
2949 tree *argarray;
b8698a0f 2950
400fbf9f 2951
fc76e425 2952 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2953 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2954
2955 /* Convert anything with function type to a pointer-to-function. */
2956 if (TREE_CODE (function) == FUNCTION_DECL)
2957 {
2958 name = DECL_NAME (function);
0a35513e
AH
2959
2960 if (flag_tm)
2961 tm_malloc_replacement (function);
a5eadacc 2962 fundecl = function;
86951993
AM
2963 /* Atomic functions have type checking/casting already done. They are
2964 often rewritten and don't match the original parameter list. */
2965 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2966 origtypes = NULL;
36536d79 2967
b72271b9 2968 if (flag_cilkplus
36536d79
BI
2969 && is_cilkplus_reduce_builtin (function))
2970 origtypes = NULL;
400fbf9f 2971 }
f2a71bbc 2972 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2973 function = function_to_pointer_conversion (loc, function);
400fbf9f 2974
6e955430
ZL
2975 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2976 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2977 if (params && !params->is_empty ())
2978 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2979
928c19bb
JM
2980 function = c_fully_fold (function, false, NULL);
2981
400fbf9f
JW
2982 fntype = TREE_TYPE (function);
2983
2984 if (TREE_CODE (fntype) == ERROR_MARK)
2985 return error_mark_node;
2986
2987 if (!(TREE_CODE (fntype) == POINTER_TYPE
2988 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2989 {
ae52741c
MLI
2990 if (!flag_diagnostics_show_caret)
2991 error_at (loc,
2992 "called object %qE is not a function or function pointer",
2993 function);
2994 else if (DECL_P (function))
2995 {
2996 error_at (loc,
2997 "called object %qD is not a function or function pointer",
2998 function);
2999 inform_declaration (function);
3000 }
3001 else
3002 error_at (loc,
3003 "called object is not a function or function pointer");
400fbf9f
JW
3004 return error_mark_node;
3005 }
3006
5ce89b2e
JM
3007 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3008 current_function_returns_abnormally = 1;
3009
400fbf9f
JW
3010 /* fntype now gets the type of function pointed to. */
3011 fntype = TREE_TYPE (fntype);
3012
ab4194da
JM
3013 /* Convert the parameters to the types declared in the
3014 function prototype, or apply default promotions. */
3015
81e5eca8
MP
3016 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3017 origtypes, function, fundecl);
ab4194da
JM
3018 if (nargs < 0)
3019 return error_mark_node;
3020
c96f4f73 3021 /* Check that the function is called through a compatible prototype.
fa337f3a 3022 If it is not, warn. */
1043771b 3023 if (CONVERT_EXPR_P (function)
c96f4f73
EB
3024 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3025 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 3026 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
3027 {
3028 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
3029
3030 /* This situation leads to run-time undefined behavior. We can't,
3031 therefore, simply error unless we can prove that all possible
3032 executions of the program must execute the code. */
fa337f3a 3033 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 3034
fa337f3a
RB
3035 if (VOID_TYPE_P (return_type)
3036 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3037 pedwarn (loc, 0,
3038 "function with qualified void return type called");
3039 }
c96f4f73 3040
9771b263 3041 argarray = vec_safe_address (params);
bbbbb16a 3042
83322951
RG
3043 /* Check that arguments to builtin functions match the expectations. */
3044 if (fundecl
3045 && DECL_BUILT_IN (fundecl)
3046 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3047 && !check_builtin_function_arguments (fundecl, nargs, argarray))
3048 return error_mark_node;
400fbf9f 3049
83322951 3050 /* Check that the arguments to the function are valid. */
dde05067 3051 check_function_arguments (fntype, nargs, argarray);
400fbf9f 3052
928c19bb
JM
3053 if (name != NULL_TREE
3054 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 3055 {
928c19bb 3056 if (require_constant_value)
b8698a0f 3057 result =
db3927fb
AH
3058 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3059 function, nargs, argarray);
928c19bb 3060 else
db3927fb
AH
3061 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3062 function, nargs, argarray);
928c19bb
JM
3063 if (TREE_CODE (result) == NOP_EXPR
3064 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3065 STRIP_TYPE_NOPS (result);
bf730f15
RS
3066 }
3067 else
db3927fb
AH
3068 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3069 function, nargs, argarray);
b0b3afb2 3070
71653180 3071 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
3072 {
3073 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 3074 pedwarn (loc, 0,
3ce62965
JM
3075 "function with qualified void return type called");
3076 return result;
3077 }
1eb8759b 3078 return require_complete_type (result);
400fbf9f 3079}
8edbfaa6
JJ
3080
3081/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3082
3083tree
3084c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3085 tree function, vec<tree, va_gc> *params,
3086 vec<tree, va_gc> *origtypes)
3087{
3088 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3089 STRIP_TYPE_NOPS (function);
3090
3091 /* Convert anything with function type to a pointer-to-function. */
3092 if (TREE_CODE (function) == FUNCTION_DECL)
3093 {
3094 /* Implement type-directed function overloading for builtins.
3095 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3096 handle all the type checking. The result is a complete expression
3097 that implements this function call. */
3098 tree tem = resolve_overloaded_builtin (loc, function, params);
3099 if (tem)
3100 return tem;
3101 }
3102 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3103}
400fbf9f 3104\f
bbbbb16a
ILT
3105/* Convert the argument expressions in the vector VALUES
3106 to the types in the list TYPELIST.
400fbf9f
JW
3107
3108 If TYPELIST is exhausted, or when an element has NULL as its type,
3109 perform the default conversions.
3110
bbbbb16a
ILT
3111 ORIGTYPES is the original types of the expressions in VALUES. This
3112 holds the type of enum values which have been converted to integral
3113 types. It may be NULL.
400fbf9f 3114
03dafa61
JM
3115 FUNCTION is a tree for the called function. It is used only for
3116 error messages, where it is formatted with %qE.
400fbf9f
JW
3117
3118 This is also where warnings about wrong number of args are generated.
3119
81e5eca8
MP
3120 ARG_LOC are locations of function arguments (if any).
3121
94a0dd7b 3122 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3123 than the length of VALUES in some error situations), or -1 on
3124 failure. */
94a0dd7b
SL
3125
3126static int
81e5eca8
MP
3127convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3128 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3129 tree function, tree fundecl)
400fbf9f 3130{
bbbbb16a
ILT
3131 tree typetail, val;
3132 unsigned int parmnum;
06302a02 3133 bool error_args = false;
b5d32c25 3134 const bool type_generic = fundecl
81e5eca8 3135 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3136 bool type_generic_remove_excess_precision = false;
03dafa61 3137 tree selector;
03dafa61 3138
2ac2f164
JM
3139 /* Change pointer to function to the function itself for
3140 diagnostics. */
03dafa61
JM
3141 if (TREE_CODE (function) == ADDR_EXPR
3142 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3143 function = TREE_OPERAND (function, 0);
03dafa61
JM
3144
3145 /* Handle an ObjC selector specially for diagnostics. */
3146 selector = objc_message_selector ();
400fbf9f 3147
8ce94e44
JM
3148 /* For type-generic built-in functions, determine whether excess
3149 precision should be removed (classification) or not
3150 (comparison). */
3151 if (type_generic
3152 && DECL_BUILT_IN (fundecl)
3153 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3154 {
3155 switch (DECL_FUNCTION_CODE (fundecl))
3156 {
3157 case BUILT_IN_ISFINITE:
3158 case BUILT_IN_ISINF:
3159 case BUILT_IN_ISINF_SIGN:
3160 case BUILT_IN_ISNAN:
3161 case BUILT_IN_ISNORMAL:
3162 case BUILT_IN_FPCLASSIFY:
3163 type_generic_remove_excess_precision = true;
3164 break;
3165
3166 default:
3167 type_generic_remove_excess_precision = false;
3168 break;
3169 }
3170 }
b72271b9 3171 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3172 return vec_safe_length (values);
8ce94e44 3173
400fbf9f 3174 /* Scan the given expressions and types, producing individual
bbbbb16a 3175 converted arguments. */
400fbf9f 3176
bbbbb16a 3177 for (typetail = typelist, parmnum = 0;
9771b263 3178 values && values->iterate (parmnum, &val);
bbbbb16a 3179 ++parmnum)
400fbf9f 3180 {
b3694847 3181 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3182 tree valtype = TREE_TYPE (val);
03dafa61
JM
3183 tree rname = function;
3184 int argnum = parmnum + 1;
4d3e6fae 3185 const char *invalid_func_diag;
8ce94e44 3186 bool excess_precision = false;
928c19bb 3187 bool npc;
bbbbb16a 3188 tree parmval;
5c1bc275
MP
3189 /* Some __atomic_* builtins have additional hidden argument at
3190 position 0. */
3191 location_t ploc
3192 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3193 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3194 : input_location;
400fbf9f
JW
3195
3196 if (type == void_type_node)
3197 {
19dc6d01 3198 if (selector)
68fca595 3199 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3200 else
68fca595 3201 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3202 inform_declaration (fundecl);
d38f7dce 3203 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3204 }
3205
03dafa61
JM
3206 if (selector && argnum > 2)
3207 {
3208 rname = selector;
3209 argnum -= 2;
3210 }
3211
928c19bb 3212 npc = null_pointer_constant_p (val);
8ce94e44
JM
3213
3214 /* If there is excess precision and a prototype, convert once to
3215 the required type rather than converting via the semantic
3216 type. Likewise without a prototype a float value represented
3217 as long double should be converted once to double. But for
3218 type-generic classification functions excess precision must
3219 be removed here. */
3220 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3221 && (type || !type_generic || !type_generic_remove_excess_precision))
3222 {
3223 val = TREE_OPERAND (val, 0);
3224 excess_precision = true;
3225 }
928c19bb 3226 val = c_fully_fold (val, false, NULL);
ed248cf7 3227 STRIP_TYPE_NOPS (val);
400fbf9f 3228
400fbf9f
JW
3229 val = require_complete_type (val);
3230
3231 if (type != 0)
3232 {
3233 /* Formal parm type is specified by a function prototype. */
400fbf9f 3234
20913689 3235 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3236 {
5c1bc275
MP
3237 error_at (ploc, "type of formal parameter %d is incomplete",
3238 parmnum + 1);
400fbf9f
JW
3239 parmval = val;
3240 }
3241 else
3242 {
bbbbb16a
ILT
3243 tree origtype;
3244
d45cf215
RS
3245 /* Optionally warn about conversions that
3246 differ from the default conversions. */
05170031 3247 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3248 {
e3a64162 3249 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3250
aae43c5f 3251 if (INTEGRAL_TYPE_P (type)
8ce94e44 3252 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3253 warning_at (ploc, OPT_Wtraditional_conversion,
3254 "passing argument %d of %qE as integer rather "
3255 "than floating due to prototype",
3256 argnum, rname);
03829ad2 3257 if (INTEGRAL_TYPE_P (type)
8ce94e44 3258 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3259 warning_at (ploc, OPT_Wtraditional_conversion,
3260 "passing argument %d of %qE as integer rather "
3261 "than complex due to prototype",
3262 argnum, rname);
aae43c5f 3263 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3264 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3265 warning_at (ploc, OPT_Wtraditional_conversion,
3266 "passing argument %d of %qE as complex rather "
3267 "than floating due to prototype",
3268 argnum, rname);
400fbf9f 3269 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3270 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3271 warning_at (ploc, OPT_Wtraditional_conversion,
3272 "passing argument %d of %qE as floating rather "
3273 "than integer due to prototype",
3274 argnum, rname);
03829ad2 3275 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3276 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3277 warning_at (ploc, OPT_Wtraditional_conversion,
3278 "passing argument %d of %qE as complex rather "
3279 "than integer due to prototype",
3280 argnum, rname);
aae43c5f 3281 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3282 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3283 warning_at (ploc, OPT_Wtraditional_conversion,
3284 "passing argument %d of %qE as floating rather "
3285 "than complex due to prototype",
3286 argnum, rname);
aae43c5f
RK
3287 /* ??? At some point, messages should be written about
3288 conversions between complex types, but that's too messy
3289 to do now. */
d45cf215 3290 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3291 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3292 {
3293 /* Warn if any argument is passed as `float',
047de90b 3294 since without a prototype it would be `double'. */
9a8ce21f
JG
3295 if (formal_prec == TYPE_PRECISION (float_type_node)
3296 && type != dfloat32_type_node)
5c1bc275
MP
3297 warning_at (ploc, 0,
3298 "passing argument %d of %qE as %<float%> "
3299 "rather than %<double%> due to prototype",
3300 argnum, rname);
9a8ce21f
JG
3301
3302 /* Warn if mismatch between argument and prototype
3303 for decimal float types. Warn of conversions with
3304 binary float types and of precision narrowing due to
3305 prototype. */
8ce94e44 3306 else if (type != valtype
9a8ce21f
JG
3307 && (type == dfloat32_type_node
3308 || type == dfloat64_type_node
c22cacf3 3309 || type == dfloat128_type_node
8ce94e44
JM
3310 || valtype == dfloat32_type_node
3311 || valtype == dfloat64_type_node
3312 || valtype == dfloat128_type_node)
c22cacf3 3313 && (formal_prec
8ce94e44 3314 <= TYPE_PRECISION (valtype)
9a8ce21f 3315 || (type == dfloat128_type_node
8ce94e44 3316 && (valtype
c22cacf3 3317 != dfloat64_type_node
8ce94e44 3318 && (valtype
9a8ce21f
JG
3319 != dfloat32_type_node)))
3320 || (type == dfloat64_type_node
8ce94e44 3321 && (valtype
9a8ce21f 3322 != dfloat32_type_node))))
5c1bc275
MP
3323 warning_at (ploc, 0,
3324 "passing argument %d of %qE as %qT "
3325 "rather than %qT due to prototype",
3326 argnum, rname, type, valtype);
9a8ce21f 3327
d45cf215 3328 }
3ed56f8a
KG
3329 /* Detect integer changing in width or signedness.
3330 These warnings are only activated with
05170031
MLI
3331 -Wtraditional-conversion, not with -Wtraditional. */
3332 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3333 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3334 {
d45cf215
RS
3335 tree would_have_been = default_conversion (val);
3336 tree type1 = TREE_TYPE (would_have_been);
3337
754a4d82 3338 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3339 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3340 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3341 /* No warning if function asks for enum
3342 and the actual arg is that enum type. */
3343 ;
3344 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3345 warning_at (ploc, OPT_Wtraditional_conversion,
3346 "passing argument %d of %qE "
3347 "with different width due to prototype",
3348 argnum, rname);
8df83eae 3349 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3350 ;
800cd3b9
RS
3351 /* Don't complain if the formal parameter type
3352 is an enum, because we can't tell now whether
3353 the value was an enum--even the same enum. */
3354 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3355 ;
400fbf9f
JW
3356 else if (TREE_CODE (val) == INTEGER_CST
3357 && int_fits_type_p (val, type))
3358 /* Change in signedness doesn't matter
3359 if a constant value is unaffected. */
3360 ;
ce9895ae
RS
3361 /* If the value is extended from a narrower
3362 unsigned type, it doesn't matter whether we
3363 pass it as signed or unsigned; the value
3364 certainly is the same either way. */
8ce94e44
JM
3365 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3366 && TYPE_UNSIGNED (valtype))
ce9895ae 3367 ;
8df83eae 3368 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3369 warning_at (ploc, OPT_Wtraditional_conversion,
3370 "passing argument %d of %qE "
3371 "as unsigned due to prototype",
3372 argnum, rname);
3ed56f8a 3373 else
5c1bc275
MP
3374 warning_at (ploc, OPT_Wtraditional_conversion,
3375 "passing argument %d of %qE "
3376 "as signed due to prototype",
3377 argnum, rname);
400fbf9f
JW
3378 }
3379 }
3380
8ce94e44
JM
3381 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3382 sake of better warnings from convert_and_check. */
3383 if (excess_precision)
3384 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3385 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3386 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3387 val, origtype, ic_argpass,
3388 npc, fundecl, function,
2ac2f164 3389 parmnum + 1);
2f6e4e97 3390
61f71b34 3391 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3392 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3393 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3394 parmval = default_conversion (parmval);
400fbf9f 3395 }
400fbf9f 3396 }
8ce94e44
JM
3397 else if (TREE_CODE (valtype) == REAL_TYPE
3398 && (TYPE_PRECISION (valtype)
2531a1d9 3399 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3400 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3401 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3402 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3403 {
3404 if (type_generic)
bbbbb16a 3405 parmval = val;
b5d32c25 3406 else
0a0b3574
MM
3407 {
3408 /* Convert `float' to `double'. */
3409 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3410 warning_at (ploc, OPT_Wdouble_promotion,
3411 "implicit conversion from %qT to %qT when passing "
3412 "argument to function",
3413 valtype, double_type_node);
0a0b3574
MM
3414 parmval = convert (double_type_node, val);
3415 }
b5d32c25 3416 }
8ce94e44
JM
3417 else if (excess_precision && !type_generic)
3418 /* A "double" argument with excess precision being passed
3419 without a prototype or in variable arguments. */
bbbbb16a 3420 parmval = convert (valtype, val);
c22cacf3
MS
3421 else if ((invalid_func_diag =
3422 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3423 {
3424 error (invalid_func_diag);
94a0dd7b 3425 return -1;
4d3e6fae 3426 }
1807ffc1
MS
3427 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3428 {
3429 return -1;
3430 }
400fbf9f
JW
3431 else
3432 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3433 parmval = default_conversion (val);
3434
9771b263 3435 (*values)[parmnum] = parmval;
06302a02
JJ
3436 if (parmval == error_mark_node)
3437 error_args = true;
400fbf9f
JW
3438
3439 if (typetail)
3440 typetail = TREE_CHAIN (typetail);
3441 }
3442
9771b263 3443 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3444
400fbf9f 3445 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3446 {
68fca595 3447 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3448 inform_declaration (fundecl);
3449 return -1;
3789b316 3450 }
400fbf9f 3451
06302a02 3452 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3453}
3454\f
43f6dfd3
RS
3455/* This is the entry point used by the parser to build unary operators
3456 in the input. CODE, a tree_code, specifies the unary operator, and
3457 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3458 CONVERT_EXPR for code.
3459
3460 LOC is the location to use for the tree generated.
3461*/
43f6dfd3
RS
3462
3463struct c_expr
c2255bc4 3464parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3465{
3466 struct c_expr result;
3467
100d537d 3468 result.original_code = code;
6866c6e8
ILT
3469 result.original_type = NULL;
3470
1807ffc1
MS
3471 if (reject_gcc_builtin (arg.value))
3472 {
3473 result.value = error_mark_node;
3474 }
3475 else
3476 {
3477 result.value = build_unary_op (loc, code, arg.value, 0);
3478
59c0753d 3479 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3480 overflow_warning (loc, result.value);
1807ffc1 3481 }
59c0753d 3482
ebedc9a3
DM
3483 /* We are typically called when parsing a prefix token at LOC acting on
3484 ARG. Reflect this by updating the source range of the result to
3485 start at LOC and end at the end of ARG. */
3486 set_c_expr_source_range (&result,
3487 loc, arg.get_finish ());
3488
43f6dfd3
RS
3489 return result;
3490}
3491
3492/* This is the entry point used by the parser to build binary operators
3493 in the input. CODE, a tree_code, specifies the binary operator, and
3494 ARG1 and ARG2 are the operands. In addition to constructing the
3495 expression, we check for operands that were written with other binary
ba47d38d
AH
3496 operators in a way that is likely to confuse the user.
3497
3498 LOCATION is the location of the binary operator. */
edc7c4ec 3499
487a92fe 3500struct c_expr
ba47d38d
AH
3501parser_build_binary_op (location_t location, enum tree_code code,
3502 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3503{
487a92fe 3504 struct c_expr result;
400fbf9f 3505
487a92fe
JM
3506 enum tree_code code1 = arg1.original_code;
3507 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3508 tree type1 = (arg1.original_type
3509 ? arg1.original_type
3510 : TREE_TYPE (arg1.value));
3511 tree type2 = (arg2.original_type
3512 ? arg2.original_type
3513 : TREE_TYPE (arg2.value));
400fbf9f 3514
ba47d38d
AH
3515 result.value = build_binary_op (location, code,
3516 arg1.value, arg2.value, 1);
487a92fe 3517 result.original_code = code;
6866c6e8 3518 result.original_type = NULL;
58bf601b 3519
487a92fe
JM
3520 if (TREE_CODE (result.value) == ERROR_MARK)
3521 return result;
400fbf9f 3522
ba47d38d
AH
3523 if (location != UNKNOWN_LOCATION)
3524 protected_set_expr_location (result.value, location);
3525
ebedc9a3
DM
3526 set_c_expr_source_range (&result,
3527 arg1.get_start (),
3528 arg2.get_finish ());
3529
400fbf9f 3530 /* Check for cases such as x+y<<z which users are likely
487a92fe 3531 to misinterpret. */
400fbf9f 3532 if (warn_parentheses)
393e8e8b
MP
3533 warn_about_parentheses (location, code, code1, arg1.value, code2,
3534 arg2.value);
001af587 3535
ca409efd 3536 if (warn_logical_op)
393e8e8b 3537 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3538 code1, arg1.value, code2, arg2.value);
63a08740 3539
05b28fd6 3540 if (warn_tautological_compare)
433068cc
MP
3541 {
3542 tree lhs = arg1.value;
3543 tree rhs = arg2.value;
3544 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3545 {
3546 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3547 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3548 lhs = NULL_TREE;
3549 else
3550 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3551 }
3552 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3553 {
3554 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3555 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3556 rhs = NULL_TREE;
3557 else
3558 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3559 }
3560 if (lhs != NULL_TREE && rhs != NULL_TREE)
3561 warn_tautological_cmp (location, code, lhs, rhs);
3562 }
05b28fd6 3563
742938c9 3564 if (warn_logical_not_paren
7ccb1a11 3565 && TREE_CODE_CLASS (code) == tcc_comparison
47c2554f 3566 && code1 == TRUTH_NOT_EXPR
01177669
JJ
3567 && code2 != TRUTH_NOT_EXPR
3568 /* Avoid warning for !!x == y. */
3569 && (TREE_CODE (arg1.value) != NE_EXPR
3570 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3571 {
3572 /* Avoid warning for !b == y where b has _Bool type. */
3573 tree t = integer_zero_node;
3574 if (TREE_CODE (arg1.value) == EQ_EXPR
3575 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3576 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3577 {
3578 t = TREE_OPERAND (arg1.value, 0);
3579 do
3580 {
3581 if (TREE_TYPE (t) != integer_type_node)
3582 break;
3583 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3584 t = C_MAYBE_CONST_EXPR_EXPR (t);
3585 else if (CONVERT_EXPR_P (t))
3586 t = TREE_OPERAND (t, 0);
3587 else
3588 break;
3589 }
3590 while (1);
3591 }
3592 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3593 warn_logical_not_parentheses (location, code, arg2.value);
3594 }
742938c9 3595
e994a705
RS
3596 /* Warn about comparisons against string literals, with the exception
3597 of testing for equality or inequality of a string literal with NULL. */
3598 if (code == EQ_EXPR || code == NE_EXPR)
3599 {
3600 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3601 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3602 warning_at (location, OPT_Waddress,
3603 "comparison with string literal results in unspecified behavior");
e994a705
RS
3604 }
3605 else if (TREE_CODE_CLASS (code) == tcc_comparison
3606 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3607 warning_at (location, OPT_Waddress,
3608 "comparison with string literal results in unspecified behavior");
e994a705 3609
b8698a0f
L
3610 if (TREE_OVERFLOW_P (result.value)
3611 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3612 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3613 overflow_warning (location, result.value);
400fbf9f 3614
6866c6e8
ILT
3615 /* Warn about comparisons of different enum types. */
3616 if (warn_enum_compare
3617 && TREE_CODE_CLASS (code) == tcc_comparison
3618 && TREE_CODE (type1) == ENUMERAL_TYPE
3619 && TREE_CODE (type2) == ENUMERAL_TYPE
3620 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3621 warning_at (location, OPT_Wenum_compare,
3622 "comparison between %qT and %qT",
3623 type1, type2);
3624
400fbf9f
JW
3625 return result;
3626}
3e4093b6 3627\f
3e4093b6
RS
3628/* Return a tree for the difference of pointers OP0 and OP1.
3629 The resulting tree has type int. */
293c9fdd 3630
3e4093b6 3631static tree
db3927fb 3632pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3633{
3e4093b6 3634 tree restype = ptrdiff_type_node;
36c5e70a 3635 tree result, inttype;
400fbf9f 3636
36c5e70a
BE
3637 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3638 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3639 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3640 tree orig_op1 = op1;
400fbf9f 3641
36c5e70a
BE
3642 /* If the operands point into different address spaces, we need to
3643 explicitly convert them to pointers into the common address space
3644 before we can subtract the numerical address values. */
3645 if (as0 != as1)
3646 {
3647 addr_space_t as_common;
3648 tree common_type;
3649
3650 /* Determine the common superset address space. This is guaranteed
3651 to exist because the caller verified that comp_target_types
3652 returned non-zero. */
3653 if (!addr_space_superset (as0, as1, &as_common))
3654 gcc_unreachable ();
3655
3656 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3657 op0 = convert (common_type, op0);
3658 op1 = convert (common_type, op1);
3659 }
3660
3661 /* Determine integer type to perform computations in. This will usually
3662 be the same as the result type (ptrdiff_t), but may need to be a wider
3663 type if pointers for the address space are wider than ptrdiff_t. */
3664 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3665 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3666 else
3667 inttype = restype;
3668
fcf73884 3669 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3670 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3671 "pointer of type %<void *%> used in subtraction");
3672 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3673 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3674 "pointer to a function used in subtraction");
400fbf9f 3675
3e4093b6
RS
3676 /* First do the subtraction as integers;
3677 then drop through to build the divide operator.
3678 Do not do default conversions on the minus operator
3679 in case restype is a short type. */
400fbf9f 3680
db3927fb 3681 op0 = build_binary_op (loc,
36c5e70a
BE
3682 MINUS_EXPR, convert (inttype, op0),
3683 convert (inttype, op1), 0);
3e4093b6
RS
3684 /* This generates an error if op1 is pointer to incomplete type. */
3685 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3686 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3687
3e4093b6 3688 op1 = c_size_in_bytes (target_type);
400fbf9f 3689
f04dda30
MP
3690 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3691 error_at (loc, "arithmetic on pointer to an empty aggregate");
3692
3e4093b6 3693 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3694 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3695 op0, convert (inttype, op1));
3696
3697 /* Convert to final result type if necessary. */
3698 return convert (restype, result);
3e4093b6
RS
3699}
3700\f
2fe0a208
MP
3701/* Expand atomic compound assignments into an appropriate sequence as
3702 specified by the C11 standard section 6.5.16.2.
3703
267bac10
JM
3704 _Atomic T1 E1
3705 T2 E2
3706 E1 op= E2
3707
3708 This sequence is used for all types for which these operations are
3709 supported.
3710
3711 In addition, built-in versions of the 'fe' prefixed routines may
3712 need to be invoked for floating point (real, complex or vector) when
3713 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3714
3715 T1 newval;
3716 T1 old;
3717 T1 *addr
3718 T2 val
3719 fenv_t fenv
3720
3721 addr = &E1;
3722 val = (E2);
3723 __atomic_load (addr, &old, SEQ_CST);
3724 feholdexcept (&fenv);
3725loop:
3726 newval = old op val;
3727 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3728 SEQ_CST))
3729 goto done;
3730 feclearexcept (FE_ALL_EXCEPT);
3731 goto loop:
3732done:
3733 feupdateenv (&fenv);
3734
2fe0a208
MP
3735 The compiler will issue the __atomic_fetch_* built-in when possible,
3736 otherwise it will generate the generic form of the atomic operations.
3737 This requires temp(s) and has their address taken. The atomic processing
3738 is smart enough to figure out when the size of an object can utilize
3739 a lock-free version, and convert the built-in call to the appropriate
3740 lock-free routine. The optimizers will then dispose of any temps that
3741 are no longer required, and lock-free implementations are utilized as
3742 long as there is target support for the required size.
267bac10
JM
3743
3744 If the operator is NOP_EXPR, then this is a simple assignment, and
3745 an __atomic_store is issued to perform the assignment rather than
2fe0a208 3746 the above loop. */
267bac10
JM
3747
3748/* Build an atomic assignment at LOC, expanding into the proper
3749 sequence to store LHS MODIFYCODE= RHS. Return a value representing
2fe0a208 3750 the result of the operation, unless RETURN_OLD_P, in which case
267bac10
JM
3751 return the old value of LHS (this is only for postincrement and
3752 postdecrement). */
2fe0a208 3753
267bac10
JM
3754static tree
3755build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3756 tree rhs, bool return_old_p)
3757{
3758 tree fndecl, func_call;
3759 vec<tree, va_gc> *params;
3760 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3761 tree old, old_addr;
3762 tree compound_stmt;
3763 tree stmt, goto_stmt;
3764 tree loop_label, loop_decl, done_label, done_decl;
3765
3766 tree lhs_type = TREE_TYPE (lhs);
3767 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3768 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3769 tree rhs_type = TREE_TYPE (rhs);
3770
3771 gcc_assert (TYPE_ATOMIC (lhs_type));
3772
3773 if (return_old_p)
3774 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3775
3776 /* Allocate enough vector items for a compare_exchange. */
3777 vec_alloc (params, 6);
3778
3779 /* Create a compound statement to hold the sequence of statements
3780 with a loop. */
3781 compound_stmt = c_begin_compound_stmt (false);
3782
3783 /* Fold the RHS if it hasn't already been folded. */
3784 if (modifycode != NOP_EXPR)
3785 rhs = c_fully_fold (rhs, false, NULL);
3786
3787 /* Remove the qualifiers for the rest of the expressions and create
3788 the VAL temp variable to hold the RHS. */
3789 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3790 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5c4abbb8 3791 val = create_tmp_var_raw (nonatomic_rhs_type);
267bac10 3792 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3793 TREE_NO_WARNING (val) = 1;
5c4abbb8
MP
3794 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3795 NULL_TREE);
267bac10
JM
3796 SET_EXPR_LOCATION (rhs, loc);
3797 add_stmt (rhs);
3798
3799 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3800 an atomic_store. */
3801 if (modifycode == NOP_EXPR)
3802 {
3803 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3804 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3805 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3806 params->quick_push (lhs_addr);
3807 params->quick_push (rhs);
3808 params->quick_push (seq_cst);
8edbfaa6 3809 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3810 add_stmt (func_call);
3811
3812 /* Finish the compound statement. */
3813 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3814
3815 /* VAL is the value which was stored, return a COMPOUND_STMT of
3816 the statement and that value. */
3817 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3818 }
3819
2fe0a208
MP
3820 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3821 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3822 isn't applicable for such builtins. ??? Do we want to handle enums? */
3823 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3824 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3825 {
3826 built_in_function fncode;
3827 switch (modifycode)
3828 {
3829 case PLUS_EXPR:
3830 case POINTER_PLUS_EXPR:
3831 fncode = (return_old_p
3832 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3833 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3834 break;
3835 case MINUS_EXPR:
3836 fncode = (return_old_p
3837 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3838 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3839 break;
3840 case BIT_AND_EXPR:
3841 fncode = (return_old_p
3842 ? BUILT_IN_ATOMIC_FETCH_AND_N
3843 : BUILT_IN_ATOMIC_AND_FETCH_N);
3844 break;
3845 case BIT_IOR_EXPR:
3846 fncode = (return_old_p
3847 ? BUILT_IN_ATOMIC_FETCH_OR_N
3848 : BUILT_IN_ATOMIC_OR_FETCH_N);
3849 break;
3850 case BIT_XOR_EXPR:
3851 fncode = (return_old_p
3852 ? BUILT_IN_ATOMIC_FETCH_XOR_N
3853 : BUILT_IN_ATOMIC_XOR_FETCH_N);
3854 break;
3855 default:
3856 goto cas_loop;
3857 }
3858
3859 /* We can only use "_1" through "_16" variants of the atomic fetch
3860 built-ins. */
3861 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
3862 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
3863 goto cas_loop;
3864
3865 /* If this is a pointer type, we need to multiply by the size of
3866 the pointer target type. */
3867 if (POINTER_TYPE_P (lhs_type))
3868 {
3869 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
3870 /* ??? This would introduce -Wdiscarded-qualifiers
3871 warning: __atomic_fetch_* expect volatile void *
3872 type as the first argument. (Assignments between
3873 atomic and non-atomic objects are OK.) */
3874 || TYPE_RESTRICT (lhs_type))
3875 goto cas_loop;
3876 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
3877 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
3878 convert (ptrdiff_type_node, rhs),
3879 convert (ptrdiff_type_node, sz));
3880 }
3881
3882 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
3883 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
3884 fndecl = builtin_decl_explicit (fncode);
3885 params->quick_push (lhs_addr);
3886 params->quick_push (rhs);
3887 params->quick_push (seq_cst);
3888 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3889
3890 newval = create_tmp_var_raw (nonatomic_lhs_type);
3891 TREE_ADDRESSABLE (newval) = 1;
3892 TREE_NO_WARNING (newval) = 1;
3893 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
3894 NULL_TREE, NULL_TREE);
3895 SET_EXPR_LOCATION (rhs, loc);
3896 add_stmt (rhs);
3897
3898 /* Finish the compound statement. */
3899 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3900
3901 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
3902 the statement and that value. */
3903 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
3904 }
3905
3906cas_loop:
267bac10 3907 /* Create the variables and labels required for the op= form. */
5c4abbb8 3908 old = create_tmp_var_raw (nonatomic_lhs_type);
267bac10 3909 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3910 TREE_ADDRESSABLE (old) = 1;
3911 TREE_NO_WARNING (old) = 1;
267bac10 3912
5c4abbb8 3913 newval = create_tmp_var_raw (nonatomic_lhs_type);
267bac10
JM
3914 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3915 TREE_ADDRESSABLE (newval) = 1;
2994fb91 3916 TREE_NO_WARNING (newval) = 1;
267bac10
JM
3917
3918 loop_decl = create_artificial_label (loc);
3919 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3920
3921 done_decl = create_artificial_label (loc);
3922 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3923
3924 /* __atomic_load (addr, &old, SEQ_CST). */
3925 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3926 params->quick_push (lhs_addr);
3927 params->quick_push (old_addr);
3928 params->quick_push (seq_cst);
8edbfaa6 3929 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5c4abbb8
MP
3930 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3931 NULL_TREE);
3932 add_stmt (old);
267bac10
JM
3933 params->truncate (0);
3934
3935 /* Create the expressions for floating-point environment
3936 manipulation, if required. */
3937 bool need_fenv = (flag_trapping_math
3938 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3939 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3940 if (need_fenv)
3941 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3942
3943 if (hold_call)
3944 add_stmt (hold_call);
3945
3946 /* loop: */
3947 add_stmt (loop_label);
3948
3949 /* newval = old + val; */
3950 rhs = build_binary_op (loc, modifycode, old, val, 1);
5c4abbb8 3951 rhs = c_fully_fold (rhs, false, NULL);
68fca595
MP
3952 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3953 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3954 NULL_TREE, 0);
3955 if (rhs != error_mark_node)
3956 {
5c4abbb8
MP
3957 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3958 NULL_TREE);
267bac10
JM
3959 SET_EXPR_LOCATION (rhs, loc);
3960 add_stmt (rhs);
3961 }
3962
3963 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3964 goto done; */
3965 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3966 params->quick_push (lhs_addr);
3967 params->quick_push (old_addr);
3968 params->quick_push (newval_addr);
3969 params->quick_push (integer_zero_node);
3970 params->quick_push (seq_cst);
3971 params->quick_push (seq_cst);
8edbfaa6 3972 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3973
3974 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3975 SET_EXPR_LOCATION (goto_stmt, loc);
3976
3977 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3978 SET_EXPR_LOCATION (stmt, loc);
3979 add_stmt (stmt);
5c4abbb8 3980
267bac10
JM
3981 if (clear_call)
3982 add_stmt (clear_call);
3983
3984 /* goto loop; */
3985 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3986 SET_EXPR_LOCATION (goto_stmt, loc);
3987 add_stmt (goto_stmt);
5c4abbb8 3988
267bac10
JM
3989 /* done: */
3990 add_stmt (done_label);
3991
3992 if (update_call)
3993 add_stmt (update_call);
3994
3995 /* Finish the compound statement. */
3996 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3997
3998 /* NEWVAL is the value that was successfully stored, return a
3999 COMPOUND_EXPR of the statement and the appropriate value. */
4000 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4001 return_old_p ? old : newval);
4002}
4003
3e4093b6
RS
4004/* Construct and perhaps optimize a tree representation
4005 for a unary operation. CODE, a tree_code, specifies the operation
4006 and XARG is the operand.
4007 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
4008 the default promotions (such as from short to int).
4009 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
4010 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
4011 arrays to pointers in C99.
4012
4013 LOCATION is the location of the operator. */
400fbf9f 4014
3e4093b6 4015tree
c9f9eb5d
AH
4016build_unary_op (location_t location,
4017 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
4018{
4019 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4020 tree arg = xarg;
4021 tree argtype = 0;
808d6eaa 4022 enum tree_code typecode;
3e4093b6 4023 tree val;
c9f9eb5d 4024 tree ret = error_mark_node;
8ce94e44 4025 tree eptype = NULL_TREE;
3e4093b6 4026 int noconvert = flag;
4de67c26 4027 const char *invalid_op_diag;
928c19bb
JM
4028 bool int_operands;
4029
4030 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
4031 if (int_operands)
4032 arg = remove_c_maybe_const_expr (arg);
400fbf9f 4033
808d6eaa
JM
4034 if (code != ADDR_EXPR)
4035 arg = require_complete_type (arg);
4036
4037 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
4038 if (typecode == ERROR_MARK)
4039 return error_mark_node;
4040 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4041 typecode = INTEGER_TYPE;
6c36d76b 4042
4de67c26
JM
4043 if ((invalid_op_diag
4044 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4045 {
c9f9eb5d 4046 error_at (location, invalid_op_diag);
4de67c26
JM
4047 return error_mark_node;
4048 }
4049
8ce94e44
JM
4050 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4051 {
4052 eptype = TREE_TYPE (arg);
4053 arg = TREE_OPERAND (arg, 0);
4054 }
4055
3e4093b6
RS
4056 switch (code)
4057 {
4058 case CONVERT_EXPR:
4059 /* This is used for unary plus, because a CONVERT_EXPR
4060 is enough to prevent anybody from looking inside for
4061 associativity, but won't generate any code. */
4062 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 4063 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 4064 || typecode == VECTOR_TYPE))
400fbf9f 4065 {
c9f9eb5d 4066 error_at (location, "wrong type argument to unary plus");
3e4093b6 4067 return error_mark_node;
400fbf9f 4068 }
3e4093b6
RS
4069 else if (!noconvert)
4070 arg = default_conversion (arg);
db3927fb 4071 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
4072 break;
4073
3e4093b6
RS
4074 case NEGATE_EXPR:
4075 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 4076 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
4077 || typecode == VECTOR_TYPE))
4078 {
c9f9eb5d 4079 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
4080 return error_mark_node;
4081 }
4082 else if (!noconvert)
4083 arg = default_conversion (arg);
400fbf9f
JW
4084 break;
4085
3e4093b6 4086 case BIT_NOT_EXPR:
462643f0
AP
4087 /* ~ works on integer types and non float vectors. */
4088 if (typecode == INTEGER_TYPE
4089 || (typecode == VECTOR_TYPE
4090 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 4091 {
3e4093b6
RS
4092 if (!noconvert)
4093 arg = default_conversion (arg);
03d5b1f5 4094 }
3e4093b6 4095 else if (typecode == COMPLEX_TYPE)
400fbf9f 4096 {
3e4093b6 4097 code = CONJ_EXPR;
c1771a20 4098 pedwarn (location, OPT_Wpedantic,
fcf73884 4099 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
4100 if (!noconvert)
4101 arg = default_conversion (arg);
4102 }
4103 else
4104 {
c9f9eb5d 4105 error_at (location, "wrong type argument to bit-complement");
3e4093b6 4106 return error_mark_node;
400fbf9f
JW
4107 }
4108 break;
4109
3e4093b6 4110 case ABS_EXPR:
11017cc7 4111 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 4112 {
c9f9eb5d 4113 error_at (location, "wrong type argument to abs");
3e4093b6 4114 return error_mark_node;
400fbf9f 4115 }
3e4093b6
RS
4116 else if (!noconvert)
4117 arg = default_conversion (arg);
400fbf9f
JW
4118 break;
4119
3e4093b6
RS
4120 case CONJ_EXPR:
4121 /* Conjugating a real value is a no-op, but allow it anyway. */
4122 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4123 || typecode == COMPLEX_TYPE))
400fbf9f 4124 {
c9f9eb5d 4125 error_at (location, "wrong type argument to conjugation");
3e4093b6 4126 return error_mark_node;
400fbf9f 4127 }
3e4093b6
RS
4128 else if (!noconvert)
4129 arg = default_conversion (arg);
400fbf9f
JW
4130 break;
4131
3e4093b6 4132 case TRUTH_NOT_EXPR:
ab22c1fa 4133 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 4134 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 4135 && typecode != COMPLEX_TYPE)
400fbf9f 4136 {
c9f9eb5d
AH
4137 error_at (location,
4138 "wrong type argument to unary exclamation mark");
3e4093b6 4139 return error_mark_node;
400fbf9f 4140 }
a27d595d
JM
4141 if (int_operands)
4142 {
4143 arg = c_objc_common_truthvalue_conversion (location, xarg);
4144 arg = remove_c_maybe_const_expr (arg);
4145 }
4146 else
4147 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 4148 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
4149 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4150 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4151 location = EXPR_LOCATION (ret);
c9f9eb5d 4152 goto return_build_unary_op;
3e4093b6 4153
3e4093b6 4154 case REALPART_EXPR:
3e4093b6 4155 case IMAGPART_EXPR:
fb52b50a
NF
4156 ret = build_real_imag_expr (location, code, arg);
4157 if (ret == error_mark_node)
4158 return error_mark_node;
8ce94e44
JM
4159 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4160 eptype = TREE_TYPE (eptype);
c9f9eb5d 4161 goto return_build_unary_op;
3e4093b6
RS
4162
4163 case PREINCREMENT_EXPR:
4164 case POSTINCREMENT_EXPR:
4165 case PREDECREMENT_EXPR:
4166 case POSTDECREMENT_EXPR:
3e4093b6 4167
928c19bb
JM
4168 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4169 {
4170 tree inner = build_unary_op (location, code,
4171 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4172 if (inner == error_mark_node)
4173 return error_mark_node;
4174 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4175 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4176 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4177 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4178 goto return_build_unary_op;
4179 }
4180
925e8657
NP
4181 /* Complain about anything that is not a true lvalue. In
4182 Objective-C, skip this check for property_refs. */
f90e8e2e 4183 if (!objc_is_property_ref (arg)
7bd11157
TT
4184 && !lvalue_or_else (location,
4185 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
4186 || code == POSTINCREMENT_EXPR)
4187 ? lv_increment
4188 : lv_decrement)))
928c19bb
JM
4189 return error_mark_node;
4190
09639a83
ILT
4191 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4192 {
4193 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4194 warning_at (location, OPT_Wc___compat,
4195 "increment of enumeration value is invalid in C++");
4196 else
4197 warning_at (location, OPT_Wc___compat,
4198 "decrement of enumeration value is invalid in C++");
4199 }
4200
928c19bb
JM
4201 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4202 arg = c_fully_fold (arg, false, NULL);
4203
267bac10
JM
4204 bool atomic_op;
4205 atomic_op = really_atomic_lvalue (arg);
4206
3e4093b6
RS
4207 /* Increment or decrement the real part of the value,
4208 and don't change the imaginary part. */
4209 if (typecode == COMPLEX_TYPE)
400fbf9f 4210 {
3e4093b6
RS
4211 tree real, imag;
4212
c1771a20 4213 pedwarn (location, OPT_Wpedantic,
509c9d60 4214 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 4215
267bac10
JM
4216 if (!atomic_op)
4217 {
4218 arg = stabilize_reference (arg);
4219 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4220 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4221 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4222 if (real == error_mark_node || imag == error_mark_node)
4223 return error_mark_node;
4224 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4225 real, imag);
4226 goto return_build_unary_op;
4227 }
400fbf9f 4228 }
3e4093b6
RS
4229
4230 /* Report invalid types. */
4231
ab22c1fa 4232 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4233 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4234 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4235 {
3e4093b6 4236 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4237 error_at (location, "wrong type argument to increment");
c22cacf3 4238 else
c9f9eb5d 4239 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4240
4241 return error_mark_node;
400fbf9f 4242 }
400fbf9f 4243
3e4093b6
RS
4244 {
4245 tree inc;
400fbf9f 4246
3e4093b6
RS
4247 argtype = TREE_TYPE (arg);
4248
4249 /* Compute the increment. */
4250
4251 if (typecode == POINTER_TYPE)
4252 {
a0e24419 4253 /* If pointer target is an incomplete type,
3e4093b6 4254 we just cannot know how to do the arithmetic. */
b70cef5d 4255 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4256 {
4257 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4258 error_at (location,
a0e24419
MP
4259 "increment of pointer to an incomplete type %qT",
4260 TREE_TYPE (argtype));
3e4093b6 4261 else
c9f9eb5d 4262 error_at (location,
a0e24419
MP
4263 "decrement of pointer to an incomplete type %qT",
4264 TREE_TYPE (argtype));
3e4093b6 4265 }
b70cef5d
JJ
4266 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4267 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4268 {
3e4093b6 4269 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4270 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4271 "wrong type argument to increment");
3e4093b6 4272 else
44d90fe1 4273 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4274 "wrong type argument to decrement");
3e4093b6
RS
4275 }
4276
b70cef5d 4277 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4278 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4279 }
b70cef5d 4280 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4281 {
4282 /* For signed fract types, we invert ++ to -- or
4283 -- to ++, and change inc from 1 to -1, because
4284 it is not possible to represent 1 in signed fract constants.
4285 For unsigned fract types, the result always overflows and
4286 we get an undefined (original) or the maximum value. */
4287 if (code == PREINCREMENT_EXPR)
4288 code = PREDECREMENT_EXPR;
4289 else if (code == PREDECREMENT_EXPR)
4290 code = PREINCREMENT_EXPR;
4291 else if (code == POSTINCREMENT_EXPR)
4292 code = POSTDECREMENT_EXPR;
4293 else /* code == POSTDECREMENT_EXPR */
4294 code = POSTINCREMENT_EXPR;
4295
4296 inc = integer_minus_one_node;
4297 inc = convert (argtype, inc);
4298 }
3e4093b6 4299 else
5be014d5 4300 {
241b71bb
TV
4301 inc = VECTOR_TYPE_P (argtype)
4302 ? build_one_cst (argtype)
4303 : integer_one_node;
5be014d5
AP
4304 inc = convert (argtype, inc);
4305 }
3e4093b6 4306
925e8657
NP
4307 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4308 need to ask Objective-C to build the increment or decrement
4309 expression for it. */
4310 if (objc_is_property_ref (arg))
f90e8e2e 4311 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4312 arg, inc);
4313
3e4093b6 4314 /* Report a read-only lvalue. */
f37acdf9 4315 if (TYPE_READONLY (argtype))
953ff289 4316 {
c02065fc 4317 readonly_error (location, arg,
953ff289
DN
4318 ((code == PREINCREMENT_EXPR
4319 || code == POSTINCREMENT_EXPR)
4320 ? lv_increment : lv_decrement));
4321 return error_mark_node;
4322 }
f37acdf9
JM
4323 else if (TREE_READONLY (arg))
4324 readonly_warning (arg,
4325 ((code == PREINCREMENT_EXPR
4326 || code == POSTINCREMENT_EXPR)
4327 ? lv_increment : lv_decrement));
3e4093b6 4328
267bac10
JM
4329 /* If the argument is atomic, use the special code sequences for
4330 atomic compound assignment. */
4331 if (atomic_op)
4332 {
4333 arg = stabilize_reference (arg);
4334 ret = build_atomic_assign (location, arg,
4335 ((code == PREINCREMENT_EXPR
4336 || code == POSTINCREMENT_EXPR)
4337 ? PLUS_EXPR
4338 : MINUS_EXPR),
4339 (FRACT_MODE_P (TYPE_MODE (argtype))
4340 ? inc
4341 : integer_one_node),
4342 (code == POSTINCREMENT_EXPR
4343 || code == POSTDECREMENT_EXPR));
4344 goto return_build_unary_op;
4345 }
4346
3e4093b6
RS
4347 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4348 val = boolean_increment (code, arg);
4349 else
53fb4de3 4350 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4351 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4352 if (TREE_CODE (val) != code)
6de9cd9a 4353 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4354 ret = val;
4355 goto return_build_unary_op;
3e4093b6
RS
4356 }
4357
4358 case ADDR_EXPR:
4359 /* Note that this operation never does default_conversion. */
4360
2b4b7036
JM
4361 /* The operand of unary '&' must be an lvalue (which excludes
4362 expressions of type void), or, in C99, the result of a [] or
4363 unary '*' operator. */
4364 if (VOID_TYPE_P (TREE_TYPE (arg))
4365 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
22d03525 4366 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
2b4b7036
JM
4367 pedwarn (location, 0, "taking address of expression of type %<void%>");
4368
3e4093b6 4369 /* Let &* cancel out to simplify resulting code. */
22d03525 4370 if (INDIRECT_REF_P (arg))
400fbf9f 4371 {
3e4093b6
RS
4372 /* Don't let this be an lvalue. */
4373 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4374 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4375 ret = TREE_OPERAND (arg, 0);
4376 goto return_build_unary_op;
400fbf9f 4377 }
1eb8759b 4378
3e4093b6
RS
4379 /* Anything not already handled and not a true memory reference
4380 or a non-lvalue array is an error. */
ee45a32d
EB
4381 if (typecode != FUNCTION_TYPE && !flag
4382 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4383 return error_mark_node;
b6a10c9f 4384
928c19bb
JM
4385 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4386 folding later. */
4387 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4388 {
4389 tree inner = build_unary_op (location, code,
4390 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4391 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4392 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4393 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4394 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4395 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4396 goto return_build_unary_op;
4397 }
4398
3e4093b6
RS
4399 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4400 argtype = TREE_TYPE (arg);
400fbf9f 4401
3e4093b6 4402 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4403 to which the address will point. This is only needed
f2c1da78 4404 for function types. */
6615c446 4405 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4406 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4407 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4408 {
4409 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4410 int quals = orig_quals;
4411
4412 if (TREE_READONLY (arg))
4413 quals |= TYPE_QUAL_CONST;
4414 if (TREE_THIS_VOLATILE (arg))
4415 quals |= TYPE_QUAL_VOLATILE;
4416
f2c1da78
JM
4417 argtype = c_build_qualified_type (argtype, quals);
4418 }
400fbf9f 4419
ee45a32d
EB
4420 switch (TREE_CODE (arg))
4421 {
4422 case COMPONENT_REF:
4423 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4424 {
4425 error ("cannot take address of bit-field %qD",
4426 TREE_OPERAND (arg, 1));
4427 return error_mark_node;
4428 }
4429
4430 /* ... fall through ... */
4431
4432 case ARRAY_REF:
4433 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4434 {
4435 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4436 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4437 {
4438 error ("cannot take address of scalar with reverse storage "
4439 "order");
4440 return error_mark_node;
4441 }
4442
4443 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4444 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4445 warning (OPT_Wscalar_storage_order, "address of array with "
4446 "reverse scalar storage order requested");
4447 }
4448
4449 default:
4450 break;
4451 }
4452
3e4093b6
RS
4453 if (!c_mark_addressable (arg))
4454 return error_mark_node;
400fbf9f 4455
abb54d14
JM
4456 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4457 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4458
5cc200fc 4459 argtype = build_pointer_type (argtype);
5e55f99d
RH
4460
4461 /* ??? Cope with user tricks that amount to offsetof. Delete this
4462 when we have proper support for integer constant expressions. */
4463 val = get_base_address (arg);
22d03525 4464 if (val && INDIRECT_REF_P (val)
3aa2ddb8
JJ
4465 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4466 {
cf9e9959 4467 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4468 goto return_build_unary_op;
3aa2ddb8 4469 }
5e55f99d 4470
5cc200fc 4471 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4472
c9f9eb5d
AH
4473 ret = val;
4474 goto return_build_unary_op;
400fbf9f 4475
3e4093b6 4476 default:
1344f9a3 4477 gcc_unreachable ();
3e4093b6 4478 }
400fbf9f 4479
3e4093b6
RS
4480 if (argtype == 0)
4481 argtype = TREE_TYPE (arg);
928c19bb
JM
4482 if (TREE_CODE (arg) == INTEGER_CST)
4483 ret = (require_constant_value
db3927fb
AH
4484 ? fold_build1_initializer_loc (location, code, argtype, arg)
4485 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4486 else
4487 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4488 return_build_unary_op:
4489 gcc_assert (ret != error_mark_node);
928c19bb
JM
4490 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4491 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4492 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4493 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4494 ret = note_integer_operands (ret);
8ce94e44
JM
4495 if (eptype)
4496 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4497 protected_set_expr_location (ret, location);
4498 return ret;
3e4093b6 4499}
400fbf9f 4500
3e4093b6
RS
4501/* Return nonzero if REF is an lvalue valid for this language.
4502 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4503 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4504
1e4ae551 4505bool
58f9752a 4506lvalue_p (const_tree ref)
3e4093b6 4507{
58f9752a 4508 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4509
3e4093b6
RS
4510 switch (code)
4511 {
4512 case REALPART_EXPR:
4513 case IMAGPART_EXPR:
4514 case COMPONENT_REF:
4515 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4516
928c19bb
JM
4517 case C_MAYBE_CONST_EXPR:
4518 return lvalue_p (TREE_OPERAND (ref, 1));
4519
3e4093b6
RS
4520 case COMPOUND_LITERAL_EXPR:
4521 case STRING_CST:
4522 return 1;
400fbf9f 4523
3e4093b6
RS
4524 case INDIRECT_REF:
4525 case ARRAY_REF:
36536d79 4526 case ARRAY_NOTATION_REF:
3e4093b6
RS
4527 case VAR_DECL:
4528 case PARM_DECL:
4529 case RESULT_DECL:
4530 case ERROR_MARK:
4531 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4532 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4533
3e4093b6 4534 case BIND_EXPR:
3e4093b6 4535 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4536
3e4093b6
RS
4537 default:
4538 return 0;
4539 }
4540}
400fbf9f 4541\f
f37acdf9
JM
4542/* Give a warning for storing in something that is read-only in GCC
4543 terms but not const in ISO C terms. */
4544
4545static void
4546readonly_warning (tree arg, enum lvalue_use use)
4547{
4548 switch (use)
4549 {
4550 case lv_assign:
4551 warning (0, "assignment of read-only location %qE", arg);
4552 break;
4553 case lv_increment:
4554 warning (0, "increment of read-only location %qE", arg);
4555 break;
4556 case lv_decrement:
4557 warning (0, "decrement of read-only location %qE", arg);
4558 break;
4559 default:
4560 gcc_unreachable ();
4561 }
4562 return;
4563}
4564
37dc0d8d
JM
4565
4566/* Return nonzero if REF is an lvalue valid for this language;
4567 otherwise, print an error message and return zero. USE says
7bd11157
TT
4568 how the lvalue is being used and so selects the error message.
4569 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4570
4571static int
7bd11157 4572lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4573{
4574 int win = lvalue_p (ref);
4575
4576 if (!win)
7bd11157 4577 lvalue_error (loc, use);
37dc0d8d
JM
4578
4579 return win;
4580}
3e4093b6
RS
4581\f
4582/* Mark EXP saying that we need to be able to take the
4583 address of it; it should not be allocated in a register.
4584 Returns true if successful. */
54c93c30 4585
3e4093b6
RS
4586bool
4587c_mark_addressable (tree exp)
400fbf9f 4588{
3e4093b6 4589 tree x = exp;
95602da1 4590
3e4093b6
RS
4591 while (1)
4592 switch (TREE_CODE (x))
4593 {
4594 case COMPONENT_REF:
3e4093b6
RS
4595 case ADDR_EXPR:
4596 case ARRAY_REF:
4597 case REALPART_EXPR:
4598 case IMAGPART_EXPR:
4599 x = TREE_OPERAND (x, 0);
4600 break;
95602da1 4601
3e4093b6
RS
4602 case COMPOUND_LITERAL_EXPR:
4603 case CONSTRUCTOR:
4604 TREE_ADDRESSABLE (x) = 1;
4605 return true;
95602da1 4606
3e4093b6
RS
4607 case VAR_DECL:
4608 case CONST_DECL:
4609 case PARM_DECL:
4610 case RESULT_DECL:
5baeaac0 4611 if (C_DECL_REGISTER (x)
3e4093b6
RS
4612 && DECL_NONLOCAL (x))
4613 {
62f9079a 4614 if (TREE_PUBLIC (x) || is_global_var (x))
3e4093b6 4615 {
0039fa55
AN
4616 error
4617 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4618 return false;
4619 }
509c9d60 4620 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4621 }
5baeaac0 4622 else if (C_DECL_REGISTER (x))
3e4093b6 4623 {
62f9079a 4624 if (TREE_PUBLIC (x) || is_global_var (x))
0039fa55
AN
4625 error ("address of global register variable %qD requested", x);
4626 else
4627 error ("address of register variable %qD requested", x);
4628 return false;
3e4093b6 4629 }
400fbf9f 4630
3e4093b6
RS
4631 /* drops in */
4632 case FUNCTION_DECL:
4633 TREE_ADDRESSABLE (x) = 1;
4634 /* drops out */
4635 default:
4636 return true;
4637 }
4638}
4639\f
2d2e923f
MLI
4640/* Convert EXPR to TYPE, warning about conversion problems with
4641 constants. SEMANTIC_TYPE is the type this conversion would use
4642 without excess precision. If SEMANTIC_TYPE is NULL, this function
4643 is equivalent to convert_and_check. This function is a wrapper that
4644 handles conversions that may be different than
4645 the usual ones because of excess precision. */
4646
4647static tree
68fca595
MP
4648ep_convert_and_check (location_t loc, tree type, tree expr,
4649 tree semantic_type)
2d2e923f
MLI
4650{
4651 if (TREE_TYPE (expr) == type)
4652 return expr;
4653
4654 if (!semantic_type)
68fca595 4655 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4656
4657 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4658 && TREE_TYPE (expr) != semantic_type)
4659 {
4660 /* For integers, we need to check the real conversion, not
4661 the conversion to the excess precision type. */
68fca595 4662 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4663 }
4664 /* Result type is the excess precision type, which should be
4665 large enough, so do not check. */
4666 return convert (type, expr);
4667}
4668
928c19bb
JM
4669/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4670 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4671 if folded to an integer constant then the unselected half may
4672 contain arbitrary operations not normally permitted in constant
c2255bc4 4673 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4674
4675tree
744aa42f 4676build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4677 tree op1, tree op1_original_type, tree op2,
4678 tree op2_original_type)
400fbf9f 4679{
3e4093b6
RS
4680 tree type1;
4681 tree type2;
4682 enum tree_code code1;
4683 enum tree_code code2;
4684 tree result_type = NULL;
2d2e923f 4685 tree semantic_result_type = NULL;
3e4093b6 4686 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4687 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4688 bool ifexp_int_operands;
928c19bb 4689 tree ret;
400fbf9f 4690
4d84fe7c
JM
4691 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4692 if (op1_int_operands)
4693 op1 = remove_c_maybe_const_expr (op1);
4694 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4695 if (op2_int_operands)
4696 op2 = remove_c_maybe_const_expr (op2);
4697 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4698 if (ifexp_int_operands)
4699 ifexp = remove_c_maybe_const_expr (ifexp);
4700
3e4093b6
RS
4701 /* Promote both alternatives. */
4702
4703 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4704 op1 = default_conversion (op1);
4705 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4706 op2 = default_conversion (op2);
4707
4708 if (TREE_CODE (ifexp) == ERROR_MARK
4709 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4710 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4711 return error_mark_node;
400fbf9f 4712
3e4093b6
RS
4713 type1 = TREE_TYPE (op1);
4714 code1 = TREE_CODE (type1);
4715 type2 = TREE_TYPE (op2);
4716 code2 = TREE_CODE (type2);
4717
1807ffc1
MS
4718 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4719 return error_mark_node;
4720
4721 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4722 return error_mark_node;
4723
b1adf557
JM
4724 /* C90 does not permit non-lvalue arrays in conditional expressions.
4725 In C99 they will be pointers by now. */
4726 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4727 {
744aa42f 4728 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4729 return error_mark_node;
4730 }
4731
8ce94e44
JM
4732 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4733 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4734 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4735 || code1 == COMPLEX_TYPE)
4736 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4737 || code2 == COMPLEX_TYPE))
4738 {
2d2e923f 4739 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4740 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4741 {
4742 op1 = TREE_OPERAND (op1, 0);
4743 type1 = TREE_TYPE (op1);
4744 gcc_assert (TREE_CODE (type1) == code1);
4745 }
4746 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4747 {
4748 op2 = TREE_OPERAND (op2, 0);
4749 type2 = TREE_TYPE (op2);
4750 gcc_assert (TREE_CODE (type2) == code2);
4751 }
4752 }
4753
d130ae11
ILT
4754 if (warn_cxx_compat)
4755 {
4756 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4757 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4758
4759 if (TREE_CODE (t1) == ENUMERAL_TYPE
4760 && TREE_CODE (t2) == ENUMERAL_TYPE
4761 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4762 warning_at (colon_loc, OPT_Wc___compat,
4763 ("different enum types in conditional is "
4764 "invalid in C++: %qT vs %qT"),
4765 t1, t2);
4766 }
4767
3e4093b6
RS
4768 /* Quickly detect the usual case where op1 and op2 have the same type
4769 after promotion. */
4770 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4771 {
3e4093b6
RS
4772 if (type1 == type2)
4773 result_type = type1;
4774 else
4775 result_type = TYPE_MAIN_VARIANT (type1);
4776 }
4777 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4778 || code1 == COMPLEX_TYPE)
4779 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4780 || code2 == COMPLEX_TYPE))
3e4093b6 4781 {
ccf7f880 4782 result_type = c_common_type (type1, type2);
0a0b3574
MM
4783 do_warn_double_promotion (result_type, type1, type2,
4784 "implicit conversion from %qT to %qT to "
4785 "match other result of conditional",
4786 colon_loc);
400fbf9f 4787
3e4093b6
RS
4788 /* If -Wsign-compare, warn here if type1 and type2 have
4789 different signedness. We'll promote the signed to unsigned
4790 and later code won't know it used to be different.
4791 Do this check on the original types, so that explicit casts
4792 will be considered, but default promotions won't. */
7d882b83 4793 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4794 {
8df83eae
RK
4795 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4796 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4797
3e4093b6
RS
4798 if (unsigned_op1 ^ unsigned_op2)
4799 {
6ac01510
ILT
4800 bool ovf;
4801
3e4093b6
RS
4802 /* Do not warn if the result type is signed, since the
4803 signed type will only be chosen if it can represent
4804 all the values of the unsigned type. */
3f75a254 4805 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4806 /* OK */;
3e4093b6 4807 else
928c19bb
JM
4808 {
4809 bool op1_maybe_const = true;
4810 bool op2_maybe_const = true;
4811
4812 /* Do not warn if the signed quantity is an
4813 unsuffixed integer literal (or some static
4814 constant expression involving such literals) and
4815 it is non-negative. This warning requires the
4816 operands to be folded for best results, so do
4817 that folding in this case even without
4818 warn_sign_compare to avoid warning options
4819 possibly affecting code generation. */
f5178456
RS
4820 c_inhibit_evaluation_warnings
4821 += (ifexp == truthvalue_false_node);
928c19bb
JM
4822 op1 = c_fully_fold (op1, require_constant_value,
4823 &op1_maybe_const);
f5178456
RS
4824 c_inhibit_evaluation_warnings
4825 -= (ifexp == truthvalue_false_node);
4826
4827 c_inhibit_evaluation_warnings
4828 += (ifexp == truthvalue_true_node);
928c19bb
JM
4829 op2 = c_fully_fold (op2, require_constant_value,
4830 &op2_maybe_const);
f5178456
RS
4831 c_inhibit_evaluation_warnings
4832 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4833
4834 if (warn_sign_compare)
4835 {
4836 if ((unsigned_op2
4837 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4838 || (unsigned_op1
4839 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4840 /* OK */;
4841 else
744aa42f
ILT
4842 warning_at (colon_loc, OPT_Wsign_compare,
4843 ("signed and unsigned type in "
4844 "conditional expression"));
928c19bb
JM
4845 }
4846 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4847 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4848 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4849 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4850 }
3e4093b6
RS
4851 }
4852 }
4853 }
4854 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4855 {
fcf73884 4856 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4857 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4858 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4859 result_type = void_type_node;
4860 }
4861 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4862 {
36c5e70a
BE
4863 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4864 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4865 addr_space_t as_common;
4866
744aa42f 4867 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4868 result_type = common_pointer_type (type1, type2);
6aa3c60d 4869 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4870 result_type = type2;
6aa3c60d 4871 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4872 result_type = type1;
4873 else if (!addr_space_superset (as1, as2, &as_common))
4874 {
4875 error_at (colon_loc, "pointers to disjoint address spaces "
4876 "used in conditional expression");
4877 return error_mark_node;
4878 }
267bac10
JM
4879 else if (VOID_TYPE_P (TREE_TYPE (type1))
4880 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4881 {
768952be
MU
4882 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4883 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4884 & ~TYPE_QUALS (TREE_TYPE (type1))))
4885 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4886 "pointer to array loses qualifier "
4887 "in conditional expression");
4888
fcf73884 4889 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4890 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4891 "ISO C forbids conditional expr between "
bda67431 4892 "%<void *%> and function pointer");
3e4093b6
RS
4893 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4894 TREE_TYPE (type2)));
34a80643 4895 }
267bac10
JM
4896 else if (VOID_TYPE_P (TREE_TYPE (type2))
4897 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4898 {
768952be
MU
4899 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4900 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4901 & ~TYPE_QUALS (TREE_TYPE (type2))))
4902 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4903 "pointer to array loses qualifier "
4904 "in conditional expression");
4905
fcf73884 4906 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4907 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4908 "ISO C forbids conditional expr between "
bda67431 4909 "%<void *%> and function pointer");
3e4093b6
RS
4910 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4911 TREE_TYPE (type1)));
1c2a9b35 4912 }
b581b85b
NP
4913 /* Objective-C pointer comparisons are a bit more lenient. */
4914 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4915 result_type = objc_common_type (type1, type2);
34a80643 4916 else
ab87f8c8 4917 {
36c5e70a
BE
4918 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4919
b581b85b
NP
4920 pedwarn (colon_loc, 0,
4921 "pointer type mismatch in conditional expression");
36c5e70a
BE
4922 result_type = build_pointer_type
4923 (build_qualified_type (void_type_node, qual));
ab87f8c8 4924 }
3e4093b6
RS
4925 }
4926 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4927 {
6aa3c60d 4928 if (!null_pointer_constant_p (orig_op2))
744aa42f 4929 pedwarn (colon_loc, 0,
509c9d60 4930 "pointer/integer type mismatch in conditional expression");
3e4093b6 4931 else
ab87f8c8 4932 {
3e4093b6 4933 op2 = null_pointer_node;
ab87f8c8 4934 }
3e4093b6
RS
4935 result_type = type1;
4936 }
4937 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4938 {
6aa3c60d 4939 if (!null_pointer_constant_p (orig_op1))
744aa42f 4940 pedwarn (colon_loc, 0,
509c9d60 4941 "pointer/integer type mismatch in conditional expression");
3e4093b6 4942 else
ab87f8c8 4943 {
3e4093b6 4944 op1 = null_pointer_node;
ab87f8c8 4945 }
3e4093b6
RS
4946 result_type = type2;
4947 }
1c2a9b35 4948
3e4093b6
RS
4949 if (!result_type)
4950 {
4951 if (flag_cond_mismatch)
4952 result_type = void_type_node;
4953 else
400fbf9f 4954 {
c2255bc4 4955 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4956 return error_mark_node;
400fbf9f 4957 }
3e4093b6 4958 }
400fbf9f 4959
3e4093b6
RS
4960 /* Merge const and volatile flags of the incoming types. */
4961 result_type
4962 = build_type_variant (result_type,
afbd0665
AS
4963 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4964 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4965
68fca595
MP
4966 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4967 semantic_result_type);
4968 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4969 semantic_result_type);
b6a10c9f 4970
928c19bb
JM
4971 if (ifexp_bcp && ifexp == truthvalue_true_node)
4972 {
4973 op2_int_operands = true;
4974 op1 = c_fully_fold (op1, require_constant_value, NULL);
4975 }
4976 if (ifexp_bcp && ifexp == truthvalue_false_node)
4977 {
4978 op1_int_operands = true;
4979 op2 = c_fully_fold (op2, require_constant_value, NULL);
4980 }
4d84fe7c 4981 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4982 && op1_int_operands
4983 && op2_int_operands);
4984 if (int_operands)
4985 {
4986 int_const = ((ifexp == truthvalue_true_node
4987 && TREE_CODE (orig_op1) == INTEGER_CST
4988 && !TREE_OVERFLOW (orig_op1))
4989 || (ifexp == truthvalue_false_node
4990 && TREE_CODE (orig_op2) == INTEGER_CST
4991 && !TREE_OVERFLOW (orig_op2)));
4992 }
9f47c7e5
IE
4993
4994 /* Need to convert condition operand into a vector mask. */
4995 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
4996 {
4997 tree vectype = TREE_TYPE (ifexp);
4998 tree elem_type = TREE_TYPE (vectype);
4999 tree zero = build_int_cst (elem_type, 0);
5000 tree zero_vec = build_vector_from_val (vectype, zero);
5001 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5002 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5003 }
5004
928c19bb 5005 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 5006 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
5007 else
5008 {
01c7ccbb
JM
5009 if (int_operands)
5010 {
f34f1c87
MP
5011 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5012 nested inside of the expression. */
5013 op1 = c_fully_fold (op1, false, NULL);
5014 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 5015 }
928c19bb
JM
5016 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5017 if (int_operands)
5018 ret = note_integer_operands (ret);
5019 }
2d2e923f
MLI
5020 if (semantic_result_type)
5021 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 5022
c2255bc4 5023 protected_set_expr_location (ret, colon_loc);
928c19bb 5024 return ret;
3e4093b6
RS
5025}
5026\f
487a92fe 5027/* Return a compound expression that performs two expressions and
c2255bc4
AH
5028 returns the value of the second of them.
5029
5030 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 5031
3e4093b6 5032tree
c2255bc4 5033build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 5034{
4d84fe7c 5035 bool expr1_int_operands, expr2_int_operands;
8ce94e44 5036 tree eptype = NULL_TREE;
928c19bb
JM
5037 tree ret;
5038
b72271b9 5039 if (flag_cilkplus
939b37da
BI
5040 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5041 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5042 {
5043 error_at (loc,
5044 "spawned function call cannot be part of a comma expression");
5045 return error_mark_node;
5046 }
4d84fe7c
JM
5047 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5048 if (expr1_int_operands)
5049 expr1 = remove_c_maybe_const_expr (expr1);
5050 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5051 if (expr2_int_operands)
5052 expr2 = remove_c_maybe_const_expr (expr2);
5053
8ce94e44
JM
5054 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5055 expr1 = TREE_OPERAND (expr1, 0);
5056 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5057 {
5058 eptype = TREE_TYPE (expr2);
5059 expr2 = TREE_OPERAND (expr2, 0);
5060 }
5061
3f75a254 5062 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
5063 {
5064 /* The left-hand operand of a comma expression is like an expression
c5409249 5065 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 5066 any side-effects, unless it was explicitly cast to (void). */
e14a6540 5067 if (warn_unused_value)
47aecf47 5068 {
e14a6540 5069 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 5070 && CONVERT_EXPR_P (expr1))
47aecf47 5071 ; /* (void) a, b */
e14a6540
JM
5072 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5073 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 5074 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
5075 ; /* (void) a, (void) b, c */
5076 else
b8698a0f 5077 warning_at (loc, OPT_Wunused_value,
c2255bc4 5078 "left-hand operand of comma expression has no effect");
47aecf47 5079 }
3e4093b6 5080 }
789eadcd
MP
5081 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5082 && warn_unused_value)
5083 {
5084 tree r = expr1;
5085 location_t cloc = loc;
5086 while (TREE_CODE (r) == COMPOUND_EXPR)
5087 {
5088 if (EXPR_HAS_LOCATION (r))
5089 cloc = EXPR_LOCATION (r);
5090 r = TREE_OPERAND (r, 1);
5091 }
5092 if (!TREE_SIDE_EFFECTS (r)
5093 && !VOID_TYPE_P (TREE_TYPE (r))
5094 && !CONVERT_EXPR_P (r))
5095 warning_at (cloc, OPT_Wunused_value,
5096 "right-hand operand of comma expression has no effect");
5097 }
400fbf9f 5098
3e4093b6
RS
5099 /* With -Wunused, we should also warn if the left-hand operand does have
5100 side-effects, but computes a value which is not used. For example, in
5101 `foo() + bar(), baz()' the result of the `+' operator is not used,
5102 so we should issue a warning. */
5103 else if (warn_unused_value)
c2255bc4 5104 warn_if_unused_value (expr1, loc);
400fbf9f 5105
e63d6886
AP
5106 if (expr2 == error_mark_node)
5107 return error_mark_node;
5108
928c19bb
JM
5109 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5110
5111 if (flag_isoc99
4d84fe7c
JM
5112 && expr1_int_operands
5113 && expr2_int_operands)
928c19bb
JM
5114 ret = note_integer_operands (ret);
5115
8ce94e44
JM
5116 if (eptype)
5117 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5118
c2255bc4 5119 protected_set_expr_location (ret, loc);
928c19bb 5120 return ret;
3e4093b6 5121}
400fbf9f 5122
67165eb3
ILT
5123/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5124 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
5125 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5126 of the cast. -Wcast-qual appeared on the command line. Named
5127 address space qualifiers are not handled here, because they result
5128 in different warnings. */
67165eb3
ILT
5129
5130static void
2ee3cb35 5131handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
5132{
5133 tree in_type = type;
5134 tree in_otype = otype;
5135 int added = 0;
5136 int discarded = 0;
5137 bool is_const;
5138
5139 /* Check that the qualifiers on IN_TYPE are a superset of the
5140 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5141 nodes is uninteresting and we stop as soon as we hit a
5142 non-POINTER_TYPE node on either type. */
5143 do
5144 {
5145 in_otype = TREE_TYPE (in_otype);
5146 in_type = TREE_TYPE (in_type);
5147
5148 /* GNU C allows cv-qualified function types. 'const' means the
5149 function is very pure, 'volatile' means it can't return. We
5150 need to warn when such qualifiers are added, not when they're
5151 taken away. */
5152 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5153 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
5154 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5155 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 5156 else
36c5e70a
BE
5157 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5158 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
5159 }
5160 while (TREE_CODE (in_type) == POINTER_TYPE
5161 && TREE_CODE (in_otype) == POINTER_TYPE);
5162
5163 if (added)
2ee3cb35
MLI
5164 warning_at (loc, OPT_Wcast_qual,
5165 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
5166
5167 if (discarded)
5168 /* There are qualifiers present in IN_OTYPE that are not present
5169 in IN_TYPE. */
2ee3cb35 5170 warning_at (loc, OPT_Wcast_qual,
7485aeea 5171 "cast discards %qv qualifier from pointer target type",
2ee3cb35 5172 discarded);
67165eb3
ILT
5173
5174 if (added || discarded)
5175 return;
5176
5177 /* A cast from **T to const **T is unsafe, because it can cause a
5178 const value to be changed with no additional warning. We only
5179 issue this warning if T is the same on both sides, and we only
5180 issue the warning if there are the same number of pointers on
5181 both sides, as otherwise the cast is clearly unsafe anyhow. A
5182 cast is unsafe when a qualifier is added at one level and const
5183 is not present at all outer levels.
5184
5185 To issue this warning, we check at each level whether the cast
5186 adds new qualifiers not already seen. We don't need to special
5187 case function types, as they won't have the same
5188 TYPE_MAIN_VARIANT. */
5189
5190 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5191 return;
5192 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5193 return;
5194
5195 in_type = type;
5196 in_otype = otype;
5197 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5198 do
5199 {
5200 in_type = TREE_TYPE (in_type);
5201 in_otype = TREE_TYPE (in_otype);
5202 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5203 && !is_const)
5204 {
2ee3cb35
MLI
5205 warning_at (loc, OPT_Wcast_qual,
5206 "to be safe all intermediate pointers in cast from "
5207 "%qT to %qT must be %<const%> qualified",
5208 otype, type);
67165eb3
ILT
5209 break;
5210 }
5211 if (is_const)
5212 is_const = TYPE_READONLY (in_type);
5213 }
5214 while (TREE_CODE (in_type) == POINTER_TYPE);
5215}
5216
b8698a0f 5217/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 5218 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 5219
3e4093b6 5220tree
c2255bc4 5221build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 5222{
8ce94e44
JM
5223 tree value;
5224
5225 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5226 expr = TREE_OPERAND (expr, 0);
5227
5228 value = expr;
400fbf9f 5229
3e4093b6
RS
5230 if (type == error_mark_node || expr == error_mark_node)
5231 return error_mark_node;
400fbf9f 5232
3e4093b6
RS
5233 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5234 only in <protocol> qualifications. But when constructing cast expressions,
5235 the protocols do matter and must be kept around. */
700686fa
ZL
5236 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5237 return build1 (NOP_EXPR, type, expr);
5238
5239 type = TYPE_MAIN_VARIANT (type);
400fbf9f 5240
3e4093b6
RS
5241 if (TREE_CODE (type) == ARRAY_TYPE)
5242 {
c2255bc4 5243 error_at (loc, "cast specifies array type");
3e4093b6
RS
5244 return error_mark_node;
5245 }
400fbf9f 5246
3e4093b6
RS
5247 if (TREE_CODE (type) == FUNCTION_TYPE)
5248 {
c2255bc4 5249 error_at (loc, "cast specifies function type");
3e4093b6
RS
5250 return error_mark_node;
5251 }
400fbf9f 5252
808d6eaa
JM
5253 if (!VOID_TYPE_P (type))
5254 {
5255 value = require_complete_type (value);
5256 if (value == error_mark_node)
5257 return error_mark_node;
5258 }
5259
3e4093b6
RS
5260 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5261 {
3e636daf 5262 if (RECORD_OR_UNION_TYPE_P (type))
c1771a20 5263 pedwarn (loc, OPT_Wpedantic,
fcf73884 5264 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5265
5266 /* Convert to remove any qualifiers from VALUE's type. */
5267 value = convert (type, value);
3e4093b6
RS
5268 }
5269 else if (TREE_CODE (type) == UNION_TYPE)
5270 {
5271 tree field;
400fbf9f 5272
910ad8de 5273 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5274 if (TREE_TYPE (field) != error_mark_node
5275 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5276 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5277 break;
5278
5279 if (field)
400fbf9f 5280 {
3e4093b6 5281 tree t;
e616f54d 5282 bool maybe_const = true;
3e4093b6 5283
c1771a20 5284 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5285 t = c_fully_fold (value, false, &maybe_const);
5286 t = build_constructor_single (type, field, t);
5287 if (!maybe_const)
5288 t = c_wrap_maybe_const (t, true);
5289 t = digest_init (loc, type, t,
bbbbb16a 5290 NULL_TREE, false, true, 0);
3e4093b6
RS
5291 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5292 return t;
400fbf9f 5293 }
c2255bc4 5294 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5295 return error_mark_node;
5296 }
5297 else
5298 {
5299 tree otype, ovalue;
400fbf9f 5300
3e4093b6 5301 if (type == void_type_node)
c2255bc4
AH
5302 {
5303 tree t = build1 (CONVERT_EXPR, type, value);
5304 SET_EXPR_LOCATION (t, loc);
5305 return t;
5306 }
400fbf9f 5307
3e4093b6 5308 otype = TREE_TYPE (value);
400fbf9f 5309
3e4093b6 5310 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5311 if (warn_cast_qual
5312 && TREE_CODE (type) == POINTER_TYPE
5313 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5314 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5315
36c5e70a
BE
5316 /* Warn about conversions between pointers to disjoint
5317 address spaces. */
5318 if (TREE_CODE (type) == POINTER_TYPE
5319 && TREE_CODE (otype) == POINTER_TYPE
5320 && !null_pointer_constant_p (value))
5321 {
5322 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5323 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5324 addr_space_t as_common;
5325
5326 if (!addr_space_superset (as_to, as_from, &as_common))
5327 {
5328 if (ADDR_SPACE_GENERIC_P (as_from))
5329 warning_at (loc, 0, "cast to %s address space pointer "
5330 "from disjoint generic address space pointer",
5331 c_addr_space_name (as_to));
5332
5333 else if (ADDR_SPACE_GENERIC_P (as_to))
5334 warning_at (loc, 0, "cast to generic address space pointer "
5335 "from disjoint %s address space pointer",
5336 c_addr_space_name (as_from));
5337
5338 else
5339 warning_at (loc, 0, "cast to %s address space pointer "
5340 "from disjoint %s address space pointer",
5341 c_addr_space_name (as_to),
5342 c_addr_space_name (as_from));
5343 }
5344 }
5345
3e4093b6 5346 /* Warn about possible alignment problems. */
3176a0c2 5347 if (STRICT_ALIGNMENT
3e4093b6
RS
5348 && TREE_CODE (type) == POINTER_TYPE
5349 && TREE_CODE (otype) == POINTER_TYPE
5350 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5351 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5352 /* Don't warn about opaque types, where the actual alignment
5353 restriction is unknown. */
3e636daf 5354 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
3e4093b6
RS
5355 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5356 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5357 warning_at (loc, OPT_Wcast_align,
5358 "cast increases required alignment of target type");
e9a25f70 5359
3176a0c2 5360 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5361 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5362 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5363 /* Unlike conversion of integers to pointers, where the
5364 warning is disabled for converting constants because
5365 of cases such as SIG_*, warn about converting constant
5366 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5367 sign extension, and a warning is appropriate. */
c2255bc4
AH
5368 warning_at (loc, OPT_Wpointer_to_int_cast,
5369 "cast from pointer to integer of different size");
400fbf9f 5370
3176a0c2 5371 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5372 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5373 warning_at (loc, OPT_Wbad_function_cast,
5374 "cast from function call of type %qT "
5375 "to non-matching type %qT", otype, type);
400fbf9f 5376
3176a0c2 5377 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5378 && TREE_CODE (otype) == INTEGER_TYPE
5379 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5380 /* Don't warn about converting any constant. */
5381 && !TREE_CONSTANT (value))
c2255bc4
AH
5382 warning_at (loc,
5383 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5384 "of different size");
400fbf9f 5385
79bedddc
SR
5386 if (warn_strict_aliasing <= 2)
5387 strict_aliasing_warning (otype, type, expr);
400fbf9f 5388
3897f229
JM
5389 /* If pedantic, warn for conversions between function and object
5390 pointer types, except for converting a null pointer constant
5391 to function pointer type. */
5392 if (pedantic
5393 && TREE_CODE (type) == POINTER_TYPE
5394 && TREE_CODE (otype) == POINTER_TYPE
5395 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5396 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5397 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5398 "conversion of function pointer to object pointer type");
3897f229
JM
5399
5400 if (pedantic
5401 && TREE_CODE (type) == POINTER_TYPE
5402 && TREE_CODE (otype) == POINTER_TYPE
5403 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5404 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5405 && !null_pointer_constant_p (value))
c1771a20 5406 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5407 "conversion of object pointer to function pointer type");
3897f229 5408
3e4093b6 5409 ovalue = value;
3e4093b6 5410 value = convert (type, value);
400fbf9f 5411
3e4093b6 5412 /* Ignore any integer overflow caused by the cast. */
928c19bb 5413 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5414 {
8bcd6380 5415 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5416 {
8bcd6380
RS
5417 if (!TREE_OVERFLOW (value))
5418 {
5419 /* Avoid clobbering a shared constant. */
5420 value = copy_node (value);
5421 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5422 }
6414bad6 5423 }
8bcd6380 5424 else if (TREE_OVERFLOW (value))
d8e1f97b 5425 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5426 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5427 }
5428 }
400fbf9f 5429
53cd18ec 5430 /* Don't let a cast be an lvalue. */
9482b620 5431 if (lvalue_p (value))
db3927fb 5432 value = non_lvalue_loc (loc, value);
e9a25f70 5433
928c19bb
JM
5434 /* Don't allow the results of casting to floating-point or complex
5435 types be confused with actual constants, or casts involving
5436 integer and pointer types other than direct integer-to-integer
5437 and integer-to-pointer be confused with integer constant
5438 expressions and null pointer constants. */
5439 if (TREE_CODE (value) == REAL_CST
5440 || TREE_CODE (value) == COMPLEX_CST
5441 || (TREE_CODE (value) == INTEGER_CST
5442 && !((TREE_CODE (expr) == INTEGER_CST
5443 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5444 || TREE_CODE (expr) == REAL_CST
5445 || TREE_CODE (expr) == COMPLEX_CST)))
5446 value = build1 (NOP_EXPR, type, value);
5447
21ba0cea 5448 protected_set_expr_location (value, loc);
3e4093b6 5449 return value;
400fbf9f
JW
5450}
5451
c2255bc4
AH
5452/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5453 location of the open paren of the cast, or the position of the cast
5454 expr. */
3e4093b6 5455tree
c2255bc4 5456c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5457{
f8893e47 5458 tree type;
928c19bb
JM
5459 tree type_expr = NULL_TREE;
5460 bool type_expr_const = true;
5461 tree ret;
3e4093b6 5462 int saved_wsp = warn_strict_prototypes;
c5c76735 5463
3e4093b6
RS
5464 /* This avoids warnings about unprototyped casts on
5465 integers. E.g. "#define SIG_DFL (void(*)())0". */
5466 if (TREE_CODE (expr) == INTEGER_CST)
5467 warn_strict_prototypes = 0;
928c19bb 5468 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5469 warn_strict_prototypes = saved_wsp;
c5c76735 5470
1807ffc1
MS
5471 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5472 && reject_gcc_builtin (expr))
5473 return error_mark_node;
5474
c2255bc4 5475 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5476 if (type_expr)
5477 {
9f33203d
JM
5478 bool inner_expr_const = true;
5479 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5480 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5481 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5482 && inner_expr_const);
c2255bc4 5483 SET_EXPR_LOCATION (ret, loc);
928c19bb 5484 }
24b97832 5485
21ba0cea
MP
5486 if (!EXPR_HAS_LOCATION (ret))
5487 protected_set_expr_location (ret, loc);
24b97832 5488
9e5b2115
PB
5489 /* C++ does not permits types to be defined in a cast, but it
5490 allows references to incomplete types. */
5491 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5492 warning_at (loc, OPT_Wc___compat,
5493 "defining a type in a cast is invalid in C++");
5494
928c19bb 5495 return ret;
400fbf9f 5496}
3e4093b6
RS
5497\f
5498/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5499 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5500 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5501 MODIFYCODE is the code for a binary operator that we use
5502 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5503 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5504 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5505 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5506
c2255bc4
AH
5507 LOCATION is the location of the MODIFYCODE operator.
5508 RHS_LOC is the location of the RHS. */
2f6e4e97 5509
3e4093b6 5510tree
32e8bb8e 5511build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5512 enum tree_code modifycode,
c2255bc4 5513 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5514{
3e4093b6
RS
5515 tree result;
5516 tree newrhs;
241f845a 5517 tree rhseval = NULL_TREE;
8ce94e44 5518 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5519 tree lhstype = TREE_TYPE (lhs);
5520 tree olhstype = lhstype;
928c19bb 5521 bool npc;
267bac10 5522 bool is_atomic_op;
e9a25f70 5523
3e4093b6
RS
5524 /* Types that aren't fully specified cannot be used in assignments. */
5525 lhs = require_complete_type (lhs);
e9a25f70 5526
3e4093b6
RS
5527 /* Avoid duplicate error messages from operands that had errors. */
5528 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5529 return error_mark_node;
400fbf9f 5530
4c2ecab0
JM
5531 /* Ensure an error for assigning a non-lvalue array to an array in
5532 C90. */
5533 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5534 {
5535 error_at (location, "assignment to expression with array type");
5536 return error_mark_node;
5537 }
5538
46a88c12 5539 /* For ObjC properties, defer this check. */
7bd11157 5540 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5541 return error_mark_node;
5542
267bac10
JM
5543 is_atomic_op = really_atomic_lvalue (lhs);
5544
8ce94e44
JM
5545 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5546 {
5547 rhs_semantic_type = TREE_TYPE (rhs);
5548 rhs = TREE_OPERAND (rhs, 0);
5549 }
5550
3e4093b6 5551 newrhs = rhs;
400fbf9f 5552
928c19bb
JM
5553 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5554 {
5555 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5556 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5557 rhs_origtype);
928c19bb
JM
5558 if (inner == error_mark_node)
5559 return error_mark_node;
5560 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5561 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5562 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5563 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5564 protected_set_expr_location (result, location);
5565 return result;
5566 }
5567
3e4093b6
RS
5568 /* If a binary op has been requested, combine the old LHS value with the RHS
5569 producing the value we should actually store into the LHS. */
5570
5571 if (modifycode != NOP_EXPR)
400fbf9f 5572 {
928c19bb 5573 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5574 lhs = stabilize_reference (lhs);
bbbbb16a 5575
267bac10
JM
5576 /* Construct the RHS for any non-atomic compound assignemnt. */
5577 if (!is_atomic_op)
5578 {
241f845a
JJ
5579 /* If in LHS op= RHS the RHS has side-effects, ensure they
5580 are preevaluated before the rest of the assignment expression's
5581 side-effects, because RHS could contain e.g. function calls
5582 that modify LHS. */
5583 if (TREE_SIDE_EFFECTS (rhs))
5584 {
5585 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5586 rhseval = newrhs;
5587 }
267bac10 5588 newrhs = build_binary_op (location,
241f845a 5589 modifycode, lhs, newrhs, 1);
267bac10
JM
5590
5591 /* The original type of the right hand side is no longer
5592 meaningful. */
5593 rhs_origtype = NULL_TREE;
5594 }
400fbf9f 5595 }
400fbf9f 5596
668ea4b1
IS
5597 if (c_dialect_objc ())
5598 {
46a88c12
NP
5599 /* Check if we are modifying an Objective-C property reference;
5600 if so, we need to generate setter calls. */
5601 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5602 if (result)
241f845a 5603 goto return_result;
46a88c12
NP
5604
5605 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5606 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5607 return error_mark_node;
5608 }
5609
9bf24266 5610 /* Give an error for storing in something that is 'const'. */
bbbd6700 5611
f37acdf9 5612 if (TYPE_READONLY (lhstype)
3e636daf 5613 || (RECORD_OR_UNION_TYPE_P (lhstype)
3e4093b6 5614 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5615 {
c02065fc 5616 readonly_error (location, lhs, lv_assign);
953ff289
DN
5617 return error_mark_node;
5618 }
f37acdf9
JM
5619 else if (TREE_READONLY (lhs))
5620 readonly_warning (lhs, lv_assign);
bbbd6700 5621
3e4093b6
RS
5622 /* If storing into a structure or union member,
5623 it has probably been given type `int'.
5624 Compute the type that would go with
5625 the actual amount of storage the member occupies. */
bbbd6700 5626
3e4093b6
RS
5627 if (TREE_CODE (lhs) == COMPONENT_REF
5628 && (TREE_CODE (lhstype) == INTEGER_TYPE
5629 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5630 || TREE_CODE (lhstype) == REAL_TYPE
5631 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5632 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5633
3e4093b6
RS
5634 /* If storing in a field that is in actuality a short or narrower than one,
5635 we must store in the field in its actual type. */
5636
5637 if (lhstype != TREE_TYPE (lhs))
5638 {
5639 lhs = copy_node (lhs);
5640 TREE_TYPE (lhs) = lhstype;
400fbf9f 5641 }
400fbf9f 5642
32e8bb8e
ILT
5643 /* Issue -Wc++-compat warnings about an assignment to an enum type
5644 when LHS does not have its original type. This happens for,
5645 e.g., an enum bitfield in a struct. */
5646 if (warn_cxx_compat
5647 && lhs_origtype != NULL_TREE
5648 && lhs_origtype != lhstype
5649 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5650 {
5651 tree checktype = (rhs_origtype != NULL_TREE
5652 ? rhs_origtype
5653 : TREE_TYPE (rhs));
5654 if (checktype != error_mark_node
267bac10
JM
5655 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5656 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5657 warning_at (location, OPT_Wc___compat,
5658 "enum conversion in assignment is invalid in C++");
5659 }
5660
267bac10
JM
5661 /* If the lhs is atomic, remove that qualifier. */
5662 if (is_atomic_op)
5663 {
5664 lhstype = build_qualified_type (lhstype,
5665 (TYPE_QUALS (lhstype)
5666 & ~TYPE_QUAL_ATOMIC));
5667 olhstype = build_qualified_type (olhstype,
5668 (TYPE_QUALS (lhstype)
5669 & ~TYPE_QUAL_ATOMIC));
5670 }
5671
8ce94e44
JM
5672 /* Convert new value to destination type. Fold it first, then
5673 restore any excess precision information, for the sake of
5674 conversion warnings. */
400fbf9f 5675
267bac10
JM
5676 if (!(is_atomic_op && modifycode != NOP_EXPR))
5677 {
5678 npc = null_pointer_constant_p (newrhs);
5679 newrhs = c_fully_fold (newrhs, false, NULL);
5680 if (rhs_semantic_type)
5681 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5682 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5683 rhs_origtype, ic_assign, npc,
5684 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5685 if (TREE_CODE (newrhs) == ERROR_MARK)
5686 return error_mark_node;
5687 }
400fbf9f 5688
6e955430
ZL
5689 /* Emit ObjC write barrier, if necessary. */
5690 if (c_dialect_objc () && flag_objc_gc)
5691 {
5692 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5693 if (result)
c9f9eb5d
AH
5694 {
5695 protected_set_expr_location (result, location);
241f845a 5696 goto return_result;
c9f9eb5d 5697 }
6e955430
ZL
5698 }
5699
ea4b7848 5700 /* Scan operands. */
400fbf9f 5701
267bac10
JM
5702 if (is_atomic_op)
5703 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5704 else
5705 {
5706 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5707 TREE_SIDE_EFFECTS (result) = 1;
5708 protected_set_expr_location (result, location);
5709 }
400fbf9f 5710
3e4093b6
RS
5711 /* If we got the LHS in a different type for storing in,
5712 convert the result back to the nominal type of LHS
5713 so that the value we return always has the same type
5714 as the LHS argument. */
e855c5ce 5715
3e4093b6 5716 if (olhstype == TREE_TYPE (result))
241f845a 5717 goto return_result;
c9f9eb5d 5718
68fca595
MP
5719 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5720 rhs_origtype, ic_assign, false, NULL_TREE,
5721 NULL_TREE, 0);
c9f9eb5d 5722 protected_set_expr_location (result, location);
241f845a
JJ
5723
5724return_result:
5725 if (rhseval)
5726 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5727 return result;
3e4093b6
RS
5728}
5729\f
478a1c5b
ILT
5730/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5731 This is used to implement -fplan9-extensions. */
5732
5733static bool
5734find_anonymous_field_with_type (tree struct_type, tree type)
5735{
5736 tree field;
5737 bool found;
5738
3e636daf 5739 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
478a1c5b
ILT
5740 found = false;
5741 for (field = TYPE_FIELDS (struct_type);
5742 field != NULL_TREE;
5743 field = TREE_CHAIN (field))
5744 {
267bac10
JM
5745 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5746 ? c_build_qualified_type (TREE_TYPE (field),
5747 TYPE_QUAL_ATOMIC)
5748 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5749 if (DECL_NAME (field) == NULL
267bac10 5750 && comptypes (type, fieldtype))
478a1c5b
ILT
5751 {
5752 if (found)
5753 return false;
5754 found = true;
5755 }
5756 else if (DECL_NAME (field) == NULL
3e636daf 5757 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
478a1c5b
ILT
5758 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5759 {
5760 if (found)
5761 return false;
5762 found = true;
5763 }
5764 }
5765 return found;
5766}
5767
5768/* RHS is an expression whose type is pointer to struct. If there is
5769 an anonymous field in RHS with type TYPE, then return a pointer to
5770 that field in RHS. This is used with -fplan9-extensions. This
5771 returns NULL if no conversion could be found. */
5772
5773static tree
5774convert_to_anonymous_field (location_t location, tree type, tree rhs)
5775{
5776 tree rhs_struct_type, lhs_main_type;
5777 tree field, found_field;
5778 bool found_sub_field;
5779 tree ret;
5780
5781 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5782 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
3e636daf 5783 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
478a1c5b
ILT
5784
5785 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5786 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5787 ? c_build_qualified_type (TREE_TYPE (type),
5788 TYPE_QUAL_ATOMIC)
5789 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5790
5791 found_field = NULL_TREE;
5792 found_sub_field = false;
5793 for (field = TYPE_FIELDS (rhs_struct_type);
5794 field != NULL_TREE;
5795 field = TREE_CHAIN (field))
5796 {
5797 if (DECL_NAME (field) != NULL_TREE
3e636daf 5798 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
478a1c5b 5799 continue;
267bac10
JM
5800 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5801 ? c_build_qualified_type (TREE_TYPE (field),
5802 TYPE_QUAL_ATOMIC)
5803 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5804 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5805 {
5806 if (found_field != NULL_TREE)
5807 return NULL_TREE;
5808 found_field = field;
5809 }
5810 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5811 lhs_main_type))
5812 {
5813 if (found_field != NULL_TREE)
5814 return NULL_TREE;
5815 found_field = field;
5816 found_sub_field = true;
5817 }
5818 }
5819
5820 if (found_field == NULL_TREE)
5821 return NULL_TREE;
5822
5823 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5824 build_fold_indirect_ref (rhs), found_field,
5825 NULL_TREE);
5826 ret = build_fold_addr_expr_loc (location, ret);
5827
5828 if (found_sub_field)
5829 {
5830 ret = convert_to_anonymous_field (location, type, ret);
5831 gcc_assert (ret != NULL_TREE);
5832 }
5833
5834 return ret;
5835}
5836
63bc4e87
MP
5837/* Issue an error message for a bad initializer component.
5838 GMSGID identifies the message.
5839 The component name is taken from the spelling stack. */
5840
5841static void
ea58ef42 5842error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5843{
5844 char *ofwhat;
5845
5846 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5847 error_at (loc, gmsgid);
63bc4e87
MP
5848 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5849 if (*ofwhat)
d7ff7ae5 5850 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5851}
5852
5853/* Issue a pedantic warning for a bad initializer component. OPT is
5854 the option OPT_* (from options.h) controlling this warning or 0 if
5855 it is unconditionally given. GMSGID identifies the message. The
5856 component name is taken from the spelling stack. */
5857
5858static void
5859pedwarn_init (location_t location, int opt, const char *gmsgid)
5860{
5861 char *ofwhat;
d7ff7ae5 5862 bool warned;
63bc4e87
MP
5863
5864 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5865 warned = pedwarn (location, opt, gmsgid);
63bc4e87 5866 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5867 if (*ofwhat && warned)
5868 inform (location, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5869}
5870
5871/* Issue a warning for a bad initializer component.
5872
5873 OPT is the OPT_W* value corresponding to the warning option that
5874 controls this warning. GMSGID identifies the message. The
5875 component name is taken from the spelling stack. */
5876
5877static void
5878warning_init (location_t loc, int opt, const char *gmsgid)
5879{
5880 char *ofwhat;
d7ff7ae5 5881 bool warned;
63bc4e87
MP
5882
5883 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5884 warned = warning_at (loc, opt, gmsgid);
63bc4e87 5885 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5886 if (*ofwhat && warned)
5887 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5888}
5889\f
5890/* If TYPE is an array type and EXPR is a parenthesized string
5891 constant, warn if pedantic that EXPR is being used to initialize an
5892 object of type TYPE. */
5893
5894void
d033409e 5895maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
5896{
5897 if (pedantic
5898 && TREE_CODE (type) == ARRAY_TYPE
5899 && TREE_CODE (expr.value) == STRING_CST
5900 && expr.original_code != STRING_CST)
d033409e 5901 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
5902 "array initialized from parenthesized string constant");
5903}
5904
bbbbb16a
ILT
5905/* Convert value RHS to type TYPE as preparation for an assignment to
5906 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5907 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5908 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5909 constant before any folding.
3e4093b6
RS
5910 The real work of conversion is done by `convert'.
5911 The purpose of this function is to generate error messages
5912 for assignments that are not allowed in C.
2ac2f164
JM
5913 ERRTYPE says whether it is argument passing, assignment,
5914 initialization or return.
2f6e4e97 5915
aabef2de
MP
5916 In the following example, '~' denotes where EXPR_LOC and '^' where
5917 LOCATION point to:
5918
5919 f (var); [ic_argpass]
5920 ^ ~~~
5921 x = var; [ic_assign]
5922 ^ ~~~;
5923 int x = var; [ic_init]
5924 ^^^
5925 return x; [ic_return]
5926 ^
5927
2ac2f164 5928 FUNCTION is a tree for the function being called.
3e4093b6 5929 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5930
3e4093b6 5931static tree
81e5eca8 5932convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5933 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5934 bool null_pointer_constant, tree fundecl,
5935 tree function, int parmnum)
3e4093b6
RS
5936{
5937 enum tree_code codel = TREE_CODE (type);
8ce94e44 5938 tree orig_rhs = rhs;
3e4093b6
RS
5939 tree rhstype;
5940 enum tree_code coder;
2ac2f164 5941 tree rname = NULL_TREE;
58393038 5942 bool objc_ok = false;
2ac2f164 5943
1c7485af
MP
5944 /* Use the expansion point location to handle cases such as user's
5945 function returning a wrong-type macro defined in a system header. */
5946 location = expansion_point_location_if_in_system_header (location);
5947
6b4ef5c1 5948 if (errtype == ic_argpass)
2ac2f164
JM
5949 {
5950 tree selector;
5951 /* Change pointer to function to the function itself for
5952 diagnostics. */
5953 if (TREE_CODE (function) == ADDR_EXPR
5954 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5955 function = TREE_OPERAND (function, 0);
5956
5957 /* Handle an ObjC selector specially for diagnostics. */
5958 selector = objc_message_selector ();
5959 rname = function;
5960 if (selector && parmnum > 2)
5961 {
5962 rname = selector;
5963 parmnum -= 2;
5964 }
5965 }
5966
5967 /* This macro is used to emit diagnostics to ensure that all format
5968 strings are complete sentences, visible to gettext and checked at
5969 compile time. */
768952be 5970#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5971 do { \
5972 switch (errtype) \
5973 { \
5974 case ic_argpass: \
5c1bc275 5975 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 5976 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5977 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
5978 "expected %qT but argument is of type %qT", \
5979 type, rhstype); \
5980 break; \
1e053dfe
MLI
5981 case ic_assign: \
5982 pedwarn (LOCATION, OPT, AS); \
5983 break; \
5984 case ic_init: \
6a8f4e12 5985 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5986 break; \
5987 case ic_return: \
d033409e 5988 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5989 break; \
5990 default: \
5991 gcc_unreachable (); \
5992 } \
2ac2f164 5993 } while (0)
cb3ca04e 5994
49706e39
MLI
5995 /* This macro is used to emit diagnostics to ensure that all format
5996 strings are complete sentences, visible to gettext and checked at
768952be 5997 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 5998 extra parameter to enumerate qualifiers. */
768952be 5999#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
6000 do { \
6001 switch (errtype) \
6002 { \
6003 case ic_argpass: \
5c1bc275 6004 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 6005 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 6006 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
6007 "expected %qT but argument is of type %qT", \
6008 type, rhstype); \
6009 break; \
6010 case ic_assign: \
5c1bc275 6011 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
6012 break; \
6013 case ic_init: \
5c1bc275 6014 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
6015 break; \
6016 case ic_return: \
5c1bc275 6017 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
6018 break; \
6019 default: \
6020 gcc_unreachable (); \
6021 } \
6022 } while (0)
6023
768952be
MU
6024 /* This macro is used to emit diagnostics to ensure that all format
6025 strings are complete sentences, visible to gettext and checked at
6026 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6027 warning_at instead of pedwarn. */
6028#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6029 do { \
6030 switch (errtype) \
6031 { \
6032 case ic_argpass: \
6033 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6034 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6035 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6036 "expected %qT but argument is of type %qT", \
6037 type, rhstype); \
6038 break; \
6039 case ic_assign: \
6040 warning_at (LOCATION, OPT, AS, QUALS); \
6041 break; \
6042 case ic_init: \
6043 warning_at (LOCATION, OPT, IN, QUALS); \
6044 break; \
6045 case ic_return: \
6046 warning_at (LOCATION, OPT, RE, QUALS); \
6047 break; \
6048 default: \
6049 gcc_unreachable (); \
6050 } \
6051 } while (0)
6052
8ce94e44
JM
6053 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6054 rhs = TREE_OPERAND (rhs, 0);
6055
3e4093b6
RS
6056 rhstype = TREE_TYPE (rhs);
6057 coder = TREE_CODE (rhstype);
6058
6059 if (coder == ERROR_MARK)
6060 return error_mark_node;
6061
58393038
ZL
6062 if (c_dialect_objc ())
6063 {
6064 int parmno;
6065
6066 switch (errtype)
6067 {
6068 case ic_return:
6069 parmno = 0;
6070 break;
6071
6072 case ic_assign:
6073 parmno = -1;
6074 break;
6075
6076 case ic_init:
6077 parmno = -2;
6078 break;
6079
6080 default:
6081 parmno = parmnum;
6082 break;
6083 }
6084
6085 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6086 }
6087
bbbbb16a
ILT
6088 if (warn_cxx_compat)
6089 {
6090 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6091 if (checktype != error_mark_node
6092 && TREE_CODE (type) == ENUMERAL_TYPE
6093 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6094 {
768952be
MU
6095 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6096 G_("enum conversion when passing argument "
6097 "%d of %qE is invalid in C++"),
6098 G_("enum conversion in assignment is "
6099 "invalid in C++"),
6100 G_("enum conversion in initialization is "
6101 "invalid in C++"),
6102 G_("enum conversion in return is "
6103 "invalid in C++"));
bbbbb16a
ILT
6104 }
6105 }
6106
3e4093b6 6107 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 6108 return rhs;
3e4093b6
RS
6109
6110 if (coder == VOID_TYPE)
400fbf9f 6111 {
6dcc04b0
JM
6112 /* Except for passing an argument to an unprototyped function,
6113 this is a constraint violation. When passing an argument to
6114 an unprototyped function, it is compile-time undefined;
6115 making it a constraint in that case was rejected in
6116 DR#252. */
c2255bc4 6117 error_at (location, "void value not ignored as it ought to be");
3e4093b6 6118 return error_mark_node;
400fbf9f 6119 }
808d6eaa
JM
6120 rhs = require_complete_type (rhs);
6121 if (rhs == error_mark_node)
6122 return error_mark_node;
1807ffc1
MS
6123
6124 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6125 return error_mark_node;
6126
cd192ccc
MS
6127 /* A non-reference type can convert to a reference. This handles
6128 va_start, va_copy and possibly port built-ins. */
6129 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 6130 {
3e4093b6 6131 if (!lvalue_p (rhs))
400fbf9f 6132 {
c2255bc4 6133 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 6134 return error_mark_node;
400fbf9f 6135 }
3e4093b6
RS
6136 if (!c_mark_addressable (rhs))
6137 return error_mark_node;
6138 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 6139 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6140
81e5eca8 6141 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
6142 build_pointer_type (TREE_TYPE (type)),
6143 rhs, origtype, errtype,
6144 null_pointer_constant, fundecl, function,
6145 parmnum);
cd192ccc
MS
6146 if (rhs == error_mark_node)
6147 return error_mark_node;
3e4093b6
RS
6148
6149 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 6150 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6151 return rhs;
400fbf9f 6152 }
3e4093b6 6153 /* Some types can interconvert without explicit casts. */
3274deff 6154 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 6155 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
6156 return convert (type, rhs);
6157 /* Arithmetic types all interconvert, and enum is treated like int. */
6158 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 6159 || codel == FIXED_POINT_TYPE
3e4093b6
RS
6160 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6161 || codel == BOOLEAN_TYPE)
6162 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 6163 || coder == FIXED_POINT_TYPE
3e4093b6
RS
6164 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6165 || coder == BOOLEAN_TYPE))
928c19bb
JM
6166 {
6167 tree ret;
6168 bool save = in_late_binary_op;
e5341100
JJ
6169 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6170 || (coder == REAL_TYPE
6171 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6172 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
928c19bb 6173 in_late_binary_op = true;
81e5eca8
MP
6174 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6175 ? expr_loc : location, type, orig_rhs);
e5341100 6176 in_late_binary_op = save;
928c19bb
JM
6177 return ret;
6178 }
400fbf9f 6179
79077aea
JJ
6180 /* Aggregates in different TUs might need conversion. */
6181 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6182 && codel == coder
6183 && comptypes (type, rhstype))
81e5eca8
MP
6184 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6185 ? expr_loc : location, type, rhs);
79077aea 6186
ebf0bf7f 6187 /* Conversion to a transparent union or record from its member types.
3e4093b6 6188 This applies only to function arguments. */
ebf0bf7f
JJ
6189 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6190 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 6191 && errtype == ic_argpass)
400fbf9f 6192 {
0257e383 6193 tree memb, marginal_memb = NULL_TREE;
3e4093b6 6194
910ad8de 6195 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 6196 {
0257e383 6197 tree memb_type = TREE_TYPE (memb);
400fbf9f 6198
3e4093b6 6199 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 6200 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 6201 break;
e58cd767 6202
3e4093b6
RS
6203 if (TREE_CODE (memb_type) != POINTER_TYPE)
6204 continue;
2f6e4e97 6205
3e4093b6
RS
6206 if (coder == POINTER_TYPE)
6207 {
6208 tree ttl = TREE_TYPE (memb_type);
6209 tree ttr = TREE_TYPE (rhstype);
400fbf9f 6210
3e4093b6
RS
6211 /* Any non-function converts to a [const][volatile] void *
6212 and vice versa; otherwise, targets must be the same.
6213 Meanwhile, the lhs target must have all the qualifiers of
6214 the rhs. */
267bac10
JM
6215 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6216 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6217 || comp_target_types (location, memb_type, rhstype))
3e4093b6 6218 {
267bac10
JM
6219 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6220 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 6221 /* If this type won't generate any warnings, use it. */
267bac10 6222 if (lquals == rquals
3e4093b6
RS
6223 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6224 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
6225 ? ((lquals | rquals) == rquals)
6226 : ((lquals | rquals) == lquals)))
3e4093b6 6227 break;
400fbf9f 6228
3e4093b6 6229 /* Keep looking for a better type, but remember this one. */
0257e383
RH
6230 if (!marginal_memb)
6231 marginal_memb = memb;
3e4093b6
RS
6232 }
6233 }
82bde854 6234
3e4093b6 6235 /* Can convert integer zero to any pointer type. */
928c19bb 6236 if (null_pointer_constant)
3e4093b6
RS
6237 {
6238 rhs = null_pointer_node;
6239 break;
6240 }
6241 }
400fbf9f 6242
0257e383 6243 if (memb || marginal_memb)
3e4093b6 6244 {
0257e383 6245 if (!memb)
3e4093b6
RS
6246 {
6247 /* We have only a marginally acceptable member type;
6248 it needs a warning. */
0257e383 6249 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 6250 tree ttr = TREE_TYPE (rhstype);
714a0864 6251
3e4093b6
RS
6252 /* Const and volatile mean something different for function
6253 types, so the usual warnings are not appropriate. */
6254 if (TREE_CODE (ttr) == FUNCTION_TYPE
6255 && TREE_CODE (ttl) == FUNCTION_TYPE)
6256 {
6257 /* Because const and volatile on functions are
6258 restrictions that say the function will not do
6259 certain things, it is okay to use a const or volatile
6260 function where an ordinary one is wanted, but not
6261 vice-versa. */
36c5e70a
BE
6262 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6263 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6264 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6265 OPT_Wdiscarded_qualifiers,
6266 G_("passing argument %d of %qE "
6267 "makes %q#v qualified function "
6268 "pointer from unqualified"),
6269 G_("assignment makes %q#v qualified "
6270 "function pointer from "
6271 "unqualified"),
6272 G_("initialization makes %q#v qualified "
6273 "function pointer from "
6274 "unqualified"),
6275 G_("return makes %q#v qualified function "
6276 "pointer from unqualified"),
6277 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6278 }
36c5e70a
BE
6279 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6280 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6281 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6282 OPT_Wdiscarded_qualifiers,
6283 G_("passing argument %d of %qE discards "
6284 "%qv qualifier from pointer target type"),
6285 G_("assignment discards %qv qualifier "
6286 "from pointer target type"),
6287 G_("initialization discards %qv qualifier "
6288 "from pointer target type"),
6289 G_("return discards %qv qualifier from "
6290 "pointer target type"),
6291 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6292
6293 memb = marginal_memb;
3e4093b6 6294 }
400fbf9f 6295
fcf73884 6296 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6297 pedwarn (location, OPT_Wpedantic,
fcf73884 6298 "ISO C prohibits argument conversion to union type");
0e7c47fa 6299
db3927fb 6300 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6301 return build_constructor_single (type, memb, rhs);
3e4093b6 6302 }
0e7c47fa
RK
6303 }
6304
3e4093b6
RS
6305 /* Conversions among pointers */
6306 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6307 && (coder == codel))
400fbf9f 6308 {
3e4093b6
RS
6309 tree ttl = TREE_TYPE (type);
6310 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6311 tree mvl = ttl;
6312 tree mvr = ttr;
3e4093b6 6313 bool is_opaque_pointer;
264fa2db 6314 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6315 addr_space_t asl;
6316 addr_space_t asr;
400fbf9f 6317
46df2823 6318 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6319 mvl = (TYPE_ATOMIC (mvl)
6320 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6321 TYPE_QUAL_ATOMIC)
6322 : TYPE_MAIN_VARIANT (mvl));
46df2823 6323 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6324 mvr = (TYPE_ATOMIC (mvr)
6325 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6326 TYPE_QUAL_ATOMIC)
6327 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6328 /* Opaque pointers are treated like void pointers. */
f83c7f63 6329 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6330
478a1c5b
ILT
6331 /* The Plan 9 compiler permits a pointer to a struct to be
6332 automatically converted into a pointer to an anonymous field
6333 within the struct. */
6334 if (flag_plan9_extensions
3e636daf
MP
6335 && RECORD_OR_UNION_TYPE_P (mvl)
6336 && RECORD_OR_UNION_TYPE_P (mvr)
478a1c5b
ILT
6337 && mvl != mvr)
6338 {
6339 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6340 if (new_rhs != NULL_TREE)
6341 {
6342 rhs = new_rhs;
6343 rhstype = TREE_TYPE (rhs);
6344 coder = TREE_CODE (rhstype);
6345 ttr = TREE_TYPE (rhstype);
6346 mvr = TYPE_MAIN_VARIANT (ttr);
6347 }
6348 }
6349
b7e20b53 6350 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6351 for the purpose of reducing the number of false positives, we
6352 tolerate the special case of
b7e20b53 6353
c22cacf3 6354 int *p = NULL;
b7e20b53 6355
c22cacf3 6356 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6357 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6358 warning_at (errtype == ic_argpass ? expr_loc : location,
6359 OPT_Wc___compat,
6360 "request for implicit conversion "
c2255bc4 6361 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6362
36c5e70a
BE
6363 /* See if the pointers point to incompatible address spaces. */
6364 asl = TYPE_ADDR_SPACE (ttl);
6365 asr = TYPE_ADDR_SPACE (ttr);
6366 if (!null_pointer_constant_p (rhs)
6367 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6368 {
6369 switch (errtype)
6370 {
6371 case ic_argpass:
8ffcdea8 6372 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6373 "non-enclosed address space", parmnum, rname);
6374 break;
6375 case ic_assign:
6376 error_at (location, "assignment from pointer to "
6377 "non-enclosed address space");
6378 break;
6379 case ic_init:
6380 error_at (location, "initialization from pointer to "
6381 "non-enclosed address space");
6382 break;
6383 case ic_return:
6384 error_at (location, "return from pointer to "
6385 "non-enclosed address space");
6386 break;
6387 default:
6388 gcc_unreachable ();
6389 }
6390 return error_mark_node;
6391 }
6392
7876a414
KG
6393 /* Check if the right-hand side has a format attribute but the
6394 left-hand side doesn't. */
90137d8f 6395 if (warn_suggest_attribute_format
104f8784 6396 && check_missing_format_attribute (type, rhstype))
c22cacf3 6397 {
104f8784
KG
6398 switch (errtype)
6399 {
6400 case ic_argpass:
8ffcdea8 6401 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6402 "argument %d of %qE might be "
6403 "a candidate for a format attribute",
6404 parmnum, rname);
104f8784
KG
6405 break;
6406 case ic_assign:
90137d8f 6407 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6408 "assignment left-hand side might be "
6409 "a candidate for a format attribute");
104f8784
KG
6410 break;
6411 case ic_init:
90137d8f 6412 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6413 "initialization left-hand side might be "
6414 "a candidate for a format attribute");
104f8784
KG
6415 break;
6416 case ic_return:
90137d8f 6417 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6418 "return type might be "
6419 "a candidate for a format attribute");
104f8784
KG
6420 break;
6421 default:
6422 gcc_unreachable ();
6423 }
7876a414 6424 }
c22cacf3 6425
3e4093b6
RS
6426 /* Any non-function converts to a [const][volatile] void *
6427 and vice versa; otherwise, targets must be the same.
6428 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6429 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6430 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6431 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6432 || is_opaque_pointer
f8a93a2e
JJ
6433 || ((c_common_unsigned_type (mvl)
6434 == c_common_unsigned_type (mvr))
267bac10
JM
6435 && (c_common_signed_type (mvl)
6436 == c_common_signed_type (mvr))
6437 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6438 {
768952be
MU
6439 /* Warn about loss of qualifers from pointers to arrays with
6440 qualifiers on the element type. */
6441 if (TREE_CODE (ttr) == ARRAY_TYPE)
6442 {
6443 ttr = strip_array_types (ttr);
6444 ttl = strip_array_types (ttl);
6445
6446 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6447 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6448 WARNING_FOR_QUALIFIERS (location, expr_loc,
6449 OPT_Wdiscarded_array_qualifiers,
6450 G_("passing argument %d of %qE discards "
6451 "%qv qualifier from pointer target type"),
6452 G_("assignment discards %qv qualifier "
6453 "from pointer target type"),
6454 G_("initialization discards %qv qualifier "
6455 "from pointer target type"),
6456 G_("return discards %qv qualifier from "
6457 "pointer target type"),
6458 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6459 }
6460 else if (pedantic
3e4093b6
RS
6461 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6462 ||
6463 (VOID_TYPE_P (ttr)
928c19bb 6464 && !null_pointer_constant
3e4093b6 6465 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6466 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6467 G_("ISO C forbids passing argument %d of "
6468 "%qE between function pointer "
6469 "and %<void *%>"),
6470 G_("ISO C forbids assignment between "
6471 "function pointer and %<void *%>"),
6472 G_("ISO C forbids initialization between "
6473 "function pointer and %<void *%>"),
6474 G_("ISO C forbids return between function "
6475 "pointer and %<void *%>"));
3e4093b6
RS
6476 /* Const and volatile mean something different for function types,
6477 so the usual warnings are not appropriate. */
6478 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6479 && TREE_CODE (ttl) != FUNCTION_TYPE)
6480 {
768952be
MU
6481 /* Don't warn about loss of qualifier for conversions from
6482 qualified void* to pointers to arrays with corresponding
6483 qualifier on the element type. */
6484 if (!pedantic)
6485 ttl = strip_array_types (ttl);
6486
267bac10
JM
6487 /* Assignments between atomic and non-atomic objects are OK. */
6488 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6489 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6490 {
768952be
MU
6491 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6492 OPT_Wdiscarded_qualifiers,
6493 G_("passing argument %d of %qE discards "
6494 "%qv qualifier from pointer target type"),
6495 G_("assignment discards %qv qualifier "
6496 "from pointer target type"),
6497 G_("initialization discards %qv qualifier "
6498 "from pointer target type"),
6499 G_("return discards %qv qualifier from "
6500 "pointer target type"),
6501 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6502 }
3e4093b6
RS
6503 /* If this is not a case of ignoring a mismatch in signedness,
6504 no warning. */
6505 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6506 || target_cmp)
3e4093b6
RS
6507 ;
6508 /* If there is a mismatch, do warn. */
f2fd3821 6509 else if (warn_pointer_sign)
768952be
MU
6510 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6511 G_("pointer targets in passing argument "
6512 "%d of %qE differ in signedness"),
6513 G_("pointer targets in assignment "
6514 "differ in signedness"),
6515 G_("pointer targets in initialization "
6516 "differ in signedness"),
6517 G_("pointer targets in return differ "
6518 "in signedness"));
3e4093b6
RS
6519 }
6520 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6521 && TREE_CODE (ttr) == FUNCTION_TYPE)
6522 {
6523 /* Because const and volatile on functions are restrictions
6524 that say the function will not do certain things,
6525 it is okay to use a const or volatile function
6526 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6527 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6528 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6529 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6530 OPT_Wdiscarded_qualifiers,
6531 G_("passing argument %d of %qE makes "
6532 "%q#v qualified function pointer "
6533 "from unqualified"),
6534 G_("assignment makes %q#v qualified function "
6535 "pointer from unqualified"),
6536 G_("initialization makes %q#v qualified "
6537 "function pointer from unqualified"),
6538 G_("return makes %q#v qualified function "
6539 "pointer from unqualified"),
6540 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6541 }
6542 }
6543 else
58393038
ZL
6544 /* Avoid warning about the volatile ObjC EH puts on decls. */
6545 if (!objc_ok)
768952be
MU
6546 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6547 OPT_Wincompatible_pointer_types,
6548 G_("passing argument %d of %qE from "
6549 "incompatible pointer type"),
6550 G_("assignment from incompatible pointer type"),
6551 G_("initialization from incompatible "
6552 "pointer type"),
6553 G_("return from incompatible pointer type"));
58393038 6554
3e4093b6
RS
6555 return convert (type, rhs);
6556 }
b494fd98
EB
6557 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6558 {
6dcc04b0
JM
6559 /* ??? This should not be an error when inlining calls to
6560 unprototyped functions. */
c2255bc4 6561 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6562 return error_mark_node;
6563 }
3e4093b6 6564 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6565 {
3e4093b6
RS
6566 /* An explicit constant 0 can convert to a pointer,
6567 or one that results from arithmetic, even including
6568 a cast to integer type. */
928c19bb 6569 if (!null_pointer_constant)
768952be
MU
6570 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6571 OPT_Wint_conversion,
6572 G_("passing argument %d of %qE makes "
6573 "pointer from integer without a cast"),
6574 G_("assignment makes pointer from integer "
6575 "without a cast"),
6576 G_("initialization makes pointer from "
6577 "integer without a cast"),
6578 G_("return makes pointer from integer "
6579 "without a cast"));
b3006337
EB
6580
6581 return convert (type, rhs);
400fbf9f 6582 }
3e4093b6 6583 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6584 {
768952be
MU
6585 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6586 OPT_Wint_conversion,
6587 G_("passing argument %d of %qE makes integer "
6588 "from pointer without a cast"),
6589 G_("assignment makes integer from pointer "
6590 "without a cast"),
6591 G_("initialization makes integer from pointer "
6592 "without a cast"),
6593 G_("return makes integer from pointer "
6594 "without a cast"));
3e4093b6
RS
6595 return convert (type, rhs);
6596 }
6597 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6598 {
6599 tree ret;
6600 bool save = in_late_binary_op;
6601 in_late_binary_op = true;
6602 ret = convert (type, rhs);
6603 in_late_binary_op = save;
6604 return ret;
6605 }
400fbf9f 6606
2ac2f164 6607 switch (errtype)
3e4093b6 6608 {
2ac2f164 6609 case ic_argpass:
8ffcdea8
MP
6610 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6611 rname);
a7da8b42 6612 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6613 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6614 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6615 break;
6616 case ic_assign:
c2255bc4
AH
6617 error_at (location, "incompatible types when assigning to type %qT from "
6618 "type %qT", type, rhstype);
2ac2f164
JM
6619 break;
6620 case ic_init:
c2255bc4 6621 error_at (location,
8ffcdea8 6622 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6623 type, rhstype);
2ac2f164
JM
6624 break;
6625 case ic_return:
c2255bc4 6626 error_at (location,
8ffcdea8 6627 "incompatible types when returning type %qT but %qT was "
c2255bc4 6628 "expected", rhstype, type);
2ac2f164
JM
6629 break;
6630 default:
6631 gcc_unreachable ();
400fbf9f 6632 }
53b01f59 6633
3e4093b6
RS
6634 return error_mark_node;
6635}
3e4093b6
RS
6636\f
6637/* If VALUE is a compound expr all of whose expressions are constant, then
6638 return its value. Otherwise, return error_mark_node.
15b732b2 6639
3e4093b6
RS
6640 This is for handling COMPOUND_EXPRs as initializer elements
6641 which is allowed with a warning when -pedantic is specified. */
15b732b2 6642
3e4093b6
RS
6643static tree
6644valid_compound_expr_initializer (tree value, tree endtype)
6645{
6646 if (TREE_CODE (value) == COMPOUND_EXPR)
6647 {
6648 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6649 == error_mark_node)
6650 return error_mark_node;
6651 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6652 endtype);
6653 }
116df786 6654 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6655 return error_mark_node;
6656 else
6657 return value;
15b732b2 6658}
400fbf9f 6659\f
3e4093b6
RS
6660/* Perform appropriate conversions on the initial value of a variable,
6661 store it in the declaration DECL,
6662 and print any error messages that are appropriate.
bbbbb16a 6663 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6664 If the init is invalid, store an ERROR_MARK.
6665
6666 INIT_LOC is the location of the initial value. */
400fbf9f 6667
3e4093b6 6668void
c2255bc4 6669store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6670{
3e4093b6 6671 tree value, type;
928c19bb 6672 bool npc = false;
400fbf9f 6673
3e4093b6 6674 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6675
3e4093b6
RS
6676 type = TREE_TYPE (decl);
6677 if (TREE_CODE (type) == ERROR_MARK)
6678 return;
400fbf9f 6679
3e4093b6 6680 /* Digest the specified initializer into an expression. */
400fbf9f 6681
928c19bb
JM
6682 if (init)
6683 npc = null_pointer_constant_p (init);
c2255bc4
AH
6684 value = digest_init (init_loc, type, init, origtype, npc,
6685 true, TREE_STATIC (decl));
400fbf9f 6686
3e4093b6 6687 /* Store the expression if valid; else report error. */
400fbf9f 6688
8400e75e 6689 if (!in_system_header_at (input_location)
3f75a254 6690 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6691 warning (OPT_Wtraditional, "traditional C rejects automatic "
6692 "aggregate initialization");
2f6e4e97 6693
dea63e49
JJ
6694 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6695 DECL_INITIAL (decl) = value;
400fbf9f 6696
3e4093b6
RS
6697 /* ANSI wants warnings about out-of-range constant initializers. */
6698 STRIP_TYPE_NOPS (value);
b8698a0f 6699 if (TREE_STATIC (decl))
c2658540 6700 constant_expression_warning (value);
400fbf9f 6701
3e4093b6
RS
6702 /* Check if we need to set array size from compound literal size. */
6703 if (TREE_CODE (type) == ARRAY_TYPE
6704 && TYPE_DOMAIN (type) == 0
6705 && value != error_mark_node)
400fbf9f 6706 {
3e4093b6
RS
6707 tree inside_init = init;
6708
ed248cf7 6709 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6710 inside_init = fold (inside_init);
6711
6712 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6713 {
8d9f82d5 6714 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6715
8d9f82d5 6716 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6717 {
6718 /* For int foo[] = (int [3]){1}; we need to set array size
6719 now since later on array initializer will be just the
6720 brace enclosed list of the compound literal. */
e30ecc5d 6721 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6722 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6723 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6724 layout_type (type);
8d9f82d5 6725 layout_decl (cldecl, 0);
e30ecc5d
JJ
6726 TREE_TYPE (decl)
6727 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6728 }
6729 }
400fbf9f 6730 }
3e4093b6
RS
6731}
6732\f
6733/* Methods for storing and printing names for error messages. */
400fbf9f 6734
3e4093b6
RS
6735/* Implement a spelling stack that allows components of a name to be pushed
6736 and popped. Each element on the stack is this structure. */
400fbf9f 6737
3e4093b6
RS
6738struct spelling
6739{
6740 int kind;
6741 union
400fbf9f 6742 {
a0f0ab9f 6743 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6744 const char *s;
6745 } u;
6746};
2f6e4e97 6747
3e4093b6
RS
6748#define SPELLING_STRING 1
6749#define SPELLING_MEMBER 2
6750#define SPELLING_BOUNDS 3
400fbf9f 6751
3e4093b6
RS
6752static struct spelling *spelling; /* Next stack element (unused). */
6753static struct spelling *spelling_base; /* Spelling stack base. */
6754static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6755
3e4093b6
RS
6756/* Macros to save and restore the spelling stack around push_... functions.
6757 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6758
3e4093b6
RS
6759#define SPELLING_DEPTH() (spelling - spelling_base)
6760#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6761
3e4093b6
RS
6762/* Push an element on the spelling stack with type KIND and assign VALUE
6763 to MEMBER. */
400fbf9f 6764
3e4093b6
RS
6765#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6766{ \
6767 int depth = SPELLING_DEPTH (); \
6768 \
6769 if (depth >= spelling_size) \
6770 { \
6771 spelling_size += 10; \
cca8ead2
BI
6772 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6773 spelling_size); \
3e4093b6
RS
6774 RESTORE_SPELLING_DEPTH (depth); \
6775 } \
6776 \
6777 spelling->kind = (KIND); \
6778 spelling->MEMBER = (VALUE); \
6779 spelling++; \
6780}
400fbf9f 6781
3e4093b6 6782/* Push STRING on the stack. Printed literally. */
400fbf9f 6783
3e4093b6
RS
6784static void
6785push_string (const char *string)
6786{
6787 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6788}
400fbf9f 6789
3e4093b6 6790/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6791
3e4093b6
RS
6792static void
6793push_member_name (tree decl)
6794{
6795 const char *const string
88388a52
JM
6796 = (DECL_NAME (decl)
6797 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6798 : _("<anonymous>"));
3e4093b6
RS
6799 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6800}
400fbf9f 6801
3e4093b6 6802/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6803
3e4093b6 6804static void
a0f0ab9f 6805push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6806{
6807 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6808}
bb58bec5 6809
3e4093b6 6810/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6811
3e4093b6
RS
6812static int
6813spelling_length (void)
6814{
6815 int size = 0;
6816 struct spelling *p;
400fbf9f 6817
3e4093b6
RS
6818 for (p = spelling_base; p < spelling; p++)
6819 {
6820 if (p->kind == SPELLING_BOUNDS)
6821 size += 25;
6822 else
6823 size += strlen (p->u.s) + 1;
6824 }
6825
6826 return size;
400fbf9f 6827}
400fbf9f 6828
3e4093b6 6829/* Print the spelling to BUFFER and return it. */
400fbf9f 6830
3e4093b6
RS
6831static char *
6832print_spelling (char *buffer)
400fbf9f 6833{
3e4093b6
RS
6834 char *d = buffer;
6835 struct spelling *p;
400fbf9f 6836
3e4093b6
RS
6837 for (p = spelling_base; p < spelling; p++)
6838 if (p->kind == SPELLING_BOUNDS)
6839 {
a0f0ab9f 6840 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6841 d += strlen (d);
6842 }
6843 else
6844 {
6845 const char *s;
6846 if (p->kind == SPELLING_MEMBER)
6847 *d++ = '.';
6848 for (s = p->u.s; (*d = *s++); d++)
6849 ;
6850 }
6851 *d++ = '\0';
6852 return buffer;
6853}
400fbf9f 6854
3e4093b6
RS
6855/* Digest the parser output INIT as an initializer for type TYPE.
6856 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6857
bbbbb16a
ILT
6858 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6859
928c19bb
JM
6860 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6861
916c5919
JM
6862 If INIT is a string constant, STRICT_STRING is true if it is
6863 unparenthesized or we should not warn here for it being parenthesized.
6864 For other types of INIT, STRICT_STRING is not used.
6865
c2255bc4
AH
6866 INIT_LOC is the location of the INIT.
6867
3e4093b6
RS
6868 REQUIRE_CONSTANT requests an error if non-constant initializers or
6869 elements are seen. */
7e842ef8 6870
3e4093b6 6871static tree
c2255bc4
AH
6872digest_init (location_t init_loc, tree type, tree init, tree origtype,
6873 bool null_pointer_constant, bool strict_string,
6874 int require_constant)
3e4093b6
RS
6875{
6876 enum tree_code code = TREE_CODE (type);
6877 tree inside_init = init;
8ce94e44 6878 tree semantic_type = NULL_TREE;
928c19bb 6879 bool maybe_const = true;
7e842ef8 6880
3e4093b6 6881 if (type == error_mark_node
f01da1a5 6882 || !init
7a0ca710 6883 || error_operand_p (init))
3e4093b6 6884 return error_mark_node;
7e842ef8 6885
ed248cf7 6886 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6887
8ce94e44
JM
6888 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6889 {
6890 semantic_type = TREE_TYPE (inside_init);
6891 inside_init = TREE_OPERAND (inside_init, 0);
6892 }
928c19bb
JM
6893 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6894 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6895
3e4093b6
RS
6896 /* Initialization of an array of chars from a string constant
6897 optionally enclosed in braces. */
7e842ef8 6898
197463ae
JM
6899 if (code == ARRAY_TYPE && inside_init
6900 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6901 {
267bac10
JM
6902 tree typ1
6903 = (TYPE_ATOMIC (TREE_TYPE (type))
6904 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6905 TYPE_QUAL_ATOMIC)
6906 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6907 /* Note that an array could be both an array of character type
6908 and an array of wchar_t if wchar_t is signed char or unsigned
6909 char. */
6910 bool char_array = (typ1 == char_type_node
6911 || typ1 == signed_char_type_node
6912 || typ1 == unsigned_char_type_node);
6913 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6914 bool char16_array = !!comptypes (typ1, char16_type_node);
6915 bool char32_array = !!comptypes (typ1, char32_type_node);
6916
6917 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6918 {
916c5919 6919 struct c_expr expr;
c466b2cd 6920 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6921 expr.value = inside_init;
6922 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6923 expr.original_type = NULL;
d033409e 6924 maybe_warn_string_init (init_loc, type, expr);
916c5919 6925
a45e580b 6926 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6927 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6928 "initialization of a flexible array member");
6929
3e4093b6 6930 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6931 TYPE_MAIN_VARIANT (type)))
3e4093b6 6932 return inside_init;
7e842ef8 6933
c466b2cd 6934 if (char_array)
3e4093b6 6935 {
c466b2cd
KVH
6936 if (typ2 != char_type_node)
6937 {
ea58ef42
MP
6938 error_init (init_loc, "char-array initialized from wide "
6939 "string");
c466b2cd
KVH
6940 return error_mark_node;
6941 }
3e4093b6 6942 }
c466b2cd 6943 else
3e4093b6 6944 {
c466b2cd
KVH
6945 if (typ2 == char_type_node)
6946 {
ea58ef42
MP
6947 error_init (init_loc, "wide character array initialized "
6948 "from non-wide string");
c466b2cd
KVH
6949 return error_mark_node;
6950 }
6951 else if (!comptypes(typ1, typ2))
6952 {
ea58ef42
MP
6953 error_init (init_loc, "wide character array initialized "
6954 "from incompatible wide string");
c466b2cd
KVH
6955 return error_mark_node;
6956 }
7e842ef8 6957 }
2f6e4e97 6958
3e4093b6
RS
6959 TREE_TYPE (inside_init) = type;
6960 if (TYPE_DOMAIN (type) != 0
6961 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6962 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6963 {
6964 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6965
c466b2cd 6966 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6967 because it's ok to ignore the terminating null char
6968 that is counted in the length of the constant. */
5eb4df45
ILT
6969 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6970 (len
6971 - (TYPE_PRECISION (typ1)
6972 / BITS_PER_UNIT))))
6973 pedwarn_init (init_loc, 0,
6974 ("initializer-string for array of chars "
6975 "is too long"));
6976 else if (warn_cxx_compat
6977 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6978 warning_at (init_loc, OPT_Wc___compat,
6979 ("initializer-string for array chars "
6980 "is too long for C++"));
6981 }
7e842ef8 6982
3e4093b6 6983 return inside_init;
7e842ef8 6984 }
197463ae
JM
6985 else if (INTEGRAL_TYPE_P (typ1))
6986 {
ea58ef42 6987 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
6988 "from string constant");
6989 return error_mark_node;
6990 }
7e842ef8
PE
6991 }
6992
3e4093b6
RS
6993 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6994 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6995 below and handle as a constructor. */
e89be13b 6996 if (code == VECTOR_TYPE
31521951 6997 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
00c8e9f6 6998 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6999 && TREE_CONSTANT (inside_init))
7000 {
7001 if (TREE_CODE (inside_init) == VECTOR_CST
7002 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7003 TYPE_MAIN_VARIANT (type)))
7004 return inside_init;
7005
7006 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7007 {
4038c495
GB
7008 unsigned HOST_WIDE_INT ix;
7009 tree value;
7010 bool constant_p = true;
e89be13b
JJ
7011
7012 /* Iterate through elements and check if all constructor
7013 elements are *_CSTs. */
4038c495
GB
7014 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7015 if (!CONSTANT_CLASS_P (value))
7016 {
7017 constant_p = false;
7018 break;
7019 }
e89be13b 7020
4038c495
GB
7021 if (constant_p)
7022 return build_vector_from_ctor (type,
7023 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
7024 }
7025 }
6035d635 7026
ca085fd7
MLI
7027 if (warn_sequence_point)
7028 verify_sequence_points (inside_init);
7029
3e4093b6
RS
7030 /* Any type can be initialized
7031 from an expression of the same type, optionally with braces. */
400fbf9f 7032
3e4093b6
RS
7033 if (inside_init && TREE_TYPE (inside_init) != 0
7034 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 7035 TYPE_MAIN_VARIANT (type))
3e4093b6 7036 || (code == ARRAY_TYPE
132da1a5 7037 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7038 || (code == VECTOR_TYPE
132da1a5 7039 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7040 || (code == POINTER_TYPE
3897f229 7041 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 7042 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 7043 TREE_TYPE (type)))))
3e4093b6
RS
7044 {
7045 if (code == POINTER_TYPE)
b494fd98 7046 {
b494fd98
EB
7047 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7048 {
f2a71bbc
JM
7049 if (TREE_CODE (inside_init) == STRING_CST
7050 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
7051 inside_init = array_to_pointer_conversion
7052 (init_loc, inside_init);
f2a71bbc
JM
7053 else
7054 {
ea58ef42 7055 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
7056 return error_mark_node;
7057 }
b494fd98 7058 }
f2a71bbc 7059 }
b494fd98 7060
bae39a73
NS
7061 if (code == VECTOR_TYPE)
7062 /* Although the types are compatible, we may require a
7063 conversion. */
7064 inside_init = convert (type, inside_init);
3e4093b6 7065
ca58211b 7066 if (require_constant
3e4093b6 7067 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 7068 {
3e4093b6
RS
7069 /* As an extension, allow initializing objects with static storage
7070 duration with compound literals (which are then treated just as
ca58211b
PB
7071 the brace enclosed list they contain). Also allow this for
7072 vectors, as we can only assign them with compound literals. */
7278465e
MP
7073 if (flag_isoc99 && code != VECTOR_TYPE)
7074 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7075 "is not constant");
3e4093b6
RS
7076 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7077 inside_init = DECL_INITIAL (decl);
400fbf9f 7078 }
3e4093b6
RS
7079
7080 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7081 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 7082 {
ea58ef42
MP
7083 error_init (init_loc, "array initialized from non-constant array "
7084 "expression");
3e4093b6 7085 return error_mark_node;
400fbf9f 7086 }
400fbf9f 7087
c1771a20 7088 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
7089 -pedantic-errors is specified. In the later case, we always want
7090 an error. In the former case, we simply want a warning. */
7091 if (require_constant && pedantic
7092 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7093 {
7094 inside_init
7095 = valid_compound_expr_initializer (inside_init,
7096 TREE_TYPE (inside_init));
7097 if (inside_init == error_mark_node)
ea58ef42 7098 error_init (init_loc, "initializer element is not constant");
2f6e4e97 7099 else
c1771a20 7100 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 7101 "initializer element is not constant");
3e4093b6
RS
7102 if (flag_pedantic_errors)
7103 inside_init = error_mark_node;
7104 }
7105 else if (require_constant
116df786
RH
7106 && !initializer_constant_valid_p (inside_init,
7107 TREE_TYPE (inside_init)))
3e4093b6 7108 {
ea58ef42 7109 error_init (init_loc, "initializer element is not constant");
3e4093b6 7110 inside_init = error_mark_node;
8b40563c 7111 }
928c19bb 7112 else if (require_constant && !maybe_const)
3aa3c9fc 7113 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7114 "initializer element is not a constant expression");
f735a153 7115
90137d8f 7116 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 7117 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
7118 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7119 type, inside_init, origtype,
bbbbb16a 7120 ic_init, null_pointer_constant,
928c19bb 7121 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
7122 return inside_init;
7123 }
f735a153 7124
3e4093b6 7125 /* Handle scalar types, including conversions. */
400fbf9f 7126
ab22c1fa
CF
7127 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7128 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7129 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 7130 {
f2a71bbc
JM
7131 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7132 && (TREE_CODE (init) == STRING_CST
7133 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 7134 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
7135 if (semantic_type)
7136 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7137 inside_init);
3e4093b6 7138 inside_init
68fca595
MP
7139 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7140 inside_init, origtype, ic_init,
7141 null_pointer_constant, NULL_TREE, NULL_TREE,
7142 0);
2f6e4e97 7143
3274deff
JW
7144 /* Check to see if we have already given an error message. */
7145 if (inside_init == error_mark_node)
7146 ;
3f75a254 7147 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 7148 {
ea58ef42 7149 error_init (init_loc, "initializer element is not constant");
3e4093b6 7150 inside_init = error_mark_node;
400fbf9f 7151 }
3e4093b6 7152 else if (require_constant
116df786
RH
7153 && !initializer_constant_valid_p (inside_init,
7154 TREE_TYPE (inside_init)))
400fbf9f 7155 {
ea58ef42
MP
7156 error_init (init_loc, "initializer element is not computable at "
7157 "load time");
3e4093b6 7158 inside_init = error_mark_node;
400fbf9f 7159 }
928c19bb 7160 else if (require_constant && !maybe_const)
451b5e48 7161 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7162 "initializer element is not a constant expression");
3e4093b6
RS
7163
7164 return inside_init;
400fbf9f 7165 }
d9fc6069 7166
3e4093b6 7167 /* Come here only for records and arrays. */
d9fc6069 7168
3e4093b6 7169 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 7170 {
ea58ef42 7171 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 7172 return error_mark_node;
d9fc6069 7173 }
3e4093b6 7174
ea58ef42 7175 error_init (init_loc, "invalid initializer");
3e4093b6 7176 return error_mark_node;
d9fc6069 7177}
400fbf9f 7178\f
3e4093b6 7179/* Handle initializers that use braces. */
400fbf9f 7180
3e4093b6
RS
7181/* Type of object we are accumulating a constructor for.
7182 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7183static tree constructor_type;
400fbf9f 7184
3e4093b6
RS
7185/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7186 left to fill. */
7187static tree constructor_fields;
400fbf9f 7188
3e4093b6
RS
7189/* For an ARRAY_TYPE, this is the specified index
7190 at which to store the next element we get. */
7191static tree constructor_index;
400fbf9f 7192
3e4093b6
RS
7193/* For an ARRAY_TYPE, this is the maximum index. */
7194static tree constructor_max_index;
400fbf9f 7195
3e4093b6
RS
7196/* For a RECORD_TYPE, this is the first field not yet written out. */
7197static tree constructor_unfilled_fields;
400fbf9f 7198
3e4093b6
RS
7199/* For an ARRAY_TYPE, this is the index of the first element
7200 not yet written out. */
7201static tree constructor_unfilled_index;
895ea614 7202
3e4093b6
RS
7203/* In a RECORD_TYPE, the byte index of the next consecutive field.
7204 This is so we can generate gaps between fields, when appropriate. */
7205static tree constructor_bit_index;
10d5caec 7206
3e4093b6
RS
7207/* If we are saving up the elements rather than allocating them,
7208 this is the list of elements so far (in reverse order,
7209 most recent first). */
9771b263 7210static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 7211
3e4093b6
RS
7212/* 1 if constructor should be incrementally stored into a constructor chain,
7213 0 if all the elements should be kept in AVL tree. */
7214static int constructor_incremental;
ad47f1e5 7215
3e4093b6
RS
7216/* 1 if so far this constructor's elements are all compile-time constants. */
7217static int constructor_constant;
ad47f1e5 7218
3e4093b6
RS
7219/* 1 if so far this constructor's elements are all valid address constants. */
7220static int constructor_simple;
ad47f1e5 7221
928c19bb
JM
7222/* 1 if this constructor has an element that cannot be part of a
7223 constant expression. */
7224static int constructor_nonconst;
7225
3e4093b6
RS
7226/* 1 if this constructor is erroneous so far. */
7227static int constructor_erroneous;
d45cf215 7228
9bac5cbb
G
7229/* 1 if this constructor is the universal zero initializer { 0 }. */
7230static int constructor_zeroinit;
7231
3e4093b6
RS
7232/* Structure for managing pending initializer elements, organized as an
7233 AVL tree. */
d45cf215 7234
3e4093b6 7235struct init_node
d45cf215 7236{
3e4093b6
RS
7237 struct init_node *left, *right;
7238 struct init_node *parent;
7239 int balance;
7240 tree purpose;
7241 tree value;
bbbbb16a 7242 tree origtype;
d45cf215
RS
7243};
7244
3e4093b6
RS
7245/* Tree of pending elements at this constructor level.
7246 These are elements encountered out of order
7247 which belong at places we haven't reached yet in actually
7248 writing the output.
7249 Will never hold tree nodes across GC runs. */
7250static struct init_node *constructor_pending_elts;
d45cf215 7251
3e4093b6
RS
7252/* The SPELLING_DEPTH of this constructor. */
7253static int constructor_depth;
d45cf215 7254
3e4093b6
RS
7255/* DECL node for which an initializer is being read.
7256 0 means we are reading a constructor expression
7257 such as (struct foo) {...}. */
7258static tree constructor_decl;
d45cf215 7259
3e4093b6
RS
7260/* Nonzero if this is an initializer for a top-level decl. */
7261static int constructor_top_level;
d45cf215 7262
3e4093b6
RS
7263/* Nonzero if there were any member designators in this initializer. */
7264static int constructor_designated;
d45cf215 7265
3e4093b6
RS
7266/* Nesting depth of designator list. */
7267static int designator_depth;
d45cf215 7268
3e4093b6 7269/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7270static int designator_erroneous;
d45cf215 7271
3e4093b6
RS
7272\f
7273/* This stack has a level for each implicit or explicit level of
7274 structuring in the initializer, including the outermost one. It
7275 saves the values of most of the variables above. */
d45cf215 7276
3e4093b6
RS
7277struct constructor_range_stack;
7278
7279struct constructor_stack
d45cf215 7280{
3e4093b6
RS
7281 struct constructor_stack *next;
7282 tree type;
7283 tree fields;
7284 tree index;
7285 tree max_index;
7286 tree unfilled_index;
7287 tree unfilled_fields;
7288 tree bit_index;
9771b263 7289 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7290 struct init_node *pending_elts;
7291 int offset;
7292 int depth;
916c5919 7293 /* If value nonzero, this value should replace the entire
3e4093b6 7294 constructor at this level. */
916c5919 7295 struct c_expr replacement_value;
3e4093b6
RS
7296 struct constructor_range_stack *range_stack;
7297 char constant;
7298 char simple;
928c19bb 7299 char nonconst;
3e4093b6
RS
7300 char implicit;
7301 char erroneous;
7302 char outer;
7303 char incremental;
7304 char designated;
976d5a22 7305 int designator_depth;
3e4093b6 7306};
d45cf215 7307
802415d1 7308static struct constructor_stack *constructor_stack;
d45cf215 7309
3e4093b6
RS
7310/* This stack represents designators from some range designator up to
7311 the last designator in the list. */
d45cf215 7312
3e4093b6
RS
7313struct constructor_range_stack
7314{
7315 struct constructor_range_stack *next, *prev;
7316 struct constructor_stack *stack;
7317 tree range_start;
7318 tree index;
7319 tree range_end;
7320 tree fields;
7321};
d45cf215 7322
802415d1 7323static struct constructor_range_stack *constructor_range_stack;
d45cf215 7324
3e4093b6
RS
7325/* This stack records separate initializers that are nested.
7326 Nested initializers can't happen in ANSI C, but GNU C allows them
7327 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7328
3e4093b6 7329struct initializer_stack
d45cf215 7330{
3e4093b6
RS
7331 struct initializer_stack *next;
7332 tree decl;
3e4093b6
RS
7333 struct constructor_stack *constructor_stack;
7334 struct constructor_range_stack *constructor_range_stack;
9771b263 7335 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7336 struct spelling *spelling;
7337 struct spelling *spelling_base;
7338 int spelling_size;
7339 char top_level;
7340 char require_constant_value;
7341 char require_constant_elements;
7342};
d45cf215 7343
802415d1 7344static struct initializer_stack *initializer_stack;
3e4093b6
RS
7345\f
7346/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7347
7348void
a396f8ae 7349start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 7350{
3e4093b6 7351 const char *locus;
cceb1885 7352 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7353
3e4093b6 7354 p->decl = constructor_decl;
3e4093b6
RS
7355 p->require_constant_value = require_constant_value;
7356 p->require_constant_elements = require_constant_elements;
7357 p->constructor_stack = constructor_stack;
7358 p->constructor_range_stack = constructor_range_stack;
7359 p->elements = constructor_elements;
7360 p->spelling = spelling;
7361 p->spelling_base = spelling_base;
7362 p->spelling_size = spelling_size;
7363 p->top_level = constructor_top_level;
7364 p->next = initializer_stack;
7365 initializer_stack = p;
400fbf9f 7366
3e4093b6 7367 constructor_decl = decl;
3e4093b6
RS
7368 constructor_designated = 0;
7369 constructor_top_level = top_level;
400fbf9f 7370
6f17bbcf 7371 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7372 {
7373 require_constant_value = TREE_STATIC (decl);
7374 require_constant_elements
7375 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7376 /* For a scalar, you can always use any value to initialize,
7377 even within braces. */
296a8c2f 7378 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
88388a52 7379 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7380 }
7381 else
7382 {
7383 require_constant_value = 0;
7384 require_constant_elements = 0;
88388a52 7385 locus = _("(anonymous)");
3e4093b6 7386 }
b71c7f8a 7387
3e4093b6
RS
7388 constructor_stack = 0;
7389 constructor_range_stack = 0;
b71c7f8a 7390
9bac5cbb 7391 found_missing_braces = 0;
3e4093b6
RS
7392
7393 spelling_base = 0;
7394 spelling_size = 0;
7395 RESTORE_SPELLING_DEPTH (0);
7396
7397 if (locus)
7398 push_string (locus);
7399}
7400
7401void
7402finish_init (void)
b71c7f8a 7403{
3e4093b6 7404 struct initializer_stack *p = initializer_stack;
b71c7f8a 7405
3e4093b6
RS
7406 /* Free the whole constructor stack of this initializer. */
7407 while (constructor_stack)
7408 {
7409 struct constructor_stack *q = constructor_stack;
7410 constructor_stack = q->next;
7411 free (q);
7412 }
7413
366de0ce 7414 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7415
7416 /* Pop back to the data of the outer initializer (if any). */
36579663 7417 free (spelling_base);
3aeb3655 7418
3e4093b6 7419 constructor_decl = p->decl;
3e4093b6
RS
7420 require_constant_value = p->require_constant_value;
7421 require_constant_elements = p->require_constant_elements;
7422 constructor_stack = p->constructor_stack;
7423 constructor_range_stack = p->constructor_range_stack;
7424 constructor_elements = p->elements;
7425 spelling = p->spelling;
7426 spelling_base = p->spelling_base;
7427 spelling_size = p->spelling_size;
7428 constructor_top_level = p->top_level;
7429 initializer_stack = p->next;
7430 free (p);
b71c7f8a 7431}
400fbf9f 7432\f
3e4093b6
RS
7433/* Call here when we see the initializer is surrounded by braces.
7434 This is instead of a call to push_init_level;
7435 it is matched by a call to pop_init_level.
400fbf9f 7436
3e4093b6
RS
7437 TYPE is the type to initialize, for a constructor expression.
7438 For an initializer for a decl, TYPE is zero. */
400fbf9f 7439
3e4093b6
RS
7440void
7441really_start_incremental_init (tree type)
400fbf9f 7442{
5d038c4c 7443 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7444
3e4093b6
RS
7445 if (type == 0)
7446 type = TREE_TYPE (constructor_decl);
400fbf9f 7447
31521951 7448 if (VECTOR_TYPE_P (type)
b6fc2cdb 7449 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7450 error ("opaque vector types cannot be initialized");
400fbf9f 7451
3e4093b6
RS
7452 p->type = constructor_type;
7453 p->fields = constructor_fields;
7454 p->index = constructor_index;
7455 p->max_index = constructor_max_index;
7456 p->unfilled_index = constructor_unfilled_index;
7457 p->unfilled_fields = constructor_unfilled_fields;
7458 p->bit_index = constructor_bit_index;
7459 p->elements = constructor_elements;
7460 p->constant = constructor_constant;
7461 p->simple = constructor_simple;
928c19bb 7462 p->nonconst = constructor_nonconst;
3e4093b6
RS
7463 p->erroneous = constructor_erroneous;
7464 p->pending_elts = constructor_pending_elts;
7465 p->depth = constructor_depth;
916c5919
JM
7466 p->replacement_value.value = 0;
7467 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7468 p->replacement_value.original_type = NULL;
3e4093b6
RS
7469 p->implicit = 0;
7470 p->range_stack = 0;
7471 p->outer = 0;
7472 p->incremental = constructor_incremental;
7473 p->designated = constructor_designated;
976d5a22 7474 p->designator_depth = designator_depth;
3e4093b6
RS
7475 p->next = 0;
7476 constructor_stack = p;
b13aca19 7477
3e4093b6
RS
7478 constructor_constant = 1;
7479 constructor_simple = 1;
928c19bb 7480 constructor_nonconst = 0;
3e4093b6 7481 constructor_depth = SPELLING_DEPTH ();
9771b263 7482 constructor_elements = NULL;
3e4093b6
RS
7483 constructor_pending_elts = 0;
7484 constructor_type = type;
7485 constructor_incremental = 1;
7486 constructor_designated = 0;
9bac5cbb 7487 constructor_zeroinit = 1;
3e4093b6 7488 designator_depth = 0;
b06df647 7489 designator_erroneous = 0;
400fbf9f 7490
3e636daf 7491 if (RECORD_OR_UNION_TYPE_P (constructor_type))
400fbf9f 7492 {
3e4093b6
RS
7493 constructor_fields = TYPE_FIELDS (constructor_type);
7494 /* Skip any nameless bit fields at the beginning. */
7495 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7496 && DECL_NAME (constructor_fields) == 0)
910ad8de 7497 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7498
3e4093b6
RS
7499 constructor_unfilled_fields = constructor_fields;
7500 constructor_bit_index = bitsize_zero_node;
400fbf9f 7501 }
3e4093b6
RS
7502 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7503 {
7504 if (TYPE_DOMAIN (constructor_type))
7505 {
7506 constructor_max_index
7507 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7508
3e4093b6
RS
7509 /* Detect non-empty initializations of zero-length arrays. */
7510 if (constructor_max_index == NULL_TREE
7511 && TYPE_SIZE (constructor_type))
9a9d280e 7512 constructor_max_index = integer_minus_one_node;
400fbf9f 7513
3e4093b6
RS
7514 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7515 to initialize VLAs will cause a proper error; avoid tree
7516 checking errors as well by setting a safe value. */
7517 if (constructor_max_index
7518 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7519 constructor_max_index = integer_minus_one_node;
59c83dbf 7520
3e4093b6
RS
7521 constructor_index
7522 = convert (bitsizetype,
7523 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7524 }
3e4093b6 7525 else
493179da
JM
7526 {
7527 constructor_index = bitsize_zero_node;
7528 constructor_max_index = NULL_TREE;
7529 }
59c83dbf 7530
3e4093b6
RS
7531 constructor_unfilled_index = constructor_index;
7532 }
31521951 7533 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7534 {
7535 /* Vectors are like simple fixed-size arrays. */
7536 constructor_max_index =
c62c040f 7537 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7538 constructor_index = bitsize_zero_node;
3e4093b6
RS
7539 constructor_unfilled_index = constructor_index;
7540 }
7541 else
7542 {
7543 /* Handle the case of int x = {5}; */
7544 constructor_fields = constructor_type;
7545 constructor_unfilled_fields = constructor_type;
7546 }
7547}
7548\f
7549/* Push down into a subobject, for initialization.
7550 If this is for an explicit set of braces, IMPLICIT is 0.
7551 If it is because the next element belongs at a lower level,
7552 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7553
3e4093b6 7554void
ea58ef42
MP
7555push_init_level (location_t loc, int implicit,
7556 struct obstack *braced_init_obstack)
3e4093b6
RS
7557{
7558 struct constructor_stack *p;
7559 tree value = NULL_TREE;
400fbf9f 7560
3e4093b6 7561 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7562 pop them now. If implicit == 1, this will have been done in
7563 process_init_element; do not repeat it here because in the case
7564 of excess initializers for an empty aggregate this leads to an
7565 infinite cycle of popping a level and immediately recreating
7566 it. */
7567 if (implicit != 1)
3e4093b6 7568 {
472d98b4
JM
7569 while (constructor_stack->implicit)
7570 {
3e636daf 7571 if (RECORD_OR_UNION_TYPE_P (constructor_type)
472d98b4 7572 && constructor_fields == 0)
34cf811f 7573 process_init_element (input_location,
ea58ef42 7574 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7575 true, braced_init_obstack);
472d98b4
JM
7576 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7577 && constructor_max_index
7578 && tree_int_cst_lt (constructor_max_index,
7579 constructor_index))
34cf811f 7580 process_init_element (input_location,
ea58ef42 7581 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7582 true, braced_init_obstack);
472d98b4
JM
7583 else
7584 break;
7585 }
3e4093b6 7586 }
400fbf9f 7587
3e4093b6
RS
7588 /* Unless this is an explicit brace, we need to preserve previous
7589 content if any. */
7590 if (implicit)
7591 {
3e636daf 7592 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
a1e3b3d9 7593 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7594 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7595 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7596 }
7597
5d038c4c 7598 p = XNEW (struct constructor_stack);
3e4093b6
RS
7599 p->type = constructor_type;
7600 p->fields = constructor_fields;
7601 p->index = constructor_index;
7602 p->max_index = constructor_max_index;
7603 p->unfilled_index = constructor_unfilled_index;
7604 p->unfilled_fields = constructor_unfilled_fields;
7605 p->bit_index = constructor_bit_index;
7606 p->elements = constructor_elements;
7607 p->constant = constructor_constant;
7608 p->simple = constructor_simple;
928c19bb 7609 p->nonconst = constructor_nonconst;
3e4093b6
RS
7610 p->erroneous = constructor_erroneous;
7611 p->pending_elts = constructor_pending_elts;
7612 p->depth = constructor_depth;
916c5919
JM
7613 p->replacement_value.value = 0;
7614 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7615 p->replacement_value.original_type = NULL;
3e4093b6
RS
7616 p->implicit = implicit;
7617 p->outer = 0;
7618 p->incremental = constructor_incremental;
7619 p->designated = constructor_designated;
976d5a22 7620 p->designator_depth = designator_depth;
3e4093b6
RS
7621 p->next = constructor_stack;
7622 p->range_stack = 0;
7623 constructor_stack = p;
400fbf9f 7624
3e4093b6
RS
7625 constructor_constant = 1;
7626 constructor_simple = 1;
928c19bb 7627 constructor_nonconst = 0;
3e4093b6 7628 constructor_depth = SPELLING_DEPTH ();
9771b263 7629 constructor_elements = NULL;
3e4093b6
RS
7630 constructor_incremental = 1;
7631 constructor_designated = 0;
7632 constructor_pending_elts = 0;
7633 if (!implicit)
400fbf9f 7634 {
3e4093b6
RS
7635 p->range_stack = constructor_range_stack;
7636 constructor_range_stack = 0;
7637 designator_depth = 0;
b06df647 7638 designator_erroneous = 0;
3e4093b6 7639 }
400fbf9f 7640
3e4093b6
RS
7641 /* Don't die if an entire brace-pair level is superfluous
7642 in the containing level. */
7643 if (constructor_type == 0)
7644 ;
3e636daf 7645 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
7646 {
7647 /* Don't die if there are extra init elts at the end. */
7648 if (constructor_fields == 0)
7649 constructor_type = 0;
7650 else
400fbf9f 7651 {
3e4093b6
RS
7652 constructor_type = TREE_TYPE (constructor_fields);
7653 push_member_name (constructor_fields);
7654 constructor_depth++;
400fbf9f 7655 }
6a358dcb
MP
7656 /* If upper initializer is designated, then mark this as
7657 designated too to prevent bogus warnings. */
7658 constructor_designated = p->designated;
3e4093b6
RS
7659 }
7660 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7661 {
7662 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7663 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7664 constructor_depth++;
400fbf9f
JW
7665 }
7666
3e4093b6 7667 if (constructor_type == 0)
400fbf9f 7668 {
ea58ef42 7669 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7670 constructor_fields = 0;
7671 constructor_unfilled_fields = 0;
7672 return;
400fbf9f
JW
7673 }
7674
3e4093b6
RS
7675 if (value && TREE_CODE (value) == CONSTRUCTOR)
7676 {
7677 constructor_constant = TREE_CONSTANT (value);
7678 constructor_simple = TREE_STATIC (value);
928c19bb 7679 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7680 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7681 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7682 && (TREE_CODE (constructor_type) == RECORD_TYPE
7683 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7684 set_nonincremental_init (braced_init_obstack);
3e4093b6 7685 }
400fbf9f 7686
9bac5cbb
G
7687 if (implicit == 1)
7688 found_missing_braces = 1;
400fbf9f 7689
3e636daf 7690 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
7691 {
7692 constructor_fields = TYPE_FIELDS (constructor_type);
7693 /* Skip any nameless bit fields at the beginning. */
7694 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7695 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7696 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7697
3e4093b6
RS
7698 constructor_unfilled_fields = constructor_fields;
7699 constructor_bit_index = bitsize_zero_node;
7700 }
31521951 7701 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7702 {
7703 /* Vectors are like simple fixed-size arrays. */
7704 constructor_max_index =
c62c040f
RG
7705 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7706 constructor_index = bitsize_int (0);
3e4093b6
RS
7707 constructor_unfilled_index = constructor_index;
7708 }
7709 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7710 {
7711 if (TYPE_DOMAIN (constructor_type))
7712 {
7713 constructor_max_index
7714 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7715
3e4093b6
RS
7716 /* Detect non-empty initializations of zero-length arrays. */
7717 if (constructor_max_index == NULL_TREE
7718 && TYPE_SIZE (constructor_type))
9a9d280e 7719 constructor_max_index = integer_minus_one_node;
de520661 7720
3e4093b6
RS
7721 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7722 to initialize VLAs will cause a proper error; avoid tree
7723 checking errors as well by setting a safe value. */
7724 if (constructor_max_index
7725 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7726 constructor_max_index = integer_minus_one_node;
b62acd60 7727
3e4093b6
RS
7728 constructor_index
7729 = convert (bitsizetype,
7730 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7731 }
7732 else
7733 constructor_index = bitsize_zero_node;
de520661 7734
3e4093b6
RS
7735 constructor_unfilled_index = constructor_index;
7736 if (value && TREE_CODE (value) == STRING_CST)
7737 {
7738 /* We need to split the char/wchar array into individual
7739 characters, so that we don't have to special case it
7740 everywhere. */
a1e3b3d9 7741 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7742 }
7743 }
7744 else
7745 {
b4519d39 7746 if (constructor_type != error_mark_node)
96b40f8d 7747 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7748 constructor_fields = constructor_type;
7749 constructor_unfilled_fields = constructor_type;
7750 }
7751}
8b6a5902 7752
3e4093b6 7753/* At the end of an implicit or explicit brace level,
916c5919
JM
7754 finish up that level of constructor. If a single expression
7755 with redundant braces initialized that level, return the
7756 c_expr structure for that expression. Otherwise, the original_code
7757 element is set to ERROR_MARK.
7758 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7759 from inner levels (process_init_element ignores that),
916c5919 7760 but return error_mark_node as the value from the outermost level
3e4093b6 7761 (that's what we want to put in DECL_INITIAL).
916c5919 7762 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7763
916c5919 7764struct c_expr
ea58ef42
MP
7765pop_init_level (location_t loc, int implicit,
7766 struct obstack *braced_init_obstack)
3e4093b6
RS
7767{
7768 struct constructor_stack *p;
916c5919
JM
7769 struct c_expr ret;
7770 ret.value = 0;
7771 ret.original_code = ERROR_MARK;
6866c6e8 7772 ret.original_type = NULL;
de520661 7773
3e4093b6
RS
7774 if (implicit == 0)
7775 {
7776 /* When we come to an explicit close brace,
7777 pop any inner levels that didn't have explicit braces. */
7778 while (constructor_stack->implicit)
34cf811f 7779 process_init_element (input_location,
ea58ef42 7780 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7781 true, braced_init_obstack);
366de0ce 7782 gcc_assert (!constructor_range_stack);
3e4093b6 7783 }
e5e809f4 7784
0066ef9c
RH
7785 /* Now output all pending elements. */
7786 constructor_incremental = 1;
a1e3b3d9 7787 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7788
3e4093b6 7789 p = constructor_stack;
e5e809f4 7790
3e4093b6
RS
7791 /* Error for initializing a flexible array member, or a zero-length
7792 array member in an inappropriate context. */
7793 if (constructor_type && constructor_fields
7794 && TREE_CODE (constructor_type) == ARRAY_TYPE
7795 && TYPE_DOMAIN (constructor_type)
3f75a254 7796 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7797 {
7798 /* Silently discard empty initializations. The parser will
7799 already have pedwarned for empty brackets. */
7800 if (integer_zerop (constructor_unfilled_index))
7801 constructor_type = NULL_TREE;
366de0ce 7802 else
3e4093b6 7803 {
366de0ce 7804 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7805
3e4093b6 7806 if (constructor_depth > 2)
ea58ef42 7807 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7808 else
d033409e 7809 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7810 "initialization of a flexible array member");
de520661 7811
3e4093b6
RS
7812 /* We have already issued an error message for the existence
7813 of a flexible array member not at the end of the structure.
535a42b1 7814 Discard the initializer so that we do not die later. */
910ad8de 7815 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7816 constructor_type = NULL_TREE;
7817 }
3e4093b6 7818 }
de520661 7819
7b33f0c8
MP
7820 switch (vec_safe_length (constructor_elements))
7821 {
7822 case 0:
7823 /* Initialization with { } counts as zeroinit. */
7824 constructor_zeroinit = 1;
7825 break;
7826 case 1:
7827 /* This might be zeroinit as well. */
7828 if (integer_zerop ((*constructor_elements)[0].value))
7829 constructor_zeroinit = 1;
7830 break;
7831 default:
7832 /* If the constructor has more than one element, it can't be { 0 }. */
7833 constructor_zeroinit = 0;
7834 break;
7835 }
9bac5cbb
G
7836
7837 /* Warn when some structs are initialized with direct aggregation. */
7838 if (!implicit && found_missing_braces && warn_missing_braces
7839 && !constructor_zeroinit)
7b33f0c8
MP
7840 warning_init (loc, OPT_Wmissing_braces,
7841 "missing braces around initializer");
9bac5cbb 7842
3e4093b6 7843 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7844 if (warn_missing_field_initializers
3e4093b6
RS
7845 && constructor_type
7846 && TREE_CODE (constructor_type) == RECORD_TYPE
7847 && constructor_unfilled_fields)
7848 {
7849 /* Do not warn for flexible array members or zero-length arrays. */
7850 while (constructor_unfilled_fields
3f75a254 7851 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7852 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7853 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7854
49819fef
AM
7855 if (constructor_unfilled_fields
7856 /* Do not warn if this level of the initializer uses member
7857 designators; it is likely to be deliberate. */
7858 && !constructor_designated
84937de2 7859 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 7860 && !constructor_zeroinit)
3e4093b6 7861 {
32397f22
MLI
7862 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7863 "missing initializer for field %qD of %qT",
7864 constructor_unfilled_fields,
7865 constructor_type))
7866 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7867 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7868 }
7869 }
de520661 7870
3e4093b6 7871 /* Pad out the end of the structure. */
916c5919 7872 if (p->replacement_value.value)
3e4093b6
RS
7873 /* If this closes a superfluous brace pair,
7874 just pass out the element between them. */
916c5919 7875 ret = p->replacement_value;
3e4093b6
RS
7876 else if (constructor_type == 0)
7877 ;
3e636daf 7878 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 7879 && TREE_CODE (constructor_type) != ARRAY_TYPE
31521951 7880 && !VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7881 {
7882 /* A nonincremental scalar initializer--just return
7883 the element, after verifying there is just one. */
9771b263 7884 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7885 {
7886 if (!constructor_erroneous)
ea58ef42 7887 error_init (loc, "empty scalar initializer");
916c5919 7888 ret.value = error_mark_node;
3e4093b6 7889 }
9771b263 7890 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 7891 {
ea58ef42 7892 error_init (loc, "extra elements in scalar initializer");
9771b263 7893 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7894 }
7895 else
9771b263 7896 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7897 }
7898 else
7899 {
7900 if (constructor_erroneous)
916c5919 7901 ret.value = error_mark_node;
3e4093b6
RS
7902 else
7903 {
916c5919 7904 ret.value = build_constructor (constructor_type,
4038c495 7905 constructor_elements);
3e4093b6 7906 if (constructor_constant)
51eed280 7907 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7908 if (constructor_constant && constructor_simple)
916c5919 7909 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7910 if (constructor_nonconst)
7911 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7912 }
7913 }
de520661 7914
928c19bb
JM
7915 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7916 {
7917 if (constructor_nonconst)
7918 ret.original_code = C_MAYBE_CONST_EXPR;
7919 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7920 ret.original_code = ERROR_MARK;
7921 }
7922
3e4093b6
RS
7923 constructor_type = p->type;
7924 constructor_fields = p->fields;
7925 constructor_index = p->index;
7926 constructor_max_index = p->max_index;
7927 constructor_unfilled_index = p->unfilled_index;
7928 constructor_unfilled_fields = p->unfilled_fields;
7929 constructor_bit_index = p->bit_index;
7930 constructor_elements = p->elements;
7931 constructor_constant = p->constant;
7932 constructor_simple = p->simple;
928c19bb 7933 constructor_nonconst = p->nonconst;
3e4093b6
RS
7934 constructor_erroneous = p->erroneous;
7935 constructor_incremental = p->incremental;
7936 constructor_designated = p->designated;
976d5a22 7937 designator_depth = p->designator_depth;
3e4093b6
RS
7938 constructor_pending_elts = p->pending_elts;
7939 constructor_depth = p->depth;
7940 if (!p->implicit)
7941 constructor_range_stack = p->range_stack;
7942 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7943
3e4093b6
RS
7944 constructor_stack = p->next;
7945 free (p);
b621a4dd 7946
5d5e98dc
VR
7947 if (ret.value == 0 && constructor_stack == 0)
7948 ret.value = error_mark_node;
916c5919 7949 return ret;
3e4093b6 7950}
8b6a5902 7951
3e4093b6
RS
7952/* Common handling for both array range and field name designators.
7953 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7954
3e4093b6 7955static int
ea58ef42
MP
7956set_designator (location_t loc, int array,
7957 struct obstack *braced_init_obstack)
de520661 7958{
3e4093b6
RS
7959 tree subtype;
7960 enum tree_code subcode;
de520661 7961
3e4093b6
RS
7962 /* Don't die if an entire brace-pair level is superfluous
7963 in the containing level. */
7964 if (constructor_type == 0)
7965 return 1;
de520661 7966
366de0ce
NS
7967 /* If there were errors in this designator list already, bail out
7968 silently. */
b06df647 7969 if (designator_erroneous)
3e4093b6 7970 return 1;
e28cae4f 7971
3e4093b6
RS
7972 if (!designator_depth)
7973 {
366de0ce 7974 gcc_assert (!constructor_range_stack);
de520661 7975
3e4093b6
RS
7976 /* Designator list starts at the level of closest explicit
7977 braces. */
7978 while (constructor_stack->implicit)
34cf811f 7979 process_init_element (input_location,
ea58ef42 7980 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7981 true, braced_init_obstack);
3e4093b6
RS
7982 constructor_designated = 1;
7983 return 0;
7984 }
de520661 7985
366de0ce 7986 switch (TREE_CODE (constructor_type))
3c3fa147 7987 {
366de0ce
NS
7988 case RECORD_TYPE:
7989 case UNION_TYPE:
3e4093b6
RS
7990 subtype = TREE_TYPE (constructor_fields);
7991 if (subtype != error_mark_node)
7992 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7993 break;
7994 case ARRAY_TYPE:
3e4093b6 7995 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7996 break;
7997 default:
7998 gcc_unreachable ();
de520661 7999 }
400fbf9f 8000
3e4093b6
RS
8001 subcode = TREE_CODE (subtype);
8002 if (array && subcode != ARRAY_TYPE)
8003 {
ea58ef42 8004 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
8005 return 1;
8006 }
8007 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8008 {
ea58ef42 8009 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
8010 return 1;
8011 }
d45cf215 8012
3e4093b6 8013 constructor_designated = 1;
ea58ef42 8014 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 8015 return 0;
de520661 8016}
400fbf9f 8017
3e4093b6
RS
8018/* If there are range designators in designator list, push a new designator
8019 to constructor_range_stack. RANGE_END is end of such stack range or
8020 NULL_TREE if there is no range designator at this level. */
400fbf9f 8021
3e4093b6 8022static void
a1e3b3d9 8023push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
8024{
8025 struct constructor_range_stack *p;
400fbf9f 8026
a1e3b3d9
LB
8027 p = (struct constructor_range_stack *)
8028 obstack_alloc (braced_init_obstack,
8029 sizeof (struct constructor_range_stack));
3e4093b6
RS
8030 p->prev = constructor_range_stack;
8031 p->next = 0;
8032 p->fields = constructor_fields;
8033 p->range_start = constructor_index;
8034 p->index = constructor_index;
8035 p->stack = constructor_stack;
8036 p->range_end = range_end;
8b6a5902 8037 if (constructor_range_stack)
3e4093b6
RS
8038 constructor_range_stack->next = p;
8039 constructor_range_stack = p;
de520661 8040}
400fbf9f 8041
3e4093b6
RS
8042/* Within an array initializer, specify the next index to be initialized.
8043 FIRST is that index. If LAST is nonzero, then initialize a range
8044 of indices, running from FIRST through LAST. */
5a7ec9d9 8045
de520661 8046void
ea58ef42 8047set_init_index (location_t loc, tree first, tree last,
d033409e 8048 struct obstack *braced_init_obstack)
de520661 8049{
ea58ef42 8050 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 8051 return;
de520661 8052
b06df647 8053 designator_erroneous = 1;
de520661 8054
3ea8cd06
JM
8055 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8056 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8057 {
ea58ef42 8058 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
8059 return;
8060 }
8061
2b6da65c
JM
8062 if (TREE_CODE (first) != INTEGER_CST)
8063 {
8064 first = c_fully_fold (first, false, NULL);
8065 if (TREE_CODE (first) == INTEGER_CST)
d033409e 8066 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8067 "array index in initializer is not "
8068 "an integer constant expression");
8069 }
8070
8071 if (last && TREE_CODE (last) != INTEGER_CST)
8072 {
8073 last = c_fully_fold (last, false, NULL);
8074 if (TREE_CODE (last) == INTEGER_CST)
d033409e 8075 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8076 "array index in initializer is not "
8077 "an integer constant expression");
8078 }
8079
3e4093b6 8080 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 8081 error_init (loc, "nonconstant array index in initializer");
3e4093b6 8082 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 8083 error_init (loc, "nonconstant array index in initializer");
3e4093b6 8084 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 8085 error_init (loc, "array index in non-array initializer");
622adc7e 8086 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 8087 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
8088 else if (constructor_max_index
8089 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 8090 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 8091 else
de520661 8092 {
928c19bb
JM
8093 constant_expression_warning (first);
8094 if (last)
8095 constant_expression_warning (last);
3e4093b6 8096 constructor_index = convert (bitsizetype, first);
40d3d530
JR
8097 if (tree_int_cst_lt (constructor_index, first))
8098 {
8099 constructor_index = copy_node (constructor_index);
8100 TREE_OVERFLOW (constructor_index) = 1;
8101 }
665f2503 8102
3e4093b6 8103 if (last)
2bede729 8104 {
3e4093b6
RS
8105 if (tree_int_cst_equal (first, last))
8106 last = 0;
8107 else if (tree_int_cst_lt (last, first))
8108 {
ea58ef42 8109 error_init (loc, "empty index range in initializer");
3e4093b6
RS
8110 last = 0;
8111 }
8112 else
8113 {
8114 last = convert (bitsizetype, last);
8115 if (constructor_max_index != 0
8116 && tree_int_cst_lt (constructor_max_index, last))
8117 {
ea58ef42
MP
8118 error_init (loc, "array index range in initializer exceeds "
8119 "array bounds");
3e4093b6
RS
8120 last = 0;
8121 }
8122 }
2bede729 8123 }
fed3cef0 8124
3e4093b6 8125 designator_depth++;
b06df647 8126 designator_erroneous = 0;
3e4093b6 8127 if (constructor_range_stack || last)
a1e3b3d9 8128 push_range_stack (last, braced_init_obstack);
de520661 8129 }
de520661 8130}
3e4093b6
RS
8131
8132/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 8133
de520661 8134void
ea58ef42
MP
8135set_init_label (location_t loc, tree fieldname,
8136 struct obstack *braced_init_obstack)
de520661 8137{
0fb96aa4 8138 tree field;
94ba5069 8139
ea58ef42 8140 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
8141 return;
8142
b06df647 8143 designator_erroneous = 1;
3e4093b6 8144
3e636daf 8145 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
94ba5069 8146 {
ea58ef42 8147 error_init (loc, "field name not in record or union initializer");
3e4093b6 8148 return;
94ba5069
RS
8149 }
8150
0fb96aa4 8151 field = lookup_field (constructor_type, fieldname);
8b6a5902 8152
0fb96aa4 8153 if (field == 0)
9babc352 8154 error_at (loc, "unknown field %qE specified in initializer", fieldname);
3e4093b6 8155 else
0fb96aa4
JM
8156 do
8157 {
8158 constructor_fields = TREE_VALUE (field);
8159 designator_depth++;
8160 designator_erroneous = 0;
8161 if (constructor_range_stack)
8162 push_range_stack (NULL_TREE, braced_init_obstack);
8163 field = TREE_CHAIN (field);
8164 if (field)
8165 {
ea58ef42 8166 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
8167 return;
8168 }
8169 }
8170 while (field != NULL_TREE);
3e4093b6
RS
8171}
8172\f
8173/* Add a new initializer to the tree of pending initializers. PURPOSE
8174 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
8175 VALUE is the value of that index or field. If ORIGTYPE is not
8176 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
8177
8178 IMPLICIT is true if value comes from pop_init_level (1),
8179 the new initializer has been merged with the existing one
8180 and thus no warnings should be emitted about overriding an
8181 existing initializer. */
de520661 8182
3e4093b6 8183static void
96b40f8d
MP
8184add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8185 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
8186{
8187 struct init_node *p, **q, *r;
8188
8189 q = &constructor_pending_elts;
8190 p = 0;
8191
8192 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8193 {
3e4093b6 8194 while (*q != 0)
91fa3c30 8195 {
3e4093b6
RS
8196 p = *q;
8197 if (tree_int_cst_lt (purpose, p->purpose))
8198 q = &p->left;
8199 else if (tree_int_cst_lt (p->purpose, purpose))
8200 q = &p->right;
8201 else
8202 {
b295aee2
JJ
8203 if (!implicit)
8204 {
8205 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8206 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8207 "initialized field with side-effects "
8208 "overwritten");
b295aee2 8209 else if (warn_override_init)
96b40f8d
MP
8210 warning_init (loc, OPT_Woverride_init,
8211 "initialized field overwritten");
b295aee2 8212 }
3e4093b6 8213 p->value = value;
bbbbb16a 8214 p->origtype = origtype;
3e4093b6
RS
8215 return;
8216 }
91fa3c30 8217 }
de520661 8218 }
3e4093b6 8219 else
de520661 8220 {
3e4093b6 8221 tree bitpos;
400fbf9f 8222
3e4093b6
RS
8223 bitpos = bit_position (purpose);
8224 while (*q != NULL)
8225 {
8226 p = *q;
8227 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8228 q = &p->left;
8229 else if (p->purpose != purpose)
8230 q = &p->right;
8231 else
8232 {
b295aee2
JJ
8233 if (!implicit)
8234 {
8235 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8236 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8237 "initialized field with side-effects "
8238 "overwritten");
b295aee2 8239 else if (warn_override_init)
96b40f8d
MP
8240 warning_init (loc, OPT_Woverride_init,
8241 "initialized field overwritten");
b295aee2 8242 }
3e4093b6 8243 p->value = value;
bbbbb16a 8244 p->origtype = origtype;
3e4093b6
RS
8245 return;
8246 }
8247 }
91fa3c30 8248 }
b71c7f8a 8249
a1e3b3d9
LB
8250 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8251 sizeof (struct init_node));
3e4093b6
RS
8252 r->purpose = purpose;
8253 r->value = value;
bbbbb16a 8254 r->origtype = origtype;
8b6a5902 8255
3e4093b6
RS
8256 *q = r;
8257 r->parent = p;
8258 r->left = 0;
8259 r->right = 0;
8260 r->balance = 0;
b71c7f8a 8261
3e4093b6 8262 while (p)
de520661 8263 {
3e4093b6 8264 struct init_node *s;
665f2503 8265
3e4093b6 8266 if (r == p->left)
2bede729 8267 {
3e4093b6
RS
8268 if (p->balance == 0)
8269 p->balance = -1;
8270 else if (p->balance < 0)
8271 {
8272 if (r->balance < 0)
8273 {
8274 /* L rotation. */
8275 p->left = r->right;
8276 if (p->left)
8277 p->left->parent = p;
8278 r->right = p;
e7b6a0ee 8279
3e4093b6
RS
8280 p->balance = 0;
8281 r->balance = 0;
39bc99c2 8282
3e4093b6
RS
8283 s = p->parent;
8284 p->parent = r;
8285 r->parent = s;
8286 if (s)
8287 {
8288 if (s->left == p)
8289 s->left = r;
8290 else
8291 s->right = r;
8292 }
8293 else
8294 constructor_pending_elts = r;
8295 }
8296 else
8297 {
8298 /* LR rotation. */
8299 struct init_node *t = r->right;
e7b6a0ee 8300
3e4093b6
RS
8301 r->right = t->left;
8302 if (r->right)
8303 r->right->parent = r;
8304 t->left = r;
8305
8306 p->left = t->right;
8307 if (p->left)
8308 p->left->parent = p;
8309 t->right = p;
8310
8311 p->balance = t->balance < 0;
8312 r->balance = -(t->balance > 0);
8313 t->balance = 0;
8314
8315 s = p->parent;
8316 p->parent = t;
8317 r->parent = t;
8318 t->parent = s;
8319 if (s)
8320 {
8321 if (s->left == p)
8322 s->left = t;
8323 else
8324 s->right = t;
8325 }
8326 else
8327 constructor_pending_elts = t;
8328 }
8329 break;
8330 }
8331 else
8332 {
8333 /* p->balance == +1; growth of left side balances the node. */
8334 p->balance = 0;
8335 break;
8336 }
2bede729 8337 }
3e4093b6
RS
8338 else /* r == p->right */
8339 {
8340 if (p->balance == 0)
8341 /* Growth propagation from right side. */
8342 p->balance++;
8343 else if (p->balance > 0)
8344 {
8345 if (r->balance > 0)
8346 {
8347 /* R rotation. */
8348 p->right = r->left;
8349 if (p->right)
8350 p->right->parent = p;
8351 r->left = p;
8352
8353 p->balance = 0;
8354 r->balance = 0;
8355
8356 s = p->parent;
8357 p->parent = r;
8358 r->parent = s;
8359 if (s)
8360 {
8361 if (s->left == p)
8362 s->left = r;
8363 else
8364 s->right = r;
8365 }
8366 else
8367 constructor_pending_elts = r;
8368 }
8369 else /* r->balance == -1 */
8370 {
8371 /* RL rotation */
8372 struct init_node *t = r->left;
8373
8374 r->left = t->right;
8375 if (r->left)
8376 r->left->parent = r;
8377 t->right = r;
8378
8379 p->right = t->left;
8380 if (p->right)
8381 p->right->parent = p;
8382 t->left = p;
8383
8384 r->balance = (t->balance < 0);
8385 p->balance = -(t->balance > 0);
8386 t->balance = 0;
8387
8388 s = p->parent;
8389 p->parent = t;
8390 r->parent = t;
8391 t->parent = s;
8392 if (s)
8393 {
8394 if (s->left == p)
8395 s->left = t;
8396 else
8397 s->right = t;
8398 }
8399 else
8400 constructor_pending_elts = t;
8401 }
8402 break;
8403 }
8404 else
8405 {
8406 /* p->balance == -1; growth of right side balances the node. */
8407 p->balance = 0;
8408 break;
8409 }
8410 }
8411
8412 r = p;
8413 p = p->parent;
8414 }
8415}
8416
8417/* Build AVL tree from a sorted chain. */
8418
8419static void
a1e3b3d9 8420set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8421{
4038c495
GB
8422 unsigned HOST_WIDE_INT ix;
8423 tree index, value;
3e4093b6
RS
8424
8425 if (TREE_CODE (constructor_type) != RECORD_TYPE
8426 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8427 return;
8428
4038c495 8429 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8430 add_pending_init (input_location, index, value, NULL_TREE, true,
8431 braced_init_obstack);
9771b263 8432 constructor_elements = NULL;
3e4093b6
RS
8433 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8434 {
8435 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8436 /* Skip any nameless bit fields at the beginning. */
8437 while (constructor_unfilled_fields != 0
8438 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8439 && DECL_NAME (constructor_unfilled_fields) == 0)
8440 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8441
de520661 8442 }
3e4093b6 8443 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8444 {
3e4093b6
RS
8445 if (TYPE_DOMAIN (constructor_type))
8446 constructor_unfilled_index
8447 = convert (bitsizetype,
8448 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8449 else
8450 constructor_unfilled_index = bitsize_zero_node;
de520661 8451 }
3e4093b6 8452 constructor_incremental = 0;
de520661 8453}
400fbf9f 8454
3e4093b6 8455/* Build AVL tree from a string constant. */
de520661 8456
3e4093b6 8457static void
a1e3b3d9
LB
8458set_nonincremental_init_from_string (tree str,
8459 struct obstack * braced_init_obstack)
de520661 8460{
3e4093b6
RS
8461 tree value, purpose, type;
8462 HOST_WIDE_INT val[2];
8463 const char *p, *end;
8464 int byte, wchar_bytes, charwidth, bitpos;
de520661 8465
366de0ce 8466 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8467
c466b2cd 8468 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8469 charwidth = TYPE_PRECISION (char_type_node);
8470 type = TREE_TYPE (constructor_type);
8471 p = TREE_STRING_POINTER (str);
8472 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8473
3e4093b6 8474 for (purpose = bitsize_zero_node;
8824edff
JJ
8475 p < end
8476 && !(constructor_max_index
8477 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8478 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8479 {
3e4093b6 8480 if (wchar_bytes == 1)
ffc5c6a9 8481 {
807e902e
KZ
8482 val[0] = (unsigned char) *p++;
8483 val[1] = 0;
ffc5c6a9
RH
8484 }
8485 else
3e4093b6 8486 {
3e4093b6 8487 val[1] = 0;
807e902e 8488 val[0] = 0;
3e4093b6
RS
8489 for (byte = 0; byte < wchar_bytes; byte++)
8490 {
8491 if (BYTES_BIG_ENDIAN)
8492 bitpos = (wchar_bytes - byte - 1) * charwidth;
8493 else
8494 bitpos = byte * charwidth;
807e902e 8495 val[bitpos % HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8496 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8497 << (bitpos % HOST_BITS_PER_WIDE_INT);
8498 }
8499 }
584ef5fe 8500
8df83eae 8501 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8502 {
8503 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8504 if (bitpos < HOST_BITS_PER_WIDE_INT)
8505 {
807e902e 8506 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
3e4093b6 8507 {
aa256c4a 8508 val[0] |= HOST_WIDE_INT_M1U << bitpos;
807e902e 8509 val[1] = -1;
3e4093b6
RS
8510 }
8511 }
8512 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8513 {
807e902e
KZ
8514 if (val[0] < 0)
8515 val[1] = -1;
3e4093b6 8516 }
807e902e 8517 else if (val[1] & (((HOST_WIDE_INT) 1)
3e4093b6 8518 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
aa256c4a 8519 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
3e4093b6 8520 }
ffc5c6a9 8521
807e902e
KZ
8522 value = wide_int_to_tree (type,
8523 wide_int::from_array (val, 2,
8524 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8525 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8526 braced_init_obstack);
9dfcc8db
BH
8527 }
8528
3e4093b6
RS
8529 constructor_incremental = 0;
8530}
de520661 8531
3e4093b6
RS
8532/* Return value of FIELD in pending initializer or zero if the field was
8533 not initialized yet. */
8534
8535static tree
a1e3b3d9 8536find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8537{
8538 struct init_node *p;
8539
8540 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8541 {
3e4093b6
RS
8542 if (constructor_incremental
8543 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8544 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8545
8546 p = constructor_pending_elts;
8547 while (p)
19d76e60 8548 {
3e4093b6
RS
8549 if (tree_int_cst_lt (field, p->purpose))
8550 p = p->left;
8551 else if (tree_int_cst_lt (p->purpose, field))
8552 p = p->right;
8553 else
8554 return p->value;
19d76e60 8555 }
19d76e60 8556 }
3e4093b6 8557 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8558 {
3e4093b6 8559 tree bitpos = bit_position (field);
de520661 8560
3e4093b6
RS
8561 if (constructor_incremental
8562 && (!constructor_unfilled_fields
8563 || tree_int_cst_lt (bitpos,
8564 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8565 set_nonincremental_init (braced_init_obstack);
de520661 8566
3e4093b6
RS
8567 p = constructor_pending_elts;
8568 while (p)
8569 {
8570 if (field == p->purpose)
8571 return p->value;
8572 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8573 p = p->left;
8574 else
8575 p = p->right;
8576 }
8577 }
8578 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8579 {
9771b263
DN
8580 if (!vec_safe_is_empty (constructor_elements)
8581 && (constructor_elements->last ().index == field))
8582 return constructor_elements->last ().value;
de520661 8583 }
3e4093b6 8584 return 0;
de520661
RS
8585}
8586
3e4093b6
RS
8587/* "Output" the next constructor element.
8588 At top level, really output it to assembler code now.
8589 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8590 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8591 TYPE is the data type that the containing data type wants here.
8592 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8593 If VALUE is a string constant, STRICT_STRING is true if it is
8594 unparenthesized or we should not warn here for it being parenthesized.
8595 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8596
3e4093b6
RS
8597 PENDING if non-nil means output pending elements that belong
8598 right after this element. (PENDING is normally 1;
b295aee2
JJ
8599 it is 0 while outputting pending elements, to avoid recursion.)
8600
8601 IMPLICIT is true if value comes from pop_init_level (1),
8602 the new initializer has been merged with the existing one
8603 and thus no warnings should be emitted about overriding an
8604 existing initializer. */
8b6a5902 8605
3e4093b6 8606static void
34cf811f
MP
8607output_init_element (location_t loc, tree value, tree origtype,
8608 bool strict_string, tree type, tree field, int pending,
8609 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8610{
8ce94e44 8611 tree semantic_type = NULL_TREE;
928c19bb
JM
8612 bool maybe_const = true;
8613 bool npc;
4038c495 8614
0a880880 8615 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8616 {
3e4093b6
RS
8617 constructor_erroneous = 1;
8618 return;
8b6a5902 8619 }
46bdb9cf
JM
8620 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8621 && (TREE_CODE (value) == STRING_CST
8622 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8623 && !(TREE_CODE (value) == STRING_CST
8624 && TREE_CODE (type) == ARRAY_TYPE
8625 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8626 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8627 TYPE_MAIN_VARIANT (type)))
c2255bc4 8628 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8629
3e4093b6 8630 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8631 && require_constant_value && pending)
8b6a5902 8632 {
3e4093b6
RS
8633 /* As an extension, allow initializing objects with static storage
8634 duration with compound literals (which are then treated just as
8635 the brace enclosed list they contain). */
4435bb92
MP
8636 if (flag_isoc99)
8637 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8638 "constant");
3e4093b6
RS
8639 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8640 value = DECL_INITIAL (decl);
8b6a5902
JJ
8641 }
8642
928c19bb 8643 npc = null_pointer_constant_p (value);
8ce94e44
JM
8644 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8645 {
8646 semantic_type = TREE_TYPE (value);
8647 value = TREE_OPERAND (value, 0);
8648 }
928c19bb
JM
8649 value = c_fully_fold (value, require_constant_value, &maybe_const);
8650
3e4093b6
RS
8651 if (value == error_mark_node)
8652 constructor_erroneous = 1;
8653 else if (!TREE_CONSTANT (value))
8654 constructor_constant = 0;
ee45a32d
EB
8655 else if (!initializer_constant_valid_p (value,
8656 TREE_TYPE (value),
8657 AGGREGATE_TYPE_P (constructor_type)
8658 && TYPE_REVERSE_STORAGE_ORDER
8659 (constructor_type))
3e636daf 8660 || (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6
RS
8661 && DECL_C_BIT_FIELD (field)
8662 && TREE_CODE (value) != INTEGER_CST))
8663 constructor_simple = 0;
928c19bb
JM
8664 if (!maybe_const)
8665 constructor_nonconst = 1;
3e4093b6 8666
116df786 8667 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8668 {
116df786
RH
8669 if (require_constant_value)
8670 {
ea58ef42 8671 error_init (loc, "initializer element is not constant");
116df786
RH
8672 value = error_mark_node;
8673 }
8674 else if (require_constant_elements)
8337d1db 8675 pedwarn (loc, OPT_Wpedantic,
509c9d60 8676 "initializer element is not computable at load time");
8b6a5902 8677 }
928c19bb
JM
8678 else if (!maybe_const
8679 && (require_constant_value || require_constant_elements))
8337d1db 8680 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8681 "initializer element is not a constant expression");
3e4093b6 8682
81f40b79
ILT
8683 /* Issue -Wc++-compat warnings about initializing a bitfield with
8684 enum type. */
8685 if (warn_cxx_compat
8686 && field != NULL_TREE
8687 && TREE_CODE (field) == FIELD_DECL
8688 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8689 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8690 != TYPE_MAIN_VARIANT (type))
8691 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8692 {
8693 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8694 if (checktype != error_mark_node
8695 && (TYPE_MAIN_VARIANT (checktype)
8696 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8697 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8698 "enum conversion in initialization is invalid in C++");
8699 }
8700
3e4093b6
RS
8701 /* If this field is empty (and not at the end of structure),
8702 don't do anything other than checking the initializer. */
8703 if (field
8704 && (TREE_TYPE (field) == error_mark_node
8705 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8706 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8707 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8708 || DECL_CHAIN (field)))))
3e4093b6
RS
8709 return;
8710
8ce94e44
JM
8711 if (semantic_type)
8712 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8713 value = digest_init (loc, type, value, origtype, npc, strict_string,
8714 require_constant_value);
3e4093b6 8715 if (value == error_mark_node)
8b6a5902 8716 {
3e4093b6
RS
8717 constructor_erroneous = 1;
8718 return;
8b6a5902 8719 }
928c19bb
JM
8720 if (require_constant_value || require_constant_elements)
8721 constant_expression_warning (value);
8b6a5902 8722
3e4093b6
RS
8723 /* If this element doesn't come next in sequence,
8724 put it on constructor_pending_elts. */
8725 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8726 && (!constructor_incremental
8727 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8728 {
3e4093b6
RS
8729 if (constructor_incremental
8730 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8731 set_nonincremental_init (braced_init_obstack);
3e4093b6 8732
96b40f8d 8733 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8734 braced_init_obstack);
3e4093b6 8735 return;
8b6a5902 8736 }
3e4093b6
RS
8737 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8738 && (!constructor_incremental
8739 || field != constructor_unfilled_fields))
8b6a5902 8740 {
3e4093b6
RS
8741 /* We do this for records but not for unions. In a union,
8742 no matter which field is specified, it can be initialized
8743 right away since it starts at the beginning of the union. */
8744 if (constructor_incremental)
8745 {
8746 if (!constructor_unfilled_fields)
a1e3b3d9 8747 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8748 else
8749 {
8750 tree bitpos, unfillpos;
8751
8752 bitpos = bit_position (field);
8753 unfillpos = bit_position (constructor_unfilled_fields);
8754
8755 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8756 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8757 }
8758 }
8759
96b40f8d 8760 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8761 braced_init_obstack);
3e4093b6 8762 return;
8b6a5902 8763 }
3e4093b6 8764 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8765 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8766 {
b295aee2
JJ
8767 if (!implicit)
8768 {
9771b263 8769 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
755e528f 8770 warning_init (loc, OPT_Woverride_init_side_effects,
b295aee2
JJ
8771 "initialized field with side-effects overwritten");
8772 else if (warn_override_init)
96b40f8d
MP
8773 warning_init (loc, OPT_Woverride_init,
8774 "initialized field overwritten");
b295aee2 8775 }
8b6a5902 8776
3e4093b6 8777 /* We can have just one union field set. */
9771b263 8778 constructor_elements = NULL;
3e4093b6 8779 }
8b6a5902 8780
3e4093b6
RS
8781 /* Otherwise, output this element either to
8782 constructor_elements or to the assembler file. */
8b6a5902 8783
f32682ca 8784 constructor_elt celt = {field, value};
9771b263 8785 vec_safe_push (constructor_elements, celt);
8b6a5902 8786
3e4093b6
RS
8787 /* Advance the variable that indicates sequential elements output. */
8788 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8789 constructor_unfilled_index
db3927fb
AH
8790 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8791 bitsize_one_node);
3e4093b6
RS
8792 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8793 {
8794 constructor_unfilled_fields
910ad8de 8795 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8796
3e4093b6
RS
8797 /* Skip any nameless bit fields. */
8798 while (constructor_unfilled_fields != 0
8799 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8800 && DECL_NAME (constructor_unfilled_fields) == 0)
8801 constructor_unfilled_fields =
910ad8de 8802 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8803 }
8804 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8805 constructor_unfilled_fields = 0;
de520661 8806
3e4093b6
RS
8807 /* Now output any pending elements which have become next. */
8808 if (pending)
a1e3b3d9 8809 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8810}
8b6a5902 8811
3e4093b6
RS
8812/* Output any pending elements which have become next.
8813 As we output elements, constructor_unfilled_{fields,index}
8814 advances, which may cause other elements to become next;
8815 if so, they too are output.
8b6a5902 8816
3e4093b6
RS
8817 If ALL is 0, we return when there are
8818 no more pending elements to output now.
665f2503 8819
3e4093b6
RS
8820 If ALL is 1, we output space as necessary so that
8821 we can output all the pending elements. */
3e4093b6 8822static void
a1e3b3d9 8823output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8824{
8825 struct init_node *elt = constructor_pending_elts;
8826 tree next;
de520661 8827
3e4093b6
RS
8828 retry:
8829
ba228239 8830 /* Look through the whole pending tree.
3e4093b6
RS
8831 If we find an element that should be output now,
8832 output it. Otherwise, set NEXT to the element
8833 that comes first among those still pending. */
8834
8835 next = 0;
8836 while (elt)
8837 {
8838 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8839 {
3e4093b6
RS
8840 if (tree_int_cst_equal (elt->purpose,
8841 constructor_unfilled_index))
34cf811f
MP
8842 output_init_element (input_location, elt->value, elt->origtype,
8843 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8844 constructor_unfilled_index, 0, false,
8845 braced_init_obstack);
3e4093b6
RS
8846 else if (tree_int_cst_lt (constructor_unfilled_index,
8847 elt->purpose))
8b6a5902 8848 {
3e4093b6
RS
8849 /* Advance to the next smaller node. */
8850 if (elt->left)
8851 elt = elt->left;
8852 else
8853 {
8854 /* We have reached the smallest node bigger than the
8855 current unfilled index. Fill the space first. */
8856 next = elt->purpose;
8857 break;
8858 }
8b6a5902 8859 }
ce662d4c
JJ
8860 else
8861 {
3e4093b6
RS
8862 /* Advance to the next bigger node. */
8863 if (elt->right)
8864 elt = elt->right;
8865 else
ce662d4c 8866 {
3e4093b6
RS
8867 /* We have reached the biggest node in a subtree. Find
8868 the parent of it, which is the next bigger node. */
8869 while (elt->parent && elt->parent->right == elt)
8870 elt = elt->parent;
8871 elt = elt->parent;
8872 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8873 elt->purpose))
8874 {
8875 next = elt->purpose;
8876 break;
8877 }
ce662d4c
JJ
8878 }
8879 }
8b6a5902 8880 }
3e636daf 8881 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
8882 {
8883 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8884
3e4093b6
RS
8885 /* If the current record is complete we are done. */
8886 if (constructor_unfilled_fields == 0)
8887 break;
de520661 8888
3e4093b6
RS
8889 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8890 elt_bitpos = bit_position (elt->purpose);
8891 /* We can't compare fields here because there might be empty
8892 fields in between. */
8893 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8894 {
8895 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8896 output_init_element (input_location, elt->value, elt->origtype,
8897 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8898 elt->purpose, 0, false,
8899 braced_init_obstack);
3e4093b6
RS
8900 }
8901 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8902 {
8903 /* Advance to the next smaller node. */
8904 if (elt->left)
8905 elt = elt->left;
8906 else
8907 {
8908 /* We have reached the smallest node bigger than the
8909 current unfilled field. Fill the space first. */
8910 next = elt->purpose;
8911 break;
8912 }
8913 }
8914 else
8915 {
8916 /* Advance to the next bigger node. */
8917 if (elt->right)
8918 elt = elt->right;
8919 else
8920 {
8921 /* We have reached the biggest node in a subtree. Find
8922 the parent of it, which is the next bigger node. */
8923 while (elt->parent && elt->parent->right == elt)
8924 elt = elt->parent;
8925 elt = elt->parent;
8926 if (elt
8927 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8928 bit_position (elt->purpose))))
8929 {
8930 next = elt->purpose;
8931 break;
8932 }
8933 }
8934 }
8935 }
8936 }
de520661 8937
3e4093b6
RS
8938 /* Ordinarily return, but not if we want to output all
8939 and there are elements left. */
3f75a254 8940 if (!(all && next != 0))
e5cfb88f
RK
8941 return;
8942
3e4093b6
RS
8943 /* If it's not incremental, just skip over the gap, so that after
8944 jumping to retry we will output the next successive element. */
3e636daf 8945 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
8946 constructor_unfilled_fields = next;
8947 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8948 constructor_unfilled_index = next;
de520661 8949
3e4093b6
RS
8950 /* ELT now points to the node in the pending tree with the next
8951 initializer to output. */
8952 goto retry;
de520661
RS
8953}
8954\f
3e4093b6
RS
8955/* Add one non-braced element to the current constructor level.
8956 This adjusts the current position within the constructor's type.
8957 This may also start or terminate implicit levels
8958 to handle a partly-braced initializer.
e5e809f4 8959
3e4093b6 8960 Once this has found the correct level for the new element,
b295aee2
JJ
8961 it calls output_init_element.
8962
8963 IMPLICIT is true if value comes from pop_init_level (1),
8964 the new initializer has been merged with the existing one
8965 and thus no warnings should be emitted about overriding an
8966 existing initializer. */
3e4093b6
RS
8967
8968void
34cf811f 8969process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 8970 struct obstack * braced_init_obstack)
e5e809f4 8971{
916c5919
JM
8972 tree orig_value = value.value;
8973 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8974 bool strict_string = value.original_code == STRING_CST;
340baef7 8975 bool was_designated = designator_depth != 0;
e5e809f4 8976
3e4093b6 8977 designator_depth = 0;
b06df647 8978 designator_erroneous = 0;
e5e809f4 8979
9bac5cbb
G
8980 if (!implicit && value.value && !integer_zerop (value.value))
8981 constructor_zeroinit = 0;
8982
3e4093b6
RS
8983 /* Handle superfluous braces around string cst as in
8984 char x[] = {"foo"}; */
8985 if (string_flag
8986 && constructor_type
340baef7 8987 && !was_designated
3e4093b6 8988 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8989 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8990 && integer_zerop (constructor_unfilled_index))
e5e809f4 8991 {
916c5919 8992 if (constructor_stack->replacement_value.value)
ea58ef42 8993 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
8994 constructor_stack->replacement_value = value;
8995 return;
e5e809f4 8996 }
8b6a5902 8997
916c5919 8998 if (constructor_stack->replacement_value.value != 0)
3e4093b6 8999 {
ea58ef42 9000 error_init (loc, "excess elements in struct initializer");
3e4093b6 9001 return;
e5e809f4
JL
9002 }
9003
3e4093b6
RS
9004 /* Ignore elements of a brace group if it is entirely superfluous
9005 and has already been diagnosed. */
9006 if (constructor_type == 0)
9007 return;
e5e809f4 9008
976d5a22
TT
9009 if (!implicit && warn_designated_init && !was_designated
9010 && TREE_CODE (constructor_type) == RECORD_TYPE
9011 && lookup_attribute ("designated_init",
9012 TYPE_ATTRIBUTES (constructor_type)))
9013 warning_init (loc,
9014 OPT_Wdesignated_init,
9015 "positional initialization of field "
9016 "in %<struct%> declared with %<designated_init%> attribute");
9017
3e4093b6
RS
9018 /* If we've exhausted any levels that didn't have braces,
9019 pop them now. */
9020 while (constructor_stack->implicit)
9021 {
3e636daf 9022 if (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 9023 && constructor_fields == 0)
ea58ef42
MP
9024 process_init_element (loc,
9025 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 9026 true, braced_init_obstack);
53650abe 9027 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
31521951 9028 || VECTOR_TYPE_P (constructor_type))
8824edff
JJ
9029 && constructor_max_index
9030 && tree_int_cst_lt (constructor_max_index,
9031 constructor_index))
ea58ef42
MP
9032 process_init_element (loc,
9033 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 9034 true, braced_init_obstack);
3e4093b6
RS
9035 else
9036 break;
9037 }
e5e809f4 9038
3e4093b6
RS
9039 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9040 if (constructor_range_stack)
e5e809f4 9041 {
3e4093b6
RS
9042 /* If value is a compound literal and we'll be just using its
9043 content, don't put it into a SAVE_EXPR. */
916c5919 9044 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
c3e38a03 9045 || !require_constant_value)
8ce94e44
JM
9046 {
9047 tree semantic_type = NULL_TREE;
9048 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9049 {
9050 semantic_type = TREE_TYPE (value.value);
9051 value.value = TREE_OPERAND (value.value, 0);
9052 }
9053 value.value = c_save_expr (value.value);
9054 if (semantic_type)
9055 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9056 value.value);
9057 }
3e4093b6 9058 }
e5e809f4 9059
3e4093b6
RS
9060 while (1)
9061 {
9062 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 9063 {
3e4093b6
RS
9064 tree fieldtype;
9065 enum tree_code fieldcode;
e5e809f4 9066
3e4093b6
RS
9067 if (constructor_fields == 0)
9068 {
ea58ef42 9069 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
9070 break;
9071 }
e5e809f4 9072
3e4093b6
RS
9073 fieldtype = TREE_TYPE (constructor_fields);
9074 if (fieldtype != error_mark_node)
9075 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9076 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9077
3e4093b6
RS
9078 /* Error for non-static initialization of a flexible array member. */
9079 if (fieldcode == ARRAY_TYPE
9080 && !require_constant_value
9081 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 9082 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 9083 {
ea58ef42
MP
9084 error_init (loc, "non-static initialization of a flexible "
9085 "array member");
3e4093b6
RS
9086 break;
9087 }
e5e809f4 9088
2cc901dc
MP
9089 /* Error for initialization of a flexible array member with
9090 a string constant if the structure is in an array. E.g.:
9091 struct S { int x; char y[]; };
9092 struct S s[] = { { 1, "foo" } };
9093 is invalid. */
9094 if (string_flag
9095 && fieldcode == ARRAY_TYPE
9096 && constructor_depth > 1
9097 && TYPE_SIZE (fieldtype) == NULL_TREE
9098 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9099 {
9100 bool in_array_p = false;
9101 for (struct constructor_stack *p = constructor_stack;
9102 p && p->type; p = p->next)
9103 if (TREE_CODE (p->type) == ARRAY_TYPE)
9104 {
9105 in_array_p = true;
9106 break;
9107 }
9108 if (in_array_p)
9109 {
9110 error_init (loc, "initialization of flexible array "
9111 "member in a nested context");
9112 break;
9113 }
9114 }
9115
3e4093b6 9116 /* Accept a string constant to initialize a subarray. */
916c5919 9117 if (value.value != 0
3e4093b6 9118 && fieldcode == ARRAY_TYPE
197463ae 9119 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9120 && string_flag)
916c5919 9121 value.value = orig_value;
3e4093b6
RS
9122 /* Otherwise, if we have come to a subaggregate,
9123 and we don't have an element of its type, push into it. */
0953878d 9124 else if (value.value != 0
916c5919
JM
9125 && value.value != error_mark_node
9126 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9127 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9128 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9129 {
ea58ef42 9130 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9131 continue;
9132 }
e5e809f4 9133
916c5919 9134 if (value.value)
3e4093b6
RS
9135 {
9136 push_member_name (constructor_fields);
34cf811f 9137 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9138 strict_string, fieldtype,
a1e3b3d9
LB
9139 constructor_fields, 1, implicit,
9140 braced_init_obstack);
3e4093b6 9141 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9142 }
9143 else
3e4093b6
RS
9144 /* Do the bookkeeping for an element that was
9145 directly output as a constructor. */
e5e809f4 9146 {
3e4093b6
RS
9147 /* For a record, keep track of end position of last field. */
9148 if (DECL_SIZE (constructor_fields))
c22cacf3 9149 constructor_bit_index
db3927fb
AH
9150 = size_binop_loc (input_location, PLUS_EXPR,
9151 bit_position (constructor_fields),
9152 DECL_SIZE (constructor_fields));
3e4093b6
RS
9153
9154 /* If the current field was the first one not yet written out,
9155 it isn't now, so update. */
9156 if (constructor_unfilled_fields == constructor_fields)
9157 {
910ad8de 9158 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9159 /* Skip any nameless bit fields. */
9160 while (constructor_unfilled_fields != 0
9161 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9162 && DECL_NAME (constructor_unfilled_fields) == 0)
9163 constructor_unfilled_fields =
910ad8de 9164 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 9165 }
e5e809f4 9166 }
3e4093b6 9167
910ad8de 9168 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9169 /* Skip any nameless bit fields at the beginning. */
9170 while (constructor_fields != 0
9171 && DECL_C_BIT_FIELD (constructor_fields)
9172 && DECL_NAME (constructor_fields) == 0)
910ad8de 9173 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9174 }
3e4093b6 9175 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 9176 {
3e4093b6
RS
9177 tree fieldtype;
9178 enum tree_code fieldcode;
e5e809f4 9179
3e4093b6
RS
9180 if (constructor_fields == 0)
9181 {
d033409e 9182 pedwarn_init (loc, 0,
509c9d60 9183 "excess elements in union initializer");
3e4093b6
RS
9184 break;
9185 }
e5e809f4 9186
3e4093b6
RS
9187 fieldtype = TREE_TYPE (constructor_fields);
9188 if (fieldtype != error_mark_node)
9189 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9190 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9191
3e4093b6
RS
9192 /* Warn that traditional C rejects initialization of unions.
9193 We skip the warning if the value is zero. This is done
9194 under the assumption that the zero initializer in user
9195 code appears conditioned on e.g. __STDC__ to avoid
9196 "missing initializer" warnings and relies on default
9197 initialization to zero in the traditional C case.
9198 We also skip the warning if the initializer is designated,
9199 again on the assumption that this must be conditional on
9200 __STDC__ anyway (and we've already complained about the
9201 member-designator already). */
8400e75e 9202 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
9203 && !(value.value && (integer_zerop (value.value)
9204 || real_zerop (value.value))))
3176a0c2
DD
9205 warning (OPT_Wtraditional, "traditional C rejects initialization "
9206 "of unions");
e5e809f4 9207
3e4093b6 9208 /* Accept a string constant to initialize a subarray. */
916c5919 9209 if (value.value != 0
3e4093b6 9210 && fieldcode == ARRAY_TYPE
197463ae 9211 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9212 && string_flag)
916c5919 9213 value.value = orig_value;
3e4093b6
RS
9214 /* Otherwise, if we have come to a subaggregate,
9215 and we don't have an element of its type, push into it. */
0953878d 9216 else if (value.value != 0
916c5919
JM
9217 && value.value != error_mark_node
9218 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9219 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9220 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9221 {
ea58ef42 9222 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9223 continue;
9224 }
e5e809f4 9225
916c5919 9226 if (value.value)
3e4093b6
RS
9227 {
9228 push_member_name (constructor_fields);
34cf811f 9229 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9230 strict_string, fieldtype,
a1e3b3d9
LB
9231 constructor_fields, 1, implicit,
9232 braced_init_obstack);
3e4093b6 9233 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9234 }
9235 else
3e4093b6
RS
9236 /* Do the bookkeeping for an element that was
9237 directly output as a constructor. */
e5e809f4 9238 {
3e4093b6 9239 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 9240 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9241 }
e5e809f4 9242
3e4093b6
RS
9243 constructor_fields = 0;
9244 }
9245 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9246 {
9247 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9248 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 9249
3e4093b6 9250 /* Accept a string constant to initialize a subarray. */
916c5919 9251 if (value.value != 0
3e4093b6 9252 && eltcode == ARRAY_TYPE
197463ae 9253 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 9254 && string_flag)
916c5919 9255 value.value = orig_value;
3e4093b6
RS
9256 /* Otherwise, if we have come to a subaggregate,
9257 and we don't have an element of its type, push into it. */
0953878d 9258 else if (value.value != 0
916c5919
JM
9259 && value.value != error_mark_node
9260 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9261 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9262 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9263 {
ea58ef42 9264 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9265 continue;
9266 }
8b6a5902 9267
3e4093b6
RS
9268 if (constructor_max_index != 0
9269 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9270 || integer_all_onesp (constructor_max_index)))
9271 {
d033409e 9272 pedwarn_init (loc, 0,
509c9d60 9273 "excess elements in array initializer");
3e4093b6
RS
9274 break;
9275 }
8b6a5902 9276
3e4093b6 9277 /* Now output the actual element. */
916c5919 9278 if (value.value)
3e4093b6 9279 {
ae7e9ddd 9280 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9281 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9282 strict_string, elttype,
a1e3b3d9
LB
9283 constructor_index, 1, implicit,
9284 braced_init_obstack);
3e4093b6
RS
9285 RESTORE_SPELLING_DEPTH (constructor_depth);
9286 }
2f6e4e97 9287
3e4093b6 9288 constructor_index
db3927fb
AH
9289 = size_binop_loc (input_location, PLUS_EXPR,
9290 constructor_index, bitsize_one_node);
8b6a5902 9291
916c5919 9292 if (!value.value)
3e4093b6
RS
9293 /* If we are doing the bookkeeping for an element that was
9294 directly output as a constructor, we must update
9295 constructor_unfilled_index. */
9296 constructor_unfilled_index = constructor_index;
9297 }
31521951 9298 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
9299 {
9300 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9301
c22cacf3
MS
9302 /* Do a basic check of initializer size. Note that vectors
9303 always have a fixed size derived from their type. */
3e4093b6
RS
9304 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9305 {
d033409e 9306 pedwarn_init (loc, 0,
509c9d60 9307 "excess elements in vector initializer");
3e4093b6
RS
9308 break;
9309 }
8b6a5902 9310
3e4093b6 9311 /* Now output the actual element. */
916c5919 9312 if (value.value)
53650abe
AP
9313 {
9314 if (TREE_CODE (value.value) == VECTOR_CST)
9315 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9316 output_init_element (loc, value.value, value.original_type,
53650abe 9317 strict_string, elttype,
a1e3b3d9
LB
9318 constructor_index, 1, implicit,
9319 braced_init_obstack);
53650abe 9320 }
8b6a5902 9321
3e4093b6 9322 constructor_index
db3927fb
AH
9323 = size_binop_loc (input_location,
9324 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9325
916c5919 9326 if (!value.value)
3e4093b6
RS
9327 /* If we are doing the bookkeeping for an element that was
9328 directly output as a constructor, we must update
9329 constructor_unfilled_index. */
9330 constructor_unfilled_index = constructor_index;
9331 }
8b6a5902 9332
3e4093b6
RS
9333 /* Handle the sole element allowed in a braced initializer
9334 for a scalar variable. */
b4519d39
SB
9335 else if (constructor_type != error_mark_node
9336 && constructor_fields == 0)
8b6a5902 9337 {
d033409e 9338 pedwarn_init (loc, 0,
509c9d60 9339 "excess elements in scalar initializer");
3e4093b6 9340 break;
8b6a5902
JJ
9341 }
9342 else
9343 {
916c5919 9344 if (value.value)
34cf811f 9345 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9346 strict_string, constructor_type,
a1e3b3d9
LB
9347 NULL_TREE, 1, implicit,
9348 braced_init_obstack);
3e4093b6 9349 constructor_fields = 0;
8b6a5902
JJ
9350 }
9351
3e4093b6
RS
9352 /* Handle range initializers either at this level or anywhere higher
9353 in the designator stack. */
9354 if (constructor_range_stack)
8b6a5902 9355 {
3e4093b6
RS
9356 struct constructor_range_stack *p, *range_stack;
9357 int finish = 0;
9358
9359 range_stack = constructor_range_stack;
9360 constructor_range_stack = 0;
9361 while (constructor_stack != range_stack->stack)
8b6a5902 9362 {
366de0ce 9363 gcc_assert (constructor_stack->implicit);
34cf811f 9364 process_init_element (loc,
ea58ef42
MP
9365 pop_init_level (loc, 1,
9366 braced_init_obstack),
a1e3b3d9 9367 true, braced_init_obstack);
8b6a5902 9368 }
3e4093b6
RS
9369 for (p = range_stack;
9370 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9371 p = p->prev)
8b6a5902 9372 {
366de0ce 9373 gcc_assert (constructor_stack->implicit);
34cf811f 9374 process_init_element (loc,
ea58ef42
MP
9375 pop_init_level (loc, 1,
9376 braced_init_obstack),
a1e3b3d9 9377 true, braced_init_obstack);
8b6a5902 9378 }
3e4093b6 9379
db3927fb
AH
9380 p->index = size_binop_loc (input_location,
9381 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9382 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9383 finish = 1;
9384
9385 while (1)
9386 {
9387 constructor_index = p->index;
9388 constructor_fields = p->fields;
9389 if (finish && p->range_end && p->index == p->range_start)
9390 {
9391 finish = 0;
9392 p->prev = 0;
9393 }
9394 p = p->next;
9395 if (!p)
9396 break;
ea58ef42 9397 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9398 p->stack = constructor_stack;
9399 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9400 p->index = p->range_start;
9401 }
9402
9403 if (!finish)
9404 constructor_range_stack = range_stack;
9405 continue;
8b6a5902
JJ
9406 }
9407
3e4093b6 9408 break;
8b6a5902
JJ
9409 }
9410
3e4093b6
RS
9411 constructor_range_stack = 0;
9412}
9413\f
9f0e2d86
ZW
9414/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9415 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9416 an ASM_EXPR node). */
3e4093b6 9417tree
9f0e2d86 9418build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9419{
6de9cd9a
DN
9420 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9421 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9422 return add_stmt (args);
8b6a5902
JJ
9423}
9424
9f0e2d86
ZW
9425/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9426 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9427 SIMPLE indicates whether there was anything at all after the
9428 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9429 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9430tree
c2255bc4 9431build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9432 tree clobbers, tree labels, bool simple)
e5e809f4 9433{
3e4093b6 9434 tree tail;
9f0e2d86 9435 tree args;
6de9cd9a
DN
9436 int i;
9437 const char *constraint;
74f0c611 9438 const char **oconstraints;
6de9cd9a 9439 bool allows_mem, allows_reg, is_inout;
74f0c611 9440 int ninputs, noutputs;
6de9cd9a
DN
9441
9442 ninputs = list_length (inputs);
9443 noutputs = list_length (outputs);
74f0c611
RH
9444 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9445
1c384bf1 9446 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9447
6de9cd9a
DN
9448 /* Remove output conversions that change the type but not the mode. */
9449 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9450 {
3e4093b6 9451 tree output = TREE_VALUE (tail);
74f0c611 9452
eadd3d0d
JJ
9453 output = c_fully_fold (output, false, NULL);
9454
74f0c611
RH
9455 /* ??? Really, this should not be here. Users should be using a
9456 proper lvalue, dammit. But there's a long history of using casts
9457 in the output operands. In cases like longlong.h, this becomes a
9458 primitive form of typechecking -- if the cast can be removed, then
9459 the output operand had a type of the proper width; otherwise we'll
9460 get an error. Gross, but ... */
3e4093b6 9461 STRIP_NOPS (output);
74f0c611 9462
7bd11157 9463 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9464 output = error_mark_node;
8b6a5902 9465
5544530a
PB
9466 if (output != error_mark_node
9467 && (TREE_READONLY (output)
9468 || TYPE_READONLY (TREE_TYPE (output))
3e636daf 9469 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
5544530a 9470 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9471 readonly_error (loc, output, lv_asm);
5544530a 9472
6de9cd9a 9473 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9474 oconstraints[i] = constraint;
9475
9476 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9477 &allows_mem, &allows_reg, &is_inout))
9478 {
9479 /* If the operand is going to end up in memory,
9480 mark it addressable. */
9481 if (!allows_reg && !c_mark_addressable (output))
9482 output = error_mark_node;
bae5cddf
JJ
9483 if (!(!allows_reg && allows_mem)
9484 && output != error_mark_node
9485 && VOID_TYPE_P (TREE_TYPE (output)))
9486 {
9487 error_at (loc, "invalid use of void expression");
9488 output = error_mark_node;
9489 }
74f0c611
RH
9490 }
9491 else
c22cacf3 9492 output = error_mark_node;
3e4093b6 9493
74f0c611 9494 TREE_VALUE (tail) = output;
8b6a5902 9495 }
3e4093b6 9496
74f0c611
RH
9497 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9498 {
9499 tree input;
9500
9501 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9502 input = TREE_VALUE (tail);
9503
74f0c611
RH
9504 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9505 oconstraints, &allows_mem, &allows_reg))
9506 {
9507 /* If the operand is going to end up in memory,
9508 mark it addressable. */
b4c33883
AP
9509 if (!allows_reg && allows_mem)
9510 {
eadd3d0d
JJ
9511 input = c_fully_fold (input, false, NULL);
9512
b4c33883
AP
9513 /* Strip the nops as we allow this case. FIXME, this really
9514 should be rejected or made deprecated. */
9515 STRIP_NOPS (input);
9516 if (!c_mark_addressable (input))
9517 input = error_mark_node;
bae5cddf 9518 }
eadd3d0d 9519 else
bae5cddf 9520 {
eadd3d0d
JJ
9521 struct c_expr expr;
9522 memset (&expr, 0, sizeof (expr));
9523 expr.value = input;
267bac10 9524 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9525 input = c_fully_fold (expr.value, false, NULL);
9526
9527 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9528 {
9529 error_at (loc, "invalid use of void expression");
9530 input = error_mark_node;
9531 }
bae5cddf 9532 }
74f0c611
RH
9533 }
9534 else
9535 input = error_mark_node;
9536
9537 TREE_VALUE (tail) = input;
9538 }
3e4093b6 9539
1c384bf1
RH
9540 /* ASMs with labels cannot have outputs. This should have been
9541 enforced by the parser. */
9542 gcc_assert (outputs == NULL || labels == NULL);
9543
9544 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9545
5544530a
PB
9546 /* asm statements without outputs, including simple ones, are treated
9547 as volatile. */
9548 ASM_INPUT_P (args) = simple;
9549 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9550
9f0e2d86 9551 return args;
e5e809f4 9552}
3e4093b6 9553\f
c2255bc4
AH
9554/* Generate a goto statement to LABEL. LOC is the location of the
9555 GOTO. */
506e2710
RH
9556
9557tree
c2255bc4 9558c_finish_goto_label (location_t loc, tree label)
506e2710 9559{
e1b7793c 9560 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9561 if (!decl)
9562 return NULL_TREE;
506e2710 9563 TREE_USED (decl) = 1;
c2255bc4
AH
9564 {
9565 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9566 SET_EXPR_LOCATION (t, loc);
9567 return add_stmt (t);
9568 }
506e2710
RH
9569}
9570
c2255bc4
AH
9571/* Generate a computed goto statement to EXPR. LOC is the location of
9572 the GOTO. */
506e2710
RH
9573
9574tree
c2255bc4 9575c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9576{
c2255bc4 9577 tree t;
c1771a20 9578 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9579 expr = c_fully_fold (expr, false, NULL);
506e2710 9580 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9581 t = build1 (GOTO_EXPR, void_type_node, expr);
9582 SET_EXPR_LOCATION (t, loc);
9583 return add_stmt (t);
506e2710
RH
9584}
9585
5088b058 9586/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9587 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9588 the location of the return statement, or the location of the expression,
9589 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9590 is the original type of RETVAL. */
de520661 9591
506e2710 9592tree
c2255bc4 9593c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9594{
0c9b182b
JJ
9595 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9596 bool no_warning = false;
928c19bb 9597 bool npc = false;
36536d79 9598 size_t rank = 0;
3e4093b6 9599
de8ddd5f
MP
9600 /* Use the expansion point to handle cases such as returning NULL
9601 in a function returning void. */
9602 source_location xloc = expansion_point_location_if_in_system_header (loc);
9603
3e4093b6 9604 if (TREE_THIS_VOLATILE (current_function_decl))
de8ddd5f 9605 warning_at (xloc, 0,
c2255bc4 9606 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9607
b72271b9 9608 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9609 {
9610 /* Array notations are allowed in a return statement if it is inside a
9611 built-in array notation reduction function. */
9612 if (!find_rank (loc, retval, retval, false, &rank))
9613 return error_mark_node;
9614 if (rank >= 1)
9615 {
9616 error_at (loc, "array notation expression cannot be used as a "
9617 "return value");
9618 return error_mark_node;
9619 }
9620 }
3af9c5e9 9621 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9622 {
9623 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9624 "allowed");
9625 return error_mark_node;
9626 }
928c19bb
JM
9627 if (retval)
9628 {
8ce94e44 9629 tree semantic_type = NULL_TREE;
928c19bb 9630 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9631 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9632 {
9633 semantic_type = TREE_TYPE (retval);
9634 retval = TREE_OPERAND (retval, 0);
9635 }
928c19bb 9636 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9637 if (semantic_type)
9638 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9639 }
9640
3e4093b6 9641 if (!retval)
de520661 9642 {
3e4093b6
RS
9643 current_function_returns_null = 1;
9644 if ((warn_return_type || flag_isoc99)
9645 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9646 {
94c40e19 9647 bool warned_here;
35aff4fb 9648 if (flag_isoc99)
94c40e19
DM
9649 warned_here = pedwarn
9650 (loc, 0,
9651 "%<return%> with no value, in function returning non-void");
35aff4fb 9652 else
94c40e19
DM
9653 warned_here = warning_at
9654 (loc, OPT_Wreturn_type,
9655 "%<return%> with no value, in function returning non-void");
0c9b182b 9656 no_warning = true;
94c40e19
DM
9657 if (warned_here)
9658 inform (DECL_SOURCE_LOCATION (current_function_decl),
9659 "declared here");
0c9b182b 9660 }
400fbf9f 9661 }
3e4093b6 9662 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9663 {
3e4093b6 9664 current_function_returns_null = 1;
94c40e19 9665 bool warned_here;
2397c575 9666 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
94c40e19
DM
9667 warned_here = pedwarn
9668 (xloc, 0,
9669 "%<return%> with a value, in function returning void");
b8698a0f 9670 else
94c40e19
DM
9671 warned_here = pedwarn
9672 (xloc, OPT_Wpedantic, "ISO C forbids "
9673 "%<return%> with expression, in function returning void");
9674 if (warned_here)
9675 inform (DECL_SOURCE_LOCATION (current_function_decl),
9676 "declared here");
de520661 9677 }
3e4093b6 9678 else
de520661 9679 {
68fca595
MP
9680 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9681 retval, origtype, ic_return,
c2255bc4 9682 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9683 tree res = DECL_RESULT (current_function_decl);
9684 tree inner;
9feb29df 9685 bool save;
3e4093b6
RS
9686
9687 current_function_returns_value = 1;
9688 if (t == error_mark_node)
506e2710 9689 return NULL_TREE;
3e4093b6 9690
9feb29df
JJ
9691 save = in_late_binary_op;
9692 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
9693 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9694 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9695 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9696 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9697 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9feb29df 9698 in_late_binary_op = true;
3e4093b6 9699 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9700 in_late_binary_op = save;
3e4093b6
RS
9701
9702 /* Strip any conversions, additions, and subtractions, and see if
9703 we are returning the address of a local variable. Warn if so. */
9704 while (1)
8b6a5902 9705 {
3e4093b6 9706 switch (TREE_CODE (inner))
8b6a5902 9707 {
849421a3
JJ
9708 CASE_CONVERT:
9709 case NON_LVALUE_EXPR:
3e4093b6 9710 case PLUS_EXPR:
849421a3 9711 case POINTER_PLUS_EXPR:
3e4093b6
RS
9712 inner = TREE_OPERAND (inner, 0);
9713 continue;
9714
9715 case MINUS_EXPR:
9716 /* If the second operand of the MINUS_EXPR has a pointer
9717 type (or is converted from it), this may be valid, so
9718 don't give a warning. */
9719 {
9720 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9721
3f75a254 9722 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9723 && (CONVERT_EXPR_P (op1)
9724 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9725 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9726
3e4093b6
RS
9727 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9728 break;
8b6a5902 9729
3e4093b6
RS
9730 inner = TREE_OPERAND (inner, 0);
9731 continue;
9732 }
400fbf9f 9733
3e4093b6
RS
9734 case ADDR_EXPR:
9735 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9736
6615c446 9737 while (REFERENCE_CLASS_P (inner)
22d03525 9738 && !INDIRECT_REF_P (inner))
3e4093b6 9739 inner = TREE_OPERAND (inner, 0);
8b6a5902 9740
a2f1f4c3 9741 if (DECL_P (inner)
3f75a254
JM
9742 && !DECL_EXTERNAL (inner)
9743 && !TREE_STATIC (inner)
3e4093b6 9744 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9745 {
9746 if (TREE_CODE (inner) == LABEL_DECL)
9747 warning_at (loc, OPT_Wreturn_local_addr,
9748 "function returns address of label");
9749 else
b4dfdc11
MG
9750 {
9751 warning_at (loc, OPT_Wreturn_local_addr,
9752 "function returns address of local variable");
9753 tree zero = build_zero_cst (TREE_TYPE (res));
9754 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9755 }
19fc9faa 9756 }
3e4093b6 9757 break;
8b6a5902 9758
3e4093b6
RS
9759 default:
9760 break;
9761 }
de520661 9762
3e4093b6
RS
9763 break;
9764 }
9765
53fb4de3 9766 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9767 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9768
9769 if (warn_sequence_point)
9770 verify_sequence_points (retval);
de520661 9771 }
8b6a5902 9772
c2255bc4 9773 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9774 TREE_NO_WARNING (ret_stmt) |= no_warning;
9775 return add_stmt (ret_stmt);
de520661 9776}
3e4093b6
RS
9777\f
9778struct c_switch {
604f5adf
ILT
9779 /* The SWITCH_EXPR being built. */
9780 tree switch_expr;
a6c0a76c 9781
89dbed81 9782 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9783 default conversion is applied. */
9784 tree orig_type;
9785
3e4093b6
RS
9786 /* A splay-tree mapping the low element of a case range to the high
9787 element, or NULL_TREE if there is no high element. Used to
9788 determine whether or not a new case label duplicates an old case
9789 label. We need a tree, rather than simply a hash table, because
9790 of the GNU case range extension. */
9791 splay_tree cases;
a6c0a76c 9792
e1b7793c
ILT
9793 /* The bindings at the point of the switch. This is used for
9794 warnings crossing decls when branching to a case label. */
9795 struct c_spot_bindings *bindings;
187230a7 9796
3e4093b6
RS
9797 /* The next node on the stack. */
9798 struct c_switch *next;
b155cfd9
MP
9799
9800 /* Remember whether the controlling expression had boolean type
9801 before integer promotions for the sake of -Wswitch-bool. */
9802 bool bool_cond_p;
9803
9804 /* Remember whether there was a case value that is outside the
9805 range of the ORIG_TYPE. */
9806 bool outside_range_p;
3e4093b6 9807};
400fbf9f 9808
3e4093b6
RS
9809/* A stack of the currently active switch statements. The innermost
9810 switch statement is on the top of the stack. There is no need to
9811 mark the stack for garbage collection because it is only active
9812 during the processing of the body of a function, and we never
9813 collect at that point. */
de520661 9814
506e2710 9815struct c_switch *c_switch_stack;
de520661 9816
3e4093b6 9817/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9818 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef 9819 SWITCH_COND_LOC is the location of the switch's condition.
b155cfd9 9820 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
de520661 9821
3e4093b6 9822tree
c2255bc4
AH
9823c_start_case (location_t switch_loc,
9824 location_t switch_cond_loc,
fedfecef 9825 tree exp, bool explicit_cast_p)
de520661 9826{
c58e8676 9827 tree orig_type = error_mark_node;
b155cfd9 9828 bool bool_cond_p = false;
3e4093b6 9829 struct c_switch *cs;
2f6e4e97 9830
3e4093b6 9831 if (exp != error_mark_node)
de520661 9832 {
3e4093b6
RS
9833 orig_type = TREE_TYPE (exp);
9834
c58e8676 9835 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9836 {
c58e8676
VR
9837 if (orig_type != error_mark_node)
9838 {
c2255bc4 9839 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9840 orig_type = error_mark_node;
9841 }
3e4093b6 9842 exp = integer_zero_node;
de520661 9843 }
3e4093b6 9844 else
de520661 9845 {
c58e8676 9846 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9847 tree e = exp;
9848
9849 /* Warn if the condition has boolean value. */
9850 while (TREE_CODE (e) == COMPOUND_EXPR)
9851 e = TREE_OPERAND (e, 1);
9852
9853 if ((TREE_CODE (type) == BOOLEAN_TYPE
9854 || truth_value_p (TREE_CODE (e)))
9855 /* Explicit cast to int suppresses this warning. */
9856 && !(TREE_CODE (type) == INTEGER_TYPE
9857 && explicit_cast_p))
b155cfd9 9858 bool_cond_p = true;
8b6a5902 9859
8400e75e 9860 if (!in_system_header_at (input_location)
3e4093b6
RS
9861 && (type == long_integer_type_node
9862 || type == long_unsigned_type_node))
c2255bc4
AH
9863 warning_at (switch_cond_loc,
9864 OPT_Wtraditional, "%<long%> switch expression not "
9865 "converted to %<int%> in ISO C");
8b6a5902 9866
928c19bb 9867 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9868 exp = default_conversion (exp);
ca085fd7
MLI
9869
9870 if (warn_sequence_point)
9871 verify_sequence_points (exp);
3e4093b6
RS
9872 }
9873 }
9874
604f5adf 9875 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9876 cs = XNEW (struct c_switch);
604f5adf 9877 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9878 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9879 cs->orig_type = orig_type;
3e4093b6 9880 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9881 cs->bindings = c_get_switch_bindings ();
b155cfd9
MP
9882 cs->bool_cond_p = bool_cond_p;
9883 cs->outside_range_p = false;
506e2710
RH
9884 cs->next = c_switch_stack;
9885 c_switch_stack = cs;
3e4093b6 9886
604f5adf 9887 return add_stmt (cs->switch_expr);
3e4093b6
RS
9888}
9889
c2255bc4 9890/* Process a case label at location LOC. */
3e4093b6
RS
9891
9892tree
c2255bc4 9893do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9894{
9895 tree label = NULL_TREE;
9896
17cede2e
JM
9897 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9898 {
9899 low_value = c_fully_fold (low_value, false, NULL);
9900 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 9901 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
9902 "case label is not an integer constant expression");
9903 }
9904
9905 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9906 {
9907 high_value = c_fully_fold (high_value, false, NULL);
9908 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9909 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9910 "case label is not an integer constant expression");
9911 }
9912
e1b7793c 9913 if (c_switch_stack == NULL)
187230a7
JM
9914 {
9915 if (low_value)
e1b7793c 9916 error_at (loc, "case label not within a switch statement");
187230a7 9917 else
e1b7793c
ILT
9918 error_at (loc, "%<default%> label not within a switch statement");
9919 return NULL_TREE;
187230a7 9920 }
de520661 9921
e1b7793c
ILT
9922 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9923 EXPR_LOCATION (c_switch_stack->switch_expr),
9924 loc))
9925 return NULL_TREE;
9926
9927 label = c_add_case_label (loc, c_switch_stack->cases,
9928 SWITCH_COND (c_switch_stack->switch_expr),
9929 c_switch_stack->orig_type,
b155cfd9
MP
9930 low_value, high_value,
9931 &c_switch_stack->outside_range_p);
e1b7793c
ILT
9932 if (label == error_mark_node)
9933 label = NULL_TREE;
3e4093b6
RS
9934 return label;
9935}
de520661 9936
083e891e
MP
9937/* Finish the switch statement. TYPE is the original type of the
9938 controlling expression of the switch, or NULL_TREE. */
de520661 9939
3e4093b6 9940void
083e891e 9941c_finish_case (tree body, tree type)
3e4093b6 9942{
506e2710 9943 struct c_switch *cs = c_switch_stack;
fbc315db 9944 location_t switch_location;
3e4093b6 9945
604f5adf 9946 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9947
6de9cd9a 9948 /* Emit warnings as needed. */
c2255bc4 9949 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 9950 c_do_switch_warnings (cs->cases, switch_location,
083e891e 9951 type ? type : TREE_TYPE (cs->switch_expr),
b155cfd9
MP
9952 SWITCH_COND (cs->switch_expr),
9953 cs->bool_cond_p, cs->outside_range_p);
6de9cd9a 9954
3e4093b6 9955 /* Pop the stack. */
506e2710 9956 c_switch_stack = cs->next;
3e4093b6 9957 splay_tree_delete (cs->cases);
e1b7793c 9958 c_release_switch_bindings (cs->bindings);
5d038c4c 9959 XDELETE (cs);
de520661 9960}
325c3691 9961\f
506e2710
RH
9962/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9963 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9964 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9965 statement, and was not surrounded with parenthesis. */
325c3691 9966
9e51cf9d 9967void
506e2710
RH
9968c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9969 tree else_block, bool nested_if)
325c3691 9970{
506e2710 9971 tree stmt;
325c3691 9972
25c22937
BI
9973 /* If the condition has array notations, then the rank of the then_block and
9974 else_block must be either 0 or be equal to the rank of the condition. If
9975 the condition does not have array notations then break them up as it is
9976 broken up in a normal expression. */
b72271b9 9977 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9978 {
9979 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9980 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9981 return;
9982 if (then_block
9983 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9984 return;
9985 if (else_block
9986 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9987 return;
9988 if (cond_rank != then_rank && then_rank != 0)
9989 {
9990 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9991 " and the then-block");
9992 return;
9993 }
9994 else if (cond_rank != else_rank && else_rank != 0)
9995 {
9996 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9997 " and the else-block");
9998 return;
9999 }
10000 }
506e2710
RH
10001 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
10002 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 10003 {
506e2710 10004 tree inner_if = then_block;
16865eaa 10005
61ada8ae 10006 /* We know from the grammar productions that there is an IF nested
506e2710
RH
10007 within THEN_BLOCK. Due to labels and c99 conditional declarations,
10008 it might not be exactly THEN_BLOCK, but should be the last
10009 non-container statement within. */
10010 while (1)
10011 switch (TREE_CODE (inner_if))
10012 {
10013 case COND_EXPR:
10014 goto found;
10015 case BIND_EXPR:
10016 inner_if = BIND_EXPR_BODY (inner_if);
10017 break;
10018 case STATEMENT_LIST:
10019 inner_if = expr_last (then_block);
10020 break;
10021 case TRY_FINALLY_EXPR:
10022 case TRY_CATCH_EXPR:
10023 inner_if = TREE_OPERAND (inner_if, 0);
10024 break;
10025 default:
366de0ce 10026 gcc_unreachable ();
506e2710
RH
10027 }
10028 found:
16865eaa 10029
506e2710 10030 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
10031 warning_at (if_locus, OPT_Wparentheses,
10032 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 10033 }
16865eaa 10034
2214de30 10035 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 10036 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 10037 add_stmt (stmt);
325c3691
RH
10038}
10039
506e2710
RH
10040/* Emit a general-purpose loop construct. START_LOCUS is the location of
10041 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10042 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 10043 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 10044 the continue label. Everything is allowed to be NULL. */
325c3691
RH
10045
10046void
506e2710
RH
10047c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10048 tree blab, tree clab, bool cond_is_first)
325c3691 10049{
506e2710
RH
10050 tree entry = NULL, exit = NULL, t;
10051
e5e44252
AK
10052 /* In theory could forbid cilk spawn for loop increment expression,
10053 but it should work just fine. */
36536d79 10054
28af952a
RS
10055 /* If the condition is zero don't generate a loop construct. */
10056 if (cond && integer_zerop (cond))
10057 {
10058 if (cond_is_first)
10059 {
10060 t = build_and_jump (&blab);
10061 SET_EXPR_LOCATION (t, start_locus);
10062 add_stmt (t);
10063 }
10064 }
10065 else
506e2710
RH
10066 {
10067 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 10068
506e2710 10069 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
10070 out of the loop, or to the top of it. If there's no exit condition,
10071 then we just build a jump back to the top. */
506e2710 10072 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 10073
28af952a 10074 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
10075 {
10076 /* Canonicalize the loop condition to the end. This means
10077 generating a branch to the loop condition. Reuse the
10078 continue label, if possible. */
10079 if (cond_is_first)
10080 {
10081 if (incr || !clab)
10082 {
10083 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10084 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10085 }
10086 else
10087 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 10088 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
10089 add_stmt (t);
10090 }
10091
506e2710 10092 t = build_and_jump (&blab);
506e2710 10093 if (cond_is_first)
db3927fb
AH
10094 exit = fold_build3_loc (start_locus,
10095 COND_EXPR, void_type_node, cond, exit, t);
506e2710 10096 else
db3927fb
AH
10097 exit = fold_build3_loc (input_location,
10098 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3 10099 }
fc402eec
AA
10100 else
10101 {
10102 /* For the backward-goto's location of an unconditional loop
10103 use the beginning of the body, or, if there is none, the
10104 top of the loop. */
10105 location_t loc = EXPR_LOCATION (expr_first (body));
10106 if (loc == UNKNOWN_LOCATION)
10107 loc = start_locus;
10108 SET_EXPR_LOCATION (exit, loc);
10109 }
c22cacf3 10110
506e2710
RH
10111 add_stmt (top);
10112 }
c22cacf3 10113
506e2710
RH
10114 if (body)
10115 add_stmt (body);
10116 if (clab)
10117 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10118 if (incr)
10119 add_stmt (incr);
10120 if (entry)
10121 add_stmt (entry);
10122 if (exit)
10123 add_stmt (exit);
10124 if (blab)
10125 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 10126}
325c3691
RH
10127
10128tree
c2255bc4 10129c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 10130{
089efaa4 10131 bool skip;
506e2710 10132 tree label = *label_p;
325c3691 10133
089efaa4
ILT
10134 /* In switch statements break is sometimes stylistically used after
10135 a return statement. This can lead to spurious warnings about
10136 control reaching the end of a non-void function when it is
10137 inlined. Note that we are calling block_may_fallthru with
10138 language specific tree nodes; this works because
10139 block_may_fallthru returns true when given something it does not
10140 understand. */
10141 skip = !block_may_fallthru (cur_stmt_list);
10142
506e2710 10143 if (!label)
089efaa4
ILT
10144 {
10145 if (!skip)
c2255bc4 10146 *label_p = label = create_artificial_label (loc);
089efaa4 10147 }
953ff289
DN
10148 else if (TREE_CODE (label) == LABEL_DECL)
10149 ;
10150 else switch (TREE_INT_CST_LOW (label))
506e2710 10151 {
953ff289 10152 case 0:
506e2710 10153 if (is_break)
c2255bc4 10154 error_at (loc, "break statement not within loop or switch");
506e2710 10155 else
c2255bc4 10156 error_at (loc, "continue statement not within a loop");
506e2710 10157 return NULL_TREE;
953ff289
DN
10158
10159 case 1:
10160 gcc_assert (is_break);
c2255bc4 10161 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
10162 return NULL_TREE;
10163
c02065fc
AH
10164 case 2:
10165 if (is_break)
10166 error ("break statement within %<#pragma simd%> loop body");
10167 else
10168 error ("continue statement within %<#pragma simd%> loop body");
10169 return NULL_TREE;
10170
953ff289
DN
10171 default:
10172 gcc_unreachable ();
506e2710 10173 }
325c3691 10174
089efaa4
ILT
10175 if (skip)
10176 return NULL_TREE;
10177
2e28e797
JH
10178 if (!is_break)
10179 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10180
53fb4de3 10181 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
10182}
10183
506e2710 10184/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
10185
10186static void
c2255bc4 10187emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 10188{
e6b5a630
RH
10189 if (expr == error_mark_node)
10190 ;
10191 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
10192 {
10193 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 10194 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 10195 }
789eadcd
MP
10196 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10197 {
10198 tree r = expr;
10199 location_t cloc = loc;
10200 while (TREE_CODE (r) == COMPOUND_EXPR)
10201 {
10202 if (EXPR_HAS_LOCATION (r))
10203 cloc = EXPR_LOCATION (r);
10204 r = TREE_OPERAND (r, 1);
10205 }
10206 if (!TREE_SIDE_EFFECTS (r)
10207 && !VOID_TYPE_P (TREE_TYPE (r))
10208 && !CONVERT_EXPR_P (r)
cc28fc7f 10209 && !TREE_NO_WARNING (r)
789eadcd
MP
10210 && !TREE_NO_WARNING (expr))
10211 warning_at (cloc, OPT_Wunused_value,
10212 "right-hand operand of comma expression has no effect");
10213 }
27f33b15 10214 else
c2255bc4 10215 warn_if_unused_value (expr, loc);
3a5b9284
RH
10216}
10217
506e2710 10218/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
10219 diagnostics, but do not call ADD_STMT. LOC is the location of the
10220 statement. */
3a5b9284 10221
506e2710 10222tree
c2255bc4 10223c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 10224{
056928b2
JJ
10225 tree exprv;
10226
3a5b9284 10227 if (!expr)
506e2710 10228 return NULL_TREE;
3a5b9284 10229
928c19bb
JM
10230 expr = c_fully_fold (expr, false, NULL);
10231
3a5b9284
RH
10232 if (warn_sequence_point)
10233 verify_sequence_points (expr);
10234
10235 if (TREE_TYPE (expr) != error_mark_node
10236 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10237 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 10238 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
10239
10240 /* If we're not processing a statement expression, warn about unused values.
10241 Warnings for statement expressions will be emitted later, once we figure
10242 out which is the result. */
10243 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 10244 && warn_unused_value)
d06f8b75 10245 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
3a5b9284 10246
056928b2
JJ
10247 exprv = expr;
10248 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10249 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
10250 while (CONVERT_EXPR_P (exprv))
10251 exprv = TREE_OPERAND (exprv, 0);
10252 if (DECL_P (exprv)
10253 || handled_component_p (exprv)
10254 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 10255 mark_exp_read (exprv);
fa8351f8 10256
3a5b9284
RH
10257 /* If the expression is not of a type to which we cannot assign a line
10258 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 10259 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
10260 {
10261 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10262 SET_EXPR_LOCATION (expr, loc);
10263 }
506e2710
RH
10264
10265 return expr;
10266}
10267
c2255bc4
AH
10268/* Emit an expression as a statement. LOC is the location of the
10269 expression. */
506e2710
RH
10270
10271tree
c2255bc4 10272c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
10273{
10274 if (expr)
c2255bc4 10275 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
10276 else
10277 return NULL;
3a5b9284
RH
10278}
10279
10280/* Do the opposite and emit a statement as an expression. To begin,
10281 create a new binding level and return it. */
325c3691
RH
10282
10283tree
10284c_begin_stmt_expr (void)
10285{
10286 tree ret;
10287
10288 /* We must force a BLOCK for this level so that, if it is not expanded
10289 later, there is a way to turn off the entire subtree of blocks that
10290 are contained in it. */
10291 keep_next_level ();
10292 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10293
10294 c_bindings_start_stmt_expr (c_switch_stack == NULL
10295 ? NULL
10296 : c_switch_stack->bindings);
325c3691
RH
10297
10298 /* Mark the current statement list as belonging to a statement list. */
10299 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10300
10301 return ret;
10302}
10303
c2255bc4
AH
10304/* LOC is the location of the compound statement to which this body
10305 belongs. */
10306
325c3691 10307tree
c2255bc4 10308c_finish_stmt_expr (location_t loc, tree body)
325c3691 10309{
3a5b9284 10310 tree last, type, tmp, val;
325c3691
RH
10311 tree *last_p;
10312
c2255bc4 10313 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10314
10315 c_bindings_end_stmt_expr (c_switch_stack == NULL
10316 ? NULL
10317 : c_switch_stack->bindings);
325c3691 10318
3a5b9284
RH
10319 /* Locate the last statement in BODY. See c_end_compound_stmt
10320 about always returning a BIND_EXPR. */
10321 last_p = &BIND_EXPR_BODY (body);
10322 last = BIND_EXPR_BODY (body);
10323
10324 continue_searching:
325c3691
RH
10325 if (TREE_CODE (last) == STATEMENT_LIST)
10326 {
3a5b9284
RH
10327 tree_stmt_iterator i;
10328
10329 /* This can happen with degenerate cases like ({ }). No value. */
10330 if (!TREE_SIDE_EFFECTS (last))
10331 return body;
10332
10333 /* If we're supposed to generate side effects warnings, process
10334 all of the statements except the last. */
27f33b15 10335 if (warn_unused_value)
325c3691 10336 {
3a5b9284 10337 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10338 {
10339 location_t tloc;
10340 tree t = tsi_stmt (i);
10341
10342 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10343 emit_side_effect_warnings (tloc, t);
10344 }
325c3691
RH
10345 }
10346 else
3a5b9284
RH
10347 i = tsi_last (last);
10348 last_p = tsi_stmt_ptr (i);
10349 last = *last_p;
325c3691
RH
10350 }
10351
3a5b9284
RH
10352 /* If the end of the list is exception related, then the list was split
10353 by a call to push_cleanup. Continue searching. */
10354 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10355 || TREE_CODE (last) == TRY_CATCH_EXPR)
10356 {
10357 last_p = &TREE_OPERAND (last, 0);
10358 last = *last_p;
10359 goto continue_searching;
10360 }
10361
26d8af35
JM
10362 if (last == error_mark_node)
10363 return last;
10364
3a5b9284
RH
10365 /* In the case that the BIND_EXPR is not necessary, return the
10366 expression out from inside it. */
26d8af35
JM
10367 if (last == BIND_EXPR_BODY (body)
10368 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10369 {
928c19bb
JM
10370 /* Even if this looks constant, do not allow it in a constant
10371 expression. */
e5a94231 10372 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10373 /* Do not warn if the return value of a statement expression is
10374 unused. */
928c19bb 10375 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10376 return last;
10377 }
325c3691
RH
10378
10379 /* Extract the type of said expression. */
10380 type = TREE_TYPE (last);
325c3691 10381
3a5b9284
RH
10382 /* If we're not returning a value at all, then the BIND_EXPR that
10383 we already have is a fine expression to return. */
10384 if (!type || VOID_TYPE_P (type))
10385 return body;
10386
10387 /* Now that we've located the expression containing the value, it seems
10388 silly to make voidify_wrapper_expr repeat the process. Create a
10389 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10390 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10391
10392 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10393 tree_expr_nonnegative_p giving up immediately. */
10394 val = last;
10395 if (TREE_CODE (val) == NOP_EXPR
10396 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10397 val = TREE_OPERAND (val, 0);
10398
53fb4de3 10399 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10400 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10401
c2255bc4
AH
10402 {
10403 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10404 SET_EXPR_LOCATION (t, loc);
10405 return t;
10406 }
325c3691
RH
10407}
10408\f
10409/* Begin and end compound statements. This is as simple as pushing
10410 and popping new statement lists from the tree. */
10411
10412tree
10413c_begin_compound_stmt (bool do_scope)
10414{
10415 tree stmt = push_stmt_list ();
10416 if (do_scope)
4dfa0342 10417 push_scope ();
325c3691
RH
10418 return stmt;
10419}
10420
c2255bc4
AH
10421/* End a compound statement. STMT is the statement. LOC is the
10422 location of the compound statement-- this is usually the location
10423 of the opening brace. */
10424
325c3691 10425tree
c2255bc4 10426c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10427{
10428 tree block = NULL;
10429
10430 if (do_scope)
10431 {
10432 if (c_dialect_objc ())
10433 objc_clear_super_receiver ();
10434 block = pop_scope ();
10435 }
10436
10437 stmt = pop_stmt_list (stmt);
c2255bc4 10438 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10439
10440 /* If this compound statement is nested immediately inside a statement
10441 expression, then force a BIND_EXPR to be created. Otherwise we'll
10442 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10443 STATEMENT_LISTs merge, and thus we can lose track of what statement
10444 was really last. */
38e01f9e 10445 if (building_stmt_list_p ()
325c3691
RH
10446 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10447 && TREE_CODE (stmt) != BIND_EXPR)
10448 {
53fb4de3 10449 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10450 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10451 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10452 }
10453
10454 return stmt;
10455}
5a508662
RH
10456
10457/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10458 when the current scope is exited. EH_ONLY is true when this is not
10459 meant to apply to normal control flow transfer. */
10460
10461void
c2255bc4 10462push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10463{
3a5b9284
RH
10464 enum tree_code code;
10465 tree stmt, list;
10466 bool stmt_expr;
10467
10468 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10469 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10470 add_stmt (stmt);
3a5b9284
RH
10471 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10472 list = push_stmt_list ();
10473 TREE_OPERAND (stmt, 0) = list;
10474 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10475}
325c3691 10476\f
9f47c7e5
IE
10477/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10478 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10479
10480static tree
10481build_vec_cmp (tree_code code, tree type,
10482 tree arg0, tree arg1)
10483{
10484 tree zero_vec = build_zero_cst (type);
10485 tree minus_one_vec = build_minus_one_cst (type);
10486 tree cmp_type = build_same_sized_truth_vector_type (type);
10487 tree cmp = build2 (code, cmp_type, arg0, arg1);
10488 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10489}
10490
3e4093b6
RS
10491/* Build a binary-operation expression without default conversions.
10492 CODE is the kind of expression to build.
ba47d38d 10493 LOCATION is the operator's location.
3e4093b6
RS
10494 This function differs from `build' in several ways:
10495 the data type of the result is computed and recorded in it,
10496 warnings are generated if arg data types are invalid,
10497 special handling for addition and subtraction of pointers is known,
10498 and some optimization is done (operations on narrow ints
10499 are done in the narrower type when that gives the same result).
10500 Constant folding is also done before the result is returned.
de520661 10501
3e4093b6
RS
10502 Note that the operands will never have enumeral types, or function
10503 or array types, because either they will have the default conversions
10504 performed or they have both just been converted to some other type in which
10505 the arithmetic is to be done. */
10506
10507tree
ba47d38d
AH
10508build_binary_op (location_t location, enum tree_code code,
10509 tree orig_op0, tree orig_op1, int convert_p)
de520661 10510{
8ce94e44
JM
10511 tree type0, type1, orig_type0, orig_type1;
10512 tree eptype;
3e4093b6
RS
10513 enum tree_code code0, code1;
10514 tree op0, op1;
c9f9eb5d 10515 tree ret = error_mark_node;
4de67c26 10516 const char *invalid_op_diag;
4d84fe7c 10517 bool op0_int_operands, op1_int_operands;
928c19bb 10518 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10519
3e4093b6
RS
10520 /* Expression code to give to the expression when it is built.
10521 Normally this is CODE, which is what the caller asked for,
10522 but in some special cases we change it. */
10523 enum tree_code resultcode = code;
8b6a5902 10524
3e4093b6
RS
10525 /* Data type in which the computation is to be performed.
10526 In the simplest cases this is the common type of the arguments. */
10527 tree result_type = NULL;
10528
8ce94e44
JM
10529 /* When the computation is in excess precision, the type of the
10530 final EXCESS_PRECISION_EXPR. */
2d2e923f 10531 tree semantic_result_type = NULL;
8ce94e44 10532
3e4093b6
RS
10533 /* Nonzero means operands have already been type-converted
10534 in whatever way is necessary.
10535 Zero means they need to be converted to RESULT_TYPE. */
10536 int converted = 0;
10537
10538 /* Nonzero means create the expression with this type, rather than
10539 RESULT_TYPE. */
10540 tree build_type = 0;
10541
10542 /* Nonzero means after finally constructing the expression
10543 convert it to this type. */
10544 tree final_type = 0;
10545
10546 /* Nonzero if this is an operation like MIN or MAX which can
10547 safely be computed in short if both args are promoted shorts.
10548 Also implies COMMON.
10549 -1 indicates a bitwise operation; this makes a difference
10550 in the exact conditions for when it is safe to do the operation
10551 in a narrower mode. */
10552 int shorten = 0;
10553
10554 /* Nonzero if this is a comparison operation;
10555 if both args are promoted shorts, compare the original shorts.
10556 Also implies COMMON. */
10557 int short_compare = 0;
10558
10559 /* Nonzero if this is a right-shift operation, which can be computed on the
10560 original short and then promoted if the operand is a promoted short. */
10561 int short_shift = 0;
10562
10563 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10564 int common = 0;
10565
58393038
ZL
10566 /* True means types are compatible as far as ObjC is concerned. */
10567 bool objc_ok;
10568
8ce94e44
JM
10569 /* True means this is an arithmetic operation that may need excess
10570 precision. */
10571 bool may_need_excess_precision;
10572
180f8dbb
JM
10573 /* True means this is a boolean operation that converts both its
10574 operands to truth-values. */
10575 bool boolean_op = false;
10576
de5a5fa1
MP
10577 /* Remember whether we're doing / or %. */
10578 bool doing_div_or_mod = false;
10579
10580 /* Remember whether we're doing << or >>. */
10581 bool doing_shift = false;
10582
10583 /* Tree holding instrumentation expression. */
10584 tree instrument_expr = NULL;
10585
ba47d38d
AH
10586 if (location == UNKNOWN_LOCATION)
10587 location = input_location;
10588
4d84fe7c
JM
10589 op0 = orig_op0;
10590 op1 = orig_op1;
10591
10592 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10593 if (op0_int_operands)
10594 op0 = remove_c_maybe_const_expr (op0);
10595 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10596 if (op1_int_operands)
10597 op1 = remove_c_maybe_const_expr (op1);
10598 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10599 if (int_operands)
10600 {
10601 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10602 && TREE_CODE (orig_op1) == INTEGER_CST);
10603 int_const = (int_const_or_overflow
10604 && !TREE_OVERFLOW (orig_op0)
10605 && !TREE_OVERFLOW (orig_op1));
10606 }
10607 else
10608 int_const = int_const_or_overflow = false;
10609
0e3a99ae 10610 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e 10611 if (convert_p
31521951 10612 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
790e9490 10613 {
4d84fe7c
JM
10614 op0 = default_conversion (op0);
10615 op1 = default_conversion (op1);
790e9490
RS
10616 }
10617
36536d79
BI
10618 /* When Cilk Plus is enabled and there are array notations inside op0, then
10619 we check to see if there are builtin array notation functions. If
10620 so, then we take on the type of the array notation inside it. */
b72271b9 10621 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10622 orig_type0 = type0 = find_correct_array_notation_type (op0);
10623 else
10624 orig_type0 = type0 = TREE_TYPE (op0);
10625
b72271b9 10626 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10627 orig_type1 = type1 = find_correct_array_notation_type (op1);
10628 else
10629 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10630
3e4093b6
RS
10631 /* The expression codes of the data types of the arguments tell us
10632 whether the arguments are integers, floating, pointers, etc. */
10633 code0 = TREE_CODE (type0);
10634 code1 = TREE_CODE (type1);
10635
10636 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10637 STRIP_TYPE_NOPS (op0);
10638 STRIP_TYPE_NOPS (op1);
10639
10640 /* If an error was already reported for one of the arguments,
10641 avoid reporting another error. */
10642
10643 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10644 return error_mark_node;
10645
1807ffc1
MS
10646 if (code0 == POINTER_TYPE
10647 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10648 return error_mark_node;
10649
10650 if (code1 == POINTER_TYPE
10651 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10652 return error_mark_node;
10653
4de67c26
JM
10654 if ((invalid_op_diag
10655 = targetm.invalid_binary_op (code, type0, type1)))
10656 {
ba47d38d 10657 error_at (location, invalid_op_diag);
4de67c26
JM
10658 return error_mark_node;
10659 }
10660
8ce94e44
JM
10661 switch (code)
10662 {
10663 case PLUS_EXPR:
10664 case MINUS_EXPR:
10665 case MULT_EXPR:
10666 case TRUNC_DIV_EXPR:
10667 case CEIL_DIV_EXPR:
10668 case FLOOR_DIV_EXPR:
10669 case ROUND_DIV_EXPR:
10670 case EXACT_DIV_EXPR:
10671 may_need_excess_precision = true;
10672 break;
10673 default:
10674 may_need_excess_precision = false;
10675 break;
10676 }
10677 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10678 {
10679 op0 = TREE_OPERAND (op0, 0);
10680 type0 = TREE_TYPE (op0);
10681 }
10682 else if (may_need_excess_precision
10683 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10684 {
10685 type0 = eptype;
10686 op0 = convert (eptype, op0);
10687 }
10688 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10689 {
10690 op1 = TREE_OPERAND (op1, 0);
10691 type1 = TREE_TYPE (op1);
10692 }
10693 else if (may_need_excess_precision
10694 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10695 {
10696 type1 = eptype;
10697 op1 = convert (eptype, op1);
10698 }
10699
58393038
ZL
10700 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10701
0e3a99ae
AS
10702 /* In case when one of the operands of the binary operation is
10703 a vector and another is a scalar -- convert scalar to vector. */
10704 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10705 {
a212e43f
MG
10706 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10707 true);
f90e8e2e 10708
0e3a99ae
AS
10709 switch (convert_flag)
10710 {
10711 case stv_error:
10712 return error_mark_node;
10713 case stv_firstarg:
10714 {
10715 bool maybe_const = true;
10716 tree sc;
10717 sc = c_fully_fold (op0, false, &maybe_const);
10718 sc = save_expr (sc);
10719 sc = convert (TREE_TYPE (type1), sc);
10720 op0 = build_vector_from_val (type1, sc);
10721 if (!maybe_const)
10722 op0 = c_wrap_maybe_const (op0, true);
10723 orig_type0 = type0 = TREE_TYPE (op0);
10724 code0 = TREE_CODE (type0);
10725 converted = 1;
10726 break;
10727 }
10728 case stv_secondarg:
10729 {
10730 bool maybe_const = true;
10731 tree sc;
10732 sc = c_fully_fold (op1, false, &maybe_const);
10733 sc = save_expr (sc);
10734 sc = convert (TREE_TYPE (type0), sc);
10735 op1 = build_vector_from_val (type0, sc);
10736 if (!maybe_const)
54b9f838 10737 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10738 orig_type1 = type1 = TREE_TYPE (op1);
10739 code1 = TREE_CODE (type1);
10740 converted = 1;
10741 break;
10742 }
10743 default:
10744 break;
10745 }
10746 }
10747
3e4093b6 10748 switch (code)
de520661 10749 {
3e4093b6
RS
10750 case PLUS_EXPR:
10751 /* Handle the pointer + int case. */
10752 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10753 {
db3927fb 10754 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10755 goto return_build_binary_op;
10756 }
3e4093b6 10757 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10758 {
db3927fb 10759 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10760 goto return_build_binary_op;
10761 }
fe67cf58 10762 else
3e4093b6
RS
10763 common = 1;
10764 break;
400fbf9f 10765
3e4093b6
RS
10766 case MINUS_EXPR:
10767 /* Subtraction of two similar pointers.
10768 We must subtract them as integers, then divide by object size. */
10769 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10770 && comp_target_types (location, type0, type1))
c9f9eb5d 10771 {
db3927fb 10772 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10773 goto return_build_binary_op;
10774 }
3e4093b6
RS
10775 /* Handle pointer minus int. Just like pointer plus int. */
10776 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10777 {
db3927fb 10778 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10779 goto return_build_binary_op;
10780 }
3e4093b6
RS
10781 else
10782 common = 1;
10783 break;
8b6a5902 10784
3e4093b6
RS
10785 case MULT_EXPR:
10786 common = 1;
10787 break;
10788
10789 case TRUNC_DIV_EXPR:
10790 case CEIL_DIV_EXPR:
10791 case FLOOR_DIV_EXPR:
10792 case ROUND_DIV_EXPR:
10793 case EXACT_DIV_EXPR:
de5a5fa1 10794 doing_div_or_mod = true;
c9f9eb5d 10795 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10796
10797 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10798 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10799 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10800 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10801 || code1 == FIXED_POINT_TYPE
3e4093b6 10802 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10803 {
5bed876a
AH
10804 enum tree_code tcode0 = code0, tcode1 = code1;
10805
3a021db2 10806 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10807 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10808 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10809 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10810
ab22c1fa
CF
10811 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10812 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10813 resultcode = RDIV_EXPR;
10814 else
10815 /* Although it would be tempting to shorten always here, that
10816 loses on some targets, since the modulo instruction is
10817 undefined if the quotient can't be represented in the
10818 computation mode. We shorten only if unsigned or if
10819 dividing by something we know != -1. */
8df83eae 10820 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10821 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10822 && !integer_all_onesp (op1)));
3e4093b6
RS
10823 common = 1;
10824 }
10825 break;
de520661 10826
3e4093b6 10827 case BIT_AND_EXPR:
3e4093b6
RS
10828 case BIT_IOR_EXPR:
10829 case BIT_XOR_EXPR:
10830 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10831 shorten = -1;
9ef0c8d9
AP
10832 /* Allow vector types which are not floating point types. */
10833 else if (code0 == VECTOR_TYPE
10834 && code1 == VECTOR_TYPE
10835 && !VECTOR_FLOAT_TYPE_P (type0)
10836 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10837 common = 1;
10838 break;
10839
10840 case TRUNC_MOD_EXPR:
10841 case FLOOR_MOD_EXPR:
de5a5fa1 10842 doing_div_or_mod = true;
c9f9eb5d 10843 warn_for_div_by_zero (location, op1);
de520661 10844
5cfd5d9b
AP
10845 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10846 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10847 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10848 common = 1;
10849 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10850 {
10851 /* Although it would be tempting to shorten always here, that loses
10852 on some targets, since the modulo instruction is undefined if the
10853 quotient can't be represented in the computation mode. We shorten
10854 only if unsigned or if dividing by something we know != -1. */
8df83eae 10855 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10856 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10857 && !integer_all_onesp (op1)));
3e4093b6
RS
10858 common = 1;
10859 }
10860 break;
de520661 10861
3e4093b6
RS
10862 case TRUTH_ANDIF_EXPR:
10863 case TRUTH_ORIF_EXPR:
10864 case TRUTH_AND_EXPR:
10865 case TRUTH_OR_EXPR:
10866 case TRUTH_XOR_EXPR:
10867 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10868 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10869 || code0 == FIXED_POINT_TYPE)
3e4093b6 10870 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10871 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10872 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10873 {
10874 /* Result of these operations is always an int,
10875 but that does not mean the operands should be
10876 converted to ints! */
10877 result_type = integer_type_node;
a27d595d
JM
10878 if (op0_int_operands)
10879 {
10880 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10881 op0 = remove_c_maybe_const_expr (op0);
10882 }
10883 else
10884 op0 = c_objc_common_truthvalue_conversion (location, op0);
10885 if (op1_int_operands)
10886 {
10887 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10888 op1 = remove_c_maybe_const_expr (op1);
10889 }
10890 else
10891 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10892 converted = 1;
180f8dbb 10893 boolean_op = true;
3e4093b6 10894 }
928c19bb
JM
10895 if (code == TRUTH_ANDIF_EXPR)
10896 {
10897 int_const_or_overflow = (int_operands
10898 && TREE_CODE (orig_op0) == INTEGER_CST
10899 && (op0 == truthvalue_false_node
10900 || TREE_CODE (orig_op1) == INTEGER_CST));
10901 int_const = (int_const_or_overflow
10902 && !TREE_OVERFLOW (orig_op0)
10903 && (op0 == truthvalue_false_node
10904 || !TREE_OVERFLOW (orig_op1)));
10905 }
10906 else if (code == TRUTH_ORIF_EXPR)
10907 {
10908 int_const_or_overflow = (int_operands
10909 && TREE_CODE (orig_op0) == INTEGER_CST
10910 && (op0 == truthvalue_true_node
10911 || TREE_CODE (orig_op1) == INTEGER_CST));
10912 int_const = (int_const_or_overflow
10913 && !TREE_OVERFLOW (orig_op0)
10914 && (op0 == truthvalue_true_node
10915 || !TREE_OVERFLOW (orig_op1)));
10916 }
3e4093b6 10917 break;
eba80994 10918
3e4093b6
RS
10919 /* Shift operations: result has same type as first operand;
10920 always convert second operand to int.
10921 Also set SHORT_SHIFT if shifting rightward. */
de520661 10922
3e4093b6 10923 case RSHIFT_EXPR:
f87bd04b
AS
10924 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10925 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10926 {
10927 result_type = type0;
10928 converted = 1;
10929 }
10930 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
451b5e48
MP
10931 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10932 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10933 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
10934 {
10935 result_type = type0;
10936 converted = 1;
10937 }
10938 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
451b5e48 10939 && code1 == INTEGER_TYPE)
3e4093b6 10940 {
de5a5fa1 10941 doing_shift = true;
928c19bb 10942 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10943 {
3e4093b6 10944 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10945 {
10946 int_const = false;
7d882b83 10947 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10948 warning_at (location, OPT_Wshift_count_negative,
10949 "right shift count is negative");
928c19bb 10950 }
3e4093b6 10951 else
bbb818c6 10952 {
3f75a254 10953 if (!integer_zerop (op1))
3e4093b6
RS
10954 short_shift = 1;
10955
10956 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10957 {
10958 int_const = false;
7d882b83 10959 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10960 warning_at (location, OPT_Wshift_count_overflow,
10961 "right shift count >= width of type");
928c19bb 10962 }
bbb818c6 10963 }
b62acd60 10964 }
de520661 10965
3e4093b6
RS
10966 /* Use the type of the value to be shifted. */
10967 result_type = type0;
3e4093b6
RS
10968 /* Avoid converting op1 to result_type later. */
10969 converted = 1;
400fbf9f 10970 }
3e4093b6 10971 break;
253b6b82 10972
3e4093b6 10973 case LSHIFT_EXPR:
f87bd04b
AS
10974 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10975 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10976 {
10977 result_type = type0;
10978 converted = 1;
10979 }
10980 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
451b5e48
MP
10981 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10982 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10983 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
10984 {
10985 result_type = type0;
10986 converted = 1;
10987 }
10988 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
451b5e48 10989 && code1 == INTEGER_TYPE)
3e4093b6 10990 {
de5a5fa1 10991 doing_shift = true;
0173bd2a
MP
10992 if (TREE_CODE (op0) == INTEGER_CST
10993 && tree_int_cst_sgn (op0) < 0)
10994 {
10995 /* Don't reject a left shift of a negative value in a context
10996 where a constant expression is needed in C90. */
10997 if (flag_isoc99)
10998 int_const = false;
10999 if (c_inhibit_evaluation_warnings == 0)
11000 warning_at (location, OPT_Wshift_negative_value,
11001 "left shift of negative value");
11002 }
928c19bb 11003 if (TREE_CODE (op1) == INTEGER_CST)
de520661 11004 {
3e4093b6 11005 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
11006 {
11007 int_const = false;
7d882b83 11008 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11009 warning_at (location, OPT_Wshift_count_negative,
11010 "left shift count is negative");
928c19bb 11011 }
3e4093b6 11012 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
11013 {
11014 int_const = false;
7d882b83 11015 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11016 warning_at (location, OPT_Wshift_count_overflow,
11017 "left shift count >= width of type");
928c19bb 11018 }
451b5e48
MP
11019 else if (TREE_CODE (op0) == INTEGER_CST
11020 && maybe_warn_shift_overflow (location, op0, op1)
11021 && flag_isoc99)
11022 int_const = false;
94ba5069 11023 }
de520661 11024
3e4093b6
RS
11025 /* Use the type of the value to be shifted. */
11026 result_type = type0;
3e4093b6
RS
11027 /* Avoid converting op1 to result_type later. */
11028 converted = 1;
400fbf9f 11029 }
3e4093b6 11030 break;
de520661 11031
3e4093b6
RS
11032 case EQ_EXPR:
11033 case NE_EXPR:
d246ab4f
AS
11034 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11035 {
11036 tree intt;
0af94e6f 11037 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11038 {
11039 error_at (location, "comparing vectors with different "
11040 "element types");
11041 return error_mark_node;
11042 }
11043
11044 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11045 {
11046 error_at (location, "comparing vectors with different "
11047 "number of elements");
11048 return error_mark_node;
11049 }
11050
11051 /* Always construct signed integer vector type. */
11052 intt = c_common_type_for_size (GET_MODE_BITSIZE
11053 (TYPE_MODE (TREE_TYPE (type0))), 0);
11054 result_type = build_opaque_vector_type (intt,
11055 TYPE_VECTOR_SUBPARTS (type0));
11056 converted = 1;
9f47c7e5
IE
11057 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11058 goto return_build_binary_op;
d246ab4f 11059 }
ae311566 11060 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
11061 warning_at (location,
11062 OPT_Wfloat_equal,
11063 "comparing floating point with == or != is unsafe");
3e4093b6
RS
11064 /* Result of comparison is always int,
11065 but don't convert the args to int! */
11066 build_type = integer_type_node;
11067 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 11068 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 11069 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 11070 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 11071 short_compare = 1;
637f1455
SZ
11072 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11073 {
dbb68221
MW
11074 if (warn_nonnull
11075 && TREE_CODE (op0) == PARM_DECL && nonnull_arg_p (op0))
11076 warning_at (location, OPT_Wnonnull,
11077 "nonnull argument %qD compared to NULL", op0);
11078
637f1455
SZ
11079 if (TREE_CODE (op0) == ADDR_EXPR
11080 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
11081 {
11082 if (code == EQ_EXPR)
11083 warning_at (location,
11084 OPT_Waddress,
11085 "the comparison will always evaluate as %<false%> "
11086 "for the address of %qD will never be NULL",
11087 TREE_OPERAND (op0, 0));
11088 else
11089 warning_at (location,
11090 OPT_Waddress,
11091 "the comparison will always evaluate as %<true%> "
11092 "for the address of %qD will never be NULL",
11093 TREE_OPERAND (op0, 0));
11094 }
11095 result_type = type0;
11096 }
11097 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11098 {
dbb68221
MW
11099 if (warn_nonnull
11100 && TREE_CODE (op1) == PARM_DECL && nonnull_arg_p (op1))
11101 warning_at (location, OPT_Wnonnull,
11102 "nonnull argument %qD compared to NULL", op1);
11103
637f1455
SZ
11104 if (TREE_CODE (op1) == ADDR_EXPR
11105 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
11106 {
11107 if (code == EQ_EXPR)
11108 warning_at (location,
f90e8e2e 11109 OPT_Waddress,
637f1455
SZ
11110 "the comparison will always evaluate as %<false%> "
11111 "for the address of %qD will never be NULL",
11112 TREE_OPERAND (op1, 0));
11113 else
11114 warning_at (location,
11115 OPT_Waddress,
11116 "the comparison will always evaluate as %<true%> "
11117 "for the address of %qD will never be NULL",
11118 TREE_OPERAND (op1, 0));
11119 }
11120 result_type = type1;
11121 }
3e4093b6
RS
11122 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11123 {
11124 tree tt0 = TREE_TYPE (type0);
11125 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
11126 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11127 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11128 addr_space_t as_common = ADDR_SPACE_GENERIC;
11129
3e4093b6
RS
11130 /* Anything compares with void *. void * compares with anything.
11131 Otherwise, the targets must be compatible
11132 and both must be object or both incomplete. */
744aa42f 11133 if (comp_target_types (location, type0, type1))
10bc1b1b 11134 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
11135 else if (!addr_space_superset (as0, as1, &as_common))
11136 {
11137 error_at (location, "comparison of pointers to "
11138 "disjoint address spaces");
11139 return error_mark_node;
11140 }
267bac10 11141 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 11142 {
36c5e70a 11143 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 11144 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11145 "comparison of %<void *%> with function pointer");
ee2990e7 11146 }
267bac10 11147 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 11148 {
36c5e70a 11149 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 11150 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11151 "comparison of %<void *%> with function pointer");
e6834654 11152 }
3e4093b6 11153 else
58393038
ZL
11154 /* Avoid warning about the volatile ObjC EH puts on decls. */
11155 if (!objc_ok)
ba47d38d 11156 pedwarn (location, 0,
509c9d60 11157 "comparison of distinct pointer types lacks a cast");
e6834654 11158
3e4093b6 11159 if (result_type == NULL_TREE)
36c5e70a
BE
11160 {
11161 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11162 result_type = build_pointer_type
11163 (build_qualified_type (void_type_node, qual));
11164 }
e6834654 11165 }
3e4093b6 11166 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 11167 {
3e4093b6 11168 result_type = type0;
ba47d38d 11169 pedwarn (location, 0, "comparison between pointer and integer");
de520661 11170 }
3e4093b6 11171 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 11172 {
3e4093b6 11173 result_type = type1;
ba47d38d 11174 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 11175 }
04159acf
MP
11176 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11177 || truth_value_p (TREE_CODE (orig_op0)))
11178 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11179 || truth_value_p (TREE_CODE (orig_op1))))
11180 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11181 break;
8b6a5902 11182
3e4093b6
RS
11183 case LE_EXPR:
11184 case GE_EXPR:
11185 case LT_EXPR:
11186 case GT_EXPR:
d246ab4f
AS
11187 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11188 {
11189 tree intt;
0af94e6f 11190 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11191 {
11192 error_at (location, "comparing vectors with different "
11193 "element types");
11194 return error_mark_node;
11195 }
11196
11197 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11198 {
11199 error_at (location, "comparing vectors with different "
11200 "number of elements");
11201 return error_mark_node;
11202 }
11203
11204 /* Always construct signed integer vector type. */
11205 intt = c_common_type_for_size (GET_MODE_BITSIZE
11206 (TYPE_MODE (TREE_TYPE (type0))), 0);
11207 result_type = build_opaque_vector_type (intt,
11208 TYPE_VECTOR_SUBPARTS (type0));
11209 converted = 1;
9f47c7e5
IE
11210 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11211 goto return_build_binary_op;
d246ab4f 11212 }
3e4093b6 11213 build_type = integer_type_node;
ab22c1fa
CF
11214 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11215 || code0 == FIXED_POINT_TYPE)
11216 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11217 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11218 short_compare = 1;
11219 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11220 {
36c5e70a
BE
11221 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11222 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11223 addr_space_t as_common;
11224
744aa42f 11225 if (comp_target_types (location, type0, type1))
3e4093b6 11226 {
10bc1b1b 11227 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
11228 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11229 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 11230 pedwarn (location, 0,
509c9d60 11231 "comparison of complete and incomplete pointers");
fcf73884 11232 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 11233 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11234 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
11235 else if (null_pointer_constant_p (orig_op0)
11236 || null_pointer_constant_p (orig_op1))
11237 warning_at (location, OPT_Wextra,
11238 "ordered comparison of pointer with null pointer");
11239
3e4093b6 11240 }
36c5e70a
BE
11241 else if (!addr_space_superset (as0, as1, &as_common))
11242 {
11243 error_at (location, "comparison of pointers to "
11244 "disjoint address spaces");
11245 return error_mark_node;
11246 }
3e4093b6
RS
11247 else
11248 {
36c5e70a
BE
11249 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11250 result_type = build_pointer_type
11251 (build_qualified_type (void_type_node, qual));
ba47d38d 11252 pedwarn (location, 0,
509c9d60 11253 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
11254 }
11255 }
6aa3c60d 11256 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
11257 {
11258 result_type = type0;
fcf73884 11259 if (pedantic)
c1771a20 11260 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
11261 "ordered comparison of pointer with integer zero");
11262 else if (extra_warnings)
ba47d38d 11263 warning_at (location, OPT_Wextra,
d42ba3b8 11264 "ordered comparison of pointer with integer zero");
3e4093b6 11265 }
6aa3c60d 11266 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
11267 {
11268 result_type = type1;
d42ba3b8 11269 if (pedantic)
c1771a20 11270 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
11271 "ordered comparison of pointer with integer zero");
11272 else if (extra_warnings)
11273 warning_at (location, OPT_Wextra,
11274 "ordered comparison of pointer with integer zero");
3e4093b6
RS
11275 }
11276 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11277 {
11278 result_type = type0;
ba47d38d 11279 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
11280 }
11281 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11282 {
11283 result_type = type1;
ba47d38d 11284 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 11285 }
04159acf
MP
11286 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11287 || truth_value_p (TREE_CODE (orig_op0)))
11288 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11289 || truth_value_p (TREE_CODE (orig_op1))))
11290 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11291 break;
64094f6a 11292
3e4093b6 11293 default:
37b2f290 11294 gcc_unreachable ();
c9fe6f9f 11295 }
8f17b5c5 11296
e57e265b
PB
11297 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11298 return error_mark_node;
11299
5bed876a
AH
11300 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11301 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 11302 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 11303 {
745e411d
DM
11304 gcc_rich_location richloc (location);
11305 richloc.maybe_add_expr (orig_op0);
11306 richloc.maybe_add_expr (orig_op1);
11307 binary_op_error (&richloc, code, type0, type1);
5bed876a
AH
11308 return error_mark_node;
11309 }
11310
3e4093b6 11311 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 11312 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
11313 &&
11314 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 11315 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 11316 {
2ca862e9
JM
11317 bool first_complex = (code0 == COMPLEX_TYPE);
11318 bool second_complex = (code1 == COMPLEX_TYPE);
11319 int none_complex = (!first_complex && !second_complex);
39b726dd 11320
3e4093b6 11321 if (shorten || common || short_compare)
3bf6bfcc
JJ
11322 {
11323 result_type = c_common_type (type0, type1);
0a0b3574
MM
11324 do_warn_double_promotion (result_type, type0, type1,
11325 "implicit conversion from %qT to %qT "
11326 "to match other operand of binary "
11327 "expression",
11328 location);
3bf6bfcc
JJ
11329 if (result_type == error_mark_node)
11330 return error_mark_node;
11331 }
400fbf9f 11332
2ca862e9
JM
11333 if (first_complex != second_complex
11334 && (code == PLUS_EXPR
11335 || code == MINUS_EXPR
11336 || code == MULT_EXPR
11337 || (code == TRUNC_DIV_EXPR && first_complex))
11338 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11339 && flag_signed_zeros)
11340 {
11341 /* An operation on mixed real/complex operands must be
11342 handled specially, but the language-independent code can
11343 more easily optimize the plain complex arithmetic if
11344 -fno-signed-zeros. */
11345 tree real_type = TREE_TYPE (result_type);
11346 tree real, imag;
11347 if (type0 != orig_type0 || type1 != orig_type1)
11348 {
11349 gcc_assert (may_need_excess_precision && common);
2d2e923f 11350 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11351 }
11352 if (first_complex)
11353 {
11354 if (TREE_TYPE (op0) != result_type)
68fca595 11355 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11356 if (TREE_TYPE (op1) != real_type)
68fca595 11357 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11358 }
11359 else
11360 {
11361 if (TREE_TYPE (op0) != real_type)
68fca595 11362 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11363 if (TREE_TYPE (op1) != result_type)
68fca595 11364 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11365 }
11366 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11367 return error_mark_node;
11368 if (first_complex)
11369 {
11370 op0 = c_save_expr (op0);
11371 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11372 op0, 1);
11373 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11374 op0, 1);
11375 switch (code)
11376 {
11377 case MULT_EXPR:
11378 case TRUNC_DIV_EXPR:
c4603e7c 11379 op1 = c_save_expr (op1);
2ca862e9
JM
11380 imag = build2 (resultcode, real_type, imag, op1);
11381 /* Fall through. */
11382 case PLUS_EXPR:
11383 case MINUS_EXPR:
11384 real = build2 (resultcode, real_type, real, op1);
11385 break;
11386 default:
11387 gcc_unreachable();
11388 }
11389 }
11390 else
11391 {
11392 op1 = c_save_expr (op1);
11393 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11394 op1, 1);
11395 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11396 op1, 1);
11397 switch (code)
11398 {
11399 case MULT_EXPR:
c4603e7c 11400 op0 = c_save_expr (op0);
2ca862e9
JM
11401 imag = build2 (resultcode, real_type, op0, imag);
11402 /* Fall through. */
11403 case PLUS_EXPR:
11404 real = build2 (resultcode, real_type, op0, real);
11405 break;
11406 case MINUS_EXPR:
11407 real = build2 (resultcode, real_type, op0, real);
11408 imag = build1 (NEGATE_EXPR, real_type, imag);
11409 break;
11410 default:
11411 gcc_unreachable();
11412 }
11413 }
11414 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11415 goto return_build_binary_op;
11416 }
11417
3e4093b6
RS
11418 /* For certain operations (which identify themselves by shorten != 0)
11419 if both args were extended from the same smaller type,
11420 do the arithmetic in that type and then extend.
400fbf9f 11421
3e4093b6
RS
11422 shorten !=0 and !=1 indicates a bitwise operation.
11423 For them, this optimization is safe only if
11424 both args are zero-extended or both are sign-extended.
11425 Otherwise, we might change the result.
11426 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11427 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11428
3e4093b6
RS
11429 if (shorten && none_complex)
11430 {
3e4093b6 11431 final_type = result_type;
b8698a0f 11432 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11433 shorten == -1);
3e4093b6 11434 }
88a3dbc1 11435
3e4093b6 11436 /* Shifts can be shortened if shifting right. */
2f6e4e97 11437
3e4093b6
RS
11438 if (short_shift)
11439 {
11440 int unsigned_arg;
11441 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11442
3e4093b6 11443 final_type = result_type;
abe80e6d 11444
3e4093b6 11445 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11446 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11447
3e4093b6 11448 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11449 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11450 /* We can shorten only if the shift count is less than the
11451 number of bits in the smaller type size. */
11452 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11453 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11454 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11455 {
11456 /* Do an unsigned shift if the operand was zero-extended. */
11457 result_type
11458 = c_common_signed_or_unsigned_type (unsigned_arg,
11459 TREE_TYPE (arg0));
11460 /* Convert value-to-be-shifted to that type. */
11461 if (TREE_TYPE (op0) != result_type)
11462 op0 = convert (result_type, op0);
11463 converted = 1;
abe80e6d 11464 }
88a3dbc1
RK
11465 }
11466
3e4093b6
RS
11467 /* Comparison operations are shortened too but differently.
11468 They identify themselves by setting short_compare = 1. */
56cb9733 11469
3e4093b6
RS
11470 if (short_compare)
11471 {
11472 /* Don't write &op0, etc., because that would prevent op0
11473 from being kept in a register.
11474 Instead, make copies of the our local variables and
11475 pass the copies by reference, then copy them back afterward. */
11476 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11477 enum tree_code xresultcode = resultcode;
11478 tree val
393e8e8b
MP
11479 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11480 &xresultcode);
8f17b5c5 11481
3e4093b6 11482 if (val != 0)
c9f9eb5d
AH
11483 {
11484 ret = val;
11485 goto return_build_binary_op;
11486 }
8f17b5c5 11487
3e4093b6
RS
11488 op0 = xop0, op1 = xop1;
11489 converted = 1;
11490 resultcode = xresultcode;
8f17b5c5 11491
7d882b83 11492 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11493 {
11494 bool op0_maybe_const = true;
11495 bool op1_maybe_const = true;
11496 tree orig_op0_folded, orig_op1_folded;
11497
11498 if (in_late_binary_op)
11499 {
11500 orig_op0_folded = orig_op0;
11501 orig_op1_folded = orig_op1;
11502 }
11503 else
11504 {
11505 /* Fold for the sake of possible warnings, as in
11506 build_conditional_expr. This requires the
11507 "original" values to be folded, not just op0 and
11508 op1. */
f5178456 11509 c_inhibit_evaluation_warnings++;
928c19bb
JM
11510 op0 = c_fully_fold (op0, require_constant_value,
11511 &op0_maybe_const);
11512 op1 = c_fully_fold (op1, require_constant_value,
11513 &op1_maybe_const);
f5178456 11514 c_inhibit_evaluation_warnings--;
928c19bb
JM
11515 orig_op0_folded = c_fully_fold (orig_op0,
11516 require_constant_value,
11517 NULL);
11518 orig_op1_folded = c_fully_fold (orig_op1,
11519 require_constant_value,
11520 NULL);
11521 }
11522
11523 if (warn_sign_compare)
11524 warn_for_sign_compare (location, orig_op0_folded,
11525 orig_op1_folded, op0, op1,
11526 result_type, resultcode);
5c2f94b4 11527 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11528 {
11529 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11530 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11531 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11532 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11533 }
3e4093b6 11534 }
2ad1815d 11535 }
64094f6a 11536 }
64094f6a 11537
3e4093b6
RS
11538 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11539 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11540 Then the expression will be built.
11541 It will be given type FINAL_TYPE if that is nonzero;
11542 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11543
3e4093b6
RS
11544 if (!result_type)
11545 {
745e411d
DM
11546 gcc_rich_location richloc (location);
11547 richloc.maybe_add_expr (orig_op0);
11548 richloc.maybe_add_expr (orig_op1);
11549 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11550 return error_mark_node;
11551 }
400fbf9f 11552
3e4093b6 11553 if (build_type == NULL_TREE)
8ce94e44
JM
11554 {
11555 build_type = result_type;
180f8dbb
JM
11556 if ((type0 != orig_type0 || type1 != orig_type1)
11557 && !boolean_op)
8ce94e44
JM
11558 {
11559 gcc_assert (may_need_excess_precision && common);
2d2e923f 11560 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11561 }
11562 }
400fbf9f 11563
2d2e923f
MLI
11564 if (!converted)
11565 {
68fca595
MP
11566 op0 = ep_convert_and_check (location, result_type, op0,
11567 semantic_result_type);
11568 op1 = ep_convert_and_check (location, result_type, op1,
11569 semantic_result_type);
2d2e923f
MLI
11570
11571 /* This can happen if one operand has a vector type, and the other
11572 has a different type. */
11573 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11574 return error_mark_node;
11575 }
11576
f8ed5150
MP
11577 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11578 | SANITIZE_FLOAT_DIVIDE))
802ac282 11579 && do_ubsan_in_current_function ()
b2aaf235
MP
11580 && (doing_div_or_mod || doing_shift)
11581 && !require_constant_value)
de5a5fa1
MP
11582 {
11583 /* OP0 and/or OP1 might have side-effects. */
11584 op0 = c_save_expr (op0);
11585 op1 = c_save_expr (op1);
11586 op0 = c_fully_fold (op0, false, NULL);
11587 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11588 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11589 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11590 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11591 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11592 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11593 }
11594
c9f9eb5d 11595 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11596 if (int_const_or_overflow)
11597 ret = (require_constant_value
db3927fb
AH
11598 ? fold_build2_initializer_loc (location, resultcode, build_type,
11599 op0, op1)
11600 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11601 else
11602 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11603 if (final_type != 0)
11604 ret = convert (final_type, ret);
11605
11606 return_build_binary_op:
11607 gcc_assert (ret != error_mark_node);
928c19bb
JM
11608 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11609 ret = (int_operands
11610 ? note_integer_operands (ret)
11611 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11612 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11613 && !in_late_binary_op)
11614 ret = note_integer_operands (ret);
2d2e923f
MLI
11615 if (semantic_result_type)
11616 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11617 protected_set_expr_location (ret, location);
de5a5fa1 11618
a24d975c 11619 if (instrument_expr != NULL)
de5a5fa1
MP
11620 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11621 instrument_expr, ret);
11622
c9f9eb5d 11623 return ret;
400fbf9f 11624}
85498824
JM
11625
11626
11627/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11628 purpose. LOCATION is the source location for the expression. */
85498824
JM
11629
11630tree
ba47d38d 11631c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11632{
928c19bb
JM
11633 bool int_const, int_operands;
11634
85498824
JM
11635 switch (TREE_CODE (TREE_TYPE (expr)))
11636 {
11637 case ARRAY_TYPE:
ba47d38d 11638 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11639 return error_mark_node;
11640
11641 case RECORD_TYPE:
ba47d38d 11642 error_at (location, "used struct type value where scalar is required");
85498824
JM
11643 return error_mark_node;
11644
11645 case UNION_TYPE:
ba47d38d 11646 error_at (location, "used union type value where scalar is required");
85498824
JM
11647 return error_mark_node;
11648
04af8788
NP
11649 case VOID_TYPE:
11650 error_at (location, "void value not ignored as it ought to be");
11651 return error_mark_node;
11652
1807ffc1
MS
11653 case POINTER_TYPE:
11654 if (reject_gcc_builtin (expr))
11655 return error_mark_node;
11656 break;
11657
46bdb9cf
JM
11658 case FUNCTION_TYPE:
11659 gcc_unreachable ();
11660
d246ab4f
AS
11661 case VECTOR_TYPE:
11662 error_at (location, "used vector type where scalar is required");
11663 return error_mark_node;
11664
85498824
JM
11665 default:
11666 break;
11667 }
11668
928c19bb
JM
11669 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11670 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11671 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11672 {
11673 expr = remove_c_maybe_const_expr (expr);
11674 expr = build2 (NE_EXPR, integer_type_node, expr,
11675 convert (TREE_TYPE (expr), integer_zero_node));
11676 expr = note_integer_operands (expr);
11677 }
11678 else
11679 /* ??? Should we also give an error for vectors rather than leaving
11680 those to give errors later? */
11681 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11682
11683 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11684 {
11685 if (TREE_OVERFLOW (expr))
11686 return expr;
11687 else
11688 return note_integer_operands (expr);
11689 }
11690 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11691 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11692 return expr;
85498824 11693}
73f397d4
JM
11694\f
11695
11696/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11697 required. */
11698
11699tree
51eed280 11700c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11701{
11702 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11703 {
11704 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11705 /* Executing a compound literal inside a function reinitializes
11706 it. */
11707 if (!TREE_STATIC (decl))
11708 *se = true;
11709 return decl;
11710 }
11711 else
11712 return expr;
11713}
953ff289 11714\f
88bae6f4
TS
11715/* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11716 statement. LOC is the location of the construct. */
41dbbb37
TS
11717
11718tree
88bae6f4
TS
11719c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11720 tree clauses)
41dbbb37 11721{
88bae6f4 11722 body = c_end_compound_stmt (loc, body, true);
41dbbb37 11723
88bae6f4 11724 tree stmt = make_node (code);
41dbbb37 11725 TREE_TYPE (stmt) = void_type_node;
88bae6f4
TS
11726 OMP_BODY (stmt) = body;
11727 OMP_CLAUSES (stmt) = clauses;
41dbbb37
TS
11728 SET_EXPR_LOCATION (stmt, loc);
11729
11730 return add_stmt (stmt);
11731}
11732
11733/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11734 statement. LOC is the location of the OACC_DATA. */
11735
11736tree
11737c_finish_oacc_data (location_t loc, tree clauses, tree block)
11738{
11739 tree stmt;
11740
11741 block = c_end_compound_stmt (loc, block, true);
11742
11743 stmt = make_node (OACC_DATA);
11744 TREE_TYPE (stmt) = void_type_node;
11745 OACC_DATA_CLAUSES (stmt) = clauses;
11746 OACC_DATA_BODY (stmt) = block;
11747 SET_EXPR_LOCATION (stmt, loc);
11748
11749 return add_stmt (stmt);
11750}
11751
37d5ad46
JB
11752/* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11753 statement. LOC is the location of the OACC_HOST_DATA. */
11754
11755tree
11756c_finish_oacc_host_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_HOST_DATA);
11763 TREE_TYPE (stmt) = void_type_node;
11764 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
11765 OACC_HOST_DATA_BODY (stmt) = block;
11766 SET_EXPR_LOCATION (stmt, loc);
11767
11768 return add_stmt (stmt);
11769}
11770
c0220ea4 11771/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11772
11773tree
11774c_begin_omp_parallel (void)
11775{
11776 tree block;
11777
11778 keep_next_level ();
11779 block = c_begin_compound_stmt (true);
11780
11781 return block;
11782}
11783
c2255bc4
AH
11784/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11785 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11786
953ff289 11787tree
c2255bc4 11788c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11789{
11790 tree stmt;
11791
c2255bc4 11792 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11793
11794 stmt = make_node (OMP_PARALLEL);
11795 TREE_TYPE (stmt) = void_type_node;
11796 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11797 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11798 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11799
11800 return add_stmt (stmt);
11801}
11802
a68ab351
JJ
11803/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11804
11805tree
11806c_begin_omp_task (void)
11807{
11808 tree block;
11809
11810 keep_next_level ();
11811 block = c_begin_compound_stmt (true);
11812
11813 return block;
11814}
11815
c2255bc4
AH
11816/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11817 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11818
11819tree
c2255bc4 11820c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11821{
11822 tree stmt;
11823
c2255bc4 11824 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11825
11826 stmt = make_node (OMP_TASK);
11827 TREE_TYPE (stmt) = void_type_node;
11828 OMP_TASK_CLAUSES (stmt) = clauses;
11829 OMP_TASK_BODY (stmt) = block;
c2255bc4 11830 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11831
11832 return add_stmt (stmt);
11833}
11834
acf0174b
JJ
11835/* Generate GOMP_cancel call for #pragma omp cancel. */
11836
11837void
11838c_finish_omp_cancel (location_t loc, tree clauses)
11839{
11840 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11841 int mask = 0;
11842 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11843 mask = 1;
11844 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11845 mask = 2;
11846 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11847 mask = 4;
11848 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11849 mask = 8;
11850 else
11851 {
11852 error_at (loc, "%<#pragma omp cancel must specify one of "
11853 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11854 "clauses");
11855 return;
11856 }
11857 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11858 if (ifc != NULL_TREE)
11859 {
11860 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11861 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11862 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11863 build_zero_cst (type));
11864 }
11865 else
11866 ifc = boolean_true_node;
11867 tree stmt = build_call_expr_loc (loc, fn, 2,
11868 build_int_cst (integer_type_node, mask),
11869 ifc);
11870 add_stmt (stmt);
11871}
11872
11873/* Generate GOMP_cancellation_point call for
11874 #pragma omp cancellation point. */
11875
11876void
11877c_finish_omp_cancellation_point (location_t loc, tree clauses)
11878{
11879 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11880 int mask = 0;
11881 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11882 mask = 1;
11883 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11884 mask = 2;
11885 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11886 mask = 4;
11887 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11888 mask = 8;
11889 else
11890 {
11891 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11892 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11893 "clauses");
11894 return;
11895 }
11896 tree stmt = build_call_expr_loc (loc, fn, 1,
11897 build_int_cst (integer_type_node, mask));
11898 add_stmt (stmt);
11899}
11900
11901/* Helper function for handle_omp_array_sections. Called recursively
11902 to handle multiple array-section-subscripts. C is the clause,
11903 T current expression (initially OMP_CLAUSE_DECL), which is either
11904 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11905 expression if specified, TREE_VALUE length expression if specified,
11906 TREE_CHAIN is what it has been specified after, or some decl.
11907 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11908 set to true if any of the array-section-subscript could have length
11909 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11910 first array-section-subscript which is known not to have length
11911 of one. Given say:
11912 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11913 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11914 all are or may have length of 1, array-section-subscript [:2] is the
d9a6bd32 11915 first one known not to have length 1. For array-section-subscript
acf0174b
JJ
11916 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11917 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11918 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11919 case though, as some lengths could be zero. */
11920
11921static tree
11922handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
d9a6bd32
JJ
11923 bool &maybe_zero_len, unsigned int &first_non_one,
11924 bool is_omp)
acf0174b
JJ
11925{
11926 tree ret, low_bound, length, type;
11927 if (TREE_CODE (t) != TREE_LIST)
11928 {
7a0ca710 11929 if (error_operand_p (t))
acf0174b 11930 return error_mark_node;
d9a6bd32
JJ
11931 ret = t;
11932 if (TREE_CODE (t) == COMPONENT_REF
11933 && is_omp
11934 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
11935 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
11936 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
11937 {
11938 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
11939 {
11940 error_at (OMP_CLAUSE_LOCATION (c),
11941 "bit-field %qE in %qs clause",
11942 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11943 return error_mark_node;
11944 }
11945 while (TREE_CODE (t) == COMPONENT_REF)
11946 {
11947 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
11948 {
11949 error_at (OMP_CLAUSE_LOCATION (c),
11950 "%qE is a member of a union", t);
11951 return error_mark_node;
11952 }
11953 t = TREE_OPERAND (t, 0);
11954 }
11955 }
0ae9bd27 11956 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
11957 {
11958 if (DECL_P (t))
11959 error_at (OMP_CLAUSE_LOCATION (c),
11960 "%qD is not a variable in %qs clause", t,
11961 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11962 else
11963 error_at (OMP_CLAUSE_LOCATION (c),
11964 "%qE is not a variable in %qs clause", t,
11965 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11966 return error_mark_node;
11967 }
11968 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
0ae9bd27 11969 && VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
11970 {
11971 error_at (OMP_CLAUSE_LOCATION (c),
11972 "%qD is threadprivate variable in %qs clause", t,
11973 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11974 return error_mark_node;
11975 }
d9a6bd32 11976 return ret;
acf0174b
JJ
11977 }
11978
11979 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
d9a6bd32 11980 maybe_zero_len, first_non_one, is_omp);
acf0174b
JJ
11981 if (ret == error_mark_node || ret == NULL_TREE)
11982 return ret;
11983
11984 type = TREE_TYPE (ret);
11985 low_bound = TREE_PURPOSE (t);
11986 length = TREE_VALUE (t);
11987
11988 if (low_bound == error_mark_node || length == error_mark_node)
11989 return error_mark_node;
11990
11991 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11992 {
11993 error_at (OMP_CLAUSE_LOCATION (c),
11994 "low bound %qE of array section does not have integral type",
11995 low_bound);
11996 return error_mark_node;
11997 }
11998 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11999 {
12000 error_at (OMP_CLAUSE_LOCATION (c),
12001 "length %qE of array section does not have integral type",
12002 length);
12003 return error_mark_node;
12004 }
12005 if (low_bound
12006 && TREE_CODE (low_bound) == INTEGER_CST
12007 && TYPE_PRECISION (TREE_TYPE (low_bound))
12008 > TYPE_PRECISION (sizetype))
12009 low_bound = fold_convert (sizetype, low_bound);
12010 if (length
12011 && TREE_CODE (length) == INTEGER_CST
12012 && TYPE_PRECISION (TREE_TYPE (length))
12013 > TYPE_PRECISION (sizetype))
12014 length = fold_convert (sizetype, length);
12015 if (low_bound == NULL_TREE)
12016 low_bound = integer_zero_node;
12017
12018 if (length != NULL_TREE)
12019 {
12020 if (!integer_nonzerop (length))
d9a6bd32
JJ
12021 {
12022 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12023 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12024 {
12025 if (integer_zerop (length))
12026 {
12027 error_at (OMP_CLAUSE_LOCATION (c),
12028 "zero length array section in %qs clause",
12029 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12030 return error_mark_node;
12031 }
12032 }
12033 else
12034 maybe_zero_len = true;
12035 }
acf0174b
JJ
12036 if (first_non_one == types.length ()
12037 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12038 first_non_one++;
12039 }
12040 if (TREE_CODE (type) == ARRAY_TYPE)
12041 {
12042 if (length == NULL_TREE
12043 && (TYPE_DOMAIN (type) == NULL_TREE
12044 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12045 {
12046 error_at (OMP_CLAUSE_LOCATION (c),
12047 "for unknown bound array type length expression must "
12048 "be specified");
12049 return error_mark_node;
12050 }
12051 if (TREE_CODE (low_bound) == INTEGER_CST
12052 && tree_int_cst_sgn (low_bound) == -1)
12053 {
12054 error_at (OMP_CLAUSE_LOCATION (c),
12055 "negative low bound in array section in %qs clause",
12056 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12057 return error_mark_node;
12058 }
12059 if (length != NULL_TREE
12060 && TREE_CODE (length) == INTEGER_CST
12061 && tree_int_cst_sgn (length) == -1)
12062 {
12063 error_at (OMP_CLAUSE_LOCATION (c),
12064 "negative length in array section in %qs clause",
12065 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12066 return error_mark_node;
12067 }
12068 if (TYPE_DOMAIN (type)
12069 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12070 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12071 == INTEGER_CST)
12072 {
12073 tree size = size_binop (PLUS_EXPR,
12074 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12075 size_one_node);
12076 if (TREE_CODE (low_bound) == INTEGER_CST)
12077 {
12078 if (tree_int_cst_lt (size, low_bound))
12079 {
12080 error_at (OMP_CLAUSE_LOCATION (c),
12081 "low bound %qE above array section size "
12082 "in %qs clause", low_bound,
12083 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12084 return error_mark_node;
12085 }
12086 if (tree_int_cst_equal (size, low_bound))
d9a6bd32
JJ
12087 {
12088 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12089 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12090 {
12091 error_at (OMP_CLAUSE_LOCATION (c),
12092 "zero length array section in %qs clause",
12093 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12094 return error_mark_node;
12095 }
12096 maybe_zero_len = true;
12097 }
acf0174b
JJ
12098 else if (length == NULL_TREE
12099 && first_non_one == types.length ()
12100 && tree_int_cst_equal
12101 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12102 low_bound))
12103 first_non_one++;
12104 }
12105 else if (length == NULL_TREE)
12106 {
d9a6bd32
JJ
12107 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12108 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12109 maybe_zero_len = true;
acf0174b
JJ
12110 if (first_non_one == types.length ())
12111 first_non_one++;
12112 }
12113 if (length && TREE_CODE (length) == INTEGER_CST)
12114 {
12115 if (tree_int_cst_lt (size, length))
12116 {
12117 error_at (OMP_CLAUSE_LOCATION (c),
12118 "length %qE above array section size "
12119 "in %qs clause", length,
12120 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12121 return error_mark_node;
12122 }
12123 if (TREE_CODE (low_bound) == INTEGER_CST)
12124 {
12125 tree lbpluslen
12126 = size_binop (PLUS_EXPR,
12127 fold_convert (sizetype, low_bound),
12128 fold_convert (sizetype, length));
12129 if (TREE_CODE (lbpluslen) == INTEGER_CST
12130 && tree_int_cst_lt (size, lbpluslen))
12131 {
12132 error_at (OMP_CLAUSE_LOCATION (c),
12133 "high bound %qE above array section size "
12134 "in %qs clause", lbpluslen,
12135 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12136 return error_mark_node;
12137 }
12138 }
12139 }
12140 }
12141 else if (length == NULL_TREE)
12142 {
d9a6bd32
JJ
12143 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12144 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12145 maybe_zero_len = true;
acf0174b
JJ
12146 if (first_non_one == types.length ())
12147 first_non_one++;
12148 }
12149
12150 /* For [lb:] we will need to evaluate lb more than once. */
12151 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12152 {
12153 tree lb = c_save_expr (low_bound);
12154 if (lb != low_bound)
12155 {
12156 TREE_PURPOSE (t) = lb;
12157 low_bound = lb;
12158 }
12159 }
12160 }
12161 else if (TREE_CODE (type) == POINTER_TYPE)
12162 {
12163 if (length == NULL_TREE)
12164 {
12165 error_at (OMP_CLAUSE_LOCATION (c),
12166 "for pointer type length expression must be specified");
12167 return error_mark_node;
12168 }
d9a6bd32
JJ
12169 if (length != NULL_TREE
12170 && TREE_CODE (length) == INTEGER_CST
12171 && tree_int_cst_sgn (length) == -1)
12172 {
12173 error_at (OMP_CLAUSE_LOCATION (c),
12174 "negative length in array section in %qs clause",
12175 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12176 return error_mark_node;
12177 }
acf0174b
JJ
12178 /* If there is a pointer type anywhere but in the very first
12179 array-section-subscript, the array section can't be contiguous. */
12180 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12181 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12182 {
12183 error_at (OMP_CLAUSE_LOCATION (c),
12184 "array section is not contiguous in %qs clause",
12185 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12186 return error_mark_node;
12187 }
12188 }
12189 else
12190 {
12191 error_at (OMP_CLAUSE_LOCATION (c),
12192 "%qE does not have pointer or array type", ret);
12193 return error_mark_node;
12194 }
12195 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12196 types.safe_push (TREE_TYPE (ret));
12197 /* We will need to evaluate lb more than once. */
12198 tree lb = c_save_expr (low_bound);
12199 if (lb != low_bound)
12200 {
12201 TREE_PURPOSE (t) = lb;
12202 low_bound = lb;
12203 }
12204 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12205 return ret;
12206}
12207
12208/* Handle array sections for clause C. */
12209
12210static bool
d9a6bd32 12211handle_omp_array_sections (tree c, bool is_omp)
acf0174b
JJ
12212{
12213 bool maybe_zero_len = false;
12214 unsigned int first_non_one = 0;
d9a6bd32 12215 auto_vec<tree, 10> types;
acf0174b 12216 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
d9a6bd32
JJ
12217 maybe_zero_len, first_non_one,
12218 is_omp);
acf0174b 12219 if (first == error_mark_node)
d9a6bd32 12220 return true;
acf0174b 12221 if (first == NULL_TREE)
d9a6bd32 12222 return false;
acf0174b
JJ
12223 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12224 {
12225 tree t = OMP_CLAUSE_DECL (c);
12226 tree tem = NULL_TREE;
acf0174b
JJ
12227 /* Need to evaluate side effects in the length expressions
12228 if any. */
12229 while (TREE_CODE (t) == TREE_LIST)
12230 {
12231 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12232 {
12233 if (tem == NULL_TREE)
12234 tem = TREE_VALUE (t);
12235 else
12236 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12237 TREE_VALUE (t), tem);
12238 }
12239 t = TREE_CHAIN (t);
12240 }
12241 if (tem)
12242 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12243 first = c_fully_fold (first, false, NULL);
12244 OMP_CLAUSE_DECL (c) = first;
12245 }
12246 else
12247 {
12248 unsigned int num = types.length (), i;
12249 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12250 tree condition = NULL_TREE;
12251
12252 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12253 maybe_zero_len = true;
12254
12255 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12256 t = TREE_CHAIN (t))
12257 {
12258 tree low_bound = TREE_PURPOSE (t);
12259 tree length = TREE_VALUE (t);
12260
12261 i--;
12262 if (low_bound
12263 && TREE_CODE (low_bound) == INTEGER_CST
12264 && TYPE_PRECISION (TREE_TYPE (low_bound))
12265 > TYPE_PRECISION (sizetype))
12266 low_bound = fold_convert (sizetype, low_bound);
12267 if (length
12268 && TREE_CODE (length) == INTEGER_CST
12269 && TYPE_PRECISION (TREE_TYPE (length))
12270 > TYPE_PRECISION (sizetype))
12271 length = fold_convert (sizetype, length);
12272 if (low_bound == NULL_TREE)
12273 low_bound = integer_zero_node;
12274 if (!maybe_zero_len && i > first_non_one)
12275 {
12276 if (integer_nonzerop (low_bound))
12277 goto do_warn_noncontiguous;
12278 if (length != NULL_TREE
12279 && TREE_CODE (length) == INTEGER_CST
12280 && TYPE_DOMAIN (types[i])
12281 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12282 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12283 == INTEGER_CST)
12284 {
12285 tree size;
12286 size = size_binop (PLUS_EXPR,
12287 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12288 size_one_node);
12289 if (!tree_int_cst_equal (length, size))
12290 {
12291 do_warn_noncontiguous:
12292 error_at (OMP_CLAUSE_LOCATION (c),
12293 "array section is not contiguous in %qs "
12294 "clause",
12295 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
acf0174b
JJ
12296 return true;
12297 }
12298 }
12299 if (length != NULL_TREE
12300 && TREE_SIDE_EFFECTS (length))
12301 {
12302 if (side_effects == NULL_TREE)
12303 side_effects = length;
12304 else
12305 side_effects = build2 (COMPOUND_EXPR,
12306 TREE_TYPE (side_effects),
12307 length, side_effects);
12308 }
12309 }
12310 else
12311 {
12312 tree l;
12313
d9a6bd32
JJ
12314 if (i > first_non_one
12315 && ((length && integer_nonzerop (length))
12316 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
acf0174b
JJ
12317 continue;
12318 if (length)
12319 l = fold_convert (sizetype, length);
12320 else
12321 {
12322 l = size_binop (PLUS_EXPR,
12323 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12324 size_one_node);
12325 l = size_binop (MINUS_EXPR, l,
12326 fold_convert (sizetype, low_bound));
12327 }
12328 if (i > first_non_one)
12329 {
12330 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12331 size_zero_node);
12332 if (condition == NULL_TREE)
12333 condition = l;
12334 else
12335 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12336 l, condition);
12337 }
12338 else if (size == NULL_TREE)
12339 {
12340 size = size_in_bytes (TREE_TYPE (types[i]));
d9a6bd32
JJ
12341 tree eltype = TREE_TYPE (types[num - 1]);
12342 while (TREE_CODE (eltype) == ARRAY_TYPE)
12343 eltype = TREE_TYPE (eltype);
12344 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12345 {
12346 if (integer_zerop (size)
12347 || integer_zerop (size_in_bytes (eltype)))
12348 {
12349 error_at (OMP_CLAUSE_LOCATION (c),
12350 "zero length array section in %qs clause",
12351 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12352 return error_mark_node;
12353 }
12354 size = size_binop (EXACT_DIV_EXPR, size,
12355 size_in_bytes (eltype));
12356 }
acf0174b
JJ
12357 size = size_binop (MULT_EXPR, size, l);
12358 if (condition)
12359 size = fold_build3 (COND_EXPR, sizetype, condition,
12360 size, size_zero_node);
12361 }
12362 else
12363 size = size_binop (MULT_EXPR, size, l);
12364 }
12365 }
acf0174b
JJ
12366 if (side_effects)
12367 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
d9a6bd32
JJ
12368 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12369 {
12370 size = size_binop (MINUS_EXPR, size, size_one_node);
12371 size = c_fully_fold (size, false, NULL);
12372 tree index_type = build_index_type (size);
12373 tree eltype = TREE_TYPE (first);
12374 while (TREE_CODE (eltype) == ARRAY_TYPE)
12375 eltype = TREE_TYPE (eltype);
12376 tree type = build_array_type (eltype, index_type);
12377 tree ptype = build_pointer_type (eltype);
12378 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12379 t = build_fold_addr_expr (t);
e01d41e5
JJ
12380 tree t2 = build_fold_addr_expr (first);
12381 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12382 ptrdiff_type_node, t2);
12383 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12384 ptrdiff_type_node, t2,
12385 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12386 ptrdiff_type_node, t));
12387 t2 = c_fully_fold (t2, false, NULL);
12388 if (tree_fits_shwi_p (t2))
12389 t = build2 (MEM_REF, type, t,
12390 build_int_cst (ptype, tree_to_shwi (t2)));
12391 else
12392 {
12393 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12394 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12395 TREE_TYPE (t), t, t2);
12396 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12397 }
d9a6bd32
JJ
12398 OMP_CLAUSE_DECL (c) = t;
12399 return false;
12400 }
acf0174b
JJ
12401 first = c_fully_fold (first, false, NULL);
12402 OMP_CLAUSE_DECL (c) = first;
12403 if (size)
12404 size = c_fully_fold (size, false, NULL);
12405 OMP_CLAUSE_SIZE (c) = size;
d9a6bd32
JJ
12406 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12407 || (TREE_CODE (t) == COMPONENT_REF
12408 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
acf0174b 12409 return false;
41dbbb37 12410 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
d9a6bd32
JJ
12411 if (is_omp)
12412 switch (OMP_CLAUSE_MAP_KIND (c))
12413 {
12414 case GOMP_MAP_ALLOC:
12415 case GOMP_MAP_TO:
12416 case GOMP_MAP_FROM:
12417 case GOMP_MAP_TOFROM:
12418 case GOMP_MAP_ALWAYS_TO:
12419 case GOMP_MAP_ALWAYS_FROM:
12420 case GOMP_MAP_ALWAYS_TOFROM:
12421 case GOMP_MAP_RELEASE:
12422 case GOMP_MAP_DELETE:
12423 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12424 break;
12425 default:
12426 break;
12427 }
acf0174b 12428 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
e01d41e5
JJ
12429 if (!is_omp)
12430 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12431 else if (TREE_CODE (t) == COMPONENT_REF)
12432 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12433 else
12434 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12435 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12436 && !c_mark_addressable (t))
acf0174b
JJ
12437 return false;
12438 OMP_CLAUSE_DECL (c2) = t;
12439 t = build_fold_addr_expr (first);
12440 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12441 tree ptr = OMP_CLAUSE_DECL (c2);
12442 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12443 ptr = build_fold_addr_expr (ptr);
12444 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12445 ptrdiff_type_node, t,
12446 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12447 ptrdiff_type_node, ptr));
12448 t = c_fully_fold (t, false, NULL);
12449 OMP_CLAUSE_SIZE (c2) = t;
12450 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12451 OMP_CLAUSE_CHAIN (c) = c2;
12452 }
12453 return false;
12454}
12455
12456/* Helper function of finish_omp_clauses. Clone STMT as if we were making
12457 an inline call. But, remap
12458 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12459 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12460
12461static tree
12462c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12463 tree decl, tree placeholder)
12464{
12465 copy_body_data id;
b787e7a2 12466 hash_map<tree, tree> decl_map;
acf0174b 12467
b787e7a2
TS
12468 decl_map.put (omp_decl1, placeholder);
12469 decl_map.put (omp_decl2, decl);
acf0174b
JJ
12470 memset (&id, 0, sizeof (id));
12471 id.src_fn = DECL_CONTEXT (omp_decl1);
12472 id.dst_fn = current_function_decl;
12473 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 12474 id.decl_map = &decl_map;
acf0174b
JJ
12475
12476 id.copy_decl = copy_decl_no_change;
12477 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12478 id.transform_new_cfg = true;
12479 id.transform_return_to_modify = false;
12480 id.transform_lang_insert_block = NULL;
12481 id.eh_lp_nr = 0;
12482 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
12483 return stmt;
12484}
12485
12486/* Helper function of c_finish_omp_clauses, called via walk_tree.
12487 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12488
12489static tree
12490c_find_omp_placeholder_r (tree *tp, int *, void *data)
12491{
12492 if (*tp == (tree) data)
12493 return *tp;
12494 return NULL_TREE;
12495}
12496
41dbbb37 12497/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
12498 Remove any elements from the list that are invalid. */
12499
12500tree
d9a6bd32 12501c_finish_omp_clauses (tree clauses, bool is_omp, bool declare_simd)
953ff289
DN
12502{
12503 bitmap_head generic_head, firstprivate_head, lastprivate_head;
e01d41e5 12504 bitmap_head aligned_head, map_head, map_field_head;
d9a6bd32
JJ
12505 tree c, t, type, *pc;
12506 tree simdlen = NULL_TREE, safelen = NULL_TREE;
acf0174b
JJ
12507 bool branch_seen = false;
12508 bool copyprivate_seen = false;
e01d41e5 12509 bool linear_variable_step_check = false;
acf0174b 12510 tree *nowait_clause = NULL;
e01d41e5
JJ
12511 bool ordered_seen = false;
12512 tree schedule_clause = NULL_TREE;
953ff289
DN
12513
12514 bitmap_obstack_initialize (NULL);
12515 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12516 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12517 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 12518 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
d9a6bd32
JJ
12519 bitmap_initialize (&map_head, &bitmap_default_obstack);
12520 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
953ff289
DN
12521
12522 for (pc = &clauses, c = clauses; c ; c = *pc)
12523 {
12524 bool remove = false;
12525 bool need_complete = false;
12526 bool need_implicitly_determined = false;
12527
aaf46ef9 12528 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
12529 {
12530 case OMP_CLAUSE_SHARED:
953ff289
DN
12531 need_implicitly_determined = true;
12532 goto check_dup_generic;
12533
12534 case OMP_CLAUSE_PRIVATE:
953ff289
DN
12535 need_complete = true;
12536 need_implicitly_determined = true;
12537 goto check_dup_generic;
12538
12539 case OMP_CLAUSE_REDUCTION:
953ff289
DN
12540 need_implicitly_determined = true;
12541 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12542 if (TREE_CODE (t) == TREE_LIST)
12543 {
12544 if (handle_omp_array_sections (c, is_omp))
12545 {
12546 remove = true;
12547 break;
12548 }
12549
12550 t = OMP_CLAUSE_DECL (c);
12551 }
12552 t = require_complete_type (t);
12553 if (t == error_mark_node)
12554 {
12555 remove = true;
12556 break;
12557 }
12558 type = TREE_TYPE (t);
12559 if (TREE_CODE (t) == MEM_REF)
12560 type = TREE_TYPE (type);
12561 if (TREE_CODE (type) == ARRAY_TYPE)
12562 {
12563 tree oatype = type;
12564 gcc_assert (TREE_CODE (t) != MEM_REF);
12565 while (TREE_CODE (type) == ARRAY_TYPE)
12566 type = TREE_TYPE (type);
12567 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12568 {
12569 error_at (OMP_CLAUSE_LOCATION (c),
12570 "%qD in %<reduction%> clause is a zero size array",
12571 t);
12572 remove = true;
12573 break;
12574 }
12575 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12576 TYPE_SIZE_UNIT (type));
12577 if (integer_zerop (size))
12578 {
12579 error_at (OMP_CLAUSE_LOCATION (c),
12580 "%qD in %<reduction%> clause is a zero size array",
12581 t);
12582 remove = true;
12583 break;
12584 }
12585 size = size_binop (MINUS_EXPR, size, size_one_node);
12586 tree index_type = build_index_type (size);
12587 tree atype = build_array_type (type, index_type);
12588 tree ptype = build_pointer_type (type);
12589 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12590 t = build_fold_addr_expr (t);
12591 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12592 OMP_CLAUSE_DECL (c) = t;
12593 }
acf0174b 12594 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
d9a6bd32
JJ
12595 && (FLOAT_TYPE_P (type)
12596 || TREE_CODE (type) == COMPLEX_TYPE))
953ff289
DN
12597 {
12598 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12599 const char *r_name = NULL;
12600
12601 switch (r_code)
12602 {
12603 case PLUS_EXPR:
12604 case MULT_EXPR:
12605 case MINUS_EXPR:
652fea39 12606 break;
20906c66 12607 case MIN_EXPR:
d9a6bd32 12608 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39
JJ
12609 r_name = "min";
12610 break;
20906c66 12611 case MAX_EXPR:
d9a6bd32 12612 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39 12613 r_name = "max";
953ff289
DN
12614 break;
12615 case BIT_AND_EXPR:
12616 r_name = "&";
12617 break;
12618 case BIT_XOR_EXPR:
12619 r_name = "^";
12620 break;
12621 case BIT_IOR_EXPR:
12622 r_name = "|";
12623 break;
12624 case TRUTH_ANDIF_EXPR:
d9a6bd32 12625 if (FLOAT_TYPE_P (type))
652fea39 12626 r_name = "&&";
953ff289
DN
12627 break;
12628 case TRUTH_ORIF_EXPR:
d9a6bd32 12629 if (FLOAT_TYPE_P (type))
652fea39 12630 r_name = "||";
953ff289
DN
12631 break;
12632 default:
12633 gcc_unreachable ();
12634 }
12635 if (r_name)
12636 {
c2255bc4
AH
12637 error_at (OMP_CLAUSE_LOCATION (c),
12638 "%qE has invalid type for %<reduction(%s)%>",
12639 t, r_name);
953ff289 12640 remove = true;
ee1d5a02 12641 break;
953ff289
DN
12642 }
12643 }
acf0174b
JJ
12644 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12645 {
12646 error_at (OMP_CLAUSE_LOCATION (c),
d9a6bd32 12647 "user defined reduction not found for %qE", t);
acf0174b 12648 remove = true;
ee1d5a02 12649 break;
acf0174b
JJ
12650 }
12651 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12652 {
12653 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
d9a6bd32 12654 type = TYPE_MAIN_VARIANT (type);
acf0174b
JJ
12655 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12656 VAR_DECL, NULL_TREE, type);
d9a6bd32 12657 tree decl_placeholder = NULL_TREE;
acf0174b
JJ
12658 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12659 DECL_ARTIFICIAL (placeholder) = 1;
12660 DECL_IGNORED_P (placeholder) = 1;
d9a6bd32
JJ
12661 if (TREE_CODE (t) == MEM_REF)
12662 {
12663 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12664 VAR_DECL, NULL_TREE, type);
12665 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12666 DECL_ARTIFICIAL (decl_placeholder) = 1;
12667 DECL_IGNORED_P (decl_placeholder) = 1;
12668 }
acf0174b
JJ
12669 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12670 c_mark_addressable (placeholder);
12671 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
d9a6bd32
JJ
12672 c_mark_addressable (decl_placeholder ? decl_placeholder
12673 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12674 OMP_CLAUSE_REDUCTION_MERGE (c)
12675 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12676 TREE_VEC_ELT (list, 0),
12677 TREE_VEC_ELT (list, 1),
d9a6bd32
JJ
12678 decl_placeholder ? decl_placeholder
12679 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b
JJ
12680 OMP_CLAUSE_REDUCTION_MERGE (c)
12681 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12682 void_type_node, NULL_TREE,
d9a6bd32 12683 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
acf0174b
JJ
12684 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12685 if (TREE_VEC_LENGTH (list) == 6)
12686 {
12687 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
d9a6bd32
JJ
12688 c_mark_addressable (decl_placeholder ? decl_placeholder
12689 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12690 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12691 c_mark_addressable (placeholder);
12692 tree init = TREE_VEC_ELT (list, 5);
12693 if (init == error_mark_node)
12694 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12695 OMP_CLAUSE_REDUCTION_INIT (c)
12696 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12697 TREE_VEC_ELT (list, 3),
d9a6bd32
JJ
12698 decl_placeholder ? decl_placeholder
12699 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b 12700 if (TREE_VEC_ELT (list, 5) == error_mark_node)
d9a6bd32
JJ
12701 {
12702 tree v = decl_placeholder ? decl_placeholder : t;
12703 OMP_CLAUSE_REDUCTION_INIT (c)
12704 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12705 OMP_CLAUSE_REDUCTION_INIT (c));
12706 }
acf0174b
JJ
12707 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12708 c_find_omp_placeholder_r,
12709 placeholder, NULL))
12710 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12711 }
12712 else
12713 {
12714 tree init;
d9a6bd32
JJ
12715 tree v = decl_placeholder ? decl_placeholder : t;
12716 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12717 init = build_constructor (TREE_TYPE (v), NULL);
acf0174b 12718 else
d9a6bd32 12719 init = fold_convert (TREE_TYPE (v), integer_zero_node);
acf0174b 12720 OMP_CLAUSE_REDUCTION_INIT (c)
d9a6bd32 12721 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
acf0174b
JJ
12722 }
12723 OMP_CLAUSE_REDUCTION_INIT (c)
12724 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12725 void_type_node, NULL_TREE,
12726 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12727 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12728 }
d9a6bd32
JJ
12729 if (TREE_CODE (t) == MEM_REF)
12730 {
12731 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12732 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12733 != INTEGER_CST)
12734 {
12735 sorry ("variable length element type in array "
12736 "%<reduction%> clause");
12737 remove = true;
12738 break;
12739 }
12740 t = TREE_OPERAND (t, 0);
e01d41e5
JJ
12741 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12742 t = TREE_OPERAND (t, 0);
d9a6bd32
JJ
12743 if (TREE_CODE (t) == ADDR_EXPR)
12744 t = TREE_OPERAND (t, 0);
12745 }
12746 goto check_dup_generic_t;
953ff289
DN
12747
12748 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
12749 copyprivate_seen = true;
12750 if (nowait_clause)
12751 {
12752 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12753 "%<nowait%> clause must not be used together "
12754 "with %<copyprivate%>");
12755 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12756 nowait_clause = NULL;
12757 }
953ff289
DN
12758 goto check_dup_generic;
12759
12760 case OMP_CLAUSE_COPYIN:
953ff289 12761 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12762 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
953ff289 12763 {
c2255bc4
AH
12764 error_at (OMP_CLAUSE_LOCATION (c),
12765 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12766 remove = true;
ee1d5a02 12767 break;
953ff289
DN
12768 }
12769 goto check_dup_generic;
12770
acf0174b 12771 case OMP_CLAUSE_LINEAR:
d9a6bd32
JJ
12772 if (!declare_simd)
12773 need_implicitly_determined = true;
acf0174b 12774 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12775 if (!declare_simd
12776 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
12777 {
12778 error_at (OMP_CLAUSE_LOCATION (c),
12779 "modifier should not be specified in %<linear%> "
12780 "clause on %<simd%> or %<for%> constructs");
12781 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
12782 }
acf0174b
JJ
12783 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12784 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12785 {
12786 error_at (OMP_CLAUSE_LOCATION (c),
12787 "linear clause applied to non-integral non-pointer "
12788 "variable with type %qT", TREE_TYPE (t));
12789 remove = true;
12790 break;
12791 }
e01d41e5
JJ
12792 if (declare_simd)
12793 {
12794 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12795 if (TREE_CODE (s) == PARM_DECL)
12796 {
12797 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
12798 /* map_head bitmap is used as uniform_head if
12799 declare_simd. */
12800 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
12801 linear_variable_step_check = true;
12802 goto check_dup_generic;
12803 }
12804 if (TREE_CODE (s) != INTEGER_CST)
12805 {
12806 error_at (OMP_CLAUSE_LOCATION (c),
12807 "%<linear%> clause step %qE is neither constant "
12808 "nor a parameter", s);
12809 remove = true;
12810 break;
12811 }
12812 }
acf0174b
JJ
12813 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12814 {
12815 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12816 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12817 OMP_CLAUSE_DECL (c), s);
12818 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
12819 sizetype, fold_convert (sizetype, s),
12820 fold_convert
12821 (sizetype, OMP_CLAUSE_DECL (c)));
acf0174b
JJ
12822 if (s == error_mark_node)
12823 s = size_one_node;
12824 OMP_CLAUSE_LINEAR_STEP (c) = s;
12825 }
da6f124d
JJ
12826 else
12827 OMP_CLAUSE_LINEAR_STEP (c)
12828 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
12829 goto check_dup_generic;
12830
953ff289
DN
12831 check_dup_generic:
12832 t = OMP_CLAUSE_DECL (c);
d9a6bd32 12833 check_dup_generic_t:
0ae9bd27 12834 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12835 {
c2255bc4 12836 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
12837 "%qE is not a variable in clause %qs", t,
12838 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12839 remove = true;
12840 }
12841 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12842 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12843 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12844 {
c2255bc4
AH
12845 error_at (OMP_CLAUSE_LOCATION (c),
12846 "%qE appears more than once in data clauses", t);
953ff289
DN
12847 remove = true;
12848 }
e01d41e5
JJ
12849 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12850 && bitmap_bit_p (&map_head, DECL_UID (t)))
12851 {
12852 error ("%qD appears both in data and map clauses", t);
12853 remove = true;
12854 }
953ff289
DN
12855 else
12856 bitmap_set_bit (&generic_head, DECL_UID (t));
12857 break;
12858
12859 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
12860 t = OMP_CLAUSE_DECL (c);
12861 need_complete = true;
12862 need_implicitly_determined = true;
0ae9bd27 12863 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12864 {
c2255bc4
AH
12865 error_at (OMP_CLAUSE_LOCATION (c),
12866 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
12867 remove = true;
12868 }
12869 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12870 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12871 {
c2255bc4
AH
12872 error_at (OMP_CLAUSE_LOCATION (c),
12873 "%qE appears more than once in data clauses", t);
953ff289
DN
12874 remove = true;
12875 }
e01d41e5
JJ
12876 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
12877 {
12878 error ("%qD appears both in data and map clauses", t);
12879 remove = true;
12880 }
953ff289
DN
12881 else
12882 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12883 break;
12884
12885 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
12886 t = OMP_CLAUSE_DECL (c);
12887 need_complete = true;
12888 need_implicitly_determined = true;
0ae9bd27 12889 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12890 {
c2255bc4
AH
12891 error_at (OMP_CLAUSE_LOCATION (c),
12892 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
12893 remove = true;
12894 }
12895 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12896 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12897 {
c2255bc4
AH
12898 error_at (OMP_CLAUSE_LOCATION (c),
12899 "%qE appears more than once in data clauses", t);
953ff289
DN
12900 remove = true;
12901 }
12902 else
12903 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12904 break;
12905
acf0174b
JJ
12906 case OMP_CLAUSE_ALIGNED:
12907 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12908 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12909 {
12910 error_at (OMP_CLAUSE_LOCATION (c),
12911 "%qE is not a variable in %<aligned%> clause", t);
12912 remove = true;
12913 }
5a9785fb
JJ
12914 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12915 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12916 {
12917 error_at (OMP_CLAUSE_LOCATION (c),
12918 "%qE in %<aligned%> clause is neither a pointer nor "
12919 "an array", t);
12920 remove = true;
12921 }
acf0174b
JJ
12922 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12923 {
12924 error_at (OMP_CLAUSE_LOCATION (c),
12925 "%qE appears more than once in %<aligned%> clauses",
12926 t);
12927 remove = true;
12928 }
12929 else
12930 bitmap_set_bit (&aligned_head, DECL_UID (t));
12931 break;
12932
12933 case OMP_CLAUSE_DEPEND:
12934 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12935 if (t == NULL_TREE)
12936 {
12937 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
12938 == OMP_CLAUSE_DEPEND_SOURCE);
12939 break;
12940 }
12941 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
12942 {
12943 gcc_assert (TREE_CODE (t) == TREE_LIST);
12944 for (; t; t = TREE_CHAIN (t))
12945 {
12946 tree decl = TREE_VALUE (t);
12947 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
12948 {
12949 tree offset = TREE_PURPOSE (t);
12950 bool neg = wi::neg_p ((wide_int) offset);
12951 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
12952 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
12953 neg ? MINUS_EXPR : PLUS_EXPR,
12954 decl, offset);
12955 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
12956 sizetype,
12957 fold_convert (sizetype, t2),
12958 fold_convert (sizetype, decl));
d9a6bd32
JJ
12959 if (t2 == error_mark_node)
12960 {
12961 remove = true;
12962 break;
12963 }
12964 TREE_PURPOSE (t) = t2;
12965 }
12966 }
12967 break;
12968 }
acf0174b
JJ
12969 if (TREE_CODE (t) == TREE_LIST)
12970 {
d9a6bd32 12971 if (handle_omp_array_sections (c, is_omp))
acf0174b
JJ
12972 remove = true;
12973 break;
12974 }
12975 if (t == error_mark_node)
12976 remove = true;
0ae9bd27 12977 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12978 {
12979 error_at (OMP_CLAUSE_LOCATION (c),
12980 "%qE is not a variable in %<depend%> clause", t);
12981 remove = true;
12982 }
12983 else if (!c_mark_addressable (t))
12984 remove = true;
12985 break;
12986
12987 case OMP_CLAUSE_MAP:
12988 case OMP_CLAUSE_TO:
12989 case OMP_CLAUSE_FROM:
41dbbb37 12990 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
12991 t = OMP_CLAUSE_DECL (c);
12992 if (TREE_CODE (t) == TREE_LIST)
12993 {
d9a6bd32 12994 if (handle_omp_array_sections (c, is_omp))
acf0174b
JJ
12995 remove = true;
12996 else
12997 {
12998 t = OMP_CLAUSE_DECL (c);
b17a8b07 12999 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13000 {
13001 error_at (OMP_CLAUSE_LOCATION (c),
13002 "array section does not have mappable type "
13003 "in %qs clause",
13004 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13005 remove = true;
13006 }
d9a6bd32
JJ
13007 while (TREE_CODE (t) == ARRAY_REF)
13008 t = TREE_OPERAND (t, 0);
13009 if (TREE_CODE (t) == COMPONENT_REF
13010 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13011 {
13012 while (TREE_CODE (t) == COMPONENT_REF)
13013 t = TREE_OPERAND (t, 0);
13014 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13015 break;
13016 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13017 {
13018 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13019 error ("%qD appears more than once in motion"
13020 " clauses", t);
13021 else
13022 error ("%qD appears more than once in map"
13023 " clauses", t);
13024 remove = true;
13025 }
13026 else
13027 {
13028 bitmap_set_bit (&map_head, DECL_UID (t));
13029 bitmap_set_bit (&map_field_head, DECL_UID (t));
13030 }
13031 }
acf0174b
JJ
13032 }
13033 break;
13034 }
13035 if (t == error_mark_node)
d9a6bd32
JJ
13036 {
13037 remove = true;
13038 break;
13039 }
13040 if (TREE_CODE (t) == COMPONENT_REF
13041 && is_omp
13042 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13043 {
13044 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13045 {
13046 error_at (OMP_CLAUSE_LOCATION (c),
13047 "bit-field %qE in %qs clause",
13048 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13049 remove = true;
13050 }
13051 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13052 {
13053 error_at (OMP_CLAUSE_LOCATION (c),
13054 "%qE does not have a mappable type in %qs clause",
13055 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13056 remove = true;
13057 }
13058 while (TREE_CODE (t) == COMPONENT_REF)
13059 {
13060 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13061 == UNION_TYPE)
13062 {
13063 error_at (OMP_CLAUSE_LOCATION (c),
13064 "%qE is a member of a union", t);
13065 remove = true;
13066 break;
13067 }
13068 t = TREE_OPERAND (t, 0);
13069 }
13070 if (remove)
13071 break;
13072 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13073 {
e01d41e5 13074 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
d9a6bd32
JJ
13075 break;
13076 }
13077 }
13078 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13079 {
13080 error_at (OMP_CLAUSE_LOCATION (c),
13081 "%qE is not a variable in %qs clause", t,
13082 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13083 remove = true;
13084 }
0ae9bd27 13085 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
13086 {
13087 error_at (OMP_CLAUSE_LOCATION (c),
13088 "%qD is threadprivate variable in %qs clause", t,
13089 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13090 remove = true;
13091 }
e4606348
JJ
13092 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13093 || (OMP_CLAUSE_MAP_KIND (c)
13094 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13095 && !c_mark_addressable (t))
acf0174b 13096 remove = true;
b17a8b07 13097 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37 13098 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
d9a6bd32
JJ
13099 || (OMP_CLAUSE_MAP_KIND (c)
13100 == GOMP_MAP_FIRSTPRIVATE_POINTER)
41dbbb37
TS
13101 || (OMP_CLAUSE_MAP_KIND (c)
13102 == GOMP_MAP_FORCE_DEVICEPTR)))
d9a6bd32 13103 && t == OMP_CLAUSE_DECL (c)
b17a8b07 13104 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13105 {
13106 error_at (OMP_CLAUSE_LOCATION (c),
13107 "%qD does not have a mappable type in %qs clause", t,
13108 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13109 remove = true;
13110 }
d9a6bd32
JJ
13111 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13112 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13113 {
13114 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13115 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13116 {
13117 error ("%qD appears more than once in data clauses", t);
13118 remove = true;
13119 }
e01d41e5 13120 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
d9a6bd32 13121 {
e01d41e5
JJ
13122 error ("%qD appears both in data and map clauses", t);
13123 remove = true;
d9a6bd32 13124 }
e01d41e5
JJ
13125 else
13126 bitmap_set_bit (&generic_head, DECL_UID (t));
d9a6bd32
JJ
13127 }
13128 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
acf0174b
JJ
13129 {
13130 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13131 error ("%qD appears more than once in motion clauses", t);
13132 else
13133 error ("%qD appears more than once in map clauses", t);
13134 remove = true;
13135 }
e01d41e5
JJ
13136 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13137 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13138 {
13139 error ("%qD appears both in data and map clauses", t);
13140 remove = true;
13141 }
acf0174b 13142 else
d9a6bd32
JJ
13143 {
13144 bitmap_set_bit (&map_head, DECL_UID (t));
13145 if (t != OMP_CLAUSE_DECL (c)
13146 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13147 bitmap_set_bit (&map_field_head, DECL_UID (t));
13148 }
13149 break;
13150
13151 case OMP_CLAUSE_TO_DECLARE:
d9a6bd32
JJ
13152 case OMP_CLAUSE_LINK:
13153 t = OMP_CLAUSE_DECL (c);
e01d41e5
JJ
13154 if (TREE_CODE (t) == FUNCTION_DECL
13155 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13156 ;
13157 else if (!VAR_P (t))
d9a6bd32 13158 {
e01d41e5
JJ
13159 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13160 error_at (OMP_CLAUSE_LOCATION (c),
13161 "%qE is neither a variable nor a function name in "
13162 "clause %qs", t,
13163 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13164 else
13165 error_at (OMP_CLAUSE_LOCATION (c),
13166 "%qE is not a variable in clause %qs", t,
13167 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
d9a6bd32
JJ
13168 remove = true;
13169 }
13170 else if (DECL_THREAD_LOCAL_P (t))
13171 {
13172 error_at (OMP_CLAUSE_LOCATION (c),
13173 "%qD is threadprivate variable in %qs clause", t,
13174 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13175 remove = true;
13176 }
13177 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13178 {
13179 error_at (OMP_CLAUSE_LOCATION (c),
13180 "%qD does not have a mappable type in %qs clause", t,
13181 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13182 remove = true;
13183 }
e01d41e5
JJ
13184 if (remove)
13185 break;
13186 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13187 {
13188 error_at (OMP_CLAUSE_LOCATION (c),
13189 "%qE appears more than once on the same "
13190 "%<declare target%> directive", t);
13191 remove = true;
13192 }
13193 else
13194 bitmap_set_bit (&generic_head, DECL_UID (t));
acf0174b
JJ
13195 break;
13196
13197 case OMP_CLAUSE_UNIFORM:
13198 t = OMP_CLAUSE_DECL (c);
13199 if (TREE_CODE (t) != PARM_DECL)
13200 {
13201 if (DECL_P (t))
13202 error_at (OMP_CLAUSE_LOCATION (c),
13203 "%qD is not an argument in %<uniform%> clause", t);
13204 else
13205 error_at (OMP_CLAUSE_LOCATION (c),
13206 "%qE is not an argument in %<uniform%> clause", t);
13207 remove = true;
ee1d5a02 13208 break;
acf0174b 13209 }
e01d41e5
JJ
13210 /* map_head bitmap is used as uniform_head if declare_simd. */
13211 bitmap_set_bit (&map_head, DECL_UID (t));
ee1d5a02 13212 goto check_dup_generic;
acf0174b 13213
d9a6bd32
JJ
13214 case OMP_CLAUSE_IS_DEVICE_PTR:
13215 case OMP_CLAUSE_USE_DEVICE_PTR:
13216 t = OMP_CLAUSE_DECL (c);
13217 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13218 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13219 {
13220 error_at (OMP_CLAUSE_LOCATION (c),
13221 "%qs variable is neither a pointer nor an array",
13222 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13223 remove = true;
13224 }
13225 goto check_dup_generic;
13226
acf0174b
JJ
13227 case OMP_CLAUSE_NOWAIT:
13228 if (copyprivate_seen)
13229 {
13230 error_at (OMP_CLAUSE_LOCATION (c),
13231 "%<nowait%> clause must not be used together "
13232 "with %<copyprivate%>");
13233 remove = true;
13234 break;
13235 }
13236 nowait_clause = pc;
13237 pc = &OMP_CLAUSE_CHAIN (c);
13238 continue;
13239
953ff289
DN
13240 case OMP_CLAUSE_IF:
13241 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
13242 case OMP_CLAUSE_NUM_TEAMS:
13243 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 13244 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
13245 case OMP_CLAUSE_UNTIED:
13246 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
13247 case OMP_CLAUSE_FINAL:
13248 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
13249 case OMP_CLAUSE_DEVICE:
13250 case OMP_CLAUSE_DIST_SCHEDULE:
13251 case OMP_CLAUSE_PARALLEL:
13252 case OMP_CLAUSE_FOR:
13253 case OMP_CLAUSE_SECTIONS:
13254 case OMP_CLAUSE_TASKGROUP:
13255 case OMP_CLAUSE_PROC_BIND:
d9a6bd32
JJ
13256 case OMP_CLAUSE_PRIORITY:
13257 case OMP_CLAUSE_GRAINSIZE:
13258 case OMP_CLAUSE_NUM_TASKS:
13259 case OMP_CLAUSE_NOGROUP:
13260 case OMP_CLAUSE_THREADS:
13261 case OMP_CLAUSE_SIMD:
13262 case OMP_CLAUSE_HINT:
13263 case OMP_CLAUSE_DEFAULTMAP:
9a771876 13264 case OMP_CLAUSE__CILK_FOR_COUNT_:
41dbbb37
TS
13265 case OMP_CLAUSE_NUM_GANGS:
13266 case OMP_CLAUSE_NUM_WORKERS:
13267 case OMP_CLAUSE_VECTOR_LENGTH:
13268 case OMP_CLAUSE_ASYNC:
13269 case OMP_CLAUSE_WAIT:
13270 case OMP_CLAUSE_AUTO:
7a5e4956 13271 case OMP_CLAUSE_INDEPENDENT:
41dbbb37
TS
13272 case OMP_CLAUSE_SEQ:
13273 case OMP_CLAUSE_GANG:
13274 case OMP_CLAUSE_WORKER:
13275 case OMP_CLAUSE_VECTOR:
7a5e4956 13276 case OMP_CLAUSE_TILE:
acf0174b
JJ
13277 pc = &OMP_CLAUSE_CHAIN (c);
13278 continue;
13279
e01d41e5
JJ
13280 case OMP_CLAUSE_SCHEDULE:
13281 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13282 {
13283 const char *p = NULL;
13284 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13285 {
13286 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13287 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13288 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13289 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13290 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13291 default: gcc_unreachable ();
13292 }
13293 if (p)
13294 {
13295 error_at (OMP_CLAUSE_LOCATION (c),
13296 "%<nonmonotonic%> modifier specified for %qs "
13297 "schedule kind", p);
13298 OMP_CLAUSE_SCHEDULE_KIND (c)
13299 = (enum omp_clause_schedule_kind)
13300 (OMP_CLAUSE_SCHEDULE_KIND (c)
13301 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13302 }
13303 }
13304 schedule_clause = c;
13305 pc = &OMP_CLAUSE_CHAIN (c);
13306 continue;
13307
13308 case OMP_CLAUSE_ORDERED:
13309 ordered_seen = true;
13310 pc = &OMP_CLAUSE_CHAIN (c);
13311 continue;
13312
d9a6bd32
JJ
13313 case OMP_CLAUSE_SAFELEN:
13314 safelen = c;
13315 pc = &OMP_CLAUSE_CHAIN (c);
13316 continue;
13317 case OMP_CLAUSE_SIMDLEN:
13318 simdlen = c;
13319 pc = &OMP_CLAUSE_CHAIN (c);
13320 continue;
13321
acf0174b
JJ
13322 case OMP_CLAUSE_INBRANCH:
13323 case OMP_CLAUSE_NOTINBRANCH:
13324 if (branch_seen)
13325 {
13326 error_at (OMP_CLAUSE_LOCATION (c),
13327 "%<inbranch%> clause is incompatible with "
13328 "%<notinbranch%>");
13329 remove = true;
13330 break;
13331 }
13332 branch_seen = true;
953ff289
DN
13333 pc = &OMP_CLAUSE_CHAIN (c);
13334 continue;
13335
13336 default:
13337 gcc_unreachable ();
13338 }
13339
13340 if (!remove)
13341 {
13342 t = OMP_CLAUSE_DECL (c);
13343
13344 if (need_complete)
13345 {
13346 t = require_complete_type (t);
13347 if (t == error_mark_node)
13348 remove = true;
13349 }
13350
13351 if (need_implicitly_determined)
13352 {
13353 const char *share_name = NULL;
13354
0ae9bd27 13355 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
953ff289
DN
13356 share_name = "threadprivate";
13357 else switch (c_omp_predetermined_sharing (t))
13358 {
13359 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13360 break;
13361 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
13362 /* const vars may be specified in firstprivate clause. */
13363 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13364 && TREE_READONLY (t))
13365 break;
953ff289
DN
13366 share_name = "shared";
13367 break;
13368 case OMP_CLAUSE_DEFAULT_PRIVATE:
13369 share_name = "private";
13370 break;
13371 default:
13372 gcc_unreachable ();
13373 }
13374 if (share_name)
13375 {
c2255bc4
AH
13376 error_at (OMP_CLAUSE_LOCATION (c),
13377 "%qE is predetermined %qs for %qs",
acf0174b
JJ
13378 t, share_name,
13379 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13380 remove = true;
13381 }
13382 }
13383 }
13384
13385 if (remove)
13386 *pc = OMP_CLAUSE_CHAIN (c);
13387 else
13388 pc = &OMP_CLAUSE_CHAIN (c);
13389 }
13390
d9a6bd32
JJ
13391 if (simdlen
13392 && safelen
13393 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13394 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13395 {
13396 error_at (OMP_CLAUSE_LOCATION (simdlen),
13397 "%<simdlen%> clause value is bigger than "
13398 "%<safelen%> clause value");
13399 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13400 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13401 }
13402
e01d41e5
JJ
13403 if (ordered_seen
13404 && schedule_clause
13405 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13406 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13407 {
13408 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13409 "%<nonmonotonic%> schedule modifier specified together "
13410 "with %<ordered%> clause");
13411 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13412 = (enum omp_clause_schedule_kind)
13413 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13414 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13415 }
13416
13417 if (linear_variable_step_check)
13418 for (pc = &clauses, c = clauses; c ; c = *pc)
13419 {
13420 bool remove = false;
13421 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13422 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13423 && !bitmap_bit_p (&map_head,
13424 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13425 {
13426 error_at (OMP_CLAUSE_LOCATION (c),
13427 "%<linear%> clause step is a parameter %qD not "
13428 "specified in %<uniform%> clause",
13429 OMP_CLAUSE_LINEAR_STEP (c));
13430 remove = true;
13431 }
13432
13433 if (remove)
13434 *pc = OMP_CLAUSE_CHAIN (c);
13435 else
13436 pc = &OMP_CLAUSE_CHAIN (c);
13437 }
13438
953ff289
DN
13439 bitmap_obstack_release (NULL);
13440 return clauses;
13441}
9ae165a0 13442
0a35513e
AH
13443/* Create a transaction node. */
13444
13445tree
13446c_finish_transaction (location_t loc, tree block, int flags)
13447{
13448 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13449 if (flags & TM_STMT_ATTR_OUTER)
13450 TRANSACTION_EXPR_OUTER (stmt) = 1;
13451 if (flags & TM_STMT_ATTR_RELAXED)
13452 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13453 return add_stmt (stmt);
13454}
13455
9ae165a0 13456/* Make a variant type in the proper way for C/C++, propagating qualifiers
e9e32ee6
JM
13457 down to the element type of an array. If ORIG_QUAL_TYPE is not
13458 NULL, then it should be used as the qualified type
13459 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13460 preserve information about the typedef name from which an array
13461 type was derived). */
9ae165a0
DG
13462
13463tree
e9e32ee6
JM
13464c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13465 size_t orig_qual_indirect)
9ae165a0
DG
13466{
13467 if (type == error_mark_node)
13468 return type;
13469
13470 if (TREE_CODE (type) == ARRAY_TYPE)
13471 {
13472 tree t;
13473 tree element_type = c_build_qualified_type (TREE_TYPE (type),
e9e32ee6
JM
13474 type_quals, orig_qual_type,
13475 orig_qual_indirect - 1);
9ae165a0
DG
13476
13477 /* See if we already have an identically qualified type. */
e9e32ee6
JM
13478 if (orig_qual_type && orig_qual_indirect == 0)
13479 t = orig_qual_type;
13480 else
13481 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13482 {
13483 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13484 && TYPE_NAME (t) == TYPE_NAME (type)
13485 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13486 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13487 TYPE_ATTRIBUTES (type)))
13488 break;
13489 }
9ae165a0
DG
13490 if (!t)
13491 {
13492 tree domain = TYPE_DOMAIN (type);
13493
13494 t = build_variant_type_copy (type);
13495 TREE_TYPE (t) = element_type;
13496
13497 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13498 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13499 SET_TYPE_STRUCTURAL_EQUALITY (t);
13500 else if (TYPE_CANONICAL (element_type) != element_type
13501 || (domain && TYPE_CANONICAL (domain) != domain))
13502 {
b8698a0f 13503 tree unqualified_canon
9ae165a0 13504 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 13505 domain? TYPE_CANONICAL (domain)
9ae165a0 13506 : NULL_TREE);
ee45a32d
EB
13507 if (TYPE_REVERSE_STORAGE_ORDER (type))
13508 {
13509 unqualified_canon
13510 = build_distinct_type_copy (unqualified_canon);
13511 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13512 }
b8698a0f 13513 TYPE_CANONICAL (t)
9ae165a0
DG
13514 = c_build_qualified_type (unqualified_canon, type_quals);
13515 }
13516 else
13517 TYPE_CANONICAL (t) = t;
13518 }
13519 return t;
13520 }
13521
13522 /* A restrict-qualified pointer type must be a pointer to object or
13523 incomplete type. Note that the use of POINTER_TYPE_P also allows
13524 REFERENCE_TYPEs, which is appropriate for C++. */
13525 if ((type_quals & TYPE_QUAL_RESTRICT)
13526 && (!POINTER_TYPE_P (type)
13527 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13528 {
13529 error ("invalid use of %<restrict%>");
13530 type_quals &= ~TYPE_QUAL_RESTRICT;
13531 }
13532
e9e32ee6
JM
13533 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13534 ? orig_qual_type
13535 : build_qualified_type (type, type_quals));
e922069e
JW
13536 /* A variant type does not inherit the list of incomplete vars from the
13537 type main variant. */
3e636daf 13538 if (RECORD_OR_UNION_TYPE_P (var_type))
e922069e
JW
13539 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13540 return var_type;
9ae165a0 13541}
72b5577d
ILT
13542
13543/* Build a VA_ARG_EXPR for the C parser. */
13544
13545tree
f187980b 13546c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
72b5577d 13547{
ec3fba51
MP
13548 if (error_operand_p (type))
13549 return error_mark_node;
f187980b
EB
13550 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13551 order because it takes the address of the expression. */
13552 else if (handled_component_p (expr)
13553 && reverse_storage_order_for_component_p (expr))
13554 {
13555 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13556 return error_mark_node;
13557 }
ec3fba51 13558 else if (!COMPLETE_TYPE_P (type))
4e81b788 13559 {
f187980b 13560 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
ec3fba51 13561 "type %qT", type);
4e81b788
MP
13562 return error_mark_node;
13563 }
ec3fba51 13564 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
f187980b 13565 warning_at (loc2, OPT_Wc___compat,
ec3fba51 13566 "C++ requires promoted type, not enum type, in %<va_arg%>");
f187980b 13567 return build_va_arg (loc2, expr, type);
72b5577d 13568}
acf0174b
JJ
13569
13570/* Return truthvalue of whether T1 is the same tree structure as T2.
13571 Return 1 if they are the same. Return 0 if they are different. */
13572
13573bool
13574c_tree_equal (tree t1, tree t2)
13575{
13576 enum tree_code code1, code2;
13577
13578 if (t1 == t2)
13579 return true;
13580 if (!t1 || !t2)
13581 return false;
13582
13583 for (code1 = TREE_CODE (t1);
13584 CONVERT_EXPR_CODE_P (code1)
13585 || code1 == NON_LVALUE_EXPR;
13586 code1 = TREE_CODE (t1))
13587 t1 = TREE_OPERAND (t1, 0);
13588 for (code2 = TREE_CODE (t2);
13589 CONVERT_EXPR_CODE_P (code2)
13590 || code2 == NON_LVALUE_EXPR;
13591 code2 = TREE_CODE (t2))
13592 t2 = TREE_OPERAND (t2, 0);
13593
13594 /* They might have become equal now. */
13595 if (t1 == t2)
13596 return true;
13597
13598 if (code1 != code2)
13599 return false;
13600
13601 switch (code1)
13602 {
13603 case INTEGER_CST:
807e902e 13604 return wi::eq_p (t1, t2);
acf0174b
JJ
13605
13606 case REAL_CST:
624d31fe 13607 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
acf0174b
JJ
13608
13609 case STRING_CST:
13610 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13611 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13612 TREE_STRING_LENGTH (t1));
13613
13614 case FIXED_CST:
13615 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13616 TREE_FIXED_CST (t2));
13617
13618 case COMPLEX_CST:
13619 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13620 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13621
13622 case VECTOR_CST:
13623 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13624
13625 case CONSTRUCTOR:
13626 /* We need to do this when determining whether or not two
13627 non-type pointer to member function template arguments
13628 are the same. */
13629 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13630 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13631 return false;
13632 {
13633 tree field, value;
13634 unsigned int i;
13635 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13636 {
13637 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13638 if (!c_tree_equal (field, elt2->index)
13639 || !c_tree_equal (value, elt2->value))
13640 return false;
13641 }
13642 }
13643 return true;
13644
13645 case TREE_LIST:
13646 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13647 return false;
13648 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
13649 return false;
13650 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
13651
13652 case SAVE_EXPR:
13653 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13654
13655 case CALL_EXPR:
13656 {
13657 tree arg1, arg2;
13658 call_expr_arg_iterator iter1, iter2;
13659 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
13660 return false;
13661 for (arg1 = first_call_expr_arg (t1, &iter1),
13662 arg2 = first_call_expr_arg (t2, &iter2);
13663 arg1 && arg2;
13664 arg1 = next_call_expr_arg (&iter1),
13665 arg2 = next_call_expr_arg (&iter2))
13666 if (!c_tree_equal (arg1, arg2))
13667 return false;
13668 if (arg1 || arg2)
13669 return false;
13670 return true;
13671 }
13672
13673 case TARGET_EXPR:
13674 {
13675 tree o1 = TREE_OPERAND (t1, 0);
13676 tree o2 = TREE_OPERAND (t2, 0);
13677
13678 /* Special case: if either target is an unallocated VAR_DECL,
13679 it means that it's going to be unified with whatever the
13680 TARGET_EXPR is really supposed to initialize, so treat it
13681 as being equivalent to anything. */
0ae9bd27 13682 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
acf0174b
JJ
13683 && !DECL_RTL_SET_P (o1))
13684 /*Nop*/;
0ae9bd27 13685 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
acf0174b
JJ
13686 && !DECL_RTL_SET_P (o2))
13687 /*Nop*/;
13688 else if (!c_tree_equal (o1, o2))
13689 return false;
13690
13691 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
13692 }
13693
13694 case COMPONENT_REF:
13695 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
13696 return false;
13697 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13698
13699 case PARM_DECL:
13700 case VAR_DECL:
13701 case CONST_DECL:
13702 case FIELD_DECL:
13703 case FUNCTION_DECL:
13704 case IDENTIFIER_NODE:
13705 case SSA_NAME:
13706 return false;
13707
13708 case TREE_VEC:
13709 {
13710 unsigned ix;
13711 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
13712 return false;
13713 for (ix = TREE_VEC_LENGTH (t1); ix--;)
13714 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
13715 TREE_VEC_ELT (t2, ix)))
13716 return false;
13717 return true;
13718 }
13719
13720 default:
13721 break;
13722 }
13723
13724 switch (TREE_CODE_CLASS (code1))
13725 {
13726 case tcc_unary:
13727 case tcc_binary:
13728 case tcc_comparison:
13729 case tcc_expression:
13730 case tcc_vl_exp:
13731 case tcc_reference:
13732 case tcc_statement:
13733 {
13734 int i, n = TREE_OPERAND_LENGTH (t1);
13735
13736 switch (code1)
13737 {
13738 case PREINCREMENT_EXPR:
13739 case PREDECREMENT_EXPR:
13740 case POSTINCREMENT_EXPR:
13741 case POSTDECREMENT_EXPR:
13742 n = 1;
13743 break;
13744 case ARRAY_REF:
13745 n = 2;
13746 break;
13747 default:
13748 break;
13749 }
13750
13751 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
13752 && n != TREE_OPERAND_LENGTH (t2))
13753 return false;
13754
13755 for (i = 0; i < n; ++i)
13756 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
13757 return false;
13758
13759 return true;
13760 }
13761
13762 case tcc_type:
13763 return comptypes (t1, t2);
13764 default:
13765 gcc_unreachable ();
13766 }
13767 /* We can get here with --disable-checking. */
13768 return false;
13769}
12893402
BI
13770
13771/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
13772 spawn-helper and BODY is the newly created body for FNDECL. */
13773
13774void
13775cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
13776{
13777 tree list = alloc_stmt_list ();
13778 tree frame = make_cilk_frame (fndecl);
13779 tree dtor = create_cilk_function_exit (frame, false, true);
13780 add_local_decl (cfun, frame);
13781
13782 DECL_SAVED_TREE (fndecl) = list;
13783 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
13784 frame);
13785 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
13786 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
13787
13788 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
13789 append_to_statement_list (detach_expr, &body_list);
13790
13791 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
13792 body = fold_build_cleanup_point_expr (void_type_node, body);
13793
13794 append_to_statement_list (body, &body_list);
13795 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
13796 body_list, dtor), &list);
13797}
1807ffc1
MS
13798
13799/* Returns true when the function declaration FNDECL is implicit,
13800 introduced as a result of a call to an otherwise undeclared
13801 function, and false otherwise. */
13802
13803bool
13804c_decl_implicit (const_tree fndecl)
13805{
13806 return C_DECL_IMPLICIT (fndecl);
13807}